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 #undef GTK_DISABLE_DEPRECATED /* GtkTooltips */
26 #include "gtkmenutoolbutton.h"
27 #include "gtktogglebutton.h"
33 #include "gtkprivate.h"
38 #define GTK_MENU_TOOL_BUTTON_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GTK_TYPE_MENU_TOOL_BUTTON, GtkMenuToolButtonPrivate))
40 struct _GtkMenuToolButtonPrivate
44 GtkWidget *arrow_button;
49 static void gtk_menu_tool_button_destroy (GtkObject *object);
51 static int menu_deactivate_cb (GtkMenuShell *menu_shell,
52 GtkMenuToolButton *button);
66 static gint signals[LAST_SIGNAL];
68 G_DEFINE_TYPE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON)
71 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
73 GtkMenuToolButtonPrivate *priv = button->priv;
75 GtkOrientation orientation;
77 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
79 if (orientation == GTK_ORIENTATION_HORIZONTAL)
81 box = gtk_hbox_new (FALSE, 0);
82 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
86 box = gtk_vbox_new (FALSE, 0);
87 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
90 if (priv->button && priv->button->parent)
92 g_object_ref (priv->button);
93 gtk_container_remove (GTK_CONTAINER (priv->button->parent),
95 gtk_container_add (GTK_CONTAINER (box), priv->button);
96 g_object_unref (priv->button);
99 if (priv->arrow_button && priv->arrow_button->parent)
101 g_object_ref (priv->arrow_button);
102 gtk_container_remove (GTK_CONTAINER (priv->arrow_button->parent),
104 gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
106 g_object_unref (priv->arrow_button);
113 /* Transfer a possible tooltip to the new box */
114 g_object_get (priv->box, "tooltip-markup", &tmp, NULL);
118 g_object_set (box, "tooltip-markup", tmp, NULL);
122 /* Note: we are not destroying the button and the arrow_button
123 * here because they were removed from their container above
125 gtk_widget_destroy (priv->box);
130 gtk_container_add (GTK_CONTAINER (button), priv->box);
131 gtk_widget_show_all (priv->box);
133 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
134 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
136 gtk_widget_queue_resize (GTK_WIDGET (button));
140 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
142 gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
145 GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
149 gtk_menu_tool_button_state_changed (GtkWidget *widget,
150 GtkStateType previous_state)
152 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
153 GtkMenuToolButtonPrivate *priv = button->priv;
155 if (!gtk_widget_is_sensitive (widget) && priv->menu)
157 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
162 gtk_menu_tool_button_set_property (GObject *object,
167 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
172 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 gtk_menu_tool_button_get_property (GObject *object,
187 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
192 g_value_set_object (value, button->priv->menu);
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
204 GObjectClass *object_class;
205 GtkObjectClass *gtk_object_class;
206 GtkWidgetClass *widget_class;
207 GtkToolItemClass *toolitem_class;
209 object_class = (GObjectClass *)klass;
210 gtk_object_class = (GtkObjectClass *)klass;
211 widget_class = (GtkWidgetClass *)klass;
212 toolitem_class = (GtkToolItemClass *)klass;
214 object_class->set_property = gtk_menu_tool_button_set_property;
215 object_class->get_property = gtk_menu_tool_button_get_property;
216 gtk_object_class->destroy = gtk_menu_tool_button_destroy;
217 widget_class->state_changed = gtk_menu_tool_button_state_changed;
218 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
221 * GtkMenuToolButton::show-menu:
222 * @button: the object on which the signal is emitted
224 * The ::show-menu signal is emitted before the menu is shown.
226 * It can be used to populate the menu on demand, using
227 * gtk_menu_tool_button_get_menu().
229 * Note that even if you populate the menu dynamically in this way,
230 * you must set an empty menu on the #GtkMenuToolButton beforehand,
231 * since the arrow is made insensitive if the menu is not set.
234 g_signal_new (I_("show-menu"),
235 G_OBJECT_CLASS_TYPE (klass),
237 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
239 g_cclosure_marshal_VOID__VOID,
242 g_object_class_install_property (object_class,
244 g_param_spec_object ("menu",
246 P_("The dropdown menu"),
248 GTK_PARAM_READWRITE));
250 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
254 menu_position_func (GtkMenu *menu,
258 GtkMenuToolButton *button)
260 GtkMenuToolButtonPrivate *priv = button->priv;
261 GtkWidget *widget = GTK_WIDGET (button);
263 GtkRequisition menu_req;
264 GtkOrientation orientation;
265 GtkTextDirection direction;
266 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);
275 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
276 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
279 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
281 if (orientation == GTK_ORIENTATION_HORIZONTAL)
283 gdk_window_get_origin (widget->window, x, y);
284 *x += widget->allocation.x;
285 *y += widget->allocation.y;
287 if (direction == GTK_TEXT_DIR_LTR)
288 *x += MAX (widget->allocation.width - menu_req.width, 0);
289 else if (menu_req.width > widget->allocation.width)
290 *x -= menu_req.width - widget->allocation.width;
292 if ((*y + priv->arrow_button->allocation.height + menu_req.height) <= monitor.y + monitor.height)
293 *y += priv->arrow_button->allocation.height;
294 else if ((*y - menu_req.height) >= monitor.y)
295 *y -= menu_req.height;
296 else if (monitor.y + monitor.height - (*y + priv->arrow_button->allocation.height) > *y)
297 *y += priv->arrow_button->allocation.height;
299 *y -= menu_req.height;
303 gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
304 gtk_widget_size_request (priv->arrow_button, &req);
306 if (direction == GTK_TEXT_DIR_LTR)
307 *x += priv->arrow_button->allocation.width;
309 *x -= menu_req.width;
311 if (*y + menu_req.height > monitor.y + monitor.height &&
312 *y + priv->arrow_button->allocation.height - monitor.y > monitor.y + monitor.height - *y)
313 *y += priv->arrow_button->allocation.height - menu_req.height;
320 popup_menu_under_arrow (GtkMenuToolButton *button,
321 GdkEventButton *event)
323 GtkMenuToolButtonPrivate *priv = button->priv;
325 g_signal_emit (button, signals[SHOW_MENU], 0);
330 gtk_menu_popup (priv->menu, NULL, NULL,
331 (GtkMenuPositionFunc) menu_position_func,
333 event ? event->button : 0,
334 event ? event->time : gtk_get_current_event_time ());
338 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
339 GtkMenuToolButton *button)
341 GtkMenuToolButtonPrivate *priv = button->priv;
346 if (gtk_toggle_button_get_active (togglebutton) &&
347 !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
349 /* we get here only when the menu is activated by a key
350 * press, so that we can select the first menu item */
351 popup_menu_under_arrow (button, NULL);
352 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
357 arrow_button_button_press_event_cb (GtkWidget *widget,
358 GdkEventButton *event,
359 GtkMenuToolButton *button)
361 if (event->button == 1)
363 popup_menu_under_arrow (button, event);
364 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
375 gtk_menu_tool_button_init (GtkMenuToolButton *button)
379 GtkWidget *arrow_button;
380 GtkWidget *real_button;
382 button->priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
384 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
386 box = gtk_hbox_new (FALSE, 0);
388 real_button = GTK_BIN (button)->child;
389 g_object_ref (real_button);
390 gtk_container_remove (GTK_CONTAINER (button), real_button);
391 gtk_container_add (GTK_CONTAINER (box), real_button);
392 g_object_unref (real_button);
394 arrow_button = gtk_toggle_button_new ();
395 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
396 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
397 gtk_box_pack_end (GTK_BOX (box), arrow_button,
400 /* the arrow button is insentive until we set a menu */
401 gtk_widget_set_sensitive (arrow_button, FALSE);
403 gtk_widget_show_all (box);
405 gtk_container_add (GTK_CONTAINER (button), box);
407 button->priv->button = real_button;
408 button->priv->arrow = arrow;
409 button->priv->arrow_button = arrow_button;
410 button->priv->box = box;
412 g_signal_connect (arrow_button, "toggled",
413 G_CALLBACK (arrow_button_toggled_cb), button);
414 g_signal_connect (arrow_button, "button-press-event",
415 G_CALLBACK (arrow_button_button_press_event_cb), button);
419 gtk_menu_tool_button_destroy (GtkObject *object)
421 GtkMenuToolButton *button;
423 button = GTK_MENU_TOOL_BUTTON (object);
425 if (button->priv->menu)
427 g_signal_handlers_disconnect_by_func (button->priv->menu,
430 gtk_menu_detach (button->priv->menu);
432 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
433 arrow_button_toggled_cb,
435 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
436 arrow_button_button_press_event_cb,
440 GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy (object);
444 * gtk_menu_tool_button_new:
445 * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
446 * @label: (allow-none): a string that will be used as label, or %NULL
448 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
451 * Return value: the new #GtkMenuToolButton
456 gtk_menu_tool_button_new (GtkWidget *icon_widget,
459 GtkMenuToolButton *button;
461 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
464 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
467 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
469 return GTK_TOOL_ITEM (button);
473 * gtk_menu_tool_button_new_from_stock:
474 * @stock_id: the name of a stock item
476 * Creates a new #GtkMenuToolButton.
477 * The new #GtkMenuToolButton will contain an icon and label from
478 * the stock item indicated by @stock_id.
480 * Return value: the new #GtkMenuToolButton
485 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
487 GtkMenuToolButton *button;
489 g_return_val_if_fail (stock_id != NULL, NULL);
491 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
492 "stock-id", stock_id,
495 return GTK_TOOL_ITEM (button);
498 /* Callback for the "deactivate" signal on the pop-up menu.
499 * This is used so that we unset the state of the toggle button
500 * when the pop-up menu disappears.
503 menu_deactivate_cb (GtkMenuShell *menu_shell,
504 GtkMenuToolButton *button)
506 GtkMenuToolButtonPrivate *priv = button->priv;
508 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
514 menu_detacher (GtkWidget *widget,
517 GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
519 g_return_if_fail (priv->menu == menu);
525 * gtk_menu_tool_button_set_menu:
526 * @button: a #GtkMenuToolButton
527 * @menu: the #GtkMenu associated with #GtkMenuToolButton
529 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
530 * If @menu is NULL, the arrow button becomes insensitive.
535 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
538 GtkMenuToolButtonPrivate *priv;
540 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
541 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
545 if (priv->menu != GTK_MENU (menu))
547 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
548 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
552 g_signal_handlers_disconnect_by_func (priv->menu,
555 gtk_menu_detach (priv->menu);
558 priv->menu = GTK_MENU (menu);
562 gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
565 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
567 g_signal_connect (priv->menu, "deactivate",
568 G_CALLBACK (menu_deactivate_cb), button);
571 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
574 g_object_notify (G_OBJECT (button), "menu");
578 * gtk_menu_tool_button_get_menu:
579 * @button: a #GtkMenuToolButton
581 * Gets the #GtkMenu associated with #GtkMenuToolButton.
583 * Return value: the #GtkMenu associated with #GtkMenuToolButton
588 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
590 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
592 return GTK_WIDGET (button->priv->menu);
596 * gtk_menu_tool_button_set_arrow_tooltip:
597 * @button: a #GtkMenuToolButton
598 * @tooltips: the #GtkTooltips object to be used
599 * @tip_text: (allow-none): text to be used as tooltip text for tool_item
600 * @tip_private: (allow-none): text to be used as private tooltip text
602 * Sets the #GtkTooltips object to be used for arrow button which
603 * pops up the menu. See gtk_tool_item_set_tooltip() for setting
604 * a tooltip on the whole #GtkMenuToolButton.
608 * Deprecated: 2.12: Use gtk_menu_tool_button_set_arrow_tooltip_text()
612 gtk_menu_tool_button_set_arrow_tooltip (GtkMenuToolButton *button,
613 GtkTooltips *tooltips,
614 const gchar *tip_text,
615 const gchar *tip_private)
617 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
619 gtk_tooltips_set_tip (tooltips, button->priv->arrow_button, tip_text, tip_private);
623 * gtk_menu_tool_button_set_arrow_tooltip_text:
624 * @button: a #GtkMenuToolButton
625 * @text: text to be used as tooltip text for button's arrow button
627 * Sets the tooltip text to be used as tooltip for the arrow button which
628 * pops up the menu. See gtk_tool_item_set_tooltip() for setting a tooltip
629 * on the whole #GtkMenuToolButton.
634 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
637 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
639 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
643 * gtk_menu_tool_button_set_arrow_tooltip_markup:
644 * @button: a #GtkMenuToolButton
645 * @markup: markup text to be used as tooltip text for button's arrow button
647 * Sets the tooltip markup text to be used as tooltip for the arrow button
648 * which pops up the menu. See gtk_tool_item_set_tooltip() for setting a
649 * tooltip on the whole #GtkMenuToolButton.
654 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
657 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
659 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);
662 #define __GTK_MENU_TOOL_BUTTON_C__
663 #include "gtkaliasdef.c"