]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/windows/windows.factor
f6f449625f0a5fec22ab1265234ee97e8ec8664e
[factor.git] / basis / ui / windows / windows.factor
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 ;
13 IN: ui.windows
14
15 SINGLETON: windows-ui-backend
16
17 : crlf>lf ( str -- str' )
18     CHAR: \r swap remove ;
19
20 : lf>crlf ( str -- str' )
21     [ [ dup CHAR: \n = [ CHAR: \r , ] when , ] each ] "" make ;
22
23 : enum-clipboard ( -- seq )
24     0
25     [ EnumClipboardFormats win32-error dup dup 0 > ]
26     [ ]
27     [ drop ]
28     produce nip ;
29
30 : with-clipboard ( quot -- )
31     f OpenClipboard win32-error=0/f
32     call
33     CloseClipboard win32-error=0/f ; inline
34
35 : paste ( -- str )
36     [
37         CF_UNICODETEXT IsClipboardFormatAvailable zero? [
38             ! nothing to paste
39             ""
40         ] [
41             CF_UNICODETEXT GetClipboardData dup win32-error=0/f
42             dup GlobalLock dup win32-error=0/f
43             GlobalUnlock win32-error=0/f
44             utf16n alien>string
45         ] if
46     ] with-clipboard
47     crlf>lf ;
48
49 : copy ( str -- )
50     lf>crlf [
51         utf16n string>alien
52         EmptyClipboard win32-error=0/f
53         GMEM_MOVEABLE over length 1+ GlobalAlloc
54             dup win32-error=0/f
55     
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
60     ] with-clipboard ;
61
62 TUPLE: pasteboard ;
63 C: <pasteboard> pasteboard
64
65 M: pasteboard clipboard-contents drop paste ;
66 M: pasteboard set-clipboard-contents drop copy ;
67
68 : init-clipboard ( -- )
69     <pasteboard> clipboard set-global
70     <clipboard> selection set-global ;
71
72 ! world-handle is a <win>
73 TUPLE: win hWnd hDC hRC world title ;
74 C: <win> win
75
76 SYMBOLS: msg-obj class-name-ptr mouse-captured ;
77
78 : style ( -- n ) WS_OVERLAPPEDWINDOW ; inline
79 : ex-style ( -- n ) WS_EX_APPWINDOW WS_EX_WINDOWEDGE bitor ; inline
80
81 : get-RECT-top-left ( RECT -- x y )
82     [ RECT-left ] keep RECT-top ;
83
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 - ;
88
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 ;
93
94 : handle-wm-size ( hWnd uMsg wParam lParam -- )
95     2nip
96     [ lo-word ] keep hi-word 2array
97     dup { 0 0 } = [ 2drop ] [ swap window (>>dim) ] if ;
98
99 : handle-wm-move ( hWnd uMsg wParam lParam -- )
100     2nip
101     [ lo-word ] keep hi-word 2array
102     swap window (>>window-loc) ;
103
104 : wm-keydown-codes ( -- key )
105     H{
106         { 8 "BACKSPACE" }
107         { 9 "TAB" }
108         { 13 "RET" }
109         { 27 "ESC" }
110         { 33 "PAGE_UP" }
111         { 34 "PAGE_DOWN" }
112         { 35 "END" }
113         { 36 "HOME" }
114         { 37 "LEFT" }
115         { 38 "UP" }
116         { 39 "RIGHT" }
117         { 40 "DOWN" }
118         { 45 "INSERT" }
119         { 46 "DELETE" }
120         { 112 "F1" }
121         { 113 "F2" }
122         { 114 "F3" }
123         { 115 "F4" }
124         { 116 "F5" }
125         { 117 "F6" }
126         { 118 "F7" }
127         { 119 "F8" }
128         { 120 "F9" }
129         { 121 "F10" }
130         { 122 "F11" }
131         { 123 "F12" }
132     } ;
133
134 : key-state-down? ( key -- ? )
135     GetKeyState 16 bit? ;
136
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 ;
147
148 : key-modifiers ( -- seq )
149     [
150         shift? [ S+ , ] when
151         ctrl? [ C+ , ] when
152         alt? [ A+ , ] when
153     ] { } make [ empty? not ] keep f ? ;
154
155 : exclude-keys-wm-keydown
156     H{
157         { 16 "SHIFT" }
158         { 17 "CTRL" }
159         { 18 "ALT" }
160         { 20 "CAPS-LOCK" }
161     } ;
162
163 : exclude-keys-wm-char
164     ! Values are ignored
165     H{
166         { 8 "BACKSPACE" }
167         { 9 "TAB" }
168         { 13 "RET" }
169         { 27 "ESC" }
170     } ;
171
172 : exclude-key-wm-keydown? ( n -- bool )
173     exclude-keys-wm-keydown key? ;
174
175 : exclude-key-wm-char? ( n -- bool )
176     exclude-keys-wm-char key? ;
177
178 : keystroke>gesture ( n -- mods sym )
179     wm-keydown-codes at* [ key-modifiers swap ] [ drop f f ] if ;
180
181 : send-key-gesture ( sym action? quot hWnd -- )
182     [ [ key-modifiers ] 3dip call ] dip
183     window-focus propagate-gesture ; inline
184
185 : send-key-down ( sym action? hWnd -- )
186     [ [ <key-down> ] ] dip send-key-gesture ;
187
188 : send-key-up ( sym action? hWnd -- )
189     [ [ <key-up> ] ] dip send-key-gesture ;
190
191 : key-sym ( wParam -- string/f action? )
192     {
193         {
194             [ dup LETTER? ]
195             [ shift? caps-lock? xor [ CHAR: a + CHAR: A - ] unless 1string f ]
196         }
197         { [ dup digit? ] [ 1string f ] }
198         [ wm-keydown-codes at t ]
199     } cond ;
200
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 [
205                 hWnd send-key-down
206             ] [ 2drop ] if
207         ] [ 2drop ] if
208     ] unless ;
209
210 :: handle-wm-char ( hWnd uMsg wParam lParam -- )
211     wParam exclude-key-wm-char? [
212         ctrl? alt? xor [
213             wParam 1string
214             [ f hWnd send-key-down ]
215             [ hWnd window-focus user-input ] bi
216         ] unless
217     ] unless ;
218
219 :: handle-wm-keyup ( hWnd uMsg wParam lParam -- )
220     wParam exclude-key-wm-keydown? [
221         wParam key-sym over [
222             hWnd send-key-up
223         ] [ 2drop ] if
224     ] unless ;
225
226 :: set-window-active ( hwnd uMsg wParam lParam ? -- n )
227     ? hwnd window (>>active?)
228     hwnd uMsg wParam lParam DefWindowProc ;
229
230 : handle-wm-syscommand ( hWnd uMsg wParam lParam -- n )
231     {
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 ] }
237     } cond ;
238
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 ;
243
244 M: windows-ui-backend (close-window)
245     dup hWnd>> unregister-window
246     dup cleanup-window
247     hWnd>> DestroyWindow win32-error=0/f ;
248
249 : handle-wm-close ( hWnd uMsg wParam lParam -- )
250     3drop window ungraft ;
251
252 : handle-wm-set-focus ( hWnd uMsg wParam lParam -- )
253     3drop window [ focus-world ] when* ;
254
255 : handle-wm-kill-focus ( hWnd uMsg wParam lParam -- )
256     3drop window [ unfocus-world ] when* ;
257
258 : message>button ( uMsg -- button down? )
259     {
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 ] }
266
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 ] }
273     } case ;
274
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.
280 SYMBOL: nc-buttons
281
282 : handle-wm-ncbutton ( hWnd uMsg wParam lParam -- )
283     2drop nip
284     message>button nc-buttons get
285     swap [ push ] [ delete ] if ;
286
287 : >lo-hi ( WORD -- array ) [ lo-word ] [ hi-word ] bi 2array ;
288
289 : mouse-wheel ( wParam -- array ) >lo-hi [ sgn neg ] map ;
290
291 : mouse-event>gesture ( uMsg -- button )
292     key-modifiers swap message>button
293     [ <button-down> ] [ <button-up> ] if ;
294
295 :: prepare-mouse ( hWnd uMsg wParam lParam -- button coordinate world )
296     uMsg mouse-event>gesture
297     lParam >lo-hi
298     hWnd window ;
299
300 : set-capture ( hwnd -- )
301     mouse-captured get [
302         drop
303     ] [
304         [ SetCapture drop ] keep
305         mouse-captured set
306     ] if ;
307
308 : release-capture ( -- )
309     ReleaseCapture win32-error=0/f
310     mouse-captured off ;
311
312 : handle-wm-buttondown ( hWnd uMsg wParam lParam -- )
313     [
314         over set-capture
315         dup message>button drop nc-buttons get delete
316     ] 2dip prepare-mouse send-button-down ;
317
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
322     ] [
323         drop prepare-mouse send-button-up
324     ] if ;
325
326 : make-TRACKMOUSEEVENT ( hWnd -- alien )
327     "TRACKMOUSEEVENT" <c-object> [ set-TRACKMOUSEEVENT-hwndTrack ] keep
328     "TRACKMOUSEEVENT" heap-size over set-TRACKMOUSEEVENT-cbSize ;
329
330 : handle-wm-mousemove ( hWnd uMsg wParam lParam -- )
331     2nip
332     over make-TRACKMOUSEEVENT
333     TME_LEAVE over set-TRACKMOUSEEVENT-dwFlags
334     0 over set-TRACKMOUSEEVENT-dwHoverTime
335     TrackMouseEvent drop
336     >lo-hi swap window move-hand fire-motion ;
337
338 :: handle-wm-mousewheel ( hWnd uMsg wParam lParam -- )
339     wParam mouse-wheel hand-loc get hWnd window send-wheel ;
340
341 : handle-wm-cancelmode ( hWnd uMsg wParam lParam -- )
342     #! message sent if windows needs application to stop dragging
343     4drop release-capture ;
344
345 : handle-wm-mouseleave ( hWnd uMsg wParam lParam -- )
346     #! message sent if mouse leaves main application 
347     4drop forget-rollover ;
348
349 SYMBOL: wm-handlers
350
351 H{ } clone wm-handlers set-global
352
353 : add-wm-handler ( quot wm -- )
354     dup array?
355     [ [ execute add-wm-handler ] with each ]
356     [ wm-handlers get-global set-at ] if ;
357
358 [ handle-wm-close 0                  ] WM_CLOSE add-wm-handler
359 [ 4dup handle-wm-paint DefWindowProc ] WM_PAINT add-wm-handler
360
361 [ handle-wm-size 0 ] WM_SIZE add-wm-handler
362 [ handle-wm-move 0 ] WM_MOVE add-wm-handler
363
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
367
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
371
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
378
379 [ 4dup handle-wm-ncbutton DefWindowProc ]
380 { WM_NCLBUTTONDOWN WM_NCMBUTTONDOWN WM_NCRBUTTONDOWN
381 WM_NCLBUTTONUP WM_NCMBUTTONUP WM_NCRBUTTONUP }
382 add-wm-handler
383
384 [ nc-buttons get-global delete-all DefWindowProc ]
385 { WM_EXITSIZEMOVE WM_EXITMENULOOP } add-wm-handler
386
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
391
392 SYMBOL: trace-messages?
393
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" [
397         pick
398         trace-messages? get-global [ dup windows-message-name name>> print flush ] when
399         wm-handlers get-global at* [ call ] [ drop DefWindowProc ] if
400      ] alien-callback ;
401
402 : peek-message? ( msg -- ? ) f 0 0 PM_REMOVE PeekMessage zero? ;
403
404 M: windows-ui-backend do-events
405     msg-obj get-global
406     dup peek-message? [ drop ui-wait ] [
407         [ TranslateMessage drop ]
408         [ DispatchMessage drop ] bi
409     ] if ;
410
411 : register-wndclassex ( -- class )
412     "WNDCLASSEX" <c-object>
413     f GetModuleHandle
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
425
426         class-name-ptr get-global over set-WNDCLASSEX-lpszClassName
427         RegisterClassEx dup win32-error=0/f
428     ] when ;
429
430 : adjust-RECT ( RECT -- )
431     style 0 ex-style AdjustWindowRectEx win32-error=0/f ;
432
433 : make-RECT ( world -- RECT )
434     [ window-loc>> dup ] [ rect-dim ] bi v+
435     "RECT" <c-object>
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 ;
440
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 ;
447
448 : make-adjusted-RECT ( rect -- RECT )
449     make-RECT
450     dup get-RECT-top-left [ zero? ] both? swap
451     dup adjust-RECT
452     swap [ dup default-position-RECT ] when ;
453
454 : create-window ( rect -- hwnd )
455     make-adjusted-RECT
456     [ class-name-ptr get-global f ] dip
457     [
458         [ ex-style ] 2dip
459         { WS_CLIPSIBLINGS WS_CLIPCHILDREN style } flags
460     ] dip get-RECT-dimensions
461     f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ;
462
463 : show-window ( hWnd -- )
464     dup SW_SHOW ShowWindow drop ! always succeeds
465     dup SetForegroundWindow drop
466     SetFocus drop ;
467
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 ;
474
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 ;
480
481 : setup-pixel-format ( hdc -- )
482     16 make-pfd [ ChoosePixelFormat dup win32-error=0/f ] 2keep
483     swapd SetPixelFormat win32-error=0/f ;
484
485 : get-dc ( hWnd -- hDC ) GetDC dup win32-error=0/f ;
486
487 : get-rc ( hDC -- hRC )
488     dup wglCreateContext dup win32-error=0/f
489     [ wglMakeCurrent win32-error=0/f ] keep ;
490
491 : setup-gl ( hwnd -- hDC hRC )
492     get-dc dup setup-pixel-format dup get-rc ;
493
494 M: windows-ui-backend (open-window) ( world -- )
495     [ create-window dup setup-gl ] keep
496     [ f <win> ] keep
497     [ swap hWnd>> register-window ] 2keep
498     dupd (>>handle)
499     hWnd>> show-window ;
500
501 M: win select-gl-context ( handle -- )
502     [ hDC>> ] keep hRC>> wglMakeCurrent win32-error=0/f ;
503
504 M: win flush-gl-context ( handle -- )
505     hDC>> SwapBuffers win32-error=0/f ;
506
507 ! Move window to front
508 M: windows-ui-backend raise-window* ( world -- )
509     handle>> [
510         hWnd>> SetFocus drop
511     ] when* ;
512
513 M: windows-ui-backend set-title ( string world -- )
514     handle>>
515     dup title>> [ free ] when*
516     [ utf16n malloc-string ] dip
517     2dup (>>title)
518     hWnd>> WM_SETTEXT 0 roll alien-address SendMessage drop ;
519
520 M: windows-ui-backend ui
521     [
522         [
523             stop-after-last-window? on
524             init-clipboard
525             init-win32-ui
526             start-ui
527             event-loop
528         ] [ cleanup-win32-ui ] [ ] cleanup
529     ] ui-running ;
530
531 M: windows-ui-backend beep ( -- )
532     0 MessageBeep drop ;
533
534 windows-ui-backend ui-backend set-global
535
536 [ "ui" ] main-vocab-hook set-global