2008-07-29 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / src / hildon-window.c
index 1f1fac6..bb0b79e 100644 (file)
 
 #define TITLE_SEPARATOR                         " - "
 
-static GtkWindowClass                           *parent_class;
-
 typedef void                                    (*HildonWindowSignal) (HildonWindow *, gint, gpointer);
 
 static void
@@ -175,6 +173,12 @@ hildon_window_realize                           (GtkWidget *widget);
 static void
 hildon_window_unrealize                         (GtkWidget *widget);
 
+static void
+hildon_window_map                               (GtkWidget *widget);
+
+static void
+hildon_window_unmap                             (GtkWidget *widget);
+
 static gboolean
 hildon_window_key_press_event                   (GtkWidget *widget,
                                                  GdkEventKey *event);
@@ -185,6 +189,9 @@ hildon_window_key_release_event                 (GtkWidget *widget,
 static gboolean
 hildon_window_window_state_event                (GtkWidget *widget, 
                                                  GdkEventWindowState *event);
+static gboolean
+hildon_window_focus_out_event                   (GtkWidget *widget, 
+                                                 GdkEventFocus *event);
 
 static void
 hildon_window_notify                            (GObject *gobject, 
@@ -194,7 +201,14 @@ static void
 hildon_window_is_topmost_notify                 (HildonWindow *window);
 
 static gboolean
-hildon_window_toggle_menu                       (HildonWindow * self);
+hildon_window_toggle_menu                       (HildonWindow * self,
+                                                guint button,
+                                                guint32 time);
+
+static gboolean
+hildon_window_toggle_menu_real                  (HildonWindow * self,
+                                                guint button,
+                                                guint32 time);
 
 static gboolean
 hildon_window_escape_timeout                    (gpointer data);
@@ -222,6 +236,9 @@ paint_toolbar                                   (GtkWidget *widget,
                                                  GdkEventExpose * event, 
                                                  gboolean fullscreen);
 
+static void
+hildon_window_unset_program_real                (HildonWindow *self);
+
 enum
 {
     PROP_0,
@@ -235,36 +252,7 @@ enum
     MAX_WIN_MESSAGES
 };
 
-/**
- * hildon_window_get_type:
- *
- * Initializes and returns the type of a hildon window.
- *
- * @Returns: GType of #HildonWindow
- */
-GType G_GNUC_CONST
-hildon_window_get_type                          (void)
-{
-    static GType window_type = 0;
-
-    if (!window_type) {
-        static const GTypeInfo window_info = {
-            sizeof(HildonWindowClass),
-            NULL,       /* base_init */
-            NULL,       /* base_finalize */
-            (GClassInitFunc) hildon_window_class_init,
-            NULL,       /* class_finalize */
-            NULL,       /* class_data */
-            sizeof(HildonWindow),
-            0,  /* n_preallocs */
-            (GInstanceInitFunc) hildon_window_init,
-        };
-        window_type = g_type_register_static(GTK_TYPE_WINDOW,
-                "HildonWindow",
-                &window_info, 0);
-    }
-    return window_type;
-}
+G_DEFINE_TYPE (HildonWindow, hildon_window, GTK_TYPE_WINDOW);
 
 static void 
 hildon_window_class_init                        (HildonWindowClass * window_class)
@@ -274,9 +262,6 @@ hildon_window_class_init                        (HildonWindowClass * window_clas
     GObjectClass *object_class          = G_OBJECT_CLASS (window_class);
     GtkContainerClass *container_class  = GTK_CONTAINER_CLASS (window_class);
 
-    /* Set the global parent_class here */
-    parent_class = g_type_class_peek_parent (window_class);
-
     object_class->get_property          = hildon_window_get_property;
     object_class->notify                = hildon_window_notify;
     widget_class->size_allocate         = hildon_window_size_allocate;
@@ -288,6 +273,9 @@ hildon_window_class_init                        (HildonWindowClass * window_clas
     widget_class->key_press_event       = hildon_window_key_press_event;
     widget_class->key_release_event     = hildon_window_key_release_event;
     widget_class->window_state_event    = hildon_window_window_state_event;
+    widget_class->focus_out_event       = hildon_window_focus_out_event;
+    widget_class->map                   = hildon_window_map;
+    widget_class->unmap                 = hildon_window_unmap;
 
     /* now the object stuff */
     object_class->finalize              = hildon_window_finalize;
@@ -295,6 +283,10 @@ hildon_window_class_init                        (HildonWindowClass * window_clas
     /* To the container */
     container_class->forall             = hildon_window_forall;
 
+    /* To this class */
+    window_class->unset_program         = hildon_window_unset_program_real;
+    window_class->toggle_menu           = hildon_window_toggle_menu_real;
+
     /* gtkobject stuff*/
     GTK_OBJECT_CLASS (window_class)->destroy = hildon_window_destroy; 
 
@@ -380,11 +372,19 @@ hildon_window_finalize                          (GObject * obj_self)
     
     self = HILDON_WINDOW (obj_self);
 
-    g_free (priv->borders);
-    g_free (priv->toolbar_borders);
+    if (priv->escape_timeout) {
+      g_source_remove (priv->escape_timeout);
+      priv->escape_timeout = 0;
+    }
+
+    if (priv->borders)
+        gtk_border_free (priv->borders);
+
+    if (priv->toolbar_borders)
+        gtk_border_free (priv->toolbar_borders);
 
-    if (G_OBJECT_CLASS (parent_class)->finalize)
-        G_OBJECT_CLASS (parent_class)->finalize (obj_self);
+    if (G_OBJECT_CLASS (hildon_window_parent_class)->finalize)
+        G_OBJECT_CLASS (hildon_window_parent_class)->finalize (obj_self);
 
 }
 
@@ -398,7 +398,7 @@ hildon_window_realize                           (GtkWidget *widget)
     Window active_window;
     HildonWindowPrivate *priv;
 
-    GTK_WIDGET_CLASS (parent_class)->realize (widget);
+    GTK_WIDGET_CLASS (hildon_window_parent_class)->realize (widget);
 
     priv = HILDON_WINDOW_GET_PRIVATE (widget);
     g_assert (priv != NULL);
@@ -453,7 +453,32 @@ hildon_window_unrealize                         (GtkWidget *widget)
             widget);
 
     gtk_widget_unrealize (GTK_WIDGET (priv->vbox));
-    GTK_WIDGET_CLASS(parent_class)->unrealize(widget);
+    GTK_WIDGET_CLASS(hildon_window_parent_class)->unrealize(widget);
+}
+
+static void
+hildon_window_map                             (GtkWidget *widget)
+{
+  HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
+  g_assert (priv != NULL);
+
+  if (GTK_WIDGET_CLASS (hildon_window_parent_class)->map)
+    GTK_WIDGET_CLASS (hildon_window_parent_class)->map (widget);
+
+  if (GTK_WIDGET_VISIBLE (priv->vbox))
+    gtk_widget_map (priv->vbox);
+}
+
+static void
+hildon_window_unmap                             (GtkWidget *widget)
+{
+  HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
+  g_assert (priv != NULL);
+
+  gtk_widget_unmap (priv->vbox);
+
+  if (GTK_WIDGET_CLASS (hildon_window_parent_class)->unmap)
+    GTK_WIDGET_CLASS (hildon_window_parent_class)->unmap (widget);
 }
 
 static void
@@ -483,29 +508,41 @@ hildon_window_get_property                      (GObject *object,
 static void
 hildon_window_get_borders                       (HildonWindow *window)
 {
+    GtkBorder zero = {0, 0, 0, 0};
     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (window);
     g_assert (priv);
 
-    g_free (priv->borders);
-    g_free (priv->toolbar_borders);
+    GtkBorder *borders = NULL;
+    GtkBorder *toolbar_borders = NULL;
 
-    gtk_widget_style_get (GTK_WIDGET (window), "borders",&priv->borders,
-            "toolbar-borders", &priv->toolbar_borders,
-            NULL);
+    if (priv->borders)
+        gtk_border_free (priv->borders);
+    if (priv->toolbar_borders)
+        gtk_border_free (priv->toolbar_borders);
 
-    if (! priv->borders)
-        priv->borders = (GtkBorder *) g_malloc0 (sizeof (GtkBorder));
+    priv->borders = NULL;
+    priv->toolbar_borders = NULL;
 
-    if (! priv->toolbar_borders)
-        priv->toolbar_borders = (GtkBorder *) g_malloc0 (sizeof (GtkBorder));
-}
+    gtk_widget_style_get (GTK_WIDGET (window), "borders",&borders,
+            "toolbar-borders", &toolbar_borders,
+            NULL);
 
-static void
-visible_toolbars                                (gpointer data, 
-                                                 gpointer user_data)
-{
-    if (GTK_WIDGET_VISIBLE (GTK_WIDGET (((GtkBoxChild *)data)->widget)))
-        (*((gint *)user_data)) ++;
+    // We're doing a copy here instead of reusing the pointer, 
+    // as we don't know where it comes from (has it been allocated using 
+    // malloc or slices... and we want to free it sanely. Blowing on 
+    // cold probbably.
+
+    if (borders) {
+        priv->borders = gtk_border_copy (borders);
+        gtk_border_free (borders);
+    } else
+        priv->borders = g_boxed_copy (GTK_TYPE_BORDER, &zero);
+
+    if (toolbar_borders) {
+        priv->toolbar_borders = gtk_border_copy (toolbar_borders);
+        gtk_border_free (toolbar_borders);
+    } else
+        priv->toolbar_borders = g_boxed_copy (GTK_TYPE_BORDER, &zero);
 }
 
 static gboolean
@@ -520,7 +557,6 @@ hildon_window_expose                            (GtkWidget *widget,
     GtkBorder *b = priv->borders;
     GtkBorder *tb = priv->toolbar_borders;
     gint tb_height = 0;
-    gint currently_visible_toolbars = 0;
 
     if (! priv->borders) {
         hildon_window_get_borders (HILDON_WINDOW (widget));
@@ -530,9 +566,6 @@ hildon_window_expose                            (GtkWidget *widget,
 
     tb_height = bx->allocation.height + tb->top + tb->bottom;
 
-    g_list_foreach (box->children, visible_toolbars, 
-            &currently_visible_toolbars);
-
     paint_toolbar (widget, box,
             event, priv->fullscreen);
 
@@ -541,7 +574,7 @@ hildon_window_expose                            (GtkWidget *widget,
         /* Draw the left and right window border */
         gint side_borders_height = widget->allocation.height - b->top;
 
-        if (currently_visible_toolbars)
+        if (priv->visible_toolbars)
             side_borders_height -= tb_height;
         else
             side_borders_height -= b->bottom;
@@ -566,7 +599,7 @@ hildon_window_expose                            (GtkWidget *widget,
         }
 
         /* If no toolbar, draw the bottom window border */
-        if (!currently_visible_toolbars && b->bottom > 0)
+        if (! priv->visible_toolbars && b->bottom > 0)
         {
             gtk_paint_box (widget->style, widget->window,
                     GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
@@ -591,10 +624,10 @@ hildon_window_expose                            (GtkWidget *widget,
 
     /* don't draw the window stuff as it overwrites our borders with a blank
      * rectangle. Instead start with the drawing of the GtkBin */
-    GTK_WIDGET_CLASS (g_type_class_peek_parent (parent_class))->expose_event (widget, event);
+    GTK_WIDGET_CLASS (g_type_class_peek_parent (hildon_window_parent_class))->expose_event (widget, event);
 
     /* FIXME Not sure why this is commented out 
-     * GTK_WIDGET_CLASS (parent_class))->
+     * GTK_WIDGET_CLASS (hildon_window_parent_class))->
      *  expose_event (widget, event); 
      */
 
@@ -734,7 +767,7 @@ hildon_window_forall                            (GtkContainer *container,
     g_return_if_fail (callback != NULL);
     g_assert (priv);
 
-    GTK_CONTAINER_CLASS (parent_class)->forall (container, include_internals,
+    GTK_CONTAINER_CLASS (hildon_window_parent_class)->forall (container, include_internals,
             callback, callback_data);
     if (include_internals && priv->vbox != NULL)
         (* callback)(GTK_WIDGET (priv->vbox), callback_data);
@@ -748,7 +781,7 @@ hildon_window_show_all                          (GtkWidget *widget)
 
     g_assert (priv != NULL);
 
-    GTK_WIDGET_CLASS (parent_class)->show_all (widget);
+    GTK_WIDGET_CLASS (hildon_window_parent_class)->show_all (widget);
     gtk_widget_show_all (priv->vbox);
 }
 
@@ -759,7 +792,7 @@ hildon_window_destroy                           (GtkObject *obj)
     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (obj);
     GList *menu_list = NULL;
     GList *menu_node = NULL;
-    
+
     g_assert (priv != NULL);
 
     if (priv->vbox != NULL)
@@ -818,10 +851,9 @@ hildon_window_destroy                           (GtkObject *obj)
 
     gtk_widget_set_events (GTK_WIDGET(obj), 0);
 
-    GTK_OBJECT_CLASS (parent_class)->destroy (obj);
+    GTK_OBJECT_CLASS (hildon_window_parent_class)->destroy (obj);
 }
 
-
 static void
 hildon_window_notify                            (GObject *gobject, 
                                                  GParamSpec *param)
@@ -838,8 +870,8 @@ hildon_window_notify                            (GObject *gobject,
         hildon_window_is_topmost_notify (window);
     }
 
-    if (G_OBJECT_CLASS(parent_class)->notify)
-        G_OBJECT_CLASS(parent_class)->notify (gobject, param);
+    if (G_OBJECT_CLASS(hildon_window_parent_class)->notify)
+        G_OBJECT_CLASS(hildon_window_parent_class)->notify (gobject, param);
 }
 
 
@@ -1074,7 +1106,7 @@ hildon_window_event_filter                      (GdkXEvent *xevent,
 
         if (xclient_message_type_check (cm, "_MB_GRAB_TRANSFER"))
         {
-            hildon_window_toggle_menu (HILDON_WINDOW ( data ));
+            hildon_window_toggle_menu (HILDON_WINDOW ( data ), cm->data.l[2], cm->data.l[0]);
             return GDK_FILTER_REMOVE;
         }
         /* opera hack clipboard client message */
@@ -1145,7 +1177,7 @@ hildon_window_key_press_event                   (GtkWidget *widget,
     switch (event->keyval)
     {
         case HILDON_HARDKEY_MENU:
-            if (hildon_window_toggle_menu (HILDON_WINDOW (widget)))
+            if (hildon_window_toggle_menu (HILDON_WINDOW (widget), 0, GDK_CURRENT_TIME))
                 return TRUE;
             break;
         case HILDON_HARDKEY_ESC:
@@ -1158,7 +1190,7 @@ hildon_window_key_press_event                   (GtkWidget *widget,
             break;
     }
 
-    return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
+    return GTK_WIDGET_CLASS (hildon_window_parent_class)->key_press_event (widget, event);
 }
 
 static gboolean
@@ -1181,7 +1213,7 @@ hildon_window_key_release_event                 (GtkWidget *widget,
             break;
     }
 
-    return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
+    return GTK_WIDGET_CLASS (hildon_window_parent_class)->key_release_event (widget, event);
 
 }
 
@@ -1199,9 +1231,9 @@ hildon_window_window_state_event                (GtkWidget *widget,
     if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
         priv->fullscreen = event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN;
 
-    if (GTK_WIDGET_CLASS (parent_class)->window_state_event)
+    if (GTK_WIDGET_CLASS (hildon_window_parent_class)->window_state_event)
     {
-        return GTK_WIDGET_CLASS (parent_class)->window_state_event (
+        return GTK_WIDGET_CLASS (hildon_window_parent_class)->window_state_event (
                 widget,
                 event);
     }
@@ -1212,6 +1244,25 @@ hildon_window_window_state_event                (GtkWidget *widget,
 }
 
 /*
+ * If the window lost focus while the user started to press the ESC key, we
+ * won't get the release event. We need to stop the timeout.
+ */
+static gboolean
+hildon_window_focus_out_event                   (GtkWidget *widget, 
+                                                 GdkEventFocus *event)
+{
+  HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (widget);
+
+  if (priv->escape_timeout)
+  {
+      g_source_remove (priv->escape_timeout);
+      priv->escape_timeout = 0;
+  }
+
+  return GTK_WIDGET_CLASS (hildon_window_parent_class)->focus_out_event (widget, event);
+}
+
+/*
  * The menu popuping needs a menu popup-function
  */
 static void
@@ -1233,7 +1284,13 @@ hildon_window_menu_popup_func                   (GtkMenu *menu,
     gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
             "vertical-offset", y, NULL);
 
-    *x += window_x;
+    if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
+    {
+        *x = GTK_WIDGET (widget)->allocation.width + window_x - GTK_WIDGET (menu)->allocation.width - *x;
+    }
+    else
+        *x += window_x;
+
     *y += window_y;
 
 }
@@ -1248,7 +1305,11 @@ hildon_window_menu_popup_func_full              (GtkMenu *menu,
     gtk_widget_style_get (GTK_WIDGET (menu), "horizontal-offset", x,
             "vertical-offset", y, NULL);
 
-    *x = MAX (0, *x);
+    if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
+        *x = GTK_WIDGET (widget)->allocation.width - GTK_WIDGET (menu)->allocation.width - *x;
+    else
+        *x = MAX (0, *x);
+
     *y = MAX (0, *y);
 }
 
@@ -1267,18 +1328,6 @@ hildon_window_is_topmost_notify                 (HildonWindow *window)
     {
         hildon_window_take_common_toolbar (window);
     }
-
-    else
-    {
-        /* If the window lost focus while the user started to press
-         * the ESC key, we won't get the release event. We need to
-         * stop the timeout*/
-        if (priv->escape_timeout)
-        {
-            g_source_remove (priv->escape_timeout);
-            priv->escape_timeout = 0;
-        }
-    }
 }
 
 /*
@@ -1311,10 +1360,10 @@ hildon_window_set_program                       (HildonWindow *self,
 
 /*
  * Unsets the program to which the window belongs. This should only be called
- * by hildon_program_add_window
+ * by hildon_program_remove_window
  */
-void G_GNUC_INTERNAL
-hildon_window_unset_program                     (HildonWindow *self)
+static void
+hildon_window_unset_program_real                (HildonWindow *self)
 {
     HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
 
@@ -1338,6 +1387,15 @@ hildon_window_unset_program                     (HildonWindow *self)
     priv->program = NULL;
 }
 
+void G_GNUC_INTERNAL
+hildon_window_unset_program                     (HildonWindow *self)
+{
+    g_return_if_fail (HILDON_IS_WINDOW (self));
+
+    if (HILDON_WINDOW_GET_CLASS (self)->unset_program != NULL)
+        HILDON_WINDOW_GET_CLASS (self)->unset_program (self);
+}
+
 /*
  * Sets whether or not the program to which this window belongs is
  * killable. This is used by the HildonProgram to signify to the
@@ -1503,13 +1561,33 @@ detach_menu_func                                (GtkWidget *attach_widget,
     /* FIXME Why is this even needed here? */
 }
 
+static gboolean
+hildon_window_toggle_menu                       (HildonWindow *self,
+                                                guint button,
+                                                guint32 time)
+{
+    g_return_val_if_fail (HILDON_IS_WINDOW (self), FALSE);
+
+    if (HILDON_WINDOW_GET_CLASS (self)->toggle_menu != NULL)
+    {
+        return HILDON_WINDOW_GET_CLASS (self)->toggle_menu (self, button, time);
+    }
+    else
+    {
+        return FALSE;
+    }
+}
+
+
 /*
  * Toggles the display of the HildonWindow menu.
  * Returns whether or not something was done (whether or not we had a menu
  * to toggle)
  */
 static gboolean
-hildon_window_toggle_menu                       (HildonWindow * self)
+hildon_window_toggle_menu_real                  (HildonWindow * self,
+                                                guint button,
+                                                guint32 time)
 {
     GtkMenu *menu_to_use = NULL;
     GList *menu_children = NULL;
@@ -1571,16 +1649,14 @@ hildon_window_toggle_menu                       (HildonWindow * self)
             gtk_menu_popup (menu_to_use, NULL, NULL,
                     (GtkMenuPositionFunc)
                     hildon_window_menu_popup_func_full,
-                    self, 0, 
-                    gtk_get_current_event_time ());
+                    self, button, time);
         }
         else
         {
             gtk_menu_popup (menu_to_use, NULL, NULL,
                     (GtkMenuPositionFunc)
                     hildon_window_menu_popup_func,
-                    self, 0, 
-                    gtk_get_current_event_time ());
+                    self, button, time);
         }
         gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_to_use), TRUE);
         return TRUE;
@@ -1671,6 +1747,34 @@ hildon_window_add_with_scrollbar                (HildonWindow *self,
     gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (scrolledw));
 }
 
+static void
+calculate_visible_toolbars                      (gpointer data,
+                                                 gpointer user_data)
+{
+  if (GTK_WIDGET_VISIBLE (GTK_WIDGET (((GtkBoxChild *)data)->widget)))
+    (*((gint *)user_data)) ++;
+}
+
+static void
+toolbar_visible_notify                          (GtkWidget *toolbar, GParamSpec *pspec,
+                                                 HildonWindow *window)
+{
+  HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (window);
+
+  g_assert (priv);
+
+  /* Recalculate from scratch the value just in case */
+  priv->visible_toolbars = 0;
+
+  g_list_foreach (GTK_BOX (priv->vbox)->children, calculate_visible_toolbars, 
+                  &priv->visible_toolbars);
+
+  if (priv->visible_toolbars == 0)
+    gtk_widget_hide (priv->vbox);
+  else
+    gtk_widget_show (priv->vbox);
+}
+
 /**
  * hildon_window_add_toolbar:
  * @self: A @HildonWindow
@@ -1679,26 +1783,36 @@ hildon_window_add_with_scrollbar                (HildonWindow *self,
  * Adds a toolbar to the window. Note that the toolbar is not automatically
  * shown. You need to call #gtk_widget_show_all on it to make it visible. 
  * It's also possible to hide the toolbar (without removing it) by calling
- * #gtk_widget_hide_all.
+ * #gtk_widget_hide.
  **/
 void 
 hildon_window_add_toolbar                       (HildonWindow *self, 
                                                  GtkToolbar *toolbar)
 {
     GtkBox *vbox;
-    HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
+    HildonWindowPrivate *priv;
 
     g_return_if_fail (HILDON_IS_WINDOW (self));
     g_return_if_fail (toolbar && GTK_IS_TOOLBAR (toolbar));
-    g_assert (priv);
+
+    priv = HILDON_WINDOW_GET_PRIVATE (self);
 
     vbox = GTK_BOX (priv->vbox);
 
-    gtk_box_pack_start (vbox, GTK_WIDGET(toolbar), TRUE, TRUE, 0);
-    gtk_box_reorder_child (vbox, GTK_WIDGET(toolbar), 0);
+    gtk_box_pack_start (vbox, GTK_WIDGET (toolbar), TRUE, TRUE, 0);
+    gtk_box_reorder_child (vbox, GTK_WIDGET (toolbar), 0);
     gtk_widget_set_size_request (GTK_WIDGET (toolbar), -1, TOOLBAR_HEIGHT);
 
-    gtk_widget_queue_resize (GTK_WIDGET(self));
+    g_signal_connect (G_OBJECT (toolbar), "notify::visible",
+                      G_CALLBACK (toolbar_visible_notify), self);
+
+    if (GTK_WIDGET_VISIBLE (toolbar))
+      {
+        priv->visible_toolbars++;
+        gtk_widget_show (priv->vbox);
+      }
+
+    gtk_widget_queue_resize (GTK_WIDGET (self));
 }
 
 /**
@@ -1714,12 +1828,21 @@ void
 hildon_window_remove_toolbar                    (HildonWindow *self, 
                                                  GtkToolbar *toolbar)
 {
-    HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
-    
+    HildonWindowPrivate *priv;
+
     g_return_if_fail (HILDON_IS_WINDOW (self));
-    g_assert (priv);
     
-    gtk_container_remove (GTK_CONTAINER (priv->vbox), GTK_WIDGET(toolbar));
+    priv = HILDON_WINDOW_GET_PRIVATE (self);
+
+    if (GTK_WIDGET_VISIBLE (toolbar))
+      {
+        if (--(priv->visible_toolbars) == 0)
+          gtk_widget_hide (priv->vbox);
+      }
+
+    g_signal_handlers_disconnect_by_func (toolbar, toolbar_visible_notify, self);
+
+    gtk_container_remove (GTK_CONTAINER (priv->vbox), GTK_WIDGET (toolbar));
 }
 
 /**
@@ -1734,49 +1857,115 @@ hildon_window_remove_toolbar                    (HildonWindow *self,
 GtkMenu*
 hildon_window_get_menu                          (HildonWindow * self)
 {
-    HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
-    
+    HildonWindowPrivate *priv;
+
     g_return_val_if_fail (HILDON_IS_WINDOW (self), NULL);
-    g_assert (priv);
+
+    priv = HILDON_WINDOW_GET_PRIVATE (self);
 
     return GTK_MENU (priv->menu);
 }
 
+/* Since we've been asking developers to call gtk_window_add_accel_group()
+ * themselves, do not trigger criticals by trying it again.
+ */
+static void
+hildon_window_add_accel_group (HildonWindow *self,
+                              GtkAccelGroup *accel_group)
+{
+    GSList *groups, *l;
+
+    groups = gtk_accel_groups_from_object (G_OBJECT (self));
+    for (l = groups; l != NULL; l = l->next)
+      if (l->data == (gpointer)accel_group)
+       /* Maybe print a warning here? */
+       return;
+
+    gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
+}
+
 /**
- * hildon_window_set_menu:
+ * hildon_window_set_main_menu:
  * @self: A #HildonWindow
  * @menu: The #GtkMenu to be used for this #HildonWindow
  * 
  * Sets the menu to be used for this window. This menu overrides
  * a program-wide menu that may have been set with
- * hildon_program_set_common_menu. Pass NULL to remove the current
- * menu. HildonWindow takes ownership of the passed menu and you're
+ * hildon_program_set_common_menu(). Pass %NULL to remove the current
+ * menu. #HildonWindow takes ownership of the passed menu and you're
  * not supposed to free it yourself anymore.
+ *
+ * Since: Hildon 2.2
  **/ 
 void
-hildon_window_set_menu                          (HildonWindow *self, 
-                                                 GtkMenu *menu)
+hildon_window_set_main_menu (HildonWindow* self,
+                            GtkMenu     * menu)
 {
-    HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
+    HildonWindowPrivate *priv;
+    GtkAccelGroup *accel_group;
 
     g_return_if_fail (HILDON_IS_WINDOW (self));
-    g_assert (priv);
 
-    if (priv->menu != NULL) {
+    priv = HILDON_WINDOW_GET_PRIVATE (self);
+
+    if (priv->menu != NULL)
+    {
+       accel_group = gtk_menu_get_accel_group (GTK_MENU (priv->menu));
+       if (accel_group != NULL)
+           gtk_window_remove_accel_group (GTK_WINDOW (self), accel_group);
+
         gtk_menu_detach (GTK_MENU (priv->menu));
         g_object_unref (priv->menu);
     }
 
     priv->menu = (menu != NULL) ? GTK_WIDGET (menu) : NULL;
-    if (priv->menu != NULL) {
+    if (priv->menu != NULL)
+    {
         gtk_widget_set_name (priv->menu, "menu_force_with_corners");
         gtk_menu_attach_to_widget (GTK_MENU (priv->menu), GTK_WIDGET (self), &detach_menu_func);
         g_object_ref (GTK_MENU (priv->menu));
-        gtk_widget_show_all (GTK_WIDGET (priv->menu));
+
+       accel_group = gtk_menu_get_accel_group (GTK_MENU (priv->menu));
+       if (accel_group != NULL)
+           hildon_window_add_accel_group (self, accel_group);
     }
 }
 
 /**
+ * hildon_window_set_menu:
+ * @self: A #HildonWindow
+ * @menu: The #GtkMenu to be used for this #HildonWindow
+ * 
+ * Sets the menu to be used for this window. This menu overrides
+ * a program-wide menu that may have been set with
+ * hildon_program_set_common_menu. Pass NULL to remove the current
+ * menu. HildonWindow takes ownership of the passed menu and you're
+ * not supposed to free it yourself anymore.
+ *
+ * Note: hildon_window_set_menu() calls gtk_widget_show_all() for the
+ * #GtkMenu. To pass control about visibility to the application
+ * developer, hildon_window_set_main_menu() was introduced, which
+ * doesn't do this.
+ *
+ * Deprecated: Hildon 2.2: use hildon_window_set_main_menu()
+ **/ 
+void
+hildon_window_set_menu                          (HildonWindow *self, 
+                                                 GtkMenu *menu)
+{
+    HildonWindowPrivate *priv;
+
+    g_return_if_fail (HILDON_IS_WINDOW (self));
+
+    hildon_window_set_main_menu (self, menu);
+
+    priv = HILDON_WINDOW_GET_PRIVATE (self);
+
+    if (priv->menu != NULL)
+        gtk_widget_show_all (GTK_WIDGET (priv->menu));
+}
+
+/**
  * hildon_window_get_is_topmost:
  * @self: A #HildonWindow
  * 
@@ -1786,11 +1975,11 @@ hildon_window_set_menu                          (HildonWindow *self,
 gboolean
 hildon_window_get_is_topmost                    (HildonWindow *self)
 {
-    HildonWindowPrivate *priv = HILDON_WINDOW_GET_PRIVATE (self);
+    HildonWindowPrivate *priv;
 
     g_return_val_if_fail (HILDON_IS_WINDOW (self), FALSE);
-    g_assert (priv);
 
+    priv = HILDON_WINDOW_GET_PRIVATE (self);
     return priv->is_topmost;
 }