]> git.sur5r.net Git - glabels/blob - glabels2/src/wdgt-text-props.c
Initial revision
[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 "wdgt-text-props.h"
28 #include "marshal.h"
29
30 #include "debug.h"
31
32 #define RED(x)   ( ((x)>>24) & 0xff )
33 #define GREEN(x) ( ((x)>>16) & 0xff )
34 #define BLUE(x)  ( ((x)>>8)  & 0xff )
35 #define ALPHA(x) (  (x)      & 0xff )
36
37 /*===========================================*/
38 /* Private types                             */
39 /*===========================================*/
40
41 enum {
42         CHANGED,
43         LAST_SIGNAL
44 };
45
46 typedef void (*glWdgtTextPropsSignal) (GObject * object,
47                                   gpointer data);
48
49 /*===========================================*/
50 /* Private globals                           */
51 /*===========================================*/
52
53 static GObjectClass *parent_class;
54
55 static gint wdgt_text_props_signals[LAST_SIGNAL] = { 0 };
56
57 /*===========================================*/
58 /* Local function prototypes                 */
59 /*===========================================*/
60
61 static void gl_wdgt_text_props_class_init (glWdgtTextPropsClass * class);
62 static void gl_wdgt_text_props_instance_init (glWdgtTextProps * text);
63 static void gl_wdgt_text_props_finalize (GObject * object);
64 static void gl_wdgt_text_props_construct (glWdgtTextProps * text, gchar * label);
65
66 static void changed_cb (glWdgtTextProps * text);
67 static void just_toggled_cb (GtkToggleButton * togglebutton,
68                              gpointer user_data);
69 \f
70 /*================================================================*/
71 /* Boilerplate Object stuff.                                      */
72 /*================================================================*/
73 guint
74 gl_wdgt_text_props_get_type (void)
75 {
76         static guint wdgt_text_props_type = 0;
77
78         if (!wdgt_text_props_type) {
79                 GTypeInfo wdgt_text_props_info = {
80                         sizeof (glWdgtTextPropsClass),
81                         NULL,
82                         NULL,
83                         (GClassInitFunc) gl_wdgt_text_props_class_init,
84                         NULL,
85                         NULL,
86                         sizeof (glWdgtTextProps),
87                         0,
88                         (GInstanceInitFunc) gl_wdgt_text_props_instance_init,
89                 };
90
91                 wdgt_text_props_type =
92                     g_type_register_static (gtk_vbox_get_type (),
93                                             "glWdgtTextProps", &wdgt_text_props_info, 0);
94         }
95
96         return wdgt_text_props_type;
97 }
98
99 static void
100 gl_wdgt_text_props_class_init (glWdgtTextPropsClass * class)
101 {
102         GObjectClass *object_class;
103
104         object_class = (GObjectClass *) class;
105
106         parent_class = gtk_type_class (gtk_vbox_get_type ());
107
108         object_class->finalize = gl_wdgt_text_props_finalize;
109
110         wdgt_text_props_signals[CHANGED] =
111             g_signal_new ("changed",
112                           G_OBJECT_CLASS_TYPE(object_class),
113                           G_SIGNAL_RUN_LAST,
114                           G_STRUCT_OFFSET (glWdgtTextPropsClass, changed),
115                           NULL, NULL,
116                           gl_marshal_VOID__VOID,
117                           G_TYPE_NONE, 0);
118
119 }
120
121 static void
122 gl_wdgt_text_props_instance_init (glWdgtTextProps * text)
123 {
124         text->font_family_entry = NULL;
125         text->font_size_spin = NULL;
126         text->font_b_button = NULL;
127         text->font_i_button = NULL;
128
129         text->color_picker = NULL;
130
131         text->left_button = NULL;
132         text->right_button = NULL;
133         text->center_button = NULL;
134 }
135
136 static void
137 gl_wdgt_text_props_finalize (GObject * object)
138 {
139         glWdgtTextProps *text;
140
141         g_return_if_fail (object != NULL);
142         g_return_if_fail (GL_IS_WDGT_TEXT_PROPS (object));
143
144         text = GL_WDGT_TEXT_PROPS (object);
145
146         G_OBJECT_CLASS (parent_class)->finalize (object);
147 }
148
149 GtkWidget *
150 gl_wdgt_text_props_new (gchar * label)
151 {
152         glWdgtTextProps *text;
153
154         text = g_object_new (gl_wdgt_text_props_get_type (), NULL);
155
156         gl_wdgt_text_props_construct (text, label);
157
158         return GTK_WIDGET (text);
159 }
160 \f
161 /*============================================================*/
162 /* Construct composite widget.                                */
163 /*============================================================*/
164 static void
165 gl_wdgt_text_props_construct (glWdgtTextProps * text,
166                         gchar * label)
167 {
168         GtkWidget *wvbox, *wframe, *wtable, *wlabel, *whbox1, *wcombo;
169         GList *family_names = NULL;
170         GtkObject *adjust;
171
172         wvbox = GTK_WIDGET (text);
173
174         wframe = gtk_frame_new (label);
175         gtk_box_pack_start (GTK_BOX (wvbox), wframe, FALSE, FALSE, 0);
176
177         wtable = gtk_table_new (3, 3, FALSE);
178         gtk_container_set_border_width (GTK_CONTAINER (wtable), 10);
179         gtk_table_set_row_spacings (GTK_TABLE (wtable), 5);
180         gtk_table_set_col_spacings (GTK_TABLE (wtable), 5);
181         gtk_container_add (GTK_CONTAINER (wframe), wtable);
182
183         /* Font label */
184         wlabel = gtk_label_new (_("Font:"));
185         gtk_misc_set_alignment (GTK_MISC (wlabel), 0, 0.5);
186         gtk_label_set_justify (GTK_LABEL (wlabel), GTK_JUSTIFY_RIGHT);
187         gtk_table_attach_defaults (GTK_TABLE (wtable), wlabel, 0, 1, 0, 1);
188
189         /* Pack these widgets into an inner hbox */
190         whbox1 = gtk_hbox_new (FALSE, GNOME_PAD);
191         gtk_table_attach_defaults (GTK_TABLE (wtable), whbox1, 1, 3, 0, 1);
192
193         /* Font family entry widget */
194         wcombo = gtk_combo_new ();
195         family_names = gnome_font_family_list ();
196         gtk_combo_set_popdown_strings (GTK_COMBO (wcombo), family_names);
197         gnome_font_family_list_free (family_names);
198         text->font_family_entry = GTK_COMBO (wcombo)->entry;
199         gtk_entry_set_editable (GTK_ENTRY (text->font_family_entry), FALSE);
200         gtk_widget_set_size_request (wcombo, 200, -1);
201         gtk_box_pack_start (GTK_BOX (whbox1), wcombo, FALSE, FALSE, 0);
202         g_signal_connect_swapped (G_OBJECT (text->font_family_entry),
203                                   "changed", G_CALLBACK (changed_cb),
204                                   G_OBJECT (text));
205
206         /* Font size entry widget */
207         adjust = gtk_adjustment_new (1.0, 1.0, 250.0, 1.0, 10.0, 10.0);
208         text->font_size_spin =
209             gtk_spin_button_new (GTK_ADJUSTMENT (adjust), 1.0, 0);
210         gtk_box_pack_start (GTK_BOX (whbox1), text->font_size_spin, FALSE,
211                             FALSE, 0);
212         g_signal_connect_swapped (G_OBJECT (text->font_size_spin), "changed",
213                                   G_CALLBACK (changed_cb),
214                                   G_OBJECT (text));
215
216         /* Font weight/italic button widgets */
217         text->font_b_button = gtk_toggle_button_new ();
218         gtk_container_add (GTK_CONTAINER (text->font_b_button),
219                            gtk_image_new_from_stock (GTK_STOCK_BOLD,
220                                                      GTK_ICON_SIZE_BUTTON));
221         gtk_box_pack_start (GTK_BOX (whbox1), text->font_b_button, FALSE, FALSE,
222                             0);
223         g_signal_connect_swapped (G_OBJECT (text->font_b_button), "toggled",
224                                   G_CALLBACK (changed_cb),
225                                   G_OBJECT (text));
226         text->font_i_button = gtk_toggle_button_new ();
227         gtk_container_add (GTK_CONTAINER (text->font_i_button),
228                            gtk_image_new_from_stock (GTK_STOCK_ITALIC,
229                                                      GTK_ICON_SIZE_BUTTON));
230         gtk_box_pack_start (GTK_BOX (whbox1), text->font_i_button, FALSE, FALSE,
231                             0);
232         g_signal_connect_swapped (G_OBJECT (text->font_i_button), "toggled",
233                                   G_CALLBACK (changed_cb),
234                                   G_OBJECT (text));
235
236         /* Text Color Label */
237         wlabel = gtk_label_new (_("Color:"));
238         gtk_misc_set_alignment (GTK_MISC (wlabel), 0, 0.5);
239         gtk_label_set_justify (GTK_LABEL (wlabel), GTK_JUSTIFY_RIGHT);
240         gtk_table_attach_defaults (GTK_TABLE (wtable), wlabel, 0, 1, 1, 2);
241
242         /* Text Color picker widget */
243         text->color_picker = gnome_color_picker_new ();
244         g_signal_connect_swapped (G_OBJECT (text->color_picker), "color_set",
245                                   G_CALLBACK (changed_cb),
246                                   G_OBJECT (text));
247         gtk_table_attach_defaults (GTK_TABLE (wtable), text->color_picker, 1, 2,
248                                    1, 2);
249
250         /* Alignment label */
251         wlabel = gtk_label_new (_("Alignment:"));
252         gtk_misc_set_alignment (GTK_MISC (wlabel), 0, 0.5);
253         gtk_label_set_justify (GTK_LABEL (wlabel), GTK_JUSTIFY_RIGHT);
254         gtk_table_attach_defaults (GTK_TABLE (wtable), wlabel, 0, 1, 2, 3);
255
256         /* Pack these widgets into an inner hbox */
257         whbox1 = gtk_hbox_new (FALSE, GNOME_PAD);
258         gtk_table_attach_defaults (GTK_TABLE (wtable), whbox1, 1, 2, 2, 3);
259
260         /* Justification entry widget */
261         text->left_button = gtk_toggle_button_new ();
262         gtk_container_add (GTK_CONTAINER (text->left_button),
263                            gtk_image_new_from_stock (GTK_STOCK_JUSTIFY_LEFT,
264                                                      GTK_ICON_SIZE_BUTTON));
265         gtk_box_pack_start (GTK_BOX (whbox1), text->left_button, FALSE, FALSE,
266                             0);
267         text->center_button = gtk_toggle_button_new ();
268         gtk_container_add (GTK_CONTAINER (text->center_button),
269                            gtk_image_new_from_stock (GTK_STOCK_JUSTIFY_CENTER,
270                                                      GTK_ICON_SIZE_BUTTON));
271         gtk_box_pack_start (GTK_BOX (whbox1), text->center_button, FALSE, FALSE,
272                             0);
273         text->right_button = gtk_toggle_button_new ();
274         gtk_container_add (GTK_CONTAINER (text->right_button),
275                            gtk_image_new_from_stock (GTK_STOCK_JUSTIFY_RIGHT,
276                                                      GTK_ICON_SIZE_BUTTON));
277         gtk_box_pack_start (GTK_BOX (whbox1), text->right_button, FALSE, FALSE,
278                             0);
279
280         /* Now connect a callback that makes these toggles mutually exclusive */
281         g_signal_connect (G_OBJECT (text->left_button), "toggled",
282                           G_CALLBACK (just_toggled_cb), text);
283         g_signal_connect (G_OBJECT (text->center_button), "toggled",
284                           G_CALLBACK (just_toggled_cb), text);
285         g_signal_connect (G_OBJECT (text->right_button), "toggled",
286                           G_CALLBACK (just_toggled_cb), text);
287
288 }
289
290 /*--------------------------------------------------------------------------*/
291 /* PRIVATE.  Callback for when any control in the widget has changed.       */
292 /*--------------------------------------------------------------------------*/
293 static void
294 changed_cb (glWdgtTextProps * text)
295 {
296         /* Emit our "changed" signal */
297         g_signal_emit (G_OBJECT (text), wdgt_text_props_signals[CHANGED], 0);
298 }
299 \f
300 /*--------------------------------------------------------------------------*/
301 /* PRIVATE.  Justify toggle button callback.                                */
302 /*--------------------------------------------------------------------------*/
303 static void
304 just_toggled_cb (GtkToggleButton * togglebutton,
305                  gpointer user_data)
306 {
307         glWdgtTextProps *text = GL_WDGT_TEXT_PROPS (user_data);
308
309         if (gtk_toggle_button_get_active (togglebutton)) {
310
311                 if (GTK_WIDGET (togglebutton) == GTK_WIDGET (text->left_button)) {
312                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
313                                                       (text->center_button),
314                                                       FALSE);
315                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
316                                                       (text->right_button),
317                                                       FALSE);
318                 } else if (GTK_WIDGET (togglebutton) ==
319                            GTK_WIDGET (text->center_button)) {
320                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
321                                                       (text->left_button),
322                                                       FALSE);
323                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
324                                                       (text->right_button),
325                                                       FALSE);
326                 } else if (GTK_WIDGET (togglebutton) ==
327                            GTK_WIDGET (text->right_button)) {
328                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
329                                                       (text->left_button),
330                                                       FALSE);
331                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
332                                                       (text->center_button),
333                                                       FALSE);
334                 }
335
336         }
337
338         /* Emit our "changed" signal */
339         g_signal_emit (G_OBJECT (text), wdgt_text_props_signals[CHANGED], 0);
340 }
341 \f
342 /*====================================================================*/
343 /* query values from controls.                                        */
344 /*====================================================================*/
345 void
346 gl_wdgt_text_props_get_params (glWdgtTextProps * text,
347                                gchar ** font_family,
348                                gdouble * font_size,
349                                GnomeFontWeight * font_weight,
350                                gboolean * font_italic_flag,
351                                guint * color,
352                                GtkJustification * just)
353 {
354         guint8 r, g, b, a;
355
356         /* ------ Get updated font information ------ */
357         *font_family =
358             gtk_editable_get_chars (GTK_EDITABLE (text->font_family_entry), 0,
359                                     -1);
360         *font_size =
361             gtk_spin_button_get_value (GTK_SPIN_BUTTON(text->font_size_spin));
362         if (gtk_toggle_button_get_active
363             (GTK_TOGGLE_BUTTON (text->font_b_button))) {
364                 *font_weight = GNOME_FONT_BOLD;
365         } else {
366                 *font_weight = GNOME_FONT_BOOK;
367         }
368         *font_italic_flag =
369             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
370                                           (text->font_i_button));
371
372         /* ------ Get updated color ------ */
373         gnome_color_picker_get_i8 (GNOME_COLOR_PICKER (text->color_picker),
374                                    &r, &g, &b, &a);
375         *color = GNOME_CANVAS_COLOR_A (r, g, b, a);
376
377         /* ------- Get updated justification ------ */
378         if (gtk_toggle_button_get_active
379             (GTK_TOGGLE_BUTTON (text->left_button))) {
380                 *just = GTK_JUSTIFY_LEFT;
381         } else
382             if (gtk_toggle_button_get_active
383                 (GTK_TOGGLE_BUTTON (text->right_button))) {
384                 *just = GTK_JUSTIFY_RIGHT;
385         } else
386             if (gtk_toggle_button_get_active
387                 (GTK_TOGGLE_BUTTON (text->center_button))) {
388                 *just = GTK_JUSTIFY_CENTER;
389         } else {
390                 *just = GTK_JUSTIFY_LEFT;       /* Should not happen. */
391         }
392
393 }
394
395 /*====================================================================*/
396 /* fill in values and ranges for controls.                            */
397 /*====================================================================*/
398 void
399 gl_wdgt_text_props_set_params (glWdgtTextProps * text,
400                                gchar * font_family,
401                                gdouble font_size,
402                                GnomeFontWeight font_weight,
403                                gboolean font_italic_flag,
404                                guint color,
405                                GtkJustification just)
406 {
407         gtk_entry_set_text (GTK_ENTRY (text->font_family_entry), font_family);
408
409         gtk_spin_button_set_value (GTK_SPIN_BUTTON (text->font_size_spin),
410                                    font_size);
411
412         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->font_b_button),
413                                       (font_weight == GNOME_FONT_BOLD));
414
415         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->font_i_button),
416                                       font_italic_flag);
417
418         gnome_color_picker_set_i8 (GNOME_COLOR_PICKER (text->color_picker),
419                                    RED (color), GREEN (color), BLUE (color),
420                                    ALPHA (color));
421
422         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->left_button),
423                                       (just == GTK_JUSTIFY_LEFT));
424         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->center_button),
425                                       (just == GTK_JUSTIFY_CENTER));
426         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->right_button),
427                                       (just == GTK_JUSTIFY_RIGHT));
428 }