Bump API version for new plugin entry points (oops)
[geany-mirror.git] / src / printing.c
blob79bb0eef369fe647312fbd20ce54873e4ddd1bd5
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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include "printing.h"
35 #include "app.h"
36 #include "dialogs.h"
37 #include "document.h"
38 #include "geany.h"
39 #include "highlighting.h"
40 #include "msgwindow.h"
41 #include "sciwrappers.h"
42 #include "spawn.h"
43 #include "support.h"
44 #include "utils.h"
45 #include "ui_utils.h"
47 #include <math.h>
48 #include <time.h>
49 #include <string.h>
52 PrintingPrefs printing_prefs;
55 /* document-related variables */
56 typedef struct
58 GeanyDocument *doc;
59 ScintillaObject *sci;
60 gdouble margin_width;
61 gdouble line_height;
62 /* set in begin_print() to hold the time when printing was started to ensure all printed
63 * pages have the same date and time (in case of slow machines and many pages where rendering
64 * takes more than a second) */
65 time_t print_time;
66 PangoLayout *layout; /* commonly used layout object */
67 gdouble sci_scale;
69 struct Sci_RangeToFormat fr;
70 GArray *pages;
71 } DocInfo;
73 /* widget references for the custom widget in the print dialog */
74 typedef struct
76 GtkWidget *check_print_linenumbers;
77 GtkWidget *check_print_pagenumbers;
78 GtkWidget *check_print_pageheader;
79 GtkWidget *check_print_basename;
80 GtkWidget *entry_print_dateformat;
81 } PrintWidgets;
84 static GtkPrintSettings *settings = NULL;
85 static GtkPageSetup *page_setup = NULL;
89 /* creates a commonly used layout object from the given context for use in get_page_count and
90 * draw_page */
91 static PangoLayout *setup_pango_layout(GtkPrintContext *context, PangoFontDescription *desc)
93 PangoLayout *layout;
95 layout = gtk_print_context_create_pango_layout(context);
96 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
97 pango_layout_set_spacing(layout, 0);
98 pango_layout_set_attributes(layout, NULL);
99 pango_layout_set_font_description(layout, desc);
101 return layout;
105 static void get_text_dimensions(PangoLayout *layout, const gchar *text, gdouble *width, gdouble *height)
107 gint layout_w, layout_h;
109 pango_layout_set_text(layout, text, -1);
110 pango_layout_get_size(layout, &layout_w, &layout_h);
111 if (layout_w <= 0)
113 gint default_w = 50 * strlen(text) * PANGO_SCALE;
115 geany_debug("Invalid layout_w (%d). Falling back to default width (%d)",
116 layout_w, default_w);
117 layout_w = default_w;
119 if (layout_h <= 0)
121 gint default_h = 100 * PANGO_SCALE;
123 geany_debug("Invalid layout_h (%d). Falling back to default height (%d)",
124 layout_h, default_h);
125 layout_h = default_h;
128 if (width)
129 *width = (gdouble)layout_w / PANGO_SCALE;
130 if (height)
131 *height = (gdouble)layout_h / PANGO_SCALE;
135 static void add_page_header(DocInfo *dinfo, cairo_t *cr, gint width, gint page_nr)
137 gint ph_height = dinfo->line_height * 3;
138 gchar *data;
139 gchar *datetime;
140 const gchar *tmp_file_name = DOC_FILENAME(dinfo->doc);
141 gchar *file_name = (printing_prefs.page_header_basename) ?
142 g_path_get_basename(tmp_file_name) : g_strdup(tmp_file_name);
143 PangoLayout *layout = dinfo->layout;
145 /* draw the frame */
146 cairo_set_line_width(cr, 0.3);
147 cairo_set_source_rgb(cr, 0, 0, 0);
148 cairo_rectangle(cr, 2, 2, width - 4, ph_height - 4);
149 cairo_stroke(cr);
151 /* width - 8: 2px between doc border and frame border, 2px between frame border and text
152 * and this on left and right side, so (2 + 2) * 2 */
153 pango_layout_set_width(layout, (width - 8) * PANGO_SCALE);
154 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_MIDDLE);
156 data = g_strdup_printf("<b>%s</b>", file_name);
157 pango_layout_set_markup(layout, data, -1);
158 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
159 cairo_move_to(cr, 4, dinfo->line_height * 0.5);
160 pango_cairo_show_layout(cr, layout);
161 g_free(data);
162 g_free(file_name);
164 data = g_strdup_printf(_("<b>Page %d of %d</b>"), page_nr + 1, dinfo->pages->len);
165 pango_layout_set_markup(layout, data, -1);
166 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
167 cairo_move_to(cr, 4, dinfo->line_height * 1.5);
168 pango_cairo_show_layout(cr, layout);
169 g_free(data);
171 datetime = utils_get_date_time(printing_prefs.page_header_datefmt, &(dinfo->print_time));
172 if (G_LIKELY(!EMPTY(datetime)))
174 data = g_strdup_printf("<b>%s</b>", datetime);
175 pango_layout_set_markup(layout, data, -1);
176 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
177 cairo_move_to(cr, 2, dinfo->line_height * 1.5);
178 pango_cairo_show_layout(cr, layout);
179 g_free(data);
181 g_free(datetime);
183 /* reset layout and re-position cairo context */
184 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
185 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
186 pango_layout_set_justify(layout, FALSE);
187 pango_layout_set_width(layout, width * PANGO_SCALE);
188 cairo_move_to(cr, 0, dinfo->line_height * 3);
192 static void custom_widget_apply(GtkPrintOperation *operation, GtkWidget *widget, gpointer user_data)
194 PrintWidgets *w = user_data;
196 printing_prefs.print_line_numbers =
197 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_linenumbers));
199 printing_prefs.print_page_numbers =
200 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_pagenumbers));
202 printing_prefs.print_page_header =
203 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_pageheader));
205 printing_prefs.page_header_basename =
206 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_basename));
208 g_free(printing_prefs.page_header_datefmt);
209 printing_prefs.page_header_datefmt =
210 g_strdup(gtk_entry_get_text(GTK_ENTRY(w->entry_print_dateformat)));
214 static void on_page_header_toggled(GtkToggleButton *togglebutton, gpointer user_data)
216 gboolean sens = gtk_toggle_button_get_active(togglebutton);
217 PrintWidgets *w = user_data;
219 gtk_widget_set_sensitive(w->check_print_basename, sens);
220 gtk_widget_set_sensitive(w->entry_print_dateformat, sens);
224 static GtkWidget *create_custom_widget(GtkPrintOperation *operation, gpointer user_data)
225 { /* copied from interface.c */
226 GtkWidget *page;
227 GtkWidget *frame33;
228 GtkWidget *alignment36;
229 GtkWidget *vbox30;
230 GtkWidget *hbox10;
231 GtkWidget *label203;
232 PrintWidgets *w = user_data;
234 gtk_print_operation_set_custom_tab_label(operation, _("Document Setup"));
236 page = gtk_vbox_new(FALSE, 0);
237 gtk_container_set_border_width(GTK_CONTAINER(page), 5);
239 w->check_print_linenumbers = gtk_check_button_new_with_mnemonic(_("Print line numbers"));
240 gtk_box_pack_start(GTK_BOX(page), w->check_print_linenumbers, FALSE, FALSE, 0);
241 gtk_widget_set_tooltip_text(w->check_print_linenumbers, _("Add line numbers to the printed page"));
242 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_linenumbers), printing_prefs.print_line_numbers);
244 w->check_print_pagenumbers = gtk_check_button_new_with_mnemonic(_("Print page numbers"));
245 gtk_box_pack_start(GTK_BOX(page), w->check_print_pagenumbers, FALSE, FALSE, 0);
246 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."));
247 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_pagenumbers), printing_prefs.print_page_numbers);
249 w->check_print_pageheader = gtk_check_button_new_with_mnemonic(_("Print page header"));
250 gtk_box_pack_start(GTK_BOX(page), w->check_print_pageheader, FALSE, FALSE, 0);
251 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."));
252 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_pageheader), printing_prefs.print_page_header);
253 g_signal_connect(w->check_print_pageheader, "toggled", G_CALLBACK(on_page_header_toggled), w);
255 frame33 = gtk_frame_new(NULL);
256 gtk_box_pack_start(GTK_BOX(page), frame33, FALSE, FALSE, 0);
257 gtk_frame_set_label_align(GTK_FRAME(frame33), 0, 0);
258 gtk_frame_set_shadow_type(GTK_FRAME(frame33), GTK_SHADOW_NONE);
260 alignment36 = gtk_alignment_new(0, 0.5, 1, 1);
261 gtk_container_add(GTK_CONTAINER(frame33), alignment36);
262 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment36), 0, 0, 12, 0);
264 vbox30 = gtk_vbox_new(FALSE, 1);
265 gtk_container_add(GTK_CONTAINER(alignment36), vbox30);
267 w->check_print_basename = gtk_check_button_new_with_mnemonic(_("Use the basename of the printed file"));
268 gtk_box_pack_start(GTK_BOX(vbox30), w->check_print_basename, FALSE, FALSE, 0);
269 gtk_widget_set_tooltip_text(w->check_print_basename, _("Print only the basename(without the path) of the printed file"));
270 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_basename), printing_prefs.page_header_basename);
272 hbox10 = gtk_hbox_new(FALSE, 5);
273 gtk_box_pack_start(GTK_BOX(vbox30), hbox10, TRUE, TRUE, 0);
275 label203 = gtk_label_new(_("Date format:"));
276 gtk_box_pack_start(GTK_BOX(hbox10), label203, FALSE, FALSE, 0);
278 w->entry_print_dateformat = gtk_entry_new();
279 ui_entry_add_clear_icon(GTK_ENTRY(w->entry_print_dateformat));
280 gtk_box_pack_start(GTK_BOX(hbox10), w->entry_print_dateformat, TRUE, TRUE, 0);
281 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."));
282 gtk_entry_set_text(GTK_ENTRY(w->entry_print_dateformat), printing_prefs.page_header_datefmt);
284 on_page_header_toggled(GTK_TOGGLE_BUTTON(w->check_print_pageheader), w);
285 gtk_widget_show_all(page);
286 return page;
290 static void end_print(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
292 DocInfo *dinfo = user_data;
294 if (dinfo == NULL)
295 return;
297 gtk_widget_hide(main_widgets.progressbar);
298 g_object_unref(dinfo->sci);
299 g_object_unref(dinfo->layout);
300 g_array_free(dinfo->pages, TRUE);
304 static void setup_range(DocInfo *dinfo, GtkPrintContext *ctx)
306 dinfo->fr.hdc = dinfo->fr.hdcTarget = gtk_print_context_get_cairo_context(ctx);
308 dinfo->fr.rcPage.left = 0;
309 dinfo->fr.rcPage.top = 0;
310 dinfo->fr.rcPage.right = gtk_print_context_get_width(ctx);
311 dinfo->fr.rcPage.bottom = gtk_print_context_get_height(ctx);
313 dinfo->fr.rc.left = dinfo->fr.rcPage.left;
314 dinfo->fr.rc.top = dinfo->fr.rcPage.top;
315 dinfo->fr.rc.right = dinfo->fr.rcPage.right;
316 dinfo->fr.rc.bottom = dinfo->fr.rcPage.bottom;
318 if (printing_prefs.print_page_header)
319 dinfo->fr.rc.top += dinfo->line_height * 3; /* header height */
320 if (printing_prefs.print_page_numbers)
321 dinfo->fr.rc.bottom -= dinfo->line_height * 1; /* footer height */
323 dinfo->fr.rcPage.left /= dinfo->sci_scale;
324 dinfo->fr.rcPage.top /= dinfo->sci_scale;
325 dinfo->fr.rcPage.right /= dinfo->sci_scale;
326 dinfo->fr.rcPage.bottom /= dinfo->sci_scale;
327 dinfo->fr.rc.left /= dinfo->sci_scale;
328 dinfo->fr.rc.top /= dinfo->sci_scale;
329 dinfo->fr.rc.right /= dinfo->sci_scale;
330 dinfo->fr.rc.bottom /= dinfo->sci_scale;
332 dinfo->fr.chrg.cpMin = 0;
333 dinfo->fr.chrg.cpMax = sci_get_length(dinfo->sci);
337 static void begin_print(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
339 DocInfo *dinfo = user_data;
340 PangoContext *pango_ctx, *widget_pango_ctx;
341 PangoFontDescription *desc;
342 gdouble pango_res, widget_res;
344 if (dinfo == NULL)
345 return;
347 gtk_widget_show(main_widgets.progressbar);
349 /* init dinfo fields */
351 /* setup printing scintilla object */
352 dinfo->sci = editor_create_widget(dinfo->doc->editor);
353 /* since we won't add the widget to any container, assume it's ownership */
354 g_object_ref_sink(dinfo->sci);
355 scintilla_send_message(dinfo->sci, SCI_SETDOCPOINTER, 0,
356 scintilla_send_message(dinfo->doc->editor->sci, SCI_GETDOCPOINTER, 0, 0));
357 highlighting_set_styles(dinfo->sci, dinfo->doc->file_type);
358 sci_set_line_numbers(dinfo->sci, printing_prefs.print_line_numbers);
359 scintilla_send_message(dinfo->sci, SCI_SETVIEWWS, SCWS_INVISIBLE, 0);
360 scintilla_send_message(dinfo->sci, SCI_SETVIEWEOL, FALSE, 0);
361 scintilla_send_message(dinfo->sci, SCI_SETEDGEMODE, EDGE_NONE, 0);
362 scintilla_send_message(dinfo->sci, SCI_SETPRINTCOLOURMODE, SC_PRINT_COLOURONWHITE, 0);
364 /* Scintilla doesn't respect the context resolution, so we'll scale ourselves.
365 * Actually Scintilla simply doesn't know about the resolution since it creates its own
366 * Pango context out of the Cairo target, and the resolution is in the GtkPrintOperation's
367 * Pango context */
368 pango_ctx = gtk_print_context_create_pango_context(context);
369 pango_res = pango_cairo_context_get_resolution(pango_ctx);
370 g_object_unref(pango_ctx);
371 widget_pango_ctx = gtk_widget_get_pango_context(GTK_WIDGET(dinfo->sci));
372 widget_res = pango_cairo_context_get_resolution(widget_pango_ctx);
373 /* On Windows, for some reason the widget's resolution is -1, so follow
374 * Pango docs and peek the font map's one. */
375 if (widget_res < 0)
377 widget_res = pango_cairo_font_map_get_resolution(
378 (PangoCairoFontMap*) pango_context_get_font_map(widget_pango_ctx));
380 dinfo->sci_scale = pango_res / widget_res;
382 dinfo->pages = g_array_new(FALSE, FALSE, sizeof(gint));
384 dinfo->print_time = time(NULL);
385 /* create a PangoLayout to be commonly used in add_page_header() and draw_page() */
386 desc = pango_font_description_from_string(interface_prefs.editor_font);
387 dinfo->layout = setup_pango_layout(context, desc);
388 pango_font_description_free(desc);
389 get_text_dimensions(dinfo->layout, "|XMfjgq_" /* reasonably representative character set */,
390 NULL, &dinfo->line_height);
391 get_text_dimensions(dinfo->layout, "99999 " /* Scintilla resets the margin to the width of "99999" when printing */,
392 &dinfo->margin_width, NULL);
393 /* setup dinfo->fr */
394 setup_range(dinfo, context);
398 static gint format_range(DocInfo *dinfo, gboolean draw)
400 gint pos;
402 cairo_save(dinfo->fr.hdc);
403 cairo_scale(dinfo->fr.hdc, dinfo->sci_scale, dinfo->sci_scale);
404 pos = (gint) scintilla_send_message(dinfo->sci, SCI_FORMATRANGE, draw, (sptr_t) &dinfo->fr);
405 cairo_restore(dinfo->fr.hdc);
407 return pos;
411 static gboolean paginate(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
413 DocInfo *dinfo = user_data;
415 /* for whatever reason we get called one more time after we returned TRUE, so avoid adding
416 * an empty page at the end */
417 if (dinfo->fr.chrg.cpMin >= dinfo->fr.chrg.cpMax)
418 return TRUE;
420 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(main_widgets.progressbar));
421 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), _("Paginating"));
423 g_array_append_val(dinfo->pages, dinfo->fr.chrg.cpMin);
424 dinfo->fr.chrg.cpMin = format_range(dinfo, FALSE);
426 gtk_print_operation_set_n_pages(operation, dinfo->pages->len);
428 return dinfo->fr.chrg.cpMin >= dinfo->fr.chrg.cpMax;
432 static void draw_page(GtkPrintOperation *operation, GtkPrintContext *context,
433 gint page_nr, gpointer user_data)
435 DocInfo *dinfo = user_data;
436 cairo_t *cr;
437 gdouble width, height;
439 g_return_if_fail(dinfo != NULL);
440 g_return_if_fail((guint)page_nr < dinfo->pages->len);
442 if (dinfo->pages->len > 0)
444 gdouble fraction = (page_nr + 1) / (gdouble) dinfo->pages->len;
445 gchar *text = g_strdup_printf(_("Page %d of %d"), page_nr + 1, dinfo->pages->len);
446 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(main_widgets.progressbar), fraction);
447 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
448 g_free(text);
451 cr = gtk_print_context_get_cairo_context(context);
452 width = gtk_print_context_get_width(context);
453 height = gtk_print_context_get_height(context);
455 if (printing_prefs.print_page_header)
456 add_page_header(dinfo, cr, width, page_nr);
458 dinfo->fr.chrg.cpMin = g_array_index(dinfo->pages, gint, page_nr);
459 if ((guint)page_nr + 1 < dinfo->pages->len)
460 dinfo->fr.chrg.cpMax = g_array_index(dinfo->pages, gint, page_nr + 1) - 1;
461 else /* it's the last page, print 'til the end */
462 dinfo->fr.chrg.cpMax = sci_get_length(dinfo->sci);
464 format_range(dinfo, TRUE);
466 /* reset color */
467 cairo_set_source_rgb(cr, 0, 0, 0);
469 if (printing_prefs.print_line_numbers)
470 { /* print a thin line between the line number margin and the data */
471 gdouble y1 = dinfo->fr.rc.top * dinfo->sci_scale;
472 gdouble y2 = dinfo->fr.rc.bottom * dinfo->sci_scale;
473 gdouble x = dinfo->fr.rc.left * dinfo->sci_scale + dinfo->margin_width;
475 if (printing_prefs.print_page_header)
476 y1 -= 2 - 0.3; /* to connect the line number line to the page header frame,
477 * 2 is the border, and 0.3 the line width */
479 cairo_set_line_width(cr, 0.3);
480 cairo_move_to(cr, x, y1);
481 cairo_line_to(cr, x, y2);
482 cairo_stroke(cr);
485 if (printing_prefs.print_page_numbers)
487 gchar *line = g_strdup_printf("<small>- %d -</small>", page_nr + 1);
488 pango_layout_set_markup(dinfo->layout, line, -1);
489 pango_layout_set_alignment(dinfo->layout, PANGO_ALIGN_CENTER);
490 cairo_move_to(cr, 0, height - dinfo->line_height);
491 pango_cairo_show_layout(cr, dinfo->layout);
492 g_free(line);
497 static void status_changed(GtkPrintOperation *op, gpointer data)
499 gchar *filename = (data != NULL) ? data : GEANY_STRING_UNTITLED;
500 if (gtk_print_operation_get_status(op) == GTK_PRINT_STATUS_FINISHED_ABORTED)
501 msgwin_status_add(_("Did not send document %s to the printing subsystem."), filename);
502 else if (gtk_print_operation_get_status(op) == GTK_PRINT_STATUS_FINISHED)
503 msgwin_status_add(_("Document %s was sent to the printing subsystem."), filename);
507 static void printing_print_gtk(GeanyDocument *doc)
509 GtkPrintOperation *op;
510 GtkPrintOperationResult res = GTK_PRINT_OPERATION_RESULT_ERROR;
511 GError *error = NULL;
512 static const DocInfo dinfo0;
513 DocInfo dinfo = dinfo0;
514 PrintWidgets *widgets;
516 /** TODO check for monospace font, detect the widest character in the font and
517 * use it at font_width */
519 widgets = g_new0(PrintWidgets, 1);
520 /* all other fields are initialised in begin_print() */
521 dinfo.doc = doc;
523 op = gtk_print_operation_new();
525 gtk_print_operation_set_unit(op, GTK_UNIT_POINTS);
526 gtk_print_operation_set_show_progress(op, TRUE);
527 gtk_print_operation_set_embed_page_setup(op, TRUE);
529 g_signal_connect(op, "begin-print", G_CALLBACK(begin_print), &dinfo);
530 g_signal_connect(op, "end-print", G_CALLBACK(end_print), &dinfo);
531 g_signal_connect(op, "paginate", G_CALLBACK(paginate), &dinfo);
532 g_signal_connect(op, "draw-page", G_CALLBACK(draw_page), &dinfo);
533 g_signal_connect(op, "status-changed", G_CALLBACK(status_changed), doc->file_name);
534 g_signal_connect(op, "create-custom-widget", G_CALLBACK(create_custom_widget), widgets);
535 g_signal_connect(op, "custom-widget-apply", G_CALLBACK(custom_widget_apply), widgets);
537 if (settings != NULL)
538 gtk_print_operation_set_print_settings(op, settings);
539 if (page_setup != NULL)
540 gtk_print_operation_set_default_page_setup(op, page_setup);
542 res = gtk_print_operation_run(
543 op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW(main_widgets.window), &error);
545 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
547 if (settings != NULL)
548 g_object_unref(settings);
549 settings = g_object_ref(gtk_print_operation_get_print_settings(op));
550 /* status message is printed in the status-changed handler */
552 else if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
554 dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Printing of %s failed (%s)."),
555 doc->file_name, error->message);
556 g_error_free(error);
559 g_object_unref(op);
560 g_free(widgets);
564 void printing_page_setup_gtk(void)
566 GtkPageSetup *new_page_setup;
568 if (settings == NULL)
569 settings = gtk_print_settings_new();
571 new_page_setup = gtk_print_run_page_setup_dialog(
572 GTK_WINDOW(main_widgets.window), page_setup, settings);
574 if (page_setup != NULL)
575 g_object_unref(page_setup);
577 page_setup = new_page_setup;
581 /* simple file print using an external tool */
582 static void print_external(GeanyDocument *doc)
584 gchar *cmdline;
586 if (doc->file_name == NULL)
587 return;
589 if (EMPTY(printing_prefs.external_print_cmd))
591 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
592 _("Please set a print command in the preferences dialog first."));
593 return;
596 cmdline = g_strdup(printing_prefs.external_print_cmd);
597 utils_str_replace_all(&cmdline, "%f", doc->file_name);
599 if (dialogs_show_question(
600 _("The file \"%s\" will be printed with the following command:\n\n%s"),
601 doc->file_name, cmdline))
603 GError *error = NULL;
604 /* /bin/sh -c emulates the system() call and makes complex commands possible
605 * but only on non-win32 systems due to the lack of win32's shell capabilities */
606 #ifdef G_OS_UNIX
607 gchar *argv[] = { "/bin/sh", "-c", cmdline, NULL };
609 if (!spawn_async(NULL, NULL, argv, NULL, NULL, &error))
610 #else
611 if (!spawn_async(NULL, cmdline, NULL, NULL, NULL, &error))
612 #endif
614 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
615 _("Printing of \"%s\" failed (return code: %s)."),
616 doc->file_name, error->message);
617 g_error_free(error);
619 else
621 msgwin_status_add(_("File %s printed."), doc->file_name);
624 g_free(cmdline);
628 void printing_print_doc(GeanyDocument *doc)
630 g_return_if_fail(DOC_VALID(doc));
632 if (printing_prefs.use_gtk_printing)
633 printing_print_gtk(doc);
634 else
635 print_external(doc);