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 io kernel math math.vectors namespaces make
7 sequences strings vectors words windows.kernel32 windows.gdi32
8 windows.user32 windows.opengl32 windows.messages windows.types
9 windows.nt windows threads libc combinators
10 combinators.short-circuit continuations command-line shuffle
11 opengl ui.render ascii math.bitwise locals symbols accessors
12 math.geometry.rect math.order ascii calendar ;
15 SINGLETON: windows-ui-backend
17 : crlf>lf ( str -- str' )
18 CHAR: \r swap remove ;
20 : lf>crlf ( str -- str' )
21 [ [ dup CHAR: \n = [ CHAR: \r , ] when , ] each ] "" make ;
23 : enum-clipboard ( -- seq )
25 [ EnumClipboardFormats win32-error dup dup 0 > ]
30 : with-clipboard ( quot -- )
31 f OpenClipboard win32-error=0/f
33 CloseClipboard win32-error=0/f ; inline
37 CF_UNICODETEXT IsClipboardFormatAvailable zero? [
41 CF_UNICODETEXT GetClipboardData dup win32-error=0/f
42 dup GlobalLock dup win32-error=0/f
43 GlobalUnlock win32-error=0/f
52 EmptyClipboard win32-error=0/f
53 GMEM_MOVEABLE over length 1+ GlobalAlloc
56 dup GlobalLock dup win32-error=0/f
57 swapd byte-array>memory
58 dup GlobalUnlock win32-error=0/f
59 CF_UNICODETEXT swap SetClipboardData win32-error=0/f
63 C: <pasteboard> pasteboard
65 M: pasteboard clipboard-contents drop paste ;
66 M: pasteboard set-clipboard-contents drop copy ;
68 : init-clipboard ( -- )
69 <pasteboard> clipboard set-global
70 <clipboard> selection set-global ;
72 ! world-handle is a <win>
73 TUPLE: win hWnd hDC hRC world title ;
76 SYMBOLS: msg-obj class-name-ptr mouse-captured ;
78 : style ( -- n ) WS_OVERLAPPEDWINDOW ; inline
79 : ex-style ( -- n ) WS_EX_APPWINDOW WS_EX_WINDOWEDGE bitor ; inline
81 : get-RECT-top-left ( RECT -- x y )
82 [ RECT-left ] keep RECT-top ;
84 : get-RECT-dimensions ( RECT -- x y width height )
85 [ get-RECT-top-left ] keep
86 [ RECT-right ] keep [ RECT-left - ] keep
87 [ RECT-bottom ] keep RECT-top - ;
89 : handle-wm-paint ( hWnd uMsg wParam lParam -- )
90 #! wParam and lParam are unused
91 #! only paint if width/height both > 0
92 3drop window relayout-1 yield ;
94 : handle-wm-size ( hWnd uMsg wParam lParam -- )
96 [ lo-word ] keep hi-word 2array
97 dup { 0 0 } = [ 2drop ] [ swap window (>>dim) ] if ;
99 : handle-wm-move ( hWnd uMsg wParam lParam -- )
101 [ lo-word ] keep hi-word 2array
102 swap window (>>window-loc) ;
104 : wm-keydown-codes ( -- key )
134 : key-state-down? ( key -- ? )
135 GetKeyState 16 bit? ;
137 : left-shift? ( -- ? ) VK_LSHIFT key-state-down? ;
138 : left-ctrl? ( -- ? ) VK_LCONTROL key-state-down? ;
139 : left-alt? ( -- ? ) VK_LMENU key-state-down? ;
140 : right-shift? ( -- ? ) VK_RSHIFT key-state-down? ;
141 : right-ctrl? ( -- ? ) VK_RCONTROL key-state-down? ;
142 : right-alt? ( -- ? ) VK_RMENU key-state-down? ;
143 : shift? ( -- ? ) left-shift? right-shift? or ;
144 : ctrl? ( -- ? ) left-ctrl? right-ctrl? or ;
145 : alt? ( -- ? ) left-alt? right-alt? or ;
146 : caps-lock? ( -- ? ) VK_CAPITAL GetKeyState zero? not ;
148 : key-modifiers ( -- seq )
153 ] { } make [ empty? not ] keep f ? ;
155 : exclude-keys-wm-keydown
163 : exclude-keys-wm-char
172 : exclude-key-wm-keydown? ( n -- bool )
173 exclude-keys-wm-keydown key? ;
175 : exclude-key-wm-char? ( n -- bool )
176 exclude-keys-wm-char key? ;
178 : keystroke>gesture ( n -- mods sym )
179 wm-keydown-codes at* [ key-modifiers swap ] [ drop f f ] if ;
181 : send-key-gesture ( sym action? quot hWnd -- )
182 [ [ key-modifiers ] 3dip call ] dip
183 window-focus propagate-gesture ; inline
185 : send-key-down ( sym action? hWnd -- )
186 [ [ <key-down> ] ] dip send-key-gesture ;
188 : send-key-up ( sym action? hWnd -- )
189 [ [ <key-up> ] ] dip send-key-gesture ;
191 : key-sym ( wParam -- string/f action? )
195 [ shift? caps-lock? xor [ CHAR: a + CHAR: A - ] unless 1string f ]
197 { [ dup digit? ] [ 1string f ] }
198 [ wm-keydown-codes at t ]
201 :: handle-wm-keydown ( hWnd uMsg wParam lParam -- )
202 wParam exclude-key-wm-keydown? [
203 wParam key-sym over [
204 dup ctrl? alt? xor or [
210 :: handle-wm-char ( hWnd uMsg wParam lParam -- )
211 wParam exclude-key-wm-char? [
214 [ f hWnd send-key-down ]
215 [ hWnd window-focus user-input ] bi
219 :: handle-wm-keyup ( hWnd uMsg wParam lParam -- )
220 wParam exclude-key-wm-keydown? [
221 wParam key-sym over [
226 :: set-window-active ( hwnd uMsg wParam lParam ? -- n )
227 ? hwnd window (>>active?)
228 hwnd uMsg wParam lParam DefWindowProc ;
230 : handle-wm-syscommand ( hWnd uMsg wParam lParam -- n )
232 { [ over SC_MINIMIZE = ] [ f set-window-active ] }
233 { [ over SC_RESTORE = ] [ t set-window-active ] }
234 { [ over SC_MAXIMIZE = ] [ t set-window-active ] }
235 { [ dup alpha? ] [ 4drop 0 ] }
236 { [ t ] [ DefWindowProc ] }
239 : cleanup-window ( handle -- )
240 dup title>> [ free ] when*
241 dup hRC>> wglDeleteContext win32-error=0/f
242 dup hWnd>> swap hDC>> ReleaseDC win32-error=0/f ;
244 M: windows-ui-backend (close-window)
245 dup hWnd>> unregister-window
247 hWnd>> DestroyWindow win32-error=0/f ;
249 : handle-wm-close ( hWnd uMsg wParam lParam -- )
250 3drop window ungraft ;
252 : handle-wm-set-focus ( hWnd uMsg wParam lParam -- )
253 3drop window [ focus-world ] when* ;
255 : handle-wm-kill-focus ( hWnd uMsg wParam lParam -- )
256 3drop window [ unfocus-world ] when* ;
258 : message>button ( uMsg -- button down? )
260 { WM_LBUTTONDOWN [ 1 t ] }
261 { WM_LBUTTONUP [ 1 f ] }
262 { WM_MBUTTONDOWN [ 2 t ] }
263 { WM_MBUTTONUP [ 2 f ] }
264 { WM_RBUTTONDOWN [ 3 t ] }
265 { WM_RBUTTONUP [ 3 f ] }
267 { WM_NCLBUTTONDOWN [ 1 t ] }
268 { WM_NCLBUTTONUP [ 1 f ] }
269 { WM_NCMBUTTONDOWN [ 2 t ] }
270 { WM_NCMBUTTONUP [ 2 f ] }
271 { WM_NCRBUTTONDOWN [ 3 t ] }
272 { WM_NCRBUTTONUP [ 3 f ] }
275 ! If the user clicks in the window border ("non-client area")
276 ! Windows sends us an NC[LMR]BUTTONDOWN message; but if the
277 ! mouse is subsequently released outside the NC area, we receive
278 ! a [LMR]BUTTONUP message and Factor can get confused. So we
279 ! ignore BUTTONUP's that are a result of an NC*BUTTONDOWN.
282 : handle-wm-ncbutton ( hWnd uMsg wParam lParam -- )
284 message>button nc-buttons get
285 swap [ push ] [ delete ] if ;
287 : >lo-hi ( WORD -- array ) [ lo-word ] [ hi-word ] bi 2array ;
289 : mouse-wheel ( wParam -- array ) >lo-hi [ sgn neg ] map ;
291 : mouse-event>gesture ( uMsg -- button )
292 key-modifiers swap message>button
293 [ <button-down> ] [ <button-up> ] if ;
295 :: prepare-mouse ( hWnd uMsg wParam lParam -- button coordinate world )
296 uMsg mouse-event>gesture
300 : set-capture ( hwnd -- )
304 [ SetCapture drop ] keep
308 : release-capture ( -- )
309 ReleaseCapture win32-error=0/f
312 : handle-wm-buttondown ( hWnd uMsg wParam lParam -- )
315 dup message>button drop nc-buttons get delete
316 ] 2dip prepare-mouse send-button-down ;
318 : handle-wm-buttonup ( hWnd uMsg wParam lParam -- )
319 mouse-captured get [ release-capture ] when
320 pick message>button drop dup nc-buttons get member? [
321 nc-buttons get delete 4drop
323 drop prepare-mouse send-button-up
326 : make-TRACKMOUSEEVENT ( hWnd -- alien )
327 "TRACKMOUSEEVENT" <c-object> [ set-TRACKMOUSEEVENT-hwndTrack ] keep
328 "TRACKMOUSEEVENT" heap-size over set-TRACKMOUSEEVENT-cbSize ;
330 : handle-wm-mousemove ( hWnd uMsg wParam lParam -- )
332 over make-TRACKMOUSEEVENT
333 TME_LEAVE over set-TRACKMOUSEEVENT-dwFlags
334 0 over set-TRACKMOUSEEVENT-dwHoverTime
336 >lo-hi swap window move-hand fire-motion ;
338 :: handle-wm-mousewheel ( hWnd uMsg wParam lParam -- )
339 wParam mouse-wheel hand-loc get hWnd window send-wheel ;
341 : handle-wm-cancelmode ( hWnd uMsg wParam lParam -- )
342 #! message sent if windows needs application to stop dragging
343 4drop release-capture ;
345 : handle-wm-mouseleave ( hWnd uMsg wParam lParam -- )
346 #! message sent if mouse leaves main application
347 4drop forget-rollover ;
351 H{ } clone wm-handlers set-global
353 : add-wm-handler ( quot wm -- )
355 [ [ execute add-wm-handler ] with each ]
356 [ wm-handlers get-global set-at ] if ;
358 [ handle-wm-close 0 ] WM_CLOSE add-wm-handler
359 [ 4dup handle-wm-paint DefWindowProc ] WM_PAINT add-wm-handler
361 [ handle-wm-size 0 ] WM_SIZE add-wm-handler
362 [ handle-wm-move 0 ] WM_MOVE add-wm-handler
364 [ 4dup handle-wm-keydown DefWindowProc ] { WM_KEYDOWN WM_SYSKEYDOWN } add-wm-handler
365 [ 4dup handle-wm-char DefWindowProc ] { WM_CHAR WM_SYSCHAR } add-wm-handler
366 [ 4dup handle-wm-keyup DefWindowProc ] { WM_KEYUP WM_SYSKEYUP } add-wm-handler
368 [ handle-wm-syscommand ] WM_SYSCOMMAND add-wm-handler
369 [ handle-wm-set-focus 0 ] WM_SETFOCUS add-wm-handler
370 [ handle-wm-kill-focus 0 ] WM_KILLFOCUS add-wm-handler
372 [ handle-wm-buttondown 0 ] WM_LBUTTONDOWN add-wm-handler
373 [ handle-wm-buttondown 0 ] WM_MBUTTONDOWN add-wm-handler
374 [ handle-wm-buttondown 0 ] WM_RBUTTONDOWN add-wm-handler
375 [ handle-wm-buttonup 0 ] WM_LBUTTONUP add-wm-handler
376 [ handle-wm-buttonup 0 ] WM_MBUTTONUP add-wm-handler
377 [ handle-wm-buttonup 0 ] WM_RBUTTONUP add-wm-handler
379 [ 4dup handle-wm-ncbutton DefWindowProc ]
380 { WM_NCLBUTTONDOWN WM_NCMBUTTONDOWN WM_NCRBUTTONDOWN
381 WM_NCLBUTTONUP WM_NCMBUTTONUP WM_NCRBUTTONUP }
384 [ nc-buttons get-global delete-all DefWindowProc ]
385 { WM_EXITSIZEMOVE WM_EXITMENULOOP } add-wm-handler
387 [ handle-wm-mousemove 0 ] WM_MOUSEMOVE add-wm-handler
388 [ handle-wm-mousewheel 0 ] WM_MOUSEWHEEL add-wm-handler
389 [ handle-wm-cancelmode 0 ] WM_CANCELMODE add-wm-handler
390 [ handle-wm-mouseleave 0 ] WM_MOUSELEAVE add-wm-handler
392 SYMBOL: trace-messages?
394 ! return 0 if you handle the message, else just let DefWindowProc return its val
395 : ui-wndproc ( -- object )
396 "uint" { "void*" "uint" "long" "long" } "stdcall" [
398 trace-messages? get-global [ dup windows-message-name name>> print flush ] when
399 wm-handlers get-global at* [ call ] [ drop DefWindowProc ] if
402 : peek-message? ( msg -- ? ) f 0 0 PM_REMOVE PeekMessage zero? ;
404 M: windows-ui-backend do-events
406 dup peek-message? [ drop ui-wait ] [
407 [ TranslateMessage drop ]
408 [ DispatchMessage drop ] bi
411 : register-wndclassex ( -- class )
412 "WNDCLASSEX" <c-object>
414 class-name-ptr get-global
415 pick GetClassInfoEx zero? [
416 "WNDCLASSEX" heap-size over set-WNDCLASSEX-cbSize
417 { CS_HREDRAW CS_VREDRAW CS_OWNDC } flags over set-WNDCLASSEX-style
418 ui-wndproc over set-WNDCLASSEX-lpfnWndProc
419 0 over set-WNDCLASSEX-cbClsExtra
420 0 over set-WNDCLASSEX-cbWndExtra
421 f GetModuleHandle over set-WNDCLASSEX-hInstance
422 f GetModuleHandle "fraptor" utf16n string>alien LoadIcon
423 over set-WNDCLASSEX-hIcon
424 f IDC_ARROW LoadCursor over set-WNDCLASSEX-hCursor
426 class-name-ptr get-global over set-WNDCLASSEX-lpszClassName
427 RegisterClassEx dup win32-error=0/f
430 : adjust-RECT ( RECT -- )
431 style 0 ex-style AdjustWindowRectEx win32-error=0/f ;
433 : make-RECT ( world -- RECT )
434 [ window-loc>> dup ] [ rect-dim ] bi v+
436 over first over set-RECT-right
437 swap second over set-RECT-bottom
438 over first over set-RECT-left
439 swap second over set-RECT-top ;
441 : default-position-RECT ( RECT -- )
442 dup get-RECT-dimensions [ 2drop ] 2dip
443 CW_USEDEFAULT + pick set-RECT-bottom
444 CW_USEDEFAULT + over set-RECT-right
445 CW_USEDEFAULT over set-RECT-left
446 CW_USEDEFAULT swap set-RECT-top ;
448 : make-adjusted-RECT ( rect -- RECT )
450 dup get-RECT-top-left [ zero? ] both? swap
452 swap [ dup default-position-RECT ] when ;
454 : create-window ( rect -- hwnd )
456 [ class-name-ptr get-global f ] dip
459 { WS_CLIPSIBLINGS WS_CLIPCHILDREN style } flags
460 ] dip get-RECT-dimensions
461 f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ;
463 : show-window ( hWnd -- )
464 dup SW_SHOW ShowWindow drop ! always succeeds
465 dup SetForegroundWindow drop
468 : init-win32-ui ( -- )
469 V{ } clone nc-buttons set-global
470 "MSG" malloc-object msg-obj set-global
471 "Factor-window" utf16n malloc-string class-name-ptr set-global
472 register-wndclassex drop
473 GetDoubleClickTime milliseconds double-click-timeout set-global ;
475 : cleanup-win32-ui ( -- )
476 class-name-ptr get-global [ dup f UnregisterClass drop free ] when*
477 msg-obj get-global [ free ] when*
478 f class-name-ptr set-global
479 f msg-obj set-global ;
481 : setup-pixel-format ( hdc -- )
482 16 make-pfd [ ChoosePixelFormat dup win32-error=0/f ] 2keep
483 swapd SetPixelFormat win32-error=0/f ;
485 : get-dc ( hWnd -- hDC ) GetDC dup win32-error=0/f ;
487 : get-rc ( hDC -- hRC )
488 dup wglCreateContext dup win32-error=0/f
489 [ wglMakeCurrent win32-error=0/f ] keep ;
491 : setup-gl ( hwnd -- hDC hRC )
492 get-dc dup setup-pixel-format dup get-rc ;
494 M: windows-ui-backend (open-window) ( world -- )
495 [ create-window dup setup-gl ] keep
497 [ swap hWnd>> register-window ] 2keep
501 M: win select-gl-context ( handle -- )
502 [ hDC>> ] keep hRC>> wglMakeCurrent win32-error=0/f ;
504 M: win flush-gl-context ( handle -- )
505 hDC>> SwapBuffers win32-error=0/f ;
507 ! Move window to front
508 M: windows-ui-backend raise-window* ( world -- )
513 M: windows-ui-backend set-title ( string world -- )
515 dup title>> [ free ] when*
516 [ utf16n malloc-string ] dip
518 hWnd>> WM_SETTEXT 0 roll alien-address SendMessage drop ;
520 M: windows-ui-backend ui
523 stop-after-last-window? on
528 ] [ cleanup-win32-ui ] [ ] cleanup
531 M: windows-ui-backend beep ( -- )
534 windows-ui-backend ui-backend set-global
536 [ "ui" ] main-vocab-hook set-global