]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenutoolbutton.c
Merge branch 'master' into broadway2
[~andy/gtk] / gtk / gtkmenutoolbutton.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2003 Ricardo Fernandez Pascual
4  * Copyright (C) 2004 Paolo Borelli
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23
24 #include "gtkmenutoolbutton.h"
25
26 #include "gtktogglebutton.h"
27 #include "gtkarrow.h"
28 #include "gtkhbox.h"
29 #include "gtkvbox.h"
30 #include "gtkmenu.h"
31 #include "gtkmain.h"
32 #include "gtksizerequest.h"
33 #include "gtkbuildable.h"
34
35 #include "gtkprivate.h"
36 #include "gtkintl.h"
37
38
39 struct _GtkMenuToolButtonPrivate
40 {
41   GtkWidget *button;
42   GtkWidget *arrow;
43   GtkWidget *arrow_button;
44   GtkWidget *box;
45   GtkMenu   *menu;
46 };
47
48 static void gtk_menu_tool_button_destroy    (GtkWidget              *widget);
49
50 static int  menu_deactivate_cb              (GtkMenuShell           *menu_shell,
51                                              GtkMenuToolButton      *button);
52
53 static void gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface   *iface);
54 static void gtk_menu_tool_button_buildable_add_child      (GtkBuildable        *buildable,
55                                                            GtkBuilder          *builder,
56                                                            GObject             *child,
57                                                            const gchar         *type);
58
59 enum
60 {
61   SHOW_MENU,
62   LAST_SIGNAL
63 };
64
65 enum
66 {
67   PROP_0,
68   PROP_MENU
69 };
70
71 static gint signals[LAST_SIGNAL];
72
73 static GtkBuildableIface *parent_buildable_iface;
74
75 G_DEFINE_TYPE_WITH_CODE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON,
76                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
77                                                 gtk_menu_tool_button_buildable_interface_init))
78
79 static void
80 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
81 {
82   GtkMenuToolButtonPrivate *priv = button->priv;
83   GtkWidget *box;
84   GtkWidget *parent;
85   GtkOrientation orientation;
86
87   orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
88
89   if (orientation == GTK_ORIENTATION_HORIZONTAL)
90     {
91       box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
92       gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
93     }
94   else
95     {
96       box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
97       gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
98     }
99
100   parent = gtk_widget_get_parent (priv->button);
101   if (priv->button && parent)
102     {
103       g_object_ref (priv->button);
104       gtk_container_remove (GTK_CONTAINER (parent),
105                             priv->button);
106       gtk_container_add (GTK_CONTAINER (box), priv->button);
107       g_object_unref (priv->button);
108     }
109
110   parent = gtk_widget_get_parent (priv->arrow_button);
111   if (priv->arrow_button && parent)
112     {
113       g_object_ref (priv->arrow_button);
114       gtk_container_remove (GTK_CONTAINER (parent),
115                             priv->arrow_button);
116       gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
117                         FALSE, FALSE, 0);
118       g_object_unref (priv->arrow_button);
119     }
120
121   if (priv->box)
122     {
123       gchar *tmp;
124
125       /* Transfer a possible tooltip to the new box */
126       g_object_get (priv->box, "tooltip-markup", &tmp, NULL);
127
128       if (tmp)
129         {
130           g_object_set (box, "tooltip-markup", tmp, NULL);
131           g_free (tmp);
132         }
133
134       /* Note: we are not destroying the button and the arrow_button
135        * here because they were removed from their container above
136        */
137       gtk_widget_destroy (priv->box);
138     }
139
140   priv->box = box;
141
142   gtk_container_add (GTK_CONTAINER (button), priv->box);
143   gtk_widget_show_all (priv->box);
144
145   gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
146                          gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
147   
148   gtk_widget_queue_resize (GTK_WIDGET (button));
149 }
150
151 static void
152 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
153 {
154   gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
155
156   /* chain up */
157   GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
158 }
159
160 static void
161 gtk_menu_tool_button_state_changed (GtkWidget    *widget,
162                                     GtkStateType  previous_state)
163 {
164   GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
165   GtkMenuToolButtonPrivate *priv = button->priv;
166
167   if (!gtk_widget_is_sensitive (widget) && priv->menu)
168     {
169       gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
170     }
171 }
172
173 static void
174 gtk_menu_tool_button_set_property (GObject      *object,
175                                    guint         prop_id,
176                                    const GValue *value,
177                                    GParamSpec   *pspec)
178 {
179   GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
180
181   switch (prop_id)
182     {
183     case PROP_MENU:
184       gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
185       break;
186
187     default:
188       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189       break;
190     }
191 }
192
193 static void
194 gtk_menu_tool_button_get_property (GObject    *object,
195                                    guint       prop_id,
196                                    GValue     *value,
197                                    GParamSpec *pspec)
198 {
199   GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
200
201   switch (prop_id)
202     {
203     case PROP_MENU:
204       g_value_set_object (value, button->priv->menu);
205       break;
206
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210     }
211 }
212
213 static void
214 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
215 {
216   GObjectClass *object_class;
217   GtkWidgetClass *widget_class;
218   GtkToolItemClass *toolitem_class;
219
220   object_class = (GObjectClass *)klass;
221   widget_class = (GtkWidgetClass *)klass;
222   toolitem_class = (GtkToolItemClass *)klass;
223
224   object_class->set_property = gtk_menu_tool_button_set_property;
225   object_class->get_property = gtk_menu_tool_button_get_property;
226
227   widget_class->destroy = gtk_menu_tool_button_destroy;
228   widget_class->state_changed = gtk_menu_tool_button_state_changed;
229
230   toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
231
232   /**
233    * GtkMenuToolButton::show-menu:
234    * @button: the object on which the signal is emitted
235    *
236    * The ::show-menu signal is emitted before the menu is shown.
237    *
238    * It can be used to populate the menu on demand, using 
239    * gtk_menu_tool_button_get_menu(). 
240
241    * Note that even if you populate the menu dynamically in this way, 
242    * you must set an empty menu on the #GtkMenuToolButton beforehand,
243    * since the arrow is made insensitive if the menu is not set.
244    */
245   signals[SHOW_MENU] =
246     g_signal_new (I_("show-menu"),
247                   G_OBJECT_CLASS_TYPE (klass),
248                   G_SIGNAL_RUN_FIRST,
249                   G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
250                   NULL, NULL,
251                   g_cclosure_marshal_VOID__VOID,
252                   G_TYPE_NONE, 0);
253
254   g_object_class_install_property (object_class,
255                                    PROP_MENU,
256                                    g_param_spec_object ("menu",
257                                                         P_("Menu"),
258                                                         P_("The dropdown menu"),
259                                                         GTK_TYPE_MENU,
260                                                         GTK_PARAM_READWRITE));
261
262   g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
263 }
264
265 static void
266 menu_position_func (GtkMenu           *menu,
267                     int               *x,
268                     int               *y,
269                     gboolean          *push_in,
270                     GtkMenuToolButton *button)
271 {
272   GtkMenuToolButtonPrivate *priv = button->priv;
273   GtkAllocation arrow_allocation;
274   GtkWidget *widget = GTK_WIDGET (button);
275   GtkRequisition req;
276   GtkRequisition menu_req;
277   GtkOrientation orientation;
278   GtkTextDirection direction;
279   GdkRectangle monitor;
280   gint monitor_num;
281   GdkScreen *screen;
282   GdkWindow *window;
283
284   gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
285                                  &menu_req, NULL);
286
287   orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
288   direction = gtk_widget_get_direction (widget);
289   window = gtk_widget_get_window (widget);
290
291   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
292   monitor_num = gdk_screen_get_monitor_at_window (screen, window);
293   if (monitor_num < 0)
294     monitor_num = 0;
295   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
296
297   if (orientation == GTK_ORIENTATION_HORIZONTAL)
298     {
299       GtkAllocation allocation;
300
301       gtk_widget_get_allocation (widget, &allocation);
302       gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
303
304       gdk_window_get_origin (window, x, y);
305       *x += allocation.x;
306       *y += allocation.y;
307
308       if (direction == GTK_TEXT_DIR_LTR)
309         *x += MAX (allocation.width - menu_req.width, 0);
310       else if (menu_req.width > allocation.width)
311         *x -= menu_req.width - allocation.width;
312
313       if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
314         *y += arrow_allocation.height;
315       else if ((*y - menu_req.height) >= monitor.y)
316         *y -= menu_req.height;
317       else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
318         *y += arrow_allocation.height;
319       else
320         *y -= menu_req.height;
321     }
322   else 
323     {
324       gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
325       gtk_widget_get_preferred_size (priv->arrow_button,
326                                      &req, NULL);
327
328       gtk_widget_get_allocation (priv->arrow_button, &arrow_allocation);
329
330       if (direction == GTK_TEXT_DIR_LTR)
331         *x += arrow_allocation.width;
332       else 
333         *x -= menu_req.width;
334
335       if (*y + menu_req.height > monitor.y + monitor.height &&
336           *y + arrow_allocation.height - monitor.y > monitor.y + monitor.height - *y)
337         *y += arrow_allocation.height - menu_req.height;
338     }
339
340   *push_in = FALSE;
341 }
342
343 static void
344 popup_menu_under_arrow (GtkMenuToolButton *button,
345                         GdkEventButton    *event)
346 {
347   GtkMenuToolButtonPrivate *priv = button->priv;
348
349   g_signal_emit (button, signals[SHOW_MENU], 0);
350
351   if (!priv->menu)
352     return;
353
354   gtk_menu_popup (priv->menu, NULL, NULL,
355                   (GtkMenuPositionFunc) menu_position_func,
356                   button,
357                   event ? event->button : 0,
358                   event ? event->time : gtk_get_current_event_time ());
359 }
360
361 static void
362 arrow_button_toggled_cb (GtkToggleButton   *togglebutton,
363                          GtkMenuToolButton *button)
364 {
365   GtkMenuToolButtonPrivate *priv = button->priv;
366
367   if (!priv->menu)
368     return;
369
370   if (gtk_toggle_button_get_active (togglebutton) &&
371       !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
372     {
373       /* we get here only when the menu is activated by a key
374        * press, so that we can select the first menu item */
375       popup_menu_under_arrow (button, NULL);
376       gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
377     }
378 }
379
380 static gboolean
381 arrow_button_button_press_event_cb (GtkWidget         *widget,
382                                     GdkEventButton    *event,
383                                     GtkMenuToolButton *button)
384 {
385   if (event->button == 1)
386     {
387       popup_menu_under_arrow (button, event);
388       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
389
390       return TRUE;
391     }
392   else
393     {
394       return FALSE;
395     }
396 }
397
398 static void
399 gtk_menu_tool_button_init (GtkMenuToolButton *button)
400 {
401   GtkWidget *box;
402   GtkWidget *arrow;
403   GtkWidget *arrow_button;
404   GtkWidget *real_button;
405
406   button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
407                                               GTK_TYPE_MENU_TOOL_BUTTON,
408                                               GtkMenuToolButtonPrivate);
409
410   gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
411
412   box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
413
414   real_button = gtk_bin_get_child (GTK_BIN (button));
415   g_object_ref (real_button);
416   gtk_container_remove (GTK_CONTAINER (button), real_button);
417   gtk_container_add (GTK_CONTAINER (box), real_button);
418   g_object_unref (real_button);
419
420   arrow_button = gtk_toggle_button_new ();
421   arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
422   gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
423   gtk_box_pack_end (GTK_BOX (box), arrow_button,
424                     FALSE, FALSE, 0);
425
426   /* the arrow button is insentive until we set a menu */
427   gtk_widget_set_sensitive (arrow_button, FALSE);
428
429   gtk_widget_show_all (box);
430
431   gtk_container_add (GTK_CONTAINER (button), box);
432
433   button->priv->button = real_button;
434   button->priv->arrow = arrow;
435   button->priv->arrow_button = arrow_button;
436   button->priv->box = box;
437
438   g_signal_connect (arrow_button, "toggled",
439                     G_CALLBACK (arrow_button_toggled_cb), button);
440   g_signal_connect (arrow_button, "button-press-event",
441                     G_CALLBACK (arrow_button_button_press_event_cb), button);
442 }
443
444 static void
445 gtk_menu_tool_button_destroy (GtkWidget *widget)
446 {
447   GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (widget);
448
449   if (button->priv->menu)
450     {
451       g_signal_handlers_disconnect_by_func (button->priv->menu, 
452                                             menu_deactivate_cb, 
453                                             button);
454       gtk_menu_detach (button->priv->menu);
455
456       g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
457                                             arrow_button_toggled_cb, 
458                                             button);
459       g_signal_handlers_disconnect_by_func (button->priv->arrow_button, 
460                                             arrow_button_button_press_event_cb, 
461                                             button);
462     }
463
464   GTK_WIDGET_CLASS (gtk_menu_tool_button_parent_class)->destroy (widget);
465 }
466
467 static void
468 gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
469                                           GtkBuilder   *builder,
470                                           GObject      *child,
471                                           const gchar  *type)
472 {
473   if (type && strcmp (type, "menu") == 0)
474     gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (buildable),
475                                    GTK_WIDGET (child));
476   else
477     parent_buildable_iface->add_child (buildable, builder, child, type);
478 }
479
480 static void
481 gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface)
482 {
483   parent_buildable_iface = g_type_interface_peek_parent (iface);
484   iface->add_child = gtk_menu_tool_button_buildable_add_child;
485 }
486
487 /**
488  * gtk_menu_tool_button_new:
489  * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
490  * @label: (allow-none): a string that will be used as label, or %NULL
491  *
492  * Creates a new #GtkMenuToolButton using @icon_widget as icon and
493  * @label as label.
494  *
495  * Return value: the new #GtkMenuToolButton
496  *
497  * Since: 2.6
498  **/
499 GtkToolItem *
500 gtk_menu_tool_button_new (GtkWidget   *icon_widget,
501                           const gchar *label)
502 {
503   GtkMenuToolButton *button;
504
505   button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
506
507   if (label)
508     gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
509
510   if (icon_widget)
511     gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
512
513   return GTK_TOOL_ITEM (button);
514 }
515
516 /**
517  * gtk_menu_tool_button_new_from_stock:
518  * @stock_id: the name of a stock item
519  *
520  * Creates a new #GtkMenuToolButton.
521  * The new #GtkMenuToolButton will contain an icon and label from
522  * the stock item indicated by @stock_id.
523  *
524  * Return value: the new #GtkMenuToolButton
525  *
526  * Since: 2.6
527  **/
528 GtkToolItem *
529 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
530 {
531   GtkMenuToolButton *button;
532
533   g_return_val_if_fail (stock_id != NULL, NULL);
534
535   button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
536                          "stock-id", stock_id,
537                          NULL);
538
539   return GTK_TOOL_ITEM (button);
540 }
541
542 /* Callback for the "deactivate" signal on the pop-up menu.
543  * This is used so that we unset the state of the toggle button
544  * when the pop-up menu disappears. 
545  */
546 static int
547 menu_deactivate_cb (GtkMenuShell      *menu_shell,
548                     GtkMenuToolButton *button)
549 {
550   GtkMenuToolButtonPrivate *priv = button->priv;
551
552   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
553
554   return TRUE;
555 }
556
557 static void
558 menu_detacher (GtkWidget *widget,
559                GtkMenu   *menu)
560 {
561   GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
562
563   g_return_if_fail (priv->menu == menu);
564
565   priv->menu = NULL;
566 }
567
568 /**
569  * gtk_menu_tool_button_set_menu:
570  * @button: a #GtkMenuToolButton
571  * @menu: the #GtkMenu associated with #GtkMenuToolButton
572  *
573  * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
574  * If @menu is NULL, the arrow button becomes insensitive.
575  *
576  * Since: 2.6
577  **/
578 void
579 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
580                                GtkWidget         *menu)
581 {
582   GtkMenuToolButtonPrivate *priv;
583
584   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
585   g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
586
587   priv = button->priv;
588
589   if (priv->menu != GTK_MENU (menu))
590     {
591       if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
592         gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
593
594       if (priv->menu)
595         {
596           g_signal_handlers_disconnect_by_func (priv->menu, 
597                                                 menu_deactivate_cb, 
598                                                 button);
599           gtk_menu_detach (priv->menu);
600         }
601
602       priv->menu = GTK_MENU (menu);
603
604       if (priv->menu)
605         {
606           gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
607                                      menu_detacher);
608
609           gtk_widget_set_sensitive (priv->arrow_button, TRUE);
610
611           g_signal_connect (priv->menu, "deactivate",
612                             G_CALLBACK (menu_deactivate_cb), button);
613         }
614       else
615        gtk_widget_set_sensitive (priv->arrow_button, FALSE);
616     }
617
618   g_object_notify (G_OBJECT (button), "menu");
619 }
620
621 /**
622  * gtk_menu_tool_button_get_menu:
623  * @button: a #GtkMenuToolButton
624  *
625  * Gets the #GtkMenu associated with #GtkMenuToolButton.
626  *
627  * Return value: (transfer none): the #GtkMenu associated
628  *     with #GtkMenuToolButton
629  *
630  * Since: 2.6
631  **/
632 GtkWidget *
633 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
634 {
635   g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
636
637   return GTK_WIDGET (button->priv->menu);
638 }
639
640 /**
641  * gtk_menu_tool_button_set_arrow_tooltip_text:
642  * @button: a #GtkMenuToolButton
643  * @text: text to be used as tooltip text for button's arrow button
644  *
645  * Sets the tooltip text to be used as tooltip for the arrow button which
646  * pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting a tooltip
647  * on the whole #GtkMenuToolButton.
648  *
649  * Since: 2.12
650  **/
651 void
652 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
653                                              const gchar       *text)
654 {
655   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
656
657   gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
658 }
659
660 /**
661  * gtk_menu_tool_button_set_arrow_tooltip_markup:
662  * @button: a #GtkMenuToolButton
663  * @markup: markup text to be used as tooltip text for button's arrow button
664  *
665  * Sets the tooltip markup text to be used as tooltip for the arrow button
666  * which pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting a
667  * tooltip on the whole #GtkMenuToolButton.
668  *
669  * Since: 2.12
670  **/
671 void
672 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
673                                                const gchar       *markup)
674 {
675   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
676
677   gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);
678 }