2008-09-24 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / src / hildon-font-selection-dialog.c
index a675c89..33910f4 100644 (file)
@@ -8,7 +8,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
  * as published by the Free Software Foundation; version 2.1 of
- * the License.
+ * the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -25,7 +25,7 @@
 /**
  * SECTION:hildon-font-selection-dialog
  * @short_description: A widget used to allow users to select a font 
- * with certain properties
+ * with certain properties.
  *
  * Font selection can be made using this widget. Users can select font name, 
  * size, style, etc. Users can also preview text in the selected font.
 #include                                        <config.h>
 #endif
 
+#include                                        <libintl.h>
 #include                                        <stdlib.h>
 #include                                        <string.h>
-#include                                        <gtk/gtkstock.h>
-#include                                        <gtk/gtkcombobox.h>
-#include                                        <gtk/gtktogglebutton.h>
-#include                                        <gtk/gtkcheckbutton.h>
-#include                                        <gtk/gtklabel.h>
-#include                                        <gtk/gtkvbox.h>
-#include                                        <gtk/gtkliststore.h>
-#include                                        <gtk/gtknotebook.h>
-#include                                        <gtk/gtk.h>
+
 #include                                        <glib.h>
 #include                                        <gdk/gdkkeysyms.h>
+
 #include                                        "hildon-font-selection-dialog.h"
 #include                                        "hildon-caption.h"
 #include                                        "hildon-color-button.h"
-#include                                        <libintl.h>
 #include                                        "hildon-font-selection-dialog-private.h"
 
 /* These are what we use as the standard font sizes, for the size list */
@@ -170,6 +163,13 @@ static GtkDialogClass*                          parent_class = NULL;
 
 #define                                         OFF_BIT 0x02
 
+/**
+ * hildon_font_selection_dialog_get_type:
+ *
+ * Initializes and returns the type of a hildon font selection dialog
+ *
+ * @Returns: GType of #HildonFontSelectionDialog
+ */
 GType G_GNUC_CONST
 hildon_font_selection_dialog_get_type           (void)
 {
@@ -204,7 +204,7 @@ hildon_font_selection_dialog_get_property       (GObject *object,
                                                  GParamSpec *pspec)
 {
     gint i;
-    GdkColor *color = NULL;
+    GdkColor color;
 
     HildonFontSelectionDialogPrivate *priv =
         HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE(object);
@@ -248,12 +248,9 @@ hildon_font_selection_dialog_get_property       (GObject *object,
             break;
 
         case PROP_COLOR:
-            color = g_new (GdkColor, 1);
             hildon_color_button_get_color
-                (HILDON_COLOR_BUTTON (priv->font_color_button), color);
-            g_value_set_boxed (value, (gconstpointer) color);
-            if(color != NULL)
-                gdk_color_free (color);
+                (HILDON_COLOR_BUTTON (priv->font_color_button), &color);
+            g_value_set_boxed (value, (gconstpointer) &color);
             break;
 
         case PROP_COLOR_SET:
@@ -518,13 +515,24 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
     gobject_class->get_property     = hildon_font_selection_dialog_get_property;
     gobject_class->set_property     = hildon_font_selection_dialog_set_property;
 
-    /* Install property to the class */
+    /* Install properties to the class */
+
+    /**
+     * HildonFontSelectionDialog:family:
+     *
+     * Font family used.
+     */
     g_object_class_install_property (gobject_class, PROP_FAMILY,
             g_param_spec_string ("family",
                 "Font family", "String defines"
                 " the font family", "Sans",
                 G_PARAM_READWRITE));
 
+    /**
+     * HildonFontSelectionDialog:family-set:
+     *
+     * Is font family set or inconsistent.
+     */
     g_object_class_install_property (gobject_class, PROP_FAMILY_SET,
             g_param_spec_boolean ("family-set",
                 "family inconsistent state",
@@ -532,6 +540,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 " is inconsistent", FALSE,
                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
+    /**
+     * HildonFontSelectionDialog:size:
+     *
+     * Font size.
+     */
     g_object_class_install_property (gobject_class, PROP_SIZE,
             g_param_spec_int ("size",
                 "Font size",
@@ -539,6 +552,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 6, 32, 16,
                 G_PARAM_READWRITE));
 
+    /**
+     * HildonFontSelectionDialog:size-set:
+     *
+     * Is font size set or inconsistent.
+     */
     g_object_class_install_property (gobject_class, PROP_SIZE_SET,
             g_param_spec_boolean ("size-set",
                 "size inconsistent state",
@@ -546,6 +564,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 " is inconsistent", FALSE,
                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
+    /**
+     * HildonFontSelectionDialog:color:
+     *
+     * GdkColor for the text.
+     */
     g_object_class_install_property (gobject_class, PROP_COLOR,
             g_param_spec_boxed ("color",
                 "text color",
@@ -553,6 +576,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 GDK_TYPE_COLOR,
                 G_PARAM_READWRITE));
 
+    /**
+     * HildonFontSelectionDialog:color-set:
+     *
+     * Is font color set or inconsistent.
+     */
     g_object_class_install_property (gobject_class, PROP_COLOR_SET,
             g_param_spec_boolean ("color-set",
                 "color inconsistent state",
@@ -560,6 +588,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 " is inconsistent", FALSE,
                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
+    /**
+     * HildonFontSelectionDialog:color-set:
+     *
+     * Is font set as bold.
+     */
     g_object_class_install_property (gobject_class, PROP_BOLD,
             g_param_spec_boolean ("bold",
                 "text weight",
@@ -567,6 +600,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 FALSE,
                 G_PARAM_READWRITE));
 
+    /**
+     * HildonFontSelectionDialog:color-set:
+     *
+     * Is font bold status set or inconsistent.
+     */
     g_object_class_install_property (gobject_class, PROP_BOLD_SET,
             g_param_spec_boolean ("bold-set",
                 "bold inconsistent state",
@@ -574,6 +612,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 " is inconsistent", FALSE,
                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
+    /**
+     * HildonFontSelectionDialog:italic:
+     *
+     * Is font set as italic.
+     */
     g_object_class_install_property (gobject_class, PROP_ITALIC,
             g_param_spec_boolean ("italic",
                 "text style",
@@ -581,6 +624,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 FALSE,
                 G_PARAM_READWRITE));
 
+    /**
+     * HildonFontSelectionDialog:italic-set:
+     *
+     * Is font italic status set or inconsistent.
+     */
     g_object_class_install_property (gobject_class, PROP_ITALIC_SET,
             g_param_spec_boolean ("italic-set",
                 "italic inconsistent state",
@@ -588,6 +636,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 " is inconsistent", FALSE,
                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
+    /**
+     * HildonFontSelectionDialog:underline:
+     *
+     * Is the font underlined.
+     */
     g_object_class_install_property (gobject_class, PROP_UNDERLINE,
             g_param_spec_boolean ("underline",
                 "text underline",
@@ -595,6 +648,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 FALSE,
                 G_PARAM_READWRITE));
 
+    /**
+     * HildonFontSelectionDialog:underline:
+     *
+     * Is font underline status set or inconsistent.
+     */
     g_object_class_install_property (gobject_class, PROP_UNDERLINE_SET,
             g_param_spec_boolean ("underline-set",
                 "underline inconsistent state",
@@ -602,6 +660,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 " is inconsistent", FALSE,
                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
+    /**
+     * HildonFontSelectionDialog:strikethrough:
+     *
+     * Is the font striken-through.
+     */
     g_object_class_install_property (gobject_class, PROP_STRIKETHROUGH,
             g_param_spec_boolean ("strikethrough",
                 "strikethroughed text",
@@ -609,6 +672,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 FALSE,
                 G_PARAM_READWRITE));
 
+    /**
+     * HildonFontSelectionDialog:strikethrough-set:
+     *
+     * Is the font strikenthrough status set.
+     */
     g_object_class_install_property (gobject_class, PROP_STRIKETHROUGH_SET,
             g_param_spec_boolean ("strikethrough-set",
                 "strikethrough inconsistent state",
@@ -616,6 +684,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 " is inconsistent", FALSE,
                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
+    /**
+     * HildonFontSelectionDialog:position:
+     *
+     * The font positioning versus baseline.
+     */
     g_object_class_install_property (gobject_class, PROP_POSITION,
             g_param_spec_int ("position",
                 "Font position",
@@ -623,13 +696,11 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 -1, 1, 0,
                 G_PARAM_READWRITE));
 
-    g_object_class_install_property (gobject_class, PROP_FONT_SCALING,
-            g_param_spec_double ("font-scaling",
-                "Font scaling",
-                "Font scaling for the preview dialog",
-                0, 10, 1,
-                G_PARAM_READWRITE));
-
+    /**
+     * HildonFontSelectionDialog:position-set:
+     *
+     * Is the font positioning set.
+     */
     g_object_class_install_property (gobject_class, PROP_POSITION_SET,
             g_param_spec_boolean ("position-set",
                 "position inconsistent state",
@@ -637,6 +708,23 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 " is inconsistent", FALSE,
                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
+    /**
+     * HildonFontSelectionDialog:font-scaling:
+     *
+     * The font scaling factor applied to the preview dialog.
+     */
+    g_object_class_install_property (gobject_class, PROP_FONT_SCALING,
+            g_param_spec_double ("font-scaling",
+                "Font scaling",
+                "Font scaling for the preview dialog",
+                0, 10, 1,
+                G_PARAM_READWRITE));
+
+    /**
+     * HildonFontSelectionDialog:preview-text:
+     *
+     * The text used for the preview dialog.
+     */
     g_object_class_install_property (gobject_class, PROP_PREVIEW_TEXT,
             g_param_spec_string("preview-text",
                 "Preview Text", 
@@ -645,7 +733,8 @@ hildon_font_selection_dialog_class_init         (HildonFontSelectionDialogClass
                 "",
                 G_PARAM_READWRITE));
 
-    g_type_class_add_private (klass, sizeof(struct _HildonFontSelectionDialogPrivate));
+    g_type_class_add_private (klass, 
+            sizeof (struct _HildonFontSelectionDialogPrivate));
 }
 
 static void 
@@ -793,6 +882,8 @@ hildon_font_selection_dialog_construct_notebook (HildonFontSelectionDialog *font
                 priv->cbx_positioning, NULL,
                 HILDON_CAPTION_OPTIONAL);
 
+    g_object_unref (group);
+
     gtk_box_pack_start (GTK_BOX (vbox_tab[2]), caption_control, FALSE, FALSE, 0);
 
     /* Populate notebook */
@@ -833,8 +924,15 @@ hildon_font_selection_dialog_finalize           (GObject *object)
     priv = HILDON_FONT_SELECTION_DIALOG_GET_PRIVATE (fontsel);
     g_assert (priv);
 
-    g_free (priv->preview_text);
-    g_free (priv->families);
+    if (priv->preview_text != NULL) {
+        g_free (priv->preview_text);
+        priv->preview_text = NULL;
+    }
+
+    if (priv->families != NULL) {
+        g_free (priv->families);
+        priv->families = NULL;
+    }
 
     if (G_OBJECT_CLASS (parent_class)->finalize)
         G_OBJECT_CLASS (parent_class)->finalize (object);
@@ -1053,19 +1151,17 @@ hildon_font_selection_dialog_show_preview       (HildonFontSelectionDialog *font
     preview_label = gtk_label_new (str);
     gtk_label_set_line_wrap (GTK_LABEL(preview_label), TRUE);
 
-    g_free (str);
-    str = NULL;
-
-    gtk_container_add (GTK_CONTAINER (GTK_DIALOG(preview_dialog)->vbox),
-            preview_label);
+    if (str) 
+        g_free (str);
 
+    str = NULL;
 
     /* set keypress handler (ESC hardkey) */
     g_signal_connect (G_OBJECT (preview_dialog), "key-press-event",
             G_CALLBACK(hildon_font_selection_dialog_preview_key_press),
             NULL);
 
-    /*Set the font*/
+    /* Set the font */
     list = (show_ref) ? hildon_font_selection_dialog_create_attrlist (fontsel, 
             strlen (_("ecdg_fi_preview_font_preview_reference")),
             strlen (priv->preview_text)) :
@@ -1075,13 +1171,16 @@ hildon_font_selection_dialog_show_preview       (HildonFontSelectionDialog *font
             &family_set, "size", &size, "size-set", &size_set,
             NULL);
 
-    /* FIXME: This is a slightly ugly hack to force the width of the window so that
-     * the whole text fits with various font sizes. It's being done in such a way, 
-     * because of some GtkLabel wrapping issues and other mysterious bugs related to 
-     * truncating ellipsizing. Needs a rethink in future */
+    /* A smallish hack to add scrollbar when font size is really big */
 
-    gint dialog_width = (size_set && size > 24) ? 600 : 500; 
-    gtk_window_set_default_size (GTK_WINDOW (preview_dialog), dialog_width, -1);
+    if (size_set && size > 24) {
+        GtkScrolledWindow *scrolled = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
+        gtk_scrolled_window_set_policy (scrolled, GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+        gtk_scrolled_window_add_with_viewport (scrolled, GTK_WIDGET (preview_label));
+        gtk_container_add (GTK_CONTAINER (GTK_DIALOG(preview_dialog)->vbox), GTK_WIDGET (scrolled));
+        gtk_widget_set_size_request (GTK_WIDGET (scrolled), -1, 400);
+    } else 
+        gtk_container_add (GTK_CONTAINER (GTK_DIALOG(preview_dialog)->vbox), GTK_WIDGET (preview_label));
 
     /* make reference text to have the same fontface and size */
     if (family_set)
@@ -1089,9 +1188,12 @@ hildon_font_selection_dialog_show_preview       (HildonFontSelectionDialog *font
         attr = pango_attr_family_new (str);
         add_preview_text_attr (list, attr, 0, strlen (_("ecdg_fi_preview_font_preview_reference")));
     }
-    g_free (str);
+    if (str != NULL)
+        g_free (str);
 
-    /*size*/
+    str = NULL;
+
+    /* size */
     if (size_set)
     {
         attr = pango_attr_size_new (size * PANGO_SCALE);
@@ -1106,11 +1208,16 @@ hildon_font_selection_dialog_show_preview       (HildonFontSelectionDialog *font
             GTK_WINDOW (fontsel));
 
     gtk_widget_show_all (preview_dialog);
+    gtk_dialog_set_default_response (GTK_DIALOG (preview_dialog), GTK_RESPONSE_OK);
+    
+    GtkBox *action_area = (GtkBox *) GTK_DIALOG (preview_dialog)->action_area;
+    GtkWidget *button = ((GtkBoxChild *) ((GSList *) action_area->children)->data)->widget;
+    gtk_widget_grab_focus (button);
+
     gtk_dialog_run (GTK_DIALOG (preview_dialog));
     gtk_widget_destroy (preview_dialog);
 }
 
-
 static gboolean 
 is_internal_font                                (const gchar * name)
 {
@@ -1254,8 +1361,8 @@ hildon_font_selection_dialog_new                (GtkWindow *parent,
  *
  * Returns: a string pointer
  */
-gchar *
-hildon_font_selection_dialog_get_preview_text(HildonFontSelectionDialog * fsd)
+gchar*
+hildon_font_selection_dialog_get_preview_text   (HildonFontSelectionDialog * fsd)
 {
     /* FIXME Return a const pointer? */
     HildonFontSelectionDialogPrivate *priv;