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_init (GtkMenuToolButton *button);
47 static void gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass);
48 static void gtk_menu_tool_button_finalize (GObject *object);
63 static gint signals[LAST_SIGNAL];
65 static GObjectClass *parent_class = NULL;
68 gtk_menu_tool_button_get_type (void)
70 static GType type = 0;
74 static const GTypeInfo info =
76 sizeof (GtkMenuToolButtonClass),
78 (GBaseFinalizeFunc) 0,
79 (GClassInitFunc) gtk_menu_tool_button_class_init,
80 (GClassFinalizeFunc) 0,
82 sizeof (GtkMenuToolButton),
84 (GInstanceInitFunc) gtk_menu_tool_button_init
87 type = g_type_register_static (GTK_TYPE_TOOL_BUTTON,
96 gtk_menu_tool_button_set_tooltip (GtkToolItem *tool_item,
97 GtkTooltips *tooltips,
99 const char *tip_private)
101 GtkMenuToolButton *button;
103 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (tool_item), FALSE);
105 button = GTK_MENU_TOOL_BUTTON (tool_item);
106 gtk_tooltips_set_tip (tooltips, button->priv->button, tip_text, tip_private);
112 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
114 GtkMenuToolButtonPrivate *priv = button->priv;
116 GtkOrientation orientation;
118 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
120 if (orientation == GTK_ORIENTATION_HORIZONTAL)
122 box = gtk_hbox_new (FALSE, 0);
123 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
127 box = gtk_vbox_new (FALSE, 0);
128 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
131 if (priv->button && priv->button->parent)
133 g_object_ref (priv->button);
134 gtk_container_remove (GTK_CONTAINER (priv->button->parent),
136 gtk_container_add (GTK_CONTAINER (box), priv->button);
137 g_object_unref (priv->button);
140 if (priv->arrow_button && priv->arrow_button->parent)
142 g_object_ref (priv->arrow_button);
143 gtk_container_remove (GTK_CONTAINER (priv->arrow_button->parent),
145 gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
147 g_object_unref (priv->arrow_button);
152 /* Note: we are not destroying the button and the arrow_button
153 * here because they were removed from their container above
155 gtk_widget_destroy (priv->box);
160 gtk_container_add (GTK_CONTAINER (button), priv->box);
161 gtk_widget_show_all (priv->box);
163 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
164 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
166 gtk_widget_queue_resize (GTK_WIDGET (button));
170 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
172 gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
175 GTK_TOOL_ITEM_CLASS (parent_class)->toolbar_reconfigured (toolitem);
179 gtk_menu_tool_button_state_changed (GtkWidget *widget,
180 GtkStateType previous_state)
182 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
183 GtkMenuToolButtonPrivate *priv = button->priv;
185 if (!GTK_WIDGET_IS_SENSITIVE (widget) && priv->menu)
187 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
192 gtk_menu_tool_button_set_property (GObject *object,
197 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
202 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 gtk_menu_tool_button_get_property (GObject *object,
217 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
222 g_value_set_object (value, button->priv->menu);
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
234 GObjectClass *object_class;
235 GtkWidgetClass *widget_class;
236 GtkToolItemClass *toolitem_class;
237 GtkToolButtonClass *toolbutton_class;
239 parent_class = g_type_class_peek_parent (klass);
241 object_class = (GObjectClass *)klass;
242 widget_class = (GtkWidgetClass *)klass;
243 toolitem_class = (GtkToolItemClass *)klass;
244 toolbutton_class = (GtkToolButtonClass *)klass;
246 object_class->set_property = gtk_menu_tool_button_set_property;
247 object_class->get_property = gtk_menu_tool_button_get_property;
248 object_class->finalize = gtk_menu_tool_button_finalize;
249 widget_class->state_changed = gtk_menu_tool_button_state_changed;
250 toolitem_class->set_tooltip = gtk_menu_tool_button_set_tooltip;
251 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
254 g_signal_new ("show-menu",
255 G_OBJECT_CLASS_TYPE (klass),
257 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
259 g_cclosure_marshal_VOID__VOID,
262 g_object_class_install_property (object_class,
264 g_param_spec_object ("menu",
266 P_("The dropdown menu"),
268 GTK_PARAM_READWRITE));
270 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
274 menu_position_func (GtkMenu *menu,
278 GtkMenuToolButton *button)
280 GtkMenuToolButtonPrivate *priv = button->priv;
282 GtkRequisition menu_req;
283 GtkOrientation orientation;
284 GtkTextDirection direction;
286 gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
287 gtk_widget_size_request (priv->arrow_button, &req);
288 gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_req);
290 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
291 direction = gtk_widget_get_direction (GTK_WIDGET (priv->arrow_button));
293 if (orientation == GTK_ORIENTATION_HORIZONTAL)
295 if (direction == GTK_TEXT_DIR_LTR)
296 *x += priv->arrow_button->allocation.width - req.width;
298 *x += req.width - menu_req.width;
299 *y += priv->arrow_button->allocation.height;
303 if (direction == GTK_TEXT_DIR_LTR)
304 *x += priv->arrow_button->allocation.width;
306 *x -= menu_req.width;
307 *y += priv->arrow_button->allocation.height - req.height;
314 popup_menu_under_arrow (GtkMenuToolButton *button,
315 GdkEventButton *event)
317 GtkMenuToolButtonPrivate *priv = button->priv;
319 g_signal_emit (button, signals[SHOW_MENU], 0);
324 gtk_menu_popup (priv->menu, NULL, NULL,
325 (GtkMenuPositionFunc) menu_position_func,
327 event ? event->button : 0,
328 event ? event->time : gtk_get_current_event_time ());
332 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
333 GtkMenuToolButton *button)
335 GtkMenuToolButtonPrivate *priv = button->priv;
340 if (gtk_toggle_button_get_active (togglebutton) &&
341 !GTK_WIDGET_VISIBLE (priv->menu))
343 /* we get here only when the menu is activated by a key
344 * press, so that we can select the first menu item */
345 popup_menu_under_arrow (button, NULL);
346 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
351 arrow_button_button_press_event_cb (GtkWidget *widget,
352 GdkEventButton *event,
353 GtkMenuToolButton *button)
355 if (event->button == 1)
357 popup_menu_under_arrow (button, event);
358 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
369 gtk_menu_tool_button_init (GtkMenuToolButton *button)
373 GtkWidget *arrow_button;
374 GtkWidget *real_button;
376 button->priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
378 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
380 box = gtk_hbox_new (FALSE, 0);
382 real_button = GTK_BIN (button)->child;
383 g_object_ref (real_button);
384 gtk_container_remove (GTK_CONTAINER (button), real_button);
385 gtk_container_add (GTK_CONTAINER (box), real_button);
386 g_object_unref (real_button);
388 arrow_button = gtk_toggle_button_new ();
389 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
390 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
391 gtk_box_pack_end (GTK_BOX (box), arrow_button,
394 /* the arrow button is insentive until we set a menu */
395 gtk_widget_set_sensitive (arrow_button, FALSE);
397 gtk_widget_show_all (box);
399 gtk_container_add (GTK_CONTAINER (button), box);
401 button->priv->button = real_button;
402 button->priv->arrow = arrow;
403 button->priv->arrow_button = arrow_button;
404 button->priv->box = box;
406 g_signal_connect (arrow_button, "toggled",
407 G_CALLBACK (arrow_button_toggled_cb), button);
408 g_signal_connect (arrow_button, "button_press_event",
409 G_CALLBACK (arrow_button_button_press_event_cb), button);
413 gtk_menu_tool_button_finalize (GObject *object)
415 GtkMenuToolButton *button;
417 button = GTK_MENU_TOOL_BUTTON (object);
419 if (button->priv->menu)
420 g_object_unref (button->priv->menu);
422 G_OBJECT_CLASS (parent_class)->finalize (object);
426 * gtk_menu_tool_button_new:
427 * @icon_widget: a widget that will be used as icon widget, or %NULL
428 * @label: a string that will be used as label, or %NULL
430 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
433 * Return value: the new #GtkMenuToolButton
438 gtk_menu_tool_button_new (GtkWidget *icon_widget,
441 GtkMenuToolButton *button;
443 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
446 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
449 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
451 return GTK_TOOL_ITEM (button);
455 * gtk_menu_tool_button_new_from_stock:
456 * @stock_id: the name of a stock item
458 * Creates a new #GtkMenuToolButton.
459 * The new #GtkMenuToolButton will contain an icon and label from
460 * the stock item indicated by @stock_id.
462 * Return value: the new #GtkMenuToolButton
467 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
469 GtkMenuToolButton *button;
471 g_return_val_if_fail (stock_id != NULL, NULL);
473 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
474 "stock-id", stock_id,
477 return GTK_TOOL_ITEM (button);
480 /* Callback for the "deactivate" signal on the pop-up menu.
481 * This is used so that we unset the state of the toggle button
482 * when the pop-up menu disappears.
485 menu_deactivate_cb (GtkMenuShell *menu_shell,
486 GtkMenuToolButton *button)
488 GtkMenuToolButtonPrivate *priv = button->priv;
490 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
496 * gtk_menu_tool_button_set_menu:
497 * @button: a #GtkMenuToolButton
498 * @menu: the #GtkMenu associated with #GtkMenuToolButton
500 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
501 * If @menu is NULL, the arrow button becomes insensitive.
506 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
509 GtkMenuToolButtonPrivate *priv;
511 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
512 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
516 if (priv->menu != GTK_MENU (menu))
518 if (priv->menu && GTK_WIDGET_VISIBLE (priv->menu))
519 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
522 g_object_unref (priv->menu);
524 priv->menu = GTK_MENU (menu);
528 g_object_ref (priv->menu);
529 gtk_object_sink (GTK_OBJECT (priv->menu));
531 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
533 g_signal_connect (button->priv->menu, "deactivate",
534 G_CALLBACK (menu_deactivate_cb), button);
537 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
540 g_object_notify (G_OBJECT (button), "menu");
544 * gtk_menu_tool_button_get_menu:
545 * @button: a #GtkMenuToolButton
547 * Gets the #GtkMenu associated with #GtkMenuToolButton.
549 * Return value: the #GtkMenu associated with #GtkMenuToolButton
554 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
556 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
558 return GTK_WIDGET (button->priv->menu);
562 * gtk_menu_tool_button_set_arrow_tooltip:
563 * @button: a #GtkMenuToolButton
564 * @tooltips: the #GtkTooltips object to be used
565 * @tip_text: text to be used as tooltip text for tool_item
566 * @tip_private: text to be used as private tooltip text
568 * Sets the #GtkTooltips object to be used for arrow button which
569 * pops up the menu. See gtk_tool_item_set_tooltip() for setting
570 * a tooltip on the whole #GtkMenuToolButton.
575 gtk_menu_tool_button_set_arrow_tooltip (GtkMenuToolButton *button,
576 GtkTooltips *tooltips,
577 const gchar *tip_text,
578 const gchar *tip_private)
580 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
582 gtk_tooltips_set_tip (tooltips, button->priv->arrow_button, tip_text, tip_private);
585 #define __GTK_MENU_TOOL_BUTTON_C__
586 #include "gtkaliasdef.c"