1 ! Copyright (C) 2005, 2006 Doug Coleman.
2 ! Portions copyright (C) 2007, 2008 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.gadgets ui.backend ui.clipboards ui.gadgets.worlds
6 ui.gestures ui.event-loop io kernel math math.vectors namespaces
7 make sequences strings vectors words windows.kernel32
8 windows.gdi32 windows.user32 windows.opengl32 windows.messages
9 windows.types windows.nt windows threads libc combinators fry
10 combinators.short-circuit continuations command-line shuffle
11 opengl ui.render ascii math.bitwise locals accessors
12 math.geometry.rect math.order ascii calendar io.encodings.utf16n
16 SINGLETON: windows-ui-backend
18 : crlf>lf ( str -- str' )
19 CHAR: \r swap remove ;
21 : lf>crlf ( str -- str' )
22 [ [ dup CHAR: \n = [ CHAR: \r , ] when , ] each ] "" make ;
24 : enum-clipboard ( -- seq )
26 [ EnumClipboardFormats win32-error dup dup 0 > ]
31 : with-clipboard ( quot -- )
32 f OpenClipboard win32-error=0/f
34 CloseClipboard win32-error=0/f ; inline
38 CF_UNICODETEXT IsClipboardFormatAvailable zero? [
42 CF_UNICODETEXT GetClipboardData dup win32-error=0/f
43 dup GlobalLock dup win32-error=0/f
44 GlobalUnlock win32-error=0/f
53 EmptyClipboard win32-error=0/f
54 GMEM_MOVEABLE over length 1+ GlobalAlloc
57 dup GlobalLock dup win32-error=0/f
58 swapd byte-array>memory
59 dup GlobalUnlock win32-error=0/f
60 CF_UNICODETEXT swap SetClipboardData win32-error=0/f
64 C: <pasteboard> pasteboard
66 M: pasteboard clipboard-contents drop paste ;
67 M: pasteboard set-clipboard-contents drop copy ;
69 : init-clipboard ( -- )
70 <pasteboard> clipboard set-global
71 <clipboard> selection set-global ;
73 TUPLE: win-base hDC hRC ;
74 TUPLE: win < win-base hWnd world title ;
75 TUPLE: win-offscreen < win-base hBitmap bits ;
77 C: <win-offscreen> win-offscreen
79 SYMBOLS: msg-obj class-name-ptr mouse-captured ;
81 : style ( -- n ) WS_OVERLAPPEDWINDOW ; inline
82 : ex-style ( -- n ) WS_EX_APPWINDOW WS_EX_WINDOWEDGE bitor ; inline
84 : get-RECT-top-left ( RECT -- x y )
85 [ RECT-left ] keep RECT-top ;
87 : get-RECT-dimensions ( RECT -- x y width height )
88 [ get-RECT-top-left ] keep
89 [ RECT-right ] keep [ RECT-left - ] keep
90 [ RECT-bottom ] keep RECT-top - ;
92 : handle-wm-paint ( hWnd uMsg wParam lParam -- )
93 #! wParam and lParam are unused
94 #! only paint if width/height both > 0
95 3drop window relayout-1 yield ;
97 : handle-wm-size ( hWnd uMsg wParam lParam -- )
99 [ lo-word ] keep hi-word 2array
100 dup { 0 0 } = [ 2drop ] [ swap window (>>dim) ] if ;
102 : handle-wm-move ( hWnd uMsg wParam lParam -- )
104 [ lo-word ] keep hi-word 2array
105 swap window (>>window-loc) ;
107 : wm-keydown-codes ( -- key )
137 : key-state-down? ( key -- ? )
138 GetKeyState 16 bit? ;
140 : left-shift? ( -- ? ) VK_LSHIFT key-state-down? ;
141 : left-ctrl? ( -- ? ) VK_LCONTROL key-state-down? ;
142 : left-alt? ( -- ? ) VK_LMENU key-state-down? ;
143 : right-shift? ( -- ? ) VK_RSHIFT key-state-down? ;
144 : right-ctrl? ( -- ? ) VK_RCONTROL key-state-down? ;
145 : right-alt? ( -- ? ) VK_RMENU key-state-down? ;
146 : shift? ( -- ? ) left-shift? right-shift? or ;
147 : ctrl? ( -- ? ) left-ctrl? right-ctrl? or ;
148 : alt? ( -- ? ) left-alt? right-alt? or ;
149 : caps-lock? ( -- ? ) VK_CAPITAL GetKeyState zero? not ;
151 : key-modifiers ( -- seq )
156 ] { } make [ empty? not ] keep f ? ;
158 : exclude-keys-wm-keydown
166 : exclude-keys-wm-char
175 : exclude-key-wm-keydown? ( n -- ? )
176 exclude-keys-wm-keydown key? ;
178 : exclude-key-wm-char? ( n -- ? )
179 exclude-keys-wm-char key? ;
181 : keystroke>gesture ( n -- mods sym )
182 wm-keydown-codes at* [ key-modifiers swap ] [ drop f f ] if ;
184 : send-key-gesture ( sym action? quot hWnd -- )
185 [ [ key-modifiers ] 3dip call ] dip
186 window propagate-key-gesture ; inline
188 : send-key-down ( sym action? hWnd -- )
189 [ [ <key-down> ] ] dip send-key-gesture ;
191 : send-key-up ( sym action? hWnd -- )
192 [ [ <key-up> ] ] dip send-key-gesture ;
194 : key-sym ( wParam -- string/f action? )
198 [ shift? caps-lock? xor [ CHAR: a + CHAR: A - ] unless 1string f ]
200 { [ dup digit? ] [ 1string f ] }
201 [ wm-keydown-codes at t ]
204 :: handle-wm-keydown ( hWnd uMsg wParam lParam -- )
205 wParam exclude-key-wm-keydown? [
206 wParam key-sym over [
207 dup ctrl? alt? xor or [
213 :: handle-wm-char ( hWnd uMsg wParam lParam -- )
214 wParam exclude-key-wm-char? [
217 [ f hWnd send-key-down ]
218 [ hWnd window user-input ] bi
222 :: handle-wm-keyup ( hWnd uMsg wParam lParam -- )
223 wParam exclude-key-wm-keydown? [
224 wParam key-sym over [
229 :: set-window-active ( hwnd uMsg wParam lParam ? -- n )
230 ? hwnd window (>>active?)
231 hwnd uMsg wParam lParam DefWindowProc ;
233 : handle-wm-syscommand ( hWnd uMsg wParam lParam -- n )
235 { [ over SC_MINIMIZE = ] [ f set-window-active ] }
236 { [ over SC_RESTORE = ] [ t set-window-active ] }
237 { [ over SC_MAXIMIZE = ] [ t set-window-active ] }
238 { [ dup alpha? ] [ 4drop 0 ] }
239 { [ t ] [ DefWindowProc ] }
242 : cleanup-window ( handle -- )
243 dup title>> [ free ] when*
244 dup hRC>> wglDeleteContext win32-error=0/f
245 dup hWnd>> swap hDC>> ReleaseDC win32-error=0/f ;
247 M: windows-ui-backend (close-window)
248 dup hWnd>> unregister-window
250 hWnd>> DestroyWindow win32-error=0/f ;
252 : handle-wm-close ( hWnd uMsg wParam lParam -- )
253 3drop window ungraft ;
255 : handle-wm-set-focus ( hWnd uMsg wParam lParam -- )
256 3drop window [ focus-world ] when* ;
258 : handle-wm-kill-focus ( hWnd uMsg wParam lParam -- )
259 3drop window [ unfocus-world ] when* ;
261 : message>button ( uMsg -- button down? )
263 { WM_LBUTTONDOWN [ 1 t ] }
264 { WM_LBUTTONUP [ 1 f ] }
265 { WM_MBUTTONDOWN [ 2 t ] }
266 { WM_MBUTTONUP [ 2 f ] }
267 { WM_RBUTTONDOWN [ 3 t ] }
268 { WM_RBUTTONUP [ 3 f ] }
270 { WM_NCLBUTTONDOWN [ 1 t ] }
271 { WM_NCLBUTTONUP [ 1 f ] }
272 { WM_NCMBUTTONDOWN [ 2 t ] }
273 { WM_NCMBUTTONUP [ 2 f ] }
274 { WM_NCRBUTTONDOWN [ 3 t ] }
275 { WM_NCRBUTTONUP [ 3 f ] }
278 ! If the user clicks in the window border ("non-client area")
279 ! Windows sends us an NC[LMR]BUTTONDOWN message; but if the
280 ! mouse is subsequently released outside the NC area, we receive
281 ! a [LMR]BUTTONUP message and Factor can get confused. So we
282 ! ignore BUTTONUP's that are a result of an NC*BUTTONDOWN.
285 : handle-wm-ncbutton ( hWnd uMsg wParam lParam -- )
287 message>button nc-buttons get
288 swap [ push ] [ delete ] if ;
290 : >lo-hi ( WORD -- array ) [ lo-word ] [ hi-word ] bi 2array ;
292 : mouse-wheel ( wParam -- array ) >lo-hi [ sgn neg ] map ;
294 : mouse-event>gesture ( uMsg -- button )
295 key-modifiers swap message>button
296 [ <button-down> ] [ <button-up> ] if ;
298 :: prepare-mouse ( hWnd uMsg wParam lParam -- button coordinate world )
299 uMsg mouse-event>gesture
303 : set-capture ( hwnd -- )
307 [ SetCapture drop ] keep
311 : release-capture ( -- )
312 ReleaseCapture win32-error=0/f
315 : handle-wm-buttondown ( hWnd uMsg wParam lParam -- )
318 dup message>button drop nc-buttons get delete
319 ] 2dip prepare-mouse send-button-down ;
321 : handle-wm-buttonup ( hWnd uMsg wParam lParam -- )
322 mouse-captured get [ release-capture ] when
323 pick message>button drop dup nc-buttons get member? [
324 nc-buttons get delete 4drop
326 drop prepare-mouse send-button-up
329 : make-TRACKMOUSEEVENT ( hWnd -- alien )
330 "TRACKMOUSEEVENT" <c-object> [ set-TRACKMOUSEEVENT-hwndTrack ] keep
331 "TRACKMOUSEEVENT" heap-size over set-TRACKMOUSEEVENT-cbSize ;
333 : handle-wm-mousemove ( hWnd uMsg wParam lParam -- )
335 over make-TRACKMOUSEEVENT
336 TME_LEAVE over set-TRACKMOUSEEVENT-dwFlags
337 0 over set-TRACKMOUSEEVENT-dwHoverTime
339 >lo-hi swap window move-hand fire-motion ;
341 :: handle-wm-mousewheel ( hWnd uMsg wParam lParam -- )
342 wParam mouse-wheel hand-loc get hWnd window send-wheel ;
344 : handle-wm-cancelmode ( hWnd uMsg wParam lParam -- )
345 #! message sent if windows needs application to stop dragging
346 4drop release-capture ;
348 : handle-wm-mouseleave ( hWnd uMsg wParam lParam -- )
349 #! message sent if mouse leaves main application
350 4drop forget-rollover ;
354 H{ } clone wm-handlers set-global
356 : add-wm-handler ( quot wm -- )
358 [ [ execute add-wm-handler ] with each ]
359 [ wm-handlers get-global set-at ] if ;
361 [ handle-wm-close 0 ] WM_CLOSE add-wm-handler
362 [ 4dup handle-wm-paint DefWindowProc ] WM_PAINT add-wm-handler
364 [ handle-wm-size 0 ] WM_SIZE add-wm-handler
365 [ handle-wm-move 0 ] WM_MOVE add-wm-handler
367 [ 4dup handle-wm-keydown DefWindowProc ] { WM_KEYDOWN WM_SYSKEYDOWN } add-wm-handler
368 [ 4dup handle-wm-char DefWindowProc ] { WM_CHAR WM_SYSCHAR } add-wm-handler
369 [ 4dup handle-wm-keyup DefWindowProc ] { WM_KEYUP WM_SYSKEYUP } add-wm-handler
371 [ handle-wm-syscommand ] WM_SYSCOMMAND add-wm-handler
372 [ handle-wm-set-focus 0 ] WM_SETFOCUS add-wm-handler
373 [ handle-wm-kill-focus 0 ] WM_KILLFOCUS add-wm-handler
375 [ handle-wm-buttondown 0 ] WM_LBUTTONDOWN add-wm-handler
376 [ handle-wm-buttondown 0 ] WM_MBUTTONDOWN add-wm-handler
377 [ handle-wm-buttondown 0 ] WM_RBUTTONDOWN add-wm-handler
378 [ handle-wm-buttonup 0 ] WM_LBUTTONUP add-wm-handler
379 [ handle-wm-buttonup 0 ] WM_MBUTTONUP add-wm-handler
380 [ handle-wm-buttonup 0 ] WM_RBUTTONUP add-wm-handler
382 [ 4dup handle-wm-ncbutton DefWindowProc ]
383 { WM_NCLBUTTONDOWN WM_NCMBUTTONDOWN WM_NCRBUTTONDOWN
384 WM_NCLBUTTONUP WM_NCMBUTTONUP WM_NCRBUTTONUP }
387 [ nc-buttons get-global delete-all DefWindowProc ]
388 { WM_EXITSIZEMOVE WM_EXITMENULOOP } add-wm-handler
390 [ handle-wm-mousemove 0 ] WM_MOUSEMOVE add-wm-handler
391 [ handle-wm-mousewheel 0 ] WM_MOUSEWHEEL add-wm-handler
392 [ handle-wm-cancelmode 0 ] WM_CANCELMODE add-wm-handler
393 [ handle-wm-mouseleave 0 ] WM_MOUSELEAVE add-wm-handler
395 SYMBOL: trace-messages?
397 ! return 0 if you handle the message, else just let DefWindowProc return its val
398 : ui-wndproc ( -- object )
399 "uint" { "void*" "uint" "long" "long" } "stdcall" [
401 trace-messages? get-global [ dup windows-message-name name>> print flush ] when
402 wm-handlers get-global at* [ call ] [ drop DefWindowProc ] if
405 : peek-message? ( msg -- ? ) f 0 0 PM_REMOVE PeekMessage zero? ;
407 M: windows-ui-backend do-events
409 dup peek-message? [ drop ui-wait ] [
410 [ TranslateMessage drop ]
411 [ DispatchMessage drop ] bi
414 : register-wndclassex ( -- class )
415 "WNDCLASSEX" <c-object>
417 class-name-ptr get-global
418 pick GetClassInfoEx zero? [
419 "WNDCLASSEX" heap-size over set-WNDCLASSEX-cbSize
420 { CS_HREDRAW CS_VREDRAW CS_OWNDC } flags over set-WNDCLASSEX-style
421 ui-wndproc over set-WNDCLASSEX-lpfnWndProc
422 0 over set-WNDCLASSEX-cbClsExtra
423 0 over set-WNDCLASSEX-cbWndExtra
424 f GetModuleHandle over set-WNDCLASSEX-hInstance
425 f GetModuleHandle "fraptor" utf16n string>alien LoadIcon
426 over set-WNDCLASSEX-hIcon
427 f IDC_ARROW LoadCursor over set-WNDCLASSEX-hCursor
429 class-name-ptr get-global over set-WNDCLASSEX-lpszClassName
430 RegisterClassEx dup win32-error=0/f
433 : adjust-RECT ( RECT -- )
434 style 0 ex-style AdjustWindowRectEx win32-error=0/f ;
436 : make-RECT ( world -- RECT )
437 [ window-loc>> dup ] [ rect-dim ] bi v+
439 over first over set-RECT-right
440 swap second over set-RECT-bottom
441 over first over set-RECT-left
442 swap second over set-RECT-top ;
444 : default-position-RECT ( RECT -- )
445 dup get-RECT-dimensions [ 2drop ] 2dip
446 CW_USEDEFAULT + pick set-RECT-bottom
447 CW_USEDEFAULT + over set-RECT-right
448 CW_USEDEFAULT over set-RECT-left
449 CW_USEDEFAULT swap set-RECT-top ;
451 : make-adjusted-RECT ( rect -- RECT )
453 dup get-RECT-top-left [ zero? ] both? swap
455 swap [ dup default-position-RECT ] when ;
457 : create-window ( rect -- hwnd )
459 [ class-name-ptr get-global f ] dip
462 { WS_CLIPSIBLINGS WS_CLIPCHILDREN style } flags
463 ] dip get-RECT-dimensions
464 f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ;
466 : show-window ( hWnd -- )
467 dup SW_SHOW ShowWindow drop ! always succeeds
468 dup SetForegroundWindow drop
471 : init-win32-ui ( -- )
472 V{ } clone nc-buttons set-global
473 "MSG" malloc-object msg-obj set-global
474 "Factor-window" utf16n malloc-string class-name-ptr set-global
475 register-wndclassex drop
476 GetDoubleClickTime milliseconds double-click-timeout set-global ;
478 : cleanup-win32-ui ( -- )
479 class-name-ptr get-global [ dup f UnregisterClass drop free ] when*
480 msg-obj get-global [ free ] when*
481 f class-name-ptr set-global
482 f msg-obj set-global ;
484 : setup-pixel-format ( hdc flags -- )
485 32 make-pfd [ ChoosePixelFormat dup win32-error=0/f ] 2keep
486 swapd SetPixelFormat win32-error=0/f ;
488 : get-dc ( hWnd -- hDC ) GetDC dup win32-error=0/f ;
490 : get-rc ( hDC -- hRC )
491 dup wglCreateContext dup win32-error=0/f
492 [ wglMakeCurrent win32-error=0/f ] keep ;
494 : setup-gl ( hwnd -- hDC hRC )
495 get-dc dup windowed-pfd-dwFlags setup-pixel-format dup get-rc ;
497 M: windows-ui-backend (open-window) ( world -- )
498 [ create-window [ setup-gl ] keep ] keep
500 [ swap hWnd>> register-window ] 2keep
504 M: win-base select-gl-context ( handle -- )
505 [ hDC>> ] keep hRC>> wglMakeCurrent win32-error=0/f
508 M: win-base flush-gl-context ( handle -- )
509 hDC>> SwapBuffers win32-error=0/f ;
511 : (bitmap-info) ( dim -- BITMAPINFO )
512 "BITMAPINFO" <c-object> [
513 BITMAPINFO-bmiHeader {
514 [ nip "BITMAPINFOHEADER" heap-size swap set-BITMAPINFOHEADER-biSize ]
515 [ [ first ] dip set-BITMAPINFOHEADER-biWidth ]
516 [ [ second ] dip set-BITMAPINFOHEADER-biHeight ]
517 [ nip 1 swap set-BITMAPINFOHEADER-biPlanes ]
518 [ nip 32 swap set-BITMAPINFOHEADER-biBitCount ]
519 [ nip BI_RGB swap set-BITMAPINFOHEADER-biCompression ]
520 [ [ first2 * 4 * ] dip set-BITMAPINFOHEADER-biSizeImage ]
521 [ nip 72 swap set-BITMAPINFOHEADER-biXPelsPerMeter ]
522 [ nip 72 swap set-BITMAPINFOHEADER-biYPelsPerMeter ]
523 [ nip 0 swap set-BITMAPINFOHEADER-biClrUsed ]
524 [ nip 0 swap set-BITMAPINFOHEADER-biClrImportant ]
528 : make-offscreen-dc-and-bitmap ( dim -- hDC hBitmap bits )
530 dup rot (bitmap-info) DIB_RGB_COLORS f <void*>
531 [ f 0 CreateDIBSection ] keep *void*
532 [ 2dup SelectObject drop ] dip ;
534 : setup-offscreen-gl ( dim -- hDC hRC hBitmap bits )
535 make-offscreen-dc-and-bitmap [
536 [ dup offscreen-pfd-dwFlags setup-pixel-format ]
540 M: windows-ui-backend (open-offscreen-buffer) ( world -- )
541 dup dim>> setup-offscreen-gl <win-offscreen>
543 M: windows-ui-backend (close-offscreen-buffer) ( handle -- )
544 [ hDC>> DeleteDC drop ]
545 [ hBitmap>> DeleteObject drop ] bi ;
547 ! Windows 32-bit bitmaps don't actually use the alpha byte of
548 ! each pixel; it's left as zero
550 : (make-opaque) ( byte-array -- byte-array' )
552 [ '[ 255 swap 4 * 3 + _ set-nth ] each ]
555 : (opaque-pixels) ( world -- pixels )
556 [ handle>> bits>> ] [ dim>> first2 * 4 * ] bi
557 memory>byte-array (make-opaque) ;
559 M: windows-ui-backend offscreen-pixels ( world -- alien w h )
560 [ (opaque-pixels) ] [ dim>> first2 ] bi ;
562 M: windows-ui-backend raise-window* ( world -- )
567 M: windows-ui-backend set-title ( string world -- )
569 dup title>> [ free ] when*
570 [ utf16n malloc-string ] dip
572 hWnd>> WM_SETTEXT 0 roll alien-address SendMessage drop ;
574 M: windows-ui-backend ui
581 ] [ cleanup-win32-ui ] [ ] cleanup
584 M: windows-ui-backend beep ( -- )
587 windows-ui-backend ui-backend set-global
589 [ "ui" ] main-vocab-hook set-global