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