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