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