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);
63 static gint signals[LAST_SIGNAL];
65 G_DEFINE_TYPE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON)
68 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
70 GtkMenuToolButtonPrivate *priv = button->priv;
72 GtkOrientation orientation;
74 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
76 if (orientation == GTK_ORIENTATION_HORIZONTAL)
78 box = gtk_hbox_new (FALSE, 0);
79 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
83 box = gtk_vbox_new (FALSE, 0);
84 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
87 if (priv->button && priv->button->parent)
89 g_object_ref (priv->button);
90 gtk_container_remove (GTK_CONTAINER (priv->button->parent),
92 gtk_container_add (GTK_CONTAINER (box), priv->button);
93 g_object_unref (priv->button);
96 if (priv->arrow_button && priv->arrow_button->parent)
98 g_object_ref (priv->arrow_button);
99 gtk_container_remove (GTK_CONTAINER (priv->arrow_button->parent),
101 gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
103 g_object_unref (priv->arrow_button);
110 /* Transfer a possible tooltip to the new box */
111 g_object_get (priv->box, "tooltip-markup", &tmp, NULL);
115 g_object_set (box, "tooltip-markup", tmp, NULL);
119 /* Note: we are not destroying the button and the arrow_button
120 * here because they were removed from their container above
122 gtk_widget_destroy (priv->box);
127 gtk_container_add (GTK_CONTAINER (button), priv->box);
128 gtk_widget_show_all (priv->box);
130 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
131 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
133 gtk_widget_queue_resize (GTK_WIDGET (button));
137 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
139 gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
142 GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
146 gtk_menu_tool_button_state_changed (GtkWidget *widget,
147 GtkStateType previous_state)
149 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
150 GtkMenuToolButtonPrivate *priv = button->priv;
152 if (!GTK_WIDGET_IS_SENSITIVE (widget) && priv->menu)
154 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
159 gtk_menu_tool_button_set_property (GObject *object,
164 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
169 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 gtk_menu_tool_button_get_property (GObject *object,
184 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
189 g_value_set_object (value, button->priv->menu);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
201 GObjectClass *object_class;
202 GtkObjectClass *gtk_object_class;
203 GtkWidgetClass *widget_class;
204 GtkToolItemClass *toolitem_class;
206 object_class = (GObjectClass *)klass;
207 gtk_object_class = (GtkObjectClass *)klass;
208 widget_class = (GtkWidgetClass *)klass;
209 toolitem_class = (GtkToolItemClass *)klass;
211 object_class->set_property = gtk_menu_tool_button_set_property;
212 object_class->get_property = gtk_menu_tool_button_get_property;
213 gtk_object_class->destroy = gtk_menu_tool_button_destroy;
214 widget_class->state_changed = gtk_menu_tool_button_state_changed;
215 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
218 g_signal_new (I_("show-menu"),
219 G_OBJECT_CLASS_TYPE (klass),
221 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
223 g_cclosure_marshal_VOID__VOID,
226 g_object_class_install_property (object_class,
228 g_param_spec_object ("menu",
230 P_("The dropdown menu"),
232 GTK_PARAM_READWRITE));
234 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
238 menu_position_func (GtkMenu *menu,
242 GtkMenuToolButton *button)
244 GtkMenuToolButtonPrivate *priv = button->priv;
245 GtkWidget *widget = GTK_WIDGET (button);
247 GtkRequisition menu_req;
248 GtkOrientation orientation;
249 GtkTextDirection direction;
250 GdkRectangle monitor;
254 gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_req);
256 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
257 direction = gtk_widget_get_direction (widget);
259 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
260 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
263 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
265 if (orientation == GTK_ORIENTATION_HORIZONTAL)
267 gdk_window_get_origin (widget->window, x, y);
268 *x += widget->allocation.x;
269 *y += widget->allocation.y;
271 if (direction == GTK_TEXT_DIR_LTR)
272 *x += MAX (widget->allocation.width - menu_req.width, 0);
273 else if (menu_req.width > widget->allocation.width)
274 *x -= menu_req.width - widget->allocation.width;
276 if ((*y + priv->arrow_button->allocation.height + menu_req.height) <= monitor.y + monitor.height)
277 *y += priv->arrow_button->allocation.height;
278 else if ((*y - menu_req.height) >= monitor.y)
279 *y -= menu_req.height;
280 else if (monitor.y + monitor.height - (*y + priv->arrow_button->allocation.height) > *y)
281 *y += priv->arrow_button->allocation.height;
283 *y -= menu_req.height;
287 gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
288 gtk_widget_size_request (priv->arrow_button, &req);
290 if (direction == GTK_TEXT_DIR_LTR)
291 *x += priv->arrow_button->allocation.width;
293 *x -= menu_req.width;
295 if (*y + menu_req.height > monitor.y + monitor.height &&
296 *y + priv->arrow_button->allocation.height - monitor.y > monitor.y + monitor.height - *y)
297 *y += priv->arrow_button->allocation.height - menu_req.height;
304 popup_menu_under_arrow (GtkMenuToolButton *button,
305 GdkEventButton *event)
307 GtkMenuToolButtonPrivate *priv = button->priv;
309 g_signal_emit (button, signals[SHOW_MENU], 0);
314 gtk_menu_popup (priv->menu, NULL, NULL,
315 (GtkMenuPositionFunc) menu_position_func,
317 event ? event->button : 0,
318 event ? event->time : gtk_get_current_event_time ());
322 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
323 GtkMenuToolButton *button)
325 GtkMenuToolButtonPrivate *priv = button->priv;
330 if (gtk_toggle_button_get_active (togglebutton) &&
331 !GTK_WIDGET_VISIBLE (priv->menu))
333 /* we get here only when the menu is activated by a key
334 * press, so that we can select the first menu item */
335 popup_menu_under_arrow (button, NULL);
336 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
341 arrow_button_button_press_event_cb (GtkWidget *widget,
342 GdkEventButton *event,
343 GtkMenuToolButton *button)
345 if (event->button == 1)
347 popup_menu_under_arrow (button, event);
348 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
359 gtk_menu_tool_button_init (GtkMenuToolButton *button)
363 GtkWidget *arrow_button;
364 GtkWidget *real_button;
366 button->priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
368 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
370 box = gtk_hbox_new (FALSE, 0);
372 real_button = GTK_BIN (button)->child;
373 g_object_ref (real_button);
374 gtk_container_remove (GTK_CONTAINER (button), real_button);
375 gtk_container_add (GTK_CONTAINER (box), real_button);
376 g_object_unref (real_button);
378 arrow_button = gtk_toggle_button_new ();
379 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
380 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
381 gtk_box_pack_end (GTK_BOX (box), arrow_button,
384 /* the arrow button is insentive until we set a menu */
385 gtk_widget_set_sensitive (arrow_button, FALSE);
387 gtk_widget_show_all (box);
389 gtk_container_add (GTK_CONTAINER (button), box);
391 button->priv->button = real_button;
392 button->priv->arrow = arrow;
393 button->priv->arrow_button = arrow_button;
394 button->priv->box = box;
396 g_signal_connect (arrow_button, "toggled",
397 G_CALLBACK (arrow_button_toggled_cb), button);
398 g_signal_connect (arrow_button, "button_press_event",
399 G_CALLBACK (arrow_button_button_press_event_cb), button);
403 gtk_menu_tool_button_destroy (GtkObject *object)
405 GtkMenuToolButton *button;
407 button = GTK_MENU_TOOL_BUTTON (object);
409 if (button->priv->menu)
411 g_signal_handlers_disconnect_by_func (button->priv->menu,
414 gtk_menu_detach (button->priv->menu);
416 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
417 arrow_button_toggled_cb,
419 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
420 arrow_button_button_press_event_cb,
424 if (GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy)
425 (*GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy) (object);
429 * gtk_menu_tool_button_new:
430 * @icon_widget: a widget that will be used as icon widget, or %NULL
431 * @label: a string that will be used as label, or %NULL
433 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
436 * Return value: the new #GtkMenuToolButton
441 gtk_menu_tool_button_new (GtkWidget *icon_widget,
444 GtkMenuToolButton *button;
446 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
449 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
452 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
454 return GTK_TOOL_ITEM (button);
458 * gtk_menu_tool_button_new_from_stock:
459 * @stock_id: the name of a stock item
461 * Creates a new #GtkMenuToolButton.
462 * The new #GtkMenuToolButton will contain an icon and label from
463 * the stock item indicated by @stock_id.
465 * Return value: the new #GtkMenuToolButton
470 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
472 GtkMenuToolButton *button;
474 g_return_val_if_fail (stock_id != NULL, NULL);
476 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
477 "stock-id", stock_id,
480 return GTK_TOOL_ITEM (button);
483 /* Callback for the "deactivate" signal on the pop-up menu.
484 * This is used so that we unset the state of the toggle button
485 * when the pop-up menu disappears.
488 menu_deactivate_cb (GtkMenuShell *menu_shell,
489 GtkMenuToolButton *button)
491 GtkMenuToolButtonPrivate *priv = button->priv;
493 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
499 menu_detacher (GtkWidget *widget,
502 GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
504 g_return_if_fail (priv->menu == menu);
510 * gtk_menu_tool_button_set_menu:
511 * @button: a #GtkMenuToolButton
512 * @menu: the #GtkMenu associated with #GtkMenuToolButton
514 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
515 * If @menu is NULL, the arrow button becomes insensitive.
520 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
523 GtkMenuToolButtonPrivate *priv;
525 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
526 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
530 if (priv->menu != GTK_MENU (menu))
532 if (priv->menu && GTK_WIDGET_VISIBLE (priv->menu))
533 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
537 g_signal_handlers_disconnect_by_func (priv->menu,
540 gtk_menu_detach (priv->menu);
543 priv->menu = GTK_MENU (menu);
547 gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
550 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
552 g_signal_connect (priv->menu, "deactivate",
553 G_CALLBACK (menu_deactivate_cb), button);
556 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
559 g_object_notify (G_OBJECT (button), "menu");
563 * gtk_menu_tool_button_get_menu:
564 * @button: a #GtkMenuToolButton
566 * Gets the #GtkMenu associated with #GtkMenuToolButton.
568 * Return value: the #GtkMenu associated with #GtkMenuToolButton
573 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
575 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
577 return GTK_WIDGET (button->priv->menu);
581 * gtk_menu_tool_button_set_arrow_tooltip:
582 * @button: a #GtkMenuToolButton
583 * @tooltips: the #GtkTooltips object to be used
584 * @tip_text: text to be used as tooltip text for tool_item
585 * @tip_private: text to be used as private tooltip text
587 * Sets the #GtkTooltips object to be used for arrow button which
588 * pops up the menu. See gtk_tool_item_set_tooltip() for setting
589 * a tooltip on the whole #GtkMenuToolButton.
593 * Deprecated: 2.12: Use gtk_menu_tool_button_set_arrow_tooltip_text()
597 gtk_menu_tool_button_set_arrow_tooltip (GtkMenuToolButton *button,
598 GtkTooltips *tooltips,
599 const gchar *tip_text,
600 const gchar *tip_private)
602 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
604 gtk_tooltips_set_tip (tooltips, button->priv->arrow_button, tip_text, tip_private);
608 * gtk_menu_tool_button_set_arrow_tooltip_text:
609 * @button: a #GtkMenuToolButton
610 * @text: text to be used as tooltip text for button's arrow button
612 * Sets the tooltip text to be used as tooltip for the arrow button which
613 * pops up the menu. See gtk_tool_item_set_tooltip() for setting a tooltip
614 * on the whole #GtkMenuToolButton.
619 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
622 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
624 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
628 * gtk_menu_tool_button_set_arrow_tooltip_markup:
629 * @button: a #GtkMenuToolButton
630 * @markup: markup text to be used as tooltip text for button's arrow button
632 * Sets the tooltip markup text to be used as tooltip for the arrow button
633 * which pops up the menu. See gtk_tool_item_set_tooltip() for setting a
634 * tooltip on the whole #GtkMenuToolButton.
639 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
642 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
644 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);
647 #define __GTK_MENU_TOOL_BUTTON_C__
648 #include "gtkaliasdef.c"