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