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