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