]> git.sur5r.net Git - glabels/blob - src/wdgt-print-copies.c
Imported Upstream version 2.2.8
[glabels] / 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, 100.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, 1.0, 10.0, 0.0);
175         copies->first_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
176                                                   1.0, 0);
177         gtk_box_pack_start (GTK_BOX(whbox1), copies->first_spin, FALSE, FALSE, 0);
178         gtk_box_pack_start (GTK_BOX(whbox1), gtk_label_new (_("to:")), FALSE, FALSE, 0);
179         adjust = gtk_adjustment_new (copies->labels_per_sheet, 1.0, copies->labels_per_sheet, 1.0, 10.0, 0.0);
180         copies->last_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
181                                                  1.0, 0);
182         gtk_box_pack_start (GTK_BOX(whbox1), copies->last_spin, FALSE, FALSE, 0);
183         gtk_widget_set_sensitive (copies->first_spin, FALSE);
184         gtk_widget_set_sensitive (copies->last_spin, FALSE);
185
186         /* Connect signals to controls */
187         g_signal_connect (G_OBJECT (copies->mini_preview), "pressed",
188                           G_CALLBACK (preview_pressed), copies);
189         g_signal_connect (G_OBJECT (copies->sheets_radio), "toggled",
190                           G_CALLBACK (sheets_radio_cb), copies);
191         g_signal_connect (G_OBJECT (copies->first_spin), "changed",
192                           G_CALLBACK (first_spin_cb), copies);
193         g_signal_connect (G_OBJECT (copies->last_spin), "changed",
194                           G_CALLBACK (last_spin_cb), copies);
195 }
196
197 /*--------------------------------------------------------------------------*/
198 /* PRIVATE.  Enable/Disable appropriate controls due to radio button toggle.*/
199 /*--------------------------------------------------------------------------*/
200 static void
201 sheets_radio_cb (GtkToggleButton * togglebutton,
202                  gpointer user_data)
203 {
204         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
205         gint first, last;
206
207         if (gtk_toggle_button_get_active (togglebutton)) {
208
209                 gtk_widget_set_sensitive (copies->sheets_spin, TRUE);
210                 gtk_widget_set_sensitive (copies->first_spin, FALSE);
211                 gtk_widget_set_sensitive (copies->last_spin, FALSE);
212
213                 gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
214                                         1, copies->labels_per_sheet);
215
216         } else {
217
218                 gtk_widget_set_sensitive (copies->sheets_spin, FALSE);
219                 gtk_widget_set_sensitive (copies->first_spin, TRUE);
220                 gtk_widget_set_sensitive (copies->last_spin, TRUE);
221
222                 first =
223                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
224                                                       (copies->first_spin));
225                 last =
226                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
227                                                       (copies->last_spin));
228                 gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
229                                                  first, last);
230
231         }
232 }
233
234 /*--------------------------------------------------------------------------*/
235 /* PRIVATE.  modify widget due to change of first spin button               */
236 /*--------------------------------------------------------------------------*/
237 static void
238 first_spin_cb (GtkSpinButton * spinbutton,
239                gpointer user_data)
240 {
241         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
242         gint first, last;
243
244         first =
245             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
246                                               (copies->first_spin));
247         last =
248             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
249                                               (copies->last_spin));
250
251         gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (copies->last_spin))->lower = first;
252
253         gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
254                                               first, last);
255 }
256
257 /*--------------------------------------------------------------------------*/
258 /* PRIVATE.  modify widget due to change of last spin button                */
259 /*--------------------------------------------------------------------------*/
260 static void
261 last_spin_cb (GtkSpinButton * spinbutton,
262               gpointer user_data)
263 {
264         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
265         gint first, last;
266
267         first =
268             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
269                                               (copies->first_spin));
270         last =
271             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
272                                               (copies->last_spin));
273
274         gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (copies->first_spin))->upper = last;
275
276         gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
277                                               first, last);
278 }
279
280 /*--------------------------------------------------------------------------*/
281 /* PRIVATE.  Canvas event handler, select first and last items.             */
282 /*--------------------------------------------------------------------------*/
283 static void
284 preview_pressed (glWdgtMiniPreview *mini_preview,
285                  gint first,
286                  gint last,
287                  gpointer user_data)
288 {
289         glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
290
291         gl_wdgt_print_copies_set_range (copies, 1, first, last);
292 }
293
294 /****************************************************************************/
295 /* query selected range of labels within sheet or number of sheets.         */
296 /****************************************************************************/
297 void
298 gl_wdgt_print_copies_get_range (glWdgtPrintCopies * copies,
299                                 gint * n_sheets,
300                                 gint * first_label,
301                                 gint * last_label)
302 {
303         gboolean sheets_active;
304
305         sheets_active =
306             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
307                                           (copies->sheets_radio));
308
309         if (sheets_active) {
310                 *n_sheets =
311                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
312                                                       (copies->sheets_spin));
313                 *first_label = 1;
314                 *last_label = copies->labels_per_sheet;
315         } else {
316                 *n_sheets = 1;
317                 *first_label =
318                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
319                                                       (copies->first_spin));
320                 *last_label =
321                     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
322                                                       (copies->last_spin));
323         }
324 }
325
326 /****************************************************************************/
327 /* set range of labels within sheet or number of sheets                     */
328 /****************************************************************************/
329 void
330 gl_wdgt_print_copies_set_range (glWdgtPrintCopies * copies,
331                                 gint n_sheets,
332                                 gint first_label,
333                                 gint last_label)
334 {
335         gint old_first_label;
336
337         old_first_label =
338             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
339                                               (copies->first_spin));
340
341         if (first_label > old_first_label) {
342                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->last_spin),
343                                            last_label);
344                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->first_spin),
345                                            first_label);
346         } else {
347                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->first_spin),
348                                            first_label);
349                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->last_spin),
350                                            last_label);
351         }
352         if ((first_label == 1) && (last_label == copies->labels_per_sheet)) {
353                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
354                                               (copies->sheets_radio), TRUE);
355                 gtk_spin_button_set_value (GTK_SPIN_BUTTON
356                                            (copies->sheets_spin), n_sheets);
357         } else {
358                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
359                                               (copies->labels_radio), TRUE);
360                 gtk_spin_button_set_value (GTK_SPIN_BUTTON
361                                            (copies->sheets_spin), 1.0);
362         }
363 }