1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * object-editor.c: object properties editor module
8 * Copyright (C) 2003 Jim Evins <evins@snaught.com>.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "object-editor.h"
28 #include <glib/gi18n.h>
29 #include <gtk/gtktogglebutton.h>
30 #include <gtk/gtkspinbutton.h>
31 #include <gtk/gtkcombobox.h>
35 #include "mygal/widget-color-combo.h"
39 #include "object-editor-private.h"
43 /*===========================================*/
45 /*===========================================*/
47 /*===========================================*/
48 /* Private data types */
49 /*===========================================*/
51 /*===========================================*/
53 /*===========================================*/
55 /*===========================================*/
56 /* Local function prototypes */
57 /*===========================================*/
59 static void align_toggle_cb (GtkToggleButton *toggle,
60 glObjectEditor *editor);
61 static void text_radio_toggled_cb (glObjectEditor *editor);
64 /*--------------------------------------------------------------------------*/
65 /* PRIVATE. Prepare size page. */
66 /*--------------------------------------------------------------------------*/
68 gl_object_editor_prepare_text_page (glObjectEditor *editor)
70 GList *family_names = NULL;
72 gl_debug (DEBUG_EDITOR, "START");
74 /* Extract widgets from XML tree. */
75 editor->priv->text_page_vbox =
76 glade_xml_get_widget (editor->priv->gui, "text_page_vbox");
77 editor->priv->text_family_combo =
78 glade_xml_get_widget (editor->priv->gui, "text_family_combo");
79 editor->priv->text_size_spin =
80 glade_xml_get_widget (editor->priv->gui, "text_size_spin");
81 editor->priv->text_bold_toggle =
82 glade_xml_get_widget (editor->priv->gui, "text_bold_toggle");
83 editor->priv->text_italic_toggle =
84 glade_xml_get_widget (editor->priv->gui, "text_italic_toggle");
85 editor->priv->text_color_combo =
86 glade_xml_get_widget (editor->priv->gui, "text_color_combo");
87 editor->priv->text_color_radio =
88 glade_xml_get_widget (editor->priv->gui, "text_color_radio");
89 editor->priv->text_color_key_radio =
90 glade_xml_get_widget (editor->priv->gui, "text_color_key_radio");
91 editor->priv->text_color_key_combo =
92 glade_xml_get_widget (editor->priv->gui, "text_color_key_combo");
93 editor->priv->text_left_toggle =
94 glade_xml_get_widget (editor->priv->gui, "text_left_toggle");
95 editor->priv->text_center_toggle =
96 glade_xml_get_widget (editor->priv->gui, "text_center_toggle");
97 editor->priv->text_right_toggle =
98 glade_xml_get_widget (editor->priv->gui, "text_right_toggle");
99 editor->priv->text_line_spacing_spin =
100 glade_xml_get_widget (editor->priv->gui, "text_line_spacing_spin");
101 editor->priv->text_auto_shrink_check =
102 glade_xml_get_widget (editor->priv->gui, "text_auto_shrink_check");
104 gl_util_combo_box_add_text_model ( GTK_COMBO_BOX(editor->priv->text_family_combo));
105 gl_util_combo_box_add_text_model ( GTK_COMBO_BOX(editor->priv->text_color_key_combo));
107 /* Load family names */
108 family_names = gl_util_get_font_family_list ();
109 gl_util_combo_box_set_strings (GTK_COMBO_BOX(editor->priv->text_family_combo),
113 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_radio), TRUE);
114 gtk_widget_set_sensitive (editor->priv->text_color_combo, TRUE);
115 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, FALSE);
118 gtk_widget_show_all (editor->priv->text_page_vbox);
120 /* Connect signals */
121 g_signal_connect_swapped (G_OBJECT (editor->priv->text_family_combo),
123 G_CALLBACK (gl_object_editor_changed_cb),
125 g_signal_connect_swapped (G_OBJECT (editor->priv->text_size_spin),
127 G_CALLBACK (gl_object_editor_changed_cb),
129 g_signal_connect_swapped (G_OBJECT (editor->priv->text_bold_toggle),
131 G_CALLBACK (gl_object_editor_changed_cb),
133 g_signal_connect_swapped (G_OBJECT (editor->priv->text_italic_toggle),
135 G_CALLBACK (gl_object_editor_changed_cb),
137 g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_combo),
139 G_CALLBACK (gl_object_editor_changed_cb),
141 g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_key_combo),
143 G_CALLBACK (gl_object_editor_changed_cb),
145 g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_radio),
147 G_CALLBACK (text_radio_toggled_cb),
149 g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_key_radio),
151 G_CALLBACK (text_radio_toggled_cb),
154 g_signal_connect (G_OBJECT (editor->priv->text_left_toggle),
156 G_CALLBACK (align_toggle_cb),
158 g_signal_connect (G_OBJECT (editor->priv->text_center_toggle),
160 G_CALLBACK (align_toggle_cb),
162 g_signal_connect (G_OBJECT (editor->priv->text_right_toggle),
164 G_CALLBACK (align_toggle_cb),
167 g_signal_connect_swapped (G_OBJECT (editor->priv->text_line_spacing_spin),
169 G_CALLBACK (gl_object_editor_changed_cb),
172 g_signal_connect_swapped (G_OBJECT (editor->priv->text_auto_shrink_check),
174 G_CALLBACK (gl_object_editor_changed_cb),
177 gl_debug (DEBUG_EDITOR, "END");
180 /*--------------------------------------------------------------------------*/
181 /* PRIVATE. Alignment togglebutton callback. */
182 /*--------------------------------------------------------------------------*/
184 align_toggle_cb (GtkToggleButton *toggle,
185 glObjectEditor *editor)
187 if (editor->priv->stop_signals) return;
189 if (gtk_toggle_button_get_active (toggle)) {
191 if (GTK_WIDGET (toggle) == GTK_WIDGET (editor->priv->text_left_toggle)) {
192 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
193 (editor->priv->text_center_toggle),
195 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
196 (editor->priv->text_right_toggle),
198 } else if (GTK_WIDGET (toggle) ==
199 GTK_WIDGET (editor->priv->text_center_toggle)) {
200 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
201 (editor->priv->text_left_toggle),
203 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
204 (editor->priv->text_right_toggle),
206 } else if (GTK_WIDGET (toggle) ==
207 GTK_WIDGET (editor->priv->text_right_toggle)) {
208 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
209 (editor->priv->text_left_toggle),
211 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
212 (editor->priv->text_center_toggle),
216 /* Emit our "changed" signal */
217 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
222 /*****************************************************************************/
223 /* Set font family. */
224 /*****************************************************************************/
226 gl_object_editor_set_font_family (glObjectEditor *editor,
227 const gchar *font_family)
230 gchar *old_font_family;
231 gchar *good_font_family;
233 gl_debug (DEBUG_EDITOR, "START");
235 editor->priv->stop_signals = TRUE;
237 old_font_family = gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->text_family_combo));
239 if ( !old_font_family || g_utf8_collate( old_font_family, font_family ) )
242 /* Make sure we have a valid font family. if not provide a good default. */
243 family_names = gl_util_get_font_family_list ();
244 if (g_list_find_custom (family_names, font_family, (GCompareFunc)g_utf8_collate)) {
245 good_font_family = g_strdup (font_family);
247 if (family_names != NULL) {
248 good_font_family = g_strdup (family_names->data); /* 1st entry */
250 good_font_family = NULL;
253 gl_util_combo_box_set_active_text (GTK_COMBO_BOX (editor->priv->text_family_combo), good_font_family);
254 g_free (good_font_family);
257 g_free (old_font_family);
259 editor->priv->stop_signals = FALSE;
261 gl_debug (DEBUG_EDITOR, "END");
264 /*****************************************************************************/
265 /* Query font family. */
266 /*****************************************************************************/
268 gl_object_editor_get_font_family (glObjectEditor *editor)
272 gl_debug (DEBUG_EDITOR, "START");
274 font_family = gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->text_family_combo));
276 gl_debug (DEBUG_EDITOR, "END");
281 /*****************************************************************************/
283 /*****************************************************************************/
285 gl_object_editor_set_font_size (glObjectEditor *editor,
288 gl_debug (DEBUG_EDITOR, "START");
290 editor->priv->stop_signals = TRUE;
292 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->text_size_spin),
295 editor->priv->stop_signals = FALSE;
297 gl_debug (DEBUG_EDITOR, "END");
300 /*****************************************************************************/
301 /* Query font size. */
302 /*****************************************************************************/
304 gl_object_editor_get_font_size (glObjectEditor *editor)
308 gl_debug (DEBUG_EDITOR, "START");
311 gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->text_size_spin));
313 gl_debug (DEBUG_EDITOR, "END");
318 /*****************************************************************************/
319 /* Set font weight. */
320 /*****************************************************************************/
322 gl_object_editor_set_font_weight (glObjectEditor *editor,
323 PangoWeight font_weight)
325 gl_debug (DEBUG_EDITOR, "START");
327 editor->priv->stop_signals = TRUE;
329 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_bold_toggle),
330 (font_weight == PANGO_WEIGHT_BOLD));
332 editor->priv->stop_signals = FALSE;
334 gl_debug (DEBUG_EDITOR, "END");
337 /*****************************************************************************/
338 /* Query font weight. */
339 /*****************************************************************************/
341 gl_object_editor_get_font_weight (glObjectEditor *editor)
343 PangoWeight font_weight;
345 gl_debug (DEBUG_EDITOR, "START");
347 if (gtk_toggle_button_get_active
348 (GTK_TOGGLE_BUTTON (editor->priv->text_bold_toggle))) {
349 font_weight = PANGO_WEIGHT_BOLD;
351 font_weight = PANGO_WEIGHT_NORMAL;
354 gl_debug (DEBUG_EDITOR, "END");
359 /*****************************************************************************/
360 /* Set font italic flag. */
361 /*****************************************************************************/
363 gl_object_editor_set_font_italic_flag (glObjectEditor *editor,
364 gboolean font_italic_flag)
366 gl_debug (DEBUG_EDITOR, "START");
368 editor->priv->stop_signals = TRUE;
370 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_italic_toggle),
373 editor->priv->stop_signals = FALSE;
375 gl_debug (DEBUG_EDITOR, "END");
378 /*****************************************************************************/
379 /* Query font italic flag. */
380 /*****************************************************************************/
382 gl_object_editor_get_font_italic_flag (glObjectEditor *editor)
384 gboolean italic_flag;
386 gl_debug (DEBUG_EDITOR, "START");
389 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
390 (editor->priv->text_italic_toggle));
392 gl_debug (DEBUG_EDITOR, "END");
397 /*****************************************************************************/
398 /* Set text alignment. */
399 /*****************************************************************************/
401 gl_object_editor_set_text_alignment (glObjectEditor *editor,
402 PangoAlignment align)
404 gl_debug (DEBUG_EDITOR, "START");
406 editor->priv->stop_signals = TRUE;
408 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_left_toggle),
409 (align == PANGO_ALIGN_LEFT));
410 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_center_toggle),
411 (align == PANGO_ALIGN_CENTER));
412 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_right_toggle),
413 (align == PANGO_ALIGN_RIGHT));
415 editor->priv->stop_signals = FALSE;
417 gl_debug (DEBUG_EDITOR, "END");
420 /*****************************************************************************/
421 /* Query text alignment. */
422 /*****************************************************************************/
424 gl_object_editor_get_text_alignment (glObjectEditor *editor)
426 PangoAlignment align;
428 gl_debug (DEBUG_EDITOR, "START");
430 if (gtk_toggle_button_get_active
431 (GTK_TOGGLE_BUTTON (editor->priv->text_left_toggle))) {
432 align = PANGO_ALIGN_LEFT;
434 if (gtk_toggle_button_get_active
435 (GTK_TOGGLE_BUTTON (editor->priv->text_right_toggle))) {
436 align = PANGO_ALIGN_RIGHT;
438 if (gtk_toggle_button_get_active
439 (GTK_TOGGLE_BUTTON (editor->priv->text_center_toggle))) {
440 align = PANGO_ALIGN_CENTER;
442 align = PANGO_ALIGN_LEFT; /* Should not happen. */
445 gl_debug (DEBUG_EDITOR, "END");
450 /*****************************************************************************/
451 /* Set text color. */
452 /*****************************************************************************/
454 gl_object_editor_set_text_color (glObjectEditor *editor,
456 glColorNode *text_color_node)
460 gl_debug (DEBUG_EDITOR, "START");
462 editor->priv->stop_signals = TRUE;
464 gl_debug (DEBUG_EDITOR, "color field %s(%d) / %X", text_color_node->key, text_color_node->field_flag, text_color_node->color);
466 gtk_widget_set_sensitive (editor->priv->text_color_key_radio, merge_flag);
468 if ( text_color_node->color == GL_COLOR_NONE ) {
470 color_combo_set_color_to_default (COLOR_COMBO(editor->priv->text_color_combo));
474 gdk_color = gl_color_to_gdk_color (text_color_node->color);
475 color_combo_set_color (COLOR_COMBO(editor->priv->text_color_combo), gdk_color);
479 if (!text_color_node->field_flag || !merge_flag) {
480 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
481 (editor->priv->text_color_radio), TRUE);
482 gtk_widget_set_sensitive (editor->priv->text_color_combo, TRUE);
483 gl_debug (DEBUG_EDITOR, "color field false 0");
484 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, FALSE);
487 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
488 (editor->priv->text_color_key_radio), TRUE);
489 gtk_widget_set_sensitive (editor->priv->text_color_combo, FALSE);
490 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, TRUE);
492 gl_debug (DEBUG_EDITOR, "color field true 1");
493 gl_util_combo_box_set_active_text (GTK_COMBO_BOX (editor->priv->text_color_key_combo), "");
494 gl_debug (DEBUG_EDITOR, "color field true 2");
497 editor->priv->stop_signals = FALSE;
499 gl_debug (DEBUG_EDITOR, "END");
502 /*****************************************************************************/
503 /* Query text color. */
504 /*****************************************************************************/
506 gl_object_editor_get_text_color (glObjectEditor *editor)
509 glColorNode *color_node;
512 gl_debug (DEBUG_EDITOR, "START");
514 color_node = gl_color_node_new_default ();
516 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_key_radio))) {
517 color_node->field_flag = TRUE;
518 color_node->color = gl_prefs->default_text_color;
520 gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->text_color_key_combo));
522 color_node->field_flag = FALSE;
523 color_node->key = NULL;
524 gdk_color = color_combo_get_color (COLOR_COMBO(editor->priv->text_color_combo),
528 color_node->color = gl_prefs->default_text_color;
530 color_node->color = gl_color_from_gdk_color (gdk_color);
534 gl_debug (DEBUG_EDITOR, "END");
540 /*****************************************************************************/
541 /* Set text line spacing. */
542 /*****************************************************************************/
544 gl_object_editor_set_text_line_spacing (glObjectEditor *editor,
545 gdouble text_line_spacing)
547 gl_debug (DEBUG_EDITOR, "START");
549 editor->priv->stop_signals = TRUE;
551 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->text_line_spacing_spin),
554 editor->priv->stop_signals = FALSE;
556 gl_debug (DEBUG_EDITOR, "END");
559 /*****************************************************************************/
560 /* Query text line spacing. */
561 /*****************************************************************************/
563 gl_object_editor_get_text_line_spacing (glObjectEditor *editor)
565 gdouble text_line_spacing;
567 gl_debug (DEBUG_EDITOR, "START");
570 gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->text_line_spacing_spin));
572 gl_debug (DEBUG_EDITOR, "END");
574 return text_line_spacing;
577 /*****************************************************************************/
578 /* Set auto shrink checkbox. */
579 /*****************************************************************************/
581 gl_object_editor_set_text_auto_shrink (glObjectEditor *editor,
582 gboolean auto_shrink)
584 gl_debug (DEBUG_EDITOR, "START");
586 editor->priv->stop_signals = TRUE;
588 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_auto_shrink_check),
591 editor->priv->stop_signals = FALSE;
593 gl_debug (DEBUG_EDITOR, "END");
596 /*****************************************************************************/
597 /* Query auto shrink checkbox. */
598 /*****************************************************************************/
599 gboolean gl_object_editor_get_text_auto_shrink (glObjectEditor *editor)
601 gboolean auto_shrink;
603 gl_debug (DEBUG_EDITOR, "START");
606 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_auto_shrink_check));
608 gl_debug (DEBUG_EDITOR, "END");
613 /*--------------------------------------------------------------------------*/
614 /* PRIVATE. color radio callback. */
615 /*--------------------------------------------------------------------------*/
617 text_radio_toggled_cb (glObjectEditor *editor)
619 if (editor->priv->stop_signals) return;
621 gl_debug (DEBUG_EDITOR, "START");
623 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_radio))) {
624 gtk_widget_set_sensitive (editor->priv->text_color_combo, TRUE);
625 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, FALSE);
627 gtk_widget_set_sensitive (editor->priv->text_color_combo, FALSE);
628 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, TRUE);
632 /* Emit our "changed" signal */
633 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
635 gl_debug (DEBUG_EDITOR, "END");