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