gtkprintbackendfile.c
来自「This GTK+ version 2.12.3. GTK+ is a mult」· C语言 代码 · 共 700 行 · 第 1/2 页
C
700 行
static gbooleanfile_write (GIOChannel *source, GIOCondition con, gpointer user_data){ gchar buf[_STREAM_MAX_CHUNK_SIZE]; gsize bytes_read; GError *error; GIOStatus read_status; _PrintStreamData *ps = (_PrintStreamData *) user_data; error = NULL; read_status = g_io_channel_read_chars (source, buf, _STREAM_MAX_CHUNK_SIZE, &bytes_read, &error); if (read_status != G_IO_STATUS_ERROR) { gsize bytes_written; g_io_channel_write_chars (ps->target_io, buf, bytes_read, &bytes_written, &error); } if (error != NULL || read_status == G_IO_STATUS_EOF) { file_print_cb (GTK_PRINT_BACKEND_FILE (ps->backend), error, user_data); if (error != NULL) { GTK_NOTE (PRINTING, g_print ("FILE Backend: %s\n", error->message)); g_error_free (error); } return FALSE; } GTK_NOTE (PRINTING, g_print ("FILE Backend: Writting %i byte chunk to target file\n", bytes_read)); return TRUE;}static voidgtk_print_backend_file_print_stream (GtkPrintBackend *print_backend, GtkPrintJob *job, GIOChannel *data_io, GtkPrintJobCompleteFunc callback, gpointer user_data, GDestroyNotify dnotify){ GError *internal_error = NULL; GtkPrinter *printer; _PrintStreamData *ps; GtkPrintSettings *settings; gchar *uri, *filename; printer = gtk_print_job_get_printer (job); settings = gtk_print_job_get_settings (job); ps = g_new0 (_PrintStreamData, 1); ps->callback = callback; ps->user_data = user_data; ps->dnotify = dnotify; ps->job = g_object_ref (job); ps->backend = print_backend; internal_error = NULL; uri = output_file_from_settings (settings, NULL); filename = g_filename_from_uri (uri, NULL, &internal_error); g_free (uri); if (filename == NULL) goto error; ps->target_io = g_io_channel_new_file (filename, "w", &internal_error); g_free (filename); if (internal_error == NULL) g_io_channel_set_encoding (ps->target_io, NULL, &internal_error);error: if (internal_error != NULL) { file_print_cb (GTK_PRINT_BACKEND_FILE (print_backend), internal_error, ps); g_error_free (internal_error); return; } g_io_add_watch (data_io, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, (GIOFunc) file_write, ps);}static voidgtk_print_backend_file_init (GtkPrintBackendFile *backend){ GtkPrinter *printer; printer = g_object_new (GTK_TYPE_PRINTER, "name", _("Print to File"), "backend", backend, "is-virtual", TRUE, NULL); gtk_printer_set_has_details (printer, TRUE); gtk_printer_set_icon_name (printer, "gtk-floppy"); gtk_printer_set_is_active (printer, TRUE); gtk_print_backend_add_printer (GTK_PRINT_BACKEND (backend), printer); g_object_unref (printer); gtk_print_backend_set_list_done (GTK_PRINT_BACKEND (backend));}static voidfile_printer_output_file_format_changed (GtkPrinterOption *format_option, GtkPrinterOptionSet *set){ GtkPrinterOption *uri_option; gchar *base = NULL; if (! format_option->value) return; uri_option = gtk_printer_option_set_lookup (set, "gtk-main-page-custom-input"); if (uri_option && uri_option->value) { const gchar *uri = uri_option->value; const gchar *dot = strrchr (uri, '.'); if (dot) { gint i; /* check if the file extension matches one of the known ones */ for (i = 0; i < N_FORMATS; i++) if (strcmp (dot + 1, formats[i]) == 0) break; if (i < N_FORMATS && strcmp (formats[i], format_option->value)) { /* the file extension is known but doesn't match the * selected one, strip it away */ base = g_strndup (uri, dot - uri); } } else { /* there's no file extension */ base = g_strdup (uri); } } if (base) { gchar *tmp = g_strdup_printf ("%s.%s", base, format_option->value); gtk_printer_option_set (uri_option, tmp); g_free (tmp); g_free (base); }}static GtkPrinterOptionSet *file_printer_get_options (GtkPrinter *printer, GtkPrintSettings *settings, GtkPageSetup *page_setup, GtkPrintCapabilities capabilities){ GtkPrinterOptionSet *set; GtkPrinterOption *option; const gchar *n_up[] = {"1", "2", "4", "6", "9", "16" }; const gchar *pages_per_sheet = NULL; const gchar *format_names[N_FORMATS] = { N_("PDF"), N_("Postscript") }; const gchar *supported_formats[N_FORMATS]; gchar *display_format_names[N_FORMATS]; gint n_formats = 0; OutputFormat format; gchar *uri; gint current_format = 0; format = format_from_settings (settings); set = gtk_printer_option_set_new (); option = gtk_printer_option_new ("gtk-n-up", _("Pages per _sheet:"), GTK_PRINTER_OPTION_TYPE_PICKONE); gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up), (char **) n_up, (char **) n_up /* FIXME i18n (localised digits)! */); if (settings) pages_per_sheet = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_NUMBER_UP); if (pages_per_sheet) gtk_printer_option_set (option, pages_per_sheet); else gtk_printer_option_set (option, "1"); gtk_printer_option_set_add (set, option); g_object_unref (option); if (capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF | GTK_PRINT_CAPABILITY_GENERATE_PS)) { if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PDF) { if (format == FORMAT_PDF || format == N_FORMATS) { format = FORMAT_PDF; current_format = n_formats; } supported_formats[n_formats] = formats[FORMAT_PDF]; display_format_names[n_formats] = _(format_names[FORMAT_PDF]); n_formats++; } if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PS) { if (format == FORMAT_PS || format == N_FORMATS) current_format = n_formats; supported_formats[n_formats] = formats[FORMAT_PS]; display_format_names[n_formats] = _(format_names[FORMAT_PS]); n_formats++; } } else { current_format = format == FORMAT_PS ? FORMAT_PS : FORMAT_PDF; for (n_formats = 0; n_formats < N_FORMATS; ++n_formats) { supported_formats[n_formats] = formats[n_formats]; display_format_names[n_formats] = _(format_names[n_formats]); } } uri = output_file_from_settings (settings, supported_formats[current_format]); option = gtk_printer_option_new ("gtk-main-page-custom-input", _("File"), GTK_PRINTER_OPTION_TYPE_FILESAVE); gtk_printer_option_set (option, uri); g_free (uri); option->group = g_strdup ("GtkPrintDialogExtension"); gtk_printer_option_set_add (set, option); if (n_formats > 1) { option = gtk_printer_option_new ("output-file-format", _("_Output format"), GTK_PRINTER_OPTION_TYPE_ALTERNATIVE); option->group = g_strdup ("GtkPrintDialogExtension"); gtk_printer_option_choices_from_array (option, n_formats, (char **) supported_formats, display_format_names); gtk_printer_option_set (option, supported_formats[current_format]); gtk_printer_option_set_add (set, option); g_signal_connect (option, "changed", G_CALLBACK (file_printer_output_file_format_changed), set); g_object_unref (option); } return set;}static voidfile_printer_get_settings_from_options (GtkPrinter *printer, GtkPrinterOptionSet *options, GtkPrintSettings *settings){ GtkPrinterOption *option; option = gtk_printer_option_set_lookup (options, "gtk-main-page-custom-input"); gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_URI, option->value); option = gtk_printer_option_set_lookup (options, "output-file-format"); if (option) gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, option->value); option = gtk_printer_option_set_lookup (options, "gtk-n-up"); if (option) gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_NUMBER_UP, option->value);}static voidfile_printer_prepare_for_print (GtkPrinter *printer, GtkPrintJob *print_job, GtkPrintSettings *settings, GtkPageSetup *page_setup){ gdouble scale; print_job->print_pages = gtk_print_settings_get_print_pages (settings); print_job->page_ranges = NULL; print_job->num_page_ranges = 0; if (print_job->print_pages == GTK_PRINT_PAGES_RANGES) print_job->page_ranges = gtk_print_settings_get_page_ranges (settings, &print_job->num_page_ranges); print_job->collate = gtk_print_settings_get_collate (settings); print_job->reverse = gtk_print_settings_get_reverse (settings); print_job->num_copies = gtk_print_settings_get_n_copies (settings); scale = gtk_print_settings_get_scale (settings); if (scale != 100.0) print_job->scale = scale/100.0; print_job->page_set = gtk_print_settings_get_page_set (settings); print_job->rotate_to_orientation = TRUE;}static GList *file_printer_list_papers (GtkPrinter *printer){ GList *result = NULL; GList *papers, *p; GtkPageSetup *page_setup; papers = gtk_paper_size_get_paper_sizes (TRUE); for (p = papers; p; p = p->next) { GtkPaperSize *paper_size = p->data; page_setup = gtk_page_setup_new (); gtk_page_setup_set_paper_size (page_setup, paper_size); gtk_paper_size_free (paper_size); result = g_list_prepend (result, page_setup); } g_list_free (papers); return g_list_reverse (result);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?