~ubuntu-branches/ubuntu/vivid/mousepad/vivid

« back to all changes in this revision

Viewing changes to mousepad/mousepad-print.c

  • Committer: Package Import Robot
  • Author(s): Lionel Le Folgoc
  • Date: 2012-12-31 14:24:28 UTC
  • mfrom: (1.1.9) (0.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20121231142428-619qngl22evfjghf
Tags: 0.3.0-1ubuntu1
* Merge from Debian experimental, remaining Ubuntu change:
  - debian/control: drop xfprint4 recommendation, unneeded.
* debian/patches/02_mru-on-save.patch: dropped, obsolete.
* Bugs closed by this new release:
  - Mousepad Find dialog always opens with "Match case" checked lp: #714252
  - mousepad window takes a lot of time to close itself. lp: #531704

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This program is free software; you can redistribute it and/or modify it
 
3
 * under the terms of the GNU General Public License as published by the Free
 
4
 * Software Foundation; either version 2 of the License, or (at your option)
 
5
 * any later version.
 
6
 *
 
7
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
8
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
9
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
10
 * more details.
 
11
 *
 
12
 * You should have received a copy of the GNU General Public License along with
 
13
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
14
 * Place, Suite 330, Boston, MA  02111-1307  USA
 
15
 */
 
16
 
 
17
#ifdef HAVE_CONFIG_H
 
18
#include <config.h>
 
19
#endif
 
20
 
 
21
#include <pango/pango.h>
 
22
#include <cairo.h>
 
23
 
 
24
#include <mousepad/mousepad-private.h>
 
25
#include <mousepad/mousepad-preferences.h>
 
26
#include <mousepad/mousepad-document.h>
 
27
#include <mousepad/mousepad-util.h>
 
28
#include <mousepad/mousepad-print.h>
 
29
 
 
30
#define DOCUMENT_SPACING (10)
 
31
 
 
32
 
 
33
 
 
34
static void           mousepad_print_finalize              (GObject                 *object);
 
35
static void           mousepad_print_settings_load         (GtkPrintOperation       *operation);
 
36
static void           mousepad_print_settings_save_foreach (const gchar             *key,
 
37
                                                            const gchar             *value,
 
38
                                                            gpointer                 user_data);
 
39
static void           mousepad_print_settings_save         (GtkPrintOperation       *operation);
 
40
static void           mousepad_print_begin_print           (GtkPrintOperation       *operation,
 
41
                                                            GtkPrintContext         *context);
 
42
static void           mousepad_print_draw_page             (GtkPrintOperation       *operation,
 
43
                                                            GtkPrintContext         *context,
 
44
                                                            gint                     page_nr);
 
45
static void           mousepad_print_page_setup_dialog     (GtkWidget               *button,
 
46
                                                            GtkPrintOperation       *operation);
 
47
static void           mousepad_print_button_toggled        (GtkWidget               *button,
 
48
                                                            MousepadPrint           *print);
 
49
static void           mousepad_print_button_font_set       (GtkFontButton           *button,
 
50
                                                            MousepadPrint           *print);
 
51
static PangoAttrList *mousepad_print_attr_list_bold        (void);
 
52
static GtkWidget     *mousepad_print_create_custom_widget  (GtkPrintOperation       *operation);
 
53
static void           mousepad_print_status_changed        (GtkPrintOperation       *operation);
 
54
static void           mousepad_print_done                  (GtkPrintOperation       *operation,
 
55
                                                            GtkPrintOperationResult  result);
 
56
 
 
57
 
 
58
 
 
59
struct _MousepadPrintClass
 
60
{
 
61
  GtkPrintOperationClass __parent__;
 
62
};
 
63
 
 
64
struct _MousepadPrint
 
65
{
 
66
  GtkPrintOperation __parent__;
 
67
 
 
68
  /* the document we're going to print */
 
69
  MousepadDocument        *document;
 
70
 
 
71
  /* print dialog widgets */
 
72
  GtkWidget                *widget_page_headers;
 
73
  GtkWidget                *widget_page_footers;
 
74
  GtkWidget                *widget_line_numbers;
 
75
  GtkWidget                *widget_text_wrapping;
 
76
  GtkWidget                *widget_syntax_highlighting;
 
77
  GtkWidget                *widget_header_font;
 
78
  GtkWidget                *widget_line_numbers_font;
 
79
  GtkWidget                *widget_body_font;
 
80
  GtkWidget                *widget_line_numbers_spin;
 
81
  GtkWidget                *widget_line_numbers_hbox;
 
82
 
 
83
  /* settings */
 
84
  gboolean                  print_line_numbers;
 
85
  gint                      line_number_increment;
 
86
 
 
87
  /* source view print compositor */
 
88
  GtkSourcePrintCompositor *compositor;
 
89
};
 
90
 
 
91
 
 
92
 
 
93
G_DEFINE_TYPE (MousepadPrint, mousepad_print, GTK_TYPE_PRINT_OPERATION);
 
94
 
 
95
 
 
96
 
 
97
static void
 
98
mousepad_print_class_init (MousepadPrintClass *klass)
 
99
{
 
100
  GObjectClass           *gobject_class;
 
101
  GtkPrintOperationClass *gtkprintoperation_class;
 
102
 
 
103
  gobject_class = G_OBJECT_CLASS (klass);
 
104
  gobject_class->finalize = mousepad_print_finalize;
 
105
 
 
106
  gtkprintoperation_class = GTK_PRINT_OPERATION_CLASS (klass);
 
107
  gtkprintoperation_class->begin_print = mousepad_print_begin_print;
 
108
  gtkprintoperation_class->draw_page = mousepad_print_draw_page;
 
109
  gtkprintoperation_class->create_custom_widget = mousepad_print_create_custom_widget;
 
110
  gtkprintoperation_class->status_changed = mousepad_print_status_changed;
 
111
  gtkprintoperation_class->done = mousepad_print_done;
 
112
}
 
113
 
 
114
 
 
115
 
 
116
static void
 
117
mousepad_print_init (MousepadPrint *print)
 
118
{
 
119
  /* init */
 
120
  print->print_line_numbers = FALSE;
 
121
  print->line_number_increment = 1;
 
122
  print->compositor = NULL;
 
123
 
 
124
  /* set a custom tab label */
 
125
  gtk_print_operation_set_custom_tab_label (GTK_PRINT_OPERATION (print), _("Document Settings"));
 
126
}
 
127
 
 
128
 
 
129
 
 
130
static void
 
131
mousepad_print_finalize (GObject *object)
 
132
{
 
133
  MousepadPrint *print = MOUSEPAD_PRINT (object);
 
134
 
 
135
  /* cleanup */
 
136
  g_object_unref (print->compositor);
 
137
 
 
138
  (*G_OBJECT_CLASS (mousepad_print_parent_class)->finalize) (object);
 
139
}
 
140
 
 
141
 
 
142
 
 
143
static void
 
144
mousepad_print_settings_load (GtkPrintOperation *operation)
 
145
{
 
146
  MousepadPrint         *print = MOUSEPAD_PRINT (operation);
 
147
  GKeyFile              *keyfile;
 
148
  gchar                 *filename;
 
149
  GtkPrintSettings      *settings = NULL;
 
150
  gchar                **keys;
 
151
  gint                   i;
 
152
  gchar                 *key;
 
153
  gchar                 *value;
 
154
  gchar                 *body_font = NULL;
 
155
  gchar                 *header_font = NULL;
 
156
  gchar                 *line_numbers_font = NULL;
 
157
  GtkPageSetup          *page_setup;
 
158
  GtkPaperSize          *paper_size;
 
159
  PangoContext          *context;
 
160
  PangoFontDescription  *font_desc;
 
161
 
 
162
  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (print->document));
 
163
  mousepad_return_if_fail (GTK_IS_WIDGET (print->document->textview));
 
164
 
 
165
  /* get the config file filename */
 
166
  filename = mousepad_util_get_save_location (MOUSEPAD_RC_RELPATH, FALSE);
 
167
  if (G_UNLIKELY (filename == NULL))
 
168
    return;
 
169
 
 
170
  /* create a new keyfile */
 
171
  keyfile = g_key_file_new ();
 
172
 
 
173
  if (G_LIKELY (g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL)))
 
174
    {
 
175
      /* get all the keys from the config file */
 
176
      keys = g_key_file_get_keys (keyfile, "Print Settings", NULL, NULL);
 
177
 
 
178
      if (G_LIKELY (keys))
 
179
        {
 
180
          /* create new settings object */
 
181
          settings = gtk_print_settings_new ();
 
182
 
 
183
          /* set all the keys */
 
184
          for (i = 0; keys[i] != NULL; i++)
 
185
            {
 
186
              /* read the value from the config file */
 
187
              value = g_key_file_get_value (keyfile, "Print Settings", keys[i], NULL);
 
188
 
 
189
              /* set the value */
 
190
              if (G_LIKELY (value))
 
191
                {
 
192
                  key = mousepad_util_key_name (keys[i]);
 
193
                  gtk_print_settings_set (settings, key, value);
 
194
                  g_free (key);
 
195
                  g_free (value);
 
196
                }
 
197
            }
 
198
 
 
199
          /* cleanup */
 
200
          g_strfreev (keys);
 
201
        }
 
202
    }
 
203
 
 
204
  /* free the key file */
 
205
  g_key_file_free (keyfile);
 
206
 
 
207
  /* cleanup */
 
208
  g_free (filename);
 
209
 
 
210
  if (G_LIKELY (settings))
 
211
    {
 
212
      /* apply the settings */
 
213
      gtk_print_operation_set_print_settings (operation, settings);
 
214
 
 
215
      if (gtk_print_settings_get_bool (settings, "page-setup-saved") == TRUE)
 
216
        {
 
217
          /* create new page setup */
 
218
          page_setup = gtk_page_setup_new ();
 
219
 
 
220
          /* set orientation */
 
221
          gtk_page_setup_set_orientation (page_setup, gtk_print_settings_get_orientation (settings));
 
222
 
 
223
          /* restore margins */
 
224
          gtk_page_setup_set_top_margin (page_setup, gtk_print_settings_get_double (settings, "top-margin"), GTK_UNIT_MM);
 
225
          gtk_page_setup_set_bottom_margin (page_setup, gtk_print_settings_get_double (settings, "bottom-margin"), GTK_UNIT_MM);
 
226
          gtk_page_setup_set_right_margin (page_setup, gtk_print_settings_get_double (settings, "right-margin"), GTK_UNIT_MM);
 
227
          gtk_page_setup_set_left_margin (page_setup, gtk_print_settings_get_double (settings, "left-margin"), GTK_UNIT_MM);
 
228
 
 
229
          /* set paper size */
 
230
          paper_size = gtk_print_settings_get_paper_size (settings);
 
231
          if (G_LIKELY (paper_size))
 
232
            gtk_page_setup_set_paper_size (page_setup, paper_size);
 
233
 
 
234
          /* set the default page setup */
 
235
          gtk_print_operation_set_default_page_setup (operation, page_setup);
 
236
 
 
237
          /* release reference */
 
238
          g_object_unref (G_OBJECT (page_setup));
 
239
        }
 
240
 
 
241
      /* restore print settings */
 
242
      g_object_set (print->compositor,
 
243
                    "print-header",
 
244
                    gtk_print_settings_get_bool (settings, "print-header"),
 
245
                    "print-line-numbers",
 
246
                    gtk_print_settings_get_int (settings, "line-numbers-increment"),
 
247
                    "wrap-mode",
 
248
                    gtk_print_settings_get_bool (settings, "text-wrapping") ? GTK_WRAP_WORD : GTK_WRAP_NONE,
 
249
                    "highlight-syntax",
 
250
                    gtk_print_settings_get_bool (settings, "highlight-syntax"),
 
251
                    NULL);
 
252
 
 
253
      print->print_line_numbers = gtk_print_settings_get_bool (settings, "print-line-numbers");
 
254
      print->line_number_increment = gtk_print_settings_get_int (settings, "line-numbers-increment");
 
255
 
 
256
      /* get the saved fonts, if set */
 
257
      body_font = g_strdup (gtk_print_settings_get (settings, "body-font-name"));
 
258
      header_font = g_strdup (gtk_print_settings_get (settings, "header-font-name"));
 
259
      line_numbers_font = g_strdup (gtk_print_settings_get (settings, "line-numbers-font-name"));
 
260
 
 
261
      /* release reference */
 
262
      g_object_unref (G_OBJECT (settings));
 
263
    }
 
264
 
 
265
    /* if no font name is set, get the one used in the widget */
 
266
    if (G_UNLIKELY (body_font == NULL))
 
267
      {
 
268
        /* get the font description from the context and convert it into a string */
 
269
        context = gtk_widget_get_pango_context (GTK_WIDGET (print->document->textview));
 
270
        font_desc = pango_context_get_font_description (context);
 
271
        body_font = pango_font_description_to_string (font_desc);
 
272
      }
 
273
 
 
274
    /* set the restored body font or the one from the textview */
 
275
    gtk_source_print_compositor_set_body_font_name (print->compositor, body_font);
 
276
 
 
277
    /* if header font restored use it, otherwise use body font */
 
278
    if (header_font)
 
279
      gtk_source_print_compositor_set_header_font_name (print->compositor, header_font);
 
280
    else
 
281
      gtk_source_print_compositor_set_header_font_name (print->compositor, body_font);
 
282
 
 
283
    /* if line numbers font restored use it, otherwise use body font */
 
284
    if (line_numbers_font)
 
285
      gtk_source_print_compositor_set_line_numbers_font_name (print->compositor, line_numbers_font);
 
286
    else
 
287
      gtk_source_print_compositor_set_line_numbers_font_name (print->compositor, body_font);
 
288
 
 
289
    /* cleanup */
 
290
    g_free (body_font);
 
291
    g_free (header_font);
 
292
    g_free (line_numbers_font);
 
293
}
 
294
 
 
295
 
 
296
 
 
297
static void
 
298
mousepad_print_settings_save_foreach (const gchar *key,
 
299
                                      const gchar *value,
 
300
                                      gpointer     user_data)
 
301
{
 
302
  GKeyFile *keyfile = user_data;
 
303
  gchar    *config;
 
304
 
 
305
  /* save the setting */
 
306
  if (G_LIKELY (key && value))
 
307
    {
 
308
      config = mousepad_util_config_name (key);
 
309
      g_key_file_set_value (keyfile, "Print Settings", config, value);
 
310
      g_free (config);
 
311
    }
 
312
}
 
313
 
 
314
 
 
315
 
 
316
static void
 
317
mousepad_print_settings_save (GtkPrintOperation *operation)
 
318
{
 
319
  MousepadPrint    *print = MOUSEPAD_PRINT (operation);
 
320
  GKeyFile         *keyfile;
 
321
  gchar            *filename;
 
322
  GtkPrintSettings *settings;
 
323
  GtkPageSetup     *page_setup;
 
324
  GtkPaperSize     *paper_size;
 
325
 
 
326
  /* get the save location */
 
327
  filename = mousepad_util_get_save_location (MOUSEPAD_RC_RELPATH, TRUE);
 
328
 
 
329
  /* create a new keyfile */
 
330
  keyfile = g_key_file_new ();
 
331
 
 
332
  /* load the existing settings */
 
333
  if (G_LIKELY (g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL)))
 
334
    {
 
335
      /* get the print settings */
 
336
      settings = gtk_print_operation_get_print_settings (operation);
 
337
 
 
338
      if (G_LIKELY (settings != NULL))
 
339
        {
 
340
          /* get the page setup */
 
341
          page_setup = gtk_print_operation_get_default_page_setup (operation);
 
342
 
 
343
          /* restore the page setup */
 
344
          if (G_LIKELY (page_setup != NULL))
 
345
            {
 
346
              /* the the settings page orienation */
 
347
              gtk_print_settings_set_orientation (settings, gtk_page_setup_get_orientation (page_setup));
 
348
 
 
349
              /* save margins */
 
350
              gtk_print_settings_set_double (settings, "top-margin", gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM));
 
351
              gtk_print_settings_set_double (settings, "bottom-margin", gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM));
 
352
              gtk_print_settings_set_double (settings, "right-margin", gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM));
 
353
              gtk_print_settings_set_double (settings, "left-margin", gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM));
 
354
 
 
355
              /* get the paper size */
 
356
              paper_size = gtk_page_setup_get_paper_size (page_setup);
 
357
 
 
358
              /* set settings page size */
 
359
              if (G_LIKELY (paper_size))
 
360
                gtk_print_settings_set_paper_size (settings, paper_size);
 
361
            }
 
362
 
 
363
          /* a bool we use for loading */
 
364
          gtk_print_settings_set_bool (settings, "page-setup-saved", page_setup != NULL);
 
365
 
 
366
          /* set print settings */
 
367
          gtk_print_settings_set_bool (settings,
 
368
                                       "print-header",
 
369
                                       gtk_source_print_compositor_get_print_header (print->compositor));
 
370
 
 
371
          gtk_print_settings_set_bool (settings,
 
372
                                       "print-line-numbers",
 
373
                                       print->print_line_numbers);
 
374
 
 
375
          gtk_print_settings_set_int (settings,
 
376
                                      "line-numbers-increment",
 
377
                                      print->line_number_increment);
 
378
 
 
379
          gtk_print_settings_set_bool (settings,
 
380
                                       "text-wrapping",
 
381
                                       gtk_source_print_compositor_get_wrap_mode (print->compositor) == GTK_WRAP_NONE ? FALSE : TRUE);
 
382
 
 
383
          gtk_print_settings_set_bool (settings,
 
384
                                       "highlight-syntax",
 
385
                                       gtk_source_print_compositor_get_highlight_syntax (print->compositor));
 
386
 
 
387
          gtk_print_settings_set (settings,
 
388
                                  "body-font-name",
 
389
                                  gtk_source_print_compositor_get_body_font_name (print->compositor));
 
390
 
 
391
          gtk_print_settings_set (settings,
 
392
                                  "header-font-name",
 
393
                                  gtk_source_print_compositor_get_header_font_name (print->compositor));
 
394
 
 
395
          gtk_print_settings_set (settings,
 
396
                                  "line-numbers-font-name",
 
397
                                  gtk_source_print_compositor_get_line_numbers_font_name (print->compositor));
 
398
 
 
399
          /* store all the print settings */
 
400
          gtk_print_settings_foreach (settings, mousepad_print_settings_save_foreach, keyfile);
 
401
 
 
402
          /* save the contents */
 
403
          mousepad_util_save_key_file (keyfile, filename);
 
404
        }
 
405
    }
 
406
 
 
407
  /* cleanup */
 
408
  g_key_file_free (keyfile);
 
409
  g_free (filename);
 
410
}
 
411
 
 
412
 
 
413
 
 
414
static void
 
415
mousepad_print_begin_print (GtkPrintOperation *operation,
 
416
                            GtkPrintContext   *context)
 
417
{
 
418
  MousepadPrint    *print = MOUSEPAD_PRINT (operation);
 
419
  MousepadDocument *document = print->document;
 
420
  gint              n_pages = 1;
 
421
  const gchar      *file_name;
 
422
 
 
423
  /* print header */
 
424
  if (gtk_source_print_compositor_get_print_header (print->compositor))
 
425
    {
 
426
      if (mousepad_document_get_filename (document))
 
427
        file_name = mousepad_document_get_filename (document);
 
428
      else
 
429
        file_name = mousepad_document_get_basename (document);
 
430
 
 
431
      gtk_source_print_compositor_set_header_format (print->compositor,
 
432
                                                     TRUE,
 
433
                                                     file_name,
 
434
                                                     NULL,
 
435
                                                     "Page %N of %Q");
 
436
    }
 
437
 
 
438
  /* paginate all of the pages at once */
 
439
  while (!gtk_source_print_compositor_paginate (print->compositor, context))
 
440
    ;
 
441
 
 
442
  n_pages = gtk_source_print_compositor_get_n_pages (print->compositor);
 
443
 
 
444
  /* set the number of pages we're going to draw */
 
445
  gtk_print_operation_set_n_pages (operation, n_pages);
 
446
}
 
447
 
 
448
 
 
449
 
 
450
static void
 
451
mousepad_print_draw_page (GtkPrintOperation *operation,
 
452
                          GtkPrintContext   *context,
 
453
                          gint               page_nr)
 
454
{
 
455
  MousepadPrint *print = MOUSEPAD_PRINT (operation);
 
456
 
 
457
  gtk_source_print_compositor_draw_page (print->compositor, context, page_nr);
 
458
}
 
459
 
 
460
 
 
461
 
 
462
static void
 
463
mousepad_print_page_setup_dialog (GtkWidget         *button,
 
464
                                  GtkPrintOperation *operation)
 
465
{
 
466
  GtkWidget        *toplevel;
 
467
  GtkPrintSettings *settings;
 
468
  GtkPageSetup     *page_setup;
 
469
 
 
470
  /* get the toplevel of the button */
 
471
  toplevel = gtk_widget_get_toplevel (button);
 
472
  if (G_UNLIKELY (!gtk_widget_is_toplevel (toplevel)))
 
473
    toplevel = NULL;
 
474
 
 
475
  /* get the print settings */
 
476
  settings = gtk_print_operation_get_print_settings (operation);
 
477
  if (G_UNLIKELY (settings == NULL))
 
478
    settings = gtk_print_settings_new ();
 
479
 
 
480
  /* get the page setup */
 
481
  page_setup = gtk_print_operation_get_default_page_setup (operation);
 
482
 
 
483
  /* run the dialog */
 
484
  page_setup = gtk_print_run_page_setup_dialog (GTK_WINDOW (toplevel), page_setup, settings);
 
485
 
 
486
  /* set the new page setup */
 
487
  gtk_print_operation_set_default_page_setup (operation, page_setup);
 
488
}
 
489
 
 
490
 
 
491
 
 
492
static void
 
493
mousepad_print_button_toggled (GtkWidget     *button,
 
494
                               MousepadPrint *print)
 
495
{
 
496
  gboolean active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
 
497
 
 
498
  /* save the correct setting */
 
499
  if (button == print->widget_page_headers)
 
500
    gtk_source_print_compositor_set_print_header (print->compositor, active);
 
501
  else if (button == print->widget_line_numbers)
 
502
  {
 
503
    print->print_line_numbers = active;
 
504
    gtk_widget_set_sensitive (print->widget_line_numbers_hbox, active);
 
505
  }
 
506
  else if (button == print->widget_text_wrapping)
 
507
    gtk_source_print_compositor_set_wrap_mode (print->compositor, active ? GTK_WRAP_WORD : GTK_WRAP_NONE);
 
508
  else if (button == print->widget_syntax_highlighting)
 
509
    gtk_source_print_compositor_set_highlight_syntax (print->compositor, active);
 
510
}
 
511
 
 
512
 
 
513
 
 
514
static void
 
515
mousepad_print_button_font_set (GtkFontButton *button,
 
516
                                MousepadPrint *print)
 
517
{
 
518
  const gchar *font;
 
519
  GtkWidget   *widget = GTK_WIDGET (button);
 
520
 
 
521
  font = gtk_font_button_get_font_name (button);
 
522
 
 
523
  if (widget == print->widget_body_font)
 
524
    gtk_source_print_compositor_set_body_font_name (print->compositor, font);
 
525
  else if (widget == print->widget_header_font)
 
526
    gtk_source_print_compositor_set_header_font_name (print->compositor, font);
 
527
  else if (widget == print->widget_line_numbers_font)
 
528
    gtk_source_print_compositor_set_line_numbers_font_name (print->compositor, font);
 
529
}
 
530
 
 
531
 
 
532
 
 
533
static void
 
534
mousepad_print_spin_value_changed (GtkSpinButton *button,
 
535
                                   MousepadPrint *print)
 
536
{
 
537
  print->line_number_increment = gtk_spin_button_get_value_as_int (button);
 
538
 
 
539
  if (print->line_number_increment > 0 && print->print_line_numbers)
 
540
    {
 
541
      gtk_source_print_compositor_set_print_line_numbers (print->compositor,
 
542
                                                          print->line_number_increment);
 
543
    }
 
544
  else
 
545
    gtk_source_print_compositor_set_print_line_numbers (print->compositor, 0);
 
546
}
 
547
 
 
548
 
 
549
 
 
550
static PangoAttrList *
 
551
mousepad_print_attr_list_bold (void)
 
552
{
 
553
  static PangoAttrList *attr_list = NULL;
 
554
  PangoAttribute       *attr;
 
555
 
 
556
  if (G_UNLIKELY (attr_list == NULL))
 
557
    {
 
558
      /* create new attributes list */
 
559
      attr_list = pango_attr_list_new ();
 
560
 
 
561
      /* create attribute */
 
562
      attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
 
563
      attr->start_index = 0;
 
564
      attr->end_index = -1;
 
565
 
 
566
      /* insert bold element */
 
567
      pango_attr_list_insert (attr_list, attr);
 
568
    }
 
569
 
 
570
  return attr_list;
 
571
}
 
572
 
 
573
 
 
574
 
 
575
static GtkWidget *
 
576
mousepad_print_create_custom_widget (GtkPrintOperation *operation)
 
577
{
 
578
  MousepadPrint *print = MOUSEPAD_PRINT (operation);
 
579
  GtkWidget     *button;
 
580
  GtkWidget     *vbox, *vbox2;
 
581
  GtkWidget     *frame;
 
582
  GtkWidget     *alignment;
 
583
  GtkWidget     *label;
 
584
  GtkWidget     *table;
 
585
  GtkAdjustment *adjustment;
 
586
 
 
587
  vbox = gtk_vbox_new (FALSE, 6);
 
588
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
 
589
 
 
590
  frame = gtk_frame_new (NULL);
 
591
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
 
592
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
 
593
  gtk_widget_show (frame);
 
594
 
 
595
  label = gtk_label_new (_("Page Setup"));
 
596
  gtk_label_set_attributes (GTK_LABEL (label), mousepad_print_attr_list_bold ());
 
597
  gtk_frame_set_label_widget (GTK_FRAME (frame), label);
 
598
  gtk_widget_show (label);
 
599
 
 
600
  alignment = gtk_alignment_new (0.0, 0.5, 0.0, 1.0);
 
601
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 6, 6, 12, 6);
 
602
  gtk_container_add (GTK_CONTAINER (frame), alignment);
 
603
  gtk_widget_show (alignment);
 
604
 
 
605
  button = mousepad_util_image_button (GTK_STOCK_PROPERTIES, _("_Adjust page size and orientation"));
 
606
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (mousepad_print_page_setup_dialog), operation);
 
607
  gtk_container_add (GTK_CONTAINER (alignment), button);
 
608
  gtk_widget_show (button);
 
609
 
 
610
  frame = gtk_frame_new (NULL);
 
611
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
 
612
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
 
613
  gtk_widget_show (frame);
 
614
 
 
615
  label = gtk_label_new (_("Appearance"));
 
616
  gtk_label_set_attributes (GTK_LABEL (label), mousepad_print_attr_list_bold ());
 
617
  gtk_frame_set_label_widget (GTK_FRAME (frame), label);
 
618
  gtk_widget_show (label);
 
619
 
 
620
  alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
 
621
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 6, 6, 12, 6);
 
622
  gtk_container_add (GTK_CONTAINER (frame), alignment);
 
623
  gtk_widget_show (alignment);
 
624
 
 
625
  vbox2 = gtk_vbox_new (FALSE, 6);
 
626
  gtk_container_add (GTK_CONTAINER (alignment), vbox2);
 
627
  gtk_widget_show (vbox2);
 
628
 
 
629
  button = print->widget_page_headers = gtk_check_button_new_with_mnemonic (_("Print page _headers"));
 
630
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
 
631
                                gtk_source_print_compositor_get_print_header (print->compositor));
 
632
  g_signal_connect (G_OBJECT (button), "toggled", G_CALLBACK (mousepad_print_button_toggled), print);
 
633
  gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
 
634
  gtk_widget_show (button);
 
635
 
 
636
  button = print->widget_line_numbers = gtk_check_button_new_with_mnemonic (_("Print _line numbers"));
 
637
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
 
638
                                print->print_line_numbers);
 
639
  g_signal_connect (G_OBJECT (button), "toggled", G_CALLBACK (mousepad_print_button_toggled), print);
 
640
  gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
 
641
  gtk_widget_show (button);
 
642
 
 
643
  alignment = gtk_alignment_new (0.0, 0.5, 0.0, 1.0);
 
644
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 24, 0);
 
645
  gtk_box_pack_start (GTK_BOX (vbox2), alignment, FALSE, FALSE, 0);
 
646
  gtk_widget_show (alignment);
 
647
 
 
648
  print->widget_line_numbers_hbox = gtk_hbox_new (FALSE, 6);
 
649
  gtk_widget_set_sensitive (print->widget_line_numbers_hbox, print->print_line_numbers);
 
650
  gtk_container_add (GTK_CONTAINER (alignment), print->widget_line_numbers_hbox);
 
651
  gtk_widget_show (print->widget_line_numbers_hbox);
 
652
 
 
653
  label = gtk_label_new (_("Number every"));
 
654
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
655
  gtk_box_pack_start (GTK_BOX (print->widget_line_numbers_hbox), label, FALSE, TRUE, 0);
 
656
  gtk_widget_show (label);
 
657
 
 
658
  adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (1.0, 1.0, 100.0, 1.0, 0.0, 0.0));
 
659
  print->widget_line_numbers_spin = gtk_spin_button_new (adjustment, 1.0, 0);
 
660
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (print->widget_line_numbers_spin),
 
661
                             (gdouble) print->line_number_increment);
 
662
  g_signal_connect (G_OBJECT (print->widget_line_numbers_spin),
 
663
                    "value-changed",
 
664
                    G_CALLBACK (mousepad_print_spin_value_changed),
 
665
                    print);
 
666
  gtk_box_pack_start (GTK_BOX (print->widget_line_numbers_hbox), print->widget_line_numbers_spin, FALSE, TRUE, 0);
 
667
  gtk_widget_show (print->widget_line_numbers_spin);
 
668
 
 
669
  label = gtk_label_new (_("line(s)"));
 
670
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
671
  gtk_box_pack_start (GTK_BOX (print->widget_line_numbers_hbox), label, FALSE, TRUE, 0);
 
672
  gtk_widget_show (label);
 
673
 
 
674
  button = print->widget_text_wrapping = gtk_check_button_new_with_mnemonic (_("Enable text _wrapping"));
 
675
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
 
676
                                gtk_source_print_compositor_get_wrap_mode (print->compositor) == GTK_WRAP_NONE ? FALSE : TRUE);
 
677
  g_signal_connect (G_OBJECT (button), "toggled", G_CALLBACK (mousepad_print_button_toggled), print);
 
678
  gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
 
679
  gtk_widget_show (button);
 
680
 
 
681
  button = print->widget_syntax_highlighting = gtk_check_button_new_with_mnemonic (_("Enable _syntax highlighting"));
 
682
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
 
683
                                gtk_source_print_compositor_get_highlight_syntax (print->compositor));
 
684
  g_signal_connect (G_OBJECT (button), "toggled", G_CALLBACK (mousepad_print_button_toggled), print);
 
685
  gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
 
686
  gtk_widget_show (button);
 
687
 
 
688
  frame = gtk_frame_new (NULL);
 
689
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
 
690
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
 
691
  gtk_widget_show (frame);
 
692
 
 
693
  label = gtk_label_new (_("Fonts"));
 
694
  gtk_label_set_attributes (GTK_LABEL (label), mousepad_print_attr_list_bold ());
 
695
  gtk_frame_set_label_widget (GTK_FRAME (frame), label);
 
696
  gtk_widget_show (label);
 
697
 
 
698
  alignment = gtk_alignment_new (0.0, 0.5, 0.0, 1.0);
 
699
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 6, 6, 12, 6);
 
700
  gtk_container_add (GTK_CONTAINER (frame), alignment);
 
701
  gtk_widget_show (alignment);
 
702
 
 
703
  table = gtk_table_new (3, 2, FALSE);
 
704
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
 
705
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
 
706
  gtk_container_add (GTK_CONTAINER (alignment), table);
 
707
  gtk_widget_show (table);
 
708
 
 
709
  label = gtk_label_new (_("Header:"));
 
710
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
711
  gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 0, 1);
 
712
  gtk_widget_show (label);
 
713
 
 
714
  print->widget_header_font = gtk_font_button_new_with_font (gtk_source_print_compositor_get_header_font_name (print->compositor));
 
715
  gtk_table_attach_defaults (GTK_TABLE (table), print->widget_header_font, 1, 2, 0, 1);
 
716
  g_signal_connect (G_OBJECT (print->widget_header_font), "font-set", G_CALLBACK (mousepad_print_button_font_set), print);
 
717
  gtk_widget_show (print->widget_header_font);
 
718
 
 
719
  label = gtk_label_new (_("Body:"));
 
720
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
721
  gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 1, 2);
 
722
  gtk_widget_show (label);
 
723
 
 
724
  print->widget_body_font = gtk_font_button_new_with_font (gtk_source_print_compositor_get_body_font_name (print->compositor));
 
725
  gtk_table_attach_defaults (GTK_TABLE (table), print->widget_body_font, 1, 2, 1, 2);
 
726
  g_signal_connect (G_OBJECT (print->widget_body_font), "font-set", G_CALLBACK (mousepad_print_button_font_set), print);
 
727
  gtk_widget_show (print->widget_body_font);
 
728
 
 
729
  label = gtk_label_new (_("Line numbers:"));
 
730
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
731
  gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);
 
732
  gtk_widget_show (label);
 
733
 
 
734
  print->widget_line_numbers_font = gtk_font_button_new_with_font (gtk_source_print_compositor_get_line_numbers_font_name (print->compositor));
 
735
  gtk_table_attach_defaults (GTK_TABLE (table), print->widget_line_numbers_font, 1, 2, 2, 3);
 
736
  g_signal_connect (G_OBJECT (print->widget_line_numbers_font), "font-set", G_CALLBACK (mousepad_print_button_font_set), print);
 
737
  gtk_widget_show (print->widget_line_numbers_font);
 
738
 
 
739
  return vbox;
 
740
}
 
741
 
 
742
 
 
743
 
 
744
static void
 
745
mousepad_print_status_changed (GtkPrintOperation *operation)
 
746
{
 
747
  /* nothing usefull for now, we could set a statusbar text or something */
 
748
}
 
749
 
 
750
 
 
751
 
 
752
static void
 
753
mousepad_print_done (GtkPrintOperation       *operation,
 
754
                     GtkPrintOperationResult  result)
 
755
{
 
756
  /* check if the print succeeded */
 
757
  if (result == GTK_PRINT_OPERATION_RESULT_APPLY)
 
758
    {
 
759
      /* save the settings */
 
760
      mousepad_print_settings_save (operation);
 
761
    }
 
762
}
 
763
 
 
764
 
 
765
 
 
766
MousepadPrint *
 
767
mousepad_print_new (void)
 
768
{
 
769
  return g_object_new (MOUSEPAD_TYPE_PRINT, NULL);
 
770
}
 
771
 
 
772
 
 
773
 
 
774
gboolean
 
775
mousepad_print_document_interactive (MousepadPrint     *print,
 
776
                                     MousepadDocument  *document,
 
777
                                     GtkWindow         *parent,
 
778
                                     GError           **error)
 
779
{
 
780
  GtkPrintOperationResult result;
 
781
 
 
782
  mousepad_return_val_if_fail (MOUSEPAD_IS_PRINT (print), FALSE);
 
783
  mousepad_return_val_if_fail (GTK_IS_PRINT_OPERATION (print), FALSE);
 
784
  mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), FALSE);
 
785
  mousepad_return_val_if_fail (GTK_IS_SOURCE_BUFFER (document->buffer), FALSE);
 
786
  mousepad_return_val_if_fail (GTK_IS_WINDOW (parent), FALSE);
 
787
  mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
788
 
 
789
  /* set the document */
 
790
  print->document = document;
 
791
  print->compositor = gtk_source_print_compositor_new (GTK_SOURCE_BUFFER (document->buffer));
 
792
 
 
793
  /* load settings */
 
794
  mousepad_print_settings_load (GTK_PRINT_OPERATION (print));
 
795
 
 
796
  /* allow async printing is support by the platform */
 
797
  gtk_print_operation_set_allow_async (GTK_PRINT_OPERATION (print), TRUE);
 
798
 
 
799
  /* run the operation */
 
800
  result = gtk_print_operation_run (GTK_PRINT_OPERATION (print),
 
801
                                    GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
 
802
                                    parent, error);
 
803
 
 
804
  return (result != GTK_PRINT_OPERATION_RESULT_ERROR);
 
805
}