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 ;
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" <c-object>
93 "PIXELFORMATDESCRIPTOR" heap-size over set-PIXELFORMATDESCRIPTOR-nSize
94 1 over set-PIXELFORMATDESCRIPTOR-nVersion
95 over >pfd-flags over set-PIXELFORMATDESCRIPTOR-dwFlags
96 PFD_TYPE_RGBA over set-PIXELFORMATDESCRIPTOR-iPixelType
97 over color-bits attr-value over set-PIXELFORMATDESCRIPTOR-cColorBits
98 over red-bits attr-value over set-PIXELFORMATDESCRIPTOR-cRedBits
99 over green-bits attr-value over set-PIXELFORMATDESCRIPTOR-cGreenBits
100 over blue-bits attr-value over set-PIXELFORMATDESCRIPTOR-cBlueBits
101 over alpha-bits attr-value over set-PIXELFORMATDESCRIPTOR-cAlphaBits
102 over accum-bits attr-value over set-PIXELFORMATDESCRIPTOR-cAccumBits
103 over accum-red-bits attr-value over set-PIXELFORMATDESCRIPTOR-cAccumRedBits
104 over accum-green-bits attr-value over set-PIXELFORMATDESCRIPTOR-cAccumGreenBits
105 over accum-blue-bits attr-value over set-PIXELFORMATDESCRIPTOR-cAccumBlueBits
106 over accum-alpha-bits attr-value over set-PIXELFORMATDESCRIPTOR-cAccumAlphaBits
107 over depth-bits attr-value over set-PIXELFORMATDESCRIPTOR-cDepthBits
108 over stencil-bits attr-value over set-PIXELFORMATDESCRIPTOR-cStencilBits
109 over aux-buffers attr-value over set-PIXELFORMATDESCRIPTOR-cAuxBuffers
110 PFD_MAIN_PLANE over set-PIXELFORMATDESCRIPTOR-dwLayerMask
113 : pfd-make-pixel-format ( world attributes -- pf )
114 [ handle>> hDC>> ] [ >pfd ] bi*
115 ChoosePixelFormat dup win32-error=0/f ;
117 : get-pfd ( pixel-format -- pfd )
118 [ world>> handle>> hDC>> ] [ handle>> ] bi
119 "PIXELFORMATDESCRIPTOR" heap-size
120 "PIXELFORMATDESCRIPTOR" <c-object>
121 [ DescribePixelFormat win32-error=0/f ] keep ;
123 : pfd-flag? ( pfd flag -- ? )
124 [ PIXELFORMATDESCRIPTOR-dwFlags ] dip bitand c-bool> ;
126 : (pfd-pixel-format-attribute) ( pfd attribute -- value )
128 { double-buffered [ PFD_DOUBLEBUFFER pfd-flag? ] }
129 { stereo [ PFD_STEREO pfd-flag? ] }
130 { offscreen [ PFD_DRAW_TO_BITMAP pfd-flag? ] }
131 { fullscreen [ PFD_DRAW_TO_WINDOW pfd-flag? ] }
132 { windowed [ PFD_DRAW_TO_WINDOW pfd-flag? ] }
133 { software-rendered [ PFD_GENERIC_FORMAT pfd-flag? ] }
134 { color-bits [ PIXELFORMATDESCRIPTOR-cColorBits ] }
135 { red-bits [ PIXELFORMATDESCRIPTOR-cRedBits ] }
136 { green-bits [ PIXELFORMATDESCRIPTOR-cGreenBits ] }
137 { blue-bits [ PIXELFORMATDESCRIPTOR-cBlueBits ] }
138 { alpha-bits [ PIXELFORMATDESCRIPTOR-cAlphaBits ] }
139 { accum-bits [ PIXELFORMATDESCRIPTOR-cAccumBits ] }
140 { accum-red-bits [ PIXELFORMATDESCRIPTOR-cAccumRedBits ] }
141 { accum-green-bits [ PIXELFORMATDESCRIPTOR-cAccumGreenBits ] }
142 { accum-blue-bits [ PIXELFORMATDESCRIPTOR-cAccumBlueBits ] }
143 { accum-alpha-bits [ PIXELFORMATDESCRIPTOR-cAccumAlphaBits ] }
144 { depth-bits [ PIXELFORMATDESCRIPTOR-cDepthBits ] }
145 { stencil-bits [ PIXELFORMATDESCRIPTOR-cStencilBits ] }
146 { aux-buffers [ PIXELFORMATDESCRIPTOR-cAuxBuffers ] }
150 : pfd-pixel-format-attribute ( pixel-format attribute -- value )
151 [ get-pfd ] dip (pfd-pixel-format-attribute) ;
153 M: windows-ui-backend (make-pixel-format)
154 over has-wglChoosePixelFormatARB?
155 [ arb-make-pixel-format ] [ pfd-make-pixel-format ] if ;
157 M: windows-ui-backend (free-pixel-format)
160 M: windows-ui-backend (pixel-format-attribute)
161 over world>> has-wglChoosePixelFormatARB?
162 [ arb-pixel-format-attribute ] [ pfd-pixel-format-attribute ] if ;
166 : lo-word ( wparam -- lo ) <short> *short ; inline
167 : hi-word ( wparam -- hi ) -16 shift lo-word ; inline
168 : >lo-hi ( WORD -- array ) [ lo-word ] [ hi-word ] bi 2array ;
170 : crlf>lf ( str -- str' )
171 CHAR: \r swap remove ;
173 : lf>crlf ( str -- str' )
174 [ [ dup CHAR: \n = [ CHAR: \r , ] when , ] each ] "" make ;
176 : enum-clipboard ( -- seq )
178 [ EnumClipboardFormats win32-error dup dup 0 > ]
182 : with-clipboard ( quot -- )
183 f OpenClipboard win32-error=0/f
185 CloseClipboard win32-error=0/f ; inline
189 CF_UNICODETEXT IsClipboardFormatAvailable zero? [
193 CF_UNICODETEXT GetClipboardData dup win32-error=0/f
194 dup GlobalLock dup win32-error=0/f
195 GlobalUnlock win32-error=0/f
204 EmptyClipboard win32-error=0/f
205 GMEM_MOVEABLE over length 1 + GlobalAlloc
208 dup GlobalLock dup win32-error=0/f
209 swapd byte-array>memory
210 dup GlobalUnlock win32-error=0/f
211 CF_UNICODETEXT swap SetClipboardData win32-error=0/f
215 C: <pasteboard> pasteboard
217 M: pasteboard clipboard-contents drop paste ;
218 M: pasteboard set-clipboard-contents drop copy ;
220 : init-clipboard ( -- )
221 <pasteboard> clipboard set-global
222 <clipboard> selection set-global ;
224 SYMBOLS: msg-obj class-name-ptr mouse-captured ;
226 CONSTANT: window-control>style
229 { minimize-button $ WS_MINIMIZEBOX }
230 { maximize-button $ WS_MAXIMIZEBOX }
231 { resize-handles $ WS_THICKFRAME }
232 { small-title-bar $ WS_CAPTION }
233 { normal-title-bar $ WS_CAPTION }
236 CONSTANT: window-control>ex-style
239 { minimize-button 0 }
240 { maximize-button 0 }
241 { resize-handles $ WS_EX_WINDOWEDGE }
242 { small-title-bar $ WS_EX_TOOLWINDOW }
243 { normal-title-bar $ WS_EX_APPWINDOW }
246 : needs-sysmenu? ( controls -- ? )
247 { close-button minimize-button maximize-button } intersects? ;
249 : has-titlebar? ( controls -- ? )
250 { small-title-bar normal-title-bar } intersects? ;
252 : world>style ( world -- n )
254 [ window-control>style symbols>flags ]
255 [ needs-sysmenu? [ WS_SYSMENU bitor ] when ]
256 [ has-titlebar? [ WS_POPUP bitor ] unless ] tri ;
258 : world>ex-style ( world -- n )
259 window-controls>> window-control>ex-style symbols>flags ;
261 : get-RECT-top-left ( RECT -- x y )
262 [ RECT-left ] keep RECT-top ;
264 : get-RECT-dimensions ( RECT -- x y width height )
265 [ get-RECT-top-left ] keep
266 [ RECT-right ] keep [ RECT-left - ] keep
267 [ RECT-bottom ] keep RECT-top - ;
269 : handle-wm-paint ( hWnd uMsg wParam lParam -- )
270 #! wParam and lParam are unused
271 #! only paint if width/height both > 0
272 3drop window relayout-1 yield ;
274 : handle-wm-size ( hWnd uMsg wParam lParam -- )
276 [ lo-word ] keep hi-word 2array
277 dup { 0 0 } = [ 2drop ] [ swap window [ (>>dim) ] [ drop ] if* ] if ;
279 : handle-wm-move ( hWnd uMsg wParam lParam -- )
281 [ lo-word ] keep hi-word 2array
282 swap window [ (>>window-loc) ] [ drop ] if* ;
284 CONSTANT: wm-keydown-codes
314 : key-state-down? ( key -- ? )
315 GetKeyState 16 bit? ;
317 : left-shift? ( -- ? ) VK_LSHIFT key-state-down? ;
318 : left-ctrl? ( -- ? ) VK_LCONTROL key-state-down? ;
319 : left-alt? ( -- ? ) VK_LMENU key-state-down? ;
320 : right-shift? ( -- ? ) VK_RSHIFT key-state-down? ;
321 : right-ctrl? ( -- ? ) VK_RCONTROL key-state-down? ;
322 : right-alt? ( -- ? ) VK_RMENU key-state-down? ;
323 : shift? ( -- ? ) left-shift? right-shift? or ;
324 : ctrl? ( -- ? ) left-ctrl? right-ctrl? or ;
325 : alt? ( -- ? ) left-alt? right-alt? or ;
326 : caps-lock? ( -- ? ) VK_CAPITAL GetKeyState zero? not ;
328 : key-modifiers ( -- seq )
333 ] { } make [ empty? not ] keep f ? ;
335 CONSTANT: exclude-keys-wm-keydown
344 CONSTANT: exclude-keys-wm-char
352 : exclude-key-wm-keydown? ( n -- ? )
353 exclude-keys-wm-keydown key? ;
355 : exclude-key-wm-char? ( n -- ? )
356 exclude-keys-wm-char key? ;
358 : keystroke>gesture ( n -- mods sym )
359 wm-keydown-codes at* [ key-modifiers swap ] [ drop f f ] if ;
361 : send-key-gesture ( sym action? quot hWnd -- )
362 [ [ key-modifiers ] 3dip call ] dip
363 window propagate-key-gesture ; inline
365 : send-key-down ( sym action? hWnd -- )
366 [ [ <key-down> ] ] dip send-key-gesture ;
368 : send-key-up ( sym action? hWnd -- )
369 [ [ <key-up> ] ] dip send-key-gesture ;
371 : key-sym ( wParam -- string/f action? )
375 [ shift? caps-lock? xor [ CHAR: a + CHAR: A - ] unless 1string f ]
377 { [ dup digit? ] [ 1string f ] }
378 [ wm-keydown-codes at t ]
381 :: handle-wm-keydown ( hWnd uMsg wParam lParam -- )
382 wParam exclude-key-wm-keydown? [
383 wParam key-sym over [
384 dup ctrl? alt? xor or [
390 :: handle-wm-char ( hWnd uMsg wParam lParam -- )
391 wParam exclude-key-wm-char? [
394 [ f hWnd send-key-down ]
395 [ hWnd window user-input ] bi
399 :: handle-wm-keyup ( hWnd uMsg wParam lParam -- )
400 wParam exclude-key-wm-keydown? [
401 wParam key-sym over [
406 :: set-window-active ( hwnd uMsg wParam lParam ? -- n )
407 ? hwnd window (>>active?)
408 hwnd uMsg wParam lParam DefWindowProc ;
410 : handle-wm-syscommand ( hWnd uMsg wParam lParam -- n )
412 { [ over SC_MINIMIZE = ] [ f set-window-active ] }
413 { [ over SC_RESTORE = ] [ t set-window-active ] }
414 { [ over SC_MAXIMIZE = ] [ t set-window-active ] }
415 { [ dup alpha? ] [ 4drop 0 ] }
416 { [ t ] [ DefWindowProc ] }
419 : cleanup-window ( handle -- )
420 dup title>> [ free ] when*
421 dup hRC>> wglDeleteContext win32-error=0/f
422 dup hWnd>> swap hDC>> ReleaseDC win32-error=0/f ;
424 M: windows-ui-backend (close-window)
425 dup hWnd>> unregister-window
427 hWnd>> DestroyWindow win32-error=0/f ;
429 : handle-wm-close ( hWnd uMsg wParam lParam -- )
430 3drop window ungraft ;
432 : handle-wm-set-focus ( hWnd uMsg wParam lParam -- )
433 3drop window [ focus-world ] when* ;
435 : handle-wm-kill-focus ( hWnd uMsg wParam lParam -- )
436 3drop window [ unfocus-world ] when* ;
438 : message>button ( uMsg -- button down? )
440 { WM_LBUTTONDOWN [ 1 t ] }
441 { WM_LBUTTONUP [ 1 f ] }
442 { WM_MBUTTONDOWN [ 2 t ] }
443 { WM_MBUTTONUP [ 2 f ] }
444 { WM_RBUTTONDOWN [ 3 t ] }
445 { WM_RBUTTONUP [ 3 f ] }
447 { WM_NCLBUTTONDOWN [ 1 t ] }
448 { WM_NCLBUTTONUP [ 1 f ] }
449 { WM_NCMBUTTONDOWN [ 2 t ] }
450 { WM_NCMBUTTONUP [ 2 f ] }
451 { WM_NCRBUTTONDOWN [ 3 t ] }
452 { WM_NCRBUTTONUP [ 3 f ] }
455 ! If the user clicks in the window border ("non-client area")
456 ! Windows sends us an NC[LMR]BUTTONDOWN message; but if the
457 ! mouse is subsequently released outside the NC area, we receive
458 ! a [LMR]BUTTONUP message and Factor can get confused. So we
459 ! ignore BUTTONUP's that are a result of an NC*BUTTONDOWN.
462 : handle-wm-ncbutton ( hWnd uMsg wParam lParam -- )
464 message>button nc-buttons get
465 swap [ push ] [ delete ] if ;
467 : mouse-wheel ( wParam -- array ) >lo-hi [ sgn neg ] map ;
469 : mouse-event>gesture ( uMsg -- button )
470 key-modifiers swap message>button
471 [ <button-down> ] [ <button-up> ] if ;
473 :: prepare-mouse ( hWnd uMsg wParam lParam -- button coordinate world )
474 uMsg mouse-event>gesture
478 : set-capture ( hwnd -- )
482 [ SetCapture drop ] keep
486 : release-capture ( -- )
487 ReleaseCapture win32-error=0/f
490 : handle-wm-buttondown ( hWnd uMsg wParam lParam -- )
493 dup message>button drop nc-buttons get delete
494 ] 2dip prepare-mouse send-button-down ;
496 : handle-wm-buttonup ( hWnd uMsg wParam lParam -- )
497 mouse-captured get [ release-capture ] when
498 pick message>button drop dup nc-buttons get member? [
499 nc-buttons get delete 4drop
501 drop prepare-mouse send-button-up
504 : make-TRACKMOUSEEVENT ( hWnd -- alien )
505 "TRACKMOUSEEVENT" <c-object> [ set-TRACKMOUSEEVENT-hwndTrack ] keep
506 "TRACKMOUSEEVENT" heap-size over set-TRACKMOUSEEVENT-cbSize ;
508 : handle-wm-mousemove ( hWnd uMsg wParam lParam -- )
510 over make-TRACKMOUSEEVENT
511 TME_LEAVE over set-TRACKMOUSEEVENT-dwFlags
512 0 over set-TRACKMOUSEEVENT-dwHoverTime
514 >lo-hi swap window move-hand fire-motion ;
516 :: handle-wm-mousewheel ( hWnd uMsg wParam lParam -- )
517 wParam mouse-wheel hand-loc get hWnd window send-wheel ;
519 : handle-wm-cancelmode ( hWnd uMsg wParam lParam -- )
520 #! message sent if windows needs application to stop dragging
521 4drop release-capture ;
523 : handle-wm-mouseleave ( hWnd uMsg wParam lParam -- )
524 #! message sent if mouse leaves main application
525 4drop forget-rollover ;
529 H{ } clone wm-handlers set-global
531 : add-wm-handler ( quot wm -- )
533 [ [ execute( -- wm ) add-wm-handler ] with each ]
534 [ wm-handlers get-global set-at ] if ;
536 [ handle-wm-close 0 ] WM_CLOSE add-wm-handler
537 [ 4dup handle-wm-paint DefWindowProc ] WM_PAINT add-wm-handler
539 [ handle-wm-size 0 ] WM_SIZE add-wm-handler
540 [ handle-wm-move 0 ] WM_MOVE add-wm-handler
542 [ 4dup handle-wm-keydown DefWindowProc ] { WM_KEYDOWN WM_SYSKEYDOWN } add-wm-handler
543 [ 4dup handle-wm-char DefWindowProc ] { WM_CHAR WM_SYSCHAR } add-wm-handler
544 [ 4dup handle-wm-keyup DefWindowProc ] { WM_KEYUP WM_SYSKEYUP } add-wm-handler
546 [ handle-wm-syscommand ] WM_SYSCOMMAND add-wm-handler
547 [ handle-wm-set-focus 0 ] WM_SETFOCUS add-wm-handler
548 [ handle-wm-kill-focus 0 ] WM_KILLFOCUS add-wm-handler
550 [ handle-wm-buttondown 0 ] WM_LBUTTONDOWN add-wm-handler
551 [ handle-wm-buttondown 0 ] WM_MBUTTONDOWN add-wm-handler
552 [ handle-wm-buttondown 0 ] WM_RBUTTONDOWN add-wm-handler
553 [ handle-wm-buttonup 0 ] WM_LBUTTONUP add-wm-handler
554 [ handle-wm-buttonup 0 ] WM_MBUTTONUP add-wm-handler
555 [ handle-wm-buttonup 0 ] WM_RBUTTONUP add-wm-handler
557 [ 4dup handle-wm-ncbutton DefWindowProc ]
558 { WM_NCLBUTTONDOWN WM_NCMBUTTONDOWN WM_NCRBUTTONDOWN
559 WM_NCLBUTTONUP WM_NCMBUTTONUP WM_NCRBUTTONUP }
562 [ nc-buttons get-global delete-all DefWindowProc ]
563 { WM_EXITSIZEMOVE WM_EXITMENULOOP } add-wm-handler
565 [ handle-wm-mousemove 0 ] WM_MOUSEMOVE add-wm-handler
566 [ handle-wm-mousewheel 0 ] WM_MOUSEWHEEL add-wm-handler
567 [ handle-wm-cancelmode 0 ] WM_CANCELMODE add-wm-handler
568 [ handle-wm-mouseleave 0 ] WM_MOUSELEAVE add-wm-handler
570 SYMBOL: trace-messages?
572 ! return 0 if you handle the message, else just let DefWindowProc return its val
573 : ui-wndproc ( -- object )
574 "uint" { "void*" "uint" "long" "long" } "stdcall" [
576 trace-messages? get-global [ dup windows-message-name name>> print flush ] when
577 wm-handlers get-global at* [ call ] [ 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" <c-object> f GetModuleHandle
591 class-name-ptr pick GetClassInfoEx 0 = [
592 "WNDCLASSEX" heap-size over set-WNDCLASSEX-cbSize
593 { CS_HREDRAW CS_VREDRAW CS_OWNDC } flags over set-WNDCLASSEX-style
594 ui-wndproc over set-WNDCLASSEX-lpfnWndProc
595 0 over set-WNDCLASSEX-cbClsExtra
596 0 over set-WNDCLASSEX-cbWndExtra
597 f GetModuleHandle over set-WNDCLASSEX-hInstance
598 f GetModuleHandle "fraptor" utf16n string>alien LoadIcon
599 over set-WNDCLASSEX-hIcon
600 f IDC_ARROW LoadCursor over set-WNDCLASSEX-hCursor
602 class-name-ptr over set-WNDCLASSEX-lpszClassName
603 RegisterClassEx win32-error=0/f
606 : adjust-RECT ( RECT style ex-style -- )
607 [ 0 ] dip AdjustWindowRectEx win32-error=0/f ;
609 : make-RECT ( world -- RECT )
610 [ window-loc>> ] [ dim>> ] bi <RECT> ;
612 : default-position-RECT ( RECT -- )
613 dup get-RECT-dimensions [ 2drop ] 2dip
614 CW_USEDEFAULT + pick set-RECT-bottom
615 CW_USEDEFAULT + over set-RECT-right
616 CW_USEDEFAULT over set-RECT-left
617 CW_USEDEFAULT swap set-RECT-top ;
619 : make-adjusted-RECT ( rect style ex-style -- RECT )
622 dup get-RECT-top-left [ zero? ] both? swap
625 swap [ dup default-position-RECT ] when ;
627 : get-window-class ( -- class-name )
629 dup expired? [ drop "Factor-window" utf16n malloc-string ] when
630 dup register-window-class
634 :: create-window ( rect style ex-style -- hwnd )
635 rect style ex-style make-adjusted-RECT
636 [ get-window-class f ] dip
639 WS_CLIPSIBLINGS WS_CLIPCHILDREN bitor style bitor
640 ] dip get-RECT-dimensions
641 f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ;
643 : show-window ( hWnd -- )
644 dup SW_SHOW ShowWindow drop ! always succeeds
645 dup SetForegroundWindow drop
648 : init-win32-ui ( -- )
649 V{ } clone nc-buttons set-global
650 "MSG" malloc-object msg-obj set-global
651 GetDoubleClickTime milliseconds double-click-timeout set-global ;
653 : cleanup-win32-ui ( -- )
654 class-name-ptr [ [ [ f UnregisterClass drop ] [ free ] bi ] when* f ] change-global
655 msg-obj [ [ free ] when* f ] change-global ;
657 : get-dc ( world -- )
658 handle>> dup hWnd>> GetDC dup win32-error=0/f >>hDC drop ;
660 : get-rc ( world -- )
661 handle>> dup hDC>> dup wglCreateContext dup win32-error=0/f
662 [ wglMakeCurrent win32-error=0/f ] keep >>hRC drop ;
664 : set-pixel-format ( pixel-format hdc -- )
666 "PIXELFORMATDESCRIPTOR" <c-object> SetPixelFormat win32-error=0/f ;
668 : setup-gl ( world -- )
670 [ swap [ handle>> hDC>> set-pixel-format ] [ get-rc ] bi ]
671 with-world-pixel-format ;
673 : disable-close-button ( hwnd -- )
675 SC_CLOSE MF_BYCOMMAND MF_GRAYED bitor EnableMenuItem drop ;
677 : ?disable-close-button ( world hwnd -- )
678 swap window-controls>> close-button swap member? not
679 [ disable-close-button ] [ drop ] if ;
681 M: windows-ui-backend (open-window) ( world -- )
684 [ ] [ world>style ] [ world>ex-style ] tri create-window
685 [ ?disable-close-button ]
686 [ [ f f ] dip f f <win> >>handle setup-gl ] 2bi
688 [ dup handle>> hWnd>> register-window ]
689 [ handle>> hWnd>> show-window ] tri ;
691 M: win-base select-gl-context ( handle -- )
692 [ hDC>> ] [ hRC>> ] bi wglMakeCurrent win32-error=0/f
695 M: win-base flush-gl-context ( handle -- )
696 hDC>> SwapBuffers win32-error=0/f ;
698 : setup-offscreen-gl ( world -- )
699 dup [ handle>> ] [ dim>> ] bi make-offscreen-dc-and-bitmap
700 [ >>hDC ] [ >>hBitmap ] [ >>bits ] tri* drop [
701 swap [ handle>> hDC>> set-pixel-format ] [ get-rc ] bi
702 ] with-world-pixel-format ;
704 M: windows-ui-backend (open-offscreen-buffer) ( world -- )
705 win-offscreen new >>handle
708 M: windows-ui-backend (close-offscreen-buffer) ( handle -- )
709 [ hDC>> DeleteDC drop ]
710 [ hBitmap>> DeleteObject drop ] bi ;
712 ! Windows 32-bit bitmaps don't actually use the alpha byte of
713 ! each pixel; it's left as zero
715 : (make-opaque) ( byte-array -- byte-array' )
717 [ '[ 255 swap 4 * 3 + _ set-nth ] each ]
720 : (opaque-pixels) ( world -- pixels )
721 [ handle>> bits>> ] [ dim>> ] bi bitmap>byte-array (make-opaque) ;
723 M: windows-ui-backend offscreen-pixels ( world -- alien w h )
724 [ (opaque-pixels) ] [ dim>> first2 ] bi ;
726 M: windows-ui-backend raise-window* ( world -- )
727 handle>> [ hWnd>> SetFocus drop ] when* ;
729 M: windows-ui-backend set-title ( string world -- )
731 dup title>> [ free ] when*
732 swap utf16n malloc-string
734 [ [ hWnd>> WM_SETTEXT 0 ] dip alien-address SendMessage drop ] bi ;
736 M: windows-ui-backend (with-ui)
743 ] [ cleanup-win32-ui ] [ ] cleanup
746 M: windows-ui-backend beep ( -- )
749 : fullscreen-RECT ( hwnd -- RECT )
750 MONITOR_DEFAULTTONEAREST MonitorFromWindow
751 "MONITORINFOEX" <c-object> dup length over set-MONITORINFOEX-cbSize
752 [ GetMonitorInfo win32-error=0/f ] keep MONITORINFOEX-rcMonitor ;
754 : client-area>RECT ( hwnd -- RECT )
756 [ GetClientRect win32-error=0/f ]
757 [ "POINT" byte-array>struct-array [ ClientToScreen drop ] with each ]
760 : hwnd>RECT ( hwnd -- RECT )
761 "RECT" <c-object> [ GetWindowRect win32-error=0/f ] keep ;
763 M: windows-ui-backend (grab-input) ( handle -- )
765 hWnd>> client-area>RECT ClipCursor drop ;
767 M: windows-ui-backend (ungrab-input) ( handle -- )
772 : fullscreen-flags ( -- n )
773 { WS_CAPTION WS_BORDER WS_THICKFRAME } flags ; inline
775 : enter-fullscreen ( world -- )
779 GWL_STYLE GetWindowLong
780 fullscreen-flags unmask
782 [ GWL_STYLE rot SetWindowLong win32-error=0/f ]
785 over hwnd>RECT get-RECT-dimensions
787 SetWindowPos win32-error=0/f
789 [ SW_MAXIMIZE ShowWindow win32-error=0/f ]
792 : exit-fullscreen ( world -- )
795 [ GWL_STYLE rot world>style SetWindowLong win32-error=0/f ]
798 over hwnd>RECT get-RECT-dimensions
799 { SWP_NOMOVE SWP_NOSIZE SWP_NOZORDER SWP_FRAMECHANGED } flags
800 SetWindowPos win32-error=0/f
802 [ SW_RESTORE ShowWindow win32-error=0/f ]
805 M: windows-ui-backend (set-fullscreen) ( ? world -- )
806 [ enter-fullscreen ] [ exit-fullscreen ] if ;
808 M: windows-ui-backend (fullscreen?) ( world -- ? )
809 [ handle>> hWnd>> hwnd>RECT ]
810 [ handle>> hWnd>> fullscreen-RECT ] bi
811 [ get-RECT-dimensions 2array 2nip ] bi@ = ;
813 windows-ui-backend ui-backend set-global
815 [ "ui.tools" ] main-vocab-hook set-global