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 3 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, see <http://www.gnu.org/licenses/>.
36 #include "gtksourceprintjob.h"
37 #include "image_viewer.h"
39 #include <glib/gi18n.h>
40 #include <gtk/gtkmain.h>
41 #include <gtk/gtktextview.h>
42 #include <libgnomeprint/gnome-print-pango.h>
57 #define DEFAULT_FONT_NAME "Monospace 10"
58 #define DEFAULT_COLOR 0x000000ff
60 #define CM(v) ((v) * 72.0 / 2.54)
61 #define A4_WIDTH (210.0 * 72 / 25.4)
62 #define A4_HEIGHT (297.0 * 72 / 25.4)
64 #define NUMBERS_TEXT_SEPARATION CM(0.5)
66 #define HEADER_FOOTER_SIZE 2.5
67 #define SEPARATOR_SPACING 1.5
68 #define SEPARATOR_LINE_WIDTH 1.0
71 typedef struct _TextSegment TextSegment;
72 typedef struct _Paragraph Paragraph;
73 typedef struct _TextStyle TextStyle;
75 /* a piece of text (within a paragraph) of the same style */
84 /* a printable line */
91 /* the style of a TextSegment */
94 PangoFontDescription *font_desc;
98 gboolean strikethrough;
99 PangoUnderline underline;
103 struct _GtkSourcePrintJobPrivate
105 /* General job configuration */
106 GnomePrintConfig *config;
107 GtkTextBuffer *buffer;
109 GtkWrapMode wrap_mode;
111 PangoLanguage *language;
112 PangoFontDescription *font;
113 PangoFontDescription *numbers_font;
116 gdouble margin_bottom;
118 gdouble margin_right;
120 /* Default header and footer configuration */
121 gboolean print_header;
122 gboolean print_footer;
123 PangoFontDescription *header_footer_font;
124 gchar *header_format_left;
125 gchar *header_format_center;
126 gchar *header_format_right;
127 gboolean header_separator;
128 gchar *footer_format_left;
129 gchar *footer_format_center;
130 gchar *footer_format_right;
131 gboolean footer_separator;
134 guint first_line_number;
135 guint last_line_number;
140 guint idle_printing_tag;
141 GnomePrintContext *print_ctxt;
142 GnomePrintJob *print_job;
143 PangoContext *pango_context;
144 PangoTabArray *tab_array;
147 gdouble available_height;
148 GSList *current_paragraph;
149 gint current_paragraph_line;
152 /* Cached information - all this information is obtained from
153 * other fields in the configuration */
154 GHashTable *tag_styles;
159 gdouble doc_margin_top;
160 gdouble doc_margin_left;
161 gdouble doc_margin_right;
162 gdouble doc_margin_bottom;
164 gdouble header_height;
165 gdouble footer_height;
166 gdouble numbers_width;
168 /* printable (for the document itself) size */
185 PROP_NUMBERS_FONT_DESC,
189 PROP_HEADER_FOOTER_FONT,
190 PROP_HEADER_FOOTER_FONT_DESC
200 static GObjectClass *parent_class = NULL;
201 static guint print_job_signals [LAST_SIGNAL] = { 0 };
203 static void gtk_source_print_job_class_init (GtkSourcePrintJobClass *klass);
204 static void gtk_source_print_job_instance_init (GtkSourcePrintJob *job);
205 static void gtk_source_print_job_finalize (GObject *object);
206 static void gtk_source_print_job_get_property (GObject *object,
210 static void gtk_source_print_job_set_property (GObject *object,
214 static void gtk_source_print_job_begin_page (GtkSourcePrintJob *job);
216 static void default_print_header (GtkSourcePrintJob *job,
219 static void default_print_footer (GtkSourcePrintJob *job,
225 gtk_source_print_job_get_type (void)
227 static GType our_type = 0;
231 static const GTypeInfo our_info = {
232 sizeof (GtkSourcePrintJobClass),
233 NULL, /* base_init */
234 NULL, /* base_finalize */
235 (GClassInitFunc) gtk_source_print_job_class_init,
236 NULL, /* class_finalize */
237 NULL, /* class_data */
238 sizeof (GtkSourcePrintJob),
240 (GInstanceInitFunc) gtk_source_print_job_instance_init
243 our_type = g_type_register_static (G_TYPE_OBJECT,
253 gtk_source_print_job_class_init (GtkSourcePrintJobClass *klass)
255 GObjectClass *object_class;
257 object_class = G_OBJECT_CLASS (klass);
258 parent_class = g_type_class_peek_parent (klass);
260 object_class->finalize = gtk_source_print_job_finalize;
261 object_class->get_property = gtk_source_print_job_get_property;
262 object_class->set_property = gtk_source_print_job_set_property;
264 klass->begin_page = gtk_source_print_job_begin_page;
265 klass->finished = NULL;
267 g_object_class_install_property (object_class,
269 g_param_spec_object ("config",
271 _("Configuration options for "
273 GNOME_TYPE_PRINT_CONFIG,
275 g_object_class_install_property (object_class,
277 g_param_spec_object ("buffer",
279 _("GtkTextBuffer object to print"),
280 GTK_TYPE_TEXT_BUFFER,
282 g_object_class_install_property (object_class,
284 g_param_spec_uint ("tabs_width",
286 _("Width in equivalent space "
287 "characters of tabs"),
290 g_object_class_install_property (object_class,
292 g_param_spec_enum ("wrap_mode",
294 _("Word wrapping mode"),
298 g_object_class_install_property (object_class,
300 g_param_spec_boolean ("highlight",
302 _("Whether to print the "
303 "document with highlighted "
307 g_object_class_install_property (object_class,
309 g_param_spec_string ("font",
311 _("GnomeFont name to use for the "
312 "document text (deprecated)"),
315 g_object_class_install_property (object_class,
317 g_param_spec_boxed ("font_desc",
318 _("Font Description"),
319 _("Font to use for the document text "
320 "(e.g. \"Monospace 10\")"),
321 PANGO_TYPE_FONT_DESCRIPTION,
323 g_object_class_install_property (object_class,
325 g_param_spec_string ("numbers_font",
327 _("GnomeFont name to use for the "
328 "line numbers (deprecated)"),
331 g_object_class_install_property (object_class,
332 PROP_NUMBERS_FONT_DESC,
333 g_param_spec_boxed ("numbers_font_desc",
335 _("Font description to use for the "
337 PANGO_TYPE_FONT_DESCRIPTION,
339 g_object_class_install_property (object_class,
341 g_param_spec_uint ("print_numbers",
342 _("Print Line Numbers"),
343 _("Interval of printed line numbers "
344 "(0 means no numbers)"),
347 g_object_class_install_property (object_class,
349 g_param_spec_boolean ("print_header",
351 _("Whether to print a header "
355 g_object_class_install_property (object_class,
357 g_param_spec_boolean ("print_footer",
359 _("Whether to print a footer "
363 g_object_class_install_property (object_class,
364 PROP_HEADER_FOOTER_FONT,
365 g_param_spec_string ("header_footer_font",
366 _("Header and Footer Font"),
367 _("GnomeFont name to use for the header "
368 "and footer (deprecated)"),
371 g_object_class_install_property (object_class,
372 PROP_HEADER_FOOTER_FONT_DESC,
373 g_param_spec_boxed ("header_footer_font_desc",
374 _("Header and Footer Font Description"),
375 _("Font to use for headers and footers "
376 "(e.g. \"Monospace 10\")"),
377 PANGO_TYPE_FONT_DESCRIPTION,
380 print_job_signals [BEGIN_PAGE] =
381 g_signal_new ("begin_page",
382 G_OBJECT_CLASS_TYPE (object_class),
384 G_STRUCT_OFFSET (GtkSourcePrintJobClass, begin_page),
386 g_cclosure_marshal_VOID__VOID,
389 print_job_signals [FINISHED] =
390 g_signal_new ("finished",
391 G_OBJECT_CLASS_TYPE (object_class),
393 G_STRUCT_OFFSET (GtkSourcePrintJobClass, finished),
395 g_cclosure_marshal_VOID__VOID,
401 gtk_source_print_job_instance_init (GtkSourcePrintJob *job)
403 GtkSourcePrintJobPrivate *priv;
405 priv = g_new0 (GtkSourcePrintJobPrivate, 1);
408 /* default job configuration */
412 priv->tabs_width = 8;
413 priv->wrap_mode = GTK_WRAP_NONE;
414 priv->highlight = TRUE;
415 priv->language = gtk_get_default_language ();
417 priv->numbers_font = NULL;
418 priv->print_numbers = 1;
419 priv->margin_top = 0.0;
420 priv->margin_bottom = 0.0;
421 priv->margin_left = 0.0;
422 priv->margin_right = 0.0;
424 priv->print_header = FALSE;
425 priv->print_footer = FALSE;
426 priv->header_footer_font = NULL;
427 priv->header_format_left = NULL;
428 priv->header_format_center = NULL;
429 priv->header_format_right = NULL;
430 priv->header_separator = FALSE;
431 priv->footer_format_left = NULL;
432 priv->footer_format_center = NULL;
433 priv->footer_format_right = NULL;
434 priv->footer_separator = FALSE;
437 priv->printing = FALSE;
438 priv->print_ctxt = NULL;
439 priv->print_job = NULL;
441 priv->page_count = 0;
443 priv->first_line_number = 0;
444 priv->paragraphs = NULL;
445 priv->tag_styles = NULL;
447 /* some default, sane values */
448 priv->page_width = A4_WIDTH;
449 priv->page_height = A4_HEIGHT;
450 priv->doc_margin_top = CM (1);
451 priv->doc_margin_left = CM (1);
452 priv->doc_margin_right = CM (1);
453 priv->doc_margin_bottom = CM (1);
457 free_paragraphs (GSList *paras)
459 while (paras != NULL)
461 Paragraph *para = paras->data;
462 TextSegment *seg = para->segment;
465 TextSegment *next = seg->next;
471 paras = g_slist_delete_link (paras, paras);
476 gtk_source_print_job_finalize (GObject *object)
478 GtkSourcePrintJob *job;
479 GtkSourcePrintJobPrivate *priv;
481 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (object));
483 job = GTK_SOURCE_PRINT_JOB (object);
488 if (priv->config != NULL)
489 gnome_print_config_unref (priv->config);
490 if (priv->buffer != NULL)
491 g_object_unref (priv->buffer);
492 if (priv->font != NULL)
493 pango_font_description_free (priv->font);
494 if (priv->numbers_font != NULL)
495 pango_font_description_free (priv->numbers_font);
496 if (priv->header_footer_font != NULL)
497 pango_font_description_free (priv->header_footer_font);
498 g_free (priv->header_format_left);
499 g_free (priv->header_format_right);
500 g_free (priv->header_format_center);
501 g_free (priv->footer_format_left);
502 g_free (priv->footer_format_right);
503 g_free (priv->footer_format_center);
505 if (priv->print_ctxt != NULL)
506 g_object_unref (priv->print_ctxt);
507 if (priv->print_job != NULL)
508 g_object_unref (priv->print_job);
509 if (priv->pango_context != NULL)
510 g_object_unref (priv->pango_context);
511 if (priv->tab_array != NULL)
512 pango_tab_array_free (priv->tab_array);
514 if (priv->paragraphs != NULL)
515 free_paragraphs (priv->paragraphs);
516 if (priv->tag_styles != NULL)
517 g_hash_table_destroy (priv->tag_styles);
523 G_OBJECT_CLASS (parent_class)->finalize (object);
527 gtk_source_print_job_get_property (GObject *object,
532 GtkSourcePrintJob *job = GTK_SOURCE_PRINT_JOB (object);
537 g_value_set_object (value, job->priv->config);
541 g_value_set_object (value, job->priv->buffer);
544 case PROP_TABS_WIDTH:
545 g_value_set_uint (value, job->priv->tabs_width);
549 g_value_set_enum (value, job->priv->wrap_mode);
553 g_value_set_boolean (value, job->priv->highlight);
557 g_value_take_string (value, gtk_source_print_job_get_font (job));
561 g_value_set_boxed (value, gtk_source_print_job_get_font_desc (job));
564 case PROP_NUMBERS_FONT:
565 g_value_take_string (value, gtk_source_print_job_get_numbers_font (job));
568 case PROP_NUMBERS_FONT_DESC:
569 g_value_set_boxed (value, gtk_source_print_job_get_numbers_font_desc (job));
572 case PROP_PRINT_NUMBERS:
573 g_value_set_uint (value, job->priv->print_numbers);
576 case PROP_PRINT_HEADER:
577 g_value_set_boolean (value, job->priv->print_header);
580 case PROP_PRINT_FOOTER:
581 g_value_set_boolean (value, job->priv->print_footer);
584 case PROP_HEADER_FOOTER_FONT:
585 g_value_take_string (value,
586 gtk_source_print_job_get_header_footer_font (job));
589 case PROP_HEADER_FOOTER_FONT_DESC:
590 g_value_set_boxed (value,
591 gtk_source_print_job_get_header_footer_font_desc (job));
595 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
601 gtk_source_print_job_set_property (GObject *object,
606 GtkSourcePrintJob *job = GTK_SOURCE_PRINT_JOB (object);
611 gtk_source_print_job_set_config (job, g_value_get_object (value));
615 gtk_source_print_job_set_buffer (job, g_value_get_object (value));
618 case PROP_TABS_WIDTH:
619 gtk_source_print_job_set_tabs_width (job, g_value_get_uint (value));
623 gtk_source_print_job_set_wrap_mode (job, g_value_get_enum (value));
627 gtk_source_print_job_set_highlight (job, g_value_get_boolean (value));
631 gtk_source_print_job_set_font (job, g_value_get_string (value));
635 gtk_source_print_job_set_font_desc (job, g_value_get_boxed (value));
638 case PROP_NUMBERS_FONT:
639 gtk_source_print_job_set_numbers_font (job, g_value_get_string (value));
642 case PROP_NUMBERS_FONT_DESC:
643 gtk_source_print_job_set_numbers_font_desc (job, g_value_get_boxed (value));
646 case PROP_PRINT_NUMBERS:
647 gtk_source_print_job_set_print_numbers (job, g_value_get_uint (value));
650 case PROP_PRINT_HEADER:
651 gtk_source_print_job_set_print_header (job, g_value_get_boolean (value));
654 case PROP_PRINT_FOOTER:
655 gtk_source_print_job_set_print_footer (job, g_value_get_boolean (value));
658 case PROP_HEADER_FOOTER_FONT:
659 gtk_source_print_job_set_header_footer_font (job,
660 g_value_get_string (value));
663 case PROP_HEADER_FOOTER_FONT_DESC:
664 gtk_source_print_job_set_header_footer_font_desc (job,
665 g_value_get_boxed (value));
669 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
675 gtk_source_print_job_begin_page (GtkSourcePrintJob *job)
677 g_return_if_fail (job->priv->printing);
679 if (job->priv->print_header && job->priv->header_height > 0)
683 x = job->priv->doc_margin_left + job->priv->margin_left;
684 y = job->priv->page_height - job->priv->doc_margin_top - job->priv->margin_top;
685 default_print_header (job, x, y);
688 if (job->priv->print_footer && job->priv->footer_height > 0)
692 x = job->priv->doc_margin_left + job->priv->margin_left;
693 y = job->priv->doc_margin_bottom +
694 job->priv->margin_bottom +
695 job->priv->footer_height;
696 default_print_footer (job, x, y);
700 /* ---- gnome-print / Pango convenience functions */
702 /* Gets the width of a layout in gnome-print coordinates */
704 get_layout_width (PangoLayout *layout)
708 pango_layout_get_size (layout, &layout_width, NULL);
709 return (gdouble) layout_width / PANGO_SCALE;
712 /* Gets the ascent/descent of a font in gnome-print coordinates */
714 get_font_ascent_descent (GtkSourcePrintJob *job,
715 PangoFontDescription *desc,
719 PangoFontMetrics *metrics;
721 metrics = pango_context_get_metrics (job->priv->pango_context,
723 job->priv->language);
726 *ascent = (gdouble) pango_font_metrics_get_ascent (metrics) / PANGO_SCALE;
728 *descent = (gdouble) pango_font_metrics_get_descent (metrics) / PANGO_SCALE;
730 pango_font_metrics_unref (metrics);
733 /* Draws the first line in a layout; we use this for one-line layouts
734 * to get baseline alignment */
736 show_first_layout_line (GnomePrintContext *print_ctxt,
739 PangoLayoutLine *line;
741 line = pango_layout_get_lines (layout)->data;
742 gnome_print_pango_layout_line (print_ctxt, line);
746 get_line_number_layout (GtkSourcePrintJob *job,
752 num_str = g_strdup_printf ("%d", line_number);
753 layout = pango_layout_new (job->priv->pango_context);
754 pango_layout_set_font_description (layout, job->priv->numbers_font);
755 pango_layout_set_text (layout, num_str, -1);
761 /* ---- Configuration functions */
764 ensure_print_config (GtkSourcePrintJob *job)
766 if (job->priv->config == NULL)
767 job->priv->config = gnome_print_config_default ();
768 if (job->priv->font == NULL)
769 job->priv->font = pango_font_description_from_string (DEFAULT_FONT_NAME);
773 update_page_size_and_margins (GtkSourcePrintJob *job)
776 gdouble ascent, descent;
778 gnome_print_job_get_page_size_from_config (job->priv->config,
779 &job->priv->page_width,
780 &job->priv->page_height);
782 gnome_print_config_get_length (job->priv->config, GNOME_PRINT_KEY_PAGE_MARGIN_TOP,
783 &job->priv->doc_margin_top, NULL);
784 gnome_print_config_get_length (job->priv->config, GNOME_PRINT_KEY_PAGE_MARGIN_BOTTOM,
785 &job->priv->doc_margin_bottom, NULL);
786 gnome_print_config_get_length (job->priv->config, GNOME_PRINT_KEY_PAGE_MARGIN_LEFT,
787 &job->priv->doc_margin_left, NULL);
788 gnome_print_config_get_length (job->priv->config, GNOME_PRINT_KEY_PAGE_MARGIN_RIGHT,
789 &job->priv->doc_margin_right, NULL);
791 /* set default fonts for numbers and header/footer */
792 if (job->priv->numbers_font == NULL)
793 job->priv->numbers_font = pango_font_description_copy (job->priv->font);
795 if (job->priv->header_footer_font == NULL)
796 job->priv->header_footer_font = pango_font_description_copy (job->priv->font);
798 /* calculate numbers width */
799 if (job->priv->print_numbers > 0)
801 layout = get_line_number_layout (job, job->priv->last_line_number);
802 job->priv->numbers_width = get_layout_width (layout) + NUMBERS_TEXT_SEPARATION;
803 g_object_unref (layout);
806 job->priv->numbers_width = 0.0;
808 get_font_ascent_descent (job, job->priv->header_footer_font, &ascent, &descent);
810 /* calculate header/footer height */
811 if (job->priv->print_header &&
812 (job->priv->header_format_left != NULL ||
813 job->priv->header_format_center != NULL ||
814 job->priv->header_format_right != NULL))
815 job->priv->header_height = HEADER_FOOTER_SIZE * (ascent + descent);
817 job->priv->header_height = 0.0;
819 if (job->priv->print_footer &&
820 (job->priv->footer_format_left != NULL ||
821 job->priv->footer_format_center != NULL ||
822 job->priv->footer_format_right != NULL))
823 job->priv->footer_height = HEADER_FOOTER_SIZE * (ascent + descent);
825 job->priv->footer_height = 0.0;
827 /* verify that the user provided margins are not too excesive
828 * and that we still have room for the text */
829 job->priv->text_width = (job->priv->page_width -
830 job->priv->doc_margin_left - job->priv->doc_margin_right -
831 job->priv->margin_left - job->priv->margin_right -
832 job->priv->numbers_width);
834 job->priv->text_height = (job->priv->page_height -
835 job->priv->doc_margin_top - job->priv->doc_margin_bottom -
836 job->priv->margin_top - job->priv->margin_bottom -
837 job->priv->header_height - job->priv->footer_height);
839 /* FIXME: put some saner values than 5cm - Gustavo */
840 g_return_val_if_fail (job->priv->text_width > CM(5.0), FALSE);
841 g_return_val_if_fail (job->priv->text_height > CM(5.0), FALSE);
846 /* We want a uniform tab width for the entire job without regard to style
847 * See comments in gtksourceview.c:calculate_real_tab_width
850 calculate_real_tab_width (GtkSourcePrintJob *job, guint tab_size, gchar c)
859 tab_string = g_strnfill (tab_size, c);
860 layout = pango_layout_new (job->priv->pango_context);
861 pango_layout_set_text (layout, tab_string, -1);
864 pango_layout_get_size (layout, &tab_width, NULL);
865 g_object_unref (G_OBJECT (layout));
871 setup_pango_context (GtkSourcePrintJob *job)
873 PangoFontMap *font_map;
876 if (!job->priv->pango_context)
878 font_map = gnome_print_pango_get_default_font_map ();
879 job->priv->pango_context = gnome_print_pango_create_context (font_map);
882 pango_context_set_language (job->priv->pango_context, job->priv->language);
883 pango_context_set_font_description (job->priv->pango_context, job->priv->font);
885 if (job->priv->tab_array)
887 pango_tab_array_free (job->priv->tab_array);
888 job->priv->tab_array = NULL;
891 real_tab_width = calculate_real_tab_width (job, job->priv->tabs_width, ' ');
892 if (real_tab_width > 0)
894 job->priv->tab_array = pango_tab_array_new (1, FALSE);
895 pango_tab_array_set_tab (job->priv->tab_array, 0, PANGO_TAB_LEFT, real_tab_width);
901 /* ----- Helper functions */
904 font_description_to_gnome_font_name (PangoFontDescription *desc)
906 GnomeFontFace *font_face;
909 /* Will always return some font */
910 font_face = gnome_font_face_find_closest_from_pango_description (desc);
912 retval = g_strdup_printf("%s %f",
913 gnome_font_face_get_name (font_face),
914 (double) pango_font_description_get_size (desc) / PANGO_SCALE);
915 g_object_unref (font_face);
921 * The following routines are duplicated in gedit/gedit/gedit-prefs-manager.c
924 /* Do this ourselves since gnome_font_find_closest() doesn't call
925 * gnome_font_face_find_closest() (probably a gnome-print bug)
928 face_and_size_from_full_name (const guchar *name,
929 GnomeFontFace **face,
935 copy = g_strdup (name);
936 str_size = strrchr (copy, ' ');
941 *size = atof (str_size);
948 *face = gnome_font_face_find_closest (copy);
952 static PangoFontDescription *
953 font_description_from_gnome_font_name (const char *font_name)
956 PangoFontDescription *desc;
961 face_and_size_from_full_name (font_name, &face, &size);
963 /* Pango and GnomePrint have basically the same numeric weight values */
964 weight = (PangoWeight) gnome_font_face_get_weight_code (face);
965 style = gnome_font_face_is_italic (face) ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
967 desc = pango_font_description_new ();
968 pango_font_description_set_family (desc, gnome_font_face_get_family_name (face));
969 pango_font_description_set_weight (desc, weight);
970 pango_font_description_set_style (desc, style);
971 pango_font_description_set_size (desc, size * PANGO_SCALE);
973 g_object_unref (face);
978 /* ---- TextStyle functions */
981 text_style_new (GtkSourcePrintJob *job, GtkTextTag *tag)
984 gboolean bg_set, fg_set;
986 g_return_val_if_fail (tag != NULL && GTK_IS_TEXT_TAG (tag), NULL);
988 style = g_new0 (TextStyle, 1);
990 g_object_get (G_OBJECT (tag),
991 "background_set", &bg_set,
992 "foreground_set", &fg_set,
993 "font_desc", &style->font_desc,
994 "scale", &style->scale,
995 "underline", &style->underline,
996 "strikethrough", &style->strikethrough,
1000 g_object_get (G_OBJECT (tag), "foreground_gdk", &style->foreground, NULL);
1003 g_object_get (G_OBJECT (tag), "background_gdk", &style->background, NULL);
1009 text_style_free (TextStyle *style)
1011 pango_font_description_free (style->font_desc);
1012 if (style->foreground)
1013 gdk_color_free (style->foreground);
1014 if (style->background)
1015 gdk_color_free (style->background);
1020 get_style (GtkSourcePrintJob *job, const GtkTextIter *iter)
1023 GtkTextTag *tag = NULL;
1024 TextStyle *style = NULL;
1026 if (job->priv->tag_styles == NULL)
1028 job->priv->tag_styles = g_hash_table_new_full (
1029 g_direct_hash, g_direct_equal,
1030 NULL, (GDestroyNotify) text_style_free);
1033 /* get the tags at iter */
1034 tags = gtk_text_iter_get_tags (iter);
1036 /* now find the GtkSourceTag (if any) which applies at iter */
1037 /* FIXME: this makes the assumption that the style at a given
1038 * iter is only determined by one GtkSourceTag (the one with
1039 * highest priority). This is true for now, but could change
1040 * in the future - Gustavo */
1044 if (GTK_IS_TEXT_TAG (t->data))
1046 t = g_slist_next (t);
1048 g_slist_free (tags);
1050 /* now we lookup the tag style in the cache */
1053 style = g_hash_table_lookup (job->priv->tag_styles, tag);
1056 /* create a style for the tag and cache it */
1057 style = text_style_new (job, tag);
1058 g_hash_table_insert (job->priv->tag_styles, tag, style);
1065 /* ----- Text fetching functions */
1068 get_text_simple (GtkSourcePrintJob *job,
1074 while (gtk_text_iter_compare (start, end) < 0)
1079 /* get a line of text */
1081 if (!gtk_text_iter_ends_line (&iter))
1082 gtk_text_iter_forward_to_line_end (&iter);
1084 if (gtk_text_iter_compare (&iter, end) > 0)
1088 seg = g_new0 (TextSegment, 1);
1089 seg->next = NULL; /* only one segment per line, since there's no style change */
1090 seg->style = NULL; /* use default style */
1091 /* FIXME: handle invisible text properly. This also
1092 * assumes the text has no embedded images and
1094 seg->text = gtk_text_iter_get_slice (start, &iter);
1096 para = g_new0 (Paragraph, 1);
1097 para->segment = seg;
1099 /* add the line of text to the job */
1100 job->priv->paragraphs = g_slist_prepend (job->priv->paragraphs, para);
1102 gtk_text_iter_forward_line (&iter);
1104 /* advance to next line */
1107 job->priv->paragraphs = g_slist_reverse (job->priv->paragraphs);
1113 get_text_with_style (GtkSourcePrintJob *job,
1117 GtkTextIter limit, next_toggle;
1118 gboolean have_toggle;
1119 GdkPixbuf *image = NULL;
1121 /* make sure the region to print is highlighted */
1122 /*_gtk_source_buffer_highlight_region (job->priv->buffer, start, end, TRUE); */
1124 next_toggle = *start;
1125 have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);
1127 /* FIXME: handle invisible text properly. This also assumes
1128 * the text has no embedded images and stuff */
1129 while (gtk_text_iter_compare (start, end) < 0)
1135 para = g_new0 (Paragraph, 1);
1137 /* get the style at the start of the line */
1138 style = get_style (job, start);
1140 /* get a line of text - limit points to the end of the line */
1142 if (!gtk_text_iter_ends_line (&limit))
1143 gtk_text_iter_forward_to_line_end (&limit);
1145 if (gtk_text_iter_compare (&limit, end) > 0)
1148 /* create the first segment for the line */
1149 para->segment = seg = g_new0 (TextSegment, 1);
1152 /* while the next tag toggle is within the line, we check to see
1153 * if the style has changed at each tag toggle position, and if so,
1154 * create new segments */
1155 while (have_toggle && gtk_text_iter_compare (&next_toggle, &limit) < 0)
1157 /* check style changes */
1158 style = get_style (job, &next_toggle);
1159 if (style != seg->style)
1161 TextSegment *new_seg;
1162 /* style has changed, thus we need to
1163 * create a new segment */
1164 /* close the current segment */
1165 seg->text = gtk_text_iter_get_slice (start, &next_toggle);
1166 if ((image = gtk_text_iter_get_pixbuf(start)) != NULL)
1169 *start = next_toggle;
1171 new_seg = g_new0 (TextSegment, 1);
1172 seg->next = new_seg;
1177 have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);
1180 /* close the line */
1182 seg->text = gtk_text_iter_get_slice (start, &limit);
1183 if ((image = gtk_text_iter_get_pixbuf(start)) != NULL)
1186 /* add the line of text to the job */
1187 job->priv->paragraphs = g_slist_prepend (job->priv->paragraphs, para);
1189 /* advance to next line */
1191 gtk_text_iter_forward_line (start);
1193 if (gtk_text_iter_compare (&next_toggle, start) < 0) {
1194 next_toggle = *start;
1195 have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);
1198 job->priv->paragraphs = g_slist_reverse (job->priv->paragraphs);
1204 get_text_to_print (GtkSourcePrintJob *job,
1205 const GtkTextIter *start,
1206 const GtkTextIter *end)
1208 GtkTextIter _start, _end;
1211 g_return_val_if_fail (start != NULL && end != NULL, FALSE);
1212 g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
1217 /* erase any previous data */
1218 if (job->priv->paragraphs != NULL)
1220 free_paragraphs (job->priv->paragraphs);
1221 job->priv->paragraphs = NULL;
1223 if (job->priv->tag_styles != NULL)
1225 g_hash_table_destroy (job->priv->tag_styles);
1226 job->priv->tag_styles = NULL;
1229 /* provide ordered iters */
1230 gtk_text_iter_order (&_start, &_end);
1232 /* save the first and last line numbers for future reference */
1233 job->priv->first_line_number = gtk_text_iter_get_line (&_start) + 1;
1234 job->priv->last_line_number = gtk_text_iter_get_line (&_end) + 1;
1236 if (!job->priv->highlight)
1237 retval = get_text_simple (job, &_start, &_end);
1239 retval = get_text_with_style (job, &_start, &_end);
1241 if (retval && job->priv->paragraphs == NULL)
1246 /* add an empty line to allow printing empty documents */
1247 seg = g_new0 (TextSegment, 1);
1249 seg->style = NULL; /* use default style */
1250 seg->text = g_strdup ("");
1252 para = g_new0 (Paragraph, 1);
1253 para->segment = seg;
1255 job->priv->paragraphs = g_slist_prepend (job->priv->paragraphs, para);
1261 /* ----- Pagination functions */
1264 add_attribute_to_list (PangoAttribute *attr,
1265 PangoAttrList *list,
1269 attr->start_index = index;
1270 attr->end_index = index + len;
1271 pango_attr_list_insert (list, attr);
1275 create_layout_for_para (GtkSourcePrintJob *job,
1280 PangoLayout *layout;
1281 PangoAttrList *attrs;
1284 GdkPixbuf *image = NULL;
1285 text = g_string_new (NULL);
1286 attrs = pango_attr_list_new ();
1288 seg = para->segment;
1293 gsize seg_len = strlen (seg->text);
1294 g_string_append (text, seg->text);
1298 PangoAttribute *attr;
1300 attr = pango_attr_font_desc_new (seg->style->font_desc);
1301 add_attribute_to_list (attr, attrs, index, seg_len);
1303 if (seg->style->scale != PANGO_SCALE_MEDIUM)
1305 attr = pango_attr_scale_new (seg->style->scale);
1306 add_attribute_to_list (attr, attrs, index, seg_len);
1309 if (seg->style->foreground)
1311 attr = pango_attr_foreground_new (seg->style->foreground->red,
1312 seg->style->foreground->green,
1313 seg->style->foreground->blue);
1314 add_attribute_to_list (attr, attrs, index, seg_len);
1317 if (seg->style->background)
1319 attr = pango_attr_background_new (seg->style->background->red,
1320 seg->style->background->green,
1321 seg->style->background->blue);
1322 add_attribute_to_list (attr, attrs, index, seg_len);
1325 if (seg->style->strikethrough)
1327 attr = pango_attr_strikethrough_new (TRUE);
1328 add_attribute_to_list (attr, attrs, index, seg_len);
1331 if (seg->style->underline != PANGO_UNDERLINE_NONE)
1333 attr = pango_attr_underline_new (seg->style->underline);
1334 add_attribute_to_list (attr, attrs, index, seg_len);
1346 if (image != NULL) {
1351 layout = pango_layout_new (job->priv->pango_context);
1353 pango_layout_set_width (layout, job->priv->text_width * PANGO_SCALE);
1355 switch (job->priv->wrap_mode) {
1357 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1360 pango_layout_set_wrap (layout, PANGO_WRAP_WORD);
1362 case GTK_WRAP_WORD_CHAR:
1363 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
1367 * Ellipsize the paragraph when text wrapping is disabled.
1368 * Another possibility would be to set the width so the text
1369 * breaks into multiple lines, and paginate/render just the
1371 * See also Comment #23 by Owen on bug #143874.
1374 /* orph says to comment this out and commit it.
1375 PANGO_ELLIPSIZE_END is not available in pango
1376 1.4.1, at least, and he says this code is never
1378 /*pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);*/
1383 if (job->priv->tab_array)
1384 pango_layout_set_tabs (layout, job->priv->tab_array);
1386 pango_layout_set_text (layout, text->str, text->len);
1387 pango_layout_set_attributes (layout, attrs);
1390 /* FIXME: <horrible-hack>
1391 * For empty paragraphs, pango_layout_iter_get_baseline() returns 0,
1392 * so I check this condition and add a space character to force
1393 * the calculation of the baseline. I don't like that, but I
1394 * didn't find a better way to do it. Note that a paragraph is
1395 * considered empty either when it has no characters, or when
1397 * See comment #22 and #23 on bug #143874.
1399 if (job->priv->print_numbers > 0)
1401 PangoLayoutIter *iter;
1402 iter = pango_layout_get_iter (layout);
1403 if (pango_layout_iter_get_baseline (iter) == 0)
1405 g_string_append_c (text, ' ');
1406 pango_layout_set_text (layout, text->str, text->len);
1408 pango_layout_iter_free (iter);
1410 /* FIXME: </horrible-hack> */
1412 g_string_free (text, TRUE);
1413 pango_attr_list_unref (attrs);
1418 /* The break logic in this function needs to match that in print_paragraph */
1420 paginate_paragraph (GtkSourcePrintJob *job,
1423 PangoLayout *layout;
1424 PangoLayoutIter *iter;
1425 PangoRectangle logical_rect;
1430 gboolean is_image = FALSE;
1432 tmp = create_layout_for_para (job, para, &is_image);
1434 layout = (PangoLayout *)tmp;
1437 image = (GdkPixbuf *)tmp;
1441 if (image == NULL) {
1442 iter = pango_layout_get_iter (layout);
1449 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1450 max = (gdouble) (logical_rect.y + logical_rect.height) / PANGO_SCALE;
1452 if (max - page_skip > job->priv->available_height)
1454 /* "create" a new page */
1455 job->priv->page_count++;
1456 job->priv->available_height = job->priv->text_height;
1457 page_skip = (gdouble) logical_rect.y / PANGO_SCALE;
1461 while (pango_layout_iter_next_line (iter));
1462 job->priv->available_height -= max - page_skip;
1463 pango_layout_iter_free (iter);
1464 g_object_unref (layout);
1466 gint max_height = job->priv->available_height;
1467 gint image_height = gdk_pixbuf_get_height(image);
1468 gint image_width = gdk_pixbuf_get_width(image);
1469 gint scaled_height = 0, scaled_width = 0;
1470 image_viewer_get_resized_size(image_width,
1472 job->priv->text_width,
1473 job->priv->text_height,
1474 &scaled_width, &scaled_height);
1475 if (scaled_height > max_height) {
1476 job->priv->page_count++;
1477 job->priv->available_height = job->priv->text_height - scaled_height;
1479 job->priv->available_height -= scaled_height;
1486 paginate_text (GtkSourcePrintJob *job)
1491 /* set these to zero so the first break_line creates a new page */
1492 job->priv->page_count = 0;
1493 job->priv->available_height = 0;
1494 line_number = job->priv->first_line_number;
1495 l = job->priv->paragraphs;
1498 Paragraph *para = l->data;
1500 para->line_number = line_number;
1501 paginate_paragraph (job, para);
1504 l = g_slist_next (l);
1507 /* FIXME: do we have any error condition which can force us to
1508 * return %FALSE? - Gustavo */
1512 /* ---- Printing functions */
1515 begin_page (GtkSourcePrintJob *job)
1517 gnome_print_beginpage (job->priv->print_ctxt, NULL);
1519 g_signal_emit (job, print_job_signals [BEGIN_PAGE], 0);
1523 end_page (GtkSourcePrintJob *job)
1525 gnome_print_showpage (job->priv->print_ctxt);
1529 print_line_number (GtkSourcePrintJob *job,
1534 PangoLayout *layout;
1536 layout = get_line_number_layout (job, line_number);
1538 x = x + job->priv->numbers_width - get_layout_width (layout) - NUMBERS_TEXT_SEPARATION;
1539 gnome_print_moveto (job->priv->print_ctxt, x, y);
1541 show_first_layout_line (job->priv->print_ctxt, layout);
1543 g_object_unref (layout);
1546 /* The break logic in this function needs to match that in paginate_paragraph
1548 * @start_line is the first line in the paragraph to print
1549 * @y is updated to the position after the portion of the paragraph we printed
1550 * @baseline_out is set to the baseline of the first line of the paragraph
1551 * if we printed it. (And not set otherwise)
1553 * Returns the first unprinted line in the paragraph (unprinted because it
1554 * flowed onto the next page) or -1 if the entire paragraph was printed.
1557 print_paragraph (GtkSourcePrintJob *job,
1562 gdouble *baseline_out,
1565 PangoLayout *layout;
1566 PangoLayoutIter *iter;
1567 PangoRectangle logical_rect;
1577 tmp = create_layout_for_para (job, para, &is_image);
1579 layout = (PangoLayout *)tmp;
1582 image = (GdkPixbuf *)tmp;
1587 iter = pango_layout_get_iter (layout);
1589 /* Skip over lines already printed on previous page(s) */
1590 for (current_line = 0; current_line < start_line; current_line++)
1591 pango_layout_iter_next_line (iter);
1598 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1599 max = (gdouble) (logical_rect.y + logical_rect.height) / PANGO_SCALE;
1601 if (current_line == start_line)
1602 page_skip = (gdouble) logical_rect.y / PANGO_SCALE;
1604 if (max - page_skip > job->priv->available_height)
1606 result = current_line; /* Save position for next page */
1610 baseline = (gdouble) pango_layout_iter_get_baseline (iter) / PANGO_SCALE;
1611 baseline = *y + page_skip - baseline; /* Adjust to global coordinates */
1612 if (current_line == 0)
1613 *baseline_out = baseline;
1615 gnome_print_moveto (job->priv->print_ctxt,
1616 x + (gdouble) logical_rect.x / PANGO_SCALE,
1618 gnome_print_pango_layout_line (job->priv->print_ctxt,
1619 pango_layout_iter_get_line (iter));
1623 while (pango_layout_iter_next_line (iter));
1625 job->priv->available_height -= max - page_skip;
1626 *y -= max - page_skip;
1628 pango_layout_iter_free (iter);
1629 g_object_unref (layout);
1631 gint max_height = job->priv->available_height;
1632 gint image_height = gdk_pixbuf_get_height(image);
1633 gint image_width = gdk_pixbuf_get_width(image);
1634 gint scaled_height = 0, scaled_width = 0;
1635 GdkPixbuf *scaled_image = NULL;
1636 image_viewer_get_resized_size(image_width,
1638 job->priv->text_width,
1639 job->priv->text_height,
1640 &scaled_width, &scaled_height);
1642 if (scaled_height > max_height) {
1646 scaled_image = gdk_pixbuf_scale_simple
1647 (image, scaled_width, scaled_height,
1648 GDK_INTERP_BILINEAR);
1650 gnome_print_moveto(job->priv->print_ctxt,
1652 gnome_print_gsave(job->priv->print_ctxt);
1653 gnome_print_translate(job->priv->print_ctxt,
1654 x, *y - scaled_height);
1655 gnome_print_scale(job->priv->print_ctxt,
1659 if (gdk_pixbuf_get_has_alpha(image))
1660 gnome_print_rgbaimage (job->priv->print_ctxt,
1661 gdk_pixbuf_get_pixels (scaled_image),
1662 gdk_pixbuf_get_width (scaled_image),
1663 gdk_pixbuf_get_height (scaled_image),
1664 gdk_pixbuf_get_rowstride (scaled_image));
1666 gnome_print_rgbimage (job->priv->print_ctxt,
1667 gdk_pixbuf_get_pixels (scaled_image),
1668 gdk_pixbuf_get_width (scaled_image),
1669 gdk_pixbuf_get_height (scaled_image),
1670 gdk_pixbuf_get_rowstride (scaled_image));
1671 g_object_unref(scaled_image);
1672 gnome_print_grestore(job->priv->print_ctxt);
1674 job->priv->available_height -= scaled_height;
1675 *y -= scaled_height;
1684 print_page (GtkSourcePrintJob *job)
1689 gboolean force_fit = TRUE;
1695 job->priv->available_height = job->priv->text_height;
1697 y = job->priv->page_height -
1698 job->priv->doc_margin_top - job->priv->margin_top -
1699 job->priv->header_height;
1700 x = job->priv->doc_margin_left + job->priv->margin_left +
1701 job->priv->numbers_width;
1702 l = job->priv->current_paragraph;
1703 line = job->priv->current_paragraph_line;
1707 Paragraph *para = l->data;
1708 gdouble baseline = 0;
1709 gint last_line = line;
1711 line = print_paragraph (job, para, line, x, &y, &baseline, force_fit);
1713 if (last_line == 0 && line != 0)
1715 /* We printed the first line of a paragraph */
1716 if (job->priv->print_numbers > 0 &&
1717 ((para->line_number % job->priv->print_numbers) == 0))
1718 print_line_number (job,
1720 job->priv->doc_margin_left +
1721 job->priv->margin_left,
1724 job->priv->printed_lines++;
1728 break; /* Didn't all fit on this page */
1735 job->priv->current_paragraph = l;
1736 job->priv->current_paragraph_line = line;
1740 setup_for_print (GtkSourcePrintJob *job)
1742 job->priv->current_paragraph = job->priv->paragraphs;
1743 job->priv->page = 0;
1744 job->priv->printed_lines = 0;
1746 if (job->priv->print_job != NULL)
1747 g_object_unref (job->priv->print_job);
1748 if (job->priv->print_ctxt != NULL)
1749 g_object_unref (job->priv->print_ctxt);
1751 job->priv->print_job = gnome_print_job_new (job->priv->config);
1752 job->priv->print_ctxt = gnome_print_job_get_context (job->priv->print_job);
1754 gnome_print_pango_update_context (job->priv->pango_context, job->priv->print_ctxt);
1758 print_job (GtkSourcePrintJob *job)
1760 while (job->priv->current_paragraph != NULL)
1763 gnome_print_job_close (job->priv->print_job);
1767 idle_printing_handler (GtkSourcePrintJob *job)
1769 g_assert (job->priv->current_paragraph != NULL);
1773 if (job->priv->current_paragraph == NULL)
1775 gnome_print_job_close (job->priv->print_job);
1776 job->priv->printing = FALSE;
1777 job->priv->idle_printing_tag = 0;
1779 g_signal_emit (job, print_job_signals [FINISHED], 0);
1780 /* after this the print job object is possibly
1781 * destroyed (common use case) */
1789 /* Public API ------------------- */
1792 * gtk_source_print_job_new:
1793 * @config: an optional #GnomePrintConfig object.
1795 * Creates a new print job object, initially setting the print configuration.
1797 * Return value: the new print job object.
1800 gtk_source_print_job_new (GnomePrintConfig *config)
1802 GtkSourcePrintJob *job;
1804 g_return_val_if_fail (config == NULL || GNOME_IS_PRINT_CONFIG (config), NULL);
1806 job = GTK_SOURCE_PRINT_JOB (g_object_new (GTK_TYPE_SOURCE_PRINT_JOB, NULL));
1808 gtk_source_print_job_set_config (job, config);
1814 * gtk_source_print_job_new_with_buffer:
1815 * @config: an optional #GnomePrintConfig.
1816 * @buffer: the #GtkTextBuffer to print (might be %NULL).
1818 * Creates a new print job to print @buffer.
1820 * Return value: a new print job object.
1823 gtk_source_print_job_new_with_buffer (GnomePrintConfig *config,
1824 GtkTextBuffer *buffer)
1826 GtkSourcePrintJob *job;
1828 g_return_val_if_fail (config == NULL || GNOME_IS_PRINT_CONFIG (config), NULL);
1829 g_return_val_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer), NULL);
1831 job = gtk_source_print_job_new (config);
1833 gtk_source_print_job_set_buffer (job, buffer);
1838 /* --- print job basic configuration */
1841 * gtk_source_print_job_set_config:
1842 * @job: a #GtkSourcePrintJob.
1843 * @config: a #GnomePrintConfig object to get printing configuration from.
1845 * Sets the print configuration for the job. If you don't set a
1846 * configuration object for the print job, when needed one will be
1847 * created with gnome_print_config_default().
1850 gtk_source_print_job_set_config (GtkSourcePrintJob *job,
1851 GnomePrintConfig *config)
1853 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1854 g_return_if_fail (GNOME_IS_PRINT_CONFIG (config));
1855 g_return_if_fail (!job->priv->printing);
1857 if (config == job->priv->config)
1860 if (job->priv->config != NULL)
1861 gnome_print_config_unref (job->priv->config);
1863 job->priv->config = config;
1864 gnome_print_config_ref (config);
1866 g_object_notify (G_OBJECT (job), "config");
1870 * gtk_source_print_job_get_config:
1871 * @job: a #GtkSourcePrintJob.
1873 * Gets the current #GnomePrintConfig the print job will use. If not
1874 * previously set, this will create a default configuration and return
1875 * it. The returned object reference is owned by the print job.
1877 * Return value: the #GnomePrintConfig for the print job.
1880 gtk_source_print_job_get_config (GtkSourcePrintJob *job)
1882 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
1884 ensure_print_config (job);
1886 return job->priv->config;
1890 * gtk_source_print_job_set_buffer:
1891 * @job: a #GtkSourcePrintJob.
1892 * @buffer: a #GtkTextBuffer.
1894 * Sets the #GtkTextBuffer the print job will print. You need to
1895 * specify a buffer to print, either by the use of this function or by
1896 * creating the print job with gtk_source_print_job_new_with_buffer().
1899 gtk_source_print_job_set_buffer (GtkSourcePrintJob *job,
1900 GtkTextBuffer *buffer)
1902 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1903 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1904 g_return_if_fail (!job->priv->printing);
1906 if (buffer == job->priv->buffer)
1909 if (job->priv->buffer != NULL)
1910 g_object_unref (job->priv->buffer);
1912 job->priv->buffer = buffer;
1913 g_object_ref (buffer);
1915 g_object_notify (G_OBJECT (job), "buffer");
1919 * gtk_source_print_job_get_buffer:
1920 * @job: a #GtkSourcePrintJob.
1922 * Gets the #GtkTextBuffer the print job would print. The returned
1923 * object reference (if non %NULL) is owned by the job object and
1924 * should not be unreferenced.
1926 * Return value: the #GtkTextBuffer to print.
1929 gtk_source_print_job_get_buffer (GtkSourcePrintJob *job)
1931 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
1933 return job->priv->buffer;
1936 /* --- print job layout and style configuration */
1939 * gtk_source_print_job_set_tabs_width:
1940 * @job: a #GtkSourcePrintJob.
1941 * @tabs_width: the number of equivalent spaces for a tabulation.
1943 * Sets the width (in equivalent spaces) of tabulations for the
1944 * printed text. The width in printing units will be calculated as
1945 * the width of a string containing @tabs_width spaces of the default
1946 * font. Tabulation stops are set for the full width of printed text.
1949 gtk_source_print_job_set_tabs_width (GtkSourcePrintJob *job,
1952 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1953 g_return_if_fail (!job->priv->printing);
1955 if (tabs_width == job->priv->tabs_width)
1958 job->priv->tabs_width = tabs_width;
1960 g_object_notify (G_OBJECT (job), "tabs_width");
1964 * gtk_source_print_job_get_tabs_width:
1965 * @job: a #GtkSourcePrintJob.
1967 * Determines the configured width (in equivalent spaces) of
1968 * tabulations. The default value is 8.
1970 * Return value: the width (in equivalent spaces) of a tabulation.
1973 gtk_source_print_job_get_tabs_width (GtkSourcePrintJob *job)
1975 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
1977 return job->priv->tabs_width;
1981 * gtk_source_print_job_set_wrap_mode:
1982 * @job: a #GtkSourcePrintJob.
1983 * @wrap: the wrap mode.
1985 * Sets the wrap mode for lines of text larger than the printable
1986 * width. See #GtkWrapMode for a definition of the possible values.
1989 gtk_source_print_job_set_wrap_mode (GtkSourcePrintJob *job,
1992 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1993 g_return_if_fail (!job->priv->printing);
1995 if (wrap == job->priv->wrap_mode)
1998 job->priv->wrap_mode = wrap;
2000 g_object_notify (G_OBJECT (job), "wrap_mode");
2004 * gtk_source_print_job_get_wrap_mode:
2005 * @job: a #GtkSourcePrintJob.
2007 * Determines the wrapping style for text lines wider than the
2008 * printable width. The default is no wrapping.
2010 * Return value: the current wrapping mode for the print job.
2013 gtk_source_print_job_get_wrap_mode (GtkSourcePrintJob *job)
2015 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), GTK_WRAP_NONE);
2017 return job->priv->wrap_mode;
2021 * gtk_source_print_job_set_highlight:
2022 * @job: a #GtkSourcePrintJob.
2023 * @highlight: %TRUE if the printed text should be highlighted.
2025 * Sets whether the printed text will be highlighted according to the
2026 * buffer rules. Both color and font style are applied.
2029 gtk_source_print_job_set_highlight (GtkSourcePrintJob *job,
2032 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2033 g_return_if_fail (!job->priv->printing);
2035 highlight = (highlight != FALSE);
2037 if (highlight == job->priv->highlight)
2040 job->priv->highlight = highlight;
2042 g_object_notify (G_OBJECT (job), "highlight");
2046 * gtk_source_print_job_get_highlight:
2047 * @job: a #GtkSourcePrintJob.
2049 * Determines if the job is configured to print the text highlighted
2050 * with colors and font styles. Note that highlighting will happen
2051 * only if the buffer to print has highlighting activated.
2053 * Return value: %TRUE if the printed output will be highlighted.
2056 gtk_source_print_job_get_highlight (GtkSourcePrintJob *job)
2058 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2060 return job->priv->highlight;
2064 * gtk_source_print_job_set_font_desc:
2065 * @job: a #GtkSourcePrintJob.
2066 * @desc: the #PangoFontDescription for the default font
2068 * Sets the default font for the printed text.
2071 gtk_source_print_job_set_font_desc (GtkSourcePrintJob *job,
2072 PangoFontDescription *desc)
2074 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2075 g_return_if_fail (desc != NULL);
2076 g_return_if_fail (!job->priv->printing);
2078 desc = pango_font_description_copy (desc);
2079 if (job->priv->font != NULL)
2080 pango_font_description_free (job->priv->font);
2081 job->priv->font = desc;
2082 g_object_freeze_notify (G_OBJECT (job));
2083 g_object_notify (G_OBJECT (job), "font");
2084 g_object_notify (G_OBJECT (job), "font_desc");
2085 g_object_thaw_notify (G_OBJECT (job));
2089 * gtk_source_print_job_set_font:
2090 * @job: a #GtkSourcePrintJob.
2091 * @font_name: the name of the default font.
2093 * Sets the default font for the printed text. @font_name should be a
2094 * <emphasis>full font name</emphasis> GnomePrint can understand
2095 * (e.g. "Monospace Regular 10.0").
2097 * Note that @font_name is a #GnomeFont name not a Pango font
2098 * description string. This function is deprecated since #GnomeFont is
2099 * no longer used when implementing printing for GtkSourceView; you
2100 * should use gtk_source_print_job_set_font_desc() instead.
2103 gtk_source_print_job_set_font (GtkSourcePrintJob *job,
2104 const gchar *font_name)
2106 PangoFontDescription *desc;
2108 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2109 g_return_if_fail (font_name != NULL);
2110 g_return_if_fail (!job->priv->printing);
2112 desc = font_description_from_gnome_font_name (font_name);
2115 gtk_source_print_job_set_font_desc (job, desc);
2116 pango_font_description_free (desc);
2121 * gtk_source_print_job_get_font_desc:
2122 * @job: a #GtkSourcePrintJob.
2124 * Determines the default font to be used for the printed text. The
2125 * returned string is of the form "Fontfamily Style Size",
2126 * for example "Monospace Regular 10.0". The returned value
2127 * should be freed when no longer needed.
2129 * Return value: the current text font description. This value is
2130 * owned by the job and must not be modified or freed.
2132 PangoFontDescription *
2133 gtk_source_print_job_get_font_desc (GtkSourcePrintJob *job)
2135 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2137 ensure_print_config (job);
2139 return job->priv->font;
2143 * gtk_source_print_job_get_font:
2144 * @job: a #GtkSourcePrintJob.
2146 * Determines the default font to be used for the printed text. The
2147 * returned string is of the form "Fontfamily Style Size",
2148 * for example "Monospace Regular 10.0". The returned value
2149 * should be freed when no longer needed.
2151 * Note that the result is a #GnomeFont name not a Pango font
2152 * description string. This function is deprecated since #GnomeFont is
2153 * no longer used when implementing printing for GtkSourceView; you
2154 * should use gtk_source_print_job_get_font_desc() instead.
2156 * Return value: a newly allocated string with the name of the current
2160 gtk_source_print_job_get_font (GtkSourcePrintJob *job)
2162 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2164 ensure_print_config (job);
2166 return font_description_to_gnome_font_name (job->priv->font);
2170 * gtk_source_print_job_setup_from_view:
2171 * @job: a #GtkSourcePrintJob.
2172 * @view: a #GtkSourceView to get configuration from.
2174 * Convenience function to set several configuration options at once,
2175 * so that the printed output matches @view. The options set are
2176 * buffer (if not set already), tabs width, highlighting, wrap mode
2180 gtk_source_print_job_setup_from_view (GtkSourcePrintJob *job,
2183 GtkTextBuffer *buffer = NULL;
2184 PangoContext *pango_context;
2186 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2187 g_return_if_fail (!job->priv->printing);
2189 buffer = gtk_text_view_get_buffer (view);
2191 if (job->priv->buffer == NULL && buffer != NULL)
2192 gtk_source_print_job_set_buffer (job, buffer);
2194 /* gtk_source_print_job_set_tabs_width (job, gtk_source_view_get_tabs_width (view)); */
2195 gtk_source_print_job_set_highlight (job, TRUE);
2196 gtk_source_print_job_set_wrap_mode (job, gtk_text_view_get_wrap_mode (view));
2198 pango_context = gtk_widget_get_pango_context (GTK_WIDGET (view));
2199 gtk_source_print_job_set_font_desc (job,
2200 pango_context_get_font_description (pango_context));
2204 * gtk_source_print_job_set_numbers_font_desc:
2205 * @job: a #GtkSourcePrintJob.
2206 * @desc: the #PangoFontDescription for the font for line numbers, or %NULL
2208 * Sets the font for printing line numbers on the left margin. If
2209 * NULL is supplied, the default font (i.e. the one being used for the
2210 * text) will be used instead.
2213 gtk_source_print_job_set_numbers_font_desc (GtkSourcePrintJob *job,
2214 PangoFontDescription *desc)
2216 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2217 g_return_if_fail (!job->priv->printing);
2220 desc = pango_font_description_copy (desc);
2221 if (job->priv->numbers_font != NULL)
2222 pango_font_description_free (job->priv->numbers_font);
2223 job->priv->numbers_font = desc;
2224 g_object_freeze_notify (G_OBJECT (job));
2225 g_object_notify (G_OBJECT (job), "numbers_font");
2226 g_object_notify (G_OBJECT (job), "numbers_font_desc");
2227 g_object_thaw_notify (G_OBJECT (job));
2231 * gtk_source_print_job_set_numbers_font:
2232 * @job: a #GtkSourcePrintJob.
2233 * @font_name: the full name of the font for line numbers, or %NULL.
2235 * Sets the font for printing line numbers on the left margin. If
2236 * %NULL is supplied, the default font (i.e. the one being used for the
2237 * text) will be used instead.
2239 * Note that @font_name is a #GnomeFont name not a Pango font
2240 * description string. This function is deprecated since #GnomeFont is
2241 * no longer used when implementing printing for GtkSourceView; you
2242 * should use gtk_source_print_job_set_numbers_font_desc() instead.
2245 gtk_source_print_job_set_numbers_font (GtkSourcePrintJob *job,
2246 const gchar *font_name)
2248 PangoFontDescription *desc;
2250 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2251 g_return_if_fail (!job->priv->printing);
2253 if (font_name != NULL)
2255 desc = font_description_from_gnome_font_name (font_name);
2258 gtk_source_print_job_set_numbers_font_desc (job, desc);
2259 pango_font_description_free (desc);
2263 gtk_source_print_job_set_numbers_font (job, NULL);
2267 * gtk_source_print_job_get_numbers_font_desc:
2268 * @job: a #GtkSourcePrintJob.
2270 * Determines the font to be used for the line numbers. This function
2271 * might return %NULL if a specific font for numbers has not been set.
2273 * Return value: the line numbers font description or %NULL. This value is
2274 * owned by the job and must not be modified or freed.
2276 PangoFontDescription *
2277 gtk_source_print_job_get_numbers_font_desc (GtkSourcePrintJob *job)
2279 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2281 return job->priv->numbers_font;
2285 * gtk_source_print_job_get_numbers_font:
2286 * @job: a #GtkSourcePrintJob.
2288 * Determines the font to be used for the line numbers. The returned
2289 * string is of the form "Fontfamily Style Size", for
2290 * example "Monospace Regular 10.0". The returned value
2291 * should be freed when no longer needed. This function might return
2292 * %NULL if a specific font for numbers has not been set.
2294 * Note that the result is a #GnomeFont name not a Pango font
2295 * description string. This function is deprecated since #GnomeFont is
2296 * no longer used when implementing printing for GtkSourceView; you
2297 * should use gtk_source_print_job_get_numbers_font_desc() instead.
2299 * Return value: a newly allocated string with the name of the current
2300 * line numbers font, or %NULL.
2303 gtk_source_print_job_get_numbers_font (GtkSourcePrintJob *job)
2305 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2307 if (job->priv->numbers_font != NULL)
2308 return font_description_to_gnome_font_name (job->priv->numbers_font);
2314 * gtk_source_print_job_set_print_numbers:
2315 * @job: a #GtkSourcePrintJob.
2316 * @interval: interval for printed line numbers.
2318 * Sets the interval for printed line numbers. If @interval is 0 no
2319 * numbers will be printed. If greater than 0, a number will be
2320 * printed every @interval lines (i.e. 1 will print all line numbers).
2323 gtk_source_print_job_set_print_numbers (GtkSourcePrintJob *job,
2326 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2327 g_return_if_fail (!job->priv->printing);
2329 if (interval == job->priv->print_numbers)
2332 job->priv->print_numbers = interval;
2334 g_object_notify (G_OBJECT (job), "print_numbers");
2338 * gtk_source_print_job_get_print_numbers:
2339 * @job: a #GtkSourcePrintJob.
2341 * Determines the interval used for line number printing. If the
2342 * value is 0, no line numbers will be printed. The default value is
2343 * 1 (i.e. numbers printed in all lines).
2345 * Return value: the interval of printed line numbers.
2348 gtk_source_print_job_get_print_numbers (GtkSourcePrintJob *job)
2350 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2352 return job->priv->print_numbers;
2356 * gtk_source_print_job_set_text_margins:
2357 * @job: a #GtkSourcePrintJob.
2358 * @top: the top user margin.
2359 * @bottom: the bottom user margin.
2360 * @left: the left user margin.
2361 * @right: the right user margin.
2363 * Sets the four user margins for the print job. These margins are in
2364 * addition to the document margins provided in the #GnomePrintConfig
2365 * and will not be used for headers, footers or line numbers (those
2366 * are calculated separatedly). You can print in the space allocated
2367 * by these margins by connecting to the <link
2368 * linkend="GtkSourcePrintJob-begin-page">"begin_page"</link> signal. The
2369 * space is around the printed text, and inside the margins specified
2370 * in the #GnomePrintConfig.
2372 * The margin numbers are given in device units. If any of the given
2373 * values is less than 0, that particular margin is not altered by
2377 gtk_source_print_job_set_text_margins (GtkSourcePrintJob *job,
2383 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2384 g_return_if_fail (!job->priv->printing);
2387 job->priv->margin_top = top;
2389 job->priv->margin_bottom = bottom;
2391 job->priv->margin_left = left;
2393 job->priv->margin_right = right;
2397 * gtk_source_print_job_get_text_margins:
2398 * @job: a #GtkSourcePrintJob.
2399 * @top: a pointer to a #gdouble to return the top margin.
2400 * @bottom: a pointer to a #gdouble to return the bottom margin.
2401 * @left: a pointer to a #gdouble to return the left margin.
2402 * @right: a pointer to a #gdouble to return the right margin.
2404 * Determines the user set margins for the job. This function
2405 * retrieves the values previously set by
2406 * gtk_source_print_job_set_text_margins(). The default for all four
2407 * margins is 0. Any of the pointers can be %NULL if you want to
2408 * ignore that value.
2411 gtk_source_print_job_get_text_margins (GtkSourcePrintJob *job,
2417 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2420 *top = job->priv->margin_top;
2422 *bottom = job->priv->margin_bottom;
2424 *left = job->priv->margin_left;
2426 *right = job->priv->margin_right;
2429 /* --- printing operations */
2432 gtk_source_print_job_prepare (GtkSourcePrintJob *job,
2433 const GtkTextIter *start,
2434 const GtkTextIter *end)
2436 PROFILE (GTimer *timer);
2438 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2439 g_return_val_if_fail (!job->priv->printing, FALSE);
2440 g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
2441 g_return_val_if_fail (start != NULL && end != NULL, FALSE);
2443 /* make sure we have a sane configuration to start printing */
2444 ensure_print_config (job);
2446 PROFILE (timer = g_timer_new ());
2448 /* get the text to print */
2449 if (!get_text_to_print (job, start, end))
2452 PROFILE (g_message ("get_text_to_print: %.2f", g_timer_elapsed (timer, NULL)));
2454 if (!setup_pango_context (job))
2458 if (!update_page_size_and_margins (job))
2461 /* split the document in pages */
2462 if (!paginate_text (job))
2466 g_message ("paginate_text: %.2f", g_timer_elapsed (timer, NULL));
2467 g_timer_destroy (timer);
2474 * gtk_source_print_job_print:
2475 * @job: a configured #GtkSourcePrintJob.
2477 * Produces a #GnomePrintJob with the printed document. The whole
2478 * contents of the configured #GtkTextBuffer are printed. The
2479 * returned job is already closed and ready to be previewed (using
2480 * gnome_print_job_preview_new()) or printed directly. The caller of
2481 * this function owns a reference to the returned object, so @job can
2482 * be destroyed and the output will still be valid, or the document
2483 * can be printed again with different settings.
2485 * Return value: a closed #GnomePrintJob with the printed document, or
2486 * %NULL if printing could not be completed.
2489 gtk_source_print_job_print (GtkSourcePrintJob *job)
2491 GtkTextIter start, end;
2493 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2494 g_return_val_if_fail (!job->priv->printing, NULL);
2495 g_return_val_if_fail (job->priv->buffer != NULL, NULL);
2497 gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (job->priv->buffer), &start, &end);
2499 return gtk_source_print_job_print_range (job, &start, &end);
2503 * gtk_source_print_job_print_range:
2504 * @job: a configured #GtkSourcePrintJob.
2505 * @start: the start of the region of text to print.
2506 * @end: the end of the region of text to print.
2508 * Similar to gtk_source_print_job_print(), except you can specify a
2509 * range of text to print. The passed #GtkTextIter values might be
2512 * Return value: a closed #GnomePrintJob with the text from @start to
2513 * @end printed, or %NULL if @job could not print.
2516 gtk_source_print_job_print_range (GtkSourcePrintJob *job,
2517 const GtkTextIter *start,
2518 const GtkTextIter *end)
2520 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2521 g_return_val_if_fail (!job->priv->printing, NULL);
2522 g_return_val_if_fail (job->priv->buffer != NULL, NULL);
2523 g_return_val_if_fail (start != NULL && end != NULL, NULL);
2524 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
2525 GTK_TEXT_BUFFER (job->priv->buffer) &&
2526 gtk_text_iter_get_buffer (end) ==
2527 GTK_TEXT_BUFFER (job->priv->buffer), NULL);
2529 if (!gtk_source_print_job_prepare (job, start, end))
2532 /* real work starts here */
2533 setup_for_print (job);
2535 job->priv->printing = TRUE;
2537 job->priv->printing = FALSE;
2539 g_object_ref (job->priv->print_job);
2540 return job->priv->print_job;
2543 /* --- asynchronous printing */
2546 * gtk_source_print_job_print_range_async:
2547 * @job: a configured #GtkSourcePrintJob.
2548 * @start: the start of the region of text to print.
2549 * @end: the end of the region of text to print.
2551 * Starts to print @job asynchronously. This function will ready the
2552 * @job for printing and install an idle handler that will render one
2555 * This function will not return immediatly, as only page rendering is
2556 * done asynchronously. Text retrieval and paginating happens within
2557 * this function. Also, if highlighting is enabled, the whole buffer
2558 * needs to be highlighted first.
2560 * To get notification when the job has finished, you must connect to
2562 * linkend="GtkSourcePrintJob-finished">"finished"</link>
2563 * signal. After this signal is emitted you can retrieve the
2564 * resulting #GnomePrintJob with gtk_source_print_job_get_print_job().
2565 * You may cancel the job with gtk_source_print_job_cancel().
2567 * Return value: %TRUE if the print started.
2570 gtk_source_print_job_print_range_async (GtkSourcePrintJob *job,
2571 const GtkTextIter *start,
2572 const GtkTextIter *end)
2574 GSource *idle_source;
2576 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2577 g_return_val_if_fail (!job->priv->printing, FALSE);
2578 g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
2579 g_return_val_if_fail (start != NULL && end != NULL, FALSE);
2580 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
2581 GTK_TEXT_BUFFER (job->priv->buffer) &&
2582 gtk_text_iter_get_buffer (end) ==
2583 GTK_TEXT_BUFFER (job->priv->buffer), FALSE);
2585 if (!gtk_source_print_job_prepare (job, start, end))
2588 /* real work starts here */
2589 setup_for_print (job);
2590 if (job->priv->current_paragraph == NULL)
2593 /* setup the idle handler to print each page at a time */
2594 idle_source = g_idle_source_new ();
2595 g_source_set_priority (idle_source, GTK_SOURCE_PRINT_JOB_PRIORITY);
2596 g_source_set_closure (idle_source,
2597 g_cclosure_new_object ((GCallback) idle_printing_handler,
2599 job->priv->idle_printing_tag = g_source_attach (idle_source, NULL);
2600 g_source_unref (idle_source);
2602 job->priv->printing = TRUE;
2608 * gtk_source_print_job_cancel:
2609 * @job: a #GtkSourcePrintJob.
2611 * Cancels an asynchronous printing operation. This will remove any
2612 * pending print idle handler and unref the current #GnomePrintJob.
2614 * Note that if you got a reference to the job's #GnomePrintJob (using
2615 * gtk_source_print_job_get_print_job()) it will not be destroyed
2616 * (since you hold a reference to it), but it will not be closed
2617 * either. If you wish to show or print the partially printed
2618 * document you need to close it yourself.
2620 * This function has no effect when called from a non-asynchronous
2624 gtk_source_print_job_cancel (GtkSourcePrintJob *job)
2626 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2627 g_return_if_fail (job->priv->printing);
2629 if (job->priv->idle_printing_tag > 0)
2631 g_source_remove (job->priv->idle_printing_tag);
2632 job->priv->current_paragraph = NULL;
2633 job->priv->idle_printing_tag = 0;
2634 job->priv->printing = FALSE;
2635 g_object_unref (job->priv->print_job);
2636 g_object_unref (job->priv->print_ctxt);
2637 job->priv->print_job = NULL;
2638 job->priv->print_ctxt = NULL;
2643 * gtk_source_print_job_get_print_job:
2644 * @job: a #GtkSourcePrintJob.
2646 * Gets a reference to the #GnomePrintJob which the @job is printing
2647 * or has recently finished printing. You need to unref the returned
2650 * You may call this function in the middle of an asynchronous
2651 * printing operation, but the returned #GnomePrintJob will not be
2652 * closed until the last page is printed and the <link
2653 * linkend="GtkSourcePrintJob-finished">"finished"</link>
2654 * signal is emitted.
2656 * Return value: a new reference to the @job's #GnomePrintJob, or
2660 gtk_source_print_job_get_print_job (GtkSourcePrintJob *job)
2662 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2664 if (job->priv->print_job)
2665 g_object_ref (job->priv->print_job);
2667 return job->priv->print_job;
2670 /* --- information for asynchronous ops and headers and footers callback */
2673 * gtk_source_print_job_get_page:
2674 * @job: a #GtkSourcePrintJob.
2676 * Determines the currently printing page number. This function is
2677 * only valid while printing (either synchronously or asynchronously).
2679 * Return value: the current page number.
2682 gtk_source_print_job_get_page (GtkSourcePrintJob *job)
2684 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2685 g_return_val_if_fail (job->priv->printing, 0);
2687 return job->priv->page;
2691 * gtk_source_print_job_get_page_count:
2692 * @job: a #GtkSourcePrintJob.
2694 * Determines the total number of pages the job will print. The
2695 * returned value is only meaninful after pagination has finished. In
2696 * practice, for synchronous printing this means when <link
2697 * linkend="GtkSourcePrintJob-begin-page">"begin_page"</link>
2698 * is emitted, or after gtk_source_print_job_print_range_async() has
2701 * Return value: the number of pages of the printed job.
2704 gtk_source_print_job_get_page_count (GtkSourcePrintJob *job)
2706 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2708 return job->priv->page_count;
2712 * gtk_source_print_job_get_print_context:
2713 * @job: a printing #GtkSourcePrintJob.
2715 * Determines the #GnomePrintContext of the current job. This
2716 * function is only valid while printing. Normally you would use this
2717 * function to print in the margins set by
2718 * gtk_source_print_job_set_margins() in a handler for the <link
2719 * linkend="GtkSourcePrintJob-begin-page">"begin_page"</link>
2722 * Return value: the current #GnomePrintContext. The returned object
2726 gtk_source_print_job_get_print_context (GtkSourcePrintJob *job)
2728 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2729 g_return_val_if_fail (job->priv->printing, NULL);
2731 return job->priv->print_ctxt;
2734 /* ---- Header and footer (default implementation) */
2736 /* Most of this code taken from GLib's g_date_strftime() in gdate.c
2737 * GLIB - Library of useful routines for C programming
2738 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald */
2741 strdup_strftime (const gchar *format, const struct tm *tm)
2743 gsize locale_format_len = 0;
2744 gchar *locale_format;
2750 GError *error = NULL;
2752 g_return_val_if_fail (format != NULL, NULL);
2753 g_return_val_if_fail (tm != NULL, NULL);
2755 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
2759 g_warning (G_STRLOC "Error converting format to locale encoding: %s",
2761 g_error_free (error);
2766 tmpbufsize = MAX (128, locale_format_len * 2);
2769 tmpbuf = g_malloc (tmpbufsize);
2771 /* Set the first byte to something other than '\0', to be able to
2772 * recognize whether strftime actually failed or just returned "".
2775 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, tm);
2777 if (tmplen == 0 && tmpbuf[0] != '\0')
2782 if (tmpbufsize > 65536)
2784 g_warning (G_STRLOC "Maximum buffer size for strdup_strftime "
2785 "exceeded: giving up");
2786 g_free (locale_format);
2793 g_free (locale_format);
2795 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
2800 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s",
2802 g_error_free (error);
2811 evaluate_format_string (GtkSourcePrintJob *job, const gchar *format)
2814 gchar *eval_str, *retval;
2815 const struct tm *tm;
2822 tm = localtime_r(&now, <);
2824 /* analyze format string and replace the codes we know */
2825 eval = g_string_new_len (NULL, strlen (format));
2826 ch = g_utf8_get_char (format);
2831 format = g_utf8_next_char (format);
2832 ch = g_utf8_get_char (format);
2834 g_string_append_printf (eval, "%d", job->priv->page);
2836 g_string_append_printf (eval, "%d", job->priv->page_count);
2839 g_string_append_c (eval, '%');
2840 g_string_append_unichar (eval, ch);
2844 g_string_append_unichar (eval, ch);
2846 format = g_utf8_next_char (format);
2847 ch = g_utf8_get_char (format);
2850 eval_str = g_string_free (eval, FALSE);
2851 retval = strdup_strftime (eval_str, tm);
2858 print_header_footer_string (GtkSourcePrintJob *job,
2859 const gchar *format,
2864 PangoLayout *layout;
2869 width = job->priv->text_width + job->priv->numbers_width;
2871 text = evaluate_format_string (job, format);
2874 layout = pango_layout_new (job->priv->pango_context);
2875 pango_layout_set_font_description (layout, job->priv->header_footer_font);
2876 pango_layout_set_text (layout, text, -1);
2878 xx = x + x_align * (width - get_layout_width (layout));
2879 gnome_print_moveto (job->priv->print_ctxt, xx, y);
2880 show_first_layout_line (job->priv->print_ctxt, layout);
2883 g_object_unref (layout);
2888 default_print_header (GtkSourcePrintJob *job,
2894 gdouble ascent, descent;
2896 width = job->priv->text_width + job->priv->numbers_width;
2898 get_font_ascent_descent (job, job->priv->header_footer_font, &ascent, &descent);
2903 if (job->priv->header_format_left != NULL)
2904 print_header_footer_string (job, job->priv->header_format_left, 0.0, x, yy);
2907 if (job->priv->header_format_right != NULL)
2908 print_header_footer_string (job, job->priv->header_format_right, 1.0, x, yy);
2911 if (job->priv->header_format_center != NULL)
2912 print_header_footer_string (job, job->priv->header_format_center, 0.5, x, yy);
2915 if (job->priv->header_separator)
2917 yy = y - (SEPARATOR_SPACING * (ascent + descent));
2918 gnome_print_setlinewidth (job->priv->print_ctxt, SEPARATOR_LINE_WIDTH);
2919 gnome_print_moveto (job->priv->print_ctxt, x, yy);
2920 gnome_print_lineto (job->priv->print_ctxt, x + width, yy);
2921 gnome_print_stroke (job->priv->print_ctxt);
2926 default_print_footer (GtkSourcePrintJob *job,
2932 gdouble ascent, descent;
2934 width = job->priv->text_width + job->priv->numbers_width;
2936 get_font_ascent_descent (job, job->priv->header_footer_font, &ascent, &descent);
2938 yy = y - job->priv->footer_height + descent;
2941 if (job->priv->footer_format_left != NULL)
2942 print_header_footer_string (job, job->priv->footer_format_left, 0.0, x, yy);
2945 if (job->priv->footer_format_right != NULL)
2946 print_header_footer_string (job, job->priv->footer_format_right, 1.0, x, yy);
2949 if (job->priv->footer_format_center != NULL)
2950 print_header_footer_string (job, job->priv->footer_format_center, 0.5, x, yy);
2953 if (job->priv->footer_separator)
2955 yy = y - job->priv->footer_height +
2956 (SEPARATOR_SPACING * (ascent + descent));
2957 gnome_print_setlinewidth (job->priv->print_ctxt, SEPARATOR_LINE_WIDTH);
2958 gnome_print_moveto (job->priv->print_ctxt, x, yy);
2959 gnome_print_lineto (job->priv->print_ctxt, x + width, yy);
2960 gnome_print_stroke (job->priv->print_ctxt);
2965 * gtk_source_print_job_set_print_header:
2966 * @job: a #GtkSourcePrintJob.
2967 * @setting: %TRUE if you want the header to be printed.
2969 * Sets whether you want to print a header in each page. The default
2970 * header consists of three pieces of text and an optional line
2971 * separator, configurable with
2972 * gtk_source_print_job_set_header_format().
2974 * Note that by default the header format is unspecified, and if it's
2975 * empty it will not be printed, regardless of this setting.
2978 gtk_source_print_job_set_print_header (GtkSourcePrintJob *job,
2981 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2982 g_return_if_fail (!job->priv->printing);
2984 setting = (setting != FALSE);
2986 if (setting == job->priv->print_header)
2989 job->priv->print_header = setting;
2991 g_object_notify (G_OBJECT (job), "print_header");
2995 * gtk_source_print_job_get_print_header:
2996 * @job: a #GtkSourcePrintJob.
2998 * Determines if a header is set to be printed for each page. A
2999 * header will be printed if this function returns %TRUE
3000 * <emphasis>and</emphasis> some format strings have been specified
3001 * with gtk_source_print_job_set_header_format().
3003 * Return value: %TRUE if the header is set to be printed.
3006 gtk_source_print_job_get_print_header (GtkSourcePrintJob *job)
3008 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
3010 return job->priv->print_header;
3014 * gtk_source_print_job_set_print_footer:
3015 * @job: a #GtkSourcePrintJob.
3016 * @setting: %TRUE if you want the footer to be printed.
3018 * Sets whether you want to print a footer in each page. The default
3019 * footer consists of three pieces of text and an optional line
3020 * separator, configurable with
3021 * gtk_source_print_job_set_footer_format().
3023 * Note that by default the footer format is unspecified, and if it's
3024 * empty it will not be printed, regardless of this setting.
3027 gtk_source_print_job_set_print_footer (GtkSourcePrintJob *job,
3030 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3031 g_return_if_fail (!job->priv->printing);
3033 setting = (setting != FALSE);
3035 if (setting == job->priv->print_footer)
3038 job->priv->print_footer = setting;
3040 g_object_notify (G_OBJECT (job), "print_footer");
3044 * gtk_source_print_job_get_print_footer:
3045 * @job: a #GtkSourcePrintJob.
3047 * Determines if a footer is set to be printed for each page. A
3048 * footer will be printed if this function returns %TRUE
3049 * <emphasis>and</emphasis> some format strings have been specified
3050 * with gtk_source_print_job_set_footer_format().
3052 * Return value: %TRUE if the footer is set to be printed.
3055 gtk_source_print_job_get_print_footer (GtkSourcePrintJob *job)
3057 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
3059 return job->priv->print_footer;
3063 * gtk_source_print_job_set_header_footer_font_desc:
3064 * @job: a #GtkSourcePrintJob.
3065 * @desc: the #PangoFontDescription for the font to be used in headers and footers, or %NULL.
3067 * Sets the font for printing headers and footers. If %NULL is
3068 * supplied, the default font (i.e. the one being used for the text)
3069 * will be used instead.
3072 gtk_source_print_job_set_header_footer_font_desc (GtkSourcePrintJob *job,
3073 PangoFontDescription *desc)
3075 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3076 g_return_if_fail (!job->priv->printing);
3079 desc = pango_font_description_copy (desc);
3080 if (job->priv->header_footer_font != NULL)
3081 pango_font_description_free (job->priv->header_footer_font);
3082 job->priv->header_footer_font = desc;
3083 g_object_freeze_notify (G_OBJECT (job));
3084 g_object_notify (G_OBJECT (job), "header_footer_font");
3085 g_object_notify (G_OBJECT (job), "header_footer_font_desc");
3086 g_object_thaw_notify (G_OBJECT (job));
3090 * gtk_source_print_job_set_header_footer_font:
3091 * @job: a #GtkSourcePrintJob.
3092 * @font_name: the full name of the font to be used in headers and footers, or %NULL.
3094 * Sets the font for printing headers and footers. If %NULL is
3095 * supplied, the default font (i.e. the one being used for the text)
3096 * will be used instead.
3098 * Note that @font_name is a #GnomeFont name not a Pango font
3099 * description string. This function is deprecated since #GnomeFont is
3100 * no longer used when implementing printing for GtkSourceView; you
3101 * should use gtk_source_print_job_set_header_footer_font_desc() instead.
3104 gtk_source_print_job_set_header_footer_font (GtkSourcePrintJob *job,
3105 const gchar *font_name)
3107 PangoFontDescription *desc;
3109 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3110 g_return_if_fail (!job->priv->printing);
3112 if (font_name != NULL)
3114 desc = font_description_from_gnome_font_name (font_name);
3117 gtk_source_print_job_set_header_footer_font_desc (job, desc);
3118 pango_font_description_free (desc);
3122 gtk_source_print_job_set_header_footer_font_desc (job, NULL);
3126 * gtk_source_print_job_get_header_footer_font_desc:
3127 * @job: a #GtkSourcePrintJob.
3129 * Determines the font to be used for the header and footer. This function
3130 * might return %NULL if a specific font has not been set.
3132 * Return value: the header and footer font description or %NULL. This value is
3133 * owned by the job and must not be modified or freed.
3135 PangoFontDescription *
3136 gtk_source_print_job_get_header_footer_font_desc (GtkSourcePrintJob *job)
3138 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
3140 return job->priv->header_footer_font;
3144 * gtk_source_print_job_get_header_footer_font:
3145 * @job: a #GtkSourcePrintJob.
3147 * Determines the font to be used for the header and footer. The
3148 * returned string is of the form "Fontfamily Style Size",
3149 * for example "Monospace Regular 10.0". The returned value
3150 * should be freed when no longer needed. This function might return
3151 * %NULL if a specific font has not been set.
3153 * Note that the result is a #GnomeFont name not a Pango font
3154 * description string. This function is deprecated since #GnomeFont is
3155 * no longer used when implementing printing for GtkSourceView; you
3156 * should use gtk_source_print_job_get_header_footer_font_desc() instead.
3158 * Return value: a newly allocated string with the name of the current
3159 * header and footer font, or %NULL.
3162 gtk_source_print_job_get_header_footer_font (GtkSourcePrintJob *job)
3164 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
3166 if (job->priv->header_footer_font != NULL)
3167 return font_description_to_gnome_font_name (job->priv->header_footer_font);
3173 * gtk_source_print_job_set_header_format:
3174 * @job: a #GtkSourcePrintJob.
3175 * @left: a format string to print on the left of the header.
3176 * @center: a format string to print on the center of the header.
3177 * @right: a format string to print on the right of the header.
3178 * @separator: %TRUE if you want a separator line to be printed.
3180 * Sets strftime like header format strings, to be printed on the
3181 * left, center and right of the top of each page. The strings may
3182 * include strftime(3) codes which will be expanded at print time.
3183 * All strftime() codes are accepted, with the addition of %N for the
3184 * page number and %Q for the page count.
3186 * @separator specifies if a solid line should be drawn to separate
3187 * the header from the document text.
3189 * If %NULL is given for any of the three arguments, that particular
3190 * string will not be printed. For the header to be printed, in
3191 * addition to specifying format strings, you need to enable header
3192 * printing with gtk_source_print_job_set_print_header().
3195 gtk_source_print_job_set_header_format (GtkSourcePrintJob *job,
3197 const gchar *center,
3201 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3202 g_return_if_fail (!job->priv->printing);
3204 /* FIXME: validate given strings? */
3205 g_free (job->priv->header_format_left);
3206 g_free (job->priv->header_format_center);
3207 g_free (job->priv->header_format_right);
3208 job->priv->header_format_left = g_strdup (left);
3209 job->priv->header_format_center = g_strdup (center);
3210 job->priv->header_format_right = g_strdup (right);
3211 job->priv->header_separator = separator;
3215 * gtk_source_print_job_set_footer_format:
3216 * @job: a #GtkSourcePrintJob.
3217 * @left: a format string to print on the left of the footer.
3218 * @center: a format string to print on the center of the footer.
3219 * @right: a format string to print on the right of the footer.
3220 * @separator: %TRUE if you want a separator line to be printed.
3222 * Like gtk_source_print_job_set_header_format(), but for the footer.
3225 gtk_source_print_job_set_footer_format (GtkSourcePrintJob *job,
3227 const gchar *center,
3231 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3232 g_return_if_fail (!job->priv->printing);
3234 /* FIXME: validate given strings? */
3235 g_free (job->priv->footer_format_left);
3236 g_free (job->priv->footer_format_center);
3237 g_free (job->priv->footer_format_right);
3238 job->priv->footer_format_left = g_strdup (left);
3239 job->priv->footer_format_center = g_strdup (center);
3240 job->priv->footer_format_right = g_strdup (right);
3241 job->priv->footer_separator = separator;