2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * wdgt_text_props.c: text properties widget module
6 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
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.
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.
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
27 #include "wdgt-text-props.h"
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 )
37 /*===========================================*/
39 /*===========================================*/
46 typedef void (*glWdgtTextPropsSignal) (GObject * object,
49 /*===========================================*/
51 /*===========================================*/
53 static GObjectClass *parent_class;
55 static gint wdgt_text_props_signals[LAST_SIGNAL] = { 0 };
57 /*===========================================*/
58 /* Local function prototypes */
59 /*===========================================*/
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);
66 static void changed_cb (glWdgtTextProps * text);
67 static void just_toggled_cb (GtkToggleButton * togglebutton,
70 /*================================================================*/
71 /* Boilerplate Object stuff. */
72 /*================================================================*/
74 gl_wdgt_text_props_get_type (void)
76 static guint wdgt_text_props_type = 0;
78 if (!wdgt_text_props_type) {
79 GTypeInfo wdgt_text_props_info = {
80 sizeof (glWdgtTextPropsClass),
83 (GClassInitFunc) gl_wdgt_text_props_class_init,
86 sizeof (glWdgtTextProps),
88 (GInstanceInitFunc) gl_wdgt_text_props_instance_init,
91 wdgt_text_props_type =
92 g_type_register_static (gtk_vbox_get_type (),
93 "glWdgtTextProps", &wdgt_text_props_info, 0);
96 return wdgt_text_props_type;
100 gl_wdgt_text_props_class_init (glWdgtTextPropsClass * class)
102 GObjectClass *object_class;
104 object_class = (GObjectClass *) class;
106 parent_class = gtk_type_class (gtk_vbox_get_type ());
108 object_class->finalize = gl_wdgt_text_props_finalize;
110 wdgt_text_props_signals[CHANGED] =
111 g_signal_new ("changed",
112 G_OBJECT_CLASS_TYPE(object_class),
114 G_STRUCT_OFFSET (glWdgtTextPropsClass, changed),
116 gl_marshal_VOID__VOID,
122 gl_wdgt_text_props_instance_init (glWdgtTextProps * text)
124 text->font_family_entry = NULL;
125 text->font_size_spin = NULL;
126 text->font_b_button = NULL;
127 text->font_i_button = NULL;
129 text->color_picker = NULL;
131 text->left_button = NULL;
132 text->right_button = NULL;
133 text->center_button = NULL;
137 gl_wdgt_text_props_finalize (GObject * object)
139 glWdgtTextProps *text;
141 g_return_if_fail (object != NULL);
142 g_return_if_fail (GL_IS_WDGT_TEXT_PROPS (object));
144 text = GL_WDGT_TEXT_PROPS (object);
146 G_OBJECT_CLASS (parent_class)->finalize (object);
150 gl_wdgt_text_props_new (gchar * label)
152 glWdgtTextProps *text;
154 text = g_object_new (gl_wdgt_text_props_get_type (), NULL);
156 gl_wdgt_text_props_construct (text, label);
158 return GTK_WIDGET (text);
161 /*============================================================*/
162 /* Construct composite widget. */
163 /*============================================================*/
165 gl_wdgt_text_props_construct (glWdgtTextProps * text,
168 GtkWidget *wvbox, *wframe, *wtable, *wlabel, *whbox1, *wcombo;
169 GList *family_names = NULL;
172 wvbox = GTK_WIDGET (text);
174 wframe = gtk_frame_new (label);
175 gtk_box_pack_start (GTK_BOX (wvbox), wframe, FALSE, FALSE, 0);
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);
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);
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);
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),
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,
212 g_signal_connect_swapped (G_OBJECT (text->font_size_spin), "changed",
213 G_CALLBACK (changed_cb),
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,
223 g_signal_connect_swapped (G_OBJECT (text->font_b_button), "toggled",
224 G_CALLBACK (changed_cb),
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,
232 g_signal_connect_swapped (G_OBJECT (text->font_i_button), "toggled",
233 G_CALLBACK (changed_cb),
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);
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),
247 gtk_table_attach_defaults (GTK_TABLE (wtable), text->color_picker, 1, 2,
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);
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);
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,
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,
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,
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);
290 /*--------------------------------------------------------------------------*/
291 /* PRIVATE. Callback for when any control in the widget has changed. */
292 /*--------------------------------------------------------------------------*/
294 changed_cb (glWdgtTextProps * text)
296 /* Emit our "changed" signal */
297 g_signal_emit (G_OBJECT (text), wdgt_text_props_signals[CHANGED], 0);
300 /*--------------------------------------------------------------------------*/
301 /* PRIVATE. Justify toggle button callback. */
302 /*--------------------------------------------------------------------------*/
304 just_toggled_cb (GtkToggleButton * togglebutton,
307 glWdgtTextProps *text = GL_WDGT_TEXT_PROPS (user_data);
309 if (gtk_toggle_button_get_active (togglebutton)) {
311 if (GTK_WIDGET (togglebutton) == GTK_WIDGET (text->left_button)) {
312 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
313 (text->center_button),
315 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
316 (text->right_button),
318 } else if (GTK_WIDGET (togglebutton) ==
319 GTK_WIDGET (text->center_button)) {
320 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
323 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
324 (text->right_button),
326 } else if (GTK_WIDGET (togglebutton) ==
327 GTK_WIDGET (text->right_button)) {
328 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
331 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
332 (text->center_button),
338 /* Emit our "changed" signal */
339 g_signal_emit (G_OBJECT (text), wdgt_text_props_signals[CHANGED], 0);
342 /*====================================================================*/
343 /* query values from controls. */
344 /*====================================================================*/
346 gl_wdgt_text_props_get_params (glWdgtTextProps * text,
347 gchar ** font_family,
349 GnomeFontWeight * font_weight,
350 gboolean * font_italic_flag,
352 GtkJustification * just)
356 /* ------ Get updated font information ------ */
358 gtk_editable_get_chars (GTK_EDITABLE (text->font_family_entry), 0,
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;
366 *font_weight = GNOME_FONT_BOOK;
369 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
370 (text->font_i_button));
372 /* ------ Get updated color ------ */
373 gnome_color_picker_get_i8 (GNOME_COLOR_PICKER (text->color_picker),
375 *color = GNOME_CANVAS_COLOR_A (r, g, b, a);
377 /* ------- Get updated justification ------ */
378 if (gtk_toggle_button_get_active
379 (GTK_TOGGLE_BUTTON (text->left_button))) {
380 *just = GTK_JUSTIFY_LEFT;
382 if (gtk_toggle_button_get_active
383 (GTK_TOGGLE_BUTTON (text->right_button))) {
384 *just = GTK_JUSTIFY_RIGHT;
386 if (gtk_toggle_button_get_active
387 (GTK_TOGGLE_BUTTON (text->center_button))) {
388 *just = GTK_JUSTIFY_CENTER;
390 *just = GTK_JUSTIFY_LEFT; /* Should not happen. */
395 /*====================================================================*/
396 /* fill in values and ranges for controls. */
397 /*====================================================================*/
399 gl_wdgt_text_props_set_params (glWdgtTextProps * text,
402 GnomeFontWeight font_weight,
403 gboolean font_italic_flag,
405 GtkJustification just)
407 gtk_entry_set_text (GTK_ENTRY (text->font_family_entry), font_family);
409 gtk_spin_button_set_value (GTK_SPIN_BUTTON (text->font_size_spin),
412 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->font_b_button),
413 (font_weight == GNOME_FONT_BOLD));
415 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->font_i_button),
418 gnome_color_picker_set_i8 (GNOME_COLOR_PICKER (text->color_picker),
419 RED (color), GREEN (color), BLUE (color),
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));