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"
40 #include <glib/gi18n.h>
41 #include <gtk/gtkmain.h>
42 #include <gtk/gtktextview.h>
43 #include <libgnomeprint/gnome-print-pango.h>
58 #define DEFAULT_FONT_NAME "Monospace 10"
59 #define DEFAULT_COLOR 0x000000ff
61 #define CM(v) ((v) * 72.0 / 2.54)
62 #define A4_WIDTH (210.0 * 72 / 25.4)
63 #define A4_HEIGHT (297.0 * 72 / 25.4)
65 #define NUMBERS_TEXT_SEPARATION CM(0.5)
67 #define HEADER_FOOTER_SIZE 2.5
68 #define SEPARATOR_SPACING 1.5
69 #define SEPARATOR_LINE_WIDTH 1.0
72 typedef struct _TextSegment TextSegment;
73 typedef struct _Paragraph Paragraph;
74 typedef struct _TextStyle TextStyle;
76 /* 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;
1120 /* make sure the region to print is highlighted */
1121 /*_gtk_source_buffer_highlight_region (job->priv->buffer, start, end, TRUE); */
1123 next_toggle = *start;
1124 have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);
1126 /* FIXME: handle invisible text properly. This also assumes
1127 * the text has no embedded images and stuff */
1128 while (gtk_text_iter_compare (start, end) < 0)
1134 para = g_new0 (Paragraph, 1);
1136 /* get the style at the start of the line */
1137 style = get_style (job, start);
1139 /* get a line of text - limit points to the end of the line */
1141 if (!gtk_text_iter_ends_line (&limit))
1142 gtk_text_iter_forward_to_line_end (&limit);
1144 if (gtk_text_iter_compare (&limit, end) > 0)
1147 /* create the first segment for the line */
1148 para->segment = seg = g_new0 (TextSegment, 1);
1151 /* while the next tag toggle is within the line, we check to see
1152 * if the style has changed at each tag toggle position, and if so,
1153 * create new segments */
1154 while (have_toggle && gtk_text_iter_compare (&next_toggle, &limit) < 0)
1156 /* check style changes */
1157 style = get_style (job, &next_toggle);
1158 if (style != seg->style)
1160 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 *start = next_toggle;
1168 new_seg = g_new0 (TextSegment, 1);
1169 seg->next = new_seg;
1174 have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);
1177 /* close the line */
1179 seg->text = gtk_text_iter_get_slice (start, &limit);
1181 /* add the line of text to the job */
1182 job->priv->paragraphs = g_slist_prepend (job->priv->paragraphs, para);
1184 /* advance to next line */
1186 gtk_text_iter_forward_line (start);
1188 if (gtk_text_iter_compare (&next_toggle, start) < 0) {
1189 next_toggle = *start;
1190 have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);
1193 job->priv->paragraphs = g_slist_reverse (job->priv->paragraphs);
1199 get_text_to_print (GtkSourcePrintJob *job,
1200 const GtkTextIter *start,
1201 const GtkTextIter *end)
1203 GtkTextIter _start, _end;
1206 g_return_val_if_fail (start != NULL && end != NULL, FALSE);
1207 g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
1212 /* erase any previous data */
1213 if (job->priv->paragraphs != NULL)
1215 free_paragraphs (job->priv->paragraphs);
1216 job->priv->paragraphs = NULL;
1218 if (job->priv->tag_styles != NULL)
1220 g_hash_table_destroy (job->priv->tag_styles);
1221 job->priv->tag_styles = NULL;
1224 /* provide ordered iters */
1225 gtk_text_iter_order (&_start, &_end);
1227 /* save the first and last line numbers for future reference */
1228 job->priv->first_line_number = gtk_text_iter_get_line (&_start) + 1;
1229 job->priv->last_line_number = gtk_text_iter_get_line (&_end) + 1;
1231 if (!job->priv->highlight)
1232 retval = get_text_simple (job, &_start, &_end);
1234 retval = get_text_with_style (job, &_start, &_end);
1236 if (retval && job->priv->paragraphs == NULL)
1241 /* add an empty line to allow printing empty documents */
1242 seg = g_new0 (TextSegment, 1);
1244 seg->style = NULL; /* use default style */
1245 seg->text = g_strdup ("");
1247 para = g_new0 (Paragraph, 1);
1248 para->segment = seg;
1250 job->priv->paragraphs = g_slist_prepend (job->priv->paragraphs, para);
1256 /* ----- Pagination functions */
1259 add_attribute_to_list (PangoAttribute *attr,
1260 PangoAttrList *list,
1264 attr->start_index = index;
1265 attr->end_index = index + len;
1266 pango_attr_list_insert (list, attr);
1269 static PangoLayout *
1270 create_layout_for_para (GtkSourcePrintJob *job,
1274 PangoLayout *layout;
1275 PangoAttrList *attrs;
1279 text = g_string_new (NULL);
1280 attrs = pango_attr_list_new ();
1282 seg = para->segment;
1287 gsize seg_len = strlen (seg->text);
1288 g_string_append (text, seg->text);
1292 PangoAttribute *attr;
1294 attr = pango_attr_font_desc_new (seg->style->font_desc);
1295 add_attribute_to_list (attr, attrs, index, seg_len);
1297 if (seg->style->scale != PANGO_SCALE_MEDIUM)
1299 attr = pango_attr_scale_new (seg->style->scale);
1300 add_attribute_to_list (attr, attrs, index, seg_len);
1303 if (seg->style->foreground)
1305 attr = pango_attr_foreground_new (seg->style->foreground->red,
1306 seg->style->foreground->green,
1307 seg->style->foreground->blue);
1308 add_attribute_to_list (attr, attrs, index, seg_len);
1311 if (seg->style->background)
1313 attr = pango_attr_background_new (seg->style->background->red,
1314 seg->style->background->green,
1315 seg->style->background->blue);
1316 add_attribute_to_list (attr, attrs, index, seg_len);
1319 if (seg->style->strikethrough)
1321 attr = pango_attr_strikethrough_new (TRUE);
1322 add_attribute_to_list (attr, attrs, index, seg_len);
1325 if (seg->style->underline != PANGO_UNDERLINE_NONE)
1327 attr = pango_attr_underline_new (seg->style->underline);
1328 add_attribute_to_list (attr, attrs, index, seg_len);
1336 layout = pango_layout_new (job->priv->pango_context);
1338 /* if (job->priv->wrap_mode != GTK_WRAP_NONE)*/
1339 pango_layout_set_width (layout, job->priv->text_width * PANGO_SCALE);
1341 switch (job->priv->wrap_mode) {
1343 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1346 pango_layout_set_wrap (layout, PANGO_WRAP_WORD);
1348 case GTK_WRAP_WORD_CHAR:
1349 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
1353 * Ellipsize the paragraph when text wrapping is disabled.
1354 * Another possibility would be to set the width so the text
1355 * breaks into multiple lines, and paginate/render just the
1357 * See also Comment #23 by Owen on bug #143874.
1360 /* orph says to comment this out and commit it.
1361 PANGO_ELLIPSIZE_END is not available in pango
1362 1.4.1, at least, and he says this code is never
1364 /*pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);*/
1369 if (job->priv->tab_array)
1370 pango_layout_set_tabs (layout, job->priv->tab_array);
1372 pango_layout_set_text (layout, text->str, text->len);
1373 pango_layout_set_attributes (layout, attrs);
1375 /* FIXME: <horrible-hack>
1376 * For empty paragraphs, pango_layout_iter_get_baseline() returns 0,
1377 * so I check this condition and add a space character to force
1378 * the calculation of the baseline. I don't like that, but I
1379 * didn't find a better way to do it. Note that a paragraph is
1380 * considered empty either when it has no characters, or when
1382 * See comment #22 and #23 on bug #143874.
1384 if (job->priv->print_numbers > 0)
1386 PangoLayoutIter *iter;
1387 iter = pango_layout_get_iter (layout);
1388 if (pango_layout_iter_get_baseline (iter) == 0)
1390 g_string_append_c (text, ' ');
1391 pango_layout_set_text (layout, text->str, text->len);
1393 pango_layout_iter_free (iter);
1395 /* FIXME: </horrible-hack> */
1397 g_string_free (text, TRUE);
1398 pango_attr_list_unref (attrs);
1403 /* The break logic in this function needs to match that in print_paragraph */
1405 paginate_paragraph (GtkSourcePrintJob *job,
1408 PangoLayout *layout;
1409 PangoLayoutIter *iter;
1410 PangoRectangle logical_rect;
1414 layout = create_layout_for_para (job, para);
1416 iter = pango_layout_get_iter (layout);
1423 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1424 max = (gdouble) (logical_rect.y + logical_rect.height) / PANGO_SCALE;
1426 if (max - page_skip > job->priv->available_height)
1428 /* "create" a new page */
1429 job->priv->page_count++;
1430 job->priv->available_height = job->priv->text_height;
1431 page_skip = (gdouble) logical_rect.y / PANGO_SCALE;
1435 while (pango_layout_iter_next_line (iter));
1437 job->priv->available_height -= max - page_skip;
1439 pango_layout_iter_free (iter);
1440 g_object_unref (layout);
1444 paginate_text (GtkSourcePrintJob *job)
1449 /* set these to zero so the first break_line creates a new page */
1450 job->priv->page_count = 0;
1451 job->priv->available_height = 0;
1452 line_number = job->priv->first_line_number;
1453 l = job->priv->paragraphs;
1456 Paragraph *para = l->data;
1458 para->line_number = line_number;
1459 paginate_paragraph (job, para);
1462 l = g_slist_next (l);
1465 /* FIXME: do we have any error condition which can force us to
1466 * return %FALSE? - Gustavo */
1470 /* ---- Printing functions */
1473 begin_page (GtkSourcePrintJob *job)
1475 gnome_print_beginpage (job->priv->print_ctxt, NULL);
1477 g_signal_emit (job, print_job_signals [BEGIN_PAGE], 0);
1481 end_page (GtkSourcePrintJob *job)
1483 gnome_print_showpage (job->priv->print_ctxt);
1487 print_line_number (GtkSourcePrintJob *job,
1492 PangoLayout *layout;
1494 layout = get_line_number_layout (job, line_number);
1496 x = x + job->priv->numbers_width - get_layout_width (layout) - NUMBERS_TEXT_SEPARATION;
1497 gnome_print_moveto (job->priv->print_ctxt, x, y);
1499 show_first_layout_line (job->priv->print_ctxt, layout);
1501 g_object_unref (layout);
1504 /* The break logic in this function needs to match that in paginate_paragraph
1506 * @start_line is the first line in the paragraph to print
1507 * @y is updated to the position after the portion of the paragraph we printed
1508 * @baseline_out is set to the baseline of the first line of the paragraph
1509 * if we printed it. (And not set otherwise)
1511 * Returns the first unprinted line in the paragraph (unprinted because it
1512 * flowed onto the next page) or -1 if the entire paragraph was printed.
1515 print_paragraph (GtkSourcePrintJob *job,
1520 gdouble *baseline_out,
1523 PangoLayout *layout;
1524 PangoLayoutIter *iter;
1525 PangoRectangle logical_rect;
1532 layout = create_layout_for_para (job, para);
1534 iter = pango_layout_get_iter (layout);
1536 /* Skip over lines already printed on previous page(s) */
1537 for (current_line = 0; current_line < start_line; current_line++)
1538 pango_layout_iter_next_line (iter);
1545 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1546 max = (gdouble) (logical_rect.y + logical_rect.height) / PANGO_SCALE;
1548 if (current_line == start_line)
1549 page_skip = (gdouble) logical_rect.y / PANGO_SCALE;
1551 if (max - page_skip > job->priv->available_height)
1553 result = current_line; /* Save position for next page */
1557 baseline = (gdouble) pango_layout_iter_get_baseline (iter) / PANGO_SCALE;
1558 baseline = *y + page_skip - baseline; /* Adjust to global coordinates */
1559 if (current_line == 0)
1560 *baseline_out = baseline;
1562 gnome_print_moveto (job->priv->print_ctxt,
1563 x + (gdouble) logical_rect.x / PANGO_SCALE,
1565 gnome_print_pango_layout_line (job->priv->print_ctxt,
1566 pango_layout_iter_get_line (iter));
1570 while (pango_layout_iter_next_line (iter));
1572 job->priv->available_height -= max - page_skip;
1573 *y -= max - page_skip;
1575 pango_layout_iter_free (iter);
1576 g_object_unref (layout);
1582 print_page (GtkSourcePrintJob *job)
1587 gboolean force_fit = TRUE;
1593 job->priv->available_height = job->priv->text_height;
1595 y = job->priv->page_height -
1596 job->priv->doc_margin_top - job->priv->margin_top -
1597 job->priv->header_height;
1598 x = job->priv->doc_margin_left + job->priv->margin_left +
1599 job->priv->numbers_width;
1600 l = job->priv->current_paragraph;
1601 line = job->priv->current_paragraph_line;
1605 Paragraph *para = l->data;
1607 gint last_line = line;
1609 line = print_paragraph (job, para, line, x, &y, &baseline, force_fit);
1611 if (last_line == 0 && line != 0)
1613 /* We printed the first line of a paragraph */
1614 if (job->priv->print_numbers > 0 &&
1615 ((para->line_number % job->priv->print_numbers) == 0))
1616 print_line_number (job,
1618 job->priv->doc_margin_left +
1619 job->priv->margin_left,
1622 job->priv->printed_lines++;
1626 break; /* Didn't all fit on this page */
1633 job->priv->current_paragraph = l;
1634 job->priv->current_paragraph_line = line;
1638 setup_for_print (GtkSourcePrintJob *job)
1640 job->priv->current_paragraph = job->priv->paragraphs;
1641 job->priv->page = 0;
1642 job->priv->printed_lines = 0;
1644 if (job->priv->print_job != NULL)
1645 g_object_unref (job->priv->print_job);
1646 if (job->priv->print_ctxt != NULL)
1647 g_object_unref (job->priv->print_ctxt);
1649 job->priv->print_job = gnome_print_job_new (job->priv->config);
1650 job->priv->print_ctxt = gnome_print_job_get_context (job->priv->print_job);
1652 gnome_print_pango_update_context (job->priv->pango_context, job->priv->print_ctxt);
1656 print_job (GtkSourcePrintJob *job)
1658 while (job->priv->current_paragraph != NULL)
1661 gnome_print_job_close (job->priv->print_job);
1665 idle_printing_handler (GtkSourcePrintJob *job)
1667 g_assert (job->priv->current_paragraph != NULL);
1671 if (job->priv->current_paragraph == NULL)
1673 gnome_print_job_close (job->priv->print_job);
1674 job->priv->printing = FALSE;
1675 job->priv->idle_printing_tag = 0;
1677 g_signal_emit (job, print_job_signals [FINISHED], 0);
1678 /* after this the print job object is possibly
1679 * destroyed (common use case) */
1687 /* Public API ------------------- */
1690 * gtk_source_print_job_new:
1691 * @config: an optional #GnomePrintConfig object.
1693 * Creates a new print job object, initially setting the print configuration.
1695 * Return value: the new print job object.
1698 gtk_source_print_job_new (GnomePrintConfig *config)
1700 GtkSourcePrintJob *job;
1702 g_return_val_if_fail (config == NULL || GNOME_IS_PRINT_CONFIG (config), NULL);
1704 job = GTK_SOURCE_PRINT_JOB (g_object_new (GTK_TYPE_SOURCE_PRINT_JOB, NULL));
1706 gtk_source_print_job_set_config (job, config);
1712 * gtk_source_print_job_new_with_buffer:
1713 * @config: an optional #GnomePrintConfig.
1714 * @buffer: the #GtkTextBuffer to print (might be %NULL).
1716 * Creates a new print job to print @buffer.
1718 * Return value: a new print job object.
1721 gtk_source_print_job_new_with_buffer (GnomePrintConfig *config,
1722 GtkTextBuffer *buffer)
1724 GtkSourcePrintJob *job;
1726 g_return_val_if_fail (config == NULL || GNOME_IS_PRINT_CONFIG (config), NULL);
1727 g_return_val_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer), NULL);
1729 job = gtk_source_print_job_new (config);
1731 gtk_source_print_job_set_buffer (job, buffer);
1736 /* --- print job basic configuration */
1739 * gtk_source_print_job_set_config:
1740 * @job: a #GtkSourcePrintJob.
1741 * @config: a #GnomePrintConfig object to get printing configuration from.
1743 * Sets the print configuration for the job. If you don't set a
1744 * configuration object for the print job, when needed one will be
1745 * created with gnome_print_config_default().
1748 gtk_source_print_job_set_config (GtkSourcePrintJob *job,
1749 GnomePrintConfig *config)
1751 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1752 g_return_if_fail (GNOME_IS_PRINT_CONFIG (config));
1753 g_return_if_fail (!job->priv->printing);
1755 if (config == job->priv->config)
1758 if (job->priv->config != NULL)
1759 gnome_print_config_unref (job->priv->config);
1761 job->priv->config = config;
1762 gnome_print_config_ref (config);
1764 g_object_notify (G_OBJECT (job), "config");
1768 * gtk_source_print_job_get_config:
1769 * @job: a #GtkSourcePrintJob.
1771 * Gets the current #GnomePrintConfig the print job will use. If not
1772 * previously set, this will create a default configuration and return
1773 * it. The returned object reference is owned by the print job.
1775 * Return value: the #GnomePrintConfig for the print job.
1778 gtk_source_print_job_get_config (GtkSourcePrintJob *job)
1780 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
1782 ensure_print_config (job);
1784 return job->priv->config;
1788 * gtk_source_print_job_set_buffer:
1789 * @job: a #GtkSourcePrintJob.
1790 * @buffer: a #GtkTextBuffer.
1792 * Sets the #GtkTextBuffer the print job will print. You need to
1793 * specify a buffer to print, either by the use of this function or by
1794 * creating the print job with gtk_source_print_job_new_with_buffer().
1797 gtk_source_print_job_set_buffer (GtkSourcePrintJob *job,
1798 GtkTextBuffer *buffer)
1800 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1801 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1802 g_return_if_fail (!job->priv->printing);
1804 if (buffer == job->priv->buffer)
1807 if (job->priv->buffer != NULL)
1808 g_object_unref (job->priv->buffer);
1810 job->priv->buffer = buffer;
1811 g_object_ref (buffer);
1813 g_object_notify (G_OBJECT (job), "buffer");
1817 * gtk_source_print_job_get_buffer:
1818 * @job: a #GtkSourcePrintJob.
1820 * Gets the #GtkTextBuffer the print job would print. The returned
1821 * object reference (if non %NULL) is owned by the job object and
1822 * should not be unreferenced.
1824 * Return value: the #GtkTextBuffer to print.
1827 gtk_source_print_job_get_buffer (GtkSourcePrintJob *job)
1829 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
1831 return job->priv->buffer;
1834 /* --- print job layout and style configuration */
1837 * gtk_source_print_job_set_tabs_width:
1838 * @job: a #GtkSourcePrintJob.
1839 * @tabs_width: the number of equivalent spaces for a tabulation.
1841 * Sets the width (in equivalent spaces) of tabulations for the
1842 * printed text. The width in printing units will be calculated as
1843 * the width of a string containing @tabs_width spaces of the default
1844 * font. Tabulation stops are set for the full width of printed text.
1847 gtk_source_print_job_set_tabs_width (GtkSourcePrintJob *job,
1850 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1851 g_return_if_fail (!job->priv->printing);
1853 if (tabs_width == job->priv->tabs_width)
1856 job->priv->tabs_width = tabs_width;
1858 g_object_notify (G_OBJECT (job), "tabs_width");
1862 * gtk_source_print_job_get_tabs_width:
1863 * @job: a #GtkSourcePrintJob.
1865 * Determines the configured width (in equivalent spaces) of
1866 * tabulations. The default value is 8.
1868 * Return value: the width (in equivalent spaces) of a tabulation.
1871 gtk_source_print_job_get_tabs_width (GtkSourcePrintJob *job)
1873 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
1875 return job->priv->tabs_width;
1879 * gtk_source_print_job_set_wrap_mode:
1880 * @job: a #GtkSourcePrintJob.
1881 * @wrap: the wrap mode.
1883 * Sets the wrap mode for lines of text larger than the printable
1884 * width. See #GtkWrapMode for a definition of the possible values.
1887 gtk_source_print_job_set_wrap_mode (GtkSourcePrintJob *job,
1890 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1891 g_return_if_fail (!job->priv->printing);
1893 if (wrap == job->priv->wrap_mode)
1896 job->priv->wrap_mode = wrap;
1898 g_object_notify (G_OBJECT (job), "wrap_mode");
1902 * gtk_source_print_job_get_wrap_mode:
1903 * @job: a #GtkSourcePrintJob.
1905 * Determines the wrapping style for text lines wider than the
1906 * printable width. The default is no wrapping.
1908 * Return value: the current wrapping mode for the print job.
1911 gtk_source_print_job_get_wrap_mode (GtkSourcePrintJob *job)
1913 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), GTK_WRAP_NONE);
1915 return job->priv->wrap_mode;
1919 * gtk_source_print_job_set_highlight:
1920 * @job: a #GtkSourcePrintJob.
1921 * @highlight: %TRUE if the printed text should be highlighted.
1923 * Sets whether the printed text will be highlighted according to the
1924 * buffer rules. Both color and font style are applied.
1927 gtk_source_print_job_set_highlight (GtkSourcePrintJob *job,
1930 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1931 g_return_if_fail (!job->priv->printing);
1933 highlight = (highlight != FALSE);
1935 if (highlight == job->priv->highlight)
1938 job->priv->highlight = highlight;
1940 g_object_notify (G_OBJECT (job), "highlight");
1944 * gtk_source_print_job_get_highlight:
1945 * @job: a #GtkSourcePrintJob.
1947 * Determines if the job is configured to print the text highlighted
1948 * with colors and font styles. Note that highlighting will happen
1949 * only if the buffer to print has highlighting activated.
1951 * Return value: %TRUE if the printed output will be highlighted.
1954 gtk_source_print_job_get_highlight (GtkSourcePrintJob *job)
1956 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
1958 return job->priv->highlight;
1962 * gtk_source_print_job_set_font_desc:
1963 * @job: a #GtkSourcePrintJob.
1964 * @desc: the #PangoFontDescription for the default font
1966 * Sets the default font for the printed text.
1969 gtk_source_print_job_set_font_desc (GtkSourcePrintJob *job,
1970 PangoFontDescription *desc)
1972 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1973 g_return_if_fail (desc != NULL);
1974 g_return_if_fail (!job->priv->printing);
1976 desc = pango_font_description_copy (desc);
1977 if (job->priv->font != NULL)
1978 pango_font_description_free (job->priv->font);
1979 job->priv->font = desc;
1980 g_object_freeze_notify (G_OBJECT (job));
1981 g_object_notify (G_OBJECT (job), "font");
1982 g_object_notify (G_OBJECT (job), "font_desc");
1983 g_object_thaw_notify (G_OBJECT (job));
1987 * gtk_source_print_job_set_font:
1988 * @job: a #GtkSourcePrintJob.
1989 * @font_name: the name of the default font.
1991 * Sets the default font for the printed text. @font_name should be a
1992 * <emphasis>full font name</emphasis> GnomePrint can understand
1993 * (e.g. "Monospace Regular 10.0").
1995 * Note that @font_name is a #GnomeFont name not a Pango font
1996 * description string. This function is deprecated since #GnomeFont is
1997 * no longer used when implementing printing for GtkSourceView; you
1998 * should use gtk_source_print_job_set_font_desc() instead.
2001 gtk_source_print_job_set_font (GtkSourcePrintJob *job,
2002 const gchar *font_name)
2004 PangoFontDescription *desc;
2006 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2007 g_return_if_fail (font_name != NULL);
2008 g_return_if_fail (!job->priv->printing);
2010 desc = font_description_from_gnome_font_name (font_name);
2013 gtk_source_print_job_set_font_desc (job, desc);
2014 pango_font_description_free (desc);
2019 * gtk_source_print_job_get_font_desc:
2020 * @job: a #GtkSourcePrintJob.
2022 * Determines the default font to be used for the printed text. The
2023 * returned string is of the form "Fontfamily Style Size",
2024 * for example "Monospace Regular 10.0". The returned value
2025 * should be freed when no longer needed.
2027 * Return value: the current text font description. This value is
2028 * owned by the job and must not be modified or freed.
2030 PangoFontDescription *
2031 gtk_source_print_job_get_font_desc (GtkSourcePrintJob *job)
2033 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2035 ensure_print_config (job);
2037 return job->priv->font;
2041 * gtk_source_print_job_get_font:
2042 * @job: a #GtkSourcePrintJob.
2044 * Determines the default font to be used for the printed text. The
2045 * returned string is of the form "Fontfamily Style Size",
2046 * for example "Monospace Regular 10.0". The returned value
2047 * should be freed when no longer needed.
2049 * Note that the result is a #GnomeFont name not a Pango font
2050 * description string. This function is deprecated since #GnomeFont is
2051 * no longer used when implementing printing for GtkSourceView; you
2052 * should use gtk_source_print_job_get_font_desc() instead.
2054 * Return value: a newly allocated string with the name of the current
2058 gtk_source_print_job_get_font (GtkSourcePrintJob *job)
2060 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2062 ensure_print_config (job);
2064 return font_description_to_gnome_font_name (job->priv->font);
2068 * gtk_source_print_job_setup_from_view:
2069 * @job: a #GtkSourcePrintJob.
2070 * @view: a #GtkSourceView to get configuration from.
2072 * Convenience function to set several configuration options at once,
2073 * so that the printed output matches @view. The options set are
2074 * buffer (if not set already), tabs width, highlighting, wrap mode
2078 gtk_source_print_job_setup_from_view (GtkSourcePrintJob *job,
2081 GtkTextBuffer *buffer = NULL;
2082 PangoContext *pango_context;
2084 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2085 g_return_if_fail (!job->priv->printing);
2087 buffer = gtk_text_view_get_buffer (view);
2089 if (job->priv->buffer == NULL && buffer != NULL)
2090 gtk_source_print_job_set_buffer (job, buffer);
2092 /* gtk_source_print_job_set_tabs_width (job, gtk_source_view_get_tabs_width (view)); */
2093 gtk_source_print_job_set_highlight (job, TRUE);
2094 gtk_source_print_job_set_wrap_mode (job, gtk_text_view_get_wrap_mode (view));
2096 pango_context = gtk_widget_get_pango_context (GTK_WIDGET (view));
2097 gtk_source_print_job_set_font_desc (job,
2098 pango_context_get_font_description (pango_context));
2102 * gtk_source_print_job_set_numbers_font_desc:
2103 * @job: a #GtkSourcePrintJob.
2104 * @desc: the #PangoFontDescription for the font for line numbers, or %NULL
2106 * Sets the font for printing line numbers on the left margin. If
2107 * NULL is supplied, the default font (i.e. the one being used for the
2108 * text) will be used instead.
2111 gtk_source_print_job_set_numbers_font_desc (GtkSourcePrintJob *job,
2112 PangoFontDescription *desc)
2114 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2115 g_return_if_fail (!job->priv->printing);
2118 desc = pango_font_description_copy (desc);
2119 if (job->priv->numbers_font != NULL)
2120 pango_font_description_free (job->priv->numbers_font);
2121 job->priv->numbers_font = desc;
2122 g_object_freeze_notify (G_OBJECT (job));
2123 g_object_notify (G_OBJECT (job), "numbers_font");
2124 g_object_notify (G_OBJECT (job), "numbers_font_desc");
2125 g_object_thaw_notify (G_OBJECT (job));
2129 * gtk_source_print_job_set_numbers_font:
2130 * @job: a #GtkSourcePrintJob.
2131 * @font_name: the full name of the font for line numbers, or %NULL.
2133 * Sets the font for printing line numbers on the left margin. If
2134 * %NULL is supplied, the default font (i.e. the one being used for the
2135 * text) will be used instead.
2137 * Note that @font_name is a #GnomeFont name not a Pango font
2138 * description string. This function is deprecated since #GnomeFont is
2139 * no longer used when implementing printing for GtkSourceView; you
2140 * should use gtk_source_print_job_set_numbers_font_desc() instead.
2143 gtk_source_print_job_set_numbers_font (GtkSourcePrintJob *job,
2144 const gchar *font_name)
2146 PangoFontDescription *desc;
2148 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2149 g_return_if_fail (!job->priv->printing);
2151 if (font_name != NULL)
2153 desc = font_description_from_gnome_font_name (font_name);
2156 gtk_source_print_job_set_numbers_font_desc (job, desc);
2157 pango_font_description_free (desc);
2161 gtk_source_print_job_set_numbers_font (job, NULL);
2165 * gtk_source_print_job_get_numbers_font_desc:
2166 * @job: a #GtkSourcePrintJob.
2168 * Determines the font to be used for the line numbers. This function
2169 * might return %NULL if a specific font for numbers has not been set.
2171 * Return value: the line numbers font description or %NULL. This value is
2172 * owned by the job and must not be modified or freed.
2174 PangoFontDescription *
2175 gtk_source_print_job_get_numbers_font_desc (GtkSourcePrintJob *job)
2177 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2179 return job->priv->numbers_font;
2183 * gtk_source_print_job_get_numbers_font:
2184 * @job: a #GtkSourcePrintJob.
2186 * Determines the font to be used for the line numbers. The returned
2187 * string is of the form "Fontfamily Style Size", for
2188 * example "Monospace Regular 10.0". The returned value
2189 * should be freed when no longer needed. This function might return
2190 * %NULL if a specific font for numbers has not been set.
2192 * Note that the result is a #GnomeFont name not a Pango font
2193 * description string. This function is deprecated since #GnomeFont is
2194 * no longer used when implementing printing for GtkSourceView; you
2195 * should use gtk_source_print_job_get_numbers_font_desc() instead.
2197 * Return value: a newly allocated string with the name of the current
2198 * line numbers font, or %NULL.
2201 gtk_source_print_job_get_numbers_font (GtkSourcePrintJob *job)
2203 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2205 if (job->priv->numbers_font != NULL)
2206 return font_description_to_gnome_font_name (job->priv->numbers_font);
2212 * gtk_source_print_job_set_print_numbers:
2213 * @job: a #GtkSourcePrintJob.
2214 * @interval: interval for printed line numbers.
2216 * Sets the interval for printed line numbers. If @interval is 0 no
2217 * numbers will be printed. If greater than 0, a number will be
2218 * printed every @interval lines (i.e. 1 will print all line numbers).
2221 gtk_source_print_job_set_print_numbers (GtkSourcePrintJob *job,
2224 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2225 g_return_if_fail (!job->priv->printing);
2227 if (interval == job->priv->print_numbers)
2230 job->priv->print_numbers = interval;
2232 g_object_notify (G_OBJECT (job), "print_numbers");
2236 * gtk_source_print_job_get_print_numbers:
2237 * @job: a #GtkSourcePrintJob.
2239 * Determines the interval used for line number printing. If the
2240 * value is 0, no line numbers will be printed. The default value is
2241 * 1 (i.e. numbers printed in all lines).
2243 * Return value: the interval of printed line numbers.
2246 gtk_source_print_job_get_print_numbers (GtkSourcePrintJob *job)
2248 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2250 return job->priv->print_numbers;
2254 * gtk_source_print_job_set_text_margins:
2255 * @job: a #GtkSourcePrintJob.
2256 * @top: the top user margin.
2257 * @bottom: the bottom user margin.
2258 * @left: the left user margin.
2259 * @right: the right user margin.
2261 * Sets the four user margins for the print job. These margins are in
2262 * addition to the document margins provided in the #GnomePrintConfig
2263 * and will not be used for headers, footers or line numbers (those
2264 * are calculated separatedly). You can print in the space allocated
2265 * by these margins by connecting to the <link
2266 * linkend="GtkSourcePrintJob-begin-page">"begin_page"</link> signal. The
2267 * space is around the printed text, and inside the margins specified
2268 * in the #GnomePrintConfig.
2270 * The margin numbers are given in device units. If any of the given
2271 * values is less than 0, that particular margin is not altered by
2275 gtk_source_print_job_set_text_margins (GtkSourcePrintJob *job,
2281 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2282 g_return_if_fail (!job->priv->printing);
2285 job->priv->margin_top = top;
2287 job->priv->margin_bottom = bottom;
2289 job->priv->margin_left = left;
2291 job->priv->margin_right = right;
2295 * gtk_source_print_job_get_text_margins:
2296 * @job: a #GtkSourcePrintJob.
2297 * @top: a pointer to a #gdouble to return the top margin.
2298 * @bottom: a pointer to a #gdouble to return the bottom margin.
2299 * @left: a pointer to a #gdouble to return the left margin.
2300 * @right: a pointer to a #gdouble to return the right margin.
2302 * Determines the user set margins for the job. This function
2303 * retrieves the values previously set by
2304 * gtk_source_print_job_set_text_margins(). The default for all four
2305 * margins is 0. Any of the pointers can be %NULL if you want to
2306 * ignore that value.
2309 gtk_source_print_job_get_text_margins (GtkSourcePrintJob *job,
2315 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2318 *top = job->priv->margin_top;
2320 *bottom = job->priv->margin_bottom;
2322 *left = job->priv->margin_left;
2324 *right = job->priv->margin_right;
2327 /* --- printing operations */
2330 gtk_source_print_job_prepare (GtkSourcePrintJob *job,
2331 const GtkTextIter *start,
2332 const GtkTextIter *end)
2334 PROFILE (GTimer *timer);
2336 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2337 g_return_val_if_fail (!job->priv->printing, FALSE);
2338 g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
2339 g_return_val_if_fail (start != NULL && end != NULL, FALSE);
2341 /* make sure we have a sane configuration to start printing */
2342 ensure_print_config (job);
2344 PROFILE (timer = g_timer_new ());
2346 /* get the text to print */
2347 if (!get_text_to_print (job, start, end))
2350 PROFILE (g_message ("get_text_to_print: %.2f", g_timer_elapsed (timer, NULL)));
2352 if (!setup_pango_context (job))
2356 if (!update_page_size_and_margins (job))
2359 /* split the document in pages */
2360 if (!paginate_text (job))
2364 g_message ("paginate_text: %.2f", g_timer_elapsed (timer, NULL));
2365 g_timer_destroy (timer);
2372 * gtk_source_print_job_print:
2373 * @job: a configured #GtkSourcePrintJob.
2375 * Produces a #GnomePrintJob with the printed document. The whole
2376 * contents of the configured #GtkTextBuffer are printed. The
2377 * returned job is already closed and ready to be previewed (using
2378 * gnome_print_job_preview_new()) or printed directly. The caller of
2379 * this function owns a reference to the returned object, so @job can
2380 * be destroyed and the output will still be valid, or the document
2381 * can be printed again with different settings.
2383 * Return value: a closed #GnomePrintJob with the printed document, or
2384 * %NULL if printing could not be completed.
2387 gtk_source_print_job_print (GtkSourcePrintJob *job)
2389 GtkTextIter start, end;
2391 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2392 g_return_val_if_fail (!job->priv->printing, NULL);
2393 g_return_val_if_fail (job->priv->buffer != NULL, NULL);
2395 gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (job->priv->buffer), &start, &end);
2397 return gtk_source_print_job_print_range (job, &start, &end);
2401 * gtk_source_print_job_print_range:
2402 * @job: a configured #GtkSourcePrintJob.
2403 * @start: the start of the region of text to print.
2404 * @end: the end of the region of text to print.
2406 * Similar to gtk_source_print_job_print(), except you can specify a
2407 * range of text to print. The passed #GtkTextIter values might be
2410 * Return value: a closed #GnomePrintJob with the text from @start to
2411 * @end printed, or %NULL if @job could not print.
2414 gtk_source_print_job_print_range (GtkSourcePrintJob *job,
2415 const GtkTextIter *start,
2416 const GtkTextIter *end)
2418 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2419 g_return_val_if_fail (!job->priv->printing, NULL);
2420 g_return_val_if_fail (job->priv->buffer != NULL, NULL);
2421 g_return_val_if_fail (start != NULL && end != NULL, NULL);
2422 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
2423 GTK_TEXT_BUFFER (job->priv->buffer) &&
2424 gtk_text_iter_get_buffer (end) ==
2425 GTK_TEXT_BUFFER (job->priv->buffer), NULL);
2427 if (!gtk_source_print_job_prepare (job, start, end))
2430 /* real work starts here */
2431 setup_for_print (job);
2433 job->priv->printing = TRUE;
2435 job->priv->printing = FALSE;
2437 g_object_ref (job->priv->print_job);
2438 return job->priv->print_job;
2441 /* --- asynchronous printing */
2444 * gtk_source_print_job_print_range_async:
2445 * @job: a configured #GtkSourcePrintJob.
2446 * @start: the start of the region of text to print.
2447 * @end: the end of the region of text to print.
2449 * Starts to print @job asynchronously. This function will ready the
2450 * @job for printing and install an idle handler that will render one
2453 * This function will not return immediatly, as only page rendering is
2454 * done asynchronously. Text retrieval and paginating happens within
2455 * this function. Also, if highlighting is enabled, the whole buffer
2456 * needs to be highlighted first.
2458 * To get notification when the job has finished, you must connect to
2460 * linkend="GtkSourcePrintJob-finished">"finished"</link>
2461 * signal. After this signal is emitted you can retrieve the
2462 * resulting #GnomePrintJob with gtk_source_print_job_get_print_job().
2463 * You may cancel the job with gtk_source_print_job_cancel().
2465 * Return value: %TRUE if the print started.
2468 gtk_source_print_job_print_range_async (GtkSourcePrintJob *job,
2469 const GtkTextIter *start,
2470 const GtkTextIter *end)
2472 GSource *idle_source;
2474 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2475 g_return_val_if_fail (!job->priv->printing, FALSE);
2476 g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
2477 g_return_val_if_fail (start != NULL && end != NULL, FALSE);
2478 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
2479 GTK_TEXT_BUFFER (job->priv->buffer) &&
2480 gtk_text_iter_get_buffer (end) ==
2481 GTK_TEXT_BUFFER (job->priv->buffer), FALSE);
2483 if (!gtk_source_print_job_prepare (job, start, end))
2486 /* real work starts here */
2487 setup_for_print (job);
2488 if (job->priv->current_paragraph == NULL)
2491 /* setup the idle handler to print each page at a time */
2492 idle_source = g_idle_source_new ();
2493 g_source_set_priority (idle_source, GTK_SOURCE_PRINT_JOB_PRIORITY);
2494 g_source_set_closure (idle_source,
2495 g_cclosure_new_object ((GCallback) idle_printing_handler,
2497 job->priv->idle_printing_tag = g_source_attach (idle_source, NULL);
2498 g_source_unref (idle_source);
2500 job->priv->printing = TRUE;
2506 * gtk_source_print_job_cancel:
2507 * @job: a #GtkSourcePrintJob.
2509 * Cancels an asynchronous printing operation. This will remove any
2510 * pending print idle handler and unref the current #GnomePrintJob.
2512 * Note that if you got a reference to the job's #GnomePrintJob (using
2513 * gtk_source_print_job_get_print_job()) it will not be destroyed
2514 * (since you hold a reference to it), but it will not be closed
2515 * either. If you wish to show or print the partially printed
2516 * document you need to close it yourself.
2518 * This function has no effect when called from a non-asynchronous
2522 gtk_source_print_job_cancel (GtkSourcePrintJob *job)
2524 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2525 g_return_if_fail (job->priv->printing);
2527 if (job->priv->idle_printing_tag > 0)
2529 g_source_remove (job->priv->idle_printing_tag);
2530 job->priv->current_paragraph = NULL;
2531 job->priv->idle_printing_tag = 0;
2532 job->priv->printing = FALSE;
2533 g_object_unref (job->priv->print_job);
2534 g_object_unref (job->priv->print_ctxt);
2535 job->priv->print_job = NULL;
2536 job->priv->print_ctxt = NULL;
2541 * gtk_source_print_job_get_print_job:
2542 * @job: a #GtkSourcePrintJob.
2544 * Gets a reference to the #GnomePrintJob which the @job is printing
2545 * or has recently finished printing. You need to unref the returned
2548 * You may call this function in the middle of an asynchronous
2549 * printing operation, but the returned #GnomePrintJob will not be
2550 * closed until the last page is printed and the <link
2551 * linkend="GtkSourcePrintJob-finished">"finished"</link>
2552 * signal is emitted.
2554 * Return value: a new reference to the @job's #GnomePrintJob, or
2558 gtk_source_print_job_get_print_job (GtkSourcePrintJob *job)
2560 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2562 if (job->priv->print_job)
2563 g_object_ref (job->priv->print_job);
2565 return job->priv->print_job;
2568 /* --- information for asynchronous ops and headers and footers callback */
2571 * gtk_source_print_job_get_page:
2572 * @job: a #GtkSourcePrintJob.
2574 * Determines the currently printing page number. This function is
2575 * only valid while printing (either synchronously or asynchronously).
2577 * Return value: the current page number.
2580 gtk_source_print_job_get_page (GtkSourcePrintJob *job)
2582 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2583 g_return_val_if_fail (job->priv->printing, 0);
2585 return job->priv->page;
2589 * gtk_source_print_job_get_page_count:
2590 * @job: a #GtkSourcePrintJob.
2592 * Determines the total number of pages the job will print. The
2593 * returned value is only meaninful after pagination has finished. In
2594 * practice, for synchronous printing this means when <link
2595 * linkend="GtkSourcePrintJob-begin-page">"begin_page"</link>
2596 * is emitted, or after gtk_source_print_job_print_range_async() has
2599 * Return value: the number of pages of the printed job.
2602 gtk_source_print_job_get_page_count (GtkSourcePrintJob *job)
2604 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2606 return job->priv->page_count;
2610 * gtk_source_print_job_get_print_context:
2611 * @job: a printing #GtkSourcePrintJob.
2613 * Determines the #GnomePrintContext of the current job. This
2614 * function is only valid while printing. Normally you would use this
2615 * function to print in the margins set by
2616 * gtk_source_print_job_set_margins() in a handler for the <link
2617 * linkend="GtkSourcePrintJob-begin-page">"begin_page"</link>
2620 * Return value: the current #GnomePrintContext. The returned object
2624 gtk_source_print_job_get_print_context (GtkSourcePrintJob *job)
2626 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2627 g_return_val_if_fail (job->priv->printing, NULL);
2629 return job->priv->print_ctxt;
2632 /* ---- Header and footer (default implementation) */
2634 /* Most of this code taken from GLib's g_date_strftime() in gdate.c
2635 * GLIB - Library of useful routines for C programming
2636 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald */
2639 strdup_strftime (const gchar *format, const struct tm *tm)
2641 gsize locale_format_len = 0;
2642 gchar *locale_format;
2648 GError *error = NULL;
2650 g_return_val_if_fail (format != NULL, NULL);
2651 g_return_val_if_fail (tm != NULL, NULL);
2653 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
2657 g_warning (G_STRLOC "Error converting format to locale encoding: %s",
2659 g_error_free (error);
2664 tmpbufsize = MAX (128, locale_format_len * 2);
2667 tmpbuf = g_malloc (tmpbufsize);
2669 /* Set the first byte to something other than '\0', to be able to
2670 * recognize whether strftime actually failed or just returned "".
2673 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, tm);
2675 if (tmplen == 0 && tmpbuf[0] != '\0')
2680 if (tmpbufsize > 65536)
2682 g_warning (G_STRLOC "Maximum buffer size for strdup_strftime "
2683 "exceeded: giving up");
2684 g_free (locale_format);
2691 g_free (locale_format);
2693 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
2698 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s",
2700 g_error_free (error);
2709 evaluate_format_string (GtkSourcePrintJob *job, const gchar *format)
2712 gchar *eval_str, *retval;
2713 const struct tm *tm;
2719 tm = localtime (&now);
2721 /* analyze format string and replace the codes we know */
2722 eval = g_string_new_len (NULL, strlen (format));
2723 ch = g_utf8_get_char (format);
2728 format = g_utf8_next_char (format);
2729 ch = g_utf8_get_char (format);
2731 g_string_append_printf (eval, "%d", job->priv->page);
2733 g_string_append_printf (eval, "%d", job->priv->page_count);
2736 g_string_append_c (eval, '%');
2737 g_string_append_unichar (eval, ch);
2741 g_string_append_unichar (eval, ch);
2743 format = g_utf8_next_char (format);
2744 ch = g_utf8_get_char (format);
2747 eval_str = g_string_free (eval, FALSE);
2748 retval = strdup_strftime (eval_str, tm);
2755 print_header_footer_string (GtkSourcePrintJob *job,
2756 const gchar *format,
2761 PangoLayout *layout;
2766 width = job->priv->text_width + job->priv->numbers_width;
2768 text = evaluate_format_string (job, format);
2771 layout = pango_layout_new (job->priv->pango_context);
2772 pango_layout_set_font_description (layout, job->priv->header_footer_font);
2773 pango_layout_set_text (layout, text, -1);
2775 xx = x + x_align * (width - get_layout_width (layout));
2776 gnome_print_moveto (job->priv->print_ctxt, xx, y);
2777 show_first_layout_line (job->priv->print_ctxt, layout);
2780 g_object_unref (layout);
2785 default_print_header (GtkSourcePrintJob *job,
2791 gdouble ascent, descent;
2793 width = job->priv->text_width + job->priv->numbers_width;
2795 get_font_ascent_descent (job, job->priv->header_footer_font, &ascent, &descent);
2800 if (job->priv->header_format_left != NULL)
2801 print_header_footer_string (job, job->priv->header_format_left, 0.0, x, yy);
2804 if (job->priv->header_format_right != NULL)
2805 print_header_footer_string (job, job->priv->header_format_right, 1.0, x, yy);
2808 if (job->priv->header_format_center != NULL)
2809 print_header_footer_string (job, job->priv->header_format_center, 0.5, x, yy);
2812 if (job->priv->header_separator)
2814 yy = y - (SEPARATOR_SPACING * (ascent + descent));
2815 gnome_print_setlinewidth (job->priv->print_ctxt, SEPARATOR_LINE_WIDTH);
2816 gnome_print_moveto (job->priv->print_ctxt, x, yy);
2817 gnome_print_lineto (job->priv->print_ctxt, x + width, yy);
2818 gnome_print_stroke (job->priv->print_ctxt);
2823 default_print_footer (GtkSourcePrintJob *job,
2829 gdouble ascent, descent;
2831 width = job->priv->text_width + job->priv->numbers_width;
2833 get_font_ascent_descent (job, job->priv->header_footer_font, &ascent, &descent);
2835 yy = y - job->priv->footer_height + descent;
2838 if (job->priv->footer_format_left != NULL)
2839 print_header_footer_string (job, job->priv->footer_format_left, 0.0, x, yy);
2842 if (job->priv->footer_format_right != NULL)
2843 print_header_footer_string (job, job->priv->footer_format_right, 1.0, x, yy);
2846 if (job->priv->footer_format_center != NULL)
2847 print_header_footer_string (job, job->priv->footer_format_center, 0.5, x, yy);
2850 if (job->priv->footer_separator)
2852 yy = y - job->priv->footer_height +
2853 (SEPARATOR_SPACING * (ascent + descent));
2854 gnome_print_setlinewidth (job->priv->print_ctxt, SEPARATOR_LINE_WIDTH);
2855 gnome_print_moveto (job->priv->print_ctxt, x, yy);
2856 gnome_print_lineto (job->priv->print_ctxt, x + width, yy);
2857 gnome_print_stroke (job->priv->print_ctxt);
2862 * gtk_source_print_job_set_print_header:
2863 * @job: a #GtkSourcePrintJob.
2864 * @setting: %TRUE if you want the header to be printed.
2866 * Sets whether you want to print a header in each page. The default
2867 * header consists of three pieces of text and an optional line
2868 * separator, configurable with
2869 * gtk_source_print_job_set_header_format().
2871 * Note that by default the header format is unspecified, and if it's
2872 * empty it will not be printed, regardless of this setting.
2875 gtk_source_print_job_set_print_header (GtkSourcePrintJob *job,
2878 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2879 g_return_if_fail (!job->priv->printing);
2881 setting = (setting != FALSE);
2883 if (setting == job->priv->print_header)
2886 job->priv->print_header = setting;
2888 g_object_notify (G_OBJECT (job), "print_header");
2892 * gtk_source_print_job_get_print_header:
2893 * @job: a #GtkSourcePrintJob.
2895 * Determines if a header is set to be printed for each page. A
2896 * header will be printed if this function returns %TRUE
2897 * <emphasis>and</emphasis> some format strings have been specified
2898 * with gtk_source_print_job_set_header_format().
2900 * Return value: %TRUE if the header is set to be printed.
2903 gtk_source_print_job_get_print_header (GtkSourcePrintJob *job)
2905 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2907 return job->priv->print_header;
2911 * gtk_source_print_job_set_print_footer:
2912 * @job: a #GtkSourcePrintJob.
2913 * @setting: %TRUE if you want the footer to be printed.
2915 * Sets whether you want to print a footer in each page. The default
2916 * footer consists of three pieces of text and an optional line
2917 * separator, configurable with
2918 * gtk_source_print_job_set_footer_format().
2920 * Note that by default the footer format is unspecified, and if it's
2921 * empty it will not be printed, regardless of this setting.
2924 gtk_source_print_job_set_print_footer (GtkSourcePrintJob *job,
2927 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2928 g_return_if_fail (!job->priv->printing);
2930 setting = (setting != FALSE);
2932 if (setting == job->priv->print_footer)
2935 job->priv->print_footer = setting;
2937 g_object_notify (G_OBJECT (job), "print_footer");
2941 * gtk_source_print_job_get_print_footer:
2942 * @job: a #GtkSourcePrintJob.
2944 * Determines if a footer is set to be printed for each page. A
2945 * footer will be printed if this function returns %TRUE
2946 * <emphasis>and</emphasis> some format strings have been specified
2947 * with gtk_source_print_job_set_footer_format().
2949 * Return value: %TRUE if the footer is set to be printed.
2952 gtk_source_print_job_get_print_footer (GtkSourcePrintJob *job)
2954 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2956 return job->priv->print_footer;
2960 * gtk_source_print_job_set_header_footer_font_desc:
2961 * @job: a #GtkSourcePrintJob.
2962 * @desc: the #PangoFontDescription for the font to be used in headers and footers, or %NULL.
2964 * Sets the font for printing headers and footers. If %NULL is
2965 * supplied, the default font (i.e. the one being used for the text)
2966 * will be used instead.
2969 gtk_source_print_job_set_header_footer_font_desc (GtkSourcePrintJob *job,
2970 PangoFontDescription *desc)
2972 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2973 g_return_if_fail (!job->priv->printing);
2976 desc = pango_font_description_copy (desc);
2977 if (job->priv->header_footer_font != NULL)
2978 pango_font_description_free (job->priv->header_footer_font);
2979 job->priv->header_footer_font = desc;
2980 g_object_freeze_notify (G_OBJECT (job));
2981 g_object_notify (G_OBJECT (job), "header_footer_font");
2982 g_object_notify (G_OBJECT (job), "header_footer_font_desc");
2983 g_object_thaw_notify (G_OBJECT (job));
2987 * gtk_source_print_job_set_header_footer_font:
2988 * @job: a #GtkSourcePrintJob.
2989 * @font_name: the full name of the font to be used in headers and footers, or %NULL.
2991 * Sets the font for printing headers and footers. If %NULL is
2992 * supplied, the default font (i.e. the one being used for the text)
2993 * will be used instead.
2995 * Note that @font_name is a #GnomeFont name not a Pango font
2996 * description string. This function is deprecated since #GnomeFont is
2997 * no longer used when implementing printing for GtkSourceView; you
2998 * should use gtk_source_print_job_set_header_footer_font_desc() instead.
3001 gtk_source_print_job_set_header_footer_font (GtkSourcePrintJob *job,
3002 const gchar *font_name)
3004 PangoFontDescription *desc;
3006 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3007 g_return_if_fail (!job->priv->printing);
3009 if (font_name != NULL)
3011 desc = font_description_from_gnome_font_name (font_name);
3014 gtk_source_print_job_set_header_footer_font_desc (job, desc);
3015 pango_font_description_free (desc);
3019 gtk_source_print_job_set_header_footer_font_desc (job, NULL);
3023 * gtk_source_print_job_get_header_footer_font_desc:
3024 * @job: a #GtkSourcePrintJob.
3026 * Determines the font to be used for the header and footer. This function
3027 * might return %NULL if a specific font has not been set.
3029 * Return value: the header and footer font description or %NULL. This value is
3030 * owned by the job and must not be modified or freed.
3032 PangoFontDescription *
3033 gtk_source_print_job_get_header_footer_font_desc (GtkSourcePrintJob *job)
3035 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
3037 return job->priv->header_footer_font;
3041 * gtk_source_print_job_get_header_footer_font:
3042 * @job: a #GtkSourcePrintJob.
3044 * Determines the font to be used for the header and footer. The
3045 * returned string is of the form "Fontfamily Style Size",
3046 * for example "Monospace Regular 10.0". The returned value
3047 * should be freed when no longer needed. This function might return
3048 * %NULL if a specific font has not been set.
3050 * Note that the result is a #GnomeFont name not a Pango font
3051 * description string. This function is deprecated since #GnomeFont is
3052 * no longer used when implementing printing for GtkSourceView; you
3053 * should use gtk_source_print_job_get_header_footer_font_desc() instead.
3055 * Return value: a newly allocated string with the name of the current
3056 * header and footer font, or %NULL.
3059 gtk_source_print_job_get_header_footer_font (GtkSourcePrintJob *job)
3061 g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
3063 if (job->priv->header_footer_font != NULL)
3064 return font_description_to_gnome_font_name (job->priv->header_footer_font);
3070 * gtk_source_print_job_set_header_format:
3071 * @job: a #GtkSourcePrintJob.
3072 * @left: a format string to print on the left of the header.
3073 * @center: a format string to print on the center of the header.
3074 * @right: a format string to print on the right of the header.
3075 * @separator: %TRUE if you want a separator line to be printed.
3077 * Sets strftime like header format strings, to be printed on the
3078 * left, center and right of the top of each page. The strings may
3079 * include strftime(3) codes which will be expanded at print time.
3080 * All strftime() codes are accepted, with the addition of %N for the
3081 * page number and %Q for the page count.
3083 * @separator specifies if a solid line should be drawn to separate
3084 * the header from the document text.
3086 * If %NULL is given for any of the three arguments, that particular
3087 * string will not be printed. For the header to be printed, in
3088 * addition to specifying format strings, you need to enable header
3089 * printing with gtk_source_print_job_set_print_header().
3092 gtk_source_print_job_set_header_format (GtkSourcePrintJob *job,
3094 const gchar *center,
3098 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3099 g_return_if_fail (!job->priv->printing);
3101 /* FIXME: validate given strings? */
3102 g_free (job->priv->header_format_left);
3103 g_free (job->priv->header_format_center);
3104 g_free (job->priv->header_format_right);
3105 job->priv->header_format_left = g_strdup (left);
3106 job->priv->header_format_center = g_strdup (center);
3107 job->priv->header_format_right = g_strdup (right);
3108 job->priv->header_separator = separator;
3112 * gtk_source_print_job_set_footer_format:
3113 * @job: a #GtkSourcePrintJob.
3114 * @left: a format string to print on the left of the footer.
3115 * @center: a format string to print on the center of the footer.
3116 * @right: a format string to print on the right of the footer.
3117 * @separator: %TRUE if you want a separator line to be printed.
3119 * Like gtk_source_print_job_set_header_format(), but for the footer.
3122 gtk_source_print_job_set_footer_format (GtkSourcePrintJob *job,
3124 const gchar *center,
3128 g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3129 g_return_if_fail (!job->priv->printing);
3131 /* FIXME: validate given strings? */
3132 g_free (job->priv->footer_format_left);
3133 g_free (job->priv->footer_format_center);
3134 g_free (job->priv->footer_format_right);
3135 job->priv->footer_format_left = g_strdup (left);
3136 job->priv->footer_format_center = g_strdup (center);
3137 job->priv->footer_format_right = g_strdup (right);
3138 job->priv->footer_separator = separator;