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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
/*
* Copyright 2013 Canonical Ltd.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gtk/gtk.h>
#include "idoactionhelper.h"
#include "idoremovablemenuitem.h"
enum
{
PROP_0,
PROP_ICON,
PROP_TEXT,
PROP_LAST
};
static GParamSpec *lProperties[PROP_LAST];
typedef struct
{
GIcon *pIcon;
char *sText;
GtkWidget *pImage;
GtkWidget *pLabel;
GtkWidget *pButton;
gboolean bClosePressed;
IdoActionHelper *pHelper;
} IdoRemovableMenuItemPrivate;
G_DEFINE_TYPE_WITH_PRIVATE(IdoRemovableMenuItem, ido_removable_menu_item, GTK_TYPE_MENU_ITEM);
static void onGetProperty(GObject *pObject, guint nProperty, GValue *pValue, GParamSpec *pParamSpec)
{
IdoRemovableMenuItem *self = IDO_REMOVABLE_MENU_ITEM(pObject);
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(self);
switch (nProperty)
{
case PROP_ICON:
{
g_value_set_object(pValue, pPrivate->pIcon);
break;
}
case PROP_TEXT:
{
g_value_set_string(pValue, pPrivate->sText);
break;
}
default:
{
G_OBJECT_WARN_INVALID_PROPERTY_ID(pObject, nProperty, pParamSpec);
break;
}
}
}
static void onSetProperty(GObject *pObject, guint nProperty, const GValue *pValue, GParamSpec *pParamSpec)
{
IdoRemovableMenuItem *self = IDO_REMOVABLE_MENU_ITEM(pObject);
switch (nProperty)
{
case PROP_ICON:
{
idoRemovableMenuItemSetIcon(self, g_value_get_object(pValue));
break;
}
case PROP_TEXT:
{
idoRemovableMenuItemSetText(self, g_value_get_string(pValue));
break;
}
default:
{
G_OBJECT_WARN_INVALID_PROPERTY_ID(pObject, nProperty, pParamSpec);
break;
}
}
}
static void onDispose(GObject *pObject)
{
IdoRemovableMenuItem *self = IDO_REMOVABLE_MENU_ITEM(pObject);
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(self);
g_clear_object (&pPrivate->pIcon);
G_OBJECT_CLASS(ido_removable_menu_item_parent_class)->dispose(pObject);
}
static void onFinalize(GObject * pObject)
{
IdoRemovableMenuItem *self = IDO_REMOVABLE_MENU_ITEM(pObject);
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(self);
g_free(pPrivate->sText);
G_OBJECT_CLASS (ido_removable_menu_item_parent_class)->finalize(pObject);
}
static void idoRemovableMenuItemStyleUpdateImage(IdoRemovableMenuItem *self)
{
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(self);
gtk_image_clear(GTK_IMAGE(pPrivate->pImage));
if (pPrivate->pIcon == NULL)
{
gtk_widget_set_visible(pPrivate->pImage, FALSE);
}
else
{
GtkIconInfo *pInfo;
const gchar *sFilename;
pInfo = gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_default(), pPrivate->pIcon, 16, 0);
sFilename = gtk_icon_info_get_filename(pInfo);
if (sFilename)
{
GdkPixbuf *pPixbuf;
pPixbuf = gdk_pixbuf_new_from_file_at_scale(sFilename, -1, 16, TRUE, NULL);
gtk_image_set_from_pixbuf(GTK_IMAGE(pPrivate->pImage), pPixbuf);
g_object_unref (pPixbuf);
}
gtk_widget_set_visible(pPrivate->pImage, sFilename != NULL);
g_object_unref(pInfo);
}
}
static void onStyleUpdated(GtkWidget *pWidget)
{
GTK_WIDGET_CLASS(ido_removable_menu_item_parent_class)->style_updated(pWidget);
idoRemovableMenuItemStyleUpdateImage(IDO_REMOVABLE_MENU_ITEM(pWidget));
gtk_widget_queue_draw(pWidget);
}
static gboolean idoRemovableMenuItemIsWidget(GtkWidget *pWidget, GdkEventButton *pEventButton)
{
if (gtk_widget_get_window(pWidget) == NULL)
{
return FALSE;
}
GtkAllocation cAllocation;
gtk_widget_get_allocation(pWidget, &cAllocation);
GdkWindow *window = gtk_widget_get_window(pWidget);
int nXWin, nYWin;
gdk_window_get_origin(window, &nXWin, &nYWin);
int nXMin = cAllocation.x;
int nXMax = cAllocation.x + cAllocation.width;
int nYMin = cAllocation.y;
int nYMax = cAllocation.y + cAllocation.height;
int nX = pEventButton->x_root - nXWin;
int nY = pEventButton->y_root - nYWin;
return nX >= nXMin && nX <= nXMax && nY >= nYMin && nY <= nYMax;
}
static gboolean onButtonPress(GtkWidget *pWidget, GdkEventButton *pEventButton)
{
g_return_val_if_fail(IDO_IS_REMOVABLE_MENU_ITEM(pWidget), FALSE);
IdoRemovableMenuItem *self = IDO_REMOVABLE_MENU_ITEM(pWidget);
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(self);
if (pEventButton->button == GDK_BUTTON_PRIMARY)
{
if (idoRemovableMenuItemIsWidget(pPrivate->pLabel, pEventButton))
{
gtk_widget_event(pPrivate->pLabel, (GdkEvent *)pEventButton);
}
else if (idoRemovableMenuItemIsWidget(pPrivate->pButton, pEventButton))
{
gtk_widget_set_state_flags(pPrivate->pButton, GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_ACTIVE, TRUE);
pPrivate->bClosePressed = TRUE;
}
}
return TRUE;
}
static gboolean onButtonRelease(GtkWidget *pWidget, GdkEventButton *pEventButton)
{
g_return_val_if_fail(IDO_IS_REMOVABLE_MENU_ITEM(pWidget), FALSE);
IdoRemovableMenuItem *self = IDO_REMOVABLE_MENU_ITEM(pWidget);
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(self);
if (pEventButton->button == GDK_BUTTON_PRIMARY)
{
if (idoRemovableMenuItemIsWidget(pPrivate->pButton, pEventButton))
{
if (pPrivate->bClosePressed)
{
GtkWidget *pParent = gtk_widget_get_parent(GTK_WIDGET(self));
GList *lMenuItems = gtk_container_get_children(GTK_CONTAINER(pParent));
guint nRemovables = 0;
for(GList *pElem = lMenuItems; pElem; pElem = pElem->next)
{
if (IDO_IS_REMOVABLE_MENU_ITEM(pElem->data))
{
nRemovables++;
}
}
g_list_free(lMenuItems);
if (pPrivate->pHelper)
{
ido_action_helper_activate(pPrivate->pHelper);
}
else
{
gtk_container_remove(GTK_CONTAINER(pParent), GTK_WIDGET(self));
}
if (GTK_IS_MENU_SHELL(pParent) && nRemovables == 1)
{
gtk_menu_shell_deactivate(GTK_MENU_SHELL(pParent));
}
}
}
else if (idoRemovableMenuItemIsWidget(pPrivate->pLabel, pEventButton))
{
gtk_widget_event(pPrivate->pLabel, (GdkEvent *)pEventButton);
}
}
gtk_widget_set_state_flags(pPrivate->pButton, GTK_STATE_FLAG_NORMAL, TRUE);
pPrivate->bClosePressed = FALSE;
return TRUE;
}
static gboolean onMotionNotify(GtkWidget *pMenuItem, GdkEventMotion *pEventMotion)
{
g_return_val_if_fail(IDO_IS_REMOVABLE_MENU_ITEM(pMenuItem), FALSE);
IdoRemovableMenuItem *self = IDO_REMOVABLE_MENU_ITEM(pMenuItem);
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(self);
GtkAllocation cAllocationSelf;
gtk_widget_get_allocation(GTK_WIDGET(self), &cAllocationSelf);
GtkWidget *pWidget;
if (idoRemovableMenuItemIsWidget(pPrivate->pButton, (GdkEventButton*)pEventMotion))
{
pWidget = pPrivate->pButton;
if (!pPrivate->bClosePressed)
{
gtk_widget_set_state_flags(pPrivate->pButton, GTK_STATE_FLAG_FOCUSED, TRUE);
}
else
{
gtk_widget_set_state_flags(pPrivate->pButton, GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_ACTIVE, TRUE);
}
}
else
{
pWidget = pPrivate->pLabel;
gtk_widget_set_state_flags(pPrivate->pButton, GTK_STATE_FLAG_NORMAL, TRUE);
}
GtkAllocation cAllocationLabel;
gtk_widget_get_allocation(pWidget, &cAllocationLabel);
GdkEventMotion *pEventMotionNew = (GdkEventMotion *)gdk_event_copy((GdkEvent *)pEventMotion);
pEventMotionNew->x = pEventMotion->x - (cAllocationLabel.x - cAllocationSelf.x);
pEventMotionNew->y = pEventMotion->y - (cAllocationLabel.y - cAllocationSelf.y);
gtk_widget_event(pWidget, (GdkEvent *)pEventMotionNew);
gdk_event_free((GdkEvent *)pEventMotionNew);
return FALSE;
}
static gboolean onLeaveNotify(GtkWidget *pWidget, GdkEventCrossing *pEventCrossing)
{
g_return_val_if_fail(IDO_IS_REMOVABLE_MENU_ITEM(pWidget), FALSE);
IdoRemovableMenuItem *self = IDO_REMOVABLE_MENU_ITEM(pWidget);
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(self);
gtk_widget_event(pPrivate->pLabel, (GdkEvent *)pEventCrossing);
return FALSE;
}
static void ido_removable_menu_item_class_init(IdoRemovableMenuItemClass *klass)
{
GObjectClass *pObject = G_OBJECT_CLASS(klass);
GtkWidgetClass *pWidget = GTK_WIDGET_CLASS(klass);
GtkMenuItemClass *pMenuItem = GTK_MENU_ITEM_CLASS(klass);
pObject->get_property = onGetProperty;
pObject->set_property = onSetProperty;
pObject->dispose = onDispose;
pObject->finalize = onFinalize;
pWidget->style_updated = onStyleUpdated;
pWidget->leave_notify_event = onLeaveNotify;
pWidget->motion_notify_event = onMotionNotify;
pWidget->button_press_event = onButtonPress;
pWidget->button_release_event = onButtonRelease;
pMenuItem->select = NULL;
GParamFlags nParamFlags = G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS;
lProperties[PROP_ICON] = g_param_spec_object("icon", "Icon", "The menuitem's GIcon", G_TYPE_OBJECT, nParamFlags);
lProperties[PROP_TEXT] = g_param_spec_string ("text", "Text", "The menuitem's text", "", nParamFlags);
g_object_class_install_properties(pObject, PROP_LAST, lProperties);
}
static gboolean onActivateLink(GtkLabel *pLabel, gchar *sUri, gpointer pUserData)
{
g_return_val_if_fail(IDO_IS_REMOVABLE_MENU_ITEM(pUserData), FALSE);
IdoRemovableMenuItem *self = IDO_REMOVABLE_MENU_ITEM(pUserData);
GError *pError = NULL;
if (!gtk_show_uri_on_window(NULL, sUri, gtk_get_current_event_time(), &pError))
{
g_warning("Unable to show '%s': %s", sUri, pError->message);
g_error_free(pError);
}
GtkWidget *pParent = gtk_widget_get_parent(GTK_WIDGET(self));
if (GTK_IS_MENU_SHELL(pParent))
{
gtk_menu_shell_deactivate(GTK_MENU_SHELL(pParent));
}
return TRUE;
}
static void ido_removable_menu_item_init(IdoRemovableMenuItem *self)
{
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(self);
pPrivate->bClosePressed = FALSE;
pPrivate->pHelper = NULL;
pPrivate->pImage = gtk_image_new();
gtk_widget_set_halign(pPrivate->pImage, GTK_ALIGN_START);
gtk_widget_set_valign(pPrivate->pImage, GTK_ALIGN_START);
pPrivate->pLabel = gtk_label_new("");
gtk_widget_set_halign(pPrivate->pLabel, GTK_ALIGN_START);
gtk_widget_set_valign(pPrivate->pLabel, GTK_ALIGN_CENTER);
pPrivate->pButton = gtk_button_new();
gtk_button_set_label(GTK_BUTTON(pPrivate->pButton), "X");
gtk_widget_set_halign(pPrivate->pButton, GTK_ALIGN_CENTER);
gtk_widget_set_valign(pPrivate->pButton, GTK_ALIGN_CENTER);
GtkWidget *pWidget = gtk_grid_new();
GtkGrid *pGrid = GTK_GRID(pWidget);
gtk_grid_attach(pGrid, pPrivate->pImage, 0, 0, 1, 1);
gtk_grid_attach(pGrid, pPrivate->pLabel, 1, 0, 1, 1);
gtk_grid_attach(pGrid, pPrivate->pButton, 2, 0, 1, 1);
g_object_set(pPrivate->pImage, "halign", GTK_ALIGN_START, "hexpand", FALSE, "valign", GTK_ALIGN_CENTER, "margin-right", 6, NULL);
g_object_set(pPrivate->pLabel, "halign", GTK_ALIGN_START, "hexpand", TRUE, "margin-right", 6, "valign", GTK_ALIGN_CENTER, NULL);
gtk_widget_show (pWidget);
gtk_container_add(GTK_CONTAINER(self), pWidget);
g_signal_connect(pPrivate->pLabel, "activate-link", G_CALLBACK(onActivateLink), self);
}
GtkWidget *ido_removable_menu_item_new (void)
{
return GTK_WIDGET(g_object_new(IDO_TYPE_REMOVABLE_MENU_ITEM, NULL));
}
void idoRemovableMenuItemSetIcon(IdoRemovableMenuItem *self, GIcon *pIcon)
{
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(self);
if (pPrivate->pIcon != pIcon)
{
if (pPrivate->pIcon)
{
g_object_unref(pPrivate->pIcon);
}
pPrivate->pIcon = pIcon ? g_object_ref(pIcon) : NULL;
idoRemovableMenuItemStyleUpdateImage(self);
}
}
void idoRemovableMenuItemSetIconFromFile(IdoRemovableMenuItem *self, const char *sFilename)
{
GFile *pFile = sFilename ? g_file_new_for_path(sFilename) : NULL;
GIcon *pIcon = pFile ? g_file_icon_new(pFile) : NULL;
idoRemovableMenuItemSetIcon(self, pIcon);
g_clear_object(&pIcon);
g_clear_object(&pFile);
}
void idoRemovableMenuItemSetText(IdoRemovableMenuItem *self, const char *sText)
{
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(self);
if (g_strcmp0(pPrivate->sText, sText))
{
g_free(pPrivate->sText);
pPrivate->sText = g_strdup(sText);
g_object_set (G_OBJECT(pPrivate->pLabel), "label", pPrivate->sText, "visible", (gboolean)(pPrivate->sText && *pPrivate->sText), NULL);
}
}
GtkMenuItem *ido_removable_menu_item_new_from_model(GMenuItem *pMenuItem, GActionGroup *pActionGroup)
{
GtkWidget *pItem = ido_removable_menu_item_new();
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(IDO_REMOVABLE_MENU_ITEM(pItem));
gboolean bUseMarkup = FALSE;
g_menu_item_get_attribute(pMenuItem, "x-ayatana-use-markup", "b", &bUseMarkup);
idoRemovableMenuItemUseMarkup(IDO_REMOVABLE_MENU_ITEM(pItem), bUseMarkup);
gchar *sLabel;
if (g_menu_item_get_attribute(pMenuItem, "label", "s", &sLabel))
{
idoRemovableMenuItemSetText(IDO_REMOVABLE_MENU_ITEM(pItem), sLabel);
g_free(sLabel);
}
GVariant *sIcon = g_menu_item_get_attribute_value(pMenuItem, "icon", NULL);
if (sIcon)
{
GIcon *pIcon = g_icon_deserialize(sIcon);
idoRemovableMenuItemSetIcon(IDO_REMOVABLE_MENU_ITEM(pItem), pIcon);
g_object_unref(pIcon);
g_variant_unref(sIcon);
}
gchar *sAction;
if (g_menu_item_get_attribute(pMenuItem, "action", "s", &sAction))
{
GVariant *sTarget = g_menu_item_get_attribute_value(pMenuItem, "target", NULL);
pPrivate->pHelper = ido_action_helper_new(pItem, pActionGroup, sAction, sTarget);
g_signal_connect_swapped(pItem, "destroy", G_CALLBACK (g_object_unref), pPrivate->pHelper);
if (sTarget)
{
g_variant_unref(sTarget);
}
g_free(sAction);
}
return GTK_MENU_ITEM(pItem);
}
void idoRemovableMenuItemUseMarkup(IdoRemovableMenuItem *self, gboolean bUse)
{
IdoRemovableMenuItemPrivate *pPrivate = ido_removable_menu_item_get_instance_private(IDO_REMOVABLE_MENU_ITEM(self));
g_object_set(pPrivate->pLabel, "use-markup", bUse, NULL);
}
|