Remove duplicate NEWS item
[geany-mirror.git] / src / printing.c
blob4dae968afe2e044eeee89c4b2c7089dc7354366c
1 /*
2 * printing.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2011 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.
24 * GTK printing support
25 * (basic code layout were adopted from Sylpheed's printing implementation, thanks)
28 #include <math.h>
29 #include <time.h>
30 #include <string.h>
32 #include "geany.h"
33 #include "printing.h"
34 #include "document.h"
35 #include "sciwrappers.h"
36 #include "editor.h"
37 #include "sciwrappers.h"
38 #include "utils.h"
39 #include "support.h"
40 #include "dialogs.h"
41 #include "utils.h"
42 #include "ui_utils.h"
43 #include "msgwindow.h"
46 PrintingPrefs printing_prefs;
49 #define ROTATE_RGB(color) \
50 (((color) & 0xFF0000) >> 16) + ((color) & 0x00FF00) + (((color) & 0x0000FF) << 16)
51 #define ADD_ATTR(l, a) \
52 pango_attr_list_insert((l), (a)); \
53 (a)->start_index = 0; \
54 (a)->end_index = G_MAXUINT;
57 enum
59 FORE = 0,
60 BACK,
61 BOLD,
62 ITALIC,
63 MAX_TYPES
67 /* document-related variables */
68 typedef struct
70 GeanyDocument *doc;
71 gint font_width;
72 gint lines;
73 gint n_pages;
74 gint lines_per_page;
75 gint max_line_number_margin;
76 gint cur_line;
77 gint cur_pos;
78 gint styles[STYLE_MAX + 1][MAX_TYPES];
79 gdouble line_height;
80 /* whether we have a wrapped line on page end to take care of on next page */
81 gboolean long_line;
82 /* set in begin_print() to hold the time when printing was started to ensure all printed
83 * pages have the same date and time (in case of slow machines and many pages where rendering
84 * takes more than a second) */
85 time_t print_time;
86 PangoLayout *layout; /* commonly used layout object */
87 } DocInfo;
89 /* widget references for the custom widget in the print dialog */
90 typedef struct
92 GtkWidget *check_print_linenumbers;
93 GtkWidget *check_print_pagenumbers;
94 GtkWidget *check_print_pageheader;
95 GtkWidget *check_print_basename;
96 GtkWidget *entry_print_dateformat;
97 } PrintWidgets;
100 static GtkPrintSettings *settings = NULL;
101 static GtkPageSetup *page_setup = NULL;
105 /* returns the "width" (count of needed characters) for the given number */
106 static gint get_line_numbers_arity(gint x)
108 gint a = 0;
109 while ((x /= 10) != 0)
110 a++;
111 return a;
115 /* split a RGB colour into the three colour components */
116 static void get_rgb_values(gint c, gint *r, gint *g, gint *b)
118 c = ROTATE_RGB(c);
119 if (interface_prefs.highlighting_invert_all)
120 c = utils_invert_color(c);
122 *r = c % 256;
123 *g = (c & - 16711936) / 256;
124 *b = (c & 0xff0000) / 65536;
126 *r *= 257;
127 *g *= 257;
128 *b *= 257;
132 /* creates a commonly used layout object from the given context for use in get_page_count and
133 * draw_page */
134 static PangoLayout *setup_pango_layout(GtkPrintContext *context, PangoFontDescription *desc)
136 PangoLayout *layout;
138 layout = gtk_print_context_create_pango_layout(context);
139 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
140 pango_layout_set_spacing(layout, 0);
141 pango_layout_set_attributes(layout, NULL);
142 pango_layout_set_font_description(layout, desc);
144 return layout;
148 static gboolean utils_font_desc_check_monospace(PangoContext *pc, PangoFontDescription *desc)
150 PangoFontFamily **families;
151 gint n_families, i;
152 const gchar *font;
153 gboolean ret = TRUE;
155 font = pango_font_description_get_family(desc);
156 pango_context_list_families(pc, &families, &n_families);
157 for (i = 0; i < n_families; i++)
159 if (utils_str_equal(font, pango_font_family_get_name(families[i])))
161 if (!pango_font_family_is_monospace(families[i]))
163 ret = FALSE;
167 g_free(families);
168 return ret;
172 /* We don't support variable width fonts (yet) */
173 static gint get_font_width(GtkPrintContext *context, PangoFontDescription *desc)
175 PangoContext *pc;
176 PangoFontMetrics *metrics;
177 gint width;
179 pc = gtk_print_context_create_pango_context(context);
181 if (!utils_font_desc_check_monospace(pc, desc))
182 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_WARNING,
183 _("The editor font is not a monospaced font!"),
184 _("Text will be wrongly spaced."));
186 metrics = pango_context_get_metrics(pc, desc, pango_context_get_language(pc));
187 /** TODO is this the best result we can get? */
188 /* digit and char width are mostly equal for monospace fonts, char width might be
189 * for dual width characters(e.g. Japanese) so use digit width to get sure we get the width
190 * for one character */
191 width = pango_font_metrics_get_approximate_digit_width(metrics) / PANGO_SCALE;
193 pango_font_metrics_unref(metrics);
194 g_object_unref(pc);
196 return width;
200 static gint get_page_count(GtkPrintContext *context, DocInfo *dinfo)
202 gdouble width, height;
203 gint layout_h;
204 gint i, j, lines_left;
205 gchar *line_buf;
207 if (dinfo == NULL)
208 return -1;
210 width = gtk_print_context_get_width(context);
211 height = gtk_print_context_get_height(context);
213 if (printing_prefs.print_line_numbers)
214 /* remove line number margin space from overall width */
215 width -= dinfo->max_line_number_margin * dinfo->font_width;
217 pango_layout_set_width(dinfo->layout, width * PANGO_SCALE);
219 /* add test text to get line height */
220 pango_layout_set_text(dinfo->layout, "Test 1", -1);
221 pango_layout_get_size(dinfo->layout, NULL, &layout_h);
222 if (layout_h <= 0)
224 geany_debug("Invalid layout_h (%d). Falling back to default height (%d)",
225 layout_h, 100 * PANGO_SCALE);
226 layout_h = 100 * PANGO_SCALE;
228 dinfo->line_height = (gdouble)layout_h / PANGO_SCALE;
229 dinfo->lines_per_page = ceil((height - dinfo->line_height) / dinfo->line_height);
230 #ifdef GEANY_PRINT_DEBUG
231 geany_debug("max lines_per_page: %d", dinfo->lines_per_page);
232 #endif
233 if (printing_prefs.print_page_numbers)
234 dinfo->lines_per_page -= 2;
235 if (printing_prefs.print_page_header)
236 dinfo->lines_per_page -= 3;
238 lines_left = dinfo->lines_per_page;
240 i = 0;
241 for (j = 0; j < dinfo->lines; j++)
243 gint lines = 1;
244 gint line_width;
246 line_buf = sci_get_line(dinfo->doc->editor->sci, j);
247 line_width = (g_utf8_strlen(line_buf, -1) + 1) * dinfo->font_width;
248 if (line_width > width)
249 lines = ceil(line_width / width);
250 #ifdef GEANY_PRINT_DEBUG
251 if (lines != 1) geany_debug("%d %d", j+1, lines);
252 #endif
254 while (lines_left < lines)
256 lines -= lines_left;
257 lines_left = dinfo->lines_per_page;
258 i++;
260 lines_left -= lines;
261 g_free(line_buf);
264 return i + 1;
268 static void add_page_header(DocInfo *dinfo, cairo_t *cr, gint width, gint page_nr)
270 gint ph_height = dinfo->line_height * 3;
271 gchar *data;
272 gchar *datetime;
273 gchar *tmp_file_name = (dinfo->doc->file_name != NULL) ?
274 dinfo->doc->file_name : GEANY_STRING_UNTITLED;
275 gchar *file_name = (printing_prefs.page_header_basename) ?
276 g_path_get_basename(tmp_file_name) : g_strdup(tmp_file_name);
277 PangoLayout *layout = dinfo->layout;
279 /* draw the frame */
280 cairo_set_line_width(cr, 0.3);
281 cairo_set_source_rgb(cr, 0, 0, 0);
282 cairo_rectangle(cr, 2, 2, width - 4, ph_height - 4);
283 cairo_stroke(cr);
285 /* width - 8: 2px between doc border and frame border, 2px between frame border and text
286 * and this on left and right side, so (2 + 2) * 2 */
287 pango_layout_set_width(layout, (width - 8) * PANGO_SCALE);
289 if ((g_utf8_strlen(file_name, -1) * dinfo->font_width) >= ((width - 4) - (dinfo->font_width * 2)))
290 /* if the filename is wider than the available space on the line, skip parts of it */
291 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_MIDDLE);
293 data = g_strdup_printf("<b>%s</b>", file_name);
294 pango_layout_set_markup(layout, data, -1);
295 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
296 cairo_move_to(cr, 4, dinfo->line_height * 0.5);
297 pango_cairo_show_layout(cr, layout);
298 g_free(data);
299 g_free(file_name);
301 data = g_strdup_printf(_("<b>Page %d of %d</b>"), page_nr + 1, dinfo->n_pages);
302 pango_layout_set_markup(layout, data, -1);
303 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
304 cairo_move_to(cr, 4, dinfo->line_height * 1.5);
305 pango_cairo_show_layout(cr, layout);
306 g_free(data);
308 datetime = utils_get_date_time(printing_prefs.page_header_datefmt, &(dinfo->print_time));
309 if (G_LIKELY(NZV(datetime)))
311 data = g_strdup_printf("<b>%s</b>", datetime);
312 pango_layout_set_markup(layout, data, -1);
313 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
314 cairo_move_to(cr, 2, dinfo->line_height * 1.5);
315 pango_cairo_show_layout(cr, layout);
316 g_free(data);
318 g_free(datetime);
320 /* reset layout and re-position cairo context */
321 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
322 pango_layout_set_ellipsize(layout, FALSE);
323 pango_layout_set_justify(layout, FALSE);
324 pango_layout_set_width(layout, width * PANGO_SCALE);
325 cairo_move_to(cr, 0, dinfo->line_height * 3);
329 static void custom_widget_apply(GtkPrintOperation *operation, GtkWidget *widget, gpointer user_data)
331 PrintWidgets *w = user_data;
333 printing_prefs.print_line_numbers =
334 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_linenumbers));
336 printing_prefs.print_page_numbers =
337 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_pagenumbers));
339 printing_prefs.print_page_header =
340 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_pageheader));
342 printing_prefs.page_header_basename =
343 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->check_print_basename));
345 g_free(printing_prefs.page_header_datefmt);
346 printing_prefs.page_header_datefmt =
347 g_strdup(gtk_entry_get_text(GTK_ENTRY(w->entry_print_dateformat)));
351 static void on_page_header_toggled(GtkToggleButton *togglebutton, gpointer user_data)
353 gboolean sens = gtk_toggle_button_get_active(togglebutton);
354 PrintWidgets *w = user_data;
356 gtk_widget_set_sensitive(w->check_print_basename, sens);
357 gtk_widget_set_sensitive(w->entry_print_dateformat, sens);
361 static GtkWidget *create_custom_widget(GtkPrintOperation *operation, gpointer user_data)
362 { /* copied from interface.c */
363 GtkWidget *page;
364 GtkWidget *frame33;
365 GtkWidget *alignment36;
366 GtkWidget *vbox30;
367 GtkWidget *hbox10;
368 GtkWidget *label203;
369 PrintWidgets *w = user_data;
371 gtk_print_operation_set_custom_tab_label(operation, _("Document Setup"));
373 page = gtk_vbox_new(FALSE, 0);
374 gtk_container_set_border_width(GTK_CONTAINER(page), 5);
376 w->check_print_linenumbers = gtk_check_button_new_with_mnemonic(_("Print line numbers"));
377 gtk_box_pack_start(GTK_BOX(page), w->check_print_linenumbers, FALSE, FALSE, 0);
378 gtk_widget_set_tooltip_text(w->check_print_linenumbers, _("Add line numbers to the printed page"));
379 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_linenumbers), printing_prefs.print_line_numbers);
381 w->check_print_pagenumbers = gtk_check_button_new_with_mnemonic(_("Print page numbers"));
382 gtk_box_pack_start(GTK_BOX(page), w->check_print_pagenumbers, FALSE, FALSE, 0);
383 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."));
384 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_pagenumbers), printing_prefs.print_page_numbers);
386 w->check_print_pageheader = gtk_check_button_new_with_mnemonic(_("Print page header"));
387 gtk_box_pack_start(GTK_BOX(page), w->check_print_pageheader, FALSE, FALSE, 0);
388 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."));
389 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_pageheader), printing_prefs.print_page_header);
390 g_signal_connect(w->check_print_pageheader, "toggled", G_CALLBACK(on_page_header_toggled), w);
392 frame33 = gtk_frame_new(NULL);
393 gtk_box_pack_start(GTK_BOX(page), frame33, FALSE, FALSE, 0);
394 gtk_frame_set_label_align(GTK_FRAME(frame33), 0, 0);
395 gtk_frame_set_shadow_type(GTK_FRAME(frame33), GTK_SHADOW_NONE);
397 alignment36 = gtk_alignment_new(0, 0.5, 1, 1);
398 gtk_container_add(GTK_CONTAINER(frame33), alignment36);
399 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment36), 0, 0, 12, 0);
401 vbox30 = gtk_vbox_new(FALSE, 1);
402 gtk_container_add(GTK_CONTAINER(alignment36), vbox30);
404 w->check_print_basename = gtk_check_button_new_with_mnemonic(_("Use the basename of the printed file"));
405 gtk_box_pack_start(GTK_BOX(vbox30), w->check_print_basename, FALSE, FALSE, 0);
406 gtk_widget_set_tooltip_text(w->check_print_basename, _("Print only the basename(without the path) of the printed file"));
407 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->check_print_basename), printing_prefs.page_header_basename);
409 hbox10 = gtk_hbox_new(FALSE, 5);
410 gtk_box_pack_start(GTK_BOX(vbox30), hbox10, TRUE, TRUE, 0);
412 label203 = gtk_label_new(_("Date format:"));
413 gtk_box_pack_start(GTK_BOX(hbox10), label203, FALSE, FALSE, 0);
415 w->entry_print_dateformat = gtk_entry_new();
416 ui_entry_add_clear_icon(GTK_ENTRY(w->entry_print_dateformat));
417 gtk_box_pack_start(GTK_BOX(hbox10), w->entry_print_dateformat, TRUE, TRUE, 0);
418 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."));
419 gtk_entry_set_text(GTK_ENTRY(w->entry_print_dateformat), printing_prefs.page_header_datefmt);
421 on_page_header_toggled(GTK_TOGGLE_BUTTON(w->check_print_pageheader), w);
422 gtk_widget_show_all(page);
423 return page;
427 static void end_print(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
429 DocInfo *dinfo = user_data;
431 if (dinfo == NULL)
432 return;
434 gtk_widget_hide(main_widgets.progressbar);
435 g_object_unref(dinfo->layout);
439 static void begin_print(GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
441 DocInfo *dinfo = user_data;
442 PangoFontDescription *desc;
443 gint i;
444 gint style_max;
446 if (dinfo == NULL)
447 return;
449 gtk_widget_show(main_widgets.progressbar);
451 desc = pango_font_description_from_string(interface_prefs.editor_font);
453 /* init dinfo fields */
454 dinfo->lines = sci_get_line_count(dinfo->doc->editor->sci);
455 dinfo->lines_per_page = 0;
456 dinfo->cur_line = 0;
457 dinfo->cur_pos = 0;
458 dinfo->long_line = FALSE;
459 dinfo->print_time = time(NULL);
460 dinfo->max_line_number_margin = get_line_numbers_arity(dinfo->lines) + 1;
461 /* increase font width by 1 (looks better) */
462 dinfo->font_width = get_font_width(context, desc) + 1;
463 /* create a PangoLayout to be commonly used in get_page_count and draw_page */
464 dinfo->layout = setup_pango_layout(context, desc);
465 /* this is necessary because of possible line breaks on the printed page and then
466 * lines_per_page differs from document line count */
467 dinfo->n_pages = get_page_count(context, dinfo);
469 /* read all styles from Scintilla */
470 style_max = pow(2, scintilla_send_message(dinfo->doc->editor->sci, SCI_GETSTYLEBITS, 0, 0));
471 /* if the lexer uses only the first 32 styles(style bits = 5),
472 * we need to add the pre-defined styles */
473 if (style_max == 32)
474 style_max = STYLE_LASTPREDEFINED;
475 for (i = 0; i < style_max; i++)
477 dinfo->styles[i][FORE] = ROTATE_RGB(scintilla_send_message(
478 dinfo->doc->editor->sci, SCI_STYLEGETFORE, i, 0));
479 if (i == STYLE_LINENUMBER)
480 { /* ignore background colour for line number margin to avoid trouble with wrapped lines */
481 dinfo->styles[STYLE_LINENUMBER][BACK] = ROTATE_RGB(scintilla_send_message(
482 dinfo->doc->editor->sci, SCI_STYLEGETBACK, STYLE_DEFAULT, 0));
484 else
486 dinfo->styles[i][BACK] = ROTATE_RGB(scintilla_send_message(
487 dinfo->doc->editor->sci, SCI_STYLEGETBACK, i, 0));
489 /* use white background color unless foreground is white to save ink */
490 if (dinfo->styles[i][FORE] != 0xffffff)
491 dinfo->styles[i][BACK] = 0xffffff;
492 dinfo->styles[i][BOLD] =
493 scintilla_send_message(dinfo->doc->editor->sci, SCI_STYLEGETBOLD, i, 0);
494 dinfo->styles[i][ITALIC] =
495 scintilla_send_message(dinfo->doc->editor->sci, SCI_STYLEGETITALIC, i, 0);
498 if (dinfo->n_pages >= 0)
499 gtk_print_operation_set_n_pages(operation, dinfo->n_pages);
501 pango_font_description_free(desc);
505 static void draw_page(GtkPrintOperation *operation, GtkPrintContext *context,
506 gint page_nr, gpointer user_data)
508 DocInfo *dinfo = user_data;
509 GeanyEditor *editor;
510 cairo_t *cr;
511 gdouble width, height;
512 gdouble x = 0.0, y = 0.0;
513 /*gint layout_h;*/
514 gint count;
515 GString *str;
517 if (dinfo == NULL || page_nr >= dinfo->n_pages)
518 return;
520 editor = dinfo->doc->editor;
522 if (dinfo->n_pages > 0)
524 gdouble fraction = (page_nr + 1) / (gdouble) dinfo->n_pages;
525 gchar *text = g_strdup_printf(_("Page %d of %d"), page_nr, dinfo->n_pages);
526 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(main_widgets.progressbar), fraction);
527 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
528 g_free(text);
531 #ifdef GEANY_PRINT_DEBUG
532 geany_debug("draw_page = %d, pages = %d, (real) lines_per_page = %d",
533 page_nr, dinfo->n_pages, dinfo->lines_per_page);
534 #endif
536 str = g_string_sized_new(256);
537 cr = gtk_print_context_get_cairo_context(context);
538 width = gtk_print_context_get_width(context);
539 height = gtk_print_context_get_height(context);
541 cairo_set_source_rgb(cr, 0, 0, 0);
542 #ifdef GEANY_PRINT_DEBUG
543 cairo_set_line_width(cr, 0.2);
544 cairo_rectangle(cr, 0, 0, width, height);
545 cairo_stroke(cr);
546 #endif
547 cairo_move_to(cr, 0, 0);
549 pango_layout_set_width(dinfo->layout, width * PANGO_SCALE);
550 pango_layout_set_alignment(dinfo->layout, PANGO_ALIGN_LEFT);
551 pango_layout_set_ellipsize(dinfo->layout, FALSE);
552 pango_layout_set_justify(dinfo->layout, FALSE);
554 if (printing_prefs.print_page_header)
555 add_page_header(dinfo, cr, width, page_nr);
557 count = 0; /* the actual line counter for the current page, might be different from
558 * dinfo->cur_line due to possible line breaks */
559 while (count < dinfo->lines_per_page)
561 gchar c = 'a';
562 gint style = -1;
563 PangoAttrList *layout_attr;
564 PangoAttribute *attr;
565 gint colours[3] = { 0 };
566 gboolean add_linenumber = TRUE;
567 gboolean at_eol;
569 while (count < dinfo->lines_per_page && c != '\0')
571 at_eol = FALSE;
573 g_string_erase(str, 0, str->len); /* clear the string */
575 /* line numbers */
576 if (printing_prefs.print_line_numbers && add_linenumber)
578 /* if we had a wrapped line on the last page which needs to be continued, don't
579 * add a line number */
580 if (dinfo->long_line)
582 add_linenumber = FALSE;
584 else
586 gchar *line_number = NULL;
587 gint cur_line_number_margin = get_line_numbers_arity(dinfo->cur_line + 1);
588 gchar *fill = g_strnfill(
589 dinfo->max_line_number_margin - cur_line_number_margin - 1, ' ');
591 line_number = g_strdup_printf("%s%d ", fill, dinfo->cur_line + 1);
592 g_string_append(str, line_number);
593 dinfo->cur_line++; /* increase document line */
594 add_linenumber = FALSE;
595 style = STYLE_LINENUMBER;
596 c = 'a'; /* dummy value */
597 g_free(fill);
598 g_free(line_number);
601 /* data */
602 else
604 style = sci_get_style_at(dinfo->doc->editor->sci, dinfo->cur_pos);
605 c = sci_get_char_at(dinfo->doc->editor->sci, dinfo->cur_pos);
606 if (c == '\0' || style == -1)
607 { /* if c gets 0, we are probably out of document boundaries,
608 * so stop to break out of outer loop */
609 count = dinfo->lines_per_page;
610 break;
612 dinfo->cur_pos++;
614 /* convert tabs to spaces which seems to be better than using Pango tabs */
615 if (c == '\t')
617 gint tab_width = sci_get_tab_width(editor->sci);
618 gchar *s = g_strnfill(tab_width, ' ');
619 g_string_append(str, s);
620 g_free(s);
622 /* don't add line breaks, they are handled manually below */
623 else if (c == '\r' || c == '\n')
625 gchar c_next = sci_get_char_at(dinfo->doc->editor->sci, dinfo->cur_pos);
626 at_eol = TRUE;
627 if (c == '\r' && c_next == '\n')
628 dinfo->cur_pos++; /* skip LF part of CR/LF */
630 else
632 g_string_append_c(str, c); /* finally add the character */
634 /* handle UTF-8: since we add char by char (better: byte by byte), we need to
635 * keep UTF-8 characters together(e.g. two bytes for one character)
636 * the input is always UTF-8 and c is signed, so all non-Ascii
637 * characters are less than 0 and consist of all bytes less than 0.
638 * style doesn't change since it is only one character with multiple bytes. */
639 while (c < 0)
641 c = sci_get_char_at(dinfo->doc->editor->sci, dinfo->cur_pos);
642 if (c < 0)
643 { /* only add the byte when it is part of the UTF-8 character
644 * otherwise we could add e.g. a '\n' and it won't be visible in the
645 * printed document */
646 g_string_append_c(str, c);
647 dinfo->cur_pos++;
653 if (! at_eol)
655 /* set text */
656 pango_layout_set_text(dinfo->layout, str->str, -1);
657 /* attributes */
658 layout_attr = pango_attr_list_new();
659 /* foreground colour */
660 get_rgb_values(dinfo->styles[style][FORE], &colours[0], &colours[1], &colours[2]);
661 attr = pango_attr_foreground_new(colours[0], colours[1], colours[2]);
662 ADD_ATTR(layout_attr, attr);
663 /* background colour */
664 get_rgb_values(dinfo->styles[style][BACK], &colours[0], &colours[1], &colours[2]);
665 attr = pango_attr_background_new(colours[0], colours[1], colours[2]);
666 ADD_ATTR(layout_attr, attr);
667 /* bold text */
668 if (dinfo->styles[style][BOLD])
670 attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
671 ADD_ATTR(layout_attr, attr);
673 /* italic text */
674 if (dinfo->styles[style][ITALIC])
676 attr = pango_attr_style_new(PANGO_STYLE_ITALIC);
677 ADD_ATTR(layout_attr, attr);
679 pango_layout_set_attributes(dinfo->layout, layout_attr);
680 pango_layout_context_changed(dinfo->layout);
681 pango_attr_list_unref(layout_attr);
684 cairo_get_current_point(cr, &x, &y);
687 /* normal line break at eol character in document */
688 if (at_eol)
690 /*pango_layout_get_size(dinfo->layout, NULL, &layout_h);*/
691 /*cairo_move_to(cr, 0, y + (gdouble)layout_h / PANGO_SCALE);*/
692 cairo_move_to(cr, 0, y + dinfo->line_height);
694 count++;
695 /* we added a new document line so request a new line number */
696 add_linenumber = TRUE;
698 else
700 gint x_offset = 0;
701 /* maybe we need to force a line break because of too long line */
702 if (x >= (width - dinfo->font_width))
704 /* don't start the line at horizontal origin because we need to skip the
705 * line number margin */
706 if (printing_prefs.print_line_numbers)
708 x_offset = (dinfo->max_line_number_margin + 1) * dinfo->font_width;
711 /*pango_layout_get_size(dinfo->layout, NULL, &layout_h);*/
712 /*cairo_move_to(cr, x_offset, y + (gdouble)layout_h / PANGO_SCALE);*/
713 /* this is faster but not exactly the same as above */
714 cairo_move_to(cr, x_offset, y + dinfo->line_height);
715 cairo_get_current_point(cr, &x, &y);
716 count++;
718 if (count < dinfo->lines_per_page)
720 /* str->len is counted in bytes not characters, so use g_utf8_strlen() */
721 x_offset = (g_utf8_strlen(str->str, -1) * dinfo->font_width);
723 if (dinfo->long_line && count == 0)
725 x_offset = (dinfo->max_line_number_margin + 1) * dinfo->font_width;
726 dinfo->long_line = FALSE;
729 pango_cairo_show_layout(cr, dinfo->layout);
730 cairo_move_to(cr, x + x_offset, y);
732 else
733 /* we are on a wrapped line but we are out of lines on this page, so continue
734 * the current line on the next page and remember to continue in current line */
735 dinfo->long_line = TRUE;
740 if (printing_prefs.print_line_numbers)
741 { /* print a thin line between the line number margin and the data */
742 gint y_start = 0;
744 if (printing_prefs.print_page_header)
745 y_start = (dinfo->line_height * 3) - 2; /* "- 2": to connect the line number line to
746 * the page header frame */
748 cairo_set_line_width(cr, 0.3);
749 cairo_move_to(cr, (dinfo->max_line_number_margin * dinfo->font_width) + 1, y_start);
750 cairo_line_to(cr, (dinfo->max_line_number_margin * dinfo->font_width) + 1,
751 y + dinfo->line_height); /* y is last added line, we reuse it */
752 cairo_stroke(cr);
755 if (printing_prefs.print_page_numbers)
757 gchar *line = g_strdup_printf("<small>- %d -</small>", page_nr + 1);
758 pango_layout_set_markup(dinfo->layout, line, -1);
759 pango_layout_set_alignment(dinfo->layout, PANGO_ALIGN_CENTER);
760 cairo_move_to(cr, 0, height - dinfo->line_height);
761 pango_cairo_show_layout(cr, dinfo->layout);
762 g_free(line);
764 #ifdef GEANY_PRINT_DEBUG
765 cairo_set_line_width(cr, 0.3);
766 cairo_move_to(cr, 0, height - (1.25 * dinfo->line_height));
767 cairo_line_to(cr, width - 1, height - (1.25 * dinfo->line_height));
768 cairo_stroke(cr);
769 #endif
771 g_string_free(str, TRUE);
775 static void status_changed(GtkPrintOperation *op, gpointer data)
777 gchar *filename = (data != NULL) ? data : GEANY_STRING_UNTITLED;
778 if (gtk_print_operation_get_status(op) == GTK_PRINT_STATUS_FINISHED_ABORTED)
779 msgwin_status_add(_("Did not send document %s to the printing subsystem."), filename);
780 else if (gtk_print_operation_get_status(op) == GTK_PRINT_STATUS_FINISHED)
781 msgwin_status_add(_("Document %s was sent to the printing subsystem."), filename);
785 static void printing_print_gtk(GeanyDocument *doc)
787 GtkPrintOperation *op;
788 GtkPrintOperationResult res = GTK_PRINT_OPERATION_RESULT_ERROR;
789 GError *error = NULL;
790 DocInfo *dinfo;
791 PrintWidgets *widgets;
793 /** TODO check for monospace font, detect the widest character in the font and
794 * use it at font_width */
796 widgets = g_new0(PrintWidgets, 1);
797 dinfo = g_new0(DocInfo, 1);
798 /* all other fields are initialised in begin_print() */
799 dinfo->doc = doc;
801 op = gtk_print_operation_new();
803 gtk_print_operation_set_unit(op, GTK_UNIT_POINTS);
804 gtk_print_operation_set_show_progress(op, TRUE);
805 #if GTK_CHECK_VERSION(2, 18, 0)
806 gtk_print_operation_set_embed_page_setup(op, TRUE);
807 #endif
809 g_signal_connect(op, "begin-print", G_CALLBACK(begin_print), dinfo);
810 g_signal_connect(op, "end-print", G_CALLBACK(end_print), dinfo);
811 g_signal_connect(op, "draw-page", G_CALLBACK(draw_page), dinfo);
812 g_signal_connect(op, "status-changed", G_CALLBACK(status_changed), doc->file_name);
813 g_signal_connect(op, "create-custom-widget", G_CALLBACK(create_custom_widget), widgets);
814 g_signal_connect(op, "custom-widget-apply", G_CALLBACK(custom_widget_apply), widgets);
816 if (settings != NULL)
817 gtk_print_operation_set_print_settings(op, settings);
818 if (page_setup != NULL)
819 gtk_print_operation_set_default_page_setup(op, page_setup);
821 res = gtk_print_operation_run(
822 op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW(main_widgets.window), &error);
824 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
826 if (settings != NULL)
827 g_object_unref(settings);
828 settings = g_object_ref(gtk_print_operation_get_print_settings(op));
829 /* status message is printed in the status-changed handler */
831 else if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
833 dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Printing of %s failed (%s)."),
834 doc->file_name, error->message);
835 g_error_free(error);
838 g_object_unref(op);
839 g_free(dinfo);
840 g_free(widgets);
844 void printing_page_setup_gtk(void)
846 GtkPageSetup *new_page_setup;
848 if (settings == NULL)
849 settings = gtk_print_settings_new();
851 new_page_setup = gtk_print_run_page_setup_dialog(
852 GTK_WINDOW(main_widgets.window), page_setup, settings);
854 if (page_setup != NULL)
855 g_object_unref(page_setup);
857 page_setup = new_page_setup;
861 /* simple file print using an external tool */
862 static void print_external(GeanyDocument *doc)
864 gchar *cmdline;
866 if (doc->file_name == NULL)
867 return;
869 if (! NZV(printing_prefs.external_print_cmd))
871 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
872 _("Please set a print command in the preferences dialog first."));
873 return;
876 cmdline = g_strdup(printing_prefs.external_print_cmd);
877 utils_str_replace_all(&cmdline, "%f", doc->file_name);
879 if (dialogs_show_question(
880 _("The file \"%s\" will be printed with the following command:\n\n%s"),
881 doc->file_name, cmdline))
883 GError *error = NULL;
885 #ifdef G_OS_WIN32
886 gchar *tmp_cmdline = g_strdup(cmdline);
887 #else
888 /* /bin/sh -c emulates the system() call and makes complex commands possible
889 * but only needed on non-win32 systems due to the lack of win32's shell capabilities */
890 gchar *tmp_cmdline = g_strconcat("/bin/sh -c \"", cmdline, "\"", NULL);
891 #endif
893 if (! g_spawn_command_line_async(tmp_cmdline, &error))
895 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
896 _("Printing of \"%s\" failed (return code: %s)."),
897 doc->file_name, error->message);
898 g_error_free(error);
900 else
902 msgwin_status_add(_("File %s printed."), doc->file_name);
904 g_free(tmp_cmdline);
906 g_free(cmdline);
910 void printing_print_doc(GeanyDocument *doc)
912 if (doc == NULL)
913 return;
915 if (printing_prefs.use_gtk_printing)
916 printing_print_gtk(doc);
917 else
918 print_external(doc);