-
Notifications
You must be signed in to change notification settings - Fork 57
/
swell-generic-gdk.cpp
3431 lines (3050 loc) · 97.3 KB
/
swell-generic-gdk.cpp
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
/* Cockos SWELL (Simple/Small Win32 Emulation Layer for Linux/OSX)
Copyright (C) 2006 and later, Cockos, Inc.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SWELL_PROVIDED_BY_APP
#include "swell.h"
//#define SWELL_GDK_IMPROVE_WINDOWRECT // does not work yet (gdk_window_get_frame_extents() does not seem to be sufficiently reliable)
#ifdef SWELL_PRELOAD
#define STR(x) #x
#define STR2(x) STR(x)
extern "C" {
char __attribute__ ((visibility ("default"))) SWELL_WANT_LOAD_LIBRARY[] = STR2(SWELL_PRELOAD);
};
#undef STR
#undef STR2
#endif
#ifdef SWELL_TARGET_GDK
#include "swell-internal.h"
#include "swell-dlggen.h"
#include "../wdlcstring.h"
#include "../wdlutf8.h"
#if !defined(SWELL_TARGET_GDK_NO_CURSOR_HACK)
#define SWELL_TARGET_GDK_CURSORHACK
#endif
#include <X11/extensions/XInput2.h>
#include <X11/Xatom.h>
#include <GL/gl.h>
#include <GL/glx.h>
static void (*_gdk_drag_drop_done)(GdkDragContext *, gboolean); // may not always be available
static guint32 _gdk_x11_window_get_desktop(GdkWindow *window)
{
Atom type;
gint format;
gulong nitems=0, bytes_after;
guchar *data;
if (!window || !gdk_x11_screen_supports_net_wm_hint(gdk_window_get_screen(window),
gdk_atom_intern_static_string("_NET_WM_DESKTOP")))
return 0;
XGetWindowProperty(GDK_WINDOW_XDISPLAY(window), GDK_WINDOW_XID(window),
gdk_x11_get_xatom_by_name_for_display(gdk_window_get_display(window), "_NET_WM_DESKTOP"),
0, G_MAXLONG, false, XA_CARDINAL, &type, &format, &nitems, &bytes_after, &data);
if (type != XA_CARDINAL || nitems<1) return 0;
nitems = *(gulong *)data;
XFree(data);
return (guint32) nitems;
}
static void _gdk_x11_window_move_to_desktop(GdkWindow *window, guint32 desktop)
{
XClientMessageEvent xclient;
if (!window || !gdk_x11_screen_supports_net_wm_hint(gdk_window_get_screen(window),
gdk_atom_intern_static_string("_NET_WM_DESKTOP")))
return;
memset (&xclient, 0, sizeof (xclient));
xclient.type = ClientMessage;
xclient.send_event = true;
xclient.window = GDK_WINDOW_XID(window);
xclient.message_type = gdk_x11_get_xatom_by_name_for_display(gdk_window_get_display(window), "_NET_WM_DESKTOP");
xclient.format = 32;
xclient.data.l[0] = desktop;
xclient.data.l[1] = 1;
XSendEvent(GDK_WINDOW_XDISPLAY(window), gdk_x11_get_default_root_xwindow(), false,
SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xclient);
}
// for m_oswindow_private
#define PRIVATE_NEEDSHOW 1
#ifndef SWELL_WINDOWSKEY_GDK_MASK
#define SWELL_WINDOWSKEY_GDK_MASK GDK_MOD4_MASK
#endif
static int SWELL_gdk_active;
static GdkEvent *s_cur_evt;
static GList *s_program_icon_list;
static SWELL_OSWINDOW swell_dragsrc_osw;
static DWORD swell_dragsrc_timeout_start;
static HWND swell_dragsrc_hwnd;
static DWORD swell_lastMessagePos;
static const char *swell_dragsrc_fn;
static int gdk_options;
#define OPTION_KEEP_OWNED_ABOVE 1
#define OPTION_OWNED_TASKLIST 2
#define OPTION_BORDERLESS_OVERRIDEREDIRECT 4
#define OPTION_BORDERLESS_DIALOG 8
#define OPTION_ALLOW_MAYBE_INACTIVE 16
#define OPTION_FULLSCREEN_FOR_OWNER_WINDOWS 32
#define OPTION_FULLSCREEN_DYNAMIC 64
static HWND s_ddrop_hwnd;
static POINT s_ddrop_pt;
static SWELL_CursorResourceIndex *SWELL_curmodule_cursorresource_head;
static int s_cursor_vis_cnt;
static HCURSOR s_last_cursor;
static HCURSOR s_last_setcursor;
static SWELL_OSWINDOW s_last_setcursor_oswnd;
static void *g_swell_touchptr; // last GDK touch sequence
static void *g_swell_touchptr_wnd; // last window of touch sequence, for forcing end of sequence on destroy
static bool g_swell_mouse_relmode;
static int g_swell_mouse_relmode_curpos_x;
static int g_swell_mouse_relmode_curpos_y;
static HANDLE s_clipboard_getstate, s_clipboard_setstate;
static GdkAtom s_clipboard_getstate_fmt, s_clipboard_setstate_fmt;
static WDL_IntKeyedArray<HANDLE> m_clip_recs(GlobalFree);
static WDL_PtrList<char> m_clip_curfmts;
static HWND s_clip_hwnd;
static void swell_gdkEventHandler(GdkEvent *event, gpointer data);
static int s_last_desktop;
static UINT_PTR s_deactivate_timer;
static guint32 s_force_window_time;
static bool swell_app_is_inactive;
int swell_is_app_inactive()
{
return swell_app_is_inactive ? 1 : (gdk_options&OPTION_ALLOW_MAYBE_INACTIVE) && s_deactivate_timer!=0 ? -1 : 0;
}
static void update_menubar_activations()
{
if (g_swell_ctheme.menubar_bg == g_swell_ctheme.menubar_bg_inactive &&
g_swell_ctheme.menubar_text == g_swell_ctheme.menubar_text_inactive) return;
HWND h = SWELL_topwindows;
while (h)
{
if (h->m_oswindow && h->m_menu)
{
DrawMenuBar(h);
}
h=h->m_next;
}
}
static void on_activate(guint32 ftime)
{
s_force_window_time = ftime;
swell_app_is_inactive=false;
HWND h = SWELL_topwindows;
while (h)
{
if (h->m_oswindow)
{
if (h->m_israised)
gdk_window_set_keep_above(h->m_oswindow,TRUE);
if (!h->m_enabled)
gdk_window_set_accept_focus(h->m_oswindow,FALSE);
}
PostMessage(h,WM_ACTIVATEAPP,1,0);
h=h->m_next;
}
s_last_desktop=0;
s_force_window_time = 0;
update_menubar_activations();
}
void swell_gdk_reactivate_app(void)
{
if (swell_app_is_inactive)
{
SWELL_focused_oswindow=NULL;
on_activate(GDK_CURRENT_TIME);
}
}
static void on_deactivate()
{
swell_app_is_inactive=true;
HWND lf = swell_oswindow_to_hwnd(SWELL_focused_oswindow);
s_last_desktop = lf && lf->m_oswindow ? _gdk_x11_window_get_desktop(lf->m_oswindow)+1 : 0;
HWND h = SWELL_topwindows;
while (h)
{
if (h->m_oswindow)
{
if (h->m_israised)
gdk_window_set_keep_above(h->m_oswindow,FALSE);
if (!h->m_enabled)
gdk_window_set_accept_focus(h->m_oswindow,TRUE); // allow the user to activate app by clicking
}
PostMessage(h,WM_ACTIVATEAPP,0,0);
h=h->m_next;
}
swell_on_toplevel_raise(NULL);
DestroyPopupMenus();
}
void swell_oswindow_destroy(HWND hwnd)
{
if (hwnd && hwnd->m_oswindow)
{
if (SWELL_focused_oswindow == hwnd->m_oswindow) SWELL_focused_oswindow = NULL;
if (g_swell_touchptr && g_swell_touchptr_wnd == hwnd->m_oswindow)
g_swell_touchptr = NULL;
gdk_window_destroy(hwnd->m_oswindow);
hwnd->m_oswindow=NULL;
#ifdef SWELL_LICE_GDI
delete hwnd->m_backingstore;
hwnd->m_backingstore=0;
#endif
if (swell_app_is_inactive)
{
HWND h = SWELL_topwindows;
while (h)
{
if (h->m_oswindow) break;
h = h->m_next;
}
if (!h) on_activate(10); // arbitrary old timestamp that is nonzero
}
}
}
void swell_oswindow_update_text(HWND hwnd)
{
if (hwnd && hwnd->m_oswindow)
{
gdk_window_set_title(hwnd->m_oswindow, (char*)hwnd->m_title.Get());
}
}
void swell_oswindow_focus(HWND hwnd)
{
if (!hwnd)
{
SWELL_focused_oswindow = NULL;
update_menubar_activations();
return;
}
while (hwnd && !hwnd->m_oswindow) hwnd=hwnd->m_parent;
if (hwnd && !swell_app_is_inactive)
{
gdk_window_raise(hwnd->m_oswindow);
if (hwnd->m_oswindow != SWELL_focused_oswindow)
{
SWELL_focused_oswindow = hwnd->m_oswindow;
gdk_window_focus(hwnd->m_oswindow,GDK_CURRENT_TIME);
update_menubar_activations();
}
}
}
void swell_recalcMinMaxInfo(HWND hwnd)
{
if (!hwnd || !hwnd->m_oswindow || !(hwnd->m_style & WS_CAPTION)) return;
MINMAXINFO mmi;
memset(&mmi,0,sizeof(mmi));
if (hwnd->m_style & WS_THICKFRAME)
{
mmi.ptMinTrackSize.x = 20;
mmi.ptMaxSize.x = mmi.ptMaxTrackSize.x = 16384;
mmi.ptMinTrackSize.y = 20;
mmi.ptMaxSize.y = mmi.ptMaxTrackSize.y = 16384;
SendMessage(hwnd,WM_GETMINMAXINFO,0,(LPARAM)&mmi);
}
else
{
RECT r=hwnd->m_position;
mmi.ptMinTrackSize.x = mmi.ptMaxSize.x = mmi.ptMaxTrackSize.x = r.right-r.left;
mmi.ptMinTrackSize.y = mmi.ptMaxSize.y = mmi.ptMaxTrackSize.y = r.bottom-r.top;
}
GdkGeometry h;
memset(&h,0,sizeof(h));
h.max_width= mmi.ptMaxSize.x;
h.max_height= mmi.ptMaxSize.y;
h.min_width= mmi.ptMinTrackSize.x;
h.min_height= mmi.ptMinTrackSize.y;
gdk_window_set_geometry_hints(hwnd->m_oswindow,&h,(GdkWindowHints) ((hwnd->m_has_had_position ? GDK_HINT_POS : 0) | GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE));
}
void SWELL_initargs(int *argc, char ***argv)
{
if (!SWELL_gdk_active)
{
XInitThreads();
#if SWELL_TARGET_GDK == 3
void (*_gdk_set_allowed_backends)(const char *);
*(void **)&_gdk_drag_drop_done = dlsym(RTLD_DEFAULT,"gdk_drag_drop_done");
*(void **)&_gdk_set_allowed_backends = dlsym(RTLD_DEFAULT,"gdk_set_allowed_backends");
if (_gdk_set_allowed_backends)
_gdk_set_allowed_backends("x11");
#endif
#ifdef SWELL_SUPPORT_GTK
SWELL_gdk_active = gtk_init_check(argc,argv) ? 1 : -1;
#else
SWELL_gdk_active = gdk_init_check(argc,argv) ? 1 : -1;
#endif
if (SWELL_gdk_active > 0)
{
char buf[1024];
GetModuleFileName(NULL,buf,sizeof(buf));
WDL_remove_filepart(buf);
lstrcatn(buf,"/Resources/main.png",sizeof(buf));
GdkPixbuf *pb = gdk_pixbuf_new_from_file(buf,NULL);
if (!pb)
{
strcpy(buf+strlen(buf)-3,"ico");
pb = gdk_pixbuf_new_from_file(buf,NULL);
}
if (pb) s_program_icon_list = g_list_append(s_program_icon_list,pb);
gdk_event_handler_set(swell_gdkEventHandler,NULL,NULL);
}
}
}
static bool swell_initwindowsys()
{
if (!SWELL_gdk_active)
{
// maybe make the main app call this with real parms
int argc=1;
char buf[32];
strcpy(buf,"blah");
char *argv[2];
argv[0] = buf;
argv[1] = buf;
char **pargv = argv;
SWELL_initargs(&argc,&pargv);
}
return SWELL_gdk_active>0;
}
#ifdef SWELL_LICE_GDI
class LICE_CairoBitmap : public LICE_IBitmap
{
public:
LICE_CairoBitmap()
{
m_fb = NULL;
m_allocsize = m_width = m_height = m_span = 0;
m_surf = NULL;
}
virtual ~LICE_CairoBitmap()
{
if (m_surf) cairo_surface_destroy(m_surf);
free(m_fb);
}
// LICE_IBitmap interface
virtual LICE_pixel *getBits()
{
const UINT_PTR extra=LICE_MEMBITMAP_ALIGNAMT;
return (LICE_pixel *) (((UINT_PTR)m_fb + extra)&~extra);
}
virtual int getWidth() { return m_width; }
virtual int getHeight() { return m_height; }
virtual int getRowSpan() { return m_span; }
virtual bool resize(int w, int h)
{
if (w<0) w=0;
if (h<0) h=0;
if (w == m_width && h == m_height) return false;
if (m_surf) cairo_surface_destroy(m_surf);
m_surf = NULL;
m_span = w ? cairo_format_stride_for_width(CAIRO_FORMAT_RGB24,w)/4 : 0;
const int sz = h * m_span * 4 + LICE_MEMBITMAP_ALIGNAMT;
if (!m_fb || m_allocsize < sz || sz < m_allocsize/4)
{
const int newalloc = m_allocsize<sz ? (sz*3)/2 : sz;
void *p = realloc(m_fb,newalloc);
if (!p) return false;
m_fb = (LICE_pixel *)p;
m_allocsize = newalloc;
}
m_width=w && h ? w :0;
m_height=w && h ? h : 0;
return true;
}
virtual INT_PTR Extended(int id, void* data)
{
if (id == 0xca140)
{
if (data)
{
// in case we want to release surface
return 0;
}
if (!m_surf)
m_surf = cairo_image_surface_create_for_data((guchar*)getBits(), CAIRO_FORMAT_RGB24,
getWidth(),getHeight(), getRowSpan()*4);
return (INT_PTR)m_surf;
}
return 0;
}
private:
LICE_pixel *m_fb;
int m_width, m_height, m_span;
int m_allocsize;
cairo_surface_t *m_surf;
};
#endif
static int swell_gdk_option(const char *name, const char *defstr, int defv)
{
char buf[64];
GetPrivateProfileString(".swell",name,"",buf,sizeof(buf),"");
if (!buf[0]) WritePrivateProfileString(".swell",name,defstr,"");
if (buf[0] >= '0' && buf[0] <= '9') return atoi(buf);
return defv;
}
static void init_options()
{
if (!gdk_options)
{
gdk_options = 0x40000000;
if (swell_gdk_option("gdk_owned_windows_keep_above", "auto (default is 1)",1))
gdk_options|=OPTION_KEEP_OWNED_ABOVE;
if (swell_gdk_option("gdk_owned_windows_in_tasklist", "auto (default is 0)",0))
gdk_options|=OPTION_OWNED_TASKLIST;
switch (swell_gdk_option("gdk_instant_menubar_inactivation", "auto (default is 1 if on Wayland, otherwise 0)",-1))
{
case -1:
if (getenv("WAYLAND_DISPLAY") == NULL) break;
// fall through
case 1:
gdk_options|=OPTION_ALLOW_MAYBE_INACTIVE;
break;
}
switch (swell_gdk_option("gdk_borderless_window_mode", "auto (default is 1=dialog hint. 2=override redirect. 0=normal hint)", 1))
{
case 1: gdk_options|=OPTION_BORDERLESS_DIALOG; break;
case 2: gdk_options|=OPTION_BORDERLESS_OVERRIDEREDIRECT; break;
default: break;
}
const char *wmname = gdk_x11_screen_get_window_manager_name(gdk_screen_get_default());
switch (swell_gdk_option("gdk_fullscreen_for_owner_windows", "auto (default is 1 on kwin, otherwise 0)",-1))
{
case -1:
if (!wmname || strnicmp(wmname,"KWin",4)) break;
// fall through
case 1:
gdk_options |= OPTION_FULLSCREEN_FOR_OWNER_WINDOWS;
break;
}
switch (swell_gdk_option("gdk_fullscreen_dynamic","auto (default is 1 on kwin, otherwise 0)",-1))
{
case -1:
if (!wmname || strnicmp(wmname,"KWin",4)) break;
// fall through
case 1:
gdk_options |= OPTION_FULLSCREEN_DYNAMIC;
break;
}
}
}
static void swell_hide_owned_windows_transient(HWND hwnd)
{
if ((gdk_options&OPTION_KEEP_OWNED_ABOVE) && hwnd->m_owned_list)
{
HWND l = SWELL_topwindows;
while (l)
{
if (l->m_oswindow && l->m_owner == hwnd && l->m_visible)
gdk_window_hide(l->m_oswindow);
l = l->m_next;
}
}
}
static void swell_set_owned_windows_transient(HWND hwnd, bool do_create)
{
if ((gdk_options&OPTION_KEEP_OWNED_ABOVE) && hwnd->m_owned_list)
{
WDL_PtrList<void> raiselist;
HWND l = SWELL_topwindows;
while (l)
{
if (l->m_owner == hwnd && l->m_visible)
{
if (l->m_oswindow) raiselist.Add(l->m_oswindow);
else if (do_create) swell_oswindow_manage(l,false);
}
l = l->m_next;
}
for (int x = raiselist.GetSize()-1; x>=0; x--)
{
SWELL_OSWINDOW r = (SWELL_OSWINDOW)raiselist.Get(x);
gdk_window_set_transient_for(r,hwnd->m_oswindow);
gdk_window_show_unraised(r);
}
}
}
bool IsModalDialogBox(HWND);
void swell_oswindow_manage(HWND hwnd, bool wantfocus)
{
if (!hwnd) return;
bool isVis = hwnd->m_oswindow != NULL;
bool wantVis = !hwnd->m_parent && hwnd->m_visible;
if (isVis != wantVis)
{
if (!wantVis)
{
RECT r;
GetWindowRect(hwnd,&r);
swell_hide_owned_windows_transient(hwnd);
swell_oswindow_destroy(hwnd);
hwnd->m_position = r;
}
else
{
if (swell_initwindowsys())
{
init_options();
SWELL_OSWINDOW transient_for=NULL;
if (hwnd->m_owner && (gdk_options&OPTION_KEEP_OWNED_ABOVE))
{
HWND own = hwnd->m_owner;
while (own->m_parent && !own->m_oswindow) own=own->m_parent;
if (!own->m_oswindow)
{
if (!IsModalDialogBox(hwnd)) return; // defer
// if a modal window, parent to any owner up the chain
while (own->m_owner && !own->m_oswindow)
{
own = own->m_owner;
while (own->m_parent && !own->m_oswindow) own=own->m_parent;
}
}
transient_for = own->m_oswindow;
}
RECT r = hwnd->m_position;
GdkWindowAttr attr={0,};
attr.title = (char *)hwnd->m_title.Get();
attr.event_mask = GDK_ALL_EVENTS_MASK|GDK_EXPOSURE_MASK;
attr.x = r.left;
attr.y = r.top;
attr.width = r.right-r.left;
attr.height = r.bottom-r.top;
attr.wclass = GDK_INPUT_OUTPUT;
const char *appname = g_swell_appname;
attr.wmclass_name = (gchar*)appname;
attr.wmclass_class = (gchar*)appname;
attr.window_type = GDK_WINDOW_TOPLEVEL;
if (GetProp(hwnd,"SWELLGdkAlphaChannel"))
attr.visual = gdk_screen_get_rgba_visual(gdk_screen_get_default());
hwnd->m_oswindow = gdk_window_new(NULL,&attr,GDK_WA_X|GDK_WA_Y|(appname?GDK_WA_WMCLASS:0)|(attr.visual ? GDK_WA_VISUAL : 0));
if (hwnd->m_oswindow)
{
bool override_redirect=false;
const bool modal = DialogBoxIsActive() == hwnd;
if (!(hwnd->m_style & WS_CAPTION))
{
if (hwnd->m_style != WS_CHILD && !(gdk_options&OPTION_BORDERLESS_OVERRIDEREDIRECT))
{
if (transient_for)
gdk_window_set_transient_for(hwnd->m_oswindow,transient_for);
gdk_window_set_type_hint(hwnd->m_oswindow, (gdk_options&OPTION_BORDERLESS_DIALOG) ? GDK_WINDOW_TYPE_HINT_DIALOG : GDK_WINDOW_TYPE_HINT_NORMAL);
gdk_window_set_decorations(hwnd->m_oswindow,(GdkWMDecoration) 0);
}
else
{
gdk_window_set_override_redirect(hwnd->m_oswindow,true);
override_redirect=true;
}
if (!SWELL_topwindows ||
(SWELL_topwindows==hwnd && !hwnd->m_next)) wantfocus=true;
}
else
{
GdkWindowTypeHint type_hint = GDK_WINDOW_TYPE_HINT_NORMAL;
GdkWMDecoration decor = (GdkWMDecoration) (GDK_DECOR_ALL | GDK_DECOR_MENU);
if (!(hwnd->m_style&WS_THICKFRAME))
decor = (GdkWMDecoration) (GDK_DECOR_BORDER|GDK_DECOR_TITLE|GDK_DECOR_MINIMIZE);
if (transient_for)
{
gdk_window_set_transient_for(hwnd->m_oswindow,transient_for);
if (modal)
gdk_window_set_modal_hint(hwnd->m_oswindow,true);
}
if (modal) type_hint = GDK_WINDOW_TYPE_HINT_DIALOG;
gdk_window_set_type_hint(hwnd->m_oswindow,type_hint);
gdk_window_set_decorations(hwnd->m_oswindow,decor);
}
if (s_force_window_time)
gdk_x11_window_set_user_time(hwnd->m_oswindow,s_force_window_time);
if (!wantfocus || swell_app_is_inactive)
gdk_window_set_focus_on_map(hwnd->m_oswindow,false);
#ifdef SWELL_LICE_GDI
if (!hwnd->m_backingstore) hwnd->m_backingstore = new LICE_CairoBitmap;
#endif
if (!override_redirect)
{
if (s_program_icon_list)
gdk_window_set_icon_list(hwnd->m_oswindow,s_program_icon_list);
}
if (hwnd->m_owner && !(gdk_options&OPTION_OWNED_TASKLIST) && !override_redirect)
{
gdk_window_set_skip_taskbar_hint(hwnd->m_oswindow,true);
}
else if (hwnd->m_style == WS_CHILD)
{
// hack: parentless visible window with WS_CHILD set will
// not appear in taskbar
gdk_window_set_skip_taskbar_hint(hwnd->m_oswindow,true);
}
if (hwnd->m_israised && !swell_app_is_inactive)
gdk_window_set_keep_above(hwnd->m_oswindow,TRUE);
gdk_window_register_dnd(hwnd->m_oswindow);
if (hwnd->m_oswindow_fullscreen)
gdk_window_fullscreen(hwnd->m_oswindow);
if (!swell_app_is_inactive && !s_force_window_time)
gdk_window_show(hwnd->m_oswindow);
else
gdk_window_show_unraised(hwnd->m_oswindow);
if (s_last_desktop>0)
_gdk_x11_window_move_to_desktop(hwnd->m_oswindow,s_last_desktop-1);
if (!hwnd->m_oswindow_fullscreen)
{
swell_oswindow_resize(hwnd->m_oswindow,hwnd->m_has_had_position?3:2,r);
swell_oswindow_postresize(hwnd,r);
}
swell_set_owned_windows_transient(hwnd, true);
}
}
}
}
if (wantVis) swell_oswindow_update_text(hwnd);
}
void swell_oswindow_maximize(HWND hwnd, bool wantmax) // false=restore
{
if (WDL_NORMALLY(hwnd && hwnd->m_oswindow))
{
if (wantmax)
gdk_window_maximize(hwnd->m_oswindow);
else
gdk_window_unmaximize(hwnd->m_oswindow);
}
}
void swell_oswindow_updatetoscreen(HWND hwnd, RECT *rect)
{
#ifdef SWELL_LICE_GDI
if (hwnd && hwnd->m_backingstore && hwnd->m_oswindow)
{
LICE_IBitmap *bm = hwnd->m_backingstore;
LICE_SubBitmap tmpbm(bm,rect->left,rect->top,rect->right-rect->left,rect->bottom-rect->top);
GdkRectangle rrr={rect->left,rect->top,rect->right-rect->left,rect->bottom-rect->top};
gdk_window_begin_paint_rect(hwnd->m_oswindow, &rrr);
cairo_t * crc = gdk_cairo_create (hwnd->m_oswindow);
cairo_surface_t *temp_surface = (cairo_surface_t*)bm->Extended(0xca140,NULL);
if (temp_surface) cairo_set_source_surface(crc, temp_surface, 0,0);
cairo_paint(crc);
cairo_destroy(crc);
gdk_window_end_paint(hwnd->m_oswindow);
if (temp_surface) bm->Extended(0xca140,temp_surface); // release
}
#endif
}
#if SWELL_TARGET_GDK == 2
#define DEF_GKY(x) GDK_##x
#else
#define DEF_GKY(x) GDK_KEY_##x
#endif
static guint swell_gdkConvertKey(guint key, bool *extended)
{
switch(key)
{
#define DEF_GK2(h, v) \
case DEF_GKY(h): *extended = true; return (v); \
case DEF_GKY(KP_##h): return (v);
DEF_GK2(Home,VK_HOME)
DEF_GK2(End,VK_END)
DEF_GK2(Up, VK_UP)
DEF_GK2(Down, VK_DOWN)
DEF_GK2(Left, VK_LEFT)
DEF_GK2(Right, VK_RIGHT)
DEF_GK2(Page_Up, VK_PRIOR)
DEF_GK2(Page_Down, VK_NEXT)
DEF_GK2(Insert,VK_INSERT)
DEF_GK2(Delete, VK_DELETE)
#undef DEF_GK2
case DEF_GKY(KP_Enter): *extended = true; return VK_RETURN;
case DEF_GKY(Return): return VK_RETURN;
case DEF_GKY(Escape): return VK_ESCAPE;
case DEF_GKY(BackSpace): return VK_BACK;
case DEF_GKY(ISO_Left_Tab):
case DEF_GKY(Tab): return VK_TAB;
case DEF_GKY(F1): return VK_F1;
case DEF_GKY(F2): return VK_F2;
case DEF_GKY(F3): return VK_F3;
case DEF_GKY(F4): return VK_F4;
case DEF_GKY(F5): return VK_F5;
case DEF_GKY(F6): return VK_F6;
case DEF_GKY(F7): return VK_F7;
case DEF_GKY(F8): return VK_F8;
case DEF_GKY(F9): return VK_F9;
case DEF_GKY(F10): return VK_F10;
case DEF_GKY(F11): return VK_F11;
case DEF_GKY(F12): return VK_F12;
case DEF_GKY(KP_0): return VK_NUMPAD0;
case DEF_GKY(KP_1): return VK_NUMPAD1;
case DEF_GKY(KP_2): return VK_NUMPAD2;
case DEF_GKY(KP_3): return VK_NUMPAD3;
case DEF_GKY(KP_4): return VK_NUMPAD4;
case DEF_GKY(KP_5): return VK_NUMPAD5;
case DEF_GKY(KP_6): return VK_NUMPAD6;
case DEF_GKY(KP_7): return VK_NUMPAD7;
case DEF_GKY(KP_8): return VK_NUMPAD8;
case DEF_GKY(KP_9): return VK_NUMPAD9;
case DEF_GKY(KP_Multiply): return VK_MULTIPLY;
case DEF_GKY(KP_Add): return VK_ADD;
case DEF_GKY(KP_Separator): return VK_SEPARATOR;
case DEF_GKY(KP_Subtract): return VK_SUBTRACT;
case DEF_GKY(KP_Decimal): return VK_DECIMAL;
case DEF_GKY(KP_Divide): return VK_DIVIDE;
case DEF_GKY(Num_Lock): return VK_NUMLOCK;
case DEF_GKY(KP_Begin): return VK_SELECT;
}
return 0;
}
LRESULT SWELL_SendMouseMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static int hex_parse(char c)
{
if (c >= '0' && c <= '9') return c-'0';
if (c >= 'A' && c <= 'F') return 10+c-'A';
if (c >= 'a' && c <= 'f') return 10+c-'a';
return -1;
}
static GdkAtom utf8atom()
{
static GdkAtom tmp;
if (!tmp) tmp = gdk_atom_intern_static_string("UTF8_STRING");
return tmp;
}
static GdkAtom tgtatom()
{
static GdkAtom tmp;
if (!tmp) tmp = gdk_atom_intern_static_string("TARGETS");
return tmp;
}
static GdkAtom urilistatom()
{
static GdkAtom tmp;
if (!tmp) tmp = gdk_atom_intern_static_string("text/uri-list");
return tmp;
}
static void OnSelectionRequestEvent(GdkEventSelection *b)
{
//printf("got sel req %s\n",gdk_atom_name(b->target));
GdkAtom prop=GDK_NONE;
if (swell_dragsrc_osw && b->window == swell_dragsrc_osw)
{
if (swell_dragsrc_hwnd)
{
if (b->target == tgtatom())
{
prop = b->property;
GdkAtom list[] = { urilistatom() };
#if SWELL_TARGET_GDK == 2
GdkWindow *pw = gdk_window_lookup(b->requestor);
if (!pw) pw = gdk_window_foreign_new(b->requestor);
#else
GdkWindow *pw = b->requestor;
#endif
if (pw)
gdk_property_change(pw,prop,GDK_SELECTION_TYPE_ATOM,32, GDK_PROP_MODE_REPLACE,(guchar*)list,(int) (sizeof(list)/sizeof(list[0])));
}
SendMessage(swell_dragsrc_hwnd,WM_USER+100,(WPARAM)b,(LPARAM)&prop);
}
}
else if (s_clipboard_setstate)
{
if (b->target == tgtatom())
{
if (s_clipboard_setstate_fmt)
{
prop = b->property;
GdkAtom list[] = { s_clipboard_setstate_fmt };
#if SWELL_TARGET_GDK == 2
GdkWindow *pw = gdk_window_lookup(b->requestor);
if (!pw) pw = gdk_window_foreign_new(b->requestor);
#else
GdkWindow *pw = b->requestor;
#endif
if (pw)
gdk_property_change(pw,prop,GDK_SELECTION_TYPE_ATOM,32, GDK_PROP_MODE_REPLACE,(guchar*)list,(int) (sizeof(list)/sizeof(list[0])));
}
}
else
{
if (b->target == s_clipboard_setstate_fmt ||
(b->target == GDK_TARGET_STRING && s_clipboard_setstate_fmt == utf8atom())
)
{
prop = b->property;
int len = GlobalSize(s_clipboard_setstate);
guchar *ptr = (guchar*)s_clipboard_setstate;
WDL_FastString str;
if (s_clipboard_setstate_fmt == utf8atom())
{
const char *rd = (const char *)s_clipboard_setstate;
while (*rd)
{
if (!strncmp(rd,"\r\n",2))
{
str.Append("\n");
rd+=2;
}
else
str.Append(rd++,1);
}
ptr = (guchar *)str.Get();
len = str.GetLength();
}
else if (s_clipboard_setstate_fmt == urilistatom())
{
if (len > (int)sizeof(DROPFILES))
{
DROPFILES *hdr = (DROPFILES *)ptr;
if (WDL_NORMALLY(hdr->pFiles < (DWORD)len) &&
WDL_NORMALLY(!hdr->fWide) // todo deal with UTF-16
)
{
const char *rd = (const char *)ptr;
DWORD rdo = hdr->pFiles;
while (rdo < (DWORD)len && rd[rdo])
{
const char *fn = rd + rdo;
rdo += strlen(rd+rdo)+1;
str.Append("file://");
while (*fn)
{
if (isalnum_safe(*fn) || *fn == '.' || *fn == '_' || *fn == '-' || *fn == '/' || *fn == '#')
str.Append(fn,1);
else
str.AppendFormatted(8,"%%%02x",*(unsigned char *)fn);
fn++;
}
str.Append("\r\n");
}
}
}
ptr = (guchar *)str.Get();
len = str.GetLength();
}
#if SWELL_TARGET_GDK == 2
GdkWindow *pw = gdk_window_lookup(b->requestor);
if (!pw) pw = gdk_window_foreign_new(b->requestor);
#else
GdkWindow *pw = b->requestor;
#endif
if (pw)
gdk_property_change(pw,prop,b->target,8, GDK_PROP_MODE_REPLACE,ptr,len);
}
}
}
gdk_selection_send_notify(b->requestor,b->selection,b->target,prop,GDK_CURRENT_TIME);
}
static void OnExposeEvent(GdkEventExpose *exp)
{
HWND hwnd = swell_oswindow_to_hwnd(exp->window);
if (!hwnd) return;
#ifdef SWELL_LICE_GDI
RECT r,cr;
// don't use GetClientRect(),since we're getting it pre-NCCALCSIZE etc
cr.left=cr.top=0;
cr.right = hwnd->m_position.right - hwnd->m_position.left;
cr.bottom = hwnd->m_position.bottom - hwnd->m_position.top;
r.left = exp->area.x;
r.top=exp->area.y;
r.bottom=r.top+exp->area.height;
r.right=r.left+exp->area.width;
if (!hwnd->m_backingstore) hwnd->m_backingstore = new LICE_CairoBitmap;
bool forceref = hwnd->m_backingstore->resize(cr.right-cr.left,cr.bottom-cr.top);
if (forceref) r = cr;
LICE_SubBitmap tmpbm(hwnd->m_backingstore,r.left,r.top,r.right-r.left,r.bottom-r.top);
if (tmpbm.getWidth()>0 && tmpbm.getHeight()>0)
{
void SWELL_internalLICEpaint(HWND hwnd, LICE_IBitmap *bmout, int bmout_xpos, int bmout_ypos, bool forceref);
SWELL_internalLICEpaint(hwnd, &tmpbm, r.left, r.top, forceref);