Minor improvements related to the search entry
[geany-mirror.git] / src / printing.c
blob6529e6825c800fce0a358ac725f13b5ab623e4c5
1 /*
2 * printing.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * GTK printing support
24 * (basic code layout were adopted from Sylpheed's printing implementation, thanks)
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include "printing.h"
33 #include "app.h"
34 #include "dialogs.h"
35 #include "document.h"
36 #include "geany.h"
37 #include "highlighting.h"
38 #include "msgwindow.h"
39 #include "sciwrappers.h"
40 #include "spawn.h"
41 #include "support.h"
42 #include "utils.h"
43 #include "ui_utils.h"
45 #include <math.h>
46 #include <time.h>
47 #include <string.h>
50 PrintingPrefs printing_prefs;
53 /* document-related variables */
54 typedef struct
56 GeanyDocument *doc;
57 ScintillaObject *sci;
58 gdouble margin_width;
59 gdouble line_height;
60 /* set in begin_print() to hold the time when printing was started to ensure all printed
61 * pages have the same date and time (in case of slow machines and many pages where rendering
62 * takes more than a second) */
63 time_t print_time;
64 PangoLayout *layout; /* commonly used layout object */
65 gdouble sci_scale;
67 struct Sci_RangeToFormat fr;
68 GArray *pages;
69 } DocInfo;
71 /* widget references for the custom widget in the print dialog */
72 typedef struct
74 GtkWidget *check_print_linenumbers;
75 GtkWidget *check_print_pagenumbers;
76 GtkWidget *check_print_pageheader;
77 GtkWidget *check_print_basename;
78 GtkWidget *entry_print_dateformat;
79 } PrintWidgets;
82 static GtkPrintSettings *settings = NULL;
83 static GtkPageSetup *page_setup = NULL;
87 /* creates a commonly used layout object from the given context for use in get_page_count and
88 * draw_page */
89 static PangoLayout *setup_pango_layout(GtkPrintContext *context, PangoFontDescription *desc)
91 PangoLayout *layout;
93 layout = gtk_print_context_create_pango_layout(context);
94 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
95 pango_layout_set_spacing(layout, 0);
96 pango_layout_set_attributes(layout, NULL);
97 pango_layout_set_font_description(layout, desc);
99 return layout;
103 static void get_text_dimensions(PangoLayout *layout, const gchar *text, gdouble *width, gdouble *height)
105 gint layout_w, layout_h;
107 pango_layout_set_text(layout, text, -1);
108 pango_layout_get_size(layout, &layout_w, &layout_h);
109 if (layout_w <= 0)
111 gint default_w = 50 * strlen(text) * PANGO_SCALE;
113 geany_debug("Invalid layout_w (%d). Falling back to default width (%d)",
114 layout_w, default_w);
115 layout_w = default_w;
117 if (layout_h <= 0)
119 gint default_h = 100 * PANGO_SCALE;
121 geany_debug("Invalid layout_h (%d). Falling back to default height (%d)",
122 layout_h, default_h);
123 layout_h = default_h;
126 if (width)
127 *width = (gdouble)layout_w / PANGO_SCALE;
128 if (height)
129 *height = (gdouble)layout_h / PANGO_SCALE;
133 static void add_page_header(DocInfo *dinfo, cairo_t *cr, gint width, gint page_nr)
135 gint ph_height = dinfo->line_height * 3;
136 gchar *data;
137 gchar *datetime;
138 const gchar *tmp_file_name = DOC_FILENAME(dinfo->doc);
139 gchar *file_name = (printing_prefs.page_header_basename) ?
140 g_path_get_basename(tmp_file_name) : g_strdup(tmp_file_name);
141 PangoLayout *layout = dinfo->layout;
143 /* draw the frame */
144 cairo_set_line_width(cr, 0.3);
145 cairo_set_source_rgb(cr, 0, 0, 0);
146 cairo_rectangle(cr, 2, 2, width - 4, ph_height - 4);
147 cairo_stroke(cr);
149 /* width - 8: 2px between doc border and frame border, 2px between frame border and text
150 * and this on left and right side, so (2 + 2) * 2 */
151 pango_layout_set_width(layout, (width - 8) * PANGO_SCALE);
152 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_MIDDLE);
154 data = g_strdup_printf("<b>%s</b>", file_name);
155 pango_layout_set_markup(layout, data, -1);
156 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
157 cairo_move_to(cr, 4, dinfo->line_height * 0.5);
158 pango_cairo_show_layout(cr, layout);
159 g_free(data);
160 g_free(file_name);
162 data = g_strdup_printf(_("<b>Page %d of %d</b>"), page_nr + 1, dinfo->pages->len);
163 pango_layout_set_markup(layout, data, -1);
164 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
165 cairo_move_to(cr, 4, dinfo->line_height * 1.5);
166 pango_cairo_show_layout(cr, layout);
167 g_free(data);
169 datetime = utils_get_date_time(printing_prefs.page_header_datefmt, &(dinfo->print_time));
170 if (G_LIKELY(!EMPTY(datetime)))
172 data = g_strdup_printf("<b>%s</b>", datetime);
173 pango_layout_set_markup(layout, data, -1);
174 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
175 cairo_move_to(cr, 2, dinfo->line_height * 1.5);
176 pango_cairo_show_layout(cr, layout);
177 g_free(data);
179 g_free(datetime);
181 /* reset layout and re-position cairo context */
182 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
183 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
184 pango_layout_set_justify(layout, FALSE);
185 pango_layout_set_width(layout, width * PANGO_SCALE);
186 cairo_move_to(cr, 0, dinfo->line_height * 3);
190 static void custom_widget_apply(GtkPrintOperation *operation, GtkWidget *widget, gpointer user_data)
192 PrintWidgets *w = user_data;
194 printing_prefs.print_line_numbers =
195 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_linenumbers));
197 printing_prefs.print_page_numbers =
198 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_pagenumbers));
200 printing_prefs.print_page_header =
201 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_pageheader));
203 printing_prefs.page_header_basename =
204 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_basename));
206 g_free(printing_prefs.page_header_datefmt);
207 printing_prefs.page_header_datefmt =
208 g_strdup(gtk_entry_get_text(GTK_ENTRY(w->entry_print_dateformat)));
212 static void on_page_header_toggled(GtkToggleButton *togglebutton, gpointer user_data)
214 gboolean sens = gtk_toggle_button_get_active(togglebutton);
215 PrintWidgets *w = user_data;
217 gtk_widget_set_sensitive(w->check_print_basename, sens);
218 gtk_widget_set_sensitive(w->entry_print_dateformat, sens);
222 static GtkWidget *create_custom_widget(GtkPrintOperation *operation, gpointer user_data)
223 { /* copied from interface.c */
224 GtkWidget *page;
225 GtkWidget *frame33;
226 GtkWidget *alignment36;
227 GtkWidget *vbox30;
228 GtkWidget *hbox10;
229 GtkWidget *label203;
230 PrintWidgets *w = user_data;
232 gtk_print_operation_set_custom_tab_label(operation, _("Document Setup"));
234 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
235 gtk_container_set_border_width(GTK_CONTAINER(page), 5);
237 w->check_print_linenumbers = gtk_check_button_new_with_mnemonic(_("Print line numbers"));
238 gtk_box_pack_start(GTK_BOX(page), w->check_print_linenumbers, FALSE, FALSE, 0);
239 gtk_widget_set_tooltip_text(w->check_print_linenumbers, _("Add line numbers to the printed page"));
240 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_linenumbers), printing_prefs.print_line_numbers);
242 w->check_print_pagenumbers = gtk_check_button_new_with_mnemonic(_("Print page numbers"));
243 gtk_box_pack_start(GTK_BOX(page), w->check_print_pagenumbers, FALSE, FALSE, 0);
244 gtk_widget_set_tooltip_text(w->check_print_pagenumbers, _("Add page numbers at the bottom of each page. It takes 2 lines of the page."));
245 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_pagenumbers), printing_prefs.print_page_numbers);
247 w->check_print_pageheader = gtk_check_button_new_with_mnemonic(_("Print page header"));
248 gtk_box_pack_start(GTK_BOX(page), w->check_print_pageheader, FALSE, FALSE, 0);
249 gtk_widget_set_tooltip_text(w->check_print_pageheader, _("Add a little header to every page containing the page number, the filename and the current date (see below). It takes 3 lines of the page."));
250 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_pageheader), printing_prefs.print_page_header);
251 g_signal_connect(w->check_print_pageheader, "toggled", G_CALLBACK(on_page_header_toggled), w);
253 frame33 = gtk_frame_new(NULL);
254 gtk_box_pack_start(GTK_BOX(page), frame33, FALSE, FALSE, 0);
255 gtk_frame_set_label_align(GTK_FRAME(frame33), 0, 0);
256 gtk_frame_set_shadow_type(GTK_FRAME(frame33), GTK_SHADOW_NONE);
258 alignment36 = gtk_alignment_new(0, 0.5, 1, 1);
259 gtk_container_add(GTK_CONTAINER(frame33), alignment36);
260 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment36), 0, 0, 12, 0);
262 vbox30 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
263 gtk_container_add(GTK_CONTAINER(alignment36), vbox30);
265 w->check_print_basename = gtk_check_button_new_with_mnemonic(_("Use the basename of the printed file"));
266 gtk_box_pack_start(GTK_BOX(vbox30), w->check_print_basename, FALSE, FALSE, 0);
267 gtk_widget_set_tooltip_text(w->check_print_basename, _("Print only the basename(without the path) of the printed file"));
268 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_basename), printing_prefs.page_header_basename);
270 hbox10 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
271 gtk_box_pack_start(GTK_BOX(vbox30), hbox10, TRUE, TRUE, 0);
273 label203 = gtk_label_new(_("Date format:"));
274 gtk_box_pack_start(GTK_BOX(hbox10), label203, FALSE, FALSE, 0);
276 w->entry_print_dateformat = gtk_entry_new();
277 ui_entry_add_clear_icon(GTK_ENTRY(w->entry_print_dateformat));
278 gtk_box_pack_start(GTK_BOX(hbox10), w->entry_print_dateformat, TRUE, TRUE, 0);
279 gtk_widget_set_tooltip_text(w->entry_print_dateformat, _("Specify a format for the date and time stamp which is added to the page header on each page. You can use any conversion specifiers which can be used with the ANSI C strftime function."));
280 gtk_entry_set_text(GTK_ENTRY(w->entry_print_dateformat), printing_prefs.page_header_datefmt);
282 on_page_header_toggled(GTK_TOGGLE_BUTTON(w->check_print_pageheader), w);
283 gtk_widget_show_all(page);
284 return page;
288 static void end_print(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
290 DocInfo *dinfo = user_data;
292 if (dinfo == NULL)
293 return;
295 gtk_widget_hide(main_widgets.progressbar);
296 g_object_unref(dinfo->sci);
297 g_object_unref(dinfo->layout);
298 g_array_free(dinfo->pages, TRUE);
302 static void setup_range(DocInfo *dinfo, GtkPrintContext *ctx)
304 dinfo->fr.hdc = dinfo->fr.hdcTarget = gtk_print_context_get_cairo_context(ctx);
306 dinfo->fr.rcPage.left = 0;
307 dinfo->fr.rcPage.top = 0;
308 dinfo->fr.rcPage.right = gtk_print_context_get_width(ctx);
309 dinfo->fr.rcPage.bottom = gtk_print_context_get_height(ctx);
311 dinfo->fr.rc.left = dinfo->fr.rcPage.left;
312 dinfo->fr.rc.top = dinfo->fr.rcPage.top;
313 dinfo->fr.rc.right = dinfo->fr.rcPage.right;
314 dinfo->fr.rc.bottom = dinfo->fr.rcPage.bottom;
316 if (printing_prefs.print_page_header)
317 dinfo->fr.rc.top += dinfo->line_height * 3; /* header height */
318 if (printing_prefs.print_page_numbers)
319 dinfo->fr.rc.bottom -= dinfo->line_height * 1; /* footer height */
321 dinfo->fr.rcPage.left /= dinfo->sci_scale;
322 dinfo->fr.rcPage.top /= dinfo->sci_scale;
323 dinfo->fr.rcPage.right /= dinfo->sci_scale;
324 dinfo->fr.rcPage.bottom /= dinfo->sci_scale;
325 dinfo->fr.rc.left /= dinfo->sci_scale;
326 dinfo->fr.rc.top /= dinfo->sci_scale;
327 dinfo->fr.rc.right /= dinfo->sci_scale;
328 dinfo->fr.rc.bottom /= dinfo->sci_scale;
330 dinfo->fr.chrg.cpMin = 0;
331 dinfo->fr.chrg.cpMax = sci_get_length(dinfo->sci);
335 static void begin_print(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
337 DocInfo *dinfo = user_data;
338 PangoContext *pango_ctx, *widget_pango_ctx;
339 PangoFontDescription *desc;
340 gdouble pango_res, widget_res;
342 if (dinfo == NULL)
343 return;
345 gtk_widget_show(main_widgets.progressbar);
347 /* init dinfo fields */
349 /* setup printing scintilla object */
350 dinfo->sci = editor_create_widget(dinfo->doc->editor);
351 /* since we won't add the widget to any container, assume it's ownership */
352 g_object_ref_sink(dinfo->sci);
353 SSM(dinfo->sci, SCI_SETDOCPOINTER, 0,
354 SSM(dinfo->doc->editor->sci, SCI_GETDOCPOINTER, 0, 0));
355 highlighting_set_styles(dinfo->sci, dinfo->doc->file_type);
356 sci_set_line_numbers(dinfo->sci, printing_prefs.print_line_numbers);
357 SSM(dinfo->sci, SCI_SETVIEWWS, SCWS_INVISIBLE, 0);
358 SSM(dinfo->sci, SCI_SETVIEWEOL, FALSE, 0);
359 SSM(dinfo->sci, SCI_SETEDGEMODE, EDGE_NONE, 0);
360 SSM(dinfo->sci, SCI_SETPRINTCOLOURMODE, SC_PRINT_COLOURONWHITE, 0);
362 /* Scintilla doesn't respect the context resolution, so we'll scale ourselves.
363 * Actually Scintilla simply doesn't know about the resolution since it creates its own
364 * Pango context out of the Cairo target, and the resolution is in the GtkPrintOperation's
365 * Pango context */
366 pango_ctx = gtk_print_context_create_pango_context(context);
367 pango_res = pango_cairo_context_get_resolution(pango_ctx);
368 g_object_unref(pango_ctx);
369 widget_pango_ctx = gtk_widget_get_pango_context(GTK_WIDGET(dinfo->sci));
370 widget_res = pango_cairo_context_get_resolution(widget_pango_ctx);
371 /* On Windows, for some reason the widget's resolution is -1, so follow
372 * Pango docs and peek the font map's one. */
373 if (widget_res < 0)
375 widget_res = pango_cairo_font_map_get_resolution(
376 (PangoCairoFontMap*) pango_context_get_font_map(widget_pango_ctx));
378 dinfo->sci_scale = pango_res / widget_res;
380 dinfo->pages = g_array_new(FALSE, FALSE, sizeof(gint));
382 dinfo->print_time = time(NULL);
383 /* create a PangoLayout to be commonly used in add_page_header() and draw_page() */
384 desc = pango_font_description_from_string(interface_prefs.editor_font);
385 dinfo->layout = setup_pango_layout(context, desc);
386 pango_font_description_free(desc);
387 get_text_dimensions(dinfo->layout, "|XMfjgq_" /* reasonably representative character set */,
388 NULL, &dinfo->line_height);
389 get_text_dimensions(dinfo->layout, "99999 " /* Scintilla resets the margin to the width of "99999" when printing */,
390 &dinfo->margin_width, NULL);
391 /* setup dinfo->fr */
392 setup_range(dinfo, context);
396 static gint format_range(DocInfo *dinfo, gboolean draw)
398 gint pos;
400 cairo_save(dinfo->fr.hdc);
401 cairo_scale(dinfo->fr.hdc, dinfo->sci_scale, dinfo->sci_scale);
402 pos = (gint) SSM(dinfo->sci, SCI_FORMATRANGE, draw, (sptr_t) &dinfo->fr);
403 cairo_restore(dinfo->fr.hdc);
405 return pos;
409 static gboolean paginate(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
411 DocInfo *dinfo = user_data;
413 /* for whatever reason we get called one more time after we returned TRUE, so avoid adding
414 * an empty page at the end */
415 if (dinfo->fr.chrg.cpMin >= dinfo->fr.chrg.cpMax)
416 return TRUE;
418 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets.progressbar));
419 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), _("Paginating"));
421 g_array_append_val(dinfo->pages, dinfo->fr.chrg.cpMin);
422 dinfo->fr.chrg.cpMin = format_range(dinfo, FALSE);
424 gtk_print_operation_set_n_pages(operation, dinfo->pages->len);
426 return dinfo->fr.chrg.cpMin >= dinfo->fr.chrg.cpMax;
430 static void draw_page(GtkPrintOperation *operation, GtkPrintContext *context,
431 gint page_nr, gpointer user_data)
433 DocInfo *dinfo = user_data;
434 cairo_t *cr;
435 gdouble width, height;
437 g_return_if_fail(dinfo != NULL);
438 g_return_if_fail((guint)page_nr < dinfo->pages->len);
440 if (dinfo->pages->len > 0)
442 gdouble fraction = (page_nr + 1) / (gdouble) dinfo->pages->len;
443 gchar *text = g_strdup_printf(_("Page %d of %d"), page_nr + 1, dinfo->pages->len);
444 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(main_widgets.progressbar), fraction);
445 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
446 g_free(text);
449 cr = gtk_print_context_get_cairo_context(context);
450 width = gtk_print_context_get_width(context);
451 height = gtk_print_context_get_height(context);
453 if (printing_prefs.print_page_header)
454 add_page_header(dinfo, cr, width, page_nr);
456 dinfo->fr.chrg.cpMin = g_array_index(dinfo->pages, gint, page_nr);
457 if ((guint)page_nr + 1 < dinfo->pages->len)
458 dinfo->fr.chrg.cpMax = g_array_index(dinfo->pages, gint, page_nr + 1) - 1;
459 else /* it's the last page, print 'til the end */
460 dinfo->fr.chrg.cpMax = sci_get_length(dinfo->sci);
462 format_range(dinfo, TRUE);
464 /* reset color */
465 cairo_set_source_rgb(cr, 0, 0, 0);
467 if (printing_prefs.print_line_numbers)
468 { /* print a thin line between the line number margin and the data */
469 gdouble y1 = dinfo->fr.rc.top * dinfo->sci_scale;
470 gdouble y2 = dinfo->fr.rc.bottom * dinfo->sci_scale;
471 gdouble x = dinfo->fr.rc.left * dinfo->sci_scale + dinfo->margin_width;
473 if (printing_prefs.print_page_header)
474 y1 -= 2 - 0.3; /* to connect the line number line to the page header frame,
475 * 2 is the border, and 0.3 the line width */
477 cairo_set_line_width(cr, 0.3);
478 cairo_move_to(cr, x, y1);
479 cairo_line_to(cr, x, y2);
480 cairo_stroke(cr);
483 if (printing_prefs.print_page_numbers)
485 gchar *line = g_strdup_printf("<small>- %d -</small>", page_nr + 1);
486 pango_layout_set_markup(dinfo->layout, line, -1);
487 pango_layout_set_alignment(dinfo->layout, PANGO_ALIGN_CENTER);
488 cairo_move_to(cr, 0, height - dinfo->line_height);
489 pango_cairo_show_layout(cr, dinfo->layout);
490 g_free(line);
495 static void status_changed(GtkPrintOperation *op, gpointer data)
497 gchar *filename = (data != NULL) ? data : GEANY_STRING_UNTITLED;
498 if (gtk_print_operation_get_status(op) == GTK_PRINT_STATUS_FINISHED_ABORTED)
499 msgwin_status_add(_("Did not send document %s to the printing subsystem."), filename);
500 else if (gtk_print_operation_get_status(op) == GTK_PRINT_STATUS_FINISHED)
501 msgwin_status_add(_("Document %s was sent to the printing subsystem."), filename);
505 static void printing_print_gtk(GeanyDocument *doc)
507 GtkPrintOperation *op;
508 GtkPrintOperationResult res = GTK_PRINT_OPERATION_RESULT_ERROR;
509 GError *error = NULL;
510 static const DocInfo dinfo0;
511 DocInfo dinfo = dinfo0;
512 PrintWidgets *widgets;
514 /** TODO check for monospace font, detect the widest character in the font and
515 * use it at font_width */
517 widgets = g_new0(PrintWidgets, 1);
518 /* all other fields are initialised in begin_print() */
519 dinfo.doc = doc;
521 op = gtk_print_operation_new();
523 gtk_print_operation_set_unit(op, GTK_UNIT_POINTS);
524 gtk_print_operation_set_show_progress(op, TRUE);
525 gtk_print_operation_set_embed_page_setup(op, TRUE);
527 g_signal_connect(op, "begin-print", G_CALLBACK(begin_print), &dinfo);
528 g_signal_connect(op, "end-print", G_CALLBACK(end_print), &dinfo);
529 g_signal_connect(op, "paginate", G_CALLBACK(paginate), &dinfo);
530 g_signal_connect(op, "draw-page", G_CALLBACK(draw_page), &dinfo);
531 g_signal_connect(op, "status-changed", G_CALLBACK(status_changed), doc->file_name);
532 g_signal_connect(op, "create-custom-widget", G_CALLBACK(create_custom_widget), widgets);
533 g_signal_connect(op, "custom-widget-apply", G_CALLBACK(custom_widget_apply), widgets);
535 if (settings != NULL)
536 gtk_print_operation_set_print_settings(op, settings);
537 if (page_setup != NULL)
538 gtk_print_operation_set_default_page_setup(op, page_setup);
540 res = gtk_print_operation_run(
541 op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW(main_widgets.window), &error);
543 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
545 if (settings != NULL)
546 g_object_unref(settings);
547 settings = g_object_ref(gtk_print_operation_get_print_settings(op));
548 /* status message is printed in the status-changed handler */
550 else if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
552 dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Printing of %s failed (%s)."),
553 doc->file_name, error->message);
554 g_error_free(error);
557 g_object_unref(op);
558 g_free(widgets);
562 void printing_page_setup_gtk(void)
564 GtkPageSetup *new_page_setup;
566 if (settings == NULL)
567 settings = gtk_print_settings_new();
569 new_page_setup = gtk_print_run_page_setup_dialog(
570 GTK_WINDOW(main_widgets.window), page_setup, settings);
572 if (page_setup != NULL)
573 g_object_unref(page_setup);
575 page_setup = new_page_setup;
579 /* simple file print using an external tool */
580 static void print_external(GeanyDocument *doc)
582 gchar *cmdline;
584 if (doc->file_name == NULL)
585 return;
587 if (EMPTY(printing_prefs.external_print_cmd))
589 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
590 _("Please set a print command in the preferences dialog first."));
591 return;
594 cmdline = g_strdup(printing_prefs.external_print_cmd);
595 utils_str_replace_all(&cmdline, "%f", doc->file_name);
597 if (dialogs_show_question(
598 _("The file \"%s\" will be printed with the following command:\n\n%s"),
599 doc->file_name, cmdline))
601 GError *error = NULL;
602 /* /bin/sh -c emulates the system() call and makes complex commands possible
603 * but only on non-win32 systems due to the lack of win32's shell capabilities */
604 #ifdef G_OS_UNIX
605 gchar *argv[] = { "/bin/sh", "-c", cmdline, NULL };
607 if (!spawn_async(NULL, NULL, argv, NULL, NULL, &error))
608 #else
609 if (!spawn_async(NULL, cmdline, NULL, NULL, NULL, &error))
610 #endif
612 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
613 _("Cannot execute print command \"%s\": %s. "
614 "Check the path setting in Preferences."),
615 printing_prefs.external_print_cmd, error->message);
616 g_error_free(error);
618 else
620 msgwin_status_add(_("File %s printed."), doc->file_name);
623 g_free(cmdline);
627 void printing_print_doc(GeanyDocument *doc)
629 g_return_if_fail(DOC_VALID(doc));
631 if (printing_prefs.use_gtk_printing)
632 printing_print_gtk(doc);
633 else
634 print_external(doc);