Added touch font selector dialog
authorRoman Moravcik <roman.moravcik@gmail.com>
Mon, 18 Jan 2010 13:44:50 +0000 (14:44 +0100)
committerRoman Moravcik <roman.moravcik@gmail.com>
Mon, 18 Jan 2010 13:44:50 +0000 (14:44 +0100)
applet/cpmpsubtitles.c

index 73b2344..f922f80 100644 (file)
@@ -24,6 +24,8 @@
 #include <config.h>
 #endif
 
+#include <stdlib.h>
+
 #include <glib.h>
 #include <glib/gi18n-lib.h>
 
 
 typedef enum
 {
+       FONT_STYLE_REGULAR,
+       FONT_STYLE_ITALIC,
+       FONT_STYLE_BOLD,
+       FONT_STYLE_ITALIC_BOLD,
+       FONT_STYLE_LAST,
+} FontStyleIndex;
+
+typedef struct {
+       int index;
+       const char *name;
+} FontStyle;
+
+static FontStyle font_styles[] = {
+       {FONT_STYLE_REGULAR,            N_("Regular")},
+       {FONT_STYLE_ITALIC,             N_("Italic")},
+       {FONT_STYLE_BOLD,               N_("Bold")},
+       {FONT_STYLE_ITALIC_BOLD,        N_("Italic Bold")}
+};
+
+static const gint font_sizes[] =
+{
+  14, 16, 18, 20, 24, 26, -1
+};
+
+typedef enum
+{
        SUBTITLE_ENCODING_CURRENT_LOCALE,
 
        SUBTITLE_ENCODING_ISO_8859_6,
@@ -146,14 +174,12 @@ typedef enum
        SUBTITLE_ENCODING_LAST
 } SubtitleEncodingIndex;
 
-
 typedef struct {
        int index;
        const char *charset;
        const char *name;
 } SubtitleEncoding;
 
-
 static SubtitleEncoding encodings[] = {
        {SUBTITLE_ENCODING_CURRENT_LOCALE,      NULL,                   N_("Current Locale")},
 
@@ -328,25 +354,163 @@ gconf_set_string (GConfClient *client,
                g_free (tmp);
 }
 
+static gboolean
+is_internal_font (const gchar * name)
+{
+       /* FIXME Extremally BAD BAD BAD way of doing things */
+
+       return strcmp (name, "DeviceSymbols") == 0
+                      || strcmp(name, "Nokia Smiley") == 0
+                      || strcmp(name, "NewCourier") == 0
+                      || strcmp(name, "NewTimes") == 0
+                      || strcmp(name, "SwissA") == 0
+                      || strcmp(name, "Nokia Sans") == 0
+                      || strcmp(name, "Nokia Sans Cn") == 0;
+}
+
+static void
+filter_out_internal_fonts (PangoFontFamily **families,
+                          int *n_families)
+{
+       int i;
+       int n; /* counts valid fonts */
+       const gchar * name = NULL;
+
+       for (i = 0, n = 0; i < * n_families; i++) {
+               name = pango_font_family_get_name (families[i]);
+
+               if(!is_internal_font(name)) {
+                       if (i != n) { /* there are filtered out families */
+                               families[n] = families[i]; /* shift the current family */
+                       }
+                       n++; /* count one more valid */
+               }
+       } /* foreach font family */
+
+       *n_families = n;
+}
+
+static int
+cmp_families (const void *a,
+             const void *b)
+{
+       const char *a_name = pango_font_family_get_name (* (PangoFontFamily **) a);
+       const char *b_name = pango_font_family_get_name (* (PangoFontFamily **) b);
+
+       return g_utf8_collate (a_name, b_name);
+}
+
 static void
 font_selector_dialog (HildonButton *button,
                      gpointer user_data)
 {
-       GtkWidget *dialog, *hbox;
+       GtkWidget *dialog, *hbox, *family_selector, *style_selector, *size_selector;
+       gint index = 0;
+       const gchar *font = NULL;
+       PangoFontDescription *font_desc = NULL;
+       PangoFontFamily **families;
+       gint n_families = 0;
+       PangoWeight pango_weight;
+       PangoStyle pango_style;
+
+       font = hildon_button_get_value (HILDON_BUTTON (button));
+       if (font == NULL)
+               return;
+
+       font_desc = pango_font_description_from_string (font);
 
        dialog = gtk_dialog_new ();
        gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
        gtk_window_set_title (GTK_WINDOW (dialog), _("Font"));
        gtk_dialog_add_button(GTK_DIALOG (dialog), "OK", GTK_RESPONSE_ACCEPT);
+       gtk_window_set_default_size (GTK_WINDOW (dialog), -1, 400);
 
        hbox = gtk_hbox_new (FALSE, 0);
        gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), hbox);
 
-       // pango_font_description_from_string
+       /* font family selector */
+       family_selector = hildon_touch_selector_new_text ();
+       gtk_box_pack_start (GTK_BOX (hbox), family_selector, TRUE, TRUE, 0);
+
+       pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (dialog)),
+                                    &families, &n_families);
+
+       filter_out_internal_fonts (families, &n_families);
+
+       qsort (families, n_families, sizeof(PangoFontFamily *), cmp_families);
+
+       for (index = 0; index < n_families; index++) {
+               const gchar *family = pango_font_family_get_name (families[index]);
+               hildon_touch_selector_insert_text (HILDON_TOUCH_SELECTOR (family_selector),
+                                                  index, family);
+
+               if (strcmp (family, pango_font_description_get_family (font_desc)) == 0) {
+                       hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (family_selector), 0,
+                                                         index);
+                       hildon_touch_selector_center_on_selected (HILDON_TOUCH_SELECTOR (family_selector));
+               }
+       }
+       g_free (families);
+
+       /* font style selector */
+       style_selector = hildon_touch_selector_new_text ();
+       gtk_box_pack_start (GTK_BOX (hbox), style_selector, TRUE, TRUE, 0);
+
+       index = 0;
+       while (index < FONT_STYLE_LAST) {
+               const gchar *style = g_strdup_printf ("%s", _(font_styles[index].name));
+               hildon_touch_selector_insert_text (HILDON_TOUCH_SELECTOR (style_selector),
+                                                  font_styles[index].index, style);
+               index++;
+       }
+       pango_weight = pango_font_description_get_weight (font_desc);
+       pango_style = pango_font_description_get_style (font_desc);
+
+       if (pango_weight == PANGO_WEIGHT_NORMAL) {
+               if (pango_style == PANGO_STYLE_NORMAL) {
+                       hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (style_selector), 0,
+                                                         FONT_STYLE_REGULAR);
+               } else {
+                       hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (style_selector), 0,
+                                                         FONT_STYLE_ITALIC);
+               }
+       } else {
+               if (pango_style == PANGO_STYLE_NORMAL) {
+                       hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (style_selector), 0,
+                                                         FONT_STYLE_BOLD);
+               } else {
+                       hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (style_selector), 0,
+                                                         FONT_STYLE_ITALIC_BOLD);
+               }
+       }
+       hildon_touch_selector_center_on_selected (HILDON_TOUCH_SELECTOR (style_selector));
+
+       /* font size selector */
+       size_selector = hildon_touch_selector_new_text ();
+       gtk_box_pack_start (GTK_BOX (hbox), size_selector, TRUE, TRUE, 0);
+
+       index = 0;
+       while (font_sizes[index] != -1) {
+               const gchar *size = g_strdup_printf ("%d", font_sizes[index]);
+               hildon_touch_selector_insert_text (HILDON_TOUCH_SELECTOR (size_selector),
+                                                  index, size);
+
+               if (font_sizes[index] == (pango_font_description_get_size (font_desc) / PANGO_SCALE)) {
+                       hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (size_selector), 0,
+                                                         index);
+                       hildon_touch_selector_center_on_selected (HILDON_TOUCH_SELECTOR (size_selector));
+               }
+
+               index++;
+       }
 
        /* Run the dialog */
        gtk_widget_show_all (GTK_WIDGET (dialog));
        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
+               printf ("family = %s, style = %s, size = %d\n",
+                       hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (family_selector)),
+                       hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (style_selector)),
+                       hildon_touch_selector_get_active (HILDON_TOUCH_SELECTOR (size_selector), 0));
                // pango_font_description_to_string
        }
 
@@ -363,7 +527,7 @@ create_encoding_selector (void)
        selector = hildon_touch_selector_new_text ();
 
        while (index < SUBTITLE_ENCODING_LAST) {
-               gchar *encoding = NULL;
+               const gchar *encoding = NULL;
 
                if (encodings[index].charset) {
                        encoding = g_strdup_printf ("%s (%s)", _(encodings[index].name),
@@ -375,8 +539,6 @@ create_encoding_selector (void)
                hildon_touch_selector_insert_text (HILDON_TOUCH_SELECTOR (selector),
                                                   encodings[index].index,
                                                   encoding);
-               if (encoding)
-                       g_free (encoding);
                index++;
        }
 
@@ -424,14 +586,16 @@ create_subtitles_font_button (GConfClient *gconf_client)
        hildon_button_set_title_alignment (HILDON_BUTTON(button), 0.0, 0.5);
        hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
 
-       g_signal_connect (button, "clicked", G_CALLBACK (font_selector_dialog), NULL);
-
        font = gconf_get_string (gconf_client, "subtitle_font");
        if (font) {
                hildon_button_set_value (HILDON_BUTTON (button), font);
        } else {
                hildon_button_set_value (HILDON_BUTTON (button), "Sans Bold 18");
        }
+
+       g_signal_connect (button, "clicked", G_CALLBACK (font_selector_dialog),
+                         NULL);
+
        return button;
 }