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