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