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.
23 #include "gtkmenutoolbutton.h"
25 #include "gtktogglebutton.h"
31 #include "gtkprivate.h"
35 #define GTK_MENU_TOOL_BUTTON_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GTK_TYPE_MENU_TOOL_BUTTON, GtkMenuToolButtonPrivate))
37 struct _GtkMenuToolButtonPrivate
41 GtkWidget *arrow_button;
46 static void gtk_menu_tool_button_destroy (GtkObject *object);
48 static int menu_deactivate_cb (GtkMenuShell *menu_shell,
49 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;
73 GtkOrientation orientation;
75 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
77 if (orientation == GTK_ORIENTATION_HORIZONTAL)
79 box = gtk_hbox_new (FALSE, 0);
80 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
84 box = gtk_vbox_new (FALSE, 0);
85 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
88 if (priv->button && priv->button->parent)
90 g_object_ref (priv->button);
91 gtk_container_remove (GTK_CONTAINER (priv->button->parent),
93 gtk_container_add (GTK_CONTAINER (box), priv->button);
94 g_object_unref (priv->button);
97 if (priv->arrow_button && priv->arrow_button->parent)
99 g_object_ref (priv->arrow_button);
100 gtk_container_remove (GTK_CONTAINER (priv->arrow_button->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 g_signal_new (I_("show-menu"),
220 G_OBJECT_CLASS_TYPE (klass),
222 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
224 g_cclosure_marshal_VOID__VOID,
227 g_object_class_install_property (object_class,
229 g_param_spec_object ("menu",
231 P_("The dropdown menu"),
233 GTK_PARAM_READWRITE));
235 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
239 menu_position_func (GtkMenu *menu,
243 GtkMenuToolButton *button)
245 GtkMenuToolButtonPrivate *priv = button->priv;
246 GtkWidget *widget = GTK_WIDGET (button);
248 GtkRequisition menu_req;
249 GtkOrientation orientation;
250 GtkTextDirection direction;
251 GdkRectangle monitor;
255 gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_req);
257 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
258 direction = gtk_widget_get_direction (widget);
260 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
261 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
264 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
266 if (orientation == GTK_ORIENTATION_HORIZONTAL)
268 gdk_window_get_origin (widget->window, x, y);
269 *x += widget->allocation.x;
270 *y += widget->allocation.y;
272 if (direction == GTK_TEXT_DIR_LTR)
273 *x += MAX (widget->allocation.width - menu_req.width, 0);
274 else if (menu_req.width > widget->allocation.width)
275 *x -= menu_req.width - widget->allocation.width;
277 if ((*y + priv->arrow_button->allocation.height + menu_req.height) <= monitor.y + monitor.height)
278 *y += priv->arrow_button->allocation.height;
279 else if ((*y - menu_req.height) >= monitor.y)
280 *y -= menu_req.height;
281 else if (monitor.y + monitor.height - (*y + priv->arrow_button->allocation.height) > *y)
282 *y += priv->arrow_button->allocation.height;
284 *y -= menu_req.height;
288 gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
289 gtk_widget_size_request (priv->arrow_button, &req);
291 if (direction == GTK_TEXT_DIR_LTR)
292 *x += priv->arrow_button->allocation.width;
294 *x -= menu_req.width;
296 if (*y + menu_req.height > monitor.y + monitor.height &&
297 *y + priv->arrow_button->allocation.height - monitor.y > monitor.y + monitor.height - *y)
298 *y += priv->arrow_button->allocation.height - menu_req.height;
305 popup_menu_under_arrow (GtkMenuToolButton *button,
306 GdkEventButton *event)
308 GtkMenuToolButtonPrivate *priv = button->priv;
310 g_signal_emit (button, signals[SHOW_MENU], 0);
315 gtk_menu_popup (priv->menu, NULL, NULL,
316 (GtkMenuPositionFunc) menu_position_func,
318 event ? event->button : 0,
319 event ? event->time : gtk_get_current_event_time ());
323 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
324 GtkMenuToolButton *button)
326 GtkMenuToolButtonPrivate *priv = button->priv;
331 if (gtk_toggle_button_get_active (togglebutton) &&
332 !GTK_WIDGET_VISIBLE (priv->menu))
334 /* we get here only when the menu is activated by a key
335 * press, so that we can select the first menu item */
336 popup_menu_under_arrow (button, NULL);
337 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
342 arrow_button_button_press_event_cb (GtkWidget *widget,
343 GdkEventButton *event,
344 GtkMenuToolButton *button)
346 if (event->button == 1)
348 popup_menu_under_arrow (button, event);
349 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
360 gtk_menu_tool_button_init (GtkMenuToolButton *button)
364 GtkWidget *arrow_button;
365 GtkWidget *real_button;
367 button->priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
369 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
371 box = gtk_hbox_new (FALSE, 0);
373 real_button = GTK_BIN (button)->child;
374 g_object_ref (real_button);
375 gtk_container_remove (GTK_CONTAINER (button), real_button);
376 gtk_container_add (GTK_CONTAINER (box), real_button);
377 g_object_unref (real_button);
379 arrow_button = gtk_toggle_button_new ();
380 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
381 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
382 gtk_box_pack_end (GTK_BOX (box), arrow_button,
385 /* the arrow button is insentive until we set a menu */
386 gtk_widget_set_sensitive (arrow_button, FALSE);
388 gtk_widget_show_all (box);
390 gtk_container_add (GTK_CONTAINER (button), box);
392 button->priv->button = real_button;
393 button->priv->arrow = arrow;
394 button->priv->arrow_button = arrow_button;
395 button->priv->box = box;
397 g_signal_connect (arrow_button, "toggled",
398 G_CALLBACK (arrow_button_toggled_cb), button);
399 g_signal_connect (arrow_button, "button_press_event",
400 G_CALLBACK (arrow_button_button_press_event_cb), button);
404 gtk_menu_tool_button_destroy (GtkObject *object)
406 GtkMenuToolButton *button;
408 button = GTK_MENU_TOOL_BUTTON (object);
410 if (button->priv->menu)
412 g_signal_handlers_disconnect_by_func (button->priv->menu,
415 gtk_menu_detach (button->priv->menu);
417 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
418 arrow_button_toggled_cb,
420 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
421 arrow_button_button_press_event_cb,
425 if (GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy)
426 (*GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy) (object);
430 * gtk_menu_tool_button_new:
431 * @icon_widget: a widget that will be used as icon widget, or %NULL
432 * @label: a string that will be used as label, or %NULL
434 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
437 * Return value: the new #GtkMenuToolButton
442 gtk_menu_tool_button_new (GtkWidget *icon_widget,
445 GtkMenuToolButton *button;
447 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
450 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
453 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
455 return GTK_TOOL_ITEM (button);
459 * gtk_menu_tool_button_new_from_stock:
460 * @stock_id: the name of a stock item
462 * Creates a new #GtkMenuToolButton.
463 * The new #GtkMenuToolButton will contain an icon and label from
464 * the stock item indicated by @stock_id.
466 * Return value: the new #GtkMenuToolButton
471 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
473 GtkMenuToolButton *button;
475 g_return_val_if_fail (stock_id != NULL, NULL);
477 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
478 "stock-id", stock_id,
481 return GTK_TOOL_ITEM (button);
484 /* Callback for the "deactivate" signal on the pop-up menu.
485 * This is used so that we unset the state of the toggle button
486 * when the pop-up menu disappears.
489 menu_deactivate_cb (GtkMenuShell *menu_shell,
490 GtkMenuToolButton *button)
492 GtkMenuToolButtonPrivate *priv = button->priv;
494 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
500 menu_detacher (GtkWidget *widget,
503 GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
505 g_return_if_fail (priv->menu == menu);
511 * gtk_menu_tool_button_set_menu:
512 * @button: a #GtkMenuToolButton
513 * @menu: the #GtkMenu associated with #GtkMenuToolButton
515 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
516 * If @menu is NULL, the arrow button becomes insensitive.
521 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
524 GtkMenuToolButtonPrivate *priv;
526 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
527 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
531 if (priv->menu != GTK_MENU (menu))
533 if (priv->menu && GTK_WIDGET_VISIBLE (priv->menu))
534 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
538 g_signal_handlers_disconnect_by_func (priv->menu,
541 gtk_menu_detach (priv->menu);
544 priv->menu = GTK_MENU (menu);
548 gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
551 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
553 g_signal_connect (priv->menu, "deactivate",
554 G_CALLBACK (menu_deactivate_cb), button);
557 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
560 g_object_notify (G_OBJECT (button), "menu");
564 * gtk_menu_tool_button_get_menu:
565 * @button: a #GtkMenuToolButton
567 * Gets the #GtkMenu associated with #GtkMenuToolButton.
569 * Return value: the #GtkMenu associated with #GtkMenuToolButton
574 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
576 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
578 return GTK_WIDGET (button->priv->menu);
582 * gtk_menu_tool_button_set_arrow_tooltip:
583 * @button: a #GtkMenuToolButton
584 * @tooltips: the #GtkTooltips object to be used
585 * @tip_text: text to be used as tooltip text for tool_item
586 * @tip_private: text to be used as private tooltip text
588 * Sets the #GtkTooltips object to be used for arrow button which
589 * pops up the menu. See gtk_tool_item_set_tooltip() for setting
590 * a tooltip on the whole #GtkMenuToolButton.
594 * Deprecated: 2.12: Use gtk_menu_tool_button_set_arrow_tooltip_text()
598 gtk_menu_tool_button_set_arrow_tooltip (GtkMenuToolButton *button,
599 GtkTooltips *tooltips,
600 const gchar *tip_text,
601 const gchar *tip_private)
603 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
605 gtk_tooltips_set_tip (tooltips, button->priv->arrow_button, tip_text, tip_private);
609 * gtk_menu_tool_button_set_arrow_tooltip_text:
610 * @button: a #GtkMenuToolButton
611 * @text: text to be used as tooltip text for button's arrow button
613 * Sets the tooltip text to be used as tooltip for the arrow button which
614 * pops up the menu. See gtk_tool_item_set_tooltip() for setting a tooltip
615 * on the whole #GtkMenuToolButton.
620 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
623 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
625 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
629 * gtk_menu_tool_button_set_arrow_tooltip_markup:
630 * @button: a #GtkMenuToolButton
631 * @markup: markup text to be used as tooltip text for button's arrow button
633 * Sets the tooltip markup text to be used as tooltip for the arrow button
634 * which pops up the menu. See gtk_tool_item_set_tooltip() for setting a
635 * tooltip on the whole #GtkMenuToolButton.
640 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
643 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
645 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);
648 #define __GTK_MENU_TOOL_BUTTON_C__
649 #include "gtkaliasdef.c"