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