* src/hildon-*.c * src/hildon-*.h: Ensure a consistent include order, include <gtk...
[hildon] / src / hildon-font-selection-dialog.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-font-selection-dialog
27  * @short_description: A widget used to allow users to select a font 
28  * with certain properties.
29  *
30  * Font selection can be made using this widget. Users can select font name, 
31  * size, style, etc. Users can also preview text in the selected font.
32  */
33
34 #ifdef                                          HAVE_CONFIG_H
35 #include                                        <config.h>
36 #endif
37
38 #include                                        <libintl.h>
39 #include                                        <stdlib.h>
40 #include                                        <string.h>
41
42 #include                                        <glib.h>
43 #include                                        <gdk/gdkkeysyms.h>
44
45 #include                                        "hildon-font-selection-dialog.h"
46 #include                                        "hildon-caption.h"
47 #include                                        "hildon-color-button.h"
48 #include                                        "hildon-font-selection-dialog-private.h"
49
50 /* These are what we use as the standard font sizes, for the size list */
51
52 static const guint16                            font_sizes[] = 
53 {
54   6, 8, 10, 12, 16, 24, 32
55 };
56
57 enum
58 {
59     PROP_0,
60     PROP_FAMILY,
61     PROP_FAMILY_SET,
62     PROP_SIZE,
63     PROP_SIZE_SET,
64     PROP_COLOR,
65     PROP_COLOR_SET,
66     PROP_BOLD,
67     PROP_BOLD_SET,
68     PROP_ITALIC,
69     PROP_ITALIC_SET,
70     PROP_UNDERLINE,
71     PROP_UNDERLINE_SET,
72     PROP_STRIKETHROUGH,
73     PROP_STRIKETHROUGH_SET,
74     PROP_POSITION,
75     PROP_POSITION_SET,
76     PROP_PREVIEW_TEXT,
77     PROP_FONT_SCALING
78 };
79
80 /* combo box active row indicator -2--inconsistent, -1--undefined 
81  * please make sure that you use settings_init settings_apply
82  * and settings_destroy, dont even try to touch this structure 
83  * without using the three above interface functions, of course
84  * if you know what you are doing, do as you please ;-)*/
85 typedef struct
86 {
87     HildonFontSelectionDialog *fsd;             /* pointer to our font selection dialog */
88
89     gint family;                                /* combo box indicator */
90     gint size;                                  /* combo box indicator */
91     GdkColor *color;                            /* free after read the setting */
92     gboolean color_inconsist;
93     gint weight;                                /* bit mask */
94     gint style;                                 /* bit mask */
95     gint underline;                             /* bit mask */
96     gint strikethrough;                         /* bit mask */
97     gint position;                              /* combo box indicator */
98
99 }                                               HildonFontSelectionDialogSettings;
100
101 static gboolean
102 hildon_font_selection_dialog_preview_key_press  (GtkWidget *widget,
103                                                  GdkEventKey *event,
104                                                  gpointer unused);
105
106 static int
107 cmp_families                                    (const void *a, 
108                                                  const void *b);
109
110 static void   
111 hildon_font_selection_dialog_show_preview       (HildonFontSelectionDialog *fontsel);
112
113 static PangoAttrList*
114 hildon_font_selection_dialog_create_attrlist    (HildonFontSelectionDialog *fontsel, 
115                                                  guint start_index,
116                                                  guint len);
117
118 static void   
119 hildon_font_selection_dialog_show_available_positionings (HildonFontSelectionDialogPrivate *priv);
120
121 static void   
122 hildon_font_selection_dialog_show_available_fonts (HildonFontSelectionDialog *fontsel);
123
124 static void   
125 hildon_font_selection_dialog_show_available_sizes (HildonFontSelectionDialogPrivate *priv);
126
127 static void   
128 hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass *klass);
129
130 static void   
131 hildon_font_selection_dialog_init               (HildonFontSelectionDialog *fontseldiag);
132
133 static void   
134 hildon_font_selection_dialog_finalize           (GObject *object);
135
136 static void   
137 hildon_font_selection_dialog_construct_notebook (HildonFontSelectionDialog *fontsel);
138
139 static void   
140 color_modified_cb                               (HildonColorButton *button,
141                                                  GParamSpec *pspec,
142                                                  gpointer fsd_priv);
143
144 static void   
145 add_preview_text_attr                           (PangoAttrList *list, 
146                                                  PangoAttribute *attr, 
147                                                  guint start, 
148                                                  guint len);
149
150 static void   
151 toggle_clicked                                  (GtkButton *button, 
152                                                  gpointer unused);
153
154 static GtkDialogClass*                          parent_class = NULL;
155
156 #define                                         _(String) dgettext("hildon-libs", String)
157
158 #define                                         SUPERSCRIPT_RISE 3333
159
160 #define                                         SUBSCRIPT_LOW -3333
161
162 #define                                         ON_BIT  0x01
163
164 #define                                         OFF_BIT 0x02
165
166 /**
167  * hildon_font_selection_dialog_get_type:
168  *
169  * Initializes and returns the type of a hildon font selection dialog
170  *
171  * @Returns: GType of #HildonFontSelectionDialog
172  */
173 GType G_GNUC_CONST
174 hildon_font_selection_dialog_get_type           (void)
175 {
176     static GType font_selection_dialog_type = 0;
177
178     if (! font_selection_dialog_type) {
179         static const GTypeInfo fontsel_diag_info = {
180             sizeof(HildonFontSelectionDialogClass),
181             NULL,       /* base_init */
182             NULL,       /* base_finalize */
183             (GClassInitFunc) hildon_font_selection_dialog_class_init,
184             NULL,       /* class_finalize */
185             NULL,       /* class_data */
186             sizeof(HildonFontSelectionDialog),
187             0,  /* n_preallocs */
188             (GInstanceInitFunc) hildon_font_selection_dialog_init,
189         };
190
191         font_selection_dialog_type =
192             g_type_register_static (GTK_TYPE_DIALOG,
193                     "HildonFontSelectionDialog",
194                     &fontsel_diag_info, 0);
195     }
196
197     return font_selection_dialog_type;
198 }
199
200 static void
201 hildon_font_selection_dialog_get_property       (GObject *object,
202                                                  guint prop_id,
203                                                  GValue *value,
204                                                  GParamSpec *pspec)
205 {
206     gint i;
207     GdkColor color;
208
209     HildonFontSelectionDialogPrivate *priv =
210         HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE(object);
211
212     g_assert (priv);
213
214     switch (prop_id)
215     {
216         case PROP_FAMILY:
217             i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_font_type));
218             if(i >= 0 && i < priv->n_families)
219                 g_value_set_string(value, 
220                         pango_font_family_get_name (priv->families[i]));
221             else
222                 g_value_set_string (value, "Sans");
223                 /* FIXME Bad hardcoding here */
224             break;
225
226         case PROP_FAMILY_SET:
227             i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_font_type));
228             if(i >= 0 && i < priv->n_families)
229                 g_value_set_boolean (value, TRUE);
230             else
231                 g_value_set_boolean (value, FALSE);
232             break;
233
234         case PROP_SIZE:
235             i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_font_size));
236             if(i >= 0 && i < G_N_ELEMENTS (font_sizes))
237                 g_value_set_int (value, font_sizes[i]);
238             else
239                 g_value_set_int (value, 16);
240             break;
241
242         case PROP_SIZE_SET:
243             i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_font_size));
244             if(i >= 0 && i < G_N_ELEMENTS (font_sizes))
245                 g_value_set_boolean (value, TRUE);
246             else
247                 g_value_set_boolean (value, FALSE);
248             break;
249
250         case PROP_COLOR:
251             hildon_color_button_get_color
252                 (HILDON_COLOR_BUTTON (priv->font_color_button), &color);
253             g_value_set_boxed (value, (gconstpointer) &color);
254             break;
255
256         case PROP_COLOR_SET:
257             g_value_set_boolean (value, priv->color_set);
258             break;
259
260         case PROP_BOLD:
261             g_value_set_boolean (value, 
262                     gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->chk_bold)));
263             break;
264
265         case PROP_BOLD_SET:
266             g_value_set_boolean (value,
267                     ! gtk_toggle_button_get_inconsistent
268                     (GTK_TOGGLE_BUTTON (priv->chk_bold)));
269             break;
270
271         case PROP_ITALIC:
272             g_value_set_boolean (value, 
273                     gtk_toggle_button_get_active
274                     (GTK_TOGGLE_BUTTON (priv->chk_italic)));
275             break;
276
277         case PROP_ITALIC_SET:
278             g_value_set_boolean (value,
279                     ! gtk_toggle_button_get_inconsistent
280                     (GTK_TOGGLE_BUTTON (priv->chk_italic)));
281             break;
282
283         case PROP_UNDERLINE:
284             g_value_set_boolean (value, 
285                     gtk_toggle_button_get_active
286                     (GTK_TOGGLE_BUTTON (priv->chk_underline)));
287             break;
288
289         case PROP_UNDERLINE_SET:
290             g_value_set_boolean (value,
291                     ! gtk_toggle_button_get_inconsistent
292                     (GTK_TOGGLE_BUTTON (priv->chk_underline)));
293             break;
294
295         case PROP_STRIKETHROUGH:
296             g_value_set_boolean(value, 
297                     gtk_toggle_button_get_active
298                     (GTK_TOGGLE_BUTTON (priv->chk_strikethrough)));
299             break;
300
301         case PROP_STRIKETHROUGH_SET:
302             g_value_set_boolean(value,
303                     ! gtk_toggle_button_get_inconsistent
304                     (GTK_TOGGLE_BUTTON (priv->chk_strikethrough)));
305             break;
306
307         case PROP_POSITION:
308             i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_positioning));
309             if(i == 1) /* super */
310                 g_value_set_int (value, 1);
311             else if(i == 2)/* sub */
312                 g_value_set_int (value, -1);
313             else
314                 g_value_set_int (value, 0);
315             break;
316
317         case PROP_FONT_SCALING:
318             g_value_set_double (value, priv->font_scaling);
319             break;
320
321         case PROP_POSITION_SET:
322             i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_positioning));
323             if(i >= 0 && i < 3)
324                 g_value_set_boolean (value, TRUE);
325             else
326                 g_value_set_boolean (value, FALSE);
327             break;
328
329         case PROP_PREVIEW_TEXT:
330             g_value_set_string (value,
331                     hildon_font_selection_dialog_get_preview_text (HILDON_FONT_SELECTION_DIALOG (object)));
332             break;
333
334         default:
335             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336             break;
337     }
338 }
339
340 static void 
341 hildon_font_selection_dialog_set_property       (GObject *object,
342                                                  guint prop_id,
343                                                  const GValue *value,
344                                                  GParamSpec *pspec)
345 {
346     gint i, size;
347     const gchar *str;
348     gboolean b;
349     GdkColor *color = NULL;
350     GdkColor black;
351
352     HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (object);
353     g_assert (priv);
354
355     black.red = black.green = black.blue = 0;
356
357     switch (prop_id)
358     {
359         case PROP_FAMILY:
360             str = g_value_get_string (value);
361             for(i = 0; i < priv->n_families; i++)
362             {
363                 if (strcmp (str, pango_font_family_get_name (priv->families[i]))
364                         == 0)
365                 {
366                     gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_type), i);
367                     break;
368                 }
369             }
370             break;
371
372         case PROP_FAMILY_SET:
373             b = g_value_get_boolean (value);
374             if(!b)
375                 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_type), -1);
376             break;
377
378         case PROP_SIZE:
379             size = g_value_get_int (value);
380             for(i = 0; i < G_N_ELEMENTS (font_sizes); i++)
381             {
382                 if(size == font_sizes[i])
383                 {
384                     gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_size), i);
385                     break;
386                 }
387             }
388             break;
389
390         case PROP_SIZE_SET:
391             b = g_value_get_boolean (value);
392             if(!b)
393                 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_size), -1);
394             break;
395
396         case PROP_COLOR:
397             color = (GdkColor *) g_value_get_boxed (value);
398             if(color != NULL)
399                 hildon_color_button_set_color (HILDON_COLOR_BUTTON
400                         (priv->font_color_button),
401                         color);
402             else
403                 hildon_color_button_set_color (HILDON_COLOR_BUTTON
404                         (priv->font_color_button),
405                         &black);
406             break;
407
408         case PROP_COLOR_SET:
409             priv->color_set = g_value_get_boolean (value);
410             if(! priv->color_set)
411             {
412                 /* set color to black, but block our signal handler */
413                 g_signal_handler_block ((gpointer) priv->font_color_button,
414                         priv->color_modified_signal_handler);
415
416                 hildon_color_button_set_color (HILDON_COLOR_BUTTON
417                         (priv->font_color_button), 
418                         &black);
419
420                 g_signal_handler_unblock ((gpointer) priv->font_color_button,
421                         priv->color_modified_signal_handler);
422             }
423             break;
424
425         case PROP_BOLD:
426             /* this call will make sure that we dont get extra clicked signal */
427             gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON(priv->chk_bold), FALSE);
428             gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(priv->chk_bold), g_value_get_boolean (value));
429             break;
430
431         case PROP_BOLD_SET:
432             gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON (priv->chk_bold),! g_value_get_boolean(value));
433             break;
434
435         case PROP_ITALIC:
436             gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(priv->chk_italic),
437                     FALSE);
438             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->chk_italic),
439                     g_value_get_boolean(value));
440             break;
441
442         case PROP_ITALIC_SET:
443             gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(priv->chk_italic),
444                     !g_value_get_boolean(value));
445             break;
446
447         case PROP_UNDERLINE:
448             gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON
449                     (priv->chk_underline),
450                     FALSE);
451             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->chk_underline),
452                     g_value_get_boolean(value));
453             break;
454
455         case PROP_UNDERLINE_SET:
456             gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(priv->chk_underline),
457                     !g_value_get_boolean(value));
458             break;
459
460         case PROP_STRIKETHROUGH:
461             gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON
462                     (priv->chk_strikethrough),
463                     FALSE);
464             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->chk_strikethrough),
465                     g_value_get_boolean(value));
466             break;
467
468         case PROP_STRIKETHROUGH_SET:
469             gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON
470                     (priv->chk_strikethrough),
471                     !g_value_get_boolean(value));
472             break;
473
474         case PROP_POSITION:
475             i = g_value_get_int(value);
476             if( i == 1 )
477                 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), 1);
478             else if(i == -1)
479                 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), 2);
480             else
481                 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), 0);
482             break;
483
484         case PROP_FONT_SCALING:
485             priv->font_scaling = g_value_get_double(value);
486             break;
487
488         case PROP_POSITION_SET:
489             b = g_value_get_boolean(value);
490             if(!b)
491                 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), -1);
492             break;
493
494         case PROP_PREVIEW_TEXT:
495             hildon_font_selection_dialog_set_preview_text(
496                     HILDON_FONT_SELECTION_DIALOG(object),
497                     g_value_get_string(value));
498             break;
499
500         default:
501             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
502             break;
503     }
504 }
505
506 static void
507 hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass *klass)
508 {
509     GObjectClass *gobject_class;
510
511     parent_class = g_type_class_peek_parent (klass);
512     gobject_class = G_OBJECT_CLASS (klass);
513
514     gobject_class->finalize         = hildon_font_selection_dialog_finalize;
515     gobject_class->get_property     = hildon_font_selection_dialog_get_property;
516     gobject_class->set_property     = hildon_font_selection_dialog_set_property;
517
518     /* Install properties to the class */
519
520     /**
521      * HildonFontSelectionDialog:family:
522      *
523      * Font family used.
524      */
525     g_object_class_install_property (gobject_class, PROP_FAMILY,
526             g_param_spec_string ("family",
527                 "Font family", "String defines"
528                 " the font family", "Sans",
529                 G_PARAM_READWRITE));
530
531     /**
532      * HildonFontSelectionDialog:family-set:
533      *
534      * Is font family set or inconsistent.
535      */
536     g_object_class_install_property (gobject_class, PROP_FAMILY_SET,
537             g_param_spec_boolean ("family-set",
538                 "family inconsistent state",
539                 "Whether the family property"
540                 " is inconsistent", FALSE,
541                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
542
543     /**
544      * HildonFontSelectionDialog:size:
545      *
546      * Font size.
547      */
548     g_object_class_install_property (gobject_class, PROP_SIZE,
549             g_param_spec_int ("size",
550                 "Font size",
551                 "Font size in Pt",
552                 6, 32, 16,
553                 G_PARAM_READWRITE));
554
555     /**
556      * HildonFontSelectionDialog:size-set:
557      *
558      * Is font size set or inconsistent.
559      */
560     g_object_class_install_property (gobject_class, PROP_SIZE_SET,
561             g_param_spec_boolean ("size-set",
562                 "size inconsistent state",
563                 "Whether the size property"
564                 " is inconsistent", FALSE,
565                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
566
567     /**
568      * HildonFontSelectionDialog:color:
569      *
570      * GdkColor for the text.
571      */
572     g_object_class_install_property (gobject_class, PROP_COLOR,
573             g_param_spec_boxed ("color",
574                 "text color",
575                 "gdk color for the text",
576                 GDK_TYPE_COLOR,
577                 G_PARAM_READWRITE));
578
579     /**
580      * HildonFontSelectionDialog:color-set:
581      *
582      * Is font color set or inconsistent.
583      */
584     g_object_class_install_property (gobject_class, PROP_COLOR_SET,
585             g_param_spec_boolean ("color-set",
586                 "color inconsistent state",
587                 "Whether the color property"
588                 " is inconsistent", FALSE,
589                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
590
591     /**
592      * HildonFontSelectionDialog:color-set:
593      *
594      * Is font set as bold.
595      */
596     g_object_class_install_property (gobject_class, PROP_BOLD,
597             g_param_spec_boolean ("bold",
598                 "text weight",
599                 "Whether the text is bold",
600                 FALSE,
601                 G_PARAM_READWRITE));
602
603     /**
604      * HildonFontSelectionDialog:color-set:
605      *
606      * Is font bold status set or inconsistent.
607      */
608     g_object_class_install_property (gobject_class, PROP_BOLD_SET,
609             g_param_spec_boolean ("bold-set",
610                 "bold inconsistent state",
611                 "Whether the bold"
612                 " is inconsistent", FALSE,
613                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
614
615     /**
616      * HildonFontSelectionDialog:italic:
617      *
618      * Is font set as italic.
619      */
620     g_object_class_install_property (gobject_class, PROP_ITALIC,
621             g_param_spec_boolean ("italic",
622                 "text style",
623                 "Whether the text is italic",
624                 FALSE,
625                 G_PARAM_READWRITE));
626
627     /**
628      * HildonFontSelectionDialog:italic-set:
629      *
630      * Is font italic status set or inconsistent.
631      */
632     g_object_class_install_property (gobject_class, PROP_ITALIC_SET,
633             g_param_spec_boolean ("italic-set",
634                 "italic inconsistent state",
635                 "Whether the italic"
636                 " is inconsistent", FALSE,
637                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
638
639     /**
640      * HildonFontSelectionDialog:underline:
641      *
642      * Is the font underlined.
643      */
644     g_object_class_install_property (gobject_class, PROP_UNDERLINE,
645             g_param_spec_boolean ("underline",
646                 "text underline",
647                 "Whether the text is underlined",
648                 FALSE,
649                 G_PARAM_READWRITE));
650
651     /**
652      * HildonFontSelectionDialog:underline:
653      *
654      * Is font underline status set or inconsistent.
655      */
656     g_object_class_install_property (gobject_class, PROP_UNDERLINE_SET,
657             g_param_spec_boolean ("underline-set",
658                 "underline inconsistent state",
659                 "Whether the underline"
660                 " is inconsistent", FALSE,
661                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
662
663     /**
664      * HildonFontSelectionDialog:strikethrough:
665      *
666      * Is the font striken-through.
667      */
668     g_object_class_install_property (gobject_class, PROP_STRIKETHROUGH,
669             g_param_spec_boolean ("strikethrough",
670                 "strikethroughed text",
671                 "Whether the text is strikethroughed",
672                 FALSE,
673                 G_PARAM_READWRITE));
674
675     /**
676      * HildonFontSelectionDialog:strikethrough-set:
677      *
678      * Is the font strikenthrough status set.
679      */
680     g_object_class_install_property (gobject_class, PROP_STRIKETHROUGH_SET,
681             g_param_spec_boolean ("strikethrough-set",
682                 "strikethrough inconsistent state",
683                 "Whether the strikethrough"
684                 " is inconsistent", FALSE,
685                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
686
687     /**
688      * HildonFontSelectionDialog:position:
689      *
690      * The font positioning versus baseline.
691      */
692     g_object_class_install_property (gobject_class, PROP_POSITION,
693             g_param_spec_int ("position",
694                 "Font position",
695                 "Font position super or subscript",
696                 -1, 1, 0,
697                 G_PARAM_READWRITE));
698
699     /**
700      * HildonFontSelectionDialog:position-set:
701      *
702      * Is the font positioning set.
703      */
704     g_object_class_install_property (gobject_class, PROP_POSITION_SET,
705             g_param_spec_boolean ("position-set",
706                 "position inconsistent state",
707                 "Whether the position"
708                 " is inconsistent", FALSE,
709                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
710
711     /**
712      * HildonFontSelectionDialog:font-scaling:
713      *
714      * The font scaling factor applied to the preview dialog.
715      */
716     g_object_class_install_property (gobject_class, PROP_FONT_SCALING,
717             g_param_spec_double ("font-scaling",
718                 "Font scaling",
719                 "Font scaling for the preview dialog",
720                 0, 10, 1,
721                 G_PARAM_READWRITE));
722
723     /**
724      * HildonFontSelectionDialog:preview-text:
725      *
726      * The text used for the preview dialog.
727      */
728     g_object_class_install_property (gobject_class, PROP_PREVIEW_TEXT,
729             g_param_spec_string("preview-text",
730                 "Preview Text", 
731                 "the text in preview dialog, which does" 
732                 "not include the reference text",
733                 "",
734                 G_PARAM_READWRITE));
735
736     g_type_class_add_private (klass, 
737             sizeof (struct _HildonFontSelectionDialogPrivate));
738 }
739
740 static void 
741 hildon_font_selection_dialog_init               (HildonFontSelectionDialog *fontseldiag)
742 {
743     HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontseldiag);
744     GtkWidget *preview_button;
745
746     g_assert (priv);
747     priv->notebook = GTK_NOTEBOOK (gtk_notebook_new ());
748
749     hildon_font_selection_dialog_construct_notebook (fontseldiag);
750
751     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (fontseldiag)->vbox),
752             GTK_WIDGET (priv->notebook), TRUE, TRUE, 0);
753
754     /* Add dialog buttons */
755     gtk_dialog_add_button (GTK_DIALOG (fontseldiag),
756             _("ecdg_bd_font_dialog_ok"),
757             GTK_RESPONSE_OK);
758
759     preview_button = gtk_button_new_with_label (_("ecdg_bd_font_dialog_preview"));
760     gtk_box_pack_start (GTK_BOX(GTK_DIALOG (fontseldiag)->action_area), 
761             preview_button, FALSE, TRUE, 0);
762
763     g_signal_connect_swapped (preview_button, "clicked",
764             G_CALLBACK
765             (hildon_font_selection_dialog_show_preview),
766             fontseldiag);
767     gtk_widget_show(preview_button);
768
769     gtk_dialog_add_button (GTK_DIALOG (fontseldiag),
770             _("ecdg_bd_font_dialog_cancel"),
771             GTK_RESPONSE_CANCEL);
772
773     /*Set default preview text*/
774     priv->preview_text = g_strdup (_("ecdg_fi_preview_font_preview_text"));
775
776     gtk_window_set_title (GTK_WINDOW (fontseldiag), _("ecdg_ti_font"));
777     /*here is the line to make sure that notebook has the default focus*/
778     gtk_container_set_focus_child (GTK_CONTAINER (GTK_DIALOG (fontseldiag)->vbox),
779             GTK_WIDGET (priv->notebook));
780 }
781
782 static void 
783 hildon_font_selection_dialog_construct_notebook (HildonFontSelectionDialog *fontsel)
784 {
785     gint i;
786     GtkWidget *vbox_tab[3];
787     GtkWidget *font_color_box;
788     GtkWidget *caption_control;
789     GtkSizeGroup *group;
790
791     HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
792     g_assert (priv);
793
794     for (i = 0; i < 3; i++)
795         vbox_tab[i] = gtk_vbox_new (TRUE, 0);
796
797     group = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL));
798
799     /* Build the first page of the GtkNotebook: font style */
800     priv->cbx_font_type = gtk_combo_box_new_text ();
801     hildon_font_selection_dialog_show_available_fonts (fontsel);
802     caption_control = hildon_caption_new (group,
803             _("ecdg_fi_font_font"),
804             priv->cbx_font_type,
805             NULL,
806             HILDON_CAPTION_OPTIONAL);
807
808     gtk_box_pack_start (GTK_BOX (vbox_tab[0]), caption_control, FALSE, FALSE, 0);
809
810     priv->cbx_font_size = gtk_combo_box_new_text ();
811     hildon_font_selection_dialog_show_available_sizes (priv);
812     caption_control = hildon_caption_new (group,
813             _("ecdg_fi_font_size"),
814             priv->cbx_font_size,
815             NULL,
816             HILDON_CAPTION_OPTIONAL);
817
818     gtk_box_pack_start (GTK_BOX (vbox_tab[0]), caption_control, FALSE, FALSE, 0);
819
820     font_color_box = gtk_hbox_new (FALSE, 0);
821     priv->font_color_button = hildon_color_button_new ();
822     priv->color_set = FALSE;
823     priv->font_scaling = 1.0;
824     priv->color_modified_signal_handler = 
825         g_signal_connect (G_OBJECT (priv->font_color_button), "notify::color",
826                 G_CALLBACK (color_modified_cb), (gpointer) priv);
827
828     gtk_box_pack_start (GTK_BOX (font_color_box), priv->font_color_button, FALSE, FALSE, 0);
829
830     caption_control =
831         hildon_caption_new (group, _("ecdg_fi_font_colour_selector"),
832                 font_color_box,
833                 NULL, HILDON_CAPTION_OPTIONAL);
834
835     gtk_box_pack_start (GTK_BOX (vbox_tab[0]), caption_control, FALSE, FALSE, 0);
836
837     /* Build the second page of the GtkNotebook: font formatting */ 
838     priv->chk_bold = gtk_check_button_new ();
839     caption_control = hildon_caption_new (group,
840             _("ecdg_fi_font_bold"),
841             priv->chk_bold,
842             NULL,
843             HILDON_CAPTION_OPTIONAL);
844
845     gtk_box_pack_start (GTK_BOX (vbox_tab[1]), caption_control, FALSE, FALSE, 0);
846     g_signal_connect (G_OBJECT (priv->chk_bold), "clicked", 
847             G_CALLBACK(toggle_clicked), NULL);
848
849     priv->chk_italic = gtk_check_button_new ();
850     caption_control = hildon_caption_new (group, _("ecdg_fi_font_italic"),
851                 priv->chk_italic,
852                 NULL, HILDON_CAPTION_OPTIONAL);
853
854     gtk_box_pack_start (GTK_BOX (vbox_tab[1]), caption_control, FALSE, FALSE, 0);
855     g_signal_connect(G_OBJECT(priv->chk_italic), "clicked", 
856             G_CALLBACK(toggle_clicked), NULL);
857
858     priv->chk_underline = gtk_check_button_new();
859     caption_control =
860         hildon_caption_new (group, _("ecdg_fi_font_underline"),
861                 priv->chk_underline, NULL,
862                 HILDON_CAPTION_OPTIONAL);
863
864     gtk_box_pack_start (GTK_BOX (vbox_tab[1]), caption_control, FALSE, FALSE, 0);
865     g_signal_connect (G_OBJECT(priv->chk_underline), "clicked", 
866             G_CALLBACK (toggle_clicked), NULL);
867
868     /* Build the third page of the GtkNotebook: other font properties */
869     priv->chk_strikethrough = gtk_check_button_new ();
870     caption_control = hildon_caption_new(group, _("ecdg_fi_font_strikethrough"),
871                 priv->chk_strikethrough, NULL,
872                 HILDON_CAPTION_OPTIONAL);
873
874     gtk_box_pack_start (GTK_BOX (vbox_tab[2]), caption_control, FALSE, FALSE, 0);
875     g_signal_connect (G_OBJECT(priv->chk_strikethrough), "clicked", 
876             G_CALLBACK (toggle_clicked), NULL);
877
878     priv->cbx_positioning = gtk_combo_box_new_text ();
879     hildon_font_selection_dialog_show_available_positionings (priv);
880     caption_control =
881         hildon_caption_new(group, _("ecdg_fi_font_special"),
882                 priv->cbx_positioning, NULL,
883                 HILDON_CAPTION_OPTIONAL);
884
885     g_object_unref (group);
886
887     gtk_box_pack_start (GTK_BOX (vbox_tab[2]), caption_control, FALSE, FALSE, 0);
888
889     /* Populate notebook */
890     gtk_notebook_insert_page (priv->notebook, vbox_tab[0], NULL, 0);
891     gtk_notebook_insert_page (priv->notebook, vbox_tab[1], NULL, 1);
892     gtk_notebook_insert_page (priv->notebook, vbox_tab[2], NULL, 2);
893
894     gtk_notebook_set_tab_label_text (priv->notebook, vbox_tab[0],
895             _("ecdg_ti_font_dialog_style"));
896     gtk_notebook_set_tab_label_text (priv->notebook, vbox_tab[1],
897             _("ecdg_ti_font_dialog_format"));
898     gtk_notebook_set_tab_label_text (priv->notebook, vbox_tab[2],
899             _("ecdg_ti_font_dialog_other"));
900
901     gtk_widget_show_all (GTK_WIDGET (priv->notebook));
902 }
903
904 static void 
905 color_modified_cb                               (HildonColorButton *button, 
906                                                  GParamSpec *pspec, 
907                                                  gpointer fsd_priv)
908 {
909     HildonFontSelectionDialogPrivate *priv = (HildonFontSelectionDialogPrivate *) fsd_priv;
910     g_assert (priv);
911
912     priv->color_set = TRUE;
913 }
914
915 static void 
916 hildon_font_selection_dialog_finalize           (GObject *object)
917 {
918     HildonFontSelectionDialogPrivate *priv;
919     HildonFontSelectionDialog *fontsel;
920
921     g_assert (HILDON_IS_FONT_SELECTION_DIALOG (object));
922     fontsel = HILDON_FONT_SELECTION_DIALOG (object);
923
924     priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
925     g_assert (priv);
926
927     if (priv->preview_text != NULL) {
928         g_free (priv->preview_text);
929         priv->preview_text = NULL;
930     }
931
932     if (priv->families != NULL) {
933         g_free (priv->families);
934         priv->families = NULL;
935     }
936
937     if (G_OBJECT_CLASS (parent_class)->finalize)
938         G_OBJECT_CLASS (parent_class)->finalize (object);
939 }
940
941 static int 
942 cmp_families                                    (const void *a, 
943                                                  const void *b)
944 {
945     const char *a_name =
946         pango_font_family_get_name (* (PangoFontFamily **) a);
947
948     const char *b_name =
949         pango_font_family_get_name (* (PangoFontFamily **) b);
950
951     return g_utf8_collate (a_name, b_name);
952 }
953
954 /* Exits the preview dialog with GTK_RESPONSE_CANCEL if Esc key
955  * was pressed 
956  * FIXME This should be handled automatically */
957 static gboolean
958 hildon_font_selection_dialog_preview_key_press  (GtkWidget *widget,
959                                                  GdkEventKey *event,
960                                                  gpointer unused)
961 {
962     g_assert (widget);
963     g_assert (event);
964
965     if (event->keyval == GDK_Escape)
966     {
967         gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_CANCEL);
968         return TRUE;
969     }
970
971     return FALSE;
972 }
973
974 static void
975 add_preview_text_attr                           (PangoAttrList *list, 
976                                                  PangoAttribute *attr, 
977                                                  guint start, 
978                                                  guint len)
979 {
980     attr->start_index = start;
981     attr->end_index = start + len;
982     pango_attr_list_insert (list, attr);
983 }
984
985 static PangoAttrList*
986 hildon_font_selection_dialog_create_attrlist    (HildonFontSelectionDialog *fontsel, 
987                                                  guint start_index, 
988                                                  guint len)
989 {
990     PangoAttrList *list;
991     PangoAttribute *attr;
992     gint size, position;
993     gboolean family_set, size_set, color_set, bold, bold_set,
994              italic, italic_set, underline, underline_set,
995              strikethrough, strikethrough_set, position_set;
996     GdkColor *color = NULL;
997     gchar *family = NULL;
998     gdouble font_scaling = 1.0;
999
1000     list = pango_attr_list_new ();
1001
1002     g_object_get (G_OBJECT (fontsel),
1003             "family", &family, "family-set", &family_set,
1004             "size", &size, "size-set", &size_set,
1005             "color", &color, "color-set", &color_set,
1006             "bold", &bold, "bold-set", &bold_set,
1007             "italic", &italic, "italic-set", &italic_set,
1008             "underline", &underline, "underline-set", &underline_set,
1009             "strikethrough", &strikethrough, "strikethrough-set", 
1010             &strikethrough_set, "position", &position, 
1011             "position-set", &position_set, 
1012             "font-scaling", &font_scaling,
1013             NULL);
1014
1015     /* family */
1016     if (family_set)
1017     {
1018         attr = pango_attr_family_new (family);
1019         add_preview_text_attr (list, attr, start_index, len);
1020     }
1021     g_free (family);
1022
1023     /* size */
1024     if (size_set)
1025     {
1026         attr = pango_attr_size_new (size * PANGO_SCALE);
1027         add_preview_text_attr (list, attr, start_index, len);
1028     }
1029
1030     /*color*/
1031     if (color_set)
1032     {
1033         attr = pango_attr_foreground_new (color->red, color->green, color->blue);
1034         add_preview_text_attr (list, attr, start_index, len);
1035     }
1036
1037     if (color != NULL)
1038         gdk_color_free (color);
1039
1040     /*weight*/
1041     if (bold_set)
1042     {
1043         if (bold)
1044             attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
1045         else
1046             attr = pango_attr_weight_new (PANGO_WEIGHT_NORMAL);
1047
1048         add_preview_text_attr(list, attr, start_index, len);
1049     }
1050
1051     /* style */
1052     if (italic_set)
1053     {
1054         if (italic)
1055             attr = pango_attr_style_new (PANGO_STYLE_ITALIC);
1056         else
1057             attr = pango_attr_style_new (PANGO_STYLE_NORMAL);
1058
1059         add_preview_text_attr(list, attr, start_index, len);
1060     }
1061
1062     /* underline */
1063     if (underline_set)
1064     {
1065         if (underline)
1066             attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
1067         else
1068             attr = pango_attr_underline_new (PANGO_UNDERLINE_NONE);
1069
1070         add_preview_text_attr(list, attr, start_index, len);
1071     }
1072
1073     /* strikethrough */
1074     if (strikethrough_set)
1075     {
1076         if (strikethrough)
1077             attr = pango_attr_strikethrough_new (TRUE);
1078         else
1079             attr = pango_attr_strikethrough_new (FALSE);
1080
1081         add_preview_text_attr(list, attr, start_index, len);
1082     }
1083
1084     /* position */
1085     if (position_set)
1086     {
1087         switch (position)
1088         {
1089             case 1: /*super*/
1090                 attr = pango_attr_rise_new (SUPERSCRIPT_RISE);
1091                 break;
1092             case -1: /*sub*/
1093                 attr = pango_attr_rise_new (SUBSCRIPT_LOW);
1094                 break;
1095             default: /*normal*/
1096                 attr = pango_attr_rise_new (0);
1097                 break;
1098         }
1099
1100         add_preview_text_attr (list, attr, start_index, len);
1101     }
1102
1103     /* font scaling for preview */
1104     if (font_scaling)
1105     {
1106         attr = pango_attr_scale_new(font_scaling);
1107         add_preview_text_attr(list, attr, 0, len + start_index);
1108     }
1109
1110     return list;
1111 }
1112
1113 static void
1114 hildon_font_selection_dialog_show_preview       (HildonFontSelectionDialog *fontsel)
1115 {
1116     HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
1117     gint size;
1118     gboolean family_set, size_set;
1119     PangoAttribute *attr;
1120     PangoAttrList *list;
1121     GtkWidget *preview_dialog;
1122     GtkWidget *preview_label;
1123     gchar *str = NULL;
1124     gboolean position_set = FALSE;
1125     gint position = 0;
1126     gboolean show_ref = FALSE;
1127
1128     g_assert (priv);
1129
1130     g_object_get (G_OBJECT (fontsel), "position-set", &position_set, NULL);
1131
1132     if (position_set) {
1133         g_object_get (G_OBJECT (fontsel), "position", &position, NULL);
1134         if (position == 1 || position == -1)
1135             show_ref = TRUE;
1136     }
1137
1138     /* preview dialog init */
1139     preview_dialog =
1140         gtk_dialog_new_with_buttons (_("ecdg_ti_preview_font"), NULL,
1141                 GTK_DIALOG_MODAL |
1142                 GTK_DIALOG_DESTROY_WITH_PARENT |
1143                 GTK_DIALOG_NO_SEPARATOR,
1144                 _("ecdg_bd_font_dialog_ok"),
1145                 GTK_RESPONSE_ACCEPT,
1146                 NULL);
1147
1148     str = (show_ref) ? g_strconcat (_("ecdg_fi_preview_font_preview_reference"), priv->preview_text, 0) :
1149         g_strdup (priv->preview_text);
1150
1151     preview_label = gtk_label_new (str);
1152     gtk_label_set_line_wrap (GTK_LABEL(preview_label), TRUE);
1153
1154     if (str) 
1155         g_free (str);
1156
1157     str = NULL;
1158
1159     /* set keypress handler (ESC hardkey) */
1160     g_signal_connect (G_OBJECT (preview_dialog), "key-press-event",
1161             G_CALLBACK(hildon_font_selection_dialog_preview_key_press),
1162             NULL);
1163
1164     /* Set the font */
1165     list = (show_ref) ? hildon_font_selection_dialog_create_attrlist (fontsel, 
1166             strlen (_("ecdg_fi_preview_font_preview_reference")),
1167             strlen (priv->preview_text)) :
1168         hildon_font_selection_dialog_create_attrlist (fontsel, 0, strlen(priv->preview_text));
1169
1170     g_object_get (G_OBJECT (fontsel), "family", &str, "family-set",
1171             &family_set, "size", &size, "size-set", &size_set,
1172             NULL);
1173
1174     /* A smallish hack to add scrollbar when font size is really big */
1175
1176     if (size_set && size > 24) {
1177         GtkScrolledWindow *scrolled = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
1178         gtk_scrolled_window_set_policy (scrolled, GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1179         gtk_scrolled_window_add_with_viewport (scrolled, GTK_WIDGET (preview_label));
1180         gtk_container_add (GTK_CONTAINER (GTK_DIALOG(preview_dialog)->vbox), GTK_WIDGET (scrolled));
1181         gtk_widget_set_size_request (GTK_WIDGET (scrolled), -1, 400);
1182     } else 
1183         gtk_container_add (GTK_CONTAINER (GTK_DIALOG(preview_dialog)->vbox), GTK_WIDGET (preview_label));
1184
1185     /* make reference text to have the same fontface and size */
1186     if (family_set)
1187     {
1188         attr = pango_attr_family_new (str);
1189         add_preview_text_attr (list, attr, 0, strlen (_("ecdg_fi_preview_font_preview_reference")));
1190     }
1191     if (str != NULL)
1192         g_free (str);
1193
1194     str = NULL;
1195
1196     /* size */
1197     if (size_set)
1198     {
1199         attr = pango_attr_size_new (size * PANGO_SCALE);
1200         add_preview_text_attr (list, attr, 0, strlen (_("ecdg_fi_preview_font_preview_reference")));
1201     }
1202
1203     gtk_label_set_attributes (GTK_LABEL (preview_label), list);
1204     pango_attr_list_unref (list);
1205
1206     /*And show the dialog*/
1207     gtk_window_set_transient_for (GTK_WINDOW (preview_dialog), 
1208             GTK_WINDOW (fontsel));
1209
1210     gtk_widget_show_all (preview_dialog);
1211     gtk_dialog_set_default_response (GTK_DIALOG (preview_dialog), GTK_RESPONSE_OK);
1212     
1213     GtkBox *action_area = (GtkBox *) GTK_DIALOG (preview_dialog)->action_area;
1214     GtkWidget *button = ((GtkBoxChild *) ((GSList *) action_area->children)->data)->widget;
1215     gtk_widget_grab_focus (button);
1216
1217     gtk_dialog_run (GTK_DIALOG (preview_dialog));
1218     gtk_widget_destroy (preview_dialog);
1219 }
1220
1221 static gboolean 
1222 is_internal_font                                (const gchar * name)
1223 {
1224     /* FIXME Extremally BAD BAD BAD way of doing things */
1225
1226     return strcmp(name, "DeviceSymbols") == 0
1227         || strcmp(name, "Nokia Smiley" ) == 0
1228         || strcmp(name, "NewCourier" ) == 0
1229         || strcmp(name, "NewTimes" ) == 0
1230         || strcmp(name, "SwissA" ) == 0
1231         || strcmp(name, "Nokia Sans"   ) == 0
1232         || strcmp(name, "Nokia Sans Cn") == 0;
1233 }
1234
1235 static void 
1236 filter_out_internal_fonts                       (PangoFontFamily **families, 
1237                                                  int *n_families)
1238 {
1239     int i;
1240     int n; /* counts valid fonts */
1241     const gchar * name = NULL;
1242
1243     for(i = 0, n = 0; i < * n_families; i++){
1244
1245         name = pango_font_family_get_name (families[i]);
1246
1247         if(!is_internal_font(name))
1248         {
1249
1250             if (i!=n){ /* there are filtered out families */
1251                 families[n] = families[i]; /* shift the current family */
1252             }
1253
1254             n++; /* count one more valid */
1255         }
1256     } /* foreach font family */
1257
1258     *n_families = n;  
1259 }
1260
1261 static void
1262 hildon_font_selection_dialog_show_available_fonts (HildonFontSelectionDialog *fontsel)
1263
1264 {
1265     gint i;
1266
1267     HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
1268     g_assert (priv);
1269
1270     pango_context_list_families (gtk_widget_get_pango_context
1271             (GTK_WIDGET (fontsel)), &priv->families,
1272             &priv->n_families);
1273
1274     filter_out_internal_fonts (priv->families, &priv->n_families);
1275
1276     qsort (priv->families, priv->n_families, sizeof(PangoFontFamily *), cmp_families);
1277
1278     for (i = 0; i < priv->n_families; i++) 
1279     {
1280         const gchar *name = pango_font_family_get_name (priv->families[i]);
1281         gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_font_type), name);
1282     }
1283 }
1284
1285 static void
1286 hildon_font_selection_dialog_show_available_positionings (HildonFontSelectionDialogPrivate *priv)
1287 {
1288     gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_positioning), _("ecdg_va_font_printpos_1"));
1289     gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_positioning), _("ecdg_va_font_printpos_2"));
1290     gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_positioning), _("ecdg_va_font_printpos_3"));
1291 }
1292
1293 /* Loads the sizes from a pre-allocated table */
1294 static void
1295 hildon_font_selection_dialog_show_available_sizes (HildonFontSelectionDialogPrivate *priv)
1296 {
1297     gchar *size_str;
1298     gint i;
1299
1300     g_assert (priv);
1301
1302     for (i = 0; i < G_N_ELEMENTS (font_sizes); i++) 
1303     {
1304         size_str = g_strdup_printf ("%i%s",
1305                 font_sizes[i],
1306                 _("ecdg_va_font_size_trailer"));
1307
1308         gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_font_size), size_str);
1309         g_free (size_str);
1310     }
1311 }
1312
1313 static void
1314 toggle_clicked                                  (GtkButton *button, 
1315                                                  gpointer unused)
1316 {
1317     GtkToggleButton *t_b = GTK_TOGGLE_BUTTON (button);
1318
1319     /* we have to remove the inconsistent state ourselves */
1320     if (gtk_toggle_button_get_inconsistent (t_b))
1321     {
1322         gtk_toggle_button_set_inconsistent (t_b, FALSE);
1323         gtk_toggle_button_set_active (t_b, FALSE);
1324     }
1325 }
1326
1327 /**
1328  * hildon_font_selection_dialog_new:
1329  * @parent: the parent window
1330  * @title: the title of font selection dialog
1331  *
1332  * If NULL is passed for title, then default title
1333  * "Font" will be used.
1334  *
1335  * Returns: a new #HildonFontSelectionDialog
1336  */
1337 GtkWidget*
1338 hildon_font_selection_dialog_new                (GtkWindow *parent,
1339                                                  const gchar *title)
1340 {
1341     HildonFontSelectionDialog *fontseldiag;
1342
1343     fontseldiag = g_object_new (HILDON_TYPE_FONT_SELECTION_DIALOG,
1344             "has-separator", FALSE, NULL);
1345
1346     if (title)
1347         gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1348
1349     if (parent)
1350         gtk_window_set_transient_for (GTK_WINDOW (fontseldiag), parent);
1351
1352     return GTK_WIDGET (fontseldiag);
1353 }
1354
1355 /**
1356  * hildon_font_selection_dialog_get_preview_text:
1357  * @fsd: the font selection dialog
1358  *
1359  * Gets the text in preview dialog, which does not include the 
1360  * reference text. The returned string must be freed by the user.
1361  *
1362  * Returns: a string pointer
1363  */
1364 gchar*
1365 hildon_font_selection_dialog_get_preview_text   (HildonFontSelectionDialog * fsd)
1366 {
1367     /* FIXME Return a const pointer? */
1368     HildonFontSelectionDialogPrivate *priv;
1369
1370     g_return_val_if_fail (HILDON_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1371         
1372     priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fsd);
1373     g_assert (priv);
1374
1375     return g_strdup (priv->preview_text);
1376 }
1377
1378 /**
1379  * hildon_font_selection_dialog_set_preview_text:
1380  * @fsd: the font selection dialog
1381  * @text: the text to be displayed in the preview dialog
1382  *
1383  * The default preview text is
1384  * "The quick brown fox jumped over the lazy dogs"
1385  */
1386 void
1387 hildon_font_selection_dialog_set_preview_text   (HildonFontSelectionDialog *fsd, 
1388                                                  const gchar * text)
1389 {
1390     HildonFontSelectionDialogPrivate *priv = NULL;
1391
1392     g_return_if_fail (HILDON_IS_FONT_SELECTION_DIALOG (fsd));
1393     g_return_if_fail (text);
1394
1395     priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fsd);
1396     g_assert (priv);
1397
1398     g_free (priv->preview_text);
1399     priv->preview_text = g_strdup (text);
1400     g_object_notify (G_OBJECT (fsd), "preview-text");
1401 }
1402