1 /* GTK - The GIMP Toolkit
3 * Copyright (C) 2003 Ricardo Fernandez Pascual
4 * Copyright (C) 2004 Paolo Borelli
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 #include "gtkmenutoolbutton.h"
24 #include "gtktogglebutton.h"
26 #include "gtkmenubutton.h"
27 #include "gtkmenubuttonprivate.h"
31 #include "gtksizerequest.h"
32 #include "gtkbuildable.h"
34 #include "gtkprivate.h"
39 * SECTION:gtkmenutoolbutton
40 * @Short_description: A GtkToolItem containing a button with an additional dropdown menu
41 * @Title: GtkMenuToolButton
42 * @See_also: #GtkToolbar, #GtkToolButton
44 * A #GtkMenuToolButton is a #GtkToolItem that contains a button and
45 * a small additional button with an arrow. When clicked, the arrow
46 * button pops up a dropdown menu.
48 * Use gtk_menu_tool_button_new() to create a new
49 * #GtkMenuToolButton. Use gtk_menu_tool_button_new_from_stock() to
50 * create a new #GtkMenuToolButton containing a stock item.
52 * <refsect2 id="GtkMenuToolButton-BUILDER-UI">
53 * <title>GtkMenuToolButton as GtkBuildable</title>
55 * The GtkMenuToolButton implementation of the GtkBuildable interface
56 * supports adding a menu by specifying "menu" as the "type"
57 * attribute of a <child> element.
60 * <title>A UI definition fragment with menus</title>
61 * <programlisting><![CDATA[
62 * <object class="GtkMenuToolButton">
64 * <object class="GtkMenu"/>
67 * ]]></programlisting>
74 struct _GtkMenuToolButtonPrivate
77 GtkWidget *arrow_button;
81 static void gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface);
82 static void gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
99 static gint signals[LAST_SIGNAL];
101 static GtkBuildableIface *parent_buildable_iface;
103 G_DEFINE_TYPE_WITH_CODE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON,
104 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
105 gtk_menu_tool_button_buildable_interface_init))
108 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
110 GtkMenuToolButtonPrivate *priv = button->priv;
113 GtkOrientation orientation;
115 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
117 if (orientation == GTK_ORIENTATION_HORIZONTAL)
119 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
120 gtk_menu_button_set_direction (GTK_MENU_BUTTON (priv->arrow_button), GTK_ARROW_DOWN);
124 GtkTextDirection direction;
127 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
128 direction = gtk_widget_get_direction (GTK_WIDGET (button));
129 type = (direction == GTK_TEXT_DIR_LTR ? GTK_ARROW_RIGHT : GTK_ARROW_LEFT);
130 gtk_menu_button_set_direction (GTK_MENU_BUTTON (priv->arrow_button), type);
133 parent = gtk_widget_get_parent (priv->button);
134 if (priv->button && parent)
136 g_object_ref (priv->button);
137 gtk_container_remove (GTK_CONTAINER (parent),
139 gtk_container_add (GTK_CONTAINER (box), priv->button);
140 g_object_unref (priv->button);
143 parent = gtk_widget_get_parent (priv->arrow_button);
144 if (priv->arrow_button && parent)
146 g_object_ref (priv->arrow_button);
147 gtk_container_remove (GTK_CONTAINER (parent),
149 gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
151 g_object_unref (priv->arrow_button);
158 /* Transfer a possible tooltip to the new box */
159 g_object_get (priv->box, "tooltip-markup", &tmp, NULL);
163 g_object_set (box, "tooltip-markup", tmp, NULL);
167 /* Note: we are not destroying the button and the arrow_button
168 * here because they were removed from their container above
170 gtk_widget_destroy (priv->box);
175 gtk_container_add (GTK_CONTAINER (button), priv->box);
176 gtk_widget_show_all (priv->box);
178 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
179 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
181 gtk_widget_queue_resize (GTK_WIDGET (button));
185 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
187 gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
190 GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
194 gtk_menu_tool_button_set_property (GObject *object,
199 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
204 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 gtk_menu_tool_button_get_property (GObject *object,
219 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
224 g_value_set_object (value, gtk_menu_button_get_popup (GTK_MENU_BUTTON (button->priv->arrow_button)));
228 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
236 GObjectClass *object_class;
237 GtkToolItemClass *toolitem_class;
239 object_class = (GObjectClass *)klass;
240 toolitem_class = (GtkToolItemClass *)klass;
242 object_class->set_property = gtk_menu_tool_button_set_property;
243 object_class->get_property = gtk_menu_tool_button_get_property;
245 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
248 * GtkMenuToolButton::show-menu:
249 * @button: the object on which the signal is emitted
251 * The ::show-menu signal is emitted before the menu is shown.
253 * It can be used to populate the menu on demand, using
254 * gtk_menu_tool_button_set_menu().
256 * Note that even if you populate the menu dynamically in this way,
257 * you must set an empty menu on the #GtkMenuToolButton beforehand,
258 * since the arrow is made insensitive if the menu is not set.
261 g_signal_new (I_("show-menu"),
262 G_OBJECT_CLASS_TYPE (klass),
264 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
266 g_cclosure_marshal_VOID__VOID,
269 g_object_class_install_property (object_class,
271 g_param_spec_object ("menu",
273 P_("The dropdown menu"),
275 GTK_PARAM_READWRITE));
277 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
281 gtk_menu_tool_button_init (GtkMenuToolButton *button)
284 GtkWidget *arrow_button;
285 GtkWidget *real_button;
287 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
288 GTK_TYPE_MENU_TOOL_BUTTON,
289 GtkMenuToolButtonPrivate);
291 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
293 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
295 real_button = gtk_bin_get_child (GTK_BIN (button));
296 g_object_ref (real_button);
297 gtk_container_remove (GTK_CONTAINER (button), real_button);
298 gtk_container_add (GTK_CONTAINER (box), real_button);
299 g_object_unref (real_button);
301 arrow_button = gtk_menu_button_new ();
302 gtk_box_pack_end (GTK_BOX (box), arrow_button,
305 /* the arrow button is insentive until we set a menu */
306 gtk_widget_set_sensitive (arrow_button, FALSE);
308 gtk_widget_show_all (box);
310 gtk_container_add (GTK_CONTAINER (button), box);
311 gtk_menu_button_set_align_widget (GTK_MENU_BUTTON (arrow_button),
312 GTK_WIDGET (button));
314 button->priv->button = real_button;
315 button->priv->arrow_button = arrow_button;
316 button->priv->box = box;
320 gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
325 if (type && strcmp (type, "menu") == 0)
326 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (buildable),
329 parent_buildable_iface->add_child (buildable, builder, child, type);
333 gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface)
335 parent_buildable_iface = g_type_interface_peek_parent (iface);
336 iface->add_child = gtk_menu_tool_button_buildable_add_child;
340 * gtk_menu_tool_button_new:
341 * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
342 * @label: (allow-none): a string that will be used as label, or %NULL
344 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
347 * Return value: the new #GtkMenuToolButton
352 gtk_menu_tool_button_new (GtkWidget *icon_widget,
355 GtkMenuToolButton *button;
357 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
360 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
363 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
365 return GTK_TOOL_ITEM (button);
369 * gtk_menu_tool_button_new_from_stock:
370 * @stock_id: the name of a stock item
372 * Creates a new #GtkMenuToolButton.
373 * The new #GtkMenuToolButton will contain an icon and label from
374 * the stock item indicated by @stock_id.
376 * Return value: the new #GtkMenuToolButton
381 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
383 GtkMenuToolButton *button;
385 g_return_val_if_fail (stock_id != NULL, NULL);
387 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
388 "stock-id", stock_id,
391 return GTK_TOOL_ITEM (button);
395 _show_menu_emit (gpointer user_data)
397 GtkMenuToolButton *button = (GtkMenuToolButton *) user_data;
398 g_signal_emit (button, signals[SHOW_MENU], 0);
402 * gtk_menu_tool_button_set_menu:
403 * @button: a #GtkMenuToolButton
404 * @menu: the #GtkMenu associated with #GtkMenuToolButton
406 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
407 * If @menu is NULL, the arrow button becomes insensitive.
412 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
415 GtkMenuToolButtonPrivate *priv;
417 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
418 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
422 _gtk_menu_button_set_popup_with_func (GTK_MENU_BUTTON (priv->arrow_button),
427 g_object_notify (G_OBJECT (button), "menu");
431 * gtk_menu_tool_button_get_menu:
432 * @button: a #GtkMenuToolButton
434 * Gets the #GtkMenu associated with #GtkMenuToolButton.
436 * Return value: (transfer none): the #GtkMenu associated
437 * with #GtkMenuToolButton
442 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
446 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
448 ret = gtk_menu_button_get_popup (GTK_MENU_BUTTON (button->priv->arrow_button));
452 return GTK_WIDGET (ret);
456 * gtk_menu_tool_button_set_arrow_tooltip_text:
457 * @button: a #GtkMenuToolButton
458 * @text: text to be used as tooltip text for button's arrow button
460 * Sets the tooltip text to be used as tooltip for the arrow button which
461 * pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a tooltip
462 * on the whole #GtkMenuToolButton.
467 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
470 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
472 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
476 * gtk_menu_tool_button_set_arrow_tooltip_markup:
477 * @button: a #GtkMenuToolButton
478 * @markup: markup text to be used as tooltip text for button's arrow button
480 * Sets the tooltip markup text to be used as tooltip for the arrow button
481 * which pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a
482 * tooltip on the whole #GtkMenuToolButton.
487 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
490 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
492 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);