! Copyright (C) 2005, 2006 Doug Coleman.
! Portions copyright (C) 2007, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.data alien.strings arrays assocs ui
-ui.private ui.gadgets ui.gadgets.private ui.backend
-ui.clipboards ui.gadgets.worlds ui.gestures ui.event-loop io
-kernel math math.vectors namespaces make sequences strings
-vectors words windows.dwmapi system-info.windows
-windows.kernel32 windows.gdi32 windows.user32 windows.opengl32
-windows.messages windows.types windows.offscreen windows threads
-libc combinators fry combinators.short-circuit continuations
-command-line shuffle opengl ui.render math.bitwise locals
-accessors math.rectangles math.order calendar ascii sets io.crlf
-io.encodings.utf16n windows.errors literals ui.pixel-formats
-ui.pixel-formats.private memoize classes colors
-specialized-arrays classes.struct ;
-FROM: namespaces => set ;
+USING: accessors alien alien.data alien.strings arrays ascii assocs
+calendar classes classes.struct colors combinators continuations fry
+io io.crlf io.encodings.utf16n kernel libc literals locals make math
+math.bitwise namespaces sequences sets specialized-arrays strings
+threads ui ui.backend ui.clipboards ui.event-loop ui.gadgets
+ui.gadgets.private ui.gadgets.worlds ui.gestures ui.pixel-formats
+ui.private windows.dwmapi windows.errors windows.gdi32
+windows.kernel32 windows.messages windows.offscreen windows.opengl32
+windows.types windows.user32 assocs.extras byte-arrays
+io.encodings.string ;
+FROM: unicode => upper-surrogate? under-surrogate? ;
SPECIALIZED-ARRAY: POINT
QUALIFIED-WITH: alien.c-types c
IN: ui.backend.windows
TUPLE: win-base hDC hRC ;
TUPLE: win < win-base hWnd world title ;
-TUPLE: win-offscreen < win-base hBitmap bits ;
C: <win> win
-C: <win-offscreen> win-offscreen
<PRIVATE
-PIXEL-FORMAT-ATTRIBUTE-TABLE: WGL_ARB { $ WGL_SUPPORT_OPENGL_ARB 1 } H{
+CONSTANT: perm-attribs { $ WGL_SUPPORT_OPENGL_ARB 1 }
+
+CONSTANT: attrib-table H{
{ double-buffered { $ WGL_DOUBLE_BUFFER_ARB 1 } }
{ stereo { $ WGL_STEREO_ARB 1 } }
{ offscreen { $ WGL_DRAW_TO_BITMAP_ARB 1 } }
drop f ;
: arb-make-pixel-format ( world attributes -- pf )
- [ handle>> hDC>> ] dip >WGL_ARB-int-array f 1 { c:int c:int }
+ [ handle>> hDC>> ] dip
+ perm-attribs attrib-table pixel-format-attributes>int-array
+ f 1 { c:int c:int }
[ wglChoosePixelFormatARB win32-error=0/f ] with-out-parameters drop ;
-: arb-pixel-format-attribute ( pixel-format attribute -- value )
- >WGL_ARB
- [ drop f ] [
- [ [ world>> handle>> hDC>> ] [ handle>> ] bi 0 1 ] dip
- first c:int <ref> { c:int }
- [ wglGetPixelFormatAttribivARB win32-error=0/f ]
- with-out-parameters
- ] if-empty ;
-
CONSTANT: pfd-flag-map H{
{ double-buffered $ PFD_DOUBLEBUFFER }
{ stereo $ PFD_STEREO }
[ handle>> hDC>> ] [ >pfd ] bi*
ChoosePixelFormat dup win32-error=0/f ;
-: get-pfd ( pixel-format -- pfd )
- [ world>> handle>> hDC>> ] [ handle>> ] bi
- PIXELFORMATDESCRIPTOR c:heap-size
- PIXELFORMATDESCRIPTOR <struct>
- [ DescribePixelFormat win32-error=0/f ] keep ;
-
-: pfd-flag? ( pfd flag -- ? )
- [ dwFlags>> ] dip bitand c:c-bool> ;
-
-: (pfd-pixel-format-attribute) ( pfd attribute -- value )
- {
- { double-buffered [ PFD_DOUBLEBUFFER pfd-flag? ] }
- { stereo [ PFD_STEREO pfd-flag? ] }
- { offscreen [ PFD_DRAW_TO_BITMAP pfd-flag? ] }
- { fullscreen [ PFD_DRAW_TO_WINDOW pfd-flag? ] }
- { windowed [ PFD_DRAW_TO_WINDOW pfd-flag? ] }
- { software-rendered [ PFD_GENERIC_FORMAT pfd-flag? ] }
- { color-bits [ cColorBits>> ] }
- { red-bits [ cRedBits>> ] }
- { green-bits [ cGreenBits>> ] }
- { blue-bits [ cBlueBits>> ] }
- { alpha-bits [ cAlphaBits>> ] }
- { accum-bits [ cAccumBits>> ] }
- { accum-red-bits [ cAccumRedBits>> ] }
- { accum-green-bits [ cAccumGreenBits>> ] }
- { accum-blue-bits [ cAccumBlueBits>> ] }
- { accum-alpha-bits [ cAccumAlphaBits>> ] }
- { depth-bits [ cDepthBits>> ] }
- { stencil-bits [ cStencilBits>> ] }
- { aux-buffers [ cAuxBuffers>> ] }
- [ 2drop f ]
- } case ;
-
-: pfd-pixel-format-attribute ( pixel-format attribute -- value )
- [ get-pfd ] dip (pfd-pixel-format-attribute) ;
-
M: windows-ui-backend (make-pixel-format)
over has-wglChoosePixelFormatARB?
[ arb-make-pixel-format ] [ pfd-make-pixel-format ] if ;
M: windows-ui-backend (free-pixel-format)
drop ;
-M: windows-ui-backend (pixel-format-attribute)
- over world>> has-wglChoosePixelFormatARB?
- [ arb-pixel-format-attribute ] [ pfd-pixel-format-attribute ] if ;
-
PRIVATE>
: GET_APPCOMMAND_LPARAM ( lParam -- appCommand )
{ minimize-button 0 }
{ maximize-button 0 }
{ resize-handles $ WS_EX_WINDOWEDGE }
- { small-title-bar $[ WS_EX_TOOLWINDOW WS_EX_TOPMOST bitor ] }
+ { small-title-bar flags{ WS_EX_TOOLWINDOW WS_EX_TOPMOST } }
{ normal-title-bar $ WS_EX_APPWINDOW }
{ dialog-window 0 }
}
[ get-RECT-top-left ] [ get-RECT-width/height ] bi ;
: handle-wm-paint ( hWnd uMsg wParam lParam -- )
- #! wParam and lParam are unused
- #! only paint if width/height both > 0
- 3drop window relayout-1 yield ;
+ ! wParam and lParam are unused
+ ! only paint if width/height both > 0
+ 3drop window t >>active? relayout-1 yield ;
: handle-wm-size ( hWnd uMsg wParam lParam -- )
2nip
: send-key-up ( sym action? hWnd -- )
[ [ <key-up> ] ] dip send-key-gesture ;
-: key-sym ( wParam -- string/f action? )
- {
- {
- [ dup LETTER? ]
- [ shift? caps-lock? xor [ CHAR: a + CHAR: A - ] unless 1string f ]
- }
- { [ dup digit? ] [ 1string f ] }
- [ wm-keydown-codes at t ]
- } cond ;
+: key-sym ( wParam -- string/f )
+ wm-keydown-codes at ; inline
-:: handle-wm-keydown ( hWnd uMsg wParam lParam -- )
+:: (handle-wm-keydown/up) ( hWnd uMsg wParam lParam send-key-down/up -- )
wParam exclude-key-wm-keydown? [
- wParam key-sym over [
- dup ctrl? alt? xor or [
- hWnd send-key-down
- ] [ 2drop ] if
- ] [ 2drop ] if
- ] unless ;
+ wParam key-sym [
+ t hWnd send-key-down/up execute( sym action? hWnd -- )
+ ] [
+ 256 <byte-array> :> keyboard-state
+ 4 <byte-array> :> chars
+ lParam -16 shift 0xff bitand :> scan-code
+ keyboard-state GetKeyboardState win32-error=0/f
+ VK_CONTROL VK_CAPITAL [ 0 swap keyboard-state set-nth ] bi@
+ wParam scan-code keyboard-state chars 2 0 ToUnicode dup win32-error=0/f
+ 1 <= [
+ 1 chars nth 8 shift 0 chars nth bitor
+ ] [
+ 3 chars nth 8 shift 2 chars nth bitor ! dead-key
+ ] if
+ 1string f hWnd send-key-down/up execute( sym action? hWnd -- )
+ ] if*
+ ] unless ; inline
+
+: handle-wm-keydown ( hWnd uMsg wParam lParam -- )
+ \ send-key-down (handle-wm-keydown/up) ;
+
+SYMBOL: upper-surrogate-wm-char
:: handle-wm-char ( hWnd uMsg wParam lParam -- )
wParam exclude-key-wm-char? [
- ctrl? alt? xor [
- wParam 1string
- [ f hWnd send-key-down ]
- [ hWnd window user-input ] bi
+ ctrl? alt? xor [ ! enable AltGr combination inputs
+ wParam {
+ { [ dup upper-surrogate? ] [
+ upper-surrogate-wm-char set-global ]
+ }
+ { [ dup under-surrogate? ] [
+ drop
+ upper-surrogate-wm-char get-global [
+ wParam "" 2sequence
+ utf16n encode utf16n decode hWnd window user-input
+ ] when* ]
+ }
+ [ 1string hWnd window user-input
+ f upper-surrogate-wm-char set-global ]
+ } cond
] unless
] unless ;
-:: handle-wm-keyup ( hWnd uMsg wParam lParam -- )
- wParam exclude-key-wm-keydown? [
- wParam key-sym over [
- hWnd send-key-up
- ] [ 2drop ] if
- ] unless ;
+: handle-wm-keyup ( hWnd uMsg wParam lParam -- )
+ \ send-key-up (handle-wm-keydown/up) ;
:: set-window-active ( hwnd uMsg wParam lParam ? -- n )
? hwnd window active?<<
{ [ over SC_RESTORE = ] [ t set-window-active ] }
{ [ over SC_MAXIMIZE = ] [ t set-window-active ] }
{ [ dup alpha? ] [ 4drop 0 ] }
- { [ t ] [ DefWindowProc ] }
+ [ DefWindowProc ]
} cond ;
: cleanup-window ( handle -- )
drop
] [
[ SetCapture drop ] keep
- mouse-captured set
+ mouse-captured namespaces:set
] if ;
: release-capture ( -- )
: handle-wm-buttonup ( hWnd uMsg wParam lParam -- )
mouse-captured get [ release-capture ] when
pick message>button drop dup nc-buttons get member? [
- nc-buttons get remove! drop 4drop
+ nc-buttons get remove! 5drop
] [
drop prepare-mouse send-button-up
] if ;
wParam mouse-scroll hand-loc get-global hWnd window send-scroll ;
: handle-wm-cancelmode ( hWnd uMsg wParam lParam -- )
- #! message sent if windows needs application to stop dragging
+ ! message sent if windows needs application to stop dragging
4drop release-capture ;
: handle-wm-mouseleave ( hWnd uMsg wParam lParam -- )
- #! message sent if mouse leaves main application
+ ! message sent if mouse leaves main application
4drop forget-rollover ;
: system-background-color ( -- color )
SYMBOL: wm-handlers
-H{ } clone wm-handlers set-global
-
-: add-wm-handler ( quot wm -- )
+: add-wm-handler ( quot: ( hWnd Msg wParam lParam -- LRESULT ) wm -- )
dup array?
[ [ execute( -- wm ) add-wm-handler ] with each ]
[ wm-handlers get-global set-at ] if ;
: remove-wm-handler ( wm -- )
wm-handlers get-global delete-at ;
-[ handle-wm-close 0 ] WM_CLOSE add-wm-handler
-[ 4dup handle-wm-paint DefWindowProc ] WM_PAINT add-wm-handler
+wm-handlers [
+ H{
+ ${ WM_CLOSE [ handle-wm-close 0 ] }
+ ! ${ WM_NCCREATE [ [ 3drop EnableNonClientDpiScaling drop ] [ DefWindowProc ] 4bi ] }
+ ! ${ WM_GETDPISCALEDSIZE [ DefWindowProc ] }
+ ! ${ WM_DPICHANGED [ DefWindowProc ] }
+ ${ WM_PAINT [ 4dup handle-wm-paint DefWindowProc ] }
-[ handle-wm-size 0 ] WM_SIZE add-wm-handler
-[ handle-wm-move 0 ] WM_MOVE add-wm-handler
+ ${ WM_SIZE [ handle-wm-size 0 ] }
+ ${ WM_MOVE [ handle-wm-move 0 ] }
-[ 4dup handle-wm-keydown DefWindowProc ] { WM_KEYDOWN WM_SYSKEYDOWN } add-wm-handler
-[ 4dup handle-wm-char DefWindowProc ] { WM_CHAR WM_SYSCHAR } add-wm-handler
-[ 4dup handle-wm-keyup DefWindowProc ] { WM_KEYUP WM_SYSKEYUP } add-wm-handler
+ { ${ WM_CHAR WM_SYSCHAR } [ 4dup handle-wm-char DefWindowProc ] }
+ { ${ WM_KEYDOWN WM_SYSKEYDOWN } [ 4dup handle-wm-keydown DefWindowProc ] }
+ { ${ WM_KEYUP WM_SYSKEYUP } [ 4dup handle-wm-keyup DefWindowProc ] }
-[ handle-wm-syscommand ] WM_SYSCOMMAND add-wm-handler
-[ handle-wm-set-focus 0 ] WM_SETFOCUS add-wm-handler
-[ handle-wm-kill-focus 0 ] WM_KILLFOCUS add-wm-handler
+ ${ WM_SYSCOMMAND [ handle-wm-syscommand ] }
+ ${ WM_SETFOCUS [ handle-wm-set-focus 0 ] }
+ ${ WM_KILLFOCUS [ handle-wm-kill-focus 0 ] }
-[ handle-app-command 0 ] WM_APPCOMMAND add-wm-handler
+ ${ WM_APPCOMMAND [ handle-app-command 0 ] }
-[ handle-wm-buttondown 0 ] WM_LBUTTONDOWN add-wm-handler
-[ handle-wm-buttondown 0 ] WM_MBUTTONDOWN add-wm-handler
-[ handle-wm-buttondown 0 ] WM_RBUTTONDOWN add-wm-handler
-[ handle-wm-buttonup 0 ] WM_LBUTTONUP add-wm-handler
-[ handle-wm-buttonup 0 ] WM_MBUTTONUP add-wm-handler
-[ handle-wm-buttonup 0 ] WM_RBUTTONUP add-wm-handler
-[ handle-wm-dwmcompositionchanged 0 ] WM_DWMCOMPOSITIONCHANGED add-wm-handler
+ { ${ WM_LBUTTONDOWN WM_MBUTTONDOWN WM_RBUTTONDOWN } [ handle-wm-buttondown 0 ] }
+ { ${ WM_LBUTTONUP WM_MBUTTONUP WM_RBUTTONUP } [ handle-wm-buttonup 0 ] }
+ ${ WM_DWMCOMPOSITIONCHANGED [ handle-wm-dwmcompositionchanged 0 ] }
-[ 4dup handle-wm-ncbutton DefWindowProc ]
-{ WM_NCLBUTTONDOWN WM_NCMBUTTONDOWN WM_NCRBUTTONDOWN
-WM_NCLBUTTONUP WM_NCMBUTTONUP WM_NCRBUTTONUP }
-add-wm-handler
+ {
+ ${
+ WM_NCLBUTTONDOWN WM_NCMBUTTONDOWN WM_NCRBUTTONDOWN
+ WM_NCLBUTTONUP WM_NCMBUTTONUP WM_NCRBUTTONUP
+ } [ 4dup handle-wm-ncbutton DefWindowProc ]
+ }
-[ nc-buttons get-global delete-all DefWindowProc ]
-{ WM_EXITSIZEMOVE WM_EXITMENULOOP } add-wm-handler
+ { ${ WM_EXITMENULOOP WM_EXITSIZEMOVE } [ nc-buttons get-global delete-all DefWindowProc ] }
-[ handle-wm-mousemove 0 ] WM_MOUSEMOVE add-wm-handler
-[ handle-wm-mousewheel 0 ] WM_MOUSEWHEEL add-wm-handler
-[ handle-wm-cancelmode 0 ] WM_CANCELMODE add-wm-handler
-[ handle-wm-mouseleave 0 ] WM_MOUSELEAVE add-wm-handler
+ ${ WM_MOUSEMOVE [ handle-wm-mousemove 0 ] }
+ ${ WM_MOUSEWHEEL [ handle-wm-mousewheel 0 ] }
+ ${ WM_CANCELMODE [ handle-wm-cancelmode 0 ] }
+ ${ WM_MOUSELEAVE [ handle-wm-mouseleave 0 ] }
+ } expand-keys-set-at
+] initialize
SYMBOL: trace-messages?
! return 0 if you handle the message, else just let DefWindowProc return its val
: ui-wndproc ( -- object )
c:uint { c:void* c:uint WPARAM LPARAM } stdcall [
- pick
-
- trace-messages? get-global
- [ dup windows-message-name name>> print flush ] when
-
- wm-handlers get-global at*
- [ call( hWnd Msg wParam lParam -- result ) ] [ drop DefWindowProc ] if
- ] alien-callback ;
+ pick wm-handlers get-global at*
+ [ flush call( hWnd Msg wParam lParam -- result ) ] [ drop DefWindowProc ] if
+ ] alien-callback ;
: peek-message? ( msg -- ? ) f 0 0 PM_REMOVE PeekMessage zero? ;
0 >>cbWndExtra
f GetModuleHandle >>hInstance
f GetModuleHandle "APPICON" native-string>alien LoadIcon >>hIcon
- f IDC_ARROW LoadCursor >>hCursor
+ f IDC_ARROW MAKEINTRESOURCE LoadCursor >>hCursor
class-name-ptr >>lpszClassName
RegisterClassEx win32-error=0/f
] [ drop ] if ;
: adjust-RECT ( RECT style ex-style -- )
+ ! [ 0 ] dip GetDpiForSystem AdjustWindowRectExForDpi win32-error=0/f ;
[ 0 ] dip AdjustWindowRectEx win32-error=0/f ;
: make-RECT ( world -- RECT )
dup
] change-global ;
+: get-device-caps ( handle -- x y )
+ GetDC
+ [ LOGPIXELSX GetDeviceCaps ]
+ [ LOGPIXELSY GetDeviceCaps ] bi ;
+
+: get-default-device-caps ( -- x y )
+ f get-device-caps ;
+
:: create-window ( rect style ex-style -- hwnd )
rect style ex-style make-adjusted-RECT
[ get-window-class f ] dip
[
[ ex-style ] 2dip
- WS_CLIPSIBLINGS WS_CLIPCHILDREN bitor style bitor
+ flags{ WS_CLIPSIBLINGS WS_CLIPCHILDREN } style bitor
] dip get-RECT-dimensions
f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ;
swap window-controls>> close-button swap member? not
[ disable-close-button ] [ drop ] if ;
-M: windows-ui-backend (open-window) ( world -- )
+M: windows-ui-backend (open-window)
[
dup
[ ] [ world>style ] [ world>ex-style ] tri create-window
[ dup handle>> hWnd>> register-window ]
[ handle>> hWnd>> show-window ] tri ;
-M: win-base select-gl-context ( handle -- )
+M: win-base select-gl-context
[ hDC>> ] [ hRC>> ] bi wglMakeCurrent win32-error=0/f
GdiFlush drop ;
-M: win-base flush-gl-context ( handle -- )
+M: win-base flush-gl-context
hDC>> SwapBuffers win32-error=0/f ;
-: setup-offscreen-gl ( world -- )
- dup [ handle>> ] [ dim>> ] bi make-offscreen-dc-and-bitmap
- [ >>hDC ] [ >>hBitmap ] [ >>bits ] tri* drop [
- swap [ handle>> hDC>> set-pixel-format ] [ get-rc ] bi
- ] with-world-pixel-format ;
-
-M: windows-ui-backend (open-offscreen-buffer) ( world -- )
- win-offscreen new >>handle
- setup-offscreen-gl ;
-
-M: windows-ui-backend (close-offscreen-buffer) ( handle -- )
- [ hDC>> DeleteDC drop ]
- [ hBitmap>> DeleteObject drop ] bi ;
-
! Windows 32-bit bitmaps don't actually use the alpha byte of
! each pixel; it's left as zero
: (opaque-pixels) ( world -- pixels )
[ handle>> bits>> ] [ dim>> ] bi bitmap>byte-array (make-opaque) ;
-M: windows-ui-backend offscreen-pixels ( world -- alien w h )
- [ (opaque-pixels) ] [ dim>> first2 ] bi ;
-
-M: windows-ui-backend raise-window* ( world -- )
+M: windows-ui-backend raise-window*
handle>> [ hWnd>> SetFocus drop ] when* ;
-M: windows-ui-backend set-title ( string world -- )
+M: windows-ui-backend set-title
handle>>
dup title>> [ free ] when*
swap utf16n malloc-string
M: windows-ui-backend (with-ui)
[
- [
- init-clipboard
- init-win32-ui
- start-ui
- event-loop
- ] [ cleanup-win32-ui ] [ ] cleanup
- ] ui-running ;
-
-M: windows-ui-backend beep ( -- )
+ init-clipboard
+ init-win32-ui
+ start-ui
+ event-loop
+ ] [ cleanup-win32-ui ] finally ;
+
+M: windows-ui-backend beep
0 MessageBeep drop ;
M: windows-ui-backend system-alert
: hwnd>RECT ( hwnd -- RECT )
RECT <struct> [ GetWindowRect win32-error=0/f ] keep ;
-M: windows-ui-backend (grab-input) ( handle -- )
+M: windows-ui-backend (grab-input)
0 ShowCursor drop
hWnd>> client-area>RECT ClipCursor drop ;
-M: windows-ui-backend (ungrab-input) ( handle -- )
+M: windows-ui-backend (ungrab-input)
drop
f ClipCursor drop
1 ShowCursor drop ;
[ drop SW_RESTORE ShowWindow win32-error=0/f ]
} 2cleave ;
-M: windows-ui-backend (set-fullscreen) ( ? world -- )
+M: windows-ui-backend (set-fullscreen)
[ enter-fullscreen ] [ exit-fullscreen ] if ;
-M: windows-ui-backend (fullscreen?) ( world -- ? )
+M: windows-ui-backend (fullscreen?)
handle>> hWnd>>
[ hwnd>RECT ] [ fullscreen-RECT ] bi
[ get-RECT-dimensions 2array 2nip ] same? ;
-M: windows-ui-backend resize-window ( world dim -- )
- [ handle>> hWnd>> dup hwnd>RECT get-RECT-top-left ]
- [ first2 FALSE ] bi* MoveWindow win32-error=0/f ;
+M:: windows-ui-backend resize-window ( world dim -- )
+ world handle>> hWnd>>
+ world window-loc>> dim <RECT> dup
+ world world>style
+ world world>ex-style
+ adjust-RECT get-RECT-dimensions FALSE
+ MoveWindow win32-error=0/f ;
M: windows-ui-backend ui-backend-available?
t ;