]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/ui/backend/windows/windows.factor
basis: Fix Windows 10 select-all for emojis.
[factor.git] / basis / ui / backend / windows / windows.factor
old mode 100755 (executable)
new mode 100644 (file)
index 75a97c7..ff60eea
@@ -9,7 +9,9 @@ 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 ;
+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
@@ -321,40 +323,56 @@ CONSTANT: exclude-keys-wm-char
 : 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 [
+                          1string wParam 1string 2array "" join
+                          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?<<
@@ -458,7 +476,7 @@ SYMBOL: nc-buttons
 : 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 ;
@@ -578,7 +596,7 @@ M: windows-ui-backend do-events
         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
@@ -659,7 +677,7 @@ M: windows-ui-backend do-events
     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
@@ -670,11 +688,11 @@ M: windows-ui-backend (open-window) ( world -- )
     [ 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 ;
 
 ! Windows 32-bit bitmaps don't actually use the alpha byte of
@@ -688,10 +706,10 @@ M: win-base flush-gl-context ( handle -- )
 : (opaque-pixels) ( world -- pixels )
     [ handle>> bits>> ] [ dim>> ] bi bitmap>byte-array (make-opaque) ;
 
-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
@@ -704,9 +722,9 @@ M: windows-ui-backend (with-ui)
         init-win32-ui
         start-ui
         event-loop
-    ] [ cleanup-win32-ui ] [ ] cleanup ;
+    ] [ cleanup-win32-ui ] finally ;
 
-M: windows-ui-backend beep ( -- )
+M: windows-ui-backend beep
     0 MessageBeep drop ;
 
 M: windows-ui-backend system-alert
@@ -727,11 +745,11 @@ 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 ;
@@ -769,17 +787,21 @@ CONSTANT: fullscreen-flags flags{ WS_CAPTION WS_BORDER WS_THICKFRAME }
         [ 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 ;