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.c-types alien.data alien.strings
5 arrays ascii assocs assocs.extras byte-arrays calendar classes
6 classes.struct colors combinators continuations io io.crlf
7 io.encodings.string io.encodings.utf16n io.encodings.utf8 kernel
8 libc literals make math math.bitwise namespaces sequences sets
9 specialized-arrays strings threads ui ui.backend ui.clipboards
10 ui.event-loop ui.gadgets ui.gadgets.private ui.gadgets.worlds
11 ui.gestures ui.pixel-formats ui.private windows.dwmapi
12 windows.errors windows.gdi32 windows.kernel32 windows.messages
13 windows.offscreen windows.ole32 windows.opengl32 windows.shell32
14 windows.types windows.user32 ;
15 FROM: unicode => upper-surrogate? under-surrogate? ;
16 SPECIALIZED-ARRAY: POINT
17 QUALIFIED-WITH: alien.c-types c
18 IN: ui.backend.windows
20 SINGLETON: windows-ui-backend
22 TUPLE: win-base hDC hRC ;
23 TUPLE: win < win-base hWnd world title ;
28 CONSTANT: perm-attribs { $ WGL_SUPPORT_OPENGL_ARB 1 }
30 CONSTANT: attrib-table 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 } }
57 : has-wglChoosePixelFormatARB? ( world -- ? )
60 : arb-make-pixel-format ( world attributes -- pf )
61 [ handle>> hDC>> ] dip
62 perm-attribs attrib-table pixel-format-attributes>int-array
64 [ wglChoosePixelFormatARB win32-error=0/f ] with-out-parameters drop ;
66 CONSTANT: pfd-flag-map H{
67 { double-buffered $ PFD_DOUBLEBUFFER }
68 { stereo $ PFD_STEREO }
69 { offscreen $ PFD_DRAW_TO_BITMAP }
70 { fullscreen $ PFD_DRAW_TO_WINDOW }
71 { windowed $ PFD_DRAW_TO_WINDOW }
72 { backing-store $ PFD_SWAP_COPY }
73 { software-rendered $ PFD_GENERIC_FORMAT }
76 : >pfd-flag ( attribute -- value )
77 pfd-flag-map at [ ] [ 0 ] if* ;
79 : >pfd-flags ( attributes -- flags )
80 [ >pfd-flag ] [ bitor ] map-reduce
81 PFD_SUPPORT_OPENGL bitor ;
83 : attr-value ( attributes name -- value )
84 [ instance? ] curry find nip
85 [ value>> ] [ 0 ] if* ;
87 : >pfd ( attributes -- pfd )
88 [ PIXELFORMATDESCRIPTOR new ] dip
90 [ drop PIXELFORMATDESCRIPTOR c:heap-size >>nSize ]
92 [ >pfd-flags >>dwFlags ]
93 [ drop PFD_TYPE_RGBA >>iPixelType ]
94 [ color-bits attr-value >>cColorBits ]
95 [ red-bits attr-value >>cRedBits ]
96 [ green-bits attr-value >>cGreenBits ]
97 [ blue-bits attr-value >>cBlueBits ]
98 [ alpha-bits attr-value >>cAlphaBits ]
99 [ accum-bits attr-value >>cAccumBits ]
100 [ accum-red-bits attr-value >>cAccumRedBits ]
101 [ accum-green-bits attr-value >>cAccumGreenBits ]
102 [ accum-blue-bits attr-value >>cAccumBlueBits ]
103 [ accum-alpha-bits attr-value >>cAccumAlphaBits ]
104 [ depth-bits attr-value >>cDepthBits ]
105 [ stencil-bits attr-value >>cStencilBits ]
106 [ aux-buffers attr-value >>cAuxBuffers ]
107 [ drop PFD_MAIN_PLANE >>dwLayerMask ]
110 : pfd-make-pixel-format ( world attributes -- pf )
111 [ handle>> hDC>> ] [ >pfd ] bi*
112 ChoosePixelFormat dup win32-error=0/f ;
114 M: windows-ui-backend (make-pixel-format)
115 over has-wglChoosePixelFormatARB?
116 [ arb-make-pixel-format ] [ pfd-make-pixel-format ] if ;
118 M: windows-ui-backend (free-pixel-format)
123 : GET_APPCOMMAND_LPARAM ( lParam -- appCommand )
124 hi-word FAPPCOMMAND_MASK lo-word bitnot bitand ; inline
126 : enum-clipboard ( -- seq )
128 [ EnumClipboardFormats win32-error dup dup 0 > ]
132 : with-clipboard ( quot -- )
133 f OpenClipboard win32-error=0/f
135 CloseClipboard win32-error=0/f ; inline
139 CF_UNICODETEXT IsClipboardFormatAvailable zero? [
143 CF_UNICODETEXT GetClipboardData dup win32-error=0/f
144 dup GlobalLock dup win32-error=0/f
145 GlobalUnlock win32-error=0/f
154 EmptyClipboard win32-error=0/f
155 GMEM_MOVEABLE over length 1 + GlobalAlloc
158 dup GlobalLock dup win32-error=0/f
159 rot binary-object memcpy
160 dup GlobalUnlock win32-error=0/f
161 CF_UNICODETEXT swap SetClipboardData win32-error=0/f
165 C: <pasteboard> pasteboard
167 M: pasteboard clipboard-contents drop paste ;
168 M: pasteboard set-clipboard-contents drop copy ;
170 : init-clipboard ( -- )
171 <pasteboard> clipboard set-global
172 <clipboard> selection set-global ;
174 SYMBOLS: msg-obj class-name-ptr mouse-captured ;
176 CONSTANT: window-control>style
179 { textured-background 0 }
180 { minimize-button $ WS_MINIMIZEBOX }
181 { maximize-button $ WS_MAXIMIZEBOX }
182 { resize-handles $ WS_THICKFRAME }
183 { small-title-bar $ WS_CAPTION }
184 { normal-title-bar $ WS_CAPTION }
188 CONSTANT: window-control>ex-style
191 { textured-background 0 }
192 { minimize-button 0 }
193 { maximize-button 0 }
194 { resize-handles $ WS_EX_WINDOWEDGE }
195 { small-title-bar flags{ WS_EX_TOOLWINDOW WS_EX_TOPMOST } }
196 { normal-title-bar $ WS_EX_APPWINDOW }
200 : needs-sysmenu? ( controls -- ? )
201 { close-button minimize-button maximize-button } intersects? ;
203 : has-titlebar? ( controls -- ? )
204 { small-title-bar normal-title-bar } intersects? ;
206 : world>style ( world -- n )
208 [ window-control>style symbols>flags ]
209 [ needs-sysmenu? [ WS_SYSMENU bitor ] when ]
210 [ has-titlebar? [ WS_POPUP bitor ] unless ] tri ;
212 : world>ex-style ( world -- n )
213 window-controls>> window-control>ex-style symbols>flags ;
215 : get-RECT-top-left ( RECT -- x y )
216 [ left>> ] [ top>> ] bi ;
218 : get-RECT-width/height ( RECT -- width height )
219 [ [ right>> ] [ left>> ] bi - ]
220 [ [ bottom>> ] [ top>> ] bi - ] bi ;
222 : get-RECT-dimensions ( RECT -- x y width height )
223 [ get-RECT-top-left ] [ get-RECT-width/height ] bi ;
225 : handle-wm-paint ( hWnd uMsg wParam lParam -- )
226 ! wParam and lParam are unused
227 ! only paint if width/height both > 0
228 3drop window t >>active? relayout-1 yield ;
230 : handle-wm-size ( hWnd uMsg wParam lParam -- )
232 [ lo-word ] keep hi-word 2array
233 dup { 0 0 } = [ 2drop ] [ swap window [ dim<< ] [ drop ] if* ] if ;
235 : handle-wm-move ( hWnd uMsg wParam lParam -- )
237 [ lo-word ] keep hi-word 2array
238 swap window [ window-loc<< ] [ drop ] if* ;
240 CONSTANT: wm-keydown-codes
270 : key-state-down? ( key -- ? )
271 GetKeyState 16 bit? ;
273 : left-shift? ( -- ? ) VK_LSHIFT key-state-down? ;
274 : left-ctrl? ( -- ? ) VK_LCONTROL key-state-down? ;
275 : left-alt? ( -- ? ) VK_LMENU key-state-down? ;
276 : right-shift? ( -- ? ) VK_RSHIFT key-state-down? ;
277 : right-ctrl? ( -- ? ) VK_RCONTROL key-state-down? ;
278 : right-alt? ( -- ? ) VK_RMENU key-state-down? ;
279 : shift? ( -- ? ) left-shift? right-shift? or ;
280 : ctrl? ( -- ? ) left-ctrl? right-ctrl? or ;
281 : alt? ( -- ? ) left-alt? right-alt? or ;
282 : caps-lock? ( -- ? ) VK_CAPITAL GetKeyState zero? not ;
284 : key-modifiers ( -- seq )
289 ] { } make [ empty? not ] keep f ? ;
291 CONSTANT: exclude-keys-wm-keydown
300 CONSTANT: exclude-keys-wm-char
308 : exclude-key-wm-keydown? ( n -- ? )
309 exclude-keys-wm-keydown key? ;
311 : exclude-key-wm-char? ( n -- ? )
312 exclude-keys-wm-char key? ;
314 : keystroke>gesture ( n -- mods sym )
315 wm-keydown-codes at* [ key-modifiers swap ] [ drop f f ] if ;
317 : send-key-gesture ( sym action? quot hWnd -- )
318 [ [ key-modifiers ] 3dip call ] dip
319 window propagate-key-gesture ; inline
321 : send-key-down ( sym action? hWnd -- )
322 [ [ <key-down> ] ] dip send-key-gesture ;
324 : send-key-up ( sym action? hWnd -- )
325 [ [ <key-up> ] ] dip send-key-gesture ;
327 : key-sym ( wParam -- string/f )
328 wm-keydown-codes at ; inline
330 :: (handle-wm-keydown/up) ( hWnd uMsg wParam lParam send-key-down/up -- )
331 wParam exclude-key-wm-keydown? [
333 t hWnd send-key-down/up execute( sym action? hWnd -- )
335 256 <byte-array> :> keyboard-state
336 4 <byte-array> :> chars
337 lParam -16 shift 0xff bitand :> scan-code
338 keyboard-state GetKeyboardState win32-error=0/f
339 VK_CONTROL VK_CAPITAL [ 0 swap keyboard-state set-nth ] bi@
340 wParam scan-code keyboard-state chars 2 0 ToUnicode
342 1 chars nth 8 shift 0 chars nth bitor
344 3 chars nth 8 shift 2 chars nth bitor ! dead-key
346 1string f hWnd send-key-down/up execute( sym action? hWnd -- )
350 : handle-wm-keydown ( hWnd uMsg wParam lParam -- )
351 \ send-key-down (handle-wm-keydown/up) ;
353 SYMBOL: upper-surrogate-wm-char
355 :: handle-wm-char ( hWnd uMsg wParam lParam -- )
356 wParam exclude-key-wm-char? [
357 ctrl? alt? xor [ ! enable AltGr combination inputs
359 { [ dup upper-surrogate? ] [
360 upper-surrogate-wm-char set-global ]
362 { [ dup under-surrogate? ] [
364 upper-surrogate-wm-char get-global [
366 utf16n encode utf16n decode hWnd window user-input
369 [ 1string hWnd window user-input
370 f upper-surrogate-wm-char set-global ]
375 : handle-wm-keyup ( hWnd uMsg wParam lParam -- )
376 \ send-key-up (handle-wm-keydown/up) ;
378 :: set-window-active ( hwnd uMsg wParam lParam ? -- n )
379 ? hwnd window active?<<
380 hwnd uMsg wParam lParam DefWindowProc ;
382 : handle-wm-syscommand ( hWnd uMsg wParam lParam -- n )
384 { [ over SC_MINIMIZE = ] [ f set-window-active ] }
385 { [ over SC_RESTORE = ] [ t set-window-active ] }
386 { [ over SC_MAXIMIZE = ] [ t set-window-active ] }
387 { [ dup alpha? ] [ 4drop 0 ] }
391 : cleanup-window ( handle -- )
392 dup title>> [ free ] when*
393 dup hRC>> wglDeleteContext win32-error=0/f
394 dup hWnd>> swap hDC>> ReleaseDC win32-error=0/f ;
396 M: windows-ui-backend (close-window)
397 dup hWnd>> unregister-window
399 hWnd>> DestroyWindow win32-error=0/f ;
401 : handle-wm-close ( hWnd uMsg wParam lParam -- )
402 3drop window ungraft ;
404 : handle-wm-set-focus ( hWnd uMsg wParam lParam -- )
405 3drop window [ focus-world ] when* ;
407 : handle-wm-kill-focus ( hWnd uMsg wParam lParam -- )
408 3drop window [ unfocus-world ] when* ;
410 : message>button ( uMsg -- button down? )
412 { WM_LBUTTONDOWN [ 1 t ] }
413 { WM_LBUTTONUP [ 1 f ] }
414 { WM_MBUTTONDOWN [ 2 t ] }
415 { WM_MBUTTONUP [ 2 f ] }
416 { WM_RBUTTONDOWN [ 3 t ] }
417 { WM_RBUTTONUP [ 3 f ] }
419 { WM_NCLBUTTONDOWN [ 1 t ] }
420 { WM_NCLBUTTONUP [ 1 f ] }
421 { WM_NCMBUTTONDOWN [ 2 t ] }
422 { WM_NCMBUTTONUP [ 2 f ] }
423 { WM_NCRBUTTONDOWN [ 3 t ] }
424 { WM_NCRBUTTONUP [ 3 f ] }
427 ! If the user clicks in the window border ("non-client area")
428 ! Windows sends us an NC[LMR]BUTTONDOWN message; but if the
429 ! mouse is subsequently released outside the NC area, we receive
430 ! a [LMR]BUTTONUP message and Factor can get confused. So we
431 ! ignore BUTTONUP's that are a result of an NC*BUTTONDOWN.
434 : handle-wm-ncbutton ( hWnd uMsg wParam lParam -- )
436 message>button nc-buttons get
437 swap [ push ] [ remove! drop ] if ;
439 : mouse-scroll ( wParam -- array )
440 >lo-hi [ -80 /f ] map ;
442 : mouse-event>gesture ( uMsg -- button )
443 key-modifiers swap message>button
444 [ <button-down> ] [ <button-up> ] if ;
446 :: prepare-mouse ( hWnd uMsg wParam lParam -- button coordinate world )
447 uMsg mouse-event>gesture
451 : set-capture ( hwnd -- )
455 [ SetCapture drop ] keep
456 mouse-captured namespaces:set
459 : release-capture ( -- )
460 ReleaseCapture win32-error=0/f
463 : handle-app-command ( hWnd uMsg wParam lParam -- )
464 GET_APPCOMMAND_LPARAM
466 { APPCOMMAND_BROWSER_BACKWARD [ pick window left-action send-action ] }
467 { APPCOMMAND_BROWSER_FORWARD [ pick window right-action send-action ] }
471 : handle-wm-buttondown ( hWnd uMsg wParam lParam -- )
474 dup message>button drop nc-buttons get remove! drop
475 ] 2dip prepare-mouse send-button-down ;
477 : handle-wm-buttonup ( hWnd uMsg wParam lParam -- )
478 mouse-captured get [ release-capture ] when
479 pick message>button drop dup nc-buttons get member? [
480 nc-buttons get remove! 5drop
482 drop prepare-mouse send-button-up
485 : make-TRACKMOUSEEVENT ( hWnd -- alien )
488 TRACKMOUSEEVENT c:heap-size >>cbSize ;
490 : handle-wm-mousemove ( hWnd uMsg wParam lParam -- )
492 over make-TRACKMOUSEEVENT
495 TrackMouseEvent win32-error=0/f
496 >lo-hi swap window move-hand fire-motion ;
498 :: handle-wm-mousewheel ( hWnd uMsg wParam lParam -- )
499 wParam mouse-scroll hand-loc get-global hWnd window send-scroll ;
501 : handle-wm-cancelmode ( hWnd uMsg wParam lParam -- )
502 ! message sent if windows needs application to stop dragging
503 4drop release-capture ;
505 : handle-wm-mouseleave ( hWnd uMsg wParam lParam -- )
506 ! message sent if mouse leaves main application
507 4drop forget-rollover ;
509 : system-background-color ( -- color )
510 COLOR_BTNFACE GetSysColor RGB>color ;
512 : ?make-glass ( world hwnd -- )
513 over window-controls>> textured-background swap member-eq? [
514 composition-enabled? [
515 full-window-margins DwmExtendFrameIntoClientArea check-hresult
516 T{ rgba f 0.0 0.0 0.0 0.0 }
517 ] [ drop system-background-color ] if >>background-color
521 : handle-wm-dwmcompositionchanged ( hWnd uMsg wParam lParam -- )
522 3drop [ window ] keep ?make-glass ;
526 : add-wm-handler ( quot: ( hWnd Msg wParam lParam -- LRESULT ) wm -- )
528 [ [ execute( -- wm ) add-wm-handler ] with each ]
529 [ wm-handlers get-global set-at ] if ;
531 : remove-wm-handler ( wm -- )
532 wm-handlers get-global delete-at ;
536 ${ WM_CLOSE [ handle-wm-close 0 ] }
537 ! ${ WM_NCCREATE [ [ 3drop EnableNonClientDpiScaling drop ] [ DefWindowProc ] 4bi ] }
538 ! ${ WM_GETDPISCALEDSIZE [ DefWindowProc ] }
539 ! ${ WM_DPICHANGED [ DefWindowProc ] }
540 ${ WM_PAINT [ 4dup handle-wm-paint DefWindowProc ] }
542 ${ WM_SIZE [ handle-wm-size 0 ] }
543 ${ WM_MOVE [ handle-wm-move 0 ] }
545 { ${ WM_CHAR WM_SYSCHAR } [ 4dup handle-wm-char DefWindowProc ] }
546 { ${ WM_KEYDOWN WM_SYSKEYDOWN } [ 4dup handle-wm-keydown DefWindowProc ] }
547 { ${ WM_KEYUP WM_SYSKEYUP } [ 4dup handle-wm-keyup DefWindowProc ] }
549 ${ WM_SYSCOMMAND [ handle-wm-syscommand ] }
550 ${ WM_SETFOCUS [ handle-wm-set-focus 0 ] }
551 ${ WM_KILLFOCUS [ handle-wm-kill-focus 0 ] }
553 ${ WM_APPCOMMAND [ handle-app-command 0 ] }
555 { ${ WM_LBUTTONDOWN WM_MBUTTONDOWN WM_RBUTTONDOWN } [ handle-wm-buttondown 0 ] }
556 { ${ WM_LBUTTONUP WM_MBUTTONUP WM_RBUTTONUP } [ handle-wm-buttonup 0 ] }
557 ${ WM_DWMCOMPOSITIONCHANGED [ handle-wm-dwmcompositionchanged 0 ] }
561 WM_NCLBUTTONDOWN WM_NCMBUTTONDOWN WM_NCRBUTTONDOWN
562 WM_NCLBUTTONUP WM_NCMBUTTONUP WM_NCRBUTTONUP
563 } [ 4dup handle-wm-ncbutton DefWindowProc ]
566 { ${ WM_EXITMENULOOP WM_EXITSIZEMOVE } [ nc-buttons get-global delete-all DefWindowProc ] }
568 ${ WM_MOUSEMOVE [ handle-wm-mousemove 0 ] }
569 ${ WM_MOUSEWHEEL [ handle-wm-mousewheel 0 ] }
570 ${ WM_CANCELMODE [ handle-wm-cancelmode 0 ] }
571 ${ WM_MOUSELEAVE [ handle-wm-mouseleave 0 ] }
575 SYMBOL: trace-messages?
577 ! return 0 if you handle the message, else just let DefWindowProc return its val
578 : ui-wndproc ( -- object )
579 c:uint { c:void* c:uint WPARAM LPARAM } stdcall [
580 pick wm-handlers get-global at*
581 [ flush call( hWnd Msg wParam lParam -- result ) ] [ 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 new f GetModuleHandle
595 class-name-ptr pick GetClassInfoEx 0 = [
596 WNDCLASSEX c:heap-size >>cbSize
597 flags{ CS_HREDRAW CS_VREDRAW CS_OWNDC } >>style
598 ui-wndproc >>lpfnWndProc
601 f GetModuleHandle >>hInstance
602 f GetModuleHandle "APPICON" native-string>alien LoadIcon >>hIcon
603 f IDC_ARROW MAKEINTRESOURCE LoadCursor >>hCursor
605 class-name-ptr >>lpszClassName
606 RegisterClassEx win32-error=0/f
609 : adjust-RECT ( RECT style ex-style -- )
610 ! [ 0 ] dip GetDpiForSystem AdjustWindowRectExForDpi win32-error=0/f ;
611 [ 0 ] dip AdjustWindowRectEx win32-error=0/f ;
613 : make-RECT ( world -- RECT )
614 [ window-loc>> ] [ dim>> ] bi <RECT> ;
616 : default-position-RECT ( RECT -- RECT' )
617 dup get-RECT-width/height
618 [ CW_USEDEFAULT + >>right ] dip
619 CW_USEDEFAULT + >>bottom
621 CW_USEDEFAULT >>top ;
623 : make-adjusted-RECT ( rect style ex-style -- RECT )
626 dup get-RECT-top-left [ zero? ] both? swap
629 swap [ default-position-RECT ] when ;
631 : get-window-class ( -- class-name )
633 dup expired? [ drop "Factor-window" utf16n malloc-string ] when
634 dup register-window-class
638 : get-device-caps ( handle -- x y )
640 [ LOGPIXELSX GetDeviceCaps ]
641 [ LOGPIXELSY GetDeviceCaps ] bi ;
643 : get-default-device-caps ( -- x y )
646 :: create-window ( rect style ex-style -- hwnd )
647 rect style ex-style make-adjusted-RECT
648 [ get-window-class f ] dip
651 flags{ WS_CLIPSIBLINGS WS_CLIPCHILDREN } style bitor
652 ] dip get-RECT-dimensions
653 f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ;
655 : show-window ( hWnd -- )
656 dup SW_SHOW ShowWindow drop ! always succeeds
657 dup SetForegroundWindow drop
660 : init-win32-ui ( -- )
661 V{ } clone nc-buttons set-global
662 MSG malloc-struct msg-obj set-global
663 GetDoubleClickTime milliseconds double-click-timeout set-global ;
665 : cleanup-win32-ui ( -- )
666 class-name-ptr [ [ [ f UnregisterClass drop ] [ free ] bi ] when* f ] change-global
667 msg-obj [ [ free ] when* f ] change-global ;
669 : get-dc ( world -- )
670 handle>> dup hWnd>> GetDC dup win32-error=0/f >>hDC drop ;
672 : get-rc ( world -- )
673 handle>> dup hDC>> dup wglCreateContext dup win32-error=0/f
674 [ wglMakeCurrent win32-error=0/f ] keep >>hRC drop ;
676 : set-pixel-format ( pixel-format hdc -- )
678 PIXELFORMATDESCRIPTOR new SetPixelFormat win32-error=0/f ;
680 : setup-gl ( world -- )
682 [ swap [ handle>> hDC>> set-pixel-format ] [ get-rc ] bi ]
683 with-world-pixel-format ;
685 : disable-close-button ( hwnd -- )
687 SC_CLOSE MF_BYCOMMAND MF_GRAYED bitor EnableMenuItem drop ;
689 : ?disable-close-button ( world hwnd -- )
690 swap window-controls>> close-button swap member? not
691 [ disable-close-button ] [ drop ] if ;
693 M: windows-ui-backend (open-window)
696 [ ] [ world>style ] [ world>ex-style ] tri create-window
698 [ ?disable-close-button ]
699 [ [ f f ] dip f f <win> >>handle setup-gl ] 2tri
701 [ dup handle>> hWnd>> register-window ]
702 [ handle>> hWnd>> show-window ] tri ;
704 ! https://github.com/factor/factor/issues/2173
705 ! ignore timeout (error 258)
706 M: win-base select-gl-context
707 [ hDC>> ] [ hRC>> ] bi wglMakeCurrent win32-error=0/f-ignore-timeout
708 GdiFlush win32-error=0/f ;
710 M: win-base flush-gl-context
711 hDC>> SwapBuffers win32-error=0/f ;
713 ! Windows 32-bit bitmaps don't actually use the alpha byte of
714 ! each pixel; it's left as zero
716 : (make-opaque) ( byte-array -- byte-array' )
718 [ '[ 255 swap 4 * 3 + _ set-nth ] each ]
721 : (opaque-pixels) ( world -- pixels )
722 [ handle>> bits>> ] [ dim>> ] bi bitmap>byte-array (make-opaque) ;
724 M: windows-ui-backend raise-window*
725 handle>> [ hWnd>> SetFocus drop ] when* ;
727 M: windows-ui-backend set-title
729 dup title>> [ free ] when*
730 swap utf16n malloc-string
732 [ [ hWnd>> WM_SETTEXT 0 ] dip alien-address SendMessage drop ] bi ;
734 M: windows-ui-backend (with-ui)
740 ] [ cleanup-win32-ui ] finally ;
742 M: windows-ui-backend beep
745 M: windows-ui-backend system-alert
746 [ f ] 2dip swap MB_OK MessageBox drop ;
748 : fullscreen-RECT ( hwnd -- RECT )
749 MONITOR_DEFAULTTONEAREST MonitorFromWindow
751 MONITORINFOEX c:heap-size >>cbSize
752 [ GetMonitorInfo win32-error=0/f ] keep rcMonitor>> ;
754 : client-area>RECT ( hwnd -- RECT )
756 [ GetClientRect win32-error=0/f ]
757 [ >c-ptr POINT cast-array [ ClientToScreen drop ] with each ]
760 : hwnd>RECT ( hwnd -- RECT )
761 RECT new [ GetWindowRect win32-error=0/f ] keep ;
763 M: windows-ui-backend (grab-input)
765 hWnd>> client-area>RECT ClipCursor drop ;
767 M: windows-ui-backend (ungrab-input)
772 CONSTANT: fullscreen-flags flags{ WS_CAPTION WS_BORDER WS_THICKFRAME }
774 : enter-fullscreen ( world -- )
778 GWL_STYLE GetWindowLong
779 fullscreen-flags unmask
781 [ GWL_STYLE rot SetWindowLong win32-error=0/f ]
784 over hwnd>RECT get-RECT-dimensions
786 SetWindowPos win32-error=0/f
788 [ SW_MAXIMIZE ShowWindow win32-error=0/f ]
791 : exit-fullscreen ( world -- )
792 [ handle>> hWnd>> ] [ world>style ] bi
794 [ [ GWL_STYLE ] dip SetWindowLong win32-error=0/f ]
798 over hwnd>RECT get-RECT-dimensions
799 flags{ SWP_NOMOVE SWP_NOSIZE SWP_NOZORDER SWP_FRAMECHANGED }
800 SetWindowPos win32-error=0/f
802 [ drop SW_RESTORE ShowWindow win32-error=0/f ]
805 : ensure-null-terminated ( str -- str' )
806 dup ?last 0 = [ "\0" append ] unless ; inline
808 : add-tray-icon ( title -- )
811 NOTIFYICONDATA heap-size >>cbSize
812 NOTIFYICON_VERSION_4 over timeout-version>> uVersion<<
813 NIF_TIP NIF_ICON bitor >>uFlags
814 world get handle>> hWnd>> >>hWnd
815 f GetModuleHandle "APPICON" native-string>alien LoadIcon >>hIcon
816 rot ensure-null-terminated utf8 encode >>szTip
817 Shell_NotifyIcon win32-error=0/f ;
819 : remove-tray-icon ( -- )
822 NOTIFYICONDATA heap-size >>cbSize
823 world get handle>> hWnd>> >>hWnd
824 Shell_NotifyIcon win32-error=0/f ;
826 M: windows-ui-backend (set-fullscreen)
827 [ enter-fullscreen ] [ exit-fullscreen ] if ;
829 M: windows-ui-backend (fullscreen?)
831 [ hwnd>RECT ] [ fullscreen-RECT ] bi
832 [ get-RECT-dimensions 2array 2nip ] same? ;
834 M:: windows-ui-backend resize-window ( world dim -- )
835 world handle>> hWnd>>
836 world window-loc>> dim <RECT> dup
839 adjust-RECT get-RECT-dimensions FALSE
840 MoveWindow win32-error=0/f ;
842 M: windows-ui-backend ui-backend-available?
845 windows-ui-backend ui-backend set-global