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"
36 #define GTK_MENU_TOOL_BUTTON_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GTK_TYPE_MENU_TOOL_BUTTON, GtkMenuToolButtonPrivate))
38 struct _GtkMenuToolButtonPrivate
42 GtkWidget *arrow_button;
47 static void gtk_menu_tool_button_destroy (GtkObject *object);
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;
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 * 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 GtkWidget *widget = GTK_WIDGET (button);
261 GtkRequisition menu_req;
262 GtkOrientation orientation;
263 GtkTextDirection direction;
264 GdkRectangle monitor;
268 gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_req);
270 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
271 direction = gtk_widget_get_direction (widget);
273 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
274 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
277 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
279 if (orientation == GTK_ORIENTATION_HORIZONTAL)
281 gdk_window_get_origin (widget->window, x, y);
282 *x += widget->allocation.x;
283 *y += widget->allocation.y;
285 if (direction == GTK_TEXT_DIR_LTR)
286 *x += MAX (widget->allocation.width - menu_req.width, 0);
287 else if (menu_req.width > widget->allocation.width)
288 *x -= menu_req.width - widget->allocation.width;
290 if ((*y + priv->arrow_button->allocation.height + menu_req.height) <= monitor.y + monitor.height)
291 *y += priv->arrow_button->allocation.height;
292 else if ((*y - menu_req.height) >= monitor.y)
293 *y -= menu_req.height;
294 else if (monitor.y + monitor.height - (*y + priv->arrow_button->allocation.height) > *y)
295 *y += priv->arrow_button->allocation.height;
297 *y -= menu_req.height;
301 gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
302 gtk_widget_size_request (priv->arrow_button, &req);
304 if (direction == GTK_TEXT_DIR_LTR)
305 *x += priv->arrow_button->allocation.width;
307 *x -= menu_req.width;
309 if (*y + menu_req.height > monitor.y + monitor.height &&
310 *y + priv->arrow_button->allocation.height - monitor.y > monitor.y + monitor.height - *y)
311 *y += priv->arrow_button->allocation.height - menu_req.height;
318 popup_menu_under_arrow (GtkMenuToolButton *button,
319 GdkEventButton *event)
321 GtkMenuToolButtonPrivate *priv = button->priv;
323 g_signal_emit (button, signals[SHOW_MENU], 0);
328 gtk_menu_popup (priv->menu, NULL, NULL,
329 (GtkMenuPositionFunc) menu_position_func,
331 event ? event->button : 0,
332 event ? event->time : gtk_get_current_event_time ());
336 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
337 GtkMenuToolButton *button)
339 GtkMenuToolButtonPrivate *priv = button->priv;
344 if (gtk_toggle_button_get_active (togglebutton) &&
345 !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
347 /* we get here only when the menu is activated by a key
348 * press, so that we can select the first menu item */
349 popup_menu_under_arrow (button, NULL);
350 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
355 arrow_button_button_press_event_cb (GtkWidget *widget,
356 GdkEventButton *event,
357 GtkMenuToolButton *button)
359 if (event->button == 1)
361 popup_menu_under_arrow (button, event);
362 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
373 gtk_menu_tool_button_init (GtkMenuToolButton *button)
377 GtkWidget *arrow_button;
378 GtkWidget *real_button;
380 button->priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
382 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
384 box = gtk_hbox_new (FALSE, 0);
386 real_button = GTK_BIN (button)->child;
387 g_object_ref (real_button);
388 gtk_container_remove (GTK_CONTAINER (button), real_button);
389 gtk_container_add (GTK_CONTAINER (box), real_button);
390 g_object_unref (real_button);
392 arrow_button = gtk_toggle_button_new ();
393 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
394 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
395 gtk_box_pack_end (GTK_BOX (box), arrow_button,
398 /* the arrow button is insentive until we set a menu */
399 gtk_widget_set_sensitive (arrow_button, FALSE);
401 gtk_widget_show_all (box);
403 gtk_container_add (GTK_CONTAINER (button), box);
405 button->priv->button = real_button;
406 button->priv->arrow = arrow;
407 button->priv->arrow_button = arrow_button;
408 button->priv->box = box;
410 g_signal_connect (arrow_button, "toggled",
411 G_CALLBACK (arrow_button_toggled_cb), button);
412 g_signal_connect (arrow_button, "button-press-event",
413 G_CALLBACK (arrow_button_button_press_event_cb), button);
417 gtk_menu_tool_button_destroy (GtkObject *object)
419 GtkMenuToolButton *button;
421 button = GTK_MENU_TOOL_BUTTON (object);
423 if (button->priv->menu)
425 g_signal_handlers_disconnect_by_func (button->priv->menu,
428 gtk_menu_detach (button->priv->menu);
430 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
431 arrow_button_toggled_cb,
433 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
434 arrow_button_button_press_event_cb,
438 GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy (object);
442 * gtk_menu_tool_button_new:
443 * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
444 * @label: (allow-none): a string that will be used as label, or %NULL
446 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
449 * Return value: the new #GtkMenuToolButton
454 gtk_menu_tool_button_new (GtkWidget *icon_widget,
457 GtkMenuToolButton *button;
459 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
462 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
465 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
467 return GTK_TOOL_ITEM (button);
471 * gtk_menu_tool_button_new_from_stock:
472 * @stock_id: the name of a stock item
474 * Creates a new #GtkMenuToolButton.
475 * The new #GtkMenuToolButton will contain an icon and label from
476 * the stock item indicated by @stock_id.
478 * Return value: the new #GtkMenuToolButton
483 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
485 GtkMenuToolButton *button;
487 g_return_val_if_fail (stock_id != NULL, NULL);
489 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
490 "stock-id", stock_id,
493 return GTK_TOOL_ITEM (button);
496 /* Callback for the "deactivate" signal on the pop-up menu.
497 * This is used so that we unset the state of the toggle button
498 * when the pop-up menu disappears.
501 menu_deactivate_cb (GtkMenuShell *menu_shell,
502 GtkMenuToolButton *button)
504 GtkMenuToolButtonPrivate *priv = button->priv;
506 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
512 menu_detacher (GtkWidget *widget,
515 GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
517 g_return_if_fail (priv->menu == menu);
523 * gtk_menu_tool_button_set_menu:
524 * @button: a #GtkMenuToolButton
525 * @menu: the #GtkMenu associated with #GtkMenuToolButton
527 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
528 * If @menu is NULL, the arrow button becomes insensitive.
533 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
536 GtkMenuToolButtonPrivate *priv;
538 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
539 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
543 if (priv->menu != GTK_MENU (menu))
545 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
546 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
550 g_signal_handlers_disconnect_by_func (priv->menu,
553 gtk_menu_detach (priv->menu);
556 priv->menu = GTK_MENU (menu);
560 gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
563 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
565 g_signal_connect (priv->menu, "deactivate",
566 G_CALLBACK (menu_deactivate_cb), button);
569 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
572 g_object_notify (G_OBJECT (button), "menu");
576 * gtk_menu_tool_button_get_menu:
577 * @button: a #GtkMenuToolButton
579 * Gets the #GtkMenu associated with #GtkMenuToolButton.
581 * Return value: the #GtkMenu associated with #GtkMenuToolButton
586 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
588 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
590 return GTK_WIDGET (button->priv->menu);
594 * gtk_menu_tool_button_set_arrow_tooltip_text:
595 * @button: a #GtkMenuToolButton
596 * @text: text to be used as tooltip text for button's arrow button
598 * Sets the tooltip text to be used as tooltip for the arrow button which
599 * pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a tooltip
600 * on the whole #GtkMenuToolButton.
605 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
608 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
610 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
614 * gtk_menu_tool_button_set_arrow_tooltip_markup:
615 * @button: a #GtkMenuToolButton
616 * @markup: markup text to be used as tooltip text for button's arrow button
618 * Sets the tooltip markup text to be used as tooltip for the arrow button
619 * which pops up the menu. See gtk_tool_item_set_tooltip_text() for setting a
620 * tooltip on the whole #GtkMenuToolButton.
625 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
628 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
630 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);
633 #define __GTK_MENU_TOOL_BUTTON_C__
634 #include "gtkaliasdef.c"