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