Put Makefile comments at start of line.
[geany-mirror.git] / src / printing.c
blob743cd8809ccb59a2676b9245307e8cb41801c929
1 /*
2 * printing.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2012 Colomban Wendling <ban(at)herbesfolles(dot)org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * GTK printing support
26 * (basic code layout were adopted from Sylpheed's printing implementation, thanks)
29 #include <math.h>
30 #include <time.h>
31 #include <string.h>
33 #include "geany.h"
34 #include "printing.h"
35 #include "document.h"
36 #include "sciwrappers.h"
37 #include "editor.h"
38 #include "utils.h"
39 #include "support.h"
40 #include "dialogs.h"
41 #include "ui_utils.h"
42 #include "msgwindow.h"
43 #include "highlighting.h"
44 #include "Scintilla.h"
47 PrintingPrefs printing_prefs;
50 /* document-related variables */
51 typedef struct
53 GeanyDocument *doc;
54 ScintillaObject *sci;
55 gdouble margin_width;
56 gdouble line_height;
57 /* set in begin_print() to hold the time when printing was started to ensure all printed
58 * pages have the same date and time (in case of slow machines and many pages where rendering
59 * takes more than a second) */
60 time_t print_time;
61 PangoLayout *layout; /* commonly used layout object */
62 gdouble sci_scale;
64 struct Sci_RangeToFormat fr;
65 GArray *pages;
66 } DocInfo;
68 /* widget references for the custom widget in the print dialog */
69 typedef struct
71 GtkWidget *check_print_linenumbers;
72 GtkWidget *check_print_pagenumbers;
73 GtkWidget *check_print_pageheader;
74 GtkWidget *check_print_basename;
75 GtkWidget *entry_print_dateformat;
76 } PrintWidgets;
79 static GtkPrintSettings *settings = NULL;
80 static GtkPageSetup *page_setup = NULL;
84 /* creates a commonly used layout object from the given context for use in get_page_count and
85 * draw_page */
86 static PangoLayout *setup_pango_layout(GtkPrintContext *context, PangoFontDescription *desc)
88 PangoLayout *layout;
90 layout = gtk_print_context_create_pango_layout(context);
91 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
92 pango_layout_set_spacing(layout, 0);
93 pango_layout_set_attributes(layout, NULL);
94 pango_layout_set_font_description(layout, desc);
96 return layout;
100 static void get_text_dimensions(PangoLayout *layout, const gchar *text, gdouble *width, gdouble *height)
102 gint layout_w, layout_h;
104 pango_layout_set_text(layout, text, -1);
105 pango_layout_get_size(layout, &layout_w, &layout_h);
106 if (layout_w <= 0)
108 gint default_w = 50 * strlen(text) * PANGO_SCALE;
110 geany_debug("Invalid layout_w (%d). Falling back to default width (%d)",
111 layout_w, default_w);
112 layout_w = default_w;
114 if (layout_h <= 0)
116 gint default_h = 100 * PANGO_SCALE;
118 geany_debug("Invalid layout_h (%d). Falling back to default height (%d)",
119 layout_h, default_h);
120 layout_h = default_h;
123 if (width)
124 *width = (gdouble)layout_w / PANGO_SCALE;
125 if (height)
126 *height = (gdouble)layout_h / PANGO_SCALE;
130 static void add_page_header(DocInfo *dinfo, cairo_t *cr, gint width, gint page_nr)
132 gint ph_height = dinfo->line_height * 3;
133 gchar *data;
134 gchar *datetime;
135 const gchar *tmp_file_name = DOC_FILENAME(dinfo->doc);
136 gchar *file_name = (printing_prefs.page_header_basename) ?
137 g_path_get_basename(tmp_file_name) : g_strdup(tmp_file_name);
138 PangoLayout *layout = dinfo->layout;
140 /* draw the frame */
141 cairo_set_line_width(cr, 0.3);
142 cairo_set_source_rgb(cr, 0, 0, 0);
143 cairo_rectangle(cr, 2, 2, width - 4, ph_height - 4);
144 cairo_stroke(cr);
146 /* width - 8: 2px between doc border and frame border, 2px between frame border and text
147 * and this on left and right side, so (2 + 2) * 2 */
148 pango_layout_set_width(layout, (width - 8) * PANGO_SCALE);
149 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_MIDDLE);
151 data = g_strdup_printf("<b>%s</b>", file_name);
152 pango_layout_set_markup(layout, data, -1);
153 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
154 cairo_move_to(cr, 4, dinfo->line_height * 0.5);
155 pango_cairo_show_layout(cr, layout);
156 g_free(data);
157 g_free(file_name);
159 data = g_strdup_printf(_("<b>Page %d of %d</b>"), page_nr + 1, dinfo->pages->len);
160 pango_layout_set_markup(layout, data, -1);
161 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
162 cairo_move_to(cr, 4, dinfo->line_height * 1.5);
163 pango_cairo_show_layout(cr, layout);
164 g_free(data);
166 datetime = utils_get_date_time(printing_prefs.page_header_datefmt, &(dinfo->print_time));
167 if (G_LIKELY(!EMPTY(datetime)))
169 data = g_strdup_printf("<b>%s</b>", datetime);
170 pango_layout_set_markup(layout, data, -1);
171 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
172 cairo_move_to(cr, 2, dinfo->line_height * 1.5);
173 pango_cairo_show_layout(cr, layout);
174 g_free(data);
176 g_free(datetime);
178 /* reset layout and re-position cairo context */
179 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
180 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
181 pango_layout_set_justify(layout, FALSE);
182 pango_layout_set_width(layout, width * PANGO_SCALE);
183 cairo_move_to(cr, 0, dinfo->line_height * 3);
187 static void custom_widget_apply(GtkPrintOperation *operation, GtkWidget *widget, gpointer user_data)
189 PrintWidgets *w = user_data;
191 printing_prefs.print_line_numbers =
192 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_linenumbers));
194 printing_prefs.print_page_numbers =
195 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_pagenumbers));
197 printing_prefs.print_page_header =
198 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_pageheader));
200 printing_prefs.page_header_basename =
201 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_basename));
203 g_free(printing_prefs.page_header_datefmt);
204 printing_prefs.page_header_datefmt =
205 g_strdup(gtk_entry_get_text(GTK_ENTRY(w->entry_print_dateformat)));
209 static void on_page_header_toggled(GtkToggleButton *togglebutton, gpointer user_data)
211 gboolean sens = gtk_toggle_button_get_active(togglebutton);
212 PrintWidgets *w = user_data;
214 gtk_widget_set_sensitive(w->check_print_basename, sens);
215 gtk_widget_set_sensitive(w->entry_print_dateformat, sens);
219 static GtkWidget *create_custom_widget(GtkPrintOperation *operation, gpointer user_data)
220 { /* copied from interface.c */
221 GtkWidget *page;
222 GtkWidget *frame33;
223 GtkWidget *alignment36;
224 GtkWidget *vbox30;
225 GtkWidget *hbox10;
226 GtkWidget *label203;
227 PrintWidgets *w = user_data;
229 gtk_print_operation_set_custom_tab_label(operation, _("Document Setup"));
231 page = gtk_vbox_new(FALSE, 0);
232 gtk_container_set_border_width(GTK_CONTAINER(page), 5);
234 w->check_print_linenumbers = gtk_check_button_new_with_mnemonic(_("Print line numbers"));
235 gtk_box_pack_start(GTK_BOX(page), w->check_print_linenumbers, FALSE, FALSE, 0);
236 gtk_widget_set_tooltip_text(w->check_print_linenumbers, _("Add line numbers to the printed page"));
237 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_linenumbers), printing_prefs.print_line_numbers);
239 w->check_print_pagenumbers = gtk_check_button_new_with_mnemonic(_("Print page numbers"));
240 gtk_box_pack_start(GTK_BOX(page), w->check_print_pagenumbers, FALSE, FALSE, 0);
241 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."));
242 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_pagenumbers), printing_prefs.print_page_numbers);
244 w->check_print_pageheader = gtk_check_button_new_with_mnemonic(_("Print page header"));
245 gtk_box_pack_start(GTK_BOX(page), w->check_print_pageheader, FALSE, FALSE, 0);
246 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."));
247 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_pageheader), printing_prefs.print_page_header);
248 g_signal_connect(w->check_print_pageheader, "toggled", G_CALLBACK(on_page_header_toggled), w);
250 frame33 = gtk_frame_new(NULL);
251 gtk_box_pack_start(GTK_BOX(page), frame33, FALSE, FALSE, 0);
252 gtk_frame_set_label_align(GTK_FRAME(frame33), 0, 0);
253 gtk_frame_set_shadow_type(GTK_FRAME(frame33), GTK_SHADOW_NONE);
255 alignment36 = gtk_alignment_new(0, 0.5, 1, 1);
256 gtk_container_add(GTK_CONTAINER(frame33), alignment36);
257 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment36), 0, 0, 12, 0);
259 vbox30 = gtk_vbox_new(FALSE, 1);
260 gtk_container_add(GTK_CONTAINER(alignment36), vbox30);
262 w->check_print_basename = gtk_check_button_new_with_mnemonic(_("Use the basename of the printed file"));
263 gtk_box_pack_start(GTK_BOX(vbox30), w->check_print_basename, FALSE, FALSE, 0);
264 gtk_widget_set_tooltip_text(w->check_print_basename, _("Print only the basename(without the path) of the printed file"));
265 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_basename), printing_prefs.page_header_basename);
267 hbox10 = gtk_hbox_new(FALSE, 5);
268 gtk_box_pack_start(GTK_BOX(vbox30), hbox10, TRUE, TRUE, 0);
270 label203 = gtk_label_new(_("Date format:"));
271 gtk_box_pack_start(GTK_BOX(hbox10), label203, FALSE, FALSE, 0);
273 w->entry_print_dateformat = gtk_entry_new();
274 ui_entry_add_clear_icon(GTK_ENTRY(w->entry_print_dateformat));
275 gtk_box_pack_start(GTK_BOX(hbox10), w->entry_print_dateformat, TRUE, TRUE, 0);
276 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."));
277 gtk_entry_set_text(GTK_ENTRY(w->entry_print_dateformat), printing_prefs.page_header_datefmt);
279 on_page_header_toggled(GTK_TOGGLE_BUTTON(w->check_print_pageheader), w);
280 gtk_widget_show_all(page);
281 return page;
285 static void end_print(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
287 DocInfo *dinfo = user_data;
289 if (dinfo == NULL)
290 return;
292 gtk_widget_hide(main_widgets.progressbar);
293 g_object_unref(dinfo->sci);
294 g_object_unref(dinfo->layout);
295 g_array_free(dinfo->pages, TRUE);
299 static void setup_range(DocInfo *dinfo, GtkPrintContext *ctx)
301 dinfo->fr.hdc = dinfo->fr.hdcTarget = gtk_print_context_get_cairo_context(ctx);
303 dinfo->fr.rcPage.left = 0;
304 dinfo->fr.rcPage.top = 0;
305 dinfo->fr.rcPage.right = gtk_print_context_get_width(ctx);
306 dinfo->fr.rcPage.bottom = gtk_print_context_get_height(ctx);
308 dinfo->fr.rc.left = dinfo->fr.rcPage.left;
309 dinfo->fr.rc.top = dinfo->fr.rcPage.top;
310 dinfo->fr.rc.right = dinfo->fr.rcPage.right;
311 dinfo->fr.rc.bottom = dinfo->fr.rcPage.bottom;
313 if (printing_prefs.print_page_header)
314 dinfo->fr.rc.top += dinfo->line_height * 3; /* header height */
315 if (printing_prefs.print_page_numbers)
316 dinfo->fr.rc.bottom -= dinfo->line_height * 1; /* footer height */
318 dinfo->fr.rcPage.left /= dinfo->sci_scale;
319 dinfo->fr.rcPage.top /= dinfo->sci_scale;
320 dinfo->fr.rcPage.right /= dinfo->sci_scale;
321 dinfo->fr.rcPage.bottom /= dinfo->sci_scale;
322 dinfo->fr.rc.left /= dinfo->sci_scale;
323 dinfo->fr.rc.top /= dinfo->sci_scale;
324 dinfo->fr.rc.right /= dinfo->sci_scale;
325 dinfo->fr.rc.bottom /= dinfo->sci_scale;
327 dinfo->fr.chrg.cpMin = 0;
328 dinfo->fr.chrg.cpMax = sci_get_length(dinfo->sci);
332 static void begin_print(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
334 DocInfo *dinfo = user_data;
335 PangoContext *pango_ctx, *widget_pango_ctx;
336 PangoFontDescription *desc;
337 gdouble pango_res, widget_res;
339 if (dinfo == NULL)
340 return;
342 gtk_widget_show(main_widgets.progressbar);
344 /* init dinfo fields */
346 /* setup printing scintilla object */
347 dinfo->sci = editor_create_widget(dinfo->doc->editor);
348 scintilla_send_message(dinfo->sci, SCI_SETDOCPOINTER, 0,
349 scintilla_send_message(dinfo->doc->editor->sci, SCI_GETDOCPOINTER, 0, 0));
350 highlighting_set_styles(dinfo->sci, dinfo->doc->file_type);
351 sci_set_line_numbers(dinfo->sci, printing_prefs.print_line_numbers, 0);
352 scintilla_send_message(dinfo->sci, SCI_SETVIEWWS, SCWS_INVISIBLE, 0);
353 scintilla_send_message(dinfo->sci, SCI_SETVIEWEOL, FALSE, 0);
354 scintilla_send_message(dinfo->sci, SCI_SETEDGEMODE, EDGE_NONE, 0);
355 scintilla_send_message(dinfo->sci, SCI_SETPRINTCOLOURMODE, SC_PRINT_COLOURONWHITE, 0);
357 /* Scintilla doesn't respect the context resolution, so we'll scale ourselves.
358 * Actually Scintilla simply doesn't know about the resolution since it creates its own
359 * Pango context out of the Cairo target, and the resolution is in the GtkPrintOperation's
360 * Pango context */
361 pango_ctx = gtk_print_context_create_pango_context(context);
362 pango_res = pango_cairo_context_get_resolution(pango_ctx);
363 g_object_unref(pango_ctx);
364 widget_pango_ctx = gtk_widget_get_pango_context(GTK_WIDGET(dinfo->sci));
365 widget_res = pango_cairo_context_get_resolution(widget_pango_ctx);
366 /* On Windows, for some reason the widget's resolution is -1, so follow
367 * Pango docs and peek the font map's one. */
368 if (widget_res < 0)
370 widget_res = pango_cairo_font_map_get_resolution(
371 (PangoCairoFontMap*) pango_context_get_font_map(widget_pango_ctx));
373 dinfo->sci_scale = pango_res / widget_res;
375 dinfo->pages = g_array_new(FALSE, FALSE, sizeof(gint));
377 dinfo->print_time = time(NULL);
378 /* create a PangoLayout to be commonly used in add_page_header() and draw_page() */
379 desc = pango_font_description_from_string(interface_prefs.editor_font);
380 dinfo->layout = setup_pango_layout(context, desc);
381 pango_font_description_free(desc);
382 get_text_dimensions(dinfo->layout, "|XMfjgq_" /* reasonably representative character set */,
383 NULL, &dinfo->line_height);
384 get_text_dimensions(dinfo->layout, "99999 " /* Scintilla resets the margin to the width of "99999" when printing */,
385 &dinfo->margin_width, NULL);
386 /* setup dinfo->fr */
387 setup_range(dinfo, context);
391 static gint format_range(DocInfo *dinfo, gboolean draw)
393 gint pos;
395 cairo_save(dinfo->fr.hdc);
396 cairo_scale(dinfo->fr.hdc, dinfo->sci_scale, dinfo->sci_scale);
397 pos = (gint) scintilla_send_message(dinfo->sci, SCI_FORMATRANGE, draw, (sptr_t) &dinfo->fr);
398 cairo_restore(dinfo->fr.hdc);
400 return pos;
404 static gboolean paginate(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
406 DocInfo *dinfo = user_data;
408 /* for whatever reason we get called one more time after we returned TRUE, so avoid adding
409 * an empty page at the end */
410 if (dinfo->fr.chrg.cpMin >= dinfo->fr.chrg.cpMax)
411 return TRUE;
413 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets.progressbar));
414 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), _("Paginating"));
416 g_array_append_val(dinfo->pages, dinfo->fr.chrg.cpMin);
417 dinfo->fr.chrg.cpMin = format_range(dinfo, FALSE);
419 gtk_print_operation_set_n_pages(operation, dinfo->pages->len);
421 return dinfo->fr.chrg.cpMin >= dinfo->fr.chrg.cpMax;
425 static void draw_page(GtkPrintOperation *operation, GtkPrintContext *context,
426 gint page_nr, gpointer user_data)
428 DocInfo *dinfo = user_data;
429 cairo_t *cr;
430 gdouble width, height;
432 g_return_if_fail(dinfo != NULL);
433 g_return_if_fail((guint)page_nr < dinfo->pages->len);
435 if (dinfo->pages->len > 0)
437 gdouble fraction = (page_nr + 1) / (gdouble) dinfo->pages->len;
438 gchar *text = g_strdup_printf(_("Page %d of %d"), page_nr + 1, dinfo->pages->len);
439 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(main_widgets.progressbar), fraction);
440 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
441 g_free(text);
444 cr = gtk_print_context_get_cairo_context(context);
445 width = gtk_print_context_get_width(context);
446 height = gtk_print_context_get_height(context);
448 if (printing_prefs.print_page_header)
449 add_page_header(dinfo, cr, width, page_nr);
451 dinfo->fr.chrg.cpMin = g_array_index(dinfo->pages, gint, page_nr);
452 if ((guint)page_nr + 1 < dinfo->pages->len)
453 dinfo->fr.chrg.cpMax = g_array_index(dinfo->pages, gint, page_nr + 1) - 1;
454 else /* it's the last page, print 'til the end */
455 dinfo->fr.chrg.cpMax = sci_get_length(dinfo->sci);
457 format_range(dinfo, TRUE);
459 /* reset color */
460 cairo_set_source_rgb(cr, 0, 0, 0);
462 if (printing_prefs.print_line_numbers)
463 { /* print a thin line between the line number margin and the data */
464 gdouble y1 = dinfo->fr.rc.top * dinfo->sci_scale;
465 gdouble y2 = dinfo->fr.rc.bottom * dinfo->sci_scale;
466 gdouble x = dinfo->fr.rc.left * dinfo->sci_scale + dinfo->margin_width;
468 if (printing_prefs.print_page_header)
469 y1 -= 2 - 0.3; /* to connect the line number line to the page header frame,
470 * 2 is the border, and 0.3 the line width */
472 cairo_set_line_width(cr, 0.3);
473 cairo_move_to(cr, x, y1);
474 cairo_line_to(cr, x, y2);
475 cairo_stroke(cr);
478 if (printing_prefs.print_page_numbers)
480 gchar *line = g_strdup_printf("<small>- %d -</small>", page_nr + 1);
481 pango_layout_set_markup(dinfo->layout, line, -1);
482 pango_layout_set_alignment(dinfo->layout, PANGO_ALIGN_CENTER);
483 cairo_move_to(cr, 0, height - dinfo->line_height);
484 pango_cairo_show_layout(cr, dinfo->layout);
485 g_free(line);
490 static void status_changed(GtkPrintOperation *op, gpointer data)
492 gchar *filename = (data != NULL) ? data : GEANY_STRING_UNTITLED;
493 if (gtk_print_operation_get_status(op) == GTK_PRINT_STATUS_FINISHED_ABORTED)
494 msgwin_status_add(_("Did not send document %s to the printing subsystem."), filename);
495 else if (gtk_print_operation_get_status(op) == GTK_PRINT_STATUS_FINISHED)
496 msgwin_status_add(_("Document %s was sent to the printing subsystem."), filename);
500 static void printing_print_gtk(GeanyDocument *doc)
502 GtkPrintOperation *op;
503 GtkPrintOperationResult res = GTK_PRINT_OPERATION_RESULT_ERROR;
504 GError *error = NULL;
505 static const DocInfo dinfo0;
506 DocInfo dinfo = dinfo0;
507 PrintWidgets *widgets;
509 /** TODO check for monospace font, detect the widest character in the font and
510 * use it at font_width */
512 widgets = g_new0(PrintWidgets, 1);
513 /* all other fields are initialised in begin_print() */
514 dinfo.doc = doc;
516 op = gtk_print_operation_new();
518 gtk_print_operation_set_unit(op, GTK_UNIT_POINTS);
519 gtk_print_operation_set_show_progress(op, TRUE);
520 #if GTK_CHECK_VERSION(2, 18, 0)
521 gtk_print_operation_set_embed_page_setup(op, TRUE);
522 #endif
524 g_signal_connect(op, "begin-print", G_CALLBACK(begin_print), &dinfo);
525 g_signal_connect(op, "end-print", G_CALLBACK(end_print), &dinfo);
526 g_signal_connect(op, "paginate", G_CALLBACK(paginate), &dinfo);
527 g_signal_connect(op, "draw-page", G_CALLBACK(draw_page), &dinfo);
528 g_signal_connect(op, "status-changed", G_CALLBACK(status_changed), doc->file_name);
529 g_signal_connect(op, "create-custom-widget", G_CALLBACK(create_custom_widget), widgets);
530 g_signal_connect(op, "custom-widget-apply", G_CALLBACK(custom_widget_apply), widgets);
532 if (settings != NULL)
533 gtk_print_operation_set_print_settings(op, settings);
534 if (page_setup != NULL)
535 gtk_print_operation_set_default_page_setup(op, page_setup);
537 res = gtk_print_operation_run(
538 op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW(main_widgets.window), &error);
540 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
542 if (settings != NULL)
543 g_object_unref(settings);
544 settings = g_object_ref(gtk_print_operation_get_print_settings(op));
545 /* status message is printed in the status-changed handler */
547 else if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
549 dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Printing of %s failed (%s)."),
550 doc->file_name, error->message);
551 g_error_free(error);
554 g_object_unref(op);
555 g_free(widgets);
559 void printing_page_setup_gtk(void)
561 GtkPageSetup *new_page_setup;
563 if (settings == NULL)
564 settings = gtk_print_settings_new();
566 new_page_setup = gtk_print_run_page_setup_dialog(
567 GTK_WINDOW(main_widgets.window), page_setup, settings);
569 if (page_setup != NULL)
570 g_object_unref(page_setup);
572 page_setup = new_page_setup;
576 /* simple file print using an external tool */
577 static void print_external(GeanyDocument *doc)
579 gchar *cmdline;
581 if (doc->file_name == NULL)
582 return;
584 if (EMPTY(printing_prefs.external_print_cmd))
586 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
587 _("Please set a print command in the preferences dialog first."));
588 return;
591 cmdline = g_strdup(printing_prefs.external_print_cmd);
592 utils_str_replace_all(&cmdline, "%f", doc->file_name);
594 if (dialogs_show_question(
595 _("The file \"%s\" will be printed with the following command:\n\n%s"),
596 doc->file_name, cmdline))
598 GError *error = NULL;
600 #ifdef G_OS_WIN32
601 gchar *tmp_cmdline = g_strdup(cmdline);
602 #else
603 /* /bin/sh -c emulates the system() call and makes complex commands possible
604 * but only needed on non-win32 systems due to the lack of win32's shell capabilities */
605 gchar *tmp_cmdline = g_strconcat("/bin/sh -c \"", cmdline, "\"", NULL);
606 #endif
608 if (! g_spawn_command_line_async(tmp_cmdline, &error))
610 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
611 _("Printing of \"%s\" failed (return code: %s)."),
612 doc->file_name, error->message);
613 g_error_free(error);
615 else
617 msgwin_status_add(_("File %s printed."), doc->file_name);
619 g_free(tmp_cmdline);
621 g_free(cmdline);
625 void printing_print_doc(GeanyDocument *doc)
627 g_return_if_fail(DOC_VALID(doc));
629 if (printing_prefs.use_gtk_printing)
630 printing_print_gtk(doc);
631 else
632 print_external(doc);