]> git.sur5r.net Git - glabels/blob - glabels2/src/object-editor-text-page.c
6160668132387c060bb8e4ab3268dafe6810f385
[glabels] / glabels2 / 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/gtktogglebutton.h>
27 #include <gtk/gtkspinbutton.h>
28 #include <gtk/gtkcombobox.h>
29 #include <math.h>
30
31 #include "prefs.h"
32 #include "color-combo.h"
33 #include "color.h"
34 #include "font-combo.h"
35 #include "font-util.h"
36 #include "util.h"
37
38 #include "object-editor-private.h"
39
40 #include "debug.h"
41
42
43 /*===========================================*/
44 /* Private macros                            */
45 /*===========================================*/
46
47
48 /*===========================================*/
49 /* Private data types                        */
50 /*===========================================*/
51
52
53 /*===========================================*/
54 /* Private globals                           */
55 /*===========================================*/
56
57
58 /*===========================================*/
59 /* Local function prototypes                 */
60 /*===========================================*/
61
62 static void align_toggle_cb (GtkToggleButton *toggle,
63                              glObjectEditor  *editor);
64 static void text_radio_toggled_cb              (glObjectEditor        *editor);
65
66
67 /*--------------------------------------------------------------------------*/
68 /* PRIVATE.  Prepare size page.                                             */
69 /*--------------------------------------------------------------------------*/
70 void
71 gl_object_editor_prepare_text_page (glObjectEditor       *editor)
72 {
73         gl_debug (DEBUG_EDITOR, "START");
74
75         /* Extract widgets from XML tree. */
76         gl_util_get_builder_widgets (editor->priv->builder,
77                                      "text_page_vbox",         &editor->priv->text_page_vbox,
78                                      "text_family_hbox",       &editor->priv->text_family_hbox,
79                                      "text_size_spin",         &editor->priv->text_size_spin,
80                                      "text_bold_toggle",       &editor->priv->text_bold_toggle,
81                                      "text_italic_toggle",     &editor->priv->text_italic_toggle,
82                                      "text_color_hbox",        &editor->priv->text_color_hbox,
83                                      "text_color_radio",       &editor->priv->text_color_radio,
84                                      "text_color_key_radio",   &editor->priv->text_color_key_radio,
85                                      "text_color_key_combo",   &editor->priv->text_color_key_combo,
86                                      "text_left_toggle",       &editor->priv->text_left_toggle,
87                                      "text_center_toggle",     &editor->priv->text_center_toggle,
88                                      "text_right_toggle",      &editor->priv->text_right_toggle,
89                                      "text_line_spacing_spin", &editor->priv->text_line_spacing_spin,
90                                      "text_auto_shrink_check", &editor->priv->text_auto_shrink_check,
91                                      NULL);
92
93         editor->priv->text_family_combo = gl_font_combo_new ("Sans");
94         gtk_box_pack_start (GTK_BOX (editor->priv->text_family_hbox),
95                             editor->priv->text_family_combo,
96                             TRUE, TRUE, 0);
97
98         editor->priv->text_color_combo = gl_color_combo_new (_("Default"),
99                                                              GL_COLOR_TEXT_DEFAULT,
100                                                              gl_prefs->default_text_color);
101         gtk_box_pack_start (GTK_BOX (editor->priv->text_color_hbox),
102                             editor->priv->text_color_combo,
103                             FALSE, FALSE, 0);
104
105         gl_util_combo_box_add_text_model ( GTK_COMBO_BOX(editor->priv->text_color_key_combo));
106
107         /* Modify widgets */
108         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_radio), TRUE);
109         gtk_widget_set_sensitive (editor->priv->text_color_combo, TRUE);
110         gtk_widget_set_sensitive (editor->priv->text_color_key_combo, FALSE);
111
112         /* Un-hide */
113         gtk_widget_show_all (editor->priv->text_page_vbox);
114
115         /* Connect signals */
116         g_signal_connect_swapped (G_OBJECT (editor->priv->text_family_combo),
117                                   "changed",
118                                   G_CALLBACK (gl_object_editor_changed_cb),
119                                   G_OBJECT (editor));
120         g_signal_connect_swapped (G_OBJECT (editor->priv->text_size_spin),
121                                   "changed",
122                                   G_CALLBACK (gl_object_editor_changed_cb),
123                                   G_OBJECT (editor));
124         g_signal_connect_swapped (G_OBJECT (editor->priv->text_bold_toggle),
125                                   "toggled",
126                                   G_CALLBACK (gl_object_editor_changed_cb),
127                                   G_OBJECT (editor));
128         g_signal_connect_swapped (G_OBJECT (editor->priv->text_italic_toggle),
129                                   "toggled",
130                                   G_CALLBACK (gl_object_editor_changed_cb),
131                                   G_OBJECT (editor));
132         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_combo),
133                                   "color_changed",
134                                   G_CALLBACK (gl_object_editor_changed_cb),
135                                   G_OBJECT (editor));
136         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_key_combo),
137                                   "changed",
138                                   G_CALLBACK (gl_object_editor_changed_cb),
139                                   G_OBJECT (editor));
140         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_radio),
141                                   "toggled",
142                                   G_CALLBACK (text_radio_toggled_cb),
143                                   G_OBJECT (editor));                             
144         g_signal_connect_swapped (G_OBJECT (editor->priv->text_color_key_radio),
145                                   "toggled",
146                                   G_CALLBACK (text_radio_toggled_cb),
147                                   G_OBJECT (editor));
148
149         g_signal_connect (G_OBJECT (editor->priv->text_left_toggle),
150                           "toggled",
151                           G_CALLBACK (align_toggle_cb),
152                           G_OBJECT (editor));
153         g_signal_connect (G_OBJECT (editor->priv->text_center_toggle),
154                           "toggled",
155                           G_CALLBACK (align_toggle_cb),
156                           G_OBJECT (editor));
157         g_signal_connect (G_OBJECT (editor->priv->text_right_toggle),
158                           "toggled",
159                           G_CALLBACK (align_toggle_cb),
160                           G_OBJECT (editor));
161
162         g_signal_connect_swapped (G_OBJECT (editor->priv->text_line_spacing_spin),
163                                   "changed",
164                                   G_CALLBACK (gl_object_editor_changed_cb),
165                                   G_OBJECT (editor));
166
167         g_signal_connect_swapped (G_OBJECT (editor->priv->text_auto_shrink_check),
168                                   "toggled",
169                                   G_CALLBACK (gl_object_editor_changed_cb),
170                                   G_OBJECT (editor));
171
172         gl_debug (DEBUG_EDITOR, "END");
173 }
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 /*****************************************************************************/
220 /* Set font family.                                                          */
221 /*****************************************************************************/
222 void
223 gl_object_editor_set_font_family (glObjectEditor      *editor,
224                                   const gchar         *font_family)
225 {
226         gchar    *old_font_family;
227
228         gl_debug (DEBUG_EDITOR, "START");
229
230         editor->priv->stop_signals = TRUE;
231
232         old_font_family = gl_font_combo_get_family (GL_FONT_COMBO (editor->priv->text_family_combo));
233
234         if ( !old_font_family || g_utf8_collate( old_font_family, font_family ) )
235         {
236                 gl_font_combo_set_family (GL_FONT_COMBO (editor->priv->text_family_combo),
237                                           font_family);
238         }
239
240         g_free (old_font_family);
241
242         editor->priv->stop_signals = FALSE;
243
244         gl_debug (DEBUG_EDITOR, "END");
245 }
246
247
248 /*****************************************************************************/
249 /* Query font family.                                                        */
250 /*****************************************************************************/
251 gchar *
252 gl_object_editor_get_font_family (glObjectEditor      *editor)
253 {
254         gchar *font_family;
255
256         gl_debug (DEBUG_EDITOR, "START");
257
258         font_family = gl_font_combo_get_family (GL_FONT_COMBO (editor->priv->text_family_combo));
259
260         gl_debug (DEBUG_EDITOR, "END");
261
262         return font_family;
263 }
264
265
266 /*****************************************************************************/
267 /* Set font size.                                                            */
268 /*****************************************************************************/
269 void
270 gl_object_editor_set_font_size (glObjectEditor      *editor,
271                                 gdouble              font_size)
272 {
273         gl_debug (DEBUG_EDITOR, "START");
274
275         editor->priv->stop_signals = TRUE;
276
277         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->text_size_spin),
278                                    font_size);
279
280         editor->priv->stop_signals = FALSE;
281
282         gl_debug (DEBUG_EDITOR, "END");
283 }
284
285
286 /*****************************************************************************/
287 /* Query font size.                                                          */
288 /*****************************************************************************/
289 gdouble
290 gl_object_editor_get_font_size (glObjectEditor      *editor)
291 {
292         gdouble font_size;
293
294         gl_debug (DEBUG_EDITOR, "START");
295
296         font_size =
297                 gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->text_size_spin));
298
299         gl_debug (DEBUG_EDITOR, "END");
300
301         return font_size;
302 }
303
304
305 /*****************************************************************************/
306 /* Set font weight.                                                          */
307 /*****************************************************************************/
308 void
309 gl_object_editor_set_font_weight (glObjectEditor      *editor,
310                                   PangoWeight          font_weight)
311 {
312         gl_debug (DEBUG_EDITOR, "START");
313
314         editor->priv->stop_signals = TRUE;
315
316         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_bold_toggle),
317                                       (font_weight == PANGO_WEIGHT_BOLD));
318
319         editor->priv->stop_signals = FALSE;
320
321         gl_debug (DEBUG_EDITOR, "END");
322 }
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 /*****************************************************************************/
349 /* Set font italic flag.                                                     */
350 /*****************************************************************************/
351 void
352 gl_object_editor_set_font_italic_flag (glObjectEditor      *editor,
353                                        gboolean             font_italic_flag)
354 {
355         gl_debug (DEBUG_EDITOR, "START");
356
357         editor->priv->stop_signals = TRUE;
358
359         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_italic_toggle),
360                                       font_italic_flag);
361
362         editor->priv->stop_signals = FALSE;
363
364         gl_debug (DEBUG_EDITOR, "END");
365 }
366
367
368 /*****************************************************************************/
369 /* Query font italic flag.                                                   */
370 /*****************************************************************************/
371 gboolean
372 gl_object_editor_get_font_italic_flag (glObjectEditor      *editor)
373 {
374         gboolean italic_flag;
375
376         gl_debug (DEBUG_EDITOR, "START");
377
378         italic_flag =
379                 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
380                                               (editor->priv->text_italic_toggle));
381
382         gl_debug (DEBUG_EDITOR, "END");
383
384         return italic_flag;
385 }
386
387
388 /*****************************************************************************/
389 /* Set text alignment.                                                       */
390 /*****************************************************************************/
391 void
392 gl_object_editor_set_text_alignment (glObjectEditor      *editor,
393                                      PangoAlignment       align)
394 {
395         gl_debug (DEBUG_EDITOR, "START");
396
397         editor->priv->stop_signals = TRUE;
398
399         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_left_toggle),
400                                       (align == PANGO_ALIGN_LEFT));
401         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_center_toggle),
402                                       (align == PANGO_ALIGN_CENTER));
403         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_right_toggle),
404                                       (align == PANGO_ALIGN_RIGHT));
405
406         editor->priv->stop_signals = FALSE;
407
408         gl_debug (DEBUG_EDITOR, "END");
409 }
410
411
412 /*****************************************************************************/
413 /* Query text alignment.                                                     */
414 /*****************************************************************************/
415 PangoAlignment
416 gl_object_editor_get_text_alignment (glObjectEditor      *editor)
417 {
418         PangoAlignment align;
419
420         gl_debug (DEBUG_EDITOR, "START");
421
422         if (gtk_toggle_button_get_active
423             (GTK_TOGGLE_BUTTON (editor->priv->text_left_toggle))) {
424                 align = PANGO_ALIGN_LEFT;
425         } else
426             if (gtk_toggle_button_get_active
427                 (GTK_TOGGLE_BUTTON (editor->priv->text_right_toggle))) {
428                 align = PANGO_ALIGN_RIGHT;
429         } else
430             if (gtk_toggle_button_get_active
431                 (GTK_TOGGLE_BUTTON (editor->priv->text_center_toggle))) {
432                 align = PANGO_ALIGN_CENTER;
433         } else {
434                 align = PANGO_ALIGN_LEFT;       /* Should not happen. */
435         }
436
437         gl_debug (DEBUG_EDITOR, "END");
438
439         return align;
440 }
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 /*****************************************************************************/
494 /* Query text color.                                                         */
495 /*****************************************************************************/
496 glColorNode*
497 gl_object_editor_get_text_color (glObjectEditor      *editor)
498 {
499         guint        color;
500         glColorNode *color_node;
501         gboolean     is_default;
502
503         gl_debug (DEBUG_EDITOR, "START");
504
505         color_node = gl_color_node_new_default ();
506         
507         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_key_radio))) {
508                 color_node->field_flag = TRUE;
509                 color_node->color = gl_prefs->default_text_color;
510                 color_node->key = 
511                         gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->text_color_key_combo));
512         } else {
513                 color_node->field_flag = FALSE;
514                 color_node->key = NULL;
515                 color = gl_color_combo_get_color (GL_COLOR_COMBO(editor->priv->text_color_combo),
516                                                   &is_default);
517
518                 if (is_default) {
519                         color_node->color = gl_prefs->default_text_color;
520                 } else {
521                         color_node->color = color;
522                 }
523         }      
524
525         gl_debug (DEBUG_EDITOR, "END");
526
527         return color_node;
528 }
529
530
531 /*****************************************************************************/
532 /* Set text line spacing.                                                    */
533 /*****************************************************************************/
534 void
535 gl_object_editor_set_text_line_spacing (glObjectEditor      *editor,
536                                         gdouble              text_line_spacing)
537 {
538         gl_debug (DEBUG_EDITOR, "START");
539
540         editor->priv->stop_signals = TRUE;
541
542         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->text_line_spacing_spin),
543                                    text_line_spacing);
544
545         editor->priv->stop_signals = FALSE;
546
547         gl_debug (DEBUG_EDITOR, "END");
548 }
549
550
551 /*****************************************************************************/
552 /* Query text line spacing.                                                  */
553 /*****************************************************************************/
554 gdouble
555 gl_object_editor_get_text_line_spacing (glObjectEditor      *editor)
556 {
557         gdouble text_line_spacing;
558
559         gl_debug (DEBUG_EDITOR, "START");
560
561         text_line_spacing = 
562                 gtk_spin_button_get_value (GTK_SPIN_BUTTON(editor->priv->text_line_spacing_spin));
563
564         gl_debug (DEBUG_EDITOR, "END");
565
566         return text_line_spacing;
567 }
568
569
570 /*****************************************************************************/
571 /* Set auto shrink checkbox.                                                 */
572 /*****************************************************************************/
573 void
574 gl_object_editor_set_text_auto_shrink (glObjectEditor      *editor,
575                                        gboolean             auto_shrink)
576 {
577         gl_debug (DEBUG_EDITOR, "START");
578
579         editor->priv->stop_signals = TRUE;
580
581         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (editor->priv->text_auto_shrink_check),
582                                       auto_shrink);
583
584         editor->priv->stop_signals = FALSE;
585
586         gl_debug (DEBUG_EDITOR, "END");
587 }
588
589
590 /*****************************************************************************/
591 /* Query auto shrink checkbox.                                               */
592 /*****************************************************************************/
593 gboolean    gl_object_editor_get_text_auto_shrink (glObjectEditor      *editor)
594 {
595         gboolean auto_shrink;
596
597         gl_debug (DEBUG_EDITOR, "START");
598
599         auto_shrink = 
600                 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_auto_shrink_check));
601
602         gl_debug (DEBUG_EDITOR, "END");
603
604         return auto_shrink;
605 }
606
607
608 /*--------------------------------------------------------------------------*/
609 /* PRIVATE.  color radio callback.                                           */
610 /*--------------------------------------------------------------------------*/
611 static void
612 text_radio_toggled_cb (glObjectEditor *editor)
613 {
614         if (editor->priv->stop_signals) return;
615
616         gl_debug (DEBUG_EDITOR, "START");
617         
618         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->text_color_radio))) {
619                 gtk_widget_set_sensitive (editor->priv->text_color_combo, TRUE);
620                 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, FALSE);
621         } else {
622                 gtk_widget_set_sensitive (editor->priv->text_color_combo, FALSE);
623                 gtk_widget_set_sensitive (editor->priv->text_color_key_combo, TRUE);
624                 
625         }
626  
627         /* Emit our "changed" signal */
628         g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
629  
630         gl_debug (DEBUG_EDITOR, "END");
631 }
632
633
634
635 /*
636  * Local Variables:       -- emacs
637  * mode: C                -- emacs
638  * c-basic-offset: 8      -- emacs
639  * tab-width: 8           -- emacs
640  * indent-tabs-mode: nil  -- emacs
641  * End:                   -- emacs
642  */