summaryrefslogtreecommitdiff
path: root/src/object-editor-size-page.c
blob: ce7e6d521190b39a422f7c211960e9ea92d42832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 *  object-editor-size-page.c
 *  Copyright (C) 2003-2009  Jim Evins <evins@snaught.com>.
 *
 *  This file is part of gLabels.
 *
 *  gLabels is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  gLabels is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "object-editor.h"

#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <math.h>

#include "prefs.h"
#include "wdgt-chain-button.h"
#include "builder-util.h"
#include "units-util.h"

#include "object-editor-private.h"

#include "debug.h"


/*===========================================*/
/* Private macros                            */
/*===========================================*/


/*===========================================*/
/* Private data types                        */
/*===========================================*/


/*===========================================*/
/* Private globals                           */
/*===========================================*/


/*===========================================*/
/* Local function prototypes                 */
/*===========================================*/

static void aspect_toggle_cb                    (glObjectEditor        *editor);
static void size_reset_cb                       (glObjectEditor        *editor);
static void w_spin_cb                           (glObjectEditor        *editor);
static void h_spin_cb                           (glObjectEditor        *editor);


/*--------------------------------------------------------------------------*/
/* PRIVATE.  Prepare size page.                                             */
/*--------------------------------------------------------------------------*/
void
gl_object_editor_prepare_size_page (glObjectEditor       *editor)
{
        lglUnits      units;
	const gchar  *units_string;
	gdouble       climb_rate;
	gint          digits;

	gl_debug (DEBUG_EDITOR, "START");

	/* Extract widgets from XML tree. */
        gl_builder_util_get_widgets (editor->priv->builder,
                                     "size_page_vbox",          &editor->priv->size_page_vbox,
                                     "size_w_spin",             &editor->priv->size_w_spin,
                                     "size_h_spin",             &editor->priv->size_h_spin,
                                     "size_w_units_label",      &editor->priv->size_w_units_label,
                                     "size_h_units_label",      &editor->priv->size_h_units_label,
                                     "size_aspect_vbox",        &editor->priv->size_aspect_vbox,
                                     "size_reset_image_button", &editor->priv->size_reset_image_button,
                                     NULL);

	editor->priv->size_aspect_checkbutton = gl_wdgt_chain_button_new (GL_WDGT_CHAIN_RIGHT);
        gtk_widget_set_tooltip_text (editor->priv->size_aspect_checkbutton, _("Lock aspect ratio."));
	gl_wdgt_chain_button_set_active (GL_WDGT_CHAIN_BUTTON(editor->priv->size_aspect_checkbutton),
                                         TRUE);
        gtk_box_pack_start (GTK_BOX (editor->priv->size_aspect_vbox),
                            editor->priv->size_aspect_checkbutton,
                            TRUE, TRUE, 0);


	/* Get configuration information */
        units = gl_prefs_model_get_units (gl_prefs);
	units_string = lgl_units_get_name (units);
	editor->priv->units_per_point = lgl_units_get_units_per_point (units);
	climb_rate = gl_units_util_get_step_size (units);
	digits = gl_units_util_get_precision (units);

	/* Modify widgets based on configuration */
	gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_w_spin), digits);
	gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_w_spin),
					climb_rate, 10.0*climb_rate);
	gtk_label_set_text (GTK_LABEL(editor->priv->size_w_units_label), units_string);
	gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_h_spin), digits);
	gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
					climb_rate, 10.0*climb_rate);
	gtk_label_set_text (GTK_LABEL(editor->priv->size_h_units_label), units_string);


	/* Connect signals */
	g_signal_connect_swapped (G_OBJECT (editor->priv->size_aspect_checkbutton),
				  "toggled",
				  G_CALLBACK (aspect_toggle_cb),
				  G_OBJECT (editor));
	g_signal_connect_swapped (G_OBJECT (editor->priv->size_w_spin),
				  "value-changed",
				  G_CALLBACK (w_spin_cb),
				  G_OBJECT (editor));
	g_signal_connect_swapped (G_OBJECT (editor->priv->size_h_spin),
				  "value-changed",
				  G_CALLBACK (h_spin_cb),
				  G_OBJECT (editor));
        g_signal_connect_swapped (G_OBJECT (editor->priv->size_reset_image_button),
                                  "clicked",
                                  G_CALLBACK (size_reset_cb),
                                  G_OBJECT (editor));

	gl_debug (DEBUG_EDITOR, "END");
}


/*--------------------------------------------------------------------------*/
/* PRIVATE.  Maintain aspect ratio checkbox callback.                       */
/*--------------------------------------------------------------------------*/
static void
aspect_toggle_cb (glObjectEditor *editor)
{
        glWdgtChainButton *toggle;
	gdouble            w, h;

	gl_debug (DEBUG_EDITOR, "START");

	toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);

        if (gl_wdgt_chain_button_get_active (toggle)) {
                                                                                
                w = gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->size_w_spin));
                h = gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->size_h_spin));
                                                                                
                editor->priv->size_aspect_ratio = h / w;
                                                                                
        }

	gl_debug (DEBUG_EDITOR, "END");
}


/*--------------------------------------------------------------------------*/
/* PRIVATE.  W spin button changed callback.                                */
/*--------------------------------------------------------------------------*/
static void
w_spin_cb (glObjectEditor *editor)
{
	gdouble            w, h;
        glWdgtChainButton *toggle;

	gl_debug (DEBUG_EDITOR, "START");

	toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);

        if (gl_wdgt_chain_button_get_active (toggle)) {

		w = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin));
                                                                                
                h = w * editor->priv->size_aspect_ratio;
                                                                                
                /* Update our sibling control, blocking recursion. */
                g_signal_handlers_block_by_func (G_OBJECT (editor->priv->size_h_spin), h_spin_cb, editor);
                gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), h);
                g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->size_h_spin), h_spin_cb, editor);
        }
                                                                                
        gl_object_editor_size_changed_cb (editor);
                                                                                
	gl_debug (DEBUG_EDITOR, "END");
}


/*--------------------------------------------------------------------------*/
/* PRIVATE.  H spin button changed callback.                                */
/*--------------------------------------------------------------------------*/
static void
h_spin_cb (glObjectEditor *editor)
{
	gdouble            w, h;
        glWdgtChainButton *toggle;

	gl_debug (DEBUG_EDITOR, "START");

        toggle = GL_WDGT_CHAIN_BUTTON (editor->priv->size_aspect_checkbutton);
                                                                                
        if (gl_wdgt_chain_button_get_active (toggle)) {

		h = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin));
                                                                                
                w = h / editor->priv->size_aspect_ratio;
                                                                                
                /* Update our sibling control, blocking recursion. */
                g_signal_handlers_block_by_func (G_OBJECT (editor->priv->size_w_spin), w_spin_cb, editor);
                gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin), w);
                g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->size_w_spin), w_spin_cb, editor);
        }
                                                                                
        gl_object_editor_size_changed_cb (editor);
                                                                                
	gl_debug (DEBUG_EDITOR, "END");
}


/*--------------------------------------------------------------------------*/
/* PRIVATE.  Reset image size callback.                                     */
/*--------------------------------------------------------------------------*/
static void
size_reset_cb (glObjectEditor *editor)
{
	gdouble w_base, h_base;
	gdouble w_max, h_max, wh_max;
	gdouble aspect_ratio;

	gl_debug (DEBUG_EDITOR, "START");


        g_signal_handlers_block_by_func (G_OBJECT (editor->priv->size_w_spin), w_spin_cb, editor);
        g_signal_handlers_block_by_func (G_OBJECT (editor->priv->size_h_spin), h_spin_cb, editor);


	w_base = editor->priv->w_base;
	h_base = editor->priv->h_base;

	w_max = editor->priv->w_max;
	h_max = editor->priv->h_max;
        wh_max = MAX( w_max, h_max );

	if ( (w_base > wh_max) || (h_base > wh_max) ) {

		aspect_ratio = h_base / w_base;

		if ( aspect_ratio < 1.0 ) {
			w_base = wh_max;
			h_base = wh_max * aspect_ratio;

		} else {
			w_base = wh_max / aspect_ratio;
			h_base = wh_max;
		}
	}

	w_base *= editor->priv->units_per_point;
	h_base *= editor->priv->units_per_point;

	gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin),
				   w_base);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin),
				   h_base);


        g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->size_w_spin), w_spin_cb, editor);
        g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->size_h_spin), h_spin_cb, editor);


        gl_object_editor_size_changed_cb (editor);

	gl_debug (DEBUG_EDITOR, "END");
}


/*****************************************************************************/
/* Set size.                                                                 */
/*****************************************************************************/
void
gl_object_editor_set_size (glObjectEditor      *editor,
                           gdouble              w,
                           gdouble              h)
{
	gl_debug (DEBUG_EDITOR, "START");


        g_signal_handlers_block_by_func (G_OBJECT (editor->priv->size_w_spin), w_spin_cb, editor);
        g_signal_handlers_block_by_func (G_OBJECT (editor->priv->size_h_spin), h_spin_cb, editor);


	/* save a copy in internal units */
	editor->priv->w = w;
	editor->priv->h = h;

	/* convert internal units to displayed units */
	gl_debug (DEBUG_EDITOR, "internal w,h = %g, %g", w, h);
	w *= editor->priv->units_per_point;
	h *= editor->priv->units_per_point;
	gl_debug (DEBUG_EDITOR, "display w,h = %g, %g", w, h);

	/* Set widget values */
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin), w);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), h);

	/* Update aspect ratio */
	editor->priv->size_aspect_ratio = h / w;


        g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->size_w_spin), w_spin_cb, editor);
        g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->size_h_spin), h_spin_cb, editor);


	gl_debug (DEBUG_EDITOR, "END");
}


/*****************************************************************************/
/* Set maximum size.                                                         */
/*****************************************************************************/
void
gl_object_editor_set_max_size (glObjectEditor      *editor,
			       gdouble              w_max,
			       gdouble              h_max)
{
	gdouble tmp;
        gdouble wh_max;

	gl_debug (DEBUG_EDITOR, "START");


        g_signal_handlers_block_by_func (G_OBJECT (editor->priv->size_w_spin), w_spin_cb, editor);
        g_signal_handlers_block_by_func (G_OBJECT (editor->priv->size_h_spin), h_spin_cb, editor);


        /* save a copy in internal units */
        editor->priv->w_max = w_max;
        editor->priv->h_max = h_max;

        /* convert internal units to displayed units */
        gl_debug (DEBUG_EDITOR, "internal w_max,h_max = %g, %g", w_max, h_max);
        w_max *= editor->priv->units_per_point;
        h_max *= editor->priv->units_per_point;
        wh_max = MAX( w_max, h_max );
        gl_debug (DEBUG_EDITOR, "display w_max,h_max = %g, %g", w_max, h_max);

        /* Set widget values */
        tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin));
        gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->size_w_spin),
                                   0.0, 2.0*wh_max);
        gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin), tmp);
        tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin));
        gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->size_h_spin),
                                   0.0, 2.0*wh_max);
        gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin), tmp);


        g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->size_w_spin), w_spin_cb, editor);
        g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->size_h_spin), h_spin_cb, editor);


	gl_debug (DEBUG_EDITOR, "END");
}


/*****************************************************************************/
/* Set base or natural size of image.                                        */
/*****************************************************************************/
void
gl_object_editor_set_base_size    (glObjectEditor      *editor,
				   gdouble              w_base,
				   gdouble              h_base)
{
	gl_debug (DEBUG_EDITOR, "Setting w_base = %g", w_base);
	gl_debug (DEBUG_EDITOR, "Setting h_base = %g", h_base);

	editor->priv->w_base = w_base;
	editor->priv->h_base = h_base;
}


/*****************************************************************************/
/* Query size.                                                               */
/*****************************************************************************/
void
gl_object_editor_get_size (glObjectEditor      *editor,
			   gdouble             *w,
			   gdouble             *h)
{
	gl_debug (DEBUG_EDITOR, "START");


	/* Get values from widgets */
	*w = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_w_spin));
	*h = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->size_h_spin));

	/* convert everything back to our internal units (points) */
	*w /= editor->priv->units_per_point;
	*h /= editor->priv->units_per_point;

	/* save a copy in internal units */
	editor->priv->w = *w;
	editor->priv->h = *h;

	gl_debug (DEBUG_EDITOR, "END");
}


/*****************************************************************************/
/* PRIVATE. Prefs changed callback.  Update units related items.            */
/*****************************************************************************/
void
size_prefs_changed_cb (glObjectEditor *editor)
{
        lglUnits      units;
	const gchar  *units_string;
	gdouble       climb_rate;
	gint          digits;

	gl_debug (DEBUG_EDITOR, "START");


        g_signal_handlers_block_by_func (G_OBJECT (editor->priv->size_w_spin), w_spin_cb, editor);
        g_signal_handlers_block_by_func (G_OBJECT (editor->priv->size_h_spin), h_spin_cb, editor);


        /* Get new configuration information */
        units = gl_prefs_model_get_units (gl_prefs);
        units_string = lgl_units_get_name (units);
        editor->priv->units_per_point = lgl_units_get_units_per_point (units);
        climb_rate = gl_units_util_get_step_size (units);
        digits = gl_units_util_get_precision (units);

	/* Update characteristics of w_spin/h_spin */
	gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_w_spin), digits);
	gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->size_h_spin), digits);
	gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_w_spin),
					climb_rate, 10.0*climb_rate);
	gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->size_h_spin),
					climb_rate, 10.0*climb_rate);

	/* Update units_labels */
	gtk_label_set_text (GTK_LABEL(editor->priv->size_w_units_label),
			    units_string);
	gtk_label_set_text (GTK_LABEL(editor->priv->size_h_units_label),
			    units_string);

	/* Update values of w_spin/h_spin */
	gl_object_editor_set_size (editor, editor->priv->w, editor->priv->h);
	gl_object_editor_set_max_size (editor, editor->priv->w_max, editor->priv->h_max);


        g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->size_w_spin), w_spin_cb, editor);
        g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->size_h_spin), h_spin_cb, editor);


	gl_debug (DEBUG_EDITOR, "END");
}



/*
 * Local Variables:       -- emacs
 * mode: C                -- emacs
 * c-basic-offset: 8      -- emacs
 * tab-width: 8           -- emacs
 * indent-tabs-mode: nil  -- emacs
 * End:                   -- emacs
 */