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"
39 struct _GtkMenuToolButtonPrivate
43 GtkWidget *arrow_button;
48 static void gtk_menu_tool_button_destroy (GtkWidget *widget);
50 static int menu_deactivate_cb (GtkMenuShell *menu_shell,
51 GtkMenuToolButton *button);
53 static void gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface);
54 static void gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
71 static gint signals[LAST_SIGNAL];
73 static GtkBuildableIface *parent_buildable_iface;
75 G_DEFINE_TYPE_WITH_CODE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON,
76 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
77 gtk_menu_tool_button_buildable_interface_init))
80 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
82 GtkMenuToolButtonPrivate *priv = button->priv;
85 GtkOrientation orientation;
87 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
89 if (orientation == GTK_ORIENTATION_HORIZONTAL)
91 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
92 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
96 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
97 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
100 parent = gtk_widget_get_parent (priv->button);
101 if (priv->button && parent)
103 g_object_ref (priv->button);
104 gtk_container_remove (GTK_CONTAINER (parent),
106 gtk_container_add (GTK_CONTAINER (box), priv->button);
107 g_object_unref (priv->button);
110 parent = gtk_widget_get_parent (priv->arrow_button);
111 if (priv->arrow_button && parent)
113 g_object_ref (priv->arrow_button);
114 gtk_container_remove (GTK_CONTAINER (parent),
116 gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
118 g_object_unref (priv->arrow_button);
125 /* Transfer a possible tooltip to the new box */
126 g_object_get (priv->box, "tooltip-markup", &tmp, NULL);
130 g_object_set (box, "tooltip-markup", tmp, NULL);
134 /* Note: we are not destroying the button and the arrow_button
135 * here because they were removed from their container above
137 gtk_widget_destroy (priv->box);
142 gtk_container_add (GTK_CONTAINER (button), priv->box);
143 gtk_widget_show_all (priv->box);
145 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
146 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
148 gtk_widget_queue_resize (GTK_WIDGET (button));
152 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
154 gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
157 GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
161 gtk_menu_tool_button_state_changed (GtkWidget *widget,
162 GtkStateType previous_state)
164 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
165 GtkMenuToolButtonPrivate *priv = button->priv;
167 if (!gtk_widget_is_sensitive (widget) && priv->menu)
169 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
174 gtk_menu_tool_button_set_property (GObject *object,
179 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
184 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 gtk_menu_tool_button_get_property (GObject *object,
199 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
204 g_value_set_object (value, button->priv->menu);
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
216 GObjectClass *object_class;
217 GtkWidgetClass *widget_class;
218 GtkToolItemClass *toolitem_class;
220 object_class = (GObjectClass *)klass;
221 widget_class = (GtkWidgetClass *)klass;
222 toolitem_class = (GtkToolItemClass *)klass;
224 object_class->set_property = gtk_menu_tool_button_set_property;
225 object_class->get_property = gtk_menu_tool_button_get_property;
227 widget_class->destroy = gtk_menu_tool_button_destroy;
228 widget_class->state_changed = gtk_menu_tool_button_state_changed;
230 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
233 * GtkMenuToolButton::show-menu:
234 * @button: the object on which the signal is emitted
236 * The ::show-menu signal is emitted before the menu is shown.
238 * It can be used to populate the menu on demand, using
239 * gtk_menu_tool_button_get_menu().
241 * Note that even if you populate the menu dynamically in this way,
242 * you must set an empty menu on the #GtkMenuToolButton beforehand,
243 * since the arrow is made insensitive if the menu is not set.
246 g_signal_new (I_("show-menu"),
247 G_OBJECT_CLASS_TYPE (klass),
249 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
251 g_cclosure_marshal_VOID__VOID,
254 g_object_class_install_property (object_class,
256 g_param_spec_object ("menu",
258 P_("The dropdown menu"),
260 GTK_PARAM_READWRITE));
262 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
266 menu_position_func (GtkMenu *menu,
270 GtkMenuToolButton *button)
272 GtkMenuToolButtonPrivate *priv = button->priv;
273 GtkAllocation arrow_allocation;
274 GtkWidget *widget = GTK_WIDGET (button);
276 GtkRequisition menu_req;
277 GtkOrientation orientation;
278 GtkTextDirection direction;
279 GdkRectangle monitor;
284 gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
287 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
288 direction = gtk_widget_get_direction (widget);
289 window = gtk_widget_get_window (widget);
291 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
292 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
295 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
297 if (orientation == GTK_ORIENTATION_HORIZONTAL)
299 GtkAllocation allocation;
301 gtk_widget_get_allocation (widget, &allocation);
302 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
304 gdk_window_get_origin (window, x, y);
308 if (direction == GTK_TEXT_DIR_LTR)
309 *x += MAX (allocation.width - menu_req.width, 0);
310 else if (menu_req.width > allocation.width)
311 *x -= menu_req.width - allocation.width;
313 if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
314 *y += arrow_allocation.height;
315 else if ((*y - menu_req.height) >= monitor.y)
316 *y -= menu_req.height;
317 else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
318 *y += arrow_allocation.height;
320 *y -= menu_req.height;
324 gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
325 gtk_widget_get_preferred_size (priv->arrow_button,
328 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
330 if (direction == GTK_TEXT_DIR_LTR)
331 *x += arrow_allocation.width;
333 *x -= menu_req.width;
335 if (*y + menu_req.height > monitor.y + monitor.height &&
336 *y + arrow_allocation.height - monitor.y > monitor.y + monitor.height - *y)
337 *y += arrow_allocation.height - menu_req.height;
344 popup_menu_under_arrow (GtkMenuToolButton *button,
345 GdkEventButton *event)
347 GtkMenuToolButtonPrivate *priv = button->priv;
349 g_signal_emit (button, signals[SHOW_MENU], 0);
354 gtk_menu_popup (priv->menu, NULL, NULL,
355 (GtkMenuPositionFunc) menu_position_func,
357 event ? event->button : 0,
358 event ? event->time : gtk_get_current_event_time ());
362 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
363 GtkMenuToolButton *button)
365 GtkMenuToolButtonPrivate *priv = button->priv;
370 if (gtk_toggle_button_get_active (togglebutton) &&
371 !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
373 /* we get here only when the menu is activated by a key
374 * press, so that we can select the first menu item */
375 popup_menu_under_arrow (button, NULL);
376 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
381 arrow_button_button_press_event_cb (GtkWidget *widget,
382 GdkEventButton *event,
383 GtkMenuToolButton *button)
385 if (event->button == 1)
387 popup_menu_under_arrow (button, event);
388 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
399 gtk_menu_tool_button_init (GtkMenuToolButton *button)
403 GtkWidget *arrow_button;
404 GtkWidget *real_button;
406 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
407 GTK_TYPE_MENU_TOOL_BUTTON,
408 GtkMenuToolButtonPrivate);
410 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
412 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
414 real_button = gtk_bin_get_child (GTK_BIN (button));
415 g_object_ref (real_button);
416 gtk_container_remove (GTK_CONTAINER (button), real_button);
417 gtk_container_add (GTK_CONTAINER (box), real_button);
418 g_object_unref (real_button);
420 arrow_button = gtk_toggle_button_new ();
421 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
422 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
423 gtk_box_pack_end (GTK_BOX (box), arrow_button,
426 /* the arrow button is insentive until we set a menu */
427 gtk_widget_set_sensitive (arrow_button, FALSE);
429 gtk_widget_show_all (box);
431 gtk_container_add (GTK_CONTAINER (button), box);
433 button->priv->button = real_button;
434 button->priv->arrow = arrow;
435 button->priv->arrow_button = arrow_button;
436 button->priv->box = box;
438 g_signal_connect (arrow_button, "toggled",
439 G_CALLBACK (arrow_button_toggled_cb), button);
440 g_signal_connect (arrow_button, "button-press-event",
441 G_CALLBACK (arrow_button_button_press_event_cb), button);
445 gtk_menu_tool_button_destroy (GtkWidget *widget)
447 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
449 if (button->priv->menu)
451 g_signal_handlers_disconnect_by_func (button->priv->menu,
454 gtk_menu_detach (button->priv->menu);
456 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
457 arrow_button_toggled_cb,
459 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
460 arrow_button_button_press_event_cb,
464 GTK_WIDGET_CLASS (gtk_menu_tool_button_parent_class)->destroy (widget);
468 gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
473 if (type && strcmp (type, "menu") == 0)
474 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (buildable),
477 parent_buildable_iface->add_child (buildable, builder, child, type);
481 gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface)
483 parent_buildable_iface = g_type_interface_peek_parent (iface);
484 iface->add_child = gtk_menu_tool_button_buildable_add_child;
488 * gtk_menu_tool_button_new:
489 * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
490 * @label: (allow-none): a string that will be used as label, or %NULL
492 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
495 * Return value: the new #GtkMenuToolButton
500 gtk_menu_tool_button_new (GtkWidget *icon_widget,
503 GtkMenuToolButton *button;
505 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
508 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
511 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
513 return GTK_TOOL_ITEM (button);
517 * gtk_menu_tool_button_new_from_stock:
518 * @stock_id: the name of a stock item
520 * Creates a new #GtkMenuToolButton.
521 * The new #GtkMenuToolButton will contain an icon and label from
522 * the stock item indicated by @stock_id.
524 * Return value: the new #GtkMenuToolButton
529 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
531 GtkMenuToolButton *button;
533 g_return_val_if_fail (stock_id != NULL, NULL);
535 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
536 "stock-id", stock_id,
539 return GTK_TOOL_ITEM (button);
542 /* Callback for the "deactivate" signal on the pop-up menu.
543 * This is used so that we unset the state of the toggle button
544 * when the pop-up menu disappears.
547 menu_deactivate_cb (GtkMenuShell *menu_shell,
548 GtkMenuToolButton *button)
550 GtkMenuToolButtonPrivate *priv = button->priv;
552 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
558 menu_detacher (GtkWidget *widget,
561 GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
563 g_return_if_fail (priv->menu == menu);
569 * gtk_menu_tool_button_set_menu:
570 * @button: a #GtkMenuToolButton
571 * @menu: the #GtkMenu associated with #GtkMenuToolButton
573 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
574 * If @menu is NULL, the arrow button becomes insensitive.
579 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
582 GtkMenuToolButtonPrivate *priv;
584 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
585 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
589 if (priv->menu != GTK_MENU (menu))
591 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
592 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
596 g_signal_handlers_disconnect_by_func (priv->menu,
599 gtk_menu_detach (priv->menu);
602 priv->menu = GTK_MENU (menu);
606 gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
609 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
611 g_signal_connect (priv->menu, "deactivate",
612 G_CALLBACK (menu_deactivate_cb), button);
615 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
618 g_object_notify (G_OBJECT (button), "menu");
622 * gtk_menu_tool_button_get_menu:
623 * @button: a #GtkMenuToolButton
625 * Gets the #GtkMenu associated with #GtkMenuToolButton.
627 * Return value: (transfer none): the #GtkMenu associated
628 * with #GtkMenuToolButton
633 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
635 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
637 return GTK_WIDGET (button->priv->menu);
641 * gtk_menu_tool_button_set_arrow_tooltip_text:
642 * @button: a #GtkMenuToolButton
643 * @text: text to be used as tooltip text for button's arrow button
645 * Sets the tooltip text to be used as tooltip for the arrow button which
646 * pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a tooltip
647 * on the whole #GtkMenuToolButton.
652 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
655 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
657 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
661 * gtk_menu_tool_button_set_arrow_tooltip_markup:
662 * @button: a #GtkMenuToolButton
663 * @markup: markup text to be used as tooltip text for button's arrow button
665 * Sets the tooltip markup text to be used as tooltip for the arrow button
666 * which pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a
667 * tooltip on the whole #GtkMenuToolButton.
672 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
675 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
677 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);