1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * print-op.c: Print operation module
8 * Copyright (C) 2001-2007 Jim Evins <evins@snaught.com>.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <glib/gi18n.h>
29 #include <glade/glade-xml.h>
33 #include <gtk/gtktogglebutton.h>
38 #include "wdgt-print-copies.h"
39 #include "wdgt-print-merge.h"
43 /*===========================================*/
44 /* Private data types */
45 /*===========================================*/
47 struct _glPrintOpPrivate {
53 GtkWidget *simple_frame;
54 GtkWidget *copies_vbox;
57 GtkWidget *merge_frame;
58 GtkWidget *prmerge_vbox;
61 GtkWidget *outline_check;
62 GtkWidget *reverse_check;
63 GtkWidget *crop_marks_check;
65 gboolean force_outline_flag;
69 gboolean outline_flag;
70 gboolean reverse_flag;
71 gboolean crop_marks_flag;
73 gboolean collate_flag;
84 /*===========================================*/
86 /*===========================================*/
88 static GtkPrintOperationClass* parent_class = NULL;
90 /*===========================================*/
91 /* Local function prototypes */
92 /*===========================================*/
94 static void gl_print_op_class_init (glPrintOpClass *klass);
95 static void gl_print_op_init (glPrintOp *op);
96 static void gl_print_op_finalize (GObject *object);
98 static void gl_print_op_construct (glPrintOp *op,
101 static void gl_print_op_construct_batch (glPrintOp *op,
107 gboolean outline_flag,
108 gboolean reverse_flag,
109 gboolean crop_marks_flag);
112 static GObject *create_custom_widget_cb (GtkPrintOperation *operation,
115 static void custom_widget_apply_cb (GtkPrintOperation *operation,
119 static void begin_print_cb (GtkPrintOperation *operation,
120 GtkPrintContext *context,
123 static void draw_page_cb (GtkPrintOperation *operation,
124 GtkPrintContext *context,
131 /*****************************************************************************/
132 /* Boilerplate object stuff. */
133 /*****************************************************************************/
135 gl_print_op_get_type (void)
137 static GType type = 0;
141 static const GTypeInfo info =
143 sizeof (glPrintOpClass),
144 NULL, /* base_init */
145 NULL, /* base_finalize */
146 (GClassInitFunc) gl_print_op_class_init,
147 NULL, /* class_finalize */
148 NULL, /* class_data */
151 (GInstanceInitFunc) gl_print_op_init,
155 type = g_type_register_static (GTK_TYPE_PRINT_OPERATION,
156 "glPrintOp", &info, 0);
163 gl_print_op_class_init (glPrintOpClass *klass)
165 GObjectClass *object_class = G_OBJECT_CLASS (klass);
166 GtkPrintOperationClass *print_class = GTK_PRINT_OPERATION_CLASS (klass);
168 gl_debug (DEBUG_PRINT, "");
170 parent_class = g_type_class_peek_parent (klass);
172 object_class->finalize = gl_print_op_finalize;
176 gl_print_op_init (glPrintOp *op)
178 GtkWidget *pp_button;
180 gl_debug (DEBUG_PRINT, "");
182 gtk_print_operation_set_use_full_page (GTK_PRINT_OPERATION (op), TRUE);
184 gtk_print_operation_set_unit (GTK_PRINT_OPERATION (op), GTK_UNIT_POINTS);
186 op->priv = g_new0 (glPrintOpPrivate, 1);
191 gl_print_op_finalize (GObject *object)
195 gl_debug (DEBUG_PRINT, "");
197 g_return_if_fail (object != NULL);
199 op = GL_PRINT_OP (object);
201 g_return_if_fail (GL_IS_PRINT_OP (op));
202 g_return_if_fail (op->priv != NULL);
204 if (op->priv->label) {
205 g_object_unref (G_OBJECT(op->priv->label));
207 g_free (op->priv->filename);
210 G_OBJECT_CLASS (parent_class)->finalize (object);
215 /*****************************************************************************/
217 /*****************************************************************************/
219 gl_print_op_new (glLabel *label)
223 gl_debug (DEBUG_PRINT, "");
225 op = GL_PRINT_OP (g_object_new (GL_TYPE_PRINT_OP, NULL));
227 gl_print_op_construct (GL_PRINT_OP(op), label);
232 /*--------------------------------------------------------------------------*/
233 /* PRIVATE. Construct op. */
234 /*--------------------------------------------------------------------------*/
236 gl_print_op_construct (glPrintOp *op,
239 op->priv->label = label;
240 op->priv->force_outline_flag = FALSE;
242 gtk_print_operation_set_custom_tab_label ( GTK_PRINT_OPERATION (op),
245 g_signal_connect (G_OBJECT (op), "create-custom-widget",
246 G_CALLBACK (create_custom_widget_cb), label);
248 g_signal_connect (G_OBJECT (op), "custom-widget-apply",
249 G_CALLBACK (custom_widget_apply_cb), label);
251 g_signal_connect (G_OBJECT (op), "begin-print",
252 G_CALLBACK (begin_print_cb), label);
254 g_signal_connect (G_OBJECT (op), "draw-page",
255 G_CALLBACK (draw_page_cb), label);
258 /*****************************************************************************/
259 /* NEW batch print operation. */
260 /*****************************************************************************/
262 gl_print_op_new_batch (glLabel *label,
267 gboolean outline_flag,
268 gboolean reverse_flag,
269 gboolean crop_marks_flag)
273 gl_debug (DEBUG_PRINT, "");
275 op = GL_PRINT_OP (g_object_new (GL_TYPE_PRINT_OP, NULL));
277 gl_print_op_construct_batch (GL_PRINT_OP(op),
290 /*--------------------------------------------------------------------------*/
291 /* PRIVATE. Construct op. */
292 /*--------------------------------------------------------------------------*/
294 gl_print_op_construct_batch (glPrintOp *op,
300 gboolean outline_flag,
301 gboolean reverse_flag,
302 gboolean crop_marks_flag)
305 glMerge *merge = NULL;
306 glTemplate *template = NULL;
307 const glTemplateLabelType *label_type = NULL;
309 op->priv->label = label;
310 op->priv->force_outline_flag = FALSE;
311 op->priv->filename = g_strdup (filename);
312 op->priv->n_sheets = n_sheets;
313 op->priv->n_copies = n_copies;
314 op->priv->first = first;
315 op->priv->outline_flag = outline_flag;
316 op->priv->reverse_flag = reverse_flag;
317 op->priv->crop_marks_flag = crop_marks_flag;
319 merge = gl_label_get_merge (label);
321 template = gl_label_get_template (label);
322 label_type = gl_template_get_first_label_type (template);
325 op->priv->merge_flag = FALSE;
327 op->priv->last = gl_template_get_n_labels (label_type);
332 op->priv->merge_flag = TRUE;
335 ceil ((double)(first-1 + n_copies * gl_merge_get_record_count(merge))
336 / gl_template_get_n_labels (label_type));;
338 g_object_unref (G_OBJECT(merge));
341 gl_template_free (template);
343 gtk_print_operation_set_export_filename (GTK_PRINT_OPERATION (op),
346 g_signal_connect (G_OBJECT (op), "begin-print",
347 G_CALLBACK (begin_print_cb), label);
349 g_signal_connect (G_OBJECT (op), "draw-page",
350 G_CALLBACK (draw_page_cb), label);
353 /*--------------------------------------------------------------------------*/
354 /* PRIVATE. "Create custom widget" callback */
355 /*--------------------------------------------------------------------------*/
357 create_custom_widget_cb (GtkPrintOperation *operation,
360 glPrintOp *op = GL_PRINT_OP (operation);
361 glLabel *label = GL_LABEL (user_data);
365 glMerge *merge = NULL;
367 op->priv->gui = glade_xml_new (GLABELS_GLADE_DIR "print-custom-widget.glade",
368 "print_custom_widget_vbox",
371 if (!op->priv->gui) {
372 g_warning ("Could not open print-op.glade, reinstall glabels!");
376 vbox = glade_xml_get_widget (op->priv->gui, "print_custom_widget_vbox");
378 /* ----- Simple print control ----- */
379 op->priv->simple_frame = glade_xml_get_widget (op->priv->gui,
381 op->priv->copies_vbox = glade_xml_get_widget (op->priv->gui,
383 op->priv->copies = gl_wdgt_print_copies_new (label);
384 gtk_box_pack_start (GTK_BOX(op->priv->copies_vbox),
385 op->priv->copies, FALSE, FALSE, 0);
387 /* ----- Merge print control ----- */
388 op->priv->merge_frame = glade_xml_get_widget (op->priv->gui,
390 op->priv->prmerge_vbox = glade_xml_get_widget (op->priv->gui,
392 op->priv->prmerge = gl_wdgt_print_merge_new (label);
393 gtk_box_pack_start (GTK_BOX(op->priv->prmerge_vbox),
394 op->priv->prmerge, FALSE, FALSE, 0);
396 /* ----- Options ----------------- */
397 op->priv->outline_check = glade_xml_get_widget (op->priv->gui,
399 op->priv->reverse_check = glade_xml_get_widget (op->priv->gui,
401 op->priv->crop_marks_check = glade_xml_get_widget (op->priv->gui,
404 /* ---- Activate either simple or merge print control widgets. ---- */
405 merge = gl_label_get_merge (op->priv->label);
408 gtk_widget_show_all (op->priv->simple_frame);
409 gtk_widget_hide_all (op->priv->merge_frame);
413 gint n_records = gl_merge_get_record_count( merge );
414 gl_wdgt_print_merge_set_copies (GL_WDGT_PRINT_MERGE(op->priv->prmerge),
415 1, 1, n_records, FALSE);
416 g_object_unref (G_OBJECT(merge));
418 gtk_widget_hide_all (op->priv->simple_frame);
419 gtk_widget_show_all (op->priv->merge_frame);
422 /* --- Do we need to force the outline flag --- */
423 if (op->priv->force_outline_flag)
425 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(op->priv->outline_check),
428 gtk_widget_set_sensitive (op->priv->outline_check, FALSE);
429 gtk_widget_set_sensitive (op->priv->reverse_check, FALSE);
430 gtk_widget_set_sensitive (op->priv->crop_marks_check, FALSE);
433 return G_OBJECT (vbox);
436 /*--------------------------------------------------------------------------*/
437 /* PRIVATE. "Custom widget apply" callback */
438 /*--------------------------------------------------------------------------*/
440 custom_widget_apply_cb (GtkPrintOperation *operation,
444 glPrintOp *op = GL_PRINT_OP (operation);
445 glMerge *merge = NULL;
448 op->priv->outline_flag =
449 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
450 (op->priv->outline_check));
451 op->priv->reverse_flag =
452 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
453 (op->priv->reverse_check));
454 op->priv->crop_marks_flag =
455 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
456 (op->priv->crop_marks_check));
458 merge = gl_label_get_merge (op->priv->label);
463 op->priv->merge_flag = FALSE;
464 gl_wdgt_print_copies_get_range (GL_WDGT_PRINT_COPIES (op->priv->copies),
472 op->priv->merge_flag = TRUE;
473 gl_wdgt_print_merge_get_copies (GL_WDGT_PRINT_MERGE (op->priv->prmerge),
476 &op->priv->collate_flag,
477 &op->priv->n_sheets);
478 g_object_unref (G_OBJECT(merge));
483 /*--------------------------------------------------------------------------*/
484 /* PRIVATE. "Begin print" callback */
485 /*--------------------------------------------------------------------------*/
487 begin_print_cb (GtkPrintOperation *operation,
488 GtkPrintContext *context,
491 glPrintOp *op = GL_PRINT_OP (operation);
493 gtk_print_operation_set_n_pages (operation, op->priv->n_sheets);
497 /*--------------------------------------------------------------------------*/
498 /* PRIVATE. "Draw page" callback. */
499 /*--------------------------------------------------------------------------*/
501 draw_page_cb (GtkPrintOperation *operation,
502 GtkPrintContext *context,
506 glPrintOp *op = GL_PRINT_OP (operation);
509 cr = gtk_print_context_get_cairo_context (context);
511 if (!op->priv->merge_flag)
513 gl_print_simple_sheet (op->priv->label,
519 op->priv->outline_flag,
520 op->priv->reverse_flag,
521 op->priv->crop_marks_flag);
525 if (op->priv->collate_flag)
527 gl_print_collated_merge_sheet (op->priv->label,
532 op->priv->outline_flag,
533 op->priv->reverse_flag,
534 op->priv->crop_marks_flag,
539 gl_print_uncollated_merge_sheet (op->priv->label,
544 op->priv->outline_flag,
545 op->priv->reverse_flag,
546 op->priv->crop_marks_flag,
553 /*****************************************************************************/
554 /* Set outline flag/checkbox. */
555 /*****************************************************************************/
557 gl_print_op_force_outline_flag (glPrintOp *op)
559 op->priv->force_outline_flag = TRUE;