]> git.sur5r.net Git - glabels/blob - glabels2/src/wdgt-print-copies.c
75dc8cdb1f345071ccaa9a47a655ffea7a09c8fd
[glabels] / glabels2 / src / wdgt-print-copies.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  wdgt_print_copies.c:  custom print copies widget 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
23 #include <config.h>
24
25 #include "wdgt-print-copies.h"
26 #include "wdgt-mini-preview.h"
27 #include "marshal.h"
28
29 #include "debug.h"
30
31 #define WDGT_MINI_PREVIEW_HEIGHT 175
32 #define WDGT_MINI_PREVIEW_WIDTH  150
33
34 /*===========================================*/
35 /* Private globals                           */
36 /*===========================================*/
37
38 static GObjectClass *parent_class;
39
40 /*===========================================*/
41 /* Local function prototypes                 */
42 /*===========================================*/
43
44 static void gl_wdgt_print_copies_class_init    (glWdgtPrintCopiesClass * class);
45 static void gl_wdgt_print_copies_instance_init (glWdgtPrintCopies * copies);
46 static void gl_wdgt_print_copies_finalize      (GObject * object);
47
48 static void gl_wdgt_print_copies_construct     (glWdgtPrintCopies * copies,
49                                                 glLabel * label);
50
51 static void sheets_radio_cb                    (GtkToggleButton * togglebutton,
52                                                 gpointer user_data);
53 static void first_spin_cb                      (GtkSpinButton * spinbutton,
54                                                 gpointer user_data);
55 static void last_spin_cb                       (GtkSpinButton * spinbutton,
56                                                 gpointer user_data);
57
58 static void
59 preview_pressed (glWdgtMiniPreview *mini_preview,
60                  gint first, gint last, gpointer user_data);
61
62 \f
63 /****************************************************************************/
64 /* Boilerplate Object stuff.                                                */
65 /****************************************************************************/
66 guint
67 gl_wdgt_print_copies_get_type (void)
68 {
69         static guint wdgt_print_copies_type = 0;
70
71         if (!wdgt_print_copies_type) {
72                 GTypeInfo wdgt_print_copies_info = {
73                         sizeof (glWdgtPrintCopiesClass),
74                         NULL,
75                         NULL,
76                         (GClassInitFunc) gl_wdgt_print_copies_class_init,
77                         NULL,
78                         NULL,
79                         sizeof (glWdgtPrintCopies),
80                         0,
81                         (GInstanceInitFunc) gl_wdgt_print_copies_instance_init,
82                 };
83
84                 wdgt_print_copies_type =
85                         g_type_register_static (gtk_hbox_get_type (),
86                                                 "glWdgtPrintCopies",
87                                                 &wdgt_print_copies_info, 0);
88         }
89
90         return wdgt_print_copies_type;
91 }
92
93 static void
94 gl_wdgt_print_copies_class_init (glWdgtPrintCopiesClass * class)
95 {
96         GObjectClass *object_class;
97
98         object_class = (GObjectClass *) class;
99
100         parent_class = gtk_type_class (gtk_hbox_get_type ());
101
102         object_class->finalize = gl_wdgt_print_copies_finalize;
103 }
104
105 static void
106 gl_wdgt_print_copies_instance_init (glWdgtPrintCopies * copies)
107 {
108         copies->labels_per_sheet = 0;
109
110         copies->mini_preview = NULL;
111
112         copies->sheets_radio = NULL;
113         copies->sheets_spin = NULL;
114
115         copies->labels_radio = NULL;
116         copies->first_spin = NULL;
117         copies->last_spin = NULL;
118 }
119
120 static void
121 gl_wdgt_print_copies_finalize (GObject * object)
122 {
123         glWdgtPrintCopies *copies;
124         glWdgtPrintCopiesClass *class;
125
126         g_return_if_fail (object != NULL);
127         g_return_if_fail (GL_IS_WDGT_PRINT_COPIES (object));
128
129         copies = GL_WDGT_PRINT_COPIES (object);
130
131         G_OBJECT_CLASS (parent_class)->finalize (object);
132 }
133
134 GtkWidget *
135 gl_wdgt_print_copies_new (glLabel * label)
136 {
137         glWdgtPrintCopies *copies;
138
139         copies = g_object_new (gl_wdgt_print_copies_get_type (), NULL);
140
141         gl_wdgt_print_copies_construct (copies, label);
142
143         return GTK_WIDGET (copies);
144 }
145
146 /*--------------------------------------------------------------------------*/
147 /* Construct composite widget.                                              */
148 /*--------------------------------------------------------------------------*/
149 static void
150 gl_wdgt_print_copies_construct (glWdgtPrintCopies * copies,
151                                 glLabel * label)
152 {
153         GtkWidget *whbox, *wvbox, *whbox1;
154         GSList *radio_group = NULL;
155         GtkObject *adjust;
156         const glTemplate *template;
157
158         whbox = GTK_WIDGET (copies);
159
160         template = gl_label_get_template (label);
161         copies->labels_per_sheet = template->nx * template->ny;
162
163         /* mini_preview canvas */
164         copies->mini_preview = gl_wdgt_mini_preview_new (WDGT_MINI_PREVIEW_HEIGHT,
165                                                     WDGT_MINI_PREVIEW_WIDTH);
166         gl_wdgt_mini_preview_set_label (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
167                                    template->name->data);
168         gtk_box_pack_start (GTK_BOX (whbox), copies->mini_preview,
169                             TRUE, TRUE, GNOME_PAD);
170
171         wvbox = gtk_vbox_new (FALSE, GNOME_PAD);
172         gtk_box_pack_start (GTK_BOX (whbox), wvbox, TRUE, TRUE, GNOME_PAD);
173
174         /* Sheet controls */
175         whbox1 = gtk_hbox_new (FALSE, GNOME_PAD);
176         gtk_box_pack_start (GTK_BOX (wvbox), whbox1, FALSE, FALSE, GNOME_PAD);
177         copies->sheets_radio =
178             gtk_radio_button_new_with_label (radio_group, _("Sheets:"));
179         gtk_box_pack_start (GTK_BOX (whbox1), copies->sheets_radio,
180                             FALSE, FALSE, 0);
181         adjust = gtk_adjustment_new (1, 1.0, 10.0, 1.0, 10.0, 10.0);
182         copies->sheets_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
183                                                    1.0, 0);
184         gtk_box_pack_start (GTK_BOX (whbox1), copies->sheets_spin,
185                             FALSE, FALSE, 0);
186         gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
187                                          1, copies->labels_per_sheet);
188
189         /* Label controls */
190         whbox1 = gtk_hbox_new (FALSE, GNOME_PAD);
191         gtk_box_pack_start (GTK_BOX (wvbox), whbox1, FALSE, FALSE, GNOME_PAD);
192         radio_group =
193             gtk_radio_button_get_group (GTK_RADIO_BUTTON (copies->sheets_radio));
194         copies->labels_radio =
195             gtk_radio_button_new_with_label (radio_group, _("Labels"));
196         gtk_box_pack_start (GTK_BOX (whbox1), copies->labels_radio,
197                             FALSE, FALSE, 0);
198         gtk_box_pack_start (GTK_BOX (whbox1), gtk_label_new (_("from:")),
199                             FALSE, FALSE, 0);
200         adjust = gtk_adjustment_new (1, 1.0, copies->labels_per_sheet,
201                                      1.0, 10.0, 10.0);
202         copies->first_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
203                                                   1.0, 0);
204         gtk_box_pack_start (GTK_BOX (whbox1), copies->first_spin,
205                             FALSE, FALSE, 0);
206         gtk_box_pack_start (GTK_BOX (whbox1), gtk_label_new (_("to:")),
207                             FALSE, FALSE, 0);
208         adjust = gtk_adjustment_new (copies->labels_per_sheet,
209                                      1.0, copies->labels_per_sheet,
210                                      1.0, 10.0, 10.0);
211         copies->last_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
212                                                  1.0, 0);
213         gtk_box_pack_start (GTK_BOX (whbox1), copies->last_spin,
214                             FALSE, FALSE, 0);
215         gtk_widget_set_sensitive (copies->first_spin, FALSE);
216         gtk_widget_set_sensitive (copies->last_spin, FALSE);
217
218         /* Connect signals to controls */
219         g_signal_connect (G_OBJECT (copies->mini_preview), "pressed",
220                           G_CALLBACK (preview_pressed), copies);
221         g_signal_connect (G_OBJECT (copies->sheets_radio), "toggled",
222                           G_CALLBACK (sheets_radio_cb), copies);
223         g_signal_connect (G_OBJECT (copies->first_spin), "changed",
224                           G_CALLBACK (first_spin_cb), copies);
225         g_signal_connect (G_OBJECT (copies->last_spin), "changed",
226                           G_CALLBACK (last_spin_cb), copies);
227 }
228
229 /*--------------------------------------------------------------------------*/
230 /* PRIVATE.  Enable/Disable appropriate controls due to radio button toggle.*/
231 /*--------------------------------------------------------------------------*/
232 static void
233 sheets_radio_cb (GtkToggleButton * togglebutton,
234                  gpointer user_data)
235 {
236         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
237         gint first, last;
238
239         if (gtk_toggle_button_get_active (togglebutton)) {
240
241                 gtk_widget_set_sensitive (copies->sheets_spin, TRUE);
242                 gtk_widget_set_sensitive (copies->first_spin, FALSE);
243                 gtk_widget_set_sensitive (copies->last_spin, FALSE);
244
245                 gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
246                                         1, copies->labels_per_sheet);
247
248         } else {
249
250                 gtk_widget_set_sensitive (copies->sheets_spin, FALSE);
251                 gtk_widget_set_sensitive (copies->first_spin, TRUE);
252                 gtk_widget_set_sensitive (copies->last_spin, TRUE);
253
254                 first =
255                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
256                                                       (copies->first_spin));
257                 last =
258                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
259                                                       (copies->last_spin));
260                 gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
261                                                  first, last);
262
263         }
264 }
265
266 /*--------------------------------------------------------------------------*/
267 /* PRIVATE.  modify widget due to change of first spin button               */
268 /*--------------------------------------------------------------------------*/
269 static void
270 first_spin_cb (GtkSpinButton * spinbutton,
271                gpointer user_data)
272 {
273         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
274         gint first, last;
275
276         first =
277             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
278                                               (copies->first_spin));
279         last =
280             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
281                                               (copies->last_spin));
282
283         gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (copies->last_spin))->
284             lower = first;
285
286         gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
287                                          first, last);
288 }
289
290 /*--------------------------------------------------------------------------*/
291 /* PRIVATE.  modify widget due to change of last spin button                */
292 /*--------------------------------------------------------------------------*/
293 static void
294 last_spin_cb (GtkSpinButton * spinbutton,
295               gpointer user_data)
296 {
297         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
298         gint first, last;
299
300         first =
301             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
302                                               (copies->first_spin));
303         last =
304             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
305                                               (copies->last_spin));
306
307         gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (copies->first_spin))->
308             upper = last;
309
310         gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
311                                          first, last);
312 }
313
314 /*--------------------------------------------------------------------------*/
315 /* PRIVATE.  Canvas event handler, select first and last items.             */
316 /*--------------------------------------------------------------------------*/
317 static void
318 preview_pressed (glWdgtMiniPreview *mini_preview,
319                  gint first,
320                  gint last,
321                  gpointer user_data)
322 {
323         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
324
325         gl_wdgt_print_copies_set_range (copies, 1, first, last);
326 }
327
328 /****************************************************************************/
329 /* query selected range of labels within sheet or number of sheets.         */
330 /****************************************************************************/
331 void
332 gl_wdgt_print_copies_get_range (glWdgtPrintCopies * copies,
333                                 gint * n_sheets,
334                                 gint * first_label,
335                                 gint * last_label)
336 {
337         gboolean sheets_active;
338
339         sheets_active =
340             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
341                                           (copies->sheets_radio));
342
343         if (sheets_active) {
344                 *n_sheets =
345                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
346                                                       (copies->sheets_spin));
347                 *first_label = 1;
348                 *last_label = copies->labels_per_sheet;
349         } else {
350                 *n_sheets = 1;
351                 *first_label =
352                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
353                                                       (copies->first_spin));
354                 *last_label =
355                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
356                                                       (copies->last_spin));
357         }
358 }
359
360 /****************************************************************************/
361 /* set range of labels within sheet or number of sheets                     */
362 /****************************************************************************/
363 void
364 gl_wdgt_print_copies_set_range (glWdgtPrintCopies * copies,
365                                 gint n_sheets,
366                                 gint first_label,
367                                 gint last_label)
368 {
369         gint old_first_label;
370
371         old_first_label =
372             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
373                                               (copies->first_spin));
374
375         if (first_label > old_first_label) {
376                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->last_spin),
377                                            last_label);
378                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->first_spin),
379                                            first_label);
380         } else {
381                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->first_spin),
382                                            first_label);
383                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->last_spin),
384                                            last_label);
385         }
386         if ((first_label == 1) && (last_label == copies->labels_per_sheet)) {
387                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
388                                               (copies->sheets_radio), TRUE);
389                 gtk_spin_button_set_value (GTK_SPIN_BUTTON
390                                            (copies->sheets_spin), n_sheets);
391         } else {
392                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
393                                               (copies->labels_radio), TRUE);
394                 gtk_spin_button_set_value (GTK_SPIN_BUTTON
395                                            (copies->sheets_spin), 1.0);
396         }
397 }