2008-08-26 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-app-menu.c
index 714292f..4f5e7e5 100644 (file)
  * Besides that, the #HildonAppMenu can contain a group of filter buttons
  * (#GtkToggleButton or #GtkRadioButton).
  *
+ * To use a #HildonAppMenu, add it to a #HildonStackableWindow with
+ * with hildon_stackable_window_set_main_menu(). The menu will appear
+ * when the user presses the window title bar.
+ *
+ * Alternatively, you can show it by hand using gtk_widget_show().
+ *
+ * The menu will be automatically hidden when one of its buttons is
+ * clicked. Use g_signal_connect_after() when connecting callbacks to
+ * buttons to make sure that they're called after the menu
+ * disappears. Alternatively, you can add the button to the menu
+ * before connecting any callback.
+ *
  * <example>
  * <title>Creating a HildonAppMenu</title>
  * <programlisting>
+ * HildonStackableWindow *win;
  * HildonAppMenu *menu;
  * GtkWidget *button;
  * GtkWidget *filter;
  * <!-- -->
+ * win = HILDON_STACKABLE_WINDOW (hildon_stackable_window_new ());
  * menu = HILDON_APP_MENU (hildon_app_menu_new ());
  * <!-- -->
  * // Create a button and add it to the menu
  * button = gtk_button_new_with_label ("Menu command one");
- * g_signal_connect (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
+ * g_signal_connect_after (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
  * hildon_app_menu_append (menu, GTK_BUTTON (button));
+ * <!-- -->
  * // Another button
  * button = gtk_button_new_with_label ("Menu command two");
- * g_signal_connect (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
+ * g_signal_connect_after (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
  * hildon_app_menu_append (menu, GTK_BUTTON (button));
  * <!-- -->
  * // Create a filter and add it to the menu
  * filter = gtk_radio_button_new_with_label (NULL, "Filter one");
  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
- * g_signal_connect (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
+ * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_one_clicked), userdata);
  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
+ * <!-- -->
  * // Add a new filter
  * filter = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (filter), "Filter two");
  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
- * g_signal_connect (filter, "clicked", G_CALLBACK (filter_three_clicked), userdata);
+ * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
  * <!-- -->
- * // Show the menu
- * gtk_widget_show (menu);
+ * // Add the menu to the window
+ * hildon_stackable_window_set_main_menu (win, menu);
  * </programlisting>
  * </example>
  *
  */
 
+#include                                        <string.h>
+#include                                        <X11/Xatom.h>
+#include                                        <gdk/gdkx.h>
+
 #include                                        "hildon-app-menu.h"
 #include                                        "hildon-app-menu-private.h"
 
-#include                                        <gdk/gdkx.h>
-#include                                        <X11/X.h>
-#include                                        <X11/Xatom.h>
-#include                                        <string.h>
-
 static GdkWindow *
 grab_transfer_window_get                        (GtkWidget *widget);
 
@@ -82,9 +97,9 @@ G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
 /**
  * hildon_app_menu_new:
  *
- * Creates a new HildonAppMenu.
+ * Creates a new #HildonAppMenu.
  *
- * Return value: A @HildonAppMenu.
+ * Return value: A #HildonAppMenu.
  **/
 GtkWidget *
 hildon_app_menu_new                             (void)
@@ -94,11 +109,11 @@ hildon_app_menu_new                             (void)
 }
 
 /**
- * hildon_app_menu_append
- * @menu : A @HildonAppMenu
- * @item : A @GtkButton to add to the HildonAppMenu
+ * hildon_app_menu_append:
+ * @menu : A #HildonAppMenu
+ * @item : A #GtkButton to add to the #HildonAppMenu
  *
- * Adds the @item to the @HildonAppMenu
+ * Adds the @item to @menu.
  */
 void
 hildon_app_menu_append                          (HildonAppMenu *menu,
@@ -128,11 +143,10 @@ hildon_app_menu_append                          (HildonAppMenu *menu,
 
 /**
  * hildon_app_menu_add_filter
- * @menu : A @HildonAppMenu
- * @filter : A @GtkButton to add to the #HildonAppMenu.
- *
- * Adds the @filter to the #HildonAppMenu.
+ * @menu : A #HildonAppMenu
+ * @filter : A #GtkButton to add to the #HildonAppMenu.
  *
+ * Adds the @filter to @menu.
  */
 void
 hildon_app_menu_add_filter                      (HildonAppMenu *menu,
@@ -147,7 +161,6 @@ hildon_app_menu_add_filter                      (HildonAppMenu *menu,
 
     /* Pack the filter in the group and set its size */
     gtk_box_pack_start (GTK_BOX (priv->filters_hbox), GTK_WIDGET (filter), TRUE, TRUE, 0);
-    gtk_size_group_add_widget (priv->sizegroup, GTK_WIDGET (filter));
 
     /* Close the menu when the button is clicked */
     g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
@@ -306,35 +319,54 @@ hildon_app_menu_realize                         (GtkWidget *widget)
 }
 
 static void
-hildon_app_menu_init                            (HildonAppMenu *menu)
+hildon_app_menu_style_set                       (GtkWidget *widget,
+                                                 GtkStyle  *previous_style)
 {
-    GtkWidget *alignment;
     GdkScreen *screen;
-    int width;
-    guint horizontal_spacing, vertical_spacing,
-            inner_border, external_border;
-    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
+    gint width;
+    guint horizontal_spacing, vertical_spacing, inner_border, external_border;
+    HildonAppMenuPrivate *priv;
+
+    if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
+        GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
+
+    priv = HILDON_APP_MENU_GET_PRIVATE (widget);
 
-    gtk_widget_style_get (GTK_WIDGET (menu),
+    gtk_widget_style_get (widget,
                           "horizontal-spacing", &horizontal_spacing,
                           "vertical-spacing", &vertical_spacing,
                           "inner-border", &inner_border,
                           "external-border", &external_border,
                           NULL);
 
+    /* Set spacings */
+    gtk_table_set_row_spacings (priv->table, vertical_spacing);
+    gtk_table_set_col_spacings (priv->table, horizontal_spacing);
+    gtk_box_set_spacing (priv->vbox, vertical_spacing);
+
+    /* Set inner border */
+    gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
+
+    /* Set default size */
+    screen = gtk_widget_get_screen (widget);
+    width = gdk_screen_get_width (screen) - external_border * 2;
+    gtk_window_set_default_size (GTK_WINDOW (widget), width, -1);
+}
+
+static void
+hildon_app_menu_init                            (HildonAppMenu *menu)
+{
+    GtkWidget *alignment;
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
+
     /* Initialize private variables */
-    priv->filters_hbox = GTK_BOX (gtk_hbox_new (FALSE, 0));
-    priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, vertical_spacing));
+    priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
+    priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
     priv->table = GTK_TABLE (gtk_table_new (1, 2, TRUE));
-    priv->sizegroup = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_BOTH));
     priv->nitems = 0;
     priv->transfer_window = NULL;
     priv->pressed_outside = FALSE;
 
-    /* Set spacing between table elements */
-    gtk_table_set_row_spacings (priv->table, vertical_spacing);
-    gtk_table_set_col_spacings (priv->table, horizontal_spacing);
-
     /* Align the filters to the center */
     alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
     gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
@@ -344,14 +376,6 @@ hildon_app_menu_init                            (HildonAppMenu *menu)
     gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
     gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
 
-    /* Set default size */
-    screen = gtk_widget_get_screen (GTK_WIDGET (menu));
-    width = gdk_screen_get_width (screen) - external_border * 2;
-    gtk_window_set_default_size (GTK_WINDOW (menu), width, -1);
-
-    /* Set inner border */
-    gtk_container_set_border_width (GTK_CONTAINER (menu), inner_border);
-
     gtk_window_set_modal (GTK_WINDOW (menu), TRUE);
 
     gtk_widget_show_all (GTK_WIDGET (priv->vbox));
@@ -362,7 +386,6 @@ hildon_app_menu_finalize                        (GObject *object)
 {
     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
 
-    g_object_unref (priv->sizegroup);
     if (priv->transfer_window)
         gdk_window_destroy (priv->transfer_window);
 
@@ -382,6 +405,7 @@ hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
     widget_class->realize = hildon_app_menu_realize;
     widget_class->button_press_event = hildon_app_menu_button_press;
     widget_class->button_release_event = hildon_app_menu_button_release;
+    widget_class->style_set = hildon_app_menu_style_set;
 
     g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
 
@@ -390,7 +414,7 @@ hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
         g_param_spec_uint (
             "horizontal-spacing",
             "Horizontal spacing on menu items",
-            "Horizontal spacing between each menu item (but not filters)",
+            "Horizontal spacing between each menu item. Does not apply to filter buttons.",
             0, G_MAXUINT, 16,
             G_PARAM_READABLE));
 
@@ -399,7 +423,7 @@ hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
         g_param_spec_uint (
             "vertical-spacing",
             "Vertical spacing on menu items",
-            "Vertical spacing between each menu item (but not filters)",
+            "Vertical spacing between each menu item. Does not apply to filter buttons.",
             0, G_MAXUINT, 16,
             G_PARAM_READABLE));