1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * wdgt_print_copies.c: custom print copies widget module
8 * Copyright (C) 2001 Jim Evins <evins@snaught.com>.
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.
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.
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
27 #include "wdgt-print-copies.h"
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>
37 #include "wdgt-mini-preview.h"
42 #define WDGT_MINI_PREVIEW_HEIGHT 175
43 #define WDGT_MINI_PREVIEW_WIDTH 150
45 /*===========================================*/
47 /*===========================================*/
49 /*===========================================*/
50 /* Local function prototypes */
51 /*===========================================*/
53 static void gl_wdgt_print_copies_finalize (GObject * object);
55 static void gl_wdgt_print_copies_construct (glWdgtPrintCopies * copies,
58 static void sheets_radio_cb (GtkToggleButton * togglebutton,
60 static void first_spin_cb (GtkSpinButton * spinbutton,
62 static void last_spin_cb (GtkSpinButton * spinbutton,
66 preview_pressed (glWdgtMiniPreview *mini_preview,
67 gint first, gint last, gpointer user_data);
70 /****************************************************************************/
71 /* Boilerplate Object stuff. */
72 /****************************************************************************/
73 G_DEFINE_TYPE (glWdgtPrintCopies, gl_wdgt_print_copies, GTK_TYPE_HBOX);
76 gl_wdgt_print_copies_class_init (glWdgtPrintCopiesClass * class)
78 GObjectClass *object_class = G_OBJECT_CLASS (class);
80 gl_wdgt_print_copies_parent_class = g_type_class_peek_parent (class);
82 object_class->finalize = gl_wdgt_print_copies_finalize;
86 gl_wdgt_print_copies_init (glWdgtPrintCopies * copies)
88 copies->labels_per_sheet = 0;
90 copies->mini_preview = NULL;
92 copies->sheets_radio = NULL;
93 copies->sheets_spin = NULL;
95 copies->labels_radio = NULL;
96 copies->first_spin = NULL;
97 copies->last_spin = NULL;
101 gl_wdgt_print_copies_finalize (GObject * object)
103 glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (object);
105 g_return_if_fail (object != NULL);
106 g_return_if_fail (GL_IS_WDGT_PRINT_COPIES (object));
108 G_OBJECT_CLASS (gl_wdgt_print_copies_parent_class)->finalize (object);
112 gl_wdgt_print_copies_new (glLabel * label)
114 glWdgtPrintCopies *copies;
116 copies = g_object_new (gl_wdgt_print_copies_get_type (), NULL);
118 gl_wdgt_print_copies_construct (copies, label);
120 return GTK_WIDGET (copies);
123 /*--------------------------------------------------------------------------*/
124 /* Construct composite widget. */
125 /*--------------------------------------------------------------------------*/
127 gl_wdgt_print_copies_construct (glWdgtPrintCopies *copies,
130 glTemplate *template;
131 const glTemplateLabelType *label_type;
132 GtkWidget *whbox, *wvbox, *whbox1;
133 GSList *radio_group = NULL;
136 whbox = GTK_WIDGET (copies);
138 template = gl_label_get_template (label);
139 label_type = gl_template_get_first_label_type (template);
141 copies->labels_per_sheet = gl_template_get_n_labels (label_type);
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),
148 gtk_box_pack_start (GTK_BOX(whbox), copies->mini_preview, FALSE, FALSE, 0);
150 wvbox = gtk_vbox_new (FALSE, GL_HIG_PAD1);
151 gtk_box_pack_start (GTK_BOX(whbox), wvbox, FALSE, FALSE, 0);
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),
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);
167 gtk_box_pack_start (GTK_BOX(wvbox), gtk_label_new (""), FALSE, FALSE, 0);
170 whbox1 = gtk_hbox_new (FALSE, GL_HIG_PAD2);
171 gtk_box_pack_start (GTK_BOX(wvbox), whbox1, FALSE, FALSE, 0);
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,
180 copies->first_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
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,
187 copies->last_spin = gtk_spin_button_new (GTK_ADJUSTMENT (adjust),
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);
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);
203 gl_template_free (template);
206 /*--------------------------------------------------------------------------*/
207 /* PRIVATE. Enable/Disable appropriate controls due to radio button toggle.*/
208 /*--------------------------------------------------------------------------*/
210 sheets_radio_cb (GtkToggleButton * togglebutton,
213 glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
216 if (gtk_toggle_button_get_active (togglebutton)) {
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);
222 gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
223 1, copies->labels_per_sheet);
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);
232 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
233 (copies->first_spin));
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),
243 /*--------------------------------------------------------------------------*/
244 /* PRIVATE. modify widget due to change of first spin button */
245 /*--------------------------------------------------------------------------*/
247 first_spin_cb (GtkSpinButton * spinbutton,
250 glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
254 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
255 (copies->first_spin));
257 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
258 (copies->last_spin));
260 gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (copies->last_spin))->
263 gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
267 /*--------------------------------------------------------------------------*/
268 /* PRIVATE. modify widget due to change of last spin button */
269 /*--------------------------------------------------------------------------*/
271 last_spin_cb (GtkSpinButton * spinbutton,
274 glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
278 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
279 (copies->first_spin));
281 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
282 (copies->last_spin));
284 gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (copies->first_spin))->
287 gl_wdgt_mini_preview_highlight_range (GL_WDGT_MINI_PREVIEW(copies->mini_preview),
291 /*--------------------------------------------------------------------------*/
292 /* PRIVATE. Canvas event handler, select first and last items. */
293 /*--------------------------------------------------------------------------*/
295 preview_pressed (glWdgtMiniPreview *mini_preview,
300 glWdgtPrintCopies *copies = GL_WDGT_PRINT_COPIES (user_data);
302 gl_wdgt_print_copies_set_range (copies, 1, first, last);
305 /****************************************************************************/
306 /* query selected range of labels within sheet or number of sheets. */
307 /****************************************************************************/
309 gl_wdgt_print_copies_get_range (glWdgtPrintCopies * copies,
314 gboolean sheets_active;
317 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
318 (copies->sheets_radio));
322 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
323 (copies->sheets_spin));
325 *last_label = copies->labels_per_sheet;
329 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
330 (copies->first_spin));
332 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
333 (copies->last_spin));
337 /****************************************************************************/
338 /* set range of labels within sheet or number of sheets */
339 /****************************************************************************/
341 gl_wdgt_print_copies_set_range (glWdgtPrintCopies * copies,
346 gint old_first_label;
349 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
350 (copies->first_spin));
352 if (first_label > old_first_label) {
353 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->last_spin),
355 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->first_spin),
358 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->first_spin),
360 gtk_spin_button_set_value (GTK_SPIN_BUTTON (copies->last_spin),
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);
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);