src/video/windows/SDL_windowskeyboard.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 21:20:30 -0800
changeset 5090 327f181542f1
parent 5086 c2539ff054c8
child 5108 d68e9722ff4d
permissions -rw-r--r--
Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.

I think this also fixes the bug relating to non-latin characters in filenames, since UNICODE wasn't defined in SDL_rwops.c
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #ifdef _WIN32_WCE
    25 #define SDL_DISABLE_WINDOWS_IME
    26 #endif
    27 
    28 #include "SDL_windowsvideo.h"
    29 
    30 #include "../../events/SDL_keyboard_c.h"
    31 #include "../../events/scancodes_windows.h"
    32 
    33 #include <imm.h>
    34 #include <oleauto.h>
    35 
    36 #ifndef SDL_DISABLE_WINDOWS_IME
    37 static void IME_Init(SDL_VideoData *videodata, HWND hwnd);
    38 static void IME_Enable(SDL_VideoData *videodata, HWND hwnd);
    39 static void IME_Disable(SDL_VideoData *videodata, HWND hwnd);
    40 static void IME_Quit(SDL_VideoData *videodata);
    41 #endif /* !SDL_DISABLE_WINDOWS_IME */
    42 
    43 #ifndef MAPVK_VK_TO_VSC
    44 #define MAPVK_VK_TO_VSC     0
    45 #endif
    46 #ifndef MAPVK_VSC_TO_VK
    47 #define MAPVK_VSC_TO_VK     1
    48 #endif
    49 #ifndef MAPVK_VK_TO_CHAR
    50 #define MAPVK_VK_TO_CHAR    2
    51 #endif
    52 
    53 /* Alphabetic scancodes for PC keyboards */
    54 BYTE alpha_scancodes[26] = {
    55     30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24,
    56     25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44
    57 };
    58 
    59 BYTE keypad_scancodes[10] = {
    60     82, 79, 80, 81, 75, 76, 77, 71, 72, 73
    61 };
    62 
    63 void
    64 WIN_InitKeyboard(_THIS)
    65 {
    66     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    67     int i;
    68 
    69     /* Make sure the alpha scancodes are correct.  T isn't usually remapped */
    70     if (MapVirtualKey('T', MAPVK_VK_TO_VSC) != alpha_scancodes['T' - 'A']) {
    71 #if 0
    72         printf
    73             ("Fixing alpha scancode map, assuming US QWERTY layout!\nPlease send the following 26 lines of output to the SDL mailing list <sdl@libsdl.org>, including a description of your keyboard hardware.\n");
    74 #endif
    75         for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
    76             alpha_scancodes[i] = MapVirtualKey('A' + i, MAPVK_VK_TO_VSC);
    77 #if 0
    78             printf("%d = %d\n", i, alpha_scancodes[i]);
    79 #endif
    80         }
    81     }
    82     if (MapVirtualKey(VK_NUMPAD0, MAPVK_VK_TO_VSC) != keypad_scancodes[0]) {
    83 #if 0
    84         printf
    85             ("Fixing keypad scancode map!\nPlease send the following 10 lines of output to the SDL mailing list <sdl@libsdl.org>, including a description of your keyboard hardware.\n");
    86 #endif
    87         for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
    88             keypad_scancodes[i] =
    89                 MapVirtualKey(VK_NUMPAD0 + i, MAPVK_VK_TO_VSC);
    90 #if 0
    91             printf("%d = %d\n", i, keypad_scancodes[i]);
    92 #endif
    93         }
    94     }
    95 
    96     data->key_layout = windows_scancode_table;
    97 
    98     data->ime_com_initialized = SDL_FALSE;
    99     data->ime_threadmgr = 0;
   100     data->ime_initialized = SDL_FALSE;
   101     data->ime_enabled = SDL_FALSE;
   102     data->ime_available = SDL_FALSE;
   103     data->ime_hwnd_main = 0;
   104     data->ime_hwnd_current = 0;
   105     data->ime_himc = 0;
   106     data->ime_composition[0] = 0;
   107     data->ime_readingstring[0] = 0;
   108     data->ime_cursor = 0;
   109 
   110     data->ime_candlist = SDL_FALSE;
   111     SDL_memset(data->ime_candidates, 0, sizeof(data->ime_candidates));
   112     data->ime_candcount = 0;
   113     data->ime_candref = 0;
   114     data->ime_candsel = 0;
   115     data->ime_candpgsize = 0;
   116     data->ime_candlistindexbase = 0;
   117     data->ime_candvertical = SDL_TRUE;
   118 
   119     data->ime_candtex = NULL;
   120     data->ime_dirty = SDL_FALSE;
   121     SDL_memset(&data->ime_rect, 0, sizeof(data->ime_rect));
   122     SDL_memset(&data->ime_candlistrect, 0, sizeof(data->ime_candlistrect));
   123     data->ime_winwidth = 0;
   124     data->ime_winheight = 0;
   125 
   126     data->ime_hkl = 0;
   127     data->ime_himm32 = 0;
   128     data->GetReadingString = 0;
   129     data->ShowReadingWindow = 0;
   130     data->ImmLockIMC = 0;
   131     data->ImmUnlockIMC = 0;
   132     data->ImmLockIMCC = 0;
   133     data->ImmUnlockIMCC = 0;
   134     data->ime_uiless = SDL_FALSE;
   135     data->ime_threadmgrex = 0;
   136     data->ime_uielemsinkcookie = TF_INVALID_COOKIE;
   137     data->ime_alpnsinkcookie = TF_INVALID_COOKIE;
   138     data->ime_openmodesinkcookie = TF_INVALID_COOKIE;
   139     data->ime_convmodesinkcookie = TF_INVALID_COOKIE;
   140     data->ime_uielemsink = 0;
   141     data->ime_ippasink = 0;
   142 
   143     WIN_UpdateKeymap();
   144 
   145     SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
   146     SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows");
   147     SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Windows");
   148 }
   149 
   150 void
   151 WIN_UpdateKeymap()
   152 {
   153     int i;
   154     SDL_ScanCode scancode;
   155     SDLKey keymap[SDL_NUM_SCANCODES];
   156 
   157     SDL_GetDefaultKeymap(keymap);
   158 
   159     for (i = 0; i < SDL_arraysize(windows_scancode_table); i++) {
   160 
   161         /* Make sure this scancode is a valid character scancode */
   162         scancode = windows_scancode_table[i];
   163         if (scancode == SDL_SCANCODE_UNKNOWN || keymap[scancode] >= 127) {
   164             continue;
   165         }
   166 
   167         /* Alphabetic keys are handled specially, since Windows remaps them */
   168         if (i >= 'A' && i <= 'Z') {
   169             BYTE vsc = alpha_scancodes[i - 'A'];
   170             keymap[scancode] = MapVirtualKey(vsc, MAPVK_VSC_TO_VK) + 0x20;
   171         } else {
   172             keymap[scancode] = (MapVirtualKey(i, MAPVK_VK_TO_CHAR) & 0x7FFF);
   173         }
   174     }
   175     SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
   176 }
   177 
   178 void
   179 WIN_QuitKeyboard(_THIS)
   180 {
   181 #ifndef SDL_DISABLE_WINDOWS_IME
   182     IME_Quit((SDL_VideoData *)_this->driverdata);
   183 #endif
   184 }
   185 
   186 void
   187 WIN_StartTextInput(_THIS)
   188 {
   189 #ifndef SDL_DISABLE_WINDOWS_IME
   190     SDL_Window *window = SDL_GetKeyboardFocus();
   191     if (window) {
   192         HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
   193         SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   194         SDL_GetWindowSize(window, &videodata->ime_winwidth, &videodata->ime_winheight);
   195         IME_Init(videodata, hwnd);
   196         IME_Enable(videodata, hwnd);
   197     }
   198 #endif /* !SDL_DISABLE_WINDOWS_IME */
   199 }
   200 
   201 void
   202 WIN_StopTextInput(_THIS)
   203 {
   204 #ifndef SDL_DISABLE_WINDOWS_IME
   205     SDL_Window *window = SDL_GetKeyboardFocus();
   206     if (window) {
   207         HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
   208         SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   209         IME_Init(videodata, hwnd);
   210         IME_Disable(videodata, hwnd);
   211     }
   212 #endif /* !SDL_DISABLE_WINDOWS_IME */
   213 }
   214 
   215 void
   216 WIN_SetTextInputRect(_THIS, SDL_Rect *rect)
   217 {
   218     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   219     videodata->ime_rect = *rect;
   220 }
   221 
   222 #ifdef SDL_DISABLE_WINDOWS_IME
   223 
   224 
   225 SDL_bool
   226 IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata)
   227 {
   228     return SDL_FALSE;
   229 }
   230 
   231 void IME_Present(SDL_VideoData *videodata)
   232 {
   233 }
   234 
   235 #else
   236 
   237 #ifdef __GNUC__
   238 #undef DEFINE_GUID
   239 #define DEFINE_GUID(n,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) static const GUID n = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}
   240 DEFINE_GUID(IID_ITfInputProcessorProfileActivationSink,        0x71C6E74E,0x0F28,0x11D8,0xA8,0x2A,0x00,0x06,0x5B,0x84,0x43,0x5C);
   241 DEFINE_GUID(IID_ITfUIElementSink,                              0xEA1EA136,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
   242 DEFINE_GUID(GUID_TFCAT_TIP_KEYBOARD,                           0x34745C63,0xB2F0,0x4784,0x8B,0x67,0x5E,0x12,0xC8,0x70,0x1A,0x31);
   243 DEFINE_GUID(IID_ITfSource,                                     0x4EA48A35,0x60AE,0x446F,0x8F,0xD6,0xE6,0xA8,0xD8,0x24,0x59,0xF7);
   244 DEFINE_GUID(IID_ITfUIElementMgr,                               0xEA1EA135,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
   245 DEFINE_GUID(IID_ITfCandidateListUIElement,                     0xEA1EA138,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
   246 DEFINE_GUID(IID_ITfReadingInformationUIElement,                0xEA1EA139,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
   247 DEFINE_GUID(IID_ITfThreadMgr,                                  0xAA80E801,0x2021,0x11D2,0x93,0xE0,0x00,0x60,0xB0,0x67,0xB8,0x6E);
   248 DEFINE_GUID(CLSID_TF_ThreadMgr,                                0x529A9E6B,0x6587,0x4F23,0xAB,0x9E,0x9C,0x7D,0x68,0x3E,0x3C,0x50);
   249 DEFINE_GUID(IID_ITfThreadMgrEx,                                0x3E90ADE3,0x7594,0x4CB0,0xBB,0x58,0x69,0x62,0x8F,0x5F,0x45,0x8C);
   250 #endif
   251 
   252 #define LANG_CHT MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL)
   253 #define LANG_CHS MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED)
   254 
   255 #define MAKEIMEVERSION(major,minor) ((DWORD) (((BYTE)(major) << 24) | ((BYTE)(minor) << 16) ))
   256 #define IMEID_VER(id) ((id) & 0xffff0000)
   257 #define IMEID_LANG(id) ((id) & 0x0000ffff)
   258 
   259 #define CHT_HKL_DAYI            ((HKL)0xE0060404)
   260 #define CHT_HKL_NEW_PHONETIC    ((HKL)0xE0080404)
   261 #define CHT_HKL_NEW_CHANG_JIE   ((HKL)0xE0090404)
   262 #define CHT_HKL_NEW_QUICK       ((HKL)0xE00A0404)
   263 #define CHT_HKL_HK_CANTONESE    ((HKL)0xE00B0404)
   264 #define CHT_IMEFILENAME1        "TINTLGNT.IME"
   265 #define CHT_IMEFILENAME2        "CINTLGNT.IME"
   266 #define CHT_IMEFILENAME3        "MSTCIPHA.IME"
   267 #define IMEID_CHT_VER42         (LANG_CHT | MAKEIMEVERSION(4, 2))
   268 #define IMEID_CHT_VER43         (LANG_CHT | MAKEIMEVERSION(4, 3))
   269 #define IMEID_CHT_VER44         (LANG_CHT | MAKEIMEVERSION(4, 4))
   270 #define IMEID_CHT_VER50         (LANG_CHT | MAKEIMEVERSION(5, 0))
   271 #define IMEID_CHT_VER51         (LANG_CHT | MAKEIMEVERSION(5, 1))
   272 #define IMEID_CHT_VER52         (LANG_CHT | MAKEIMEVERSION(5, 2))
   273 #define IMEID_CHT_VER60         (LANG_CHT | MAKEIMEVERSION(6, 0))
   274 #define IMEID_CHT_VER_VISTA     (LANG_CHT | MAKEIMEVERSION(7, 0))
   275 
   276 #define CHS_HKL                 ((HKL)0xE00E0804)
   277 #define CHS_IMEFILENAME1        "PINTLGNT.IME"
   278 #define CHS_IMEFILENAME2        "MSSCIPYA.IME"
   279 #define IMEID_CHS_VER41         (LANG_CHS | MAKEIMEVERSION(4, 1))
   280 #define IMEID_CHS_VER42         (LANG_CHS | MAKEIMEVERSION(4, 2))
   281 #define IMEID_CHS_VER53         (LANG_CHS | MAKEIMEVERSION(5, 3))
   282 
   283 #define LANG() LOWORD((videodata->ime_hkl))
   284 #define PRIMLANG() ((WORD)PRIMARYLANGID(LANG()))
   285 #define SUBLANG() SUBLANGID(LANG())
   286 
   287 static void IME_UpdateInputLocale(SDL_VideoData *videodata);
   288 static void IME_ClearComposition(SDL_VideoData *videodata);
   289 static void IME_SetWindow(SDL_VideoData* videodata, HWND hwnd);
   290 static void IME_SetupAPI(SDL_VideoData *videodata);
   291 static DWORD IME_GetId(SDL_VideoData *videodata, UINT uIndex);
   292 static void IME_SendEditingEvent(SDL_VideoData *videodata);
   293 static void IME_DestroyTextures(SDL_VideoData *videodata);
   294 
   295 #define SDL_IsEqualIID(riid1, riid2) SDL_IsEqualGUID(riid1, riid2)
   296 #define SDL_IsEqualGUID(rguid1, rguid2) (!SDL_memcmp(rguid1, rguid2, sizeof(GUID)))
   297 
   298 static SDL_bool UILess_SetupSinks(SDL_VideoData *videodata);
   299 static void UILess_ReleaseSinks(SDL_VideoData *videodata);
   300 static void UILess_EnableUIUpdates(SDL_VideoData *videodata);
   301 static void UILess_DisableUIUpdates(SDL_VideoData *videodata);
   302 
   303 static void
   304 IME_Init(SDL_VideoData *videodata, HWND hwnd)
   305 {
   306     if (videodata->ime_initialized)
   307         return;
   308 
   309     videodata->ime_hwnd_main = hwnd;
   310     if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) {
   311         videodata->ime_com_initialized = SDL_TRUE;
   312         CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, (LPVOID *)&videodata->ime_threadmgr);
   313     }
   314     videodata->ime_initialized = SDL_TRUE;
   315     videodata->ime_himm32 = SDL_LoadObject("imm32.dll");
   316     if (!videodata->ime_himm32) {
   317         videodata->ime_available = SDL_FALSE;
   318         return;
   319     }
   320     videodata->ImmLockIMC = (LPINPUTCONTEXT2 (WINAPI *)(HIMC))SDL_LoadFunction(videodata->ime_himm32, "ImmLockIMC");
   321     videodata->ImmUnlockIMC = (BOOL (WINAPI *)(HIMC))SDL_LoadFunction(videodata->ime_himm32, "ImmUnlockIMC");
   322     videodata->ImmLockIMCC = (LPVOID (WINAPI *)(HIMCC))SDL_LoadFunction(videodata->ime_himm32, "ImmLockIMCC");
   323     videodata->ImmUnlockIMCC = (BOOL (WINAPI *)(HIMCC))SDL_LoadFunction(videodata->ime_himm32, "ImmUnlockIMCC");
   324 
   325     IME_SetWindow(videodata, hwnd);
   326     videodata->ime_himc = ImmGetContext(hwnd);
   327     ImmReleaseContext(hwnd, videodata->ime_himc);
   328     if (!videodata->ime_himc) {
   329         videodata->ime_available = SDL_FALSE;
   330         IME_Disable(videodata, hwnd);
   331         return;
   332     }
   333     videodata->ime_available = SDL_TRUE;
   334     IME_UpdateInputLocale(videodata);
   335     IME_SetupAPI(videodata);
   336     videodata->ime_uiless = UILess_SetupSinks(videodata);
   337     IME_UpdateInputLocale(videodata);
   338     IME_Disable(videodata, hwnd);
   339 }
   340 
   341 static void
   342 IME_Enable(SDL_VideoData *videodata, HWND hwnd)
   343 {
   344     if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
   345         return;
   346 
   347     if (!videodata->ime_available) {
   348         IME_Disable(videodata, hwnd);
   349         return;
   350     }
   351     if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
   352         ImmAssociateContext(videodata->ime_hwnd_current, videodata->ime_himc);
   353 
   354     videodata->ime_enabled = SDL_TRUE;
   355     IME_UpdateInputLocale(videodata);
   356     UILess_EnableUIUpdates(videodata);
   357 }
   358 
   359 static void
   360 IME_Disable(SDL_VideoData *videodata, HWND hwnd)
   361 {
   362     if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
   363         return;
   364 
   365     IME_ClearComposition(videodata);
   366     if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
   367         ImmAssociateContext(videodata->ime_hwnd_current, (HIMC)0);
   368 
   369     videodata->ime_enabled = SDL_FALSE;
   370     UILess_DisableUIUpdates(videodata);
   371 }
   372 
   373 static void
   374 IME_Quit(SDL_VideoData *videodata)
   375 {
   376     if (!videodata->ime_initialized)
   377         return;
   378 
   379     UILess_ReleaseSinks(videodata);
   380     if (videodata->ime_hwnd_main)
   381         ImmAssociateContext(videodata->ime_hwnd_main, videodata->ime_himc);
   382 
   383     videodata->ime_hwnd_main = 0;
   384     videodata->ime_himc = 0;
   385     if (videodata->ime_himm32) {
   386         SDL_UnloadObject(videodata->ime_himm32);
   387         videodata->ime_himm32 = 0;
   388     }
   389     if (videodata->ime_threadmgr) {
   390         videodata->ime_threadmgr->lpVtbl->Release(videodata->ime_threadmgr);
   391         videodata->ime_threadmgr = 0;
   392     }
   393     if (videodata->ime_com_initialized) {
   394         CoUninitialize();
   395         videodata->ime_com_initialized = SDL_FALSE;
   396     }
   397     IME_DestroyTextures(videodata);
   398     videodata->ime_initialized = SDL_FALSE;
   399 }
   400 
   401 static void
   402 IME_GetReadingString(SDL_VideoData *videodata, HWND hwnd)
   403 {
   404     DWORD id = 0;
   405     HIMC himc = 0;
   406     WCHAR buffer[16];
   407     WCHAR *s = buffer;
   408     DWORD len = 0;
   409     INT err = 0;
   410     BOOL vertical = FALSE;
   411     UINT maxuilen = 0;
   412     static OSVERSIONINFOA osversion = {0};
   413     if (videodata->ime_uiless)
   414         return;
   415 
   416     videodata->ime_readingstring[0] = 0;
   417     if (!osversion.dwOSVersionInfoSize) {
   418         osversion.dwOSVersionInfoSize = sizeof(osversion);
   419         GetVersionExA(&osversion);
   420     }
   421     id = IME_GetId(videodata, 0);
   422     if (!id)
   423         return;
   424 
   425     himc = ImmGetContext(hwnd);
   426     if (!himc)
   427         return;
   428 
   429     if (videodata->GetReadingString) {
   430         len = videodata->GetReadingString(himc, 0, 0, &err, &vertical, &maxuilen);
   431         if (len) {
   432             if (len > SDL_arraysize(buffer))
   433                 len = SDL_arraysize(buffer);
   434 
   435             len = videodata->GetReadingString(himc, len, s, &err, &vertical, &maxuilen);
   436         }
   437         SDL_wcslcpy(videodata->ime_readingstring, s, len);
   438     }
   439     else {
   440         LPINPUTCONTEXT2 lpimc = videodata->ImmLockIMC(himc);
   441         LPBYTE p = 0;
   442         s = 0;
   443         switch (id)
   444         {
   445         case IMEID_CHT_VER42:
   446         case IMEID_CHT_VER43:
   447         case IMEID_CHT_VER44:
   448             p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 24);
   449             if (!p)
   450                 break;
   451 
   452             len = *(DWORD *)(p + 7*4 + 32*4);
   453             s = (WCHAR *)(p + 56);
   454             break;
   455         case IMEID_CHT_VER51:
   456         case IMEID_CHT_VER52:
   457         case IMEID_CHS_VER53:
   458             p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 4);
   459             if (!p)
   460                 break;
   461 
   462             p = *(LPBYTE *)((LPBYTE)p + 1*4 + 5*4);
   463             if (!p)
   464                 break;
   465 
   466             len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2);
   467             s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4);
   468             break;
   469         case IMEID_CHS_VER41:
   470             {
   471                 int offset = (IME_GetId(videodata, 1) >= 0x00000002) ? 8 : 7;
   472                 p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + offset * 4);
   473                 if (!p)
   474                     break;
   475 
   476                 len = *(DWORD *)(p + 7*4 + 16*2*4);
   477                 s = (WCHAR *)(p + 6*4 + 16*2*1);
   478             }
   479             break;
   480         case IMEID_CHS_VER42:
   481             if (osversion.dwPlatformId != VER_PLATFORM_WIN32_NT)
   482                 break;
   483 
   484             p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 1*4 + 1*4 + 6*4);
   485             if (!p)
   486                 break;
   487 
   488             len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2);
   489             s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4);
   490             break;
   491         }
   492         if (s)
   493             SDL_wcslcpy(videodata->ime_readingstring, s, len + 1);
   494 
   495         videodata->ImmUnlockIMCC(lpimc->hPrivate);
   496         videodata->ImmUnlockIMC(himc);
   497     }
   498     ImmReleaseContext(hwnd, himc);
   499     IME_SendEditingEvent(videodata);
   500 }
   501 
   502 static void
   503 IME_InputLangChanged(SDL_VideoData *videodata)
   504 {
   505     UINT lang = PRIMLANG();
   506     IME_UpdateInputLocale(videodata);
   507     if (!videodata->ime_uiless)
   508         videodata->ime_candlistindexbase = (videodata->ime_hkl == CHT_HKL_DAYI) ? 0 : 1;
   509 
   510     IME_SetupAPI(videodata);
   511     if (lang != PRIMLANG()) {
   512         IME_ClearComposition(videodata);
   513     }
   514 }
   515 
   516 static DWORD
   517 IME_GetId(SDL_VideoData *videodata, UINT uIndex)
   518 {
   519     static HKL hklprev = 0;
   520     static DWORD dwRet[2] = {0};
   521     DWORD dwVerSize = 0;
   522     DWORD dwVerHandle = 0;
   523     LPVOID lpVerBuffer = 0;
   524     LPVOID lpVerData = 0;
   525     UINT cbVerData = 0;
   526     char szTemp[256];
   527     HKL hkl = 0;
   528     DWORD dwLang = 0;
   529     if (uIndex >= sizeof(dwRet) / sizeof(dwRet[0]))
   530         return 0;
   531 
   532     hkl = videodata->ime_hkl;
   533     if (hklprev == hkl)
   534         return dwRet[uIndex];
   535 
   536     hklprev = hkl;
   537     dwLang = ((DWORD)hkl & 0xffff);
   538     if (videodata->ime_uiless && LANG() == LANG_CHT) {
   539         dwRet[0] = IMEID_CHT_VER_VISTA;
   540         dwRet[1] = 0;
   541         return dwRet[0];
   542     }
   543     if (hkl != CHT_HKL_NEW_PHONETIC
   544         && hkl != CHT_HKL_NEW_CHANG_JIE
   545         && hkl != CHT_HKL_NEW_QUICK
   546         && hkl != CHT_HKL_HK_CANTONESE
   547         && hkl != CHS_HKL) {
   548         dwRet[0] = dwRet[1] = 0;
   549         return dwRet[uIndex];
   550     }
   551     if (ImmGetIMEFileNameA(hkl, szTemp, sizeof(szTemp) - 1) <= 0) {
   552         dwRet[0] = dwRet[1] = 0;
   553         return dwRet[uIndex];
   554     }
   555     if (!videodata->GetReadingString) {
   556         #define LCID_INVARIANT MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT)
   557         if (CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME1, -1) != 2
   558             && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME2, -1) != 2
   559             && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME3, -1) != 2
   560             && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME1, -1) != 2
   561             && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME2, -1) != 2) {
   562             dwRet[0] = dwRet[1] = 0;
   563             return dwRet[uIndex];
   564         }
   565         #undef LCID_INVARIANT
   566         dwVerSize = GetFileVersionInfoSizeA(szTemp, &dwVerHandle);
   567         if (dwVerSize) {
   568             lpVerBuffer = SDL_malloc(dwVerSize);
   569             if (lpVerBuffer) {
   570                 if (GetFileVersionInfoA(szTemp, dwVerHandle, dwVerSize, lpVerBuffer)) {
   571                     if (VerQueryValueA(lpVerBuffer, "\\", &lpVerData, &cbVerData)) {
   572                         #define pVerFixedInfo   ((VS_FIXEDFILEINFO FAR*)lpVerData)
   573                         DWORD dwVer = pVerFixedInfo->dwFileVersionMS;
   574                         dwVer = (dwVer & 0x00ff0000) << 8 | (dwVer & 0x000000ff) << 16;
   575                         if (videodata->GetReadingString ||
   576                             dwLang == LANG_CHT && (
   577                             dwVer == MAKEIMEVERSION(4, 2) ||
   578                             dwVer == MAKEIMEVERSION(4, 3) ||
   579                             dwVer == MAKEIMEVERSION(4, 4) ||
   580                             dwVer == MAKEIMEVERSION(5, 0) ||
   581                             dwVer == MAKEIMEVERSION(5, 1) ||
   582                             dwVer == MAKEIMEVERSION(5, 2) ||
   583                             dwVer == MAKEIMEVERSION(6, 0))
   584                             ||
   585                             dwLang == LANG_CHS && (
   586                             dwVer == MAKEIMEVERSION(4, 1) ||
   587                             dwVer == MAKEIMEVERSION(4, 2) ||
   588                             dwVer == MAKEIMEVERSION(5, 3))) {
   589                             dwRet[0] = dwVer | dwLang;
   590                             dwRet[1] = pVerFixedInfo->dwFileVersionLS;
   591                             SDL_free(lpVerBuffer);
   592                             return dwRet[0];
   593                         }
   594                         #undef pVerFixedInfo
   595                     }
   596                 }
   597             }
   598             SDL_free(lpVerBuffer);
   599         }
   600     }
   601     dwRet[0] = dwRet[1] = 0;
   602     return dwRet[uIndex];
   603 }
   604 
   605 static void
   606 IME_SetupAPI(SDL_VideoData *videodata)
   607 {
   608     char ime_file[MAX_PATH + 1];
   609     void* hime = 0;
   610     HKL hkl = 0;
   611     videodata->GetReadingString = 0;
   612     videodata->ShowReadingWindow = 0;
   613     if (videodata->ime_uiless)
   614         return;
   615 
   616     hkl = videodata->ime_hkl;
   617     if (ImmGetIMEFileNameA(hkl, ime_file, sizeof(ime_file) - 1) <= 0)
   618         return;
   619 
   620     hime = SDL_LoadObject(ime_file);
   621     if (!hime)
   622         return;
   623 
   624     videodata->GetReadingString = (UINT (WINAPI *)(HIMC, UINT, LPWSTR, PINT, BOOL*, PUINT))
   625         SDL_LoadFunction(hime, "GetReadingString");
   626     videodata->ShowReadingWindow = (BOOL (WINAPI *)(HIMC, BOOL))
   627         SDL_LoadFunction(hime, "ShowReadingWindow");
   628 
   629     if (videodata->ShowReadingWindow) {
   630         HIMC himc = ImmGetContext(videodata->ime_hwnd_current);
   631         if (himc) {
   632             videodata->ShowReadingWindow(himc, FALSE);
   633             ImmReleaseContext(videodata->ime_hwnd_current, himc);
   634         }
   635     }
   636 }
   637 
   638 static void
   639 IME_SetWindow(SDL_VideoData* videodata, HWND hwnd)
   640 {
   641     videodata->ime_hwnd_current = hwnd;
   642     if (videodata->ime_threadmgr) {
   643         struct ITfDocumentMgr *document_mgr = 0;
   644         if (SUCCEEDED(videodata->ime_threadmgr->lpVtbl->AssociateFocus(videodata->ime_threadmgr, hwnd, NULL, &document_mgr))) {
   645             if (document_mgr)
   646                 document_mgr->lpVtbl->Release(document_mgr);
   647         }
   648     }
   649 }
   650 
   651 static void
   652 IME_UpdateInputLocale(SDL_VideoData *videodata)
   653 {
   654     static HKL hklprev = 0;
   655     videodata->ime_hkl = GetKeyboardLayout(0);
   656     if (hklprev == videodata->ime_hkl)
   657         return;
   658 
   659     hklprev = videodata->ime_hkl;
   660     switch (PRIMLANG()) {
   661     case LANG_CHINESE:
   662         videodata->ime_candvertical = SDL_TRUE;
   663         if (SUBLANG() == SUBLANG_CHINESE_SIMPLIFIED)
   664             videodata->ime_candvertical = SDL_FALSE;
   665 
   666         break;
   667     case LANG_JAPANESE:
   668         videodata->ime_candvertical = SDL_TRUE;
   669         break;
   670     case LANG_KOREAN:
   671         videodata->ime_candvertical = SDL_FALSE;
   672         break;
   673     }
   674 }
   675 
   676 static void
   677 IME_ClearComposition(SDL_VideoData *videodata)
   678 {
   679     HIMC himc = 0;
   680     if (!videodata->ime_initialized)
   681         return;
   682 
   683     himc = ImmGetContext(videodata->ime_hwnd_current);
   684     if (!himc)
   685         return;
   686 
   687     ImmNotifyIME(himc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
   688     if (videodata->ime_uiless)
   689         ImmSetCompositionString(himc, SCS_SETSTR, TEXT(""), sizeof(TCHAR), TEXT(""), sizeof(TCHAR));
   690 
   691     ImmNotifyIME(himc, NI_CLOSECANDIDATE, 0, 0);
   692     ImmReleaseContext(videodata->ime_hwnd_current, himc);
   693     SDL_SendEditingText("", 0, 0);
   694 }
   695 
   696 static void
   697 IME_GetCompositionString(SDL_VideoData *videodata, HIMC himc, DWORD string)
   698 {
   699     LONG length = ImmGetCompositionStringW(himc, string, videodata->ime_composition, sizeof(videodata->ime_composition));
   700     if (length < 0)
   701         length = 0;
   702 
   703     length /= sizeof(videodata->ime_composition[0]);
   704     videodata->ime_cursor = LOWORD(ImmGetCompositionStringW(himc, GCS_CURSORPOS, 0, 0));
   705     if (videodata->ime_composition[videodata->ime_cursor] == 0x3000) {
   706         int i;
   707         for (i = videodata->ime_cursor + 1; i < length; ++i)
   708             videodata->ime_composition[i - 1] = videodata->ime_composition[i];
   709 
   710         --length;
   711     }
   712     videodata->ime_composition[length] = 0;
   713 }
   714 
   715 static void
   716 IME_SendInputEvent(SDL_VideoData *videodata)
   717 {
   718     char *s = 0;
   719     s = WIN_StringToUTF8(videodata->ime_composition);
   720     SDL_SendKeyboardText(s);
   721     SDL_free(s);
   722 
   723     videodata->ime_composition[0] = 0;
   724     videodata->ime_readingstring[0] = 0;
   725     videodata->ime_cursor = 0;
   726 }
   727 
   728 static void
   729 IME_SendEditingEvent(SDL_VideoData *videodata)
   730 {
   731     char *s = 0;
   732     WCHAR buffer[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
   733     buffer[0] = 0;
   734     if (videodata->ime_readingstring[0]) {
   735         size_t len = SDL_min(SDL_wcslen(videodata->ime_composition), (size_t)videodata->ime_cursor);
   736         SDL_wcslcpy(buffer, videodata->ime_composition, len + 1);
   737         SDL_wcslcat(buffer, videodata->ime_readingstring, sizeof(buffer));
   738         SDL_wcslcat(buffer, &videodata->ime_composition[len], sizeof(buffer) - len);
   739     }
   740     else {
   741         SDL_wcslcpy(buffer, videodata->ime_composition, sizeof(videodata->ime_composition));
   742     }
   743     s = WIN_StringToUTF8(buffer);
   744     SDL_SendEditingText(s, videodata->ime_cursor + SDL_wcslen(videodata->ime_readingstring), 0);
   745     SDL_free(s);
   746 }
   747 
   748 static void
   749 IME_AddCandidate(SDL_VideoData *videodata, UINT i, LPCWSTR candidate)
   750 {
   751     LPWSTR dst = videodata->ime_candidates[i];
   752     *dst++ = (WCHAR)(TEXT('0') + ((i + videodata->ime_candlistindexbase) % 10));
   753     if (videodata->ime_candvertical)
   754         *dst++ = TEXT(' ');
   755 
   756     while (*candidate && (SDL_arraysize(videodata->ime_candidates[i]) > (dst - videodata->ime_candidates[i])))
   757         *dst++ = *candidate++;
   758 
   759     *dst = (WCHAR)'\0';
   760 }
   761 
   762 static void
   763 IME_GetCandidateList(HIMC himc, SDL_VideoData *videodata)
   764 {
   765     LPCANDIDATELIST cand_list = 0;
   766     DWORD size = ImmGetCandidateListW(himc, 0, 0, 0);
   767     if (size) {
   768         cand_list = (LPCANDIDATELIST)SDL_malloc(size);
   769         if (cand_list) {
   770             size = ImmGetCandidateListW(himc, 0, cand_list, size);
   771             if (size) {
   772                 int i = 0;
   773                 int j = 0;
   774                 int page_start = 0;
   775                 videodata->ime_candsel = cand_list->dwSelection;
   776                 videodata->ime_candcount = cand_list->dwCount;
   777 
   778                 if (LANG() == LANG_CHS && IME_GetId(videodata, 0)) {
   779                     const UINT maxcandchar = 18;
   780                     UINT i = 0;
   781                     UINT cchars = 0;
   782 
   783                     for (; i < videodata->ime_candcount; ++i) {
   784                         UINT len = SDL_wcslen((LPWSTR)((DWORD)cand_list + cand_list->dwOffset[i])) + 1;
   785                         if (len + cchars > maxcandchar) {
   786                             if (i > cand_list->dwSelection)
   787                                 break;
   788 
   789                             page_start = i;
   790                             cchars = len;
   791                         }
   792                         else {
   793                             cchars += len;
   794                         }
   795                     }
   796                     videodata->ime_candpgsize = i - page_start;
   797                 }
   798                 else {
   799                     videodata->ime_candpgsize = SDL_min(cand_list->dwPageSize, MAX_CANDLIST);
   800                     page_start = (cand_list->dwSelection / videodata->ime_candpgsize) * videodata->ime_candpgsize;
   801                 }
   802                 SDL_memset(&videodata->ime_candidates, 0, sizeof(videodata->ime_candidates));
   803                 for (i = page_start, j = 0; (DWORD)i < cand_list->dwCount && j < (int)videodata->ime_candpgsize; i++, j++) {
   804                     LPCWSTR candidate = (LPCWSTR)((DWORD)cand_list + cand_list->dwOffset[i]);
   805                     IME_AddCandidate(videodata, j, candidate);
   806                 }
   807                 if (PRIMLANG() == LANG_KOREAN || (PRIMLANG() == LANG_CHT && !IME_GetId(videodata, 0)))
   808                     videodata->ime_candsel = -1;
   809 
   810             }
   811             SDL_free(cand_list);
   812         }
   813     }
   814 }
   815 
   816 static void
   817 IME_ShowCandidateList(SDL_VideoData *videodata)
   818 {
   819     videodata->ime_dirty = SDL_TRUE;
   820     videodata->ime_candlist = SDL_TRUE;
   821     IME_DestroyTextures(videodata);
   822     IME_SendEditingEvent(videodata);
   823 }
   824 
   825 static void
   826 IME_HideCandidateList(SDL_VideoData *videodata)
   827 {
   828     videodata->ime_dirty = SDL_FALSE;
   829     videodata->ime_candlist = SDL_FALSE;
   830     IME_DestroyTextures(videodata);
   831     IME_SendEditingEvent(videodata);
   832 }
   833 
   834 SDL_bool
   835 IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata)
   836 {
   837     SDL_bool trap = SDL_FALSE;
   838     HIMC himc = 0;
   839     if (!videodata->ime_initialized || !videodata->ime_available || !videodata->ime_enabled)
   840         return SDL_FALSE;
   841 
   842     switch (msg) {
   843     case WM_INPUTLANGCHANGE:
   844         IME_InputLangChanged(videodata);
   845         break;
   846     case WM_IME_SETCONTEXT:
   847         *lParam = 0;
   848         break;
   849     case WM_IME_STARTCOMPOSITION:
   850         trap = SDL_TRUE;
   851         break;
   852     case WM_IME_COMPOSITION:
   853         trap = SDL_TRUE;
   854         himc = ImmGetContext(hwnd);
   855         if (*lParam & GCS_RESULTSTR) {
   856             IME_GetCompositionString(videodata, himc, GCS_RESULTSTR);
   857             IME_SendInputEvent(videodata);
   858         }
   859         if (*lParam & GCS_COMPSTR) {
   860             if (!videodata->ime_uiless)
   861                 videodata->ime_readingstring[0] = 0;
   862 
   863             IME_GetCompositionString(videodata, himc, GCS_COMPSTR);
   864             IME_SendEditingEvent(videodata);
   865         }
   866         ImmReleaseContext(hwnd, himc);
   867         break;
   868     case WM_IME_ENDCOMPOSITION:
   869         videodata->ime_composition[0] = 0;
   870         videodata->ime_readingstring[0] = 0;
   871         videodata->ime_cursor = 0;
   872         SDL_SendEditingText("", 0, 0);
   873         break;
   874     case WM_IME_NOTIFY:
   875         switch (wParam) {
   876         case IMN_SETCONVERSIONMODE:
   877         case IMN_SETOPENSTATUS:
   878             IME_UpdateInputLocale(videodata);
   879             break;
   880         case IMN_OPENCANDIDATE:
   881         case IMN_CHANGECANDIDATE:
   882             if (videodata->ime_uiless)
   883                 break;
   884 
   885             trap = SDL_TRUE;
   886             IME_ShowCandidateList(videodata);
   887             himc = ImmGetContext(hwnd);
   888             if (!himc)
   889                 break;
   890 
   891             IME_GetCandidateList(himc, videodata);
   892             ImmReleaseContext(hwnd, himc);
   893             break;
   894         case IMN_CLOSECANDIDATE:
   895             trap = SDL_TRUE;
   896             IME_HideCandidateList(videodata);
   897             break;
   898         case IMN_PRIVATE:
   899             {
   900                 DWORD dwId = IME_GetId(videodata, 0);
   901                 IME_GetReadingString(videodata, hwnd);
   902                 switch (dwId)
   903                 {
   904                 case IMEID_CHT_VER42:
   905                 case IMEID_CHT_VER43:
   906                 case IMEID_CHT_VER44:
   907                 case IMEID_CHS_VER41:
   908                 case IMEID_CHS_VER42:
   909                     if (*lParam == 1 || *lParam == 2)
   910                         trap = SDL_TRUE;
   911 
   912                     break;
   913                 case IMEID_CHT_VER50:
   914                 case IMEID_CHT_VER51:
   915                 case IMEID_CHT_VER52:
   916                 case IMEID_CHT_VER60:
   917                 case IMEID_CHS_VER53:
   918                     if (*lParam == 16
   919                         || *lParam == 17
   920                         || *lParam == 26
   921                         || *lParam == 27
   922                         || *lParam == 28)
   923                         trap = SDL_TRUE;
   924                     break;
   925                 }
   926             }
   927             break;
   928         default:
   929             trap = SDL_TRUE;
   930             break;
   931         }
   932         break;
   933     }
   934     return trap;
   935 }
   936 
   937 static void
   938 IME_CloseCandidateList(SDL_VideoData *videodata)
   939 {
   940     IME_HideCandidateList(videodata);
   941     videodata->ime_candcount = 0;
   942     SDL_memset(videodata->ime_candidates, 0, sizeof(videodata->ime_candidates));
   943 }
   944 
   945 static void
   946 UILess_GetCandidateList(SDL_VideoData *videodata, ITfCandidateListUIElement *pcandlist)
   947 {
   948     UINT selection = 0;
   949     UINT count = 0;
   950     UINT page = 0;
   951     UINT pgcount = 0;
   952     DWORD pgstart = 0;
   953     DWORD pgsize = 0;
   954     UINT i, j;
   955     pcandlist->lpVtbl->GetSelection(pcandlist, &selection);
   956     pcandlist->lpVtbl->GetCount(pcandlist, &count);
   957     pcandlist->lpVtbl->GetCurrentPage(pcandlist, &page);
   958 
   959     videodata->ime_candsel = selection;
   960     videodata->ime_candcount = count;
   961     IME_ShowCandidateList(videodata);
   962 
   963     pcandlist->lpVtbl->GetPageIndex(pcandlist, 0, 0, &pgcount);
   964     if (pgcount > 0) {
   965         UINT *idxlist = SDL_malloc(sizeof(UINT) * pgcount);
   966         if (idxlist) {
   967             pcandlist->lpVtbl->GetPageIndex(pcandlist, idxlist, pgcount, &pgcount);
   968             pgstart = idxlist[page];
   969             if (page < pgcount - 1)
   970                 pgsize = SDL_min(count, idxlist[page + 1]) - pgstart;
   971             else
   972                 pgsize = count - pgstart;
   973 
   974             SDL_free(idxlist);
   975         }
   976     }
   977     videodata->ime_candpgsize = SDL_min(pgsize, MAX_CANDLIST);
   978     videodata->ime_candsel = videodata->ime_candsel - pgstart;
   979 
   980     SDL_memset(videodata->ime_candidates, 0, sizeof(videodata->ime_candidates));
   981     for (i = pgstart, j = 0; (DWORD)i < count && j < videodata->ime_candpgsize; i++, j++) {
   982         BSTR bstr;
   983         if (SUCCEEDED(pcandlist->lpVtbl->GetString(pcandlist, i, &bstr))) {
   984             if (bstr) {
   985                 IME_AddCandidate(videodata, j, bstr);
   986                 SysFreeString(bstr);
   987             }
   988         }
   989     }
   990     if (PRIMLANG() == LANG_KOREAN)
   991         videodata->ime_candsel = -1;
   992 }
   993 
   994 STDMETHODIMP_(ULONG) TSFSink_AddRef(TSFSink *sink)
   995 {
   996     return ++sink->refcount;
   997 }
   998 
   999 STDMETHODIMP_(ULONG)TSFSink_Release(TSFSink *sink)
  1000 {
  1001     --sink->refcount;
  1002     if (sink->refcount == 0) {
  1003         SDL_free(sink);
  1004         return 0;
  1005     }
  1006     return sink->refcount;
  1007 }
  1008 
  1009 STDMETHODIMP UIElementSink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv)
  1010 {
  1011     if (!ppv)
  1012         return E_INVALIDARG;
  1013 
  1014     *ppv = 0;
  1015     if (SDL_IsEqualIID(riid, &IID_IUnknown))
  1016         *ppv = (IUnknown *)sink;
  1017     else if (SDL_IsEqualIID(riid, &IID_ITfUIElementSink))
  1018         *ppv = (ITfUIElementSink *)sink;
  1019 
  1020     if (*ppv) {
  1021         TSFSink_AddRef(sink);
  1022         return S_OK;
  1023     }
  1024     return E_NOINTERFACE;
  1025 }
  1026 
  1027 ITfUIElement *UILess_GetUIElement(SDL_VideoData *videodata, DWORD dwUIElementId)
  1028 {
  1029     ITfUIElementMgr *puiem = 0;
  1030     ITfUIElement *pelem = 0;
  1031     ITfThreadMgrEx *threadmgrex = videodata->ime_threadmgrex;
  1032 
  1033     if (SUCCEEDED(threadmgrex->lpVtbl->QueryInterface(threadmgrex, &IID_ITfUIElementMgr, (LPVOID *)&puiem))) {
  1034         puiem->lpVtbl->GetUIElement(puiem, dwUIElementId, &pelem);
  1035         puiem->lpVtbl->Release(puiem);
  1036     }
  1037     return pelem;
  1038 }
  1039 
  1040 STDMETHODIMP UIElementSink_BeginUIElement(TSFSink *sink, DWORD dwUIElementId, BOOL *pbShow)
  1041 {
  1042     ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
  1043     ITfReadingInformationUIElement *preading = 0;
  1044     ITfCandidateListUIElement *pcandlist = 0;
  1045     SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
  1046     if (!element)
  1047         return E_INVALIDARG;
  1048 
  1049     *pbShow = FALSE;
  1050     if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
  1051         BSTR bstr;
  1052         if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr) {
  1053             WCHAR *s = (WCHAR *)bstr;
  1054             SysFreeString(bstr);
  1055         }
  1056         preading->lpVtbl->Release(preading);
  1057     }
  1058     else if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfCandidateListUIElement, (LPVOID *)&pcandlist)))	{
  1059         videodata->ime_candref++;
  1060         UILess_GetCandidateList(videodata, pcandlist);
  1061         pcandlist->lpVtbl->Release(pcandlist);
  1062     }
  1063     return S_OK;
  1064 }
  1065 
  1066 STDMETHODIMP UIElementSink_UpdateUIElement(TSFSink *sink, DWORD dwUIElementId)
  1067 {
  1068     ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
  1069     ITfReadingInformationUIElement *preading = 0;
  1070     ITfCandidateListUIElement *pcandlist = 0;
  1071     SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
  1072     if (!element)
  1073         return E_INVALIDARG;
  1074 
  1075     if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
  1076         BSTR bstr;
  1077         if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr) {
  1078             WCHAR *s = (WCHAR *)bstr;
  1079             SDL_wcslcpy(videodata->ime_readingstring, s, sizeof(videodata->ime_readingstring));
  1080             IME_SendEditingEvent(videodata);
  1081             SysFreeString(bstr);
  1082         }
  1083         preading->lpVtbl->Release(preading);
  1084     }
  1085     else if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfCandidateListUIElement, (LPVOID *)&pcandlist)))	{
  1086         UILess_GetCandidateList(videodata, pcandlist);
  1087         pcandlist->lpVtbl->Release(pcandlist);
  1088     }
  1089     return S_OK;
  1090 }
  1091 
  1092 STDMETHODIMP UIElementSink_EndUIElement(TSFSink *sink, DWORD dwUIElementId)
  1093 {
  1094     ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
  1095     ITfReadingInformationUIElement *preading = 0;
  1096     ITfCandidateListUIElement *pcandlist = 0;
  1097     SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
  1098     if (!element)
  1099         return E_INVALIDARG;
  1100 
  1101     if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
  1102         videodata->ime_readingstring[0] = 0;
  1103         IME_SendEditingEvent(videodata);
  1104         preading->lpVtbl->Release(preading);
  1105     }
  1106     if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfCandidateListUIElement, (LPVOID *)&pcandlist))) {
  1107         videodata->ime_candref--;
  1108         if (videodata->ime_candref == 0)
  1109             IME_CloseCandidateList(videodata);
  1110 
  1111         pcandlist->lpVtbl->Release(pcandlist);
  1112     }
  1113     return S_OK;
  1114 }
  1115 
  1116 STDMETHODIMP IPPASink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv)
  1117 {
  1118     if (!ppv)
  1119         return E_INVALIDARG;
  1120 
  1121     *ppv = 0;
  1122     if (SDL_IsEqualIID(riid, &IID_IUnknown))
  1123         *ppv = (IUnknown *)sink;
  1124     else if (SDL_IsEqualIID(riid, &IID_ITfInputProcessorProfileActivationSink))
  1125         *ppv = (ITfInputProcessorProfileActivationSink *)sink;
  1126 
  1127     if (*ppv) {
  1128         TSFSink_AddRef(sink);
  1129         return S_OK;
  1130     }
  1131     return E_NOINTERFACE;
  1132 }
  1133 
  1134 STDMETHODIMP IPPASink_OnActivated(TSFSink *sink, DWORD dwProfileType, LANGID langid, REFCLSID clsid, REFGUID catid, REFGUID guidProfile, HKL hkl, DWORD dwFlags)
  1135 {
  1136     static GUID TF_PROFILE_DAYI = {0x037B2C25, 0x480C, 0x4D7F, 0xB0, 0x27, 0xD6, 0xCA, 0x6B, 0x69, 0x78, 0x8A};
  1137     SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
  1138     videodata->ime_candlistindexbase = SDL_IsEqualGUID(&TF_PROFILE_DAYI, guidProfile) ? 0 : 1;
  1139     if (SDL_IsEqualIID(catid, &GUID_TFCAT_TIP_KEYBOARD) && (dwFlags & TF_IPSINK_FLAG_ACTIVE))
  1140         IME_InputLangChanged((SDL_VideoData *)sink->data);
  1141 
  1142     IME_HideCandidateList(videodata);
  1143     return S_OK;
  1144 }
  1145 
  1146 static void *vtUIElementSink[] = {
  1147     (void *)(UIElementSink_QueryInterface),
  1148     (void *)(TSFSink_AddRef),
  1149     (void *)(TSFSink_Release),
  1150     (void *)(UIElementSink_BeginUIElement),
  1151     (void *)(UIElementSink_UpdateUIElement),
  1152     (void *)(UIElementSink_EndUIElement)
  1153 };
  1154 
  1155 static void *vtIPPASink[] = {
  1156     (void *)(IPPASink_QueryInterface),
  1157     (void *)(TSFSink_AddRef),
  1158     (void *)(TSFSink_Release),
  1159     (void *)(IPPASink_OnActivated)
  1160 };
  1161 
  1162 static void
  1163 UILess_EnableUIUpdates(SDL_VideoData *videodata)
  1164 {
  1165     ITfSource *source = 0;
  1166     if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie != TF_INVALID_COOKIE)
  1167         return;
  1168 
  1169     if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
  1170         source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie);
  1171         source->lpVtbl->Release(source);
  1172     }
  1173 }
  1174 
  1175 static void
  1176 UILess_DisableUIUpdates(SDL_VideoData *videodata)
  1177 {
  1178     ITfSource *source = 0;
  1179     if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie == TF_INVALID_COOKIE)
  1180         return;
  1181 
  1182     if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
  1183         source->lpVtbl->UnadviseSink(source, videodata->ime_uielemsinkcookie);
  1184         videodata->ime_uielemsinkcookie = TF_INVALID_COOKIE;
  1185         source->lpVtbl->Release(source);
  1186     }
  1187 }
  1188 
  1189 static SDL_bool
  1190 UILess_SetupSinks(SDL_VideoData *videodata)
  1191 {
  1192     TfClientId clientid = 0;
  1193     SDL_bool result = SDL_FALSE;
  1194     ITfSource *source = 0;
  1195     if (FAILED(CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgrEx, (LPVOID *)&videodata->ime_threadmgrex)))
  1196         return SDL_FALSE;
  1197 
  1198     if (FAILED(videodata->ime_threadmgrex->lpVtbl->ActivateEx(videodata->ime_threadmgrex, &clientid, TF_TMAE_UIELEMENTENABLEDONLY)))
  1199         return SDL_FALSE;
  1200 
  1201     videodata->ime_uielemsink = SDL_malloc(sizeof(TSFSink));
  1202     videodata->ime_ippasink = SDL_malloc(sizeof(TSFSink));
  1203 
  1204     videodata->ime_uielemsink->lpVtbl = vtUIElementSink;
  1205     videodata->ime_uielemsink->refcount = 1;
  1206     videodata->ime_uielemsink->data = videodata;
  1207 
  1208     videodata->ime_ippasink->lpVtbl = vtIPPASink;
  1209     videodata->ime_ippasink->refcount = 1;
  1210     videodata->ime_ippasink->data = videodata;
  1211 
  1212     if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
  1213         if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie))) {
  1214             if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfInputProcessorProfileActivationSink, (IUnknown *)videodata->ime_ippasink, &videodata->ime_alpnsinkcookie))) {
  1215                 result = SDL_TRUE;
  1216             }
  1217         }
  1218         source->lpVtbl->Release(source);
  1219     }
  1220     return result;
  1221 }
  1222 
  1223 #define SAFE_RELEASE(p)                             \
  1224 {                                                   \
  1225     if (p) {                                        \
  1226         (p)->lpVtbl->Release((p));                  \
  1227         (p) = 0;                                    \
  1228     }                                               \
  1229 }
  1230 
  1231 static void
  1232 UILess_ReleaseSinks(SDL_VideoData *videodata)
  1233 {
  1234     ITfSource *source = 0;
  1235     if (videodata->ime_threadmgrex && SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
  1236         source->lpVtbl->UnadviseSink(source, videodata->ime_uielemsinkcookie);
  1237         source->lpVtbl->UnadviseSink(source, videodata->ime_alpnsinkcookie);
  1238         SAFE_RELEASE(source);
  1239         videodata->ime_threadmgrex->lpVtbl->Deactivate(videodata->ime_threadmgrex);
  1240         SAFE_RELEASE(videodata->ime_threadmgrex);
  1241         TSFSink_Release(videodata->ime_uielemsink);
  1242         videodata->ime_uielemsink = 0;
  1243         TSFSink_Release(videodata->ime_ippasink);
  1244         videodata->ime_ippasink = 0;
  1245     }
  1246 }
  1247 
  1248 static void *
  1249 StartDrawToBitmap(HDC hdc, HBITMAP *hhbm, int width, int height)
  1250 {
  1251     BITMAPINFO info = {0};
  1252     BITMAPINFOHEADER *infoHeader = &info.bmiHeader;
  1253     BYTE *bits = NULL;
  1254     if (hhbm) {
  1255         infoHeader->biSize = sizeof(BITMAPINFOHEADER);
  1256         infoHeader->biWidth = width;
  1257         infoHeader->biHeight = -1 * SDL_abs(height);
  1258         infoHeader->biPlanes = 1;
  1259         infoHeader->biBitCount = 32;
  1260         infoHeader->biCompression = BI_RGB;
  1261         *hhbm = CreateDIBSection(hdc, &info, DIB_RGB_COLORS, (void **)&bits, 0, 0);
  1262         if (*hhbm)
  1263             SelectObject(hdc, *hhbm);
  1264     }
  1265     return bits;
  1266 }
  1267 
  1268 static void
  1269 StopDrawToBitmap(HDC hdc, HBITMAP *hhbm)
  1270 {
  1271     if (hhbm && *hhbm) {
  1272         DeleteObject(*hhbm);
  1273         *hhbm = NULL;
  1274     }
  1275 }
  1276 
  1277 static void
  1278 BitmapToTexture(HBITMAP hbm, BYTE *bits, int width, int height, SDL_Texture **texture)
  1279 {
  1280     SDL_Surface *surface = NULL;
  1281     BITMAP bm = {0};
  1282 
  1283     if (GetObject(hbm, sizeof(bm), &bm) == 0)
  1284         return;
  1285 
  1286     if (bits && texture) {
  1287         /*
  1288             For transparency:
  1289 
  1290             const Uint8 alpha = 130;
  1291             unsigned long *p = (unsigned long *)bits;
  1292             unsigned long *end = (unsigned long *)(bits + (bm.bmWidthBytes * bm.bmHeight));
  1293             while (p < end) {
  1294                 *p = RGB(GetRValue(*p), GetGValue(*p), GetBValue(*p)) | (alpha << 24);
  1295                 ++p;
  1296             }
  1297             surface = SDL_CreateRGBSurfaceFrom(bits, width, height, bm.bmBitsPixel, bm.bmWidthBytes, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  1298         */
  1299         surface = SDL_CreateRGBSurfaceFrom(bits, width, height, bm.bmBitsPixel, bm.bmWidthBytes, 0x00ff0000, 0x0000ff00, 0x000000ff, 0);
  1300         if (surface) {
  1301             *texture = SDL_CreateTextureFromSurface(0, surface);
  1302             SDL_FreeSurface(surface);
  1303         }
  1304     }
  1305 }
  1306 
  1307 /* This draws only within the specified area and fills the entire region. */
  1308 static void
  1309 DrawRect(HDC hdc, int left, int top, int right, int bottom, int pensize)
  1310 {
  1311     /* The case of no pen (PenSize = 0) is automatically taken care of. */
  1312     const int penadjust = (int)SDL_floor(pensize / 2.0f - 0.5f);
  1313     left += pensize / 2;
  1314     top += pensize / 2;
  1315     right -= penadjust;
  1316     bottom -= penadjust;
  1317     Rectangle(hdc, left, top, right, bottom);
  1318 }
  1319 
  1320 static void
  1321 DestroyTexture(SDL_Texture **texture)
  1322 {
  1323     if (texture && *texture) {
  1324         SDL_DestroyTexture(*texture);
  1325         *texture = NULL;
  1326     }
  1327 }
  1328 
  1329 static void
  1330 IME_DestroyTextures(SDL_VideoData *videodata)
  1331 {
  1332     DestroyTexture(&videodata->ime_candtex);
  1333 }
  1334 
  1335 #define SDL_swap(a,b) { \
  1336     int c = (a);        \
  1337     (a) = (b);          \
  1338     (b) = c;            \
  1339     }
  1340 
  1341 static void
  1342 IME_PositionCandidateList(SDL_VideoData *videodata, SIZE size)
  1343 {
  1344     int left, top, right, bottom;
  1345     SDL_bool ok = SDL_FALSE;
  1346     int winw = videodata->ime_winwidth;
  1347     int winh = videodata->ime_winheight;
  1348 
  1349     /* Bottom */
  1350     left = videodata->ime_rect.x;
  1351     top = videodata->ime_rect.y + videodata->ime_rect.h;
  1352     right = left + size.cx;
  1353     bottom = top + size.cy;
  1354     if (right >= winw) {
  1355         left -= right - winw;
  1356         right = winw;
  1357     }
  1358     if (bottom < winh)
  1359         ok = SDL_TRUE;
  1360 
  1361     /* Top */
  1362     if (!ok) {
  1363         left = videodata->ime_rect.x;
  1364         top = videodata->ime_rect.y - size.cy;
  1365         right = left + size.cx;
  1366         bottom = videodata->ime_rect.y;
  1367         if (right >= winw) {
  1368             left -= right - winw;
  1369             right = winw;
  1370         }
  1371         if (top >= 0)
  1372             ok = SDL_TRUE;
  1373     }
  1374 
  1375     /* Right */
  1376     if (!ok) {
  1377         left = videodata->ime_rect.x + size.cx;
  1378         top = 0;
  1379         right = left + size.cx;
  1380         bottom = size.cy;
  1381         if (right < winw)
  1382             ok = SDL_TRUE;
  1383     }
  1384 
  1385     /* Left */
  1386     if (!ok) {
  1387         left = videodata->ime_rect.x - size.cx;
  1388         top = 0;
  1389         right = videodata->ime_rect.x;
  1390         bottom = size.cy;
  1391         if (right >= 0)
  1392             ok = SDL_TRUE;
  1393     }
  1394 
  1395     /* Window too small, show at (0,0) */
  1396     if (!ok) {
  1397         left = 0;
  1398         top = 0;
  1399         right = size.cx;
  1400         bottom = size.cy;
  1401     }
  1402 
  1403     videodata->ime_candlistrect.x = left;
  1404     videodata->ime_candlistrect.y = top;
  1405     videodata->ime_candlistrect.w = right - left;
  1406     videodata->ime_candlistrect.h = bottom - top;
  1407 }
  1408 
  1409 static void
  1410 IME_RenderCandidateList(SDL_VideoData *videodata, HDC hdc)
  1411 {
  1412     int i, j;
  1413     SIZE size = {0};
  1414     SIZE candsizes[MAX_CANDLIST];
  1415     SIZE maxcandsize = {0};
  1416     HBITMAP hbm = NULL;
  1417     BYTE *bits = NULL;
  1418     const int candcount = SDL_min(SDL_min(MAX_CANDLIST, videodata->ime_candcount), videodata->ime_candpgsize);
  1419     SDL_bool vertical = videodata->ime_candvertical;
  1420 
  1421     const int listborder = 1;
  1422     const int listpadding = 0;
  1423     const int listbordercolor = RGB(0xB4, 0xC7, 0xAA);
  1424     const int listfillcolor = RGB(255, 255, 255);
  1425 
  1426     const int candborder = 1;
  1427     const int candpadding = 0;
  1428     const int candmargin = 1;
  1429     const COLORREF candbordercolor = RGB(255, 255, 255);
  1430     const COLORREF candfillcolor = RGB(255, 255, 255);
  1431     const COLORREF candtextcolor = RGB(0, 0, 0);
  1432     const COLORREF selbordercolor = RGB(0x84, 0xAC, 0xDD);
  1433     const COLORREF selfillcolor = RGB(0xD2, 0xE6, 0xFF);
  1434     const COLORREF seltextcolor = RGB(0, 0, 0);
  1435     const int horzcandspacing = 5;
  1436 
  1437     HPEN listpen = listborder != 0 ? CreatePen(PS_SOLID, listborder, listbordercolor) : (HPEN)GetStockObject(NULL_PEN);
  1438     HBRUSH listbrush = CreateSolidBrush(listfillcolor);
  1439     HPEN candpen = candborder != 0 ? CreatePen(PS_SOLID, candborder, candbordercolor) : (HPEN)GetStockObject(NULL_PEN);
  1440     HBRUSH candbrush = CreateSolidBrush(candfillcolor);
  1441     HPEN selpen = candborder != 0 ? CreatePen(PS_DOT, candborder, selbordercolor) : (HPEN)GetStockObject(NULL_PEN);
  1442     HBRUSH selbrush = CreateSolidBrush(selfillcolor);
  1443     HFONT font = CreateFont((int)(1 + videodata->ime_rect.h * 0.75f), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH | FF_SWISS, TEXT("Microsoft Sans Serif"));
  1444 
  1445     SetBkMode(hdc, TRANSPARENT);
  1446     SelectObject(hdc, font);
  1447 
  1448     for (i = 0; i < candcount; ++i) {
  1449         const WCHAR *s = videodata->ime_candidates[i];
  1450         if (!*s)
  1451             break;
  1452 
  1453         GetTextExtentPoint32W(hdc, s, SDL_wcslen(s), &candsizes[i]);
  1454         maxcandsize.cx = SDL_max(maxcandsize.cx, candsizes[i].cx);
  1455         maxcandsize.cy = SDL_max(maxcandsize.cy, candsizes[i].cy);
  1456 
  1457     }
  1458     if (vertical) {
  1459         size.cx =
  1460             (listborder * 2) +
  1461             (listpadding * 2) +
  1462             (candmargin * 2) +
  1463             (candborder * 2) +
  1464             (candpadding * 2) +
  1465             (maxcandsize.cx)
  1466             ;
  1467         size.cy =
  1468             (listborder * 2) +
  1469             (listpadding * 2) +
  1470             ((candcount + 1) * candmargin) +
  1471             (candcount * candborder * 2) +
  1472             (candcount * candpadding * 2) +
  1473             (candcount * maxcandsize.cy)
  1474             ;
  1475     }
  1476     else {
  1477         size.cx =
  1478             (listborder * 2) +
  1479             (listpadding * 2) +
  1480             ((candcount + 1) * candmargin) +
  1481             (candcount * candborder * 2) +
  1482             (candcount * candpadding * 2) +
  1483             ((candcount - 1) * horzcandspacing);
  1484         ;
  1485 
  1486         for (i = 0; i < candcount; ++i)
  1487             size.cx += candsizes[i].cx;
  1488 
  1489         size.cy =
  1490             (listborder * 2) +
  1491             (listpadding * 2) +
  1492             (candmargin * 2) +
  1493             (candborder * 2) +
  1494             (candpadding * 2) +
  1495             (maxcandsize.cy)
  1496             ;
  1497     }
  1498 
  1499     bits = StartDrawToBitmap(hdc, &hbm, size.cx, size.cy);
  1500 
  1501     SelectObject(hdc, listpen);
  1502     SelectObject(hdc, listbrush);
  1503     DrawRect(hdc, 0, 0, size.cx, size.cy, listborder);
  1504 
  1505     SelectObject(hdc, candpen);
  1506     SelectObject(hdc, candbrush);
  1507     SetTextColor(hdc, candtextcolor);
  1508     SetBkMode(hdc, TRANSPARENT);
  1509 
  1510     for (i = 0; i < candcount; ++i) {
  1511         const WCHAR *s = videodata->ime_candidates[i];
  1512         int left, top, right, bottom;
  1513         if (!*s)
  1514             break;
  1515 
  1516         if (vertical) {
  1517             left = listborder + listpadding + candmargin;
  1518             top = listborder + listpadding + (i * candborder * 2) + (i * candpadding * 2) + ((i + 1) * candmargin) + (i * maxcandsize.cy);
  1519             right = size.cx - listborder - listpadding - candmargin;
  1520             bottom = top + maxcandsize.cy + (candpadding * 2) + (candborder * 2);
  1521         }
  1522         else {
  1523             left = listborder + listpadding + (i * candborder * 2) + (i * candpadding * 2) + ((i + 1) * candmargin) + (i * horzcandspacing);
  1524 
  1525             for (j = 0; j < i; ++j)
  1526                 left += candsizes[j].cx;
  1527 
  1528             top = listborder + listpadding + candmargin;
  1529             right = left + candsizes[i].cx + (candpadding * 2) + (candborder * 2);
  1530             bottom = size.cy - listborder - listpadding - candmargin;
  1531         }
  1532 
  1533         if (i == videodata->ime_candsel) {
  1534             SelectObject(hdc, selpen);
  1535             SelectObject(hdc, selbrush);
  1536             SetTextColor(hdc, seltextcolor);
  1537         }
  1538         else {
  1539             SelectObject(hdc, candpen);
  1540             SelectObject(hdc, candbrush);
  1541             SetTextColor(hdc, candtextcolor);
  1542         }
  1543 
  1544         DrawRect(hdc, left, top, right, bottom, candborder);
  1545         ExtTextOutW(hdc, left + candborder + candpadding, top + candborder + candpadding, 0, NULL, s, SDL_wcslen(s), NULL);
  1546     }
  1547     BitmapToTexture(hbm, bits, size.cx, size.cy, &videodata->ime_candtex);
  1548     StopDrawToBitmap(hdc, &hbm);
  1549 
  1550     DeleteObject(listpen);
  1551     DeleteObject(listbrush);
  1552     DeleteObject(candpen);
  1553     DeleteObject(candbrush);
  1554     DeleteObject(selpen);
  1555     DeleteObject(selbrush);
  1556     DeleteObject(font);
  1557 
  1558     IME_PositionCandidateList(videodata, size);
  1559 }
  1560 
  1561 static void
  1562 IME_Render(SDL_VideoData *videodata)
  1563 {
  1564     HDC hdc = CreateCompatibleDC(NULL);
  1565 
  1566     if (videodata->ime_candlist)
  1567         IME_RenderCandidateList(videodata, hdc);
  1568 
  1569     DeleteDC(hdc);
  1570 
  1571     videodata->ime_dirty = SDL_FALSE;
  1572 }
  1573 
  1574 void IME_Present(SDL_VideoData *videodata)
  1575 {
  1576     if (videodata->ime_dirty)
  1577         IME_Render(videodata);
  1578 
  1579     SDL_RenderCopy(videodata->ime_candtex, NULL, &videodata->ime_candlistrect);
  1580 }
  1581 
  1582 #endif /* SDL_DISABLE_WINDOWS_IME */
  1583 
  1584 /* vi: set ts=4 sw=4 expandtab: */