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