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"
32 #include "gtksizerequest.h"
33 #include "gtkbuildable.h"
35 #include "gtkprivate.h"
40 * SECTION:gtkmenutoolbutton
41 * @Short_description: A GtkToolItem containing a button with an additional dropdown menu
42 * @Title: GtkMenuToolButton
43 * @See_also: #GtkToolbar, #GtkToolButton
45 * A #GtkMenuToolButton is a #GtkToolItem that contains a button and
46 * a small additional button with an arrow. When clicked, the arrow
47 * button pops up a dropdown menu.
49 * Use gtk_menu_tool_button_new() to create a new
50 * #GtkMenuToolButton. Use gtk_menu_tool_button_new_from_stock() to
51 * create a new #GtkMenuToolButton containing a stock item.
53 * <refsect2 id="GtkMenuToolButton-BUILDER-UI">
54 * <title>GtkMenuToolButton as GtkBuildable</title>
56 * The GtkMenuToolButton implementation of the GtkBuildable interface
57 * supports adding a menu by specifying "menu" as the "type"
58 * attribute of a <child> element.
61 * <title>A UI definition fragment with menus</title>
62 * <programlisting><![CDATA[
63 * <object class="GtkMenuToolButton">
65 * <object class="GtkMenu"/>
68 * ]]></programlisting>
75 struct _GtkMenuToolButtonPrivate
79 GtkWidget *arrow_button;
84 static void gtk_menu_tool_button_destroy (GtkWidget *widget);
86 static int menu_deactivate_cb (GtkMenuShell *menu_shell,
87 GtkMenuToolButton *button);
89 static void gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface);
90 static void gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
107 static gint signals[LAST_SIGNAL];
109 static GtkBuildableIface *parent_buildable_iface;
111 G_DEFINE_TYPE_WITH_CODE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON,
112 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
113 gtk_menu_tool_button_buildable_interface_init))
116 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
118 GtkMenuToolButtonPrivate *priv = button->priv;
121 GtkOrientation orientation;
123 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
125 if (orientation == GTK_ORIENTATION_HORIZONTAL)
127 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
128 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
132 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
133 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
136 parent = gtk_widget_get_parent (priv->button);
137 if (priv->button && parent)
139 g_object_ref (priv->button);
140 gtk_container_remove (GTK_CONTAINER (parent),
142 gtk_container_add (GTK_CONTAINER (box), priv->button);
143 g_object_unref (priv->button);
146 parent = gtk_widget_get_parent (priv->arrow_button);
147 if (priv->arrow_button && parent)
149 g_object_ref (priv->arrow_button);
150 gtk_container_remove (GTK_CONTAINER (parent),
152 gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
154 g_object_unref (priv->arrow_button);
161 /* Transfer a possible tooltip to the new box */
162 g_object_get (priv->box, "tooltip-markup", &tmp, NULL);
166 g_object_set (box, "tooltip-markup", tmp, NULL);
170 /* Note: we are not destroying the button and the arrow_button
171 * here because they were removed from their container above
173 gtk_widget_destroy (priv->box);
178 gtk_container_add (GTK_CONTAINER (button), priv->box);
179 gtk_widget_show_all (priv->box);
181 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
182 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
184 gtk_widget_queue_resize (GTK_WIDGET (button));
188 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
190 gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
193 GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
197 gtk_menu_tool_button_state_changed (GtkWidget *widget,
198 GtkStateType previous_state)
200 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
201 GtkMenuToolButtonPrivate *priv = button->priv;
203 if (!gtk_widget_is_sensitive (widget) && priv->menu)
205 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
210 gtk_menu_tool_button_set_property (GObject *object,
215 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
220 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 gtk_menu_tool_button_get_property (GObject *object,
235 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
240 g_value_set_object (value, button->priv->menu);
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
252 GObjectClass *object_class;
253 GtkWidgetClass *widget_class;
254 GtkToolItemClass *toolitem_class;
256 object_class = (GObjectClass *)klass;
257 widget_class = (GtkWidgetClass *)klass;
258 toolitem_class = (GtkToolItemClass *)klass;
260 object_class->set_property = gtk_menu_tool_button_set_property;
261 object_class->get_property = gtk_menu_tool_button_get_property;
263 widget_class->destroy = gtk_menu_tool_button_destroy;
264 widget_class->state_changed = gtk_menu_tool_button_state_changed;
266 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
269 * GtkMenuToolButton::show-menu:
270 * @button: the object on which the signal is emitted
272 * The ::show-menu signal is emitted before the menu is shown.
274 * It can be used to populate the menu on demand, using
275 * gtk_menu_tool_button_get_menu().
277 * Note that even if you populate the menu dynamically in this way,
278 * you must set an empty menu on the #GtkMenuToolButton beforehand,
279 * since the arrow is made insensitive if the menu is not set.
282 g_signal_new (I_("show-menu"),
283 G_OBJECT_CLASS_TYPE (klass),
285 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
287 g_cclosure_marshal_VOID__VOID,
290 g_object_class_install_property (object_class,
292 g_param_spec_object ("menu",
294 P_("The dropdown menu"),
296 GTK_PARAM_READWRITE));
298 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
302 menu_position_func (GtkMenu *menu,
306 GtkMenuToolButton *button)
308 GtkMenuToolButtonPrivate *priv = button->priv;
309 GtkAllocation arrow_allocation;
310 GtkWidget *widget = GTK_WIDGET (button);
312 GtkRequisition menu_req;
313 GtkOrientation orientation;
314 GtkTextDirection direction;
315 GdkRectangle monitor;
320 gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
323 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
324 direction = gtk_widget_get_direction (widget);
325 window = gtk_widget_get_window (widget);
327 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
328 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
331 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
333 if (orientation == GTK_ORIENTATION_HORIZONTAL)
335 GtkAllocation allocation;
337 gtk_widget_get_allocation (widget, &allocation);
338 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
340 gdk_window_get_origin (window, x, y);
344 if (direction == GTK_TEXT_DIR_LTR)
345 *x += MAX (allocation.width - menu_req.width, 0);
346 else if (menu_req.width > allocation.width)
347 *x -= menu_req.width - allocation.width;
349 if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
350 *y += arrow_allocation.height;
351 else if ((*y - menu_req.height) >= monitor.y)
352 *y -= menu_req.height;
353 else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
354 *y += arrow_allocation.height;
356 *y -= menu_req.height;
360 gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
361 gtk_widget_get_preferred_size (priv->arrow_button,
364 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
366 if (direction == GTK_TEXT_DIR_LTR)
367 *x += arrow_allocation.width;
369 *x -= menu_req.width;
371 if (*y + menu_req.height > monitor.y + monitor.height &&
372 *y + arrow_allocation.height - monitor.y > monitor.y + monitor.height - *y)
373 *y += arrow_allocation.height - menu_req.height;
380 popup_menu_under_arrow (GtkMenuToolButton *button,
381 GdkEventButton *event)
383 GtkMenuToolButtonPrivate *priv = button->priv;
385 g_signal_emit (button, signals[SHOW_MENU], 0);
390 gtk_menu_popup (priv->menu, NULL, NULL,
391 (GtkMenuPositionFunc) menu_position_func,
393 event ? event->button : 0,
394 event ? event->time : gtk_get_current_event_time ());
398 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
399 GtkMenuToolButton *button)
401 GtkMenuToolButtonPrivate *priv = button->priv;
406 if (gtk_toggle_button_get_active (togglebutton) &&
407 !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
409 /* we get here only when the menu is activated by a key
410 * press, so that we can select the first menu item */
411 popup_menu_under_arrow (button, NULL);
412 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
417 arrow_button_button_press_event_cb (GtkWidget *widget,
418 GdkEventButton *event,
419 GtkMenuToolButton *button)
421 if (event->button == 1)
423 popup_menu_under_arrow (button, event);
424 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
435 gtk_menu_tool_button_init (GtkMenuToolButton *button)
439 GtkWidget *arrow_button;
440 GtkWidget *real_button;
442 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
443 GTK_TYPE_MENU_TOOL_BUTTON,
444 GtkMenuToolButtonPrivate);
446 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
448 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
450 real_button = gtk_bin_get_child (GTK_BIN (button));
451 g_object_ref (real_button);
452 gtk_container_remove (GTK_CONTAINER (button), real_button);
453 gtk_container_add (GTK_CONTAINER (box), real_button);
454 g_object_unref (real_button);
456 arrow_button = gtk_toggle_button_new ();
457 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
458 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
459 gtk_box_pack_end (GTK_BOX (box), arrow_button,
462 /* the arrow button is insentive until we set a menu */
463 gtk_widget_set_sensitive (arrow_button, FALSE);
465 gtk_widget_show_all (box);
467 gtk_container_add (GTK_CONTAINER (button), box);
469 button->priv->button = real_button;
470 button->priv->arrow = arrow;
471 button->priv->arrow_button = arrow_button;
472 button->priv->box = box;
474 g_signal_connect (arrow_button, "toggled",
475 G_CALLBACK (arrow_button_toggled_cb), button);
476 g_signal_connect (arrow_button, "button-press-event",
477 G_CALLBACK (arrow_button_button_press_event_cb), button);
481 gtk_menu_tool_button_destroy (GtkWidget *widget)
483 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
485 if (button->priv->menu)
487 g_signal_handlers_disconnect_by_func (button->priv->menu,
490 gtk_menu_detach (button->priv->menu);
492 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
493 arrow_button_toggled_cb,
495 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
496 arrow_button_button_press_event_cb,
500 GTK_WIDGET_CLASS (gtk_menu_tool_button_parent_class)->destroy (widget);
504 gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
509 if (type && strcmp (type, "menu") == 0)
510 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (buildable),
513 parent_buildable_iface->add_child (buildable, builder, child, type);
517 gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface)
519 parent_buildable_iface = g_type_interface_peek_parent (iface);
520 iface->add_child = gtk_menu_tool_button_buildable_add_child;
524 * gtk_menu_tool_button_new:
525 * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
526 * @label: (allow-none): a string that will be used as label, or %NULL
528 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
531 * Return value: the new #GtkMenuToolButton
536 gtk_menu_tool_button_new (GtkWidget *icon_widget,
539 GtkMenuToolButton *button;
541 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
544 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
547 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
549 return GTK_TOOL_ITEM (button);
553 * gtk_menu_tool_button_new_from_stock:
554 * @stock_id: the name of a stock item
556 * Creates a new #GtkMenuToolButton.
557 * The new #GtkMenuToolButton will contain an icon and label from
558 * the stock item indicated by @stock_id.
560 * Return value: the new #GtkMenuToolButton
565 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
567 GtkMenuToolButton *button;
569 g_return_val_if_fail (stock_id != NULL, NULL);
571 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
572 "stock-id", stock_id,
575 return GTK_TOOL_ITEM (button);
578 /* Callback for the "deactivate" signal on the pop-up menu.
579 * This is used so that we unset the state of the toggle button
580 * when the pop-up menu disappears.
583 menu_deactivate_cb (GtkMenuShell *menu_shell,
584 GtkMenuToolButton *button)
586 GtkMenuToolButtonPrivate *priv = button->priv;
588 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
594 menu_detacher (GtkWidget *widget,
597 GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
599 g_return_if_fail (priv->menu == menu);
605 * gtk_menu_tool_button_set_menu:
606 * @button: a #GtkMenuToolButton
607 * @menu: the #GtkMenu associated with #GtkMenuToolButton
609 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
610 * If @menu is NULL, the arrow button becomes insensitive.
615 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
618 GtkMenuToolButtonPrivate *priv;
620 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
621 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
625 if (priv->menu != GTK_MENU (menu))
627 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
628 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
632 g_signal_handlers_disconnect_by_func (priv->menu,
635 gtk_menu_detach (priv->menu);
638 priv->menu = GTK_MENU (menu);
642 gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
645 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
647 g_signal_connect (priv->menu, "deactivate",
648 G_CALLBACK (menu_deactivate_cb), button);
651 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
654 g_object_notify (G_OBJECT (button), "menu");
658 * gtk_menu_tool_button_get_menu:
659 * @button: a #GtkMenuToolButton
661 * Gets the #GtkMenu associated with #GtkMenuToolButton.
663 * Return value: (transfer none): the #GtkMenu associated
664 * with #GtkMenuToolButton
669 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
671 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
673 return GTK_WIDGET (button->priv->menu);
677 * gtk_menu_tool_button_set_arrow_tooltip_text:
678 * @button: a #GtkMenuToolButton
679 * @text: text to be used as tooltip text for button's arrow button
681 * Sets the tooltip text to be used as tooltip for the arrow button which
682 * pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a tooltip
683 * on the whole #GtkMenuToolButton.
688 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
691 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
693 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
697 * gtk_menu_tool_button_set_arrow_tooltip_markup:
698 * @button: a #GtkMenuToolButton
699 * @markup: markup text to be used as tooltip text for button's arrow button
701 * Sets the tooltip markup text to be used as tooltip for the arrow button
702 * which pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a
703 * tooltip on the whole #GtkMenuToolButton.
708 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
711 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
713 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);