]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkmenutoolbutton.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkmenutoolbutton.c
index f1443c5d984fa57e2a7db6373b438ba923b003f7..c133f88a7b5bc10e63dfa8fe5a754719516b3b5a 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "config.h"
 
-#undef GTK_DISABLE_DEPRECATED /* GtkTooltips */
-
 #include "gtkmenutoolbutton.h"
+
 #include "gtktogglebutton.h"
 #include "gtkarrow.h"
-#include "gtkhbox.h"
-#include "gtkvbox.h"
+#include "gtkmenubutton.h"
+#include "gtkmenubuttonprivate.h"
+#include "gtkbox.h"
 #include "gtkmenu.h"
 #include "gtkmain.h"
+#include "gtksizerequest.h"
+#include "gtkbuildable.h"
+
 #include "gtkprivate.h"
 #include "gtkintl.h"
-#include "gtkalias.h"
 
 
-#define GTK_MENU_TOOL_BUTTON_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GTK_TYPE_MENU_TOOL_BUTTON, GtkMenuToolButtonPrivate))
+/**
+ * SECTION:gtkmenutoolbutton
+ * @Short_description: A GtkToolItem containing a button with an additional dropdown menu
+ * @Title: GtkMenuToolButton
+ * @See_also: #GtkToolbar, #GtkToolButton
+ *
+ * A #GtkMenuToolButton is a #GtkToolItem that contains a button and
+ * a small additional button with an arrow. When clicked, the arrow
+ * button pops up a dropdown menu.
+ *
+ * Use gtk_menu_tool_button_new() to create a new
+ * #GtkMenuToolButton. Use gtk_menu_tool_button_new_from_stock() to
+ * create a new #GtkMenuToolButton containing a stock item.
+ *
+ * <refsect2 id="GtkMenuToolButton-BUILDER-UI">
+ * <title>GtkMenuToolButton as GtkBuildable</title>
+ * <para>
+ * The GtkMenuToolButton implementation of the GtkBuildable interface
+ * supports adding a menu by specifying "menu" as the "type"
+ * attribute of a &lt;child&gt; element.
+ *
+ * <example>
+ * <title>A UI definition fragment with menus</title>
+ * <programlisting><![CDATA[
+ * <object class="GtkMenuToolButton">
+ *   <child type="menu">
+ *     <object class="GtkMenu"/>
+ *   </child>
+ * </object>
+ * ]]></programlisting>
+ * </example>
+ * </para>
+ * </refsect2>
+ */
+
 
 struct _GtkMenuToolButtonPrivate
 {
   GtkWidget *button;
-  GtkWidget *arrow;
   GtkWidget *arrow_button;
   GtkWidget *box;
-  GtkMenu   *menu;
 };
 
-static void gtk_menu_tool_button_destroy    (GtkObject              *object);
-
-static int  menu_deactivate_cb              (GtkMenuShell           *menu_shell,
-                                            GtkMenuToolButton      *button);
+static void gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface   *iface);
+static void gtk_menu_tool_button_buildable_add_child      (GtkBuildable        *buildable,
+                                                          GtkBuilder          *builder,
+                                                          GObject             *child,
+                                                          const gchar         *type);
 
 enum
 {
@@ -65,41 +98,53 @@ enum
 
 static gint signals[LAST_SIGNAL];
 
-G_DEFINE_TYPE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON)
+static GtkBuildableIface *parent_buildable_iface;
+
+G_DEFINE_TYPE_WITH_CODE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON,
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
+                                                gtk_menu_tool_button_buildable_interface_init))
 
 static void
 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
 {
   GtkMenuToolButtonPrivate *priv = button->priv;
   GtkWidget *box;
+  GtkWidget *parent;
   GtkOrientation orientation;
 
   orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
 
   if (orientation == GTK_ORIENTATION_HORIZONTAL)
     {
-      box = gtk_hbox_new (FALSE, 0);
-      gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
+      box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
+      gtk_menu_button_set_direction (GTK_MENU_BUTTON (priv->arrow_button), GTK_ARROW_DOWN);
     }
   else
     {
-      box = gtk_vbox_new (FALSE, 0);
-      gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
+      GtkTextDirection direction;
+      GtkArrowType type;
+
+      box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
+      direction = gtk_widget_get_direction (GTK_WIDGET (button));
+      type = (direction == GTK_TEXT_DIR_LTR ? GTK_ARROW_RIGHT : GTK_ARROW_LEFT);
+      gtk_menu_button_set_direction (GTK_MENU_BUTTON (priv->arrow_button), type);
     }
 
-  if (priv->button && priv->button->parent)
+  parent = gtk_widget_get_parent (priv->button);
+  if (priv->button && parent)
     {
       g_object_ref (priv->button);
-      gtk_container_remove (GTK_CONTAINER (priv->button->parent),
+      gtk_container_remove (GTK_CONTAINER (parent),
                             priv->button);
       gtk_container_add (GTK_CONTAINER (box), priv->button);
       g_object_unref (priv->button);
     }
 
-  if (priv->arrow_button && priv->arrow_button->parent)
+  parent = gtk_widget_get_parent (priv->arrow_button);
+  if (priv->arrow_button && parent)
     {
       g_object_ref (priv->arrow_button);
-      gtk_container_remove (GTK_CONTAINER (priv->arrow_button->parent),
+      gtk_container_remove (GTK_CONTAINER (parent),
                             priv->arrow_button);
       gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
                         FALSE, FALSE, 0);
@@ -145,19 +190,6 @@ gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
   GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
 }
 
-static void
-gtk_menu_tool_button_state_changed (GtkWidget    *widget,
-                                   GtkStateType  previous_state)
-{
-  GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
-  GtkMenuToolButtonPrivate *priv = button->priv;
-
-  if (!GTK_WIDGET_IS_SENSITIVE (widget) && priv->menu)
-    {
-      gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
-    }
-}
-
 static void
 gtk_menu_tool_button_set_property (GObject      *object,
                                    guint         prop_id,
@@ -189,7 +221,7 @@ gtk_menu_tool_button_get_property (GObject    *object,
   switch (prop_id)
     {
     case PROP_MENU:
-      g_value_set_object (value, button->priv->menu);
+      g_value_set_object (value, gtk_menu_button_get_popup (GTK_MENU_BUTTON (button->priv->arrow_button)));
       break;
 
     default:
@@ -202,21 +234,29 @@ static void
 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
 {
   GObjectClass *object_class;
-  GtkObjectClass *gtk_object_class;
-  GtkWidgetClass *widget_class;
   GtkToolItemClass *toolitem_class;
 
   object_class = (GObjectClass *)klass;
-  gtk_object_class = (GtkObjectClass *)klass;
-  widget_class = (GtkWidgetClass *)klass;
   toolitem_class = (GtkToolItemClass *)klass;
 
   object_class->set_property = gtk_menu_tool_button_set_property;
   object_class->get_property = gtk_menu_tool_button_get_property;
-  gtk_object_class->destroy = gtk_menu_tool_button_destroy;
-  widget_class->state_changed = gtk_menu_tool_button_state_changed;
+
   toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
 
+  /**
+   * GtkMenuToolButton::show-menu:
+   * @button: the object on which the signal is emitted
+   *
+   * The ::show-menu signal is emitted before the menu is shown.
+   *
+   * It can be used to populate the menu on demand, using 
+   * gtk_menu_tool_button_set_menu().
+
+   * Note that even if you populate the menu dynamically in this way, 
+   * you must set an empty menu on the #GtkMenuToolButton beforehand,
+   * since the arrow is made insensitive if the menu is not set.
+   */
   signals[SHOW_MENU] =
     g_signal_new (I_("show-menu"),
                   G_OBJECT_CLASS_TYPE (klass),
@@ -237,150 +277,28 @@ gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
   g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
 }
 
-static void
-menu_position_func (GtkMenu           *menu,
-                    int               *x,
-                    int               *y,
-                    gboolean          *push_in,
-                    GtkMenuToolButton *button)
-{
-  GtkMenuToolButtonPrivate *priv = button->priv;
-  GtkWidget *widget = GTK_WIDGET (button);
-  GtkRequisition req;
-  GtkRequisition menu_req;
-  GtkOrientation orientation;
-  GtkTextDirection direction;
-  GdkRectangle monitor;
-  gint monitor_num;
-  GdkScreen *screen;
-
-  gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_req);
-
-  orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
-  direction = gtk_widget_get_direction (widget);
-
-  screen = gtk_widget_get_screen (GTK_WIDGET (menu));
-  monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
-  if (monitor_num < 0)
-    monitor_num = 0;
-  gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
-
-  if (orientation == GTK_ORIENTATION_HORIZONTAL)
-    {
-      gdk_window_get_origin (widget->window, x, y);
-      *x += widget->allocation.x;
-      *y += widget->allocation.y;
-
-      if (direction == GTK_TEXT_DIR_LTR)
-       *x += MAX (widget->allocation.width - menu_req.width, 0);
-      else if (menu_req.width > widget->allocation.width)
-        *x -= menu_req.width - widget->allocation.width;
-
-      if ((*y + priv->arrow_button->allocation.height + menu_req.height) <= monitor.y + monitor.height)
-       *y += priv->arrow_button->allocation.height;
-      else if ((*y - menu_req.height) >= monitor.y)
-       *y -= menu_req.height;
-      else if (monitor.y + monitor.height - (*y + priv->arrow_button->allocation.height) > *y)
-       *y += priv->arrow_button->allocation.height;
-      else
-       *y -= menu_req.height;
-    }
-  else 
-    {
-      gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
-      gtk_widget_size_request (priv->arrow_button, &req);
-
-      if (direction == GTK_TEXT_DIR_LTR)
-        *x += priv->arrow_button->allocation.width;
-      else 
-        *x -= menu_req.width;
-
-      if (*y + menu_req.height > monitor.y + monitor.height &&
-         *y + priv->arrow_button->allocation.height - monitor.y > monitor.y + monitor.height - *y)
-       *y += priv->arrow_button->allocation.height - menu_req.height;
-    }
-
-  *push_in = FALSE;
-}
-
-static void
-popup_menu_under_arrow (GtkMenuToolButton *button,
-                        GdkEventButton    *event)
-{
-  GtkMenuToolButtonPrivate *priv = button->priv;
-
-  g_signal_emit (button, signals[SHOW_MENU], 0);
-
-  if (!priv->menu)
-    return;
-
-  gtk_menu_popup (priv->menu, NULL, NULL,
-                  (GtkMenuPositionFunc) menu_position_func,
-                  button,
-                  event ? event->button : 0,
-                  event ? event->time : gtk_get_current_event_time ());
-}
-
-static void
-arrow_button_toggled_cb (GtkToggleButton   *togglebutton,
-                         GtkMenuToolButton *button)
-{
-  GtkMenuToolButtonPrivate *priv = button->priv;
-
-  if (!priv->menu)
-    return;
-
-  if (gtk_toggle_button_get_active (togglebutton) &&
-      !GTK_WIDGET_VISIBLE (priv->menu))
-    {
-      /* we get here only when the menu is activated by a key
-       * press, so that we can select the first menu item */
-      popup_menu_under_arrow (button, NULL);
-      gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
-    }
-}
-
-static gboolean
-arrow_button_button_press_event_cb (GtkWidget         *widget,
-                                    GdkEventButton    *event,
-                                    GtkMenuToolButton *button)
-{
-  if (event->button == 1)
-    {
-      popup_menu_under_arrow (button, event);
-      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
-
-      return TRUE;
-    }
-  else
-    {
-      return FALSE;
-    }
-}
-
 static void
 gtk_menu_tool_button_init (GtkMenuToolButton *button)
 {
   GtkWidget *box;
-  GtkWidget *arrow;
   GtkWidget *arrow_button;
   GtkWidget *real_button;
 
-  button->priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
+  button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
+                                              GTK_TYPE_MENU_TOOL_BUTTON,
+                                              GtkMenuToolButtonPrivate);
 
   gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
 
-  box = gtk_hbox_new (FALSE, 0);
+  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
 
-  real_button = GTK_BIN (button)->child;
+  real_button = gtk_bin_get_child (GTK_BIN (button));
   g_object_ref (real_button);
   gtk_container_remove (GTK_CONTAINER (button), real_button);
   gtk_container_add (GTK_CONTAINER (box), real_button);
   g_object_unref (real_button);
 
-  arrow_button = gtk_toggle_button_new ();
-  arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
-  gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
+  arrow_button = gtk_menu_button_new ();
   gtk_box_pack_end (GTK_BOX (box), arrow_button,
                     FALSE, FALSE, 0);
 
@@ -390,47 +308,38 @@ gtk_menu_tool_button_init (GtkMenuToolButton *button)
   gtk_widget_show_all (box);
 
   gtk_container_add (GTK_CONTAINER (button), box);
+  gtk_menu_button_set_align_widget (GTK_MENU_BUTTON (arrow_button),
+                                    GTK_WIDGET (button));
 
   button->priv->button = real_button;
-  button->priv->arrow = arrow;
   button->priv->arrow_button = arrow_button;
   button->priv->box = box;
-
-  g_signal_connect (arrow_button, "toggled",
-                   G_CALLBACK (arrow_button_toggled_cb), button);
-  g_signal_connect (arrow_button, "button-press-event",
-                   G_CALLBACK (arrow_button_button_press_event_cb), button);
 }
 
 static void
-gtk_menu_tool_button_destroy (GtkObject *object)
+gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
+                                         GtkBuilder   *builder,
+                                         GObject      *child,
+                                         const gchar  *type)
 {
-  GtkMenuToolButton *button;
-
-  button = GTK_MENU_TOOL_BUTTON (object);
-
-  if (button->priv->menu)
-    {
-      g_signal_handlers_disconnect_by_func (button->priv->menu, 
-                                           menu_deactivate_cb, 
-                                           button);
-      gtk_menu_detach (button->priv->menu);
-
-      g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
-                                           arrow_button_toggled_cb, 
-                                           button);
-      g_signal_handlers_disconnect_by_func (button->priv->arrow_button, 
-                                           arrow_button_button_press_event_cb, 
-                                           button);
-    }
+  if (type && strcmp (type, "menu") == 0)
+    gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (buildable),
+                                   GTK_WIDGET (child));
+  else
+    parent_buildable_iface->add_child (buildable, builder, child, type);
+}
 
-  GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy (object);
+static void
+gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface)
+{
+  parent_buildable_iface = g_type_interface_peek_parent (iface);
+  iface->add_child = gtk_menu_tool_button_buildable_add_child;
 }
 
 /**
  * gtk_menu_tool_button_new:
- * @icon_widget: a widget that will be used as icon widget, or %NULL
- * @label: a string that will be used as label, or %NULL
+ * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
+ * @label: (allow-none): a string that will be used as label, or %NULL
  *
  * Creates a new #GtkMenuToolButton using @icon_widget as icon and
  * @label as label.
@@ -482,30 +391,11 @@ gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
   return GTK_TOOL_ITEM (button);
 }
 
-/* Callback for the "deactivate" signal on the pop-up menu.
- * This is used so that we unset the state of the toggle button
- * when the pop-up menu disappears. 
- */
-static int
-menu_deactivate_cb (GtkMenuShell      *menu_shell,
-                   GtkMenuToolButton *button)
-{
-  GtkMenuToolButtonPrivate *priv = button->priv;
-
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
-
-  return TRUE;
-}
-
 static void
-menu_detacher (GtkWidget *widget,
-               GtkMenu   *menu)
+_show_menu_emit (gpointer user_data)
 {
-  GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
-
-  g_return_if_fail (priv->menu == menu);
-
-  priv->menu = NULL;
+  GtkMenuToolButton *button = (GtkMenuToolButton *) user_data;
+  g_signal_emit (button, signals[SHOW_MENU], 0);
 }
 
 /**
@@ -529,34 +419,10 @@ gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
 
   priv = button->priv;
 
-  if (priv->menu != GTK_MENU (menu))
-    {
-      if (priv->menu && GTK_WIDGET_VISIBLE (priv->menu))
-        gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
-
-      if (priv->menu)
-       {
-          g_signal_handlers_disconnect_by_func (priv->menu, 
-                                               menu_deactivate_cb, 
-                                               button);
-         gtk_menu_detach (priv->menu);
-       }
-
-      priv->menu = GTK_MENU (menu);
-
-      if (priv->menu)
-        {
-          gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
-                                     menu_detacher);
-
-          gtk_widget_set_sensitive (priv->arrow_button, TRUE);
-
-          g_signal_connect (priv->menu, "deactivate",
-                            G_CALLBACK (menu_deactivate_cb), button);
-        }
-      else
-       gtk_widget_set_sensitive (priv->arrow_button, FALSE);
-    }
+  _gtk_menu_button_set_popup_with_func (GTK_MENU_BUTTON (priv->arrow_button),
+                                        menu,
+                                        _show_menu_emit,
+                                        button);
 
   g_object_notify (G_OBJECT (button), "menu");
 }
@@ -567,43 +433,23 @@ gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
  *
  * Gets the #GtkMenu associated with #GtkMenuToolButton.
  *
- * Return value: the #GtkMenu associated with #GtkMenuToolButton
+ * Return value: (transfer none): the #GtkMenu associated
+ *     with #GtkMenuToolButton
  *
  * Since: 2.6
  **/
 GtkWidget *
 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
 {
-  g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
+  GtkMenu *ret;
 
-  return GTK_WIDGET (button->priv->menu);
-}
+  g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
 
-/**
- * gtk_menu_tool_button_set_arrow_tooltip:
- * @button: a #GtkMenuToolButton
- * @tooltips: the #GtkTooltips object to be used
- * @tip_text: text to be used as tooltip text for tool_item
- * @tip_private: text to be used as private tooltip text
- *
- * Sets the #GtkTooltips object to be used for arrow button which
- * pops up the menu. See gtk_tool_item_set_tooltip() for setting
- * a tooltip on the whole #GtkMenuToolButton.
- *
- * Since: 2.6
- *
- * Deprecated: 2.12: Use gtk_menu_tool_button_set_arrow_tooltip_text()
- * instead.
- **/
-void
-gtk_menu_tool_button_set_arrow_tooltip (GtkMenuToolButton *button,
-                                        GtkTooltips       *tooltips,
-                                        const gchar       *tip_text,
-                                        const gchar       *tip_private)
-{
-  g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
+  ret = gtk_menu_button_get_popup (GTK_MENU_BUTTON (button->priv->arrow_button));
+  if (!ret)
+    return NULL;
 
-  gtk_tooltips_set_tip (tooltips, button->priv->arrow_button, tip_text, tip_private);
+  return GTK_WIDGET (ret);
 }
 
 /**
@@ -612,7 +458,7 @@ gtk_menu_tool_button_set_arrow_tooltip (GtkMenuToolButton *button,
  * @text: text to be used as tooltip text for button's arrow button
  *
  * Sets the tooltip text to be used as tooltip for the arrow button which
- * pops up the menu.  See gtk_tool_item_set_tooltip() for setting a tooltip
+ * pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting a tooltip
  * on the whole #GtkMenuToolButton.
  *
  * Since: 2.12
@@ -632,7 +478,7 @@ gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
  * @markup: markup text to be used as tooltip text for button's arrow button
  *
  * Sets the tooltip markup text to be used as tooltip for the arrow button
- * which pops up the menu.  See gtk_tool_item_set_tooltip() for setting a
+ * which pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting a
  * tooltip on the whole #GtkMenuToolButton.
  *
  * Since: 2.12
@@ -645,6 +491,3 @@ gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
 
   gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);
 }
-
-#define __GTK_MENU_TOOL_BUTTON_C__
-#include "gtkaliasdef.c"