2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@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
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. Users can also preview text in the selected font.
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>
50 #include <gdk/gdkkeysyms.h>
51 #include "hildon-font-selection-dialog.h"
52 #include "hildon-caption.h"
53 #include "hildon-color-button.h"
55 #include "hildon-font-selection-dialog-private.h"
57 /* These are what we use as the standard font sizes, for the size list */
59 static const guint16 font_sizes[] =
61 6, 8, 10, 12, 16, 24, 32
80 PROP_STRIKETHROUGH_SET,
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 ;-)*/
94 HildonFontSelectionDialog *fsd; /* pointer to our font selection dialog */
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 */
106 } HildonFontSelectionDialogSettings;
109 hildon_font_selection_dialog_preview_key_press (GtkWidget *widget,
114 cmp_families (const void *a,
118 hildon_font_selection_dialog_show_preview (HildonFontSelectionDialog *fontsel);
120 static PangoAttrList*
121 hildon_font_selection_dialog_create_attrlist (HildonFontSelectionDialog *fontsel,
126 hildon_font_selection_dialog_show_available_positionings (HildonFontSelectionDialogPrivate *priv);
129 hildon_font_selection_dialog_show_available_fonts (HildonFontSelectionDialog *fontsel);
132 hildon_font_selection_dialog_show_available_sizes (HildonFontSelectionDialogPrivate *priv);
135 hildon_font_selection_dialog_class_init (HildonFontSelectionDialogClass *klass);
138 hildon_font_selection_dialog_init (HildonFontSelectionDialog *fontseldiag);
141 hildon_font_selection_dialog_finalize (GObject *object);
144 hildon_font_selection_dialog_construct_notebook (HildonFontSelectionDialog *fontsel);
147 color_modified_cb (HildonColorButton *button,
152 add_preview_text_attr (PangoAttrList *list,
153 PangoAttribute *attr,
158 toggle_clicked (GtkButton *button,
161 static GtkDialogClass* parent_class = NULL;
163 #define _(String) dgettext("hildon-libs", String)
165 #define SUPERSCRIPT_RISE 3333
167 #define SUBSCRIPT_LOW -3333
174 hildon_font_selection_dialog_get_type (void)
176 static GType font_selection_dialog_type = 0;
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),
188 (GInstanceInitFunc) hildon_font_selection_dialog_init,
191 font_selection_dialog_type =
192 g_type_register_static (GTK_TYPE_DIALOG,
193 "HildonFontSelectionDialog",
194 &fontsel_diag_info, 0);
197 return font_selection_dialog_type;
201 hildon_font_selection_dialog_get_property (GObject *object,
207 GdkColor *color = NULL;
209 HildonFontSelectionDialogPrivate *priv =
210 HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE(object);
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]));
222 g_value_set_string (value, "Sans");
223 /* FIXME Bad hardcoding here */
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);
231 g_value_set_boolean (value, FALSE);
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]);
239 g_value_set_int (value, 16);
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);
247 g_value_set_boolean (value, FALSE);
251 color = g_new (GdkColor, 1);
252 hildon_color_button_get_color
253 (HILDON_COLOR_BUTTON (priv->font_color_button), color);
254 g_value_set_boxed (value, (gconstpointer) color);
256 gdk_color_free (color);
260 g_value_set_boolean (value, priv->color_set);
264 g_value_set_boolean (value,
265 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->chk_bold)));
269 g_value_set_boolean (value,
270 ! gtk_toggle_button_get_inconsistent
271 (GTK_TOGGLE_BUTTON (priv->chk_bold)));
275 g_value_set_boolean (value,
276 gtk_toggle_button_get_active
277 (GTK_TOGGLE_BUTTON (priv->chk_italic)));
280 case PROP_ITALIC_SET:
281 g_value_set_boolean (value,
282 ! gtk_toggle_button_get_inconsistent
283 (GTK_TOGGLE_BUTTON (priv->chk_italic)));
287 g_value_set_boolean (value,
288 gtk_toggle_button_get_active
289 (GTK_TOGGLE_BUTTON (priv->chk_underline)));
292 case PROP_UNDERLINE_SET:
293 g_value_set_boolean (value,
294 ! gtk_toggle_button_get_inconsistent
295 (GTK_TOGGLE_BUTTON (priv->chk_underline)));
298 case PROP_STRIKETHROUGH:
299 g_value_set_boolean(value,
300 gtk_toggle_button_get_active
301 (GTK_TOGGLE_BUTTON (priv->chk_strikethrough)));
304 case PROP_STRIKETHROUGH_SET:
305 g_value_set_boolean(value,
306 ! gtk_toggle_button_get_inconsistent
307 (GTK_TOGGLE_BUTTON (priv->chk_strikethrough)));
311 i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_positioning));
312 if(i == 1) /* super */
313 g_value_set_int (value, 1);
314 else if(i == 2)/* sub */
315 g_value_set_int (value, -1);
317 g_value_set_int (value, 0);
320 case PROP_FONT_SCALING:
321 g_value_set_double (value, priv->font_scaling);
324 case PROP_POSITION_SET:
325 i = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->cbx_positioning));
327 g_value_set_boolean (value, TRUE);
329 g_value_set_boolean (value, FALSE);
332 case PROP_PREVIEW_TEXT:
333 g_value_set_string (value,
334 hildon_font_selection_dialog_get_preview_text (HILDON_FONT_SELECTION_DIALOG (object)));
338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 hildon_font_selection_dialog_set_property (GObject *object,
352 GdkColor *color = NULL;
355 HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (object);
358 black.red = black.green = black.blue = 0;
363 str = g_value_get_string (value);
364 for(i = 0; i < priv->n_families; i++)
366 if (strcmp (str, pango_font_family_get_name (priv->families[i]))
369 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_type), i);
375 case PROP_FAMILY_SET:
376 b = g_value_get_boolean (value);
378 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_type), -1);
382 size = g_value_get_int (value);
383 for(i = 0; i < G_N_ELEMENTS (font_sizes); i++)
385 if(size == font_sizes[i])
387 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_size), i);
394 b = g_value_get_boolean (value);
396 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->cbx_font_size), -1);
400 color = (GdkColor *) g_value_get_boxed (value);
402 hildon_color_button_set_color (HILDON_COLOR_BUTTON
403 (priv->font_color_button),
406 hildon_color_button_set_color (HILDON_COLOR_BUTTON
407 (priv->font_color_button),
412 priv->color_set = g_value_get_boolean (value);
413 if(! priv->color_set)
415 /* set color to black, but block our signal handler */
416 g_signal_handler_block ((gpointer) priv->font_color_button,
417 priv->color_modified_signal_handler);
419 hildon_color_button_set_color (HILDON_COLOR_BUTTON
420 (priv->font_color_button),
423 g_signal_handler_unblock ((gpointer) priv->font_color_button,
424 priv->color_modified_signal_handler);
429 /* this call will make sure that we dont get extra clicked signal */
430 gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON(priv->chk_bold), FALSE);
431 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(priv->chk_bold), g_value_get_boolean (value));
435 gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON (priv->chk_bold),! g_value_get_boolean(value));
439 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(priv->chk_italic),
441 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->chk_italic),
442 g_value_get_boolean(value));
445 case PROP_ITALIC_SET:
446 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(priv->chk_italic),
447 !g_value_get_boolean(value));
451 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON
452 (priv->chk_underline),
454 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->chk_underline),
455 g_value_get_boolean(value));
458 case PROP_UNDERLINE_SET:
459 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(priv->chk_underline),
460 !g_value_get_boolean(value));
463 case PROP_STRIKETHROUGH:
464 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON
465 (priv->chk_strikethrough),
467 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->chk_strikethrough),
468 g_value_get_boolean(value));
471 case PROP_STRIKETHROUGH_SET:
472 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON
473 (priv->chk_strikethrough),
474 !g_value_get_boolean(value));
478 i = g_value_get_int(value);
480 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), 1);
482 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), 2);
484 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), 0);
487 case PROP_FONT_SCALING:
488 priv->font_scaling = g_value_get_double(value);
491 case PROP_POSITION_SET:
492 b = g_value_get_boolean(value);
494 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->cbx_positioning), -1);
497 case PROP_PREVIEW_TEXT:
498 hildon_font_selection_dialog_set_preview_text(
499 HILDON_FONT_SELECTION_DIALOG(object),
500 g_value_get_string(value));
504 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
510 hildon_font_selection_dialog_class_init (HildonFontSelectionDialogClass *klass)
512 GObjectClass *gobject_class;
514 parent_class = g_type_class_peek_parent (klass);
515 gobject_class = G_OBJECT_CLASS (klass);
517 gobject_class->finalize = hildon_font_selection_dialog_finalize;
518 gobject_class->get_property = hildon_font_selection_dialog_get_property;
519 gobject_class->set_property = hildon_font_selection_dialog_set_property;
521 /* Install property to the class */
522 g_object_class_install_property (gobject_class, PROP_FAMILY,
523 g_param_spec_string ("family",
524 "Font family", "String defines"
525 " the font family", "Sans",
528 g_object_class_install_property (gobject_class, PROP_FAMILY_SET,
529 g_param_spec_boolean ("family-set",
530 "family inconsistent state",
531 "Whether the family property"
532 " is inconsistent", FALSE,
533 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
535 g_object_class_install_property (gobject_class, PROP_SIZE,
536 g_param_spec_int ("size",
542 g_object_class_install_property (gobject_class, PROP_SIZE_SET,
543 g_param_spec_boolean ("size-set",
544 "size inconsistent state",
545 "Whether the size property"
546 " is inconsistent", FALSE,
547 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
549 g_object_class_install_property (gobject_class, PROP_COLOR,
550 g_param_spec_boxed ("color",
552 "gdk color for the text",
556 g_object_class_install_property (gobject_class, PROP_COLOR_SET,
557 g_param_spec_boolean ("color-set",
558 "color inconsistent state",
559 "Whether the color property"
560 " is inconsistent", FALSE,
561 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
563 g_object_class_install_property (gobject_class, PROP_BOLD,
564 g_param_spec_boolean ("bold",
566 "Whether the text is bold",
570 g_object_class_install_property (gobject_class, PROP_BOLD_SET,
571 g_param_spec_boolean ("bold-set",
572 "bold inconsistent state",
574 " is inconsistent", FALSE,
575 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
577 g_object_class_install_property (gobject_class, PROP_ITALIC,
578 g_param_spec_boolean ("italic",
580 "Whether the text is italic",
584 g_object_class_install_property (gobject_class, PROP_ITALIC_SET,
585 g_param_spec_boolean ("italic-set",
586 "italic inconsistent state",
588 " is inconsistent", FALSE,
589 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
591 g_object_class_install_property (gobject_class, PROP_UNDERLINE,
592 g_param_spec_boolean ("underline",
594 "Whether the text is underlined",
598 g_object_class_install_property (gobject_class, PROP_UNDERLINE_SET,
599 g_param_spec_boolean ("underline-set",
600 "underline inconsistent state",
601 "Whether the underline"
602 " is inconsistent", FALSE,
603 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
605 g_object_class_install_property (gobject_class, PROP_STRIKETHROUGH,
606 g_param_spec_boolean ("strikethrough",
607 "strikethroughed text",
608 "Whether the text is strikethroughed",
612 g_object_class_install_property (gobject_class, PROP_STRIKETHROUGH_SET,
613 g_param_spec_boolean ("strikethrough-set",
614 "strikethrough inconsistent state",
615 "Whether the strikethrough"
616 " is inconsistent", FALSE,
617 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
619 g_object_class_install_property (gobject_class, PROP_POSITION,
620 g_param_spec_int ("position",
622 "Font position super or subscript",
626 g_object_class_install_property (gobject_class, PROP_FONT_SCALING,
627 g_param_spec_double ("font-scaling",
629 "Font scaling for the preview dialog",
633 g_object_class_install_property (gobject_class, PROP_POSITION_SET,
634 g_param_spec_boolean ("position-set",
635 "position inconsistent state",
636 "Whether the position"
637 " is inconsistent", FALSE,
638 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
640 g_object_class_install_property (gobject_class, PROP_PREVIEW_TEXT,
641 g_param_spec_string("preview-text",
643 "the text in preview dialog, which does"
644 "not include the reference text",
648 g_type_class_add_private (klass, sizeof(struct _HildonFontSelectionDialogPrivate));
652 hildon_font_selection_dialog_init (HildonFontSelectionDialog *fontseldiag)
654 HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontseldiag);
655 GtkWidget *preview_button;
658 priv->notebook = GTK_NOTEBOOK (gtk_notebook_new ());
660 hildon_font_selection_dialog_construct_notebook (fontseldiag);
662 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (fontseldiag)->vbox),
663 GTK_WIDGET (priv->notebook), TRUE, TRUE, 0);
665 /* Add dialog buttons */
666 gtk_dialog_add_button (GTK_DIALOG (fontseldiag),
667 _("ecdg_bd_font_dialog_ok"),
670 preview_button = gtk_button_new_with_label (_("ecdg_bd_font_dialog_preview"));
671 gtk_box_pack_start (GTK_BOX(GTK_DIALOG (fontseldiag)->action_area),
672 preview_button, FALSE, TRUE, 0);
674 g_signal_connect_swapped (preview_button, "clicked",
676 (hildon_font_selection_dialog_show_preview),
678 gtk_widget_show(preview_button);
680 gtk_dialog_add_button (GTK_DIALOG (fontseldiag),
681 _("ecdg_bd_font_dialog_cancel"),
682 GTK_RESPONSE_CANCEL);
684 /*Set default preview text*/
685 priv->preview_text = g_strdup (_("ecdg_fi_preview_font_preview_text"));
687 gtk_window_set_title (GTK_WINDOW (fontseldiag), _("ecdg_ti_font"));
688 /*here is the line to make sure that notebook has the default focus*/
689 gtk_container_set_focus_child (GTK_CONTAINER (GTK_DIALOG (fontseldiag)->vbox),
690 GTK_WIDGET (priv->notebook));
694 hildon_font_selection_dialog_construct_notebook (HildonFontSelectionDialog *fontsel)
697 GtkWidget *vbox_tab[3];
698 GtkWidget *font_color_box;
699 GtkWidget *caption_control;
702 HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
705 for (i = 0; i < 3; i++)
706 vbox_tab[i] = gtk_vbox_new (TRUE, 0);
708 group = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL));
710 /* Build the first page of the GtkNotebook: font style */
711 priv->cbx_font_type = gtk_combo_box_new_text ();
712 hildon_font_selection_dialog_show_available_fonts (fontsel);
713 caption_control = hildon_caption_new (group,
714 _("ecdg_fi_font_font"),
717 HILDON_CAPTION_OPTIONAL);
719 gtk_box_pack_start (GTK_BOX (vbox_tab[0]), caption_control, FALSE, FALSE, 0);
721 priv->cbx_font_size = gtk_combo_box_new_text ();
722 hildon_font_selection_dialog_show_available_sizes (priv);
723 caption_control = hildon_caption_new (group,
724 _("ecdg_fi_font_size"),
727 HILDON_CAPTION_OPTIONAL);
729 gtk_box_pack_start (GTK_BOX (vbox_tab[0]), caption_control, FALSE, FALSE, 0);
731 font_color_box = gtk_hbox_new (FALSE, 0);
732 priv->font_color_button = hildon_color_button_new ();
733 priv->color_set = FALSE;
734 priv->font_scaling = 1.0;
735 priv->color_modified_signal_handler =
736 g_signal_connect (G_OBJECT (priv->font_color_button), "notify::color",
737 G_CALLBACK (color_modified_cb), (gpointer) priv);
739 gtk_box_pack_start (GTK_BOX (font_color_box), priv->font_color_button, FALSE, FALSE, 0);
742 hildon_caption_new (group, _("ecdg_fi_font_colour_selector"),
744 NULL, HILDON_CAPTION_OPTIONAL);
746 gtk_box_pack_start (GTK_BOX (vbox_tab[0]), caption_control, FALSE, FALSE, 0);
748 /* Build the second page of the GtkNotebook: font formatting */
749 priv->chk_bold = gtk_check_button_new ();
750 caption_control = hildon_caption_new (group,
751 _("ecdg_fi_font_bold"),
754 HILDON_CAPTION_OPTIONAL);
756 gtk_box_pack_start (GTK_BOX (vbox_tab[1]), caption_control, FALSE, FALSE, 0);
757 g_signal_connect (G_OBJECT (priv->chk_bold), "clicked",
758 G_CALLBACK(toggle_clicked), NULL);
760 priv->chk_italic = gtk_check_button_new ();
761 caption_control = hildon_caption_new (group, _("ecdg_fi_font_italic"),
763 NULL, HILDON_CAPTION_OPTIONAL);
765 gtk_box_pack_start (GTK_BOX (vbox_tab[1]), caption_control, FALSE, FALSE, 0);
766 g_signal_connect(G_OBJECT(priv->chk_italic), "clicked",
767 G_CALLBACK(toggle_clicked), NULL);
769 priv->chk_underline = gtk_check_button_new();
771 hildon_caption_new (group, _("ecdg_fi_font_underline"),
772 priv->chk_underline, NULL,
773 HILDON_CAPTION_OPTIONAL);
775 gtk_box_pack_start (GTK_BOX (vbox_tab[1]), caption_control, FALSE, FALSE, 0);
776 g_signal_connect (G_OBJECT(priv->chk_underline), "clicked",
777 G_CALLBACK (toggle_clicked), NULL);
779 /* Build the third page of the GtkNotebook: other font properties */
780 priv->chk_strikethrough = gtk_check_button_new ();
781 caption_control = hildon_caption_new(group, _("ecdg_fi_font_strikethrough"),
782 priv->chk_strikethrough, NULL,
783 HILDON_CAPTION_OPTIONAL);
785 gtk_box_pack_start (GTK_BOX (vbox_tab[2]), caption_control, FALSE, FALSE, 0);
786 g_signal_connect (G_OBJECT(priv->chk_strikethrough), "clicked",
787 G_CALLBACK (toggle_clicked), NULL);
789 priv->cbx_positioning = gtk_combo_box_new_text ();
790 hildon_font_selection_dialog_show_available_positionings (priv);
792 hildon_caption_new(group, _("ecdg_fi_font_special"),
793 priv->cbx_positioning, NULL,
794 HILDON_CAPTION_OPTIONAL);
796 gtk_box_pack_start (GTK_BOX (vbox_tab[2]), caption_control, FALSE, FALSE, 0);
798 /* Populate notebook */
799 gtk_notebook_insert_page (priv->notebook, vbox_tab[0], NULL, 0);
800 gtk_notebook_insert_page (priv->notebook, vbox_tab[1], NULL, 1);
801 gtk_notebook_insert_page (priv->notebook, vbox_tab[2], NULL, 2);
803 gtk_notebook_set_tab_label_text (priv->notebook, vbox_tab[0],
804 _("ecdg_ti_font_dialog_style"));
805 gtk_notebook_set_tab_label_text (priv->notebook, vbox_tab[1],
806 _("ecdg_ti_font_dialog_format"));
807 gtk_notebook_set_tab_label_text (priv->notebook, vbox_tab[2],
808 _("ecdg_ti_font_dialog_other"));
810 gtk_widget_show_all (GTK_WIDGET (priv->notebook));
814 color_modified_cb (HildonColorButton *button,
818 HildonFontSelectionDialogPrivate *priv = (HildonFontSelectionDialogPrivate *) fsd_priv;
821 priv->color_set = TRUE;
825 hildon_font_selection_dialog_finalize (GObject *object)
827 HildonFontSelectionDialogPrivate *priv;
828 HildonFontSelectionDialog *fontsel;
830 g_assert (HILDON_IS_FONT_SELECTION_DIALOG (object));
831 fontsel = HILDON_FONT_SELECTION_DIALOG (object);
833 priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
836 g_free (priv->preview_text);
837 g_free (priv->families);
839 if (G_OBJECT_CLASS (parent_class)->finalize)
840 G_OBJECT_CLASS (parent_class)->finalize (object);
844 cmp_families (const void *a,
848 pango_font_family_get_name (* (PangoFontFamily **) a);
851 pango_font_family_get_name (* (PangoFontFamily **) b);
853 return g_utf8_collate (a_name, b_name);
856 /* Exits the preview dialog with GTK_RESPONSE_CANCEL if Esc key
858 * FIXME This should be handled automatically */
860 hildon_font_selection_dialog_preview_key_press (GtkWidget *widget,
867 if (event->keyval == GDK_Escape)
869 gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_CANCEL);
877 add_preview_text_attr (PangoAttrList *list,
878 PangoAttribute *attr,
882 attr->start_index = start;
883 attr->end_index = start + len;
884 pango_attr_list_insert (list, attr);
887 static PangoAttrList*
888 hildon_font_selection_dialog_create_attrlist (HildonFontSelectionDialog *fontsel,
893 PangoAttribute *attr;
895 gboolean family_set, size_set, color_set, bold, bold_set,
896 italic, italic_set, underline, underline_set,
897 strikethrough, strikethrough_set, position_set;
898 GdkColor *color = NULL;
899 gchar *family = NULL;
900 gdouble font_scaling = 1.0;
902 list = pango_attr_list_new ();
904 g_object_get (G_OBJECT (fontsel),
905 "family", &family, "family-set", &family_set,
906 "size", &size, "size-set", &size_set,
907 "color", &color, "color-set", &color_set,
908 "bold", &bold, "bold-set", &bold_set,
909 "italic", &italic, "italic-set", &italic_set,
910 "underline", &underline, "underline-set", &underline_set,
911 "strikethrough", &strikethrough, "strikethrough-set",
912 &strikethrough_set, "position", &position,
913 "position-set", &position_set,
914 "font-scaling", &font_scaling,
920 attr = pango_attr_family_new (family);
921 add_preview_text_attr (list, attr, start_index, len);
928 attr = pango_attr_size_new (size * PANGO_SCALE);
929 add_preview_text_attr (list, attr, start_index, len);
935 attr = pango_attr_foreground_new (color->red, color->green, color->blue);
936 add_preview_text_attr (list, attr, start_index, len);
940 gdk_color_free (color);
946 attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
948 attr = pango_attr_weight_new (PANGO_WEIGHT_NORMAL);
950 add_preview_text_attr(list, attr, start_index, len);
957 attr = pango_attr_style_new (PANGO_STYLE_ITALIC);
959 attr = pango_attr_style_new (PANGO_STYLE_NORMAL);
961 add_preview_text_attr(list, attr, start_index, len);
968 attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
970 attr = pango_attr_underline_new (PANGO_UNDERLINE_NONE);
972 add_preview_text_attr(list, attr, start_index, len);
976 if (strikethrough_set)
979 attr = pango_attr_strikethrough_new (TRUE);
981 attr = pango_attr_strikethrough_new (FALSE);
983 add_preview_text_attr(list, attr, start_index, len);
992 attr = pango_attr_rise_new (SUPERSCRIPT_RISE);
995 attr = pango_attr_rise_new (SUBSCRIPT_LOW);
998 attr = pango_attr_rise_new (0);
1002 add_preview_text_attr (list, attr, start_index, len);
1005 /* font scaling for preview */
1008 attr = pango_attr_scale_new(font_scaling);
1009 add_preview_text_attr(list, attr, 0, len + start_index);
1016 hildon_font_selection_dialog_show_preview (HildonFontSelectionDialog *fontsel)
1018 HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
1020 gboolean family_set, size_set;
1021 PangoAttribute *attr;
1022 PangoAttrList *list;
1023 GtkWidget *preview_dialog;
1024 GtkWidget *preview_label;
1026 gboolean position_set = FALSE;
1028 gboolean show_ref = FALSE;
1032 g_object_get (G_OBJECT (fontsel), "position-set", &position_set, NULL);
1035 g_object_get (G_OBJECT (fontsel), "position", &position, NULL);
1036 if (position == 1 || position == -1)
1040 /* preview dialog init */
1042 gtk_dialog_new_with_buttons (_("ecdg_ti_preview_font"), NULL,
1044 GTK_DIALOG_DESTROY_WITH_PARENT |
1045 GTK_DIALOG_NO_SEPARATOR,
1046 _("ecdg_bd_font_dialog_ok"),
1047 GTK_RESPONSE_ACCEPT,
1050 str = (show_ref) ? g_strconcat (_("ecdg_fi_preview_font_preview_reference"), priv->preview_text, 0) :
1051 g_strdup (priv->preview_text);
1053 preview_label = gtk_label_new (str);
1054 gtk_label_set_line_wrap (GTK_LABEL(preview_label), TRUE);
1059 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(preview_dialog)->vbox),
1063 /* set keypress handler (ESC hardkey) */
1064 g_signal_connect (G_OBJECT (preview_dialog), "key-press-event",
1065 G_CALLBACK(hildon_font_selection_dialog_preview_key_press),
1069 list = (show_ref) ? hildon_font_selection_dialog_create_attrlist (fontsel,
1070 strlen (_("ecdg_fi_preview_font_preview_reference")),
1071 strlen (priv->preview_text)) :
1072 hildon_font_selection_dialog_create_attrlist (fontsel, 0, strlen(priv->preview_text));
1074 g_object_get (G_OBJECT (fontsel), "family", &str, "family-set",
1075 &family_set, "size", &size, "size-set", &size_set,
1078 /* FIXME: This is a slightly ugly hack to force the width of the window so that
1079 * the whole text fits with various font sizes. It's being done in such a way,
1080 * because of some GtkLabel wrapping issues and other mysterious bugs related to
1081 * truncating ellipsizing. Needs a rethink in future */
1083 gint dialog_width = (size_set && size > 24) ? 600 : 500;
1084 gtk_window_set_default_size (GTK_WINDOW (preview_dialog), dialog_width, -1);
1086 /* make reference text to have the same fontface and size */
1089 attr = pango_attr_family_new (str);
1090 add_preview_text_attr (list, attr, 0, strlen (_("ecdg_fi_preview_font_preview_reference")));
1097 attr = pango_attr_size_new (size * PANGO_SCALE);
1098 add_preview_text_attr (list, attr, 0, strlen (_("ecdg_fi_preview_font_preview_reference")));
1101 gtk_label_set_attributes (GTK_LABEL (preview_label), list);
1102 pango_attr_list_unref (list);
1104 /*And show the dialog*/
1105 gtk_window_set_transient_for (GTK_WINDOW (preview_dialog),
1106 GTK_WINDOW (fontsel));
1108 gtk_widget_show_all (preview_dialog);
1109 gtk_dialog_run (GTK_DIALOG (preview_dialog));
1110 gtk_widget_destroy (preview_dialog);
1115 is_internal_font (const gchar * name)
1117 /* FIXME Extremally BAD BAD BAD way of doing things */
1119 return strcmp(name, "DeviceSymbols") == 0
1120 || strcmp(name, "Nokia Smiley" ) == 0
1121 || strcmp(name, "NewCourier" ) == 0
1122 || strcmp(name, "NewTimes" ) == 0
1123 || strcmp(name, "SwissA" ) == 0
1124 || strcmp(name, "Nokia Sans" ) == 0
1125 || strcmp(name, "Nokia Sans Cn") == 0;
1129 filter_out_internal_fonts (PangoFontFamily **families,
1133 int n; /* counts valid fonts */
1134 const gchar * name = NULL;
1136 for(i = 0, n = 0; i < * n_families; i++){
1138 name = pango_font_family_get_name (families[i]);
1140 if(!is_internal_font(name))
1143 if (i!=n){ /* there are filtered out families */
1144 families[n] = families[i]; /* shift the current family */
1147 n++; /* count one more valid */
1149 } /* foreach font family */
1155 hildon_font_selection_dialog_show_available_fonts (HildonFontSelectionDialog *fontsel)
1160 HildonFontSelectionDialogPrivate *priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
1163 pango_context_list_families (gtk_widget_get_pango_context
1164 (GTK_WIDGET (fontsel)), &priv->families,
1167 filter_out_internal_fonts (priv->families, &priv->n_families);
1169 qsort (priv->families, priv->n_families, sizeof(PangoFontFamily *), cmp_families);
1171 for (i = 0; i < priv->n_families; i++)
1173 const gchar *name = pango_font_family_get_name (priv->families[i]);
1174 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_font_type), name);
1179 hildon_font_selection_dialog_show_available_positionings (HildonFontSelectionDialogPrivate *priv)
1181 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_positioning), _("ecdg_va_font_printpos_1"));
1182 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_positioning), _("ecdg_va_font_printpos_2"));
1183 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_positioning), _("ecdg_va_font_printpos_3"));
1186 /* Loads the sizes from a pre-allocated table */
1188 hildon_font_selection_dialog_show_available_sizes (HildonFontSelectionDialogPrivate *priv)
1195 for (i = 0; i < G_N_ELEMENTS (font_sizes); i++)
1197 size_str = g_strdup_printf ("%i%s",
1199 _("ecdg_va_font_size_trailer"));
1201 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->cbx_font_size), size_str);
1207 toggle_clicked (GtkButton *button,
1210 GtkToggleButton *t_b = GTK_TOGGLE_BUTTON (button);
1212 /* we have to remove the inconsistent state ourselves */
1213 if (gtk_toggle_button_get_inconsistent (t_b))
1215 gtk_toggle_button_set_inconsistent (t_b, FALSE);
1216 gtk_toggle_button_set_active (t_b, FALSE);
1221 * hildon_font_selection_dialog_new:
1222 * @parent: the parent window
1223 * @title: the title of font selection dialog
1225 * If NULL is passed for title, then default title
1226 * "Font" will be used.
1228 * Returns: a new #HildonFontSelectionDialog
1231 hildon_font_selection_dialog_new (GtkWindow *parent,
1234 HildonFontSelectionDialog *fontseldiag;
1236 fontseldiag = g_object_new (HILDON_TYPE_FONT_SELECTION_DIALOG,
1237 "has-separator", FALSE, NULL);
1240 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1243 gtk_window_set_transient_for (GTK_WINDOW (fontseldiag), parent);
1245 return GTK_WIDGET (fontseldiag);
1249 * hildon_font_selection_dialog_get_preview_text:
1250 * @fsd: the font selection dialog
1252 * Gets the text in preview dialog, which does not include the
1253 * reference text. The returned string must be freed by the user.
1255 * Returns: a string pointer
1258 hildon_font_selection_dialog_get_preview_text(HildonFontSelectionDialog * fsd)
1260 /* FIXME Return a const pointer? */
1261 HildonFontSelectionDialogPrivate *priv;
1263 g_return_val_if_fail (HILDON_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1265 priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fsd);
1268 return g_strdup (priv->preview_text);
1272 * hildon_font_selection_dialog_set_preview_text:
1273 * @fsd: the font selection dialog
1274 * @text: the text to be displayed in the preview dialog
1276 * The default preview text is
1277 * "The quick brown fox jumped over the lazy dogs"
1280 hildon_font_selection_dialog_set_preview_text (HildonFontSelectionDialog *fsd,
1283 HildonFontSelectionDialogPrivate *priv = NULL;
1285 g_return_if_fail (HILDON_IS_FONT_SELECTION_DIALOG (fsd));
1286 g_return_if_fail (text);
1288 priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fsd);
1291 g_free (priv->preview_text);
1292 priv->preview_text = g_strdup (text);
1293 g_object_notify (G_OBJECT (fsd), "preview-text");