2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
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.
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.
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
26 * SECTION:hildon-font-selection-dialog
27 * @short_description: A widget used to allow users to select a font
28 * with certain properties.
30 * Font selection can be made using this widget. Users can select font name,
31 * size, style, etc. Since hildon 2.2, the previously available preview dialog
35 #undef HILDON_DISABLE_DEPRECATED
46 #include <gdk/gdkkeysyms.h>
48 #include "hildon-font-selection-dialog.h"
49 #include "hildon-caption.h"
50 #include "hildon-color-button.h"
51 #include "hildon-font-selection-dialog-private.h"
53 /* These are what we use as the standard font sizes, for the size list */
55 static const guint16 font_sizes[] =
57 6, 8, 10, 12, 16, 24, 32
76 PROP_STRIKETHROUGH_SET,
83 /* combo box active row indicator -2--inconsistent, -1--undefined
84 * please make sure that you use settings_init settings_apply
85 * and settings_destroy, dont even try to touch this structure
86 * without using the three above interface functions, of course
87 * if you know what you are doing, do as you please ;-)*/
90 HildonFontSelectionDialog *fsd; /* pointer to our font selection dialog */
92 gint family; /* combo box indicator */
93 gint size; /* combo box indicator */
94 GdkColor *color; /* free after read the setting */
95 gboolean color_inconsist;
96 gint weight; /* bit mask */
97 gint style; /* bit mask */
98 gint underline; /* bit mask */
99 gint strikethrough; /* bit mask */
100 gint position; /* combo box indicator */
102 } HildonFontSelectionDialogSettings;
106 hildon_font_selection_dialog_preview_key_press (GtkWidget *widget,
112 cmp_families (const void *a,
116 hildon_font_selection_dialog_show_preview (HildonFontSelectionDialog *fontsel);
118 static PangoAttrList*
119 hildon_font_selection_dialog_create_attrlist (HildonFontSelectionDialog *fontsel,
125 hildon_font_selection_dialog_show_available_positionings (HildonFontSelectionDialogPrivate *priv);
128 hildon_font_selection_dialog_show_available_fonts (HildonFontSelectionDialog *fontsel);
131 hildon_font_selection_dialog_show_available_sizes (HildonFontSelectionDialogPrivate *priv);
134 hildon_font_selection_dialog_class_init (HildonFontSelectionDialogClass *klass);
137 hildon_font_selection_dialog_init (HildonFontSelectionDialog *fontseldiag);
140 hildon_font_selection_dialog_finalize (GObject *object);
143 hildon_font_selection_dialog_construct_notebook (HildonFontSelectionDialog *fontsel);
146 color_modified_cb (HildonColorButton *button,
152 add_preview_text_attr (PangoAttrList *list,
153 PangoAttribute *attr,
159 toggle_clicked (GtkButton *button,
162 static GtkDialogClass* parent_class = NULL;
164 #define _(String) dgettext("hildon-libs", String)
166 #define SUPERSCRIPT_RISE 3333
168 #define SUBSCRIPT_LOW -3333
175 * hildon_font_selection_dialog_get_type:
177 * Initializes and returns the type of a hildon font selection dialog
179 * Returns: GType of #HildonFontSelectionDialog
182 hildon_font_selection_dialog_get_type (void)
184 static GType font_selection_dialog_type = 0;
186 if (! font_selection_dialog_type) {
187 static const GTypeInfo fontsel_diag_info = {
188 sizeof(HildonFontSelectionDialogClass),
189 NULL, /* base_init */
190 NULL, /* base_finalize */
191 (GClassInitFunc) hildon_font_selection_dialog_class_init,
192 NULL, /* class_finalize */
193 NULL, /* class_data */
194 sizeof(HildonFontSelectionDialog),
196 (GInstanceInitFunc) hildon_font_selection_dialog_init,
199 font_selection_dialog_type =
200 g_type_register_static (GTK_TYPE_DIALOG,
201 "HildonFontSelectionDialog",
202 &fontsel_diag_info, 0);
205 return font_selection_dialog_type;
209 hildon_font_selection_dialog_get_property (GObject *object,
217 HildonFontSelectionDialogPrivate *priv =
218 HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE(object);
225 i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_font_type));
226 if(i >= 0 && i < priv->n_families)
227 g_value_set_string(value,
228 pango_font_family_get_name (priv->families[i]));
230 g_value_set_string (value, "Sans");
231 /* FIXME Bad hardcoding here */
234 case PROP_FAMILY_SET:
235 i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_font_type));
236 if(i >= 0 && i < priv->n_families)
237 g_value_set_boolean (value, TRUE);
239 g_value_set_boolean (value, FALSE);
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_int (value, font_sizes[i]);
247 g_value_set_int (value, 16);
251 i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_font_size));
252 if(i >= 0 && i < G_N_ELEMENTS (font_sizes))
253 g_value_set_boolean (value, TRUE);
255 g_value_set_boolean (value, FALSE);
259 hildon_color_button_get_color
260 (HILDON_COLOR_BUTTON (priv->font_color_button), &color);
261 g_value_set_boxed (value, (gconstpointer) &color);
265 g_value_set_boolean (value, priv->color_set);
269 g_value_set_boolean (value,
270 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->chk_bold)));
274 g_value_set_boolean (value,
275 ! gtk_toggle_button_get_inconsistent
276 (GTK_TOGGLE_BUTTON (priv->chk_bold)));
280 g_value_set_boolean (value,
281 gtk_toggle_button_get_active
282 (GTK_TOGGLE_BUTTON (priv->chk_italic)));
285 case PROP_ITALIC_SET:
286 g_value_set_boolean (value,
287 ! gtk_toggle_button_get_inconsistent
288 (GTK_TOGGLE_BUTTON (priv->chk_italic)));
292 g_value_set_boolean (value,
293 gtk_toggle_button_get_active
294 (GTK_TOGGLE_BUTTON (priv->chk_underline)));
297 case PROP_UNDERLINE_SET:
298 g_value_set_boolean (value,
299 ! gtk_toggle_button_get_inconsistent
300 (GTK_TOGGLE_BUTTON (priv->chk_underline)));
303 case PROP_STRIKETHROUGH:
304 g_value_set_boolean(value,
305 gtk_toggle_button_get_active
306 (GTK_TOGGLE_BUTTON (priv->chk_strikethrough)));
309 case PROP_STRIKETHROUGH_SET:
310 g_value_set_boolean(value,
311 ! gtk_toggle_button_get_inconsistent
312 (GTK_TOGGLE_BUTTON (priv->chk_strikethrough)));
316 i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_positioning));
317 if(i == 1) /* super */
318 g_value_set_int (value, 1);
319 else if(i == 2)/* sub */
320 g_value_set_int (value, -1);
322 g_value_set_int (value, 0);
325 case PROP_FONT_SCALING:
326 g_value_set_double (value, priv->font_scaling);
329 case PROP_POSITION_SET:
330 i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_positioning));
332 g_value_set_boolean (value, TRUE);
334 g_value_set_boolean (value, FALSE);
337 case PROP_PREVIEW_TEXT:
338 g_value_set_string (value,
339 hildon_font_selection_dialog_get_preview_text (HILDON_FONT_SELECTION_DIALOG (object)));
343 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
349 hildon_font_selection_dialog_set_property (GObject *object,
357 GdkColor *color = NULL;
360 HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (object);
363 black.red = black.green = black.blue = 0;
368 family = g_value_get_string (value);
369 g_return_if_fail (family != NULL);
370 for(i = 0; i < priv->n_families; i++)
372 if (strcmp (family, pango_font_family_get_name (priv->families[i]))
375 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_type), i);
381 case PROP_FAMILY_SET:
382 b = g_value_get_boolean (value);
384 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_type), -1);
388 size = g_value_get_int (value);
389 for(i = 0; i < G_N_ELEMENTS (font_sizes); i++)
391 if(size == font_sizes[i])
393 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_size), i);
400 b = g_value_get_boolean (value);
402 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_size), -1);
406 color = (GdkColor *) g_value_get_boxed (value);
408 hildon_color_button_set_color (HILDON_COLOR_BUTTON
409 (priv->font_color_button),
412 hildon_color_button_set_color (HILDON_COLOR_BUTTON
413 (priv->font_color_button),
418 priv->color_set = g_value_get_boolean (value);
419 if(! priv->color_set)
421 /* set color to black, but block our signal handler */
422 g_signal_handler_block ((gpointer) priv->font_color_button,
423 priv->color_modified_signal_handler);
425 hildon_color_button_set_color (HILDON_COLOR_BUTTON
426 (priv->font_color_button),
429 g_signal_handler_unblock ((gpointer) priv->font_color_button,
430 priv->color_modified_signal_handler);
435 /* this call will make sure that we dont get extra clicked signal */
436 gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON(priv->chk_bold), FALSE);
437 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(priv->chk_bold), g_value_get_boolean (value));
441 gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON (priv->chk_bold),! g_value_get_boolean(value));
445 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(priv->chk_italic),
447 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->chk_italic),
448 g_value_get_boolean(value));
451 case PROP_ITALIC_SET:
452 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(priv->chk_italic),
453 !g_value_get_boolean(value));
457 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON
458 (priv->chk_underline),
460 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->chk_underline),
461 g_value_get_boolean(value));
464 case PROP_UNDERLINE_SET:
465 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(priv->chk_underline),
466 !g_value_get_boolean(value));
469 case PROP_STRIKETHROUGH:
470 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON
471 (priv->chk_strikethrough),
473 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->chk_strikethrough),
474 g_value_get_boolean(value));
477 case PROP_STRIKETHROUGH_SET:
478 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON
479 (priv->chk_strikethrough),
480 !g_value_get_boolean(value));
484 i = g_value_get_int(value);
486 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), 1);
488 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), 2);
490 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), 0);
493 case PROP_FONT_SCALING:
494 priv->font_scaling = g_value_get_double(value);
497 case PROP_POSITION_SET:
498 b = g_value_get_boolean(value);
500 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), -1);
503 case PROP_PREVIEW_TEXT:
504 hildon_font_selection_dialog_set_preview_text(
505 HILDON_FONT_SELECTION_DIALOG(object),
506 g_value_get_string(value));
510 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
516 hildon_font_selection_dialog_class_init (HildonFontSelectionDialogClass *klass)
518 GObjectClass *gobject_class;
520 parent_class = g_type_class_peek_parent (klass);
521 gobject_class = G_OBJECT_CLASS (klass);
523 gobject_class->finalize = hildon_font_selection_dialog_finalize;
524 gobject_class->get_property = hildon_font_selection_dialog_get_property;
525 gobject_class->set_property = hildon_font_selection_dialog_set_property;
527 /* Install properties to the class */
530 * HildonFontSelectionDialog:family:
534 g_object_class_install_property (gobject_class, PROP_FAMILY,
535 g_param_spec_string ("family",
536 "Font family", "String defines"
537 " the font family", "Sans",
541 * HildonFontSelectionDialog:family-set:
543 * Is font family set or inconsistent.
545 g_object_class_install_property (gobject_class, PROP_FAMILY_SET,
546 g_param_spec_boolean ("family-set",
547 "family inconsistent state",
548 "Whether the family property"
549 " is inconsistent", FALSE,
550 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
553 * HildonFontSelectionDialog:size:
557 g_object_class_install_property (gobject_class, PROP_SIZE,
558 g_param_spec_int ("size",
565 * HildonFontSelectionDialog:size-set:
567 * Is font size set or inconsistent.
569 g_object_class_install_property (gobject_class, PROP_SIZE_SET,
570 g_param_spec_boolean ("size-set",
571 "size inconsistent state",
572 "Whether the size property"
573 " is inconsistent", FALSE,
574 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
577 * HildonFontSelectionDialog:color:
579 * GdkColor for the text.
581 g_object_class_install_property (gobject_class, PROP_COLOR,
582 g_param_spec_boxed ("color",
584 "gdk color for the text",
589 * HildonFontSelectionDialog:color-set:
591 * Is font color set or inconsistent.
593 g_object_class_install_property (gobject_class, PROP_COLOR_SET,
594 g_param_spec_boolean ("color-set",
595 "color inconsistent state",
596 "Whether the color property"
597 " is inconsistent", FALSE,
598 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
601 * HildonFontSelectionDialog:color-set:
603 * Is font set as bold.
605 g_object_class_install_property (gobject_class, PROP_BOLD,
606 g_param_spec_boolean ("bold",
608 "Whether the text is bold",
613 * HildonFontSelectionDialog:color-set:
615 * Is font bold status set or inconsistent.
617 g_object_class_install_property (gobject_class, PROP_BOLD_SET,
618 g_param_spec_boolean ("bold-set",
619 "bold inconsistent state",
621 " is inconsistent", FALSE,
622 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
625 * HildonFontSelectionDialog:italic:
627 * Is font set as italic.
629 g_object_class_install_property (gobject_class, PROP_ITALIC,
630 g_param_spec_boolean ("italic",
632 "Whether the text is italic",
637 * HildonFontSelectionDialog:italic-set:
639 * Is font italic status set or inconsistent.
641 g_object_class_install_property (gobject_class, PROP_ITALIC_SET,
642 g_param_spec_boolean ("italic-set",
643 "italic inconsistent state",
645 " is inconsistent", FALSE,
646 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
649 * HildonFontSelectionDialog:underline:
651 * Is the font underlined.
653 g_object_class_install_property (gobject_class, PROP_UNDERLINE,
654 g_param_spec_boolean ("underline",
656 "Whether the text is underlined",
661 * HildonFontSelectionDialog:underline:
663 * Is font underline status set or inconsistent.
665 g_object_class_install_property (gobject_class, PROP_UNDERLINE_SET,
666 g_param_spec_boolean ("underline-set",
667 "underline inconsistent state",
668 "Whether the underline"
669 " is inconsistent", FALSE,
670 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
673 * HildonFontSelectionDialog:strikethrough:
675 * Is the font striken-through.
677 g_object_class_install_property (gobject_class, PROP_STRIKETHROUGH,
678 g_param_spec_boolean ("strikethrough",
679 "strikethroughed text",
680 "Whether the text is strikethroughed",
685 * HildonFontSelectionDialog:strikethrough-set:
687 * Is the font strikenthrough status set.
689 g_object_class_install_property (gobject_class, PROP_STRIKETHROUGH_SET,
690 g_param_spec_boolean ("strikethrough-set",
691 "strikethrough inconsistent state",
692 "Whether the strikethrough"
693 " is inconsistent", FALSE,
694 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
697 * HildonFontSelectionDialog:position:
699 * The font positioning versus baseline.
701 g_object_class_install_property (gobject_class, PROP_POSITION,
702 g_param_spec_int ("position",
704 "Font position super or subscript",
709 * HildonFontSelectionDialog:position-set:
711 * Is the font positioning set.
713 g_object_class_install_property (gobject_class, PROP_POSITION_SET,
714 g_param_spec_boolean ("position-set",
715 "position inconsistent state",
716 "Whether the position"
717 " is inconsistent", FALSE,
718 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
721 * HildonFontSelectionDialog:font-scaling:
723 * The font scaling factor applied to the preview dialog.
725 g_object_class_install_property (gobject_class, PROP_FONT_SCALING,
726 g_param_spec_double ("font-scaling",
728 "Font scaling for the preview dialog",
733 * HildonFontSelectionDialog:preview-text:
735 * The text used for the preview dialog.
737 * Deprecated: this property is unused since hildon 2.2
739 g_object_class_install_property (gobject_class, PROP_PREVIEW_TEXT,
740 g_param_spec_string("preview-text",
742 "the text in preview dialog, which does"
743 "not include the reference text",
747 g_type_class_add_private (klass,
748 sizeof (struct _HildonFontSelectionDialogPrivate));
752 hildon_font_selection_dialog_init (HildonFontSelectionDialog *fontseldiag)
754 HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontseldiag);
755 /* GtkWidget *preview_button; */
758 priv->notebook = GTK_NOTEBOOK (gtk_notebook_new ());
760 hildon_font_selection_dialog_construct_notebook (fontseldiag);
762 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (fontseldiag)->vbox),
763 GTK_WIDGET (priv->notebook), TRUE, TRUE, 0);
765 /* Add dialog buttons */
766 gtk_dialog_add_button (GTK_DIALOG (fontseldiag),
771 preview_button = gtk_button_new_with_label (_("ecdg_bd_font_dialog_preview"));
772 gtk_box_pack_start (GTK_BOX(GTK_DIALOG (fontseldiag)->action_area),
773 preview_button, FALSE, TRUE, 0);
775 g_signal_connect_swapped (preview_button, "clicked",
777 (hildon_font_selection_dialog_show_preview),
779 gtk_widget_show(preview_button);
782 /*Set default preview text*/
783 priv->preview_text = g_strdup (_("ecdg_fi_preview_font_preview_text"));
785 gtk_window_set_title (GTK_WINDOW (fontseldiag), _("ecdg_ti_font"));
786 /*here is the line to make sure that notebook has the default focus*/
787 gtk_container_set_focus_child (GTK_CONTAINER (GTK_DIALOG (fontseldiag)->vbox),
788 GTK_WIDGET (priv->notebook));
792 hildon_font_selection_dialog_construct_notebook (HildonFontSelectionDialog *fontsel)
795 GtkWidget *vbox_tab[3];
796 GtkWidget *font_color_box;
797 GtkWidget *caption_control;
800 HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
803 for (i = 0; i < 3; i++)
804 vbox_tab[i] = gtk_vbox_new (TRUE, 0);
806 group = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL));
808 /* Build the first page of the GtkNotebook: font style */
809 priv->cbx_font_type = gtk_combo_box_new_text ();
810 hildon_font_selection_dialog_show_available_fonts (fontsel);
811 caption_control = hildon_caption_new (group,
812 _("ecdg_fi_font_font"),
815 HILDON_CAPTION_OPTIONAL);
817 gtk_box_pack_start (GTK_BOX (vbox_tab[0]), caption_control, FALSE, FALSE, 0);
819 priv->cbx_font_size = gtk_combo_box_new_text ();
820 hildon_font_selection_dialog_show_available_sizes (priv);
821 caption_control = hildon_caption_new (group,
822 _("ecdg_fi_font_size"),
825 HILDON_CAPTION_OPTIONAL);
827 gtk_box_pack_start (GTK_BOX (vbox_tab[0]), caption_control, FALSE, FALSE, 0);
829 font_color_box = gtk_hbox_new (FALSE, 0);
830 priv->font_color_button = hildon_color_button_new ();
831 priv->color_set = FALSE;
832 priv->font_scaling = 1.0;
833 priv->color_modified_signal_handler =
834 g_signal_connect (G_OBJECT (priv->font_color_button), "notify::color",
835 G_CALLBACK (color_modified_cb), (gpointer) priv);
837 gtk_box_pack_start (GTK_BOX (font_color_box), priv->font_color_button, FALSE, FALSE, 0);
840 hildon_caption_new (group, _("ecdg_fi_font_colour_selector"),
842 NULL, HILDON_CAPTION_OPTIONAL);
844 gtk_box_pack_start (GTK_BOX (vbox_tab[0]), caption_control, FALSE, FALSE, 0);
846 /* Build the second page of the GtkNotebook: font formatting */
847 priv->chk_bold = gtk_check_button_new ();
848 caption_control = hildon_caption_new (group,
849 _("ecdg_fi_font_bold"),
852 HILDON_CAPTION_OPTIONAL);
854 gtk_box_pack_start (GTK_BOX (vbox_tab[1]), caption_control, FALSE, FALSE, 0);
855 g_signal_connect (G_OBJECT (priv->chk_bold), "clicked",
856 G_CALLBACK(toggle_clicked), NULL);
858 priv->chk_italic = gtk_check_button_new ();
859 caption_control = hildon_caption_new (group, _("ecdg_fi_font_italic"),
861 NULL, HILDON_CAPTION_OPTIONAL);
863 gtk_box_pack_start (GTK_BOX (vbox_tab[1]), caption_control, FALSE, FALSE, 0);
864 g_signal_connect(G_OBJECT(priv->chk_italic), "clicked",
865 G_CALLBACK(toggle_clicked), NULL);
867 priv->chk_underline = gtk_check_button_new();
869 hildon_caption_new (group, _("ecdg_fi_font_underline"),
870 priv->chk_underline, NULL,
871 HILDON_CAPTION_OPTIONAL);
873 gtk_box_pack_start (GTK_BOX (vbox_tab[1]), caption_control, FALSE, FALSE, 0);
874 g_signal_connect (G_OBJECT(priv->chk_underline), "clicked",
875 G_CALLBACK (toggle_clicked), NULL);
877 /* Build the third page of the GtkNotebook: other font properties */
878 priv->chk_strikethrough = gtk_check_button_new ();
879 caption_control = hildon_caption_new(group, _("ecdg_fi_font_strikethrough"),
880 priv->chk_strikethrough, NULL,
881 HILDON_CAPTION_OPTIONAL);
883 gtk_box_pack_start (GTK_BOX (vbox_tab[2]), caption_control, FALSE, FALSE, 0);
884 g_signal_connect (G_OBJECT(priv->chk_strikethrough), "clicked",
885 G_CALLBACK (toggle_clicked), NULL);
887 priv->cbx_positioning = gtk_combo_box_new_text ();
888 hildon_font_selection_dialog_show_available_positionings (priv);
890 hildon_caption_new(group, _("ecdg_fi_font_special"),
891 priv->cbx_positioning, NULL,
892 HILDON_CAPTION_OPTIONAL);
894 g_object_unref (group);
896 gtk_box_pack_start (GTK_BOX (vbox_tab[2]), caption_control, FALSE, FALSE, 0);
898 /* Populate notebook */
899 gtk_notebook_insert_page (priv->notebook, vbox_tab[0], NULL, 0);
900 gtk_notebook_insert_page (priv->notebook, vbox_tab[1], NULL, 1);
901 gtk_notebook_insert_page (priv->notebook, vbox_tab[2], NULL, 2);
903 gtk_notebook_set_tab_label_text (priv->notebook, vbox_tab[0],
904 _("ecdg_ti_font_dialog_style"));
905 gtk_notebook_set_tab_label_text (priv->notebook, vbox_tab[1],
906 _("ecdg_ti_font_dialog_format"));
907 gtk_notebook_set_tab_label_text (priv->notebook, vbox_tab[2],
908 _("ecdg_ti_font_dialog_other"));
910 gtk_widget_show_all (GTK_WIDGET (priv->notebook));
914 color_modified_cb (HildonColorButton *button,
918 HildonFontSelectionDialogPrivate *priv = (HildonFontSelectionDialogPrivate *) fsd_priv;
921 priv->color_set = TRUE;
925 hildon_font_selection_dialog_finalize (GObject *object)
927 HildonFontSelectionDialogPrivate *priv;
928 HildonFontSelectionDialog *fontsel;
930 g_assert (HILDON_IS_FONT_SELECTION_DIALOG (object));
931 fontsel = HILDON_FONT_SELECTION_DIALOG (object);
933 priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
936 if (priv->preview_text != NULL) {
937 g_free (priv->preview_text);
938 priv->preview_text = NULL;
941 if (priv->families != NULL) {
942 g_free (priv->families);
943 priv->families = NULL;
946 if (G_OBJECT_CLASS (parent_class)->finalize)
947 G_OBJECT_CLASS (parent_class)->finalize (object);
951 cmp_families (const void *a,
955 pango_font_family_get_name (* (PangoFontFamily **) a);
958 pango_font_family_get_name (* (PangoFontFamily **) b);
960 return g_utf8_collate (a_name, b_name);
964 /* Exits the preview dialog with GTK_RESPONSE_CANCEL if Esc key
966 * FIXME This should be handled automatically */
968 hildon_font_selection_dialog_preview_key_press (GtkWidget *widget,
975 if (event->keyval == GDK_Escape)
977 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_CANCEL);
985 add_preview_text_attr (PangoAttrList *list,
986 PangoAttribute *attr,
990 attr->start_index = start;
991 attr->end_index = start + len;
992 pango_attr_list_insert (list, attr);
995 static PangoAttrList*
996 hildon_font_selection_dialog_create_attrlist (HildonFontSelectionDialog *fontsel,
1000 PangoAttrList *list;
1001 PangoAttribute *attr;
1002 gint size, position;
1003 gboolean family_set, size_set, color_set, bold, bold_set,
1004 italic, italic_set, underline, underline_set,
1005 strikethrough, strikethrough_set, position_set;
1006 GdkColor *color = NULL;
1007 gchar *family = NULL;
1008 gdouble font_scaling = 1.0;
1010 list = pango_attr_list_new ();
1012 g_object_get (G_OBJECT (fontsel),
1013 "family", &family, "family-set", &family_set,
1014 "size", &size, "size-set", &size_set,
1015 "color", &color, "color-set", &color_set,
1016 "bold", &bold, "bold-set", &bold_set,
1017 "italic", &italic, "italic-set", &italic_set,
1018 "underline", &underline, "underline-set", &underline_set,
1019 "strikethrough", &strikethrough, "strikethrough-set",
1020 &strikethrough_set, "position", &position,
1021 "position-set", &position_set,
1022 "font-scaling", &font_scaling,
1028 attr = pango_attr_family_new (family);
1029 add_preview_text_attr (list, attr, start_index, len);
1036 attr = pango_attr_size_new (size * PANGO_SCALE);
1037 add_preview_text_attr (list, attr, start_index, len);
1043 attr = pango_attr_foreground_new (color->red, color->green, color->blue);
1044 add_preview_text_attr (list, attr, start_index, len);
1048 gdk_color_free (color);
1054 attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
1056 attr = pango_attr_weight_new (PANGO_WEIGHT_NORMAL);
1058 add_preview_text_attr(list, attr, start_index, len);
1065 attr = pango_attr_style_new (PANGO_STYLE_ITALIC);
1067 attr = pango_attr_style_new (PANGO_STYLE_NORMAL);
1069 add_preview_text_attr(list, attr, start_index, len);
1076 attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
1078 attr = pango_attr_underline_new (PANGO_UNDERLINE_NONE);
1080 add_preview_text_attr(list, attr, start_index, len);
1084 if (strikethrough_set)
1087 attr = pango_attr_strikethrough_new (TRUE);
1089 attr = pango_attr_strikethrough_new (FALSE);
1091 add_preview_text_attr(list, attr, start_index, len);
1100 attr = pango_attr_rise_new (SUPERSCRIPT_RISE);
1103 attr = pango_attr_rise_new (SUBSCRIPT_LOW);
1106 attr = pango_attr_rise_new (0);
1110 add_preview_text_attr (list, attr, start_index, len);
1113 /* font scaling for preview */
1116 attr = pango_attr_scale_new(font_scaling);
1117 add_preview_text_attr(list, attr, 0, len + start_index);
1124 hildon_font_selection_dialog_show_preview (HildonFontSelectionDialog *fontsel)
1126 HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
1128 gboolean family_set, size_set;
1129 PangoAttribute *attr;
1130 PangoAttrList *list;
1131 GtkWidget *preview_dialog;
1132 GtkWidget *preview_label;
1134 gboolean position_set = FALSE;
1136 gboolean show_ref = FALSE;
1140 g_object_get (G_OBJECT (fontsel), "position-set", &position_set, NULL);
1143 g_object_get (G_OBJECT (fontsel), "position", &position, NULL);
1144 if (position == 1 || position == -1)
1148 /* preview dialog init */
1150 gtk_dialog_new_with_buttons (_("ecdg_ti_preview_font"), NULL,
1152 GTK_DIALOG_DESTROY_WITH_PARENT |
1153 GTK_DIALOG_NO_SEPARATOR,
1155 GTK_RESPONSE_ACCEPT,
1158 str = (show_ref) ? g_strconcat (_("ecdg_fi_preview_font_preview_reference"), priv->preview_text, 0) :
1159 g_strdup (priv->preview_text);
1161 preview_label = gtk_label_new (str);
1162 gtk_label_set_line_wrap (GTK_LABEL(preview_label), TRUE);
1169 /* set keypress handler (ESC hardkey) */
1170 g_signal_connect (G_OBJECT (preview_dialog), "key-press-event",
1171 G_CALLBACK(hildon_font_selection_dialog_preview_key_press),
1175 list = (show_ref) ? hildon_font_selection_dialog_create_attrlist (fontsel,
1176 strlen (_("ecdg_fi_preview_font_preview_reference")),
1177 strlen (priv->preview_text)) :
1178 hildon_font_selection_dialog_create_attrlist (fontsel, 0, strlen(priv->preview_text));
1180 g_object_get (G_OBJECT (fontsel), "family", &str, "family-set",
1181 &family_set, "size", &size, "size-set", &size_set,
1184 /* A smallish hack to add scrollbar when font size is really big */
1186 if (size_set && size > 24) {
1187 GtkScrolledWindow *scrolled = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
1188 gtk_scrolled_window_set_policy (scrolled, GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1189 gtk_scrolled_window_add_with_viewport (scrolled, GTK_WIDGET (preview_label));
1190 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(preview_dialog)->vbox), GTK_WIDGET (scrolled));
1191 gtk_widget_set_size_request (GTK_WIDGET (scrolled), -1, 400);
1193 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(preview_dialog)->vbox), GTK_WIDGET (preview_label));
1195 /* make reference text to have the same fontface and size */
1198 attr = pango_attr_family_new (str);
1199 add_preview_text_attr (list, attr, 0, strlen (_("ecdg_fi_preview_font_preview_reference")));
1209 attr = pango_attr_size_new (size * PANGO_SCALE);
1210 add_preview_text_attr (list, attr, 0, strlen (_("ecdg_fi_preview_font_preview_reference")));
1213 gtk_label_set_attributes (GTK_LABEL (preview_label), list);
1214 pango_attr_list_unref (list);
1216 /*And show the dialog*/
1217 gtk_window_set_transient_for (GTK_WINDOW (preview_dialog),
1218 GTK_WINDOW (fontsel));
1220 gtk_widget_show_all (preview_dialog);
1221 gtk_dialog_set_default_response (GTK_DIALOG (preview_dialog), GTK_RESPONSE_OK);
1223 GtkBox *action_area = (GtkBox *) GTK_DIALOG (preview_dialog)->action_area;
1224 GtkWidget *button = ((GtkBoxChild *) ((GSList *) action_area->children)->data)->widget;
1225 gtk_widget_grab_focus (button);
1227 gtk_dialog_run (GTK_DIALOG (preview_dialog));
1228 gtk_widget_destroy (preview_dialog);
1233 is_internal_font (const gchar * name)
1235 /* FIXME Extremally BAD BAD BAD way of doing things */
1237 return strcmp(name, "DeviceSymbols") == 0
1238 || strcmp(name, "Nokia Smiley" ) == 0
1239 || strcmp(name, "NewCourier" ) == 0
1240 || strcmp(name, "NewTimes" ) == 0
1241 || strcmp(name, "SwissA" ) == 0
1242 || strcmp(name, "Nokia Sans" ) == 0
1243 || strcmp(name, "Nokia Sans Cn") == 0;
1247 filter_out_internal_fonts (PangoFontFamily **families,
1251 int n; /* counts valid fonts */
1252 const gchar * name = NULL;
1254 for(i = 0, n = 0; i < * n_families; i++){
1256 name = pango_font_family_get_name (families[i]);
1258 if(!is_internal_font(name))
1261 if (i!=n){ /* there are filtered out families */
1262 families[n] = families[i]; /* shift the current family */
1265 n++; /* count one more valid */
1267 } /* foreach font family */
1273 hildon_font_selection_dialog_show_available_fonts (HildonFontSelectionDialog *fontsel)
1278 HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
1281 pango_context_list_families (gtk_widget_get_pango_context
1282 (GTK_WIDGET (fontsel)), &priv->families,
1285 filter_out_internal_fonts (priv->families, &priv->n_families);
1287 qsort (priv->families, priv->n_families, sizeof(PangoFontFamily *), cmp_families);
1289 for (i = 0; i < priv->n_families; i++)
1291 const gchar *name = pango_font_family_get_name (priv->families[i]);
1292 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_font_type), name);
1297 hildon_font_selection_dialog_show_available_positionings (HildonFontSelectionDialogPrivate *priv)
1299 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_positioning), _("ecdg_va_font_printpos_1"));
1300 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_positioning), _("ecdg_va_font_printpos_2"));
1301 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_positioning), _("ecdg_va_font_printpos_3"));
1304 /* Loads the sizes from a pre-allocated table */
1306 hildon_font_selection_dialog_show_available_sizes (HildonFontSelectionDialogPrivate *priv)
1313 for (i = 0; i < G_N_ELEMENTS (font_sizes); i++)
1315 size_str = g_strdup_printf ("%i%s",
1317 _("ecdg_va_font_size_trailer"));
1319 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_font_size), size_str);
1325 toggle_clicked (GtkButton *button,
1328 GtkToggleButton *t_b = GTK_TOGGLE_BUTTON (button);
1330 /* we have to remove the inconsistent state ourselves */
1331 if (gtk_toggle_button_get_inconsistent (t_b))
1333 gtk_toggle_button_set_inconsistent (t_b, FALSE);
1334 gtk_toggle_button_set_active (t_b, FALSE);
1339 * hildon_font_selection_dialog_new:
1340 * @parent: the parent window
1341 * @title: the title of font selection dialog
1343 * If NULL is passed for title, then default title
1344 * "Font" will be used.
1346 * Returns: a new #HildonFontSelectionDialog
1349 hildon_font_selection_dialog_new (GtkWindow *parent,
1352 HildonFontSelectionDialog *fontseldiag;
1354 fontseldiag = g_object_new (HILDON_TYPE_FONT_SELECTION_DIALOG,
1355 "has-separator", FALSE, NULL);
1358 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1361 gtk_window_set_transient_for (GTK_WINDOW (fontseldiag), parent);
1363 return GTK_WIDGET (fontseldiag);
1367 * hildon_font_selection_dialog_get_preview_text:
1368 * @fsd: the font selection dialog
1370 * Gets the text in preview dialog, which does not include the
1371 * reference text. The returned string must be freed by the user.
1372 * Please note that since hildon 2.2, the preview has been discontinued,
1373 * so this setting has no effect.
1375 * Returns: a string pointer
1378 hildon_font_selection_dialog_get_preview_text (HildonFontSelectionDialog * fsd)
1380 /* FIXME Return a const pointer? */
1381 HildonFontSelectionDialogPrivate *priv;
1383 g_return_val_if_fail (HILDON_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1385 priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fsd);
1388 return g_strdup (priv->preview_text);
1392 * hildon_font_selection_dialog_set_preview_text:
1393 * @fsd: the font selection dialog
1394 * @text: the text to be displayed in the preview dialog
1396 * The default preview text is
1397 * "The quick brown fox jumped over the lazy dogs". Please note that since
1398 * hildon 2.2, the preview has been discontinued, so this setting has no effect.
1402 hildon_font_selection_dialog_set_preview_text (HildonFontSelectionDialog *fsd,
1405 HildonFontSelectionDialogPrivate *priv = NULL;
1407 g_return_if_fail (HILDON_IS_FONT_SELECTION_DIALOG (fsd));
1408 g_return_if_fail (text);
1410 priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fsd);
1413 g_free (priv->preview_text);
1414 priv->preview_text = g_strdup (text);
1415 g_object_notify (G_OBJECT (fsd), "preview-text");