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 ui.private
5 ui.gadgets ui.gadgets.private ui.backend ui.clipboards
6 ui.gadgets.worlds ui.gestures ui.event-loop io kernel math
7 math.vectors namespaces make sequences strings vectors words
8 windows.kernel32 windows.gdi32 windows.user32 windows.opengl32
9 windows.messages windows.types windows.offscreen windows.nt
10 threads libc combinators fry combinators.short-circuit continuations
11 command-line shuffle opengl ui.render math.bitwise locals
12 accessors math.rectangles math.order calendar ascii sets
13 io.encodings.utf16n windows.errors literals ui.pixel-formats
14 ui.pixel-formats.private memoize classes struct-arrays classes.struct ;
15 IN: ui.backend.windows
17 SINGLETON: windows-ui-backend
19 TUPLE: win-base hDC hRC ;
20 TUPLE: win < win-base hWnd world title ;
21 TUPLE: win-offscreen < win-base hBitmap bits ;
23 C: <win-offscreen> win-offscreen
27 PIXEL-FORMAT-ATTRIBUTE-TABLE: WGL_ARB { $ WGL_SUPPORT_OPENGL_ARB 1 } H{
28 { double-buffered { $ WGL_DOUBLE_BUFFER_ARB 1 } }
29 { stereo { $ WGL_STEREO_ARB 1 } }
30 { offscreen { $ WGL_DRAW_TO_BITMAP_ARB 1 } }
31 { fullscreen { $ WGL_DRAW_TO_WINDOW_ARB 1 } }
32 { windowed { $ WGL_DRAW_TO_WINDOW_ARB 1 } }
33 { accelerated { $ WGL_ACCELERATION_ARB $ WGL_FULL_ACCELERATION_ARB } }
34 { software-rendered { $ WGL_ACCELERATION_ARB $ WGL_NO_ACCELERATION_ARB } }
35 { backing-store { $ WGL_SWAP_METHOD_ARB $ WGL_SWAP_COPY_ARB } }
36 { color-float { $ WGL_TYPE_RGBA_FLOAT_ARB 1 } }
37 { color-bits { $ WGL_COLOR_BITS_ARB } }
38 { red-bits { $ WGL_RED_BITS_ARB } }
39 { green-bits { $ WGL_GREEN_BITS_ARB } }
40 { blue-bits { $ WGL_BLUE_BITS_ARB } }
41 { alpha-bits { $ WGL_ALPHA_BITS_ARB } }
42 { accum-bits { $ WGL_ACCUM_BITS_ARB } }
43 { accum-red-bits { $ WGL_ACCUM_RED_BITS_ARB } }
44 { accum-green-bits { $ WGL_ACCUM_GREEN_BITS_ARB } }
45 { accum-blue-bits { $ WGL_ACCUM_BLUE_BITS_ARB } }
46 { accum-alpha-bits { $ WGL_ACCUM_ALPHA_BITS_ARB } }
47 { depth-bits { $ WGL_DEPTH_BITS_ARB } }
48 { stencil-bits { $ WGL_STENCIL_BITS_ARB } }
49 { aux-buffers { $ WGL_AUX_BUFFERS_ARB } }
50 { sample-buffers { $ WGL_SAMPLE_BUFFERS_ARB } }
51 { samples { $ WGL_SAMPLES_ARB } }
54 : has-wglChoosePixelFormatARB? ( world -- ? )
57 : arb-make-pixel-format ( world attributes -- pf )
58 [ handle>> hDC>> ] dip >WGL_ARB-int-array f 1 0 <int> 0 <int>
59 [ wglChoosePixelFormatARB win32-error=0/f ] 2keep drop *int ;
61 : arb-pixel-format-attribute ( pixel-format attribute -- value )
64 [ [ world>> handle>> hDC>> ] [ handle>> ] bi 0 1 ] dip
66 [ wglGetPixelFormatAttribivARB win32-error=0/f ]
70 CONSTANT: pfd-flag-map H{
71 { double-buffered $ PFD_DOUBLEBUFFER }
72 { stereo $ PFD_STEREO }
73 { offscreen $ PFD_DRAW_TO_BITMAP }
74 { fullscreen $ PFD_DRAW_TO_WINDOW }
75 { windowed $ PFD_DRAW_TO_WINDOW }
76 { backing-store $ PFD_SWAP_COPY }
77 { software-rendered $ PFD_GENERIC_FORMAT }
80 : >pfd-flag ( attribute -- value )
81 pfd-flag-map at [ ] [ 0 ] if* ;
83 : >pfd-flags ( attributes -- flags )
84 [ >pfd-flag ] [ bitor ] map-reduce
85 PFD_SUPPORT_OPENGL bitor ;
87 : attr-value ( attributes name -- value )
88 [ instance? ] curry find nip
89 [ value>> ] [ 0 ] if* ;
91 : >pfd ( attributes -- pfd )
92 [ PIXELFORMATDESCRIPTOR <struct> ] dip
94 [ drop PIXELFORMATDESCRIPTOR heap-size >>nSize ]
96 [ >pfd-flags >>dwFlags ]
97 [ drop PFD_TYPE_RGBA >>iPixelType ]
98 [ color-bits attr-value >>cColorBits ]
99 [ red-bits attr-value >>cRedBits ]
100 [ green-bits attr-value >>cGreenBits ]
101 [ blue-bits attr-value >>cBlueBits ]
102 [ alpha-bits attr-value >>cAlphaBits ]
103 [ accum-bits attr-value >>cAccumBits ]
104 [ accum-red-bits attr-value >>cAccumRedBits ]
105 [ accum-green-bits attr-value >>cAccumGreenBits ]
106 [ accum-blue-bits attr-value >>cAccumBlueBits ]
107 [ accum-alpha-bits attr-value >>cAccumAlphaBits ]
108 [ depth-bits attr-value >>cDepthBits ]
109 [ stencil-bits attr-value >>cStencilBits ]
110 [ aux-buffers attr-value >>cAuxBuffers ]
111 [ drop PFD_MAIN_PLANE >>dwLayerMask ]
114 : pfd-make-pixel-format ( world attributes -- pf )
115 [ handle>> hDC>> ] [ >pfd ] bi*
116 ChoosePixelFormat dup win32-error=0/f ;
118 : get-pfd ( pixel-format -- pfd )
119 [ world>> handle>> hDC>> ] [ handle>> ] bi
120 PIXELFORMATDESCRIPTOR heap-size
121 PIXELFORMATDESCRIPTOR <struct>
122 [ DescribePixelFormat win32-error=0/f ] keep ;
124 : pfd-flag? ( pfd flag -- ? )
125 [ dwFlags>> ] dip bitand c-bool> ;
127 : (pfd-pixel-format-attribute) ( pfd attribute -- value )
129 { double-buffered [ PFD_DOUBLEBUFFER pfd-flag? ] }
130 { stereo [ PFD_STEREO pfd-flag? ] }
131 { offscreen [ PFD_DRAW_TO_BITMAP pfd-flag? ] }
132 { fullscreen [ PFD_DRAW_TO_WINDOW pfd-flag? ] }
133 { windowed [ PFD_DRAW_TO_WINDOW pfd-flag? ] }
134 { software-rendered [ PFD_GENERIC_FORMAT pfd-flag? ] }
135 { color-bits [ cColorBits>> ] }
136 { red-bits [ cRedBits>> ] }
137 { green-bits [ cGreenBits>> ] }
138 { blue-bits [ cBlueBits>> ] }
139 { alpha-bits [ cAlphaBits>> ] }
140 { accum-bits [ cAccumBits>> ] }
141 { accum-red-bits [ cAccumRedBits>> ] }
142 { accum-green-bits [ cAccumGreenBits>> ] }
143 { accum-blue-bits [ cAccumBlueBits>> ] }
144 { accum-alpha-bits [ cAccumAlphaBits>> ] }
145 { depth-bits [ cDepthBits>> ] }
146 { stencil-bits [ cStencilBits>> ] }
147 { aux-buffers [ cAuxBuffers>> ] }
151 : pfd-pixel-format-attribute ( pixel-format attribute -- value )
152 [ get-pfd ] dip (pfd-pixel-format-attribute) ;
154 M: windows-ui-backend (make-pixel-format)
155 over has-wglChoosePixelFormatARB?
156 [ arb-make-pixel-format ] [ pfd-make-pixel-format ] if ;
158 M: windows-ui-backend (free-pixel-format)
161 M: windows-ui-backend (pixel-format-attribute)
162 over world>> has-wglChoosePixelFormatARB?
163 [ arb-pixel-format-attribute ] [ pfd-pixel-format-attribute ] if ;
167 : lo-word ( wparam -- lo ) <short> *short ; inline
168 : hi-word ( wparam -- hi ) -16 shift lo-word ; inline
169 : >lo-hi ( WORD -- array ) [ lo-word ] [ hi-word ] bi 2array ;
171 : crlf>lf ( str -- str' )
172 CHAR: \r swap remove ;
174 : lf>crlf ( str -- str' )
175 [ [ dup CHAR: \n = [ CHAR: \r , ] when , ] each ] "" make ;
177 : enum-clipboard ( -- seq )
179 [ EnumClipboardFormats win32-error dup dup 0 > ]
183 : with-clipboard ( quot -- )
184 f OpenClipboard win32-error=0/f
186 CloseClipboard win32-error=0/f ; inline
190 CF_UNICODETEXT IsClipboardFormatAvailable zero? [
194 CF_UNICODETEXT GetClipboardData dup win32-error=0/f
195 dup GlobalLock dup win32-error=0/f
196 GlobalUnlock win32-error=0/f
205 EmptyClipboard win32-error=0/f
206 GMEM_MOVEABLE over length 1 + GlobalAlloc
209 dup GlobalLock dup win32-error=0/f
210 swapd byte-array>memory
211 dup GlobalUnlock win32-error=0/f
212 CF_UNICODETEXT swap SetClipboardData win32-error=0/f
216 C: <pasteboard> pasteboard
218 M: pasteboard clipboard-contents drop paste ;
219 M: pasteboard set-clipboard-contents drop copy ;
221 : init-clipboard ( -- )
222 <pasteboard> clipboard set-global
223 <clipboard> selection set-global ;
225 SYMBOLS: msg-obj class-name-ptr mouse-captured ;
227 CONSTANT: window-control>style
230 { minimize-button $ WS_MINIMIZEBOX }
231 { maximize-button $ WS_MAXIMIZEBOX }
232 { resize-handles $ WS_THICKFRAME }
233 { small-title-bar $ WS_CAPTION }
234 { normal-title-bar $ WS_CAPTION }
237 CONSTANT: window-control>ex-style
240 { minimize-button 0 }
241 { maximize-button 0 }
242 { resize-handles $ WS_EX_WINDOWEDGE }
243 { small-title-bar $ WS_EX_TOOLWINDOW }
244 { normal-title-bar $ WS_EX_APPWINDOW }
247 : needs-sysmenu? ( controls -- ? )
248 { close-button minimize-button maximize-button } intersects? ;
250 : has-titlebar? ( controls -- ? )
251 { small-title-bar normal-title-bar } intersects? ;
253 : world>style ( world -- n )
255 [ window-control>style symbols>flags ]
256 [ needs-sysmenu? [ WS_SYSMENU bitor ] when ]
257 [ has-titlebar? [ WS_POPUP bitor ] unless ] tri ;
259 : world>ex-style ( world -- n )
260 window-controls>> window-control>ex-style symbols>flags ;
262 : get-RECT-top-left ( RECT -- x y )
263 [ left>> ] [ top>> ] bi ;
265 : get-RECT-width/height ( RECT -- width height )
266 [ [ right>> ] [ left>> ] bi - ]
267 [ [ bottom>> ] [ top>> ] bi - ] bi ;
269 : get-RECT-dimensions ( RECT -- x y width height )
270 [ get-RECT-top-left ] [ get-RECT-width/height ] bi ;
272 : handle-wm-paint ( hWnd uMsg wParam lParam -- )
273 #! wParam and lParam are unused
274 #! only paint if width/height both > 0
275 3drop window relayout-1 yield ;
277 : handle-wm-size ( hWnd uMsg wParam lParam -- )
279 [ lo-word ] keep hi-word 2array
280 dup { 0 0 } = [ 2drop ] [ swap window [ (>>dim) ] [ drop ] if* ] if ;
282 : handle-wm-move ( hWnd uMsg wParam lParam -- )
284 [ lo-word ] keep hi-word 2array
285 swap window [ (>>window-loc) ] [ drop ] if* ;
287 CONSTANT: wm-keydown-codes
317 : key-state-down? ( key -- ? )
318 GetKeyState 16 bit? ;
320 : left-shift? ( -- ? ) VK_LSHIFT key-state-down? ;
321 : left-ctrl? ( -- ? ) VK_LCONTROL key-state-down? ;
322 : left-alt? ( -- ? ) VK_LMENU key-state-down? ;
323 : right-shift? ( -- ? ) VK_RSHIFT key-state-down? ;
324 : right-ctrl? ( -- ? ) VK_RCONTROL key-state-down? ;
325 : right-alt? ( -- ? ) VK_RMENU key-state-down? ;
326 : shift? ( -- ? ) left-shift? right-shift? or ;
327 : ctrl? ( -- ? ) left-ctrl? right-ctrl? or ;
328 : alt? ( -- ? ) left-alt? right-alt? or ;
329 : caps-lock? ( -- ? ) VK_CAPITAL GetKeyState zero? not ;
331 : key-modifiers ( -- seq )
336 ] { } make [ empty? not ] keep f ? ;
338 CONSTANT: exclude-keys-wm-keydown
347 CONSTANT: exclude-keys-wm-char
355 : exclude-key-wm-keydown? ( n -- ? )
356 exclude-keys-wm-keydown key? ;
358 : exclude-key-wm-char? ( n -- ? )
359 exclude-keys-wm-char key? ;
361 : keystroke>gesture ( n -- mods sym )
362 wm-keydown-codes at* [ key-modifiers swap ] [ drop f f ] if ;
364 : send-key-gesture ( sym action? quot hWnd -- )
365 [ [ key-modifiers ] 3dip call ] dip
366 window propagate-key-gesture ; inline
368 : send-key-down ( sym action? hWnd -- )
369 [ [ <key-down> ] ] dip send-key-gesture ;
371 : send-key-up ( sym action? hWnd -- )
372 [ [ <key-up> ] ] dip send-key-gesture ;
374 : key-sym ( wParam -- string/f action? )
378 [ shift? caps-lock? xor [ CHAR: a + CHAR: A - ] unless 1string f ]
380 { [ dup digit? ] [ 1string f ] }
381 [ wm-keydown-codes at t ]
384 :: handle-wm-keydown ( hWnd uMsg wParam lParam -- )
385 wParam exclude-key-wm-keydown? [
386 wParam key-sym over [
387 dup ctrl? alt? xor or [
393 :: handle-wm-char ( hWnd uMsg wParam lParam -- )
394 wParam exclude-key-wm-char? [
397 [ f hWnd send-key-down ]
398 [ hWnd window user-input ] bi
402 :: handle-wm-keyup ( hWnd uMsg wParam lParam -- )
403 wParam exclude-key-wm-keydown? [
404 wParam key-sym over [
409 :: set-window-active ( hwnd uMsg wParam lParam ? -- n )
410 ? hwnd window (>>active?)
411 hwnd uMsg wParam lParam DefWindowProc ;
413 : handle-wm-syscommand ( hWnd uMsg wParam lParam -- n )
415 { [ over SC_MINIMIZE = ] [ f set-window-active ] }
416 { [ over SC_RESTORE = ] [ t set-window-active ] }
417 { [ over SC_MAXIMIZE = ] [ t set-window-active ] }
418 { [ dup alpha? ] [ 4drop 0 ] }
419 { [ t ] [ DefWindowProc ] }
422 : cleanup-window ( handle -- )
423 dup title>> [ free ] when*
424 dup hRC>> wglDeleteContext win32-error=0/f
425 dup hWnd>> swap hDC>> ReleaseDC win32-error=0/f ;
427 M: windows-ui-backend (close-window)
428 dup hWnd>> unregister-window
430 hWnd>> DestroyWindow win32-error=0/f ;
432 : handle-wm-close ( hWnd uMsg wParam lParam -- )
433 3drop window ungraft ;
435 : handle-wm-set-focus ( hWnd uMsg wParam lParam -- )
436 3drop window [ focus-world ] when* ;
438 : handle-wm-kill-focus ( hWnd uMsg wParam lParam -- )
439 3drop window [ unfocus-world ] when* ;
441 : message>button ( uMsg -- button down? )
443 { WM_LBUTTONDOWN [ 1 t ] }
444 { WM_LBUTTONUP [ 1 f ] }
445 { WM_MBUTTONDOWN [ 2 t ] }
446 { WM_MBUTTONUP [ 2 f ] }
447 { WM_RBUTTONDOWN [ 3 t ] }
448 { WM_RBUTTONUP [ 3 f ] }
450 { WM_NCLBUTTONDOWN [ 1 t ] }
451 { WM_NCLBUTTONUP [ 1 f ] }
452 { WM_NCMBUTTONDOWN [ 2 t ] }
453 { WM_NCMBUTTONUP [ 2 f ] }
454 { WM_NCRBUTTONDOWN [ 3 t ] }
455 { WM_NCRBUTTONUP [ 3 f ] }
458 ! If the user clicks in the window border ("non-client area")
459 ! Windows sends us an NC[LMR]BUTTONDOWN message; but if the
460 ! mouse is subsequently released outside the NC area, we receive
461 ! a [LMR]BUTTONUP message and Factor can get confused. So we
462 ! ignore BUTTONUP's that are a result of an NC*BUTTONDOWN.
465 : handle-wm-ncbutton ( hWnd uMsg wParam lParam -- )
467 message>button nc-buttons get
468 swap [ push ] [ delete ] if ;
470 : mouse-wheel ( wParam -- array ) >lo-hi [ sgn neg ] map ;
472 : mouse-event>gesture ( uMsg -- button )
473 key-modifiers swap message>button
474 [ <button-down> ] [ <button-up> ] if ;
476 :: prepare-mouse ( hWnd uMsg wParam lParam -- button coordinate world )
477 uMsg mouse-event>gesture
481 : set-capture ( hwnd -- )
485 [ SetCapture drop ] keep
489 : release-capture ( -- )
490 ReleaseCapture win32-error=0/f
493 : handle-wm-buttondown ( hWnd uMsg wParam lParam -- )
496 dup message>button drop nc-buttons get delete
497 ] 2dip prepare-mouse send-button-down ;
499 : handle-wm-buttonup ( hWnd uMsg wParam lParam -- )
500 mouse-captured get [ release-capture ] when
501 pick message>button drop dup nc-buttons get member? [
502 nc-buttons get delete 4drop
504 drop prepare-mouse send-button-up
507 : make-TRACKMOUSEEVENT ( hWnd -- alien )
508 TRACKMOUSEEVENT <struct>
510 TRACKMOUSEEVENT heap-size >>cbSize ;
512 : handle-wm-mousemove ( hWnd uMsg wParam lParam -- )
514 over make-TRACKMOUSEEVENT
518 >lo-hi swap window move-hand fire-motion ;
520 :: handle-wm-mousewheel ( hWnd uMsg wParam lParam -- )
521 wParam mouse-wheel hand-loc get hWnd window send-wheel ;
523 : handle-wm-cancelmode ( hWnd uMsg wParam lParam -- )
524 #! message sent if windows needs application to stop dragging
525 4drop release-capture ;
527 : handle-wm-mouseleave ( hWnd uMsg wParam lParam -- )
528 #! message sent if mouse leaves main application
529 4drop forget-rollover ;
533 H{ } clone wm-handlers set-global
535 : add-wm-handler ( quot wm -- )
537 [ [ execute( -- wm ) add-wm-handler ] with each ]
538 [ wm-handlers get-global set-at ] if ;
540 [ handle-wm-close 0 ] WM_CLOSE add-wm-handler
541 [ 4dup handle-wm-paint DefWindowProc ] WM_PAINT add-wm-handler
543 [ handle-wm-size 0 ] WM_SIZE add-wm-handler
544 [ handle-wm-move 0 ] WM_MOVE add-wm-handler
546 [ 4dup handle-wm-keydown DefWindowProc ] { WM_KEYDOWN WM_SYSKEYDOWN } add-wm-handler
547 [ 4dup handle-wm-char DefWindowProc ] { WM_CHAR WM_SYSCHAR } add-wm-handler
548 [ 4dup handle-wm-keyup DefWindowProc ] { WM_KEYUP WM_SYSKEYUP } add-wm-handler
550 [ handle-wm-syscommand ] WM_SYSCOMMAND add-wm-handler
551 [ handle-wm-set-focus 0 ] WM_SETFOCUS add-wm-handler
552 [ handle-wm-kill-focus 0 ] WM_KILLFOCUS add-wm-handler
554 [ handle-wm-buttondown 0 ] WM_LBUTTONDOWN add-wm-handler
555 [ handle-wm-buttondown 0 ] WM_MBUTTONDOWN add-wm-handler
556 [ handle-wm-buttondown 0 ] WM_RBUTTONDOWN add-wm-handler
557 [ handle-wm-buttonup 0 ] WM_LBUTTONUP add-wm-handler
558 [ handle-wm-buttonup 0 ] WM_MBUTTONUP add-wm-handler
559 [ handle-wm-buttonup 0 ] WM_RBUTTONUP add-wm-handler
561 [ 4dup handle-wm-ncbutton DefWindowProc ]
562 { WM_NCLBUTTONDOWN WM_NCMBUTTONDOWN WM_NCRBUTTONDOWN
563 WM_NCLBUTTONUP WM_NCMBUTTONUP WM_NCRBUTTONUP }
566 [ nc-buttons get-global delete-all DefWindowProc ]
567 { WM_EXITSIZEMOVE WM_EXITMENULOOP } add-wm-handler
569 [ handle-wm-mousemove 0 ] WM_MOUSEMOVE add-wm-handler
570 [ handle-wm-mousewheel 0 ] WM_MOUSEWHEEL add-wm-handler
571 [ handle-wm-cancelmode 0 ] WM_CANCELMODE add-wm-handler
572 [ handle-wm-mouseleave 0 ] WM_MOUSELEAVE add-wm-handler
574 SYMBOL: trace-messages?
576 ! return 0 if you handle the message, else just let DefWindowProc return its val
577 : ui-wndproc ( -- object )
578 "uint" { "void*" "uint" "long" "long" } "stdcall" [
580 trace-messages? get-global [ dup windows-message-name name>> print flush ] when
581 wm-handlers get-global at* [ call ] [ drop DefWindowProc ] if
584 : peek-message? ( msg -- ? ) f 0 0 PM_REMOVE PeekMessage zero? ;
586 M: windows-ui-backend do-events
588 dup peek-message? [ drop ui-wait ] [
589 [ TranslateMessage drop ]
590 [ DispatchMessage drop ] bi
593 :: register-window-class ( class-name-ptr -- )
594 WNDCLASSEX <struct> f GetModuleHandle
595 class-name-ptr pick GetClassInfoEx 0 = [
596 WNDCLASSEX heap-size >>cbSize
597 { CS_HREDRAW CS_VREDRAW CS_OWNDC } flags >>style
598 ui-wndproc >>lpfnWndProc
601 f GetModuleHandle >>hInstance
602 f GetModuleHandle "fraptor" utf16n string>alien LoadIcon >>hIcon
603 f IDC_ARROW LoadCursor >>hCursor
605 class-name-ptr >>lpszClassName
606 RegisterClassEx win32-error=0/f
609 : adjust-RECT ( RECT style ex-style -- )
610 [ 0 ] dip AdjustWindowRectEx win32-error=0/f ;
612 : make-RECT ( world -- RECT )
613 [ window-loc>> ] [ dim>> ] bi <RECT> ;
615 : default-position-RECT ( RECT -- RECT' )
616 dup get-RECT-width/height
617 [ CW_USEDEFAULT + >>bottom ] dip
618 CW_USEDEFAULT + >>right
620 CW_USEDEFAULT >>top ;
622 : make-adjusted-RECT ( rect style ex-style -- RECT )
625 dup get-RECT-top-left [ zero? ] both? swap
628 swap [ default-position-RECT ] when ;
630 : get-window-class ( -- class-name )
632 dup expired? [ drop "Factor-window" utf16n malloc-string ] when
633 dup register-window-class
637 :: create-window ( rect style ex-style -- hwnd )
638 rect style ex-style make-adjusted-RECT
639 [ get-window-class f ] dip
642 WS_CLIPSIBLINGS WS_CLIPCHILDREN bitor style bitor
643 ] dip get-RECT-dimensions
644 f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ;
646 : show-window ( hWnd -- )
647 dup SW_SHOW ShowWindow drop ! always succeeds
648 dup SetForegroundWindow drop
651 : init-win32-ui ( -- )
652 V{ } clone nc-buttons set-global
653 "MSG" malloc-object msg-obj set-global
654 GetDoubleClickTime milliseconds double-click-timeout set-global ;
656 : cleanup-win32-ui ( -- )
657 class-name-ptr [ [ [ f UnregisterClass drop ] [ free ] bi ] when* f ] change-global
658 msg-obj [ [ free ] when* f ] change-global ;
660 : get-dc ( world -- )
661 handle>> dup hWnd>> GetDC dup win32-error=0/f >>hDC drop ;
663 : get-rc ( world -- )
664 handle>> dup hDC>> dup wglCreateContext dup win32-error=0/f
665 [ wglMakeCurrent win32-error=0/f ] keep >>hRC drop ;
667 : set-pixel-format ( pixel-format hdc -- )
669 PIXELFORMATDESCRIPTOR <struct> SetPixelFormat win32-error=0/f ;
671 : setup-gl ( world -- )
673 [ swap [ handle>> hDC>> set-pixel-format ] [ get-rc ] bi ]
674 with-world-pixel-format ;
676 : disable-close-button ( hwnd -- )
678 SC_CLOSE MF_BYCOMMAND MF_GRAYED bitor EnableMenuItem drop ;
680 : ?disable-close-button ( world hwnd -- )
681 swap window-controls>> close-button swap member? not
682 [ disable-close-button ] [ drop ] if ;
684 M: windows-ui-backend (open-window) ( world -- )
687 [ ] [ world>style ] [ world>ex-style ] tri create-window
688 [ ?disable-close-button ]
689 [ [ f f ] dip f f <win> >>handle setup-gl ] 2bi
691 [ dup handle>> hWnd>> register-window ]
692 [ handle>> hWnd>> show-window ] tri ;
694 M: win-base select-gl-context ( handle -- )
695 [ hDC>> ] [ hRC>> ] bi wglMakeCurrent win32-error=0/f
698 M: win-base flush-gl-context ( handle -- )
699 hDC>> SwapBuffers win32-error=0/f ;
701 : setup-offscreen-gl ( world -- )
702 dup [ handle>> ] [ dim>> ] bi make-offscreen-dc-and-bitmap
703 [ >>hDC ] [ >>hBitmap ] [ >>bits ] tri* drop [
704 swap [ handle>> hDC>> set-pixel-format ] [ get-rc ] bi
705 ] with-world-pixel-format ;
707 M: windows-ui-backend (open-offscreen-buffer) ( world -- )
708 win-offscreen new >>handle
711 M: windows-ui-backend (close-offscreen-buffer) ( handle -- )
712 [ hDC>> DeleteDC drop ]
713 [ hBitmap>> DeleteObject drop ] bi ;
715 ! Windows 32-bit bitmaps don't actually use the alpha byte of
716 ! each pixel; it's left as zero
718 : (make-opaque) ( byte-array -- byte-array' )
720 [ '[ 255 swap 4 * 3 + _ set-nth ] each ]
723 : (opaque-pixels) ( world -- pixels )
724 [ handle>> bits>> ] [ dim>> ] bi bitmap>byte-array (make-opaque) ;
726 M: windows-ui-backend offscreen-pixels ( world -- alien w h )
727 [ (opaque-pixels) ] [ dim>> first2 ] bi ;
729 M: windows-ui-backend raise-window* ( world -- )
730 handle>> [ hWnd>> SetFocus drop ] when* ;
732 M: windows-ui-backend set-title ( string world -- )
734 dup title>> [ free ] when*
735 swap utf16n malloc-string
737 [ [ hWnd>> WM_SETTEXT 0 ] dip alien-address SendMessage drop ] bi ;
739 M: windows-ui-backend (with-ui)
746 ] [ cleanup-win32-ui ] [ ] cleanup
749 M: windows-ui-backend beep ( -- )
752 : fullscreen-RECT ( hwnd -- RECT )
753 MONITOR_DEFAULTTONEAREST MonitorFromWindow
754 MONITORINFOEX <struct>
755 MONITORINFOEX heap-size >>cbSize
756 [ GetMonitorInfo win32-error=0/f ] keep rcMonitor>> ;
758 : client-area>RECT ( hwnd -- RECT )
760 [ GetClientRect win32-error=0/f ]
761 [ "POINT" byte-array>struct-array [ ClientToScreen drop ] with each ]
764 : hwnd>RECT ( hwnd -- RECT )
765 RECT <struct> [ GetWindowRect win32-error=0/f ] keep ;
767 M: windows-ui-backend (grab-input) ( handle -- )
769 hWnd>> client-area>RECT ClipCursor drop ;
771 M: windows-ui-backend (ungrab-input) ( handle -- )
776 : fullscreen-flags ( -- n )
777 { WS_CAPTION WS_BORDER WS_THICKFRAME } flags ; inline
779 : enter-fullscreen ( world -- )
783 GWL_STYLE GetWindowLong
784 fullscreen-flags unmask
786 [ GWL_STYLE rot SetWindowLong win32-error=0/f ]
789 over hwnd>RECT get-RECT-dimensions
791 SetWindowPos win32-error=0/f
793 [ SW_MAXIMIZE ShowWindow win32-error=0/f ]
796 : exit-fullscreen ( world -- )
799 [ GWL_STYLE rot world>style SetWindowLong win32-error=0/f ]
802 over hwnd>RECT get-RECT-dimensions
803 { SWP_NOMOVE SWP_NOSIZE SWP_NOZORDER SWP_FRAMECHANGED } flags
804 SetWindowPos win32-error=0/f
806 [ SW_RESTORE ShowWindow win32-error=0/f ]
809 M: windows-ui-backend (set-fullscreen) ( ? world -- )
810 [ enter-fullscreen ] [ exit-fullscreen ] if ;
812 M: windows-ui-backend (fullscreen?) ( world -- ? )
813 [ handle>> hWnd>> hwnd>RECT ]
814 [ handle>> hWnd>> fullscreen-RECT ] bi
815 [ get-RECT-dimensions 2array 2nip ] bi@ = ;
817 windows-ui-backend ui-backend set-global
819 [ "ui.tools" ] main-vocab-hook set-global