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