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