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 "mygal/widget-color-combo.h"
29 #include "wdgt-text-props.h"
35 /*===========================================*/
37 /*===========================================*/
44 typedef void (*glWdgtTextPropsSignal) (GObject *object, gpointer data);
46 /*===========================================*/
48 /*===========================================*/
50 static GObjectClass *parent_class;
52 static gint wdgt_text_props_signals[LAST_SIGNAL] = { 0 };
54 /*===========================================*/
55 /* Local function prototypes */
56 /*===========================================*/
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);
63 static void family_changed_cb (GtkEntry *entry,
64 glWdgtTextProps *text);
66 static void changed_cb (glWdgtTextProps *text);
68 static void just_toggled_cb (GtkToggleButton *togglebutton,
71 /*****************************************************************************/
72 /* Boilerplate Object stuff. */
73 /*****************************************************************************/
75 gl_wdgt_text_props_get_type (void)
77 static guint wdgt_text_props_type = 0;
79 if (!wdgt_text_props_type) {
80 GTypeInfo wdgt_text_props_info = {
81 sizeof (glWdgtTextPropsClass),
84 (GClassInitFunc) gl_wdgt_text_props_class_init,
87 sizeof (glWdgtTextProps),
89 (GInstanceInitFunc) gl_wdgt_text_props_instance_init,
92 wdgt_text_props_type =
93 g_type_register_static (gl_hig_vbox_get_type (),
95 &wdgt_text_props_info, 0);
98 return wdgt_text_props_type;
102 gl_wdgt_text_props_class_init (glWdgtTextPropsClass *class)
104 GObjectClass *object_class;
106 object_class = (GObjectClass *) class;
108 parent_class = g_type_class_peek_parent (class);
110 object_class->finalize = gl_wdgt_text_props_finalize;
112 wdgt_text_props_signals[CHANGED] =
113 g_signal_new ("changed",
114 G_OBJECT_CLASS_TYPE(object_class),
116 G_STRUCT_OFFSET (glWdgtTextPropsClass, changed),
118 gl_marshal_VOID__VOID,
124 gl_wdgt_text_props_instance_init (glWdgtTextProps *text)
126 text->font_family_entry = NULL;
127 text->font_size_spin = NULL;
128 text->font_b_button = NULL;
129 text->font_i_button = NULL;
131 text->color_picker = NULL;
133 text->left_button = NULL;
134 text->right_button = NULL;
135 text->center_button = NULL;
139 gl_wdgt_text_props_finalize (GObject *object)
141 glWdgtTextProps *text;
143 g_return_if_fail (object != NULL);
144 g_return_if_fail (GL_IS_WDGT_TEXT_PROPS (object));
146 text = GL_WDGT_TEXT_PROPS (object);
148 G_OBJECT_CLASS (parent_class)->finalize (object);
151 /*****************************************************************************/
153 /*****************************************************************************/
155 gl_wdgt_text_props_new (void)
157 glWdgtTextProps *text;
159 text = g_object_new (gl_wdgt_text_props_get_type (), NULL);
161 gl_wdgt_text_props_construct (text);
163 return GTK_WIDGET (text);
166 /*--------------------------------------------------------------------------*/
167 /* PRIVATE. Construct composite widget. */
168 /*--------------------------------------------------------------------------*/
170 gl_wdgt_text_props_construct (glWdgtTextProps *text)
172 GtkWidget *wvbox, *whbox, *wcombo, *wbhbox;
173 GList *family_names = NULL;
178 wvbox = GTK_WIDGET (text);
180 /* ---- Font line ---- */
181 whbox = gl_hig_hbox_new ();
182 gl_hig_vbox_add_widget (GL_HIG_VBOX(wvbox), whbox);
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);
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);
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),
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),
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),
229 /* ---- Color line ---- */
230 whbox = gl_hig_hbox_new ();
231 gl_hig_vbox_add_widget (GL_HIG_VBOX(wvbox), whbox);
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);
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),
245 g_signal_connect_swapped (G_OBJECT (text->color_picker), "color_changed",
246 G_CALLBACK (changed_cb),
248 gl_hig_hbox_add_widget (GL_HIG_HBOX(whbox), text->color_picker);
250 /* ---- Alignment line ---- */
251 whbox = gl_hig_hbox_new ();
252 gl_hig_vbox_add_widget (GL_HIG_VBOX(wvbox), whbox);
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);
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);
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);
286 /*--------------------------------------------------------------------------*/
287 /* PRIVATE. modify widget due to change in selection */
288 /*--------------------------------------------------------------------------*/
290 family_changed_cb (GtkEntry *entry,
291 glWdgtTextProps *text)
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);
301 g_free (family_name);
304 /*--------------------------------------------------------------------------*/
305 /* PRIVATE. Callback for when any control in the widget has changed. */
306 /*--------------------------------------------------------------------------*/
308 changed_cb (glWdgtTextProps *text)
310 /* Emit our "changed" signal */
311 g_signal_emit (G_OBJECT (text), wdgt_text_props_signals[CHANGED], 0);
314 /*--------------------------------------------------------------------------*/
315 /* PRIVATE. Justify toggle button callback. */
316 /*--------------------------------------------------------------------------*/
318 just_toggled_cb (GtkToggleButton *togglebutton,
321 glWdgtTextProps *text = GL_WDGT_TEXT_PROPS (user_data);
323 if (gtk_toggle_button_get_active (togglebutton)) {
325 if (GTK_WIDGET (togglebutton) == GTK_WIDGET (text->left_button)) {
326 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
327 (text->center_button),
329 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
330 (text->right_button),
332 } else if (GTK_WIDGET (togglebutton) ==
333 GTK_WIDGET (text->center_button)) {
334 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
337 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
338 (text->right_button),
340 } else if (GTK_WIDGET (togglebutton) ==
341 GTK_WIDGET (text->right_button)) {
342 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
345 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
346 (text->center_button),
352 /* Emit our "changed" signal */
353 g_signal_emit (G_OBJECT (text), wdgt_text_props_signals[CHANGED], 0);
356 /*****************************************************************************/
357 /* query values from controls. */
358 /*****************************************************************************/
360 gl_wdgt_text_props_get_params (glWdgtTextProps *text,
363 GnomeFontWeight *font_weight,
364 gboolean *font_italic_flag,
366 GtkJustification *just)
371 /* ------ Get updated font information ------ */
373 gtk_editable_get_chars (GTK_EDITABLE (text->font_family_entry), 0,
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;
381 *font_weight = GNOME_FONT_BOOK;
384 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
385 (text->font_i_button));
387 /* ------ Get updated color ------ */
388 gdk_color = color_combo_get_color (COLOR_COMBO(text->color_picker),
392 *color = gl_prefs->default_text_color;
394 *color = gl_color_from_gdk_color (gdk_color);
397 /* ------- Get updated justification ------ */
398 if (gtk_toggle_button_get_active
399 (GTK_TOGGLE_BUTTON (text->left_button))) {
400 *just = GTK_JUSTIFY_LEFT;
402 if (gtk_toggle_button_get_active
403 (GTK_TOGGLE_BUTTON (text->right_button))) {
404 *just = GTK_JUSTIFY_RIGHT;
406 if (gtk_toggle_button_get_active
407 (GTK_TOGGLE_BUTTON (text->center_button))) {
408 *just = GTK_JUSTIFY_CENTER;
410 *just = GTK_JUSTIFY_LEFT; /* Should not happen. */
415 /*****************************************************************************/
416 /* fill in values and ranges for controls. */
417 /*****************************************************************************/
419 gl_wdgt_text_props_set_params (glWdgtTextProps *text,
422 GnomeFontWeight font_weight,
423 gboolean font_italic_flag,
425 GtkJustification just)
428 gchar *good_font_family;
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);
436 if (family_names != NULL) {
437 good_font_family = g_strdup (family_names->data); /* 1st entry */
439 good_font_family = NULL;
442 gnome_font_family_list_free (family_names);
444 gtk_entry_set_text (GTK_ENTRY (text->font_family_entry), good_font_family);
445 g_free (good_font_family);
447 gtk_spin_button_set_value (GTK_SPIN_BUTTON (text->font_size_spin),
450 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->font_b_button),
451 (font_weight == GNOME_FONT_BOLD));
453 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text->font_i_button),
456 gdk_color = gl_color_to_gdk_color (color);
457 color_combo_set_color (COLOR_COMBO(text->color_picker), gdk_color);
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));
468 /*****************************************************************************/
469 /* Set size group for internal labels */
470 /*****************************************************************************/
472 gl_wdgt_text_props_set_label_size_group (glWdgtTextProps *text,
473 GtkSizeGroup *label_size_group)
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);