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"
25 #include "gtktogglebutton.h"
31 #include "gtkprivate.h"
35 struct _GtkMenuToolButtonPrivate
39 GtkWidget *arrow_button;
44 static void gtk_menu_tool_button_destroy (GtkObject *object);
46 static int menu_deactivate_cb (GtkMenuShell *menu_shell,
47 GtkMenuToolButton *button);
61 static gint signals[LAST_SIGNAL];
63 G_DEFINE_TYPE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON)
66 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
68 GtkMenuToolButtonPrivate *priv = button->priv;
71 GtkOrientation orientation;
73 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
75 if (orientation == GTK_ORIENTATION_HORIZONTAL)
77 box = gtk_hbox_new (FALSE, 0);
78 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
82 box = gtk_vbox_new (FALSE, 0);
83 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
86 parent = gtk_widget_get_parent (priv->button);
87 if (priv->button && parent)
89 g_object_ref (priv->button);
90 gtk_container_remove (GTK_CONTAINER (parent),
92 gtk_container_add (GTK_CONTAINER (box), priv->button);
93 g_object_unref (priv->button);
96 parent = gtk_widget_get_parent (priv->arrow_button);
97 if (priv->arrow_button && parent)
99 g_object_ref (priv->arrow_button);
100 gtk_container_remove (GTK_CONTAINER (parent),
102 gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
104 g_object_unref (priv->arrow_button);
111 /* Transfer a possible tooltip to the new box */
112 g_object_get (priv->box, "tooltip-markup", &tmp, NULL);
116 g_object_set (box, "tooltip-markup", tmp, NULL);
120 /* Note: we are not destroying the button and the arrow_button
121 * here because they were removed from their container above
123 gtk_widget_destroy (priv->box);
128 gtk_container_add (GTK_CONTAINER (button), priv->box);
129 gtk_widget_show_all (priv->box);
131 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
132 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
134 gtk_widget_queue_resize (GTK_WIDGET (button));
138 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
140 gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
143 GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
147 gtk_menu_tool_button_state_changed (GtkWidget *widget,
148 GtkStateType previous_state)
150 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
151 GtkMenuToolButtonPrivate *priv = button->priv;
153 if (!gtk_widget_is_sensitive (widget) && priv->menu)
155 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
160 gtk_menu_tool_button_set_property (GObject *object,
165 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
170 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180 gtk_menu_tool_button_get_property (GObject *object,
185 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
190 g_value_set_object (value, button->priv->menu);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
202 GObjectClass *object_class;
203 GtkObjectClass *gtk_object_class;
204 GtkWidgetClass *widget_class;
205 GtkToolItemClass *toolitem_class;
207 object_class = (GObjectClass *)klass;
208 gtk_object_class = (GtkObjectClass *)klass;
209 widget_class = (GtkWidgetClass *)klass;
210 toolitem_class = (GtkToolItemClass *)klass;
212 object_class->set_property = gtk_menu_tool_button_set_property;
213 object_class->get_property = gtk_menu_tool_button_get_property;
214 gtk_object_class->destroy = gtk_menu_tool_button_destroy;
215 widget_class->state_changed = gtk_menu_tool_button_state_changed;
216 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
219 * GtkMenuToolButton::show-menu:
220 * @button: the object on which the signal is emitted
222 * The ::show-menu signal is emitted before the menu is shown.
224 * It can be used to populate the menu on demand, using
225 * gtk_menu_tool_button_get_menu().
227 * Note that even if you populate the menu dynamically in this way,
228 * you must set an empty menu on the #GtkMenuToolButton beforehand,
229 * since the arrow is made insensitive if the menu is not set.
232 g_signal_new (I_("show-menu"),
233 G_OBJECT_CLASS_TYPE (klass),
235 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
237 g_cclosure_marshal_VOID__VOID,
240 g_object_class_install_property (object_class,
242 g_param_spec_object ("menu",
244 P_("The dropdown menu"),
246 GTK_PARAM_READWRITE));
248 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
252 menu_position_func (GtkMenu *menu,
256 GtkMenuToolButton *button)
258 GtkMenuToolButtonPrivate *priv = button->priv;
259 GtkAllocation arrow_allocation;
260 GtkWidget *widget = GTK_WIDGET (button);
262 GtkRequisition menu_req;
263 GtkOrientation orientation;
264 GtkTextDirection direction;
265 GdkRectangle monitor;
270 gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_req);
272 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
273 direction = gtk_widget_get_direction (widget);
274 window = gtk_widget_get_window (widget);
276 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
277 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
280 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
282 if (orientation == GTK_ORIENTATION_HORIZONTAL)
284 GtkAllocation allocation;
286 gtk_widget_get_allocation (widget, &allocation);
287 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
289 gdk_window_get_origin (window, x, y);
293 if (direction == GTK_TEXT_DIR_LTR)
294 *x += MAX (allocation.width - menu_req.width, 0);
295 else if (menu_req.width > allocation.width)
296 *x -= menu_req.width - allocation.width;
298 if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
299 *y += arrow_allocation.height;
300 else if ((*y - menu_req.height) >= monitor.y)
301 *y -= menu_req.height;
302 else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
303 *y += arrow_allocation.height;
305 *y -= menu_req.height;
309 gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
310 gtk_widget_size_request (priv->arrow_button, &req);
312 gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
314 if (direction == GTK_TEXT_DIR_LTR)
315 *x += arrow_allocation.width;
317 *x -= menu_req.width;
319 if (*y + menu_req.height > monitor.y + monitor.height &&
320 *y + arrow_allocation.height - monitor.y > monitor.y + monitor.height - *y)
321 *y += arrow_allocation.height - menu_req.height;
328 popup_menu_under_arrow (GtkMenuToolButton *button,
329 GdkEventButton *event)
331 GtkMenuToolButtonPrivate *priv = button->priv;
333 g_signal_emit (button, signals[SHOW_MENU], 0);
338 gtk_menu_popup (priv->menu, NULL, NULL,
339 (GtkMenuPositionFunc) menu_position_func,
341 event ? event->button : 0,
342 event ? event->time : gtk_get_current_event_time ());
346 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
347 GtkMenuToolButton *button)
349 GtkMenuToolButtonPrivate *priv = button->priv;
354 if (gtk_toggle_button_get_active (togglebutton) &&
355 !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
357 /* we get here only when the menu is activated by a key
358 * press, so that we can select the first menu item */
359 popup_menu_under_arrow (button, NULL);
360 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
365 arrow_button_button_press_event_cb (GtkWidget *widget,
366 GdkEventButton *event,
367 GtkMenuToolButton *button)
369 if (event->button == 1)
371 popup_menu_under_arrow (button, event);
372 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
383 gtk_menu_tool_button_init (GtkMenuToolButton *button)
387 GtkWidget *arrow_button;
388 GtkWidget *real_button;
390 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
391 GTK_TYPE_MENU_TOOL_BUTTON,
392 GtkMenuToolButtonPrivate);
394 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
396 box = gtk_hbox_new (FALSE, 0);
398 real_button = gtk_bin_get_child (GTK_BIN (button));
399 g_object_ref (real_button);
400 gtk_container_remove (GTK_CONTAINER (button), real_button);
401 gtk_container_add (GTK_CONTAINER (box), real_button);
402 g_object_unref (real_button);
404 arrow_button = gtk_toggle_button_new ();
405 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
406 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
407 gtk_box_pack_end (GTK_BOX (box), arrow_button,
410 /* the arrow button is insentive until we set a menu */
411 gtk_widget_set_sensitive (arrow_button, FALSE);
413 gtk_widget_show_all (box);
415 gtk_container_add (GTK_CONTAINER (button), box);
417 button->priv->button = real_button;
418 button->priv->arrow = arrow;
419 button->priv->arrow_button = arrow_button;
420 button->priv->box = box;
422 g_signal_connect (arrow_button, "toggled",
423 G_CALLBACK (arrow_button_toggled_cb), button);
424 g_signal_connect (arrow_button, "button-press-event",
425 G_CALLBACK (arrow_button_button_press_event_cb), button);
429 gtk_menu_tool_button_destroy (GtkObject *object)
431 GtkMenuToolButton *button;
433 button = GTK_MENU_TOOL_BUTTON (object);
435 if (button->priv->menu)
437 g_signal_handlers_disconnect_by_func (button->priv->menu,
440 gtk_menu_detach (button->priv->menu);
442 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
443 arrow_button_toggled_cb,
445 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
446 arrow_button_button_press_event_cb,
450 GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy (object);
454 * gtk_menu_tool_button_new:
455 * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
456 * @label: (allow-none): a string that will be used as label, or %NULL
458 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
461 * Return value: the new #GtkMenuToolButton
466 gtk_menu_tool_button_new (GtkWidget *icon_widget,
469 GtkMenuToolButton *button;
471 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
474 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
477 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
479 return GTK_TOOL_ITEM (button);
483 * gtk_menu_tool_button_new_from_stock:
484 * @stock_id: the name of a stock item
486 * Creates a new #GtkMenuToolButton.
487 * The new #GtkMenuToolButton will contain an icon and label from
488 * the stock item indicated by @stock_id.
490 * Return value: the new #GtkMenuToolButton
495 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
497 GtkMenuToolButton *button;
499 g_return_val_if_fail (stock_id != NULL, NULL);
501 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
502 "stock-id", stock_id,
505 return GTK_TOOL_ITEM (button);
508 /* Callback for the "deactivate" signal on the pop-up menu.
509 * This is used so that we unset the state of the toggle button
510 * when the pop-up menu disappears.
513 menu_deactivate_cb (GtkMenuShell *menu_shell,
514 GtkMenuToolButton *button)
516 GtkMenuToolButtonPrivate *priv = button->priv;
518 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
524 menu_detacher (GtkWidget *widget,
527 GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
529 g_return_if_fail (priv->menu == menu);
535 * gtk_menu_tool_button_set_menu:
536 * @button: a #GtkMenuToolButton
537 * @menu: the #GtkMenu associated with #GtkMenuToolButton
539 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
540 * If @menu is NULL, the arrow button becomes insensitive.
545 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
548 GtkMenuToolButtonPrivate *priv;
550 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
551 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
555 if (priv->menu != GTK_MENU (menu))
557 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
558 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
562 g_signal_handlers_disconnect_by_func (priv->menu,
565 gtk_menu_detach (priv->menu);
568 priv->menu = GTK_MENU (menu);
572 gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
575 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
577 g_signal_connect (priv->menu, "deactivate",
578 G_CALLBACK (menu_deactivate_cb), button);
581 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
584 g_object_notify (G_OBJECT (button), "menu");
588 * gtk_menu_tool_button_get_menu:
589 * @button: a #GtkMenuToolButton
591 * Gets the #GtkMenu associated with #GtkMenuToolButton.
593 * Return value: the #GtkMenu associated with #GtkMenuToolButton
598 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
600 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
602 return GTK_WIDGET (button->priv->menu);
606 * gtk_menu_tool_button_set_arrow_tooltip_text:
607 * @button: a #GtkMenuToolButton
608 * @text: text to be used as tooltip text for button's arrow button
610 * Sets the tooltip text to be used as tooltip for the arrow button which
611 * pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a tooltip
612 * on the whole #GtkMenuToolButton.
617 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
620 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
622 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
626 * gtk_menu_tool_button_set_arrow_tooltip_markup:
627 * @button: a #GtkMenuToolButton
628 * @markup: markup text to be used as tooltip text for button's arrow button
630 * Sets the tooltip markup text to be used as tooltip for the arrow button
631 * which pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a
632 * tooltip on the whole #GtkMenuToolButton.
637 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
640 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
642 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);