-
Notifications
You must be signed in to change notification settings - Fork 5
/
nw_winapi.lua
2465 lines (2055 loc) · 65 KB
/
nw_winapi.lua
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
--native windows - winapi backend.
--Written by Cosmin Apreutesei. Public domain.
local ffi = require'ffi'
local bit = require'bit'
local glue = require'glue'
local box2d = require'box2d'
local bitmap = require'bitmap' --for clipboard
local winapi = require'winapi'
local time = require'time'
require'winapi.spi'
require'winapi.sysinfo'
require'winapi.systemmetrics'
require'winapi.windowclass'
require'winapi.gdi'
require'winapi.bitmap'
require'winapi.dibitmap'
require'winapi.icon'
require'winapi.dpiaware'
require'winapi.devcaps'
require'winapi.monitor'
require'winapi.ddev'
require'winapi.cursor'
require'winapi.keyboard'
require'winapi.rawinput'
require'winapi.mouse'
require'winapi.notifyiconclass'
require'winapi.filedialogs'
require'winapi.clipboard'
require'winapi.shellapi'
require'winapi.dragdrop'
require'winapi.panelclass'
require'winapi.module'
require'winapi.sync'
require'winapi.tooltipclass'
local nw = {name = 'winapi'}
--helpers --------------------------------------------------------------------
local function unpack_rect(rect)
return rect.x, rect.y, rect.w, rect.h
end
local function pack_rect(rect, x, y, w, h)
rect = rect or winapi.RECT()
rect.x1, rect.y1, rect.x2, rect.y2 = x, y, x + w, y + h
return rect
end
--app object -----------------------------------------------------------------
local app = {}
nw.app = app
function app:init(frontend)
self.frontend = frontend
--enable WM_INPUT for keyboard events
local rid = winapi.types.RAWINPUTDEVICE()
rid.dwFlags = 0
rid.usUsagePage = 1 --generic desktop controls
rid.usUsage = 6 --keyboard
winapi.RegisterRawInputDevices(rid, 1, ffi.sizeof(rid))
--enable waking up this app instance by other instances of this app
self:_init_wakeup()
return self
end
--version checks -------------------------------------------------------------
function app:ver(what)
if what == 'windows' then
local vinfo = winapi.RtlGetVersion()
return string.format('%d.%d.%d.%d',
vinfo.dwMajorVersion, vinfo.dwMinorVersion,
vinfo.wServicePackMajor, vinfo.wServicePackMinor)
end
end
--message loop ---------------------------------------------------------------
function app:_repaint_all_at(clock)
local windows = self.frontend._windows
for i = 1, #windows do
local win = windows[i]
if not win:dead() then
win.backend:repaint(clock)
local views = win._views
for i = 1, #views do
local view = views[i]
view.backend:repaint(clock)
end
end
end
end
--manual repainting of all windows based on the `_invalid_clock` flag.
--returns the number of seconds to wait until calling this again.
function app:_repaint_all()
local clock = time.clock()
local frame_duration = 1 / self.frontend:maxfps()
--unthrottled painting.
if frame_duration <= 0 then
self:_repaint_all_at(clock)
return 0
end
--get the time relative to the time of the first frame.
local clock0 = self._first_frame_clock
if not clock0 then
clock0 = clock
self._first_frame_clock = clock0
end
local t = clock - clock0
--skip painting if called too early.
local last_frame = self._last_frame or -1
local frame_pos = t / frame_duration
local behind = frame_pos >= last_frame + 1
local current_frame = math.floor(frame_pos + .5)
if not behind then
local next_frame = current_frame + (current_frame > last_frame and 0 or 1)
return (next_frame - frame_pos) * frame_duration
end
self._last_frame = current_frame
if current_frame > last_frame + 1 then
--TODO: still losing a few frames each second when the event queue
--is empty due to the innacuracy of MsgWaitForMultipleObjectsEx().
--print('lost frames', current_frame - last_frame - 1)
end
self:_repaint_all_at(clock)
--compute wait time until the next frame.
local t = time.clock() - clock0
local frame_pos = t / frame_duration
local next_frame = math.max(current_frame + 1, math.floor(frame_pos + .5))
return (next_frame - frame_pos) * frame_duration
end
function app:poll(timeout)
local repaint_timeout = self:_repaint_all()
timeout = math.min(timeout, repaint_timeout)
return winapi.ProcessNextMessage(timeout)
end
function app:run()
while true do
local timeout = self:_repaint_all()
if timeout > 0 then
timeout = math.max(1/1000, timeout) --prevent spin-lock at < 1ms
end
repeat
local more, exit_code = winapi.ProcessNextMessage(timeout)
if not more and exit_code then
return exit_code
end
timeout = 0
until not more
end
end
function app:stop()
winapi.PostQuitMessage()
end
--timers ---------------------------------------------------------------------
local appwin
function app:runevery(seconds, func)
appwin = appwin or winapi.Window{visible = false}
appwin:settimer(seconds, func)
end
--windows --------------------------------------------------------------------
local window = {}
app.window = window
local Window = winapi.subclass({}, winapi.Window)
local winmap = {} --winapi_window->frontend_window
local last_active_window
function window:new(app, frontend, t)
self = glue.update({app = app, frontend = frontend}, self)
local framed = t.frame == 'normal' or t.frame == 'toolbox'
self._layered = t.transparent
self._bg_brush = t.background_color
and winapi.CreateSolidBrush(t.background_color) or nil
if self._bg_brush then
self._windows_background = true
end
--NOTE: resizeable flag (WS_SIZEBOX) needs the frame flag (WS_DLGFRAME),
--which means we can't have frameless windows that are also resizeable.
self.win = Window{
--state
x = t.x,
y = t.y,
w = t.w,
h = t.h,
min_cw = t.min_cw,
min_ch = t.min_ch,
max_cw = t.max_cw,
max_ch = t.max_ch,
visible = false,
minimized = t.minimized,
maximized = t.maximized,
enabled = t.enabled,
--frame
title = t.title,
border = framed,
frame = framed,
window_edge = framed, --must be off for frameless windows!
layered = self._layered,
tool_window = t.frame == 'toolbox',
owner = t.parent and t.parent.backend.win,
background = self._bg_brush,
--behavior
topmost = t.topmost,
minimizable = t.minimizable,
maximizable = t.maximizable,
closeable = t.closeable,
resizeable = framed and t.resizeable, --must be off for frameless windows!
activable = t.activable,
receive_double_clicks = false, --we do our own double-clicking
own_dc = t.opengl and true or nil,
}
self:_set_region()
--init keyboard state
self.win.__wantallkeys = true --don't let IsDialogMessage() filter out our precious WM_CHARs
self:_reset_keystate()
--start tracking mouse leave
winapi.TrackMouseEvent{hwnd = self.win.hwnd, flags = winapi.TME_LEAVE}
--set window state
self._fullscreen = false
--set back-references
self.win.frontend = frontend
self.win.backend = self
self.win.app = app
self:_init_icon_api()
self:_init_drop_target()
--announce acceptance to drop files into the window.
winapi.DragAcceptFiles(self.win.hwnd, true)
self:_init_opengl(t.opengl)
--register window
winmap[self.win] = self.frontend
--if this is the first window, register it as the last active window
--just in case the user calls app:activate() before this window activates.
if not last_active_window then
last_active_window = self
end
self.win[app._wakeup_wm_name] = function(win)
if last_active_window == self then --receive this on one window only
app.frontend:_backend_wakeup()
end
end
return self
end
function window:_set_region()
local cr = self.frontend:corner_radius()
if cr == 0 then return end
local r = self.win.rect
--yes, it's w+1, h+1 with regions and yes, we need the redraw flag.
self._hrgn = winapi.CreateRoundRectRgn(0, 0, r.w + 1, r.h + 1, cr, cr)
winapi.SetWindowRgn(self.win.hwnd, self._hrgn, true)
end
--closing --------------------------------------------------------------------
function window:forceclose()
self.win._forceclose = true --because win:close() calls on_close().
self.win:close()
end
function Window:on_close()
if not self._forceclose and not self.frontend:_backend_closing() then
return false
end
end
--NOTE: closing a window's owner in the on_destroy() event triggers
--another on_destroy() event on the owned window!
function Window:on_destroy()
if not self.nw_destroying then
self.nw_destroying = true
self.frontend:_backend_closed() --this may trigger on_destroy() again!
end
if not self.nw_destroyed then
self.nw_destroyed = true
self.backend:_free_bitmap()
self.backend:_free_opengl()
self.backend:_free_icon()
self.backend:_free_drop_target()
if self.backend._bg_brush then
winapi.DeleteObject(self.backend._bg_brush)
self.backend._bg_brush = false
end
winmap[self] = nil
--register another random window as the last active window so that
--app:activate() works even before the next window gets activated.
--in any case we want to release the reference to self.
if last_active_window == self then
local _, frontend = next(winmap)
last_active_window = frontend.backend
end
end
end
--activation -----------------------------------------------------------------
function app:activate()
--unlike OSX, in Windows you don't activate an app, you have to activate
--a specific window. Activating this app means activating the last window
--of this app that was active before the app got deactivated.
local win = last_active_window
if win and not win.frontend:dead() then
win.win:setforeground()
end
end
function app:active_window()
--foreground_window returns the active window only if the app is active,
--which is consistent with OSX.
return winmap[winapi.Windows.foreground_window] or nil
end
function app:active()
return self:active_window() and true or false
end
function window:activate()
--for consistency with OSX, if the app is inactive, this function
--doesn't activate the window, instead it marks the window that must
--be activated on the next call to app:activate().
last_active_window = self
self.win:activate() --note: using activate() instead of setforeground()
end
function window:active()
--returns true only if the app is active, consistent with OSX.
return not self._inactive and self.app:active_window() == self.frontend
end
--NOTE: this also triggers when the app is inactive and another window
--was closed, so we need to set last_active_window here.
function Window:on_activate()
self.backend._inactive = nil --no need for this anymore
last_active_window = self.backend --for the next app:activate()
end
--this event is received when the window's titlebar is activated.
--this is more accurate event-wise than on_activate() which also triggers when
--the app is inactive and the window flashes its taskbar button instead of activating.
function Window:on_nc_activate()
self.backend._inactive = nil --no need for this anymore
self.backend.app.frontend:_backend_changed()
self.backend:_reset_keystate()
self.frontend:_backend_changed()
end
--NOTE: GetActiveWindow() and GetForegroundWindow() still point to the window
--that received the event at the time of the event, hence the _inactive flag.
function Window:on_deactivate()
self.backend._inactive = true
self.backend:_reset_keystate()
self.frontend:_backend_changed()
end
function Window:on_deactivate_app() --triggered after on_deactivate().
self.frontend.app:_backend_changed()
end
--single app instance --------------------------------------------------------
function app:id()
return self.frontend.nw.app_id
or winapi.GetModuleFilename():lower():gsub('[\\/%:]', '_')
end
function app:_init_wakeup()
self._wakeup_wm_name = 'WM_'..self:id()
self._wakeup_wm_code = winapi.RegisterWindowMessage(self._wakeup_wm_name)
end
function app:already_running()
local mutex, err = winapi.CreateMutex(nil, false, self:id())
return err == 'already_exists'
end
function app:wakeup_other_instances()
winapi.PostMessage(winapi.HWND_BROADCAST, self._wakeup_wm_code, 0, 0)
end
--state/app visibility -------------------------------------------------------
function app:visible() return true end
function app:hide() end
function app:unhide() end
--state ----------------------------------------------------------------------
function window:visible()
return self.win.visible
end
function window:show()
if self.win.minimized then --NOTE: this assumes that minimize() is synchronous
--show minimized without activating, consistent with Linux and OSX.
--self.win:show() also shows the window in minimized state, but it
--selects the window on the taskbar (it activates it).
self:minimize()
else
self.win:show() --sync call
end
end
function window:hide()
self.win:hide() --sync call
end
function window:minimized()
return self.win.minimized
end
--NOTE: minimize() is not activating the window, consistent with OSX and Linux.
function window:minimize()
if self:fullscreen() then return end --TODO: remove this after fixing OSX
self.win:minimize() --sync call, assumed by show()
end
function window:maximized()
if self._fullscreen then
return self._fs.maximized
elseif self.win.minimized then
return self.win.restore_to_maximized
end
return self.win.maximized
end
function window:maximize()
if self:fullscreen() then return end --TODO: remove this after fixing OSX
self.win:maximize() --sync call, assumed by enter_fullscreen()
end
function window:restore()
self.win:restore() --sync call
self.frontend.app:activate() --because maximized hidden windows don't activate
end
function window:shownormal()
if self:fullscreen() then return end --TODO: remove this after fixing OSX
self.win:shownormal() --sync call
if not self:active() then
--activating because minimize->hide->shownormal doesn't.
self:activate()
self.app:activate()
end
end
function Window:on_pos_change(pos)
if not self.frontend then return end --early resize from setting constraints: ignore
self.frontend:_backend_changed()
end
--state/fullscreen -----------------------------------------------------------
function window:fullscreen()
return self._fullscreen
end
function window:enter_fullscreen()
if self._fullscreen then return end
--save state for restoring
self._fs = {
maximized = self:maximized(), --NOTE: this assumes that maximize() is synchronous
normal_rect = self.win.normal_rect,
frame = self.win.frame,
resizeable = self.win.resizeable,
}
--if it's a layered window, clear it, otherwise the taskbar won't
--dissapear quite immediately when the window will be repainted (WinXP).
self:_clear_layered()
--disable events while we're changing the frame, size and state.
local events = self.frontend:events(false)
--this flickers but without it the taskbar won't dissapear immediately.
self.win:hide()
--remove the frame: this enlarges client_rect to what frame_rect was!
self.win.frame = false
self.win.border = false
self.win.resizeable = false
--set normal rect
local display = self:display() or self.app:active_display()
local dx, dy, dw, dh = display:screen_rect()
self.win.normal_rect = pack_rect(nil, dx, dy, dw, dh)
--restore events, invalidate and show.
self._fullscreen = true
self.frontend:events(events)
--show synchronously to avoid re-entring.
self.win:shownormal()
end
function window:exit_fullscreen()
if not self._fullscreen then return end
--disable events while we're changing the frame and size.
local events = self.frontend:events(false)
--put back the frame and normal rect
self.win.frame = self._fs.frame
self.win.border = self._fs.frame
self.win.resizeable = self._fs.resizeable
self.win.normal_rect = self._fs.normal_rect --we set this after maximize() above.
--restore events, invalidate and show.
self._fullscreen = false
self.frontend:events(events)
self.frontend:invalidate()
--restore synchronously to avoid re-entring.
if self._fs.maximized then
self.win:maximize()
end
self.frontend:_backend_changed()
end
function Window:on_minimizing()
--refuse to minimize a fullscreen window to avoid undefined behavior.
if self.backend._fullscreen then
return false
end
end
--state/enabled --------------------------------------------------------------
function window:get_enabled()
return self.win.enabled
end
function window:set_enabled(enabled)
self.win.enabled = enabled
end
--positioning/frame extents --------------------------------------------------
local function frame_args(frame, has_menu, resizeable)
local framed = frame == 'normal' or frame == 'toolbox'
return {
border = framed,
frame = framed,
window_edge = framed,
resizeable = resizeable,
tool_window = frame == 'toolbox',
menu = has_menu or nil,
}
end
function app:frame_extents(frame, has_menu, resizeable)
local cx, cy, cw, ch = 200, 200, 400, 400
local rect = pack_rect(nil, cx, cy, cw, ch)
local rect = winapi.Window:client_to_frame(frame_args(frame, has_menu, resizeable), rect)
local x, y, w, h = unpack_rect(rect)
return cx-x, cy-y, w-cw-(cx-x), h-ch-(cy-y)
end
--positioning/rectangles -----------------------------------------------------
local r = winapi.RECT()
function window:get_client_size()
local r = self.win:get_client_rect(r)
return r.w, r.h
end
function window:get_client_pos()
local p = self.win:map_point(nil, 0, 0)
return p.x, p.y
end
function window:get_normal_frame_rect()
if self._fullscreen then
return unpack_rect(self._fs.normal_rect)
else
return unpack_rect(self.win.normal_rect)
end
end
function window:get_frame_rect()
return unpack_rect(self.win.screen_rect)
end
function window:set_frame_rect(x, y, w, h)
self.win.rect = pack_rect(nil, x, y, w, h)
self.frontend:_backend_changed()
end
--positioning/constraints ----------------------------------------------------
function window:get_minsize()
return self.win.min_cw, self.win.min_ch
end
function window:set_minsize(w, h)
self.win.min_cw = w
self.win.min_ch = h
self.win:resize(self.win.w, self.win.h)
end
function window:get_maxsize()
return self.win.max_cw, self.win.max_ch
end
function window:set_maxsize(w, h)
self.win.max_cw = w
self.win.max_ch = h
self.win:resize(self.win.w, self.win.h)
end
--positioning/resizing -------------------------------------------------------
function Window:on_begin_sizemove()
self.nw_sizemove_how = false --don't know if it's a resize or move yet
end
function Window:on_end_sizemove()
local how = self.nw_sizemove_how
if how then
self.nw_sizemove_how = false
self.frontend:_backend_sizing('end', how)
end
end
function Window:WM_NCLBUTTONDOWN(x, y, ht)
self.nw_mousedown_x = x
self.nw_mousedown_y = y
end
function Window:nw_frame_changing(how, rect)
if not self.nw_sizemove_how then
if how == 'move' then
--when moving the window, we want its position relative to the mouse
--position to remain constant, and we're going to enforce that.
self.nw_dx = self.nw_mousedown_x - self.x
self.nw_dy = self.nw_mousedown_y - self.y
end
--trigger the deferred start_resize event, once.
self.frontend:_backend_sizing('start', how)
self.nw_sizemove_how = how
end
if how == 'move' then
--set window's position based on current mouse position and initial
--offset, regardless of how the coordinates are adjusted by the user
--on each event. this is consistent with OSX and it feels better.
local m = winapi.Windows.cursor_pos
local w, h = rect.w, rect.h
rect.x1 = m.x - self.nw_dx
rect.y1 = m.y - self.nw_dy
rect.x2 = rect.x1 + w
rect.y2 = rect.y1 + h
end
pack_rect(rect, self.frontend:_backend_sizing('progress', how, unpack_rect(rect)))
if how == 'move' then
--move sticky children too to emulate default OSX behavior.
local children = self.frontend:children()
if #children > 0 then
local x, y = rect.x, rect.y
local x0, y0 = self.backend:get_frame_rect()
local dx = x - x0
local dy = y - y0
for _,win in ipairs(children) do
if win:sticky() then
local x, y = win:frame_rect()
win.backend.win:move(x + dx, y + dy)
end
end
end
end
end
function Window:on_moving(rect)
self:nw_frame_changing('move', rect)
return true --signal that the position was modified
end
function Window:on_resizing(how, rect)
self:nw_frame_changing(how, rect)
end
function Window:on_moved()
if not self.frontend then return end
self.frontend:_backend_changed()
end
function Window:on_resized(flag)
if not self.backend then return end --early resize from setting constraints: ignore.
if flag == 'maximized' then
if self.nw_maximizing then return end
--frameless windows maximize to the entire screen, covering the taskbar. fix that.
if not self.frame then
self.nw_maximizing = true --on_resized() barrier
self.rect = pack_rect(nil, self.backend:display():desktop_rect())
self.nw_maximizing = false
end
self.frontend:invalidate()
elseif flag == 'restored' then --also triggered on show and on resize
self.frontend:invalidate()
end
self.backend:_set_region()
self.frontend:_backend_changed()
end
--positioning/magnets --------------------------------------------------------
function window:magnets()
local t = {} --{{x, y, w, h}, ...}
local rect
for i,hwnd in ipairs(winapi.EnumChildWindows()) do --front-to-back order assured
if hwnd ~= self.win.hwnd --exclude self
and winapi.IsVisible(hwnd) --exclude invisible
and not winapi.IsZoomed(hwnd) --exclude maximized (TODO: also excludes constrained maximized)
then
rect = winapi.GetWindowRect(hwnd, rect)
t[#t+1] = {x = rect.x, y = rect.y, w = rect.w, h = rect.h}
end
end
return t
end
--titlebar -------------------------------------------------------------------
function window:get_title()
return self.win.title
end
function window:set_title(title)
self.win.title = title
end
--z-order --------------------------------------------------------------------
function window:get_topmost()
return self.win.topmost
end
function window:set_topmost(topmost)
self.win.topmost = topmost
end
function window:raise(relto)
self.win:bring_to_front(relto and relto.backend.win)
end
function window:lower(relto)
self.win:send_to_back(relto and relto.backend.win)
end
--displays -------------------------------------------------------------------
function app:_display(monitor)
local ok, info = pcall(winapi.GetMonitorInfo, monitor)
if not ok then return end
--skip displays that are mirroring pseudo-displays.
local dd = winapi.EnumDisplayDevices(info.szDevice)
if bit.band(dd.state_flags, winapi.DISPLAY_DEVICE_MIRRORING_DRIVER) ~= 0 then return end
local sf = self:_get_scaling_factor(monitor)
return self.frontend:_display{
x = info.monitor_rect.x,
y = info.monitor_rect.y,
w = info.monitor_rect.w,
h = info.monitor_rect.h,
cx = info.work_rect.x,
cy = info.work_rect.y,
cw = info.work_rect.w,
ch = info.work_rect.h,
scalingfactor = sf,
}
end
function app:displays()
local monitors = winapi.EnumDisplayMonitors() --the order is undefined
local displays = {}
for i = 1, #monitors do
local display = self:_display(monitors[i])
if display then
table.insert(displays, display)
end
end
return displays
end
function app:display_count()
--NOTE: SM_CMONITORS doesn't count mirroring pseudo-displays
--so it matches the number of displays returned with app:displays().
return winapi.GetSystemMetrics'SM_CMONITORS'
end
function app:main_display()
local p = winapi.POINT(0,0) --primary display is at (0,0) by definition.
return self:_display(winapi.MonitorFromPoint(p, 'MONITOR_DEFAULTTOPRIMARY'))
end
function app:active_display()
--NOTE: we're using GetForegroundWindow() as opposed to GetActiveWindow()
--or GetFocus() which only return handles from our own process.
local hwnd = winapi.GetForegroundWindow()
if hwnd then
return self:_display(winapi.MonitorFromWindow(hwnd, 'MONITOR_DEFAULTTONEAREST'))
else
--in case there's no foreground window, fallback the primary display.
return self:main_display()
end
end
--NOTE: the default flag for self.win.monitor is MONITOR_DEFAULTTONULL,
--which is what we need to emulate OSX behavior for off-screen windows.
function window:display()
return self.app:_display(self.win.monitor)
end
function Window:on_display_change(x, y, bpp)
self.app.frontend:_backend_displays_changed()
end
--cursors --------------------------------------------------------------------
local cursors = {
--pointers
arrow = winapi.IDC_ARROW,
text = winapi.IDC_IBEAM,
hand = winapi.IDC_HAND,
cross = winapi.IDC_CROSS,
forbidden = winapi.IDC_NO,
--move and resize
size_diag1 = winapi.IDC_SIZENESW,
size_diag2 = winapi.IDC_SIZENWSE,
size_h = winapi.IDC_SIZEWE,
size_v = winapi.IDC_SIZENS,
move = winapi.IDC_SIZEALL,
--app state
busy_arrow = winapi.IDC_APPSTARTING,
}
--resize sides and corners
cursors.topleft = cursors.size_diag2
cursors.topright = cursors.size_diag1
cursors.bottomleft = cursors.size_diag1
cursors.bottomright = cursors.size_diag2
cursors.top = cursors.size_v
cursors.bottom = cursors.size_v
cursors.left = cursors.size_h
cursors.right = cursors.size_h
function window:update_cursor()
--trigger WM_SETCURSOR without having to invalidate the whole window.
local p = winapi.GetCursorPos()
winapi.SetCursorPos(p.x, p.y)
if (self.win.capture_count or 0) > 0 then
--when the mouse is captured, WM_SETCURSOR events are not sent.
self.win:on_set_cursor(nil, winapi.HTCLIENT)
end
end
function Window:on_set_cursor(_, ht)
if ht ~= winapi.HTCLIENT then return end
local cursor, visible = self.frontend:cursor()
if not visible then
winapi.SetCursor(nil)
else
local cursor = assert(cursors[cursor])
winapi.SetCursor(winapi.LoadCursor(cursor))
end
return true --important
end
--keyboard -------------------------------------------------------------------
local keynames = { --vkey code -> vkey name
[winapi.VK_OEM_1] = ';', --on US keyboards
[winapi.VK_OEM_PLUS] = '=',
[winapi.VK_OEM_COMMA] = ',',
[winapi.VK_OEM_MINUS] = '-',
[winapi.VK_OEM_PERIOD] = '.',
[winapi.VK_OEM_2] = '/', --on US keyboards
[winapi.VK_OEM_3] = '`', --on US keyboards
[winapi.VK_OEM_4] = '[', --on US keyboards
[winapi.VK_OEM_5] = '\\', --on US keyboards
[winapi.VK_OEM_6] = ']', --on US keyboards
[winapi.VK_OEM_7] = '\'', --on US keyboards
[winapi.VK_BACK] = 'backspace',
[winapi.VK_TAB] = 'tab',
[winapi.VK_SPACE] = 'space',
[winapi.VK_ESCAPE] = 'esc',
[winapi.VK_F1] = 'F1',
[winapi.VK_F2] = 'F2',
[winapi.VK_F3] = 'F3',
[winapi.VK_F4] = 'F4',
[winapi.VK_F5] = 'F5',
[winapi.VK_F6] = 'F6',
[winapi.VK_F7] = 'F7',
[winapi.VK_F8] = 'F8',
[winapi.VK_F9] = 'F9',
[winapi.VK_F10] = 'F10',
[winapi.VK_F11] = 'F11',
[winapi.VK_F12] = 'F12',
[winapi.VK_CAPITAL] = 'capslock',
[winapi.VK_NUMLOCK] = 'numlock', --win keyboard; mapped to 'numclear' on mac
[winapi.VK_SNAPSHOT] = 'printscreen', --win keyboard; mapped to 'F13' on mac;
--taken on windows (screen snapshot)
[winapi.VK_SCROLL] = 'scrolllock', --win keyboard; mapped to 'F14' on mac
[winapi.VK_NUMPAD0] = 'num0',
[winapi.VK_NUMPAD1] = 'num1',
[winapi.VK_NUMPAD2] = 'num2',
[winapi.VK_NUMPAD3] = 'num3',
[winapi.VK_NUMPAD4] = 'num4',
[winapi.VK_NUMPAD5] = 'num5',
[winapi.VK_NUMPAD6] = 'num6',
[winapi.VK_NUMPAD7] = 'num7',
[winapi.VK_NUMPAD8] = 'num8',
[winapi.VK_NUMPAD9] = 'num9',
[winapi.VK_DECIMAL] = 'num.',
[winapi.VK_MULTIPLY] = 'num*',
[winapi.VK_ADD] = 'num+',
[winapi.VK_SUBTRACT] = 'num-',
[winapi.VK_DIVIDE] = 'num/',
[winapi.VK_CLEAR] = 'numclear',
[winapi.VK_VOLUME_MUTE] = 'mute',
[winapi.VK_VOLUME_DOWN] = 'volumedown',
[winapi.VK_VOLUME_UP] = 'volumeup',
[0xff] = 'lwin', --win keyboard; mapped to 'lcommand' on mac
[winapi.VK_RWIN] = 'rwin', --win keyboard; mapped to 'rcommand' on mac
[winapi.VK_APPS] = 'menu', --win keyboard
[winapi.VK_OEM_NEC_EQUAL] = 'num=', --mac keyboard
}
for ascii = string.byte('0'), string.byte('9') do --ASCII 0-9 -> '0'-'9'
keynames[ascii] = string.char(ascii)
end
for ascii = string.byte('A'), string.byte('Z') do --ASCII A-Z -> 'A'-'Z'
keynames[ascii] = string.char(ascii)
end
local keynames_ext = {}
keynames_ext[false] = { --vkey code -> vkey name when flags.extended_key is false