]> git.sur5r.net Git - glabels/blob - src/object-editor-shadow-page.c
Imported Upstream version 3.0.0
[glabels] / src / object-editor-shadow-page.c
1 /*
2  *  object-editor-shadow-page.c
3  *  Copyright (C) 2006-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 "color-combo.h"
31 #include "color.h"
32 #include "field-button.h"
33 #include "builder-util.h"
34 #include "units-util.h"
35
36 #include "object-editor-private.h"
37
38 #include "debug.h"
39
40
41 /*===========================================*/
42 /* Private macros                            */
43 /*===========================================*/
44
45
46 /*===========================================*/
47 /* Private data types                        */
48 /*===========================================*/
49
50
51 /*===========================================*/
52 /* Private globals                           */
53 /*===========================================*/
54
55
56 /*===========================================*/
57 /* Local function prototypes                 */
58 /*===========================================*/
59
60 static void shadow_enable_check_toggled_cb (glObjectEditor        *editor);
61 static void shadow_color_radio_toggled_cb  (glObjectEditor        *editor);
62
63
64 /*--------------------------------------------------------------------------*/
65 /* PRIVATE.  Prepare shadow page.                                         */
66 /*--------------------------------------------------------------------------*/
67 void
68 gl_object_editor_prepare_shadow_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                                      "shadow_page_vbox",      &editor->priv->shadow_page_vbox,
80                                      "shadow_enable_check",   &editor->priv->shadow_enable_check,
81                                      "shadow_controls_table", &editor->priv->shadow_controls_table,
82                                      "shadow_x_spin",         &editor->priv->shadow_x_spin,
83                                      "shadow_y_spin",         &editor->priv->shadow_y_spin,
84                                      "shadow_x_units_label",  &editor->priv->shadow_x_units_label,
85                                      "shadow_y_units_label",  &editor->priv->shadow_y_units_label,
86                                      "shadow_color_radio",    &editor->priv->shadow_color_radio,
87                                      "shadow_key_radio",      &editor->priv->shadow_key_radio,
88                                      "shadow_color_hbox",     &editor->priv->shadow_color_hbox,
89                                      "shadow_key_hbox",       &editor->priv->shadow_key_hbox,
90                                      "shadow_opacity_spin",   &editor->priv->shadow_opacity_spin,
91                                      NULL);
92
93         editor->priv->shadow_color_combo = gl_color_combo_new (_("Default"),
94                                                                GL_COLOR_SHADOW_DEFAULT,
95                                                                GL_COLOR_SHADOW_DEFAULT);
96         gtk_container_add (GTK_CONTAINER (editor->priv->shadow_color_hbox),
97                            editor->priv->shadow_color_combo);
98
99         editor->priv->shadow_key_combo = gl_field_button_new (NULL);
100         gtk_box_pack_start (GTK_BOX (editor->priv->shadow_key_hbox),
101                             editor->priv->shadow_key_combo,
102                             TRUE, TRUE, 0);
103
104
105         /* Get configuration information */
106         units = gl_prefs_model_get_units (gl_prefs);
107         units_string = lgl_units_get_name (units);
108         editor->priv->units_per_point = lgl_units_get_units_per_point (units);
109         climb_rate = gl_units_util_get_step_size (units);
110         digits = gl_units_util_get_precision (units);
111
112         /* Modify widgets based on configuration */
113         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin), digits);
114         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
115                                         climb_rate, 10.0*climb_rate);
116         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_x_units_label), units_string);
117         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin), digits);
118         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
119                                         climb_rate, 10.0*climb_rate);
120         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_y_units_label), units_string);
121
122         /* Un-hide */
123         gtk_widget_show_all (editor->priv->shadow_page_vbox);
124
125         /* Connect signals */
126         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_enable_check),
127                                   "toggled",
128                                   G_CALLBACK (shadow_enable_check_toggled_cb),
129                                   G_OBJECT (editor));                             
130         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_x_spin),
131                                   "value-changed",
132                                   G_CALLBACK (gl_object_editor_changed_cb),
133                                   G_OBJECT (editor));
134         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_y_spin),
135                                   "value-changed",
136                                   G_CALLBACK (gl_object_editor_changed_cb),
137                                   G_OBJECT (editor));
138         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_color_combo),
139                                   "color_changed",
140                                   G_CALLBACK (gl_object_editor_changed_cb),
141                                   G_OBJECT (editor));
142         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_key_combo),
143                                   "changed",
144                                   G_CALLBACK (gl_object_editor_changed_cb),
145                                   G_OBJECT (editor));
146         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_color_radio),
147                                   "toggled",
148                                   G_CALLBACK (shadow_color_radio_toggled_cb),
149                                   G_OBJECT (editor));                             
150         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_key_radio),
151                                   "toggled",
152                                   G_CALLBACK (shadow_color_radio_toggled_cb),
153                                   G_OBJECT (editor));                             
154         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_opacity_spin),
155                                   "value-changed",
156                                   G_CALLBACK (gl_object_editor_changed_cb),
157                                   G_OBJECT (editor));
158
159         gl_debug (DEBUG_EDITOR, "END");
160 }
161
162
163 /*****************************************************************************/
164 /* Set shadow parameters.                                                    */
165 /*****************************************************************************/
166 void
167 gl_object_editor_set_shadow_state (glObjectEditor      *editor,
168                                    gboolean             state)
169 {
170         gl_debug (DEBUG_EDITOR, "START");
171
172
173         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_enable_check),
174                                          shadow_enable_check_toggled_cb, editor);
175
176
177         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check), state);
178         gtk_widget_set_sensitive (editor->priv->shadow_controls_table, state);
179
180
181         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_enable_check),
182                                            shadow_enable_check_toggled_cb, editor);
183
184
185         gl_debug (DEBUG_EDITOR, "END");
186 }
187
188
189 void
190 gl_object_editor_set_shadow_offset (glObjectEditor      *editor,
191                                     gdouble              x,
192                                     gdouble              y)
193 {
194         gl_debug (DEBUG_EDITOR, "START");
195
196
197         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_x_spin),
198                                          gl_object_editor_changed_cb, editor);
199         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_y_spin),
200                                          gl_object_editor_changed_cb, editor);
201
202
203         /* save a copy in internal units */
204         editor->priv->shadow_x = x;
205         editor->priv->shadow_y = y;
206
207         /* convert internal units to displayed units */
208         gl_debug (DEBUG_EDITOR, "internal x,y = %g, %g", x, y);
209         x *= editor->priv->units_per_point;
210         y *= editor->priv->units_per_point;
211         gl_debug (DEBUG_EDITOR, "display x,y = %g, %g", x, y);
212
213         /* Set widget values */
214         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin), x);
215         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin), y);
216
217
218         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_x_spin),
219                                            gl_object_editor_changed_cb, editor);
220         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_y_spin),
221                                            gl_object_editor_changed_cb, editor);
222
223
224         gl_debug (DEBUG_EDITOR, "END");
225 }
226
227
228 void
229 gl_object_editor_set_shadow_color (glObjectEditor      *editor,
230                                    gboolean             merge_flag,
231                                    glColorNode         *color_node)
232 {
233         gl_debug (DEBUG_EDITOR, "START");
234
235         if (color_node == NULL)
236         {
237                 return;
238         }
239
240
241         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_color_combo),
242                                          gl_object_editor_changed_cb, editor);
243         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_color_radio),
244                                          shadow_color_radio_toggled_cb, editor);
245         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_key_radio),
246                                          shadow_color_radio_toggled_cb, editor);
247         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_key_combo),
248                                          gl_object_editor_changed_cb, editor);
249
250
251         gtk_widget_set_sensitive (editor->priv->shadow_key_radio, merge_flag);
252
253         if ( color_node->color == GL_COLOR_NONE )
254         {
255                 gl_color_combo_set_to_default (GL_COLOR_COMBO(editor->priv->shadow_color_combo));
256         }
257         else
258         {
259                 gl_color_combo_set_color (GL_COLOR_COMBO(editor->priv->shadow_color_combo),
260                                           color_node->color);
261         }
262         
263         if (!color_node->field_flag || !merge_flag)
264         {
265                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_color_radio), TRUE); 
266                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, TRUE);
267                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, FALSE);
268                 
269         }
270         else
271         {
272                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_key_radio), TRUE); 
273                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, FALSE);
274                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, TRUE);
275                 
276                 gl_field_button_set_key (GL_FIELD_BUTTON (editor->priv->shadow_key_combo),
277                                          color_node->key);
278         }
279         
280
281         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_color_combo),
282                                            gl_object_editor_changed_cb, editor);
283         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_color_radio),
284                                            shadow_color_radio_toggled_cb, editor);
285         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_key_radio),
286                                            shadow_color_radio_toggled_cb, editor);
287         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_key_combo),
288                                            gl_object_editor_changed_cb, editor);
289
290
291         gl_debug (DEBUG_EDITOR, "END");
292 }
293
294
295 void
296 gl_object_editor_set_shadow_opacity (glObjectEditor      *editor,
297                                      gdouble              alpha)
298 {
299         gl_debug (DEBUG_EDITOR, "START");
300
301
302         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_opacity_spin),
303                                          gl_object_editor_changed_cb, editor);
304
305
306         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_opacity_spin), alpha * 100.0);
307
308
309         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_opacity_spin),
310                                            gl_object_editor_changed_cb, editor);
311
312
313         gl_debug (DEBUG_EDITOR, "END");
314 }
315
316
317 /*****************************************************************************/
318 /* Set maximum shadow offset.                                                */
319 /*****************************************************************************/
320 void
321 gl_object_editor_set_max_shadow_offset (glObjectEditor      *editor,
322                                         gdouble              x_max,
323                                         gdouble              y_max)
324 {
325         gdouble tmp;
326
327         gl_debug (DEBUG_EDITOR, "START");
328
329
330         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_x_spin),
331                                          gl_object_editor_changed_cb, editor);
332         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_y_spin),
333                                          gl_object_editor_changed_cb, editor);
334
335
336         /* save a copy in internal units */
337         editor->priv->shadow_x_max = x_max;
338         editor->priv->shadow_y_max = y_max;
339
340         /* convert internal units to displayed units */
341         gl_debug (DEBUG_EDITOR, "internal x_max,y_max = %g, %g", x_max, y_max);
342         x_max *= editor->priv->units_per_point;
343         y_max *= editor->priv->units_per_point;
344         gl_debug (DEBUG_EDITOR, "display x_max,y_max = %g, %g", x_max, y_max);
345
346         /* Set widget values */
347         tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin));
348         gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin), -x_max, x_max);
349         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin), tmp);
350         tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin));
351         gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin), -y_max, y_max);
352         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin), tmp);
353
354
355         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_x_spin),
356                                            gl_object_editor_changed_cb, editor);
357         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_y_spin),
358                                            gl_object_editor_changed_cb, editor);
359
360
361         gl_debug (DEBUG_EDITOR, "END");
362 }
363
364
365 /*****************************************************************************/
366 /* Query shadow parameters.                                                  */
367 /*****************************************************************************/
368 gboolean
369 gl_object_editor_get_shadow_state (glObjectEditor      *editor)
370 {
371         gboolean state;
372
373         gl_debug (DEBUG_EDITOR, "START");
374
375         state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check));
376
377         gl_debug (DEBUG_EDITOR, "END");
378
379         return state;
380 }
381
382
383 void
384 gl_object_editor_get_shadow_offset (glObjectEditor      *editor,
385                                     gdouble             *x,
386                                     gdouble             *y)
387 {
388         gl_debug (DEBUG_EDITOR, "START");
389
390         /* Get values from widgets */
391         *x = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin));
392         *y = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin));
393
394         /* convert everything back to our internal units (points) */
395         *x /= editor->priv->units_per_point;
396         *y /= editor->priv->units_per_point;
397
398         /* save a copy in internal units */
399         editor->priv->shadow_x = *x;
400         editor->priv->shadow_y = *y;
401
402         gl_debug (DEBUG_EDITOR, "END");
403 }
404
405
406 glColorNode*
407 gl_object_editor_get_shadow_color (glObjectEditor      *editor)
408 {
409         guint        color;
410         gboolean     is_default;
411         glColorNode *color_node;
412  
413         gl_debug (DEBUG_EDITOR, "START");
414
415         color_node = gl_color_node_new_default ();
416         
417         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_key_radio))) {
418                 color_node->field_flag = TRUE;
419                 color_node->key = 
420                         gl_field_button_get_key (GL_FIELD_BUTTON (editor->priv->shadow_key_combo));
421         } else {
422                 color_node->field_flag = FALSE;
423                 color_node->key = NULL;
424                 color = gl_color_combo_get_color (GL_COLOR_COMBO(editor->priv->shadow_color_combo),
425                                                   &is_default);
426
427                 if (!is_default) {
428                         color_node->color = color;
429                 }
430         }
431
432         gl_debug (DEBUG_EDITOR, "END");
433
434         return color_node;
435 }
436
437
438 gdouble
439 gl_object_editor_get_shadow_opacity (glObjectEditor      *editor)
440 {
441         gdouble alpha;
442
443         gl_debug (DEBUG_EDITOR, "START");
444
445         alpha = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_opacity_spin));
446
447         gl_debug (DEBUG_EDITOR, "END");
448
449         return alpha / 100.0;
450 }
451
452
453 /*****************************************************************************/
454 /* PRIVATE. Prefs changed callback.  Update units related items.            */
455 /*****************************************************************************/
456 void
457 shadow_prefs_changed_cb (glObjectEditor *editor)
458 {
459         lglUnits      units;
460         const gchar  *units_string;
461         gdouble       climb_rate;
462         gint          digits;
463
464         gl_debug (DEBUG_EDITOR, "START");
465
466
467         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_x_spin),
468                                          gl_object_editor_changed_cb, editor);
469         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->shadow_y_spin),
470                                          gl_object_editor_changed_cb, editor);
471
472
473         /* Get new configuration information */
474         units = gl_prefs_model_get_units (gl_prefs);
475         units_string = lgl_units_get_name (units);
476         editor->priv->units_per_point = lgl_units_get_units_per_point (units);
477         climb_rate = gl_units_util_get_step_size (units);
478         digits = gl_units_util_get_precision (units);
479
480         /* Update characteristics of x_spin/y_spin */
481         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
482                                     digits);
483         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
484                                     digits);
485         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
486                                         climb_rate, 10.0*climb_rate);
487         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
488                                         climb_rate, 10.0*climb_rate);
489
490         /* Update units_labels */
491         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_x_units_label),
492                             units_string);
493         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_y_units_label),
494                             units_string);
495
496         /* Update values of x_spin/y_spin */
497         gl_object_editor_set_shadow_offset (editor,
498                                             editor->priv->shadow_x,
499                                             editor->priv->shadow_y);
500         gl_object_editor_set_max_shadow_offset (editor,
501                                                 editor->priv->shadow_x_max,
502                                                 editor->priv->shadow_y_max);
503
504
505         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_x_spin),
506                                            gl_object_editor_changed_cb, editor);
507         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->shadow_y_spin),
508                                            gl_object_editor_changed_cb, editor);
509
510
511         gl_debug (DEBUG_EDITOR, "END");
512 }
513
514
515 /*--------------------------------------------------------------------------*/
516 /* PRIVATE.  shadow enable check callback.                                  */
517 /*--------------------------------------------------------------------------*/
518 static void
519 shadow_enable_check_toggled_cb (glObjectEditor *editor)
520 {
521         gboolean state;
522
523         gl_debug (DEBUG_EDITOR, "START");
524
525         state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check));
526
527         gtk_widget_set_sensitive (editor->priv->shadow_controls_table, state);
528
529         gl_object_editor_changed_cb (editor);
530
531         gl_debug (DEBUG_EDITOR, "END");
532 }
533
534
535 /*--------------------------------------------------------------------------*/
536 /* PRIVATE.  shadow color radio callback.                                   */
537 /*--------------------------------------------------------------------------*/
538 static void
539 shadow_color_radio_toggled_cb (glObjectEditor *editor)
540 {
541         gl_debug (DEBUG_EDITOR, "START");
542         
543         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_color_radio))) {
544                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, TRUE);
545                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, FALSE);
546         } else {
547                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, FALSE);
548                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, TRUE);
549                 
550         }
551  
552         gl_object_editor_changed_cb (editor);
553  
554         gl_debug (DEBUG_EDITOR, "END");
555 }
556
557
558
559 /*
560  * Local Variables:       -- emacs
561  * mode: C                -- emacs
562  * c-basic-offset: 8      -- emacs
563  * tab-width: 8           -- emacs
564  * indent-tabs-mode: nil  -- emacs
565  * End:                   -- emacs
566  */