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 * object-editor.c: object properties editor module
8 * Copyright (C) 2003 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
26 #include "object-editor.h"
28 #include <glib/gi18n.h>
29 #include <gtk/gtklabel.h>
30 #include <gtk/gtkspinbutton.h>
34 #include "wdgt-chain-button.h"
36 #include "object-editor-private.h"
40 /*===========================================*/
42 /*===========================================*/
44 /*===========================================*/
45 /* Private data types */
46 /*===========================================*/
48 /*===========================================*/
50 /*===========================================*/
52 /*===========================================*/
53 /* Local function prototypes */
54 /*===========================================*/
56 static void aspect_toggle_cb (glObjectEditor *editor);
57 static void size_reset_cb (glObjectEditor *editor);
58 static void w_spin_cb (glObjectEditor *editor);
59 static void h_spin_cb (glObjectEditor *editor);
62 /*--------------------------------------------------------------------------*/
63 /* PRIVATE. Prepare size page. */
64 /*--------------------------------------------------------------------------*/
66 gl_object_editor_prepare_size_page (glObjectEditor *editor,
67 glObjectEditorOption option)
69 const gchar *units_string;
73 gl_debug (DEBUG_EDITOR, "START");
75 /* Extract widgets from XML tree. */
76 editor->priv->size_page_vbox =
77 glade_xml_get_widget (editor->priv->gui, "size_page_vbox");
78 editor->priv->size_w_spin =
79 glade_xml_get_widget (editor->priv->gui, "size_w_spin");
80 editor->priv->size_h_spin =
81 glade_xml_get_widget (editor->priv->gui, "size_h_spin");
82 editor->priv->size_w_units_label =
83 glade_xml_get_widget (editor->priv->gui, "size_w_units_label");
84 editor->priv->size_h_units_label =
85 glade_xml_get_widget (editor->priv->gui, "size_h_units_label");
86 editor->priv->size_aspect_checkbutton =
87 glade_xml_get_widget (editor->priv->gui, "size_aspect_checkbutton");
88 editor->priv->size_reset_image_button =
89 glade_xml_get_widget (editor->priv->gui, "size_reset_image_button");
91 /* Get configuration information */
92 units_string = gl_prefs_get_units_string ();
93 editor->priv->units_per_point = gl_prefs_get_units_per_point ();
94 climb_rate = gl_prefs_get_units_step_size ();
95 digits = gl_prefs_get_units_precision ();
97 /* Modify widgets based on configuration */
98 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_w_spin), digits);
99 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_w_spin),
100 climb_rate, 10.0*climb_rate);
101 gtk_label_set_text (GTK_LABEL(editor->priv->size_w_units_label), units_string);
102 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_h_spin), digits);
103 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
104 climb_rate, 10.0*climb_rate);
105 gtk_label_set_text (GTK_LABEL(editor->priv->size_h_units_label), units_string);
108 gtk_widget_show_all (editor->priv->size_page_vbox);
109 if (option != GL_OBJECT_EDITOR_SIZE_IMAGE_PAGE) {
110 gtk_widget_hide (editor->priv->size_reset_image_button);
113 /* Connect signals */
114 g_signal_connect_swapped (G_OBJECT (editor->priv->size_aspect_checkbutton),
116 G_CALLBACK (aspect_toggle_cb),
118 g_signal_connect_swapped (G_OBJECT (editor->priv->size_w_spin),
120 G_CALLBACK (w_spin_cb),
122 g_signal_connect_swapped (G_OBJECT (editor->priv->size_h_spin),
124 G_CALLBACK (h_spin_cb),
127 if (option == GL_OBJECT_EDITOR_SIZE_IMAGE_PAGE) {
128 g_signal_connect_swapped (G_OBJECT (editor->priv->size_reset_image_button),
130 G_CALLBACK (size_reset_cb),
134 gl_debug (DEBUG_EDITOR, "END");
137 /*--------------------------------------------------------------------------*/
138 /* PRIVATE. Maintain aspect ratio checkbox callback. */
139 /*--------------------------------------------------------------------------*/
141 aspect_toggle_cb (glObjectEditor *editor)
143 glWdgtChainButton *toggle;
146 if (editor->priv->stop_signals) return;
148 gl_debug (DEBUG_EDITOR, "START");
150 toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);
152 if (gl_wdgt_chain_button_get_active (toggle)) {
154 w = gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->size_w_spin));
155 h = gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->size_h_spin));
157 editor->priv->size_aspect_ratio = h / w;
161 gl_debug (DEBUG_EDITOR, "END");
164 /*--------------------------------------------------------------------------*/
165 /* PRIVATE. W spin button changed callback. */
166 /*--------------------------------------------------------------------------*/
168 w_spin_cb (glObjectEditor *editor)
171 glWdgtChainButton *toggle;
173 if (editor->priv->stop_signals) return;
175 gl_debug (DEBUG_EDITOR, "START");
177 toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);
179 if (gl_wdgt_chain_button_get_active (toggle)) {
181 w = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin));
183 h = w * editor->priv->size_aspect_ratio;
185 /* Update our sibling control, blocking recursion. */
186 editor->priv->stop_signals = TRUE;
187 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), h);
188 editor->priv->stop_signals = FALSE;
191 /* Emit our "changed" signal */
192 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
193 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[SIZE_CHANGED], 0);
195 gl_debug (DEBUG_EDITOR, "END");
198 /*--------------------------------------------------------------------------*/
199 /* PRIVATE. H spin button changed callback. */
200 /*--------------------------------------------------------------------------*/
202 h_spin_cb (glObjectEditor *editor)
205 glWdgtChainButton *toggle;
207 if (editor->priv->stop_signals) return;
209 gl_debug (DEBUG_EDITOR, "START");
211 toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);
213 if (gl_wdgt_chain_button_get_active (toggle)) {
215 h = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin));
217 w = h / editor->priv->size_aspect_ratio;
219 /* Update our sibling control, blocking recursion. */
220 editor->priv->stop_signals = TRUE;
221 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin), w);
222 editor->priv->stop_signals = FALSE;
225 /* Emit our "changed" signal */
226 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
227 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[SIZE_CHANGED], 0);
229 gl_debug (DEBUG_EDITOR, "END");
232 /*--------------------------------------------------------------------------*/
233 /* PRIVATE. Reset image size callback. */
234 /*--------------------------------------------------------------------------*/
236 size_reset_cb (glObjectEditor *editor)
238 gdouble w_base, h_base;
239 gdouble w_max, h_max, wh_max;
240 gdouble aspect_ratio;
242 if (editor->priv->stop_signals) return;
244 gl_debug (DEBUG_EDITOR, "START");
246 editor->priv->stop_signals = TRUE;
248 w_base = editor->priv->w_base;
249 h_base = editor->priv->h_base;
251 w_max = editor->priv->w_max;
252 h_max = editor->priv->h_max;
253 wh_max = MAX( w_max, h_max );
255 if ( (w_base > wh_max) || (h_base > wh_max) ) {
257 aspect_ratio = h_base / w_base;
259 if ( aspect_ratio < 1.0 ) {
261 h_base = wh_max * aspect_ratio;
264 w_base = wh_max / aspect_ratio;
269 w_base *= editor->priv->units_per_point;
270 h_base *= editor->priv->units_per_point;
272 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin),
274 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin),
277 /* Emit our "changed" signal */
278 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
279 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[SIZE_CHANGED], 0);
281 editor->priv->stop_signals = FALSE;
283 gl_debug (DEBUG_EDITOR, "END");
286 /*****************************************************************************/
288 /*****************************************************************************/
290 gl_object_editor_set_size (glObjectEditor *editor,
294 gl_debug (DEBUG_EDITOR, "START");
296 editor->priv->stop_signals = TRUE;
298 /* save a copy in internal units */
302 /* convert internal units to displayed units */
303 gl_debug (DEBUG_EDITOR, "internal w,h = %g, %g", w, h);
304 w *= editor->priv->units_per_point;
305 h *= editor->priv->units_per_point;
306 gl_debug (DEBUG_EDITOR, "display w,h = %g, %g", w, h);
308 /* Set widget values */
309 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin), w);
310 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), h);
312 /* Update aspect ratio */
313 editor->priv->size_aspect_ratio = h / w;
315 editor->priv->stop_signals = FALSE;
317 gl_debug (DEBUG_EDITOR, "END");
320 /*****************************************************************************/
321 /* Set maximum size. */
322 /*****************************************************************************/
324 gl_object_editor_set_max_size (glObjectEditor *editor,
331 gl_debug (DEBUG_EDITOR, "START");
333 if (editor->priv->size_page_vbox)
336 editor->priv->stop_signals = TRUE;
338 /* save a copy in internal units */
339 editor->priv->w_max = w_max;
340 editor->priv->h_max = h_max;
342 /* convert internal units to displayed units */
343 gl_debug (DEBUG_EDITOR, "internal w_max,h_max = %g, %g", w_max, h_max);
344 w_max *= editor->priv->units_per_point;
345 h_max *= editor->priv->units_per_point;
346 wh_max = MAX( w_max, h_max );
347 gl_debug (DEBUG_EDITOR, "display w_max,h_max = %g, %g", w_max, h_max);
349 /* Set widget values */
350 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin));
351 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->size_w_spin),
353 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin), tmp);
354 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin));
355 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->size_h_spin),
357 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), tmp);
359 editor->priv->stop_signals = FALSE;
363 gl_debug (DEBUG_EDITOR, "END");
366 /*****************************************************************************/
367 /* Set base or natural size of image. */
368 /*****************************************************************************/
370 gl_object_editor_set_base_size (glObjectEditor *editor,
374 gl_debug (DEBUG_EDITOR, "Setting w_base = %g", w_base);
375 gl_debug (DEBUG_EDITOR, "Setting h_base = %g", h_base);
377 editor->priv->w_base = w_base;
378 editor->priv->h_base = h_base;
381 /*****************************************************************************/
383 /*****************************************************************************/
385 gl_object_editor_get_size (glObjectEditor *editor,
389 gl_debug (DEBUG_EDITOR, "START");
392 /* Get values from widgets */
393 *w = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin));
394 *h = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin));
396 /* convert everything back to our internal units (points) */
397 *w /= editor->priv->units_per_point;
398 *h /= editor->priv->units_per_point;
400 /* save a copy in internal units */
401 editor->priv->w = *w;
402 editor->priv->h = *h;
404 gl_debug (DEBUG_EDITOR, "END");
407 /*****************************************************************************/
408 /* PRIVATE. Prefs changed callback. Update units related items. */
409 /*****************************************************************************/
411 size_prefs_changed_cb (glObjectEditor *editor)
413 const gchar *units_string;
417 gl_debug (DEBUG_EDITOR, "START");
419 /* Get new configuration information */
420 units_string = gl_prefs_get_units_string ();
421 editor->priv->units_per_point = gl_prefs_get_units_per_point ();
422 climb_rate = gl_prefs_get_units_step_size ();
423 digits = gl_prefs_get_units_precision ();
425 /* Update characteristics of w_spin/h_spin */
426 editor->priv->stop_signals = TRUE;
427 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_w_spin),
429 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
431 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_w_spin),
432 climb_rate, 10.0*climb_rate);
433 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
434 climb_rate, 10.0*climb_rate);
435 editor->priv->stop_signals = FALSE;
437 /* Update units_labels */
438 gtk_label_set_text (GTK_LABEL(editor->priv->size_w_units_label),
440 gtk_label_set_text (GTK_LABEL(editor->priv->size_h_units_label),
443 /* Update values of w_spin/h_spin */
444 gl_object_editor_set_size (editor,
447 gl_object_editor_set_max_size (editor,
449 editor->priv->h_max);
451 gl_debug (DEBUG_EDITOR, "END");