]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenutoolbutton.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21
22 #include "gtkmenutoolbutton.h"
23
24 #include "gtktogglebutton.h"
25 #include "gtkarrow.h"
26 #include "gtkmenubutton.h"
27 #include "gtkmenubuttonprivate.h"
28 #include "gtkbox.h"
29 #include "gtkmenu.h"
30 #include "gtkmain.h"
31 #include "gtksizerequest.h"
32 #include "gtkbuildable.h"
33
34 #include "gtkprivate.h"
35 #include "gtkintl.h"
36
37
38 /**
39  * SECTION:gtkmenutoolbutton
40  * @Short_description: A GtkToolItem containing a button with an additional dropdown menu
41  * @Title: GtkMenuToolButton
42  * @See_also: #GtkToolbar, #GtkToolButton
43  *
44  * A #GtkMenuToolButton is a #GtkToolItem that contains a button and
45  * a small additional button with an arrow. When clicked, the arrow
46  * button pops up a dropdown menu.
47  *
48  * Use gtk_menu_tool_button_new() to create a new
49  * #GtkMenuToolButton. Use gtk_menu_tool_button_new_from_stock() to
50  * create a new #GtkMenuToolButton containing a stock item.
51  *
52  * <refsect2 id="GtkMenuToolButton-BUILDER-UI">
53  * <title>GtkMenuToolButton as GtkBuildable</title>
54  * <para>
55  * The GtkMenuToolButton implementation of the GtkBuildable interface
56  * supports adding a menu by specifying "menu" as the "type"
57  * attribute of a &lt;child&gt; element.
58  *
59  * <example>
60  * <title>A UI definition fragment with menus</title>
61  * <programlisting><![CDATA[
62  * <object class="GtkMenuToolButton">
63  *   <child type="menu">
64  *     <object class="GtkMenu"/>
65  *   </child>
66  * </object>
67  * ]]></programlisting>
68  * </example>
69  * </para>
70  * </refsect2>
71  */
72
73
74 struct _GtkMenuToolButtonPrivate
75 {
76   GtkWidget *button;
77   GtkWidget *arrow_button;
78   GtkWidget *box;
79 };
80
81 static void gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface   *iface);
82 static void gtk_menu_tool_button_buildable_add_child      (GtkBuildable        *buildable,
83                                                            GtkBuilder          *builder,
84                                                            GObject             *child,
85                                                            const gchar         *type);
86
87 enum
88 {
89   SHOW_MENU,
90   LAST_SIGNAL
91 };
92
93 enum
94 {
95   PROP_0,
96   PROP_MENU
97 };
98
99 static gint signals[LAST_SIGNAL];
100
101 static GtkBuildableIface *parent_buildable_iface;
102
103 G_DEFINE_TYPE_WITH_CODE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON,
104                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
105                                                 gtk_menu_tool_button_buildable_interface_init))
106
107 static void
108 gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
109 {
110   GtkMenuToolButtonPrivate *priv = button->priv;
111   GtkWidget *box;
112   GtkWidget *parent;
113   GtkOrientation orientation;
114
115   orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));
116
117   if (orientation == GTK_ORIENTATION_HORIZONTAL)
118     {
119       box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
120       gtk_menu_button_set_direction (GTK_MENU_BUTTON (priv->arrow_button), GTK_ARROW_DOWN);
121     }
122   else
123     {
124       GtkTextDirection direction;
125       GtkArrowType type;
126
127       box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
128       direction = gtk_widget_get_direction (GTK_WIDGET (button));
129       type = (direction == GTK_TEXT_DIR_LTR ? GTK_ARROW_RIGHT : GTK_ARROW_LEFT);
130       gtk_menu_button_set_direction (GTK_MENU_BUTTON (priv->arrow_button), type);
131     }
132
133   parent = gtk_widget_get_parent (priv->button);
134   if (priv->button && parent)
135     {
136       g_object_ref (priv->button);
137       gtk_container_remove (GTK_CONTAINER (parent),
138                             priv->button);
139       gtk_container_add (GTK_CONTAINER (box), priv->button);
140       g_object_unref (priv->button);
141     }
142
143   parent = gtk_widget_get_parent (priv->arrow_button);
144   if (priv->arrow_button && parent)
145     {
146       g_object_ref (priv->arrow_button);
147       gtk_container_remove (GTK_CONTAINER (parent),
148                             priv->arrow_button);
149       gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
150                         FALSE, FALSE, 0);
151       g_object_unref (priv->arrow_button);
152     }
153
154   if (priv->box)
155     {
156       gchar *tmp;
157
158       /* Transfer a possible tooltip to the new box */
159       g_object_get (priv->box, "tooltip-markup", &tmp, NULL);
160
161       if (tmp)
162         {
163           g_object_set (box, "tooltip-markup", tmp, NULL);
164           g_free (tmp);
165         }
166
167       /* Note: we are not destroying the button and the arrow_button
168        * here because they were removed from their container above
169        */
170       gtk_widget_destroy (priv->box);
171     }
172
173   priv->box = box;
174
175   gtk_container_add (GTK_CONTAINER (button), priv->box);
176   gtk_widget_show_all (priv->box);
177
178   gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
179                          gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
180   
181   gtk_widget_queue_resize (GTK_WIDGET (button));
182 }
183
184 static void
185 gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
186 {
187   gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));
188
189   /* chain up */
190   GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
191 }
192
193 static void
194 gtk_menu_tool_button_set_property (GObject      *object,
195                                    guint         prop_id,
196                                    const GValue *value,
197                                    GParamSpec   *pspec)
198 {
199   GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
200
201   switch (prop_id)
202     {
203     case PROP_MENU:
204       gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
205       break;
206
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210     }
211 }
212
213 static void
214 gtk_menu_tool_button_get_property (GObject    *object,
215                                    guint       prop_id,
216                                    GValue     *value,
217                                    GParamSpec *pspec)
218 {
219   GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);
220
221   switch (prop_id)
222     {
223     case PROP_MENU:
224       g_value_set_object (value, gtk_menu_button_get_popup (GTK_MENU_BUTTON (button->priv->arrow_button)));
225       break;
226
227     default:
228       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229       break;
230     }
231 }
232
233 static void
234 gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
235 {
236   GObjectClass *object_class;
237   GtkToolItemClass *toolitem_class;
238
239   object_class = (GObjectClass *)klass;
240   toolitem_class = (GtkToolItemClass *)klass;
241
242   object_class->set_property = gtk_menu_tool_button_set_property;
243   object_class->get_property = gtk_menu_tool_button_get_property;
244
245   toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;
246
247   /**
248    * GtkMenuToolButton::show-menu:
249    * @button: the object on which the signal is emitted
250    *
251    * The ::show-menu signal is emitted before the menu is shown.
252    *
253    * It can be used to populate the menu on demand, using 
254    * gtk_menu_tool_button_set_menu().
255
256    * Note that even if you populate the menu dynamically in this way, 
257    * you must set an empty menu on the #GtkMenuToolButton beforehand,
258    * since the arrow is made insensitive if the menu is not set.
259    */
260   signals[SHOW_MENU] =
261     g_signal_new (I_("show-menu"),
262                   G_OBJECT_CLASS_TYPE (klass),
263                   G_SIGNAL_RUN_FIRST,
264                   G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
265                   NULL, NULL,
266                   g_cclosure_marshal_VOID__VOID,
267                   G_TYPE_NONE, 0);
268
269   g_object_class_install_property (object_class,
270                                    PROP_MENU,
271                                    g_param_spec_object ("menu",
272                                                         P_("Menu"),
273                                                         P_("The dropdown menu"),
274                                                         GTK_TYPE_MENU,
275                                                         GTK_PARAM_READWRITE));
276
277   g_type_class_add_private (object_class, sizeof (GtkMenuToolButtonPrivate));
278 }
279
280 static void
281 gtk_menu_tool_button_init (GtkMenuToolButton *button)
282 {
283   GtkWidget *box;
284   GtkWidget *arrow_button;
285   GtkWidget *real_button;
286
287   button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
288                                               GTK_TYPE_MENU_TOOL_BUTTON,
289                                               GtkMenuToolButtonPrivate);
290
291   gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);
292
293   box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
294
295   real_button = gtk_bin_get_child (GTK_BIN (button));
296   g_object_ref (real_button);
297   gtk_container_remove (GTK_CONTAINER (button), real_button);
298   gtk_container_add (GTK_CONTAINER (box), real_button);
299   g_object_unref (real_button);
300
301   arrow_button = gtk_menu_button_new ();
302   gtk_box_pack_end (GTK_BOX (box), arrow_button,
303                     FALSE, FALSE, 0);
304
305   /* the arrow button is insentive until we set a menu */
306   gtk_widget_set_sensitive (arrow_button, FALSE);
307
308   gtk_widget_show_all (box);
309
310   gtk_container_add (GTK_CONTAINER (button), box);
311   gtk_menu_button_set_align_widget (GTK_MENU_BUTTON (arrow_button),
312                                     GTK_WIDGET (button));
313
314   button->priv->button = real_button;
315   button->priv->arrow_button = arrow_button;
316   button->priv->box = box;
317 }
318
319 static void
320 gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
321                                           GtkBuilder   *builder,
322                                           GObject      *child,
323                                           const gchar  *type)
324 {
325   if (type && strcmp (type, "menu") == 0)
326     gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (buildable),
327                                    GTK_WIDGET (child));
328   else
329     parent_buildable_iface->add_child (buildable, builder, child, type);
330 }
331
332 static void
333 gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface)
334 {
335   parent_buildable_iface = g_type_interface_peek_parent (iface);
336   iface->add_child = gtk_menu_tool_button_buildable_add_child;
337 }
338
339 /**
340  * gtk_menu_tool_button_new:
341  * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
342  * @label: (allow-none): a string that will be used as label, or %NULL
343  *
344  * Creates a new #GtkMenuToolButton using @icon_widget as icon and
345  * @label as label.
346  *
347  * Return value: the new #GtkMenuToolButton
348  *
349  * Since: 2.6
350  **/
351 GtkToolItem *
352 gtk_menu_tool_button_new (GtkWidget   *icon_widget,
353                           const gchar *label)
354 {
355   GtkMenuToolButton *button;
356
357   button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);
358
359   if (label)
360     gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);
361
362   if (icon_widget)
363     gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);
364
365   return GTK_TOOL_ITEM (button);
366 }
367
368 /**
369  * gtk_menu_tool_button_new_from_stock:
370  * @stock_id: the name of a stock item
371  *
372  * Creates a new #GtkMenuToolButton.
373  * The new #GtkMenuToolButton will contain an icon and label from
374  * the stock item indicated by @stock_id.
375  *
376  * Return value: the new #GtkMenuToolButton
377  *
378  * Since: 2.6
379  **/
380 GtkToolItem *
381 gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
382 {
383   GtkMenuToolButton *button;
384
385   g_return_val_if_fail (stock_id != NULL, NULL);
386
387   button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
388                          "stock-id", stock_id,
389                          NULL);
390
391   return GTK_TOOL_ITEM (button);
392 }
393
394 static void
395 _show_menu_emit (gpointer user_data)
396 {
397   GtkMenuToolButton *button = (GtkMenuToolButton *) user_data;
398   g_signal_emit (button, signals[SHOW_MENU], 0);
399 }
400
401 /**
402  * gtk_menu_tool_button_set_menu:
403  * @button: a #GtkMenuToolButton
404  * @menu: the #GtkMenu associated with #GtkMenuToolButton
405  *
406  * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
407  * If @menu is NULL, the arrow button becomes insensitive.
408  *
409  * Since: 2.6
410  **/
411 void
412 gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
413                                GtkWidget         *menu)
414 {
415   GtkMenuToolButtonPrivate *priv;
416
417   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
418   g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
419
420   priv = button->priv;
421
422   _gtk_menu_button_set_popup_with_func (GTK_MENU_BUTTON (priv->arrow_button),
423                                         menu,
424                                         _show_menu_emit,
425                                         button);
426
427   g_object_notify (G_OBJECT (button), "menu");
428 }
429
430 /**
431  * gtk_menu_tool_button_get_menu:
432  * @button: a #GtkMenuToolButton
433  *
434  * Gets the #GtkMenu associated with #GtkMenuToolButton.
435  *
436  * Return value: (transfer none): the #GtkMenu associated
437  *     with #GtkMenuToolButton
438  *
439  * Since: 2.6
440  **/
441 GtkWidget *
442 gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
443 {
444   GtkMenu *ret;
445
446   g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);
447
448   ret = gtk_menu_button_get_popup (GTK_MENU_BUTTON (button->priv->arrow_button));
449   if (!ret)
450     return NULL;
451
452   return GTK_WIDGET (ret);
453 }
454
455 /**
456  * gtk_menu_tool_button_set_arrow_tooltip_text:
457  * @button: a #GtkMenuToolButton
458  * @text: text to be used as tooltip text for button's arrow button
459  *
460  * Sets the tooltip text to be used as tooltip for the arrow button which
461  * pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting a tooltip
462  * on the whole #GtkMenuToolButton.
463  *
464  * Since: 2.12
465  **/
466 void
467 gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
468                                              const gchar       *text)
469 {
470   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
471
472   gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
473 }
474
475 /**
476  * gtk_menu_tool_button_set_arrow_tooltip_markup:
477  * @button: a #GtkMenuToolButton
478  * @markup: markup text to be used as tooltip text for button's arrow button
479  *
480  * Sets the tooltip markup text to be used as tooltip for the arrow button
481  * which pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting a
482  * tooltip on the whole #GtkMenuToolButton.
483  *
484  * Since: 2.12
485  **/
486 void
487 gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
488                                                const gchar       *markup)
489 {
490   g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
491
492   gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);
493 }