]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkmenubutton.c
treeview: Don't emit cursor-changed in destruction
[~andy/gtk] / gtk / gtkmenubutton.c
index c84748269beea97d85a96d7db4c0950d61e3b206..95b2bc73b187ca0dac80335dc4e6ab83793eef00 100644 (file)
 #include "gtkmenubutton.h"
 #include "gtkmenubuttonprivate.h"
 #include "gtkarrow.h"
+#include "gtktypebuiltins.h"
+#include "gtkwindow.h"
+#include "gtkmain.h"
 
 #include "gtkprivate.h"
 #include "gtkintl.h"
@@ -173,7 +176,6 @@ enum
 
 G_DEFINE_TYPE(GtkMenuButton, gtk_menu_button, GTK_TYPE_TOGGLE_BUTTON)
 
-static void gtk_menu_button_constructed (GObject *object);
 static void gtk_menu_button_dispose (GObject *object);
 
 static void
@@ -250,6 +252,7 @@ menu_position_up_down_func (GtkMenu       *menu,
 {
   GtkMenuButtonPrivate *priv = menu_button->priv;
   GtkWidget *widget = GTK_WIDGET (menu_button);
+  GtkWidget *toplevel;
   GtkRequisition menu_req;
   GtkTextDirection direction;
   GdkRectangle monitor;
@@ -259,6 +262,16 @@ menu_position_up_down_func (GtkMenu       *menu,
   GtkAllocation allocation, arrow_allocation;
   GtkAlign align;
 
+  /* In the common case the menu button is showing a dropdown menu, set the
+   * corresponding type hint on the toplevel, so the WM can omit the top side
+   * of the shadows.
+   */
+  if (priv->arrow_type == GTK_ARROW_DOWN)
+    {
+      toplevel = gtk_widget_get_toplevel (GTK_WIDGET (priv->popup));
+      gtk_window_set_type_hint (GTK_WINDOW (toplevel), GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU);
+    }
+
   gtk_widget_get_preferred_size (GTK_WIDGET (priv->popup),
                                  &menu_req, NULL);
 
@@ -326,12 +339,14 @@ menu_position_side_func (GtkMenu       *menu,
   GdkScreen *screen;
   GdkWindow *window;
   GtkAlign align;
+  GtkTextDirection direction;
 
   gtk_widget_get_preferred_size (GTK_WIDGET (priv->popup),
                                  &menu_req, NULL);
 
   window = gtk_widget_get_window (widget);
 
+  direction = gtk_widget_get_direction (widget);
   align = gtk_widget_get_valign (GTK_WIDGET (menu));
   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
   monitor_num = gdk_screen_get_monitor_at_window (screen, window);
@@ -343,7 +358,9 @@ menu_position_side_func (GtkMenu       *menu,
 
   gtk_widget_get_allocation (widget, &allocation);
 
-  if (priv->arrow_type == GTK_ARROW_RIGHT)
+  if ((priv->arrow_type == GTK_ARROW_RIGHT && direction == GTK_TEXT_DIR_LTR) ||
+      (priv->arrow_type == GTK_ARROW_LEFT && direction == GTK_TEXT_DIR_RTL))
+
     {
       if (*x + allocation.width + menu_req.width <= monitor.x + monitor.width)
         *x += allocation.width;
@@ -439,23 +456,50 @@ gtk_menu_button_button_press_event (GtkWidget      *widget,
   return GTK_WIDGET_CLASS (gtk_menu_button_parent_class)->button_press_event (widget, event);
 }
 
+static void
+gtk_menu_button_add (GtkContainer *container,
+                     GtkWidget    *child)
+{
+  GtkMenuButton *button = GTK_MENU_BUTTON (container);
+
+  if (button->priv->arrow_widget)
+    gtk_container_remove (container, button->priv->arrow_widget);
+
+  GTK_CONTAINER_CLASS (gtk_menu_button_parent_class)->add (container, child);
+}
+
+static void
+gtk_menu_button_remove (GtkContainer *container,
+                        GtkWidget    *child)
+{
+  GtkMenuButton *button = GTK_MENU_BUTTON (container);
+
+  if (child == button->priv->arrow_widget)
+    button->priv->arrow_widget = NULL;
+
+  GTK_CONTAINER_CLASS (gtk_menu_button_parent_class)->remove (container, child);
+}
+
 static void
 gtk_menu_button_class_init (GtkMenuButtonClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
   GtkToggleButtonClass *toggle_button_class = GTK_TOGGLE_BUTTON_CLASS (klass);
 
   g_type_class_add_private (klass, sizeof (GtkMenuButtonPrivate));
 
   gobject_class->set_property = gtk_menu_button_set_property;
   gobject_class->get_property = gtk_menu_button_get_property;
-  gobject_class->constructed = gtk_menu_button_constructed;
   gobject_class->dispose = gtk_menu_button_dispose;
 
   widget_class->state_flags_changed = gtk_menu_button_state_flags_changed;
   widget_class->button_press_event = gtk_menu_button_button_press_event;
 
+  container_class->add = gtk_menu_button_add;
+  container_class->remove = gtk_menu_button_remove;
+
   toggle_button_class->toggled = gtk_menu_button_toggled;
 
   /**
@@ -472,28 +516,13 @@ gtk_menu_button_class_init (GtkMenuButtonClass *klass)
                                                         P_("The dropdown menu."),
                                                         GTK_TYPE_MENU,
                                                         G_PARAM_READWRITE));
-   /**
-   * GtkMenuButton:menu:
-   *
-   * The #GtkMenu that will be popped up when the button is clicked.
-   * This property has been renamed to "popup".  "menu" will be
-   * removed before 3.6.0.
-   *
-   * Since: 3.6
-   */
-  g_object_class_install_property (gobject_class,
-                                   PROP_POPUP, /* [sic] */
-                                   g_param_spec_object ("menu",
-                                                        P_("menu"),
-                                                        P_("The dropdown menu."),
-                                                        GTK_TYPE_MENU,
-                                                        G_PARAM_DEPRECATED | G_PARAM_READWRITE));
+
   /**
    * GtkMenuButton:menu-model:
    *
    * The #GMenuModel from which the menu to pop up will be created.
    * See gtk_menu_button_set_menu_model() for the interaction with the
-   * #GtkMenuButton:menu property.
+   * #GtkMenuButton:popup property.
    *
    * Since: 3.6
    */
@@ -533,7 +562,7 @@ gtk_menu_button_class_init (GtkMenuButtonClass *klass)
                                                       P_("The direction the arrow should point."),
                                                       GTK_TYPE_ARROW_TYPE,
                                                       GTK_ARROW_DOWN,
-                                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+                                                      G_PARAM_READWRITE));
 }
 
 static void
@@ -556,6 +585,8 @@ gtk_menu_button_init (GtkMenuButton *menu_button)
   menu_button->priv = priv;
   priv->arrow_type = GTK_ARROW_DOWN;
 
+  add_arrow (menu_button);
+
   gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
 }
 
@@ -653,7 +684,7 @@ _gtk_menu_button_set_popup_with_func (GtkMenuButton                 *menu_button
       gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
     }
 
-  g_object_notify (G_OBJECT (menu_button), "menu");
+  g_object_notify (G_OBJECT (menu_button), "popup");
   g_object_notify (G_OBJECT (menu_button), "menu-model");
 }
 
@@ -701,31 +732,18 @@ gtk_menu_button_get_popup (GtkMenuButton *menu_button)
   return GTK_MENU (menu_button->priv->popup);
 }
 
-void
-gtk_menu_button_set_menu (GtkMenuButton *menu_button,
-                          GtkWidget     *menu)
-{
-  gtk_menu_button_set_popup (menu_button, menu);
-}
-
-GtkMenu *
-gtk_menu_button_get_menu (GtkMenuButton *menu_button)
-{
-  return gtk_menu_button_get_popup (menu_button);
-}
-
 /**
  * gtk_menu_button_set_menu_model:
  * @menu_button: a #GtkMenuButton
  * @menu_model: (allow-none): a #GMenuModel
  *
- * Sets the #GMenuModel from which the #GtkMenuButton:menu property will be
+ * Sets the #GMenuModel from which the #GtkMenuButton:popup property will be
  * filled in, or %NULL to disable the button.
  *
  * The #GtkMenu will be created with gtk_menu_new_from_model(), so actions
  * will be connected as documented there.
  *
- * If you #GtkMenuButton:menu * is already set, then its content will be lost
+ * If #GtkMenuButton:popup is already set, then its content will be lost
  * and replaced by our newly created #GtkMenu.
  *
  * Since: 3.6
@@ -773,6 +791,23 @@ gtk_menu_button_get_menu_model (GtkMenuButton *menu_button)
   return menu_button->priv->model;
 }
 
+static void
+set_align_widget_pointer (GtkMenuButton *menu_button,
+                          GtkWidget     *align_widget)
+{
+  GtkMenuButtonPrivate *priv;
+
+  priv = menu_button->priv;
+
+  if (priv->align_widget)
+    g_object_remove_weak_pointer (G_OBJECT (priv->align_widget), (gpointer *) &priv->align_widget);
+
+  priv->align_widget = align_widget;
+
+  if (align_widget)
+    g_object_add_weak_pointer (G_OBJECT (priv->align_widget), (gpointer *) &priv->align_widget);
+}
+
 /**
  * gtk_menu_button_set_align_widget:
  * @menu_button: a #GtkMenuButton
@@ -799,10 +834,7 @@ gtk_menu_button_set_align_widget (GtkMenuButton *menu_button,
   if (priv->align_widget == align_widget)
     return;
 
-  priv->align_widget = align_widget;
-
-  if (priv->align_widget)
-    g_object_add_weak_pointer (G_OBJECT (priv->align_widget), (gpointer *) &priv->align_widget);
+  set_align_widget_pointer (menu_button, align_widget);
 
   g_object_notify (G_OBJECT (menu_button), "align-widget");
 }
@@ -837,8 +869,8 @@ gtk_menu_button_get_align_widget (GtkMenuButton *menu_button)
  * If the menu when popped out would have collided with screen edges,
  * we will do our best to keep it inside the screen and fully visible.
  *
- * If you pass GTK_ARROW_NONE for a @direction, the menu will behave
- * as if you passed GTK_ARROW_DOWN (although you won't see any arrows).
+ * If you pass %GTK_ARROW_NONE for a @direction, the menu will behave
+ * as if you passed %GTK_ARROW_DOWN (although you won't see any arrows).
  *
  * Since: 3.6
  */
@@ -856,11 +888,8 @@ gtk_menu_button_set_direction (GtkMenuButton *menu_button,
 
   priv->arrow_type = direction;
 
-  child = gtk_bin_get_child (GTK_BIN (menu_button));
-  if (child == NULL)
-    return;
-
   /* Is it custom content? We don't change that */
+  child = gtk_bin_get_child (GTK_BIN (menu_button));
   if (priv->arrow_widget != child)
     return;
 
@@ -885,17 +914,6 @@ gtk_menu_button_get_direction (GtkMenuButton *menu_button)
   return menu_button->priv->arrow_type;
 }
 
-static void
-gtk_menu_button_constructed (GObject *object)
-{
-  GtkMenuButton *button = GTK_MENU_BUTTON (object);
-
-  if (button->priv->arrow_type != GTK_ARROW_NONE)
-    add_arrow (button);
-
-  G_OBJECT_CLASS (gtk_menu_button_parent_class)->constructed (object);
-}
-
 static void
 gtk_menu_button_dispose (GObject *object)
 {
@@ -907,8 +925,11 @@ gtk_menu_button_dispose (GObject *object)
                                             menu_deactivate_cb,
                                             object);
       gtk_menu_detach (GTK_MENU (priv->popup));
+      priv->popup = NULL;
     }
 
+  set_align_widget_pointer (GTK_MENU_BUTTON (object), NULL);
+
   g_clear_object (&priv->model);
 
   G_OBJECT_CLASS (gtk_menu_button_parent_class)->dispose (object);