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 g_signal_new (I_("show-menu"),
222 G_OBJECT_CLASS_TYPE (klass),
224 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
226 g_cclosure_marshal_VOID__VOID,
229 g_object_class_install_property (object_class,
231 g_param_spec_object ("menu",
233 P_("The dropdown menu"),
235 GTK_PARAM_READWRITE));
237 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
241 menu_position_func (GtkMenu *menu,
245 GtkMenuToolButton *button)
247 GtkMenuToolButtonPrivate *priv = button->priv;
248 GtkWidget *widget = GTK_WIDGET (button);
250 GtkRequisition menu_req;
251 GtkOrientation orientation;
252 GtkTextDirection direction;
253 GdkRectangle monitor;
257 gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_req);
259 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
260 direction = gtk_widget_get_direction (widget);
262 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
263 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
266 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
268 if (orientation == GTK_ORIENTATION_HORIZONTAL)
270 gdk_window_get_origin (widget->window, x, y);
271 *x += widget->allocation.x;
272 *y += widget->allocation.y;
274 if (direction == GTK_TEXT_DIR_LTR)
275 *x += MAX (widget->allocation.width - menu_req.width, 0);
276 else if (menu_req.width > widget->allocation.width)
277 *x -= menu_req.width - widget->allocation.width;
279 if ((*y + priv->arrow_button->allocation.height + menu_req.height) <= monitor.y + monitor.height)
280 *y += priv->arrow_button->allocation.height;
281 else if ((*y - menu_req.height) >= monitor.y)
282 *y -= menu_req.height;
283 else if (monitor.y + monitor.height - (*y + priv->arrow_button->allocation.height) > *y)
284 *y += priv->arrow_button->allocation.height;
286 *y -= menu_req.height;
290 gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
291 gtk_widget_size_request (priv->arrow_button, &req);
293 if (direction == GTK_TEXT_DIR_LTR)
294 *x += priv->arrow_button->allocation.width;
296 *x -= menu_req.width;
298 if (*y + menu_req.height > monitor.y + monitor.height &&
299 *y + priv->arrow_button->allocation.height - monitor.y > monitor.y + monitor.height - *y)
300 *y += priv->arrow_button->allocation.height - menu_req.height;
307 popup_menu_under_arrow (GtkMenuToolButton *button,
308 GdkEventButton *event)
310 GtkMenuToolButtonPrivate *priv = button->priv;
312 g_signal_emit (button, signals[SHOW_MENU], 0);
317 gtk_menu_popup (priv->menu, NULL, NULL,
318 (GtkMenuPositionFunc) menu_position_func,
320 event ? event->button : 0,
321 event ? event->time : gtk_get_current_event_time ());
325 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
326 GtkMenuToolButton *button)
328 GtkMenuToolButtonPrivate *priv = button->priv;
333 if (gtk_toggle_button_get_active (togglebutton) &&
334 !GTK_WIDGET_VISIBLE (priv->menu))
336 /* we get here only when the menu is activated by a key
337 * press, so that we can select the first menu item */
338 popup_menu_under_arrow (button, NULL);
339 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
344 arrow_button_button_press_event_cb (GtkWidget *widget,
345 GdkEventButton *event,
346 GtkMenuToolButton *button)
348 if (event->button == 1)
350 popup_menu_under_arrow (button, event);
351 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
362 gtk_menu_tool_button_init (GtkMenuToolButton *button)
366 GtkWidget *arrow_button;
367 GtkWidget *real_button;
369 button->priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
371 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
373 box = gtk_hbox_new (FALSE, 0);
375 real_button = GTK_BIN (button)->child;
376 g_object_ref (real_button);
377 gtk_container_remove (GTK_CONTAINER (button), real_button);
378 gtk_container_add (GTK_CONTAINER (box), real_button);
379 g_object_unref (real_button);
381 arrow_button = gtk_toggle_button_new ();
382 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
383 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
384 gtk_box_pack_end (GTK_BOX (box), arrow_button,
387 /* the arrow button is insentive until we set a menu */
388 gtk_widget_set_sensitive (arrow_button, FALSE);
390 gtk_widget_show_all (box);
392 gtk_container_add (GTK_CONTAINER (button), box);
394 button->priv->button = real_button;
395 button->priv->arrow = arrow;
396 button->priv->arrow_button = arrow_button;
397 button->priv->box = box;
399 g_signal_connect (arrow_button, "toggled",
400 G_CALLBACK (arrow_button_toggled_cb), button);
401 g_signal_connect (arrow_button, "button_press_event",
402 G_CALLBACK (arrow_button_button_press_event_cb), button);
406 gtk_menu_tool_button_destroy (GtkObject *object)
408 GtkMenuToolButton *button;
410 button = GTK_MENU_TOOL_BUTTON (object);
412 if (button->priv->menu)
414 g_signal_handlers_disconnect_by_func (button->priv->menu,
417 gtk_menu_detach (button->priv->menu);
419 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
420 arrow_button_toggled_cb,
422 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
423 arrow_button_button_press_event_cb,
427 if (GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy)
428 (*GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy) (object);
432 * gtk_menu_tool_button_new:
433 * @icon_widget: a widget that will be used as icon widget, or %NULL
434 * @label: a string that will be used as label, or %NULL
436 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
439 * Return value: the new #GtkMenuToolButton
444 gtk_menu_tool_button_new (GtkWidget *icon_widget,
447 GtkMenuToolButton *button;
449 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
452 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
455 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
457 return GTK_TOOL_ITEM (button);
461 * gtk_menu_tool_button_new_from_stock:
462 * @stock_id: the name of a stock item
464 * Creates a new #GtkMenuToolButton.
465 * The new #GtkMenuToolButton will contain an icon and label from
466 * the stock item indicated by @stock_id.
468 * Return value: the new #GtkMenuToolButton
473 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
475 GtkMenuToolButton *button;
477 g_return_val_if_fail (stock_id != NULL, NULL);
479 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
480 "stock-id", stock_id,
483 return GTK_TOOL_ITEM (button);
486 /* Callback for the "deactivate" signal on the pop-up menu.
487 * This is used so that we unset the state of the toggle button
488 * when the pop-up menu disappears.
491 menu_deactivate_cb (GtkMenuShell *menu_shell,
492 GtkMenuToolButton *button)
494 GtkMenuToolButtonPrivate *priv = button->priv;
496 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
502 menu_detacher (GtkWidget *widget,
505 GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
507 g_return_if_fail (priv->menu == menu);
513 * gtk_menu_tool_button_set_menu:
514 * @button: a #GtkMenuToolButton
515 * @menu: the #GtkMenu associated with #GtkMenuToolButton
517 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
518 * If @menu is NULL, the arrow button becomes insensitive.
523 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
526 GtkMenuToolButtonPrivate *priv;
528 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
529 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
533 if (priv->menu != GTK_MENU (menu))
535 if (priv->menu && GTK_WIDGET_VISIBLE (priv->menu))
536 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
540 g_signal_handlers_disconnect_by_func (priv->menu,
543 gtk_menu_detach (priv->menu);
546 priv->menu = GTK_MENU (menu);
550 gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
553 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
555 g_signal_connect (priv->menu, "deactivate",
556 G_CALLBACK (menu_deactivate_cb), button);
559 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
562 g_object_notify (G_OBJECT (button), "menu");
566 * gtk_menu_tool_button_get_menu:
567 * @button: a #GtkMenuToolButton
569 * Gets the #GtkMenu associated with #GtkMenuToolButton.
571 * Return value: the #GtkMenu associated with #GtkMenuToolButton
576 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
578 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
580 return GTK_WIDGET (button->priv->menu);
584 * gtk_menu_tool_button_set_arrow_tooltip:
585 * @button: a #GtkMenuToolButton
586 * @tooltips: the #GtkTooltips object to be used
587 * @tip_text: text to be used as tooltip text for tool_item
588 * @tip_private: text to be used as private tooltip text
590 * Sets the #GtkTooltips object to be used for arrow button which
591 * pops up the menu. See gtk_tool_item_set_tooltip() for setting
592 * a tooltip on the whole #GtkMenuToolButton.
596 * Deprecated: 2.12: Use gtk_menu_tool_button_set_arrow_tooltip_text()
600 gtk_menu_tool_button_set_arrow_tooltip (GtkMenuToolButton *button,
601 GtkTooltips *tooltips,
602 const gchar *tip_text,
603 const gchar *tip_private)
605 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
607 gtk_tooltips_set_tip (tooltips, button->priv->arrow_button, tip_text, tip_private);
611 * gtk_menu_tool_button_set_arrow_tooltip_text:
612 * @button: a #GtkMenuToolButton
613 * @text: text to be used as tooltip text for button's arrow button
615 * Sets the tooltip text to be used as tooltip for the arrow button which
616 * pops up the menu. See gtk_tool_item_set_tooltip() for setting a tooltip
617 * on the whole #GtkMenuToolButton.
622 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
625 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
627 gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
631 * gtk_menu_tool_button_set_arrow_tooltip_markup:
632 * @button: a #GtkMenuToolButton
633 * @markup: markup text to be used as tooltip text for button's arrow button
635 * Sets the tooltip markup text to be used as tooltip for the arrow button
636 * which pops up the menu. See gtk_tool_item_set_tooltip() for setting a
637 * tooltip on the whole #GtkMenuToolButton.
642 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
645 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
647 gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);
650 #define __GTK_MENU_TOOL_BUTTON_C__
651 #include "gtkaliasdef.c"