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