]> git.sur5r.net Git - glabels/blob - glabels2/src/print-dialog.c
Initial revision
[glabels] / glabels2 / src / print-dialog.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  print.c:  Print module
5  *
6  *  Copyright (C) 2001  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22 #include <config.h>
23
24 #include <math.h>
25 #include <time.h>
26 #include <ctype.h>
27 #include <gtk/gtk.h>
28 #include <libgnomeprint/gnome-print-paper.h>
29 #include <libgnomeprintui/gnome-printer-dialog.h>
30 #include <libgnomeprintui/gnome-print-dialog.h>
31 #include <libgnomeprint/gnome-print-master.h>
32 #include <libgnomeprintui/gnome-print-master-preview.h>
33
34 #include "print-dialog.h"
35 #include "print.h"
36 #include "label.h"
37 #include "bc.h"
38 #include "template.h"
39 #include "mdi.h"
40 #include "glabels.h"
41
42 #include "wdgt-print-copies.h"
43 #include "wdgt-print-merge.h"
44
45 #include "debug.h"
46
47 #define RED(x)   ( (((x)>>24) & 0xff) / 255.0 )
48 #define GREEN(x) ( (((x)>>16) & 0xff) / 255.0 )
49 #define BLUE(x)  ( (((x)>>8)  & 0xff) / 255.0 )
50 #define ALPHA(x) ( ( (x)      & 0xff) / 255.0 )
51
52 /*===========================================*/
53 /* Private types.                            */
54 /*===========================================*/
55
56 /*===========================================*/
57 /* Private globals.                          */
58 /*===========================================*/
59
60 /* remember state of dialog. */
61 static gboolean outline_flag = FALSE;
62 static gboolean reverse_flag = FALSE;
63 static gboolean collate_flag = FALSE;
64 static gint first = 1, last = 1, n_sheets = 0, n_copies = 1;
65
66 /*===========================================*/
67 /* Private function prototypes.              */
68 /*===========================================*/
69 static GtkWidget *job_page_new     (GtkWidget *dlg, glLabel *label);
70 static GtkWidget *printer_page_new (GtkWidget *dlg, glLabel *label);
71
72 static void print_response (GtkDialog *dlg,
73                             gint      response,
74                             glLabel   *label);
75
76 static void print_sheets (GnomePrintConfig *config, glLabel * label,
77                           gboolean preview_flag,
78                           gint n_sheets, gint first, gint last,
79                           gboolean outline_flag, gboolean reverse_flag);
80
81 static void print_sheets_merge (GnomePrintConfig *config, glLabel * label,
82                                 GList *record_list,
83                                 gboolean preview_flag,
84                                 gint n_copies, gint first,
85                                 gboolean collate_flag,
86                                 gboolean outline_flag, gboolean reverse_flag);
87
88 \f
89 /*****************************************************************************/
90 /* "Print" dialog.                                                           */
91 /*****************************************************************************/
92 void
93 gl_print_dialog (glLabel *label, BonoboWindow *win)
94 {
95         GtkWidget *dlg;
96         GtkWidget *pp_button, *notebook, *page;
97
98         g_return_if_fail (label != NULL);
99         g_return_if_fail (win != NULL);
100
101         /* ----- Contstruct basic print dialog with notebook ----- */
102         dlg = gtk_dialog_new_with_buttons (_("Print"), GTK_WINDOW(win),
103                                            GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
104                                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
105                                            GTK_STOCK_PRINT, GNOME_PRINT_DIALOG_RESPONSE_PRINT,
106
107                                            NULL);
108         pp_button = gtk_dialog_add_button (GTK_DIALOG (dlg),
109                                            GTK_STOCK_PRINT_PREVIEW, 
110                                            GNOME_PRINT_DIALOG_RESPONSE_PREVIEW);
111         gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (GTK_DIALOG (dlg)->action_area), 
112                                             pp_button, TRUE);
113         gtk_dialog_set_default_response (GTK_DIALOG (dlg),
114                                          GNOME_PRINT_DIALOG_RESPONSE_PRINT);
115         notebook = gtk_notebook_new ();
116         gtk_container_set_border_width (GTK_CONTAINER (notebook), 4);
117         gtk_widget_show (notebook);
118         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dlg)->vbox), notebook);
119
120
121         /* ----- Create Job notebook page ----- */
122         page = job_page_new (dlg, label);
123         gtk_notebook_append_page (GTK_NOTEBOOK(notebook), page,
124                                   gtk_label_new_with_mnemonic (_("_Job")));
125
126         /* ----- Create Printer notebook page ----- */
127         page = printer_page_new (dlg, label);
128         gtk_notebook_append_page (GTK_NOTEBOOK(notebook), page,
129                                   gtk_label_new_with_mnemonic (_("P_rinter")));
130
131         g_signal_connect (G_OBJECT(dlg), "response",
132                           G_CALLBACK (print_response), label);
133
134         gtk_widget_show_all (GTK_WIDGET (dlg));
135 }
136
137 /*---------------------------------------------------------------------------*/
138 /* PRIVATE.  Create "Job" page.                                              */
139 /*---------------------------------------------------------------------------*/
140 static GtkWidget *
141 job_page_new (GtkWidget *dlg,
142               glLabel   *label)
143 {
144         GtkWidget *vbox;
145         glMerge *merge;
146         GtkWidget *wframe;
147         GtkWidget *copies = NULL, *prmerge = NULL;
148         GtkWidget *wvbox, *outline_check, *reverse_check;
149         GList *record_list;
150         gint n_records;
151
152         vbox = gtk_vbox_new (FALSE, 0);
153
154         merge = gl_label_get_merge (label);
155         if (merge->type == GL_MERGE_NONE) {
156
157                 /* ----------- Add simple-copies widget ------------ */
158                 wframe = gtk_frame_new (_("Copies"));
159                 gtk_box_pack_start (GTK_BOX (vbox), wframe, FALSE, FALSE, 0);
160
161                 copies = gl_wdgt_print_copies_new (label);
162                 gtk_container_set_border_width (GTK_CONTAINER (copies),
163                                                 GNOME_PAD_SMALL);
164                 gtk_container_add (GTK_CONTAINER (wframe), copies);
165
166                 if (n_sheets) {
167                         gl_wdgt_print_copies_set_range (GL_WDGT_PRINT_COPIES (copies),
168                                                         n_sheets, first, last);
169                 }
170
171         } else {
172
173                 /* ------- Otherwise add merge control widget ------------ */
174                 wframe = gtk_frame_new (_("Document merge control"));
175                 gtk_box_pack_start (GTK_BOX (vbox), wframe, FALSE, FALSE, 0);
176
177                 prmerge = gl_wdgt_print_merge_new (label);
178                 gtk_container_set_border_width (GTK_CONTAINER (prmerge),
179                                                 GNOME_PAD_SMALL);
180                 gtk_container_add (GTK_CONTAINER (wframe), prmerge);
181
182                 record_list = gl_merge_read_data (merge->type,
183                                                   merge->field_defs,
184                                                   merge->src);
185                 n_records = gl_merge_count_records( record_list );
186                 gl_wdgt_print_merge_set_copies (GL_WDGT_PRINT_MERGE(prmerge),
187                                            n_copies, first, n_records,
188                                            collate_flag);
189         }
190         gtk_widget_show_all (wframe);
191         g_object_set_data (G_OBJECT(dlg), "copies", copies);
192         g_object_set_data (G_OBJECT(dlg), "prmerge", prmerge);
193
194         /* ----------- Add custom print options area ------------ */
195         wframe = gtk_frame_new (_("Options"));
196         gtk_box_pack_start (GTK_BOX (vbox), wframe, FALSE, FALSE, 0);
197         wvbox = gtk_vbox_new (FALSE, GNOME_PAD);
198         gtk_container_set_border_width (GTK_CONTAINER (wvbox),
199                                         GNOME_PAD_SMALL);
200         gtk_container_add (GTK_CONTAINER (wframe), wvbox);
201
202         /* add Outline check button */
203         outline_check =
204             gtk_check_button_new_with_label (
205                     _("print outlines (to test printer alignment)"));
206         gtk_box_pack_start (GTK_BOX (wvbox), outline_check, FALSE, FALSE, 0);
207         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (outline_check),
208                                       outline_flag);
209         g_object_set_data (G_OBJECT(dlg), "outline_check", outline_check);
210
211         /* add Reverse check button */
212         reverse_check =
213             gtk_check_button_new_with_label (
214                     _("print in reverse (i.e. a mirror image)"));
215         gtk_box_pack_start (GTK_BOX (wvbox), reverse_check, FALSE, FALSE, 0);
216         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (reverse_check),
217                                       reverse_flag);
218         g_object_set_data (G_OBJECT(dlg), "reverse_check", reverse_check);
219
220         gtk_widget_show_all (wframe);
221
222         return vbox;
223 }
224
225 /*---------------------------------------------------------------------------*/
226 /* PRIVATE.  Create "Printer" page.                                          */
227 /*---------------------------------------------------------------------------*/
228 static GtkWidget *
229 printer_page_new (GtkWidget *dlg,
230                   glLabel   *label)
231 {
232         GtkWidget *vbox;
233         GtkWidget *printer_select;
234
235         vbox = gtk_vbox_new (FALSE, 0);
236
237         printer_select =
238                 gnome_printer_selection_new (gnome_print_config_default ());
239         gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
240         gtk_widget_show (printer_select);
241         gtk_box_pack_start (GTK_BOX(vbox), printer_select, FALSE, FALSE, 0);
242
243         g_object_set_data (G_OBJECT(dlg), "printer_select", printer_select);
244
245         return vbox;
246 }
247
248 /*---------------------------------------------------------------------------*/
249 /* PRIVATE.  Print "response" callback.                                      */
250 /*---------------------------------------------------------------------------*/
251 static void
252 print_response (GtkDialog *dlg,
253                 gint      response,
254                 glLabel   *label)
255 {
256         GtkWidget *copies, *prmerge;
257         GtkWidget *outline_check, *reverse_check;
258         GtkWidget *printer_select;
259         GnomePrintConfig *config;
260         glMerge *merge;
261         GList *record_list;
262
263         switch (response) {
264
265         case GNOME_PRINT_DIALOG_RESPONSE_PRINT:
266         case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW:
267                 copies         = g_object_get_data (G_OBJECT(dlg), "copies");
268                 prmerge        = g_object_get_data (G_OBJECT(dlg), "prmerge");
269                 outline_check  = g_object_get_data (G_OBJECT(dlg),
270                                                     "outline_check");
271                 reverse_check  = g_object_get_data (G_OBJECT(dlg),
272                                                     "reverse_check");
273                 printer_select = g_object_get_data (G_OBJECT(dlg),
274                                                     "printer_select");
275
276                 config = gnome_printer_selection_get_config (GNOME_PRINTER_SELECTION(printer_select));
277
278                 outline_flag =
279                         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
280                                                       (outline_check));
281                 reverse_flag =
282                         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
283                                                       (reverse_check));
284
285                 merge = gl_label_get_merge (label);
286
287                 if (merge->type == GL_MERGE_NONE) {
288
289                         gl_wdgt_print_copies_get_range (GL_WDGT_PRINT_COPIES (copies),
290                                                         &n_sheets, &first, &last);
291                         print_sheets (config, label,
292                                       (response == GNOME_PRINT_DIALOG_RESPONSE_PREVIEW),
293                                       n_sheets, first, last,
294                                       outline_flag, reverse_flag);
295
296                 } else {
297
298                         record_list = gl_merge_read_data (merge->type,
299                                                           merge->field_defs,
300                                                           merge->src);
301                         gl_wdgt_print_merge_get_copies (GL_WDGT_PRINT_MERGE (prmerge),
302                                                         &n_copies, &first,
303                                                         &collate_flag);
304                         print_sheets_merge (config, label, record_list,
305                                             (response == GNOME_PRINT_DIALOG_RESPONSE_PREVIEW),
306                                             n_copies, first,
307                                             collate_flag,
308                                             outline_flag,
309                                             reverse_flag);
310                 }
311
312                 gl_merge_free (&merge);
313                 break;
314
315         default:
316                 break;
317
318         }
319
320         gtk_widget_destroy (GTK_WIDGET (dlg));
321 }
322
323 /*---------------------------------------------------------------------------*/
324 /* PRIVATE.  print the sheets                                                */
325 /*---------------------------------------------------------------------------*/
326 static void
327 print_sheets (GnomePrintConfig *config,
328               glLabel * label,
329               gboolean preview_flag,
330               gint n_sheets,
331               gint first,
332               gint last,
333               gboolean outline_flag,
334               gboolean reverse_flag)
335 {
336         GnomePrintMaster *master;
337
338         master = gnome_print_master_new_from_config (config);
339         gl_print_simple (master, label, n_sheets, first, last,
340                          outline_flag, reverse_flag);
341         gnome_print_master_close (master);
342
343         if (preview_flag) {
344                 GtkWidget *preview_widget =
345                     gnome_print_master_preview_new (master, _("Print preview"));
346                 gtk_widget_show (GTK_WIDGET (preview_widget));
347         } else {
348                 gnome_print_master_print (master);
349         }
350
351         g_object_unref (G_OBJECT (master));
352 }
353
354 /*---------------------------------------------------------------------------*/
355 /* PRIVATE.  print the sheets with merge data                                */
356 /*---------------------------------------------------------------------------*/
357 static void
358 print_sheets_merge (GnomePrintConfig *config,
359                     glLabel * label,
360                     GList * record_list,
361                     gboolean preview_flag,
362                     gint n_copies,
363                     gint first,
364                     gboolean collate_flag,
365                     gboolean outline_flag,
366                     gboolean reverse_flag)
367 {
368         GnomePrintMaster *master;
369
370         master = gnome_print_master_new_from_config (config);
371         if ( collate_flag ) {
372                 gl_print_merge_collated (master, label, record_list,
373                                          n_copies, first,
374                                          outline_flag, reverse_flag);
375         } else {
376                 gl_print_merge_uncollated (master, label, record_list,
377                                            n_copies, first,
378                                            outline_flag, reverse_flag);
379         }
380         gnome_print_master_close (master);
381
382         if (preview_flag) {
383                 GtkWidget *preview_widget =
384                     gnome_print_master_preview_new (master, _("Print preview"));
385                 gtk_widget_show (GTK_WIDGET (preview_widget));
386         } else {
387                 gnome_print_master_print (master);
388         }
389
390         g_object_unref (G_OBJECT (master));
391 }
392