-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtkUnixSysTray.c
More file actions
1731 lines (1593 loc) · 43.6 KB
/
tkUnixSysTray.c
File metadata and controls
1731 lines (1593 loc) · 43.6 KB
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
/*
* tkUnixSysTray.c --
*
* tkUnixSysTray.c implements a "systray" Tcl command which permits to
* change the system tray/taskbar icon of a Tk toplevel window and
* to post system notifications.
*
* Copyright © 2005 Anton Kovalenko.
* Copyright © 2020 Kevin Walzer/WordTech Communications LLC.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkInt.h"
#include "tkUnixInt.h"
/*
* Based extensively on the tktray extension package. Here we are removing
* non-essential parts of tktray.
*/
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <X11/X.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
/* XEmbed definitions
* See http://www.freedesktop.org/wiki/Standards_2fxembed_2dspec
* */
#define XEMBED_MAPPED (1<<0)
/* System tray opcodes
* See http://www.freedesktop.org/wiki/Standards_2fsystemtray_2dspec
* */
#define SYSTEM_TRAY_REQUEST_DOCK 0
#define SYSTEM_TRAY_BEGIN_MESSAGE 1
#define SYSTEM_TRAY_CANCEL_MESSAGE 2
/* Flags of widget configuration options */
#define ICON_CONF_IMAGE (1<<0) /* Image changed */
#define ICON_CONF_REDISPLAY (1<<1) /* Redisplay required */
#define ICON_CONF_XEMBED (1<<2) /* Remapping or unmapping required */
#define ICON_CONF_CLASS (1<<3) /* TODO WM_CLASS update required */
#define ICON_CONF_FIRST_TIME (1<<4) /* For IconConfigureMethod invoked by the constructor */
/* Widget states */
#define ICON_FLAG_REDRAW_PENDING (1<<0)
#define ICON_FLAG_ARGB32 (1<<1)
#define ICON_FLAG_DIRTY_EDGES (1<<2)
#define TKU_NO_BAD_WINDOW_BEGIN(display) \
{ Tk_ErrorHandler error__handler = \
Tk_CreateErrorHandler(display, BadWindow, -1, -1, NULL, NULL);
#define TKU_NO_BAD_WINDOW_END Tk_DeleteErrorHandler(error__handler); }
/*Declaration for utility functions.*/
static void TKU_WmWithdraw(Tk_Window winPtr, Tcl_Interp* interp);
static Tk_Window TKU_GetWrapper(Tk_Window winPtr);
void TKU_AddInput(Display* dpy, Window win, long add_to_mask);
static Tk_Window TKU_Wrapper(Tk_Window w, Tcl_Interp* interp);
static Window TKU_XID(Tk_Window w);
/* Customized window withdraw */
static void
TKU_WmWithdraw(
Tk_Window winPtr,
TCL_UNUSED(Tcl_Interp *))
{
TkpWmSetState((TkWindow*)winPtr, WithdrawnState);
}
/* The wrapper should exist */
static Tk_Window
TKU_GetWrapper(
Tk_Window winPtr)
{
return (Tk_Window)
TkpGetWrapperWindow((TkWindow*)winPtr);
}
/* Subscribe for extra X11 events (needed for MANAGER selection) */
void
TKU_AddInput(
Display* dpy,
Window win,
long add_to_mask)
{
XWindowAttributes xswa;
TKU_NO_BAD_WINDOW_BEGIN(dpy)
XGetWindowAttributes(dpy,win,&xswa);
XSelectInput(dpy,win,xswa.your_event_mask|add_to_mask);
TKU_NO_BAD_WINDOW_END
}
/* Get Tk Window wrapper (make it exist if ny) */
static Tk_Window
TKU_Wrapper(
Tk_Window w,
Tcl_Interp* interp)
{
Tk_Window wrapper = TKU_GetWrapper(w);
if (!wrapper) {
Tk_MakeWindowExist(w);
TKU_WmWithdraw(w, interp);
Tk_MapWindow(w);
wrapper = (Tk_Window) TKU_GetWrapper(w);
}
return wrapper;
}
/* Return X window id for Tk window (make it exist if ny) */
static Window
TKU_XID(
Tk_Window w)
{
Window xid = Tk_WindowId(w);
if (xid == None) {
Tk_MakeWindowExist(w);
xid = Tk_WindowId(w);
}
return xid;
}
/* Data structure representing dock widget */
typedef struct {
/* standard for widget */
Tk_Window tkwin, drawingWin;
Window wrapper;
Window myManager;
Window trayManager;
Tk_OptionTable options;
Tcl_Interp *interp;
Tcl_Command widgetCmd;
Tk_Image image; /* image to be drawn */
/* Only one of imageVisualInstance and photo is needed for argb32
* operations. Unless imageString changes, imageVisualInstance is
* always valid for the same drawingWin instance, but photo is
* invalidated by any "whole image" type change. */
Tk_Image imageVisualInstance; /* image instance for use with argb32 */
Tk_PhotoHandle photo; /* !null if it's really a photo */
/* Offscreen pixmap is created for a given imageWidth,
* imageHeight, drawingWin, and invalidated (and freed) on image
* resize or drawingWin destruction.
* Contents of this pixmap is synced on demand; when image changes
* but is not resized, pixmap is marked as out-of-sync. Next time
* when redisplay is needed, pixmap is updated before drawing
* operation.
*/
Pixmap offscreenPixmap;
/* There is no need to recreate GC ever; it remains valid once
* created */
GC offscreenGC;
/* XImage for drawing ARGB32 photo on offscreenPixmap. Should be
* freed and nullified each time when a pixmap is freed. Needed
* (and created) when redrawing an image being a photo on ARGB32
* offscreen pixmap. */
XImage *offscreenImage; /* for photo (argb32) drawing code */
Visual *bestVisual; /* Visual, when it's specified by tray
* manager AND is guessed to be
* ARGB32 */
Colormap bestColormap; /* Colormap for bestVisual */
Atom aMANAGER;
Atom a_NET_SYSTEM_TRAY_Sn;
Atom a_XEMBED_INFO;
Atom a_NET_SYSTEM_TRAY_MESSAGE_DATA;
Atom a_NET_SYSTEM_TRAY_OPCODE;
Atom a_NET_SYSTEM_TRAY_ORIENTATION;
Atom a_NET_SYSTEM_TRAY_VISUAL;
int flags; /* ICON_FLAG_ - see defines above */
int msgid; /* Last balloon message ID */
int useShapeExt;
int x,y,width,height;
int imageWidth, imageHeight;
int requestedWidth, requestedHeight;
int visible; /* whether XEMBED_MAPPED should be set */
int docked; /* whether an icon should be docked */
Tcl_Obj *imageObj; /* option: -image */
Tcl_Obj *classObj; /* option: -class */
} DockIcon;
/*
* Forward declarations for procedures defined in this file.
*/
static Tcl_ObjCmdProc TrayIconCreateCmd;
static Tcl_ObjCmdProc TrayIconObjectCmd;
static int TrayIconConfigureMethod(DockIcon *icon, Tcl_Interp *interp,
Tcl_Size objc, Tcl_Obj *const objv[], int addflags);
static int PostBalloon(DockIcon* icon, const char *utf8msg,
long timeout);
static void CancelBalloon(DockIcon* icon, int msgid);
static int QueryTrayOrientation(DockIcon* icon);
static Tcl_CmdDeleteProc TrayIconDeleteProc;
static Atom DockSelectionAtomFor(Tk_Window tkwin);
static void DockToManager(DockIcon *icon);
static void CreateTrayIconWindow(DockIcon *icon);
static void TrayIconRequestSize(DockIcon* icon, int w, int h);
static void TrayIconForceImageChange(DockIcon* icon);
static void TrayIconUpdate(DockIcon* icon, int mask);
static void EventuallyRedrawIcon(DockIcon* icon);
static void DisplayIcon(void *cd);
static void RetargetEvent(DockIcon *icon, XEvent *ev);
static void TrayIconEvent(void *cd, XEvent* ev);
static void UserIconEvent(void *cd, XEvent* ev);
static void TrayIconWrapperEvent(void *cd, XEvent* ev);
static int IconGenericHandler(void *cd, XEvent *ev);
int Tktray_Init (Tcl_Interp* interp );
/*
*----------------------------------------------------------------------
*
* TrayIconObjectCmd --
*
* Manage attributes of tray icon.
*
* Results:
* Various values of the tray icon are set and retrieved.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
TrayIconObjectCmd(
void *cd,
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
DockIcon *icon = (DockIcon*)cd;
int bbox[4] = {0,0,0,0};
Tcl_Obj * bboxObj;
int wcmd;
int i;
XWindowAttributes xwa;
Window bogus;
int msgid;
enum {XWC_CONFIGURE = 0, XWC_CGET, XWC_BALLOON, XWC_CANCEL,
XWC_BBOX, XWC_DOCKED, XWC_ORIENTATION};
const char *st_wcmd[] = {"configure", "cget", "balloon", "cancel",
"bbox", "docked", "orientation", NULL};
long timeout = 0;
Tcl_Obj* optionValue;
if (objc<2) {
Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?args?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objv[1], st_wcmd,
"subcommand", TCL_EXACT, &wcmd) != TCL_OK) {
return TCL_ERROR;
}
switch (wcmd) {
case XWC_CONFIGURE:
return TrayIconConfigureMethod(icon,interp,objc-2,objv+2,0);
case XWC_CGET:
if (objc != 3) {
Tcl_WrongNumArgs(interp,2,objv,"option");
return TCL_ERROR;
}
optionValue = Tk_GetOptionValue(interp,(char*)icon,
icon->options,objv[2],icon->tkwin);
if (optionValue) {
Tcl_SetObjResult(interp,optionValue);
return TCL_OK;
} else {
return TCL_ERROR;
}
case XWC_BALLOON:
if ((objc != 3) && (objc != 4)) {
Tcl_WrongNumArgs(interp, 2, objv, "message ?timeout?");
return TCL_ERROR;
}
if (objc == 4) {
if (Tcl_GetLongFromObj(interp,objv[3],&timeout) != TCL_OK) {
return TCL_ERROR;
}
}
msgid = PostBalloon(icon,Tcl_GetString(objv[2]), timeout);
Tcl_SetObjResult(interp,Tcl_NewIntObj(msgid));
return TCL_OK;
case XWC_CANCEL:
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "messageId");
return TCL_ERROR;
}
if (Tcl_GetIntFromObj(interp,objv[2],&msgid) != TCL_OK) {
return TCL_ERROR;
}
if (msgid) {
CancelBalloon(icon,msgid);
}
return TCL_OK;
case XWC_BBOX:
if (icon->drawingWin) {
XGetWindowAttributes(Tk_Display(icon->drawingWin),
TKU_XID(icon->drawingWin), &xwa);
XTranslateCoordinates(Tk_Display(icon->drawingWin),
TKU_XID(icon->drawingWin), xwa.root, 0,0,
&icon->x, &icon->y, &bogus);
bbox[0] = icon->x;
bbox[1] = icon->y;
bbox[2] = bbox[0] + icon->width - 1;
bbox[3] = bbox[1] + icon->height - 1;
}
bboxObj = Tcl_NewObj();
for (i = 0; i < 4; ++i) {
Tcl_ListObjAppendElement(interp, bboxObj, Tcl_NewIntObj(bbox[i]));
}
Tcl_SetObjResult(interp, bboxObj);
return TCL_OK;
case XWC_DOCKED:
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(icon->myManager != None));
return TCL_OK;
case XWC_ORIENTATION:
if (icon->myManager == None || icon->wrapper == None) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("none", TCL_INDEX_NONE));
} else {
switch(QueryTrayOrientation(icon)) {
case 0:
Tcl_SetObjResult(interp, Tcl_NewStringObj("horizontal", TCL_INDEX_NONE));
break;
case 1:
Tcl_SetObjResult(interp, Tcl_NewStringObj("vertical", TCL_INDEX_NONE));
break;
default:
Tcl_SetObjResult(interp, Tcl_NewStringObj("unknown", TCL_INDEX_NONE));
break;
}
}
return TCL_OK;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* QueryTrayOrientation --
*
* Obtain the orientation of the tray icon.
*
* Results:
* Orientation is returned.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
QueryTrayOrientation(
DockIcon* icon)
{
Atom retType = None;
int retFormat = 32;
unsigned long retNitems, retBytesAfter;
unsigned char *retProp = NULL;
int result = -1;
if (icon->wrapper != None && icon->myManager != None) {
XGetWindowProperty(Tk_Display(icon->tkwin),
icon->myManager,
icon->a_NET_SYSTEM_TRAY_ORIENTATION,
/* offset */ 0,
/* length */ 1,
/* delete */ False,
/* type */ XA_CARDINAL,
&retType, &retFormat, &retNitems,
&retBytesAfter, &retProp);
if (retType == XA_CARDINAL && retFormat == 32 && retNitems == 1) {
result = (int) *(long*)retProp;
}
if (retProp) {
XFree(retProp);
}
}
return result;
}
/*
*----------------------------------------------------------------------
*
* DockSelectionAtomFor --
*
* Obtain the dock selection atom.
*
* Results:
* Selection returned.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static Atom
DockSelectionAtomFor(
Tk_Window tkwin)
{
char buf[256];
snprintf(buf,256,"_NET_SYSTEM_TRAY_S%d",Tk_ScreenNumber(tkwin));
return Tk_InternAtom(tkwin,buf);
}
/*
*----------------------------------------------------------------------
*
* XembedSetState --
*
* Set the xembed state.
*
* Results:
* Updates the xembed state.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
XembedSetState(
DockIcon *icon,
long xembedState)
{
long info[] = { 0, 0 };
info[1] = xembedState;
if (icon->drawingWin) {
XChangeProperty(Tk_Display(icon->drawingWin),
icon->wrapper,
icon->a_XEMBED_INFO,
icon->a_XEMBED_INFO, 32,
PropModeReplace, (unsigned char*)info, 2);
}
}
/*
*----------------------------------------------------------------------
*
* XembedRequestDock --
*
* Obtain the docking window.
*
* Results:
* The dock window is requested.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
XembedRequestDock(
DockIcon *icon)
{
Tk_Window tkwin = icon->drawingWin;
XEvent ev;
Display *dpy = Tk_Display(tkwin);
memset(&ev, 0, sizeof(ev));
ev.xclient.type = ClientMessage;
ev.xclient.window = icon->myManager;
ev.xclient.message_type = icon->a_NET_SYSTEM_TRAY_OPCODE;
ev.xclient.format = 32;
ev.xclient.data.l[0] = 0;
ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
ev.xclient.data.l[2] = icon->wrapper;
ev.xclient.data.l[3] = 0;
ev.xclient.data.l[4] = 0;
XSendEvent(dpy, icon->myManager, True, StructureNotifyMask|SubstructureNotifyMask, &ev);
}
/*
*----------------------------------------------------------------------
*
* CheckArgbVisual --
*
* Find out if a visual is recommended and if it looks like argb32.
*
* Results:
* Render the visual as needed.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
CheckArgbVisual(
DockIcon *icon)
{
/* Find out if a visual is recommended and if it looks like argb32.
* For such visuals we should:
* Recreate a window if it's created but the depth is wrong;
* Don't use ParentRelative but blank background.
* For photo images, draw into a window by XPutImage.
*/
Atom retType = None;
int retFormat = 32;
unsigned long retNitems, retBytesAfter;
unsigned char *retProp = NULL;
Visual *match = NULL;
int depth = 0;
Colormap cmap = None;
TKU_NO_BAD_WINDOW_BEGIN(Tk_Display(icon->tkwin))
XGetWindowProperty(Tk_Display(icon->tkwin),
icon->trayManager,
icon->a_NET_SYSTEM_TRAY_VISUAL,
/* offset */ 0,
/* length */ 1,
/* delete */ False,
/* type */ XA_VISUALID,
&retType, &retFormat, &retNitems,
&retBytesAfter, &retProp);
TKU_NO_BAD_WINDOW_END
if (retType == XA_VISUALID &&
retNitems == 1 &&
retFormat == 32) {
char numeric[256];
snprintf(numeric,256,"%ld",*(long*)retProp);
XFree(retProp);
match = Tk_GetVisual(icon->interp, icon->tkwin,
numeric, &depth, &cmap);
}
if (match&& depth == 32 &&
match->red_mask == 0xFF0000UL &&
match->green_mask == 0x00FF00UL &&
match->blue_mask == 0x0000FFUL) {
icon->bestVisual = match;
icon->bestColormap = cmap;
} else {
icon->bestVisual = NULL;
icon->bestColormap = None;
}
}
/*
*----------------------------------------------------------------------
*
* CreateTrayIconWindow --
*
* Create and configure the window for the icon tray.
*
* Results:
* The window is created and displayed.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
CreateTrayIconWindow(
DockIcon *icon)
{
Tcl_InterpState saved;
Tk_Window tkwin;
Tk_Window wrapper;
XSetWindowAttributes attr;
saved = Tcl_SaveInterpState(icon->interp, TCL_OK);
/* Use the same name (tail) as the widget name, to enable
* name-based icon management for supporting trays, as promised by
* the docs.
*/
tkwin = icon->drawingWin = Tk_CreateWindow(icon->interp, icon->tkwin,
Tk_Name(icon->tkwin), "");
if (tkwin) {
Tk_SetClass(icon->drawingWin, Tcl_GetString(icon->classObj));
Tk_CreateEventHandler(icon->drawingWin,ExposureMask|StructureNotifyMask|
ButtonPressMask|ButtonReleaseMask|
EnterWindowMask|LeaveWindowMask|PointerMotionMask,
TrayIconEvent, icon);
if(icon->bestVisual) {
Tk_SetWindowVisual(icon->drawingWin,icon->bestVisual,
32,icon->bestColormap);
icon->flags |= ICON_FLAG_ARGB32;
Tk_SetWindowBackground(tkwin, 0);
} else {
Tk_SetWindowBackgroundPixmap(tkwin, ParentRelative);
icon->flags &= ~ICON_FLAG_ARGB32;
}
Tk_MakeWindowExist(tkwin);
TKU_WmWithdraw(tkwin,icon->interp);
wrapper = TKU_Wrapper(tkwin,icon->interp);
attr.override_redirect = True;
Tk_ChangeWindowAttributes(wrapper,CWOverrideRedirect,&attr);
Tk_CreateEventHandler(wrapper,StructureNotifyMask,TrayIconWrapperEvent, icon);
if (!icon->bestVisual) {
Tk_SetWindowBackgroundPixmap(wrapper, ParentRelative);
} else {
Tk_SetWindowBackground(tkwin, 0);
}
icon->wrapper = TKU_XID(wrapper);
TrayIconForceImageChange(icon);
} else {
Tcl_BackgroundError(icon->interp);
}
Tcl_RestoreInterpState(icon->interp, saved);
}
/*
*----------------------------------------------------------------------
*
* DockToManager --
*
* Helper function to manage icon in display.
*
* Results:
* Icon is created and displayed.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
DockToManager(
DockIcon *icon)
{
icon->myManager = icon->trayManager;
Tk_SendVirtualEvent(icon->tkwin,Tk_GetUid("IconCreate"), NULL);
XembedSetState(icon, icon->visible ? XEMBED_MAPPED : 0);
XembedRequestDock(icon);
}
static const
Tk_OptionSpec IconOptionSpec[] = {
{TK_OPTION_STRING,"-image","image","Image",
NULL, offsetof(DockIcon, imageObj), TCL_INDEX_NONE,
TK_OPTION_NULL_OK, NULL,
ICON_CONF_IMAGE | ICON_CONF_REDISPLAY},
{TK_OPTION_STRING,"-class","class","Class",
"TrayIcon", offsetof(DockIcon, classObj), TCL_INDEX_NONE,
0, NULL, ICON_CONF_CLASS},
{TK_OPTION_BOOLEAN,"-docked","docked","Docked",
"1", TCL_INDEX_NONE, offsetof(DockIcon, docked), 0, NULL,
ICON_CONF_XEMBED | ICON_CONF_REDISPLAY},
{TK_OPTION_BOOLEAN,"-shape","shape","Shape",
"0", TCL_INDEX_NONE, offsetof(DockIcon, useShapeExt), 0, NULL,
ICON_CONF_IMAGE | ICON_CONF_REDISPLAY},
{TK_OPTION_BOOLEAN,"-visible","visible","Visible",
"1", TCL_INDEX_NONE, offsetof(DockIcon, visible), 0, NULL,
ICON_CONF_XEMBED | ICON_CONF_REDISPLAY},
{TK_OPTION_END, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0}
};
/*
*----------------------------------------------------------------------
*
* TrayIconRequestSize --
*
* Set icon size.
*
* Results:
* Icon size is obtained/set.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
TrayIconRequestSize(
DockIcon* icon,
int w,
int h)
{
if (icon->drawingWin) {
if (icon->requestedWidth != w ||
icon->requestedHeight != h) {
Tk_SetMinimumRequestSize(icon->drawingWin,w,h);
Tk_GeometryRequest(icon->drawingWin,w,h);
Tk_SetGrid(icon->drawingWin,1,1,w,h);
icon->requestedWidth = w;
icon->requestedHeight = h;
}
} else {
/* Sign that no size is requested yet */
icon->requestedWidth = 0;
icon->requestedHeight = 0;
}
}
/*
*----------------------------------------------------------------------
*
* TrayIconImageChanged --
*
* Fires when icon state changes.
*
* Results:
* Icon changes are rendered.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
TrayIconImageChanged(
void *cd,
int x,
int y,
int w,
int h,
int imgw,
int imgh)
{
DockIcon *icon = (DockIcon*) cd;
if (imgw != icon->imageWidth || imgh != icon->imageHeight) {
if (icon->offscreenImage) {
XDestroyImage(icon->offscreenImage);
icon->offscreenImage = NULL;
}
if (icon->offscreenPixmap) {
/* its size is bad */
Tk_FreePixmap(Tk_Display(icon->tkwin), icon->offscreenPixmap);
icon->offscreenPixmap = None;
}
/* if some image dimension decreases,
* empty areas around the image should be cleared */
if (imgw < icon->imageWidth || imgh < icon->imageHeight) {
icon->flags |= ICON_FLAG_DIRTY_EDGES;
}
}
icon->imageWidth = imgw;
icon->imageHeight = imgh;
if (imgw == w && imgh == h && x == 0 && y == 0) {
icon->photo = NULL; /* invalidate */
}
TrayIconRequestSize(icon,imgw,imgh);
EventuallyRedrawIcon(icon);
}
/*
*----------------------------------------------------------------------
*
* IgnoreImageChange --
*
* Currently no-op.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
IgnoreImageChange(
TCL_UNUSED(void *),
TCL_UNUSED(int),
TCL_UNUSED(int),
TCL_UNUSED(int),
TCL_UNUSED(int),
TCL_UNUSED(int),
TCL_UNUSED(int))
{
}
/*
*----------------------------------------------------------------------
*
* ForceImageChange --
*
* Push icon changes through.
*
* Results:
* Icon image is updated.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
TrayIconForceImageChange(
DockIcon* icon)
{
if (icon->image) {
int w,h;
Tk_SizeOfImage(icon->image,&w,&h);
TrayIconImageChanged(icon, 0, 0, w, h, w, h);
}
}
/*
*----------------------------------------------------------------------
*
* EventuallyRedrawIcon --
*
* Update image icon.
*
* Results:
* Icon image is updated.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
EventuallyRedrawIcon(
DockIcon* icon)
{
if (icon->drawingWin && icon->myManager) { /* don't redraw invisible icon */
if (!(icon->flags & ICON_FLAG_REDRAW_PENDING)) { /* don't schedule multiple redraw ops */
icon->flags |= ICON_FLAG_REDRAW_PENDING;
Tcl_DoWhenIdle(DisplayIcon, icon);
}
}
}
/*
*----------------------------------------------------------------------
*
* DisplayIcon --
*
* Main function for displaying icon.
*
* Results:
* Icon image is displayed.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
DisplayIcon(
void *cd)
{
DockIcon *icon = (DockIcon*)cd;
int w = icon->imageWidth, h = icon->imageHeight;
int imgx, imgy, outx, outy, outw, outh;
imgx = (icon->width >= w) ? 0 : -(icon->width - w)/2;
imgy = (icon->height >= h) ? 0 : -(icon->height - h)/2;
outx = (icon->width >= w) ? (icon->width - w)/2 : 0;
outy = (icon->height >= h) ? (icon->height - h)/2 : 0;
outw = (icon->width >= w) ? w : icon->width;
outh = (icon->height >= h) ? h : icon->height;
icon->flags &= (~ICON_FLAG_REDRAW_PENDING);
if (icon->drawingWin && icon->docked) {
if (icon->flags & ICON_FLAG_ARGB32) {
/* ARGB32 redraw: never use a ParentRelative method, and
no need to clear window except FIXME when its size changed.
Draw on the offscreen pixmap instead, then copy to the window.
*/
if (icon->offscreenPixmap == None) {
icon->offscreenPixmap = Tk_GetPixmap(Tk_Display(icon->drawingWin),
Tk_WindowId(icon->drawingWin), w, h, 32);
}
if (!icon->photo) {
icon->photo = Tk_FindPhoto(icon->interp, Tcl_GetString(icon->imageObj));
}
if (!icon->photo && !icon->imageVisualInstance) {
Tcl_InterpState saved
= Tcl_SaveInterpState(icon->interp, TCL_OK);
icon->imageVisualInstance = Tk_GetImage(icon->interp,icon->drawingWin,
Tcl_GetString(icon->imageObj), IgnoreImageChange, NULL);
Tcl_RestoreInterpState(icon->interp,saved);
}
if (icon->photo && !icon->offscreenImage) {
icon->offscreenImage = XGetImage(Tk_Display(icon->drawingWin),
icon->offscreenPixmap, 0, 0, w, h, AllPlanes, ZPixmap);
}
if (icon->offscreenGC == NULL) {
XGCValues gcv;
gcv.function = GXcopy;
gcv.plane_mask = AllPlanes;
gcv.foreground = 0;
gcv.background = 0;
icon->offscreenGC = Tk_GetGC(icon->drawingWin,
GCFunction|GCPlaneMask|GCForeground|GCBackground, &gcv);
}
if (icon->flags & ICON_FLAG_DIRTY_EDGES) {
XClearWindow(Tk_Display(icon->drawingWin), TKU_XID(icon->drawingWin));
icon->flags &= ~ICON_FLAG_DIRTY_EDGES;
}
if (icon->photo) {
Tk_PhotoImageBlock pib;
int cx,cy;
XImage *xim = icon->offscreenImage;
/* redraw photo using raw data */
Tk_PhotoGetImage(icon->photo,&pib);
for (cy = 0; cy < h; ++cy) {
for (cx = 0; cx < w; ++cx) {
XPutPixel(xim,cx,cy,
(*(pib.pixelPtr +
pib.pixelSize*cx +
pib.pitch*cy +
pib.offset[0])<<16) |
(*(pib.pixelPtr +
pib.pixelSize*cx +
pib.pitch*cy +
pib.offset[1])<<8) |
(*(pib.pixelPtr +
pib.pixelSize*cx +
pib.pitch*cy +
pib.offset[2])) |
(pib.offset[3] ?
(*(pib.pixelPtr +
pib.pixelSize*cx +
pib.pitch*cy +
pib.offset[3])<<24) : 0));
}
}
XPutImage(Tk_Display(icon->drawingWin),
icon->offscreenPixmap,
icon->offscreenGC,
icon->offscreenImage,
0,0,0,0,w,h);
} else {
XFillRectangle(Tk_Display(icon->drawingWin),
icon->offscreenPixmap,
icon->offscreenGC,
0,0,w,h);
if (icon->imageVisualInstance) {
Tk_RedrawImage(icon->imageVisualInstance,
0,0,w,h,
icon->offscreenPixmap,
0,0);
}
}
XCopyArea(Tk_Display(icon->drawingWin),
icon->offscreenPixmap,
TKU_XID(icon->drawingWin),
icon->offscreenGC,
imgx,imgy,outw,outh,outx,outy);
} else {
/* Non-argb redraw: clear window and draw an image over it.
For photos it gives a correct alpha blending with a parent
window background, even if it's a fancy pixmap (proved to
work with lxpanel fancy backgrounds).
*/
XClearWindow(Tk_Display(icon->drawingWin),
TKU_XID(icon->drawingWin));
if (icon->image && icon->visible) {
Tk_RedrawImage(icon->image,imgx,imgy,outw,outh,
TKU_XID(icon->drawingWin), outx, outy);
}
}
}
}
/*
*----------------------------------------------------------------------
*