]> git.sur5r.net Git - glabels/blob - src/print-op.c
Organized master branch to be top-level directory for glabels, instead of
[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/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 lglTemplateFrame *frame;
187
188         op->priv->label              = label;
189         op->priv->force_outline_flag = FALSE;
190
191         merge = gl_label_get_merge (label);
192         frame = (lglTemplateFrame *)label->template->frames->data;
193
194         op->priv->merge_flag         = (merge != NULL);
195         op->priv->n_sheets           = 1;
196         op->priv->first              = 1;
197         op->priv->last               = lgl_template_frame_get_n_labels (frame);
198         op->priv->n_copies           = 1;
199
200         set_page_size (op, label);
201
202         gtk_print_operation_set_custom_tab_label ( GTK_PRINT_OPERATION (op),
203                                                    _("Labels"));
204
205         g_signal_connect (G_OBJECT (op), "begin-print",
206                           G_CALLBACK (begin_print_cb), label);
207
208         g_signal_connect (G_OBJECT (op), "draw-page",
209                           G_CALLBACK (draw_page_cb), label);
210 }
211
212
213 /*****************************************************************************/
214 /* Set outline flag/checkbox.                                                */
215 /*****************************************************************************/
216 void
217 gl_print_op_force_outline (glPrintOp *op)
218 {
219         op->priv->force_outline_flag = TRUE;
220 }
221
222
223 /*****************************************************************************/
224 /* Set outline flag/checkbox.                                                */
225 /*****************************************************************************/
226 gboolean
227 gl_print_op_is_outline_forced (glPrintOp *op)
228 {
229         return op->priv->force_outline_flag;
230 }
231
232
233 /*****************************************************************************/
234 /* Set job parameters.                                                       */
235 /*****************************************************************************/
236 void
237 gl_print_op_set_filename (glPrintOp *op,
238                           gchar     *filename)
239 {
240         gtk_print_operation_set_export_filename (GTK_PRINT_OPERATION (op),
241                                                  filename);
242 }
243
244
245 void
246 gl_print_op_set_n_sheets (glPrintOp *op,
247                           gint       n_sheets)
248 {
249         op->priv->n_sheets = n_sheets;
250 }
251
252
253 void
254 gl_print_op_set_n_copies (glPrintOp *op,
255                           gint       n_copies)
256 {
257         op->priv->n_copies = n_copies;
258 }
259
260
261 void
262 gl_print_op_set_first (glPrintOp *op,
263                        gint       first)
264 {
265         op->priv->first = first;
266 }
267
268
269 void
270 gl_print_op_set_last (glPrintOp *op,
271                       gint       last)
272 {
273         op->priv->last = last;
274 }
275
276
277 void
278 gl_print_op_set_collate_flag (glPrintOp *op,
279                               gboolean   collate_flag)
280 {
281         op->priv->collate_flag = collate_flag;
282 }
283
284
285 void
286 gl_print_op_set_outline_flag (glPrintOp *op,
287                               gboolean   outline_flag)
288 {
289         op->priv->outline_flag = outline_flag;
290 }
291
292
293 void
294 gl_print_op_set_reverse_flag (glPrintOp *op,
295                               gboolean   reverse_flag)
296 {
297         op->priv->reverse_flag = reverse_flag;
298 }
299
300
301 void
302 gl_print_op_set_crop_marks_flag (glPrintOp *op,
303                                  gboolean   crop_marks_flag)
304 {
305         op->priv->crop_marks_flag = crop_marks_flag;
306 }
307
308
309 /*****************************************************************************/
310 /* Get job parameters.                                                       */
311 /*****************************************************************************/
312 gchar *
313 gl_print_op_get_filename (glPrintOp *op)
314 {
315         gchar *filename = NULL;
316
317         g_object_get (G_OBJECT (op),
318                       "export_filename", filename,
319                       NULL);
320
321         return filename;
322 }
323
324
325 gint
326 gl_print_op_get_n_sheets (glPrintOp *op)
327 {
328         return op->priv->n_sheets;
329 }
330
331
332 gint
333 gl_print_op_get_n_copies (glPrintOp *op)
334 {
335         return op->priv->n_copies;
336 }
337
338
339 gint
340 gl_print_op_get_first (glPrintOp *op)
341 {
342         return op->priv->first;
343 }
344
345
346 gint
347 gl_print_op_get_last (glPrintOp *op)
348 {
349         return op->priv->last;
350 }
351
352
353 gboolean
354 gl_print_op_get_collate_flag (glPrintOp *op)
355 {
356         return op->priv->collate_flag;
357 }
358
359
360 gboolean
361 gl_print_op_get_outline_flag (glPrintOp *op)
362 {
363         return op->priv->outline_flag;
364 }
365
366
367 gboolean
368 gl_print_op_get_reverse_flag (glPrintOp *op)
369 {
370         return op->priv->reverse_flag;
371 }
372
373
374 gboolean
375 gl_print_op_get_crop_marks_flag (glPrintOp *op)
376 {
377         return op->priv->crop_marks_flag;
378 }
379
380
381 /*--------------------------------------------------------------------------*/
382 /* PRIVATE.  Set page size.                                                 */
383 /*--------------------------------------------------------------------------*/
384 static void
385 set_page_size (glPrintOp  *op,
386                glLabel    *label)
387 {
388         GtkPaperSize *psize;
389         GtkPageSetup *su;
390         lglPaper     *paper;
391
392         gl_debug (DEBUG_PRINT, "begin");
393
394         paper = lgl_db_lookup_paper_from_id (label->template->paper_id);
395
396         if (!paper)
397         {
398                 const gchar *name;
399
400                 name = gtk_paper_size_get_default ();
401                 psize = gtk_paper_size_new (name);
402
403                 gl_debug (DEBUG_PRINT, "Using default size = \"%s\"", name);
404         }
405         else if (lgl_db_is_paper_id_other (paper->id))
406         {
407                 psize = gtk_paper_size_new_custom (paper->id,
408                                                    paper->name,
409                                                    label->template->page_width,
410                                                    label->template->page_height,
411                                                    GTK_UNIT_POINTS);
412                 gl_debug (DEBUG_PRINT, "Using custom size = %g x %g points",
413                           label->template->page_width,
414                           label->template->page_height);
415
416         }
417         else
418         {
419                 psize = gtk_paper_size_new (paper->pwg_size);
420                 gl_debug (DEBUG_PRINT, "Using PWG size \"%s\"", paper->pwg_size);
421         }
422         lgl_paper_free (paper);
423
424         su = gtk_page_setup_new ();
425         gtk_page_setup_set_paper_size (su, psize);
426         gtk_print_operation_set_default_page_setup (GTK_PRINT_OPERATION (op), su);
427         g_object_unref (su);
428
429         gtk_paper_size_free (psize);
430
431         gl_debug (DEBUG_PRINT, "end");
432 }
433
434
435 /*****************************************************************************/
436 /* Get print operation settings.                                             */
437 /*****************************************************************************/
438 glPrintOpSettings *
439 gl_print_op_get_settings (glPrintOp         *print_op)
440 {
441         glPrintOpSettings *settings;
442
443         settings = g_new0 (glPrintOpSettings, 1);
444
445         if ( settings )
446         {
447                 settings->gtk_settings =
448                         gtk_print_operation_get_print_settings (GTK_PRINT_OPERATION (print_op));
449
450                 settings->outline_flag     = print_op->priv->outline_flag;
451                 settings->reverse_flag     = print_op->priv->reverse_flag;
452                 settings->crop_marks_flag  = print_op->priv->crop_marks_flag;
453                 settings->collate_flag     = print_op->priv->collate_flag;
454
455                 settings->first            = print_op->priv->first;
456                 settings->last             = print_op->priv->last;
457                 settings->n_sheets         = print_op->priv->n_sheets;
458                 settings->n_copies         = print_op->priv->n_copies;
459         }
460
461         return settings;
462 }
463
464
465 /*****************************************************************************/
466 /* Set print operation settings.                                             */
467 /*****************************************************************************/
468 void
469 gl_print_op_set_settings (glPrintOp         *print_op,
470                           glPrintOpSettings *settings)
471 {
472
473         if ( settings )
474         {
475                 gtk_print_operation_set_print_settings (GTK_PRINT_OPERATION (print_op),
476                                                         settings->gtk_settings);
477
478                 print_op->priv->outline_flag     = settings->outline_flag;
479                 print_op->priv->reverse_flag     = settings->reverse_flag;
480                 print_op->priv->crop_marks_flag  = settings->crop_marks_flag;
481                 print_op->priv->collate_flag     = settings->collate_flag;
482
483                 print_op->priv->first            = settings->first;
484                 print_op->priv->last             = settings->last;
485                 print_op->priv->n_sheets         = settings->n_sheets;
486                 print_op->priv->n_copies         = settings->n_copies;
487         }
488
489 }
490
491
492 /*****************************************************************************/
493 /* Free print operation settings structure.                                  */
494 /*****************************************************************************/
495 void
496 gl_print_op_free_settings(glPrintOpSettings *settings)
497 {
498         
499         if ( settings )
500         {
501                 if ( settings->gtk_settings )
502                 {
503                         g_object_unref (settings->gtk_settings);
504                 }
505
506                 g_free (settings);
507         }
508 }
509
510
511 /*--------------------------------------------------------------------------*/
512 /* PRIVATE.  "Begin print" callback                                         */
513 /*--------------------------------------------------------------------------*/
514 static void
515 begin_print_cb (GtkPrintOperation *operation,
516                 GtkPrintContext   *context,
517                 gpointer           user_data)
518 {
519         glPrintOp *op = GL_PRINT_OP (operation);
520
521         gtk_print_operation_set_n_pages (operation, op->priv->n_sheets);
522
523 }
524
525
526 /*--------------------------------------------------------------------------*/
527 /* PRIVATE.  "Draw page" callback.                                          */
528 /*--------------------------------------------------------------------------*/
529 static void
530 draw_page_cb (GtkPrintOperation *operation,
531               GtkPrintContext   *context,
532               int                page_nr,
533               gpointer           user_data)
534 {
535         glPrintOp *op = GL_PRINT_OP (operation);
536         cairo_t       *cr;
537
538         cr = gtk_print_context_get_cairo_context (context);
539
540         if (!op->priv->merge_flag)
541         {
542                 gl_print_simple_sheet (op->priv->label,
543                                        cr,
544                                        page_nr,
545                                        op->priv->n_sheets,
546                                        op->priv->first,
547                                        op->priv->last,
548                                        op->priv->outline_flag,
549                                        op->priv->reverse_flag,
550                                        op->priv->crop_marks_flag);
551         }
552         else
553         {
554                 if (op->priv->collate_flag)
555                 {
556                         gl_print_collated_merge_sheet (op->priv->label,
557                                                        cr,
558                                                        page_nr,
559                                                        op->priv->n_copies,
560                                                        op->priv->first,
561                                                        op->priv->outline_flag,
562                                                        op->priv->reverse_flag,
563                                                        op->priv->crop_marks_flag,
564                                                        &op->priv->state);
565                 }
566                 else
567                 {
568                         gl_print_uncollated_merge_sheet (op->priv->label,
569                                                          cr,
570                                                          page_nr,
571                                                          op->priv->n_copies,
572                                                          op->priv->first,
573                                                          op->priv->outline_flag,
574                                                          op->priv->reverse_flag,
575                                                          op->priv->crop_marks_flag,
576                                                          &op->priv->state);
577                 }
578         }
579 }
580
581
582
583
584 /*
585  * Local Variables:       -- emacs
586  * mode: C                -- emacs
587  * c-basic-offset: 8      -- emacs
588  * tab-width: 8           -- emacs
589  * indent-tabs-mode: nil  -- emacs
590  * End:                   -- emacs
591  */