2 * object-editor-size-page.c
3 * Copyright (C) 2003-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * gLabels is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
23 #include "object-editor.h"
25 #include <glib/gi18n.h>
30 #include "wdgt-chain-button.h"
31 #include "builder-util.h"
32 #include "units-util.h"
34 #include "object-editor-private.h"
39 /*===========================================*/
41 /*===========================================*/
44 /*===========================================*/
45 /* Private data types */
46 /*===========================================*/
49 /*===========================================*/
51 /*===========================================*/
54 /*===========================================*/
55 /* Local function prototypes */
56 /*===========================================*/
58 static void aspect_toggle_cb (glObjectEditor *editor);
59 static void size_reset_cb (glObjectEditor *editor);
60 static void w_spin_cb (glObjectEditor *editor);
61 static void h_spin_cb (glObjectEditor *editor);
64 /*--------------------------------------------------------------------------*/
65 /* PRIVATE. Prepare size page. */
66 /*--------------------------------------------------------------------------*/
68 gl_object_editor_prepare_size_page (glObjectEditor *editor)
71 const gchar *units_string;
75 gl_debug (DEBUG_EDITOR, "START");
77 /* Extract widgets from XML tree. */
78 gl_builder_util_get_widgets (editor->priv->builder,
79 "size_page_vbox", &editor->priv->size_page_vbox,
80 "size_w_spin", &editor->priv->size_w_spin,
81 "size_h_spin", &editor->priv->size_h_spin,
82 "size_w_units_label", &editor->priv->size_w_units_label,
83 "size_h_units_label", &editor->priv->size_h_units_label,
84 "size_aspect_vbox", &editor->priv->size_aspect_vbox,
85 "size_reset_image_button", &editor->priv->size_reset_image_button,
88 editor->priv->size_aspect_checkbutton = gl_wdgt_chain_button_new (GL_WDGT_CHAIN_RIGHT);
89 gl_wdgt_chain_button_set_active (GL_WDGT_CHAIN_BUTTON(editor->priv->size_aspect_checkbutton),
91 gtk_box_pack_start (GTK_BOX (editor->priv->size_aspect_vbox),
92 editor->priv->size_aspect_checkbutton,
96 /* Get configuration information */
97 units = gl_prefs_model_get_units (gl_prefs);
98 units_string = lgl_units_get_name (units);
99 editor->priv->units_per_point = lgl_units_get_units_per_point (units);
100 climb_rate = gl_units_util_get_step_size (units);
101 digits = gl_units_util_get_precision (units);
103 /* Modify widgets based on configuration */
104 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_w_spin), digits);
105 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_w_spin),
106 climb_rate, 10.0*climb_rate);
107 gtk_label_set_text (GTK_LABEL(editor->priv->size_w_units_label), units_string);
108 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_h_spin), digits);
109 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
110 climb_rate, 10.0*climb_rate);
111 gtk_label_set_text (GTK_LABEL(editor->priv->size_h_units_label), units_string);
114 /* Connect signals */
115 g_signal_connect_swapped (G_OBJECT (editor->priv->size_aspect_checkbutton),
117 G_CALLBACK (aspect_toggle_cb),
119 g_signal_connect_swapped (G_OBJECT (editor->priv->size_w_spin),
121 G_CALLBACK (w_spin_cb),
123 g_signal_connect_swapped (G_OBJECT (editor->priv->size_h_spin),
125 G_CALLBACK (h_spin_cb),
127 g_signal_connect_swapped (G_OBJECT (editor->priv->size_reset_image_button),
129 G_CALLBACK (size_reset_cb),
132 gl_debug (DEBUG_EDITOR, "END");
136 /*--------------------------------------------------------------------------*/
137 /* PRIVATE. Maintain aspect ratio checkbox callback. */
138 /*--------------------------------------------------------------------------*/
140 aspect_toggle_cb (glObjectEditor *editor)
142 glWdgtChainButton *toggle;
145 if (editor->priv->stop_signals) return;
147 gl_debug (DEBUG_EDITOR, "START");
149 toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);
151 if (gl_wdgt_chain_button_get_active (toggle)) {
153 w = gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->size_w_spin));
154 h = gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->size_h_spin));
156 editor->priv->size_aspect_ratio = h / w;
160 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 gl_object_editor_size_changed_cb (editor);
193 gl_debug (DEBUG_EDITOR, "END");
197 /*--------------------------------------------------------------------------*/
198 /* PRIVATE. H spin button changed callback. */
199 /*--------------------------------------------------------------------------*/
201 h_spin_cb (glObjectEditor *editor)
204 glWdgtChainButton *toggle;
206 if (editor->priv->stop_signals) return;
208 gl_debug (DEBUG_EDITOR, "START");
210 toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);
212 if (gl_wdgt_chain_button_get_active (toggle)) {
214 h = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin));
216 w = h / editor->priv->size_aspect_ratio;
218 /* Update our sibling control, blocking recursion. */
219 editor->priv->stop_signals = TRUE;
220 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin), w);
221 editor->priv->stop_signals = FALSE;
224 gl_object_editor_size_changed_cb (editor);
226 gl_debug (DEBUG_EDITOR, "END");
230 /*--------------------------------------------------------------------------*/
231 /* PRIVATE. Reset image size callback. */
232 /*--------------------------------------------------------------------------*/
234 size_reset_cb (glObjectEditor *editor)
236 gdouble w_base, h_base;
237 gdouble w_max, h_max, wh_max;
238 gdouble aspect_ratio;
240 if (editor->priv->stop_signals) return;
242 gl_debug (DEBUG_EDITOR, "START");
244 editor->priv->stop_signals = TRUE;
246 w_base = editor->priv->w_base;
247 h_base = editor->priv->h_base;
249 w_max = editor->priv->w_max;
250 h_max = editor->priv->h_max;
251 wh_max = MAX( w_max, h_max );
253 if ( (w_base > wh_max) || (h_base > wh_max) ) {
255 aspect_ratio = h_base / w_base;
257 if ( aspect_ratio < 1.0 ) {
259 h_base = wh_max * aspect_ratio;
262 w_base = wh_max / aspect_ratio;
267 w_base *= editor->priv->units_per_point;
268 h_base *= editor->priv->units_per_point;
270 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin),
272 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin),
275 editor->priv->stop_signals = FALSE;
277 gl_object_editor_size_changed_cb (editor);
279 gl_debug (DEBUG_EDITOR, "END");
283 /*****************************************************************************/
285 /*****************************************************************************/
287 gl_object_editor_set_size (glObjectEditor *editor,
291 gl_debug (DEBUG_EDITOR, "START");
293 editor->priv->stop_signals = TRUE;
295 /* save a copy in internal units */
299 /* convert internal units to displayed units */
300 gl_debug (DEBUG_EDITOR, "internal w,h = %g, %g", w, h);
301 w *= editor->priv->units_per_point;
302 h *= editor->priv->units_per_point;
303 gl_debug (DEBUG_EDITOR, "display w,h = %g, %g", w, h);
305 /* Set widget values */
306 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin), w);
307 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), h);
309 /* Update aspect ratio */
310 editor->priv->size_aspect_ratio = h / w;
312 editor->priv->stop_signals = FALSE;
314 gl_debug (DEBUG_EDITOR, "END");
318 /*****************************************************************************/
319 /* Set maximum size. */
320 /*****************************************************************************/
322 gl_object_editor_set_max_size (glObjectEditor *editor,
329 gl_debug (DEBUG_EDITOR, "START");
331 if (editor->priv->size_page_vbox)
334 editor->priv->stop_signals = TRUE;
336 /* save a copy in internal units */
337 editor->priv->w_max = w_max;
338 editor->priv->h_max = h_max;
340 /* convert internal units to displayed units */
341 gl_debug (DEBUG_EDITOR, "internal w_max,h_max = %g, %g", w_max, h_max);
342 w_max *= editor->priv->units_per_point;
343 h_max *= editor->priv->units_per_point;
344 wh_max = MAX( w_max, h_max );
345 gl_debug (DEBUG_EDITOR, "display w_max,h_max = %g, %g", w_max, h_max);
347 /* Set widget values */
348 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin));
349 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->size_w_spin),
351 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin), tmp);
352 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin));
353 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->size_h_spin),
355 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), tmp);
357 editor->priv->stop_signals = FALSE;
361 gl_debug (DEBUG_EDITOR, "END");
365 /*****************************************************************************/
366 /* Set base or natural size of image. */
367 /*****************************************************************************/
369 gl_object_editor_set_base_size (glObjectEditor *editor,
373 gl_debug (DEBUG_EDITOR, "Setting w_base = %g", w_base);
374 gl_debug (DEBUG_EDITOR, "Setting h_base = %g", h_base);
376 editor->priv->w_base = w_base;
377 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");
408 /*****************************************************************************/
409 /* PRIVATE. Prefs changed callback. Update units related items. */
410 /*****************************************************************************/
412 size_prefs_changed_cb (glObjectEditor *editor)
415 const gchar *units_string;
419 gl_debug (DEBUG_EDITOR, "START");
421 /* Get new configuration information */
422 units = gl_prefs_model_get_units (gl_prefs);
423 units_string = lgl_units_get_name (units);
424 editor->priv->units_per_point = lgl_units_get_units_per_point (units);
425 climb_rate = gl_units_util_get_step_size (units);
426 digits = gl_units_util_get_precision (units);
428 /* Update characteristics of w_spin/h_spin */
429 editor->priv->stop_signals = TRUE;
430 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_w_spin),
432 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
434 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_w_spin),
435 climb_rate, 10.0*climb_rate);
436 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
437 climb_rate, 10.0*climb_rate);
438 editor->priv->stop_signals = FALSE;
440 /* Update units_labels */
441 gtk_label_set_text (GTK_LABEL(editor->priv->size_w_units_label),
443 gtk_label_set_text (GTK_LABEL(editor->priv->size_h_units_label),
446 /* Update values of w_spin/h_spin */
447 gl_object_editor_set_size (editor,
450 gl_object_editor_set_max_size (editor,
452 editor->priv->h_max);
454 gl_debug (DEBUG_EDITOR, "END");
460 * Local Variables: -- emacs
462 * c-basic-offset: 8 -- emacs
463 * tab-width: 8 -- emacs
464 * indent-tabs-mode: nil -- emacs