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: accessors alien alien.data alien.strings arrays ascii assocs
5 calendar classes classes.struct colors combinators continuations fry
6 io io.crlf io.encodings.utf16 kernel libc literals locals make math
7 math.bitwise namespaces sequences sets specialized-arrays strings
8 threads ui ui.backend ui.clipboards ui.event-loop ui.gadgets
9 ui.gadgets.private ui.gadgets.worlds ui.gestures ui.pixel-formats
10 ui.private windows.dwmapi windows.errors windows.gdi32
11 windows.kernel32 windows.messages windows.offscreen windows.opengl32
12 windows.types windows.user32 assocs.extras byte-arrays
14 FROM: unicode => upper-surrogate? under-surrogate? ;
15 SPECIALIZED-ARRAY: POINT
16 QUALIFIED-WITH: alien.c-types c
17 IN: ui.backend.windows
19 SINGLETON: windows-ui-backend
21 TUPLE: win-base hDC hRC ;
22 TUPLE: win < win-base hWnd world title ;
27 CONSTANT: perm-attribs { $ WGL_SUPPORT_OPENGL_ARB 1 }
29 CONSTANT: attrib-table H{
30 { double-buffered { $ WGL_DOUBLE_BUFFER_ARB 1 } }
31 { stereo { $ WGL_STEREO_ARB 1 } }
32 { offscreen { $ WGL_DRAW_TO_BITMAP_ARB 1 } }
33 { fullscreen { $ WGL_DRAW_TO_WINDOW_ARB 1 } }
34 { windowed { $ WGL_DRAW_TO_WINDOW_ARB 1 } }
35 { accelerated { $ WGL_ACCELERATION_ARB $ WGL_FULL_ACCELERATION_ARB } }
36 { software-rendered { $ WGL_ACCELERATION_ARB $ WGL_NO_ACCELERATION_ARB } }
37 { backing-store { $ WGL_SWAP_METHOD_ARB $ WGL_SWAP_COPY_ARB } }
38 { color-float { $ WGL_TYPE_RGBA_FLOAT_ARB 1 } }
39 { color-bits { $ WGL_COLOR_BITS_ARB } }
40 { red-bits { $ WGL_RED_BITS_ARB } }
41 { green-bits { $ WGL_GREEN_BITS_ARB } }
42 { blue-bits { $ WGL_BLUE_BITS_ARB } }
43 { alpha-bits { $ WGL_ALPHA_BITS_ARB } }
44 { accum-bits { $ WGL_ACCUM_BITS_ARB } }
45 { accum-red-bits { $ WGL_ACCUM_RED_BITS_ARB } }
46 { accum-green-bits { $ WGL_ACCUM_GREEN_BITS_ARB } }
47 { accum-blue-bits { $ WGL_ACCUM_BLUE_BITS_ARB } }
48 { accum-alpha-bits { $ WGL_ACCUM_ALPHA_BITS_ARB } }
49 { depth-bits { $ WGL_DEPTH_BITS_ARB } }
50 { stencil-bits { $ WGL_STENCIL_BITS_ARB } }
51 { aux-buffers { $ WGL_AUX_BUFFERS_ARB } }
52 { sample-buffers { $ WGL_SAMPLE_BUFFERS_ARB } }
53 { samples { $ WGL_SAMPLES_ARB } }
56 : has-wglChoosePixelFormatARB? ( world -- ? )
59 : arb-make-pixel-format ( world attributes -- pf )
60 [ handle>> hDC>> ] dip
61 perm-attribs attrib-table pixel-format-attributes>int-array
63 [ wglChoosePixelFormatARB win32-error=0/f ] with-out-parameters drop ;
65 CONSTANT: pfd-flag-map H{
66 { double-buffered $ PFD_DOUBLEBUFFER }
67 { stereo $ PFD_STEREO }
68 { offscreen $ PFD_DRAW_TO_BITMAP }
69 { fullscreen $ PFD_DRAW_TO_WINDOW }
70 { windowed $ PFD_DRAW_TO_WINDOW }
71 { backing-store $ PFD_SWAP_COPY }
72 { software-rendered $ PFD_GENERIC_FORMAT }
75 : >pfd-flag ( attribute -- value )
76 pfd-flag-map at [ ] [ 0 ] if* ;
78 : >pfd-flags ( attributes -- flags )
79 [ >pfd-flag ] [ bitor ] map-reduce
80 PFD_SUPPORT_OPENGL bitor ;
82 : attr-value ( attributes name -- value )
83 [ instance? ] curry find nip
84 [ value>> ] [ 0 ] if* ;
86 : >pfd ( attributes -- pfd )
87 [ PIXELFORMATDESCRIPTOR <struct> ] dip
89 [ drop PIXELFORMATDESCRIPTOR c:heap-size >>nSize ]
91 [ >pfd-flags >>dwFlags ]
92 [ drop PFD_TYPE_RGBA >>iPixelType ]
93 [ color-bits attr-value >>cColorBits ]
94 [ red-bits attr-value >>cRedBits ]
95 [ green-bits attr-value >>cGreenBits ]
96 [ blue-bits attr-value >>cBlueBits ]
97 [ alpha-bits attr-value >>cAlphaBits ]
98 [ accum-bits attr-value >>cAccumBits ]
99 [ accum-red-bits attr-value >>cAccumRedBits ]
100 [ accum-green-bits attr-value >>cAccumGreenBits ]
101 [ accum-blue-bits attr-value >>cAccumBlueBits ]
102 [ accum-alpha-bits attr-value >>cAccumAlphaBits ]
103 [ depth-bits attr-value >>cDepthBits ]
104 [ stencil-bits attr-value >>cStencilBits ]
105 [ aux-buffers attr-value >>cAuxBuffers ]
106 [ drop PFD_MAIN_PLANE >>dwLayerMask ]
109 : pfd-make-pixel-format ( world attributes -- pf )
110 [ handle>> hDC>> ] [ >pfd ] bi*
111 ChoosePixelFormat dup win32-error=0/f ;
113 M: windows-ui-backend (make-pixel-format)
114 over has-wglChoosePixelFormatARB?
115 [ arb-make-pixel-format ] [ pfd-make-pixel-format ] if ;
117 M: windows-ui-backend (free-pixel-format)
122 : GET_APPCOMMAND_LPARAM ( lParam -- appCommand )
123 hi-word FAPPCOMMAND_MASK lo-word bitnot bitand ; inline
125 : enum-clipboard ( -- seq )
127 [ EnumClipboardFormats win32-error dup dup 0 > ]
131 : with-clipboard ( quot -- )
132 f OpenClipboard win32-error=0/f
134 CloseClipboard win32-error=0/f ; inline
138 CF_UNICODETEXT IsClipboardFormatAvailable zero? [
142 CF_UNICODETEXT GetClipboardData dup win32-error=0/f
143 dup GlobalLock dup win32-error=0/f
144 GlobalUnlock win32-error=0/f
153 EmptyClipboard win32-error=0/f
154 GMEM_MOVEABLE over length 1 + GlobalAlloc
157 dup GlobalLock dup win32-error=0/f
158 rot binary-object memcpy
159 dup GlobalUnlock win32-error=0/f
160 CF_UNICODETEXT swap SetClipboardData win32-error=0/f
164 C: <pasteboard> pasteboard
166 M: pasteboard clipboard-contents drop paste ;
167 M: pasteboard set-clipboard-contents drop copy ;
169 : init-clipboard ( -- )
170 <pasteboard> clipboard set-global
171 <clipboard> selection set-global ;
173 SYMBOLS: msg-obj class-name-ptr mouse-captured ;
175 CONSTANT: window-control>style
178 { textured-background 0 }
179 { minimize-button $ WS_MINIMIZEBOX }
180 { maximize-button $ WS_MAXIMIZEBOX }
181 { resize-handles $ WS_THICKFRAME }
182 { small-title-bar $ WS_CAPTION }
183 { normal-title-bar $ WS_CAPTION }
187 CONSTANT: window-control>ex-style
190 { textured-background 0 }
191 { minimize-button 0 }
192 { maximize-button 0 }
193 { resize-handles $ WS_EX_WINDOWEDGE }
194 { small-title-bar $[ WS_EX_TOOLWINDOW WS_EX_TOPMOST bitor ] }
195 { normal-title-bar $ WS_EX_APPWINDOW }
199 : needs-sysmenu? ( controls -- ? )
200 { close-button minimize-button maximize-button } intersects? ;
202 : has-titlebar? ( controls -- ? )
203 { small-title-bar normal-title-bar } intersects? ;
205 : world>style ( world -- n )
207 [ window-control>style symbols>flags ]
208 [ needs-sysmenu? [ WS_SYSMENU bitor ] when ]
209 [ has-titlebar? [ WS_POPUP bitor ] unless ] tri ;
211 : world>ex-style ( world -- n )
212 window-controls>> window-control>ex-style symbols>flags ;
214 : get-RECT-top-left ( RECT -- x y )
215 [ left>> ] [ top>> ] bi ;
217 : get-RECT-width/height ( RECT -- width height )
218 [ [ right>> ] [ left>> ] bi - ]
219 [ [ bottom>> ] [ top>> ] bi - ] bi ;
221 : get-RECT-dimensions ( RECT -- x y width height )
222 [ get-RECT-top-left ] [ get-RECT-width/height ] bi ;
224 : handle-wm-paint ( hWnd uMsg wParam lParam -- )
225 ! wParam and lParam are unused
226 ! only paint if width/height both > 0
227 3drop window t >>active? relayout-1 yield ;
229 : handle-wm-size ( hWnd uMsg wParam lParam -- )
231 [ lo-word ] keep hi-word 2array
232 dup { 0 0 } = [ 2drop ] [ swap window [ dim<< ] [ drop ] if* ] if ;
234 : handle-wm-move ( hWnd uMsg wParam lParam -- )
236 [ lo-word ] keep hi-word 2array
237 swap window [ window-loc<< ] [ drop ] if* ;
239 CONSTANT: wm-keydown-codes
269 : key-state-down? ( key -- ? )
270 GetKeyState 16 bit? ;
272 : left-shift? ( -- ? ) VK_LSHIFT key-state-down? ;
273 : left-ctrl? ( -- ? ) VK_LCONTROL key-state-down? ;
274 : left-alt? ( -- ? ) VK_LMENU key-state-down? ;
275 : right-shift? ( -- ? ) VK_RSHIFT key-state-down? ;
276 : right-ctrl? ( -- ? ) VK_RCONTROL key-state-down? ;
277 : right-alt? ( -- ? ) VK_RMENU key-state-down? ;
278 : shift? ( -- ? ) left-shift? right-shift? or ;
279 : ctrl? ( -- ? ) left-ctrl? right-ctrl? or ;
280 : alt? ( -- ? ) left-alt? right-alt? or ;
281 : caps-lock? ( -- ? ) VK_CAPITAL GetKeyState zero? not ;
283 : key-modifiers ( -- seq )
288 ] { } make [ empty? not ] keep f ? ;
290 CONSTANT: exclude-keys-wm-keydown
299 CONSTANT: exclude-keys-wm-char
307 : exclude-key-wm-keydown? ( n -- ? )
308 exclude-keys-wm-keydown key? ;
310 : exclude-key-wm-char? ( n -- ? )
311 exclude-keys-wm-char key? ;
313 : keystroke>gesture ( n -- mods sym )
314 wm-keydown-codes at* [ key-modifiers swap ] [ drop f f ] if ;
316 : send-key-gesture ( sym action? quot hWnd -- )
317 [ [ key-modifiers ] 3dip call ] dip
318 window propagate-key-gesture ; inline
320 : send-key-down ( sym action? hWnd -- )
321 [ [ <key-down> ] ] dip send-key-gesture ;
323 : send-key-up ( sym action? hWnd -- )
324 [ [ <key-up> ] ] dip send-key-gesture ;
326 : key-sym ( wParam -- string/f )
327 wm-keydown-codes at ; inline
329 :: (handle-wm-keydown/up) ( hWnd uMsg wParam lParam send-key-down/up -- )
330 wParam exclude-key-wm-keydown? [
332 t hWnd send-key-down/up execute( sym action? hWnd -- )
334 256 <byte-array> :> keyboard-state
335 4 <byte-array> :> chars
336 lParam -16 shift 0xff bitand :> scan-code
337 keyboard-state GetKeyboardState win32-error=0/f
338 VK_CONTROL VK_CAPITAL [ 0 swap keyboard-state set-nth ] bi@
339 wParam scan-code keyboard-state chars 2 0 ToUnicode dup win32-error=0/f
341 1 chars nth 8 shift 0 chars nth bitor
343 3 chars nth 8 shift 2 chars nth bitor ! dead-key
345 1string f hWnd send-key-down/up execute( sym action? hWnd -- )
349 : handle-wm-keydown ( hWnd uMsg wParam lParam -- )
350 \ send-key-down (handle-wm-keydown/up) ;
352 SYMBOL: upper-surrogate-wm-char
354 :: handle-wm-char ( hWnd uMsg wParam lParam -- )
355 wParam exclude-key-wm-char? [
356 ctrl? alt? xor [ ! enable AltGr combination inputs
358 { [ dup upper-surrogate? ] [
359 upper-surrogate-wm-char set-global ]
361 { [ dup under-surrogate? ] [
363 upper-surrogate-wm-char get-global [
365 utf16n encode utf16n decode hWnd window user-input
368 [ 1string hWnd window user-input
369 f upper-surrogate-wm-char set-global ]
374 : handle-wm-keyup ( hWnd uMsg wParam lParam -- )
375 \ send-key-up (handle-wm-keydown/up) ;
377 :: set-window-active ( hwnd uMsg wParam lParam ? -- n )
378 ? hwnd window active?<<
379 hwnd uMsg wParam lParam DefWindowProc ;
381 : handle-wm-syscommand ( hWnd uMsg wParam lParam -- n )
383 { [ over SC_MINIMIZE = ] [ f set-window-active ] }
384 { [ over SC_RESTORE = ] [ t set-window-active ] }
385 { [ over SC_MAXIMIZE = ] [ t set-window-active ] }
386 { [ dup alpha? ] [ 4drop 0 ] }
390 : cleanup-window ( handle -- )
391 dup title>> [ free ] when*
392 dup hRC>> wglDeleteContext win32-error=0/f
393 dup hWnd>> swap hDC>> ReleaseDC win32-error=0/f ;
395 M: windows-ui-backend (close-window)
396 dup hWnd>> unregister-window
398 hWnd>> DestroyWindow win32-error=0/f ;
400 : handle-wm-close ( hWnd uMsg wParam lParam -- )
401 3drop window ungraft ;
403 : handle-wm-set-focus ( hWnd uMsg wParam lParam -- )
404 3drop window [ focus-world ] when* ;
406 : handle-wm-kill-focus ( hWnd uMsg wParam lParam -- )
407 3drop window [ unfocus-world ] when* ;
409 : message>button ( uMsg -- button down? )
411 { WM_LBUTTONDOWN [ 1 t ] }
412 { WM_LBUTTONUP [ 1 f ] }
413 { WM_MBUTTONDOWN [ 2 t ] }
414 { WM_MBUTTONUP [ 2 f ] }
415 { WM_RBUTTONDOWN [ 3 t ] }
416 { WM_RBUTTONUP [ 3 f ] }
418 { WM_NCLBUTTONDOWN [ 1 t ] }
419 { WM_NCLBUTTONUP [ 1 f ] }
420 { WM_NCMBUTTONDOWN [ 2 t ] }
421 { WM_NCMBUTTONUP [ 2 f ] }
422 { WM_NCRBUTTONDOWN [ 3 t ] }
423 { WM_NCRBUTTONUP [ 3 f ] }
426 ! If the user clicks in the window border ("non-client area")
427 ! Windows sends us an NC[LMR]BUTTONDOWN message; but if the
428 ! mouse is subsequently released outside the NC area, we receive
429 ! a [LMR]BUTTONUP message and Factor can get confused. So we
430 ! ignore BUTTONUP's that are a result of an NC*BUTTONDOWN.
433 : handle-wm-ncbutton ( hWnd uMsg wParam lParam -- )
435 message>button nc-buttons get
436 swap [ push ] [ remove! drop ] if ;
438 : mouse-scroll ( wParam -- array )
439 >lo-hi [ -80 /f ] map ;
441 : mouse-event>gesture ( uMsg -- button )
442 key-modifiers swap message>button
443 [ <button-down> ] [ <button-up> ] if ;
445 :: prepare-mouse ( hWnd uMsg wParam lParam -- button coordinate world )
446 uMsg mouse-event>gesture
450 : set-capture ( hwnd -- )
454 [ SetCapture drop ] keep
455 mouse-captured namespaces:set
458 : release-capture ( -- )
459 ReleaseCapture win32-error=0/f
462 : handle-app-command ( hWnd uMsg wParam lParam -- )
463 GET_APPCOMMAND_LPARAM
465 { APPCOMMAND_BROWSER_BACKWARD [ pick window left-action send-action ] }
466 { APPCOMMAND_BROWSER_FORWARD [ pick window right-action send-action ] }
470 : handle-wm-buttondown ( hWnd uMsg wParam lParam -- )
473 dup message>button drop nc-buttons get remove! drop
474 ] 2dip prepare-mouse send-button-down ;
476 : handle-wm-buttonup ( hWnd uMsg wParam lParam -- )
477 mouse-captured get [ release-capture ] when
478 pick message>button drop dup nc-buttons get member? [
479 nc-buttons get remove! 5drop
481 drop prepare-mouse send-button-up
484 : make-TRACKMOUSEEVENT ( hWnd -- alien )
485 TRACKMOUSEEVENT <struct>
487 TRACKMOUSEEVENT c:heap-size >>cbSize ;
489 : handle-wm-mousemove ( hWnd uMsg wParam lParam -- )
491 over make-TRACKMOUSEEVENT
495 >lo-hi swap window move-hand fire-motion ;
497 :: handle-wm-mousewheel ( hWnd uMsg wParam lParam -- )
498 wParam mouse-scroll hand-loc get-global hWnd window send-scroll ;
500 : handle-wm-cancelmode ( hWnd uMsg wParam lParam -- )
501 ! message sent if windows needs application to stop dragging
502 4drop release-capture ;
504 : handle-wm-mouseleave ( hWnd uMsg wParam lParam -- )
505 ! message sent if mouse leaves main application
506 4drop forget-rollover ;
508 : system-background-color ( -- color )
509 COLOR_BTNFACE GetSysColor RGB>color ;
511 : ?make-glass ( world hwnd -- )
512 over window-controls>> textured-background swap member-eq? [
513 composition-enabled? [
514 full-window-margins DwmExtendFrameIntoClientArea drop
515 T{ rgba f 0.0 0.0 0.0 0.0 }
516 ] [ drop system-background-color ] if >>background-color
520 : handle-wm-dwmcompositionchanged ( hWnd uMsg wParam lParam -- )
521 3drop [ window ] keep ?make-glass ;
525 : add-wm-handler ( quot: ( hWnd Msg wParam lParam -- LRESULT ) wm -- )
527 [ [ execute( -- wm ) add-wm-handler ] with each ]
528 [ wm-handlers get-global set-at ] if ;
530 : remove-wm-handler ( wm -- )
531 wm-handlers get-global delete-at ;
535 ${ WM_CLOSE [ handle-wm-close 0 ] }
536 ${ WM_PAINT [ 4dup handle-wm-paint DefWindowProc ] }
538 ${ WM_SIZE [ handle-wm-size 0 ] }
539 ${ WM_MOVE [ handle-wm-move 0 ] }
541 { ${ WM_CHAR WM_SYSCHAR } [ 4dup handle-wm-char DefWindowProc ] }
542 { ${ WM_KEYDOWN WM_SYSKEYDOWN } [ 4dup handle-wm-keydown DefWindowProc ] }
543 { ${ WM_KEYUP WM_SYSKEYUP } [ 4dup handle-wm-keyup DefWindowProc ] }
545 ${ WM_SYSCOMMAND [ handle-wm-syscommand ] }
546 ${ WM_SETFOCUS [ handle-wm-set-focus 0 ] }
547 ${ WM_KILLFOCUS [ handle-wm-kill-focus 0 ] }
549 ${ WM_APPCOMMAND [ handle-app-command 0 ] }
551 { ${ WM_LBUTTONDOWN WM_MBUTTONDOWN WM_RBUTTONDOWN } [ handle-wm-buttondown 0 ] }
552 { ${ WM_LBUTTONUP WM_MBUTTONUP WM_RBUTTONUP } [ handle-wm-buttonup 0 ] }
553 ${ WM_DWMCOMPOSITIONCHANGED [ handle-wm-dwmcompositionchanged 0 ] }
557 WM_NCLBUTTONDOWN WM_NCMBUTTONDOWN WM_NCRBUTTONDOWN
558 WM_NCLBUTTONUP WM_NCMBUTTONUP WM_NCRBUTTONUP
559 } [ 4dup handle-wm-ncbutton DefWindowProc ]
562 { ${ WM_EXITMENULOOP WM_EXITSIZEMOVE } [ nc-buttons get-global delete-all DefWindowProc ] }
564 ${ WM_MOUSEMOVE [ handle-wm-mousemove 0 ] }
565 ${ WM_MOUSEWHEEL [ handle-wm-mousewheel 0 ] }
566 ${ WM_CANCELMODE [ handle-wm-cancelmode 0 ] }
567 ${ WM_MOUSELEAVE [ handle-wm-mouseleave 0 ] }
571 SYMBOL: trace-messages?
573 ! return 0 if you handle the message, else just let DefWindowProc return its val
574 : ui-wndproc ( -- object )
575 c:uint { c:void* c:uint WPARAM LPARAM } stdcall [
576 pick wm-handlers get-global at*
577 [ flush call( hWnd Msg wParam lParam -- result ) ] [ drop DefWindowProc ] if
580 : peek-message? ( msg -- ? ) f 0 0 PM_REMOVE PeekMessage zero? ;
582 M: windows-ui-backend do-events
584 dup peek-message? [ drop ui-wait ] [
585 [ TranslateMessage drop ]
586 [ DispatchMessage drop ] bi
589 :: register-window-class ( class-name-ptr -- )
590 WNDCLASSEX <struct> f GetModuleHandle
591 class-name-ptr pick GetClassInfoEx 0 = [
592 WNDCLASSEX c:heap-size >>cbSize
593 flags{ CS_HREDRAW CS_VREDRAW CS_OWNDC } >>style
594 ui-wndproc >>lpfnWndProc
597 f GetModuleHandle >>hInstance
598 f GetModuleHandle "APPICON" native-string>alien LoadIcon >>hIcon
599 f IDC_ARROW MAKEINTRESOURCE LoadCursor >>hCursor
601 class-name-ptr >>lpszClassName
602 RegisterClassEx win32-error=0/f
605 : adjust-RECT ( RECT style ex-style -- )
606 [ 0 ] dip AdjustWindowRectEx win32-error=0/f ;
608 : make-RECT ( world -- RECT )
609 [ window-loc>> ] [ dim>> ] bi <RECT> ;
611 : default-position-RECT ( RECT -- RECT' )
612 dup get-RECT-width/height
613 [ CW_USEDEFAULT + >>right ] dip
614 CW_USEDEFAULT + >>bottom
616 CW_USEDEFAULT >>top ;
618 : make-adjusted-RECT ( rect style ex-style -- RECT )
621 dup get-RECT-top-left [ zero? ] both? swap
624 swap [ default-position-RECT ] when ;
626 : get-window-class ( -- class-name )
628 dup expired? [ drop "Factor-window" utf16n malloc-string ] when
629 dup register-window-class
633 :: create-window ( rect style ex-style -- hwnd )
634 rect style ex-style make-adjusted-RECT
635 [ get-window-class f ] dip
638 WS_CLIPSIBLINGS WS_CLIPCHILDREN bitor style bitor
639 ] dip get-RECT-dimensions
640 f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ;
642 : show-window ( hWnd -- )
643 dup SW_SHOW ShowWindow drop ! always succeeds
644 dup SetForegroundWindow drop
647 : init-win32-ui ( -- )
648 V{ } clone nc-buttons set-global
649 MSG malloc-struct msg-obj set-global
650 GetDoubleClickTime milliseconds double-click-timeout set-global ;
652 : cleanup-win32-ui ( -- )
653 class-name-ptr [ [ [ f UnregisterClass drop ] [ free ] bi ] when* f ] change-global
654 msg-obj [ [ free ] when* f ] change-global ;
656 : get-dc ( world -- )
657 handle>> dup hWnd>> GetDC dup win32-error=0/f >>hDC drop ;
659 : get-rc ( world -- )
660 handle>> dup hDC>> dup wglCreateContext dup win32-error=0/f
661 [ wglMakeCurrent win32-error=0/f ] keep >>hRC drop ;
663 : set-pixel-format ( pixel-format hdc -- )
665 PIXELFORMATDESCRIPTOR <struct> SetPixelFormat win32-error=0/f ;
667 : setup-gl ( world -- )
669 [ swap [ handle>> hDC>> set-pixel-format ] [ get-rc ] bi ]
670 with-world-pixel-format ;
672 : disable-close-button ( hwnd -- )
674 SC_CLOSE MF_BYCOMMAND MF_GRAYED bitor EnableMenuItem drop ;
676 : ?disable-close-button ( world hwnd -- )
677 swap window-controls>> close-button swap member? not
678 [ disable-close-button ] [ drop ] if ;
680 M: windows-ui-backend (open-window)
683 [ ] [ world>style ] [ world>ex-style ] tri create-window
685 [ ?disable-close-button ]
686 [ [ f f ] dip f f <win> >>handle setup-gl ] 2tri
688 [ dup handle>> hWnd>> register-window ]
689 [ handle>> hWnd>> show-window ] tri ;
691 M: win-base select-gl-context
692 [ hDC>> ] [ hRC>> ] bi wglMakeCurrent win32-error=0/f
695 M: win-base flush-gl-context
696 hDC>> SwapBuffers win32-error=0/f ;
698 ! Windows 32-bit bitmaps don't actually use the alpha byte of
699 ! each pixel; it's left as zero
701 : (make-opaque) ( byte-array -- byte-array' )
703 [ '[ 255 swap 4 * 3 + _ set-nth ] each ]
706 : (opaque-pixels) ( world -- pixels )
707 [ handle>> bits>> ] [ dim>> ] bi bitmap>byte-array (make-opaque) ;
709 M: windows-ui-backend raise-window*
710 handle>> [ hWnd>> SetFocus drop ] when* ;
712 M: windows-ui-backend set-title
714 dup title>> [ free ] when*
715 swap utf16n malloc-string
717 [ [ hWnd>> WM_SETTEXT 0 ] dip alien-address SendMessage drop ] bi ;
719 M: windows-ui-backend (with-ui)
725 ] [ cleanup-win32-ui ] finally ;
727 M: windows-ui-backend beep
730 M: windows-ui-backend system-alert
731 [ f ] 2dip swap MB_OK MessageBox drop ;
733 : fullscreen-RECT ( hwnd -- RECT )
734 MONITOR_DEFAULTTONEAREST MonitorFromWindow
735 MONITORINFOEX <struct>
736 MONITORINFOEX c:heap-size >>cbSize
737 [ GetMonitorInfo win32-error=0/f ] keep rcMonitor>> ;
739 : client-area>RECT ( hwnd -- RECT )
741 [ GetClientRect win32-error=0/f ]
742 [ >c-ptr POINT cast-array [ ClientToScreen drop ] with each ]
745 : hwnd>RECT ( hwnd -- RECT )
746 RECT <struct> [ GetWindowRect win32-error=0/f ] keep ;
748 M: windows-ui-backend (grab-input)
750 hWnd>> client-area>RECT ClipCursor drop ;
752 M: windows-ui-backend (ungrab-input)
757 CONSTANT: fullscreen-flags flags{ WS_CAPTION WS_BORDER WS_THICKFRAME }
759 : enter-fullscreen ( world -- )
763 GWL_STYLE GetWindowLong
764 fullscreen-flags unmask
766 [ GWL_STYLE rot SetWindowLong win32-error=0/f ]
769 over hwnd>RECT get-RECT-dimensions
771 SetWindowPos win32-error=0/f
773 [ SW_MAXIMIZE ShowWindow win32-error=0/f ]
776 : exit-fullscreen ( world -- )
777 [ handle>> hWnd>> ] [ world>style ] bi
779 [ [ GWL_STYLE ] dip SetWindowLong win32-error=0/f ]
783 over hwnd>RECT get-RECT-dimensions
784 flags{ SWP_NOMOVE SWP_NOSIZE SWP_NOZORDER SWP_FRAMECHANGED }
785 SetWindowPos win32-error=0/f
787 [ drop SW_RESTORE ShowWindow win32-error=0/f ]
790 M: windows-ui-backend (set-fullscreen)
791 [ enter-fullscreen ] [ exit-fullscreen ] if ;
793 M: windows-ui-backend (fullscreen?)
795 [ hwnd>RECT ] [ fullscreen-RECT ] bi
796 [ get-RECT-dimensions 2array 2nip ] same? ;
798 M:: windows-ui-backend resize-window ( world dim -- )
799 world handle>> hWnd>>
800 world window-loc>> dim <RECT> dup
803 adjust-RECT get-RECT-dimensions FALSE
804 MoveWindow win32-error=0/f ;
806 M: windows-ui-backend ui-backend-available?
809 windows-ui-backend ui-backend set-global