]> git.sur5r.net Git - glabels/blob - glabels2/src/wdgt-text-props.c
- Replaced gnome color pickers with mygal color combos.
[glabels] / glabels2 / src / wdgt-text-props.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  wdgt_text_props.c:  text properties widget module
5  *
6  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <config.h>
24
25 #include <gnome.h>
26
27 #include "mygal/widget-color-combo.h"
28 #include "prefs.h"
29 #include "wdgt-text-props.h"
30 #include "marshal.h"
31 #include "color.h"
32
33 #include "debug.h"
34
35 /*===========================================*/
36 /* Private types                             */
37 /*===========================================*/
38
39 enum {
40         CHANGED,
41         LAST_SIGNAL
42 };
43
44 typedef void (*glWdgtTextPropsSignal) (GObject *object, gpointer data);
45
46 /*===========================================*/
47 /* Private globals                           */
48 /*===========================================*/
49
50 static GObjectClass *parent_class;
51
52 static gint wdgt_text_props_signals[LAST_SIGNAL] = { 0 };
53
54 /*===========================================*/
55 /* Local function prototypes                 */
56 /*===========================================*/
57
58 static void gl_wdgt_text_props_class_init    (glWdgtTextPropsClass *class);
59 static void gl_wdgt_text_props_instance_init (glWdgtTextProps      *text);
60 static void gl_wdgt_text_props_finalize      (GObject              *object);
61 static void gl_wdgt_text_props_construct     (glWdgtTextProps      *text);
62
63 static void family_changed_cb (GtkEntry        *entry,
64                                glWdgtTextProps *text);
65
66 static void changed_cb        (glWdgtTextProps *text);
67
68 static void just_toggled_cb   (GtkToggleButton *togglebutton,
69                                gpointer         user_data);
70 \f
71 /*****************************************************************************/
72 /* Boilerplate Object stuff.                                                 */
73 /*****************************************************************************/
74 guint
75 gl_wdgt_text_props_get_type (void)
76 {
77         static guint wdgt_text_props_type = 0;
78
79         if (!wdgt_text_props_type) {
80                 GTypeInfo wdgt_text_props_info = {
81                         sizeof (glWdgtTextPropsClass),
82                         NULL,
83                         NULL,
84                         (GClassInitFunc) gl_wdgt_text_props_class_init,
85                         NULL,
86                         NULL,
87                         sizeof (glWdgtTextProps),
88                         0,
89                         (GInstanceInitFunc) gl_wdgt_text_props_instance_init,
90                 };
91
92                 wdgt_text_props_type =
93                     g_type_register_static (gl_hig_vbox_get_type (),
94                                             "glWdgtTextProps",
95                                             &wdgt_text_props_info, 0);
96         }
97
98         return wdgt_text_props_type;
99 }
100
101 static void
102 gl_wdgt_text_props_class_init (glWdgtTextPropsClass *class)
103 {
104         GObjectClass *object_class;
105
106         object_class = (GObjectClass *) class;
107
108         parent_class = g_type_class_peek_parent (class);
109
110         object_class->finalize = gl_wdgt_text_props_finalize;
111
112         wdgt_text_props_signals[CHANGED] =
113             g_signal_new ("changed",
114                           G_OBJECT_CLASS_TYPE(object_class),
115                           G_SIGNAL_RUN_LAST,
116                           G_STRUCT_OFFSET (glWdgtTextPropsClass, changed),
117                           NULL, NULL,
118                           gl_marshal_VOID__VOID,
119                           G_TYPE_NONE, 0);
120
121 }
122
123 static void
124 gl_wdgt_text_props_instance_init (glWdgtTextProps *text)
125 {
126         text->font_family_entry = NULL;
127         text->font_size_spin = NULL;
128         text->font_b_button = NULL;
129         text->font_i_button = NULL;
130
131         text->color_picker = NULL;
132
133         text->left_button = NULL;
134         text->right_button = NULL;
135         text->center_button = NULL;
136 }
137
138 static void
139 gl_wdgt_text_props_finalize (GObject *object)
140 {
141         glWdgtTextProps *text;
142
143         g_return_if_fail (object != NULL);
144         g_return_if_fail (GL_IS_WDGT_TEXT_PROPS (object));
145
146         text = GL_WDGT_TEXT_PROPS (object);
147
148         G_OBJECT_CLASS (parent_class)->finalize (object);
149 }
150
151 /*****************************************************************************/
152 /* New widget.                                                               */
153 /*****************************************************************************/
154 GtkWidget *
155 gl_wdgt_text_props_new (void)
156 {
157         glWdgtTextProps *text;
158
159         text = g_object_new (gl_wdgt_text_props_get_type (), NULL);
160
161         gl_wdgt_text_props_construct (text);
162
163         return GTK_WIDGET (text);
164 }
165
166 /*--------------------------------------------------------------------------*/
167 /* PRIVATE. Construct composite widget.                                     */
168 /*--------------------------------------------------------------------------*/
169 static void
170 gl_wdgt_text_props_construct (glWdgtTextProps *text)
171 {
172         GtkWidget  *wvbox, *whbox, *wcombo, *wbhbox;
173         GList      *family_names = NULL;
174         GtkObject  *adjust;
175         ColorGroup *cg;
176         GdkColor   *gdk_color;
177
178         wvbox = GTK_WIDGET (text);
179
180         /* ---- Font line ---- */
181         whbox = gl_hig_hbox_new ();
182         gl_hig_vbox_add_widget (GL_HIG_VBOX(wvbox), whbox);
183
184         /* Font label */
185         text->font_label = gtk_label_new (_("Font:"));
186         gtk_misc_set_alignment (GTK_MISC (text->font_label), 0, 0.5);
187         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), text->font_label);
188
189         /* Font family entry widget */
190         wcombo = gtk_combo_new ();
191         family_names = gnome_font_family_list ();
192         gtk_combo_set_popdown_strings (GTK_COMBO (wcombo), family_names);
193         gnome_font_family_list_free (family_names);
194         text->font_family_entry = GTK_COMBO (wcombo)->entry;
195         gtk_combo_set_value_in_list (GTK_COMBO(wcombo), TRUE, FALSE);
196         gtk_entry_set_editable (GTK_ENTRY (text->font_family_entry), FALSE);
197         gtk_widget_set_size_request (wcombo, 200, -1);
198         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), wcombo);
199         g_signal_connect (G_OBJECT (text->font_family_entry),
200                           "changed", G_CALLBACK (family_changed_cb), text);
201
202         /* Font size entry widget */
203         adjust = gtk_adjustment_new (1.0, 1.0, 250.0, 1.0, 10.0, 10.0);
204         text->font_size_spin =
205             gtk_spin_button_new (GTK_ADJUSTMENT (adjust), 1.0, 0);
206         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), text->font_size_spin);
207         g_signal_connect_swapped (G_OBJECT (text->font_size_spin), "changed",
208                                   G_CALLBACK (changed_cb),
209                                   G_OBJECT (text));
210
211         /* Font weight/italic button widgets */
212         text->font_b_button = gtk_toggle_button_new ();
213         gtk_container_add (GTK_CONTAINER (text->font_b_button),
214                            gtk_image_new_from_stock (GTK_STOCK_BOLD,
215                                                      GTK_ICON_SIZE_BUTTON));
216         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), text->font_b_button);
217         g_signal_connect_swapped (G_OBJECT (text->font_b_button), "toggled",
218                                   G_CALLBACK (changed_cb),
219                                   G_OBJECT (text));
220         text->font_i_button = gtk_toggle_button_new ();
221         gtk_container_add (GTK_CONTAINER (text->font_i_button),
222                            gtk_image_new_from_stock (GTK_STOCK_ITALIC,
223                                                      GTK_ICON_SIZE_BUTTON));
224         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), text->font_i_button);
225         g_signal_connect_swapped (G_OBJECT (text->font_i_button), "toggled",
226                                   G_CALLBACK (changed_cb),
227                                   G_OBJECT (text));
228
229         /* ---- Color line ---- */
230         whbox = gl_hig_hbox_new ();
231         gl_hig_vbox_add_widget (GL_HIG_VBOX(wvbox), whbox);
232
233         /* Text Color Label */
234         text->color_label = gtk_label_new (_("Color:"));
235         gtk_misc_set_alignment (GTK_MISC (text->color_label), 0, 0.5);
236         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), text->color_label);
237
238         /* Text Color picker widget */
239         cg = color_group_fetch ("text_color_group", NULL);
240         gdk_color = gl_color_to_gdk_color (gl_prefs->default_line_color);
241         text->color_picker = color_combo_new (NULL, _("Default"), gdk_color, cg);
242         color_combo_box_set_preview_relief (COLOR_COMBO(text->color_picker),
243                                             GTK_RELIEF_NORMAL);
244         g_free (gdk_color);
245         g_signal_connect_swapped (G_OBJECT (text->color_picker), "color_changed",
246                                   G_CALLBACK (changed_cb),
247                                   G_OBJECT (text));
248         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), text->color_picker);
249
250         /* ---- Alignment line ---- */
251         whbox = gl_hig_hbox_new ();
252         gl_hig_vbox_add_widget (GL_HIG_VBOX(wvbox), whbox);
253
254         /* Alignment label */
255         text->alignment_label = gtk_label_new (_("Alignment:"));
256         gtk_misc_set_alignment (GTK_MISC (text->alignment_label), 0, 0.5);
257         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), text->alignment_label);
258
259         /* Justification entry widget */
260         text->left_button = gtk_toggle_button_new ();
261         gtk_container_add (GTK_CONTAINER (text->left_button),
262                            gtk_image_new_from_stock (GTK_STOCK_JUSTIFY_LEFT,
263                                                      GTK_ICON_SIZE_BUTTON));
264         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), text->left_button);
265         text->center_button = gtk_toggle_button_new ();
266         gtk_container_add (GTK_CONTAINER (text->center_button),
267                            gtk_image_new_from_stock (GTK_STOCK_JUSTIFY_CENTER,
268                                                      GTK_ICON_SIZE_BUTTON));
269         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), text->center_button);
270         text->right_button = gtk_toggle_button_new ();
271         gtk_container_add (GTK_CONTAINER (text->right_button),
272                            gtk_image_new_from_stock (GTK_STOCK_JUSTIFY_RIGHT,
273                                                      GTK_ICON_SIZE_BUTTON));
274         gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), text->right_button);
275
276         /* Now connect a callback that makes these toggles mutually exclusive */
277         g_signal_connect (G_OBJECT (text->left_button), "toggled",
278                           G_CALLBACK (just_toggled_cb), text);
279         g_signal_connect (G_OBJECT (text->center_button), "toggled",
280                           G_CALLBACK (just_toggled_cb), text);
281         g_signal_connect (G_OBJECT (text->right_button), "toggled",
282                           G_CALLBACK (just_toggled_cb), text);
283
284 }
285
286 /*--------------------------------------------------------------------------*/
287 /* PRIVATE.  modify widget due to change in selection                       */
288 /*--------------------------------------------------------------------------*/
289 static void
290 family_changed_cb (GtkEntry        *entry,
291                    glWdgtTextProps *text)
292 {
293         gchar *family_name;
294
295         family_name = gtk_editable_get_chars (GTK_EDITABLE (entry), 0, -1);
296         if ( strlen(family_name) ) {
297                 /* Emit our "changed" signal */
298                 g_signal_emit (G_OBJECT (text),
299                                wdgt_text_props_signals[CHANGED], 0);
300         }
301         g_free (family_name);
302 }
303
304 /*--------------------------------------------------------------------------*/
305 /* PRIVATE.  Callback for when any control in the widget has changed.       */
306 /*--------------------------------------------------------------------------*/
307 static void
308 changed_cb (glWdgtTextProps *text)
309 {
310         /* Emit our "changed" signal */
311         g_signal_emit (G_OBJECT (text), wdgt_text_props_signals[CHANGED], 0);
312 }
313 \f
314 /*--------------------------------------------------------------------------*/
315 /* PRIVATE.  Justify toggle button callback.                                */
316 /*--------------------------------------------------------------------------*/
317 static void
318 just_toggled_cb (GtkToggleButton *togglebutton,
319                  gpointer         user_data)
320 {
321         glWdgtTextProps *text = GL_WDGT_TEXT_PROPS (user_data);
322
323         if (gtk_toggle_button_get_active (togglebutton)) {
324
325                 if (GTK_WIDGET (togglebutton) == GTK_WIDGET (text->left_button)) {
326                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
327                                                       (text->center_button),
328                                                       FALSE);
329                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
330                                                       (text->right_button),
331                                                       FALSE);
332                 } else if (GTK_WIDGET (togglebutton) ==
333                            GTK_WIDGET (text->center_button)) {
334                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
335                                                       (text->left_button),
336                                                       FALSE);
337                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
338                                                       (text->right_button),
339                                                       FALSE);
340                 } else if (GTK_WIDGET (togglebutton) ==
341                            GTK_WIDGET (text->right_button)) {
342                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
343                                                       (text->left_button),
344                                                       FALSE);
345                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
346                                                       (text->center_button),
347                                                       FALSE);
348                 }
349
350         }
351
352         /* Emit our "changed" signal */
353         g_signal_emit (G_OBJECT (text), wdgt_text_props_signals[CHANGED], 0);
354 }
355 \f
356 /*****************************************************************************/
357 /* query values from controls.                                               */
358 /*****************************************************************************/
359 void
360 gl_wdgt_text_props_get_params (glWdgtTextProps  *text,
361                                gchar            **font_family,
362                                gdouble          *font_size,
363                                GnomeFontWeight  *font_weight,
364                                gboolean         *font_italic_flag,
365                                guint            *color,
366                                GtkJustification *just)
367 {
368         GdkColor *gdk_color;
369         gboolean  is_default;
370
371         /* ------ Get updated font information ------ */
372         *font_family =
373             gtk_editable_get_chars (GTK_EDITABLE (text->font_family_entry), 0,
374                                     -1);
375         *font_size =
376             gtk_spin_button_get_value (GTK_SPIN_BUTTON(text->font_size_spin));
377         if (gtk_toggle_button_get_active
378             (GTK_TOGGLE_BUTTON (text->font_b_button))) {
379                 *font_weight = GNOME_FONT_BOLD;
380         } else {
381                 *font_weight = GNOME_FONT_BOOK;
382         }
383         *font_italic_flag =
384             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
385                                           (text->font_i_button));
386
387         /* ------ Get updated color ------ */
388         gdk_color = color_combo_get_color (COLOR_COMBO(text->color_picker),
389                                            &is_default);
390
391         if (is_default) {
392                 *color = gl_prefs->default_text_color;
393         } else {
394                 *color = gl_color_from_gdk_color (gdk_color);
395         }
396
397         /* ------- Get updated justification ------ */
398         if (gtk_toggle_button_get_active
399             (GTK_TOGGLE_BUTTON (text->left_button))) {
400                 *just = GTK_JUSTIFY_LEFT;
401         } else
402             if (gtk_toggle_button_get_active
403                 (GTK_TOGGLE_BUTTON (text->right_button))) {
404                 *just = GTK_JUSTIFY_RIGHT;
405         } else
406             if (gtk_toggle_button_get_active
407                 (GTK_TOGGLE_BUTTON (text->center_button))) {
408                 *just = GTK_JUSTIFY_CENTER;
409         } else {
410                 *just = GTK_JUSTIFY_LEFT;       /* Should not happen. */
411         }
412
413 }
414
415 /*****************************************************************************/
416 /* fill in values and ranges for controls.                                   */
417 /*****************************************************************************/
418 void
419 gl_wdgt_text_props_set_params (glWdgtTextProps  *text,
420                                gchar            *font_family,
421                                gdouble          font_size,
422                                GnomeFontWeight  font_weight,
423                                gboolean         font_italic_flag,
424                                guint            color,
425                                GtkJustification just)
426 {
427         GList    *family_names;
428         gchar    *good_font_family;
429         GdkColor *gdk_color;
430
431         /* Make sure we have a valid font family.  if not provide a good default. */
432         family_names = gnome_font_family_list ();
433         if (g_list_find_custom (family_names, font_family, (GCompareFunc)g_utf8_collate)) {
434                 good_font_family = g_strdup (font_family);
435         } else {
436                 if (family_names != NULL) {
437                         good_font_family = g_strdup (family_names->data); /* 1st entry */
438                 } else {
439                         good_font_family = NULL;
440                 }
441         }
442         gnome_font_family_list_free (family_names);
443
444         gtk_entry_set_text (GTK_ENTRY (text->font_family_entry), good_font_family);
445         g_free (good_font_family);
446
447         gtk_spin_button_set_value (GTK_SPIN_BUTTON (text->font_size_spin),
448                                    font_size);
449
450         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->font_b_button),
451                                       (font_weight == GNOME_FONT_BOLD));
452
453         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->font_i_button),
454                                       font_italic_flag);
455
456         gdk_color = gl_color_to_gdk_color (color);
457         color_combo_set_color (COLOR_COMBO(text->color_picker), gdk_color);
458         g_free (gdk_color);
459
460         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->left_button),
461                                       (just == GTK_JUSTIFY_LEFT));
462         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->center_button),
463                                       (just == GTK_JUSTIFY_CENTER));
464         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->right_button),
465                                       (just == GTK_JUSTIFY_RIGHT));
466 }
467
468 /*****************************************************************************/
469 /* Set size group for internal labels                                        */
470 /*****************************************************************************/
471 void
472 gl_wdgt_text_props_set_label_size_group (glWdgtTextProps *text,
473                                          GtkSizeGroup    *label_size_group)
474 {
475         gtk_size_group_add_widget (label_size_group, text->font_label);
476         gtk_size_group_add_widget (label_size_group, text->color_label);
477         gtk_size_group_add_widget (label_size_group, text->alignment_label);
478 }
479