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