Add 'Refresh' popup menu item (part of geany-plugins #2999858).
[geany-mirror.git] / src / printing.c
blob7b6aec190cf18432edfe62a92a46b79ad5cbe2d8
1 /*
2 * printing.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $Id$
26 * GTK 2.10 printing support
27 * (basic code layout were adopted from Sylpheed's printing implementation, thanks)
30 #include <math.h>
31 #include <time.h>
32 #include <string.h>
34 #include "geany.h"
35 #include "printing.h"
36 #include "document.h"
37 #include "sciwrappers.h"
38 #include "editor.h"
39 #include "sciwrappers.h"
40 #include "utils.h"
41 #include "support.h"
42 #include "dialogs.h"
43 #include "utils.h"
44 #include "ui_utils.h"
45 #include "msgwindow.h"
48 PrintingPrefs printing_prefs;
51 #if GTK_CHECK_VERSION(2, 10, 0)
54 #define ROTATE_RGB(color) \
55 (((color) & 0xFF0000) >> 16) + ((color) & 0x00FF00) + (((color) & 0x0000FF) << 16)
56 #define ADD_ATTR(l, a) \
57 pango_attr_list_insert((l), (a)); \
58 (a)->start_index = 0; \
59 (a)->end_index = -1;
62 enum
64 FORE = 0,
65 BACK,
66 BOLD,
67 ITALIC,
68 MAX_TYPES
72 /* document-related variables */
73 typedef struct
75 GeanyDocument *doc;
76 gint font_width;
77 gint lines;
78 gint n_pages;
79 gint lines_per_page;
80 gint max_line_number_margin;
81 gint cur_line;
82 gint cur_pos;
83 gint styles[STYLE_MAX + 1][MAX_TYPES];
84 gdouble line_height;
85 /* whether we have a wrapped line on page end to take care of on next page */
86 gboolean long_line;
87 /* set in begin_print() to hold the time when printing was started to ensure all printed
88 * pages have the same date and time (in case of slow machines and many pages where rendering
89 * takes more than a second) */
90 time_t print_time;
91 PangoLayout *layout; /* commonly used layout object */
92 } DocInfo;
94 /* widget references for the custom widget in the print dialog */
95 typedef struct
97 GtkWidget *check_print_linenumbers;
98 GtkWidget *check_print_pagenumbers;
99 GtkWidget *check_print_pageheader;
100 GtkWidget *check_print_basename;
101 GtkWidget *entry_print_dateformat;
102 } PrintWidgets;
105 static GtkPrintSettings *settings = NULL;
106 static GtkPageSetup *page_setup = NULL;
110 /* returns the "width" (count of needed characters) for the given number */
111 static gint get_line_numbers_arity(gint x)
113 gint a = 0;
114 while ((x /= 10) != 0)
115 a++;
116 return a;
120 /* split a RGB colour into the three colour components */
121 static void get_rgb_values(gint c, gint *r, gint *g, gint *b)
123 c = ROTATE_RGB(c);
124 if (interface_prefs.highlighting_invert_all)
125 c = utils_invert_color(c);
127 *r = c % 256;
128 *g = (c & - 16711936) / 256;
129 *b = (c & 0xff0000) / 65536;
131 *r *= 257;
132 *g *= 257;
133 *b *= 257;
137 /* creates a commonly used layout object from the given context for use in get_page_count and
138 * draw_page */
139 static PangoLayout *setup_pango_layout(GtkPrintContext *context, PangoFontDescription *desc)
141 PangoLayout *layout;
143 layout = gtk_print_context_create_pango_layout(context);
144 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
145 pango_layout_set_spacing(layout, 0);
146 pango_layout_set_attributes(layout, NULL);
147 pango_layout_set_font_description(layout, desc);
149 return layout;
153 static gboolean utils_font_desc_check_monospace(PangoContext *pc, PangoFontDescription *desc)
155 PangoFontFamily **families;
156 gint n_families, i;
157 const gchar *font;
158 gboolean ret = TRUE;
160 font = pango_font_description_get_family(desc);
161 pango_context_list_families(pc, &families, &n_families);
162 for (i = 0; i < n_families; i++)
164 if (utils_str_equal(font, pango_font_family_get_name(families[i])))
166 if (!pango_font_family_is_monospace(families[i]))
168 ret = FALSE;
172 g_free(families);
173 return ret;
177 /* We don't support variable width fonts (yet) */
178 static gint get_font_width(GtkPrintContext *context, PangoFontDescription *desc)
180 PangoContext *pc;
181 PangoFontMetrics *metrics;
182 gint width;
184 pc = gtk_print_context_create_pango_context(context);
186 if (!utils_font_desc_check_monospace(pc, desc))
187 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_WARNING,
188 _("The editor font is not a monospaced font!"),
189 _("Text will be wrongly spaced."));
191 metrics = pango_context_get_metrics(pc, desc, pango_context_get_language(pc));
192 /** TODO is this the best result we can get? */
193 /* digit and char width are mostly equal for monospace fonts, char width might be
194 * for dual width characters(e.g. Japanese) so use digit width to get sure we get the width
195 * for one character */
196 width = pango_font_metrics_get_approximate_digit_width(metrics) / PANGO_SCALE;
198 pango_font_metrics_unref(metrics);
199 g_object_unref(pc);
201 return width;
205 static gint get_page_count(GtkPrintContext *context, DocInfo *dinfo)
207 gdouble width, height;
208 gint layout_h;
209 gint i, j, lines_left;
210 gchar *line_buf;
212 if (dinfo == NULL)
213 return -1;
215 width = gtk_print_context_get_width(context);
216 height = gtk_print_context_get_height(context);
218 if (printing_prefs.print_line_numbers)
219 /* remove line number margin space from overall width */
220 width -= dinfo->max_line_number_margin * dinfo->font_width;
222 pango_layout_set_width(dinfo->layout, width * PANGO_SCALE);
224 /* add test text to get line height */
225 pango_layout_set_text(dinfo->layout, "Test 1", -1);
226 pango_layout_get_size(dinfo->layout, NULL, &layout_h);
227 if (layout_h <= 0)
229 geany_debug("Invalid layout_h (%d). Falling back to default height (%d)",
230 layout_h, 100 * PANGO_SCALE);
231 layout_h = 100 * PANGO_SCALE;
233 dinfo->line_height = (gdouble)layout_h / PANGO_SCALE;
234 dinfo->lines_per_page = ceil((height - dinfo->line_height) / dinfo->line_height);
235 #ifdef GEANY_PRINT_DEBUG
236 geany_debug("max lines_per_page: %d", dinfo->lines_per_page);
237 #endif
238 if (printing_prefs.print_page_numbers)
239 dinfo->lines_per_page -= 2;
240 if (printing_prefs.print_page_header)
241 dinfo->lines_per_page -= 3;
243 lines_left = dinfo->lines_per_page;
245 i = 0;
246 for (j = 0; j < dinfo->lines; j++)
248 gint lines = 1;
249 gint line_width;
251 line_buf = sci_get_line(dinfo->doc->editor->sci, j);
252 line_width = (g_utf8_strlen(line_buf, -1) + 1) * dinfo->font_width;
253 if (line_width > width)
254 lines = ceil(line_width / width);
255 #ifdef GEANY_PRINT_DEBUG
256 if (lines != 1) geany_debug("%d %d", j+1, lines);
257 #endif
259 while (lines_left < lines)
261 lines -= lines_left;
262 lines_left = dinfo->lines_per_page;
263 i++;
265 lines_left -= lines;
266 g_free(line_buf);
269 return i + 1;
273 static void add_page_header(DocInfo *dinfo, cairo_t *cr, gint width, gint page_nr)
275 gint ph_height = dinfo->line_height * 3;
276 gchar *data;
277 gchar *datetime;
278 gchar *tmp_file_name = (dinfo->doc->file_name != NULL) ?
279 dinfo->doc->file_name : GEANY_STRING_UNTITLED;
280 gchar *file_name = (printing_prefs.page_header_basename) ?
281 g_path_get_basename(tmp_file_name) : g_strdup(tmp_file_name);
282 PangoLayout *layout = dinfo->layout;
284 /* draw the frame */
285 cairo_set_line_width(cr, 0.3);
286 cairo_set_source_rgb(cr, 0, 0, 0);
287 cairo_rectangle(cr, 2, 2, width - 4, ph_height - 4);
288 cairo_stroke(cr);
290 /* width - 8: 2px between doc border and frame border, 2px between frame border and text
291 * and this on left and right side, so (2 + 2) * 2 */
292 pango_layout_set_width(layout, (width - 8) * PANGO_SCALE);
294 if ((g_utf8_strlen(file_name, -1) * dinfo->font_width) >= ((width - 4) - (dinfo->font_width * 2)))
295 /* if the filename is wider than the available space on the line, skip parts of it */
296 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_MIDDLE);
298 data = g_strdup_printf("<b>%s</b>", file_name);
299 pango_layout_set_markup(layout, data, -1);
300 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
301 cairo_move_to(cr, 4, dinfo->line_height * 0.5);
302 pango_cairo_show_layout(cr, layout);
303 g_free(data);
304 g_free(file_name);
306 data = g_strdup_printf(_("<b>Page %d of %d</b>"), page_nr + 1, dinfo->n_pages);
307 pango_layout_set_markup(layout, data, -1);
308 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
309 cairo_move_to(cr, 4, dinfo->line_height * 1.5);
310 pango_cairo_show_layout(cr, layout);
311 g_free(data);
313 datetime = utils_get_date_time(printing_prefs.page_header_datefmt, &(dinfo->print_time));
314 if (NZV(datetime))
316 data = g_strdup_printf("<b>%s</b>", datetime);
317 pango_layout_set_markup(layout, data, -1);
318 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
319 cairo_move_to(cr, 2, dinfo->line_height * 1.5);
320 pango_cairo_show_layout(cr, layout);
321 g_free(data);
323 g_free(datetime);
325 /* reset layout and re-position cairo context */
326 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
327 pango_layout_set_ellipsize(layout, FALSE);
328 pango_layout_set_justify(layout, FALSE);
329 pango_layout_set_width(layout, width * PANGO_SCALE);
330 cairo_move_to(cr, 0, dinfo->line_height * 3);
334 static void custom_widget_apply(GtkPrintOperation *operation, GtkWidget *widget, gpointer user_data)
336 PrintWidgets *w = user_data;
338 printing_prefs.print_line_numbers =
339 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_linenumbers));
341 printing_prefs.print_page_numbers =
342 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_pagenumbers));
344 printing_prefs.print_page_header =
345 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_pageheader));
347 printing_prefs.page_header_basename =
348 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_basename));
350 g_free(printing_prefs.page_header_datefmt);
351 printing_prefs.page_header_datefmt =
352 g_strdup(gtk_entry_get_text(GTK_ENTRY(w->entry_print_dateformat)));
356 static void on_page_header_toggled(GtkToggleButton *togglebutton, gpointer user_data)
358 gboolean sens = gtk_toggle_button_get_active(togglebutton);
359 PrintWidgets *w = user_data;
361 gtk_widget_set_sensitive(w->check_print_basename, sens);
362 gtk_widget_set_sensitive(w->entry_print_dateformat, sens);
366 static GtkWidget *create_custom_widget(GtkPrintOperation *operation, gpointer user_data)
367 { /* copied from interface.c */
368 GtkWidget *page;
369 GtkWidget *frame33;
370 GtkWidget *alignment36;
371 GtkWidget *vbox30;
372 GtkWidget *hbox10;
373 GtkWidget *label203;
374 PrintWidgets *w = user_data;
376 gtk_print_operation_set_custom_tab_label(operation, _("Document Setup"));
378 page = gtk_vbox_new(FALSE, 0);
379 gtk_container_set_border_width(GTK_CONTAINER(page), 5);
381 w->check_print_linenumbers = gtk_check_button_new_with_mnemonic(_("Print line numbers"));
382 gtk_box_pack_start(GTK_BOX(page), w->check_print_linenumbers, FALSE, FALSE, 0);
383 ui_widget_set_tooltip_text(w->check_print_linenumbers, _("Add line numbers to the printed page"));
384 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_linenumbers), printing_prefs.print_line_numbers);
386 w->check_print_pagenumbers = gtk_check_button_new_with_mnemonic(_("Print page numbers"));
387 gtk_box_pack_start(GTK_BOX(page), w->check_print_pagenumbers, FALSE, FALSE, 0);
388 ui_widget_set_tooltip_text(w->check_print_pagenumbers, _("Add page numbers at the bottom of each page. It takes 2 lines of the page."));
389 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_pagenumbers), printing_prefs.print_page_numbers);
391 w->check_print_pageheader = gtk_check_button_new_with_mnemonic(_("Print page header"));
392 gtk_box_pack_start(GTK_BOX(page), w->check_print_pageheader, FALSE, FALSE, 0);
393 ui_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."));
394 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_pageheader), printing_prefs.print_page_header);
395 g_signal_connect(w->check_print_pageheader, "toggled", G_CALLBACK(on_page_header_toggled), w);
397 frame33 = gtk_frame_new(NULL);
398 gtk_box_pack_start(GTK_BOX(page), frame33, FALSE, FALSE, 0);
399 gtk_frame_set_label_align(GTK_FRAME(frame33), 0, 0);
400 gtk_frame_set_shadow_type(GTK_FRAME(frame33), GTK_SHADOW_NONE);
402 alignment36 = gtk_alignment_new(0, 0.5, 1, 1);
403 gtk_container_add(GTK_CONTAINER(frame33), alignment36);
404 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment36), 0, 0, 12, 0);
406 vbox30 = gtk_vbox_new(FALSE, 1);
407 gtk_container_add(GTK_CONTAINER(alignment36), vbox30);
409 w->check_print_basename = gtk_check_button_new_with_mnemonic(_("Use the basename of the printed file"));
410 gtk_box_pack_start(GTK_BOX(vbox30), w->check_print_basename, FALSE, FALSE, 0);
411 ui_widget_set_tooltip_text(w->check_print_basename, _("Print only the basename(without the path) of the printed file"));
412 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_basename), printing_prefs.page_header_basename);
414 hbox10 = gtk_hbox_new(FALSE, 5);
415 gtk_box_pack_start(GTK_BOX(vbox30), hbox10, TRUE, TRUE, 0);
417 label203 = gtk_label_new(_("Date format:"));
418 gtk_box_pack_start(GTK_BOX(hbox10), label203, FALSE, FALSE, 0);
420 w->entry_print_dateformat = gtk_entry_new();
421 ui_entry_add_clear_icon(GTK_ENTRY(w->entry_print_dateformat));
422 gtk_box_pack_start(GTK_BOX(hbox10), w->entry_print_dateformat, TRUE, TRUE, 0);
423 ui_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."));
424 gtk_entry_set_text(GTK_ENTRY(w->entry_print_dateformat), printing_prefs.page_header_datefmt);
426 on_page_header_toggled(GTK_TOGGLE_BUTTON(w->check_print_pageheader), w);
427 gtk_widget_show_all(page);
428 return page;
432 static void end_print(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
434 DocInfo *dinfo = user_data;
436 if (dinfo == NULL)
437 return;
439 gtk_widget_hide(main_widgets.progressbar);
440 g_object_unref(dinfo->layout);
444 static void begin_print(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
446 DocInfo *dinfo = user_data;
447 PangoFontDescription *desc;
448 gint i;
449 gint style_max;
451 if (dinfo == NULL)
452 return;
454 gtk_widget_show(main_widgets.progressbar);
456 desc = pango_font_description_from_string(interface_prefs.editor_font);
458 /* init dinfo fields */
459 dinfo->lines = sci_get_line_count(dinfo->doc->editor->sci);
460 dinfo->lines_per_page = 0;
461 dinfo->cur_line = 0;
462 dinfo->cur_pos = 0;
463 dinfo->long_line = FALSE;
464 dinfo->print_time = time(NULL);
465 dinfo->max_line_number_margin = get_line_numbers_arity(dinfo->lines) + 1;
466 /* increase font width by 1 (looks better) */
467 dinfo->font_width = get_font_width(context, desc) + 1;
468 /* create a PangoLayout to be commonly used in get_page_count and draw_page */
469 dinfo->layout = setup_pango_layout(context, desc);
470 /* this is necessary because of possible line breaks on the printed page and then
471 * lines_per_page differs from document line count */
472 dinfo->n_pages = get_page_count(context, dinfo);
474 /* read all styles from Scintilla */
475 style_max = pow(2, scintilla_send_message(dinfo->doc->editor->sci, SCI_GETSTYLEBITS, 0, 0));
476 /* if the lexer uses only the first 32 styles(style bits = 5),
477 * we need to add the pre-defined styles */
478 if (style_max == 32)
479 style_max = STYLE_LASTPREDEFINED;
480 for (i = 0; i < style_max; i++)
482 dinfo->styles[i][FORE] = ROTATE_RGB(scintilla_send_message(
483 dinfo->doc->editor->sci, SCI_STYLEGETFORE, i, 0));
484 if (i == STYLE_LINENUMBER)
485 { /* ignore background colour for line number margin to avoid trouble with wrapped lines */
486 dinfo->styles[STYLE_LINENUMBER][BACK] = ROTATE_RGB(scintilla_send_message(
487 dinfo->doc->editor->sci, SCI_STYLEGETBACK, STYLE_DEFAULT, 0));
489 else
491 dinfo->styles[i][BACK] = ROTATE_RGB(scintilla_send_message(
492 dinfo->doc->editor->sci, SCI_STYLEGETBACK, i, 0));
494 dinfo->styles[i][BOLD] =
495 scintilla_send_message(dinfo->doc->editor->sci, SCI_STYLEGETBOLD, i, 0);
496 dinfo->styles[i][ITALIC] =
497 scintilla_send_message(dinfo->doc->editor->sci, SCI_STYLEGETITALIC, i, 0);
500 if (dinfo->n_pages >= 0)
501 gtk_print_operation_set_n_pages(operation, dinfo->n_pages);
503 pango_font_description_free(desc);
507 static void draw_page(GtkPrintOperation *operation, GtkPrintContext *context,
508 gint page_nr, gpointer user_data)
510 DocInfo *dinfo = user_data;
511 GeanyEditor *editor;
512 cairo_t *cr;
513 gdouble width, height;
514 gdouble x, y;
515 /*gint layout_h;*/
516 gint count;
517 GString *str;
519 if (dinfo == NULL || page_nr >= dinfo->n_pages)
520 return;
522 editor = dinfo->doc->editor;
524 if (dinfo->n_pages > 0)
526 gdouble fraction = (page_nr + 1) / (gdouble) dinfo->n_pages;
527 gchar *text = g_strdup_printf(_("Page %d of %d"), page_nr, dinfo->n_pages);
528 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(main_widgets.progressbar), fraction);
529 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
530 g_free(text);
533 #ifdef GEANY_PRINT_DEBUG
534 geany_debug("draw_page = %d, pages = %d, (real) lines_per_page = %d",
535 page_nr, dinfo->n_pages, dinfo->lines_per_page);
536 #endif
538 str = g_string_sized_new(256);
539 cr = gtk_print_context_get_cairo_context(context);
540 width = gtk_print_context_get_width(context);
541 height = gtk_print_context_get_height(context);
543 cairo_set_source_rgb(cr, 0, 0, 0);
544 #ifdef GEANY_PRINT_DEBUG
545 cairo_set_line_width(cr, 0.2);
546 cairo_rectangle(cr, 0, 0, width, height);
547 cairo_stroke(cr);
548 #endif
549 cairo_move_to(cr, 0, 0);
551 pango_layout_set_width(dinfo->layout, width * PANGO_SCALE);
552 pango_layout_set_alignment(dinfo->layout, PANGO_ALIGN_LEFT);
553 pango_layout_set_ellipsize(dinfo->layout, FALSE);
554 pango_layout_set_justify(dinfo->layout, FALSE);
556 if (printing_prefs.print_page_header)
557 add_page_header(dinfo, cr, width, page_nr);
559 count = 0; /* the actual line counter for the current page, might be different from
560 * dinfo->cur_line due to possible line breaks */
561 while (count < dinfo->lines_per_page)
563 gchar c = 'a';
564 gint style = -1;
565 PangoAttrList *layout_attr;
566 PangoAttribute *attr;
567 gint colours[3] = { 0 };
568 gboolean add_linenumber = TRUE;
569 gboolean at_eol;
571 while (count < dinfo->lines_per_page && c != '\0')
573 at_eol = FALSE;
575 g_string_erase(str, 0, str->len); /* clear the string */
577 /* line numbers */
578 if (printing_prefs.print_line_numbers && add_linenumber)
580 /* if we had a wrapped line on the last page which needs to be continued, don't
581 * add a line number */
582 if (dinfo->long_line)
584 add_linenumber = FALSE;
586 else
588 gchar *line_number = NULL;
589 gint cur_line_number_margin = get_line_numbers_arity(dinfo->cur_line + 1);
590 gchar *fill = g_strnfill(
591 dinfo->max_line_number_margin - cur_line_number_margin - 1, ' ');
593 line_number = g_strdup_printf("%s%d ", fill, dinfo->cur_line + 1);
594 g_string_append(str, line_number);
595 dinfo->cur_line++; /* increase document line */
596 add_linenumber = FALSE;
597 style = STYLE_LINENUMBER;
598 c = 'a'; /* dummy value */
599 g_free(fill);
600 g_free(line_number);
603 /* data */
604 else
606 style = sci_get_style_at(dinfo->doc->editor->sci, dinfo->cur_pos);
607 c = sci_get_char_at(dinfo->doc->editor->sci, dinfo->cur_pos);
608 if (c == '\0' || style == -1)
609 { /* if c gets 0, we are probably out of document boundaries,
610 * so stop to break out of outer loop */
611 count = dinfo->lines_per_page;
612 break;
614 dinfo->cur_pos++;
616 /* convert tabs to spaces which seems to be better than using Pango tabs */
617 if (c == '\t')
619 gint tab_width = sci_get_tab_width(editor->sci);
620 gchar *s = g_strnfill(tab_width, ' ');
621 g_string_append(str, s);
622 g_free(s);
624 /* don't add line breaks, they are handled manually below */
625 else if (c == '\r' || c == '\n')
627 gchar c_next = sci_get_char_at(dinfo->doc->editor->sci, dinfo->cur_pos);
628 at_eol = TRUE;
629 if (c == '\r' && c_next == '\n')
630 dinfo->cur_pos++; /* skip LF part of CR/LF */
632 else
634 g_string_append_c(str, c); /* finally add the character */
636 /* handle UTF-8: since we add char by char (better: byte by byte), we need to
637 * keep UTF-8 characters together(e.g. two bytes for one character)
638 * the input is always UTF-8 and c is signed, so all non-Ascii
639 * characters are less than 0 and consist of all bytes less than 0.
640 * style doesn't change since it is only one character with multiple bytes. */
641 while (c < 0)
643 c = sci_get_char_at(dinfo->doc->editor->sci, dinfo->cur_pos);
644 if (c < 0)
645 { /* only add the byte when it is part of the UTF-8 character
646 * otherwise we could add e.g. a '\n' and it won't be visible in the
647 * printed document */
648 g_string_append_c(str, c);
649 dinfo->cur_pos++;
655 if (! at_eol)
657 /* set text */
658 pango_layout_set_text(dinfo->layout, str->str, -1);
659 /* attributes */
660 layout_attr = pango_attr_list_new();
661 /* foreground colour */
662 get_rgb_values(dinfo->styles[style][FORE], &colours[0], &colours[1], &colours[2]);
663 attr = pango_attr_foreground_new(colours[0], colours[1], colours[2]);
664 ADD_ATTR(layout_attr, attr);
665 /* background colour */
666 get_rgb_values(dinfo->styles[style][BACK], &colours[0], &colours[1], &colours[2]);
667 attr = pango_attr_background_new(colours[0], colours[1], colours[2]);
668 ADD_ATTR(layout_attr, attr);
669 /* bold text */
670 if (dinfo->styles[style][BOLD])
672 attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
673 ADD_ATTR(layout_attr, attr);
675 /* italic text */
676 if (dinfo->styles[style][ITALIC])
678 attr = pango_attr_style_new(PANGO_STYLE_ITALIC);
679 ADD_ATTR(layout_attr, attr);
681 pango_layout_set_attributes(dinfo->layout, layout_attr);
682 pango_layout_context_changed(dinfo->layout);
683 pango_attr_list_unref(layout_attr);
686 cairo_get_current_point(cr, &x, &y);
689 /* normal line break at eol character in document */
690 if (at_eol)
692 /*pango_layout_get_size(dinfo->layout, NULL, &layout_h);*/
693 /*cairo_move_to(cr, 0, y + (gdouble)layout_h / PANGO_SCALE);*/
694 cairo_move_to(cr, 0, y + dinfo->line_height);
696 count++;
697 /* we added a new document line so request a new line number */
698 add_linenumber = TRUE;
700 else
702 gint x_offset = 0;
703 /* maybe we need to force a line break because of too long line */
704 if (x >= (width - dinfo->font_width))
706 /* don't start the line at horizontal origin because we need to skip the
707 * line number margin */
708 if (printing_prefs.print_line_numbers)
710 x_offset = (dinfo->max_line_number_margin + 1) * dinfo->font_width;
713 /*pango_layout_get_size(dinfo->layout, NULL, &layout_h);*/
714 /*cairo_move_to(cr, x_offset, y + (gdouble)layout_h / PANGO_SCALE);*/
715 /* this is faster but not exactly the same as above */
716 cairo_move_to(cr, x_offset, y + dinfo->line_height);
717 cairo_get_current_point(cr, &x, &y);
718 count++;
720 if (count < dinfo->lines_per_page)
722 /* str->len is counted in bytes not characters, so use g_utf8_strlen() */
723 x_offset = (g_utf8_strlen(str->str, -1) * dinfo->font_width);
725 if (dinfo->long_line && count == 0)
727 x_offset = (dinfo->max_line_number_margin + 1) * dinfo->font_width;
728 dinfo->long_line = FALSE;
731 pango_cairo_show_layout(cr, dinfo->layout);
732 cairo_move_to(cr, x + x_offset, y);
734 else
735 /* we are on a wrapped line but we are out of lines on this page, so continue
736 * the current line on the next page and remember to continue in current line */
737 dinfo->long_line = TRUE;
742 if (printing_prefs.print_line_numbers)
743 { /* print a thin line between the line number margin and the data */
744 gint y_start = 0;
746 if (printing_prefs.print_page_header)
747 y_start = (dinfo->line_height * 3) - 2; /* "- 2": to connect the line number line to
748 * the page header frame */
750 cairo_set_line_width(cr, 0.3);
751 cairo_move_to(cr, (dinfo->max_line_number_margin * dinfo->font_width) + 1, y_start);
752 cairo_line_to(cr, (dinfo->max_line_number_margin * dinfo->font_width) + 1,
753 y + dinfo->line_height); /* y is last added line, we reuse it */
754 cairo_stroke(cr);
757 if (printing_prefs.print_page_numbers)
759 gchar *line = g_strdup_printf("<small>- %d -</small>", page_nr + 1);
760 pango_layout_set_markup(dinfo->layout, line, -1);
761 pango_layout_set_alignment(dinfo->layout, PANGO_ALIGN_CENTER);
762 cairo_move_to(cr, 0, height - dinfo->line_height);
763 pango_cairo_show_layout(cr, dinfo->layout);
764 g_free(line);
766 #ifdef GEANY_PRINT_DEBUG
767 cairo_set_line_width(cr, 0.3);
768 cairo_move_to(cr, 0, height - (1.25 * dinfo->line_height));
769 cairo_line_to(cr, width - 1, height - (1.25 * dinfo->line_height));
770 cairo_stroke(cr);
771 #endif
773 g_string_free(str, TRUE);
777 static void status_changed(GtkPrintOperation *op, gpointer data)
779 gchar *filename = (data != NULL) ? data : GEANY_STRING_UNTITLED;
780 if (gtk_print_operation_get_status(op) == GTK_PRINT_STATUS_FINISHED_ABORTED)
781 msgwin_status_add(_("Did not send document %s to the printing subsystem."), filename);
782 else if (gtk_print_operation_get_status(op) == GTK_PRINT_STATUS_FINISHED)
783 msgwin_status_add(_("Document %s was sent to the printing subsystem."), filename);
787 static void printing_print_gtk(GeanyDocument *doc)
789 GtkPrintOperation *op;
790 GtkPrintOperationResult res = GTK_PRINT_OPERATION_RESULT_ERROR;
791 GError *error = NULL;
792 DocInfo *dinfo;
793 PrintWidgets *widgets;
795 /** TODO check for monospace font, detect the widest character in the font and
796 * use it at font_width */
798 widgets = g_new0(PrintWidgets, 1);
799 dinfo = g_new0(DocInfo, 1);
800 /* all other fields are initialised in begin_print() */
801 dinfo->doc = doc;
803 op = gtk_print_operation_new();
805 gtk_print_operation_set_unit(op, GTK_UNIT_POINTS);
806 gtk_print_operation_set_show_progress(op, TRUE);
807 #if GTK_CHECK_VERSION(2, 18, 0)
808 gtk_print_operation_set_embed_page_setup(op, TRUE);
809 #endif
811 g_signal_connect(op, "begin-print", G_CALLBACK(begin_print), dinfo);
812 g_signal_connect(op, "end-print", G_CALLBACK(end_print), dinfo);
813 g_signal_connect(op, "draw-page", G_CALLBACK(draw_page), dinfo);
814 g_signal_connect(op, "status-changed", G_CALLBACK(status_changed), doc->file_name);
815 g_signal_connect(op, "create-custom-widget", G_CALLBACK(create_custom_widget), widgets);
816 g_signal_connect(op, "custom-widget-apply", G_CALLBACK(custom_widget_apply), widgets);
818 if (settings != NULL)
819 gtk_print_operation_set_print_settings(op, settings);
820 if (page_setup != NULL)
821 gtk_print_operation_set_default_page_setup(op, page_setup);
823 res = gtk_print_operation_run(
824 op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW(main_widgets.window), &error);
826 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
828 if (settings != NULL)
829 g_object_unref(settings);
830 settings = g_object_ref(gtk_print_operation_get_print_settings(op));
831 /* status message is printed in the status-changed handler */
833 else if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
835 dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Printing of %s failed (%s)."),
836 doc->file_name, error->message);
837 g_error_free(error);
840 g_object_unref(op);
841 g_free(dinfo);
842 g_free(widgets);
846 void printing_page_setup_gtk(void)
848 GtkPageSetup *new_page_setup;
850 if (settings == NULL)
851 settings = gtk_print_settings_new();
853 new_page_setup = gtk_print_run_page_setup_dialog(
854 GTK_WINDOW(main_widgets.window), page_setup, settings);
856 if (page_setup != NULL)
857 g_object_unref(page_setup);
859 page_setup = new_page_setup;
861 #endif /* GTK 2.10 */
864 /* simple file print using an external tool */
865 static void print_external(GeanyDocument *doc)
867 gchar *cmdline;
869 if (doc->file_name == NULL)
870 return;
872 if (! NZV(printing_prefs.external_print_cmd))
874 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
875 _("Please set a print command in the preferences dialog first."));
876 return;
879 cmdline = g_strdup(printing_prefs.external_print_cmd);
880 utils_str_replace_all(&cmdline, "%f", doc->file_name);
882 if (dialogs_show_question(
883 _("The file \"%s\" will be printed with the following command:\n\n%s"),
884 doc->file_name, cmdline))
886 GError *error = NULL;
888 #ifdef G_OS_WIN32
889 gchar *tmp_cmdline = g_strdup(cmdline);
890 #else
891 /* /bin/sh -c emulates the system() call and makes complex commands possible
892 * but only needed on non-win32 systems due to the lack of win32's shell capabilities */
893 gchar *tmp_cmdline = g_strconcat("/bin/sh -c \"", cmdline, "\"", NULL);
894 #endif
896 if (! g_spawn_command_line_async(tmp_cmdline, &error))
898 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
899 _("Printing of \"%s\" failed (return code: %s)."),
900 doc->file_name, error->message);
901 g_error_free(error);
903 else
905 msgwin_status_add(_("File %s printed."), doc->file_name);
907 g_free(tmp_cmdline);
909 g_free(cmdline);
913 void printing_print_doc(GeanyDocument *doc)
915 if (doc == NULL)
916 return;
918 #if GTK_CHECK_VERSION(2, 10, 0)
919 if (gtk_check_version(2, 10, 0) == NULL && printing_prefs.use_gtk_printing)
920 printing_print_gtk(doc);
921 else
922 #endif
923 print_external(doc);