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