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_destroy (GtkObject *object);
50 static int menu_deactivate_cb (GtkMenuShell *menu_shell,
51 GtkMenuToolButton *button);
66 static gint signals[LAST_SIGNAL];
68 static GObjectClass *parent_class = NULL;
71 gtk_menu_tool_button_get_type (void)
73 static GType type = 0;
77 static const GTypeInfo info =
79 sizeof (GtkMenuToolButtonClass),
81 (GBaseFinalizeFunc) 0,
82 (GClassInitFunc) gtk_menu_tool_button_class_init,
83 (GClassFinalizeFunc) 0,
85 sizeof (GtkMenuToolButton),
87 (GInstanceInitFunc) gtk_menu_tool_button_init
90 type = g_type_register_static (GTK_TYPE_TOOL_BUTTON,
91 I_("GtkMenuToolButton"),
99 gtk_menu_tool_button_set_tooltip (GtkToolItem *tool_item,
100 GtkTooltips *tooltips,
101 const char *tip_text,
102 const char *tip_private)
104 GtkMenuToolButton *button;
106 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (tool_item), FALSE);
108 button = GTK_MENU_TOOL_BUTTON (tool_item);
109 gtk_tooltips_set_tip (tooltips, button->priv->button, tip_text, tip_private);
115 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
117 GtkMenuToolButtonPrivate *priv = button->priv;
119 GtkOrientation orientation;
121 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
123 if (orientation == GTK_ORIENTATION_HORIZONTAL)
125 box = gtk_hbox_new (FALSE, 0);
126 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
130 box = gtk_vbox_new (FALSE, 0);
131 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
134 if (priv->button && priv->button->parent)
136 g_object_ref (priv->button);
137 gtk_container_remove (GTK_CONTAINER (priv->button->parent),
139 gtk_container_add (GTK_CONTAINER (box), priv->button);
140 g_object_unref (priv->button);
143 if (priv->arrow_button && priv->arrow_button->parent)
145 g_object_ref (priv->arrow_button);
146 gtk_container_remove (GTK_CONTAINER (priv->arrow_button->parent),
148 gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
150 g_object_unref (priv->arrow_button);
155 /* Note: we are not destroying the button and the arrow_button
156 * here because they were removed from their container above
158 gtk_widget_destroy (priv->box);
163 gtk_container_add (GTK_CONTAINER (button), priv->box);
164 gtk_widget_show_all (priv->box);
166 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
167 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
169 gtk_widget_queue_resize (GTK_WIDGET (button));
173 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
175 gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
178 GTK_TOOL_ITEM_CLASS (parent_class)->toolbar_reconfigured (toolitem);
182 gtk_menu_tool_button_state_changed (GtkWidget *widget,
183 GtkStateType previous_state)
185 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
186 GtkMenuToolButtonPrivate *priv = button->priv;
188 if (!GTK_WIDGET_IS_SENSITIVE (widget) && priv->menu)
190 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
195 gtk_menu_tool_button_set_property (GObject *object,
200 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
205 gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gtk_menu_tool_button_get_property (GObject *object,
220 GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
225 g_value_set_object (value, button->priv->menu);
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
237 GObjectClass *object_class;
238 GtkObjectClass *gtk_object_class;
239 GtkWidgetClass *widget_class;
240 GtkToolItemClass *toolitem_class;
241 GtkToolButtonClass *toolbutton_class;
243 parent_class = g_type_class_peek_parent (klass);
245 object_class = (GObjectClass *)klass;
246 gtk_object_class = (GtkObjectClass *)klass;
247 widget_class = (GtkWidgetClass *)klass;
248 toolitem_class = (GtkToolItemClass *)klass;
249 toolbutton_class = (GtkToolButtonClass *)klass;
251 object_class->set_property = gtk_menu_tool_button_set_property;
252 object_class->get_property = gtk_menu_tool_button_get_property;
253 gtk_object_class->destroy = gtk_menu_tool_button_destroy;
254 widget_class->state_changed = gtk_menu_tool_button_state_changed;
255 toolitem_class->set_tooltip = gtk_menu_tool_button_set_tooltip;
256 toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
259 g_signal_new (I_("show-menu"),
260 G_OBJECT_CLASS_TYPE (klass),
262 G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
264 g_cclosure_marshal_VOID__VOID,
267 g_object_class_install_property (object_class,
269 g_param_spec_object ("menu",
271 P_("The dropdown menu"),
273 GTK_PARAM_READWRITE));
275 g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
279 menu_position_func (GtkMenu *menu,
283 GtkMenuToolButton *button)
285 GtkMenuToolButtonPrivate *priv = button->priv;
286 GtkWidget *widget = GTK_WIDGET (button);
288 GtkRequisition menu_req;
289 GtkOrientation orientation;
290 GtkTextDirection direction;
291 GdkRectangle monitor;
295 gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_req);
297 orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
298 direction = gtk_widget_get_direction (widget);
300 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
301 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
304 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
306 if (orientation == GTK_ORIENTATION_HORIZONTAL)
308 gdk_window_get_origin (widget->window, x, y);
309 *x += widget->allocation.x;
310 *y += widget->allocation.y;
312 if (direction == GTK_TEXT_DIR_LTR)
313 *x += MAX (widget->allocation.width - menu_req.width, 0);
314 else if (menu_req.width > widget->allocation.width)
315 *x -= menu_req.width - widget->allocation.width;
317 if ((*y + priv->arrow_button->allocation.height + menu_req.height) <= monitor.y + monitor.height)
318 *y += priv->arrow_button->allocation.height;
319 else if ((*y - menu_req.height) >= monitor.y)
320 *y -= menu_req.height;
321 else if (monitor.y + monitor.height - (*y + priv->arrow_button->allocation.height) > *y)
322 *y += priv->arrow_button->allocation.height;
324 *y -= menu_req.height;
328 gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
329 gtk_widget_size_request (priv->arrow_button, &req);
331 if (direction == GTK_TEXT_DIR_LTR)
332 *x += priv->arrow_button->allocation.width;
334 *x -= menu_req.width;
336 if (*y + menu_req.height > monitor.y + monitor.height &&
337 *y + priv->arrow_button->allocation.height - monitor.y > monitor.y + monitor.height - *y)
338 *y += priv->arrow_button->allocation.height - menu_req.height;
345 popup_menu_under_arrow (GtkMenuToolButton *button,
346 GdkEventButton *event)
348 GtkMenuToolButtonPrivate *priv = button->priv;
350 g_signal_emit (button, signals[SHOW_MENU], 0);
355 gtk_menu_popup (priv->menu, NULL, NULL,
356 (GtkMenuPositionFunc) menu_position_func,
358 event ? event->button : 0,
359 event ? event->time : gtk_get_current_event_time ());
363 arrow_button_toggled_cb (GtkToggleButton *togglebutton,
364 GtkMenuToolButton *button)
366 GtkMenuToolButtonPrivate *priv = button->priv;
371 if (gtk_toggle_button_get_active (togglebutton) &&
372 !GTK_WIDGET_VISIBLE (priv->menu))
374 /* we get here only when the menu is activated by a key
375 * press, so that we can select the first menu item */
376 popup_menu_under_arrow (button, NULL);
377 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
382 arrow_button_button_press_event_cb (GtkWidget *widget,
383 GdkEventButton *event,
384 GtkMenuToolButton *button)
386 if (event->button == 1)
388 popup_menu_under_arrow (button, event);
389 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
400 gtk_menu_tool_button_init (GtkMenuToolButton *button)
404 GtkWidget *arrow_button;
405 GtkWidget *real_button;
407 button->priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
409 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
411 box = gtk_hbox_new (FALSE, 0);
413 real_button = GTK_BIN (button)->child;
414 g_object_ref (real_button);
415 gtk_container_remove (GTK_CONTAINER (button), real_button);
416 gtk_container_add (GTK_CONTAINER (box), real_button);
417 g_object_unref (real_button);
419 arrow_button = gtk_toggle_button_new ();
420 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
421 gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
422 gtk_box_pack_end (GTK_BOX (box), arrow_button,
425 /* the arrow button is insentive until we set a menu */
426 gtk_widget_set_sensitive (arrow_button, FALSE);
428 gtk_widget_show_all (box);
430 gtk_container_add (GTK_CONTAINER (button), box);
432 button->priv->button = real_button;
433 button->priv->arrow = arrow;
434 button->priv->arrow_button = arrow_button;
435 button->priv->box = box;
437 g_signal_connect (arrow_button, "toggled",
438 G_CALLBACK (arrow_button_toggled_cb), button);
439 g_signal_connect (arrow_button, "button_press_event",
440 G_CALLBACK (arrow_button_button_press_event_cb), button);
444 gtk_menu_tool_button_destroy (GtkObject *object)
446 GtkMenuToolButton *button;
448 button = GTK_MENU_TOOL_BUTTON (object);
450 if (button->priv->menu)
452 g_signal_handlers_disconnect_by_func (button->priv->menu,
455 g_object_unref (button->priv->menu);
456 button->priv->menu = NULL;
458 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
459 arrow_button_toggled_cb,
461 g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
462 arrow_button_button_press_event_cb,
466 if (GTK_OBJECT_CLASS (parent_class)->destroy)
467 (*GTK_OBJECT_CLASS (parent_class)->destroy) (object);
471 * gtk_menu_tool_button_new:
472 * @icon_widget: a widget that will be used as icon widget, or %NULL
473 * @label: a string that will be used as label, or %NULL
475 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
478 * Return value: the new #GtkMenuToolButton
483 gtk_menu_tool_button_new (GtkWidget *icon_widget,
486 GtkMenuToolButton *button;
488 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
491 gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
494 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
496 return GTK_TOOL_ITEM (button);
500 * gtk_menu_tool_button_new_from_stock:
501 * @stock_id: the name of a stock item
503 * Creates a new #GtkMenuToolButton.
504 * The new #GtkMenuToolButton will contain an icon and label from
505 * the stock item indicated by @stock_id.
507 * Return value: the new #GtkMenuToolButton
512 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
514 GtkMenuToolButton *button;
516 g_return_val_if_fail (stock_id != NULL, NULL);
518 button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
519 "stock-id", stock_id,
522 return GTK_TOOL_ITEM (button);
525 /* Callback for the "deactivate" signal on the pop-up menu.
526 * This is used so that we unset the state of the toggle button
527 * when the pop-up menu disappears.
530 menu_deactivate_cb (GtkMenuShell *menu_shell,
531 GtkMenuToolButton *button)
533 GtkMenuToolButtonPrivate *priv = button->priv;
535 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
541 * gtk_menu_tool_button_set_menu:
542 * @button: a #GtkMenuToolButton
543 * @menu: the #GtkMenu associated with #GtkMenuToolButton
545 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
546 * If @menu is NULL, the arrow button becomes insensitive.
551 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
554 GtkMenuToolButtonPrivate *priv;
556 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
557 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
561 if (priv->menu != GTK_MENU (menu))
563 if (priv->menu && GTK_WIDGET_VISIBLE (priv->menu))
564 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
568 g_signal_handlers_disconnect_by_func (priv->menu,
571 g_object_unref (priv->menu);
574 priv->menu = GTK_MENU (menu);
578 g_object_ref (priv->menu);
579 gtk_object_sink (GTK_OBJECT (priv->menu));
581 gtk_widget_set_sensitive (priv->arrow_button, TRUE);
583 g_signal_connect (button->priv->menu, "deactivate",
584 G_CALLBACK (menu_deactivate_cb), button);
587 gtk_widget_set_sensitive (priv->arrow_button, FALSE);
590 g_object_notify (G_OBJECT (button), "menu");
594 * gtk_menu_tool_button_get_menu:
595 * @button: a #GtkMenuToolButton
597 * Gets the #GtkMenu associated with #GtkMenuToolButton.
599 * Return value: the #GtkMenu associated with #GtkMenuToolButton
604 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
606 g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
608 return GTK_WIDGET (button->priv->menu);
612 * gtk_menu_tool_button_set_arrow_tooltip:
613 * @button: a #GtkMenuToolButton
614 * @tooltips: the #GtkTooltips object to be used
615 * @tip_text: text to be used as tooltip text for tool_item
616 * @tip_private: text to be used as private tooltip text
618 * Sets the #GtkTooltips object to be used for arrow button which
619 * pops up the menu. See gtk_tool_item_set_tooltip() for setting
620 * a tooltip on the whole #GtkMenuToolButton.
625 gtk_menu_tool_button_set_arrow_tooltip (GtkMenuToolButton *button,
626 GtkTooltips *tooltips,
627 const gchar *tip_text,
628 const gchar *tip_private)
630 g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
632 gtk_tooltips_set_tip (tooltips, button->priv->arrow_button, tip_text, tip_private);
635 #define __GTK_MENU_TOOL_BUTTON_C__
636 #include "gtkaliasdef.c"