/
mate-panel-applet.c
2430 lines (2039 loc) · 68.6 KB
/
mate-panel-applet.c
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* mate-panel-applet.c: panel applet writing library.
*
* Copyright (c) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
* Copyright (C) 2001 Sun Microsystems, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Authors:
* Mark McLoughlin <mark@skynet.ie>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.h>
#include <cairo.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#ifdef HAVE_X11
#include <cairo-xlib.h>
#include <gdk/gdkx.h>
#include <gtk/gtkx.h>
#include <X11/Xatom.h>
#include "panel-plug-private.h"
#endif
#include "mate-panel-applet.h"
#include "panel-applet-private.h"
#include "mate-panel-applet-factory.h"
#include "mate-panel-applet-marshal.h"
#include "mate-panel-applet-enums.h"
#define MATE_PANEL_APPLET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PANEL_TYPE_APPLET, MatePanelAppletPrivate))
struct _MatePanelAppletPrivate {
GtkWidget *plug;
GtkWidget *applet;
GDBusConnection *connection;
gboolean out_of_process;
char *id;
GClosure *closure;
char *object_path;
guint object_id;
char *prefs_path;
GtkUIManager *ui_manager;
GtkActionGroup *applet_action_group;
GtkActionGroup *panel_action_group;
MatePanelAppletFlags flags;
MatePanelAppletOrient orient;
guint size;
char *background;
int previous_width;
int previous_height;
int *size_hints;
int size_hints_len;
gboolean moving_focus_out;
gboolean locked;
gboolean locked_down;
};
enum {
CHANGE_ORIENT,
CHANGE_SIZE,
CHANGE_BACKGROUND,
MOVE_FOCUS_OUT_OF_APPLET,
LAST_SIGNAL
};
static guint mate_panel_applet_signals[LAST_SIGNAL] = { 0 };
enum {
PROP_0,
PROP_OUT_OF_PROCESS,
PROP_ID,
PROP_CLOSURE,
PROP_CONNECTION,
PROP_PREFS_PATH,
PROP_ORIENT,
PROP_SIZE,
PROP_BACKGROUND,
PROP_FLAGS,
PROP_SIZE_HINTS,
PROP_LOCKED,
PROP_LOCKED_DOWN
};
static void mate_panel_applet_handle_background (MatePanelApplet *applet);
static GtkAction *mate_panel_applet_menu_get_action (MatePanelApplet *applet,
const gchar *action);
static void mate_panel_applet_menu_update_actions (MatePanelApplet *applet);
static void mate_panel_applet_menu_cmd_remove (GtkAction *action,
MatePanelApplet *applet);
static void mate_panel_applet_menu_cmd_move (GtkAction *action,
MatePanelApplet *applet);
static void mate_panel_applet_menu_cmd_lock (GtkAction *action,
MatePanelApplet *applet);
static void mate_panel_applet_register_object (MatePanelApplet *applet);
void _mate_panel_applet_apply_css (GtkWidget* widget, MatePanelAppletBackgroundType type);
static const gchar panel_menu_ui[] =
"<ui>\n"
" <popup name=\"MatePanelAppletPopup\" action=\"PopupAction\">\n"
" <placeholder name=\"AppletItems\"/>\n"
" <separator/>\n"
" <menuitem name=\"RemoveItem\" action=\"Remove\"/>\n"
" <menuitem name=\"MoveItem\" action=\"Move\"/>\n"
" <separator/>\n"
" <menuitem name=\"LockItem\" action=\"Lock\"/>\n"
" </popup>\n"
"</ui>\n";
static const GtkActionEntry menu_entries[] = {
{ "Remove", "list-remove", N_("_Remove From Panel"),
NULL, NULL,
G_CALLBACK (mate_panel_applet_menu_cmd_remove) },
{ "Move", NULL, N_("_Move"),
NULL, NULL,
G_CALLBACK (mate_panel_applet_menu_cmd_move) }
};
static const GtkToggleActionEntry menu_toggle_entries[] = {
{ "Lock", NULL, N_("Loc_k To Panel"),
NULL, NULL,
G_CALLBACK (mate_panel_applet_menu_cmd_lock) }
};
G_DEFINE_TYPE (MatePanelApplet, mate_panel_applet, GTK_TYPE_EVENT_BOX)
#define MATE_PANEL_APPLET_INTERFACE "org.mate.panel.applet.Applet"
#define MATE_PANEL_APPLET_OBJECT_PATH "/org/mate/panel/applet/%s/%d"
char *
mate_panel_applet_get_preferences_path (MatePanelApplet *applet)
{
g_return_val_if_fail (PANEL_IS_APPLET (applet), NULL);
if (!applet->priv->prefs_path)
return NULL;
return g_strdup (applet->priv->prefs_path);
}
static void
mate_panel_applet_set_preferences_path (MatePanelApplet *applet,
const char *prefs_path)
{
if (applet->priv->prefs_path == prefs_path)
return;
if (g_strcmp0 (applet->priv->prefs_path, prefs_path) == 0)
return;
if (prefs_path) {
applet->priv->prefs_path = g_strdup (prefs_path);
}
g_object_notify (G_OBJECT (applet), "prefs-path");
}
MatePanelAppletFlags
mate_panel_applet_get_flags (MatePanelApplet *applet)
{
g_return_val_if_fail (PANEL_IS_APPLET (applet), MATE_PANEL_APPLET_FLAGS_NONE);
return applet->priv->flags;
}
void
mate_panel_applet_set_flags (MatePanelApplet *applet,
MatePanelAppletFlags flags)
{
g_return_if_fail (PANEL_IS_APPLET (applet));
if (applet->priv->flags == flags)
return;
applet->priv->flags = flags;
g_object_notify (G_OBJECT (applet), "flags");
if (applet->priv->connection) {
GVariantBuilder *builder;
GVariantBuilder *invalidated_builder;
GError *error = NULL;
builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
invalidated_builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
g_variant_builder_add (builder, "{sv}", "Flags",
g_variant_new_uint32 (applet->priv->flags));
g_dbus_connection_emit_signal (applet->priv->connection,
NULL,
applet->priv->object_path,
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
g_variant_new ("(sa{sv}as)",
MATE_PANEL_APPLET_INTERFACE,
builder,
invalidated_builder),
&error);
if (error) {
g_printerr ("Failed to send signal PropertiesChanged::Flags: %s\n",
error->message);
g_error_free (error);
}
}
}
static void
mate_panel_applet_size_hints_ensure (MatePanelApplet *applet,
int new_size)
{
if (applet->priv->size_hints && applet->priv->size_hints_len < new_size) {
g_free (applet->priv->size_hints);
applet->priv->size_hints = g_new (gint, new_size);
} else if (!applet->priv->size_hints) {
applet->priv->size_hints = g_new (gint, new_size);
}
applet->priv->size_hints_len = new_size;
}
static gboolean
mate_panel_applet_size_hints_changed (MatePanelApplet *applet,
const int *size_hints,
int n_elements,
int base_size)
{
gint i;
if (!applet->priv->size_hints)
return TRUE;
if (applet->priv->size_hints_len != n_elements)
return TRUE;
for (i = 0; i < n_elements; i++) {
if (size_hints[i] + base_size != applet->priv->size_hints[i])
return TRUE;
}
return FALSE;
}
/**
* mate_panel_applet_set_size_hints:
* @applet: applet
* @size_hints: (array length=n_elements): List of integers
* @n_elements: Length of @size_hints
* @base_size: base_size
*/
void
mate_panel_applet_set_size_hints (MatePanelApplet *applet,
const int *size_hints,
int n_elements,
int base_size)
{
gint i;
/* Make sure property has really changed to avoid bus traffic */
if (!mate_panel_applet_size_hints_changed (applet, size_hints, n_elements, base_size))
return;
mate_panel_applet_size_hints_ensure (applet, n_elements);
for (i = 0; i < n_elements; i++)
applet->priv->size_hints[i] = size_hints[i] + base_size;
g_object_notify (G_OBJECT (applet), "size-hints");
if (applet->priv->connection) {
GVariantBuilder *builder;
GVariantBuilder *invalidated_builder;
GVariant **children;
GError *error = NULL;
builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
invalidated_builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
children = g_new (GVariant *, applet->priv->size_hints_len);
for (i = 0; i < n_elements; i++)
children[i] = g_variant_new_int32 (applet->priv->size_hints[i]);
g_variant_builder_add (builder, "{sv}", "SizeHints",
g_variant_new_array (G_VARIANT_TYPE_INT32,
children, applet->priv->size_hints_len));
g_free (children);
g_dbus_connection_emit_signal (applet->priv->connection,
NULL,
applet->priv->object_path,
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
g_variant_new ("(sa{sv}as)",
MATE_PANEL_APPLET_INTERFACE,
builder,
invalidated_builder),
&error);
if (error) {
g_printerr ("Failed to send signal PropertiesChanged::SizeHints: %s\n",
error->message);
g_error_free (error);
}
}
}
guint
mate_panel_applet_get_size (MatePanelApplet *applet)
{
g_return_val_if_fail (PANEL_IS_APPLET (applet), 0);
return applet->priv->size;
}
/* Applets cannot set their size, so API is not public. */
static void
mate_panel_applet_set_size (MatePanelApplet *applet,
guint size)
{
g_return_if_fail (PANEL_IS_APPLET (applet));
if (applet->priv->size == size)
return;
applet->priv->size = size;
g_signal_emit (G_OBJECT (applet),
mate_panel_applet_signals [CHANGE_SIZE],
0, size);
g_object_notify (G_OBJECT (applet), "size");
}
MatePanelAppletOrient
mate_panel_applet_get_orient (MatePanelApplet *applet)
{
g_return_val_if_fail (PANEL_IS_APPLET (applet), 0);
return applet->priv->orient;
}
/* Applets cannot set their orientation, so API is not public. */
static void
mate_panel_applet_set_orient (MatePanelApplet *applet,
MatePanelAppletOrient orient)
{
g_return_if_fail (PANEL_IS_APPLET (applet));
if (applet->priv->orient == orient)
return;
applet->priv->orient = orient;
g_signal_emit (G_OBJECT (applet),
mate_panel_applet_signals [CHANGE_ORIENT],
0, orient);
g_object_notify (G_OBJECT (applet), "orient");
}
static void
mate_panel_applet_set_locked (MatePanelApplet *applet,
gboolean locked)
{
GtkAction *action;
g_return_if_fail (PANEL_IS_APPLET (applet));
if (applet->priv->locked == locked)
return;
applet->priv->locked = locked;
action = mate_panel_applet_menu_get_action (applet, "Lock");
g_signal_handlers_block_by_func (action,
mate_panel_applet_menu_cmd_lock,
applet);
gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), locked);
g_signal_handlers_unblock_by_func (action,
mate_panel_applet_menu_cmd_lock,
applet);
mate_panel_applet_menu_update_actions (applet);
g_object_notify (G_OBJECT (applet), "locked");
if (applet->priv->connection) {
GError *error = NULL;
g_dbus_connection_emit_signal (applet->priv->connection,
NULL,
applet->priv->object_path,
MATE_PANEL_APPLET_INTERFACE,
locked ? "Lock" : "Unlock",
NULL, &error);
if (error) {
g_printerr ("Failed to send signal %s: %s\n",
locked ? "Lock" : "Unlock",
error->message);
g_error_free (error);
}
}
}
gboolean
mate_panel_applet_get_locked_down (MatePanelApplet *applet)
{
g_return_val_if_fail (PANEL_IS_APPLET (applet), FALSE);
return applet->priv->locked_down;
}
/* Applets cannot set the lockdown state, so API is not public. */
static void
mate_panel_applet_set_locked_down (MatePanelApplet *applet,
gboolean locked_down)
{
g_return_if_fail (PANEL_IS_APPLET (applet));
if (applet->priv->locked_down == locked_down)
return;
applet->priv->locked_down = locked_down;
mate_panel_applet_menu_update_actions (applet);
g_object_notify (G_OBJECT (applet), "locked-down");
}
#ifdef HAVE_X11
static Atom _net_wm_window_type = None;
static Atom _net_wm_window_type_dock = None;
static Atom _net_active_window = None;
static void
mate_panel_applet_init_atoms (Display *xdisplay)
{
if (_net_wm_window_type == None)
_net_wm_window_type = XInternAtom (xdisplay,
"_NET_WM_WINDOW_TYPE",
False);
if (_net_wm_window_type_dock == None)
_net_wm_window_type_dock = XInternAtom (xdisplay,
"_NET_WM_WINDOW_TYPE_DOCK",
False);
if (_net_active_window == None)
_net_active_window = XInternAtom (xdisplay,
"_NET_ACTIVE_WINDOW",
False);
}
static Window
mate_panel_applet_find_toplevel_dock_window (MatePanelApplet *applet,
Display *xdisplay)
{
GtkWidget *toplevel;
Window xwin;
Window root, parent, *child;
int num_children;
toplevel = gtk_widget_get_toplevel (GTK_WIDGET (applet));
if (!gtk_widget_get_realized (toplevel))
return None;
xwin = GDK_WINDOW_XID (gtk_widget_get_window (toplevel));
child = NULL;
parent = root = None;
do {
Atom type_return;
Atom window_type;
int format_return;
gulong number_return, bytes_after_return;
guchar *data_return;
XGetWindowProperty (xdisplay,
xwin,
_net_wm_window_type,
0, 1, False,
XA_ATOM,
&type_return, &format_return,
&number_return,
&bytes_after_return,
&data_return);
if (type_return == XA_ATOM) {
window_type = *(Atom *) data_return;
XFree (data_return);
data_return = NULL;
if (window_type == _net_wm_window_type_dock)
return xwin;
}
if (!XQueryTree (xdisplay,
xwin,
&root, &parent, &child,
(guint *) &num_children)) {
return None;
}
if (child && num_children > 0)
XFree (child);
xwin = parent;
} while (xwin != None && xwin != root);
return None;
}
#endif // HAVE_X11
/* This function
* 1) Gets the window id of the panel that contains the applet
* using XQueryTree and XGetWindowProperty to find an ancestor
* window with the _NET_WM_WINDOW_TYPE_DOCK window type.
* 2) Sends a _NET_ACTIVE_WINDOW message to get that panel focused
*/
void
mate_panel_applet_request_focus (MatePanelApplet *applet,
guint32 timestamp)
{
#ifdef HAVE_X11
GdkScreen *screen;
GdkWindow *root;
GdkDisplay *display;
Display *xdisplay;
Window dock_xwindow;
Window xroot;
XEvent xev;
if (!GDK_IS_X11_DISPLAY (gdk_display_get_default ()))
return;
g_return_if_fail (PANEL_IS_APPLET (applet));
screen = gtk_window_get_screen (GTK_WINDOW (applet->priv->plug));
root = gdk_screen_get_root_window (screen);
display = gdk_screen_get_display (screen);
xdisplay = GDK_DISPLAY_XDISPLAY (display);
xroot = GDK_WINDOW_XID (root);
mate_panel_applet_init_atoms (xdisplay);
dock_xwindow = mate_panel_applet_find_toplevel_dock_window (applet, xdisplay);
if (dock_xwindow == None)
return;
xev.xclient.type = ClientMessage;
xev.xclient.serial = 0;
xev.xclient.send_event = True;
xev.xclient.window = dock_xwindow;
xev.xclient.message_type = _net_active_window;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 1; /* requestor type; we're an app, I guess */
xev.xclient.data.l[1] = timestamp;
xev.xclient.data.l[2] = None; /* "currently active window", supposedly */
xev.xclient.data.l[3] = 0;
xev.xclient.data.l[4] = 0;
XSendEvent (xdisplay,
xroot, False,
SubstructureRedirectMask | SubstructureNotifyMask,
&xev);
#endif
}
static GtkAction *
mate_panel_applet_menu_get_action (MatePanelApplet *applet,
const gchar *action)
{
return gtk_action_group_get_action (applet->priv->panel_action_group, action);
}
static void
mate_panel_applet_menu_update_actions (MatePanelApplet *applet)
{
gboolean locked = applet->priv->locked;
gboolean locked_down = applet->priv->locked_down;
g_object_set (mate_panel_applet_menu_get_action (applet, "Lock"),
"visible", !locked_down, NULL);
g_object_set (mate_panel_applet_menu_get_action (applet, "Move"),
"sensitive", !locked,
"visible", !locked_down,
NULL);
g_object_set (mate_panel_applet_menu_get_action (applet, "Remove"),
"sensitive", !locked,
"visible", !locked_down,
NULL);
}
static void
mate_panel_applet_menu_cmd_remove (GtkAction *action,
MatePanelApplet *applet)
{
GError *error = NULL;
if (!applet->priv->connection)
return;
g_dbus_connection_emit_signal (applet->priv->connection,
NULL,
applet->priv->object_path,
MATE_PANEL_APPLET_INTERFACE,
"RemoveFromPanel",
NULL, &error);
if (error) {
g_printerr ("Failed to send signal RemoveFromPanel: %s\n",
error->message);
g_error_free (error);
}
}
static void
mate_panel_applet_menu_cmd_move (GtkAction *action,
MatePanelApplet *applet)
{
GError *error = NULL;
if (!applet->priv->connection)
return;
g_dbus_connection_emit_signal (applet->priv->connection,
NULL,
applet->priv->object_path,
MATE_PANEL_APPLET_INTERFACE,
"Move",
NULL, &error);
if (error) {
g_printerr ("Failed to send signal RemoveFromPanel: %s\n",
error->message);
g_error_free (error);
}
}
static void
mate_panel_applet_menu_cmd_lock (GtkAction *action,
MatePanelApplet *applet)
{
gboolean locked;
locked = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
mate_panel_applet_set_locked (applet, locked);
}
void
mate_panel_applet_setup_menu (MatePanelApplet *applet,
const gchar *xml,
GtkActionGroup *applet_action_group)
{
gchar *new_xml;
GError *error = NULL;
g_return_if_fail (PANEL_IS_APPLET (applet));
g_return_if_fail (xml != NULL);
if (applet->priv->applet_action_group)
return;
applet->priv->applet_action_group = g_object_ref (applet_action_group);
gtk_ui_manager_insert_action_group (applet->priv->ui_manager,
applet_action_group, 0);
new_xml = g_strdup_printf ("<ui><popup name=\"MatePanelAppletPopup\" action=\"AppletItems\">"
"<placeholder name=\"AppletItems\">%s\n</placeholder>\n"
"</popup></ui>\n", xml);
gtk_ui_manager_add_ui_from_string (applet->priv->ui_manager, new_xml, -1, &error);
g_free (new_xml);
gtk_ui_manager_ensure_update (applet->priv->ui_manager);
if (error) {
g_warning ("Error merging menus: %s\n", error->message);
g_error_free (error);
}
}
void
mate_panel_applet_setup_menu_from_file (MatePanelApplet *applet,
const gchar *filename,
GtkActionGroup *applet_action_group)
{
gchar *xml = NULL;
GError *error = NULL;
if (g_file_get_contents (filename, &xml, NULL, &error)) {
mate_panel_applet_setup_menu (applet, xml, applet_action_group);
} else {
g_warning ("%s", error->message);
g_error_free (error);
}
g_free (xml);
}
/**
* mate_panel_applet_setup_menu_from_resource:
* @applet: a #MatePanelApplet.
* @resource_path: a resource path
* @action_group: a #GtkActionGroup.
*
* Sets up the context menu of @applet. @filename is a resource path to a menu
* XML file, containing a #GtkUIManager UI definition that describes how to
* display the menu items. @action_group contains the various #GtkAction that
* are referenced in @xml.
*
* See also the <link linkend="getting-started.context-menu">Context
* Menu</link> section.
*
* Since: 1.20.1
**/
void
mate_panel_applet_setup_menu_from_resource (MatePanelApplet *applet,
const gchar *resource_path,
GtkActionGroup *action_group)
{
GBytes *bytes;
GError *error = NULL;
bytes = g_resources_lookup_data (resource_path,
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
if (bytes) {
mate_panel_applet_setup_menu (applet,
g_bytes_get_data (bytes, NULL),
action_group);
} else {
g_warning ("%s", error->message);
g_error_free (error);
}
g_bytes_unref (bytes);
}
static void
mate_panel_applet_finalize (GObject *object)
{
MatePanelApplet *applet = MATE_PANEL_APPLET (object);
if (applet->priv->connection) {
if (applet->priv->object_id)
g_dbus_connection_unregister_object (applet->priv->connection,
applet->priv->object_id);
applet->priv->object_id = 0;
g_object_unref (applet->priv->connection);
applet->priv->connection = NULL;
}
if (applet->priv->object_path) {
g_free (applet->priv->object_path);
applet->priv->object_path = NULL;
}
mate_panel_applet_set_preferences_path (applet, NULL);
if (applet->priv->applet_action_group) {
g_object_unref (applet->priv->applet_action_group);
applet->priv->applet_action_group = NULL;
}
if (applet->priv->panel_action_group) {
g_object_unref (applet->priv->panel_action_group);
applet->priv->panel_action_group = NULL;
}
if (applet->priv->ui_manager) {
g_object_unref (applet->priv->ui_manager);
applet->priv->ui_manager = NULL;
}
g_free (applet->priv->size_hints);
g_free (applet->priv->prefs_path);
g_free (applet->priv->background);
g_free (applet->priv->id);
/* closure is owned by the factory */
applet->priv->closure = NULL;
G_OBJECT_CLASS (mate_panel_applet_parent_class)->finalize (object);
}
static gboolean
container_has_focusable_child (GtkContainer *container)
{
GtkWidget *child;
GList *list;
GList *t;
gboolean retval = FALSE;
list = gtk_container_get_children (container);
for (t = list; t; t = t->next) {
child = GTK_WIDGET (t->data);
if (gtk_widget_get_can_focus (child)) {
retval = TRUE;
break;
} else if (GTK_IS_CONTAINER (child)) {
retval = container_has_focusable_child (GTK_CONTAINER (child));
if (retval)
break;
}
}
g_list_free (list);
return retval;
}
static void
mate_panel_applet_menu_popup (MatePanelApplet *applet,
GdkEvent *event)
{
GtkWidget *menu;
menu = gtk_ui_manager_get_widget (applet->priv->ui_manager,
"/MatePanelAppletPopup");
/* Set up theme and transparency support */
GtkWidget *toplevel = gtk_widget_get_toplevel (menu);
/* Fix any failures of compiz/other wm's to communicate with gtk for transparency */
GdkScreen *screen = gtk_widget_get_screen(GTK_WIDGET(toplevel));
GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
gtk_widget_set_visual(GTK_WIDGET(toplevel), visual);
/* Set menu and it's toplevel window to follow panel theme */
GtkStyleContext *context;
context = gtk_widget_get_style_context (GTK_WIDGET(toplevel));
gtk_style_context_add_class(context,"gnome-panel-menu-bar");
gtk_style_context_add_class(context,"mate-panel-menu-bar");
gtk_menu_popup_at_widget (GTK_MENU (menu),
GTK_WIDGET (applet),
GDK_GRAVITY_NORTH_WEST,
GDK_GRAVITY_NORTH_WEST,
event);
}
static gboolean
mate_panel_applet_can_focus (GtkWidget *widget)
{
/*
* A MatePanelApplet widget can focus if it has a tooltip or it does
* not have any focusable children.
*/
if (gtk_widget_get_has_tooltip (widget))
return TRUE;
if (!PANEL_IS_APPLET (widget))
return FALSE;
return !container_has_focusable_child (GTK_CONTAINER (widget));
}
/* Taken from libmatecomponentui/matecomponent/matecomponent-plug.c */
static gboolean
mate_panel_applet_button_event (MatePanelApplet *applet,
GdkEventButton *event)
{
#ifdef HAVE_X11
GtkWidget *widget;
GdkWindow *window;
GdkWindow *socket_window;
XEvent xevent;
GdkDisplay *display;
if (!applet->priv->out_of_process)
return FALSE;
widget = applet->priv->plug;
if (!gtk_widget_is_toplevel (widget))
return FALSE;
window = gtk_widget_get_window (widget);
socket_window = gtk_plug_get_socket_window (GTK_PLUG (widget));
display = gdk_display_get_default ();
if (!GDK_IS_X11_DISPLAY (display))
return FALSE;
if (event->type == GDK_BUTTON_PRESS) {
GdkSeat *seat;
xevent.xbutton.type = ButtonPress;
seat = gdk_display_get_default_seat (display);
/* X does an automatic pointer grab on button press
* if we have both button press and release events
* selected.
* We don't want to hog the pointer on our parent.
*/
gdk_seat_ungrab (seat);
} else {
xevent.xbutton.type = ButtonRelease;
}
xevent.xbutton.display = GDK_WINDOW_XDISPLAY (window);
xevent.xbutton.window = GDK_WINDOW_XID (socket_window);
xevent.xbutton.root = GDK_WINDOW_XID (gdk_screen_get_root_window
(gdk_window_get_screen (window)));
/*
* FIXME: the following might cause
* big problems for non-GTK apps
*/
xevent.xbutton.x = 0;
xevent.xbutton.y = 0;
xevent.xbutton.x_root = 0;
xevent.xbutton.y_root = 0;
xevent.xbutton.state = event->state;
xevent.xbutton.button = event->button;
xevent.xbutton.same_screen = TRUE; /* FIXME ? */
gdk_x11_display_error_trap_push (display);
XSendEvent (GDK_WINDOW_XDISPLAY (window),
GDK_WINDOW_XID (socket_window),
False, NoEventMask, &xevent);
gdk_display_flush (display);
gdk_x11_display_error_trap_pop_ignored (display);
return TRUE;
#else
return FALSE;
#endif
}
static gboolean
mate_panel_applet_button_press (GtkWidget *widget,
GdkEventButton *event)
{
MatePanelApplet *applet = MATE_PANEL_APPLET (widget);
if (!container_has_focusable_child (GTK_CONTAINER (applet))) {
if (!gtk_widget_has_focus (widget)) {
gtk_widget_set_can_focus (widget, TRUE);
gtk_widget_grab_focus (widget);
}
}
if (event->button == 3) {
mate_panel_applet_menu_popup (applet, (GdkEvent *) event);
return TRUE;
}
return mate_panel_applet_button_event (applet, event);
}
static gboolean
mate_panel_applet_button_release (GtkWidget *widget,
GdkEventButton *event)
{
MatePanelApplet *applet = MATE_PANEL_APPLET (widget);
return mate_panel_applet_button_event (applet, event);
}
static gboolean
mate_panel_applet_key_press_event (GtkWidget *widget,
GdkEventKey *event)
{
mate_panel_applet_menu_popup (MATE_PANEL_APPLET (widget), (GdkEvent *) event);
return TRUE;
}
static void
mate_panel_applet_get_preferred_width (GtkWidget *widget,
int *minimum_width,
int *natural_width)