]> git.sur5r.net Git - glabels/blob - glabels2/src/wdgt-print-copies.c
023d71262a3e2436d91d7b531ebf3720f709d482
[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         g_return_if_fail (object != NULL);
104         g_return_if_fail (GL_IS_WDGT_PRINT_COPIES (object));
105
106         G_OBJECT_CLASS (gl_wdgt_print_copies_parent_class)->finalize (object);
107 }
108
109 GtkWidget *
110 gl_wdgt_print_copies_new (glLabel * label)
111 {
112         glWdgtPrintCopies *copies;
113
114         copies = g_object_new (gl_wdgt_print_copies_get_type (), NULL);
115
116         gl_wdgt_print_copies_construct (copies, label);
117
118         return GTK_WIDGET (copies);
119 }
120
121 /*--------------------------------------------------------------------------*/
122 /* Construct composite widget.                                              */
123 /*--------------------------------------------------------------------------*/
124 static void
125 gl_wdgt_print_copies_construct (glWdgtPrintCopies *copies,
126                                 glLabel           *label)
127 {
128         const lglTemplateFrame    *frame;
129         GtkWidget                 *whbox, *wvbox, *whbox1;
130         GSList                    *radio_group = NULL;
131         GtkObject                 *adjust;
132
133         whbox = GTK_WIDGET (copies);
134
135         frame = (lglTemplateFrame *)label->template->frames->data;
136
137         copies->labels_per_sheet = lgl_template_frame_get_n_labels (frame);
138
139         /* mini_preview canvas */
140         copies->mini_preview = gl_wdgt_mini_preview_new (WDGT_MINI_PREVIEW_HEIGHT,
141                                                     WDGT_MINI_PREVIEW_WIDTH);
142         gl_wdgt_mini_preview_set_template (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
143                                            label->template);
144         gtk_box_pack_start (GTK_BOX(whbox), copies->mini_preview, FALSE, FALSE, 0);
145
146         wvbox = gtk_vbox_new (FALSE, GL_HIG_PAD1);
147         gtk_box_pack_start (GTK_BOX(whbox), wvbox, FALSE, FALSE, 0);
148
149         /* Sheet controls */
150         whbox1 = gtk_hbox_new (FALSE, GL_HIG_PAD2);
151         gtk_box_pack_start (GTK_BOX(wvbox), whbox1, FALSE, FALSE, 0);
152         copies->sheets_radio =
153             gtk_radio_button_new_with_label (radio_group, _("Sheets:"));
154         gtk_box_pack_start (GTK_BOX(whbox1), copies->sheets_radio, FALSE, FALSE, 0);
155         adjust = gtk_adjustment_new (1, 1.0, 10.0, 1.0, 10.0, 0.0);
156         copies->sheets_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
157                                                    1.0, 0);
158         gtk_box_pack_start (GTK_BOX(whbox1), copies->sheets_spin, FALSE, FALSE, 0);
159         gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
160                                               1, copies->labels_per_sheet);
161
162         /* Blank line */
163         gtk_box_pack_start (GTK_BOX(wvbox), gtk_label_new (""), FALSE, FALSE, 0);
164
165         /* Label controls */
166         whbox1 = gtk_hbox_new (FALSE, GL_HIG_PAD2);
167         gtk_box_pack_start (GTK_BOX(wvbox), whbox1, FALSE, FALSE, 0);
168         radio_group =
169             gtk_radio_button_get_group (GTK_RADIO_BUTTON (copies->sheets_radio));
170         copies->labels_radio =
171             gtk_radio_button_new_with_label (radio_group, _("Labels"));
172         gtk_box_pack_start (GTK_BOX(whbox1), copies->labels_radio, FALSE, FALSE, 0);
173         gtk_box_pack_start (GTK_BOX(whbox1), gtk_label_new (_("from:")), FALSE, FALSE, 0);
174         adjust = gtk_adjustment_new (1, 1.0, copies->labels_per_sheet,
175                                      1.0, 10.0, 0.0);
176         copies->first_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
177                                                   1.0, 0);
178         gtk_box_pack_start (GTK_BOX(whbox1), copies->first_spin, FALSE, FALSE, 0);
179         gtk_box_pack_start (GTK_BOX(whbox1), gtk_label_new (_("to:")), FALSE, FALSE, 0);
180         adjust = gtk_adjustment_new (copies->labels_per_sheet,
181                                      1.0, copies->labels_per_sheet,
182                                      1.0, 10.0, 0.0);
183         copies->last_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
184                                                  1.0, 0);
185         gtk_box_pack_start (GTK_BOX(whbox1), copies->last_spin, FALSE, FALSE, 0);
186         gtk_widget_set_sensitive (copies->first_spin, FALSE);
187         gtk_widget_set_sensitive (copies->last_spin, FALSE);
188
189         /* Connect signals to controls */
190         g_signal_connect (G_OBJECT (copies->mini_preview), "pressed",
191                           G_CALLBACK (preview_pressed), copies);
192         g_signal_connect (G_OBJECT (copies->sheets_radio), "toggled",
193                           G_CALLBACK (sheets_radio_cb), copies);
194         g_signal_connect (G_OBJECT (copies->first_spin), "changed",
195                           G_CALLBACK (first_spin_cb), copies);
196         g_signal_connect (G_OBJECT (copies->last_spin), "changed",
197                           G_CALLBACK (last_spin_cb), copies);
198 }
199
200 /*--------------------------------------------------------------------------*/
201 /* PRIVATE.  Enable/Disable appropriate controls due to radio button toggle.*/
202 /*--------------------------------------------------------------------------*/
203 static void
204 sheets_radio_cb (GtkToggleButton * togglebutton,
205                  gpointer user_data)
206 {
207         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
208         gint first, last;
209
210         if (gtk_toggle_button_get_active (togglebutton)) {
211
212                 gtk_widget_set_sensitive (copies->sheets_spin, TRUE);
213                 gtk_widget_set_sensitive (copies->first_spin, FALSE);
214                 gtk_widget_set_sensitive (copies->last_spin, FALSE);
215
216                 gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
217                                         1, copies->labels_per_sheet);
218
219         } else {
220
221                 gtk_widget_set_sensitive (copies->sheets_spin, FALSE);
222                 gtk_widget_set_sensitive (copies->first_spin, TRUE);
223                 gtk_widget_set_sensitive (copies->last_spin, TRUE);
224
225                 first =
226                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
227                                                       (copies->first_spin));
228                 last =
229                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
230                                                       (copies->last_spin));
231                 gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
232                                                  first, last);
233
234         }
235 }
236
237 /*--------------------------------------------------------------------------*/
238 /* PRIVATE.  modify widget due to change of first spin button               */
239 /*--------------------------------------------------------------------------*/
240 static void
241 first_spin_cb (GtkSpinButton * spinbutton,
242                gpointer user_data)
243 {
244         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
245         gint first, last;
246
247         first =
248             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
249                                               (copies->first_spin));
250         last =
251             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
252                                               (copies->last_spin));
253
254         gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (copies->last_spin))->
255             lower = first;
256
257         gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
258                                          first, last);
259 }
260
261 /*--------------------------------------------------------------------------*/
262 /* PRIVATE.  modify widget due to change of last spin button                */
263 /*--------------------------------------------------------------------------*/
264 static void
265 last_spin_cb (GtkSpinButton * spinbutton,
266               gpointer user_data)
267 {
268         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
269         gint first, last;
270
271         first =
272             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
273                                               (copies->first_spin));
274         last =
275             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
276                                               (copies->last_spin));
277
278         gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (copies->first_spin))->
279             upper = last;
280
281         gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
282                                          first, last);
283 }
284
285 /*--------------------------------------------------------------------------*/
286 /* PRIVATE.  Canvas event handler, select first and last items.             */
287 /*--------------------------------------------------------------------------*/
288 static void
289 preview_pressed (glWdgtMiniPreview *mini_preview,
290                  gint first,
291                  gint last,
292                  gpointer user_data)
293 {
294         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
295
296         gl_wdgt_print_copies_set_range (copies, 1, first, last);
297 }
298
299 /****************************************************************************/
300 /* query selected range of labels within sheet or number of sheets.         */
301 /****************************************************************************/
302 void
303 gl_wdgt_print_copies_get_range (glWdgtPrintCopies * copies,
304                                 gint * n_sheets,
305                                 gint * first_label,
306                                 gint * last_label)
307 {
308         gboolean sheets_active;
309
310         sheets_active =
311             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
312                                           (copies->sheets_radio));
313
314         if (sheets_active) {
315                 *n_sheets =
316                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
317                                                       (copies->sheets_spin));
318                 *first_label = 1;
319                 *last_label = copies->labels_per_sheet;
320         } else {
321                 *n_sheets = 1;
322                 *first_label =
323                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
324                                                       (copies->first_spin));
325                 *last_label =
326                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
327                                                       (copies->last_spin));
328         }
329 }
330
331 /****************************************************************************/
332 /* set range of labels within sheet or number of sheets                     */
333 /****************************************************************************/
334 void
335 gl_wdgt_print_copies_set_range (glWdgtPrintCopies * copies,
336                                 gint n_sheets,
337                                 gint first_label,
338                                 gint last_label)
339 {
340         gint old_first_label;
341
342         old_first_label =
343             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
344                                               (copies->first_spin));
345
346         if (first_label > old_first_label) {
347                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->last_spin),
348                                            last_label);
349                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->first_spin),
350                                            first_label);
351         } else {
352                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->first_spin),
353                                            first_label);
354                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->last_spin),
355                                            last_label);
356         }
357         if ((first_label == 1) && (last_label == copies->labels_per_sheet)) {
358                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
359                                               (copies->sheets_radio), TRUE);
360                 gtk_spin_button_set_value (GTK_SPIN_BUTTON
361                                            (copies->sheets_spin), n_sheets);
362         } else {
363                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
364                                               (copies->labels_radio), TRUE);
365                 gtk_spin_button_set_value (GTK_SPIN_BUTTON
366                                            (copies->sheets_spin), 1.0);
367         }
368 }