2005-06-28 [colin] 1.9.12cvs2
[claws.git] / src / gtk / gtksourceprintjob.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
2 /*
3  * gtksourceprintjob.c
4  * This file is part of GtkSourceView
5  *
6  * Derived from gedit-print.c
7  *
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.
12  *
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.
17  *
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.
22  *
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. 
27  */
28  
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #ifdef USE_GNOMEPRINT
34
35 #include <string.h>
36 #include <time.h>
37
38 #include "gtksourceprintjob.h"
39
40 #include <glib/gi18n.h>
41 #include <gtk/gtkmain.h>
42 #include <gtk/gtktextview.h>
43 #include <libgnomeprint/gnome-print-pango.h>
44
45 #ifdef ENABLE_PROFILE
46 #define PROFILE(x) x
47 #else
48 #define PROFILE(x)
49 #endif
50
51 #ifdef ENABLE_DEBUG
52 #define DEBUG(x) x
53 #else
54 #define DEBUG(x)
55 #endif
56
57
58 #define DEFAULT_FONT_NAME   "Monospace 10"
59 #define DEFAULT_COLOR       0x000000ff
60
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)
64
65 #define NUMBERS_TEXT_SEPARATION CM(0.5)
66
67 #define HEADER_FOOTER_SIZE      2.5
68 #define SEPARATOR_SPACING       1.5
69 #define SEPARATOR_LINE_WIDTH    1.0
70
71
72 typedef struct _TextSegment TextSegment;
73 typedef struct _Paragraph   Paragraph;
74 typedef struct _TextStyle   TextStyle;
75
76 /* a piece of text (within a paragraph) of the same style */
77 struct _TextSegment
78 {
79         TextSegment             *next;
80         TextStyle               *style;
81         gchar                   *text;
82 };
83
84 /* a printable line */
85 struct _Paragraph
86 {
87         guint                    line_number;
88         TextSegment             *segment;
89 };
90
91 /* the style of a TextSegment */
92 struct _TextStyle
93 {
94         PangoFontDescription    *font_desc;
95         GdkColor                *foreground;
96         GdkColor                *background;
97         gdouble                  scale;
98         gboolean                 strikethrough;
99         PangoUnderline           underline;
100 };
101
102
103 struct _GtkSourcePrintJobPrivate
104 {
105         /* General job configuration */
106         GnomePrintConfig        *config;
107         GtkTextBuffer           *buffer;
108         guint                    tabs_width;
109         GtkWrapMode              wrap_mode;
110         gboolean                 highlight;
111         PangoLanguage           *language;
112         PangoFontDescription    *font;
113         PangoFontDescription    *numbers_font;
114         guint                    print_numbers;
115         gdouble                  margin_top;
116         gdouble                  margin_bottom;
117         gdouble                  margin_left;
118         gdouble                  margin_right;
119
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;
132
133         /* Job data */
134         guint                    first_line_number;
135         guint                    last_line_number;
136         GSList                  *paragraphs;
137
138         /* Job state */
139         gboolean                 printing;
140         guint                    idle_printing_tag;
141         GnomePrintContext       *print_ctxt;
142         GnomePrintJob           *print_job;
143         PangoContext            *pango_context;
144         PangoTabArray           *tab_array;
145         gint                     page;
146         gint                     page_count;
147         gdouble                  available_height;
148         GSList                  *current_paragraph;
149         gint                     current_paragraph_line;
150         guint                    printed_lines;
151
152         /* Cached information - all this information is obtained from
153          * other fields in the configuration */
154         GHashTable              *tag_styles;
155
156         gdouble                  page_width;
157         gdouble                  page_height;
158         /* outer margins */
159         gdouble                  doc_margin_top;
160         gdouble                  doc_margin_left;
161         gdouble                  doc_margin_right;
162         gdouble                  doc_margin_bottom;
163
164         gdouble                  header_height;
165         gdouble                  footer_height;
166         gdouble                  numbers_width;
167
168         /* printable (for the document itself) size */
169         gdouble                  text_width;
170         gdouble                  text_height;
171 };
172
173
174 enum
175 {
176         PROP_0,
177         PROP_CONFIG,
178         PROP_BUFFER,
179         PROP_TABS_WIDTH,
180         PROP_WRAP_MODE,
181         PROP_HIGHLIGHT,
182         PROP_FONT,
183         PROP_FONT_DESC,
184         PROP_NUMBERS_FONT,
185         PROP_NUMBERS_FONT_DESC,
186         PROP_PRINT_NUMBERS,
187         PROP_PRINT_HEADER,
188         PROP_PRINT_FOOTER,
189         PROP_HEADER_FOOTER_FONT,
190         PROP_HEADER_FOOTER_FONT_DESC
191 };
192
193 enum
194 {
195         BEGIN_PAGE = 0,
196         FINISHED,
197         LAST_SIGNAL
198 };
199
200 static GObjectClass *parent_class = NULL;
201 static guint         print_job_signals [LAST_SIGNAL] = { 0 };
202
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,
207                                                     guint                   property_id,
208                                                     GValue                 *value,
209                                                     GParamSpec             *pspec);
210 static void     gtk_source_print_job_set_property  (GObject                *object,
211                                                     guint                   property_id,
212                                                     const GValue           *value,
213                                                     GParamSpec             *pspec);
214 static void     gtk_source_print_job_begin_page    (GtkSourcePrintJob      *job);
215
216 static void     default_print_header               (GtkSourcePrintJob      *job,
217                                                     gdouble                 x,
218                                                     gdouble                 y);
219 static void     default_print_footer               (GtkSourcePrintJob      *job,
220                                                     gdouble                 x,
221                                                     gdouble                 y);
222
223
224 GType
225 gtk_source_print_job_get_type (void)
226 {
227         static GType our_type = 0;
228
229         if (our_type == 0)
230         {
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),
239                         0,      /* n_preallocs */
240                         (GInstanceInitFunc) gtk_source_print_job_instance_init
241                 };
242
243                 our_type = g_type_register_static (G_TYPE_OBJECT,
244                                                    "GtkSourcePrintJob",
245                                                    &our_info, 
246                                                    0);
247         }
248         
249         return our_type;
250 }
251         
252 static void
253 gtk_source_print_job_class_init (GtkSourcePrintJobClass *klass)
254 {
255         GObjectClass *object_class;
256
257         object_class = G_OBJECT_CLASS (klass);
258         parent_class = g_type_class_peek_parent (klass);
259                 
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;
263
264         klass->begin_page = gtk_source_print_job_begin_page;
265         klass->finished = NULL;
266         
267         g_object_class_install_property (object_class,
268                                          PROP_CONFIG,
269                                          g_param_spec_object ("config",
270                                                               _("Configuration"),
271                                                               _("Configuration options for "
272                                                                 "the print job"),
273                                                               GNOME_TYPE_PRINT_CONFIG,
274                                                               G_PARAM_READWRITE));
275         g_object_class_install_property (object_class,
276                                          PROP_BUFFER,
277                                          g_param_spec_object ("buffer",
278                                                               _("Source Buffer"),
279                                                               _("GtkTextBuffer object to print"),
280                                                               GTK_TYPE_TEXT_BUFFER,
281                                                               G_PARAM_READWRITE));
282         g_object_class_install_property (object_class,
283                                          PROP_TABS_WIDTH,
284                                          g_param_spec_uint ("tabs_width",
285                                                             _("Tabs Width"),
286                                                             _("Width in equivalent space "
287                                                               "characters of tabs"),
288                                                             0, 100, 8,
289                                                             G_PARAM_READWRITE));
290         g_object_class_install_property (object_class,
291                                          PROP_WRAP_MODE,
292                                          g_param_spec_enum ("wrap_mode",
293                                                             _("Wrap Mode"),
294                                                             _("Word wrapping mode"),
295                                                             GTK_TYPE_WRAP_MODE,
296                                                             GTK_WRAP_NONE,
297                                                             G_PARAM_READWRITE));
298         g_object_class_install_property (object_class,
299                                          PROP_HIGHLIGHT,
300                                          g_param_spec_boolean ("highlight",
301                                                                _("Highlight"),
302                                                                _("Whether to print the "
303                                                                  "document with highlighted "
304                                                                  "syntax"),
305                                                                TRUE,
306                                                                G_PARAM_READWRITE));
307         g_object_class_install_property (object_class,
308                                          PROP_FONT,
309                                          g_param_spec_string ("font",
310                                                               _("Font"),
311                                                               _("GnomeFont name to use for the "
312                                                                 "document text (deprecated)"),
313                                                               NULL,
314                                                               G_PARAM_READWRITE));
315         g_object_class_install_property (object_class,
316                                          PROP_FONT_DESC,
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,
322                                                               G_PARAM_READWRITE));
323         g_object_class_install_property (object_class,
324                                          PROP_NUMBERS_FONT,
325                                          g_param_spec_string ("numbers_font",
326                                                               _("Numbers Font"),
327                                                               _("GnomeFont name to use for the "
328                                                                 "line numbers (deprecated)"),
329                                                               NULL,
330                                                               G_PARAM_READWRITE));
331         g_object_class_install_property (object_class,
332                                          PROP_NUMBERS_FONT_DESC,
333                                          g_param_spec_boxed ("numbers_font_desc",
334                                                              _("Numbers Font"),
335                                                              _("Font description to use for the "
336                                                                "line numbers"),
337                                                              PANGO_TYPE_FONT_DESCRIPTION,
338                                                               G_PARAM_READWRITE));
339         g_object_class_install_property (object_class,
340                                          PROP_PRINT_NUMBERS,
341                                          g_param_spec_uint ("print_numbers",
342                                                             _("Print Line Numbers"),
343                                                             _("Interval of printed line numbers "
344                                                               "(0 means no numbers)"),
345                                                             0, 100, 1,
346                                                             G_PARAM_READWRITE));
347         g_object_class_install_property (object_class,
348                                          PROP_PRINT_HEADER,
349                                          g_param_spec_boolean ("print_header",
350                                                                _("Print Header"),
351                                                                _("Whether to print a header "
352                                                                  "in each page"),
353                                                                FALSE,
354                                                                G_PARAM_READWRITE));
355         g_object_class_install_property (object_class,
356                                          PROP_PRINT_FOOTER,
357                                          g_param_spec_boolean ("print_footer",
358                                                                _("Print Footer"),
359                                                                _("Whether to print a footer "
360                                                                  "in each page"),
361                                                                FALSE,
362                                                                G_PARAM_READWRITE));
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)"),
369                                                               NULL,
370                                                               G_PARAM_READWRITE));
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,
378                                                              G_PARAM_READWRITE));
379         
380         print_job_signals [BEGIN_PAGE] =
381             g_signal_new ("begin_page",
382                           G_OBJECT_CLASS_TYPE (object_class),
383                           G_SIGNAL_RUN_LAST,
384                           G_STRUCT_OFFSET (GtkSourcePrintJobClass, begin_page),
385                           NULL, NULL,
386                           g_cclosure_marshal_VOID__VOID,
387                           G_TYPE_NONE, 
388                           0);
389         print_job_signals [FINISHED] =
390             g_signal_new ("finished",
391                           G_OBJECT_CLASS_TYPE (object_class),
392                           G_SIGNAL_RUN_FIRST,
393                           G_STRUCT_OFFSET (GtkSourcePrintJobClass, finished),
394                           NULL, NULL,
395                           g_cclosure_marshal_VOID__VOID,
396                           G_TYPE_NONE, 
397                           0);
398 }
399
400 static void
401 gtk_source_print_job_instance_init (GtkSourcePrintJob *job)
402 {
403         GtkSourcePrintJobPrivate *priv;
404
405         priv = g_new0 (GtkSourcePrintJobPrivate, 1);
406         job->priv = priv;
407
408         /* default job configuration */
409         priv->config = NULL;
410         priv->buffer = NULL;
411
412         priv->tabs_width = 8;
413         priv->wrap_mode = GTK_WRAP_NONE;
414         priv->highlight = TRUE;
415         priv->language = gtk_get_default_language ();
416         priv->font = NULL;
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;
423
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;
435
436         /* initial state */
437         priv->printing = FALSE;
438         priv->print_ctxt = NULL;
439         priv->print_job = NULL;
440         priv->page = 0;
441         priv->page_count = 0;
442
443         priv->first_line_number = 0;
444         priv->paragraphs = NULL;
445         priv->tag_styles = NULL;
446
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);
454 }
455
456 static void
457 free_paragraphs (GSList *paras)
458 {
459         while (paras != NULL)
460         {
461                 Paragraph *para = paras->data;
462                 TextSegment *seg =  para->segment;
463                 while (seg != NULL)
464                 {
465                         TextSegment *next = seg->next;
466                         g_free (seg->text);
467                         g_free (seg);
468                         seg = next;
469                 }
470                 g_free (para);
471                 paras = g_slist_delete_link (paras, paras);
472         }
473 }
474
475 static void
476 gtk_source_print_job_finalize (GObject *object)
477 {
478         GtkSourcePrintJob *job;
479         GtkSourcePrintJobPrivate *priv;
480         
481         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (object));
482         
483         job = GTK_SOURCE_PRINT_JOB (object);
484         priv = job->priv;
485         
486         if (priv != NULL)
487         {
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);
504                 
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);
513
514                 if (priv->paragraphs != NULL)
515                         free_paragraphs (priv->paragraphs);
516                 if (priv->tag_styles != NULL)
517                         g_hash_table_destroy (priv->tag_styles);
518                 
519                 g_free (priv);
520                 job->priv = NULL;
521         }
522         
523         G_OBJECT_CLASS (parent_class)->finalize (object);
524 }
525
526 static void 
527 gtk_source_print_job_get_property (GObject    *object,
528                                    guint       prop_id,
529                                    GValue     *value,
530                                    GParamSpec *pspec)
531 {
532         GtkSourcePrintJob *job = GTK_SOURCE_PRINT_JOB (object);
533
534         switch (prop_id)
535         {
536                 case PROP_CONFIG:
537                         g_value_set_object (value, job->priv->config);
538                         break;
539                         
540                 case PROP_BUFFER:
541                         g_value_set_object (value, job->priv->buffer);
542                         break;
543
544                 case PROP_TABS_WIDTH:
545                         g_value_set_uint (value, job->priv->tabs_width);
546                         break;
547                         
548                 case PROP_WRAP_MODE:
549                         g_value_set_enum (value, job->priv->wrap_mode);
550                         break;
551
552                 case PROP_HIGHLIGHT:
553                         g_value_set_boolean (value, job->priv->highlight);
554                         break;
555                         
556                 case PROP_FONT:
557                         g_value_take_string (value, gtk_source_print_job_get_font (job));
558                         break;
559                         
560                 case PROP_FONT_DESC:
561                         g_value_set_boxed (value, gtk_source_print_job_get_font_desc (job));
562                         break;
563                         
564                 case PROP_NUMBERS_FONT:
565                         g_value_take_string (value, gtk_source_print_job_get_numbers_font (job));
566                         break;
567                         
568                 case PROP_NUMBERS_FONT_DESC:
569                         g_value_set_boxed (value, gtk_source_print_job_get_numbers_font_desc (job));
570                         break;
571                         
572                 case PROP_PRINT_NUMBERS:
573                         g_value_set_uint (value, job->priv->print_numbers);
574                         break;
575                         
576                 case PROP_PRINT_HEADER:
577                         g_value_set_boolean (value, job->priv->print_header);
578                         break;
579                         
580                 case PROP_PRINT_FOOTER:
581                         g_value_set_boolean (value, job->priv->print_footer);
582                         break;
583                         
584                 case PROP_HEADER_FOOTER_FONT:
585                         g_value_take_string (value,
586                                              gtk_source_print_job_get_header_footer_font (job));
587                         break;
588                         
589                 case PROP_HEADER_FOOTER_FONT_DESC:
590                         g_value_set_boxed (value,
591                                            gtk_source_print_job_get_header_footer_font_desc (job));
592                         break;
593                         
594                 default:
595                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
596                         break;
597         }
598 }
599
600 static void 
601 gtk_source_print_job_set_property (GObject      *object,
602                                    guint         prop_id,
603                                    const GValue *value,
604                                    GParamSpec   *pspec)
605 {
606         GtkSourcePrintJob *job = GTK_SOURCE_PRINT_JOB (object);
607
608         switch (prop_id)
609         {
610                 case PROP_CONFIG:
611                         gtk_source_print_job_set_config (job, g_value_get_object (value));
612                         break;
613                         
614                 case PROP_BUFFER:
615                         gtk_source_print_job_set_buffer (job, g_value_get_object (value));
616                         break;
617                         
618                 case PROP_TABS_WIDTH:
619                         gtk_source_print_job_set_tabs_width (job, g_value_get_uint (value));
620                         break;
621                         
622                 case PROP_WRAP_MODE:
623                         gtk_source_print_job_set_wrap_mode (job, g_value_get_enum (value));
624                         break;
625
626                 case PROP_HIGHLIGHT:
627                         gtk_source_print_job_set_highlight (job, g_value_get_boolean (value));
628                         break;
629
630                 case PROP_FONT:
631                         gtk_source_print_job_set_font (job, g_value_get_string (value));
632                         break;
633
634                 case PROP_FONT_DESC:
635                         gtk_source_print_job_set_font_desc (job, g_value_get_boxed (value));
636                         break;
637                         
638                 case PROP_NUMBERS_FONT:
639                         gtk_source_print_job_set_numbers_font (job, g_value_get_string (value));
640                         break;
641                         
642                 case PROP_NUMBERS_FONT_DESC:
643                         gtk_source_print_job_set_numbers_font_desc (job, g_value_get_boxed (value));
644                         break;
645
646                 case PROP_PRINT_NUMBERS:
647                         gtk_source_print_job_set_print_numbers (job, g_value_get_uint (value));
648                         break;
649                         
650                 case PROP_PRINT_HEADER:
651                         gtk_source_print_job_set_print_header (job, g_value_get_boolean (value));
652                         break;
653
654                 case PROP_PRINT_FOOTER:
655                         gtk_source_print_job_set_print_footer (job, g_value_get_boolean (value));
656                         break;
657
658                 case PROP_HEADER_FOOTER_FONT:
659                         gtk_source_print_job_set_header_footer_font (job,
660                                                                      g_value_get_string (value));
661                         break;
662                         
663                 case PROP_HEADER_FOOTER_FONT_DESC:
664                         gtk_source_print_job_set_header_footer_font_desc (job,
665                                                                           g_value_get_boxed (value));
666                         break;
667
668                 default:
669                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
670                         break;
671         }
672 }
673
674 static void 
675 gtk_source_print_job_begin_page (GtkSourcePrintJob *job)
676 {
677         g_return_if_fail (job->priv->printing);
678         
679         if (job->priv->print_header && job->priv->header_height > 0)
680         {
681                 gdouble x, y;
682
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);
686         }
687
688         if (job->priv->print_footer && job->priv->footer_height > 0)
689         {
690                 gdouble x, y;
691
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);
697         }
698 }
699
700 /* ---- gnome-print / Pango convenience functions */
701
702 /* Gets the width of a layout in gnome-print coordinates */
703 static gdouble
704 get_layout_width (PangoLayout *layout)
705 {
706         gint layout_width;
707
708         pango_layout_get_size (layout, &layout_width, NULL);
709         return (gdouble) layout_width / PANGO_SCALE;
710 }
711
712 /* Gets the ascent/descent of a font in gnome-print coordinates */
713 static void
714 get_font_ascent_descent (GtkSourcePrintJob    *job,
715                          PangoFontDescription *desc,
716                          gdouble              *ascent,
717                          gdouble              *descent)
718 {
719         PangoFontMetrics *metrics;
720         
721         metrics = pango_context_get_metrics (job->priv->pango_context,
722                                              desc,
723                                              job->priv->language);
724
725         if (ascent)
726                 *ascent = (gdouble) pango_font_metrics_get_ascent (metrics) / PANGO_SCALE;
727         if (descent)
728                 *descent = (gdouble) pango_font_metrics_get_descent (metrics) / PANGO_SCALE;
729
730         pango_font_metrics_unref (metrics);
731 }
732
733 /* Draws the first line in a layout; we use this for one-line layouts
734  * to get baseline alignment */
735 static void
736 show_first_layout_line (GnomePrintContext *print_ctxt,
737                         PangoLayout       *layout)
738 {
739         PangoLayoutLine *line;
740
741         line = pango_layout_get_lines (layout)->data;
742         gnome_print_pango_layout_line (print_ctxt, line);
743 }
744
745 static PangoLayout *
746 get_line_number_layout (GtkSourcePrintJob *job,
747                         guint              line_number)
748 {
749         PangoLayout *layout;
750         gchar *num_str;
751
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);
756         g_free (num_str);
757
758         return layout;
759 }
760
761 /* ---- Configuration functions */
762
763 static void
764 ensure_print_config (GtkSourcePrintJob *job)
765 {
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);
770 }
771
772 static gboolean
773 update_page_size_and_margins (GtkSourcePrintJob *job)
774 {
775         PangoLayout *layout;
776         gdouble ascent, descent;
777         
778         gnome_print_job_get_page_size_from_config (job->priv->config, 
779                                                    &job->priv->page_width,
780                                                    &job->priv->page_height);
781
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);
790
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);
794         
795         if (job->priv->header_footer_font == NULL)
796                 job->priv->header_footer_font = pango_font_description_copy (job->priv->font);
797         
798         /* calculate numbers width */
799         if (job->priv->print_numbers > 0)
800         {
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);
804         }
805         else
806                 job->priv->numbers_width = 0.0;
807
808         get_font_ascent_descent (job, job->priv->header_footer_font, &ascent, &descent);
809
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);
816         else
817                 job->priv->header_height = 0.0;
818
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);
824         else
825                 job->priv->footer_height = 0.0;
826
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);
833         
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);
838
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);
842
843         return TRUE;
844 }
845
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
848  */
849 static gint
850 calculate_real_tab_width (GtkSourcePrintJob *job, guint tab_size, gchar c)
851 {
852         PangoLayout *layout;
853         gchar *tab_string;
854         gint tab_width = 0;
855
856         if (tab_size == 0)
857                 return -1;
858
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);
862         g_free (tab_string);
863
864         pango_layout_get_size (layout, &tab_width, NULL);
865         g_object_unref (G_OBJECT (layout));
866         
867         return tab_width;
868 }
869
870 static gboolean
871 setup_pango_context (GtkSourcePrintJob *job)
872 {
873         PangoFontMap *font_map;
874         gint real_tab_width;
875
876         if (!job->priv->pango_context)
877         {
878                 font_map = gnome_print_pango_get_default_font_map ();
879                 job->priv->pango_context = gnome_print_pango_create_context (font_map);
880         }
881
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);
884
885         if (job->priv->tab_array)
886         {
887                 pango_tab_array_free (job->priv->tab_array);
888                 job->priv->tab_array = NULL;
889         }
890         
891         real_tab_width = calculate_real_tab_width (job, job->priv->tabs_width, ' ');
892         if (real_tab_width > 0)
893         {
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);
896         }
897         
898         return TRUE;
899 }
900
901 /* ----- Helper functions */
902
903 static gchar * 
904 font_description_to_gnome_font_name (PangoFontDescription *desc)
905 {
906         GnomeFontFace *font_face;
907         gchar *retval;
908
909         /* Will always return some font */
910         font_face = gnome_font_face_find_closest_from_pango_description (desc);
911
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);
916
917         return retval;
918 }
919
920 /*
921  * The following routines are duplicated in gedit/gedit/gedit-prefs-manager.c
922  */
923
924 /* Do this ourselves since gnome_font_find_closest() doesn't call
925  * gnome_font_face_find_closest() (probably a gnome-print bug)
926  */
927 static void
928 face_and_size_from_full_name (const guchar   *name,
929                               GnomeFontFace **face,
930                               gdouble        *size)
931 {
932         char *copy;
933         char *str_size;
934
935         copy = g_strdup (name);
936         str_size = strrchr (copy, ' ');
937         if (str_size)
938         {
939                 *str_size = 0;
940                 str_size ++;
941                 *size = atof (str_size);
942         }
943         else
944         {
945                 *size = 12;
946         }
947
948         *face = gnome_font_face_find_closest (copy);
949         g_free (copy);
950 }
951
952 static PangoFontDescription *
953 font_description_from_gnome_font_name (const char *font_name)
954 {
955         GnomeFontFace *face;
956         PangoFontDescription *desc;
957         PangoStyle style;
958         PangoWeight weight;
959         gdouble size;
960
961         face_and_size_from_full_name (font_name, &face, &size);
962
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;
966
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);
972
973         g_object_unref (face);
974
975         return desc;
976 }
977
978 /* ---- TextStyle functions */
979
980 static TextStyle * 
981 text_style_new (GtkSourcePrintJob *job, GtkTextTag *tag)
982 {
983         TextStyle *style;
984         gboolean bg_set, fg_set;
985         gdouble scale;
986         PangoFontDescription *font_desc;
987         
988         g_return_val_if_fail (tag != NULL && GTK_IS_TEXT_TAG (tag), NULL);
989
990         style = g_new0 (TextStyle, 1);
991
992         g_object_get (G_OBJECT (tag),
993                       "background_set", &bg_set,
994                       "foreground_set", &fg_set,
995                       "font_desc", &style->font_desc,
996                       "scale", &style->scale,
997                       "underline", &style->underline,
998                       "strikethrough", &style->strikethrough,
999                       NULL);
1000
1001         if (fg_set)
1002                 g_object_get (G_OBJECT (tag), "foreground_gdk", &style->foreground, NULL);
1003
1004         if (bg_set)
1005                 g_object_get (G_OBJECT (tag), "background_gdk", &style->background, NULL);
1006         
1007         return style;
1008 }
1009
1010 static void
1011 text_style_free (TextStyle *style)
1012 {
1013         pango_font_description_free (style->font_desc);
1014         if (style->foreground)
1015                 gdk_color_free (style->foreground);
1016         if (style->background)
1017                 gdk_color_free (style->background);
1018         g_free (style);
1019 }
1020
1021 static TextStyle * 
1022 get_style (GtkSourcePrintJob *job, const GtkTextIter *iter)
1023 {
1024         GSList *tags, *t;
1025         GtkTextTag *tag = NULL;
1026         TextStyle *style = NULL;
1027         
1028         if (job->priv->tag_styles == NULL)
1029         {
1030                 job->priv->tag_styles = g_hash_table_new_full (
1031                         g_direct_hash, g_direct_equal,
1032                         NULL, (GDestroyNotify) text_style_free);
1033         }
1034         
1035         /* get the tags at iter */
1036         tags = gtk_text_iter_get_tags (iter);
1037
1038         /* now find the GtkSourceTag (if any) which applies at iter */
1039         /* FIXME: this makes the assumption that the style at a given
1040          * iter is only determined by one GtkSourceTag (the one with
1041          * highest priority).  This is true for now, but could change
1042          * in the future - Gustavo */
1043         t = tags;
1044         while (t != NULL)
1045         {
1046                 if (GTK_IS_TEXT_TAG (t->data))
1047                         tag = t->data;
1048                 t = g_slist_next (t);
1049         }
1050         g_slist_free (tags);
1051
1052         /* now we lookup the tag style in the cache */
1053         if (tag != NULL)
1054         {
1055                 style = g_hash_table_lookup (job->priv->tag_styles, tag);
1056                 if (style == NULL)
1057                 {
1058                         /* create a style for the tag and cache it */
1059                         style = text_style_new (job, tag);
1060                         g_hash_table_insert (job->priv->tag_styles, tag, style);
1061                 }
1062         }
1063
1064         return style;
1065 }
1066
1067 /* ----- Text fetching functions */
1068
1069 static gboolean 
1070 get_text_simple (GtkSourcePrintJob *job,
1071                  GtkTextIter       *start,
1072                  GtkTextIter       *end)
1073 {
1074         GtkTextIter iter;
1075
1076         while (gtk_text_iter_compare (start, end) < 0)
1077         {
1078                 Paragraph *para;
1079                 TextSegment *seg;
1080                 
1081                 /* get a line of text */
1082                 iter = *start;
1083                 if (!gtk_text_iter_ends_line (&iter))
1084                         gtk_text_iter_forward_to_line_end (&iter);
1085                 
1086                 if (gtk_text_iter_compare (&iter, end) > 0)
1087                         iter = *end;
1088
1089                 
1090                 seg = g_new0 (TextSegment, 1);
1091                 seg->next = NULL;  /* only one segment per line, since there's no style change */
1092                 seg->style = NULL; /* use default style */
1093                 /* FIXME: handle invisible text properly.  This also
1094                  * assumes the text has no embedded images and
1095                  * stuff */
1096                 seg->text = gtk_text_iter_get_slice (start, &iter);
1097
1098                 para = g_new0 (Paragraph, 1);
1099                 para->segment = seg;
1100
1101                 /* add the line of text to the job */
1102                 job->priv->paragraphs = g_slist_prepend (job->priv->paragraphs, para);
1103
1104                 gtk_text_iter_forward_line (&iter);
1105                 
1106                 /* advance to next line */
1107                 *start = iter;
1108         }
1109         job->priv->paragraphs = g_slist_reverse (job->priv->paragraphs);
1110
1111         return TRUE;
1112 }
1113
1114 static gboolean 
1115 get_text_with_style (GtkSourcePrintJob *job,
1116                      GtkTextIter       *start,
1117                      GtkTextIter       *end)
1118 {
1119         GtkTextIter limit, next_toggle;
1120         gboolean have_toggle;
1121         
1122         /* make sure the region to print is highlighted */
1123         /*_gtk_source_buffer_highlight_region (job->priv->buffer, start, end, TRUE); */
1124
1125         next_toggle = *start;
1126         have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);
1127         
1128         /* FIXME: handle invisible text properly.  This also assumes
1129          * the text has no embedded images and stuff */
1130         while (gtk_text_iter_compare (start, end) < 0)
1131         {
1132                 TextStyle *style;
1133                 TextSegment *seg;
1134                 Paragraph *para;
1135                 
1136                 para = g_new0 (Paragraph, 1);
1137
1138                 /* get the style at the start of the line */
1139                 style = get_style (job, start);
1140
1141                 /* get a line of text - limit points to the end of the line */
1142                 limit = *start;
1143                 if (!gtk_text_iter_ends_line (&limit))
1144                         gtk_text_iter_forward_to_line_end (&limit);
1145                 
1146                 if (gtk_text_iter_compare (&limit, end) > 0)
1147                         limit = *end;
1148
1149                 /* create the first segment for the line */
1150                 para->segment = seg = g_new0 (TextSegment, 1);
1151                 seg->style = style;
1152
1153                 /* while the next tag toggle is within the line, we check to see
1154                  * if the style has changed at each tag toggle position, and if so,
1155                  * create new segments */
1156                 while (have_toggle && gtk_text_iter_compare (&next_toggle, &limit) < 0)
1157                 {
1158                         /* check style changes */
1159                         style = get_style (job, &next_toggle);
1160                         if (style != seg->style)
1161                         {
1162                                 TextSegment *new_seg;
1163                                 
1164                                 /* style has changed, thus we need to
1165                                  * create a new segment */
1166                                 /* close the current segment */
1167                                 seg->text = gtk_text_iter_get_slice (start, &next_toggle);
1168                                 *start = next_toggle;
1169                                 
1170                                 new_seg = g_new0 (TextSegment, 1);
1171                                 seg->next = new_seg;
1172                                 seg = new_seg;
1173                                 seg->style = style;
1174                         }
1175
1176                         have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);                 
1177                 }
1178                 
1179                 /* close the line */
1180                 seg->next = NULL;
1181                 seg->text = gtk_text_iter_get_slice (start, &limit);
1182
1183                 /* add the line of text to the job */
1184                 job->priv->paragraphs = g_slist_prepend (job->priv->paragraphs, para);
1185
1186                 /* advance to next line */
1187                 *start = limit;
1188                 gtk_text_iter_forward_line (start);
1189
1190                 if (gtk_text_iter_compare (&next_toggle, start) < 0) {
1191                         next_toggle = *start;
1192                         have_toggle = gtk_text_iter_forward_to_tag_toggle (&next_toggle, NULL);
1193                 }
1194         }
1195         job->priv->paragraphs = g_slist_reverse (job->priv->paragraphs);
1196
1197         return TRUE;
1198 }
1199
1200 static gboolean 
1201 get_text_to_print (GtkSourcePrintJob *job,
1202                    const GtkTextIter *start,
1203                    const GtkTextIter *end)
1204 {
1205         GtkTextIter _start, _end;
1206         gboolean retval;
1207         
1208         g_return_val_if_fail (start != NULL && end != NULL, FALSE);
1209         g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
1210
1211         _start = *start;
1212         _end = *end;
1213
1214         /* erase any previous data */
1215         if (job->priv->paragraphs != NULL)
1216         {
1217                 free_paragraphs (job->priv->paragraphs);
1218                 job->priv->paragraphs = NULL;
1219         }
1220         if (job->priv->tag_styles != NULL)
1221         {
1222                 g_hash_table_destroy (job->priv->tag_styles);
1223                 job->priv->tag_styles = NULL;
1224         }
1225
1226         /* provide ordered iters */
1227         gtk_text_iter_order (&_start, &_end);
1228
1229         /* save the first and last line numbers for future reference */
1230         job->priv->first_line_number = gtk_text_iter_get_line (&_start) + 1;
1231         job->priv->last_line_number = gtk_text_iter_get_line (&_end) + 1;
1232
1233         if (!job->priv->highlight)
1234                 retval = get_text_simple (job, &_start, &_end);
1235         else
1236                 retval = get_text_with_style (job, &_start, &_end);
1237
1238         if (retval && job->priv->paragraphs == NULL)
1239         {
1240                 Paragraph *para;
1241                 TextSegment *seg;
1242                 
1243                 /* add an empty line to allow printing empty documents */
1244                 seg = g_new0 (TextSegment, 1);
1245                 seg->next = NULL;
1246                 seg->style = NULL; /* use default style */
1247                 seg->text = g_strdup ("");
1248
1249                 para = g_new0 (Paragraph, 1);
1250                 para->segment = seg;
1251
1252                 job->priv->paragraphs = g_slist_prepend (job->priv->paragraphs, para);
1253         }
1254
1255         return retval;
1256 }
1257
1258 /* ----- Pagination functions */
1259
1260 static void
1261 add_attribute_to_list (PangoAttribute *attr, 
1262                        PangoAttrList  *list,
1263                        guint           index,
1264                        gsize           len)
1265 {
1266         attr->start_index = index;
1267         attr->end_index = index + len;
1268         pango_attr_list_insert (list, attr);
1269 }
1270
1271 static PangoLayout *
1272 create_layout_for_para (GtkSourcePrintJob *job,
1273                         Paragraph         *para)
1274 {
1275         GString *text;
1276         PangoLayout *layout;
1277         PangoAttrList *attrs;
1278         TextSegment *seg;
1279         gint index;
1280
1281         text = g_string_new (NULL);
1282         attrs = pango_attr_list_new ();
1283         
1284         seg = para->segment;
1285         index = 0;
1286
1287         while (seg != NULL)
1288         {
1289                 gsize seg_len = strlen (seg->text);
1290                 g_string_append (text, seg->text);
1291
1292                 if (seg->style)
1293                 {
1294                         PangoAttribute *attr;
1295
1296                         attr = pango_attr_font_desc_new (seg->style->font_desc);
1297                         add_attribute_to_list (attr, attrs, index, seg_len);
1298
1299                         if (seg->style->scale != PANGO_SCALE_MEDIUM) 
1300                         {
1301                                 attr = pango_attr_scale_new (seg->style->scale);
1302                                 add_attribute_to_list (attr, attrs, index, seg_len);
1303                         }
1304
1305                         if (seg->style->foreground)
1306                         {
1307                                 attr = pango_attr_foreground_new (seg->style->foreground->red,
1308                                                                   seg->style->foreground->green,
1309                                                                   seg->style->foreground->blue);
1310                                 add_attribute_to_list (attr, attrs, index, seg_len);
1311                         }
1312
1313                         if (seg->style->background)
1314                         {
1315                                 attr = pango_attr_background_new (seg->style->background->red,
1316                                                                   seg->style->background->green,
1317                                                                   seg->style->background->blue);
1318                                 add_attribute_to_list (attr, attrs, index, seg_len);
1319                         }
1320
1321                         if (seg->style->strikethrough)
1322                         {
1323                                 attr = pango_attr_strikethrough_new (TRUE);
1324                                 add_attribute_to_list (attr, attrs, index, seg_len);
1325                         }
1326
1327                         if (seg->style->underline != PANGO_UNDERLINE_NONE)
1328                         {
1329                                 attr = pango_attr_underline_new (seg->style->underline);
1330                                 add_attribute_to_list (attr, attrs, index, seg_len);
1331                         }
1332                 }
1333
1334                 index += seg_len;
1335                 seg = seg->next;
1336         }
1337
1338         layout = pango_layout_new (job->priv->pango_context);
1339         
1340 /*      if (job->priv->wrap_mode != GTK_WRAP_NONE)*/
1341                 pango_layout_set_width (layout, job->priv->text_width * PANGO_SCALE);
1342         
1343         switch (job->priv->wrap_mode)   {
1344         case GTK_WRAP_CHAR:
1345                 pango_layout_set_wrap (layout, PANGO_WRAP_CHAR);
1346                 break;
1347         case GTK_WRAP_WORD:
1348                 pango_layout_set_wrap (layout, PANGO_WRAP_WORD);
1349                 break;
1350         case GTK_WRAP_WORD_CHAR:
1351                 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
1352                 break;
1353         case GTK_WRAP_NONE:
1354                 /* FIXME: hack 
1355                  * Ellipsize the paragraph when text wrapping is disabled.
1356                  * Another possibility would be to set the width so the text 
1357                  * breaks into multiple lines, and paginate/render just the 
1358                  * first one.
1359                  * See also Comment #23 by Owen on bug #143874.
1360                  */
1361
1362                 /* orph says to comment this out and commit it.
1363                    PANGO_ELLIPSIZE_END is not available in pango
1364                    1.4.1, at least, and he says this code is never
1365                    used. */
1366                 /*pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);*/
1367
1368                 break;
1369         }
1370
1371         if (job->priv->tab_array)
1372                 pango_layout_set_tabs (layout, job->priv->tab_array);
1373         
1374         pango_layout_set_text (layout, text->str, text->len);
1375         pango_layout_set_attributes (layout, attrs);
1376
1377         /* FIXME: <horrible-hack> 
1378          * For empty paragraphs, pango_layout_iter_get_baseline() returns 0,
1379          * so I check this condition and add a space character to force 
1380          * the calculation of the baseline. I don't like that, but I
1381          * didn't find a better way to do it. Note that a paragraph is 
1382          * considered empty either when it has no characters, or when 
1383          * it only has tabs.
1384          * See comment #22 and #23 on bug #143874.
1385          */
1386         if (job->priv->print_numbers > 0)
1387         {
1388                 PangoLayoutIter *iter;
1389                 iter = pango_layout_get_iter (layout);
1390                 if (pango_layout_iter_get_baseline (iter) == 0)
1391                 {
1392                         g_string_append_c (text, ' ');
1393                         pango_layout_set_text (layout, text->str, text->len);
1394                 }
1395                 pango_layout_iter_free (iter);
1396         }
1397         /* FIXME: </horrible-hack> */
1398         
1399         g_string_free (text, TRUE);
1400         pango_attr_list_unref (attrs);
1401
1402         return layout;
1403 }
1404
1405 /* The break logic in this function needs to match that in print_paragraph */
1406 static void
1407 paginate_paragraph (GtkSourcePrintJob *job,
1408                     Paragraph         *para)
1409 {
1410         PangoLayout *layout;
1411         PangoLayoutIter *iter;
1412         PangoRectangle logical_rect;
1413         gdouble max;
1414         gdouble page_skip;
1415
1416         layout = create_layout_for_para (job, para);
1417
1418         iter = pango_layout_get_iter (layout);
1419
1420         max = 0;
1421         page_skip = 0;
1422
1423         do
1424         {
1425                 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1426                 max = (gdouble) (logical_rect.y + logical_rect.height) / PANGO_SCALE;
1427                 
1428                 if (max - page_skip > job->priv->available_height)
1429                 {
1430                         /* "create" a new page */
1431                         job->priv->page_count++;
1432                         job->priv->available_height = job->priv->text_height;
1433                         page_skip = (gdouble) logical_rect.y / PANGO_SCALE;
1434                 }
1435
1436         }
1437         while (pango_layout_iter_next_line (iter));
1438
1439         job->priv->available_height -= max - page_skip;
1440         
1441         pango_layout_iter_free (iter);
1442         g_object_unref (layout);
1443 }
1444
1445 static gboolean 
1446 paginate_text (GtkSourcePrintJob *job)
1447 {
1448         GSList *l;
1449         guint line_number;
1450         
1451         /* set these to zero so the first break_line creates a new page */
1452         job->priv->page_count = 0;
1453         job->priv->available_height = 0;
1454         line_number = job->priv->first_line_number;
1455         l = job->priv->paragraphs;
1456         while (l != NULL)
1457         {
1458                 Paragraph *para = l->data;
1459
1460                 para->line_number = line_number;
1461                 paginate_paragraph (job, para);
1462                 
1463                 line_number++;
1464                 l = g_slist_next (l);
1465         }
1466
1467         /* FIXME: do we have any error condition which can force us to
1468          * return %FALSE? - Gustavo */
1469         return TRUE;
1470 }
1471
1472 /* ---- Printing functions */
1473
1474 static void
1475 begin_page (GtkSourcePrintJob *job)
1476 {
1477         gnome_print_beginpage (job->priv->print_ctxt, NULL);
1478
1479         g_signal_emit (job, print_job_signals [BEGIN_PAGE], 0);
1480 }
1481
1482 static void
1483 end_page (GtkSourcePrintJob *job)
1484 {
1485         gnome_print_showpage (job->priv->print_ctxt);
1486 }
1487
1488 static void 
1489 print_line_number (GtkSourcePrintJob *job,
1490                    guint              line_number,
1491                    gdouble            x,
1492                    gdouble            y)
1493 {
1494         PangoLayout *layout;
1495
1496         layout = get_line_number_layout (job, line_number);
1497
1498         x = x + job->priv->numbers_width - get_layout_width (layout) - NUMBERS_TEXT_SEPARATION;
1499         gnome_print_moveto (job->priv->print_ctxt, x, y);
1500         
1501         show_first_layout_line (job->priv->print_ctxt, layout);
1502         
1503         g_object_unref (layout);
1504 }       
1505
1506 /* The break logic in this function needs to match that in paginate_paragraph
1507  *
1508  * @start_line is the first line in the paragraph to print
1509  * @y is updated to the position after the portion of the paragraph we printed
1510  * @baseline_out is set to the baseline of the first line of the paragraph
1511  *   if we printed it. (And not set otherwise)
1512  * 
1513  * Returns the first unprinted line in the paragraph (unprinted because it
1514  * flowed onto the next page) or -1 if the entire paragraph was printed.
1515  */
1516 static gint
1517 print_paragraph (GtkSourcePrintJob *job,
1518                  Paragraph         *para,
1519                  gint               start_line,
1520                  gdouble            x,
1521                  gdouble           *y,
1522                  gdouble           *baseline_out,
1523                  gboolean           force_fit)
1524 {
1525         PangoLayout *layout;
1526         PangoLayoutIter *iter;
1527         PangoRectangle logical_rect;
1528         int current_line;
1529         gdouble max;
1530         gdouble page_skip;
1531         gdouble baseline;
1532         int result = -1;
1533         
1534         layout = create_layout_for_para (job, para);
1535
1536         iter = pango_layout_get_iter (layout);
1537
1538         /* Skip over lines already printed on previous page(s) */
1539         for (current_line = 0; current_line < start_line; current_line++)
1540                 pango_layout_iter_next_line (iter);
1541
1542         max = 0;
1543         page_skip = 0;
1544         
1545         do
1546         {
1547                 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1548                 max = (gdouble) (logical_rect.y + logical_rect.height) / PANGO_SCALE;
1549                 
1550                 if (current_line == start_line)
1551                         page_skip = (gdouble) logical_rect.y / PANGO_SCALE;
1552
1553                 if (max - page_skip > job->priv->available_height)
1554                 {
1555                         result = current_line; /* Save position for next page */
1556                         break;
1557                 }
1558
1559                 baseline = (gdouble) pango_layout_iter_get_baseline (iter) / PANGO_SCALE;
1560                 baseline = *y + page_skip - baseline; /* Adjust to global coordinates */
1561                 if (current_line == 0)
1562                         *baseline_out = baseline;
1563                 
1564                 gnome_print_moveto (job->priv->print_ctxt,
1565                                     x + (gdouble) logical_rect.x / PANGO_SCALE,
1566                                     baseline);
1567                 gnome_print_pango_layout_line (job->priv->print_ctxt,
1568                                                pango_layout_iter_get_line (iter));
1569
1570                 current_line++;
1571         }
1572         while (pango_layout_iter_next_line (iter));
1573
1574         job->priv->available_height -= max - page_skip;
1575         *y -= max - page_skip;
1576
1577         pango_layout_iter_free (iter);
1578         g_object_unref (layout);
1579         
1580         return result;
1581 }
1582
1583 static void
1584 print_page (GtkSourcePrintJob *job)
1585 {
1586         GSList *l;
1587         gdouble x, y;
1588         gint line;
1589         gboolean force_fit = TRUE;
1590         
1591         job->priv->page++;
1592         
1593         
1594         begin_page (job);
1595         job->priv->available_height = job->priv->text_height;
1596
1597         y = job->priv->page_height -
1598                 job->priv->doc_margin_top - job->priv->margin_top -
1599                 job->priv->header_height;
1600         x = job->priv->doc_margin_left + job->priv->margin_left +
1601                 job->priv->numbers_width;
1602         l = job->priv->current_paragraph;
1603         line = job->priv->current_paragraph_line;
1604
1605         while (l != NULL)
1606         {
1607                 Paragraph *para = l->data;
1608                 gdouble baseline;
1609                 gint last_line = line;
1610                 
1611                 line = print_paragraph (job, para, line, x, &y, &baseline, force_fit);
1612
1613                 if (last_line == 0 && line != 0)
1614                 {
1615                         /* We printed the first line of a paragraph */
1616                         if (job->priv->print_numbers > 0 &&
1617                             ((para->line_number % job->priv->print_numbers) == 0))
1618                                 print_line_number (job,
1619                                                    para->line_number,
1620                                                    job->priv->doc_margin_left +
1621                                                    job->priv->margin_left,
1622                                                    baseline);
1623
1624                         job->priv->printed_lines++;
1625                 }
1626
1627                 if (line >= 0)
1628                         break;  /* Didn't all fit on this page */
1629                 
1630                 l = l->next;
1631                 line = 0;
1632                 force_fit = FALSE;
1633         }
1634         end_page (job);
1635         job->priv->current_paragraph = l;
1636         job->priv->current_paragraph_line = line;
1637 }
1638
1639 static void
1640 setup_for_print (GtkSourcePrintJob *job)
1641 {
1642         job->priv->current_paragraph = job->priv->paragraphs;
1643         job->priv->page = 0;
1644         job->priv->printed_lines = 0;
1645
1646         if (job->priv->print_job != NULL)
1647                 g_object_unref (job->priv->print_job);
1648         if (job->priv->print_ctxt != NULL)
1649                 g_object_unref (job->priv->print_ctxt);
1650         
1651         job->priv->print_job = gnome_print_job_new (job->priv->config);
1652         job->priv->print_ctxt = gnome_print_job_get_context (job->priv->print_job);
1653
1654         gnome_print_pango_update_context (job->priv->pango_context, job->priv->print_ctxt);
1655 }
1656
1657 static void
1658 print_job (GtkSourcePrintJob *job)
1659 {
1660         while (job->priv->current_paragraph != NULL)
1661                 print_page (job);
1662
1663         gnome_print_job_close (job->priv->print_job);
1664 }
1665
1666 static gboolean
1667 idle_printing_handler (GtkSourcePrintJob *job)
1668 {
1669         g_assert (job->priv->current_paragraph != NULL);
1670
1671         print_page (job);
1672
1673         if (job->priv->current_paragraph == NULL)
1674         {
1675                 gnome_print_job_close (job->priv->print_job);
1676                 job->priv->printing = FALSE;
1677                 job->priv->idle_printing_tag = 0;
1678
1679                 g_signal_emit (job, print_job_signals [FINISHED], 0);
1680                 /* after this the print job object is possibly
1681                  * destroyed (common use case) */
1682                 
1683                 return FALSE;
1684         }
1685         return TRUE;
1686 }
1687
1688
1689 /* Public API ------------------- */
1690
1691 /**
1692  * gtk_source_print_job_new:
1693  * @config: an optional #GnomePrintConfig object.
1694  * 
1695  * Creates a new print job object, initially setting the print configuration.
1696  * 
1697  * Return value: the new print job object.
1698  **/
1699 GtkSourcePrintJob *
1700 gtk_source_print_job_new (GnomePrintConfig  *config)
1701 {
1702         GtkSourcePrintJob *job;
1703
1704         g_return_val_if_fail (config == NULL || GNOME_IS_PRINT_CONFIG (config), NULL);
1705
1706         job = GTK_SOURCE_PRINT_JOB (g_object_new (GTK_TYPE_SOURCE_PRINT_JOB, NULL));
1707         if (config != NULL)
1708                 gtk_source_print_job_set_config (job, config);
1709
1710         return job;
1711 }
1712
1713 /**
1714  * gtk_source_print_job_new_with_buffer:
1715  * @config: an optional #GnomePrintConfig.
1716  * @buffer: the #GtkTextBuffer to print (might be %NULL).
1717  * 
1718  * Creates a new print job to print @buffer.
1719  * 
1720  * Return value: a new print job object.
1721  **/
1722 GtkSourcePrintJob *
1723 gtk_source_print_job_new_with_buffer (GnomePrintConfig  *config,
1724                                       GtkTextBuffer   *buffer)
1725 {
1726         GtkSourcePrintJob *job;
1727
1728         g_return_val_if_fail (config == NULL || GNOME_IS_PRINT_CONFIG (config), NULL);
1729         g_return_val_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer), NULL);
1730
1731         job = gtk_source_print_job_new (config);
1732         if (buffer != NULL)
1733                 gtk_source_print_job_set_buffer (job, buffer);
1734
1735         return job;
1736 }
1737
1738 /* --- print job basic configuration */
1739
1740 /**
1741  * gtk_source_print_job_set_config:
1742  * @job: a #GtkSourcePrintJob.
1743  * @config: a #GnomePrintConfig object to get printing configuration from.
1744  * 
1745  * Sets the print configuration for the job.  If you don't set a
1746  * configuration object for the print job, when needed one will be
1747  * created with gnome_print_config_default().
1748  **/
1749 void
1750 gtk_source_print_job_set_config (GtkSourcePrintJob *job,
1751                                  GnomePrintConfig  *config)
1752 {
1753         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1754         g_return_if_fail (GNOME_IS_PRINT_CONFIG (config));
1755         g_return_if_fail (!job->priv->printing);
1756         
1757         if (config == job->priv->config)
1758                 return;
1759         
1760         if (job->priv->config != NULL)
1761                 gnome_print_config_unref (job->priv->config);
1762
1763         job->priv->config = config;
1764         gnome_print_config_ref (config);
1765
1766         g_object_notify (G_OBJECT (job), "config");
1767 }
1768
1769 /**
1770  * gtk_source_print_job_get_config:
1771  * @job: a #GtkSourcePrintJob.
1772  * 
1773  * Gets the current #GnomePrintConfig the print job will use.  If not
1774  * previously set, this will create a default configuration and return
1775  * it.  The returned object reference is owned by the print job.
1776  * 
1777  * Return value: the #GnomePrintConfig for the print job.
1778  **/
1779 GnomePrintConfig * 
1780 gtk_source_print_job_get_config (GtkSourcePrintJob *job)
1781 {
1782         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
1783
1784         ensure_print_config (job);
1785         
1786         return job->priv->config;
1787 }
1788
1789 /**
1790  * gtk_source_print_job_set_buffer:
1791  * @job: a #GtkSourcePrintJob.
1792  * @buffer: a #GtkTextBuffer.
1793  * 
1794  * Sets the #GtkTextBuffer the print job will print.  You need to
1795  * specify a buffer to print, either by the use of this function or by
1796  * creating the print job with gtk_source_print_job_new_with_buffer().
1797  **/
1798 void 
1799 gtk_source_print_job_set_buffer (GtkSourcePrintJob *job,
1800                                  GtkTextBuffer   *buffer)
1801 {
1802         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1803         g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1804         g_return_if_fail (!job->priv->printing);
1805
1806         if (buffer == job->priv->buffer)
1807                 return;
1808         
1809         if (job->priv->buffer != NULL)
1810                 g_object_unref (job->priv->buffer);
1811
1812         job->priv->buffer = buffer;
1813         g_object_ref (buffer);
1814
1815         g_object_notify (G_OBJECT (job), "buffer");
1816 }
1817
1818 /**
1819  * gtk_source_print_job_get_buffer:
1820  * @job: a #GtkSourcePrintJob.
1821  * 
1822  * Gets the #GtkTextBuffer the print job would print.  The returned
1823  * object reference (if non %NULL) is owned by the job object and
1824  * should not be unreferenced.
1825  * 
1826  * Return value: the #GtkTextBuffer to print.
1827  **/
1828 GtkTextBuffer *
1829 gtk_source_print_job_get_buffer (GtkSourcePrintJob *job)
1830 {
1831         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
1832
1833         return job->priv->buffer;
1834 }
1835
1836 /* --- print job layout and style configuration */
1837
1838 /**
1839  * gtk_source_print_job_set_tabs_width:
1840  * @job: a #GtkSourcePrintJob.
1841  * @tabs_width: the number of equivalent spaces for a tabulation.
1842  * 
1843  * Sets the width (in equivalent spaces) of tabulations for the
1844  * printed text.  The width in printing units will be calculated as
1845  * the width of a string containing @tabs_width spaces of the default
1846  * font.  Tabulation stops are set for the full width of printed text.
1847  **/
1848 void 
1849 gtk_source_print_job_set_tabs_width (GtkSourcePrintJob *job,
1850                                      guint              tabs_width)
1851 {
1852         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1853         g_return_if_fail (!job->priv->printing);
1854
1855         if (tabs_width == job->priv->tabs_width)
1856                 return;
1857         
1858         job->priv->tabs_width = tabs_width;
1859
1860         g_object_notify (G_OBJECT (job), "tabs_width");
1861 }
1862
1863 /**
1864  * gtk_source_print_job_get_tabs_width:
1865  * @job: a #GtkSourcePrintJob.
1866  * 
1867  * Determines the configured width (in equivalent spaces) of
1868  * tabulations.  The default value is 8.
1869  * 
1870  * Return value: the width (in equivalent spaces) of a tabulation.
1871  **/
1872 guint 
1873 gtk_source_print_job_get_tabs_width (GtkSourcePrintJob *job)
1874 {
1875         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
1876
1877         return job->priv->tabs_width;
1878 }
1879
1880 /**
1881  * gtk_source_print_job_set_wrap_mode:
1882  * @job: a #GtkSourcePrintJob.
1883  * @wrap: the wrap mode.
1884  * 
1885  * Sets the wrap mode for lines of text larger than the printable
1886  * width.  See #GtkWrapMode for a definition of the possible values.
1887  **/
1888 void 
1889 gtk_source_print_job_set_wrap_mode (GtkSourcePrintJob *job,
1890                                     GtkWrapMode        wrap)
1891 {
1892         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1893         g_return_if_fail (!job->priv->printing);
1894
1895         if (wrap == job->priv->wrap_mode)
1896                 return;
1897         
1898         job->priv->wrap_mode = wrap;
1899
1900         g_object_notify (G_OBJECT (job), "wrap_mode");
1901 }
1902
1903 /**
1904  * gtk_source_print_job_get_wrap_mode:
1905  * @job: a #GtkSourcePrintJob.
1906  * 
1907  * Determines the wrapping style for text lines wider than the
1908  * printable width.  The default is no wrapping.
1909  * 
1910  * Return value: the current wrapping mode for the print job.
1911  **/
1912 GtkWrapMode 
1913 gtk_source_print_job_get_wrap_mode (GtkSourcePrintJob *job)
1914 {
1915         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), GTK_WRAP_NONE);
1916
1917         return job->priv->wrap_mode;
1918 }
1919
1920 /**
1921  * gtk_source_print_job_set_highlight:
1922  * @job: a #GtkSourcePrintJob.
1923  * @highlight: %TRUE if the printed text should be highlighted.
1924  * 
1925  * Sets whether the printed text will be highlighted according to the
1926  * buffer rules.  Both color and font style are applied.
1927  **/
1928 void 
1929 gtk_source_print_job_set_highlight (GtkSourcePrintJob *job,
1930                                     gboolean           highlight)
1931 {
1932         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1933         g_return_if_fail (!job->priv->printing);
1934
1935         highlight = (highlight != FALSE);
1936         
1937         if (highlight == job->priv->highlight)
1938                 return;
1939         
1940         job->priv->highlight = highlight;
1941
1942         g_object_notify (G_OBJECT (job), "highlight");
1943 }
1944
1945 /**
1946  * gtk_source_print_job_get_highlight:
1947  * @job: a #GtkSourcePrintJob.
1948  * 
1949  * Determines if the job is configured to print the text highlighted
1950  * with colors and font styles.  Note that highlighting will happen
1951  * only if the buffer to print has highlighting activated.
1952  * 
1953  * Return value: %TRUE if the printed output will be highlighted.
1954  **/
1955 gboolean 
1956 gtk_source_print_job_get_highlight (GtkSourcePrintJob *job)
1957 {
1958         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
1959
1960         return job->priv->highlight;
1961 }
1962
1963 /**
1964  * gtk_source_print_job_set_font_desc:
1965  * @job: a #GtkSourcePrintJob.
1966  * @desc: the #PangoFontDescription for the default font
1967  * 
1968  * Sets the default font for the printed text.
1969  **/
1970 void 
1971 gtk_source_print_job_set_font_desc (GtkSourcePrintJob    *job,
1972                                     PangoFontDescription *desc)
1973 {
1974         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
1975         g_return_if_fail (desc != NULL);
1976         g_return_if_fail (!job->priv->printing);
1977
1978         desc = pango_font_description_copy (desc);
1979         if (job->priv->font != NULL)
1980                 pango_font_description_free (job->priv->font);
1981         job->priv->font = desc;
1982         g_object_freeze_notify (G_OBJECT (job));
1983         g_object_notify (G_OBJECT (job), "font");
1984         g_object_notify (G_OBJECT (job), "font_desc");
1985         g_object_thaw_notify (G_OBJECT (job));
1986 }
1987
1988 /**
1989  * gtk_source_print_job_set_font:
1990  * @job: a #GtkSourcePrintJob.
1991  * @font_name: the name of the default font.
1992  * 
1993  * Sets the default font for the printed text.  @font_name should be a
1994  * <emphasis>full font name</emphasis> GnomePrint can understand
1995  * (e.g. &quot;Monospace Regular 10.0&quot;).
1996  *
1997  * Note that @font_name is a #GnomeFont name not a Pango font
1998  * description string. This function is deprecated since #GnomeFont is
1999  * no longer used when implementing printing for GtkSourceView; you
2000  * should use gtk_source_print_job_set_font_desc() instead.
2001  **/
2002 void 
2003 gtk_source_print_job_set_font (GtkSourcePrintJob *job,
2004                                const gchar       *font_name)
2005 {
2006         PangoFontDescription *desc;
2007         
2008         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2009         g_return_if_fail (font_name != NULL);
2010         g_return_if_fail (!job->priv->printing);
2011
2012         desc = font_description_from_gnome_font_name (font_name);
2013         if (desc)
2014         {
2015                 gtk_source_print_job_set_font_desc (job, desc);
2016                 pango_font_description_free (desc);
2017         }
2018 }
2019
2020 /**
2021  * gtk_source_print_job_get_font_desc:
2022  * @job: a #GtkSourcePrintJob.
2023  * 
2024  * Determines the default font to be used for the printed text.  The
2025  * returned string is of the form &quot;Fontfamily Style Size&quot;,
2026  * for example &quot;Monospace Regular 10.0&quot;.  The returned value
2027  * should be freed when no longer needed.
2028  * 
2029  * Return value: the current text font description. This value is
2030  *  owned by the job and must not be modified or freed.
2031  **/
2032 PangoFontDescription *
2033 gtk_source_print_job_get_font_desc (GtkSourcePrintJob *job)
2034 {
2035         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2036
2037         ensure_print_config (job);
2038         
2039         return job->priv->font;
2040 }
2041
2042 /**
2043  * gtk_source_print_job_get_font:
2044  * @job: a #GtkSourcePrintJob.
2045  * 
2046  * Determines the default font to be used for the printed text.  The
2047  * returned string is of the form &quot;Fontfamily Style Size&quot;,
2048  * for example &quot;Monospace Regular 10.0&quot;.  The returned value
2049  * should be freed when no longer needed.
2050  * 
2051  * Note that the result is a #GnomeFont name not a Pango font
2052  * description string. This function is deprecated since #GnomeFont is
2053  * no longer used when implementing printing for GtkSourceView; you
2054  * should use gtk_source_print_job_get_font_desc() instead.
2055  *
2056  * Return value: a newly allocated string with the name of the current
2057  * text font.
2058  **/
2059 gchar *
2060 gtk_source_print_job_get_font (GtkSourcePrintJob *job)
2061 {
2062         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2063
2064         ensure_print_config (job);
2065         
2066         return font_description_to_gnome_font_name (job->priv->font);
2067 }
2068
2069 /**
2070  * gtk_source_print_job_setup_from_view:
2071  * @job: a #GtkSourcePrintJob.
2072  * @view: a #GtkSourceView to get configuration from.
2073  * 
2074  * Convenience function to set several configuration options at once,
2075  * so that the printed output matches @view.  The options set are
2076  * buffer (if not set already), tabs width, highlighting, wrap mode
2077  * and default font.
2078  **/
2079 void 
2080 gtk_source_print_job_setup_from_view (GtkSourcePrintJob *job,
2081                                       GtkTextView       *view)
2082 {
2083         GtkTextBuffer *buffer = NULL;
2084         PangoContext *pango_context;
2085         
2086         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2087         g_return_if_fail (!job->priv->printing);
2088
2089         buffer = gtk_text_view_get_buffer (view);
2090         
2091         if (job->priv->buffer == NULL && buffer != NULL)
2092                 gtk_source_print_job_set_buffer (job, buffer);
2093
2094         /* gtk_source_print_job_set_tabs_width (job, gtk_source_view_get_tabs_width (view)); */
2095         gtk_source_print_job_set_highlight (job, TRUE);
2096         gtk_source_print_job_set_wrap_mode (job, gtk_text_view_get_wrap_mode (view));
2097
2098         pango_context = gtk_widget_get_pango_context (GTK_WIDGET (view));
2099         gtk_source_print_job_set_font_desc (job, 
2100                                             pango_context_get_font_description (pango_context));
2101 }
2102
2103 /**
2104  * gtk_source_print_job_set_numbers_font_desc:
2105  * @job: a #GtkSourcePrintJob.
2106  * @desc: the #PangoFontDescription for the font for line numbers, or %NULL
2107  * 
2108  * Sets the font for printing line numbers on the left margin.  If
2109  * NULL is supplied, the default font (i.e. the one being used for the
2110  * text) will be used instead.
2111  **/
2112 void 
2113 gtk_source_print_job_set_numbers_font_desc (GtkSourcePrintJob    *job,
2114                                             PangoFontDescription *desc)
2115 {
2116         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2117         g_return_if_fail (!job->priv->printing);
2118         
2119         if (desc)
2120                 desc = pango_font_description_copy (desc);
2121         if (job->priv->numbers_font != NULL)
2122                 pango_font_description_free (job->priv->numbers_font);
2123         job->priv->numbers_font = desc;
2124         g_object_freeze_notify (G_OBJECT (job));
2125         g_object_notify (G_OBJECT (job), "numbers_font");
2126         g_object_notify (G_OBJECT (job), "numbers_font_desc");
2127         g_object_thaw_notify (G_OBJECT (job));
2128 }
2129
2130 /**
2131  * gtk_source_print_job_set_numbers_font:
2132  * @job: a #GtkSourcePrintJob.
2133  * @font_name: the full name of the font for line numbers, or %NULL.
2134  * 
2135  * Sets the font for printing line numbers on the left margin.  If
2136  * %NULL is supplied, the default font (i.e. the one being used for the
2137  * text) will be used instead.
2138  *
2139  * Note that @font_name is a #GnomeFont name not a Pango font
2140  * description string. This function is deprecated since #GnomeFont is
2141  * no longer used when implementing printing for GtkSourceView; you
2142  * should use gtk_source_print_job_set_numbers_font_desc() instead.
2143  **/
2144 void 
2145 gtk_source_print_job_set_numbers_font (GtkSourcePrintJob *job,
2146                                        const gchar       *font_name)
2147 {
2148         PangoFontDescription *desc;
2149         
2150         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2151         g_return_if_fail (!job->priv->printing);
2152
2153         if (font_name != NULL)
2154         {
2155                 desc = font_description_from_gnome_font_name (font_name);
2156                 if (desc)
2157                 {
2158                         gtk_source_print_job_set_numbers_font_desc (job, desc);
2159                         pango_font_description_free (desc);
2160                 }
2161         }
2162         else
2163                 gtk_source_print_job_set_numbers_font (job, NULL);
2164 }
2165
2166 /**
2167  * gtk_source_print_job_get_numbers_font_desc:
2168  * @job: a #GtkSourcePrintJob.
2169  * 
2170  * Determines the font to be used for the line numbers. This function
2171  * might return %NULL if a specific font for numbers has not been set.
2172  * 
2173  * Return value: the line numbers font description or %NULL. This value is
2174  * owned by the job and must not be modified or freed.
2175  **/
2176 PangoFontDescription *
2177 gtk_source_print_job_get_numbers_font_desc (GtkSourcePrintJob *job)
2178 {
2179         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2180
2181         return job->priv->numbers_font;
2182 }
2183
2184 /**
2185  * gtk_source_print_job_get_numbers_font:
2186  * @job: a #GtkSourcePrintJob.
2187  * 
2188  * Determines the font to be used for the line numbers.  The returned
2189  * string is of the form &quot;Fontfamily Style Size&quot;, for
2190  * example &quot;Monospace Regular 10.0&quot;.  The returned value
2191  * should be freed when no longer needed.  This function might return
2192  * %NULL if a specific font for numbers has not been set.
2193  * 
2194  * Note that the result is a #GnomeFont name not a Pango font
2195  * description string. This function is deprecated since #GnomeFont is
2196  * no longer used when implementing printing for GtkSourceView; you
2197  * should use gtk_source_print_job_get_numbers_font_desc() instead.
2198  *
2199  * Return value: a newly allocated string with the name of the current
2200  * line numbers font, or %NULL.
2201  **/
2202 gchar *
2203 gtk_source_print_job_get_numbers_font (GtkSourcePrintJob *job)
2204 {
2205         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2206
2207         if (job->priv->numbers_font != NULL)
2208                 return font_description_to_gnome_font_name (job->priv->numbers_font);
2209         else
2210                 return NULL;
2211 }
2212
2213 /**
2214  * gtk_source_print_job_set_print_numbers:
2215  * @job: a #GtkSourcePrintJob.
2216  * @interval: interval for printed line numbers.
2217  * 
2218  * Sets the interval for printed line numbers.  If @interval is 0 no
2219  * numbers will be printed.  If greater than 0, a number will be
2220  * printed every @interval lines (i.e. 1 will print all line numbers).
2221  **/
2222 void 
2223 gtk_source_print_job_set_print_numbers (GtkSourcePrintJob *job,
2224                                         guint              interval)
2225 {
2226         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2227         g_return_if_fail (!job->priv->printing);
2228
2229         if (interval == job->priv->print_numbers)
2230                 return;
2231         
2232         job->priv->print_numbers = interval;
2233
2234         g_object_notify (G_OBJECT (job), "print_numbers");
2235 }
2236
2237 /**
2238  * gtk_source_print_job_get_print_numbers:
2239  * @job: a #GtkSourcePrintJob.
2240  * 
2241  * Determines the interval used for line number printing.  If the
2242  * value is 0, no line numbers will be printed.  The default value is
2243  * 1 (i.e. numbers printed in all lines).
2244  * 
2245  * Return value: the interval of printed line numbers.
2246  **/
2247 guint 
2248 gtk_source_print_job_get_print_numbers (GtkSourcePrintJob *job)
2249 {
2250         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2251
2252         return job->priv->print_numbers;
2253 }
2254
2255 /**
2256  * gtk_source_print_job_set_text_margins:
2257  * @job: a #GtkSourcePrintJob.
2258  * @top: the top user margin.
2259  * @bottom: the bottom user margin.
2260  * @left: the left user margin.
2261  * @right: the right user margin.
2262  * 
2263  * Sets the four user margins for the print job.  These margins are in
2264  * addition to the document margins provided in the #GnomePrintConfig
2265  * and will not be used for headers, footers or line numbers (those
2266  * are calculated separatedly).  You can print in the space allocated
2267  * by these margins by connecting to the <link
2268  * linkend="GtkSourcePrintJob-begin-page">&quot;begin_page&quot;</link> signal.  The
2269  * space is around the printed text, and inside the margins specified
2270  * in the #GnomePrintConfig.
2271  *
2272  * The margin numbers are given in device units.  If any of the given
2273  * values is less than 0, that particular margin is not altered by
2274  * this function.
2275  **/
2276 void 
2277 gtk_source_print_job_set_text_margins (GtkSourcePrintJob *job,
2278                                        gdouble            top,
2279                                        gdouble            bottom,
2280                                        gdouble            left,
2281                                        gdouble            right)
2282 {
2283         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2284         g_return_if_fail (!job->priv->printing);
2285
2286         if (top >= 0)
2287                 job->priv->margin_top = top;
2288         if (bottom >= 0)
2289                 job->priv->margin_bottom = bottom;
2290         if (left >= 0)
2291                 job->priv->margin_left = left;
2292         if (right >= 0)
2293                 job->priv->margin_right = right;
2294 }
2295
2296 /**
2297  * gtk_source_print_job_get_text_margins:
2298  * @job: a #GtkSourcePrintJob.
2299  * @top: a pointer to a #gdouble to return the top margin.
2300  * @bottom: a pointer to a #gdouble to return the bottom margin.
2301  * @left: a pointer to a #gdouble to return the left margin.
2302  * @right: a pointer to a #gdouble to return the right margin.
2303  * 
2304  * Determines the user set margins for the job.  This function
2305  * retrieves the values previously set by
2306  * gtk_source_print_job_set_text_margins().  The default for all four
2307  * margins is 0.  Any of the pointers can be %NULL if you want to
2308  * ignore that value.
2309  **/
2310 void 
2311 gtk_source_print_job_get_text_margins (GtkSourcePrintJob *job,
2312                                        gdouble           *top,
2313                                        gdouble           *bottom,
2314                                        gdouble           *left,
2315                                        gdouble           *right)
2316 {
2317         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2318
2319         if (top != NULL)
2320                 *top = job->priv->margin_top;
2321         if (bottom != NULL)
2322                 *bottom = job->priv->margin_bottom;
2323         if (left != NULL)
2324                 *left = job->priv->margin_left;
2325         if (right != NULL)
2326                 *right = job->priv->margin_right;
2327 }
2328
2329 /* --- printing operations */
2330
2331 static gboolean
2332 gtk_source_print_job_prepare (GtkSourcePrintJob *job,
2333                               const GtkTextIter *start,
2334                               const GtkTextIter *end)
2335 {
2336         PROFILE (GTimer *timer);
2337         
2338         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2339         g_return_val_if_fail (!job->priv->printing, FALSE);
2340         g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
2341         g_return_val_if_fail (start != NULL && end != NULL, FALSE);
2342
2343         /* make sure we have a sane configuration to start printing */
2344         ensure_print_config (job);
2345
2346         PROFILE (timer = g_timer_new ());
2347
2348         /* get the text to print */
2349         if (!get_text_to_print (job, start, end))
2350                 return FALSE;
2351
2352         PROFILE (g_message ("get_text_to_print: %.2f", g_timer_elapsed (timer, NULL)));
2353
2354         if (!setup_pango_context (job))
2355                 return FALSE;
2356
2357         /* check margins */
2358         if (!update_page_size_and_margins (job))
2359                 return FALSE;
2360
2361         /* split the document in pages */
2362         if (!paginate_text (job))
2363                 return FALSE;
2364
2365         PROFILE ({
2366                 g_message ("paginate_text: %.2f", g_timer_elapsed (timer, NULL));
2367                 g_timer_destroy (timer);
2368         });
2369
2370         return TRUE;
2371 }
2372
2373 /**
2374  * gtk_source_print_job_print:
2375  * @job: a configured #GtkSourcePrintJob.
2376  * 
2377  * Produces a #GnomePrintJob with the printed document.  The whole
2378  * contents of the configured #GtkTextBuffer are printed.  The
2379  * returned job is already closed and ready to be previewed (using
2380  * gnome_print_job_preview_new()) or printed directly.  The caller of
2381  * this function owns a reference to the returned object, so @job can
2382  * be destroyed and the output will still be valid, or the document
2383  * can be printed again with different settings.
2384  * 
2385  * Return value: a closed #GnomePrintJob with the printed document, or
2386  * %NULL if printing could not be completed.
2387  **/
2388 GnomePrintJob *
2389 gtk_source_print_job_print (GtkSourcePrintJob *job)
2390 {
2391         GtkTextIter start, end;
2392
2393         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2394         g_return_val_if_fail (!job->priv->printing, NULL);
2395         g_return_val_if_fail (job->priv->buffer != NULL, NULL);
2396
2397         gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (job->priv->buffer), &start, &end);
2398
2399         return gtk_source_print_job_print_range (job, &start, &end);
2400 }
2401
2402 /**
2403  * gtk_source_print_job_print_range:
2404  * @job: a configured #GtkSourcePrintJob.
2405  * @start: the start of the region of text to print.
2406  * @end: the end of the region of text to print.
2407  * 
2408  * Similar to gtk_source_print_job_print(), except you can specify a
2409  * range of text to print.  The passed #GtkTextIter values might be
2410  * out of order.
2411  * 
2412  * Return value: a closed #GnomePrintJob with the text from @start to
2413  * @end printed, or %NULL if @job could not print.
2414  **/
2415 GnomePrintJob *
2416 gtk_source_print_job_print_range (GtkSourcePrintJob *job,
2417                                   const GtkTextIter *start,
2418                                   const GtkTextIter *end)
2419 {
2420         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2421         g_return_val_if_fail (!job->priv->printing, NULL);
2422         g_return_val_if_fail (job->priv->buffer != NULL, NULL);
2423         g_return_val_if_fail (start != NULL && end != NULL, NULL);
2424         g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
2425                               GTK_TEXT_BUFFER (job->priv->buffer) &&
2426                               gtk_text_iter_get_buffer (end) ==
2427                               GTK_TEXT_BUFFER (job->priv->buffer), NULL);
2428
2429         if (!gtk_source_print_job_prepare (job, start, end))
2430                 return NULL;
2431
2432         /* real work starts here */
2433         setup_for_print (job);
2434
2435         job->priv->printing = TRUE;
2436         print_job (job);
2437         job->priv->printing = FALSE;
2438
2439         g_object_ref (job->priv->print_job);
2440         return job->priv->print_job;
2441 }
2442
2443 /* --- asynchronous printing */
2444
2445 /**
2446  * gtk_source_print_job_print_range_async:
2447  * @job: a configured #GtkSourcePrintJob.
2448  * @start: the start of the region of text to print.
2449  * @end: the end of the region of text to print.
2450  * 
2451  * Starts to print @job asynchronously.  This function will ready the
2452  * @job for printing and install an idle handler that will render one
2453  * page at a time.
2454  *
2455  * This function will not return immediatly, as only page rendering is
2456  * done asynchronously.  Text retrieval and paginating happens within
2457  * this function.  Also, if highlighting is enabled, the whole buffer
2458  * needs to be highlighted first.
2459  *
2460  * To get notification when the job has finished, you must connect to
2461  * the <link
2462  * linkend="GtkSourcePrintJob-finished">&quot;finished&quot;</link>
2463  * signal.  After this signal is emitted you can retrieve the
2464  * resulting #GnomePrintJob with gtk_source_print_job_get_print_job().
2465  * You may cancel the job with gtk_source_print_job_cancel().
2466  *
2467  * Return value: %TRUE if the print started.
2468  **/
2469 gboolean 
2470 gtk_source_print_job_print_range_async (GtkSourcePrintJob *job,
2471                                         const GtkTextIter *start,
2472                                         const GtkTextIter *end)
2473 {
2474         GSource *idle_source;
2475
2476         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2477         g_return_val_if_fail (!job->priv->printing, FALSE);
2478         g_return_val_if_fail (job->priv->buffer != NULL, FALSE);
2479         g_return_val_if_fail (start != NULL && end != NULL, FALSE);
2480         g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
2481                               GTK_TEXT_BUFFER (job->priv->buffer) &&
2482                               gtk_text_iter_get_buffer (end) ==
2483                               GTK_TEXT_BUFFER (job->priv->buffer), FALSE);
2484
2485         if (!gtk_source_print_job_prepare (job, start, end))
2486                 return FALSE;
2487
2488         /* real work starts here */
2489         setup_for_print (job);
2490         if (job->priv->current_paragraph == NULL)
2491                 return FALSE;
2492         
2493         /* setup the idle handler to print each page at a time */
2494         idle_source = g_idle_source_new ();
2495         g_source_set_priority (idle_source, GTK_SOURCE_PRINT_JOB_PRIORITY);
2496         g_source_set_closure (idle_source,
2497                               g_cclosure_new_object ((GCallback) idle_printing_handler,
2498                                                      G_OBJECT (job)));
2499         job->priv->idle_printing_tag = g_source_attach (idle_source, NULL);
2500         g_source_unref (idle_source);
2501
2502         job->priv->printing = TRUE;
2503
2504         return TRUE;
2505 }
2506
2507 /**
2508  * gtk_source_print_job_cancel:
2509  * @job: a #GtkSourcePrintJob.
2510  * 
2511  * Cancels an asynchronous printing operation.  This will remove any
2512  * pending print idle handler and unref the current #GnomePrintJob.
2513  *
2514  * Note that if you got a reference to the job's #GnomePrintJob (using
2515  * gtk_source_print_job_get_print_job()) it will not be destroyed
2516  * (since you hold a reference to it), but it will not be closed
2517  * either.  If you wish to show or print the partially printed
2518  * document you need to close it yourself.
2519  *
2520  * This function has no effect when called from a non-asynchronous
2521  * print operation.
2522  **/
2523 void 
2524 gtk_source_print_job_cancel (GtkSourcePrintJob *job)
2525 {
2526         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2527         g_return_if_fail (job->priv->printing);
2528
2529         if (job->priv->idle_printing_tag > 0)
2530         {
2531                 g_source_remove (job->priv->idle_printing_tag);
2532                 job->priv->current_paragraph = NULL;
2533                 job->priv->idle_printing_tag = 0;
2534                 job->priv->printing = FALSE;
2535                 g_object_unref (job->priv->print_job);
2536                 g_object_unref (job->priv->print_ctxt);
2537                 job->priv->print_job = NULL;
2538                 job->priv->print_ctxt = NULL;
2539         }
2540 }
2541
2542 /**
2543  * gtk_source_print_job_get_print_job:
2544  * @job: a #GtkSourcePrintJob.
2545  * 
2546  * Gets a reference to the #GnomePrintJob which the @job is printing
2547  * or has recently finished printing.  You need to unref the returned
2548  * object.
2549  *
2550  * You may call this function in the middle of an asynchronous
2551  * printing operation, but the returned #GnomePrintJob will not be
2552  * closed until the last page is printed and the <link
2553  * linkend="GtkSourcePrintJob-finished">&quot;finished&quot;</link>
2554  * signal is emitted.
2555  * 
2556  * Return value: a new reference to the @job's #GnomePrintJob, or
2557  * %NULL.
2558  **/
2559 GnomePrintJob *
2560 gtk_source_print_job_get_print_job (GtkSourcePrintJob *job)
2561 {
2562         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2563
2564         if (job->priv->print_job)
2565                 g_object_ref (job->priv->print_job);
2566         
2567         return job->priv->print_job;
2568 }
2569
2570 /* --- information for asynchronous ops and headers and footers callback */
2571
2572 /**
2573  * gtk_source_print_job_get_page:
2574  * @job: a #GtkSourcePrintJob.
2575  * 
2576  * Determines the currently printing page number.  This function is
2577  * only valid while printing (either synchronously or asynchronously).
2578  * 
2579  * Return value: the current page number.
2580  **/
2581 guint 
2582 gtk_source_print_job_get_page (GtkSourcePrintJob *job)
2583 {
2584         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2585         g_return_val_if_fail (job->priv->printing, 0);
2586
2587         return job->priv->page;
2588 }
2589
2590 /**
2591  * gtk_source_print_job_get_page_count:
2592  * @job: a #GtkSourcePrintJob.
2593  * 
2594  * Determines the total number of pages the job will print.  The
2595  * returned value is only meaninful after pagination has finished.  In
2596  * practice, for synchronous printing this means when <link
2597  * linkend="GtkSourcePrintJob-begin-page">&quot;begin_page&quot;</link>
2598  * is emitted, or after gtk_source_print_job_print_range_async() has
2599  * returned.
2600  * 
2601  * Return value: the number of pages of the printed job.
2602  **/
2603 guint 
2604 gtk_source_print_job_get_page_count (GtkSourcePrintJob *job)
2605 {
2606         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), 0);
2607
2608         return job->priv->page_count;
2609 }
2610
2611 /**
2612  * gtk_source_print_job_get_print_context:
2613  * @job: a printing #GtkSourcePrintJob.
2614  * 
2615  * Determines the #GnomePrintContext of the current job.  This
2616  * function is only valid while printing.  Normally you would use this
2617  * function to print in the margins set by
2618  * gtk_source_print_job_set_margins() in a handler for the <link
2619  * linkend="GtkSourcePrintJob-begin-page">&quot;begin_page&quot;</link>
2620  * signal.
2621  * 
2622  * Return value: the current #GnomePrintContext.  The returned object
2623  * is owned by @job.
2624  **/
2625 GnomePrintContext *
2626 gtk_source_print_job_get_print_context (GtkSourcePrintJob *job)
2627 {
2628         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
2629         g_return_val_if_fail (job->priv->printing, NULL);
2630
2631         return job->priv->print_ctxt;
2632 }
2633
2634 /* ---- Header and footer (default implementation) */
2635
2636 /* Most of this code taken from GLib's g_date_strftime() in gdate.c
2637  * GLIB - Library of useful routines for C programming
2638  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald */
2639
2640 static gchar *
2641 strdup_strftime (const gchar *format, const struct tm *tm)
2642 {
2643         gsize locale_format_len = 0;
2644         gchar *locale_format;
2645         gsize tmplen;
2646         gchar *tmpbuf;
2647         gsize tmpbufsize;
2648         gchar *convbuf;
2649         gsize convlen = 0;
2650         GError *error = NULL;
2651
2652         g_return_val_if_fail (format != NULL, NULL);
2653         g_return_val_if_fail (tm != NULL, NULL);
2654
2655         locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
2656
2657         if (error)
2658         {
2659                 g_warning (G_STRLOC "Error converting format to locale encoding: %s",
2660                            error->message);
2661                 g_error_free (error);
2662                 
2663                 return NULL;
2664         }
2665
2666         tmpbufsize = MAX (128, locale_format_len * 2);
2667         while (TRUE)
2668         {
2669                 tmpbuf = g_malloc (tmpbufsize);
2670                 
2671                 /* Set the first byte to something other than '\0', to be able to
2672                  * recognize whether strftime actually failed or just returned "".
2673                  */
2674                 tmpbuf[0] = '\1';
2675                 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, tm);
2676                 
2677                 if (tmplen == 0 && tmpbuf[0] != '\0')
2678                 {
2679                         g_free (tmpbuf);
2680                         tmpbufsize *= 2;
2681                         
2682                         if (tmpbufsize > 65536)
2683                         {
2684                                 g_warning (G_STRLOC "Maximum buffer size for strdup_strftime "
2685                                            "exceeded: giving up");
2686                                 g_free (locale_format);
2687                                 return NULL;
2688                         }
2689                 }
2690                 else
2691                         break;
2692         }
2693         g_free (locale_format);
2694
2695         convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
2696         g_free (tmpbuf);
2697
2698         if (error)
2699         {
2700                 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s",
2701                            error->message);
2702                 g_error_free (error);
2703                 
2704                 return NULL;
2705         }
2706
2707         return convbuf;
2708 }
2709
2710 static gchar *
2711 evaluate_format_string (GtkSourcePrintJob *job, const gchar *format)
2712 {
2713         GString *eval;
2714         gchar *eval_str, *retval;
2715         const struct tm *tm;
2716         time_t now;
2717         gunichar ch;
2718         
2719         /* get time */
2720         time (&now);
2721         tm = localtime (&now);
2722
2723         /* analyze format string and replace the codes we know */
2724         eval = g_string_new_len (NULL, strlen (format));
2725         ch = g_utf8_get_char (format);
2726         while (ch != 0)
2727         {
2728                 if (ch == '%')
2729                 {
2730                         format = g_utf8_next_char (format);
2731                         ch = g_utf8_get_char (format);
2732                         if (ch == 'N')
2733                                 g_string_append_printf (eval, "%d", job->priv->page);
2734                         else if (ch == 'Q')
2735                                 g_string_append_printf (eval, "%d", job->priv->page_count);
2736                         else
2737                         {
2738                                 g_string_append_c (eval, '%');
2739                                 g_string_append_unichar (eval, ch);
2740                         }
2741                 }
2742                 else
2743                         g_string_append_unichar (eval, ch);
2744
2745                 format = g_utf8_next_char (format);
2746                 ch = g_utf8_get_char (format);
2747         }
2748
2749         eval_str = g_string_free (eval, FALSE);
2750         retval = strdup_strftime (eval_str, tm);
2751         g_free (eval_str);
2752
2753         return retval;
2754 }
2755
2756 static void
2757 print_header_footer_string (GtkSourcePrintJob *job,
2758                             const gchar       *format,
2759                             gdouble            x_align,
2760                             gdouble            x,
2761                             gdouble            y)
2762 {
2763         PangoLayout *layout;
2764         gchar *text;
2765         gdouble width;
2766         gdouble xx;
2767         
2768         width = job->priv->text_width + job->priv->numbers_width;
2769         
2770         text = evaluate_format_string (job, format);
2771         if (text != NULL)
2772         {
2773                 layout = pango_layout_new (job->priv->pango_context);
2774                 pango_layout_set_font_description (layout, job->priv->header_footer_font);
2775                 pango_layout_set_text (layout, text, -1);
2776                 
2777                 xx = x + x_align * (width - get_layout_width (layout));
2778                 gnome_print_moveto (job->priv->print_ctxt, xx, y);
2779                 show_first_layout_line (job->priv->print_ctxt, layout);
2780                 
2781                 g_free (text);
2782                 g_object_unref (layout);
2783         }
2784 }
2785
2786 static void 
2787 default_print_header (GtkSourcePrintJob *job,
2788                       gdouble            x,
2789                       gdouble            y)
2790 {
2791         gdouble width;
2792         gdouble yy;
2793         gdouble ascent, descent;
2794         
2795         width = job->priv->text_width + job->priv->numbers_width;
2796
2797         get_font_ascent_descent (job, job->priv->header_footer_font, &ascent, &descent);
2798
2799         yy = y - ascent;
2800
2801         /* left format */
2802         if (job->priv->header_format_left != NULL)
2803                 print_header_footer_string (job, job->priv->header_format_left, 0.0, x, yy);
2804         
2805         /* right format */
2806         if (job->priv->header_format_right != NULL)
2807                 print_header_footer_string (job, job->priv->header_format_right, 1.0, x, yy);
2808
2809         /* center format */
2810         if (job->priv->header_format_center != NULL)
2811                 print_header_footer_string (job, job->priv->header_format_center, 0.5, x, yy);
2812
2813         /* separator */
2814         if (job->priv->header_separator)
2815         {
2816                 yy = y - (SEPARATOR_SPACING * (ascent + descent));
2817                 gnome_print_setlinewidth (job->priv->print_ctxt, SEPARATOR_LINE_WIDTH);
2818                 gnome_print_moveto (job->priv->print_ctxt, x, yy);
2819                 gnome_print_lineto (job->priv->print_ctxt, x + width, yy);
2820                 gnome_print_stroke (job->priv->print_ctxt);
2821         }
2822 }
2823
2824 static void 
2825 default_print_footer (GtkSourcePrintJob *job,
2826                       gdouble            x,
2827                       gdouble            y)
2828 {
2829         gdouble width;
2830         gdouble yy;
2831         gdouble ascent, descent;
2832         
2833         width = job->priv->text_width + job->priv->numbers_width;
2834
2835         get_font_ascent_descent (job, job->priv->header_footer_font, &ascent, &descent);
2836
2837         yy = y - job->priv->footer_height + descent;
2838
2839         /* left format */
2840         if (job->priv->footer_format_left != NULL)
2841                 print_header_footer_string (job, job->priv->footer_format_left, 0.0, x, yy);
2842         
2843         /* right format */
2844         if (job->priv->footer_format_right != NULL)
2845                 print_header_footer_string (job, job->priv->footer_format_right, 1.0, x, yy);
2846
2847         /* center format */
2848         if (job->priv->footer_format_center != NULL)
2849                 print_header_footer_string (job, job->priv->footer_format_center, 0.5, x, yy);
2850
2851         /* separator */
2852         if (job->priv->footer_separator)
2853         {
2854                 yy = y - job->priv->footer_height +
2855                         (SEPARATOR_SPACING * (ascent + descent));
2856                 gnome_print_setlinewidth (job->priv->print_ctxt, SEPARATOR_LINE_WIDTH);
2857                 gnome_print_moveto (job->priv->print_ctxt, x, yy);
2858                 gnome_print_lineto (job->priv->print_ctxt, x + width, yy);
2859                 gnome_print_stroke (job->priv->print_ctxt);
2860         }
2861 }
2862
2863 /**
2864  * gtk_source_print_job_set_print_header:
2865  * @job: a #GtkSourcePrintJob.
2866  * @setting: %TRUE if you want the header to be printed.
2867  * 
2868  * Sets whether you want to print a header in each page.  The default
2869  * header consists of three pieces of text and an optional line
2870  * separator, configurable with
2871  * gtk_source_print_job_set_header_format().
2872  *
2873  * Note that by default the header format is unspecified, and if it's
2874  * empty it will not be printed, regardless of this setting.
2875  **/
2876 void 
2877 gtk_source_print_job_set_print_header (GtkSourcePrintJob *job,
2878                                        gboolean           setting)
2879 {
2880         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2881         g_return_if_fail (!job->priv->printing);
2882
2883         setting = (setting != FALSE);
2884         
2885         if (setting == job->priv->print_header)
2886                 return;
2887         
2888         job->priv->print_header = setting;
2889
2890         g_object_notify (G_OBJECT (job), "print_header");
2891 }
2892
2893 /**
2894  * gtk_source_print_job_get_print_header:
2895  * @job: a #GtkSourcePrintJob.
2896  * 
2897  * Determines if a header is set to be printed for each page.  A
2898  * header will be printed if this function returns %TRUE
2899  * <emphasis>and</emphasis> some format strings have been specified
2900  * with gtk_source_print_job_set_header_format().
2901  * 
2902  * Return value: %TRUE if the header is set to be printed.
2903  **/
2904 gboolean 
2905 gtk_source_print_job_get_print_header (GtkSourcePrintJob *job)
2906 {
2907         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2908
2909         return job->priv->print_header;
2910 }
2911
2912 /**
2913  * gtk_source_print_job_set_print_footer:
2914  * @job: a #GtkSourcePrintJob.
2915  * @setting: %TRUE if you want the footer to be printed.
2916  * 
2917  * Sets whether you want to print a footer in each page.  The default
2918  * footer consists of three pieces of text and an optional line
2919  * separator, configurable with
2920  * gtk_source_print_job_set_footer_format().
2921  *
2922  * Note that by default the footer format is unspecified, and if it's
2923  * empty it will not be printed, regardless of this setting.
2924  **/
2925 void 
2926 gtk_source_print_job_set_print_footer (GtkSourcePrintJob *job,
2927                                        gboolean           setting)
2928 {
2929         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2930         g_return_if_fail (!job->priv->printing);
2931
2932         setting = (setting != FALSE);
2933         
2934         if (setting == job->priv->print_footer)
2935                 return;
2936         
2937         job->priv->print_footer = setting;
2938
2939         g_object_notify (G_OBJECT (job), "print_footer");
2940 }
2941
2942 /**
2943  * gtk_source_print_job_get_print_footer:
2944  * @job: a #GtkSourcePrintJob.
2945  * 
2946  * Determines if a footer is set to be printed for each page.  A
2947  * footer will be printed if this function returns %TRUE
2948  * <emphasis>and</emphasis> some format strings have been specified
2949  * with gtk_source_print_job_set_footer_format().
2950  * 
2951  * Return value: %TRUE if the footer is set to be printed.
2952  **/
2953 gboolean 
2954 gtk_source_print_job_get_print_footer (GtkSourcePrintJob *job)
2955 {
2956         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), FALSE);
2957
2958         return job->priv->print_footer;
2959 }
2960
2961 /**
2962  * gtk_source_print_job_set_header_footer_font_desc:
2963  * @job: a #GtkSourcePrintJob.
2964  * @desc: the #PangoFontDescription for the font to be used in headers and footers, or %NULL.
2965  * 
2966  * Sets the font for printing headers and footers.  If %NULL is
2967  * supplied, the default font (i.e. the one being used for the text)
2968  * will be used instead.
2969  **/
2970 void 
2971 gtk_source_print_job_set_header_footer_font_desc (GtkSourcePrintJob    *job,
2972                                                   PangoFontDescription *desc)
2973 {
2974         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
2975         g_return_if_fail (!job->priv->printing);
2976         
2977         if (desc)
2978                 desc = pango_font_description_copy (desc);
2979         if (job->priv->header_footer_font != NULL)
2980                 pango_font_description_free (job->priv->header_footer_font);
2981         job->priv->header_footer_font = desc;
2982         g_object_freeze_notify (G_OBJECT (job));
2983         g_object_notify (G_OBJECT (job), "header_footer_font");
2984         g_object_notify (G_OBJECT (job), "header_footer_font_desc");
2985         g_object_thaw_notify (G_OBJECT (job));
2986 }
2987
2988 /**
2989  * gtk_source_print_job_set_header_footer_font:
2990  * @job: a #GtkSourcePrintJob.
2991  * @font_name: the full name of the font to be used in headers and footers, or %NULL.
2992  * 
2993  * Sets the font for printing headers and footers.  If %NULL is
2994  * supplied, the default font (i.e. the one being used for the text)
2995  * will be used instead.
2996  *
2997  * Note that @font_name is a #GnomeFont name not a Pango font
2998  * description string. This function is deprecated since #GnomeFont is
2999  * no longer used when implementing printing for GtkSourceView; you
3000  * should use gtk_source_print_job_set_header_footer_font_desc() instead.
3001  **/
3002 void 
3003 gtk_source_print_job_set_header_footer_font (GtkSourcePrintJob *job,
3004                                              const gchar       *font_name)
3005 {
3006         PangoFontDescription *desc;
3007         
3008         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3009         g_return_if_fail (!job->priv->printing);
3010
3011         if (font_name != NULL)
3012         {
3013                 desc = font_description_from_gnome_font_name (font_name);
3014                 if (desc)
3015                 {
3016                         gtk_source_print_job_set_header_footer_font_desc (job, desc);
3017                         pango_font_description_free (desc);
3018                 }
3019         }
3020         else
3021                 gtk_source_print_job_set_header_footer_font_desc (job, NULL);
3022 }
3023
3024 /**
3025  * gtk_source_print_job_get_header_footer_font_desc:
3026  * @job: a #GtkSourcePrintJob.
3027  * 
3028  * Determines the font to be used for the header and footer.  This function
3029  * might return %NULL if a specific font has not been set.
3030  * 
3031  * Return value: the header and footer font description or %NULL. This value is
3032  * owned by the job and must not be modified or freed.
3033  **/
3034 PangoFontDescription *
3035 gtk_source_print_job_get_header_footer_font_desc (GtkSourcePrintJob *job)
3036 {
3037         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
3038
3039         return job->priv->header_footer_font;
3040 }
3041
3042 /**
3043  * gtk_source_print_job_get_header_footer_font:
3044  * @job: a #GtkSourcePrintJob.
3045  * 
3046  * Determines the font to be used for the header and footer.  The
3047  * returned string is of the form &quot;Fontfamily Style Size&quot;,
3048  * for example &quot;Monospace Regular 10.0&quot;.  The returned value
3049  * should be freed when no longer needed.  This function might return
3050  * %NULL if a specific font has not been set.
3051  * 
3052  * Note that the result is a #GnomeFont name not a Pango font
3053  * description string. This function is deprecated since #GnomeFont is
3054  * no longer used when implementing printing for GtkSourceView; you
3055  * should use gtk_source_print_job_get_header_footer_font_desc() instead.
3056  *
3057  * Return value: a newly allocated string with the name of the current
3058  * header and footer font, or %NULL.
3059  **/
3060 gchar *
3061 gtk_source_print_job_get_header_footer_font (GtkSourcePrintJob *job)
3062 {
3063         g_return_val_if_fail (GTK_IS_SOURCE_PRINT_JOB (job), NULL);
3064
3065         if (job->priv->header_footer_font != NULL)
3066                 return font_description_to_gnome_font_name (job->priv->header_footer_font);
3067         else
3068                 return NULL;
3069 }
3070
3071 /**
3072  * gtk_source_print_job_set_header_format:
3073  * @job: a #GtkSourcePrintJob.
3074  * @left: a format string to print on the left of the header.
3075  * @center: a format string to print on the center of the header.
3076  * @right: a format string to print on the right of the header.
3077  * @separator: %TRUE if you want a separator line to be printed.
3078  * 
3079  * Sets strftime like header format strings, to be printed on the
3080  * left, center and right of the top of each page.  The strings may
3081  * include strftime(3) codes which will be expanded at print time.
3082  * All strftime() codes are accepted, with the addition of %N for the
3083  * page number and %Q for the page count.
3084  *
3085  * @separator specifies if a solid line should be drawn to separate
3086  * the header from the document text.
3087  *
3088  * If %NULL is given for any of the three arguments, that particular
3089  * string will not be printed.  For the header to be printed, in
3090  * addition to specifying format strings, you need to enable header
3091  * printing with gtk_source_print_job_set_print_header().
3092  **/
3093 void 
3094 gtk_source_print_job_set_header_format (GtkSourcePrintJob *job,
3095                                         const gchar       *left,
3096                                         const gchar       *center,
3097                                         const gchar       *right,
3098                                         gboolean           separator)
3099 {
3100         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3101         g_return_if_fail (!job->priv->printing);
3102
3103         /* FIXME: validate given strings? */
3104         g_free (job->priv->header_format_left);
3105         g_free (job->priv->header_format_center);
3106         g_free (job->priv->header_format_right);
3107         job->priv->header_format_left = g_strdup (left);
3108         job->priv->header_format_center = g_strdup (center);
3109         job->priv->header_format_right = g_strdup (right);
3110         job->priv->header_separator = separator;
3111 }
3112
3113 /**
3114  * gtk_source_print_job_set_footer_format:
3115  * @job: a #GtkSourcePrintJob.
3116  * @left: a format string to print on the left of the footer.
3117  * @center: a format string to print on the center of the footer.
3118  * @right: a format string to print on the right of the footer.
3119  * @separator: %TRUE if you want a separator line to be printed.
3120  * 
3121  * Like gtk_source_print_job_set_header_format(), but for the footer.
3122  **/
3123 void 
3124 gtk_source_print_job_set_footer_format (GtkSourcePrintJob *job,
3125                                         const gchar       *left,
3126                                         const gchar       *center,
3127                                         const gchar       *right,
3128                                         gboolean           separator)
3129 {
3130         g_return_if_fail (GTK_IS_SOURCE_PRINT_JOB (job));
3131         g_return_if_fail (!job->priv->printing);
3132
3133         /* FIXME: validate given strings? */
3134         g_free (job->priv->footer_format_left);
3135         g_free (job->priv->footer_format_center);
3136         g_free (job->priv->footer_format_right);
3137         job->priv->footer_format_left = g_strdup (left);
3138         job->priv->footer_format_center = g_strdup (center);
3139         job->priv->footer_format_right = g_strdup (right);
3140         job->priv->footer_separator = separator;
3141 }
3142 #endif