]> git.sur5r.net Git - glabels/blob - src/print-op.c
Imported Upstream version 3.0.0
[glabels] / src / print-op.c
1 /*
2  *  print-op.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "print-op.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <math.h>
28 #include <time.h>
29 #include <ctype.h>
30
31 #include <libglabels.h>
32 #include "print.h"
33 #include "label.h"
34
35 #include "debug.h"
36
37
38 /*===========================================*/
39 /* Private data types                        */
40 /*===========================================*/
41
42 struct _glPrintOpPrivate {
43
44         glLabel   *label;
45
46         gboolean   force_outline_flag;
47
48         gchar     *filename;
49
50         gboolean   outline_flag;
51         gboolean   reverse_flag;
52         gboolean   crop_marks_flag;
53         gboolean   merge_flag;
54         gboolean   collate_flag;
55
56         gint       first;
57         gint       last;
58         gint       n_sheets;
59         gint       n_copies;
60
61         glPrintState state;
62 };
63
64 struct _glPrintOpSettings
65 {
66
67         GtkPrintSettings *gtk_settings;
68
69         gboolean          outline_flag;
70         gboolean          reverse_flag;
71         gboolean          crop_marks_flag;
72         gboolean          collate_flag;
73
74         gint              first;
75         gint              last;
76         gint              n_sheets;
77         gint              n_copies;
78         
79 };
80
81
82 /*===========================================*/
83 /* Private globals                           */
84 /*===========================================*/
85
86
87 /*===========================================*/
88 /* Local function prototypes                 */
89 /*===========================================*/
90
91 static void     gl_print_op_finalize          (GObject           *object);
92
93 static void     set_page_size                 (glPrintOp         *op,
94                                                glLabel           *label);
95
96 static void     begin_print_cb                (GtkPrintOperation *operation,
97                                                GtkPrintContext   *context,
98                                                gpointer           user_data);
99
100 static void     draw_page_cb                  (GtkPrintOperation *operation,
101                                                GtkPrintContext   *context,
102                                                int                page_nr,
103                                                gpointer           user_data);
104
105
106 /*****************************************************************************/
107 /* Boilerplate object stuff.                                                 */
108 /*****************************************************************************/
109 G_DEFINE_TYPE (glPrintOp, gl_print_op, GTK_TYPE_PRINT_OPERATION)
110
111
112 static void
113 gl_print_op_class_init (glPrintOpClass *class)
114 {
115         GObjectClass           *object_class = G_OBJECT_CLASS (class);
116
117         gl_debug (DEBUG_PRINT, "");
118         
119         gl_print_op_parent_class = g_type_class_peek_parent (class);
120
121         object_class->finalize = gl_print_op_finalize;
122 }
123
124
125 static void
126 gl_print_op_init (glPrintOp *op)
127 {
128         gl_debug (DEBUG_PRINT, "");
129
130         gtk_print_operation_set_use_full_page (GTK_PRINT_OPERATION (op), TRUE);
131
132         gtk_print_operation_set_unit (GTK_PRINT_OPERATION (op), GTK_UNIT_POINTS);
133
134         op->priv = g_new0 (glPrintOpPrivate, 1);
135
136 }
137
138
139 static void 
140 gl_print_op_finalize (GObject *object)
141 {
142         glPrintOp *op = GL_PRINT_OP (object);
143         
144         gl_debug (DEBUG_PRINT, "");
145
146         g_return_if_fail (object != NULL);
147         g_return_if_fail (GL_IS_PRINT_OP (op));
148         g_return_if_fail (op->priv != NULL);
149
150         g_object_unref (G_OBJECT(op->priv->label));
151         g_free (op->priv->filename);
152         g_free (op->priv);
153
154         G_OBJECT_CLASS (gl_print_op_parent_class)->finalize (object);
155
156         g_free (op->priv);
157 }
158
159
160 /*****************************************************************************/
161 /* NEW print op.                                                         */
162 /*****************************************************************************/
163 glPrintOp *
164 gl_print_op_new (glLabel      *label)
165 {
166         glPrintOp *op;
167
168         gl_debug (DEBUG_PRINT, "");
169
170         op = GL_PRINT_OP (g_object_new (GL_TYPE_PRINT_OP, NULL));
171
172         gl_print_op_construct (GL_PRINT_OP(op), label);
173
174         return op;
175 }
176
177
178 /*****************************************************************************/
179 /* Construct print op.                                                       */
180 /*****************************************************************************/
181 void
182 gl_print_op_construct (glPrintOp      *op,
183                        glLabel        *label)
184 {
185         glMerge                *merge = NULL;
186         const lglTemplate      *template;
187         const lglTemplateFrame *frame;
188
189         op->priv->label              = label;
190         op->priv->force_outline_flag = FALSE;
191
192         merge    = gl_label_get_merge (label);
193         template = gl_label_get_template (label);
194         frame    = (lglTemplateFrame *)template->frames->data;
195
196         op->priv->merge_flag         = (merge != NULL);
197         op->priv->n_sheets           = 1;
198         op->priv->first              = 1;
199         op->priv->last               = lgl_template_frame_get_n_labels (frame);
200         op->priv->n_copies           = 1;
201
202         set_page_size (op, label);
203
204         gtk_print_operation_set_custom_tab_label ( GTK_PRINT_OPERATION (op),
205                                                    _("Labels"));
206
207         g_signal_connect (G_OBJECT (op), "begin-print",
208                           G_CALLBACK (begin_print_cb), label);
209
210         g_signal_connect (G_OBJECT (op), "draw-page",
211                           G_CALLBACK (draw_page_cb), label);
212 }
213
214
215 /*****************************************************************************/
216 /* Set outline flag/checkbox.                                                */
217 /*****************************************************************************/
218 void
219 gl_print_op_force_outline (glPrintOp *op)
220 {
221         op->priv->force_outline_flag = TRUE;
222 }
223
224
225 /*****************************************************************************/
226 /* Set outline flag/checkbox.                                                */
227 /*****************************************************************************/
228 gboolean
229 gl_print_op_is_outline_forced (glPrintOp *op)
230 {
231         return op->priv->force_outline_flag;
232 }
233
234
235 /*****************************************************************************/
236 /* Set job parameters.                                                       */
237 /*****************************************************************************/
238 void
239 gl_print_op_set_filename (glPrintOp *op,
240                           gchar     *filename)
241 {
242         gtk_print_operation_set_export_filename (GTK_PRINT_OPERATION (op),
243                                                  filename);
244 }
245
246
247 void
248 gl_print_op_set_n_sheets (glPrintOp *op,
249                           gint       n_sheets)
250 {
251         op->priv->n_sheets = n_sheets;
252 }
253
254
255 void
256 gl_print_op_set_n_copies (glPrintOp *op,
257                           gint       n_copies)
258 {
259         op->priv->n_copies = n_copies;
260 }
261
262
263 void
264 gl_print_op_set_first (glPrintOp *op,
265                        gint       first)
266 {
267         op->priv->first = first;
268 }
269
270
271 void
272 gl_print_op_set_last (glPrintOp *op,
273                       gint       last)
274 {
275         op->priv->last = last;
276 }
277
278
279 void
280 gl_print_op_set_collate_flag (glPrintOp *op,
281                               gboolean   collate_flag)
282 {
283         op->priv->collate_flag = collate_flag;
284 }
285
286
287 void
288 gl_print_op_set_outline_flag (glPrintOp *op,
289                               gboolean   outline_flag)
290 {
291         op->priv->outline_flag = outline_flag;
292 }
293
294
295 void
296 gl_print_op_set_reverse_flag (glPrintOp *op,
297                               gboolean   reverse_flag)
298 {
299         op->priv->reverse_flag = reverse_flag;
300 }
301
302
303 void
304 gl_print_op_set_crop_marks_flag (glPrintOp *op,
305                                  gboolean   crop_marks_flag)
306 {
307         op->priv->crop_marks_flag = crop_marks_flag;
308 }
309
310
311 /*****************************************************************************/
312 /* Get job parameters.                                                       */
313 /*****************************************************************************/
314 gchar *
315 gl_print_op_get_filename (glPrintOp *op)
316 {
317         gchar *filename = NULL;
318
319         g_object_get (G_OBJECT (op),
320                       "export_filename", filename,
321                       NULL);
322
323         return filename;
324 }
325
326
327 gint
328 gl_print_op_get_n_sheets (glPrintOp *op)
329 {
330         return op->priv->n_sheets;
331 }
332
333
334 gint
335 gl_print_op_get_n_copies (glPrintOp *op)
336 {
337         return op->priv->n_copies;
338 }
339
340
341 gint
342 gl_print_op_get_first (glPrintOp *op)
343 {
344         return op->priv->first;
345 }
346
347
348 gint
349 gl_print_op_get_last (glPrintOp *op)
350 {
351         return op->priv->last;
352 }
353
354
355 gboolean
356 gl_print_op_get_collate_flag (glPrintOp *op)
357 {
358         return op->priv->collate_flag;
359 }
360
361
362 gboolean
363 gl_print_op_get_outline_flag (glPrintOp *op)
364 {
365         return op->priv->outline_flag;
366 }
367
368
369 gboolean
370 gl_print_op_get_reverse_flag (glPrintOp *op)
371 {
372         return op->priv->reverse_flag;
373 }
374
375
376 gboolean
377 gl_print_op_get_crop_marks_flag (glPrintOp *op)
378 {
379         return op->priv->crop_marks_flag;
380 }
381
382
383 /*--------------------------------------------------------------------------*/
384 /* PRIVATE.  Set page size.                                                 */
385 /*--------------------------------------------------------------------------*/
386 static void
387 set_page_size (glPrintOp  *op,
388                glLabel    *label)
389 {
390         const lglTemplate *template;
391         GtkPaperSize      *psize;
392         GtkPageSetup      *su;
393         lglPaper          *paper;
394
395         gl_debug (DEBUG_PRINT, "begin");
396
397         template = gl_label_get_template (label);
398         paper = lgl_db_lookup_paper_from_id (template->paper_id);
399
400         if (!paper)
401         {
402                 const gchar *name;
403
404                 name = gtk_paper_size_get_default ();
405                 psize = gtk_paper_size_new (name);
406
407                 gl_debug (DEBUG_PRINT, "Using default size = \"%s\"", name);
408         }
409         else if (lgl_db_is_paper_id_other (paper->id))
410         {
411                 psize = gtk_paper_size_new_custom (paper->id,
412                                                    paper->name,
413                                                    template->page_width,
414                                                    template->page_height,
415                                                    GTK_UNIT_POINTS);
416                 gl_debug (DEBUG_PRINT, "Using custom size = %g x %g points",
417                           template->page_width,
418                           template->page_height);
419
420         }
421         else
422         {
423                 psize = gtk_paper_size_new (paper->pwg_size);
424                 gl_debug (DEBUG_PRINT, "Using PWG size \"%s\"", paper->pwg_size);
425         }
426         lgl_paper_free (paper);
427
428         su = gtk_page_setup_new ();
429         gtk_page_setup_set_paper_size (su, psize);
430         gtk_print_operation_set_default_page_setup (GTK_PRINT_OPERATION (op), su);
431         g_object_unref (su);
432
433         gtk_paper_size_free (psize);
434
435         gl_debug (DEBUG_PRINT, "end");
436 }
437
438
439 /*****************************************************************************/
440 /* Get print operation settings.                                             */
441 /*****************************************************************************/
442 glPrintOpSettings *
443 gl_print_op_get_settings (glPrintOp         *print_op)
444 {
445         glPrintOpSettings *settings;
446
447         settings = g_new0 (glPrintOpSettings, 1);
448
449         if ( settings )
450         {
451                 settings->gtk_settings =
452                         gtk_print_operation_get_print_settings (GTK_PRINT_OPERATION (print_op));
453
454                 settings->outline_flag     = print_op->priv->outline_flag;
455                 settings->reverse_flag     = print_op->priv->reverse_flag;
456                 settings->crop_marks_flag  = print_op->priv->crop_marks_flag;
457                 settings->collate_flag     = print_op->priv->collate_flag;
458
459                 settings->first            = print_op->priv->first;
460                 settings->last             = print_op->priv->last;
461                 settings->n_sheets         = print_op->priv->n_sheets;
462                 settings->n_copies         = print_op->priv->n_copies;
463         }
464
465         return settings;
466 }
467
468
469 /*****************************************************************************/
470 /* Set print operation settings.                                             */
471 /*****************************************************************************/
472 void
473 gl_print_op_set_settings (glPrintOp         *print_op,
474                           glPrintOpSettings *settings)
475 {
476
477         if ( settings )
478         {
479                 gtk_print_operation_set_print_settings (GTK_PRINT_OPERATION (print_op),
480                                                         settings->gtk_settings);
481
482                 print_op->priv->outline_flag     = settings->outline_flag;
483                 print_op->priv->reverse_flag     = settings->reverse_flag;
484                 print_op->priv->crop_marks_flag  = settings->crop_marks_flag;
485                 print_op->priv->collate_flag     = settings->collate_flag;
486
487                 print_op->priv->first            = settings->first;
488                 print_op->priv->last             = settings->last;
489                 print_op->priv->n_sheets         = settings->n_sheets;
490                 print_op->priv->n_copies         = settings->n_copies;
491         }
492
493 }
494
495
496 /*****************************************************************************/
497 /* Free print operation settings structure.                                  */
498 /*****************************************************************************/
499 void
500 gl_print_op_free_settings(glPrintOpSettings *settings)
501 {
502         
503         if ( settings )
504         {
505                 if ( settings->gtk_settings )
506                 {
507                         g_object_unref (settings->gtk_settings);
508                 }
509
510                 g_free (settings);
511         }
512 }
513
514
515 /*--------------------------------------------------------------------------*/
516 /* PRIVATE.  "Begin print" callback                                         */
517 /*--------------------------------------------------------------------------*/
518 static void
519 begin_print_cb (GtkPrintOperation *operation,
520                 GtkPrintContext   *context,
521                 gpointer           user_data)
522 {
523         glPrintOp *op = GL_PRINT_OP (operation);
524
525         gtk_print_operation_set_n_pages (operation, op->priv->n_sheets);
526
527 }
528
529
530 /*--------------------------------------------------------------------------*/
531 /* PRIVATE.  "Draw page" callback.                                          */
532 /*--------------------------------------------------------------------------*/
533 static void
534 draw_page_cb (GtkPrintOperation *operation,
535               GtkPrintContext   *context,
536               int                page_nr,
537               gpointer           user_data)
538 {
539         glPrintOp *op = GL_PRINT_OP (operation);
540         cairo_t       *cr;
541
542         cr = gtk_print_context_get_cairo_context (context);
543
544         if (!op->priv->merge_flag)
545         {
546                 gl_print_simple_sheet (op->priv->label,
547                                        cr,
548                                        page_nr,
549                                        op->priv->n_sheets,
550                                        op->priv->first,
551                                        op->priv->last,
552                                        op->priv->outline_flag,
553                                        op->priv->reverse_flag,
554                                        op->priv->crop_marks_flag);
555         }
556         else
557         {
558                 if (op->priv->collate_flag)
559                 {
560                         gl_print_collated_merge_sheet (op->priv->label,
561                                                        cr,
562                                                        page_nr,
563                                                        op->priv->n_copies,
564                                                        op->priv->first,
565                                                        op->priv->outline_flag,
566                                                        op->priv->reverse_flag,
567                                                        op->priv->crop_marks_flag,
568                                                        &op->priv->state);
569                 }
570                 else
571                 {
572                         gl_print_uncollated_merge_sheet (op->priv->label,
573                                                          cr,
574                                                          page_nr,
575                                                          op->priv->n_copies,
576                                                          op->priv->first,
577                                                          op->priv->outline_flag,
578                                                          op->priv->reverse_flag,
579                                                          op->priv->crop_marks_flag,
580                                                          &op->priv->state);
581                 }
582         }
583 }
584
585
586
587
588 /*
589  * Local Variables:       -- emacs
590  * mode: C                -- emacs
591  * c-basic-offset: 8      -- emacs
592  * tab-width: 8           -- emacs
593  * indent-tabs-mode: nil  -- emacs
594  * End:                   -- emacs
595  */