⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gvxprn.c

📁 GSview 4.6 PostScript previewer。Ghostscript在MS-Windows, OS/2 and Unix下的图形化接口
💻 C
📖 第 1 页 / 共 5 页
字号:
	gtk_container_add(GTK_CONTAINER(scrolled_window), clist);
	gtk_widget_show(clist);
    }

    /* Create and place the buttons in a vbox at the right */
    vbox = gtk_vbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 5);
    gtk_widget_show(vbox);

    button_ok = gtk_button_new_with_label(get_string(IDS_AAOK));
    button_cancel = gtk_button_new_with_label(get_string(IDS_AACANCEL));
    button_prop = gtk_button_new_with_label(get_string(IDS_AAPROPERTIES));
    button_help = gtk_button_new_with_label(get_string(IDS_AAHELP));

    gtk_box_pack_start(GTK_BOX(vbox), button_ok, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(vbox), button_cancel, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(vbox), button_prop, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(vbox), button_help, FALSE, FALSE, 5);

    /* Connect our callbacks to the buttons */
    gtk_signal_connect(GTK_OBJECT(button_ok), "clicked",
                              GTK_SIGNAL_FUNC(modal_ok), &rc);
    gtk_signal_connect(GTK_OBJECT(button_cancel), "clicked",
                              GTK_SIGNAL_FUNC(modal_cancel), &rc);
    gtk_signal_connect(GTK_OBJECT(button_help), "clicked",
                              GTK_SIGNAL_FUNC(modal_help), &rc);
    gtk_signal_connect(GTK_OBJECT(button_prop), "clicked",
                              GTK_SIGNAL_FUNC(prop_dialog), &dr);

    gtk_widget_show(button_ok);
    gtk_widget_show(button_cancel);
    gtk_widget_show(button_prop);
    gtk_widget_show(button_help);

    if (psfile.dsc != (CDSC *)NULL) {
	button_all = gtk_button_new_with_label(get_string(IDS_AAALLPAGES));
	button_odd = gtk_button_new_with_label(get_string(IDS_AAODDPAGES));
	button_even = gtk_button_new_with_label(get_string(IDS_AAEVENPAGES));
	gtk_box_pack_start(GTK_BOX(vbox), button_all, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(vbox), button_odd, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(vbox), button_even, FALSE, FALSE, 5);
	gtk_signal_connect(GTK_OBJECT(button_all), "clicked",
				  GTK_SIGNAL_FUNC(selpage_all),
				  (gpointer) clist);
	gtk_signal_connect(GTK_OBJECT(button_odd), "clicked",
				  GTK_SIGNAL_FUNC(selpage_odd),
				  (gpointer) clist);
	gtk_signal_connect(GTK_OBJECT(button_even), "clicked",
				  GTK_SIGNAL_FUNC(selpage_even),
				  (gpointer) clist);
	gtk_widget_show(button_all);
	gtk_widget_show(button_odd);
	gtk_widget_show(button_even);
    }

    if (psfile.dsc != (CDSC *)NULL) {
	char buf[64];
	char *p = buf;
	for (i=0; i< (int)(psfile.dsc->page_count); i++) {
	    page_ordlabel(buf, i);
	    gtk_clist_append( (GtkCList *) clist, &p);
	    if (psfile.page_list.select[i]
		|| (i == psfile.page_list.current))
		gtk_clist_select_row(GTK_CLIST(clist), i, 0);
	}
	gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_EXTENDED);
	selpage_all(GTK_BUTTON(button_all), (gpointer)clist);
    }

    /* show dialog and wait for OK, Cancel or close */
    gtk_window_set_modal(GTK_WINDOW(window), TRUE);
    gtk_widget_show(window);
    gtk_main();

    if (rc == IDOK) {
	char buf[MAXSTR];
	if (psfile.dsc != (CDSC *)NULL) {
	    GList *l = GTK_CLIST(clist)->selection;
	    for (i = 0; i < (int)psfile.dsc->page_count; i++)
		psfile.page_list.select[i] = FALSE;
	    while (l) {
		psfile.page_list.current = (int)(l->data);
		psfile.page_list.select[(int)(l->data)] = TRUE;
		l = g_list_next(l);
	    }
	    psfile.page_list.reverse = option.print_reverse = 
		gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button_reverse));
	}
	if (convert) {
	    strncpy(option.convert_device, 
		gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(device)->entry)),
		sizeof(option.convert_device)-1);
	    strncpy(option.convert_resolution, 
		gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(resolution)->entry)),
		sizeof(option.convert_resolution)-1);
	    strncpy(buf, gtk_entry_get_text( GTK_ENTRY(
		GTK_COMBO(combo_fixed_media)->entry)), sizeof(buf)-1);
	    for (i=0; i<3; i++) {
		if (strcmp(buf, get_string(IDS_PAGESIZE_VARIABLE+i)) == 0)
		    option.convert_fixed_media = i;
	    }
	}
	else {
	    strncpy(option.printer_device, 
		gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(device)->entry)),
		sizeof(option.printer_device)-1);
	    strncpy(option.printer_resolution, 
		gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(resolution)->entry)),
		sizeof(option.printer_resolution)-1);
	    strncpy(buf, gtk_entry_get_text( GTK_ENTRY(
		GTK_COMBO(combo_fixed_media)->entry)), sizeof(buf)-1);
	    for (i=0; i<3; i++) {
		if (strcmp(buf, get_string(IDS_PAGESIZE_VARIABLE+i)) == 0)
		    option.print_fixed_media = i;
	    }
	    option.print_to_file = gtk_toggle_button_get_active(
		GTK_TOGGLE_BUTTON(button_printfile));
	    if (gtk_toggle_button_get_active(
		GTK_TOGGLE_BUTTON(button_psprint)))
	        option.print_method = PRINT_PS;
	    else
	        option.print_method = PRINT_GS;
	    strncpy(option.printer_queue, 
		gtk_entry_get_text(GTK_ENTRY(printer)),
		sizeof(option.printer_queue)-1);
	}
	if ( (prf = profile_open(szIniFile)) != (PROFILE *)NULL ) {
	    char section[MAXSTR];
	    strcpy(section, convert ? option.convert_device :
		option.printer_device);
	    strcat(section, " Options");
	    profile_write_string(prf, section, "Options", 
	    	gtk_entry_get_text(GTK_ENTRY(options)));
	    profile_close(prf);
	}
    }

    gtk_signal_disconnect(GTK_OBJECT(GTK_COMBO(device)->list), res_signal);
    gtk_signal_disconnect(GTK_OBJECT(GTK_COMBO(device)->list), options_signal);

    gtk_widget_destroy(window);

    return (rc == IDOK);
}

GtkWidget *print_window;
GtkWidget *print_text;
/* should have a label which show % printed */

gint delete_print_message(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    if (debug & DEBUG_GENERAL)
	gs_addmess("delete_print_message:\n");
    print_window = NULL;
    print_text = NULL;
    /* emit the "destroy" signal */
    return FALSE;
}

void percent_print_message(int percent)
{
    if (print_window != NULL) {
	char buf[MAXSTR];
	sprintf(buf, "%d%% - %s", percent, get_string(IDS_AAGSVIEWPRINT));
	gtk_window_set_title(GTK_WINDOW(print_window), buf);
    }
}

void add_print_message(char *str, int len) 
{
   if (debug & DEBUG_GENERAL)
	write(fileno(stdout), str, len);
   if (print_text != NULL) 
       gtk_text_insert(GTK_TEXT(print_text), NULL, NULL, NULL, str, len); 
}

void close_print_message(void)
{
    if (print_window != NULL) {
        gtk_widget_hide(print_window);
        gtk_widget_destroy(print_window);
    }
    print_window = NULL;
    print_text = NULL;
}

void show_print_message(void)
{
    GtkWidget *table;
    GtkWidget *vscrollbar;

    close_print_message();	/* close any existing window */

    print_window=gtk_window_new(GTK_WINDOW_DIALOG);
    gtk_window_set_title(GTK_WINDOW(print_window), get_string(IDS_AAGSVIEWPRINT));
    gtk_widget_set_usize(GTK_WIDGET(print_window), 450, 300);

    gtk_signal_connect(GTK_OBJECT(print_window), "delete-event",
                       GTK_SIGNAL_FUNC(delete_print_message), NULL);
    
    table = gtk_table_new(2, 2, FALSE);
    gtk_table_set_row_spacing(GTK_TABLE(table), 0, 2);
    gtk_table_set_col_spacing(GTK_TABLE(table), 0, 2);
    gtk_container_add(GTK_CONTAINER(print_window), table);
    gtk_widget_show(table);
   
    /* Create text */
    print_text = gtk_text_new(NULL, NULL);
    gtk_table_attach(GTK_TABLE(table), print_text, 0, 1, 0, 1,
	(GtkAttachOptions)(GTK_EXPAND | GTK_SHRINK | GTK_FILL),
	(GtkAttachOptions)(GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
    gtk_widget_show(print_text);

    /* Add a vertical scroll bar to the GtkText widget */
    vscrollbar = gtk_vscrollbar_new(GTK_TEXT(print_text)->vadj);
    gtk_table_attach(GTK_TABLE(table), vscrollbar, 1, 2, 0, 1,
	(GtkAttachOptions)(GTK_FILL),
	(GtkAttachOptions)(GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
    gtk_widget_show(vscrollbar);

    gtk_widget_show(print_window);
    /* modeless dialog */
}


int gs_print_pipe_stdin[2] = {-1, -1};
int gs_print_pipe_stdout[2] = {-1, -1};
int gs_print_pipe_stderr[2] = {-1, -1};
gint gs_print_pipe_stdin_tag = -1;
gint gs_print_pipe_stdout_tag = -1;
gint gs_print_pipe_stderr_tag = -1;
int gs_print_file;
char *gs_print_buffer;
int gs_print_buffer_index;	/* offset to next byte to send */
int gs_print_buffer_count;	/* remaining bytes to send */
int gs_print_buffer_length;	/* length of buffer */

long gs_print_bytes_done;
long gs_print_bytes_size;
int gs_print_percent;

void print_start_gs(void);
int print_stop_gs(void);
void print_check_zombie(void);
void print_stop_stdin(void);
void print_start_stdin(void);
void print_stop_stdout(void);
void print_start_stdout(void);
void print_stop_stderr(void);
void print_start_stderr(void);
void print_write_fn(gpointer data, gint fd, GdkInputCondition condition);
void print_read_stdout_fn(gpointer data, gint fd, GdkInputCondition condition);

int print_stop_gs(void)
{
    BOOL keep_message = FALSE;
    if (debug & DEBUG_GENERAL)
	gs_addmess("print_stop_gs\n");
    print_stop_stdin();
    if (gs_print_pipe_stdin[1] != -1) {
	close(gs_print_pipe_stdin[1]);
	gs_print_pipe_stdin[1] = -1;
    }
    print_stop_stdout();
    if (gs_print_pipe_stdout[0] != -1) {
	close(gs_print_pipe_stdout[0]);
	gs_print_pipe_stdout[0] = -1;
    }
    print_stop_stderr();
    if (gs_print_pipe_stderr[0] != -1) {
	close(gs_print_pipe_stderr[0]);
	gs_print_pipe_stderr[0] = -1;
    }

    if (printer.pid > 0) {
	int rc;
	int status = 0;
	/* get termination code */
	usleep(100000);	/* allow a little time for child to finish */
	rc = waitpid(printer.pid, &status, WNOHANG);
	if (debug & DEBUG_GENERAL)
	    gs_addmessf("print_stop_gs: waitpid rc=%d status=%d errno=%d\n", rc, status, errno);
	if (rc > 0) {
	    /* child process has ended */
	    if (WIFEXITED(status)) {
		if (debug & DEBUG_GENERAL)
		    gs_addmessf("print_stop_gs: Ghostscript (zombie) exit code %d\n", 
		    WEXITSTATUS(status));
		if (WEXITSTATUS(status) != 0) {
		    gs_showmess();	/* show error message */
		    keep_message = TRUE;
	 	}
/* TESTING TESTING TESTING */
/* should show printer window */
	    }
	    else {
		if (debug & DEBUG_GENERAL)
		    gs_addmessf("print_stop_gs: child ended, but rc=%d, status=%d\n", 
			rc, status); 
	    }
	}
	else if (rc == 0) {
	    /* child has not yet ended */
	    if (debug & DEBUG_GENERAL)
		gs_addmess("print_stop_gs: killing Ghostscript\n");
	    kill(printer.pid, SIGTERM);
	    waitpid(printer.pid, &status, 0);
	}
	else {
	    /* error - no such process */
	    if (debug & DEBUG_GENERAL)
		gs_addmessf("print_stop_gs: waitpid rc=%d errno=%d\n", rc, errno);
	}

	printer.pid = 0;
    }

    if (gs_print_file > 0) {
	close(gs_print_file);
	gs_print_file = 0;
    }
    if (printer.psname[0]) {
	if (!(debug & DEBUG_GENERAL))
	    unlink(printer.psname);
	printer.psname[0] = '\0';
    }
    if (printer.optname[0]) {
	if (!(debug & DEBUG_GENERAL))
	    unlink(printer.optname);
	printer.optname[0] = '\0';
    }
    if (gs_print_buffer) {
	free(gs_print_buffer);
	gs_print_buffer = NULL;
    }
    gs_print_buffer_length = 0;
    gs_print_buffer_count = 0;
    gs_print_buffer_index = 0;

    if (!keep_message && !(debug & DEBUG_GENERAL))
	close_print_message();

    return 0;
}


/* see if print has finished */
/* May need to call this before gsview_command() to cleanup
 * before next print 
 */
void print_check_zombie(void)
{
    /* if process is a zombie, close and delete temporary files */
    if (printer.pid > 0) {
	/* check if Ghostscript has exited prematurely */
	int status = 0;
	if (waitpid(printer.pid, &status, WNOHANG) > 0) {
	    if (debug & DEBUG_GENERAL)
		gs_addmessf("print_check_zombie: calling print_stop_gs()\n");
	    /* Printing Ghostscript has exited, release resources */
	    printer.pid = -1;
	    print_stop_gs();
	}
    }
}

/* Asynchronous write to GS print stdin.
 * This is called from event loop when a write is possible 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -