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