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