Subject: Re: bug#10299: Emacs doesn't handle Unicode characters in keyboard layout on MS Windows

From: Joakim Hårsman <[email-masked]>
Date: Mon, 23 Jan 2012 20:15:15 +0100

I've amended the patch to take your comments into account.

Note that SetWindowText is already a macro that expands to
SetWindowTextA or SetWindowTextW depending on whether UNICODE is
defined. However, to retain compatibility with Windows 95, Emacs
doesn't define UNICODE and instead uses Unicode APIs directly where it
needs them. Still, the duplicated code was annoying, so I defined a
simple wrapper function which also gives a good place to document why
the code looks the way it does.

I also put the initialization of the window class structure into a
macro in w32.h, I wasn't entirely sure where to put it.

Here's the current version of the patch:

=== modified file 'src/w32.h'
--- src/w32.h   2011-10-28 09:54:02 +0000
+++ src/w32.h   2012-01-23 19:10:04 +0000
@@ -159,5 +159,18 @@
                                   const void* buf, size_t sz);
 #endif /* HAVE_GNUTLS */

+#define INIT_WINDOW_CLASS(wc)                   \
+    wc.style = CS_HREDRAW | CS_VREDRAW;         \
+    wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;    \
+    wc.cbClsExtra = 0;                          \
+    wc.cbWndExtra = WND_EXTRA_BYTES;            \
+    wc.hInstance = hinst;                       \
+    wc.hIcon = LoadIcon (hinst, EMACS_CLASS);   \
+    wc.hCursor = w32_load_cursor (IDC_ARROW);   \
+    wc.hbrBackground = NULL;                    \
+    wc.lpszMenuName = NULL;                     \
+
+
+
 #endif /* EMACS_W32_H */

=== modified file 'src/w32fns.c'
--- src/w32fns.c        2011-12-04 08:02:42 +0000
+++ src/w32fns.c        2012-01-23 19:10:04 +0000
@@ -1641,6 +1641,20 @@

 }

+/* Set text of w32 frame with handle HWND to TEXT.
+
+   We explicitly switch between the Unicode and ANSI version of
+   SetWindowText because Emacs isn't compiled with UNICODE defined to
+   retain compatibility with Windows 95. */
+
+void
+w32_set_frame_text(HWND hwnd, LPCSTR text)
+{
+  if (os_subtype == OS_NT)
+    SetWindowTextW (hwnd, (LPCWSTR)text);
+  else
+    SetWindowTextA (hwnd, text);
+}

 /* Change the name of frame F to NAME.  If NAME is nil, set F's name to
        w32_id_name.
@@ -1697,10 +1711,10 @@
   if (FRAME_W32_WINDOW (f))
     {
       if (STRING_MULTIBYTE (name))
-       name = ENCODE_SYSTEM (name);
-
+        name = ENCODE_SYSTEM (name);
+
       BLOCK_INPUT;
-      SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
+      w32_set_frame_text(FRAME_W32_WINDOW (f), SDATA (name));
       UNBLOCK_INPUT;
     }
 }
@@ -1746,7 +1760,7 @@
        name = ENCODE_SYSTEM (name);

       BLOCK_INPUT;
-      SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
+      w32_set_frame_text(FRAME_W32_WINDOW (f), SDATA (name));
       UNBLOCK_INPUT;
     }
 }
@@ -1785,20 +1799,23 @@
 static BOOL
 w32_init_class (HINSTANCE hinst)
 {
-  WNDCLASS wc;
-
-  wc.style = CS_HREDRAW | CS_VREDRAW;
-  wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
-  wc.cbClsExtra = 0;
-  wc.cbWndExtra = WND_EXTRA_BYTES;
-  wc.hInstance = hinst;
-  wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
-  wc.hCursor = w32_load_cursor (IDC_ARROW);
-  wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH);  */
-  wc.lpszMenuName = NULL;
-  wc.lpszClassName = EMACS_CLASS;
-
-  return (RegisterClass (&wc));
+
+  if (os_subtype == OS_NT)
+    {
+      WNDCLASSW  uwc;
+      INIT_WINDOW_CLASS(uwc);
+      uwc.lpszClassName = L"Emacs";
+
+      return (RegisterClassW (&uwc));
+    }
+  else
+    {
+      WNDCLASS  wc;
+      INIT_WINDOW_CLASS(wc);
+      wc.lpszClassName = EMACS_CLASS;
+
+      return (RegisterClass (&wc));
+    }
 }

 static HWND
@@ -2248,8 +2265,16 @@

   msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);

-  while (GetMessage (&msg, NULL, 0, 0))
+  while (1)
     {
+      if (os_subtype == OS_NT)
+        result = GetMessageW (&msg, NULL, 0, 0);
+      else
+        result = GetMessage (&msg, NULL, 0, 0);
+
+      if (!result)
+        break;
+
       if (msg.hwnd == NULL)
        {
          switch (msg.message)
@@ -2915,8 +2940,21 @@

     case WM_SYSCHAR:
     case WM_CHAR:
-      post_character_message (hwnd, msg, wParam, lParam,
-                             w32_get_key_modifiers (wParam, lParam));
+      if (wParam > 255 )
+        {
+          unsigned short lo = wParam & 0x0000FFFF;
+          unsigned short hi = (wParam & 0xFFFF0000) >> 8;
+          wParam  = hi | lo;
+
+          W32Msg wmsg;
+          wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
+          signal_user_input ();
+          my_post_msg (&wmsg, hwnd, WM_UNICHAR, wParam, lParam);
+
+        }
+      else
+        post_character_message (hwnd, msg, wParam, lParam,
+                                w32_get_key_modifiers (wParam, lParam));
       break;

     case WM_UNICHAR:

On 16 January 2012 15:03, Stefan Monnier <[email-masked]> wrote:
> Note: I know very little about w32.
>
>> -      SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
>> +      if (os_subtype == OS_NT)
>> +        SetWindowTextW (FRAME_W32_WINDOW (f), SDATA (name));
>> +      else
>> +        SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
>
> Can we move the test elsewhere by defining (or #defining) our own
> SetWindowText which uses either of the two?
>
>> @@ -1785,20 +1791,39 @@
>>  static BOOL
>>  w32_init_class (HINSTANCE hinst)
>>  {
>> -  WNDCLASS wc;
>> -
>> -  wc.style = CS_HREDRAW | CS_VREDRAW;
>> -  wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
>> -  wc.cbClsExtra = 0;
>> -  wc.cbWndExtra = WND_EXTRA_BYTES;
>> -  wc.hInstance = hinst;
>> -  wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
>> -  wc.hCursor = w32_load_cursor (IDC_ARROW);
>> -  wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH);  */
>> -  wc.lpszMenuName = NULL;
>> -  wc.lpszClassName = EMACS_CLASS;
>> -
>> -  return (RegisterClass (&wc));
>> +  WNDCLASSW uwc;
>> +  WNDCLASS  wc;
>> +
>> +  if (os_subtype == OS_NT)
>> +    {
>> +      uwc.style = CS_HREDRAW | CS_VREDRAW;
>> +      uwc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
>> +      uwc.cbClsExtra = 0;
>> +      uwc.cbWndExtra = WND_EXTRA_BYTES;
>> +      uwc.hInstance = hinst;
>> +      uwc.hIcon = LoadIcon (hinst, EMACS_CLASS);
>> +      uwc.hCursor = w32_load_cursor (IDC_ARROW);
>> +      uwc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH);  */
>> +      uwc.lpszMenuName = NULL;
>> +      uwc.lpszClassName = L"Emacs";
>> +
>> +      return (RegisterClassW (&uwc));
>> +    }
>> +  else
>> +    {
>> +      wc.style = CS_HREDRAW | CS_VREDRAW;
>> +      wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
>> +      wc.cbClsExtra = 0;
>> +      wc.cbWndExtra = WND_EXTRA_BYTES;
>> +      wc.hInstance = hinst;
>> +      wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
>> +      wc.hCursor = w32_load_cursor (IDC_ARROW);
>> +      wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH);  */
>> +      wc.lpszMenuName = NULL;
>> +      wc.lpszClassName = EMACS_CLASS;
>> +
>> +      return (RegisterClass (&wc));
>> +    }
>>  }
>
> The two var declarations (WNDCLASSW uwc and WNDCLASS wc) should be moved
> within their respective branch.  And it'd be better if we could share
> some code between the two branches, e.g. using a macro.
>
>
>        Stefan