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 gtk_widget_set_tooltip_text (editor->priv->size_aspect_checkbutton, _("Lock aspect ratio."));
90 gl_wdgt_chain_button_set_active (GL_WDGT_CHAIN_BUTTON(editor->priv->size_aspect_checkbutton),
92 gtk_box_pack_start (GTK_BOX (editor->priv->size_aspect_vbox),
93 editor->priv->size_aspect_checkbutton,
97 /* Get configuration information */
98 units = gl_prefs_model_get_units (gl_prefs);
99 units_string = lgl_units_get_name (units);
100 editor->priv->units_per_point = lgl_units_get_units_per_point (units);
101 climb_rate = gl_units_util_get_step_size (units);
102 digits = gl_units_util_get_precision (units);
104 /* Modify widgets based on configuration */
105 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_w_spin), digits);
106 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_w_spin),
107 climb_rate, 10.0*climb_rate);
108 gtk_label_set_text (GTK_LABEL(editor->priv->size_w_units_label), units_string);
109 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_h_spin), digits);
110 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
111 climb_rate, 10.0*climb_rate);
112 gtk_label_set_text (GTK_LABEL(editor->priv->size_h_units_label), units_string);
115 /* Connect signals */
116 g_signal_connect_swapped (G_OBJECT (editor->priv->size_aspect_checkbutton),
118 G_CALLBACK (aspect_toggle_cb),
120 g_signal_connect_swapped (G_OBJECT (editor->priv->size_w_spin),
122 G_CALLBACK (w_spin_cb),
124 g_signal_connect_swapped (G_OBJECT (editor->priv->size_h_spin),
126 G_CALLBACK (h_spin_cb),
128 g_signal_connect_swapped (G_OBJECT (editor->priv->size_reset_image_button),
130 G_CALLBACK (size_reset_cb),
133 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");
165 /*--------------------------------------------------------------------------*/
166 /* PRIVATE. W spin button changed callback. */
167 /*--------------------------------------------------------------------------*/
169 w_spin_cb (glObjectEditor *editor)
172 glWdgtChainButton *toggle;
174 if (editor->priv->stop_signals) return;
176 gl_debug (DEBUG_EDITOR, "START");
178 toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);
180 if (gl_wdgt_chain_button_get_active (toggle)) {
182 w = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin));
184 h = w * editor->priv->size_aspect_ratio;
186 /* Update our sibling control, blocking recursion. */
187 editor->priv->stop_signals = TRUE;
188 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), h);
189 editor->priv->stop_signals = FALSE;
192 gl_object_editor_size_changed_cb (editor);
194 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 gl_object_editor_size_changed_cb (editor);
227 gl_debug (DEBUG_EDITOR, "END");
231 /*--------------------------------------------------------------------------*/
232 /* PRIVATE. Reset image size callback. */
233 /*--------------------------------------------------------------------------*/
235 size_reset_cb (glObjectEditor *editor)
237 gdouble w_base, h_base;
238 gdouble w_max, h_max, wh_max;
239 gdouble aspect_ratio;
241 if (editor->priv->stop_signals) return;
243 gl_debug (DEBUG_EDITOR, "START");
245 editor->priv->stop_signals = TRUE;
247 w_base = editor->priv->w_base;
248 h_base = editor->priv->h_base;
250 w_max = editor->priv->w_max;
251 h_max = editor->priv->h_max;
252 wh_max = MAX( w_max, h_max );
254 if ( (w_base > wh_max) || (h_base > wh_max) ) {
256 aspect_ratio = h_base / w_base;
258 if ( aspect_ratio < 1.0 ) {
260 h_base = wh_max * aspect_ratio;
263 w_base = wh_max / aspect_ratio;
268 w_base *= editor->priv->units_per_point;
269 h_base *= editor->priv->units_per_point;
271 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin),
273 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin),
276 editor->priv->stop_signals = FALSE;
278 gl_object_editor_size_changed_cb (editor);
280 gl_debug (DEBUG_EDITOR, "END");
284 /*****************************************************************************/
286 /*****************************************************************************/
288 gl_object_editor_set_size (glObjectEditor *editor,
292 gl_debug (DEBUG_EDITOR, "START");
294 editor->priv->stop_signals = TRUE;
296 /* save a copy in internal units */
300 /* convert internal units to displayed units */
301 gl_debug (DEBUG_EDITOR, "internal w,h = %g, %g", w, h);
302 w *= editor->priv->units_per_point;
303 h *= editor->priv->units_per_point;
304 gl_debug (DEBUG_EDITOR, "display w,h = %g, %g", w, h);
306 /* Set widget values */
307 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin), w);
308 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), h);
310 /* Update aspect ratio */
311 editor->priv->size_aspect_ratio = h / w;
313 editor->priv->stop_signals = FALSE;
315 gl_debug (DEBUG_EDITOR, "END");
319 /*****************************************************************************/
320 /* Set maximum size. */
321 /*****************************************************************************/
323 gl_object_editor_set_max_size (glObjectEditor *editor,
330 gl_debug (DEBUG_EDITOR, "START");
332 if (editor->priv->size_page_vbox)
335 editor->priv->stop_signals = TRUE;
337 /* save a copy in internal units */
338 editor->priv->w_max = w_max;
339 editor->priv->h_max = h_max;
341 /* convert internal units to displayed units */
342 gl_debug (DEBUG_EDITOR, "internal w_max,h_max = %g, %g", w_max, h_max);
343 w_max *= editor->priv->units_per_point;
344 h_max *= editor->priv->units_per_point;
345 wh_max = MAX( w_max, h_max );
346 gl_debug (DEBUG_EDITOR, "display w_max,h_max = %g, %g", w_max, h_max);
348 /* Set widget values */
349 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin));
350 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->size_w_spin),
352 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin), tmp);
353 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin));
354 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->size_h_spin),
356 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), tmp);
358 editor->priv->stop_signals = FALSE;
362 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;
382 /*****************************************************************************/
384 /*****************************************************************************/
386 gl_object_editor_get_size (glObjectEditor *editor,
390 gl_debug (DEBUG_EDITOR, "START");
393 /* Get values from widgets */
394 *w = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin));
395 *h = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin));
397 /* convert everything back to our internal units (points) */
398 *w /= editor->priv->units_per_point;
399 *h /= editor->priv->units_per_point;
401 /* save a copy in internal units */
402 editor->priv->w = *w;
403 editor->priv->h = *h;
405 gl_debug (DEBUG_EDITOR, "END");
409 /*****************************************************************************/
410 /* PRIVATE. Prefs changed callback. Update units related items. */
411 /*****************************************************************************/
413 size_prefs_changed_cb (glObjectEditor *editor)
416 const gchar *units_string;
420 gl_debug (DEBUG_EDITOR, "START");
422 /* Get new configuration information */
423 units = gl_prefs_model_get_units (gl_prefs);
424 units_string = lgl_units_get_name (units);
425 editor->priv->units_per_point = lgl_units_get_units_per_point (units);
426 climb_rate = gl_units_util_get_step_size (units);
427 digits = gl_units_util_get_precision (units);
429 /* Update characteristics of w_spin/h_spin */
430 editor->priv->stop_signals = TRUE;
431 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_w_spin),
433 gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
435 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_w_spin),
436 climb_rate, 10.0*climb_rate);
437 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
438 climb_rate, 10.0*climb_rate);
439 editor->priv->stop_signals = FALSE;
441 /* Update units_labels */
442 gtk_label_set_text (GTK_LABEL(editor->priv->size_w_units_label),
444 gtk_label_set_text (GTK_LABEL(editor->priv->size_h_units_label),
447 /* Update values of w_spin/h_spin */
448 gl_object_editor_set_size (editor,
451 gl_object_editor_set_max_size (editor,
453 editor->priv->h_max);
455 gl_debug (DEBUG_EDITOR, "END");
461 * Local Variables: -- emacs
463 * c-basic-offset: 8 -- emacs
464 * tab-width: 8 -- emacs
465 * indent-tabs-mode: nil -- emacs