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"
26 #include "gtktogglebutton.h"
34 #define GTK_MENU_TOOL_BUTTON_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GTK_TYPE_MENU_TOOL_BUTTON, GtkMenuToolButtonPrivate))
36 struct _GtkMenuToolButtonPrivate
40 GtkWidget *arrow_button;
45 static void gtk_menu_tool_button_init (GtkMenuToolButton *button);
46 static void gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass);
47 static void gtk_menu_tool_button_finalize (GObject *object);
62 static gint signals[LAST_SIGNAL];
64 static GObjectClass *parent_class = NULL;
67 gtk_menu_tool_button_get_type (void)
69 static GType type = 0;
73 static const GTypeInfo info =
75 sizeof (GtkMenuToolButtonClass),
77 (GBaseFinalizeFunc) 0,
78 (GClassInitFunc) gtk_menu_tool_button_class_init,
79 (GClassFinalizeFunc) 0,
81 sizeof (GtkMenuToolButton),
83 (GInstanceInitFunc) gtk_menu_tool_button_init
86 type = g_type_register_static (GTK_TYPE_TOOL_BUTTON,
95 gtk_menu_tool_button_set_tooltip (GtkToolItem *tool_item,
96 GtkTooltips *tooltips,
98 const char *tip_private)
100 GtkMenuToolButton *button;
102 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (tool_item), FALSE);
104 button = GTK_MENU_TOOL_BUTTON (tool_item);
105 gtk_tooltips_set_tip (tooltips, button->priv->button, tip_text, tip_private);
111 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
113 GtkMenuToolButtonPrivate *priv;
115 GtkOrientation orientation;
117 priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
119 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
121 if (orientation == GTK_ORIENTATION_HORIZONTAL)
123 box = gtk_hbox_new (FALSE, 0);
124 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
128 box = gtk_vbox_new (FALSE, 0);
129 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
132 if (priv->button && priv->button->parent)
134 g_object_ref (priv->button);
135 gtk_container_remove (GTK_CONTAINER (priv->button->parent),
137 gtk_container_add (GTK_CONTAINER (box), priv->button);
138 g_object_unref (priv->button);
141 if (priv->arrow_button && priv->arrow_button->parent)
143 g_object_ref (priv->arrow_button);
144 gtk_container_remove (GTK_CONTAINER (priv->arrow_button->parent),
146 gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
148 g_object_unref (priv->arrow_button);
153 /* Note: we are not destroying the button and the arrow_button
154 * here because they were removed from their container above
156 gtk_widget_destroy (priv->box);
161 gtk_container_add (GTK_CONTAINER (button), priv->box);
162 gtk_widget_show_all (priv->box);
164 gtk_widget_queue_resize (GTK_WIDGET (button));
168 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
170 gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
173 GTK_TOOL_ITEM_CLASS (parent_class)->toolbar_reconfigured (toolitem);
177 gtk_menu_tool_button_set_property (GObject *object,
182 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
187 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
197 gtk_menu_tool_button_get_property (GObject *object,
202 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
207 g_value_set_object (value, button->priv->menu);
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
219 GObjectClass *object_class;
220 GtkToolItemClass *toolitem_class;
221 GtkToolButtonClass *toolbutton_class;
223 parent_class = g_type_class_peek_parent (klass);
225 object_class = (GObjectClass *)klass;
226 toolitem_class = (GtkToolItemClass *)klass;
227 toolbutton_class = (GtkToolButtonClass *)klass;
229 object_class->set_property = gtk_menu_tool_button_set_property;
230 object_class->get_property = gtk_menu_tool_button_get_property;
231 object_class->finalize = gtk_menu_tool_button_finalize;
232 toolitem_class->set_tooltip = gtk_menu_tool_button_set_tooltip;
233 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
236 g_signal_new ("show-menu",
237 G_OBJECT_CLASS_TYPE (klass),
239 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
241 g_cclosure_marshal_VOID__VOID,
244 g_object_class_install_property (object_class,
246 g_param_spec_object ("menu",
248 P_("The dropdown menu"),
250 G_PARAM_READABLE | G_PARAM_WRITABLE));
252 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
256 button_state_changed_cb (GtkWidget *widget,
257 GtkStateType previous_state,
258 GtkMenuToolButton *button)
260 GtkMenuToolButtonPrivate *priv;
262 GtkStateType state = GTK_WIDGET_STATE (widget);
264 priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
266 b = (widget == priv->arrow_button) ? priv->button : priv->arrow_button;
268 g_signal_handlers_block_by_func (G_OBJECT (b),
269 G_CALLBACK (button_state_changed_cb),
272 if (state == GTK_STATE_PRELIGHT &&
273 previous_state != GTK_STATE_ACTIVE)
275 gtk_widget_set_state (b, state);
277 else if (state == GTK_STATE_NORMAL)
279 gtk_widget_set_state (b, state);
281 else if (state == GTK_STATE_ACTIVE)
283 gtk_widget_set_state (b, GTK_STATE_NORMAL);
286 g_signal_handlers_unblock_by_func (G_OBJECT (b),
287 G_CALLBACK (button_state_changed_cb),
292 menu_position_func (GtkMenu *menu,
296 GtkMenuToolButton *button)
298 GtkMenuToolButtonPrivate *priv;
300 GtkRequisition menu_req;
301 GtkOrientation orientation;
302 GtkTextDirection direction;
304 priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
306 gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
307 gtk_widget_size_request (priv->arrow_button, &req);
308 gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_req);
310 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
311 direction = gtk_widget_get_direction (GTK_WIDGET (priv->arrow_button));
313 if (orientation == GTK_ORIENTATION_HORIZONTAL)
315 if (direction == GTK_TEXT_DIR_LTR)
316 *x += priv->arrow_button->allocation.width - req.width;
318 *x += req.width - menu_req.width;
319 *y += priv->arrow_button->allocation.height;
323 if (direction == GTK_TEXT_DIR_LTR)
324 *x += priv->arrow_button->allocation.width;
326 *x -= menu_req.width;
327 *y += priv->arrow_button->allocation.height - req.height;
334 popup_menu_under_arrow (GtkMenuToolButton *button,
335 GdkEventButton *event)
337 GtkMenuToolButtonPrivate *priv;
339 priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
341 g_signal_emit (button, signals[SHOW_MENU], 0);
346 gtk_menu_popup (priv->menu, NULL, NULL,
347 (GtkMenuPositionFunc) menu_position_func,
349 event ? event->button : 0,
350 event ? event->time : gtk_get_current_event_time ());
354 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
355 GtkMenuToolButton *button)
357 GtkMenuToolButtonPrivate *priv;
359 priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
364 if (gtk_toggle_button_get_active (togglebutton) &&
365 !GTK_WIDGET_VISIBLE (priv->menu))
367 /* we get here only when the menu is activated by a key
368 * press, so that we can select the first menu item */
369 popup_menu_under_arrow (button, NULL);
370 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
375 arrow_button_button_press_event_cb (GtkWidget *widget,
376 GdkEventButton *event,
377 GtkMenuToolButton *button)
379 popup_menu_under_arrow (button, event);
380 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
386 gtk_menu_tool_button_init (GtkMenuToolButton *button)
390 GtkWidget *arrow_button;
391 GtkWidget *real_button;
393 button->priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
395 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
397 box = gtk_hbox_new (FALSE, 0);
399 real_button = GTK_BIN (button)->child;
400 g_object_ref (real_button);
401 gtk_container_remove (GTK_CONTAINER (button), real_button);
402 gtk_container_add (GTK_CONTAINER (box), real_button);
403 g_object_unref (real_button);
405 arrow_button = gtk_toggle_button_new ();
406 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
407 gtk_button_set_relief (GTK_BUTTON (arrow_button), GTK_RELIEF_NONE);
408 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
409 gtk_box_pack_end (GTK_BOX (box), arrow_button,
412 /* the arrow button is insentive until we set a menu */
413 gtk_widget_set_sensitive (arrow_button, FALSE);
415 gtk_widget_show_all (box);
417 gtk_container_add (GTK_CONTAINER (button), box);
419 button->priv->button = real_button;
420 button->priv->arrow = arrow;
421 button->priv->arrow_button = arrow_button;
422 button->priv->box = box;
424 g_signal_connect (real_button, "state_changed",
425 G_CALLBACK (button_state_changed_cb), button);
426 g_signal_connect (arrow_button, "state_changed",
427 G_CALLBACK (button_state_changed_cb), button);
428 g_signal_connect (arrow_button, "toggled",
429 G_CALLBACK (arrow_button_toggled_cb), button);
430 g_signal_connect (arrow_button, "button_press_event",
431 G_CALLBACK (arrow_button_button_press_event_cb), button);
435 gtk_menu_tool_button_finalize (GObject *object)
437 GtkMenuToolButton *button;
439 button = GTK_MENU_TOOL_BUTTON (object);
441 if (button->priv->menu)
442 g_object_unref (button->priv->menu);
444 G_OBJECT_CLASS (parent_class)->finalize (object);
448 * gtk_menu_tool_button_new:
449 * @icon_widget: a widget that will be used as icon widget, or %NULL
450 * @label: a string that will be used as label, or %NULL
452 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
455 * Return value: the new #GtkMenuToolButton
460 gtk_menu_tool_button_new (GtkWidget *icon_widget,
463 GtkMenuToolButton *button;
465 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
468 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
471 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
473 return GTK_TOOL_ITEM (button);
477 * gtk_menu_tool_button_new_from_stock:
478 * @stock_id: the name of a stock item
480 * Creates a new #GtkMenuToolButton.
481 * The new #GtkMenuToolButton will contain an icon and label from
482 * the stock item indicated by @stock_id.
484 * Return value: the new #GtkMenuToolButton
489 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
491 GtkMenuToolButton *button;
493 g_return_val_if_fail (stock_id != NULL, NULL);
495 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
496 "stock_id", stock_id,
499 return GTK_TOOL_ITEM (button);
502 /* Callback for the "deactivate" signal on the pop-up menu.
503 * This is used so that we unset the state of the toggle button
504 * when the pop-up menu disappears.
507 menu_deactivate_cb (GtkMenuShell *menu_shell,
508 GtkMenuToolButton *button)
510 GtkMenuToolButtonPrivate *priv = button->priv;
512 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
518 * gtk_menu_tool_button_set_menu:
519 * @button: a #GtkMenuToolButton
520 * @menu: the #GtkMenu associated with #GtkMenuToolButton
522 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
523 * If @menu is NULL, the arrow button becomes insensitive.
528 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
531 GtkMenuToolButtonPrivate *priv;
533 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
534 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
538 if (priv->menu != GTK_MENU (menu))
540 if (priv->menu && GTK_WIDGET_VISIBLE (priv->menu))
541 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
544 g_object_unref (priv->menu);
546 priv->menu = GTK_MENU (menu);
550 g_object_ref (priv->menu);
551 gtk_object_sink (GTK_OBJECT (priv->menu));
553 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
555 g_signal_connect (button->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
568 * @menu: the #GtkMenu associated with #GtkMenuToolButton
570 * Gets the #GtkMenu associated with #GtkMenuToolButton.
572 * Return value: the #GtkMenu associated with #GtkMenuToolButton
577 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
579 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
581 return GTK_WIDGET (button->priv->menu);
585 * gtk_menu_tool_set_arrow_tooltip:
586 * @button: a #GtkMenuToolButton
587 * @tooltips: the #GtkTooltips object to be used
588 * @tip_text: text to be used as tooltip text for tool_item
589 * @tip_private: text to be used as private tooltip text
591 * Sets the #GtkTooltips object to be used for arrow button which
592 * pops up the menu. See gtk_tool_item_set_tooltip() for setting
593 * a tooltip on the whole #GtkMenuToolButton.
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);