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"
29 #include "gtksizerequest.h"
30 #include "gtkbuildable.h"
32 #include "gtkprivate.h"
37 * SECTION:gtkmenutoolbutton
38 * @Short_description: A GtkToolItem containing a button with an additional dropdown menu
39 * @Title: GtkMenuToolButton
40 * @See_also: #GtkToolbar, #GtkToolButton
42 * A #GtkMenuToolButton is a #GtkToolItem that contains a button and
43 * a small additional button with an arrow. When clicked, the arrow
44 * button pops up a dropdown menu.
46 * Use gtk_menu_tool_button_new() to create a new
47 * #GtkMenuToolButton. Use gtk_menu_tool_button_new_from_stock() to
48 * create a new #GtkMenuToolButton containing a stock item.
50 * <refsect2 id="GtkMenuToolButton-BUILDER-UI">
51 * <title>GtkMenuToolButton as GtkBuildable</title>
53 * The GtkMenuToolButton implementation of the GtkBuildable interface
54 * supports adding a menu by specifying "menu" as the "type"
55 * attribute of a <child> element.
58 * <title>A UI definition fragment with menus</title>
59 * <programlisting><![CDATA[
60 * <object class="GtkMenuToolButton">
62 * <object class="GtkMenu"/>
65 * ]]></programlisting>
72 struct _GtkMenuToolButtonPrivate
76 GtkWidget *arrow_button;
81 static void gtk_menu_tool_button_destroy (GtkWidget *widget);
83 static int menu_deactivate_cb (GtkMenuShell *menu_shell,
84 GtkMenuToolButton *button);
86 static void gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface);
87 static void gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
104 static gint signals[LAST_SIGNAL];
106 static GtkBuildableIface *parent_buildable_iface;
108 G_DEFINE_TYPE_WITH_CODE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON,
109 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
110 gtk_menu_tool_button_buildable_interface_init))
113 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
115 GtkMenuToolButtonPrivate *priv = button->priv;
118 GtkOrientation orientation;
120 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
122 if (orientation == GTK_ORIENTATION_HORIZONTAL)
124 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
125 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
129 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
130 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
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_state_changed (GtkWidget *widget,
195 GtkStateType previous_state)
197 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
198 GtkMenuToolButtonPrivate *priv = button->priv;
200 if (!gtk_widget_is_sensitive (widget) && priv->menu)
202 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
207 gtk_menu_tool_button_set_property (GObject *object,
212 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
217 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 gtk_menu_tool_button_get_property (GObject *object,
232 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
237 g_value_set_object (value, button->priv->menu);
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
249 GObjectClass *object_class;
250 GtkWidgetClass *widget_class;
251 GtkToolItemClass *toolitem_class;
253 object_class = (GObjectClass *)klass;
254 widget_class = (GtkWidgetClass *)klass;
255 toolitem_class = (GtkToolItemClass *)klass;
257 object_class->set_property = gtk_menu_tool_button_set_property;
258 object_class->get_property = gtk_menu_tool_button_get_property;
260 widget_class->destroy = gtk_menu_tool_button_destroy;
261 widget_class->state_changed = gtk_menu_tool_button_state_changed;
263 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
266 * GtkMenuToolButton::show-menu:
267 * @button: the object on which the signal is emitted
269 * The ::show-menu signal is emitted before the menu is shown.
271 * It can be used to populate the menu on demand, using
272 * gtk_menu_tool_button_set_menu().
274 * Note that even if you populate the menu dynamically in this way,
275 * you must set an empty menu on the #GtkMenuToolButton beforehand,
276 * since the arrow is made insensitive if the menu is not set.
279 g_signal_new (I_("show-menu"),
280 G_OBJECT_CLASS_TYPE (klass),
282 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
284 g_cclosure_marshal_VOID__VOID,
287 g_object_class_install_property (object_class,
289 g_param_spec_object ("menu",
291 P_("The dropdown menu"),
293 GTK_PARAM_READWRITE));
295 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
299 menu_position_func (GtkMenu *menu,
303 GtkMenuToolButton *button)
305 GtkMenuToolButtonPrivate *priv = button->priv;
306 GtkAllocation arrow_allocation;
307 GtkWidget *widget = GTK_WIDGET (button);
308 GtkRequisition menu_req;
309 GtkOrientation orientation;
310 GtkTextDirection direction;
311 GdkRectangle monitor;
316 gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
319 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
320 direction = gtk_widget_get_direction (widget);
321 window = gtk_widget_get_window (widget);
323 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
324 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
327 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
329 if (orientation == GTK_ORIENTATION_HORIZONTAL)
331 GtkAllocation allocation;
333 gtk_widget_get_allocation (widget, &allocation);
334 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
336 gdk_window_get_origin (window, x, y);
340 if (direction == GTK_TEXT_DIR_LTR)
341 *x += MAX (allocation.width - menu_req.width, 0);
342 else if (menu_req.width > allocation.width)
343 *x -= menu_req.width - allocation.width;
345 if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
346 *y += arrow_allocation.height;
347 else if ((*y - menu_req.height) >= monitor.y)
348 *y -= menu_req.height;
349 else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
350 *y += arrow_allocation.height;
352 *y -= menu_req.height;
356 gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
358 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
360 if (direction == GTK_TEXT_DIR_LTR)
361 *x += arrow_allocation.width;
363 *x -= menu_req.width;
365 if (*y + menu_req.height > monitor.y + monitor.height &&
366 *y + arrow_allocation.height - monitor.y > monitor.y + monitor.height - *y)
367 *y += arrow_allocation.height - menu_req.height;
374 popup_menu_under_arrow (GtkMenuToolButton *button,
375 GdkEventButton *event)
377 GtkMenuToolButtonPrivate *priv = button->priv;
379 g_signal_emit (button, signals[SHOW_MENU], 0);
384 gtk_menu_popup (priv->menu, NULL, NULL,
385 (GtkMenuPositionFunc) menu_position_func,
387 event ? event->button : 0,
388 event ? event->time : gtk_get_current_event_time ());
392 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
393 GtkMenuToolButton *button)
395 GtkMenuToolButtonPrivate *priv = button->priv;
400 if (gtk_toggle_button_get_active (togglebutton) &&
401 !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
403 /* we get here only when the menu is activated by a key
404 * press, so that we can select the first menu item */
405 popup_menu_under_arrow (button, NULL);
406 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
411 arrow_button_button_press_event_cb (GtkWidget *widget,
412 GdkEventButton *event,
413 GtkMenuToolButton *button)
415 if (event->button == GDK_BUTTON_PRIMARY)
417 popup_menu_under_arrow (button, event);
418 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
429 gtk_menu_tool_button_init (GtkMenuToolButton *button)
433 GtkWidget *arrow_button;
434 GtkWidget *real_button;
436 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
437 GTK_TYPE_MENU_TOOL_BUTTON,
438 GtkMenuToolButtonPrivate);
440 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
442 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
444 real_button = gtk_bin_get_child (GTK_BIN (button));
445 g_object_ref (real_button);
446 gtk_container_remove (GTK_CONTAINER (button), real_button);
447 gtk_container_add (GTK_CONTAINER (box), real_button);
448 g_object_unref (real_button);
450 arrow_button = gtk_toggle_button_new ();
451 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
452 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
453 gtk_box_pack_end (GTK_BOX (box), arrow_button,
456 /* the arrow button is insentive until we set a menu */
457 gtk_widget_set_sensitive (arrow_button, FALSE);
459 gtk_widget_show_all (box);
461 gtk_container_add (GTK_CONTAINER (button), box);
463 button->priv->button = real_button;
464 button->priv->arrow = arrow;
465 button->priv->arrow_button = arrow_button;
466 button->priv->box = box;
468 g_signal_connect (arrow_button, "toggled",
469 G_CALLBACK (arrow_button_toggled_cb), button);
470 g_signal_connect (arrow_button, "button-press-event",
471 G_CALLBACK (arrow_button_button_press_event_cb), button);
475 gtk_menu_tool_button_destroy (GtkWidget *widget)
477 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
479 if (button->priv->menu)
481 g_signal_handlers_disconnect_by_func (button->priv->menu,
484 gtk_menu_detach (button->priv->menu);
486 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
487 arrow_button_toggled_cb,
489 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
490 arrow_button_button_press_event_cb,
494 GTK_WIDGET_CLASS (gtk_menu_tool_button_parent_class)->destroy (widget);
498 gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
503 if (type && strcmp (type, "menu") == 0)
504 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (buildable),
507 parent_buildable_iface->add_child (buildable, builder, child, type);
511 gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface)
513 parent_buildable_iface = g_type_interface_peek_parent (iface);
514 iface->add_child = gtk_menu_tool_button_buildable_add_child;
518 * gtk_menu_tool_button_new:
519 * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
520 * @label: (allow-none): a string that will be used as label, or %NULL
522 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
525 * Return value: the new #GtkMenuToolButton
530 gtk_menu_tool_button_new (GtkWidget *icon_widget,
533 GtkMenuToolButton *button;
535 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
538 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
541 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
543 return GTK_TOOL_ITEM (button);
547 * gtk_menu_tool_button_new_from_stock:
548 * @stock_id: the name of a stock item
550 * Creates a new #GtkMenuToolButton.
551 * The new #GtkMenuToolButton will contain an icon and label from
552 * the stock item indicated by @stock_id.
554 * Return value: the new #GtkMenuToolButton
559 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
561 GtkMenuToolButton *button;
563 g_return_val_if_fail (stock_id != NULL, NULL);
565 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
566 "stock-id", stock_id,
569 return GTK_TOOL_ITEM (button);
572 /* Callback for the "deactivate" signal on the pop-up menu.
573 * This is used so that we unset the state of the toggle button
574 * when the pop-up menu disappears.
577 menu_deactivate_cb (GtkMenuShell *menu_shell,
578 GtkMenuToolButton *button)
580 GtkMenuToolButtonPrivate *priv = button->priv;
582 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
588 menu_detacher (GtkWidget *widget,
591 GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
593 g_return_if_fail (priv->menu == menu);
599 * gtk_menu_tool_button_set_menu:
600 * @button: a #GtkMenuToolButton
601 * @menu: the #GtkMenu associated with #GtkMenuToolButton
603 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
604 * If @menu is NULL, the arrow button becomes insensitive.
609 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
612 GtkMenuToolButtonPrivate *priv;
614 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
615 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
619 if (priv->menu != GTK_MENU (menu))
621 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
622 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
626 g_signal_handlers_disconnect_by_func (priv->menu,
629 gtk_menu_detach (priv->menu);
632 priv->menu = GTK_MENU (menu);
636 gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
639 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
641 g_signal_connect (priv->menu, "deactivate",
642 G_CALLBACK (menu_deactivate_cb), button);
645 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
648 g_object_notify (G_OBJECT (button), "menu");
652 * gtk_menu_tool_button_get_menu:
653 * @button: a #GtkMenuToolButton
655 * Gets the #GtkMenu associated with #GtkMenuToolButton.
657 * Return value: (transfer none): the #GtkMenu associated
658 * with #GtkMenuToolButton
663 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
665 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
667 return GTK_WIDGET (button->priv->menu);
671 * gtk_menu_tool_button_set_arrow_tooltip_text:
672 * @button: a #GtkMenuToolButton
673 * @text: text to be used as tooltip text for button's arrow button
675 * Sets the tooltip text to be used as tooltip for the arrow button which
676 * pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a tooltip
677 * on the whole #GtkMenuToolButton.
682 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
685 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
687 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
691 * gtk_menu_tool_button_set_arrow_tooltip_markup:
692 * @button: a #GtkMenuToolButton
693 * @markup: markup text to be used as tooltip text for button's arrow button
695 * Sets the tooltip markup text to be used as tooltip for the arrow button
696 * which pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a
697 * tooltip on the whole #GtkMenuToolButton.
702 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
705 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
707 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);