]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/windows/windows.factor
reset frame inset when windows dwm is toggled off and on, and use the system dialog...
[factor.git] / basis / ui / backend / windows / windows.factor
1 ! Copyright (C) 2005, 2006 Doug Coleman.
2 ! Portions copyright (C) 2007, 2009 Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: alien alien.c-types alien.strings arrays assocs ui
5 ui.private ui.gadgets ui.gadgets.private ui.backend
6 ui.clipboards ui.gadgets.worlds ui.gestures ui.event-loop io
7 kernel math math.vectors namespaces make sequences strings
8 vectors words windows.dwmapi system-info.windows windows.kernel32
9 windows.gdi32 windows.user32 windows.opengl32 windows.messages
10 windows.types windows.offscreen windows.nt threads libc combinators
11 fry combinators.short-circuit continuations command-line shuffle
12 opengl ui.render math.bitwise locals accessors math.rectangles
13 math.order calendar ascii sets io.encodings.utf16n
14 windows.errors literals ui.pixel-formats
15 ui.pixel-formats.private memoize classes colors
16 specialized-arrays classes.struct alien.data ;
17 SPECIALIZED-ARRAY: POINT
18 IN: ui.backend.windows
19
20 SINGLETON: windows-ui-backend
21
22 TUPLE: win-base hDC hRC ;
23 TUPLE: win < win-base hWnd world title ;
24 TUPLE: win-offscreen < win-base hBitmap bits ;
25 C: <win> win
26 C: <win-offscreen> win-offscreen
27
28 <PRIVATE
29
30 PIXEL-FORMAT-ATTRIBUTE-TABLE: WGL_ARB { $ WGL_SUPPORT_OPENGL_ARB 1 } H{
31     { double-buffered { $ WGL_DOUBLE_BUFFER_ARB 1 } }
32     { stereo { $ WGL_STEREO_ARB 1 } }
33     { offscreen { $ WGL_DRAW_TO_BITMAP_ARB 1 } }
34     { fullscreen { $ WGL_DRAW_TO_WINDOW_ARB 1 } }
35     { windowed { $ WGL_DRAW_TO_WINDOW_ARB 1 } }
36     { accelerated { $ WGL_ACCELERATION_ARB $ WGL_FULL_ACCELERATION_ARB } }
37     { software-rendered { $ WGL_ACCELERATION_ARB $ WGL_NO_ACCELERATION_ARB } }
38     { backing-store { $ WGL_SWAP_METHOD_ARB $ WGL_SWAP_COPY_ARB } }
39     { color-float { $ WGL_TYPE_RGBA_FLOAT_ARB 1 } }
40     { color-bits { $ WGL_COLOR_BITS_ARB } }
41     { red-bits { $ WGL_RED_BITS_ARB } }
42     { green-bits { $ WGL_GREEN_BITS_ARB } }
43     { blue-bits { $ WGL_BLUE_BITS_ARB } }
44     { alpha-bits { $ WGL_ALPHA_BITS_ARB } }
45     { accum-bits { $ WGL_ACCUM_BITS_ARB } }
46     { accum-red-bits { $ WGL_ACCUM_RED_BITS_ARB } }
47     { accum-green-bits { $ WGL_ACCUM_GREEN_BITS_ARB } }
48     { accum-blue-bits { $ WGL_ACCUM_BLUE_BITS_ARB } }
49     { accum-alpha-bits { $ WGL_ACCUM_ALPHA_BITS_ARB } }
50     { depth-bits { $ WGL_DEPTH_BITS_ARB } }
51     { stencil-bits { $ WGL_STENCIL_BITS_ARB } }
52     { aux-buffers { $ WGL_AUX_BUFFERS_ARB } }
53     { sample-buffers { $ WGL_SAMPLE_BUFFERS_ARB } }
54     { samples { $ WGL_SAMPLES_ARB } }
55 }
56
57 : has-wglChoosePixelFormatARB? ( world -- ? )
58     drop f ;
59
60 : arb-make-pixel-format ( world attributes -- pf )
61     [ handle>> hDC>> ] dip >WGL_ARB-int-array f 1 0 <int> 0 <int>
62     [ wglChoosePixelFormatARB win32-error=0/f ] 2keep drop *int ;
63
64 : arb-pixel-format-attribute ( pixel-format attribute -- value )
65     >WGL_ARB
66     [ drop f ] [
67         [ [ world>> handle>> hDC>> ] [ handle>> ] bi 0 1 ] dip
68         first <int> 0 <int>
69         [ wglGetPixelFormatAttribivARB win32-error=0/f ]
70         keep *int
71     ] if-empty ;
72
73 CONSTANT: pfd-flag-map H{
74     { double-buffered $ PFD_DOUBLEBUFFER }
75     { stereo $ PFD_STEREO }
76     { offscreen $ PFD_DRAW_TO_BITMAP }
77     { fullscreen $ PFD_DRAW_TO_WINDOW }
78     { windowed $ PFD_DRAW_TO_WINDOW }
79     { backing-store $ PFD_SWAP_COPY }
80     { software-rendered $ PFD_GENERIC_FORMAT }
81 }
82
83 : >pfd-flag ( attribute -- value )
84     pfd-flag-map at [ ] [ 0 ] if* ;
85
86 : >pfd-flags ( attributes -- flags )
87     [ >pfd-flag ] [ bitor ] map-reduce
88     PFD_SUPPORT_OPENGL bitor ;
89
90 : attr-value ( attributes name -- value )
91     [ instance? ] curry find nip
92     [ value>> ] [ 0 ] if* ;
93
94 : >pfd ( attributes -- pfd )
95     [ PIXELFORMATDESCRIPTOR <struct> ] dip
96     {
97         [ drop PIXELFORMATDESCRIPTOR heap-size >>nSize ]
98         [ drop 1 >>nVersion ]
99         [ >pfd-flags >>dwFlags ]
100         [ drop PFD_TYPE_RGBA >>iPixelType ]
101         [ color-bits attr-value >>cColorBits ]
102         [ red-bits attr-value >>cRedBits ]
103         [ green-bits attr-value >>cGreenBits ]
104         [ blue-bits attr-value >>cBlueBits ]
105         [ alpha-bits attr-value >>cAlphaBits ]
106         [ accum-bits attr-value >>cAccumBits ]
107         [ accum-red-bits attr-value >>cAccumRedBits ]
108         [ accum-green-bits attr-value >>cAccumGreenBits ]
109         [ accum-blue-bits attr-value >>cAccumBlueBits ]
110         [ accum-alpha-bits attr-value >>cAccumAlphaBits ]
111         [ depth-bits attr-value >>cDepthBits ]
112         [ stencil-bits attr-value >>cStencilBits ]
113         [ aux-buffers attr-value >>cAuxBuffers ]
114         [ drop PFD_MAIN_PLANE >>dwLayerMask ]
115     } cleave ;
116
117 : pfd-make-pixel-format ( world attributes -- pf )
118     [ handle>> hDC>> ] [ >pfd ] bi*
119     ChoosePixelFormat dup win32-error=0/f ;
120
121 : get-pfd ( pixel-format -- pfd )
122     [ world>> handle>> hDC>> ] [ handle>> ] bi
123     PIXELFORMATDESCRIPTOR heap-size
124     PIXELFORMATDESCRIPTOR <struct>
125     [ DescribePixelFormat win32-error=0/f ] keep ;
126
127 : pfd-flag? ( pfd flag -- ? )
128     [ dwFlags>> ] dip bitand c-bool> ;
129
130 : (pfd-pixel-format-attribute) ( pfd attribute -- value )
131     {
132         { double-buffered [ PFD_DOUBLEBUFFER pfd-flag? ] }
133         { stereo [ PFD_STEREO pfd-flag? ] }
134         { offscreen [ PFD_DRAW_TO_BITMAP pfd-flag? ] }
135         { fullscreen [ PFD_DRAW_TO_WINDOW pfd-flag? ] }
136         { windowed [ PFD_DRAW_TO_WINDOW pfd-flag? ] }
137         { software-rendered [ PFD_GENERIC_FORMAT pfd-flag? ] }
138         { color-bits [ cColorBits>> ] }
139         { red-bits [ cRedBits>> ] }
140         { green-bits [ cGreenBits>> ] }
141         { blue-bits [ cBlueBits>> ] }
142         { alpha-bits [ cAlphaBits>> ] }
143         { accum-bits [ cAccumBits>> ] }
144         { accum-red-bits [ cAccumRedBits>> ] }
145         { accum-green-bits [ cAccumGreenBits>> ] }
146         { accum-blue-bits [ cAccumBlueBits>> ] }
147         { accum-alpha-bits [ cAccumAlphaBits>> ] }
148         { depth-bits [ cDepthBits>> ] }
149         { stencil-bits [ cStencilBits>> ] }
150         { aux-buffers [ cAuxBuffers>> ] }
151         [ 2drop f ]
152     } case ;
153
154 : pfd-pixel-format-attribute ( pixel-format attribute -- value )
155     [ get-pfd ] dip (pfd-pixel-format-attribute) ;
156
157 M: windows-ui-backend (make-pixel-format)
158     over has-wglChoosePixelFormatARB?
159     [ arb-make-pixel-format ] [ pfd-make-pixel-format ] if ;
160
161 M: windows-ui-backend (free-pixel-format)
162     drop ;
163
164 M: windows-ui-backend (pixel-format-attribute)
165     over world>> has-wglChoosePixelFormatARB?
166     [ arb-pixel-format-attribute ] [ pfd-pixel-format-attribute ] if ;
167
168 M: windows-ui-backend system-background-color
169     composition-enabled?
170     [ T{ rgba f 0.0 0.0 0.0 0.0 } ]
171     [ COLOR_BTNFACE GetSysColor RGB>color ] if ;
172
173 PRIVATE>
174
175 : lo-word ( wparam -- lo ) <short> *short ; inline
176 : hi-word ( wparam -- hi ) -16 shift lo-word ; inline
177 : >lo-hi ( WORD -- array ) [ lo-word ] [ hi-word ] bi 2array ;
178
179 : crlf>lf ( str -- str' )
180     CHAR: \r swap remove ;
181
182 : lf>crlf ( str -- str' )
183     [ [ dup CHAR: \n = [ CHAR: \r , ] when , ] each ] "" make ;
184
185 : enum-clipboard ( -- seq )
186     0
187     [ EnumClipboardFormats win32-error dup dup 0 > ]
188     [ ]
189     produce 2nip ;
190
191 : with-clipboard ( quot -- )
192     f OpenClipboard win32-error=0/f
193     call
194     CloseClipboard win32-error=0/f ; inline
195
196 : paste ( -- str )
197     [
198         CF_UNICODETEXT IsClipboardFormatAvailable zero? [
199             ! nothing to paste
200             ""
201         ] [
202             CF_UNICODETEXT GetClipboardData dup win32-error=0/f
203             dup GlobalLock dup win32-error=0/f
204             GlobalUnlock win32-error=0/f
205             utf16n alien>string
206         ] if
207     ] with-clipboard
208     crlf>lf ;
209
210 : copy ( str -- )
211     lf>crlf [
212         utf16n string>alien
213         EmptyClipboard win32-error=0/f
214         GMEM_MOVEABLE over length 1 + GlobalAlloc
215             dup win32-error=0/f
216     
217         dup GlobalLock dup win32-error=0/f
218         swapd byte-array>memory
219         dup GlobalUnlock win32-error=0/f
220         CF_UNICODETEXT swap SetClipboardData win32-error=0/f
221     ] with-clipboard ;
222
223 TUPLE: pasteboard ;
224 C: <pasteboard> pasteboard
225
226 M: pasteboard clipboard-contents drop paste ;
227 M: pasteboard set-clipboard-contents drop copy ;
228
229 : init-clipboard ( -- )
230     <pasteboard> clipboard set-global
231     <clipboard> selection set-global ;
232
233 SYMBOLS: msg-obj class-name-ptr mouse-captured ;
234
235 CONSTANT: window-control>style
236     H{
237         { close-button 0 }
238         { textured-background 0 }
239         { minimize-button $ WS_MINIMIZEBOX }
240         { maximize-button $ WS_MAXIMIZEBOX }
241         { resize-handles $ WS_THICKFRAME }
242         { small-title-bar $ WS_CAPTION }
243         { normal-title-bar $ WS_CAPTION }
244     }
245
246 CONSTANT: window-control>ex-style
247     H{
248         { close-button 0 }
249         { textured-background 0 }
250         { minimize-button 0 }
251         { maximize-button 0 }
252         { resize-handles $ WS_EX_WINDOWEDGE }
253         { small-title-bar $ WS_EX_TOOLWINDOW }
254         { normal-title-bar $ WS_EX_APPWINDOW }
255     }
256
257 : needs-sysmenu? ( controls -- ? )
258     { close-button minimize-button maximize-button } intersects? ;
259
260 : has-titlebar? ( controls -- ? )
261     { small-title-bar normal-title-bar } intersects? ;
262
263 : world>style ( world -- n )
264     window-controls>>
265     [ window-control>style symbols>flags ]
266     [ needs-sysmenu? [ WS_SYSMENU bitor ] when ]
267     [ has-titlebar? [ WS_POPUP bitor ] unless ] tri ;
268
269 : world>ex-style ( world -- n )
270     window-controls>> window-control>ex-style symbols>flags ;
271
272 : get-RECT-top-left ( RECT -- x y )
273     [ left>> ] [ top>> ] bi ;
274
275 : get-RECT-width/height ( RECT -- width height )
276     [ [ right>> ] [ left>> ] bi - ]
277     [ [ bottom>> ] [ top>> ] bi - ] bi ;
278
279 : get-RECT-dimensions ( RECT -- x y width height )
280     [ get-RECT-top-left ] [ get-RECT-width/height ] bi ;
281
282 : handle-wm-paint ( hWnd uMsg wParam lParam -- )
283     #! wParam and lParam are unused
284     #! only paint if width/height both > 0
285     3drop window relayout-1 yield ;
286
287 : handle-wm-size ( hWnd uMsg wParam lParam -- )
288     2nip
289     [ lo-word ] keep hi-word 2array
290     dup { 0 0 } = [ 2drop ] [ swap window [ (>>dim) ] [ drop ] if* ] if ;
291
292 : handle-wm-move ( hWnd uMsg wParam lParam -- )
293     2nip
294     [ lo-word ] keep hi-word 2array
295     swap window [ (>>window-loc) ] [ drop ] if* ;
296
297 CONSTANT: wm-keydown-codes
298     H{
299         { 8 "BACKSPACE" }
300         { 9 "TAB" }
301         { 13 "RET" }
302         { 27 "ESC" }
303         { 33 "PAGE_UP" }
304         { 34 "PAGE_DOWN" }
305         { 35 "END" }
306         { 36 "HOME" }
307         { 37 "LEFT" }
308         { 38 "UP" }
309         { 39 "RIGHT" }
310         { 40 "DOWN" }
311         { 45 "INSERT" }
312         { 46 "DELETE" }
313         { 112 "F1" }
314         { 113 "F2" }
315         { 114 "F3" }
316         { 115 "F4" }
317         { 116 "F5" }
318         { 117 "F6" }
319         { 118 "F7" }
320         { 119 "F8" }
321         { 120 "F9" }
322         { 121 "F10" }
323         { 122 "F11" }
324         { 123 "F12" }
325     }
326
327 : key-state-down? ( key -- ? )
328     GetKeyState 16 bit? ;
329
330 : left-shift? ( -- ? ) VK_LSHIFT key-state-down? ;
331 : left-ctrl? ( -- ? ) VK_LCONTROL key-state-down? ;
332 : left-alt? ( -- ? ) VK_LMENU key-state-down? ;
333 : right-shift? ( -- ? ) VK_RSHIFT key-state-down? ;
334 : right-ctrl? ( -- ? ) VK_RCONTROL key-state-down? ;
335 : right-alt? ( -- ? ) VK_RMENU key-state-down? ;
336 : shift? ( -- ? ) left-shift? right-shift? or ;
337 : ctrl? ( -- ? ) left-ctrl? right-ctrl? or ;
338 : alt? ( -- ? ) left-alt? right-alt? or ;
339 : caps-lock? ( -- ? ) VK_CAPITAL GetKeyState zero? not ;
340
341 : key-modifiers ( -- seq )
342     [
343         shift? [ S+ , ] when
344         ctrl? [ C+ , ] when
345         alt? [ A+ , ] when
346     ] { } make [ empty? not ] keep f ? ;
347
348 CONSTANT: exclude-keys-wm-keydown
349     H{
350         { 16 "SHIFT" }
351         { 17 "CTRL" }
352         { 18 "ALT" }
353         { 20 "CAPS-LOCK" }
354     }
355
356 ! Values are ignored
357 CONSTANT: exclude-keys-wm-char
358     H{
359         { 8 "BACKSPACE" }
360         { 9 "TAB" }
361         { 13 "RET" }
362         { 27 "ESC" }
363     }
364
365 : exclude-key-wm-keydown? ( n -- ? )
366     exclude-keys-wm-keydown key? ;
367
368 : exclude-key-wm-char? ( n -- ? )
369     exclude-keys-wm-char key? ;
370
371 : keystroke>gesture ( n -- mods sym )
372     wm-keydown-codes at* [ key-modifiers swap ] [ drop f f ] if ;
373
374 : send-key-gesture ( sym action? quot hWnd -- )
375     [ [ key-modifiers ] 3dip call ] dip
376     window propagate-key-gesture ; inline
377
378 : send-key-down ( sym action? hWnd -- )
379     [ [ <key-down> ] ] dip send-key-gesture ;
380
381 : send-key-up ( sym action? hWnd -- )
382     [ [ <key-up> ] ] dip send-key-gesture ;
383
384 : key-sym ( wParam -- string/f action? )
385     {
386         {
387             [ dup LETTER? ]
388             [ shift? caps-lock? xor [ CHAR: a + CHAR: A - ] unless 1string f ]
389         }
390         { [ dup digit? ] [ 1string f ] }
391         [ wm-keydown-codes at t ]
392     } cond ;
393
394 :: handle-wm-keydown ( hWnd uMsg wParam lParam -- )
395     wParam exclude-key-wm-keydown? [
396         wParam key-sym over [
397             dup ctrl? alt? xor or [
398                 hWnd send-key-down
399             ] [ 2drop ] if
400         ] [ 2drop ] if
401     ] unless ;
402
403 :: handle-wm-char ( hWnd uMsg wParam lParam -- )
404     wParam exclude-key-wm-char? [
405         ctrl? alt? xor [
406             wParam 1string
407             [ f hWnd send-key-down ]
408             [ hWnd window user-input ] bi
409         ] unless
410     ] unless ;
411
412 :: handle-wm-keyup ( hWnd uMsg wParam lParam -- )
413     wParam exclude-key-wm-keydown? [
414         wParam key-sym over [
415             hWnd send-key-up
416         ] [ 2drop ] if
417     ] unless ;
418
419 :: set-window-active ( hwnd uMsg wParam lParam ? -- n )
420     ? hwnd window (>>active?)
421     hwnd uMsg wParam lParam DefWindowProc ;
422
423 : handle-wm-syscommand ( hWnd uMsg wParam lParam -- n )
424     {
425         { [ over SC_MINIMIZE = ] [ f set-window-active ] }
426         { [ over SC_RESTORE = ] [ t set-window-active ] }
427         { [ over SC_MAXIMIZE = ] [ t set-window-active ] }
428         { [ dup alpha? ] [ 4drop 0 ] }
429         { [ t ] [ DefWindowProc ] }
430     } cond ;
431
432 : cleanup-window ( handle -- )
433     dup title>> [ free ] when*
434     dup hRC>> wglDeleteContext win32-error=0/f
435     dup hWnd>> swap hDC>> ReleaseDC win32-error=0/f ;
436
437 M: windows-ui-backend (close-window)
438     dup hWnd>> unregister-window
439     dup cleanup-window
440     hWnd>> DestroyWindow win32-error=0/f ;
441
442 : handle-wm-close ( hWnd uMsg wParam lParam -- )
443     3drop window ungraft ;
444
445 : handle-wm-set-focus ( hWnd uMsg wParam lParam -- )
446     3drop window [ focus-world ] when* ;
447
448 : handle-wm-kill-focus ( hWnd uMsg wParam lParam -- )
449     3drop window [ unfocus-world ] when* ;
450
451 : message>button ( uMsg -- button down? )
452     {
453         { WM_LBUTTONDOWN   [ 1 t ] }
454         { WM_LBUTTONUP     [ 1 f ] }
455         { WM_MBUTTONDOWN   [ 2 t ] }
456         { WM_MBUTTONUP     [ 2 f ] }
457         { WM_RBUTTONDOWN   [ 3 t ] }
458         { WM_RBUTTONUP     [ 3 f ] }
459
460         { WM_NCLBUTTONDOWN [ 1 t ] }
461         { WM_NCLBUTTONUP   [ 1 f ] }
462         { WM_NCMBUTTONDOWN [ 2 t ] }
463         { WM_NCMBUTTONUP   [ 2 f ] }
464         { WM_NCRBUTTONDOWN [ 3 t ] }
465         { WM_NCRBUTTONUP   [ 3 f ] }
466     } case ;
467
468 ! If the user clicks in the window border ("non-client area")
469 ! Windows sends us an NC[LMR]BUTTONDOWN message; but if the
470 ! mouse is subsequently released outside the NC area, we receive
471 ! a [LMR]BUTTONUP message and Factor can get confused. So we
472 ! ignore BUTTONUP's that are a result of an NC*BUTTONDOWN.
473 SYMBOL: nc-buttons
474
475 : handle-wm-ncbutton ( hWnd uMsg wParam lParam -- )
476     2drop nip
477     message>button nc-buttons get
478     swap [ push ] [ delete ] if ;
479
480 : mouse-wheel ( wParam -- array ) >lo-hi [ sgn neg ] map ;
481
482 : mouse-event>gesture ( uMsg -- button )
483     key-modifiers swap message>button
484     [ <button-down> ] [ <button-up> ] if ;
485
486 :: prepare-mouse ( hWnd uMsg wParam lParam -- button coordinate world )
487     uMsg mouse-event>gesture
488     lParam >lo-hi
489     hWnd window ;
490
491 : set-capture ( hwnd -- )
492     mouse-captured get [
493         drop
494     ] [
495         [ SetCapture drop ] keep
496         mouse-captured set
497     ] if ;
498
499 : release-capture ( -- )
500     ReleaseCapture win32-error=0/f
501     mouse-captured off ;
502
503 : handle-wm-buttondown ( hWnd uMsg wParam lParam -- )
504     [
505         over set-capture
506         dup message>button drop nc-buttons get delete
507     ] 2dip prepare-mouse send-button-down ;
508
509 : handle-wm-buttonup ( hWnd uMsg wParam lParam -- )
510     mouse-captured get [ release-capture ] when
511     pick message>button drop dup nc-buttons get member? [
512         nc-buttons get delete 4drop
513     ] [
514         drop prepare-mouse send-button-up
515     ] if ;
516
517 : make-TRACKMOUSEEVENT ( hWnd -- alien )
518     TRACKMOUSEEVENT <struct>
519         swap >>hwndTrack
520         TRACKMOUSEEVENT heap-size >>cbSize ;
521
522 : handle-wm-mousemove ( hWnd uMsg wParam lParam -- )
523     2nip
524     over make-TRACKMOUSEEVENT
525         TME_LEAVE >>dwFlags
526         0 >>dwHoverTime
527     TrackMouseEvent drop
528     >lo-hi swap window move-hand fire-motion ;
529
530 :: handle-wm-mousewheel ( hWnd uMsg wParam lParam -- )
531     wParam mouse-wheel hand-loc get hWnd window send-wheel ;
532
533 : handle-wm-cancelmode ( hWnd uMsg wParam lParam -- )
534     #! message sent if windows needs application to stop dragging
535     4drop release-capture ;
536
537 : handle-wm-mouseleave ( hWnd uMsg wParam lParam -- )
538     #! message sent if mouse leaves main application 
539     4drop forget-rollover ;
540
541 : ?make-glass ( world hwnd -- )
542     swap { [ transparent?>> ] [ drop windows-major 6 >= ] } 1&&
543     [ full-window-margins DwmExtendFrameIntoClientArea drop ]
544     [ drop ] if ;
545
546 : handle-wm-dwmcompositionchanged ( hWnd uMsg wParam lParam -- )
547     3drop [ window ] keep ?make-glass ;
548
549 SYMBOL: wm-handlers
550
551 H{ } clone wm-handlers set-global
552
553 : add-wm-handler ( quot wm -- )
554     dup array?
555     [ [ execute( -- wm ) add-wm-handler ] with each ]
556     [ wm-handlers get-global set-at ] if ;
557
558 [ handle-wm-close 0                  ] WM_CLOSE add-wm-handler
559 [ 4dup handle-wm-paint DefWindowProc ] WM_PAINT add-wm-handler
560
561 [ handle-wm-size 0 ] WM_SIZE add-wm-handler
562 [ handle-wm-move 0 ] WM_MOVE add-wm-handler
563
564 [ 4dup handle-wm-keydown DefWindowProc ] { WM_KEYDOWN WM_SYSKEYDOWN } add-wm-handler
565 [ 4dup handle-wm-char DefWindowProc    ] { WM_CHAR WM_SYSCHAR }       add-wm-handler
566 [ 4dup handle-wm-keyup DefWindowProc   ] { WM_KEYUP WM_SYSKEYUP }     add-wm-handler
567
568 [ handle-wm-syscommand   ] WM_SYSCOMMAND add-wm-handler
569 [ handle-wm-set-focus 0  ] WM_SETFOCUS add-wm-handler
570 [ handle-wm-kill-focus 0 ] WM_KILLFOCUS add-wm-handler
571
572 [ handle-wm-buttondown 0 ] WM_LBUTTONDOWN add-wm-handler
573 [ handle-wm-buttondown 0 ] WM_MBUTTONDOWN add-wm-handler
574 [ handle-wm-buttondown 0 ] WM_RBUTTONDOWN add-wm-handler
575 [ handle-wm-buttonup 0   ] WM_LBUTTONUP   add-wm-handler
576 [ handle-wm-buttonup 0   ] WM_MBUTTONUP   add-wm-handler
577 [ handle-wm-buttonup 0   ] WM_RBUTTONUP   add-wm-handler
578 [ handle-wm-dwmcompositionchanged 0   ] WM_DWMCOMPOSITIONCHANGED add-wm-handler
579
580 [ 4dup handle-wm-ncbutton DefWindowProc ]
581 { WM_NCLBUTTONDOWN WM_NCMBUTTONDOWN WM_NCRBUTTONDOWN
582 WM_NCLBUTTONUP WM_NCMBUTTONUP WM_NCRBUTTONUP }
583 add-wm-handler
584
585 [ nc-buttons get-global delete-all DefWindowProc ]
586 { WM_EXITSIZEMOVE WM_EXITMENULOOP } add-wm-handler
587
588 [ handle-wm-mousemove 0  ] WM_MOUSEMOVE  add-wm-handler
589 [ handle-wm-mousewheel 0 ] WM_MOUSEWHEEL add-wm-handler
590 [ handle-wm-cancelmode 0 ] WM_CANCELMODE add-wm-handler
591 [ handle-wm-mouseleave 0 ] WM_MOUSELEAVE add-wm-handler
592
593 SYMBOL: trace-messages?
594
595 ! return 0 if you handle the message, else just let DefWindowProc return its val
596 : ui-wndproc ( -- object )
597     "uint" { "void*" "uint" "long" "long" } "stdcall" [
598         pick
599         trace-messages? get-global [ dup windows-message-name name>> print flush ] when
600         wm-handlers get-global at* [ call ] [ drop DefWindowProc ] if
601      ] alien-callback ;
602
603 : peek-message? ( msg -- ? ) f 0 0 PM_REMOVE PeekMessage zero? ;
604
605 M: windows-ui-backend do-events
606     msg-obj get-global
607     dup peek-message? [ drop ui-wait ] [
608         [ TranslateMessage drop ]
609         [ DispatchMessage drop ] bi
610     ] if ;
611
612 :: register-window-class ( class-name-ptr -- )
613     WNDCLASSEX <struct> f GetModuleHandle
614     class-name-ptr pick GetClassInfoEx 0 = [
615         WNDCLASSEX heap-size >>cbSize
616         { CS_HREDRAW CS_VREDRAW CS_OWNDC } flags >>style
617         ui-wndproc >>lpfnWndProc
618         0 >>cbClsExtra
619         0 >>cbWndExtra
620         f GetModuleHandle >>hInstance
621         f GetModuleHandle "fraptor" utf16n string>alien LoadIcon >>hIcon
622         f IDC_ARROW LoadCursor >>hCursor
623
624         class-name-ptr >>lpszClassName
625         RegisterClassEx win32-error=0/f
626     ] [ drop ] if ;
627
628 : adjust-RECT ( RECT style ex-style -- )
629     [ 0 ] dip AdjustWindowRectEx win32-error=0/f ;
630
631 : make-RECT ( world -- RECT )
632     [ window-loc>> ] [ dim>> ] bi <RECT> ;
633
634 : default-position-RECT ( RECT -- RECT' )
635     dup get-RECT-width/height
636         [ CW_USEDEFAULT + >>right ] dip
637         CW_USEDEFAULT + >>bottom
638         CW_USEDEFAULT >>left
639         CW_USEDEFAULT >>top ;
640
641 : make-adjusted-RECT ( rect style ex-style -- RECT )
642     [
643         make-RECT
644         dup get-RECT-top-left [ zero? ] both? swap
645         dup
646     ] 2dip adjust-RECT
647     swap [ default-position-RECT ] when ;
648
649 : get-window-class ( -- class-name )
650     class-name-ptr [
651         dup expired? [ drop "Factor-window" utf16n malloc-string ] when
652         dup register-window-class
653         dup
654     ] change-global ;
655
656 :: create-window ( rect style ex-style -- hwnd )
657     rect style ex-style make-adjusted-RECT
658     [ get-window-class f ] dip
659     [
660         [ ex-style ] 2dip
661         WS_CLIPSIBLINGS WS_CLIPCHILDREN bitor style bitor
662     ] dip get-RECT-dimensions
663     f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ;
664
665 : show-window ( hWnd -- )
666     dup SW_SHOW ShowWindow drop ! always succeeds
667     dup SetForegroundWindow drop
668     SetFocus drop ;
669
670 : init-win32-ui ( -- )
671     V{ } clone nc-buttons set-global
672     MSG malloc-struct msg-obj set-global
673     GetDoubleClickTime milliseconds double-click-timeout set-global ;
674
675 : cleanup-win32-ui ( -- )
676     class-name-ptr [ [ [ f UnregisterClass drop ] [ free ] bi ] when* f ] change-global
677     msg-obj [ [ free ] when* f ] change-global ;
678
679 : get-dc ( world -- )
680     handle>> dup hWnd>> GetDC dup win32-error=0/f >>hDC drop ;
681
682 : get-rc ( world -- )
683     handle>> dup hDC>> dup wglCreateContext dup win32-error=0/f
684     [ wglMakeCurrent win32-error=0/f ] keep >>hRC drop ;
685
686 : set-pixel-format ( pixel-format hdc -- )
687     swap handle>>
688     PIXELFORMATDESCRIPTOR <struct> SetPixelFormat win32-error=0/f ;
689
690 : setup-gl ( world -- )
691     [ get-dc ] keep
692     [ swap [ handle>> hDC>> set-pixel-format ] [ get-rc ] bi ]
693     with-world-pixel-format ;
694
695 : disable-close-button ( hwnd -- )
696     0 GetSystemMenu
697     SC_CLOSE MF_BYCOMMAND MF_GRAYED bitor EnableMenuItem drop ;
698
699 : ?disable-close-button ( world hwnd -- )
700     swap window-controls>> close-button swap member? not
701     [ disable-close-button ] [ drop ] if ;
702
703 M: windows-ui-backend (open-window) ( world -- )
704     [
705         dup
706         [ ] [ world>style ] [ world>ex-style ] tri create-window
707         [ ?make-glass ]
708         [ ?disable-close-button ]
709         [ [ f f ] dip f f <win> >>handle setup-gl ] 2tri
710     ]
711     [ dup handle>> hWnd>> register-window ]
712     [ handle>> hWnd>> show-window ] tri ;
713
714 M: win-base select-gl-context ( handle -- )
715     [ hDC>> ] [ hRC>> ] bi wglMakeCurrent win32-error=0/f
716     GdiFlush drop ;
717
718 M: win-base flush-gl-context ( handle -- )
719     hDC>> SwapBuffers win32-error=0/f ;
720
721 : setup-offscreen-gl ( world -- )
722     dup [ handle>> ] [ dim>> ] bi make-offscreen-dc-and-bitmap
723     [ >>hDC ] [ >>hBitmap ] [ >>bits ] tri* drop [
724         swap [ handle>> hDC>> set-pixel-format ] [ get-rc ] bi
725     ] with-world-pixel-format ;
726
727 M: windows-ui-backend (open-offscreen-buffer) ( world -- )
728     win-offscreen new >>handle
729     setup-offscreen-gl ;
730
731 M: windows-ui-backend (close-offscreen-buffer) ( handle -- )
732     [ hDC>> DeleteDC drop ]
733     [ hBitmap>> DeleteObject drop ] bi ;
734
735 ! Windows 32-bit bitmaps don't actually use the alpha byte of
736 ! each pixel; it's left as zero
737
738 : (make-opaque) ( byte-array -- byte-array' )
739     [ length 4 /i ]
740     [ '[ 255 swap 4 * 3 + _ set-nth ] each ]
741     [ ] tri ;
742
743 : (opaque-pixels) ( world -- pixels )
744     [ handle>> bits>> ] [ dim>> ] bi bitmap>byte-array (make-opaque) ;
745
746 M: windows-ui-backend offscreen-pixels ( world -- alien w h )
747     [ (opaque-pixels) ] [ dim>> first2 ] bi ;
748
749 M: windows-ui-backend raise-window* ( world -- )
750     handle>> [ hWnd>> SetFocus drop ] when* ;
751
752 M: windows-ui-backend set-title ( string world -- )
753     handle>>
754     dup title>> [ free ] when*
755     swap utf16n malloc-string
756     [ >>title ]
757     [ [ hWnd>> WM_SETTEXT 0 ] dip alien-address SendMessage drop ] bi ;
758
759 M: windows-ui-backend (with-ui)
760     [
761         [
762             init-clipboard
763             init-win32-ui
764             start-ui
765             event-loop
766         ] [ cleanup-win32-ui ] [ ] cleanup
767     ] ui-running ;
768
769 M: windows-ui-backend beep ( -- )
770     0 MessageBeep drop ;
771
772 : fullscreen-RECT ( hwnd -- RECT )
773     MONITOR_DEFAULTTONEAREST MonitorFromWindow
774     MONITORINFOEX <struct>
775         MONITORINFOEX heap-size >>cbSize
776     [ GetMonitorInfo win32-error=0/f ] keep rcMonitor>> ;
777
778 : client-area>RECT ( hwnd -- RECT )
779     RECT <struct>
780     [ GetClientRect win32-error=0/f ]
781     [ >c-ptr byte-array>POINT-array [ ClientToScreen drop ] with each ]
782     [ nip ] 2tri ;
783
784 : hwnd>RECT ( hwnd -- RECT )
785     RECT <struct> [ GetWindowRect win32-error=0/f ] keep ;
786
787 M: windows-ui-backend (grab-input) ( handle -- )
788     0 ShowCursor drop
789     hWnd>> client-area>RECT ClipCursor drop ;
790
791 M: windows-ui-backend (ungrab-input) ( handle -- )
792     drop
793     f ClipCursor drop
794     1 ShowCursor drop ;
795
796 : fullscreen-flags ( -- n )
797     { WS_CAPTION WS_BORDER WS_THICKFRAME } flags ; inline
798
799 : enter-fullscreen ( world -- )
800     handle>> hWnd>>
801     {
802         [
803             GWL_STYLE GetWindowLong
804             fullscreen-flags unmask
805         ]
806         [ GWL_STYLE rot SetWindowLong win32-error=0/f ]
807         [
808             HWND_TOP
809             over hwnd>RECT get-RECT-dimensions
810             SWP_FRAMECHANGED
811             SetWindowPos win32-error=0/f
812         ]
813         [ SW_MAXIMIZE ShowWindow win32-error=0/f ]
814     } cleave ;
815
816 : exit-fullscreen ( world -- )
817     dup handle>> hWnd>>
818     {
819         [ GWL_STYLE rot world>style SetWindowLong win32-error=0/f ]
820         [
821             f
822             over hwnd>RECT get-RECT-dimensions
823             { SWP_NOMOVE SWP_NOSIZE SWP_NOZORDER SWP_FRAMECHANGED } flags
824             SetWindowPos win32-error=0/f
825         ]
826         [ SW_RESTORE ShowWindow win32-error=0/f ]
827     } cleave ;
828
829 M: windows-ui-backend (set-fullscreen) ( ? world -- )
830     [ enter-fullscreen ] [ exit-fullscreen ] if ;
831
832 M: windows-ui-backend (fullscreen?) ( world -- ? )
833     [ handle>> hWnd>> hwnd>RECT ]
834     [ handle>> hWnd>> fullscreen-RECT ] bi
835     [ get-RECT-dimensions 2array 2nip ] bi@ = ;
836
837 windows-ui-backend ui-backend set-global
838
839 [ "ui.tools" ] main-vocab-hook set-global