]> git.sur5r.net Git - glabels/blob - src/object-editor-shadow-page.c
Imported Upstream version 2.2.8
[glabels] / 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 || !merge_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         if (editor->priv->shadow_page_vbox)
285         {
286
287                 editor->priv->stop_signals = TRUE;
288
289                 /* save a copy in internal units */
290                 editor->priv->shadow_x_max = x_max;
291                 editor->priv->shadow_y_max = y_max;
292
293                 /* convert internal units to displayed units */
294                 gl_debug (DEBUG_EDITOR, "internal x_max,y_max = %g, %g", x_max, y_max);
295                 x_max *= editor->priv->units_per_point;
296                 y_max *= editor->priv->units_per_point;
297                 gl_debug (DEBUG_EDITOR, "display x_max,y_max = %g, %g", x_max, y_max);
298
299                 /* Set widget values */
300                 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin));
301                 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin),
302                                            -x_max, x_max);
303                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_x_spin), tmp);
304                 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin));
305                 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin),
306                                            -y_max, y_max);
307                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->shadow_y_spin), tmp);
308
309                 editor->priv->stop_signals = FALSE;
310
311         }
312
313         gl_debug (DEBUG_EDITOR, "END");
314 }
315
316 /*****************************************************************************/
317 /* Query shadow parameters.                                                  */
318 /*****************************************************************************/
319 gboolean
320 gl_object_editor_get_shadow_state (glObjectEditor      *editor)
321 {
322         gboolean state;
323
324         gl_debug (DEBUG_EDITOR, "START");
325
326         state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check));
327
328         gl_debug (DEBUG_EDITOR, "END");
329
330         return state;
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 glColorNode*
356 gl_object_editor_get_shadow_color (glObjectEditor      *editor)
357 {
358         GdkColor    *gdk_color;
359         gboolean     is_default;
360         glColorNode *color_node;
361  
362         gl_debug (DEBUG_EDITOR, "START");
363
364         color_node = gl_color_node_new_default ();
365         
366         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_key_radio))) {
367                 color_node->field_flag = TRUE;
368                 color_node->key = 
369                         gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->shadow_key_combo));
370     } else {
371                 color_node->field_flag = FALSE;
372                 color_node->key = NULL;
373                 gdk_color = color_combo_get_color (COLOR_COMBO(editor->priv->shadow_color_combo),
374                                            &is_default);
375
376                 if (!is_default) {
377                 color_node->color = gl_color_from_gdk_color (gdk_color);
378         }
379         }
380
381         gl_debug (DEBUG_EDITOR, "END");
382
383         return color_node;
384 }
385
386 gdouble
387 gl_object_editor_get_shadow_opacity (glObjectEditor      *editor)
388 {
389         gdouble alpha;
390
391         gl_debug (DEBUG_EDITOR, "START");
392
393         alpha = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->shadow_opacity_spin));
394
395         gl_debug (DEBUG_EDITOR, "END");
396
397         return alpha / 100.0;
398 }
399
400
401 /*****************************************************************************/
402 /* PRIVATE. Prefs changed callback.  Update units related items.            */
403 /*****************************************************************************/
404 void
405 shadow_prefs_changed_cb (glObjectEditor *editor)
406 {
407         const gchar  *units_string;
408         gdouble       climb_rate;
409         gint          digits;
410
411         gl_debug (DEBUG_EDITOR, "START");
412
413         /* Get new configuration information */
414         units_string = gl_prefs_get_units_string ();
415         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
416         climb_rate = gl_prefs_get_units_step_size ();
417         digits = gl_prefs_get_units_precision ();
418
419         /* Update characteristics of x_spin/y_spin */
420         editor->priv->stop_signals = TRUE;
421         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
422                                     digits);
423         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
424                                     digits);
425         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_x_spin),
426                                         climb_rate, 10.0*climb_rate);
427         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->shadow_y_spin),
428                                         climb_rate, 10.0*climb_rate);
429         editor->priv->stop_signals = FALSE;
430
431         /* Update units_labels */
432         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_x_units_label),
433                             units_string);
434         gtk_label_set_text (GTK_LABEL(editor->priv->shadow_y_units_label),
435                             units_string);
436
437         /* Update values of x_spin/y_spin */
438         gl_object_editor_set_shadow_offset (editor,
439                                             editor->priv->shadow_x,
440                                             editor->priv->shadow_y);
441         gl_object_editor_set_max_shadow_offset (editor,
442                                                 editor->priv->shadow_x_max,
443                                                 editor->priv->shadow_y_max);
444
445         gl_debug (DEBUG_EDITOR, "END");
446 }
447
448 /*--------------------------------------------------------------------------*/
449 /* PRIVATE.  shadow enable check callback.                                  */
450 /*--------------------------------------------------------------------------*/
451 static void
452 shadow_enable_check_toggled_cb (glObjectEditor *editor)
453 {
454         gboolean state;
455
456         if (editor->priv->stop_signals) return;
457
458         gl_debug (DEBUG_EDITOR, "START");
459
460         state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_enable_check));
461
462         gtk_widget_set_sensitive (editor->priv->shadow_controls_table, state);
463
464         /* Emit our "changed" signal */
465         g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
466
467         gl_debug (DEBUG_EDITOR, "END");
468 }
469
470
471 /*--------------------------------------------------------------------------*/
472 /* PRIVATE.  shadow color radio callback.                                   */
473 /*--------------------------------------------------------------------------*/
474 static void
475 shadow_color_radio_toggled_cb (glObjectEditor *editor)
476 {
477         if (editor->priv->stop_signals) return;
478
479         gl_debug (DEBUG_EDITOR, "START");
480         
481         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->shadow_color_radio))) {
482                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, TRUE);
483                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, FALSE);
484         } else {
485                 gtk_widget_set_sensitive (editor->priv->shadow_color_combo, FALSE);
486                 gtk_widget_set_sensitive (editor->priv->shadow_key_combo, TRUE);
487                 
488         }
489  
490         /* Emit our "changed" signal */
491         g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
492  
493         gl_debug (DEBUG_EDITOR, "END");
494 }