1 ! Copyright (C) 2005, 2006 Doug Coleman.
2 ! Portions copyright (C) 2007, 2010 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 FROM: namespaces => set ;
18 SPECIALIZED-ARRAY: POINT
19 IN: ui.backend.windows
21 SINGLETON: windows-ui-backend
23 TUPLE: win-base hDC hRC ;
24 TUPLE: win < win-base hWnd world title ;
25 TUPLE: win-offscreen < win-base hBitmap bits ;
27 C: <win-offscreen> win-offscreen
31 PIXEL-FORMAT-ATTRIBUTE-TABLE: WGL_ARB { $ WGL_SUPPORT_OPENGL_ARB 1 } H{
32 { double-buffered { $ WGL_DOUBLE_BUFFER_ARB 1 } }
33 { stereo { $ WGL_STEREO_ARB 1 } }
34 { offscreen { $ WGL_DRAW_TO_BITMAP_ARB 1 } }
35 { fullscreen { $ WGL_DRAW_TO_WINDOW_ARB 1 } }
36 { windowed { $ WGL_DRAW_TO_WINDOW_ARB 1 } }
37 { accelerated { $ WGL_ACCELERATION_ARB $ WGL_FULL_ACCELERATION_ARB } }
38 { software-rendered { $ WGL_ACCELERATION_ARB $ WGL_NO_ACCELERATION_ARB } }
39 { backing-store { $ WGL_SWAP_METHOD_ARB $ WGL_SWAP_COPY_ARB } }
40 { color-float { $ WGL_TYPE_RGBA_FLOAT_ARB 1 } }
41 { color-bits { $ WGL_COLOR_BITS_ARB } }
42 { red-bits { $ WGL_RED_BITS_ARB } }
43 { green-bits { $ WGL_GREEN_BITS_ARB } }
44 { blue-bits { $ WGL_BLUE_BITS_ARB } }
45 { alpha-bits { $ WGL_ALPHA_BITS_ARB } }
46 { accum-bits { $ WGL_ACCUM_BITS_ARB } }
47 { accum-red-bits { $ WGL_ACCUM_RED_BITS_ARB } }
48 { accum-green-bits { $ WGL_ACCUM_GREEN_BITS_ARB } }
49 { accum-blue-bits { $ WGL_ACCUM_BLUE_BITS_ARB } }
50 { accum-alpha-bits { $ WGL_ACCUM_ALPHA_BITS_ARB } }
51 { depth-bits { $ WGL_DEPTH_BITS_ARB } }
52 { stencil-bits { $ WGL_STENCIL_BITS_ARB } }
53 { aux-buffers { $ WGL_AUX_BUFFERS_ARB } }
54 { sample-buffers { $ WGL_SAMPLE_BUFFERS_ARB } }
55 { samples { $ WGL_SAMPLES_ARB } }
58 : has-wglChoosePixelFormatARB? ( world -- ? )
61 : arb-make-pixel-format ( world attributes -- pf )
62 [ handle>> hDC>> ] dip >WGL_ARB-int-array f 1 { int int }
63 [ wglChoosePixelFormatARB win32-error=0/f ] [ ] with-out-parameters drop ;
65 : arb-pixel-format-attribute ( pixel-format attribute -- value )
68 [ [ world>> handle>> hDC>> ] [ handle>> ] bi 0 1 ] dip
70 [ wglGetPixelFormatAttribivARB win32-error=0/f ] [ ]
74 CONSTANT: pfd-flag-map H{
75 { double-buffered $ PFD_DOUBLEBUFFER }
76 { stereo $ PFD_STEREO }
77 { offscreen $ PFD_DRAW_TO_BITMAP }
78 { fullscreen $ PFD_DRAW_TO_WINDOW }
79 { windowed $ PFD_DRAW_TO_WINDOW }
80 { backing-store $ PFD_SWAP_COPY }
81 { software-rendered $ PFD_GENERIC_FORMAT }
84 : >pfd-flag ( attribute -- value )
85 pfd-flag-map at [ ] [ 0 ] if* ;
87 : >pfd-flags ( attributes -- flags )
88 [ >pfd-flag ] [ bitor ] map-reduce
89 PFD_SUPPORT_OPENGL bitor ;
91 : attr-value ( attributes name -- value )
92 [ instance? ] curry find nip
93 [ value>> ] [ 0 ] if* ;
95 : >pfd ( attributes -- pfd )
96 [ PIXELFORMATDESCRIPTOR <struct> ] dip
98 [ drop PIXELFORMATDESCRIPTOR heap-size >>nSize ]
100 [ >pfd-flags >>dwFlags ]
101 [ drop PFD_TYPE_RGBA >>iPixelType ]
102 [ color-bits attr-value >>cColorBits ]
103 [ red-bits attr-value >>cRedBits ]
104 [ green-bits attr-value >>cGreenBits ]
105 [ blue-bits attr-value >>cBlueBits ]
106 [ alpha-bits attr-value >>cAlphaBits ]
107 [ accum-bits attr-value >>cAccumBits ]
108 [ accum-red-bits attr-value >>cAccumRedBits ]
109 [ accum-green-bits attr-value >>cAccumGreenBits ]
110 [ accum-blue-bits attr-value >>cAccumBlueBits ]
111 [ accum-alpha-bits attr-value >>cAccumAlphaBits ]
112 [ depth-bits attr-value >>cDepthBits ]
113 [ stencil-bits attr-value >>cStencilBits ]
114 [ aux-buffers attr-value >>cAuxBuffers ]
115 [ drop PFD_MAIN_PLANE >>dwLayerMask ]
118 : pfd-make-pixel-format ( world attributes -- pf )
119 [ handle>> hDC>> ] [ >pfd ] bi*
120 ChoosePixelFormat dup win32-error=0/f ;
122 : get-pfd ( pixel-format -- pfd )
123 [ world>> handle>> hDC>> ] [ handle>> ] bi
124 PIXELFORMATDESCRIPTOR heap-size
125 PIXELFORMATDESCRIPTOR <struct>
126 [ DescribePixelFormat win32-error=0/f ] keep ;
128 : pfd-flag? ( pfd flag -- ? )
129 [ dwFlags>> ] dip bitand c-bool> ;
131 : (pfd-pixel-format-attribute) ( pfd attribute -- value )
133 { double-buffered [ PFD_DOUBLEBUFFER pfd-flag? ] }
134 { stereo [ PFD_STEREO pfd-flag? ] }
135 { offscreen [ PFD_DRAW_TO_BITMAP pfd-flag? ] }
136 { fullscreen [ PFD_DRAW_TO_WINDOW pfd-flag? ] }
137 { windowed [ PFD_DRAW_TO_WINDOW pfd-flag? ] }
138 { software-rendered [ PFD_GENERIC_FORMAT pfd-flag? ] }
139 { color-bits [ cColorBits>> ] }
140 { red-bits [ cRedBits>> ] }
141 { green-bits [ cGreenBits>> ] }
142 { blue-bits [ cBlueBits>> ] }
143 { alpha-bits [ cAlphaBits>> ] }
144 { accum-bits [ cAccumBits>> ] }
145 { accum-red-bits [ cAccumRedBits>> ] }
146 { accum-green-bits [ cAccumGreenBits>> ] }
147 { accum-blue-bits [ cAccumBlueBits>> ] }
148 { accum-alpha-bits [ cAccumAlphaBits>> ] }
149 { depth-bits [ cDepthBits>> ] }
150 { stencil-bits [ cStencilBits>> ] }
151 { aux-buffers [ cAuxBuffers>> ] }
155 : pfd-pixel-format-attribute ( pixel-format attribute -- value )
156 [ get-pfd ] dip (pfd-pixel-format-attribute) ;
158 M: windows-ui-backend (make-pixel-format)
159 over has-wglChoosePixelFormatARB?
160 [ arb-make-pixel-format ] [ pfd-make-pixel-format ] if ;
162 M: windows-ui-backend (free-pixel-format)
165 M: windows-ui-backend (pixel-format-attribute)
166 over world>> has-wglChoosePixelFormatARB?
167 [ arb-pixel-format-attribute ] [ pfd-pixel-format-attribute ] if ;
171 : lo-word ( wparam -- lo ) <short> *short ; inline
172 : hi-word ( wparam -- hi ) -16 shift lo-word ; inline
173 : >lo-hi ( WORD -- array ) [ lo-word ] [ hi-word ] bi 2array ;
174 : GET_APPCOMMAND_LPARAM ( lParam -- appCommand )
175 hi-word FAPPCOMMAND_MASK lo-word bitnot bitand ; inline
177 : crlf>lf ( str -- str' )
178 CHAR: \r swap remove ;
180 : lf>crlf ( str -- str' )
181 [ [ dup CHAR: \n = [ CHAR: \r , ] when , ] each ] "" make ;
183 : enum-clipboard ( -- seq )
185 [ EnumClipboardFormats win32-error dup dup 0 > ]
189 : with-clipboard ( quot -- )
190 f OpenClipboard win32-error=0/f
192 CloseClipboard win32-error=0/f ; inline
196 CF_UNICODETEXT IsClipboardFormatAvailable zero? [
200 CF_UNICODETEXT GetClipboardData dup win32-error=0/f
201 dup GlobalLock dup win32-error=0/f
202 GlobalUnlock win32-error=0/f
211 EmptyClipboard win32-error=0/f
212 GMEM_MOVEABLE over length 1 + GlobalAlloc
215 dup GlobalLock dup win32-error=0/f
216 rot binary-object memcpy
217 dup GlobalUnlock win32-error=0/f
218 CF_UNICODETEXT swap SetClipboardData win32-error=0/f
222 C: <pasteboard> pasteboard
224 M: pasteboard clipboard-contents drop paste ;
225 M: pasteboard set-clipboard-contents drop copy ;
227 : init-clipboard ( -- )
228 <pasteboard> clipboard set-global
229 <clipboard> selection set-global ;
231 SYMBOLS: msg-obj class-name-ptr mouse-captured ;
233 CONSTANT: window-control>style
236 { textured-background 0 }
237 { minimize-button $ WS_MINIMIZEBOX }
238 { maximize-button $ WS_MAXIMIZEBOX }
239 { resize-handles $ WS_THICKFRAME }
240 { small-title-bar $ WS_CAPTION }
241 { normal-title-bar $ WS_CAPTION }
244 CONSTANT: window-control>ex-style
247 { textured-background 0 }
248 { minimize-button 0 }
249 { maximize-button 0 }
250 { resize-handles $ WS_EX_WINDOWEDGE }
251 { small-title-bar $[ WS_EX_TOOLWINDOW WS_EX_TOPMOST bitor ] }
252 { normal-title-bar $ WS_EX_APPWINDOW }
255 : needs-sysmenu? ( controls -- ? )
256 { close-button minimize-button maximize-button } intersects? ;
258 : has-titlebar? ( controls -- ? )
259 { small-title-bar normal-title-bar } intersects? ;
261 : world>style ( world -- n )
263 [ window-control>style symbols>flags ]
264 [ needs-sysmenu? [ WS_SYSMENU bitor ] when ]
265 [ has-titlebar? [ WS_POPUP bitor ] unless ] tri ;
267 : world>ex-style ( world -- n )
268 window-controls>> window-control>ex-style symbols>flags ;
270 : get-RECT-top-left ( RECT -- x y )
271 [ left>> ] [ top>> ] bi ;
273 : get-RECT-width/height ( RECT -- width height )
274 [ [ right>> ] [ left>> ] bi - ]
275 [ [ bottom>> ] [ top>> ] bi - ] bi ;
277 : get-RECT-dimensions ( RECT -- x y width height )
278 [ get-RECT-top-left ] [ get-RECT-width/height ] bi ;
280 : handle-wm-paint ( hWnd uMsg wParam lParam -- )
281 #! wParam and lParam are unused
282 #! only paint if width/height both > 0
283 3drop window relayout-1 yield ;
285 : handle-wm-size ( hWnd uMsg wParam lParam -- )
287 [ lo-word ] keep hi-word 2array
288 dup { 0 0 } = [ 2drop ] [ swap window [ dim<< ] [ drop ] if* ] if ;
290 : handle-wm-move ( hWnd uMsg wParam lParam -- )
292 [ lo-word ] keep hi-word 2array
293 swap window [ window-loc<< ] [ drop ] if* ;
295 CONSTANT: wm-keydown-codes
325 : key-state-down? ( key -- ? )
326 GetKeyState 16 bit? ;
328 : left-shift? ( -- ? ) VK_LSHIFT key-state-down? ;
329 : left-ctrl? ( -- ? ) VK_LCONTROL key-state-down? ;
330 : left-alt? ( -- ? ) VK_LMENU key-state-down? ;
331 : right-shift? ( -- ? ) VK_RSHIFT key-state-down? ;
332 : right-ctrl? ( -- ? ) VK_RCONTROL key-state-down? ;
333 : right-alt? ( -- ? ) VK_RMENU key-state-down? ;
334 : shift? ( -- ? ) left-shift? right-shift? or ;
335 : ctrl? ( -- ? ) left-ctrl? right-ctrl? or ;
336 : alt? ( -- ? ) left-alt? right-alt? or ;
337 : caps-lock? ( -- ? ) VK_CAPITAL GetKeyState zero? not ;
339 : key-modifiers ( -- seq )
344 ] { } make [ empty? not ] keep f ? ;
346 CONSTANT: exclude-keys-wm-keydown
355 CONSTANT: exclude-keys-wm-char
363 : exclude-key-wm-keydown? ( n -- ? )
364 exclude-keys-wm-keydown key? ;
366 : exclude-key-wm-char? ( n -- ? )
367 exclude-keys-wm-char key? ;
369 : keystroke>gesture ( n -- mods sym )
370 wm-keydown-codes at* [ key-modifiers swap ] [ drop f f ] if ;
372 : send-key-gesture ( sym action? quot hWnd -- )
373 [ [ key-modifiers ] 3dip call ] dip
374 window propagate-key-gesture ; inline
376 : send-key-down ( sym action? hWnd -- )
377 [ [ <key-down> ] ] dip send-key-gesture ;
379 : send-key-up ( sym action? hWnd -- )
380 [ [ <key-up> ] ] dip send-key-gesture ;
382 : key-sym ( wParam -- string/f action? )
386 [ shift? caps-lock? xor [ CHAR: a + CHAR: A - ] unless 1string f ]
388 { [ dup digit? ] [ 1string f ] }
389 [ wm-keydown-codes at t ]
392 :: handle-wm-keydown ( hWnd uMsg wParam lParam -- )
393 wParam exclude-key-wm-keydown? [
394 wParam key-sym over [
395 dup ctrl? alt? xor or [
401 :: handle-wm-char ( hWnd uMsg wParam lParam -- )
402 wParam exclude-key-wm-char? [
405 [ f hWnd send-key-down ]
406 [ hWnd window user-input ] bi
410 :: handle-wm-keyup ( hWnd uMsg wParam lParam -- )
411 wParam exclude-key-wm-keydown? [
412 wParam key-sym over [
417 :: set-window-active ( hwnd uMsg wParam lParam ? -- n )
418 ? hwnd window active?<<
419 hwnd uMsg wParam lParam DefWindowProc ;
421 : handle-wm-syscommand ( hWnd uMsg wParam lParam -- n )
423 { [ over SC_MINIMIZE = ] [ f set-window-active ] }
424 { [ over SC_RESTORE = ] [ t set-window-active ] }
425 { [ over SC_MAXIMIZE = ] [ t set-window-active ] }
426 { [ dup alpha? ] [ 4drop 0 ] }
427 { [ t ] [ DefWindowProc ] }
430 : cleanup-window ( handle -- )
431 dup title>> [ free ] when*
432 dup hRC>> wglDeleteContext win32-error=0/f
433 dup hWnd>> swap hDC>> ReleaseDC win32-error=0/f ;
435 M: windows-ui-backend (close-window)
436 dup hWnd>> unregister-window
438 hWnd>> DestroyWindow win32-error=0/f ;
440 : handle-wm-close ( hWnd uMsg wParam lParam -- )
441 3drop window ungraft ;
443 : handle-wm-set-focus ( hWnd uMsg wParam lParam -- )
444 3drop window [ focus-world ] when* ;
446 : handle-wm-kill-focus ( hWnd uMsg wParam lParam -- )
447 3drop window [ unfocus-world ] when* ;
449 : message>button ( uMsg -- button down? )
451 { WM_LBUTTONDOWN [ 1 t ] }
452 { WM_LBUTTONUP [ 1 f ] }
453 { WM_MBUTTONDOWN [ 2 t ] }
454 { WM_MBUTTONUP [ 2 f ] }
455 { WM_RBUTTONDOWN [ 3 t ] }
456 { WM_RBUTTONUP [ 3 f ] }
458 { WM_NCLBUTTONDOWN [ 1 t ] }
459 { WM_NCLBUTTONUP [ 1 f ] }
460 { WM_NCMBUTTONDOWN [ 2 t ] }
461 { WM_NCMBUTTONUP [ 2 f ] }
462 { WM_NCRBUTTONDOWN [ 3 t ] }
463 { WM_NCRBUTTONUP [ 3 f ] }
466 ! If the user clicks in the window border ("non-client area")
467 ! Windows sends us an NC[LMR]BUTTONDOWN message; but if the
468 ! mouse is subsequently released outside the NC area, we receive
469 ! a [LMR]BUTTONUP message and Factor can get confused. So we
470 ! ignore BUTTONUP's that are a result of an NC*BUTTONDOWN.
473 : handle-wm-ncbutton ( hWnd uMsg wParam lParam -- )
475 message>button nc-buttons get
476 swap [ push ] [ remove! drop ] if ;
478 : mouse-scroll ( wParam -- array )
479 >lo-hi [ -80 /f ] map ;
481 : mouse-event>gesture ( uMsg -- button )
482 key-modifiers swap message>button
483 [ <button-down> ] [ <button-up> ] if ;
485 :: prepare-mouse ( hWnd uMsg wParam lParam -- button coordinate world )
486 uMsg mouse-event>gesture
490 : set-capture ( hwnd -- )
494 [ SetCapture drop ] keep
498 : release-capture ( -- )
499 ReleaseCapture win32-error=0/f
502 : handle-app-command ( hWnd uMsg wParam lParam -- )
503 GET_APPCOMMAND_LPARAM
505 { APPCOMMAND_BROWSER_BACKWARD [ pick window left-action send-action ] }
506 { APPCOMMAND_BROWSER_FORWARD [ pick window right-action send-action ] }
510 : handle-wm-buttondown ( hWnd uMsg wParam lParam -- )
513 dup message>button drop nc-buttons get remove! drop
514 ] 2dip prepare-mouse send-button-down ;
516 : handle-wm-buttonup ( hWnd uMsg wParam lParam -- )
517 mouse-captured get [ release-capture ] when
518 pick message>button drop dup nc-buttons get member? [
519 nc-buttons get remove! drop 4drop
521 drop prepare-mouse send-button-up
524 : make-TRACKMOUSEEVENT ( hWnd -- alien )
525 TRACKMOUSEEVENT <struct>
527 TRACKMOUSEEVENT heap-size >>cbSize ;
529 : handle-wm-mousemove ( hWnd uMsg wParam lParam -- )
531 over make-TRACKMOUSEEVENT
535 >lo-hi swap window move-hand fire-motion ;
537 :: handle-wm-mousewheel ( hWnd uMsg wParam lParam -- )
538 wParam mouse-scroll hand-loc get hWnd window send-scroll ;
540 : handle-wm-cancelmode ( hWnd uMsg wParam lParam -- )
541 #! message sent if windows needs application to stop dragging
542 4drop release-capture ;
544 : handle-wm-mouseleave ( hWnd uMsg wParam lParam -- )
545 #! message sent if mouse leaves main application
546 4drop forget-rollover ;
548 : system-background-color ( -- color )
549 COLOR_BTNFACE GetSysColor RGB>color ;
551 : ?make-glass ( world hwnd -- )
552 over window-controls>> textured-background swap member-eq? [
553 composition-enabled? [
554 full-window-margins DwmExtendFrameIntoClientArea drop
555 T{ rgba f 0.0 0.0 0.0 0.0 }
556 ] [ drop system-background-color ] if >>background-color
560 : handle-wm-dwmcompositionchanged ( hWnd uMsg wParam lParam -- )
561 3drop [ window ] keep ?make-glass ;
565 H{ } clone wm-handlers set-global
567 : add-wm-handler ( quot wm -- )
569 [ [ execute( -- wm ) add-wm-handler ] with each ]
570 [ wm-handlers get-global set-at ] if ;
572 : remove-wm-handler ( wm -- )
573 wm-handlers get-global delete-at ;
575 [ handle-wm-close 0 ] WM_CLOSE add-wm-handler
576 [ 4dup handle-wm-paint DefWindowProc ] WM_PAINT add-wm-handler
578 [ handle-wm-size 0 ] WM_SIZE add-wm-handler
579 [ handle-wm-move 0 ] WM_MOVE add-wm-handler
581 [ 4dup handle-wm-keydown DefWindowProc ] { WM_KEYDOWN WM_SYSKEYDOWN } add-wm-handler
582 [ 4dup handle-wm-char DefWindowProc ] { WM_CHAR WM_SYSCHAR } add-wm-handler
583 [ 4dup handle-wm-keyup DefWindowProc ] { WM_KEYUP WM_SYSKEYUP } add-wm-handler
585 [ handle-wm-syscommand ] WM_SYSCOMMAND add-wm-handler
586 [ handle-wm-set-focus 0 ] WM_SETFOCUS add-wm-handler
587 [ handle-wm-kill-focus 0 ] WM_KILLFOCUS add-wm-handler
589 [ handle-app-command 0 ] WM_APPCOMMAND add-wm-handler
591 [ handle-wm-buttondown 0 ] WM_LBUTTONDOWN add-wm-handler
592 [ handle-wm-buttondown 0 ] WM_MBUTTONDOWN add-wm-handler
593 [ handle-wm-buttondown 0 ] WM_RBUTTONDOWN add-wm-handler
594 [ handle-wm-buttonup 0 ] WM_LBUTTONUP add-wm-handler
595 [ handle-wm-buttonup 0 ] WM_MBUTTONUP add-wm-handler
596 [ handle-wm-buttonup 0 ] WM_RBUTTONUP add-wm-handler
597 [ handle-wm-dwmcompositionchanged 0 ] WM_DWMCOMPOSITIONCHANGED add-wm-handler
599 [ 4dup handle-wm-ncbutton DefWindowProc ]
600 { WM_NCLBUTTONDOWN WM_NCMBUTTONDOWN WM_NCRBUTTONDOWN
601 WM_NCLBUTTONUP WM_NCMBUTTONUP WM_NCRBUTTONUP }
604 [ nc-buttons get-global delete-all DefWindowProc ]
605 { WM_EXITSIZEMOVE WM_EXITMENULOOP } add-wm-handler
607 [ handle-wm-mousemove 0 ] WM_MOUSEMOVE add-wm-handler
608 [ handle-wm-mousewheel 0 ] WM_MOUSEWHEEL add-wm-handler
609 [ handle-wm-cancelmode 0 ] WM_CANCELMODE add-wm-handler
610 [ handle-wm-mouseleave 0 ] WM_MOUSELEAVE add-wm-handler
612 SYMBOL: trace-messages?
614 ! return 0 if you handle the message, else just let DefWindowProc return its val
615 : ui-wndproc ( -- object )
616 uint { void* uint long long } stdcall [
618 trace-messages? get-global [ dup windows-message-name name>> print flush ] when
619 wm-handlers get-global at* [ call ] [ drop DefWindowProc ] if
622 : peek-message? ( msg -- ? ) f 0 0 PM_REMOVE PeekMessage zero? ;
624 M: windows-ui-backend do-events
626 dup peek-message? [ drop ui-wait ] [
627 [ TranslateMessage drop ]
628 [ DispatchMessage drop ] bi
631 :: register-window-class ( class-name-ptr -- )
632 WNDCLASSEX <struct> f GetModuleHandle
633 class-name-ptr pick GetClassInfoEx 0 = [
634 WNDCLASSEX heap-size >>cbSize
635 flags{ CS_HREDRAW CS_VREDRAW CS_OWNDC } >>style
636 ui-wndproc >>lpfnWndProc
639 f GetModuleHandle >>hInstance
640 f GetModuleHandle "APPICON" utf16n string>alien LoadIcon >>hIcon
641 f IDC_ARROW LoadCursor >>hCursor
643 class-name-ptr >>lpszClassName
644 RegisterClassEx win32-error=0/f
647 : adjust-RECT ( RECT style ex-style -- )
648 [ 0 ] dip AdjustWindowRectEx win32-error=0/f ;
650 : make-RECT ( world -- RECT )
651 [ window-loc>> ] [ dim>> ] bi <RECT> ;
653 : default-position-RECT ( RECT -- RECT' )
654 dup get-RECT-width/height
655 [ CW_USEDEFAULT + >>right ] dip
656 CW_USEDEFAULT + >>bottom
658 CW_USEDEFAULT >>top ;
660 : make-adjusted-RECT ( rect style ex-style -- RECT )
663 dup get-RECT-top-left [ zero? ] both? swap
666 swap [ default-position-RECT ] when ;
668 : get-window-class ( -- class-name )
670 dup expired? [ drop "Factor-window" utf16n malloc-string ] when
671 dup register-window-class
675 :: create-window ( rect style ex-style -- hwnd )
676 rect style ex-style make-adjusted-RECT
677 [ get-window-class f ] dip
680 WS_CLIPSIBLINGS WS_CLIPCHILDREN bitor style bitor
681 ] dip get-RECT-dimensions
682 f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ;
684 : show-window ( hWnd -- )
685 dup SW_SHOW ShowWindow drop ! always succeeds
686 dup SetForegroundWindow drop
689 : init-win32-ui ( -- )
690 V{ } clone nc-buttons set-global
691 MSG malloc-struct msg-obj set-global
692 GetDoubleClickTime milliseconds double-click-timeout set-global ;
694 : cleanup-win32-ui ( -- )
695 class-name-ptr [ [ [ f UnregisterClass drop ] [ free ] bi ] when* f ] change-global
696 msg-obj [ [ free ] when* f ] change-global ;
698 : get-dc ( world -- )
699 handle>> dup hWnd>> GetDC dup win32-error=0/f >>hDC drop ;
701 : get-rc ( world -- )
702 handle>> dup hDC>> dup wglCreateContext dup win32-error=0/f
703 [ wglMakeCurrent win32-error=0/f ] keep >>hRC drop ;
705 : set-pixel-format ( pixel-format hdc -- )
707 PIXELFORMATDESCRIPTOR <struct> SetPixelFormat win32-error=0/f ;
709 : setup-gl ( world -- )
711 [ swap [ handle>> hDC>> set-pixel-format ] [ get-rc ] bi ]
712 with-world-pixel-format ;
714 : disable-close-button ( hwnd -- )
716 SC_CLOSE MF_BYCOMMAND MF_GRAYED bitor EnableMenuItem drop ;
718 : ?disable-close-button ( world hwnd -- )
719 swap window-controls>> close-button swap member? not
720 [ disable-close-button ] [ drop ] if ;
722 M: windows-ui-backend (open-window) ( world -- )
725 [ ] [ world>style ] [ world>ex-style ] tri create-window
727 [ ?disable-close-button ]
728 [ [ f f ] dip f f <win> >>handle setup-gl ] 2tri
730 [ dup handle>> hWnd>> register-window ]
731 [ handle>> hWnd>> show-window ] tri ;
733 M: win-base select-gl-context ( handle -- )
734 [ hDC>> ] [ hRC>> ] bi wglMakeCurrent win32-error=0/f
737 M: win-base flush-gl-context ( handle -- )
738 hDC>> SwapBuffers win32-error=0/f ;
740 : setup-offscreen-gl ( world -- )
741 dup [ handle>> ] [ dim>> ] bi make-offscreen-dc-and-bitmap
742 [ >>hDC ] [ >>hBitmap ] [ >>bits ] tri* drop [
743 swap [ handle>> hDC>> set-pixel-format ] [ get-rc ] bi
744 ] with-world-pixel-format ;
746 M: windows-ui-backend (open-offscreen-buffer) ( world -- )
747 win-offscreen new >>handle
750 M: windows-ui-backend (close-offscreen-buffer) ( handle -- )
751 [ hDC>> DeleteDC drop ]
752 [ hBitmap>> DeleteObject drop ] bi ;
754 ! Windows 32-bit bitmaps don't actually use the alpha byte of
755 ! each pixel; it's left as zero
757 : (make-opaque) ( byte-array -- byte-array' )
759 [ '[ 255 swap 4 * 3 + _ set-nth ] each ]
762 : (opaque-pixels) ( world -- pixels )
763 [ handle>> bits>> ] [ dim>> ] bi bitmap>byte-array (make-opaque) ;
765 M: windows-ui-backend offscreen-pixels ( world -- alien w h )
766 [ (opaque-pixels) ] [ dim>> first2 ] bi ;
768 M: windows-ui-backend raise-window* ( world -- )
769 handle>> [ hWnd>> SetFocus drop ] when* ;
771 M: windows-ui-backend set-title ( string world -- )
773 dup title>> [ free ] when*
774 swap utf16n malloc-string
776 [ [ hWnd>> WM_SETTEXT 0 ] dip alien-address SendMessage drop ] bi ;
778 M: windows-ui-backend (with-ui)
785 ] [ cleanup-win32-ui ] [ ] cleanup
788 M: windows-ui-backend beep ( -- )
791 M: windows-ui-backend system-alert
792 [ f ] 2dip swap MB_OK MessageBox drop ;
794 : fullscreen-RECT ( hwnd -- RECT )
795 MONITOR_DEFAULTTONEAREST MonitorFromWindow
796 MONITORINFOEX <struct>
797 MONITORINFOEX heap-size >>cbSize
798 [ GetMonitorInfo win32-error=0/f ] keep rcMonitor>> ;
800 : client-area>RECT ( hwnd -- RECT )
802 [ GetClientRect win32-error=0/f ]
803 [ >c-ptr POINT-array-cast [ ClientToScreen drop ] with each ]
806 : hwnd>RECT ( hwnd -- RECT )
807 RECT <struct> [ GetWindowRect win32-error=0/f ] keep ;
809 M: windows-ui-backend (grab-input) ( handle -- )
811 hWnd>> client-area>RECT ClipCursor drop ;
813 M: windows-ui-backend (ungrab-input) ( handle -- )
818 CONSTANT: fullscreen-flags flags{ WS_CAPTION WS_BORDER WS_THICKFRAME }
820 : enter-fullscreen ( world -- )
824 GWL_STYLE GetWindowLong
825 fullscreen-flags unmask
827 [ GWL_STYLE rot SetWindowLong win32-error=0/f ]
830 over hwnd>RECT get-RECT-dimensions
832 SetWindowPos win32-error=0/f
834 [ SW_MAXIMIZE ShowWindow win32-error=0/f ]
837 : exit-fullscreen ( world -- )
838 [ handle>> hWnd>> ] [ world>style ] bi
840 [ [ GWL_STYLE ] dip SetWindowLong win32-error=0/f ]
844 over hwnd>RECT get-RECT-dimensions
845 flags{ SWP_NOMOVE SWP_NOSIZE SWP_NOZORDER SWP_FRAMECHANGED }
846 SetWindowPos win32-error=0/f
848 [ drop SW_RESTORE ShowWindow win32-error=0/f ]
851 M: windows-ui-backend (set-fullscreen) ( ? world -- )
852 [ enter-fullscreen ] [ exit-fullscreen ] if ;
854 M: windows-ui-backend (fullscreen?) ( world -- ? )
856 [ hwnd>RECT ] [ fullscreen-RECT ] bi
857 [ get-RECT-dimensions 2array 2nip ] bi@ = ;
859 windows-ui-backend ui-backend set-global
861 [ "ui.tools" ] main-vocab-hook set-global