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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gtkmenutoolbutton.h"
26 #include "gtktogglebutton.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
78 GtkWidget *arrow_button;
83 static void gtk_menu_tool_button_destroy (GtkWidget *widget);
85 static int menu_deactivate_cb (GtkMenuShell *menu_shell,
86 GtkMenuToolButton *button);
88 static void gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface);
89 static void gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
106 static gint signals[LAST_SIGNAL];
108 static GtkBuildableIface *parent_buildable_iface;
110 G_DEFINE_TYPE_WITH_CODE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON,
111 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
112 gtk_menu_tool_button_buildable_interface_init))
115 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
117 GtkMenuToolButtonPrivate *priv = button->priv;
120 GtkOrientation orientation;
122 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
124 if (orientation == GTK_ORIENTATION_HORIZONTAL)
126 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
127 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
131 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
132 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
135 parent = gtk_widget_get_parent (priv->button);
136 if (priv->button && parent)
138 g_object_ref (priv->button);
139 gtk_container_remove (GTK_CONTAINER (parent),
141 gtk_container_add (GTK_CONTAINER (box), priv->button);
142 g_object_unref (priv->button);
145 parent = gtk_widget_get_parent (priv->arrow_button);
146 if (priv->arrow_button && parent)
148 g_object_ref (priv->arrow_button);
149 gtk_container_remove (GTK_CONTAINER (parent),
151 gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
153 g_object_unref (priv->arrow_button);
160 /* Transfer a possible tooltip to the new box */
161 g_object_get (priv->box, "tooltip-markup", &tmp, NULL);
165 g_object_set (box, "tooltip-markup", tmp, NULL);
169 /* Note: we are not destroying the button and the arrow_button
170 * here because they were removed from their container above
172 gtk_widget_destroy (priv->box);
177 gtk_container_add (GTK_CONTAINER (button), priv->box);
178 gtk_widget_show_all (priv->box);
180 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
181 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
183 gtk_widget_queue_resize (GTK_WIDGET (button));
187 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
189 gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
192 GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
196 gtk_menu_tool_button_state_changed (GtkWidget *widget,
197 GtkStateType previous_state)
199 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
200 GtkMenuToolButtonPrivate *priv = button->priv;
202 if (!gtk_widget_is_sensitive (widget) && priv->menu)
204 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
209 gtk_menu_tool_button_set_property (GObject *object,
214 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
219 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229 gtk_menu_tool_button_get_property (GObject *object,
234 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
239 g_value_set_object (value, button->priv->menu);
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
251 GObjectClass *object_class;
252 GtkWidgetClass *widget_class;
253 GtkToolItemClass *toolitem_class;
255 object_class = (GObjectClass *)klass;
256 widget_class = (GtkWidgetClass *)klass;
257 toolitem_class = (GtkToolItemClass *)klass;
259 object_class->set_property = gtk_menu_tool_button_set_property;
260 object_class->get_property = gtk_menu_tool_button_get_property;
262 widget_class->destroy = gtk_menu_tool_button_destroy;
263 widget_class->state_changed = gtk_menu_tool_button_state_changed;
265 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
268 * GtkMenuToolButton::show-menu:
269 * @button: the object on which the signal is emitted
271 * The ::show-menu signal is emitted before the menu is shown.
273 * It can be used to populate the menu on demand, using
274 * gtk_menu_tool_button_get_menu().
276 * Note that even if you populate the menu dynamically in this way,
277 * you must set an empty menu on the #GtkMenuToolButton beforehand,
278 * since the arrow is made insensitive if the menu is not set.
281 g_signal_new (I_("show-menu"),
282 G_OBJECT_CLASS_TYPE (klass),
284 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
286 g_cclosure_marshal_VOID__VOID,
289 g_object_class_install_property (object_class,
291 g_param_spec_object ("menu",
293 P_("The dropdown menu"),
295 GTK_PARAM_READWRITE));
297 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
301 menu_position_func (GtkMenu *menu,
305 GtkMenuToolButton *button)
307 GtkMenuToolButtonPrivate *priv = button->priv;
308 GtkAllocation arrow_allocation;
309 GtkWidget *widget = GTK_WIDGET (button);
311 GtkRequisition menu_req;
312 GtkOrientation orientation;
313 GtkTextDirection direction;
314 GdkRectangle monitor;
319 gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
322 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
323 direction = gtk_widget_get_direction (widget);
324 window = gtk_widget_get_window (widget);
326 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
327 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
330 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
332 if (orientation == GTK_ORIENTATION_HORIZONTAL)
334 GtkAllocation allocation;
336 gtk_widget_get_allocation (widget, &allocation);
337 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
339 gdk_window_get_origin (window, x, y);
343 if (direction == GTK_TEXT_DIR_LTR)
344 *x += MAX (allocation.width - menu_req.width, 0);
345 else if (menu_req.width > allocation.width)
346 *x -= menu_req.width - allocation.width;
348 if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
349 *y += arrow_allocation.height;
350 else if ((*y - menu_req.height) >= monitor.y)
351 *y -= menu_req.height;
352 else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
353 *y += arrow_allocation.height;
355 *y -= menu_req.height;
359 gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
360 gtk_widget_get_preferred_size (priv->arrow_button,
363 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
365 if (direction == GTK_TEXT_DIR_LTR)
366 *x += arrow_allocation.width;
368 *x -= menu_req.width;
370 if (*y + menu_req.height > monitor.y + monitor.height &&
371 *y + arrow_allocation.height - monitor.y > monitor.y + monitor.height - *y)
372 *y += arrow_allocation.height - menu_req.height;
379 popup_menu_under_arrow (GtkMenuToolButton *button,
380 GdkEventButton *event)
382 GtkMenuToolButtonPrivate *priv = button->priv;
384 g_signal_emit (button, signals[SHOW_MENU], 0);
389 gtk_menu_popup (priv->menu, NULL, NULL,
390 (GtkMenuPositionFunc) menu_position_func,
392 event ? event->button : 0,
393 event ? event->time : gtk_get_current_event_time ());
397 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
398 GtkMenuToolButton *button)
400 GtkMenuToolButtonPrivate *priv = button->priv;
405 if (gtk_toggle_button_get_active (togglebutton) &&
406 !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
408 /* we get here only when the menu is activated by a key
409 * press, so that we can select the first menu item */
410 popup_menu_under_arrow (button, NULL);
411 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
416 arrow_button_button_press_event_cb (GtkWidget *widget,
417 GdkEventButton *event,
418 GtkMenuToolButton *button)
420 if (event->button == GDK_BUTTON_PRIMARY)
422 popup_menu_under_arrow (button, event);
423 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
434 gtk_menu_tool_button_init (GtkMenuToolButton *button)
438 GtkWidget *arrow_button;
439 GtkWidget *real_button;
441 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
442 GTK_TYPE_MENU_TOOL_BUTTON,
443 GtkMenuToolButtonPrivate);
445 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
447 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
449 real_button = gtk_bin_get_child (GTK_BIN (button));
450 g_object_ref (real_button);
451 gtk_container_remove (GTK_CONTAINER (button), real_button);
452 gtk_container_add (GTK_CONTAINER (box), real_button);
453 g_object_unref (real_button);
455 arrow_button = gtk_toggle_button_new ();
456 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
457 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
458 gtk_box_pack_end (GTK_BOX (box), arrow_button,
461 /* the arrow button is insentive until we set a menu */
462 gtk_widget_set_sensitive (arrow_button, FALSE);
464 gtk_widget_show_all (box);
466 gtk_container_add (GTK_CONTAINER (button), box);
468 button->priv->button = real_button;
469 button->priv->arrow = arrow;
470 button->priv->arrow_button = arrow_button;
471 button->priv->box = box;
473 g_signal_connect (arrow_button, "toggled",
474 G_CALLBACK (arrow_button_toggled_cb), button);
475 g_signal_connect (arrow_button, "button-press-event",
476 G_CALLBACK (arrow_button_button_press_event_cb), button);
480 gtk_menu_tool_button_destroy (GtkWidget *widget)
482 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
484 if (button->priv->menu)
486 g_signal_handlers_disconnect_by_func (button->priv->menu,
489 gtk_menu_detach (button->priv->menu);
491 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
492 arrow_button_toggled_cb,
494 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
495 arrow_button_button_press_event_cb,
499 GTK_WIDGET_CLASS (gtk_menu_tool_button_parent_class)->destroy (widget);
503 gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
508 if (type && strcmp (type, "menu") == 0)
509 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (buildable),
512 parent_buildable_iface->add_child (buildable, builder, child, type);
516 gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface)
518 parent_buildable_iface = g_type_interface_peek_parent (iface);
519 iface->add_child = gtk_menu_tool_button_buildable_add_child;
523 * gtk_menu_tool_button_new:
524 * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
525 * @label: (allow-none): a string that will be used as label, or %NULL
527 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
530 * Return value: the new #GtkMenuToolButton
535 gtk_menu_tool_button_new (GtkWidget *icon_widget,
538 GtkMenuToolButton *button;
540 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
543 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
546 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
548 return GTK_TOOL_ITEM (button);
552 * gtk_menu_tool_button_new_from_stock:
553 * @stock_id: the name of a stock item
555 * Creates a new #GtkMenuToolButton.
556 * The new #GtkMenuToolButton will contain an icon and label from
557 * the stock item indicated by @stock_id.
559 * Return value: the new #GtkMenuToolButton
564 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
566 GtkMenuToolButton *button;
568 g_return_val_if_fail (stock_id != NULL, NULL);
570 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
571 "stock-id", stock_id,
574 return GTK_TOOL_ITEM (button);
577 /* Callback for the "deactivate" signal on the pop-up menu.
578 * This is used so that we unset the state of the toggle button
579 * when the pop-up menu disappears.
582 menu_deactivate_cb (GtkMenuShell *menu_shell,
583 GtkMenuToolButton *button)
585 GtkMenuToolButtonPrivate *priv = button->priv;
587 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
593 menu_detacher (GtkWidget *widget,
596 GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
598 g_return_if_fail (priv->menu == menu);
604 * gtk_menu_tool_button_set_menu:
605 * @button: a #GtkMenuToolButton
606 * @menu: the #GtkMenu associated with #GtkMenuToolButton
608 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
609 * If @menu is NULL, the arrow button becomes insensitive.
614 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
617 GtkMenuToolButtonPrivate *priv;
619 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
620 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
624 if (priv->menu != GTK_MENU (menu))
626 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
627 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
631 g_signal_handlers_disconnect_by_func (priv->menu,
634 gtk_menu_detach (priv->menu);
637 priv->menu = GTK_MENU (menu);
641 gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
644 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
646 g_signal_connect (priv->menu, "deactivate",
647 G_CALLBACK (menu_deactivate_cb), button);
650 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
653 g_object_notify (G_OBJECT (button), "menu");
657 * gtk_menu_tool_button_get_menu:
658 * @button: a #GtkMenuToolButton
660 * Gets the #GtkMenu associated with #GtkMenuToolButton.
662 * Return value: (transfer none): the #GtkMenu associated
663 * with #GtkMenuToolButton
668 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
670 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
672 return GTK_WIDGET (button->priv->menu);
676 * gtk_menu_tool_button_set_arrow_tooltip_text:
677 * @button: a #GtkMenuToolButton
678 * @text: text to be used as tooltip text for button's arrow button
680 * Sets the tooltip text to be used as tooltip for the arrow button which
681 * pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a tooltip
682 * on the whole #GtkMenuToolButton.
687 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
690 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
692 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
696 * gtk_menu_tool_button_set_arrow_tooltip_markup:
697 * @button: a #GtkMenuToolButton
698 * @markup: markup text to be used as tooltip text for button's arrow button
700 * Sets the tooltip markup text to be used as tooltip for the arrow button
701 * which pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a
702 * tooltip on the whole #GtkMenuToolButton.
707 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
710 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
712 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);