]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenutoolbutton.c
Merge branch 'gtk-2-90'
[~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 #include "gtkalias.h"
34
35
36 #define GTK_MENU_TOOL_BUTTON_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GTK_TYPE_MENU_TOOL_BUTTON, GtkMenuToolButtonPrivate))
37
38 struct _GtkMenuToolButtonPrivate
39 {
40   GtkWidget *button;
41   GtkWidget *arrow;
42   GtkWidget *arrow_button;
43   GtkWidget *box;
44   GtkMenu   *menu;
45 };
46
47 static void gtk_menu_tool_button_destroy    (GtkObject              *object);
48
49 static int  menu_deactivate_cb              (GtkMenuShell           *menu_shell,
50                                              GtkMenuToolButton      *button);
51
52 enum
53 {
54   SHOW_MENU,
55   LAST_SIGNAL
56 };
57
58 enum
59 {
60   PROP_0,
61   PROP_MENU
62 };
63
64 static gint signals[LAST_SIGNAL];
65
66 G_DEFINE_TYPE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON)
67
68 static void
69 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
70 {
71   GtkMenuToolButtonPrivate *priv = button->priv;
72   GtkWidget *box;
73   GtkOrientation orientation;
74
75   orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
76
77   if (orientation == GTK_ORIENTATION_HORIZONTAL)
78     {
79       box = gtk_hbox_new (FALSE, 0);
80       gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
81     }
82   else
83     {
84       box = gtk_vbox_new (FALSE, 0);
85       gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
86     }
87
88   if (priv->button && priv->button->parent)
89     {
90       g_object_ref (priv->button);
91       gtk_container_remove (GTK_CONTAINER (priv->button->parent),
92                             priv->button);
93       gtk_container_add (GTK_CONTAINER (box), priv->button);
94       g_object_unref (priv->button);
95     }
96
97   if (priv->arrow_button && priv->arrow_button->parent)
98     {
99       g_object_ref (priv->arrow_button);
100       gtk_container_remove (GTK_CONTAINER (priv->arrow_button->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   GtkWidget *widget = GTK_WIDGET (button);
260   GtkRequisition req;
261   GtkRequisition menu_req;
262   GtkOrientation orientation;
263   GtkTextDirection direction;
264   GdkRectangle monitor;
265   gint monitor_num;
266   GdkScreen *screen;
267
268   gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_req);
269
270   orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
271   direction = gtk_widget_get_direction (widget);
272
273   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
274   monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
275   if (monitor_num < 0)
276     monitor_num = 0;
277   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
278
279   if (orientation == GTK_ORIENTATION_HORIZONTAL)
280     {
281       gdk_window_get_origin (widget->window, x, y);
282       *x += widget->allocation.x;
283       *y += widget->allocation.y;
284
285       if (direction == GTK_TEXT_DIR_LTR)
286         *x += MAX (widget->allocation.width - menu_req.width, 0);
287       else if (menu_req.width > widget->allocation.width)
288         *x -= menu_req.width - widget->allocation.width;
289
290       if ((*y + priv->arrow_button->allocation.height + menu_req.height) <= monitor.y + monitor.height)
291         *y += priv->arrow_button->allocation.height;
292       else if ((*y - menu_req.height) >= monitor.y)
293         *y -= menu_req.height;
294       else if (monitor.y + monitor.height - (*y + priv->arrow_button->allocation.height) > *y)
295         *y += priv->arrow_button->allocation.height;
296       else
297         *y -= menu_req.height;
298     }
299   else 
300     {
301       gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
302       gtk_widget_size_request (priv->arrow_button, &req);
303
304       if (direction == GTK_TEXT_DIR_LTR)
305         *x += priv->arrow_button->allocation.width;
306       else 
307         *x -= menu_req.width;
308
309       if (*y + menu_req.height > monitor.y + monitor.height &&
310           *y + priv->arrow_button->allocation.height - monitor.y > monitor.y + monitor.height - *y)
311         *y += priv->arrow_button->allocation.height - menu_req.height;
312     }
313
314   *push_in = FALSE;
315 }
316
317 static void
318 popup_menu_under_arrow (GtkMenuToolButton *button,
319                         GdkEventButton    *event)
320 {
321   GtkMenuToolButtonPrivate *priv = button->priv;
322
323   g_signal_emit (button, signals[SHOW_MENU], 0);
324
325   if (!priv->menu)
326     return;
327
328   gtk_menu_popup (priv->menu, NULL, NULL,
329                   (GtkMenuPositionFunc) menu_position_func,
330                   button,
331                   event ? event->button : 0,
332                   event ? event->time : gtk_get_current_event_time ());
333 }
334
335 static void
336 arrow_button_toggled_cb (GtkToggleButton   *togglebutton,
337                          GtkMenuToolButton *button)
338 {
339   GtkMenuToolButtonPrivate *priv = button->priv;
340
341   if (!priv->menu)
342     return;
343
344   if (gtk_toggle_button_get_active (togglebutton) &&
345       !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
346     {
347       /* we get here only when the menu is activated by a key
348        * press, so that we can select the first menu item */
349       popup_menu_under_arrow (button, NULL);
350       gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
351     }
352 }
353
354 static gboolean
355 arrow_button_button_press_event_cb (GtkWidget         *widget,
356                                     GdkEventButton    *event,
357                                     GtkMenuToolButton *button)
358 {
359   if (event->button == 1)
360     {
361       popup_menu_under_arrow (button, event);
362       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
363
364       return TRUE;
365     }
366   else
367     {
368       return FALSE;
369     }
370 }
371
372 static void
373 gtk_menu_tool_button_init (GtkMenuToolButton *button)
374 {
375   GtkWidget *box;
376   GtkWidget *arrow;
377   GtkWidget *arrow_button;
378   GtkWidget *real_button;
379
380   button->priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
381
382   gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
383
384   box = gtk_hbox_new (FALSE, 0);
385
386   real_button = GTK_BIN (button)->child;
387   g_object_ref (real_button);
388   gtk_container_remove (GTK_CONTAINER (button), real_button);
389   gtk_container_add (GTK_CONTAINER (box), real_button);
390   g_object_unref (real_button);
391
392   arrow_button = gtk_toggle_button_new ();
393   arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
394   gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
395   gtk_box_pack_end (GTK_BOX (box), arrow_button,
396                     FALSE, FALSE, 0);
397
398   /* the arrow button is insentive until we set a menu */
399   gtk_widget_set_sensitive (arrow_button, FALSE);
400
401   gtk_widget_show_all (box);
402
403   gtk_container_add (GTK_CONTAINER (button), box);
404
405   button->priv->button = real_button;
406   button->priv->arrow = arrow;
407   button->priv->arrow_button = arrow_button;
408   button->priv->box = box;
409
410   g_signal_connect (arrow_button, "toggled",
411                     G_CALLBACK (arrow_button_toggled_cb), button);
412   g_signal_connect (arrow_button, "button-press-event",
413                     G_CALLBACK (arrow_button_button_press_event_cb), button);
414 }
415
416 static void
417 gtk_menu_tool_button_destroy (GtkObject *object)
418 {
419   GtkMenuToolButton *button;
420
421   button = GTK_MENU_TOOL_BUTTON (object);
422
423   if (button->priv->menu)
424     {
425       g_signal_handlers_disconnect_by_func (button->priv->menu, 
426                                             menu_deactivate_cb, 
427                                             button);
428       gtk_menu_detach (button->priv->menu);
429
430       g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
431                                             arrow_button_toggled_cb, 
432                                             button);
433       g_signal_handlers_disconnect_by_func (button->priv->arrow_button, 
434                                             arrow_button_button_press_event_cb, 
435                                             button);
436     }
437
438   GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy (object);
439 }
440
441 /**
442  * gtk_menu_tool_button_new:
443  * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
444  * @label: (allow-none): a string that will be used as label, or %NULL
445  *
446  * Creates a new #GtkMenuToolButton using @icon_widget as icon and
447  * @label as label.
448  *
449  * Return value: the new #GtkMenuToolButton
450  *
451  * Since: 2.6
452  **/
453 GtkToolItem *
454 gtk_menu_tool_button_new (GtkWidget   *icon_widget,
455                           const gchar *label)
456 {
457   GtkMenuToolButton *button;
458
459   button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
460
461   if (label)
462     gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
463
464   if (icon_widget)
465     gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
466
467   return GTK_TOOL_ITEM (button);
468 }
469
470 /**
471  * gtk_menu_tool_button_new_from_stock:
472  * @stock_id: the name of a stock item
473  *
474  * Creates a new #GtkMenuToolButton.
475  * The new #GtkMenuToolButton will contain an icon and label from
476  * the stock item indicated by @stock_id.
477  *
478  * Return value: the new #GtkMenuToolButton
479  *
480  * Since: 2.6
481  **/
482 GtkToolItem *
483 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
484 {
485   GtkMenuToolButton *button;
486
487   g_return_val_if_fail (stock_id != NULL, NULL);
488
489   button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
490                          "stock-id", stock_id,
491                          NULL);
492
493   return GTK_TOOL_ITEM (button);
494 }
495
496 /* Callback for the "deactivate" signal on the pop-up menu.
497  * This is used so that we unset the state of the toggle button
498  * when the pop-up menu disappears. 
499  */
500 static int
501 menu_deactivate_cb (GtkMenuShell      *menu_shell,
502                     GtkMenuToolButton *button)
503 {
504   GtkMenuToolButtonPrivate *priv = button->priv;
505
506   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
507
508   return TRUE;
509 }
510
511 static void
512 menu_detacher (GtkWidget *widget,
513                GtkMenu   *menu)
514 {
515   GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
516
517   g_return_if_fail (priv->menu == menu);
518
519   priv->menu = NULL;
520 }
521
522 /**
523  * gtk_menu_tool_button_set_menu:
524  * @button: a #GtkMenuToolButton
525  * @menu: the #GtkMenu associated with #GtkMenuToolButton
526  *
527  * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
528  * If @menu is NULL, the arrow button becomes insensitive.
529  *
530  * Since: 2.6
531  **/
532 void
533 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
534                                GtkWidget         *menu)
535 {
536   GtkMenuToolButtonPrivate *priv;
537
538   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
539   g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
540
541   priv = button->priv;
542
543   if (priv->menu != GTK_MENU (menu))
544     {
545       if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
546         gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
547
548       if (priv->menu)
549         {
550           g_signal_handlers_disconnect_by_func (priv->menu, 
551                                                 menu_deactivate_cb, 
552                                                 button);
553           gtk_menu_detach (priv->menu);
554         }
555
556       priv->menu = GTK_MENU (menu);
557
558       if (priv->menu)
559         {
560           gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
561                                      menu_detacher);
562
563           gtk_widget_set_sensitive (priv->arrow_button, TRUE);
564
565           g_signal_connect (priv->menu, "deactivate",
566                             G_CALLBACK (menu_deactivate_cb), button);
567         }
568       else
569        gtk_widget_set_sensitive (priv->arrow_button, FALSE);
570     }
571
572   g_object_notify (G_OBJECT (button), "menu");
573 }
574
575 /**
576  * gtk_menu_tool_button_get_menu:
577  * @button: a #GtkMenuToolButton
578  *
579  * Gets the #GtkMenu associated with #GtkMenuToolButton.
580  *
581  * Return value: the #GtkMenu associated with #GtkMenuToolButton
582  *
583  * Since: 2.6
584  **/
585 GtkWidget *
586 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
587 {
588   g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
589
590   return GTK_WIDGET (button->priv->menu);
591 }
592
593 /**
594  * gtk_menu_tool_button_set_arrow_tooltip_text:
595  * @button: a #GtkMenuToolButton
596  * @text: text to be used as tooltip text for button's arrow button
597  *
598  * Sets the tooltip text to be used as tooltip for the arrow button which
599  * pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting a tooltip
600  * on the whole #GtkMenuToolButton.
601  *
602  * Since: 2.12
603  **/
604 void
605 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
606                                              const gchar       *text)
607 {
608   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
609
610   gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
611 }
612
613 /**
614  * gtk_menu_tool_button_set_arrow_tooltip_markup:
615  * @button: a #GtkMenuToolButton
616  * @markup: markup text to be used as tooltip text for button's arrow button
617  *
618  * Sets the tooltip markup text to be used as tooltip for the arrow button
619  * which pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting a
620  * tooltip on the whole #GtkMenuToolButton.
621  *
622  * Since: 2.12
623  **/
624 void
625 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
626                                                const gchar       *markup)
627 {
628   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
629
630   gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);
631 }
632
633 #define __GTK_MENU_TOOL_BUTTON_C__
634 #include "gtkaliasdef.c"