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