]> git.sur5r.net Git - glabels/blob - glabels2/src/wdgt-print-copies.c
Created 2 methods for setting label for mini-preview widget. The first is "by name...
[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 (gl_hig_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 = g_type_class_peek_parent (class);
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 = gl_template_get_n_labels (template);
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_template (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
167                                            template);
168         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), copies->mini_preview);
169
170         wvbox = gl_hig_vbox_new (GL_HIG_VBOX_INNER);
171         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), wvbox);
172
173         /* Sheet controls */
174         whbox1 = gl_hig_hbox_new ();
175         gl_hig_vbox_add_widget (GL_HIG_VBOX(wvbox), whbox1);
176         copies->sheets_radio =
177             gtk_radio_button_new_with_label (radio_group, _("Sheets:"));
178         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox1), copies->sheets_radio);
179         adjust = gtk_adjustment_new (1, 1.0, 10.0, 1.0, 10.0, 10.0);
180         copies->sheets_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
181                                                    1.0, 0);
182         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox1), copies->sheets_spin);
183         gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
184                                               1, copies->labels_per_sheet);
185
186         /* Blank line */
187         gl_hig_vbox_add_widget (GL_HIG_VBOX(wvbox), gtk_label_new (""));
188
189         /* Label controls */
190         whbox1 = gl_hig_hbox_new ();
191         gl_hig_vbox_add_widget (GL_HIG_VBOX(wvbox), whbox1);
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         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox1), copies->labels_radio);
197         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox1),
198                                 gtk_label_new (_("from:")));
199         adjust = gtk_adjustment_new (1, 1.0, copies->labels_per_sheet,
200                                      1.0, 10.0, 10.0);
201         copies->first_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
202                                                   1.0, 0);
203         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox1), copies->first_spin);
204         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox1),
205                                 gtk_label_new (_("to:")));
206         adjust = gtk_adjustment_new (copies->labels_per_sheet,
207                                      1.0, copies->labels_per_sheet,
208                                      1.0, 10.0, 10.0);
209         copies->last_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
210                                                  1.0, 0);
211         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox1), copies->last_spin);
212         gtk_widget_set_sensitive (copies->first_spin, FALSE);
213         gtk_widget_set_sensitive (copies->last_spin, FALSE);
214
215         /* Connect signals to controls */
216         g_signal_connect (G_OBJECT (copies->mini_preview), "pressed",
217                           G_CALLBACK (preview_pressed), copies);
218         g_signal_connect (G_OBJECT (copies->sheets_radio), "toggled",
219                           G_CALLBACK (sheets_radio_cb), copies);
220         g_signal_connect (G_OBJECT (copies->first_spin), "changed",
221                           G_CALLBACK (first_spin_cb), copies);
222         g_signal_connect (G_OBJECT (copies->last_spin), "changed",
223                           G_CALLBACK (last_spin_cb), copies);
224 }
225
226 /*--------------------------------------------------------------------------*/
227 /* PRIVATE.  Enable/Disable appropriate controls due to radio button toggle.*/
228 /*--------------------------------------------------------------------------*/
229 static void
230 sheets_radio_cb (GtkToggleButton * togglebutton,
231                  gpointer user_data)
232 {
233         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
234         gint first, last;
235
236         if (gtk_toggle_button_get_active (togglebutton)) {
237
238                 gtk_widget_set_sensitive (copies->sheets_spin, TRUE);
239                 gtk_widget_set_sensitive (copies->first_spin, FALSE);
240                 gtk_widget_set_sensitive (copies->last_spin, FALSE);
241
242                 gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
243                                         1, copies->labels_per_sheet);
244
245         } else {
246
247                 gtk_widget_set_sensitive (copies->sheets_spin, FALSE);
248                 gtk_widget_set_sensitive (copies->first_spin, TRUE);
249                 gtk_widget_set_sensitive (copies->last_spin, TRUE);
250
251                 first =
252                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
253                                                       (copies->first_spin));
254                 last =
255                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
256                                                       (copies->last_spin));
257                 gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
258                                                  first, last);
259
260         }
261 }
262
263 /*--------------------------------------------------------------------------*/
264 /* PRIVATE.  modify widget due to change of first spin button               */
265 /*--------------------------------------------------------------------------*/
266 static void
267 first_spin_cb (GtkSpinButton * spinbutton,
268                gpointer user_data)
269 {
270         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
271         gint first, last;
272
273         first =
274             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
275                                               (copies->first_spin));
276         last =
277             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
278                                               (copies->last_spin));
279
280         gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (copies->last_spin))->
281             lower = first;
282
283         gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
284                                          first, last);
285 }
286
287 /*--------------------------------------------------------------------------*/
288 /* PRIVATE.  modify widget due to change of last spin button                */
289 /*--------------------------------------------------------------------------*/
290 static void
291 last_spin_cb (GtkSpinButton * spinbutton,
292               gpointer user_data)
293 {
294         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
295         gint first, last;
296
297         first =
298             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
299                                               (copies->first_spin));
300         last =
301             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
302                                               (copies->last_spin));
303
304         gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (copies->first_spin))->
305             upper = last;
306
307         gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
308                                          first, last);
309 }
310
311 /*--------------------------------------------------------------------------*/
312 /* PRIVATE.  Canvas event handler, select first and last items.             */
313 /*--------------------------------------------------------------------------*/
314 static void
315 preview_pressed (glWdgtMiniPreview *mini_preview,
316                  gint first,
317                  gint last,
318                  gpointer user_data)
319 {
320         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
321
322         gl_wdgt_print_copies_set_range (copies, 1, first, last);
323 }
324
325 /****************************************************************************/
326 /* query selected range of labels within sheet or number of sheets.         */
327 /****************************************************************************/
328 void
329 gl_wdgt_print_copies_get_range (glWdgtPrintCopies * copies,
330                                 gint * n_sheets,
331                                 gint * first_label,
332                                 gint * last_label)
333 {
334         gboolean sheets_active;
335
336         sheets_active =
337             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
338                                           (copies->sheets_radio));
339
340         if (sheets_active) {
341                 *n_sheets =
342                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
343                                                       (copies->sheets_spin));
344                 *first_label = 1;
345                 *last_label = copies->labels_per_sheet;
346         } else {
347                 *n_sheets = 1;
348                 *first_label =
349                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
350                                                       (copies->first_spin));
351                 *last_label =
352                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
353                                                       (copies->last_spin));
354         }
355 }
356
357 /****************************************************************************/
358 /* set range of labels within sheet or number of sheets                     */
359 /****************************************************************************/
360 void
361 gl_wdgt_print_copies_set_range (glWdgtPrintCopies * copies,
362                                 gint n_sheets,
363                                 gint first_label,
364                                 gint last_label)
365 {
366         gint old_first_label;
367
368         old_first_label =
369             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
370                                               (copies->first_spin));
371
372         if (first_label > old_first_label) {
373                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->last_spin),
374                                            last_label);
375                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->first_spin),
376                                            first_label);
377         } else {
378                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->first_spin),
379                                            first_label);
380                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->last_spin),
381                                            last_label);
382         }
383         if ((first_label == 1) && (last_label == copies->labels_per_sheet)) {
384                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
385                                               (copies->sheets_radio), TRUE);
386                 gtk_spin_button_set_value (GTK_SPIN_BUTTON
387                                            (copies->sheets_spin), n_sheets);
388         } else {
389                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
390                                               (copies->labels_radio), TRUE);
391                 gtk_spin_button_set_value (GTK_SPIN_BUTTON
392                                            (copies->sheets_spin), 1.0);
393         }
394 }