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