Version bump (0.3.2010.24-1+0m5-2)
[mafwsubrenderer] / applet / cpmpsubtitles.c
index 73b2344..5bf8b1c 100644 (file)
@@ -2,8 +2,11 @@
  *  Subtitles control panel applet.
  *  Copyright (C) 2010 Roman Moravcik
  *
- * encodings structure imported from totem-subtitle-encoding.c
- * Copyright (C) 2001-2006 Bastien Nocera <hadess@hadess.net>
+ *  encodings structure imported from totem-subtitle-encoding.c
+ *  Copyright (C) 2001-2006 Bastien Nocera <hadess@hadess.net>
+ *
+ *  font family detection imported from hildon-font-selection-dialog.c
+ *  Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -24,6 +27,8 @@
 #include <config.h>
 #endif
 
+#include <stdlib.h>
+
 #include <glib.h>
 #include <glib/gi18n-lib.h>
 
 
 #include <gconf/gconf-client.h>
 
-#define GCONF_MAFW_GST_SUBTITLE_RENDERER "/system/mafw/mafw-gst-renderer"
+#define GCONF_MAFW_GST_SUBTITLE_RENDERER "/system/mafw/mafw-gst-subtitles-renderer"
 #define _HL(str) dgettext("hildon-libs",str)
 
 typedef enum
 {
-       SUBTITLE_ENCODING_CURRENT_LOCALE,
+       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[] =
+{
+  10, 12, 14, 16, 18, 20, 22, 24, 26, 28, -1
+};
 
+typedef enum
+{
        SUBTITLE_ENCODING_ISO_8859_6,
        SUBTITLE_ENCODING_IBM_864,
        SUBTITLE_ENCODING_MAC_ARABIC,
@@ -143,20 +172,18 @@ typedef enum
        SUBTITLE_ENCODING_VISCII,
        SUBTITLE_ENCODING_WINDOWS_1258,
 
+       SUBTITLE_ENCODING_CURRENT_LOCALE,
+
        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")},
-
        {SUBTITLE_ENCODING_ISO_8859_6,          "ISO-8859-6",           N_("Arabic")},
        {SUBTITLE_ENCODING_IBM_864,             "IBM864",               N_("Arabic")},
        {SUBTITLE_ENCODING_MAC_ARABIC,          "MAC_ARABIC",           N_("Arabic")},
@@ -258,7 +285,9 @@ static SubtitleEncoding encodings[] = {
 
        {SUBTITLE_ENCODING_TCVN,                "TCVN",                 N_("Vietnamese")},
        {SUBTITLE_ENCODING_VISCII,              "VISCII",               N_("Vietnamese")},
-       {SUBTITLE_ENCODING_WINDOWS_1258,        "WINDOWS-1258",         N_("Vietnamese")}
+       {SUBTITLE_ENCODING_WINDOWS_1258,        "WINDOWS-1258",         N_("Vietnamese")},
+
+       {SUBTITLE_ENCODING_CURRENT_LOCALE,      NULL,                   N_("Current Locale")}
 };
 
 static gboolean
@@ -328,28 +357,208 @@ 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;
+}
+
+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 int
+cmp_encodings (const void *a,
+              const void *b)
+{
+       const SubtitleEncoding *a_encoding = (SubtitleEncoding *) a;
+       const SubtitleEncoding *b_encoding = (SubtitleEncoding *) b;
+
+       return g_utf8_collate (_(a_encoding->name), _(b_encoding->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_widget_set_size_request (style_selector, 200, -1);
+       gtk_box_pack_start (GTK_BOX (hbox), style_selector, FALSE, 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_widget_set_size_request (size_selector, 100, -1);
+       gtk_box_pack_start (GTK_BOX (hbox), size_selector, FALSE, 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) {
-               // pango_font_description_to_string
+               if (font_desc)
+                       pango_font_description_free (font_desc);
+
+               font_desc = pango_font_description_new ();
+
+               /* set font family */
+               pango_font_description_set_family (font_desc,
+                                                  hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (family_selector)));
+
+               /* set font style */
+               switch (hildon_touch_selector_get_active (HILDON_TOUCH_SELECTOR (style_selector), 0)) {
+                       case FONT_STYLE_REGULAR:
+                               pango_font_description_set_style (font_desc, PANGO_STYLE_NORMAL);
+                               pango_font_description_set_weight (font_desc, PANGO_WEIGHT_NORMAL);
+                               break;
+
+                       case FONT_STYLE_ITALIC:
+                               pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC);
+                               pango_font_description_set_weight (font_desc, PANGO_WEIGHT_NORMAL);
+                               break;
+
+                       case FONT_STYLE_BOLD:
+                               pango_font_description_set_style (font_desc, PANGO_STYLE_NORMAL);
+                               pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
+                               break;
+
+                       case FONT_STYLE_ITALIC_BOLD:
+                               pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC);
+                               pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
+                               break;
+               }
+
+               /* set font size */
+               pango_font_description_set_size (font_desc,
+                                                font_sizes[hildon_touch_selector_get_active (HILDON_TOUCH_SELECTOR (size_selector), 0)] * PANGO_SCALE);
+
+               hildon_button_set_value (HILDON_BUTTON (button), pango_font_description_to_string (font_desc));
        }
 
+       if (font_desc)
+               pango_font_description_free (font_desc);
+
        gtk_widget_destroy(GTK_WIDGET(dialog));
 }
 
@@ -362,8 +571,10 @@ create_encoding_selector (void)
 
        selector = hildon_touch_selector_new_text ();
 
+       qsort (encodings, SUBTITLE_ENCODING_LAST - 1, sizeof (SubtitleEncoding), cmp_encodings);
+
        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),
@@ -373,10 +584,8 @@ create_encoding_selector (void)
                }
 
                hildon_touch_selector_insert_text (HILDON_TOUCH_SELECTOR (selector),
-                                                  encodings[index].index,
+                                                  index,
                                                   encoding);
-               if (encoding)
-                       g_free (encoding);
                index++;
        }
 
@@ -423,8 +632,7 @@ create_subtitles_font_button (GConfClient *gconf_client)
        hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
        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);
+       hildon_button_set_style (HILDON_BUTTON (button), HILDON_BUTTON_STYLE_PICKER);
 
        font = gconf_get_string (gconf_client, "subtitle_font");
        if (font) {
@@ -432,6 +640,10 @@ create_subtitles_font_button (GConfClient *gconf_client)
        } else {
                hildon_button_set_value (HILDON_BUTTON (button), "Sans Bold 18");
        }
+
+       g_signal_connect (button, "clicked", G_CALLBACK (font_selector_dialog),
+                         NULL);
+
        return button;
 }
 
@@ -464,14 +676,15 @@ create_subtitles_encoding_button (GConfClient *gconf_client)
 
        encoding = gconf_get_string (gconf_client, "subtitle_encoding");
        if (encoding) {
-               /* skip current locale */
-               gint index = 1;
+               gint index = 0;
 
                while (index < SUBTITLE_ENCODING_LAST) {
-                       if (strcmp (encodings[index].charset, encoding) == 0) {
-                               hildon_picker_button_set_active (HILDON_PICKER_BUTTON (button),
-                                                                index);
-                               break;
+                       if (encodings[index].charset) {
+                               if (strcmp (encodings[index].charset, encoding) == 0) {
+                                       hildon_picker_button_set_active (HILDON_PICKER_BUTTON (button),
+                                                                        index);
+                                       break;
+                               }
                        }
                        index++;
                }