1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
4 * This file is part of GtkSourceView
6 * Derived from gedit-print.c
8 * Copyright (C) 2000, 2001 Chema Celorio, Paolo Maggi
9 * Copyright (C) 2002 Paolo Maggi
10 * Copyright (C) 2003 Gustavo Giráldez
11 * Copyright (C) 2004 Red Hat, Inc.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330,
26 * Boston, MA 02111-1307, USA.
38 #include "gtksourceprintjob.h"
39 #include "image_viewer.h"
41 #include <glib/gi18n.h>
42 #include <gtk/gtkmain.h>
43 #include <gtk/gtktextview.h>
44 #include <libgnomeprint/gnome-print-pango.h>
59 #define DEFAULT_FONT_NAME "Monospace 10"
60 #define DEFAULT_COLOR 0x000000ff
62 #define CM(v) ((v) * 72.0 / 2.54)
63 #define A4_WIDTH (210.0 * 72 / 25.4)
64 #define A4_HEIGHT (297.0 * 72 / 25.4)
66 #define NUMBERS_TEXT_SEPARATION CM(0.5)
68 #define HEADER_FOOTER_SIZE 2.5
69 #define SEPARATOR_SPACING 1.5
70 #define SEPARATOR_LINE_WIDTH 1.0
73 typedef struct _TextSegment TextSegment;
74 typedef struct _Paragraph Paragraph;
75 typedef struct _TextStyle TextStyle;
77 /* a piece of text (within a paragraph) of the same style */
86 /* a printable line */
93 /* the style of a TextSegment */
96 PangoFontDescription *font_desc;
100 gboolean strikethrough;
101 PangoUnderline underline;
105 struct _GtkSourcePrintJobPrivate
107 /* General job configuration */
108 GnomePrintConfig *config;
109 GtkTextBuffer *buffer;
111 GtkWrapMode wrap_mode;
113 PangoLanguage *language;
114 PangoFontDescription *font;
115 PangoFontDescription *numbers_font;
118 gdouble margin_bottom;
120 gdouble margin_right;
122 /* Default header and footer configuration */
123 gboolean print_header;
124 gboolean print_footer;
125 PangoFontDescription *header_footer_font;
126 gchar *header_format_left;
127 gchar *header_format_center;
128 gchar *header_format_right;
129 gboolean header_separator;
130 gchar *footer_format_left;
131 gchar *footer_format_center;
132 gchar *footer_format_right;
133 gboolean footer_separator;
136 guint first_line_number;
137 guint last_line_number;
142 guint idle_printing_tag;
143 GnomePrintContext *print_ctxt;
144 GnomePrintJob *print_job;
145 PangoContext *pango_context;
146 PangoTabArray *tab_array;
149 gdouble available_height;
150 GSList *current_paragraph;
151 gint current_paragraph_line;
154 /* Cached information - all this information is obtained from
155 * other fields in the configuration */
156 GHashTable *tag_styles;
161 gdouble doc_margin_top;
162 gdouble doc_margin_left;
163 gdouble doc_margin_right;
164 gdouble doc_margin_bottom;
166 gdouble header_height;
167 gdouble footer_height;
168 gdouble numbers_width;
170 /* printable (for the document itself) size */
187 PROP_NUMBERS_FONT_DESC,
191 PROP_HEADER_FOOTER_FONT,
192 PROP_HEADER_FOOTER_FONT_DESC
202 static GObjectClass *parent_class = NULL;
203 static guint print_job_signals [LAST_SIGNAL] = { 0 };
205 static void gtk_source_print_job_class_init (GtkSourcePrintJobClass *klass);
206 static void gtk_source_print_job_instance_init (GtkSourcePrintJob *job);
207 static void gtk_source_print_job_finalize (GObject *object);
208 static void gtk_source_print_job_get_property (GObject *object,
212 static void gtk_source_print_job_set_property (GObject *object,
216 static void gtk_source_print_job_begin_page (GtkSourcePrintJob *job);
218 static void default_print_header (GtkSourcePrintJob *job,
221 static void default_print_footer (GtkSourcePrintJob *job,
227 gtk_source_print_job_get_type (void)
229 static GType our_type = 0;
233 static const GTypeInfo our_info = {
234 sizeof (GtkSourcePrintJobClass),
235 NULL, /* base_init */
236 NULL, /* base_finalize */
237 (GClassInitFunc) gtk_source_print_job_class_init,
238 NULL, /* class_finalize */
239 NULL, /* class_data */
240 sizeof (GtkSourcePrintJob),
242 (GInstanceInitFunc) gtk_source_print_job_instance_init
245 our_type = g_type_register_static (G_TYPE_OBJECT,
255 gtk_source_print_job_class_init (GtkSourcePrintJobClass *klass)
257 GObjectClass *object_class;
259 object_class = G_OBJECT_CLASS (klass);
260 parent_class = g_type_class_peek_parent (klass);
262 object_class->finalize = gtk_source_print_job_finalize;
263 object_class->get_property = gtk_source_print_job_get_property;
264 object_class->set_property = gtk_source_print_job_set_property;
266 klass->begin_page = gtk_source_print_job_begin_page;
267 klass->finished = NULL;
269 g_object_class_install_property (object_class,
271 g_param_spec_object ("config",
273 _("Configuration options for "
275 GNOME_TYPE_PRINT_CONFIG,
277 g_object_class_install_property (object_class,
279 g_param_spec_object ("buffer",
281 _("GtkTextBuffer object to print"),
282 GTK_TYPE_TEXT_BUFFER,
284 g_object_class_install_property (object_class,
286 g_param_spec_uint ("tabs_width",
288 _("Width in equivalent space "
289 "characters of tabs"),
292 g_object_class_install_property (object_class,
294 g_param_spec_enum ("wrap_mode",
296 _("Word wrapping mode"),
300 g_object_class_install_property (object_class,
302 g_param_spec_boolean ("highlight",
304 _("Whether to print the "
305 "document with highlighted "
309 g_object_class_install_property (object_class,
311 g_param_spec_string ("font",
313 _("GnomeFont name to use for the "
314 "document text (deprecated)"),
317 g_object_class_install_property (object_class,
319 g_param_spec_boxed ("font_desc",
320 _("Font Description"),
321 _("Font to use for the document text "
322 "(e.g. \"Monospace 10\")"),
323 PANGO_TYPE_FONT_DESCRIPTION,
325 g_object_class_install_property (object_class,
327 g_param_spec_string ("numbers_font",
329 _("GnomeFont name to use for the "
330 "line numbers (deprecated)"),
333 g_object_class_install_property (object_class,
334 PROP_NUMBERS_FONT_DESC,
335 g_param_spec_boxed ("numbers_font_desc",
337 _("Font description to use for the "
339 PANGO_TYPE_FONT_DESCRIPTION,
341 g_object_class_install_property (object_class,
343 g_param_spec_uint ("print_numbers",
344 _("Print Line Numbers"),
345 _("Interval of printed line numbers "
346 "(0 means no numbers)"),
349 g_object_class_install_property (object_class,
351 g_param_spec_boolean ("print_header",
353 _("Whether to print a header "
357 g_object_class_install_property (object_class,
359 g_param_spec_boolean ("print_footer",
361 _("Whether to print a footer "
365 g_object_class_install_property (object_class,
366 PROP_HEADER_FOOTER_FONT,
367 g_param_spec_string ("header_footer_font",
368 _("Header and Footer Font"),
369 _("GnomeFont name to use for the header "
370 "and footer (deprecated)"),
373 g_object_class_install_property (object_class,
374 PROP_HEADER_FOOTER_FONT_DESC,
375 g_param_spec_boxed ("header_footer_font_desc",
376 _("Header and Footer Font Description"),
377 _("Font to use for headers and footers "
378 "(e.g. \"Monospace 10\")"),
379 PANGO_TYPE_FONT_DESCRIPTION,
382 print_job_signals [BEGIN_PAGE] =
383 g_signal_new ("begin_page",
384 G_OBJECT_CLASS_TYPE (object_class),
386 G_STRUCT_OFFSET (GtkSourcePrintJobClass, begin_page),
388 g_cclosure_marshal_VOID__VOID,
391 print_job_signals [FINISHED] =
392 g_signal_new ("finished",
393 G_OBJECT_CLASS_TYPE (object_class),
395 G_STRUCT_OFFSET (GtkSourcePrintJobClass, finished),
397 g_cclosure_marshal_VOID__VOID,
403 gtk_source_print_job_instance_init (GtkSourcePrintJob *job)
405 GtkSourcePrintJobPrivate *priv;
407 priv = g_new0 (GtkSourcePrintJobPrivate, 1);
410 /* default job configuration */
414 priv->tabs_width = 8;
415 priv->wrap_mode = GTK_WRAP_NONE;
416 priv->highlight = TRUE;
417 priv->language = gtk_get_default_language ();
419 priv->numbers_font = NULL;
420 priv->print_numbers = 1;
421 priv->margin_top = 0.0;
422 priv->margin_bottom = 0.0;
423 priv->margin_left = 0.0;
424 priv->margin_right = 0.0;
426 priv->print_header = FALSE;
427 priv->print_footer = FALSE;
428 priv->header_footer_font = NULL;
429 priv->header_format_left = NULL;
430 priv->header_format_center = NULL;
431 priv->header_format_right = NULL;
432 priv->header_separator = FALSE;
433 priv->footer_format_left = NULL;
434 priv->footer_format_center = NULL;
435 priv->footer_format_right = NULL;
436 priv->footer_separator = FALSE;
439 priv->printing = FALSE;
440 priv->print_ctxt = NULL;
441 priv->print_job = NULL;
443 priv->page_count = 0;
445 priv->first_line_number = 0;
446 priv->paragraphs = NULL;
447 priv->tag_styles = NULL;
449 /* some default, sane values */
450 priv->page_width = A4_WIDTH;
451 priv->page_height = A4_HEIGHT;
452 priv->doc_margin_top = CM (1);
453 priv->doc_margin_left = CM (1);
454 priv->doc_margin_right = CM (1);
455 priv->doc_margin_bottom = CM (1);
459 free_paragraphs (GSList *paras)
461 while (paras != NULL)
463 Paragraph *para = paras->data;
464 TextSegment *seg = para->segment;
467 TextSegment *next = seg->next;
473 paras = g_slist_delete_link (paras, paras);
478 gtk_source_print_job_finalize (GObject *object)
480 GtkSourcePrintJob *job;
481 GtkSourcePrintJobPrivate *priv;
483 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (object));
485 job = GTK_SOURCE_PRINT_JOB (object);
490 if (priv->config != NULL)
491 gnome_print_config_unref (priv->config);
492 if (priv->buffer != NULL)
493 g_object_unref (priv->buffer);
494 if (priv->font != NULL)
495 pango_font_description_free (priv->font);
496 if (priv->numbers_font != NULL)
497 pango_font_description_free (priv->numbers_font);
498 if (priv->header_footer_font != NULL)
499 pango_font_description_free (priv->header_footer_font);
500 g_free (priv->header_format_left);
501 g_free (priv->header_format_right);
502 g_free (priv->header_format_center);
503 g_free (priv->footer_format_left);
504 g_free (priv->footer_format_right);
505 g_free (priv->footer_format_center);
507 if (priv->print_ctxt != NULL)
508 g_object_unref (priv->print_ctxt);
509 if (priv->print_job != NULL)
510 g_object_unref (priv->print_job);
511 if (priv->pango_context != NULL)
512 g_object_unref (priv->pango_context);
513 if (priv->tab_array != NULL)
514 pango_tab_array_free (priv->tab_array);
516 if (priv->paragraphs != NULL)
517 free_paragraphs (priv->paragraphs);
518 if (priv->tag_styles != NULL)
519 g_hash_table_destroy (priv->tag_styles);
525 G_OBJECT_CLASS (parent_class)->finalize (object);
529 gtk_source_print_job_get_property (GObject *object,
534 GtkSourcePrintJob *job = GTK_SOURCE_PRINT_JOB (object);
539 g_value_set_object (value, job->priv->config);
543 g_value_set_object (value, job->priv->buffer);
546 case PROP_TABS_WIDTH:
547 g_value_set_uint (value, job->priv->tabs_width);
551 g_value_set_enum (value, job->priv->wrap_mode);
555 g_value_set_boolean (value, job->priv->highlight);
559 g_value_take_string (value, gtk_source_print_job_get_font (job));
563 g_value_set_boxed (value, gtk_source_print_job_get_font_desc (job));
566 case PROP_NUMBERS_FONT:
567 g_value_take_string (value, gtk_source_print_job_get_numbers_font (job));
570 case PROP_NUMBERS_FONT_DESC:
571 g_value_set_boxed (value, gtk_source_print_job_get_numbers_font_desc (job));
574 case PROP_PRINT_NUMBERS:
575 g_value_set_uint (value, job->priv->print_numbers);
578 case PROP_PRINT_HEADER:
579 g_value_set_boolean (value, job->priv->print_header);
582 case PROP_PRINT_FOOTER:
583 g_value_set_boolean (value, job->priv->print_footer);
586 case PROP_HEADER_FOOTER_FONT:
587 g_value_take_string (value,
588 gtk_source_print_job_get_header_footer_font (job));
591 case PROP_HEADER_FOOTER_FONT_DESC:
592 g_value_set_boxed (value,
593 gtk_source_print_job_get_header_footer_font_desc (job));
597 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
603 gtk_source_print_job_set_property (GObject *object,
608 GtkSourcePrintJob *job = GTK_SOURCE_PRINT_JOB (object);
613 gtk_source_print_job_set_config (job, g_value_get_object (value));
617 gtk_source_print_job_set_buffer (job, g_value_get_object (value));
620 case PROP_TABS_WIDTH:
621 gtk_source_print_job_set_tabs_width (job, g_value_get_uint (value));
625 gtk_source_print_job_set_wrap_mode (job, g_value_get_enum (value));
629 gtk_source_print_job_set_highlight (job, g_value_get_boolean (value));
633 gtk_source_print_job_set_font (job, g_value_get_string (value));
637 gtk_source_print_job_set_font_desc (job, g_value_get_boxed (value));
640 case PROP_NUMBERS_FONT:
641 gtk_source_print_job_set_numbers_font (job, g_value_get_string (value));
644 case PROP_NUMBERS_FONT_DESC:
645 gtk_source_print_job_set_numbers_font_desc (job, g_value_get_boxed (value));
648 case PROP_PRINT_NUMBERS:
649 gtk_source_print_job_set_print_numbers (job, g_value_get_uint (value));
652 case PROP_PRINT_HEADER:
653 gtk_source_print_job_set_print_header (job, g_value_get_boolean (value));
656 case PROP_PRINT_FOOTER:
657 gtk_source_print_job_set_print_footer (job, g_value_get_boolean (value));
660 case PROP_HEADER_FOOTER_FONT:
661 gtk_source_print_job_set_header_footer_font (job,
662 g_value_get_string (value));
665 case PROP_HEADER_FOOTER_FONT_DESC:
666 gtk_source_print_job_set_header_footer_font_desc (job,
667 g_value_get_boxed (value));
671 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
677 gtk_source_print_job_begin_page (GtkSourcePrintJob *job)
679 g_return_if_fail (job->priv->printing);
681 if (job->priv->print_header && job->priv->header_height > 0)
685 x = job->priv->doc_margin_left + job->priv->margin_left;
686 y = job->priv->page_height - job->priv->doc_margin_top - job->priv->margin_top;
687 default_print_header (job, x, y);
690 if (job->priv->print_footer && job->priv->footer_height > 0)
694 x = job->priv->doc_margin_left + job->priv->margin_left;
695 y = job->priv->doc_margin_bottom +
696 job->priv->margin_bottom +
697 job->priv->footer_height;
698 default_print_footer (job, x, y);
702 /* ---- gnome-print / Pango convenience functions */
704 /* Gets the width of a layout in gnome-print coordinates */
706 get_layout_width (PangoLayout *layout)
710 pango_layout_get_size (layout, &layout_width, NULL);
711 return (gdouble) layout_width / PANGO_SCALE;
714 /* Gets the ascent/descent of a font in gnome-print coordinates */
716 get_font_ascent_descent (GtkSourcePrintJob *job,
717 PangoFontDescription *desc,
721 PangoFontMetrics *metrics;
723 metrics = pango_context_get_metrics (job->priv->pango_context,
725 job->priv->language);
728 *ascent = (gdouble) pango_font_metrics_get_ascent (metrics) / PANGO_SCALE;
730 *descent = (gdouble) pango_font_metrics_get_descent (metrics) / PANGO_SCALE;
732 pango_font_metrics_unref (metrics);
735 /* Draws the first line in a layout; we use this for one-line layouts
736 * to get baseline alignment */
738 show_first_layout_line (GnomePrintContext *print_ctxt,
741 PangoLayoutLine *line;
743 line = pango_layout_get_lines (layout)->data;
744 gnome_print_pango_layout_line (print_ctxt, line);
748 get_line_number_layout (GtkSourcePrintJob *job,
754 num_str = g_strdup_printf ("%d", line_number);
755 layout = pango_layout_new (job->priv->pango_context);
756 pango_layout_set_font_description (layout, job->priv->numbers_font);
757 pango_layout_set_text (layout, num_str, -1);
763 /* ---- Configuration functions */
766 ensure_print_config (GtkSourcePrintJob *job)
768 if (job->priv->config == NULL)
769 job->priv->config = gnome_print_config_default ();
770 if (job->priv->font == NULL)
771 job->priv->font = pango_font_description_from_string (DEFAULT_FONT_NAME);
775 update_page_size_and_margins (GtkSourcePrintJob *job)
778 gdouble ascent, descent;
780 gnome_print_job_get_page_size_from_config (job->priv->config,
781 &job->priv->page_width,
782 &job->priv->page_height);
784 gnome_print_config_get_length (job->priv->config, GNOME_PRINT_KEY_PAGE_MARGIN_TOP,
785 &job->priv->doc_margin_top, NULL);
786 gnome_print_config_get_length (job->priv->config, GNOME_PRINT_KEY_PAGE_MARGIN_BOTTOM,
787 &job->priv->doc_margin_bottom, NULL);
788 gnome_print_config_get_length (job->priv->config, GNOME_PRINT_KEY_PAGE_MARGIN_LEFT,
789 &job->priv->doc_margin_left, NULL);
790 gnome_print_config_get_length (job->priv->config, GNOME_PRINT_KEY_PAGE_MARGIN_RIGHT,
791 &job->priv->doc_margin_right, NULL);
793 /* set default fonts for numbers and header/footer */
794 if (job->priv->numbers_font == NULL)
795 job->priv->numbers_font = pango_font_description_copy (job->priv->font);
797 if (job->priv->header_footer_font == NULL)
798 job->priv->header_footer_font = pango_font_description_copy (job->priv->font);
800 /* calculate numbers width */
801 if (job->priv->print_numbers > 0)
803 layout = get_line_number_layout (job, job->priv->last_line_number);
804 job->priv->numbers_width = get_layout_width (layout) + NUMBERS_TEXT_SEPARATION;
805 g_object_unref (layout);
808 job->priv->numbers_width = 0.0;
810 get_font_ascent_descent (job, job->priv->header_footer_font, &ascent, &descent);
812 /* calculate header/footer height */
813 if (job->priv->print_header &&
814 (job->priv->header_format_left != NULL ||
815 job->priv->header_format_center != NULL ||
816 job->priv->header_format_right != NULL))
817 job->priv->header_height = HEADER_FOOTER_SIZE * (ascent + descent);
819 job->priv->header_height = 0.0;
821 if (job->priv->print_footer &&
822 (job->priv->footer_format_left != NULL ||
823 job->priv->footer_format_center != NULL ||
824 job->priv->footer_format_right != NULL))
825 job->priv->footer_height = HEADER_FOOTER_SIZE * (ascent + descent);
827 job->priv->footer_height = 0.0;
829 /* verify that the user provided margins are not too excesive
830 * and that we still have room for the text */
831 job->priv->text_width = (job->priv->page_width -
832 job->priv->doc_margin_left - job->priv->doc_margin_right -
833 job->priv->margin_left - job->priv->margin_right -
834 job->priv->numbers_width);
836 job->priv->text_height = (job->priv->page_height -
837 job->priv->doc_margin_top - job->priv->doc_margin_bottom -
838 job->priv->margin_top - job->priv->margin_bottom -
839 job->priv->header_height - job->priv->footer_height);
841 /* FIXME: put some saner values than 5cm - Gustavo */
842 g_return_val_if_fail (job->priv->text_width > CM(5.0), FALSE);
843 g_return_val_if_fail (job->priv->text_height > CM(5.0), FALSE);
848 /* We want a uniform tab width for the entire job without regard to style
849 * See comments in gtksourceview.c:calculate_real_tab_width
852 calculate_real_tab_width (GtkSourcePrintJob *job, guint tab_size, gchar c)
861 tab_string = g_strnfill (tab_size, c);
862 layout = pango_layout_new (job->priv->pango_context);
863 pango_layout_set_text (layout, tab_string, -1);
866 pango_layout_get_size (layout, &tab_width, NULL);
867 g_object_unref (G_OBJECT (layout));
873 setup_pango_context (GtkSourcePrintJob *job)
875 PangoFontMap *font_map;
878 if (!job->priv->pango_context)
880 font_map = gnome_print_pango_get_default_font_map ();
881 job->priv->pango_context = gnome_print_pango_create_context (font_map);
884 pango_context_set_language (job->priv->pango_context, job->priv->language);
885 pango_context_set_font_description (job->priv->pango_context, job->priv->font);
887 if (job->priv->tab_array)
889 pango_tab_array_free (job->priv->tab_array);
890 job->priv->tab_array = NULL;
893 real_tab_width = calculate_real_tab_width (job, job->priv->tabs_width, ' ');
894 if (real_tab_width > 0)
896 job->priv->tab_array = pango_tab_array_new (1, FALSE);
897 pango_tab_array_set_tab (job->priv->tab_array, 0, PANGO_TAB_LEFT, real_tab_width);
903 /* ----- Helper functions */
906 font_description_to_gnome_font_name (PangoFontDescription *desc)
908 GnomeFontFace *font_face;
911 /* Will always return some font */
912 font_face = gnome_font_face_find_closest_from_pango_description (desc);
914 retval = g_strdup_printf("%s %f",
915 gnome_font_face_get_name (font_face),
916 (double) pango_font_description_get_size (desc) / PANGO_SCALE);
917 g_object_unref (font_face);
923 * The following routines are duplicated in gedit/gedit/gedit-prefs-manager.c
926 /* Do this ourselves since gnome_font_find_closest() doesn't call
927 * gnome_font_face_find_closest() (probably a gnome-print bug)
930 face_and_size_from_full_name (const guchar *name,
931 GnomeFontFace **face,
937 copy = g_strdup (name);
938 str_size = strrchr (copy, ' ');
943 *size = atof (str_size);
950 *face = gnome_font_face_find_closest (copy);
954 static PangoFontDescription *
955 font_description_from_gnome_font_name (const char *font_name)
958 PangoFontDescription *desc;
963 face_and_size_from_full_name (font_name, &face, &size);
965 /* Pango and GnomePrint have basically the same numeric weight values */
966 weight = (PangoWeight) gnome_font_face_get_weight_code (face);
967 style = gnome_font_face_is_italic (face) ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
969 desc = pango_font_description_new ();
970 pango_font_description_set_family (desc, gnome_font_face_get_family_name (face));
971 pango_font_description_set_weight (desc, weight);
972 pango_font_description_set_style (desc, style);
973 pango_font_description_set_size (desc, size * PANGO_SCALE);
975 g_object_unref (face);
980 /* ---- TextStyle functions */
983 text_style_new (GtkSourcePrintJob *job, GtkTextTag *tag)
986 gboolean bg_set, fg_set;
988 g_return_val_if_fail (tag != NULL && GTK_IS_TEXT_TAG (tag), NULL);
990 style = g_new0 (TextStyle, 1);
992 g_object_get (G_OBJECT (tag),
993 "background_set", &bg_set,
994 "foreground_set", &fg_set,
995 "font_desc", &style->font_desc,
996 "scale", &style->scale,
997 "underline", &style->underline,
998 "strikethrough", &style->strikethrough,
1002 g_object_get (G_OBJECT (tag), "foreground_gdk", &style->foreground, NULL);
1005 g_object_get (G_OBJECT (tag), "background_gdk", &style->background, NULL);
1011 text_style_free (TextStyle *style)
1013 pango_font_description_free (style->font_desc);
1014 if (style->foreground)
1015 gdk_color_free (style->foreground);
1016 if (style->background)
1017 gdk_color_free (style->background);
1022 get_style (GtkSourcePrintJob *job, const GtkTextIter *iter)
1025 GtkTextTag *tag = NULL;
1026 TextStyle *style = NULL;
1028 if (job->priv->tag_styles == NULL)
1030 job->priv->tag_styles = g_hash_table_new_full (
1031 g_direct_hash, g_direct_equal,
1032 NULL, (GDestroyNotify) text_style_free);
1035 /* get the tags at iter */
1036 tags = gtk_text_iter_get_tags (iter);
1038 /* now find the GtkSourceTag (if any) which applies at iter */
1039 /* FIXME: this makes the assumption that the style at a given
1040 * iter is only determined by one GtkSourceTag (the one with
1041 * highest priority). This is true for now, but could change
1042 * in the future - Gustavo */
1046 if (GTK_IS_TEXT_TAG (t->data))
1048 t = g_slist_next (t);
1050 g_slist_free (tags);
1052 /* now we lookup the tag style in the cache */
1055 style = g_hash_table_lookup (job->priv->tag_styles, tag);
1058 /* create a style for the tag and cache it */
1059 style = text_style_new (job, tag);
1060 g_hash_table_insert (job->priv->tag_styles, tag, style);
1067 /* ----- Text fetching functions */
1070 get_text_simple (GtkSourcePrintJob *job,
1076 while (gtk_text_iter_compare (start, end) < 0)
1081 /* get a line of text */
1083 if (!gtk_text_iter_ends_line (&iter))
1084 gtk_text_iter_forward_to_line_end (&iter);
1086 if (gtk_text_iter_compare (&iter, end) > 0)
1090 seg = g_new0 (TextSegment, 1);
1091 seg->next = NULL; /* only one segment per line, since there's no style change */
1092 seg->style = NULL; /* use default style */
1093 /* FIXME: handle invisible text properly. This also
1094 * assumes the text has no embedded images and
1096 seg->text = gtk_text_iter_get_slice (start, &iter);
1098 para = g_new0 (Paragraph, 1);
1099 para->segment = seg;
1101 /* add the line of text to the job */
1102 job->priv->paragraphs = g_slist_prepend (job->priv->paragraphs, para);
1104 gtk_text_iter_forward_line (&iter);
1106 /* advance to next line */
1109 job->priv->paragraphs = g_slist_reverse (job->priv->paragraphs);
1115 get_text_with_style (GtkSourcePrintJob *job,
1119 GtkTextIter limit, next_toggle;
1120 gboolean have_toggle;
1121 GdkPixbuf *image = NULL;
1123 /* make sure the region to print is highlighted */
1124 /*_gtk_source_buffer_highlight_region (job->priv->buffer, start, end, TRUE); */
1126 next_toggle = *start;
1127 have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);
1129 /* FIXME: handle invisible text properly. This also assumes
1130 * the text has no embedded images and stuff */
1131 while (gtk_text_iter_compare (start, end) < 0)
1137 para = g_new0 (Paragraph, 1);
1139 /* get the style at the start of the line */
1140 style = get_style (job, start);
1142 /* get a line of text - limit points to the end of the line */
1144 if (!gtk_text_iter_ends_line (&limit))
1145 gtk_text_iter_forward_to_line_end (&limit);
1147 if (gtk_text_iter_compare (&limit, end) > 0)
1150 /* create the first segment for the line */
1151 para->segment = seg = g_new0 (TextSegment, 1);
1154 /* while the next tag toggle is within the line, we check to see
1155 * if the style has changed at each tag toggle position, and if so,
1156 * create new segments */
1157 while (have_toggle && gtk_text_iter_compare (&next_toggle, &limit) < 0)
1159 /* check style changes */
1160 style = get_style (job, &next_toggle);
1161 if (style != seg->style)
1163 TextSegment *new_seg;
1164 /* style has changed, thus we need to
1165 * create a new segment */
1166 /* close the current segment */
1167 seg->text = gtk_text_iter_get_slice (start, &next_toggle);
1168 if ((image = gtk_text_iter_get_pixbuf(start)) != NULL)
1171 *start = next_toggle;
1173 new_seg = g_new0 (TextSegment, 1);
1174 seg->next = new_seg;
1179 have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);
1182 /* close the line */
1184 seg->text = gtk_text_iter_get_slice (start, &limit);
1185 if ((image = gtk_text_iter_get_pixbuf(start)) != NULL)
1188 /* add the line of text to the job */
1189 job->priv->paragraphs = g_slist_prepend (job->priv->paragraphs, para);
1191 /* advance to next line */
1193 gtk_text_iter_forward_line (start);
1195 if (gtk_text_iter_compare (&next_toggle, start) < 0) {
1196 next_toggle = *start;
1197 have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);
1200 job->priv->paragraphs = g_slist_reverse (job->priv->paragraphs);
1206 get_text_to_print (GtkSourcePrintJob *job,
1207 const GtkTextIter *start,
1208 const GtkTextIter *end)
1210 GtkTextIter _start, _end;
1213 g_return_val_if_fail (start != NULL && end != NULL, FALSE);
1214 g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
1219 /* erase any previous data */
1220 if (job->priv->paragraphs != NULL)
1222 free_paragraphs (job->priv->paragraphs);
1223 job->priv->paragraphs = NULL;
1225 if (job->priv->tag_styles != NULL)
1227 g_hash_table_destroy (job->priv->tag_styles);
1228 job->priv->tag_styles = NULL;
1231 /* provide ordered iters */
1232 gtk_text_iter_order (&_start, &_end);
1234 /* save the first and last line numbers for future reference */
1235 job->priv->first_line_number = gtk_text_iter_get_line (&_start) + 1;
1236 job->priv->last_line_number = gtk_text_iter_get_line (&_end) + 1;
1238 if (!job->priv->highlight)
1239 retval = get_text_simple (job, &_start, &_end);
1241 retval = get_text_with_style (job, &_start, &_end);
1243 if (retval && job->priv->paragraphs == NULL)
1248 /* add an empty line to allow printing empty documents */
1249 seg = g_new0 (TextSegment, 1);
1251 seg->style = NULL; /* use default style */
1252 seg->text = g_strdup ("");
1254 para = g_new0 (Paragraph, 1);
1255 para->segment = seg;
1257 job->priv->paragraphs = g_slist_prepend (job->priv->paragraphs, para);
1263 /* ----- Pagination functions */
1266 add_attribute_to_list (PangoAttribute *attr,
1267 PangoAttrList *list,
1271 attr->start_index = index;
1272 attr->end_index = index + len;
1273 pango_attr_list_insert (list, attr);
1277 create_layout_for_para (GtkSourcePrintJob *job,
1282 PangoLayout *layout;
1283 PangoAttrList *attrs;
1286 GdkPixbuf *image = NULL;
1287 text = g_string_new (NULL);
1288 attrs = pango_attr_list_new ();
1290 seg = para->segment;
1295 gsize seg_len = strlen (seg->text);
1296 g_string_append (text, seg->text);
1300 PangoAttribute *attr;
1302 attr = pango_attr_font_desc_new (seg->style->font_desc);
1303 add_attribute_to_list (attr, attrs, index, seg_len);
1305 if (seg->style->scale != PANGO_SCALE_MEDIUM)
1307 attr = pango_attr_scale_new (seg->style->scale);
1308 add_attribute_to_list (attr, attrs, index, seg_len);
1311 if (seg->style->foreground)
1313 attr = pango_attr_foreground_new (seg->style->foreground->red,
1314 seg->style->foreground->green,
1315 seg->style->foreground->blue);
1316 add_attribute_to_list (attr, attrs, index, seg_len);
1319 if (seg->style->background)
1321 attr = pango_attr_background_new (seg->style->background->red,
1322 seg->style->background->green,
1323 seg->style->background->blue);
1324 add_attribute_to_list (attr, attrs, index, seg_len);
1327 if (seg->style->strikethrough)
1329 attr = pango_attr_strikethrough_new (TRUE);
1330 add_attribute_to_list (attr, attrs, index, seg_len);
1333 if (seg->style->underline != PANGO_UNDERLINE_NONE)
1335 attr = pango_attr_underline_new (seg->style->underline);
1336 add_attribute_to_list (attr, attrs, index, seg_len);
1348 if (image != NULL) {
1353 layout = pango_layout_new (job->priv->pango_context);
1355 pango_layout_set_width (layout, job->priv->text_width * PANGO_SCALE);
1357 switch (job->priv->wrap_mode) {
1359 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1362 pango_layout_set_wrap (layout, PANGO_WRAP_WORD);
1364 case GTK_WRAP_WORD_CHAR:
1365 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
1369 * Ellipsize the paragraph when text wrapping is disabled.
1370 * Another possibility would be to set the width so the text
1371 * breaks into multiple lines, and paginate/render just the
1373 * See also Comment #23 by Owen on bug #143874.
1376 /* orph says to comment this out and commit it.
1377 PANGO_ELLIPSIZE_END is not available in pango
1378 1.4.1, at least, and he says this code is never
1380 /*pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);*/
1385 if (job->priv->tab_array)
1386 pango_layout_set_tabs (layout, job->priv->tab_array);
1388 pango_layout_set_text (layout, text->str, text->len);
1389 pango_layout_set_attributes (layout, attrs);
1392 /* FIXME: <horrible-hack>
1393 * For empty paragraphs, pango_layout_iter_get_baseline() returns 0,
1394 * so I check this condition and add a space character to force
1395 * the calculation of the baseline. I don't like that, but I
1396 * didn't find a better way to do it. Note that a paragraph is
1397 * considered empty either when it has no characters, or when
1399 * See comment #22 and #23 on bug #143874.
1401 if (job->priv->print_numbers > 0)
1403 PangoLayoutIter *iter;
1404 iter = pango_layout_get_iter (layout);
1405 if (pango_layout_iter_get_baseline (iter) == 0)
1407 g_string_append_c (text, ' ');
1408 pango_layout_set_text (layout, text->str, text->len);
1410 pango_layout_iter_free (iter);
1412 /* FIXME: </horrible-hack> */
1414 g_string_free (text, TRUE);
1415 pango_attr_list_unref (attrs);
1420 /* The break logic in this function needs to match that in print_paragraph */
1422 paginate_paragraph (GtkSourcePrintJob *job,
1425 PangoLayout *layout;
1426 PangoLayoutIter *iter;
1427 PangoRectangle logical_rect;
1432 gboolean is_image = FALSE;
1434 tmp = create_layout_for_para (job, para, &is_image);
1436 layout = (PangoLayout *)tmp;
1439 image = (GdkPixbuf *)tmp;
1443 if (image == NULL) {
1444 iter = pango_layout_get_iter (layout);
1451 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1452 max = (gdouble) (logical_rect.y + logical_rect.height) / PANGO_SCALE;
1454 if (max - page_skip > job->priv->available_height)
1456 /* "create" a new page */
1457 job->priv->page_count++;
1458 job->priv->available_height = job->priv->text_height;
1459 page_skip = (gdouble) logical_rect.y / PANGO_SCALE;
1463 while (pango_layout_iter_next_line (iter));
1465 job->priv->available_height -= max - page_skip;
1467 pango_layout_iter_free (iter);
1468 g_object_unref (layout);
1470 gint max_height = job->priv->available_height;
1471 gint image_height = gdk_pixbuf_get_height(image);
1472 gint image_width = gdk_pixbuf_get_width(image);
1473 gint scaled_height = 0, scaled_width = 0;
1474 image_viewer_get_resized_size(image_width,
1476 job->priv->text_width,
1477 job->priv->text_height,
1478 &scaled_width, &scaled_height);
1480 if (scaled_height > max_height) {
1481 job->priv->page_count++;
1482 job->priv->available_height = job->priv->text_height;
1484 job->priv->available_height -= scaled_height;
1491 paginate_text (GtkSourcePrintJob *job)
1496 /* set these to zero so the first break_line creates a new page */
1497 job->priv->page_count = 0;
1498 job->priv->available_height = 0;
1499 line_number = job->priv->first_line_number;
1500 l = job->priv->paragraphs;
1503 Paragraph *para = l->data;
1505 para->line_number = line_number;
1506 paginate_paragraph (job, para);
1509 l = g_slist_next (l);
1512 /* FIXME: do we have any error condition which can force us to
1513 * return %FALSE? - Gustavo */
1517 /* ---- Printing functions */
1520 begin_page (GtkSourcePrintJob *job)
1522 gnome_print_beginpage (job->priv->print_ctxt, NULL);
1524 g_signal_emit (job, print_job_signals [BEGIN_PAGE], 0);
1528 end_page (GtkSourcePrintJob *job)
1530 gnome_print_showpage (job->priv->print_ctxt);
1534 print_line_number (GtkSourcePrintJob *job,
1539 PangoLayout *layout;
1541 layout = get_line_number_layout (job, line_number);
1543 x = x + job->priv->numbers_width - get_layout_width (layout) - NUMBERS_TEXT_SEPARATION;
1544 gnome_print_moveto (job->priv->print_ctxt, x, y);
1546 show_first_layout_line (job->priv->print_ctxt, layout);
1548 g_object_unref (layout);
1551 /* The break logic in this function needs to match that in paginate_paragraph
1553 * @start_line is the first line in the paragraph to print
1554 * @y is updated to the position after the portion of the paragraph we printed
1555 * @baseline_out is set to the baseline of the first line of the paragraph
1556 * if we printed it. (And not set otherwise)
1558 * Returns the first unprinted line in the paragraph (unprinted because it
1559 * flowed onto the next page) or -1 if the entire paragraph was printed.
1562 print_paragraph (GtkSourcePrintJob *job,
1567 gdouble *baseline_out,
1570 PangoLayout *layout;
1571 PangoLayoutIter *iter;
1572 PangoRectangle logical_rect;
1582 tmp = create_layout_for_para (job, para, &is_image);
1584 layout = (PangoLayout *)tmp;
1587 image = (GdkPixbuf *)tmp;
1592 iter = pango_layout_get_iter (layout);
1594 /* Skip over lines already printed on previous page(s) */
1595 for (current_line = 0; current_line < start_line; current_line++)
1596 pango_layout_iter_next_line (iter);
1603 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1604 max = (gdouble) (logical_rect.y + logical_rect.height) / PANGO_SCALE;
1606 if (current_line == start_line)
1607 page_skip = (gdouble) logical_rect.y / PANGO_SCALE;
1609 if (max - page_skip > job->priv->available_height)
1611 result = current_line; /* Save position for next page */
1615 baseline = (gdouble) pango_layout_iter_get_baseline (iter) / PANGO_SCALE;
1616 baseline = *y + page_skip - baseline; /* Adjust to global coordinates */
1617 if (current_line == 0)
1618 *baseline_out = baseline;
1620 gnome_print_moveto (job->priv->print_ctxt,
1621 x + (gdouble) logical_rect.x / PANGO_SCALE,
1623 gnome_print_pango_layout_line (job->priv->print_ctxt,
1624 pango_layout_iter_get_line (iter));
1628 while (pango_layout_iter_next_line (iter));
1630 job->priv->available_height -= max - page_skip;
1631 *y -= max - page_skip;
1633 pango_layout_iter_free (iter);
1634 g_object_unref (layout);
1636 gint max_height = job->priv->available_height;
1637 gint image_height = gdk_pixbuf_get_height(image);
1638 gint image_width = gdk_pixbuf_get_width(image);
1639 gint scaled_height = 0, scaled_width = 0;
1640 GdkPixbuf *scaled_image = NULL;
1641 image_viewer_get_resized_size(image_width,
1643 job->priv->text_width,
1644 job->priv->text_height,
1645 &scaled_width, &scaled_height);
1647 if (scaled_height > max_height) {
1651 scaled_image = gdk_pixbuf_scale_simple
1652 (image, scaled_width, scaled_height,
1653 GDK_INTERP_BILINEAR);
1655 gnome_print_moveto(job->priv->print_ctxt,
1657 gnome_print_gsave(job->priv->print_ctxt);
1658 gnome_print_translate(job->priv->print_ctxt,
1659 x, *y - scaled_height);
1660 gnome_print_scale(job->priv->print_ctxt,
1664 if (gdk_pixbuf_get_has_alpha(image))
1665 gnome_print_rgbaimage (job->priv->print_ctxt,
1666 gdk_pixbuf_get_pixels (scaled_image),
1667 gdk_pixbuf_get_width (scaled_image),
1668 gdk_pixbuf_get_height (scaled_image),
1669 gdk_pixbuf_get_rowstride (scaled_image));
1671 gnome_print_rgbimage (job->priv->print_ctxt,
1672 gdk_pixbuf_get_pixels (scaled_image),
1673 gdk_pixbuf_get_width (scaled_image),
1674 gdk_pixbuf_get_height (scaled_image),
1675 gdk_pixbuf_get_rowstride (scaled_image));
1676 g_object_unref(scaled_image);
1677 gnome_print_grestore(job->priv->print_ctxt);
1679 job->priv->available_height -= scaled_height;
1680 *y -= scaled_height;
1689 print_page (GtkSourcePrintJob *job)
1694 gboolean force_fit = TRUE;
1700 job->priv->available_height = job->priv->text_height;
1702 y = job->priv->page_height -
1703 job->priv->doc_margin_top - job->priv->margin_top -
1704 job->priv->header_height;
1705 x = job->priv->doc_margin_left + job->priv->margin_left +
1706 job->priv->numbers_width;
1707 l = job->priv->current_paragraph;
1708 line = job->priv->current_paragraph_line;
1712 Paragraph *para = l->data;
1714 gint last_line = line;
1716 line = print_paragraph (job, para, line, x, &y, &baseline, force_fit);
1718 if (last_line == 0 && line != 0)
1720 /* We printed the first line of a paragraph */
1721 if (job->priv->print_numbers > 0 &&
1722 ((para->line_number % job->priv->print_numbers) == 0))
1723 print_line_number (job,
1725 job->priv->doc_margin_left +
1726 job->priv->margin_left,
1729 job->priv->printed_lines++;
1733 break; /* Didn't all fit on this page */
1740 job->priv->current_paragraph = l;
1741 job->priv->current_paragraph_line = line;
1745 setup_for_print (GtkSourcePrintJob *job)
1747 job->priv->current_paragraph = job->priv->paragraphs;
1748 job->priv->page = 0;
1749 job->priv->printed_lines = 0;
1751 if (job->priv->print_job != NULL)
1752 g_object_unref (job->priv->print_job);
1753 if (job->priv->print_ctxt != NULL)
1754 g_object_unref (job->priv->print_ctxt);
1756 job->priv->print_job = gnome_print_job_new (job->priv->config);
1757 job->priv->print_ctxt = gnome_print_job_get_context (job->priv->print_job);
1759 gnome_print_pango_update_context (job->priv->pango_context, job->priv->print_ctxt);
1763 print_job (GtkSourcePrintJob *job)
1765 while (job->priv->current_paragraph != NULL)
1768 gnome_print_job_close (job->priv->print_job);
1772 idle_printing_handler (GtkSourcePrintJob *job)
1774 g_assert (job->priv->current_paragraph != NULL);
1778 if (job->priv->current_paragraph == NULL)
1780 gnome_print_job_close (job->priv->print_job);
1781 job->priv->printing = FALSE;
1782 job->priv->idle_printing_tag = 0;
1784 g_signal_emit (job, print_job_signals [FINISHED], 0);
1785 /* after this the print job object is possibly
1786 * destroyed (common use case) */
1794 /* Public API ------------------- */
1797 * gtk_source_print_job_new:
1798 * @config: an optional #GnomePrintConfig object.
1800 * Creates a new print job object, initially setting the print configuration.
1802 * Return value: the new print job object.
1805 gtk_source_print_job_new (GnomePrintConfig *config)
1807 GtkSourcePrintJob *job;
1809 g_return_val_if_fail (config == NULL || GNOME_IS_PRINT_CONFIG (config), NULL);
1811 job = GTK_SOURCE_PRINT_JOB (g_object_new (GTK_TYPE_SOURCE_PRINT_JOB, NULL));
1813 gtk_source_print_job_set_config (job, config);
1819 * gtk_source_print_job_new_with_buffer:
1820 * @config: an optional #GnomePrintConfig.
1821 * @buffer: the #GtkTextBuffer to print (might be %NULL).
1823 * Creates a new print job to print @buffer.
1825 * Return value: a new print job object.
1828 gtk_source_print_job_new_with_buffer (GnomePrintConfig *config,
1829 GtkTextBuffer *buffer)
1831 GtkSourcePrintJob *job;
1833 g_return_val_if_fail (config == NULL || GNOME_IS_PRINT_CONFIG (config), NULL);
1834 g_return_val_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer), NULL);
1836 job = gtk_source_print_job_new (config);
1838 gtk_source_print_job_set_buffer (job, buffer);
1843 /* --- print job basic configuration */
1846 * gtk_source_print_job_set_config:
1847 * @job: a #GtkSourcePrintJob.
1848 * @config: a #GnomePrintConfig object to get printing configuration from.
1850 * Sets the print configuration for the job. If you don't set a
1851 * configuration object for the print job, when needed one will be
1852 * created with gnome_print_config_default().
1855 gtk_source_print_job_set_config (GtkSourcePrintJob *job,
1856 GnomePrintConfig *config)
1858 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1859 g_return_if_fail (GNOME_IS_PRINT_CONFIG (config));
1860 g_return_if_fail (!job->priv->printing);
1862 if (config == job->priv->config)
1865 if (job->priv->config != NULL)
1866 gnome_print_config_unref (job->priv->config);
1868 job->priv->config = config;
1869 gnome_print_config_ref (config);
1871 g_object_notify (G_OBJECT (job), "config");
1875 * gtk_source_print_job_get_config:
1876 * @job: a #GtkSourcePrintJob.
1878 * Gets the current #GnomePrintConfig the print job will use. If not
1879 * previously set, this will create a default configuration and return
1880 * it. The returned object reference is owned by the print job.
1882 * Return value: the #GnomePrintConfig for the print job.
1885 gtk_source_print_job_get_config (GtkSourcePrintJob *job)
1887 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
1889 ensure_print_config (job);
1891 return job->priv->config;
1895 * gtk_source_print_job_set_buffer:
1896 * @job: a #GtkSourcePrintJob.
1897 * @buffer: a #GtkTextBuffer.
1899 * Sets the #GtkTextBuffer the print job will print. You need to
1900 * specify a buffer to print, either by the use of this function or by
1901 * creating the print job with gtk_source_print_job_new_with_buffer().
1904 gtk_source_print_job_set_buffer (GtkSourcePrintJob *job,
1905 GtkTextBuffer *buffer)
1907 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1908 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1909 g_return_if_fail (!job->priv->printing);
1911 if (buffer == job->priv->buffer)
1914 if (job->priv->buffer != NULL)
1915 g_object_unref (job->priv->buffer);
1917 job->priv->buffer = buffer;
1918 g_object_ref (buffer);
1920 g_object_notify (G_OBJECT (job), "buffer");
1924 * gtk_source_print_job_get_buffer:
1925 * @job: a #GtkSourcePrintJob.
1927 * Gets the #GtkTextBuffer the print job would print. The returned
1928 * object reference (if non %NULL) is owned by the job object and
1929 * should not be unreferenced.
1931 * Return value: the #GtkTextBuffer to print.
1934 gtk_source_print_job_get_buffer (GtkSourcePrintJob *job)
1936 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
1938 return job->priv->buffer;
1941 /* --- print job layout and style configuration */
1944 * gtk_source_print_job_set_tabs_width:
1945 * @job: a #GtkSourcePrintJob.
1946 * @tabs_width: the number of equivalent spaces for a tabulation.
1948 * Sets the width (in equivalent spaces) of tabulations for the
1949 * printed text. The width in printing units will be calculated as
1950 * the width of a string containing @tabs_width spaces of the default
1951 * font. Tabulation stops are set for the full width of printed text.
1954 gtk_source_print_job_set_tabs_width (GtkSourcePrintJob *job,
1957 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1958 g_return_if_fail (!job->priv->printing);
1960 if (tabs_width == job->priv->tabs_width)
1963 job->priv->tabs_width = tabs_width;
1965 g_object_notify (G_OBJECT (job), "tabs_width");
1969 * gtk_source_print_job_get_tabs_width:
1970 * @job: a #GtkSourcePrintJob.
1972 * Determines the configured width (in equivalent spaces) of
1973 * tabulations. The default value is 8.
1975 * Return value: the width (in equivalent spaces) of a tabulation.
1978 gtk_source_print_job_get_tabs_width (GtkSourcePrintJob *job)
1980 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
1982 return job->priv->tabs_width;
1986 * gtk_source_print_job_set_wrap_mode:
1987 * @job: a #GtkSourcePrintJob.
1988 * @wrap: the wrap mode.
1990 * Sets the wrap mode for lines of text larger than the printable
1991 * width. See #GtkWrapMode for a definition of the possible values.
1994 gtk_source_print_job_set_wrap_mode (GtkSourcePrintJob *job,
1997 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1998 g_return_if_fail (!job->priv->printing);
2000 if (wrap == job->priv->wrap_mode)
2003 job->priv->wrap_mode = wrap;
2005 g_object_notify (G_OBJECT (job), "wrap_mode");
2009 * gtk_source_print_job_get_wrap_mode:
2010 * @job: a #GtkSourcePrintJob.
2012 * Determines the wrapping style for text lines wider than the
2013 * printable width. The default is no wrapping.
2015 * Return value: the current wrapping mode for the print job.
2018 gtk_source_print_job_get_wrap_mode (GtkSourcePrintJob *job)
2020 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), GTK_WRAP_NONE);
2022 return job->priv->wrap_mode;
2026 * gtk_source_print_job_set_highlight:
2027 * @job: a #GtkSourcePrintJob.
2028 * @highlight: %TRUE if the printed text should be highlighted.
2030 * Sets whether the printed text will be highlighted according to the
2031 * buffer rules. Both color and font style are applied.
2034 gtk_source_print_job_set_highlight (GtkSourcePrintJob *job,
2037 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2038 g_return_if_fail (!job->priv->printing);
2040 highlight = (highlight != FALSE);
2042 if (highlight == job->priv->highlight)
2045 job->priv->highlight = highlight;
2047 g_object_notify (G_OBJECT (job), "highlight");
2051 * gtk_source_print_job_get_highlight:
2052 * @job: a #GtkSourcePrintJob.
2054 * Determines if the job is configured to print the text highlighted
2055 * with colors and font styles. Note that highlighting will happen
2056 * only if the buffer to print has highlighting activated.
2058 * Return value: %TRUE if the printed output will be highlighted.
2061 gtk_source_print_job_get_highlight (GtkSourcePrintJob *job)
2063 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2065 return job->priv->highlight;
2069 * gtk_source_print_job_set_font_desc:
2070 * @job: a #GtkSourcePrintJob.
2071 * @desc: the #PangoFontDescription for the default font
2073 * Sets the default font for the printed text.
2076 gtk_source_print_job_set_font_desc (GtkSourcePrintJob *job,
2077 PangoFontDescription *desc)
2079 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2080 g_return_if_fail (desc != NULL);
2081 g_return_if_fail (!job->priv->printing);
2083 desc = pango_font_description_copy (desc);
2084 if (job->priv->font != NULL)
2085 pango_font_description_free (job->priv->font);
2086 job->priv->font = desc;
2087 g_object_freeze_notify (G_OBJECT (job));
2088 g_object_notify (G_OBJECT (job), "font");
2089 g_object_notify (G_OBJECT (job), "font_desc");
2090 g_object_thaw_notify (G_OBJECT (job));
2094 * gtk_source_print_job_set_font:
2095 * @job: a #GtkSourcePrintJob.
2096 * @font_name: the name of the default font.
2098 * Sets the default font for the printed text. @font_name should be a
2099 * <emphasis>full font name</emphasis> GnomePrint can understand
2100 * (e.g. "Monospace Regular 10.0").
2102 * Note that @font_name is a #GnomeFont name not a Pango font
2103 * description string. This function is deprecated since #GnomeFont is
2104 * no longer used when implementing printing for GtkSourceView; you
2105 * should use gtk_source_print_job_set_font_desc() instead.
2108 gtk_source_print_job_set_font (GtkSourcePrintJob *job,
2109 const gchar *font_name)
2111 PangoFontDescription *desc;
2113 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2114 g_return_if_fail (font_name != NULL);
2115 g_return_if_fail (!job->priv->printing);
2117 desc = font_description_from_gnome_font_name (font_name);
2120 gtk_source_print_job_set_font_desc (job, desc);
2121 pango_font_description_free (desc);
2126 * gtk_source_print_job_get_font_desc:
2127 * @job: a #GtkSourcePrintJob.
2129 * Determines the default font to be used for the printed text. The
2130 * returned string is of the form "Fontfamily Style Size",
2131 * for example "Monospace Regular 10.0". The returned value
2132 * should be freed when no longer needed.
2134 * Return value: the current text font description. This value is
2135 * owned by the job and must not be modified or freed.
2137 PangoFontDescription *
2138 gtk_source_print_job_get_font_desc (GtkSourcePrintJob *job)
2140 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2142 ensure_print_config (job);
2144 return job->priv->font;
2148 * gtk_source_print_job_get_font:
2149 * @job: a #GtkSourcePrintJob.
2151 * Determines the default font to be used for the printed text. The
2152 * returned string is of the form "Fontfamily Style Size",
2153 * for example "Monospace Regular 10.0". The returned value
2154 * should be freed when no longer needed.
2156 * Note that the result is a #GnomeFont name not a Pango font
2157 * description string. This function is deprecated since #GnomeFont is
2158 * no longer used when implementing printing for GtkSourceView; you
2159 * should use gtk_source_print_job_get_font_desc() instead.
2161 * Return value: a newly allocated string with the name of the current
2165 gtk_source_print_job_get_font (GtkSourcePrintJob *job)
2167 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2169 ensure_print_config (job);
2171 return font_description_to_gnome_font_name (job->priv->font);
2175 * gtk_source_print_job_setup_from_view:
2176 * @job: a #GtkSourcePrintJob.
2177 * @view: a #GtkSourceView to get configuration from.
2179 * Convenience function to set several configuration options at once,
2180 * so that the printed output matches @view. The options set are
2181 * buffer (if not set already), tabs width, highlighting, wrap mode
2185 gtk_source_print_job_setup_from_view (GtkSourcePrintJob *job,
2188 GtkTextBuffer *buffer = NULL;
2189 PangoContext *pango_context;
2191 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2192 g_return_if_fail (!job->priv->printing);
2194 buffer = gtk_text_view_get_buffer (view);
2196 if (job->priv->buffer == NULL && buffer != NULL)
2197 gtk_source_print_job_set_buffer (job, buffer);
2199 /* gtk_source_print_job_set_tabs_width (job, gtk_source_view_get_tabs_width (view)); */
2200 gtk_source_print_job_set_highlight (job, TRUE);
2201 gtk_source_print_job_set_wrap_mode (job, gtk_text_view_get_wrap_mode (view));
2203 pango_context = gtk_widget_get_pango_context (GTK_WIDGET (view));
2204 gtk_source_print_job_set_font_desc (job,
2205 pango_context_get_font_description (pango_context));
2209 * gtk_source_print_job_set_numbers_font_desc:
2210 * @job: a #GtkSourcePrintJob.
2211 * @desc: the #PangoFontDescription for the font for line numbers, or %NULL
2213 * Sets the font for printing line numbers on the left margin. If
2214 * NULL is supplied, the default font (i.e. the one being used for the
2215 * text) will be used instead.
2218 gtk_source_print_job_set_numbers_font_desc (GtkSourcePrintJob *job,
2219 PangoFontDescription *desc)
2221 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2222 g_return_if_fail (!job->priv->printing);
2225 desc = pango_font_description_copy (desc);
2226 if (job->priv->numbers_font != NULL)
2227 pango_font_description_free (job->priv->numbers_font);
2228 job->priv->numbers_font = desc;
2229 g_object_freeze_notify (G_OBJECT (job));
2230 g_object_notify (G_OBJECT (job), "numbers_font");
2231 g_object_notify (G_OBJECT (job), "numbers_font_desc");
2232 g_object_thaw_notify (G_OBJECT (job));
2236 * gtk_source_print_job_set_numbers_font:
2237 * @job: a #GtkSourcePrintJob.
2238 * @font_name: the full name of the font for line numbers, or %NULL.
2240 * Sets the font for printing line numbers on the left margin. If
2241 * %NULL is supplied, the default font (i.e. the one being used for the
2242 * text) will be used instead.
2244 * Note that @font_name is a #GnomeFont name not a Pango font
2245 * description string. This function is deprecated since #GnomeFont is
2246 * no longer used when implementing printing for GtkSourceView; you
2247 * should use gtk_source_print_job_set_numbers_font_desc() instead.
2250 gtk_source_print_job_set_numbers_font (GtkSourcePrintJob *job,
2251 const gchar *font_name)
2253 PangoFontDescription *desc;
2255 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2256 g_return_if_fail (!job->priv->printing);
2258 if (font_name != NULL)
2260 desc = font_description_from_gnome_font_name (font_name);
2263 gtk_source_print_job_set_numbers_font_desc (job, desc);
2264 pango_font_description_free (desc);
2268 gtk_source_print_job_set_numbers_font (job, NULL);
2272 * gtk_source_print_job_get_numbers_font_desc:
2273 * @job: a #GtkSourcePrintJob.
2275 * Determines the font to be used for the line numbers. This function
2276 * might return %NULL if a specific font for numbers has not been set.
2278 * Return value: the line numbers font description or %NULL. This value is
2279 * owned by the job and must not be modified or freed.
2281 PangoFontDescription *
2282 gtk_source_print_job_get_numbers_font_desc (GtkSourcePrintJob *job)
2284 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2286 return job->priv->numbers_font;
2290 * gtk_source_print_job_get_numbers_font:
2291 * @job: a #GtkSourcePrintJob.
2293 * Determines the font to be used for the line numbers. The returned
2294 * string is of the form "Fontfamily Style Size", for
2295 * example "Monospace Regular 10.0". The returned value
2296 * should be freed when no longer needed. This function might return
2297 * %NULL if a specific font for numbers has not been set.
2299 * Note that the result is a #GnomeFont name not a Pango font
2300 * description string. This function is deprecated since #GnomeFont is
2301 * no longer used when implementing printing for GtkSourceView; you
2302 * should use gtk_source_print_job_get_numbers_font_desc() instead.
2304 * Return value: a newly allocated string with the name of the current
2305 * line numbers font, or %NULL.
2308 gtk_source_print_job_get_numbers_font (GtkSourcePrintJob *job)
2310 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2312 if (job->priv->numbers_font != NULL)
2313 return font_description_to_gnome_font_name (job->priv->numbers_font);
2319 * gtk_source_print_job_set_print_numbers:
2320 * @job: a #GtkSourcePrintJob.
2321 * @interval: interval for printed line numbers.
2323 * Sets the interval for printed line numbers. If @interval is 0 no
2324 * numbers will be printed. If greater than 0, a number will be
2325 * printed every @interval lines (i.e. 1 will print all line numbers).
2328 gtk_source_print_job_set_print_numbers (GtkSourcePrintJob *job,
2331 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2332 g_return_if_fail (!job->priv->printing);
2334 if (interval == job->priv->print_numbers)
2337 job->priv->print_numbers = interval;
2339 g_object_notify (G_OBJECT (job), "print_numbers");
2343 * gtk_source_print_job_get_print_numbers:
2344 * @job: a #GtkSourcePrintJob.
2346 * Determines the interval used for line number printing. If the
2347 * value is 0, no line numbers will be printed. The default value is
2348 * 1 (i.e. numbers printed in all lines).
2350 * Return value: the interval of printed line numbers.
2353 gtk_source_print_job_get_print_numbers (GtkSourcePrintJob *job)
2355 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2357 return job->priv->print_numbers;
2361 * gtk_source_print_job_set_text_margins:
2362 * @job: a #GtkSourcePrintJob.
2363 * @top: the top user margin.
2364 * @bottom: the bottom user margin.
2365 * @left: the left user margin.
2366 * @right: the right user margin.
2368 * Sets the four user margins for the print job. These margins are in
2369 * addition to the document margins provided in the #GnomePrintConfig
2370 * and will not be used for headers, footers or line numbers (those
2371 * are calculated separatedly). You can print in the space allocated
2372 * by these margins by connecting to the <link
2373 * linkend="GtkSourcePrintJob-begin-page">"begin_page"</link> signal. The
2374 * space is around the printed text, and inside the margins specified
2375 * in the #GnomePrintConfig.
2377 * The margin numbers are given in device units. If any of the given
2378 * values is less than 0, that particular margin is not altered by
2382 gtk_source_print_job_set_text_margins (GtkSourcePrintJob *job,
2388 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2389 g_return_if_fail (!job->priv->printing);
2392 job->priv->margin_top = top;
2394 job->priv->margin_bottom = bottom;
2396 job->priv->margin_left = left;
2398 job->priv->margin_right = right;
2402 * gtk_source_print_job_get_text_margins:
2403 * @job: a #GtkSourcePrintJob.
2404 * @top: a pointer to a #gdouble to return the top margin.
2405 * @bottom: a pointer to a #gdouble to return the bottom margin.
2406 * @left: a pointer to a #gdouble to return the left margin.
2407 * @right: a pointer to a #gdouble to return the right margin.
2409 * Determines the user set margins for the job. This function
2410 * retrieves the values previously set by
2411 * gtk_source_print_job_set_text_margins(). The default for all four
2412 * margins is 0. Any of the pointers can be %NULL if you want to
2413 * ignore that value.
2416 gtk_source_print_job_get_text_margins (GtkSourcePrintJob *job,
2422 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2425 *top = job->priv->margin_top;
2427 *bottom = job->priv->margin_bottom;
2429 *left = job->priv->margin_left;
2431 *right = job->priv->margin_right;
2434 /* --- printing operations */
2437 gtk_source_print_job_prepare (GtkSourcePrintJob *job,
2438 const GtkTextIter *start,
2439 const GtkTextIter *end)
2441 PROFILE (GTimer *timer);
2443 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2444 g_return_val_if_fail (!job->priv->printing, FALSE);
2445 g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
2446 g_return_val_if_fail (start != NULL && end != NULL, FALSE);
2448 /* make sure we have a sane configuration to start printing */
2449 ensure_print_config (job);
2451 PROFILE (timer = g_timer_new ());
2453 /* get the text to print */
2454 if (!get_text_to_print (job, start, end))
2457 PROFILE (g_message ("get_text_to_print: %.2f", g_timer_elapsed (timer, NULL)));
2459 if (!setup_pango_context (job))
2463 if (!update_page_size_and_margins (job))
2466 /* split the document in pages */
2467 if (!paginate_text (job))
2471 g_message ("paginate_text: %.2f", g_timer_elapsed (timer, NULL));
2472 g_timer_destroy (timer);
2479 * gtk_source_print_job_print:
2480 * @job: a configured #GtkSourcePrintJob.
2482 * Produces a #GnomePrintJob with the printed document. The whole
2483 * contents of the configured #GtkTextBuffer are printed. The
2484 * returned job is already closed and ready to be previewed (using
2485 * gnome_print_job_preview_new()) or printed directly. The caller of
2486 * this function owns a reference to the returned object, so @job can
2487 * be destroyed and the output will still be valid, or the document
2488 * can be printed again with different settings.
2490 * Return value: a closed #GnomePrintJob with the printed document, or
2491 * %NULL if printing could not be completed.
2494 gtk_source_print_job_print (GtkSourcePrintJob *job)
2496 GtkTextIter start, end;
2498 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2499 g_return_val_if_fail (!job->priv->printing, NULL);
2500 g_return_val_if_fail (job->priv->buffer != NULL, NULL);
2502 gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (job->priv->buffer), &start, &end);
2504 return gtk_source_print_job_print_range (job, &start, &end);
2508 * gtk_source_print_job_print_range:
2509 * @job: a configured #GtkSourcePrintJob.
2510 * @start: the start of the region of text to print.
2511 * @end: the end of the region of text to print.
2513 * Similar to gtk_source_print_job_print(), except you can specify a
2514 * range of text to print. The passed #GtkTextIter values might be
2517 * Return value: a closed #GnomePrintJob with the text from @start to
2518 * @end printed, or %NULL if @job could not print.
2521 gtk_source_print_job_print_range (GtkSourcePrintJob *job,
2522 const GtkTextIter *start,
2523 const GtkTextIter *end)
2525 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2526 g_return_val_if_fail (!job->priv->printing, NULL);
2527 g_return_val_if_fail (job->priv->buffer != NULL, NULL);
2528 g_return_val_if_fail (start != NULL && end != NULL, NULL);
2529 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
2530 GTK_TEXT_BUFFER (job->priv->buffer) &&
2531 gtk_text_iter_get_buffer (end) ==
2532 GTK_TEXT_BUFFER (job->priv->buffer), NULL);
2534 if (!gtk_source_print_job_prepare (job, start, end))
2537 /* real work starts here */
2538 setup_for_print (job);
2540 job->priv->printing = TRUE;
2542 job->priv->printing = FALSE;
2544 g_object_ref (job->priv->print_job);
2545 return job->priv->print_job;
2548 /* --- asynchronous printing */
2551 * gtk_source_print_job_print_range_async:
2552 * @job: a configured #GtkSourcePrintJob.
2553 * @start: the start of the region of text to print.
2554 * @end: the end of the region of text to print.
2556 * Starts to print @job asynchronously. This function will ready the
2557 * @job for printing and install an idle handler that will render one
2560 * This function will not return immediatly, as only page rendering is
2561 * done asynchronously. Text retrieval and paginating happens within
2562 * this function. Also, if highlighting is enabled, the whole buffer
2563 * needs to be highlighted first.
2565 * To get notification when the job has finished, you must connect to
2567 * linkend="GtkSourcePrintJob-finished">"finished"</link>
2568 * signal. After this signal is emitted you can retrieve the
2569 * resulting #GnomePrintJob with gtk_source_print_job_get_print_job().
2570 * You may cancel the job with gtk_source_print_job_cancel().
2572 * Return value: %TRUE if the print started.
2575 gtk_source_print_job_print_range_async (GtkSourcePrintJob *job,
2576 const GtkTextIter *start,
2577 const GtkTextIter *end)
2579 GSource *idle_source;
2581 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2582 g_return_val_if_fail (!job->priv->printing, FALSE);
2583 g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
2584 g_return_val_if_fail (start != NULL && end != NULL, FALSE);
2585 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
2586 GTK_TEXT_BUFFER (job->priv->buffer) &&
2587 gtk_text_iter_get_buffer (end) ==
2588 GTK_TEXT_BUFFER (job->priv->buffer), FALSE);
2590 if (!gtk_source_print_job_prepare (job, start, end))
2593 /* real work starts here */
2594 setup_for_print (job);
2595 if (job->priv->current_paragraph == NULL)
2598 /* setup the idle handler to print each page at a time */
2599 idle_source = g_idle_source_new ();
2600 g_source_set_priority (idle_source, GTK_SOURCE_PRINT_JOB_PRIORITY);
2601 g_source_set_closure (idle_source,
2602 g_cclosure_new_object ((GCallback) idle_printing_handler,
2604 job->priv->idle_printing_tag = g_source_attach (idle_source, NULL);
2605 g_source_unref (idle_source);
2607 job->priv->printing = TRUE;
2613 * gtk_source_print_job_cancel:
2614 * @job: a #GtkSourcePrintJob.
2616 * Cancels an asynchronous printing operation. This will remove any
2617 * pending print idle handler and unref the current #GnomePrintJob.
2619 * Note that if you got a reference to the job's #GnomePrintJob (using
2620 * gtk_source_print_job_get_print_job()) it will not be destroyed
2621 * (since you hold a reference to it), but it will not be closed
2622 * either. If you wish to show or print the partially printed
2623 * document you need to close it yourself.
2625 * This function has no effect when called from a non-asynchronous
2629 gtk_source_print_job_cancel (GtkSourcePrintJob *job)
2631 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2632 g_return_if_fail (job->priv->printing);
2634 if (job->priv->idle_printing_tag > 0)
2636 g_source_remove (job->priv->idle_printing_tag);
2637 job->priv->current_paragraph = NULL;
2638 job->priv->idle_printing_tag = 0;
2639 job->priv->printing = FALSE;
2640 g_object_unref (job->priv->print_job);
2641 g_object_unref (job->priv->print_ctxt);
2642 job->priv->print_job = NULL;
2643 job->priv->print_ctxt = NULL;
2648 * gtk_source_print_job_get_print_job:
2649 * @job: a #GtkSourcePrintJob.
2651 * Gets a reference to the #GnomePrintJob which the @job is printing
2652 * or has recently finished printing. You need to unref the returned
2655 * You may call this function in the middle of an asynchronous
2656 * printing operation, but the returned #GnomePrintJob will not be
2657 * closed until the last page is printed and the <link
2658 * linkend="GtkSourcePrintJob-finished">"finished"</link>
2659 * signal is emitted.
2661 * Return value: a new reference to the @job's #GnomePrintJob, or
2665 gtk_source_print_job_get_print_job (GtkSourcePrintJob *job)
2667 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2669 if (job->priv->print_job)
2670 g_object_ref (job->priv->print_job);
2672 return job->priv->print_job;
2675 /* --- information for asynchronous ops and headers and footers callback */
2678 * gtk_source_print_job_get_page:
2679 * @job: a #GtkSourcePrintJob.
2681 * Determines the currently printing page number. This function is
2682 * only valid while printing (either synchronously or asynchronously).
2684 * Return value: the current page number.
2687 gtk_source_print_job_get_page (GtkSourcePrintJob *job)
2689 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2690 g_return_val_if_fail (job->priv->printing, 0);
2692 return job->priv->page;
2696 * gtk_source_print_job_get_page_count:
2697 * @job: a #GtkSourcePrintJob.
2699 * Determines the total number of pages the job will print. The
2700 * returned value is only meaninful after pagination has finished. In
2701 * practice, for synchronous printing this means when <link
2702 * linkend="GtkSourcePrintJob-begin-page">"begin_page"</link>
2703 * is emitted, or after gtk_source_print_job_print_range_async() has
2706 * Return value: the number of pages of the printed job.
2709 gtk_source_print_job_get_page_count (GtkSourcePrintJob *job)
2711 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2713 return job->priv->page_count;
2717 * gtk_source_print_job_get_print_context:
2718 * @job: a printing #GtkSourcePrintJob.
2720 * Determines the #GnomePrintContext of the current job. This
2721 * function is only valid while printing. Normally you would use this
2722 * function to print in the margins set by
2723 * gtk_source_print_job_set_margins() in a handler for the <link
2724 * linkend="GtkSourcePrintJob-begin-page">"begin_page"</link>
2727 * Return value: the current #GnomePrintContext. The returned object
2731 gtk_source_print_job_get_print_context (GtkSourcePrintJob *job)
2733 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2734 g_return_val_if_fail (job->priv->printing, NULL);
2736 return job->priv->print_ctxt;
2739 /* ---- Header and footer (default implementation) */
2741 /* Most of this code taken from GLib's g_date_strftime() in gdate.c
2742 * GLIB - Library of useful routines for C programming
2743 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald */
2746 strdup_strftime (const gchar *format, const struct tm *tm)
2748 gsize locale_format_len = 0;
2749 gchar *locale_format;
2755 GError *error = NULL;
2757 g_return_val_if_fail (format != NULL, NULL);
2758 g_return_val_if_fail (tm != NULL, NULL);
2760 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
2764 g_warning (G_STRLOC "Error converting format to locale encoding: %s",
2766 g_error_free (error);
2771 tmpbufsize = MAX (128, locale_format_len * 2);
2774 tmpbuf = g_malloc (tmpbufsize);
2776 /* Set the first byte to something other than '\0', to be able to
2777 * recognize whether strftime actually failed or just returned "".
2780 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, tm);
2782 if (tmplen == 0 && tmpbuf[0] != '\0')
2787 if (tmpbufsize > 65536)
2789 g_warning (G_STRLOC "Maximum buffer size for strdup_strftime "
2790 "exceeded: giving up");
2791 g_free (locale_format);
2798 g_free (locale_format);
2800 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
2805 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s",
2807 g_error_free (error);
2816 evaluate_format_string (GtkSourcePrintJob *job, const gchar *format)
2819 gchar *eval_str, *retval;
2820 const struct tm *tm;
2826 tm = localtime (&now);
2828 /* analyze format string and replace the codes we know */
2829 eval = g_string_new_len (NULL, strlen (format));
2830 ch = g_utf8_get_char (format);
2835 format = g_utf8_next_char (format);
2836 ch = g_utf8_get_char (format);
2838 g_string_append_printf (eval, "%d", job->priv->page);
2840 g_string_append_printf (eval, "%d", job->priv->page_count);
2843 g_string_append_c (eval, '%');
2844 g_string_append_unichar (eval, ch);
2848 g_string_append_unichar (eval, ch);
2850 format = g_utf8_next_char (format);
2851 ch = g_utf8_get_char (format);
2854 eval_str = g_string_free (eval, FALSE);
2855 retval = strdup_strftime (eval_str, tm);
2862 print_header_footer_string (GtkSourcePrintJob *job,
2863 const gchar *format,
2868 PangoLayout *layout;
2873 width = job->priv->text_width + job->priv->numbers_width;
2875 text = evaluate_format_string (job, format);
2878 layout = pango_layout_new (job->priv->pango_context);
2879 pango_layout_set_font_description (layout, job->priv->header_footer_font);
2880 pango_layout_set_text (layout, text, -1);
2882 xx = x + x_align * (width - get_layout_width (layout));
2883 gnome_print_moveto (job->priv->print_ctxt, xx, y);
2884 show_first_layout_line (job->priv->print_ctxt, layout);
2887 g_object_unref (layout);
2892 default_print_header (GtkSourcePrintJob *job,
2898 gdouble ascent, descent;
2900 width = job->priv->text_width + job->priv->numbers_width;
2902 get_font_ascent_descent (job, job->priv->header_footer_font, &ascent, &descent);
2907 if (job->priv->header_format_left != NULL)
2908 print_header_footer_string (job, job->priv->header_format_left, 0.0, x, yy);
2911 if (job->priv->header_format_right != NULL)
2912 print_header_footer_string (job, job->priv->header_format_right, 1.0, x, yy);
2915 if (job->priv->header_format_center != NULL)
2916 print_header_footer_string (job, job->priv->header_format_center, 0.5, x, yy);
2919 if (job->priv->header_separator)
2921 yy = y - (SEPARATOR_SPACING * (ascent + descent));
2922 gnome_print_setlinewidth (job->priv->print_ctxt, SEPARATOR_LINE_WIDTH);
2923 gnome_print_moveto (job->priv->print_ctxt, x, yy);
2924 gnome_print_lineto (job->priv->print_ctxt, x + width, yy);
2925 gnome_print_stroke (job->priv->print_ctxt);
2930 default_print_footer (GtkSourcePrintJob *job,
2936 gdouble ascent, descent;
2938 width = job->priv->text_width + job->priv->numbers_width;
2940 get_font_ascent_descent (job, job->priv->header_footer_font, &ascent, &descent);
2942 yy = y - job->priv->footer_height + descent;
2945 if (job->priv->footer_format_left != NULL)
2946 print_header_footer_string (job, job->priv->footer_format_left, 0.0, x, yy);
2949 if (job->priv->footer_format_right != NULL)
2950 print_header_footer_string (job, job->priv->footer_format_right, 1.0, x, yy);
2953 if (job->priv->footer_format_center != NULL)
2954 print_header_footer_string (job, job->priv->footer_format_center, 0.5, x, yy);
2957 if (job->priv->footer_separator)
2959 yy = y - job->priv->footer_height +
2960 (SEPARATOR_SPACING * (ascent + descent));
2961 gnome_print_setlinewidth (job->priv->print_ctxt, SEPARATOR_LINE_WIDTH);
2962 gnome_print_moveto (job->priv->print_ctxt, x, yy);
2963 gnome_print_lineto (job->priv->print_ctxt, x + width, yy);
2964 gnome_print_stroke (job->priv->print_ctxt);
2969 * gtk_source_print_job_set_print_header:
2970 * @job: a #GtkSourcePrintJob.
2971 * @setting: %TRUE if you want the header to be printed.
2973 * Sets whether you want to print a header in each page. The default
2974 * header consists of three pieces of text and an optional line
2975 * separator, configurable with
2976 * gtk_source_print_job_set_header_format().
2978 * Note that by default the header format is unspecified, and if it's
2979 * empty it will not be printed, regardless of this setting.
2982 gtk_source_print_job_set_print_header (GtkSourcePrintJob *job,
2985 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2986 g_return_if_fail (!job->priv->printing);
2988 setting = (setting != FALSE);
2990 if (setting == job->priv->print_header)
2993 job->priv->print_header = setting;
2995 g_object_notify (G_OBJECT (job), "print_header");
2999 * gtk_source_print_job_get_print_header:
3000 * @job: a #GtkSourcePrintJob.
3002 * Determines if a header is set to be printed for each page. A
3003 * header will be printed if this function returns %TRUE
3004 * <emphasis>and</emphasis> some format strings have been specified
3005 * with gtk_source_print_job_set_header_format().
3007 * Return value: %TRUE if the header is set to be printed.
3010 gtk_source_print_job_get_print_header (GtkSourcePrintJob *job)
3012 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
3014 return job->priv->print_header;
3018 * gtk_source_print_job_set_print_footer:
3019 * @job: a #GtkSourcePrintJob.
3020 * @setting: %TRUE if you want the footer to be printed.
3022 * Sets whether you want to print a footer in each page. The default
3023 * footer consists of three pieces of text and an optional line
3024 * separator, configurable with
3025 * gtk_source_print_job_set_footer_format().
3027 * Note that by default the footer format is unspecified, and if it's
3028 * empty it will not be printed, regardless of this setting.
3031 gtk_source_print_job_set_print_footer (GtkSourcePrintJob *job,
3034 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3035 g_return_if_fail (!job->priv->printing);
3037 setting = (setting != FALSE);
3039 if (setting == job->priv->print_footer)
3042 job->priv->print_footer = setting;
3044 g_object_notify (G_OBJECT (job), "print_footer");
3048 * gtk_source_print_job_get_print_footer:
3049 * @job: a #GtkSourcePrintJob.
3051 * Determines if a footer is set to be printed for each page. A
3052 * footer will be printed if this function returns %TRUE
3053 * <emphasis>and</emphasis> some format strings have been specified
3054 * with gtk_source_print_job_set_footer_format().
3056 * Return value: %TRUE if the footer is set to be printed.
3059 gtk_source_print_job_get_print_footer (GtkSourcePrintJob *job)
3061 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
3063 return job->priv->print_footer;
3067 * gtk_source_print_job_set_header_footer_font_desc:
3068 * @job: a #GtkSourcePrintJob.
3069 * @desc: the #PangoFontDescription for the font to be used in headers and footers, or %NULL.
3071 * Sets the font for printing headers and footers. If %NULL is
3072 * supplied, the default font (i.e. the one being used for the text)
3073 * will be used instead.
3076 gtk_source_print_job_set_header_footer_font_desc (GtkSourcePrintJob *job,
3077 PangoFontDescription *desc)
3079 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3080 g_return_if_fail (!job->priv->printing);
3083 desc = pango_font_description_copy (desc);
3084 if (job->priv->header_footer_font != NULL)
3085 pango_font_description_free (job->priv->header_footer_font);
3086 job->priv->header_footer_font = desc;
3087 g_object_freeze_notify (G_OBJECT (job));
3088 g_object_notify (G_OBJECT (job), "header_footer_font");
3089 g_object_notify (G_OBJECT (job), "header_footer_font_desc");
3090 g_object_thaw_notify (G_OBJECT (job));
3094 * gtk_source_print_job_set_header_footer_font:
3095 * @job: a #GtkSourcePrintJob.
3096 * @font_name: the full name of the font to be used in headers and footers, or %NULL.
3098 * Sets the font for printing headers and footers. If %NULL is
3099 * supplied, the default font (i.e. the one being used for the text)
3100 * will be used instead.
3102 * Note that @font_name is a #GnomeFont name not a Pango font
3103 * description string. This function is deprecated since #GnomeFont is
3104 * no longer used when implementing printing for GtkSourceView; you
3105 * should use gtk_source_print_job_set_header_footer_font_desc() instead.
3108 gtk_source_print_job_set_header_footer_font (GtkSourcePrintJob *job,
3109 const gchar *font_name)
3111 PangoFontDescription *desc;
3113 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3114 g_return_if_fail (!job->priv->printing);
3116 if (font_name != NULL)
3118 desc = font_description_from_gnome_font_name (font_name);
3121 gtk_source_print_job_set_header_footer_font_desc (job, desc);
3122 pango_font_description_free (desc);
3126 gtk_source_print_job_set_header_footer_font_desc (job, NULL);
3130 * gtk_source_print_job_get_header_footer_font_desc:
3131 * @job: a #GtkSourcePrintJob.
3133 * Determines the font to be used for the header and footer. This function
3134 * might return %NULL if a specific font has not been set.
3136 * Return value: the header and footer font description or %NULL. This value is
3137 * owned by the job and must not be modified or freed.
3139 PangoFontDescription *
3140 gtk_source_print_job_get_header_footer_font_desc (GtkSourcePrintJob *job)
3142 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
3144 return job->priv->header_footer_font;
3148 * gtk_source_print_job_get_header_footer_font:
3149 * @job: a #GtkSourcePrintJob.
3151 * Determines the font to be used for the header and footer. The
3152 * returned string is of the form "Fontfamily Style Size",
3153 * for example "Monospace Regular 10.0". The returned value
3154 * should be freed when no longer needed. This function might return
3155 * %NULL if a specific font has not been set.
3157 * Note that the result is a #GnomeFont name not a Pango font
3158 * description string. This function is deprecated since #GnomeFont is
3159 * no longer used when implementing printing for GtkSourceView; you
3160 * should use gtk_source_print_job_get_header_footer_font_desc() instead.
3162 * Return value: a newly allocated string with the name of the current
3163 * header and footer font, or %NULL.
3166 gtk_source_print_job_get_header_footer_font (GtkSourcePrintJob *job)
3168 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
3170 if (job->priv->header_footer_font != NULL)
3171 return font_description_to_gnome_font_name (job->priv->header_footer_font);
3177 * gtk_source_print_job_set_header_format:
3178 * @job: a #GtkSourcePrintJob.
3179 * @left: a format string to print on the left of the header.
3180 * @center: a format string to print on the center of the header.
3181 * @right: a format string to print on the right of the header.
3182 * @separator: %TRUE if you want a separator line to be printed.
3184 * Sets strftime like header format strings, to be printed on the
3185 * left, center and right of the top of each page. The strings may
3186 * include strftime(3) codes which will be expanded at print time.
3187 * All strftime() codes are accepted, with the addition of %N for the
3188 * page number and %Q for the page count.
3190 * @separator specifies if a solid line should be drawn to separate
3191 * the header from the document text.
3193 * If %NULL is given for any of the three arguments, that particular
3194 * string will not be printed. For the header to be printed, in
3195 * addition to specifying format strings, you need to enable header
3196 * printing with gtk_source_print_job_set_print_header().
3199 gtk_source_print_job_set_header_format (GtkSourcePrintJob *job,
3201 const gchar *center,
3205 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3206 g_return_if_fail (!job->priv->printing);
3208 /* FIXME: validate given strings? */
3209 g_free (job->priv->header_format_left);
3210 g_free (job->priv->header_format_center);
3211 g_free (job->priv->header_format_right);
3212 job->priv->header_format_left = g_strdup (left);
3213 job->priv->header_format_center = g_strdup (center);
3214 job->priv->header_format_right = g_strdup (right);
3215 job->priv->header_separator = separator;
3219 * gtk_source_print_job_set_footer_format:
3220 * @job: a #GtkSourcePrintJob.
3221 * @left: a format string to print on the left of the footer.
3222 * @center: a format string to print on the center of the footer.
3223 * @right: a format string to print on the right of the footer.
3224 * @separator: %TRUE if you want a separator line to be printed.
3226 * Like gtk_source_print_job_set_header_format(), but for the footer.
3229 gtk_source_print_job_set_footer_format (GtkSourcePrintJob *job,
3231 const gchar *center,
3235 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3236 g_return_if_fail (!job->priv->printing);
3238 /* FIXME: validate given strings? */
3239 g_free (job->priv->footer_format_left);
3240 g_free (job->priv->footer_format_center);
3241 g_free (job->priv->footer_format_right);
3242 job->priv->footer_format_left = g_strdup (left);
3243 job->priv->footer_format_center = g_strdup (center);
3244 job->priv->footer_format_right = g_strdup (right);
3245 job->priv->footer_separator = separator;