]> git.sur5r.net Git - glabels/blob - src/object-editor-size-page.c
Cleanup of build system.
[glabels] / src / object-editor-size-page.c
1 /*
2  *  object-editor-size-page.c
3  *  Copyright (C) 2003-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <config.h>
22
23 #include "object-editor.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <math.h>
28
29 #include "prefs.h"
30 #include "wdgt-chain-button.h"
31 #include "builder-util.h"
32 #include "units-util.h"
33
34 #include "object-editor-private.h"
35
36 #include "debug.h"
37
38
39 /*===========================================*/
40 /* Private macros                            */
41 /*===========================================*/
42
43
44 /*===========================================*/
45 /* Private data types                        */
46 /*===========================================*/
47
48
49 /*===========================================*/
50 /* Private globals                           */
51 /*===========================================*/
52
53
54 /*===========================================*/
55 /* Local function prototypes                 */
56 /*===========================================*/
57
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);
62
63
64 /*--------------------------------------------------------------------------*/
65 /* PRIVATE.  Prepare size page.                                             */
66 /*--------------------------------------------------------------------------*/
67 void
68 gl_object_editor_prepare_size_page (glObjectEditor       *editor)
69 {
70         lglUnits      units;
71         const gchar  *units_string;
72         gdouble       climb_rate;
73         gint          digits;
74
75         gl_debug (DEBUG_EDITOR, "START");
76
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,
86                                      NULL);
87
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),
91                                          TRUE);
92         gtk_box_pack_start (GTK_BOX (editor->priv->size_aspect_vbox),
93                             editor->priv->size_aspect_checkbutton,
94                             TRUE, TRUE, 0);
95
96
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);
103
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);
113
114
115         /* Connect signals */
116         g_signal_connect_swapped (G_OBJECT (editor->priv->size_aspect_checkbutton),
117                                   "toggled",
118                                   G_CALLBACK (aspect_toggle_cb),
119                                   G_OBJECT (editor));
120         g_signal_connect_swapped (G_OBJECT (editor->priv->size_w_spin),
121                                   "changed",
122                                   G_CALLBACK (w_spin_cb),
123                                   G_OBJECT (editor));
124         g_signal_connect_swapped (G_OBJECT (editor->priv->size_h_spin),
125                                   "changed",
126                                   G_CALLBACK (h_spin_cb),
127                                   G_OBJECT (editor));
128         g_signal_connect_swapped (G_OBJECT (editor->priv->size_reset_image_button),
129                                   "clicked",
130                                   G_CALLBACK (size_reset_cb),
131                                   G_OBJECT (editor));
132
133         gl_debug (DEBUG_EDITOR, "END");
134 }
135
136
137 /*--------------------------------------------------------------------------*/
138 /* PRIVATE.  Maintain aspect ratio checkbox callback.                       */
139 /*--------------------------------------------------------------------------*/
140 static void
141 aspect_toggle_cb (glObjectEditor *editor)
142 {
143         glWdgtChainButton *toggle;
144         gdouble            w, h;
145
146         if (editor->priv->stop_signals) return;
147
148         gl_debug (DEBUG_EDITOR, "START");
149
150         toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);
151
152         if (gl_wdgt_chain_button_get_active (toggle)) {
153                                                                                 
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));
156                                                                                 
157                 editor->priv->size_aspect_ratio = h / w;
158                                                                                 
159         }
160
161         gl_debug (DEBUG_EDITOR, "END");
162 }
163
164
165 /*--------------------------------------------------------------------------*/
166 /* PRIVATE.  W spin button changed callback.                                */
167 /*--------------------------------------------------------------------------*/
168 static void
169 w_spin_cb (glObjectEditor *editor)
170 {
171         gdouble            w, h;
172         glWdgtChainButton *toggle;
173
174         if (editor->priv->stop_signals) return;
175
176         gl_debug (DEBUG_EDITOR, "START");
177
178         toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);
179
180         if (gl_wdgt_chain_button_get_active (toggle)) {
181
182                 w = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin));
183                                                                                 
184                 h = w * editor->priv->size_aspect_ratio;
185                                                                                 
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;
190         }
191                                                                                 
192         gl_object_editor_size_changed_cb (editor);
193                                                                                 
194         gl_debug (DEBUG_EDITOR, "END");
195 }
196
197
198 /*--------------------------------------------------------------------------*/
199 /* PRIVATE.  H spin button changed callback.                                */
200 /*--------------------------------------------------------------------------*/
201 static void
202 h_spin_cb (glObjectEditor *editor)
203 {
204         gdouble            w, h;
205         glWdgtChainButton *toggle;
206
207         if (editor->priv->stop_signals) return;
208
209         gl_debug (DEBUG_EDITOR, "START");
210
211         toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);
212                                                                                 
213         if (gl_wdgt_chain_button_get_active (toggle)) {
214
215                 h = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin));
216                                                                                 
217                 w = h / editor->priv->size_aspect_ratio;
218                                                                                 
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;
223         }
224                                                                                 
225         gl_object_editor_size_changed_cb (editor);
226                                                                                 
227         gl_debug (DEBUG_EDITOR, "END");
228 }
229
230
231 /*--------------------------------------------------------------------------*/
232 /* PRIVATE.  Reset image size callback.                                     */
233 /*--------------------------------------------------------------------------*/
234 static void
235 size_reset_cb (glObjectEditor *editor)
236 {
237         gdouble w_base, h_base;
238         gdouble w_max, h_max, wh_max;
239         gdouble aspect_ratio;
240
241         if (editor->priv->stop_signals) return;
242
243         gl_debug (DEBUG_EDITOR, "START");
244
245         editor->priv->stop_signals = TRUE;
246
247         w_base = editor->priv->w_base;
248         h_base = editor->priv->h_base;
249
250         w_max = editor->priv->w_max;
251         h_max = editor->priv->h_max;
252         wh_max = MAX( w_max, h_max );
253
254         if ( (w_base > wh_max) || (h_base > wh_max) ) {
255
256                 aspect_ratio = h_base / w_base;
257
258                 if ( aspect_ratio < 1.0 ) {
259                         w_base = wh_max;
260                         h_base = wh_max * aspect_ratio;
261
262                 } else {
263                         w_base = wh_max / aspect_ratio;
264                         h_base = wh_max;
265                 }
266         }
267
268         w_base *= editor->priv->units_per_point;
269         h_base *= editor->priv->units_per_point;
270
271         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin),
272                                    w_base);
273         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin),
274                                    h_base);
275
276         editor->priv->stop_signals = FALSE;
277
278         gl_object_editor_size_changed_cb (editor);
279
280         gl_debug (DEBUG_EDITOR, "END");
281 }
282
283
284 /*****************************************************************************/
285 /* Set size.                                                                 */
286 /*****************************************************************************/
287 void
288 gl_object_editor_set_size (glObjectEditor      *editor,
289                              gdouble                w,
290                              gdouble                h)
291 {
292         gl_debug (DEBUG_EDITOR, "START");
293
294         editor->priv->stop_signals = TRUE;
295
296         /* save a copy in internal units */
297         editor->priv->w = w;
298         editor->priv->h = h;
299
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);
305
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);
309
310         /* Update aspect ratio */
311         editor->priv->size_aspect_ratio = h / w;
312
313         editor->priv->stop_signals = FALSE;
314
315         gl_debug (DEBUG_EDITOR, "END");
316 }
317
318
319 /*****************************************************************************/
320 /* Set maximum size.                                                         */
321 /*****************************************************************************/
322 void
323 gl_object_editor_set_max_size (glObjectEditor      *editor,
324                                gdouble              w_max,
325                                gdouble              h_max)
326 {
327         gdouble tmp;
328         gdouble wh_max;
329
330         gl_debug (DEBUG_EDITOR, "START");
331
332         if (editor->priv->size_page_vbox)
333         {
334
335                 editor->priv->stop_signals = TRUE;
336
337                 /* save a copy in internal units */
338                 editor->priv->w_max = w_max;
339                 editor->priv->h_max = h_max;
340
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);
347
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),
351                                            0.0, 2.0*wh_max);
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),
355                                            0.0, 2.0*wh_max);
356                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), tmp);
357
358                 editor->priv->stop_signals = FALSE;
359
360         }
361
362         gl_debug (DEBUG_EDITOR, "END");
363 }
364
365
366 /*****************************************************************************/
367 /* Set base or natural size of image.                                        */
368 /*****************************************************************************/
369 void
370 gl_object_editor_set_base_size    (glObjectEditor      *editor,
371                                    gdouble              w_base,
372                                    gdouble              h_base)
373 {
374         gl_debug (DEBUG_EDITOR, "Setting w_base = %g", w_base);
375         gl_debug (DEBUG_EDITOR, "Setting h_base = %g", h_base);
376
377         editor->priv->w_base = w_base;
378         editor->priv->h_base = h_base;
379 }
380
381
382 /*****************************************************************************/
383 /* Query size.                                                               */
384 /*****************************************************************************/
385 void
386 gl_object_editor_get_size (glObjectEditor      *editor,
387                            gdouble             *w,
388                            gdouble             *h)
389 {
390         gl_debug (DEBUG_EDITOR, "START");
391
392
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));
396
397         /* convert everything back to our internal units (points) */
398         *w /= editor->priv->units_per_point;
399         *h /= editor->priv->units_per_point;
400
401         /* save a copy in internal units */
402         editor->priv->w = *w;
403         editor->priv->h = *h;
404
405         gl_debug (DEBUG_EDITOR, "END");
406 }
407
408
409 /*****************************************************************************/
410 /* PRIVATE. Prefs changed callback.  Update units related items.            */
411 /*****************************************************************************/
412 void
413 size_prefs_changed_cb (glObjectEditor *editor)
414 {
415         lglUnits      units;
416         const gchar  *units_string;
417         gdouble       climb_rate;
418         gint          digits;
419
420         gl_debug (DEBUG_EDITOR, "START");
421
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);
428
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),
432                                     digits);
433         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
434                                     digits);
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;
440
441         /* Update units_labels */
442         gtk_label_set_text (GTK_LABEL(editor->priv->size_w_units_label),
443                             units_string);
444         gtk_label_set_text (GTK_LABEL(editor->priv->size_h_units_label),
445                             units_string);
446
447         /* Update values of w_spin/h_spin */
448         gl_object_editor_set_size (editor,
449                                    editor->priv->w,
450                                    editor->priv->h);
451         gl_object_editor_set_max_size (editor,
452                                        editor->priv->w_max,
453                                        editor->priv->h_max);
454
455         gl_debug (DEBUG_EDITOR, "END");
456 }
457
458
459
460 /*
461  * Local Variables:       -- emacs
462  * mode: C                -- emacs
463  * c-basic-offset: 8      -- emacs
464  * tab-width: 8           -- emacs
465  * indent-tabs-mode: nil  -- emacs
466  * End:                   -- emacs
467  */