]> git.sur5r.net Git - glabels/blob - src/object-editor-text-page.c
Removed non-existent filenames from po/POTFILES.in
[glabels] / src / object-editor-text-page.c
1 /*
2  *  object-editor-text-page.c
3  *  Copyright (C) 2003-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "object-editor.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <math.h>
28
29 #include "prefs.h"
30 #include "color-combo.h"
31 #include "color.h"
32 #include "font-combo.h"
33 #include "font-util.h"
34 #include "combo-util.h"
35 #include "builder-util.h"
36
37 #include "object-editor-private.h"
38
39 #include "debug.h"
40
41
42 /*===========================================*/
43 /* Private macros                            */
44 /*===========================================*/
45
46
47 /*===========================================*/
48 /* Private data types                        */
49 /*===========================================*/
50
51
52 /*===========================================*/
53 /* Private globals                           */
54 /*===========================================*/
55
56
57 /*===========================================*/
58 /* Local function prototypes                 */
59 /*===========================================*/
60
61 static void align_toggle_cb (GtkToggleButton *toggle,
62                              glObjectEditor  *editor);
63 static void text_radio_toggled_cb              (glObjectEditor        *editor);
64
65
66 /*--------------------------------------------------------------------------*/
67 /* PRIVATE.  Prepare size page.                                             */
68 /*--------------------------------------------------------------------------*/
69 void
70 gl_object_editor_prepare_text_page (glObjectEditor       *editor)
71 {
72         gl_debug (DEBUG_EDITOR, "START");
73
74         /* Extract widgets from XML tree. */
75         gl_builder_util_get_widgets (editor->priv->builder,
76                                      "text_page_vbox",         &editor->priv->text_page_vbox,
77                                      "text_family_hbox",       &editor->priv->text_family_hbox,
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_family_combo = gl_font_combo_new ("Sans");
93         gtk_box_pack_start (GTK_BOX (editor->priv->text_family_hbox),
94                             editor->priv->text_family_combo,
95                             TRUE, TRUE, 0);
96
97         editor->priv->text_color_combo = gl_color_combo_new (_("Default"),
98                                                              GL_COLOR_TEXT_DEFAULT,
99                                                              gl_prefs->default_text_color);
100         gtk_box_pack_start (GTK_BOX (editor->priv->text_color_hbox),
101                             editor->priv->text_color_combo,
102                             FALSE, FALSE, 0);
103
104         gl_combo_util_add_text_model ( GTK_COMBO_BOX(editor->priv->text_color_key_combo));
105
106         /* Modify widgets */
107         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_radio), TRUE);
108         gtk_widget_set_sensitive (editor->priv->text_color_combo, TRUE);
109         gtk_widget_set_sensitive (editor->priv->text_color_key_combo, FALSE);
110
111         /* Un-hide */
112         gtk_widget_show_all (editor->priv->text_page_vbox);
113
114         /* Connect signals */
115         g_signal_connect_swapped (G_OBJECT (editor->priv->text_family_combo),
116                                   "changed",
117                                   G_CALLBACK (gl_object_editor_changed_cb),
118                                   G_OBJECT (editor));
119         g_signal_connect_swapped (G_OBJECT (editor->priv->text_size_spin),
120                                   "changed",
121                                   G_CALLBACK (gl_object_editor_changed_cb),
122                                   G_OBJECT (editor));
123         g_signal_connect_swapped (G_OBJECT (editor->priv->text_bold_toggle),
124                                   "toggled",
125                                   G_CALLBACK (gl_object_editor_changed_cb),
126                                   G_OBJECT (editor));
127         g_signal_connect_swapped (G_OBJECT (editor->priv->text_italic_toggle),
128                                   "toggled",
129                                   G_CALLBACK (gl_object_editor_changed_cb),
130                                   G_OBJECT (editor));
131         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_combo),
132                                   "color_changed",
133                                   G_CALLBACK (gl_object_editor_changed_cb),
134                                   G_OBJECT (editor));
135         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_key_combo),
136                                   "changed",
137                                   G_CALLBACK (gl_object_editor_changed_cb),
138                                   G_OBJECT (editor));
139         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_radio),
140                                   "toggled",
141                                   G_CALLBACK (text_radio_toggled_cb),
142                                   G_OBJECT (editor));                             
143         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_key_radio),
144                                   "toggled",
145                                   G_CALLBACK (text_radio_toggled_cb),
146                                   G_OBJECT (editor));
147
148         g_signal_connect (G_OBJECT (editor->priv->text_left_toggle),
149                           "toggled",
150                           G_CALLBACK (align_toggle_cb),
151                           G_OBJECT (editor));
152         g_signal_connect (G_OBJECT (editor->priv->text_center_toggle),
153                           "toggled",
154                           G_CALLBACK (align_toggle_cb),
155                           G_OBJECT (editor));
156         g_signal_connect (G_OBJECT (editor->priv->text_right_toggle),
157                           "toggled",
158                           G_CALLBACK (align_toggle_cb),
159                           G_OBJECT (editor));
160
161         g_signal_connect_swapped (G_OBJECT (editor->priv->text_line_spacing_spin),
162                                   "changed",
163                                   G_CALLBACK (gl_object_editor_changed_cb),
164                                   G_OBJECT (editor));
165
166         g_signal_connect_swapped (G_OBJECT (editor->priv->text_auto_shrink_check),
167                                   "toggled",
168                                   G_CALLBACK (gl_object_editor_changed_cb),
169                                   G_OBJECT (editor));
170
171         gl_debug (DEBUG_EDITOR, "END");
172 }
173
174
175 /*--------------------------------------------------------------------------*/
176 /* PRIVATE.  Alignment togglebutton callback.                               */
177 /*--------------------------------------------------------------------------*/
178 static void
179 align_toggle_cb (GtkToggleButton *toggle,
180                  glObjectEditor  *editor)
181 {
182         if (editor->priv->stop_signals) return;
183
184         if (gtk_toggle_button_get_active (toggle)) {
185  
186                 if (GTK_WIDGET (toggle) == GTK_WIDGET (editor->priv->text_left_toggle)) {
187                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
188                                                       (editor->priv->text_center_toggle),
189                                                       FALSE);
190                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
191                                                       (editor->priv->text_right_toggle),
192                                                       FALSE);
193                 } else if (GTK_WIDGET (toggle) ==
194                            GTK_WIDGET (editor->priv->text_center_toggle)) {
195                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
196                                                       (editor->priv->text_left_toggle),
197                                                       FALSE);
198                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
199                                                       (editor->priv->text_right_toggle),
200                                                       FALSE);
201                 } else if (GTK_WIDGET (toggle) ==
202                            GTK_WIDGET (editor->priv->text_right_toggle)) {
203                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
204                                                       (editor->priv->text_left_toggle),
205                                                       FALSE);
206                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
207                                                       (editor->priv->text_center_toggle),
208                                                       FALSE);
209                 }
210
211                 /* Emit our "changed" signal */
212                 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
213         }
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         gchar    *old_font_family;
226
227         gl_debug (DEBUG_EDITOR, "START");
228
229         editor->priv->stop_signals = TRUE;
230
231         old_font_family = gl_font_combo_get_family (GL_FONT_COMBO (editor->priv->text_family_combo));
232
233         if ( !old_font_family || g_utf8_collate( old_font_family, font_family ) )
234         {
235                 gl_font_combo_set_family (GL_FONT_COMBO (editor->priv->text_family_combo),
236                                           font_family);
237         }
238
239         g_free (old_font_family);
240
241         editor->priv->stop_signals = FALSE;
242
243         gl_debug (DEBUG_EDITOR, "END");
244 }
245
246
247 /*****************************************************************************/
248 /* Query font family.                                                        */
249 /*****************************************************************************/
250 gchar *
251 gl_object_editor_get_font_family (glObjectEditor      *editor)
252 {
253         gchar *font_family;
254
255         gl_debug (DEBUG_EDITOR, "START");
256
257         font_family = gl_font_combo_get_family (GL_FONT_COMBO (editor->priv->text_family_combo));
258
259         gl_debug (DEBUG_EDITOR, "END");
260
261         return font_family;
262 }
263
264
265 /*****************************************************************************/
266 /* Set font size.                                                            */
267 /*****************************************************************************/
268 void
269 gl_object_editor_set_font_size (glObjectEditor      *editor,
270                                 gdouble              font_size)
271 {
272         gl_debug (DEBUG_EDITOR, "START");
273
274         editor->priv->stop_signals = TRUE;
275
276         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->text_size_spin),
277                                    font_size);
278
279         editor->priv->stop_signals = FALSE;
280
281         gl_debug (DEBUG_EDITOR, "END");
282 }
283
284
285 /*****************************************************************************/
286 /* Query font size.                                                          */
287 /*****************************************************************************/
288 gdouble
289 gl_object_editor_get_font_size (glObjectEditor      *editor)
290 {
291         gdouble font_size;
292
293         gl_debug (DEBUG_EDITOR, "START");
294
295         font_size =
296                 gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->text_size_spin));
297
298         gl_debug (DEBUG_EDITOR, "END");
299
300         return font_size;
301 }
302
303
304 /*****************************************************************************/
305 /* Set font weight.                                                          */
306 /*****************************************************************************/
307 void
308 gl_object_editor_set_font_weight (glObjectEditor      *editor,
309                                   PangoWeight          font_weight)
310 {
311         gl_debug (DEBUG_EDITOR, "START");
312
313         editor->priv->stop_signals = TRUE;
314
315         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_bold_toggle),
316                                       (font_weight == PANGO_WEIGHT_BOLD));
317
318         editor->priv->stop_signals = FALSE;
319
320         gl_debug (DEBUG_EDITOR, "END");
321 }
322
323
324 /*****************************************************************************/
325 /* Query font weight.                                                        */
326 /*****************************************************************************/
327 PangoWeight
328 gl_object_editor_get_font_weight (glObjectEditor      *editor)
329 {
330         PangoWeight font_weight;
331
332         gl_debug (DEBUG_EDITOR, "START");
333
334         if (gtk_toggle_button_get_active
335             (GTK_TOGGLE_BUTTON (editor->priv->text_bold_toggle))) {
336                 font_weight = PANGO_WEIGHT_BOLD;
337         } else {
338                 font_weight = PANGO_WEIGHT_NORMAL;
339         }
340
341         gl_debug (DEBUG_EDITOR, "END");
342
343         return font_weight;
344 }
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 /*****************************************************************************/
368 /* Query font italic flag.                                                   */
369 /*****************************************************************************/
370 gboolean
371 gl_object_editor_get_font_italic_flag (glObjectEditor      *editor)
372 {
373         gboolean italic_flag;
374
375         gl_debug (DEBUG_EDITOR, "START");
376
377         italic_flag =
378                 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
379                                               (editor->priv->text_italic_toggle));
380
381         gl_debug (DEBUG_EDITOR, "END");
382
383         return italic_flag;
384 }
385
386
387 /*****************************************************************************/
388 /* Set text alignment.                                                       */
389 /*****************************************************************************/
390 void
391 gl_object_editor_set_text_alignment (glObjectEditor      *editor,
392                                      PangoAlignment       align)
393 {
394         gl_debug (DEBUG_EDITOR, "START");
395
396         editor->priv->stop_signals = TRUE;
397
398         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_left_toggle),
399                                       (align == PANGO_ALIGN_LEFT));
400         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_center_toggle),
401                                       (align == PANGO_ALIGN_CENTER));
402         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_right_toggle),
403                                       (align == PANGO_ALIGN_RIGHT));
404
405         editor->priv->stop_signals = FALSE;
406
407         gl_debug (DEBUG_EDITOR, "END");
408 }
409
410
411 /*****************************************************************************/
412 /* Query text alignment.                                                     */
413 /*****************************************************************************/
414 PangoAlignment
415 gl_object_editor_get_text_alignment (glObjectEditor      *editor)
416 {
417         PangoAlignment align;
418
419         gl_debug (DEBUG_EDITOR, "START");
420
421         if (gtk_toggle_button_get_active
422             (GTK_TOGGLE_BUTTON (editor->priv->text_left_toggle))) {
423                 align = PANGO_ALIGN_LEFT;
424         } else
425             if (gtk_toggle_button_get_active
426                 (GTK_TOGGLE_BUTTON (editor->priv->text_right_toggle))) {
427                 align = PANGO_ALIGN_RIGHT;
428         } else
429             if (gtk_toggle_button_get_active
430                 (GTK_TOGGLE_BUTTON (editor->priv->text_center_toggle))) {
431                 align = PANGO_ALIGN_CENTER;
432         } else {
433                 align = PANGO_ALIGN_LEFT;       /* Should not happen. */
434         }
435
436         gl_debug (DEBUG_EDITOR, "END");
437
438         return align;
439 }
440
441
442 /*****************************************************************************/
443 /* Set text color.                                                           */
444 /*****************************************************************************/
445 void
446 gl_object_editor_set_text_color (glObjectEditor      *editor,
447                                  gboolean             merge_flag,
448                                  glColorNode         *text_color_node)
449 {
450         gl_debug (DEBUG_EDITOR, "START");
451
452         editor->priv->stop_signals = TRUE;
453
454         gl_debug (DEBUG_EDITOR, "color field %s(%d) / %X", text_color_node->key, text_color_node->field_flag, text_color_node->color);
455         
456         gtk_widget_set_sensitive (editor->priv->text_color_key_radio, merge_flag);
457
458         if ( text_color_node->color == GL_COLOR_NONE ) {
459
460                 gl_color_combo_set_to_default (GL_COLOR_COMBO(editor->priv->text_color_combo));
461
462         } else {
463                 
464                 gl_color_combo_set_color (GL_COLOR_COMBO(editor->priv->text_color_combo),
465                                           text_color_node->color);
466         }
467         
468         if (!text_color_node->field_flag) {
469                 gl_debug (DEBUG_EDITOR, "color field false");
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                 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, FALSE);
474                 
475         } else {
476                 gl_debug (DEBUG_EDITOR, "color field true");
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_combo_util_set_active_text (GTK_COMBO_BOX (editor->priv->text_color_key_combo), "");
483         }
484
485         editor->priv->stop_signals = FALSE;
486
487         gl_debug (DEBUG_EDITOR, "END");
488 }
489
490
491 /*****************************************************************************/
492 /* Query text color.                                                         */
493 /*****************************************************************************/
494 glColorNode*
495 gl_object_editor_get_text_color (glObjectEditor      *editor)
496 {
497         guint        color;
498         glColorNode *color_node;
499         gboolean     is_default;
500
501         gl_debug (DEBUG_EDITOR, "START");
502
503         color_node = gl_color_node_new_default ();
504         
505         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_key_radio))) {
506                 color_node->field_flag = TRUE;
507                 color_node->color = gl_prefs->default_text_color;
508                 color_node->key = 
509                         gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->text_color_key_combo));
510         } else {
511                 color_node->field_flag = FALSE;
512                 color_node->key = NULL;
513                 color = gl_color_combo_get_color (GL_COLOR_COMBO(editor->priv->text_color_combo),
514                                                   &is_default);
515
516                 if (is_default) {
517                         color_node->color = gl_prefs->default_text_color;
518                 } else {
519                         color_node->color = color;
520                 }
521         }      
522
523         gl_debug (DEBUG_EDITOR, "END");
524
525         return color_node;
526 }
527
528
529 /*****************************************************************************/
530 /* Set text line spacing.                                                    */
531 /*****************************************************************************/
532 void
533 gl_object_editor_set_text_line_spacing (glObjectEditor      *editor,
534                                         gdouble              text_line_spacing)
535 {
536         gl_debug (DEBUG_EDITOR, "START");
537
538         editor->priv->stop_signals = TRUE;
539
540         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->text_line_spacing_spin),
541                                    text_line_spacing);
542
543         editor->priv->stop_signals = FALSE;
544
545         gl_debug (DEBUG_EDITOR, "END");
546 }
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 /*****************************************************************************/
569 /* Set auto shrink checkbox.                                                 */
570 /*****************************************************************************/
571 void
572 gl_object_editor_set_text_auto_shrink (glObjectEditor      *editor,
573                                        gboolean             auto_shrink)
574 {
575         gl_debug (DEBUG_EDITOR, "START");
576
577         editor->priv->stop_signals = TRUE;
578
579         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_auto_shrink_check),
580                                       auto_shrink);
581
582         editor->priv->stop_signals = FALSE;
583
584         gl_debug (DEBUG_EDITOR, "END");
585 }
586
587
588 /*****************************************************************************/
589 /* Query auto shrink checkbox.                                               */
590 /*****************************************************************************/
591 gboolean    gl_object_editor_get_text_auto_shrink (glObjectEditor      *editor)
592 {
593         gboolean auto_shrink;
594
595         gl_debug (DEBUG_EDITOR, "START");
596
597         auto_shrink = 
598                 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_auto_shrink_check));
599
600         gl_debug (DEBUG_EDITOR, "END");
601
602         return auto_shrink;
603 }
604
605
606 /*--------------------------------------------------------------------------*/
607 /* PRIVATE.  color radio callback.                                           */
608 /*--------------------------------------------------------------------------*/
609 static void
610 text_radio_toggled_cb (glObjectEditor *editor)
611 {
612         if (editor->priv->stop_signals) return;
613
614         gl_debug (DEBUG_EDITOR, "START");
615         
616         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_radio))) {
617                 gtk_widget_set_sensitive (editor->priv->text_color_combo, TRUE);
618                 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, FALSE);
619         } else {
620                 gtk_widget_set_sensitive (editor->priv->text_color_combo, FALSE);
621                 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, TRUE);
622                 
623         }
624  
625         /* Emit our "changed" signal */
626         g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
627  
628         gl_debug (DEBUG_EDITOR, "END");
629 }
630
631
632
633 /*
634  * Local Variables:       -- emacs
635  * mode: C                -- emacs
636  * c-basic-offset: 8      -- emacs
637  * tab-width: 8           -- emacs
638  * indent-tabs-mode: nil  -- emacs
639  * End:                   -- emacs
640  */