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"
34 #include "gtkprivate.h"
38 struct _GtkMenuToolButtonPrivate
42 GtkWidget *arrow_button;
47 static void gtk_menu_tool_button_destroy (GtkWidget *widget);
49 static int menu_deactivate_cb (GtkMenuShell *menu_shell,
50 GtkMenuToolButton *button);
64 static gint signals[LAST_SIGNAL];
66 G_DEFINE_TYPE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON)
69 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
71 GtkMenuToolButtonPrivate *priv = button->priv;
74 GtkOrientation orientation;
76 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
78 if (orientation == GTK_ORIENTATION_HORIZONTAL)
80 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
81 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
85 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
86 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
89 parent = gtk_widget_get_parent (priv->button);
90 if (priv->button && parent)
92 g_object_ref (priv->button);
93 gtk_container_remove (GTK_CONTAINER (parent),
95 gtk_container_add (GTK_CONTAINER (box), priv->button);
96 g_object_unref (priv->button);
99 parent = gtk_widget_get_parent (priv->arrow_button);
100 if (priv->arrow_button && parent)
102 g_object_ref (priv->arrow_button);
103 gtk_container_remove (GTK_CONTAINER (parent),
105 gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
107 g_object_unref (priv->arrow_button);
114 /* Transfer a possible tooltip to the new box */
115 g_object_get (priv->box, "tooltip-markup", &tmp, NULL);
119 g_object_set (box, "tooltip-markup", tmp, NULL);
123 /* Note: we are not destroying the button and the arrow_button
124 * here because they were removed from their container above
126 gtk_widget_destroy (priv->box);
131 gtk_container_add (GTK_CONTAINER (button), priv->box);
132 gtk_widget_show_all (priv->box);
134 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
135 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
137 gtk_widget_queue_resize (GTK_WIDGET (button));
141 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
143 gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
146 GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
150 gtk_menu_tool_button_state_changed (GtkWidget *widget,
151 GtkStateType previous_state)
153 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
154 GtkMenuToolButtonPrivate *priv = button->priv;
156 if (!gtk_widget_is_sensitive (widget) && priv->menu)
158 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
163 gtk_menu_tool_button_set_property (GObject *object,
168 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
173 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 gtk_menu_tool_button_get_property (GObject *object,
188 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
193 g_value_set_object (value, button->priv->menu);
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
205 GObjectClass *object_class;
206 GtkWidgetClass *widget_class;
207 GtkToolItemClass *toolitem_class;
209 object_class = (GObjectClass *)klass;
210 widget_class = (GtkWidgetClass *)klass;
211 toolitem_class = (GtkToolItemClass *)klass;
213 object_class->set_property = gtk_menu_tool_button_set_property;
214 object_class->get_property = gtk_menu_tool_button_get_property;
216 widget_class->destroy = gtk_menu_tool_button_destroy;
217 widget_class->state_changed = gtk_menu_tool_button_state_changed;
219 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
222 * GtkMenuToolButton::show-menu:
223 * @button: the object on which the signal is emitted
225 * The ::show-menu signal is emitted before the menu is shown.
227 * It can be used to populate the menu on demand, using
228 * gtk_menu_tool_button_get_menu().
230 * Note that even if you populate the menu dynamically in this way,
231 * you must set an empty menu on the #GtkMenuToolButton beforehand,
232 * since the arrow is made insensitive if the menu is not set.
235 g_signal_new (I_("show-menu"),
236 G_OBJECT_CLASS_TYPE (klass),
238 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
240 g_cclosure_marshal_VOID__VOID,
243 g_object_class_install_property (object_class,
245 g_param_spec_object ("menu",
247 P_("The dropdown menu"),
249 GTK_PARAM_READWRITE));
251 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
255 menu_position_func (GtkMenu *menu,
259 GtkMenuToolButton *button)
261 GtkMenuToolButtonPrivate *priv = button->priv;
262 GtkAllocation arrow_allocation;
263 GtkWidget *widget = GTK_WIDGET (button);
265 GtkRequisition menu_req;
266 GtkOrientation orientation;
267 GtkTextDirection direction;
268 GdkRectangle monitor;
273 gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
276 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
277 direction = gtk_widget_get_direction (widget);
278 window = gtk_widget_get_window (widget);
280 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
281 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
284 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
286 if (orientation == GTK_ORIENTATION_HORIZONTAL)
288 GtkAllocation allocation;
290 gtk_widget_get_allocation (widget, &allocation);
291 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
293 gdk_window_get_origin (window, x, y);
297 if (direction == GTK_TEXT_DIR_LTR)
298 *x += MAX (allocation.width - menu_req.width, 0);
299 else if (menu_req.width > allocation.width)
300 *x -= menu_req.width - allocation.width;
302 if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
303 *y += arrow_allocation.height;
304 else if ((*y - menu_req.height) >= monitor.y)
305 *y -= menu_req.height;
306 else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
307 *y += arrow_allocation.height;
309 *y -= menu_req.height;
313 gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
314 gtk_widget_get_preferred_size (priv->arrow_button,
317 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
319 if (direction == GTK_TEXT_DIR_LTR)
320 *x += arrow_allocation.width;
322 *x -= menu_req.width;
324 if (*y + menu_req.height > monitor.y + monitor.height &&
325 *y + arrow_allocation.height - monitor.y > monitor.y + monitor.height - *y)
326 *y += arrow_allocation.height - menu_req.height;
333 popup_menu_under_arrow (GtkMenuToolButton *button,
334 GdkEventButton *event)
336 GtkMenuToolButtonPrivate *priv = button->priv;
338 g_signal_emit (button, signals[SHOW_MENU], 0);
343 gtk_menu_popup (priv->menu, NULL, NULL,
344 (GtkMenuPositionFunc) menu_position_func,
346 event ? event->button : 0,
347 event ? event->time : gtk_get_current_event_time ());
351 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
352 GtkMenuToolButton *button)
354 GtkMenuToolButtonPrivate *priv = button->priv;
359 if (gtk_toggle_button_get_active (togglebutton) &&
360 !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
362 /* we get here only when the menu is activated by a key
363 * press, so that we can select the first menu item */
364 popup_menu_under_arrow (button, NULL);
365 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
370 arrow_button_button_press_event_cb (GtkWidget *widget,
371 GdkEventButton *event,
372 GtkMenuToolButton *button)
374 if (event->button == 1)
376 popup_menu_under_arrow (button, event);
377 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
388 gtk_menu_tool_button_init (GtkMenuToolButton *button)
392 GtkWidget *arrow_button;
393 GtkWidget *real_button;
395 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
396 GTK_TYPE_MENU_TOOL_BUTTON,
397 GtkMenuToolButtonPrivate);
399 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
401 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
403 real_button = gtk_bin_get_child (GTK_BIN (button));
404 g_object_ref (real_button);
405 gtk_container_remove (GTK_CONTAINER (button), real_button);
406 gtk_container_add (GTK_CONTAINER (box), real_button);
407 g_object_unref (real_button);
409 arrow_button = gtk_toggle_button_new ();
410 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
411 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
412 gtk_box_pack_end (GTK_BOX (box), arrow_button,
415 /* the arrow button is insentive until we set a menu */
416 gtk_widget_set_sensitive (arrow_button, FALSE);
418 gtk_widget_show_all (box);
420 gtk_container_add (GTK_CONTAINER (button), box);
422 button->priv->button = real_button;
423 button->priv->arrow = arrow;
424 button->priv->arrow_button = arrow_button;
425 button->priv->box = box;
427 g_signal_connect (arrow_button, "toggled",
428 G_CALLBACK (arrow_button_toggled_cb), button);
429 g_signal_connect (arrow_button, "button-press-event",
430 G_CALLBACK (arrow_button_button_press_event_cb), button);
434 gtk_menu_tool_button_destroy (GtkWidget *widget)
436 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
438 if (button->priv->menu)
440 g_signal_handlers_disconnect_by_func (button->priv->menu,
443 gtk_menu_detach (button->priv->menu);
445 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
446 arrow_button_toggled_cb,
448 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
449 arrow_button_button_press_event_cb,
453 GTK_WIDGET_CLASS (gtk_menu_tool_button_parent_class)->destroy (widget);
457 * gtk_menu_tool_button_new:
458 * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
459 * @label: (allow-none): a string that will be used as label, or %NULL
461 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
464 * Return value: the new #GtkMenuToolButton
469 gtk_menu_tool_button_new (GtkWidget *icon_widget,
472 GtkMenuToolButton *button;
474 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
477 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
480 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
482 return GTK_TOOL_ITEM (button);
486 * gtk_menu_tool_button_new_from_stock:
487 * @stock_id: the name of a stock item
489 * Creates a new #GtkMenuToolButton.
490 * The new #GtkMenuToolButton will contain an icon and label from
491 * the stock item indicated by @stock_id.
493 * Return value: the new #GtkMenuToolButton
498 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
500 GtkMenuToolButton *button;
502 g_return_val_if_fail (stock_id != NULL, NULL);
504 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
505 "stock-id", stock_id,
508 return GTK_TOOL_ITEM (button);
511 /* Callback for the "deactivate" signal on the pop-up menu.
512 * This is used so that we unset the state of the toggle button
513 * when the pop-up menu disappears.
516 menu_deactivate_cb (GtkMenuShell *menu_shell,
517 GtkMenuToolButton *button)
519 GtkMenuToolButtonPrivate *priv = button->priv;
521 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
527 menu_detacher (GtkWidget *widget,
530 GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
532 g_return_if_fail (priv->menu == menu);
538 * gtk_menu_tool_button_set_menu:
539 * @button: a #GtkMenuToolButton
540 * @menu: the #GtkMenu associated with #GtkMenuToolButton
542 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
543 * If @menu is NULL, the arrow button becomes insensitive.
548 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
551 GtkMenuToolButtonPrivate *priv;
553 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
554 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
558 if (priv->menu != GTK_MENU (menu))
560 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
561 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
565 g_signal_handlers_disconnect_by_func (priv->menu,
568 gtk_menu_detach (priv->menu);
571 priv->menu = GTK_MENU (menu);
575 gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
578 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
580 g_signal_connect (priv->menu, "deactivate",
581 G_CALLBACK (menu_deactivate_cb), button);
584 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
587 g_object_notify (G_OBJECT (button), "menu");
591 * gtk_menu_tool_button_get_menu:
592 * @button: a #GtkMenuToolButton
594 * Gets the #GtkMenu associated with #GtkMenuToolButton.
596 * Return value: (transfer none): the #GtkMenu associated
597 * with #GtkMenuToolButton
602 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
604 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
606 return GTK_WIDGET (button->priv->menu);
610 * gtk_menu_tool_button_set_arrow_tooltip_text:
611 * @button: a #GtkMenuToolButton
612 * @text: text to be used as tooltip text for button's arrow button
614 * Sets the tooltip text to be used as tooltip for the arrow button which
615 * pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a tooltip
616 * on the whole #GtkMenuToolButton.
621 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
624 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
626 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
630 * gtk_menu_tool_button_set_arrow_tooltip_markup:
631 * @button: a #GtkMenuToolButton
632 * @markup: markup text to be used as tooltip text for button's arrow button
634 * Sets the tooltip markup text to be used as tooltip for the arrow button
635 * which pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a
636 * tooltip on the whole #GtkMenuToolButton.
641 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
644 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
646 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);