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