]> git.sur5r.net Git - glabels/blob - glabels2/src/object-editor-shadow-page.c
2007-11-26 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / object-editor-shadow-page.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  object-editor.c:  object properties editor module
7  *
8  *  Copyright (C) 2006  Jim Evins <evins@snaught.com>.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24 #include <config.h>
25
26 #include "object-editor.h"
27
28 #include <glib/gi18n.h>
29 #include <gtk/gtktogglebutton.h>
30 #include <gtk/gtklabel.h>
31 #include <gtk/gtkspinbutton.h>
32 #include <gtk/gtkcombobox.h>
33 #include <math.h>
34
35 #include "prefs.h"
36 #include "mygal/widget-color-combo.h"
37 #include "color.h"
38 #include "util.h"
39
40 #include "object-editor-private.h"
41
42 #include "debug.h"
43
44 /*===========================================*/
45 /* Private macros                            */
46 /*===========================================*/
47
48 /*===========================================*/
49 /* Private data types                        */
50 /*===========================================*/
51
52 /*===========================================*/
53 /* Private globals                           */
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         const gchar  *units_string;
71         gdouble       climb_rate;
72         gint          digits;
73
74         gl_debug (DEBUG_EDITOR, "START");
75
76         /* Extract widgets from XML tree. */
77         editor->priv->shadow_page_vbox
78                 = glade_xml_get_widget (editor->priv->gui, "shadow_page_vbox");
79         editor->priv->shadow_enable_check
80                 = glade_xml_get_widget (editor->priv->gui, "shadow_enable_check");
81         editor->priv->shadow_controls_table
82                 = glade_xml_get_widget (editor->priv->gui, "shadow_controls_table");
83         editor->priv->shadow_x_spin
84                 = glade_xml_get_widget (editor->priv->gui, "shadow_x_spin");
85         editor->priv->shadow_y_spin
86                 = glade_xml_get_widget (editor->priv->gui, "shadow_y_spin");
87         editor->priv->shadow_x_units_label
88                 = glade_xml_get_widget (editor->priv->gui, "shadow_x_units_label");
89         editor->priv->shadow_y_units_label
90                 = glade_xml_get_widget (editor->priv->gui, "shadow_y_units_label");
91         editor->priv->shadow_color_radio
92                 = glade_xml_get_widget (editor->priv->gui, "shadow_color_radio");       
93         editor->priv->shadow_key_radio
94                 = glade_xml_get_widget (editor->priv->gui, "shadow_key_radio"); 
95         editor->priv->shadow_color_combo
96                 = glade_xml_get_widget (editor->priv->gui, "shadow_color_combo");
97         editor->priv->shadow_key_combo
98                 = glade_xml_get_widget (editor->priv->gui, "shadow_key_combo"); 
99         editor->priv->shadow_opacity_spin
100                 = glade_xml_get_widget (editor->priv->gui, "shadow_opacity_spin");
101
102         gl_util_combo_box_add_text_model ( GTK_COMBO_BOX(editor->priv->shadow_key_combo));
103
104         /* Get configuration information */
105         units_string = gl_prefs_get_units_string ();
106         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
107         climb_rate = gl_prefs_get_units_step_size ();
108         digits = gl_prefs_get_units_precision ();
109
110         /* Modify widgets based on configuration */
111         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin), digits);
112         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
113                                         climb_rate, 10.0*climb_rate);
114         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_x_units_label), units_string);
115         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin), digits);
116         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
117                                         climb_rate, 10.0*climb_rate);
118         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_y_units_label), units_string);
119
120         /* Un-hide */
121         gtk_widget_show_all (editor->priv->shadow_page_vbox);
122
123         /* Connect signals */
124         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_enable_check),
125                                   "toggled",
126                                   G_CALLBACK (shadow_enable_check_toggled_cb),
127                                   G_OBJECT (editor));                             
128         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_x_spin),
129                                   "changed",
130                                   G_CALLBACK (gl_object_editor_changed_cb),
131                                   G_OBJECT (editor));
132         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_y_spin),
133                                   "changed",
134                                   G_CALLBACK (gl_object_editor_changed_cb),
135                                   G_OBJECT (editor));
136         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_color_combo),
137                                   "color_changed",
138                                   G_CALLBACK (gl_object_editor_changed_cb),
139                                   G_OBJECT (editor));
140         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_key_combo),
141                                   "changed",
142                                   G_CALLBACK (gl_object_editor_changed_cb),
143                                   G_OBJECT (editor));
144         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_color_radio),
145                                   "toggled",
146                                   G_CALLBACK (shadow_color_radio_toggled_cb),
147                                   G_OBJECT (editor));                             
148         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_key_radio),
149                                   "toggled",
150                                   G_CALLBACK (shadow_color_radio_toggled_cb),
151                                   G_OBJECT (editor));                             
152         g_signal_connect_swapped (G_OBJECT (editor->priv->shadow_opacity_spin),
153                                   "changed",
154                                   G_CALLBACK (gl_object_editor_changed_cb),
155                                   G_OBJECT (editor));
156
157         gl_debug (DEBUG_EDITOR, "END");
158 }
159
160 /*****************************************************************************/
161 /* Set shadow parameters.                                                    */
162 /*****************************************************************************/
163 void
164 gl_object_editor_set_shadow_state (glObjectEditor      *editor,
165                                    gboolean             state)
166 {
167         gl_debug (DEBUG_EDITOR, "START");
168
169         editor->priv->stop_signals = TRUE;
170
171         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check),
172                                       state);
173         gtk_widget_set_sensitive (editor->priv->shadow_controls_table, state);
174
175         editor->priv->stop_signals = FALSE;
176
177         gl_debug (DEBUG_EDITOR, "END");
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 void
209 gl_object_editor_set_shadow_color (glObjectEditor      *editor,
210                                    gboolean             merge_flag,
211                                    glColorNode         *color_node)
212 {
213         GdkColor *gdk_color;
214
215         gl_debug (DEBUG_EDITOR, "START");
216
217         editor->priv->stop_signals = TRUE;
218
219         gtk_widget_set_sensitive (editor->priv->shadow_key_radio, merge_flag);
220
221         if ( color_node->color == GL_COLOR_NONE ) {
222
223                 color_combo_set_color_to_default (COLOR_COMBO(editor->priv->shadow_color_combo));
224
225         } else {
226
227                 gdk_color = gl_color_to_gdk_color (color_node->color);
228                 color_combo_set_color (COLOR_COMBO(editor->priv->shadow_color_combo),
229                                            gdk_color);
230                 g_free (gdk_color);
231
232         }
233         
234         if (!color_node->field_flag) {
235                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
236                                                   (editor->priv->shadow_color_radio), TRUE); 
237                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, TRUE);
238                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, FALSE);
239                 
240         } else {
241                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
242                                                   (editor->priv->shadow_key_radio), TRUE); 
243                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, FALSE);
244                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, TRUE);
245                 
246                 gl_util_combo_box_set_active_text (GTK_COMBO_BOX (editor->priv->shadow_key_combo),
247                                                    color_node->key);
248         }
249         
250         editor->priv->stop_signals = FALSE;
251
252         gl_debug (DEBUG_EDITOR, "END");
253 }
254
255 void
256 gl_object_editor_set_shadow_opacity (glObjectEditor      *editor,
257                                      gdouble              alpha)
258 {
259         gl_debug (DEBUG_EDITOR, "START");
260
261         editor->priv->stop_signals = TRUE;
262
263         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_opacity_spin),
264                                    alpha * 100.0);
265
266         editor->priv->stop_signals = FALSE;
267
268         gl_debug (DEBUG_EDITOR, "END");
269 }
270
271
272 /*****************************************************************************/
273 /* Set maximum shadow offset.                                                */
274 /*****************************************************************************/
275 void
276 gl_object_editor_set_max_shadow_offset (glObjectEditor      *editor,
277                                         gdouble              x_max,
278                                         gdouble              y_max)
279 {
280         gdouble tmp;
281
282         gl_debug (DEBUG_EDITOR, "START");
283
284         editor->priv->stop_signals = TRUE;
285
286         /* save a copy in internal units */
287         editor->priv->shadow_x_max = x_max;
288         editor->priv->shadow_y_max = y_max;
289
290         /* convert internal units to displayed units */
291         gl_debug (DEBUG_EDITOR, "internal x_max,y_max = %g, %g", x_max, y_max);
292         x_max *= editor->priv->units_per_point;
293         y_max *= editor->priv->units_per_point;
294         gl_debug (DEBUG_EDITOR, "display x_max,y_max = %g, %g", x_max, y_max);
295
296         /* Set widget values */
297         tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin));
298         gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin),
299                                    -x_max, x_max);
300         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin), tmp);
301         tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin));
302         gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin),
303                                    -y_max, y_max);
304         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin), tmp);
305
306         editor->priv->stop_signals = FALSE;
307
308         gl_debug (DEBUG_EDITOR, "END");
309 }
310
311 /*****************************************************************************/
312 /* Query shadow parameters.                                                  */
313 /*****************************************************************************/
314 gboolean
315 gl_object_editor_get_shadow_state (glObjectEditor      *editor)
316 {
317         gboolean state;
318
319         gl_debug (DEBUG_EDITOR, "START");
320
321         state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check));
322
323         gl_debug (DEBUG_EDITOR, "END");
324
325         return state;
326 }
327
328 void
329 gl_object_editor_get_shadow_offset (glObjectEditor      *editor,
330                                     gdouble             *x,
331                                     gdouble             *y)
332 {
333         gl_debug (DEBUG_EDITOR, "START");
334
335         /* Get values from widgets */
336         *x = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin));
337         *y = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin));
338
339         /* convert everything back to our internal units (points) */
340         *x /= editor->priv->units_per_point;
341         *y /= editor->priv->units_per_point;
342
343         /* save a copy in internal units */
344         editor->priv->shadow_x = *x;
345         editor->priv->shadow_y = *y;
346
347         gl_debug (DEBUG_EDITOR, "END");
348 }
349
350 glColorNode*
351 gl_object_editor_get_shadow_color (glObjectEditor      *editor)
352 {
353         GdkColor    *gdk_color;
354         gboolean     is_default;
355         glColorNode *color_node;
356  
357         gl_debug (DEBUG_EDITOR, "START");
358
359         color_node = gl_color_node_new_default ();
360         
361         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_key_radio))) {
362                 color_node->field_flag = TRUE;
363                 color_node->key = 
364                         gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->shadow_key_combo));
365     } else {
366                 color_node->field_flag = FALSE;
367                 color_node->key = NULL;
368                 gdk_color = color_combo_get_color (COLOR_COMBO(editor->priv->shadow_color_combo),
369                                            &is_default);
370
371                 if (!is_default) {
372                 color_node->color = gl_color_from_gdk_color (gdk_color);
373         }
374         }
375
376         gl_debug (DEBUG_EDITOR, "END");
377
378         return color_node;
379 }
380
381 gdouble
382 gl_object_editor_get_shadow_opacity (glObjectEditor      *editor)
383 {
384         gdouble alpha;
385
386         gl_debug (DEBUG_EDITOR, "START");
387
388         alpha = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_opacity_spin));
389
390         gl_debug (DEBUG_EDITOR, "END");
391
392         return alpha / 100.0;
393 }
394
395
396 /*****************************************************************************/
397 /* PRIVATE. Prefs changed callback.  Update units related items.            */
398 /*****************************************************************************/
399 void
400 shadow_prefs_changed_cb (glObjectEditor *editor)
401 {
402         const gchar  *units_string;
403         gdouble       climb_rate;
404         gint          digits;
405
406         gl_debug (DEBUG_EDITOR, "START");
407
408         /* Get new configuration information */
409         units_string = gl_prefs_get_units_string ();
410         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
411         climb_rate = gl_prefs_get_units_step_size ();
412         digits = gl_prefs_get_units_precision ();
413
414         /* Update characteristics of x_spin/y_spin */
415         editor->priv->stop_signals = TRUE;
416         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
417                                     digits);
418         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
419                                     digits);
420         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
421                                         climb_rate, 10.0*climb_rate);
422         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
423                                         climb_rate, 10.0*climb_rate);
424         editor->priv->stop_signals = FALSE;
425
426         /* Update units_labels */
427         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_x_units_label),
428                             units_string);
429         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_y_units_label),
430                             units_string);
431
432         /* Update values of x_spin/y_spin */
433         gl_object_editor_set_shadow_offset (editor,
434                                             editor->priv->shadow_x,
435                                             editor->priv->shadow_y);
436         gl_object_editor_set_max_shadow_offset (editor,
437                                                 editor->priv->shadow_x_max,
438                                                 editor->priv->shadow_y_max);
439
440         gl_debug (DEBUG_EDITOR, "END");
441 }
442
443 /*--------------------------------------------------------------------------*/
444 /* PRIVATE.  shadow enable check callback.                                  */
445 /*--------------------------------------------------------------------------*/
446 static void
447 shadow_enable_check_toggled_cb (glObjectEditor *editor)
448 {
449         gboolean state;
450
451         if (editor->priv->stop_signals) return;
452
453         gl_debug (DEBUG_EDITOR, "START");
454
455         state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check));
456
457         gtk_widget_set_sensitive (editor->priv->shadow_controls_table, state);
458
459         /* Emit our "changed" signal */
460         g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
461
462         gl_debug (DEBUG_EDITOR, "END");
463 }
464
465
466 /*--------------------------------------------------------------------------*/
467 /* PRIVATE.  shadow color radio callback.                                   */
468 /*--------------------------------------------------------------------------*/
469 static void
470 shadow_color_radio_toggled_cb (glObjectEditor *editor)
471 {
472         if (editor->priv->stop_signals) return;
473
474         gl_debug (DEBUG_EDITOR, "START");
475         
476         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_color_radio))) {
477                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, TRUE);
478                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, FALSE);
479         } else {
480                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, FALSE);
481                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, TRUE);
482                 
483         }
484  
485         /* Emit our "changed" signal */
486         g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
487  
488         gl_debug (DEBUG_EDITOR, "END");
489 }