]> git.sur5r.net Git - glabels/blob - glabels2/src/object-editor-shadow-page.c
2008-10-12 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 "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         gl_debug (DEBUG_EDITOR, "START");
214
215         editor->priv->stop_signals = TRUE;
216
217         gtk_widget_set_sensitive (editor->priv->shadow_key_radio, merge_flag);
218
219         if ( color_node->color == GL_COLOR_NONE ) {
220
221                 gl_color_combo_set_to_default (GL_COLOR_COMBO(editor->priv->shadow_color_combo));
222
223         } else {
224
225                 gl_color_combo_set_color (GL_COLOR_COMBO(editor->priv->shadow_color_combo),
226                                           color_node->color);
227
228         }
229         
230         if (!color_node->field_flag) {
231                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
232                                                   (editor->priv->shadow_color_radio), TRUE); 
233                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, TRUE);
234                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, FALSE);
235                 
236         } else {
237                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
238                                                   (editor->priv->shadow_key_radio), TRUE); 
239                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, FALSE);
240                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, TRUE);
241                 
242                 gl_util_combo_box_set_active_text (GTK_COMBO_BOX (editor->priv->shadow_key_combo),
243                                                    color_node->key);
244         }
245         
246         editor->priv->stop_signals = FALSE;
247
248         gl_debug (DEBUG_EDITOR, "END");
249 }
250
251 void
252 gl_object_editor_set_shadow_opacity (glObjectEditor      *editor,
253                                      gdouble              alpha)
254 {
255         gl_debug (DEBUG_EDITOR, "START");
256
257         editor->priv->stop_signals = TRUE;
258
259         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_opacity_spin),
260                                    alpha * 100.0);
261
262         editor->priv->stop_signals = FALSE;
263
264         gl_debug (DEBUG_EDITOR, "END");
265 }
266
267
268 /*****************************************************************************/
269 /* Set maximum shadow offset.                                                */
270 /*****************************************************************************/
271 void
272 gl_object_editor_set_max_shadow_offset (glObjectEditor      *editor,
273                                         gdouble              x_max,
274                                         gdouble              y_max)
275 {
276         gdouble tmp;
277
278         gl_debug (DEBUG_EDITOR, "START");
279
280         if (editor->priv->shadow_page_vbox)
281         {
282
283                 editor->priv->stop_signals = TRUE;
284
285                 /* save a copy in internal units */
286                 editor->priv->shadow_x_max = x_max;
287                 editor->priv->shadow_y_max = y_max;
288
289                 /* convert internal units to displayed units */
290                 gl_debug (DEBUG_EDITOR, "internal x_max,y_max = %g, %g", x_max, y_max);
291                 x_max *= editor->priv->units_per_point;
292                 y_max *= editor->priv->units_per_point;
293                 gl_debug (DEBUG_EDITOR, "display x_max,y_max = %g, %g", x_max, y_max);
294
295                 /* Set widget values */
296                 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin));
297                 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin),
298                                            -x_max, x_max);
299                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin), tmp);
300                 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin));
301                 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin),
302                                            -y_max, y_max);
303                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin), tmp);
304
305                 editor->priv->stop_signals = FALSE;
306
307         }
308
309         gl_debug (DEBUG_EDITOR, "END");
310 }
311
312 /*****************************************************************************/
313 /* Query shadow parameters.                                                  */
314 /*****************************************************************************/
315 gboolean
316 gl_object_editor_get_shadow_state (glObjectEditor      *editor)
317 {
318         gboolean state;
319
320         gl_debug (DEBUG_EDITOR, "START");
321
322         state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check));
323
324         gl_debug (DEBUG_EDITOR, "END");
325
326         return state;
327 }
328
329 void
330 gl_object_editor_get_shadow_offset (glObjectEditor      *editor,
331                                     gdouble             *x,
332                                     gdouble             *y)
333 {
334         gl_debug (DEBUG_EDITOR, "START");
335
336         /* Get values from widgets */
337         *x = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin));
338         *y = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin));
339
340         /* convert everything back to our internal units (points) */
341         *x /= editor->priv->units_per_point;
342         *y /= editor->priv->units_per_point;
343
344         /* save a copy in internal units */
345         editor->priv->shadow_x = *x;
346         editor->priv->shadow_y = *y;
347
348         gl_debug (DEBUG_EDITOR, "END");
349 }
350
351 glColorNode*
352 gl_object_editor_get_shadow_color (glObjectEditor      *editor)
353 {
354         guint        color;
355         gboolean     is_default;
356         glColorNode *color_node;
357  
358         gl_debug (DEBUG_EDITOR, "START");
359
360         color_node = gl_color_node_new_default ();
361         
362         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_key_radio))) {
363                 color_node->field_flag = TRUE;
364                 color_node->key = 
365                         gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->shadow_key_combo));
366         } else {
367                 color_node->field_flag = FALSE;
368                 color_node->key = NULL;
369                 color = gl_color_combo_get_color (GL_COLOR_COMBO(editor->priv->shadow_color_combo),
370                                                   &is_default);
371
372                 if (!is_default) {
373                         color_node->color = color;
374                 }
375         }
376
377         gl_debug (DEBUG_EDITOR, "END");
378
379         return color_node;
380 }
381
382 gdouble
383 gl_object_editor_get_shadow_opacity (glObjectEditor      *editor)
384 {
385         gdouble alpha;
386
387         gl_debug (DEBUG_EDITOR, "START");
388
389         alpha = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_opacity_spin));
390
391         gl_debug (DEBUG_EDITOR, "END");
392
393         return alpha / 100.0;
394 }
395
396
397 /*****************************************************************************/
398 /* PRIVATE. Prefs changed callback.  Update units related items.            */
399 /*****************************************************************************/
400 void
401 shadow_prefs_changed_cb (glObjectEditor *editor)
402 {
403         const gchar  *units_string;
404         gdouble       climb_rate;
405         gint          digits;
406
407         gl_debug (DEBUG_EDITOR, "START");
408
409         /* Get new configuration information */
410         units_string = gl_prefs_get_units_string ();
411         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
412         climb_rate = gl_prefs_get_units_step_size ();
413         digits = gl_prefs_get_units_precision ();
414
415         /* Update characteristics of x_spin/y_spin */
416         editor->priv->stop_signals = TRUE;
417         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
418                                     digits);
419         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
420                                     digits);
421         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
422                                         climb_rate, 10.0*climb_rate);
423         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
424                                         climb_rate, 10.0*climb_rate);
425         editor->priv->stop_signals = FALSE;
426
427         /* Update units_labels */
428         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_x_units_label),
429                             units_string);
430         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_y_units_label),
431                             units_string);
432
433         /* Update values of x_spin/y_spin */
434         gl_object_editor_set_shadow_offset (editor,
435                                             editor->priv->shadow_x,
436                                             editor->priv->shadow_y);
437         gl_object_editor_set_max_shadow_offset (editor,
438                                                 editor->priv->shadow_x_max,
439                                                 editor->priv->shadow_y_max);
440
441         gl_debug (DEBUG_EDITOR, "END");
442 }
443
444 /*--------------------------------------------------------------------------*/
445 /* PRIVATE.  shadow enable check callback.                                  */
446 /*--------------------------------------------------------------------------*/
447 static void
448 shadow_enable_check_toggled_cb (glObjectEditor *editor)
449 {
450         gboolean state;
451
452         if (editor->priv->stop_signals) return;
453
454         gl_debug (DEBUG_EDITOR, "START");
455
456         state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check));
457
458         gtk_widget_set_sensitive (editor->priv->shadow_controls_table, state);
459
460         /* Emit our "changed" signal */
461         g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
462
463         gl_debug (DEBUG_EDITOR, "END");
464 }
465
466
467 /*--------------------------------------------------------------------------*/
468 /* PRIVATE.  shadow color radio callback.                                   */
469 /*--------------------------------------------------------------------------*/
470 static void
471 shadow_color_radio_toggled_cb (glObjectEditor *editor)
472 {
473         if (editor->priv->stop_signals) return;
474
475         gl_debug (DEBUG_EDITOR, "START");
476         
477         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_color_radio))) {
478                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, TRUE);
479                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, FALSE);
480         } else {
481                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, FALSE);
482                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, TRUE);
483                 
484         }
485  
486         /* Emit our "changed" signal */
487         g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
488  
489         gl_debug (DEBUG_EDITOR, "END");
490 }