]> git.sur5r.net Git - glabels/blob - glabels2/src/object-editor-text-page.c
79f130513043e579207ddbac8d0445cfc23e69c8
[glabels] / glabels2 / src / object-editor-text-page.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  object-editor.c:  object properties editor module
7  *
8  *  Copyright (C) 2003  Jim Evins <evins@snaught.com>.
9  *
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.
14  *
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.
19  *
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
23  */
24 #include <config.h>
25
26 #include "object-editor.h"
27
28 #include <glib/gi18n.h>
29 #include <gtk/gtktogglebutton.h>
30 #include <gtk/gtkspinbutton.h>
31 #include <gtk/gtkcombobox.h>
32 #include <math.h>
33
34 #include "prefs.h"
35 #include "color-combo.h"
36 #include "color.h"
37 #include "util.h"
38
39 #include "object-editor-private.h"
40
41 #include "debug.h"
42
43 /*===========================================*/
44 /* Private macros                            */
45 /*===========================================*/
46
47 /*===========================================*/
48 /* Private data types                        */
49 /*===========================================*/
50
51 /*===========================================*/
52 /* Private globals                           */
53 /*===========================================*/
54
55 /*===========================================*/
56 /* Local function prototypes                 */
57 /*===========================================*/
58
59 static void align_toggle_cb (GtkToggleButton *toggle,
60                              glObjectEditor  *editor);
61 static void text_radio_toggled_cb              (glObjectEditor        *editor);
62
63
64 /*--------------------------------------------------------------------------*/
65 /* PRIVATE.  Prepare size page.                                             */
66 /*--------------------------------------------------------------------------*/
67 void
68 gl_object_editor_prepare_text_page (glObjectEditor       *editor)
69 {
70         GList        *family_names = NULL;
71
72         gl_debug (DEBUG_EDITOR, "START");
73
74         /* Extract widgets from XML tree. */
75         gl_util_get_builder_widgets (editor->priv->gui,
76                                      "text_page_vbox",         &editor->priv->text_page_vbox,
77                                      "text_family_combo",      &editor->priv->text_family_combo,
78                                      "text_size_spin",         &editor->priv->text_size_spin,
79                                      "text_bold_toggle",       &editor->priv->text_bold_toggle,
80                                      "text_italic_toggle",     &editor->priv->text_italic_toggle,
81                                      "text_color_hbox",        &editor->priv->text_color_hbox,
82                                      "text_color_radio",       &editor->priv->text_color_radio,
83                                      "text_color_key_radio",   &editor->priv->text_color_key_radio,
84                                      "text_color_key_combo",   &editor->priv->text_color_key_combo,
85                                      "text_left_toggle",       &editor->priv->text_left_toggle,
86                                      "text_center_toggle",     &editor->priv->text_center_toggle,
87                                      "text_right_toggle",      &editor->priv->text_right_toggle,
88                                      "text_line_spacing_spin", &editor->priv->text_line_spacing_spin,
89                                      "text_auto_shrink_check", &editor->priv->text_auto_shrink_check,
90                                      NULL);
91
92         editor->priv->text_color_combo = gl_color_combo_new (NULL,
93                                                              _("Default"),
94                                                              GL_COLOR_TEXT_DEFAULT,
95                                                              gl_prefs->default_text_color);
96         gtk_container_add (GTK_CONTAINER (editor->priv->text_color_hbox),
97                            editor->priv->text_color_combo);
98
99         gl_util_combo_box_add_text_model ( GTK_COMBO_BOX(editor->priv->text_family_combo));
100         gl_util_combo_box_add_text_model ( GTK_COMBO_BOX(editor->priv->text_color_key_combo));
101
102         /* Load family names */
103         family_names = gl_util_get_font_family_list ();
104         gl_util_combo_box_set_strings (GTK_COMBO_BOX(editor->priv->text_family_combo),
105                                        family_names);
106         gl_util_font_family_list_free (family_names);
107
108         /* Modify widgets */
109         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_radio), TRUE);
110         gtk_widget_set_sensitive (editor->priv->text_color_combo, TRUE);
111         gtk_widget_set_sensitive (editor->priv->text_color_key_combo, FALSE);
112
113         /* Un-hide */
114         gtk_widget_show_all (editor->priv->text_page_vbox);
115
116         /* Connect signals */
117         g_signal_connect_swapped (G_OBJECT (editor->priv->text_family_combo),
118                                   "changed",
119                                   G_CALLBACK (gl_object_editor_changed_cb),
120                                   G_OBJECT (editor));
121         g_signal_connect_swapped (G_OBJECT (editor->priv->text_size_spin),
122                                   "changed",
123                                   G_CALLBACK (gl_object_editor_changed_cb),
124                                   G_OBJECT (editor));
125         g_signal_connect_swapped (G_OBJECT (editor->priv->text_bold_toggle),
126                                   "toggled",
127                                   G_CALLBACK (gl_object_editor_changed_cb),
128                                   G_OBJECT (editor));
129         g_signal_connect_swapped (G_OBJECT (editor->priv->text_italic_toggle),
130                                   "toggled",
131                                   G_CALLBACK (gl_object_editor_changed_cb),
132                                   G_OBJECT (editor));
133         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_combo),
134                                   "color_changed",
135                                   G_CALLBACK (gl_object_editor_changed_cb),
136                                   G_OBJECT (editor));
137         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_key_combo),
138                                   "changed",
139                                   G_CALLBACK (gl_object_editor_changed_cb),
140                                   G_OBJECT (editor));
141         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_radio),
142                                   "toggled",
143                                   G_CALLBACK (text_radio_toggled_cb),
144                                   G_OBJECT (editor));                             
145         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_key_radio),
146                                   "toggled",
147                                   G_CALLBACK (text_radio_toggled_cb),
148                                   G_OBJECT (editor));
149
150         g_signal_connect (G_OBJECT (editor->priv->text_left_toggle),
151                           "toggled",
152                           G_CALLBACK (align_toggle_cb),
153                           G_OBJECT (editor));
154         g_signal_connect (G_OBJECT (editor->priv->text_center_toggle),
155                           "toggled",
156                           G_CALLBACK (align_toggle_cb),
157                           G_OBJECT (editor));
158         g_signal_connect (G_OBJECT (editor->priv->text_right_toggle),
159                           "toggled",
160                           G_CALLBACK (align_toggle_cb),
161                           G_OBJECT (editor));
162
163         g_signal_connect_swapped (G_OBJECT (editor->priv->text_line_spacing_spin),
164                                   "changed",
165                                   G_CALLBACK (gl_object_editor_changed_cb),
166                                   G_OBJECT (editor));
167
168         g_signal_connect_swapped (G_OBJECT (editor->priv->text_auto_shrink_check),
169                                   "toggled",
170                                   G_CALLBACK (gl_object_editor_changed_cb),
171                                   G_OBJECT (editor));
172
173         gl_debug (DEBUG_EDITOR, "END");
174 }
175
176 /*--------------------------------------------------------------------------*/
177 /* PRIVATE.  Alignment togglebutton callback.                               */
178 /*--------------------------------------------------------------------------*/
179 static void
180 align_toggle_cb (GtkToggleButton *toggle,
181                  glObjectEditor  *editor)
182 {
183         if (editor->priv->stop_signals) return;
184
185         if (gtk_toggle_button_get_active (toggle)) {
186  
187                 if (GTK_WIDGET (toggle) == GTK_WIDGET (editor->priv->text_left_toggle)) {
188                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
189                                                       (editor->priv->text_center_toggle),
190                                                       FALSE);
191                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
192                                                       (editor->priv->text_right_toggle),
193                                                       FALSE);
194                 } else if (GTK_WIDGET (toggle) ==
195                            GTK_WIDGET (editor->priv->text_center_toggle)) {
196                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
197                                                       (editor->priv->text_left_toggle),
198                                                       FALSE);
199                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
200                                                       (editor->priv->text_right_toggle),
201                                                       FALSE);
202                 } else if (GTK_WIDGET (toggle) ==
203                            GTK_WIDGET (editor->priv->text_right_toggle)) {
204                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
205                                                       (editor->priv->text_left_toggle),
206                                                       FALSE);
207                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
208                                                       (editor->priv->text_center_toggle),
209                                                       FALSE);
210                 }
211
212                 /* Emit our "changed" signal */
213                 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
214         }
215
216 }
217
218 /*****************************************************************************/
219 /* Set font family.                                                          */
220 /*****************************************************************************/
221 void
222 gl_object_editor_set_font_family (glObjectEditor      *editor,
223                                   const gchar         *font_family)
224 {
225         GList    *family_names;
226         gchar    *good_font_family;
227
228         gl_debug (DEBUG_EDITOR, "START");
229
230         editor->priv->stop_signals = TRUE;
231
232         /* Make sure we have a valid font family.  if not provide a good default. */
233         family_names = gl_util_get_font_family_list ();
234         if (g_list_find_custom (family_names, font_family, (GCompareFunc)g_utf8_collate)) {
235                 good_font_family = g_strdup (font_family);
236         } else {
237                 if (family_names != NULL) {
238                         good_font_family = g_strdup (family_names->data); /* 1st entry */
239                 } else {
240                         good_font_family = NULL;
241                 }
242         }
243         gl_util_font_family_list_free (family_names);
244         gl_util_combo_box_set_active_text (GTK_COMBO_BOX (editor->priv->text_family_combo), good_font_family);
245         g_free (good_font_family);
246
247         editor->priv->stop_signals = FALSE;
248
249         gl_debug (DEBUG_EDITOR, "END");
250 }
251
252 /*****************************************************************************/
253 /* Query font family.                                                        */
254 /*****************************************************************************/
255 gchar *
256 gl_object_editor_get_font_family (glObjectEditor      *editor)
257 {
258         gchar *font_family;
259
260         gl_debug (DEBUG_EDITOR, "START");
261
262         font_family = gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->text_family_combo));
263
264         gl_debug (DEBUG_EDITOR, "END");
265
266         return font_family;
267 }
268
269 /*****************************************************************************/
270 /* Set font size.                                                            */
271 /*****************************************************************************/
272 void
273 gl_object_editor_set_font_size (glObjectEditor      *editor,
274                                 gdouble              font_size)
275 {
276         gl_debug (DEBUG_EDITOR, "START");
277
278         editor->priv->stop_signals = TRUE;
279
280         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->text_size_spin),
281                                    font_size);
282
283         editor->priv->stop_signals = FALSE;
284
285         gl_debug (DEBUG_EDITOR, "END");
286 }
287
288 /*****************************************************************************/
289 /* Query font size.                                                          */
290 /*****************************************************************************/
291 gdouble
292 gl_object_editor_get_font_size (glObjectEditor      *editor)
293 {
294         gdouble font_size;
295
296         gl_debug (DEBUG_EDITOR, "START");
297
298         font_size =
299                 gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->text_size_spin));
300
301         gl_debug (DEBUG_EDITOR, "END");
302
303         return font_size;
304 }
305
306 /*****************************************************************************/
307 /* Set font weight.                                                          */
308 /*****************************************************************************/
309 void
310 gl_object_editor_set_font_weight (glObjectEditor      *editor,
311                                   PangoWeight          font_weight)
312 {
313         gl_debug (DEBUG_EDITOR, "START");
314
315         editor->priv->stop_signals = TRUE;
316
317         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_bold_toggle),
318                                       (font_weight == PANGO_WEIGHT_BOLD));
319
320         editor->priv->stop_signals = FALSE;
321
322         gl_debug (DEBUG_EDITOR, "END");
323 }
324
325 /*****************************************************************************/
326 /* Query font weight.                                                        */
327 /*****************************************************************************/
328 PangoWeight
329 gl_object_editor_get_font_weight (glObjectEditor      *editor)
330 {
331         PangoWeight font_weight;
332
333         gl_debug (DEBUG_EDITOR, "START");
334
335         if (gtk_toggle_button_get_active
336             (GTK_TOGGLE_BUTTON (editor->priv->text_bold_toggle))) {
337                 font_weight = PANGO_WEIGHT_BOLD;
338         } else {
339                 font_weight = PANGO_WEIGHT_NORMAL;
340         }
341
342         gl_debug (DEBUG_EDITOR, "END");
343
344         return font_weight;
345 }
346
347 /*****************************************************************************/
348 /* Set font italic flag.                                                     */
349 /*****************************************************************************/
350 void
351 gl_object_editor_set_font_italic_flag (glObjectEditor      *editor,
352                                        gboolean             font_italic_flag)
353 {
354         gl_debug (DEBUG_EDITOR, "START");
355
356         editor->priv->stop_signals = TRUE;
357
358         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_italic_toggle),
359                                       font_italic_flag);
360
361         editor->priv->stop_signals = FALSE;
362
363         gl_debug (DEBUG_EDITOR, "END");
364 }
365
366 /*****************************************************************************/
367 /* Query font italic flag.                                                   */
368 /*****************************************************************************/
369 gboolean
370 gl_object_editor_get_font_italic_flag (glObjectEditor      *editor)
371 {
372         gboolean italic_flag;
373
374         gl_debug (DEBUG_EDITOR, "START");
375
376         italic_flag =
377                 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
378                                               (editor->priv->text_italic_toggle));
379
380         gl_debug (DEBUG_EDITOR, "END");
381
382         return italic_flag;
383 }
384
385 /*****************************************************************************/
386 /* Set text alignment.                                                       */
387 /*****************************************************************************/
388 void
389 gl_object_editor_set_text_alignment (glObjectEditor      *editor,
390                                      PangoAlignment       align)
391 {
392         gl_debug (DEBUG_EDITOR, "START");
393
394         editor->priv->stop_signals = TRUE;
395
396         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_left_toggle),
397                                       (align == PANGO_ALIGN_LEFT));
398         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_center_toggle),
399                                       (align == PANGO_ALIGN_CENTER));
400         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_right_toggle),
401                                       (align == PANGO_ALIGN_RIGHT));
402
403         editor->priv->stop_signals = FALSE;
404
405         gl_debug (DEBUG_EDITOR, "END");
406 }
407
408 /*****************************************************************************/
409 /* Query text alignment.                                                     */
410 /*****************************************************************************/
411 PangoAlignment
412 gl_object_editor_get_text_alignment (glObjectEditor      *editor)
413 {
414         PangoAlignment align;
415
416         gl_debug (DEBUG_EDITOR, "START");
417
418         if (gtk_toggle_button_get_active
419             (GTK_TOGGLE_BUTTON (editor->priv->text_left_toggle))) {
420                 align = PANGO_ALIGN_LEFT;
421         } else
422             if (gtk_toggle_button_get_active
423                 (GTK_TOGGLE_BUTTON (editor->priv->text_right_toggle))) {
424                 align = PANGO_ALIGN_RIGHT;
425         } else
426             if (gtk_toggle_button_get_active
427                 (GTK_TOGGLE_BUTTON (editor->priv->text_center_toggle))) {
428                 align = PANGO_ALIGN_CENTER;
429         } else {
430                 align = PANGO_ALIGN_LEFT;       /* Should not happen. */
431         }
432
433         gl_debug (DEBUG_EDITOR, "END");
434
435         return align;
436 }
437
438 /*****************************************************************************/
439 /* Set text color.                                                           */
440 /*****************************************************************************/
441 void
442 gl_object_editor_set_text_color (glObjectEditor      *editor,
443                                  gboolean             merge_flag,
444                                  glColorNode         *text_color_node)
445 {
446         gl_debug (DEBUG_EDITOR, "START");
447
448         editor->priv->stop_signals = TRUE;
449
450         gl_debug (DEBUG_EDITOR, "color field %s(%d) / %X", text_color_node->key, text_color_node->field_flag, text_color_node->color);
451         
452         gtk_widget_set_sensitive (editor->priv->text_color_key_radio, merge_flag);
453
454         if ( text_color_node->color == GL_COLOR_NONE ) {
455
456                 gl_color_combo_set_to_default (GL_COLOR_COMBO(editor->priv->text_color_combo));
457
458         } else {
459                 
460                 gl_color_combo_set_color (GL_COLOR_COMBO(editor->priv->text_color_combo),
461                                           text_color_node->color);
462         }
463         
464         if (!text_color_node->field_flag) {
465                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
466                                                   (editor->priv->text_color_radio), TRUE); 
467                 gtk_widget_set_sensitive (editor->priv->text_color_combo, TRUE);
468                 gl_debug (DEBUG_EDITOR, "color field false 0");
469                 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, FALSE);
470                 
471         } else {
472                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
473                                                   (editor->priv->text_color_key_radio), TRUE); 
474                 gtk_widget_set_sensitive (editor->priv->text_color_combo, FALSE);
475                 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, TRUE);
476                 
477                 gl_debug (DEBUG_EDITOR, "color field true 1");
478                 gl_util_combo_box_set_active_text (GTK_COMBO_BOX (editor->priv->text_color_key_combo), "");
479                 gl_debug (DEBUG_EDITOR, "color field true 2");
480         }
481
482         editor->priv->stop_signals = FALSE;
483
484         gl_debug (DEBUG_EDITOR, "END");
485 }
486
487 /*****************************************************************************/
488 /* Query text color.                                                         */
489 /*****************************************************************************/
490 glColorNode*
491 gl_object_editor_get_text_color (glObjectEditor      *editor)
492 {
493         guint        color;
494         glColorNode *color_node;
495         gboolean     is_default;
496
497         gl_debug (DEBUG_EDITOR, "START");
498
499         color_node = gl_color_node_new_default ();
500         
501         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_key_radio))) {
502                 color_node->field_flag = TRUE;
503                 color_node->color = gl_prefs->default_text_color;
504                 color_node->key = 
505                         gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->text_color_key_combo));
506     } else {
507                 color_node->field_flag = FALSE;
508                 color_node->key = NULL;
509                 color = gl_color_combo_get_color (GL_COLOR_COMBO(editor->priv->text_color_combo),
510                                                   &is_default);
511
512         if (is_default) {
513                 color_node->color = gl_prefs->default_text_color;
514         } else {
515                 color_node->color = color;
516         }
517         }      
518
519         gl_debug (DEBUG_EDITOR, "END");
520
521         return color_node;
522 }
523
524
525 /*****************************************************************************/
526 /* Set text line spacing.                                                    */
527 /*****************************************************************************/
528 void
529 gl_object_editor_set_text_line_spacing (glObjectEditor      *editor,
530                                         gdouble              text_line_spacing)
531 {
532         gl_debug (DEBUG_EDITOR, "START");
533
534         editor->priv->stop_signals = TRUE;
535
536         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->text_line_spacing_spin),
537                                    text_line_spacing);
538
539         editor->priv->stop_signals = FALSE;
540
541         gl_debug (DEBUG_EDITOR, "END");
542 }
543
544 /*****************************************************************************/
545 /* Query text line spacing.                                                  */
546 /*****************************************************************************/
547 gdouble
548 gl_object_editor_get_text_line_spacing (glObjectEditor      *editor)
549 {
550         gdouble text_line_spacing;
551
552         gl_debug (DEBUG_EDITOR, "START");
553
554         text_line_spacing = 
555                 gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->text_line_spacing_spin));
556
557         gl_debug (DEBUG_EDITOR, "END");
558
559         return text_line_spacing;
560 }
561
562 /*****************************************************************************/
563 /* Set auto shrink checkbox.                                                 */
564 /*****************************************************************************/
565 void
566 gl_object_editor_set_text_auto_shrink (glObjectEditor      *editor,
567                                        gboolean             auto_shrink)
568 {
569         gl_debug (DEBUG_EDITOR, "START");
570
571         editor->priv->stop_signals = TRUE;
572
573         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_auto_shrink_check),
574                                       auto_shrink);
575
576         editor->priv->stop_signals = FALSE;
577
578         gl_debug (DEBUG_EDITOR, "END");
579 }
580
581 /*****************************************************************************/
582 /* Query auto shrink checkbox.                                               */
583 /*****************************************************************************/
584 gboolean    gl_object_editor_get_text_auto_shrink (glObjectEditor      *editor)
585 {
586         gboolean auto_shrink;
587
588         gl_debug (DEBUG_EDITOR, "START");
589
590         auto_shrink = 
591                 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_auto_shrink_check));
592
593         gl_debug (DEBUG_EDITOR, "END");
594
595         return auto_shrink;
596 }
597
598 /*--------------------------------------------------------------------------*/
599 /* PRIVATE.  color radio callback.                                           */
600 /*--------------------------------------------------------------------------*/
601 static void
602 text_radio_toggled_cb (glObjectEditor *editor)
603 {
604         if (editor->priv->stop_signals) return;
605
606         gl_debug (DEBUG_EDITOR, "START");
607         
608         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_radio))) {
609                 gtk_widget_set_sensitive (editor->priv->text_color_combo, TRUE);
610                 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, FALSE);
611         } else {
612                 gtk_widget_set_sensitive (editor->priv->text_color_combo, FALSE);
613                 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, TRUE);
614                 
615         }
616  
617         /* Emit our "changed" signal */
618         g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
619  
620         gl_debug (DEBUG_EDITOR, "END");
621 }