]> git.sur5r.net Git - glabels/blob - src/object-editor-shadow-page.c
Reference glabels.org website
[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 "combo-util.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_combo",      &editor->priv->shadow_key_combo,
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         gl_combo_util_add_text_model ( GTK_COMBO_BOX(editor->priv->shadow_key_combo));
100
101         /* Get configuration information */
102         units = gl_prefs_model_get_units (gl_prefs);
103         units_string = lgl_units_get_name (units);
104         editor->priv->units_per_point = lgl_units_get_units_per_point (units);
105         climb_rate = gl_units_util_get_step_size (units);
106         digits = gl_units_util_get_precision (units);
107
108         /* Modify widgets based on configuration */
109         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin), digits);
110         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
111                                         climb_rate, 10.0*climb_rate);
112         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_x_units_label), units_string);
113         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin), digits);
114         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
115                                         climb_rate, 10.0*climb_rate);
116         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_y_units_label), units_string);
117
118         /* Un-hide */
119         gtk_widget_show_all (editor->priv->shadow_page_vbox);
120
121         /* Connect signals */
122         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_enable_check),
123                                   "toggled",
124                                   G_CALLBACK (shadow_enable_check_toggled_cb),
125                                   G_OBJECT (editor));                             
126         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_x_spin),
127                                   "changed",
128                                   G_CALLBACK (gl_object_editor_changed_cb),
129                                   G_OBJECT (editor));
130         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_y_spin),
131                                   "changed",
132                                   G_CALLBACK (gl_object_editor_changed_cb),
133                                   G_OBJECT (editor));
134         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_color_combo),
135                                   "color_changed",
136                                   G_CALLBACK (gl_object_editor_changed_cb),
137                                   G_OBJECT (editor));
138         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_key_combo),
139                                   "changed",
140                                   G_CALLBACK (gl_object_editor_changed_cb),
141                                   G_OBJECT (editor));
142         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_color_radio),
143                                   "toggled",
144                                   G_CALLBACK (shadow_color_radio_toggled_cb),
145                                   G_OBJECT (editor));                             
146         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_key_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_opacity_spin),
151                                   "changed",
152                                   G_CALLBACK (gl_object_editor_changed_cb),
153                                   G_OBJECT (editor));
154
155         gl_debug (DEBUG_EDITOR, "END");
156 }
157
158
159 /*****************************************************************************/
160 /* Set shadow parameters.                                                    */
161 /*****************************************************************************/
162 void
163 gl_object_editor_set_shadow_state (glObjectEditor      *editor,
164                                    gboolean             state)
165 {
166         gl_debug (DEBUG_EDITOR, "START");
167
168         editor->priv->stop_signals = TRUE;
169
170         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check),
171                                       state);
172         gtk_widget_set_sensitive (editor->priv->shadow_controls_table, state);
173
174         editor->priv->stop_signals = FALSE;
175
176         gl_debug (DEBUG_EDITOR, "END");
177 }
178
179
180 void
181 gl_object_editor_set_shadow_offset (glObjectEditor      *editor,
182                                     gdouble              x,
183                                     gdouble              y)
184 {
185         gl_debug (DEBUG_EDITOR, "START");
186
187         editor->priv->stop_signals = TRUE;
188
189         /* save a copy in internal units */
190         editor->priv->shadow_x = x;
191         editor->priv->shadow_y = y;
192
193         /* convert internal units to displayed units */
194         gl_debug (DEBUG_EDITOR, "internal x,y = %g, %g", x, y);
195         x *= editor->priv->units_per_point;
196         y *= editor->priv->units_per_point;
197         gl_debug (DEBUG_EDITOR, "display x,y = %g, %g", x, y);
198
199         /* Set widget values */
200         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin), x);
201         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin), y);
202
203         editor->priv->stop_signals = FALSE;
204
205         gl_debug (DEBUG_EDITOR, "END");
206 }
207
208
209 void
210 gl_object_editor_set_shadow_color (glObjectEditor      *editor,
211                                    gboolean             merge_flag,
212                                    glColorNode         *color_node)
213 {
214         gl_debug (DEBUG_EDITOR, "START");
215
216         editor->priv->stop_signals = TRUE;
217
218         gtk_widget_set_sensitive (editor->priv->shadow_key_radio, merge_flag);
219
220         if ( color_node->color == GL_COLOR_NONE ) {
221
222                 gl_color_combo_set_to_default (GL_COLOR_COMBO(editor->priv->shadow_color_combo));
223
224         } else {
225
226                 gl_color_combo_set_color (GL_COLOR_COMBO(editor->priv->shadow_color_combo),
227                                           color_node->color);
228
229         }
230         
231         if (!color_node->field_flag || !merge_flag) {
232                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
233                                                   (editor->priv->shadow_color_radio), TRUE); 
234                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, TRUE);
235                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, FALSE);
236                 
237         } else {
238                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
239                                                   (editor->priv->shadow_key_radio), TRUE); 
240                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, FALSE);
241                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, TRUE);
242                 
243                 gl_combo_util_set_active_text (GTK_COMBO_BOX (editor->priv->shadow_key_combo),
244                                                color_node->key);
245         }
246         
247         editor->priv->stop_signals = FALSE;
248
249         gl_debug (DEBUG_EDITOR, "END");
250 }
251
252
253 void
254 gl_object_editor_set_shadow_opacity (glObjectEditor      *editor,
255                                      gdouble              alpha)
256 {
257         gl_debug (DEBUG_EDITOR, "START");
258
259         editor->priv->stop_signals = TRUE;
260
261         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_opacity_spin),
262                                    alpha * 100.0);
263
264         editor->priv->stop_signals = FALSE;
265
266         gl_debug (DEBUG_EDITOR, "END");
267 }
268
269
270 /*****************************************************************************/
271 /* Set maximum shadow offset.                                                */
272 /*****************************************************************************/
273 void
274 gl_object_editor_set_max_shadow_offset (glObjectEditor      *editor,
275                                         gdouble              x_max,
276                                         gdouble              y_max)
277 {
278         gdouble tmp;
279
280         gl_debug (DEBUG_EDITOR, "START");
281
282         if (editor->priv->shadow_page_vbox)
283         {
284
285                 editor->priv->stop_signals = TRUE;
286
287                 /* save a copy in internal units */
288                 editor->priv->shadow_x_max = x_max;
289                 editor->priv->shadow_y_max = y_max;
290
291                 /* convert internal units to displayed units */
292                 gl_debug (DEBUG_EDITOR, "internal x_max,y_max = %g, %g", x_max, y_max);
293                 x_max *= editor->priv->units_per_point;
294                 y_max *= editor->priv->units_per_point;
295                 gl_debug (DEBUG_EDITOR, "display x_max,y_max = %g, %g", x_max, y_max);
296
297                 /* Set widget values */
298                 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin));
299                 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin),
300                                            -x_max, x_max);
301                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin), tmp);
302                 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin));
303                 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin),
304                                            -y_max, y_max);
305                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin), tmp);
306
307                 editor->priv->stop_signals = FALSE;
308
309         }
310
311         gl_debug (DEBUG_EDITOR, "END");
312 }
313
314
315 /*****************************************************************************/
316 /* Query shadow parameters.                                                  */
317 /*****************************************************************************/
318 gboolean
319 gl_object_editor_get_shadow_state (glObjectEditor      *editor)
320 {
321         gboolean state;
322
323         gl_debug (DEBUG_EDITOR, "START");
324
325         state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check));
326
327         gl_debug (DEBUG_EDITOR, "END");
328
329         return state;
330 }
331
332
333 void
334 gl_object_editor_get_shadow_offset (glObjectEditor      *editor,
335                                     gdouble             *x,
336                                     gdouble             *y)
337 {
338         gl_debug (DEBUG_EDITOR, "START");
339
340         /* Get values from widgets */
341         *x = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin));
342         *y = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin));
343
344         /* convert everything back to our internal units (points) */
345         *x /= editor->priv->units_per_point;
346         *y /= editor->priv->units_per_point;
347
348         /* save a copy in internal units */
349         editor->priv->shadow_x = *x;
350         editor->priv->shadow_y = *y;
351
352         gl_debug (DEBUG_EDITOR, "END");
353 }
354
355
356 glColorNode*
357 gl_object_editor_get_shadow_color (glObjectEditor      *editor)
358 {
359         guint        color;
360         gboolean     is_default;
361         glColorNode *color_node;
362  
363         gl_debug (DEBUG_EDITOR, "START");
364
365         color_node = gl_color_node_new_default ();
366         
367         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_key_radio))) {
368                 color_node->field_flag = TRUE;
369                 color_node->key = 
370                         gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->shadow_key_combo));
371         } else {
372                 color_node->field_flag = FALSE;
373                 color_node->key = NULL;
374                 color = gl_color_combo_get_color (GL_COLOR_COMBO(editor->priv->shadow_color_combo),
375                                                   &is_default);
376
377                 if (!is_default) {
378                         color_node->color = color;
379                 }
380         }
381
382         gl_debug (DEBUG_EDITOR, "END");
383
384         return color_node;
385 }
386
387
388 gdouble
389 gl_object_editor_get_shadow_opacity (glObjectEditor      *editor)
390 {
391         gdouble alpha;
392
393         gl_debug (DEBUG_EDITOR, "START");
394
395         alpha = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_opacity_spin));
396
397         gl_debug (DEBUG_EDITOR, "END");
398
399         return alpha / 100.0;
400 }
401
402
403 /*****************************************************************************/
404 /* PRIVATE. Prefs changed callback.  Update units related items.            */
405 /*****************************************************************************/
406 void
407 shadow_prefs_changed_cb (glObjectEditor *editor)
408 {
409         lglUnits      units;
410         const gchar  *units_string;
411         gdouble       climb_rate;
412         gint          digits;
413
414         gl_debug (DEBUG_EDITOR, "START");
415
416         /* Get new configuration information */
417         units = gl_prefs_model_get_units (gl_prefs);
418         units_string = lgl_units_get_name (units);
419         editor->priv->units_per_point = lgl_units_get_units_per_point (units);
420         climb_rate = gl_units_util_get_step_size (units);
421         digits = gl_units_util_get_precision (units);
422
423         /* Update characteristics of x_spin/y_spin */
424         editor->priv->stop_signals = TRUE;
425         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
426                                     digits);
427         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
428                                     digits);
429         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
430                                         climb_rate, 10.0*climb_rate);
431         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
432                                         climb_rate, 10.0*climb_rate);
433         editor->priv->stop_signals = FALSE;
434
435         /* Update units_labels */
436         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_x_units_label),
437                             units_string);
438         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_y_units_label),
439                             units_string);
440
441         /* Update values of x_spin/y_spin */
442         gl_object_editor_set_shadow_offset (editor,
443                                             editor->priv->shadow_x,
444                                             editor->priv->shadow_y);
445         gl_object_editor_set_max_shadow_offset (editor,
446                                                 editor->priv->shadow_x_max,
447                                                 editor->priv->shadow_y_max);
448
449         gl_debug (DEBUG_EDITOR, "END");
450 }
451
452
453 /*--------------------------------------------------------------------------*/
454 /* PRIVATE.  shadow enable check callback.                                  */
455 /*--------------------------------------------------------------------------*/
456 static void
457 shadow_enable_check_toggled_cb (glObjectEditor *editor)
458 {
459         gboolean state;
460
461         if (editor->priv->stop_signals) return;
462
463         gl_debug (DEBUG_EDITOR, "START");
464
465         state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check));
466
467         gtk_widget_set_sensitive (editor->priv->shadow_controls_table, state);
468
469         /* Emit our "changed" signal */
470         g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
471
472         gl_debug (DEBUG_EDITOR, "END");
473 }
474
475
476 /*--------------------------------------------------------------------------*/
477 /* PRIVATE.  shadow color radio callback.                                   */
478 /*--------------------------------------------------------------------------*/
479 static void
480 shadow_color_radio_toggled_cb (glObjectEditor *editor)
481 {
482         if (editor->priv->stop_signals) return;
483
484         gl_debug (DEBUG_EDITOR, "START");
485         
486         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_color_radio))) {
487                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, TRUE);
488                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, FALSE);
489         } else {
490                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, FALSE);
491                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, TRUE);
492                 
493         }
494  
495         /* Emit our "changed" signal */
496         g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
497  
498         gl_debug (DEBUG_EDITOR, "END");
499 }
500
501
502
503 /*
504  * Local Variables:       -- emacs
505  * mode: C                -- emacs
506  * c-basic-offset: 8      -- emacs
507  * tab-width: 8           -- emacs
508  * indent-tabs-mode: nil  -- emacs
509  * End:                   -- emacs
510  */