]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenutoolbutton.c
b3d7ab25e02dc9f7221c88337e9a312ea721203e
[~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   signals[SHOW_MENU] =
221     g_signal_new (I_("show-menu"),
222                   G_OBJECT_CLASS_TYPE (klass),
223                   G_SIGNAL_RUN_FIRST,
224                   G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
225                   NULL, NULL,
226                   g_cclosure_marshal_VOID__VOID,
227                   G_TYPE_NONE, 0);
228
229   g_object_class_install_property (object_class,
230                                    PROP_MENU,
231                                    g_param_spec_object ("menu",
232                                                         P_("Menu"),
233                                                         P_("The dropdown menu"),
234                                                         GTK_TYPE_MENU,
235                                                         GTK_PARAM_READWRITE));
236
237   g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
238 }
239
240 static void
241 menu_position_func (GtkMenu           *menu,
242                     int               *x,
243                     int               *y,
244                     gboolean          *push_in,
245                     GtkMenuToolButton *button)
246 {
247   GtkMenuToolButtonPrivate *priv = button->priv;
248   GtkWidget *widget = GTK_WIDGET (button);
249   GtkRequisition req;
250   GtkRequisition menu_req;
251   GtkOrientation orientation;
252   GtkTextDirection direction;
253   GdkRectangle monitor;
254   gint monitor_num;
255   GdkScreen *screen;
256
257   gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_req);
258
259   orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
260   direction = gtk_widget_get_direction (widget);
261
262   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
263   monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
264   if (monitor_num < 0)
265     monitor_num = 0;
266   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
267
268   if (orientation == GTK_ORIENTATION_HORIZONTAL)
269     {
270       gdk_window_get_origin (widget->window, x, y);
271       *x += widget->allocation.x;
272       *y += widget->allocation.y;
273
274       if (direction == GTK_TEXT_DIR_LTR)
275         *x += MAX (widget->allocation.width - menu_req.width, 0);
276       else if (menu_req.width > widget->allocation.width)
277         *x -= menu_req.width - widget->allocation.width;
278
279       if ((*y + priv->arrow_button->allocation.height + menu_req.height) <= monitor.y + monitor.height)
280         *y += priv->arrow_button->allocation.height;
281       else if ((*y - menu_req.height) >= monitor.y)
282         *y -= menu_req.height;
283       else if (monitor.y + monitor.height - (*y + priv->arrow_button->allocation.height) > *y)
284         *y += priv->arrow_button->allocation.height;
285       else
286         *y -= menu_req.height;
287     }
288   else 
289     {
290       gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
291       gtk_widget_size_request (priv->arrow_button, &req);
292
293       if (direction == GTK_TEXT_DIR_LTR)
294         *x += priv->arrow_button->allocation.width;
295       else 
296         *x -= menu_req.width;
297
298       if (*y + menu_req.height > monitor.y + monitor.height &&
299           *y + priv->arrow_button->allocation.height - monitor.y > monitor.y + monitor.height - *y)
300         *y += priv->arrow_button->allocation.height - menu_req.height;
301     }
302
303   *push_in = FALSE;
304 }
305
306 static void
307 popup_menu_under_arrow (GtkMenuToolButton *button,
308                         GdkEventButton    *event)
309 {
310   GtkMenuToolButtonPrivate *priv = button->priv;
311
312   g_signal_emit (button, signals[SHOW_MENU], 0);
313
314   if (!priv->menu)
315     return;
316
317   gtk_menu_popup (priv->menu, NULL, NULL,
318                   (GtkMenuPositionFunc) menu_position_func,
319                   button,
320                   event ? event->button : 0,
321                   event ? event->time : gtk_get_current_event_time ());
322 }
323
324 static void
325 arrow_button_toggled_cb (GtkToggleButton   *togglebutton,
326                          GtkMenuToolButton *button)
327 {
328   GtkMenuToolButtonPrivate *priv = button->priv;
329
330   if (!priv->menu)
331     return;
332
333   if (gtk_toggle_button_get_active (togglebutton) &&
334       !GTK_WIDGET_VISIBLE (priv->menu))
335     {
336       /* we get here only when the menu is activated by a key
337        * press, so that we can select the first menu item */
338       popup_menu_under_arrow (button, NULL);
339       gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
340     }
341 }
342
343 static gboolean
344 arrow_button_button_press_event_cb (GtkWidget         *widget,
345                                     GdkEventButton    *event,
346                                     GtkMenuToolButton *button)
347 {
348   if (event->button == 1)
349     {
350       popup_menu_under_arrow (button, event);
351       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
352
353       return TRUE;
354     }
355   else
356     {
357       return FALSE;
358     }
359 }
360
361 static void
362 gtk_menu_tool_button_init (GtkMenuToolButton *button)
363 {
364   GtkWidget *box;
365   GtkWidget *arrow;
366   GtkWidget *arrow_button;
367   GtkWidget *real_button;
368
369   button->priv = GTK_MENU_TOOL_BUTTON_GET_PRIVATE (button);
370
371   gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
372
373   box = gtk_hbox_new (FALSE, 0);
374
375   real_button = GTK_BIN (button)->child;
376   g_object_ref (real_button);
377   gtk_container_remove (GTK_CONTAINER (button), real_button);
378   gtk_container_add (GTK_CONTAINER (box), real_button);
379   g_object_unref (real_button);
380
381   arrow_button = gtk_toggle_button_new ();
382   arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
383   gtk_container_add (GTK_CONTAINER (arrow_button), arrow);
384   gtk_box_pack_end (GTK_BOX (box), arrow_button,
385                     FALSE, FALSE, 0);
386
387   /* the arrow button is insentive until we set a menu */
388   gtk_widget_set_sensitive (arrow_button, FALSE);
389
390   gtk_widget_show_all (box);
391
392   gtk_container_add (GTK_CONTAINER (button), box);
393
394   button->priv->button = real_button;
395   button->priv->arrow = arrow;
396   button->priv->arrow_button = arrow_button;
397   button->priv->box = box;
398
399   g_signal_connect (arrow_button, "toggled",
400                     G_CALLBACK (arrow_button_toggled_cb), button);
401   g_signal_connect (arrow_button, "button_press_event",
402                     G_CALLBACK (arrow_button_button_press_event_cb), button);
403 }
404
405 static void
406 gtk_menu_tool_button_destroy (GtkObject *object)
407 {
408   GtkMenuToolButton *button;
409
410   button = GTK_MENU_TOOL_BUTTON (object);
411
412   if (button->priv->menu)
413     {
414       g_signal_handlers_disconnect_by_func (button->priv->menu, 
415                                             menu_deactivate_cb, 
416                                             button);
417       gtk_menu_detach (button->priv->menu);
418
419       g_signal_handlers_disconnect_by_func (button->priv->arrow_button,
420                                             arrow_button_toggled_cb, 
421                                             button);
422       g_signal_handlers_disconnect_by_func (button->priv->arrow_button, 
423                                             arrow_button_button_press_event_cb, 
424                                             button);
425     }
426   
427   if (GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy)
428     (*GTK_OBJECT_CLASS (gtk_menu_tool_button_parent_class)->destroy) (object);
429 }
430
431 /**
432  * gtk_menu_tool_button_new:
433  * @icon_widget: a widget that will be used as icon widget, or %NULL
434  * @label: a string that will be used as label, or %NULL
435  *
436  * Creates a new #GtkMenuToolButton using @icon_widget as icon and
437  * @label as label.
438  *
439  * Return value: the new #GtkMenuToolButton
440  *
441  * Since: 2.6
442  **/
443 GtkToolItem *
444 gtk_menu_tool_button_new (GtkWidget   *icon_widget,
445                           const gchar *label)
446 {
447   GtkMenuToolButton *button;
448
449   button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
450
451   if (label)
452     gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
453
454   if (icon_widget)
455     gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
456
457   return GTK_TOOL_ITEM (button);
458 }
459
460 /**
461  * gtk_menu_tool_button_new_from_stock:
462  * @stock_id: the name of a stock item
463  *
464  * Creates a new #GtkMenuToolButton.
465  * The new #GtkMenuToolButton will contain an icon and label from
466  * the stock item indicated by @stock_id.
467  *
468  * Return value: the new #GtkMenuToolButton
469  *
470  * Since: 2.6
471  **/
472 GtkToolItem *
473 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
474 {
475   GtkMenuToolButton *button;
476
477   g_return_val_if_fail (stock_id != NULL, NULL);
478
479   button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
480                          "stock-id", stock_id,
481                          NULL);
482
483   return GTK_TOOL_ITEM (button);
484 }
485
486 /* Callback for the "deactivate" signal on the pop-up menu.
487  * This is used so that we unset the state of the toggle button
488  * when the pop-up menu disappears. 
489  */
490 static int
491 menu_deactivate_cb (GtkMenuShell      *menu_shell,
492                     GtkMenuToolButton *button)
493 {
494   GtkMenuToolButtonPrivate *priv = button->priv;
495
496   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
497
498   return TRUE;
499 }
500
501 static void
502 menu_detacher (GtkWidget *widget,
503                GtkMenu   *menu)
504 {
505   GtkMenuToolButtonPrivate *priv = GTK_MENU_TOOL_BUTTON (widget)->priv;
506
507   g_return_if_fail (priv->menu == menu);
508
509   priv->menu = NULL;
510 }
511
512 /**
513  * gtk_menu_tool_button_set_menu:
514  * @button: a #GtkMenuToolButton
515  * @menu: the #GtkMenu associated with #GtkMenuToolButton
516  *
517  * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
518  * If @menu is NULL, the arrow button becomes insensitive.
519  *
520  * Since: 2.6
521  **/
522 void
523 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
524                                GtkWidget         *menu)
525 {
526   GtkMenuToolButtonPrivate *priv;
527
528   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
529   g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
530
531   priv = button->priv;
532
533   if (priv->menu != GTK_MENU (menu))
534     {
535       if (priv->menu && GTK_WIDGET_VISIBLE (priv->menu))
536         gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
537
538       if (priv->menu)
539         {
540           g_signal_handlers_disconnect_by_func (priv->menu, 
541                                                 menu_deactivate_cb, 
542                                                 button);
543           gtk_menu_detach (priv->menu);
544         }
545
546       priv->menu = GTK_MENU (menu);
547
548       if (priv->menu)
549         {
550           gtk_menu_attach_to_widget (priv->menu, GTK_WIDGET (button),
551                                      menu_detacher);
552
553           gtk_widget_set_sensitive (priv->arrow_button, TRUE);
554
555           g_signal_connect (priv->menu, "deactivate",
556                             G_CALLBACK (menu_deactivate_cb), button);
557         }
558       else
559        gtk_widget_set_sensitive (priv->arrow_button, FALSE);
560     }
561
562   g_object_notify (G_OBJECT (button), "menu");
563 }
564
565 /**
566  * gtk_menu_tool_button_get_menu:
567  * @button: a #GtkMenuToolButton
568  *
569  * Gets the #GtkMenu associated with #GtkMenuToolButton.
570  *
571  * Return value: the #GtkMenu associated with #GtkMenuToolButton
572  *
573  * Since: 2.6
574  **/
575 GtkWidget *
576 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
577 {
578   g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
579
580   return GTK_WIDGET (button->priv->menu);
581 }
582
583 /**
584  * gtk_menu_tool_button_set_arrow_tooltip:
585  * @button: a #GtkMenuToolButton
586  * @tooltips: the #GtkTooltips object to be used
587  * @tip_text: text to be used as tooltip text for tool_item
588  * @tip_private: text to be used as private tooltip text
589  *
590  * Sets the #GtkTooltips object to be used for arrow button which
591  * pops up the menu. See gtk_tool_item_set_tooltip() for setting
592  * a tooltip on the whole #GtkMenuToolButton.
593  *
594  * Since: 2.6
595  *
596  * Deprecated: 2.12: Use gtk_menu_tool_button_set_arrow_tooltip_text()
597  * instead.
598  **/
599 void
600 gtk_menu_tool_button_set_arrow_tooltip (GtkMenuToolButton *button,
601                                         GtkTooltips       *tooltips,
602                                         const gchar       *tip_text,
603                                         const gchar       *tip_private)
604 {
605   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
606
607   gtk_tooltips_set_tip (tooltips, button->priv->arrow_button, tip_text, tip_private);
608 }
609
610 /**
611  * gtk_menu_tool_button_set_arrow_tooltip_text:
612  * @button: a #GtkMenuToolButton
613  * @text: text to be used as tooltip text for button's arrow button
614  *
615  * Sets the tooltip text to be used as tooltip for the arrow button which
616  * pops up the menu.  See gtk_tool_item_set_tooltip() for setting a tooltip
617  * on the whole #GtkMenuToolButton.
618  *
619  * Since: 2.12
620  **/
621 void
622 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
623                                              const gchar       *text)
624 {
625   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
626
627   gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
628 }
629
630 /**
631  * gtk_menu_tool_button_set_arrow_tooltip_markup:
632  * @button: a #GtkMenuToolButton
633  * @markup: markup text to be used as tooltip text for button's arrow button
634  *
635  * Sets the tooltip markup text to be used as tooltip for the arrow button
636  * which pops up the menu.  See gtk_tool_item_set_tooltip() for setting a
637  * tooltip on the whole #GtkMenuToolButton.
638  *
639  * Since: 2.12
640  **/
641 void
642 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
643                                                const gchar       *markup)
644 {
645   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
646
647   gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);
648 }
649
650 #define __GTK_MENU_TOOL_BUTTON_C__
651 #include "gtkaliasdef.c"