IMEManager.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. * $Archive: /Commando/Code/wwui/IMEManager.cpp $
  22. *
  23. * DESCRIPTION
  24. * Input Method Editor Manager for input of far east characters.
  25. *
  26. * PROGRAMMER
  27. * $Author: Denzil_l $
  28. *
  29. * VERSION INFO
  30. * $Revision: 6 $
  31. * $Modtime: 1/12/02 9:44p $
  32. *
  33. ******************************************************************************/
  34. #include "IMEManager.h"
  35. #include "WWString.h"
  36. #include "WWDebug.h"
  37. #include <locale.h>
  38. #include <mbctype.h>
  39. #if defined(_MSC_VER)
  40. #pragma warning(push, 3)
  41. #endif
  42. #include <algorithm>
  43. #include <memory>
  44. #if defined(_MSC_VER)
  45. #pragma warning(pop)
  46. #endif
  47. #pragma comment(lib, "imm32.lib")
  48. namespace IME {
  49. /******************************************************************************
  50. *
  51. * NAME
  52. * IMEManager::Create
  53. *
  54. * DESCRIPTION
  55. * Create an IME manager instance
  56. *
  57. * INPUTS
  58. * Window - HWND to associate IME manager instance with.
  59. *
  60. * RESULT
  61. * IMEManager - Pointer to IME manager
  62. *
  63. ******************************************************************************/
  64. IMEManager* IMEManager::Create(HWND hwnd)
  65. {
  66. if (hwnd)
  67. {
  68. IMEManager* ime = new IMEManager;
  69. if (ime)
  70. {
  71. if (ime->FinalizeCreate(hwnd))
  72. {
  73. return ime;
  74. }
  75. ime->Release_Ref();
  76. }
  77. }
  78. return NULL;
  79. }
  80. /******************************************************************************
  81. *
  82. * NAME
  83. * IMEManager::IMEManager
  84. *
  85. * DESCRIPTION
  86. * Constructor
  87. *
  88. * INPUTS
  89. * NONE
  90. *
  91. * RESULT
  92. * NONE
  93. *
  94. ******************************************************************************/
  95. IMEManager::IMEManager() :
  96. mHWND(NULL),
  97. mHIMC(NULL),
  98. mDisabledHIMC(NULL),
  99. mDisableCount(0),
  100. mCodePage(CP_ACP),
  101. mIMEProperties(0),
  102. mHilite(true),
  103. mStartCandListFrom1(true),
  104. mOSCanUnicode(false),
  105. mUseUnicode(false),
  106. mInComposition(false)
  107. {
  108. WWDEBUG_SAY(("IMEManager: Instantiated\n"));
  109. mLangID = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
  110. ResetComposition();
  111. mResultString[0] = 0;
  112. }
  113. /******************************************************************************
  114. *
  115. * NAME
  116. * IMEManager::~IMEManager
  117. *
  118. * DESCRIPTION
  119. * Destructor
  120. *
  121. * INPUTS
  122. * NONE
  123. *
  124. * RESULT
  125. * NONE
  126. *
  127. ******************************************************************************/
  128. IMEManager::~IMEManager()
  129. {
  130. WWDEBUG_SAY(("IMEManager: destroyed\n"));
  131. if (mHIMC)
  132. {
  133. ImmAssociateContext(mHWND, mDefaultHIMC);
  134. ImmDestroyContext(mHIMC);
  135. }
  136. mCandidateColl.clear();
  137. }
  138. /******************************************************************************
  139. *
  140. * NAME
  141. * IMEManager::FinalizeCreate
  142. *
  143. * DESCRIPTION
  144. * Post creation finalization.
  145. *
  146. * INPUTS
  147. * HWND - Window to associate IME context with.
  148. *
  149. * RESULT
  150. * True if successful
  151. *
  152. ******************************************************************************/
  153. bool IMEManager::FinalizeCreate(HWND hwnd)
  154. {
  155. if (hwnd == NULL)
  156. {
  157. return false;
  158. }
  159. mHWND = hwnd;
  160. // Check the OS version, if Win98 or better then we can use unicode
  161. OSVERSIONINFO osvi;
  162. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  163. GetVersionEx(&osvi);
  164. bool isWin98orLater = (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) && ((osvi.dwMajorVersion > 4) || ((osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion >= 10)));
  165. bool isNT4orLater = (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) && ((osvi.dwMajorVersion > 4) || ((osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion >= 0)));
  166. mOSCanUnicode = (isWin98orLater || isNT4orLater);
  167. // Create new input context for the specified window.
  168. mHIMC = ImmCreateContext();
  169. if (mHIMC == NULL)
  170. {
  171. return false;
  172. }
  173. // Associate the new context with this window.
  174. mDefaultHIMC = ImmAssociateContext(mHWND, mHIMC);
  175. // Set the language for the current keyboard layout.
  176. InputLanguageChanged(GetKeyboardLayout(0));
  177. mCandidateColl.resize(32);
  178. return true;
  179. }
  180. /******************************************************************************
  181. *
  182. * NAME
  183. * IMEManager::Activate
  184. *
  185. * DESCRIPTION
  186. *
  187. * INPUTS
  188. * NONE
  189. *
  190. * RESULT
  191. * NONE
  192. *
  193. ******************************************************************************/
  194. void IMEManager::Activate(void)
  195. {
  196. WWDEBUG_SAY(("IMEManager: Activate\n"));
  197. HIMC imc = ImmGetContext(mHWND);
  198. if (imc)
  199. {
  200. BOOL open = ImmGetOpenStatus(imc);
  201. if (!open)
  202. {
  203. ImmSetOpenStatus(imc, TRUE);
  204. IMEEvent action(IME_ACTIVATED, this);
  205. NotifyObservers(action);
  206. }
  207. ImmReleaseContext(mHWND, imc);
  208. }
  209. }
  210. /******************************************************************************
  211. *
  212. * NAME
  213. * IMEManager::Deactivate
  214. *
  215. * DESCRIPTION
  216. *
  217. * INPUTS
  218. * NONE
  219. *
  220. * RESULT
  221. * NONE
  222. *
  223. ******************************************************************************/
  224. void IMEManager::Deactivate(void)
  225. {
  226. WWDEBUG_SAY(("IMEManager: Deactivate\n"));
  227. HIMC imc = ImmGetContext(mHWND);
  228. if (imc)
  229. {
  230. BOOL open = ImmGetOpenStatus(imc);
  231. if (open)
  232. {
  233. ImmSetOpenStatus(imc, FALSE);
  234. IMEEvent action(IME_DEACTIVATED, this);
  235. NotifyObservers(action);
  236. }
  237. ImmReleaseContext(mHWND, imc);
  238. }
  239. ResetComposition();
  240. }
  241. /******************************************************************************
  242. *
  243. * NAME
  244. * IMEManager::IsActive
  245. *
  246. * DESCRIPTION
  247. * Check if IME is active.
  248. *
  249. * INPUTS
  250. * NONE
  251. *
  252. * RESULT
  253. * True if IME is currently active.
  254. *
  255. ******************************************************************************/
  256. bool IMEManager::IsActive(void) const
  257. {
  258. bool isActive = false;
  259. HIMC imc = ImmGetContext(mHWND);
  260. if (imc)
  261. {
  262. BOOL open = ImmGetOpenStatus(imc);
  263. isActive = (open != 0);
  264. ImmReleaseContext(mHWND, imc);
  265. }
  266. return isActive;
  267. }
  268. /******************************************************************************
  269. *
  270. * NAME
  271. * IMEManager::Disable
  272. *
  273. * DESCRIPTION
  274. *
  275. * INPUTS
  276. * NONE
  277. *
  278. * RESULT
  279. * NONE
  280. *
  281. ******************************************************************************/
  282. void IMEManager::Disable(void)
  283. {
  284. ++mDisableCount;
  285. // If this is the first disable lock the perform the actuall disabling.
  286. if (1 == mDisableCount)
  287. {
  288. WWDEBUG_SAY(("IMEManager: Disabled\n"));
  289. mDisabledHIMC = ImmAssociateContext(mHWND, NULL);
  290. IMEEvent action(IME_DISABLED, this);
  291. NotifyObservers(action);
  292. }
  293. }
  294. /******************************************************************************
  295. *
  296. * NAME
  297. * IMEManager::Enable
  298. *
  299. * DESCRIPTION
  300. *
  301. * INPUTS
  302. * NONE
  303. *
  304. * RESULT
  305. * NONE
  306. *
  307. ******************************************************************************/
  308. void IMEManager::Enable(void)
  309. {
  310. if (mDisableCount > 0)
  311. {
  312. --mDisableCount;
  313. // Re-enable when there is no disable locks.
  314. if (0 == mDisableCount)
  315. {
  316. WWDEBUG_SAY(("IMEManager: Enabled\n"));
  317. ImmAssociateContext(mHWND, mDisabledHIMC);
  318. mDisabledHIMC = NULL;
  319. IMEEvent action(IME_ENABLED, this);
  320. NotifyObservers(action);
  321. }
  322. }
  323. }
  324. /******************************************************************************
  325. *
  326. * NAME
  327. * IMEManager::IsDisabled
  328. *
  329. * DESCRIPTION
  330. * Check if IME is turned off.
  331. *
  332. * INPUTS
  333. * NONE
  334. *
  335. * RESULT
  336. * True if IME is currently off.
  337. *
  338. ******************************************************************************/
  339. bool IMEManager::IsDisabled(void) const
  340. {
  341. return (mDisableCount > 0);
  342. }
  343. /******************************************************************************
  344. *
  345. * NAME
  346. * IMEManager::ProcessMessage
  347. *
  348. * DESCRIPTION
  349. *
  350. * INPUTS
  351. *
  352. * RESULT
  353. * True of message handled and therefore should NOT be passed to the
  354. * default window procedure.
  355. *
  356. ******************************************************************************/
  357. bool IMEManager::ProcessMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT& outResult)
  358. {
  359. if (hwnd != mHWND)
  360. {
  361. return false;
  362. }
  363. bool handled = true;
  364. outResult = 0;
  365. switch (msg)
  366. {
  367. // Request New keyboard layout and / or Input method
  368. case WM_INPUTLANGCHANGEREQUEST:
  369. {
  370. HKL layout = InputLanguageChangeRequest((HKL)lParam);
  371. if (layout)
  372. {
  373. lParam = (LPARAM)layout;
  374. handled = false;
  375. }
  376. }
  377. break;
  378. // Input language has changed.
  379. case WM_INPUTLANGCHANGE:
  380. InputLanguageChanged((HKL)lParam);
  381. outResult = TRUE;
  382. handled = false;
  383. break;
  384. // Sent when the system is about to change the current IME.
  385. case WM_IME_SELECT:
  386. WWDEBUG_SAY(("IMEManager: WM_IME_SELECT\n"));
  387. break;
  388. // We will handle all of the UI so clear all of the flags.
  389. case WM_IME_SETCONTEXT:
  390. lParam &= ~(ISC_SHOWUIALL);
  391. handled = false;
  392. break;
  393. // Sent when a composition string is about to be generated in response to a
  394. // keystroke. This message informs us to prepare for composition.
  395. case WM_IME_STARTCOMPOSITION:
  396. StartComposition();
  397. break;
  398. // Sent when composition status has changed in response to a keystroke.
  399. case WM_IME_COMPOSITION:
  400. DoComposition(wParam, lParam);
  401. break;
  402. // Sent when composition has closed.
  403. case WM_IME_ENDCOMPOSITION:
  404. EndComposition();
  405. break;
  406. // Sent when unable to extend the composition to accomodate any more characters.
  407. case WM_IME_COMPOSITIONFULL:
  408. {
  409. WWDEBUG_SAY(("IMEManager: WM_IME_COMPOSITIONFULL\n"));
  410. CompositionEvent event(COMPOSITION_FULL, this);
  411. NotifyObservers(event);
  412. }
  413. break;
  414. // Sent when the IME status has changed.
  415. case WM_IME_NOTIFY:
  416. outResult = IMENotify(wParam, lParam);
  417. break;
  418. // IMEs send this message when the user accepts the conversion string.
  419. // wParam contains a single-byte or double-byte character.
  420. case WM_IME_CHAR:
  421. handled = IMECharHandler((unsigned short)wParam);
  422. if (handled)
  423. {
  424. outResult = TRUE;
  425. }
  426. break;
  427. case WM_CHAR:
  428. handled = CharHandler((unsigned short)wParam);
  429. if (handled)
  430. {
  431. outResult = TRUE;
  432. }
  433. break;
  434. case WM_KEYDOWN:
  435. if (mInComposition)
  436. {
  437. outResult = DefWindowProc(hwnd, msg, wParam, lParam);
  438. }
  439. else
  440. {
  441. handled = false;
  442. }
  443. break;
  444. case WM_KEYUP:
  445. if (mInComposition)
  446. {
  447. #ifdef SHOW_IME_TYPING
  448. bool typingChanged = false;
  449. UINT virtualKey = wParam;
  450. if (VK_BACK == virtualKey)
  451. {
  452. if (mTypingCursorPos > 0)
  453. {
  454. --mTypingCursorPos;
  455. mTypingString[mTypingCursorPos] = 0;
  456. typingChanged = true;
  457. }
  458. }
  459. else if ((0x30 <= virtualKey && 0x39 >= virtualKey) || (0x41 <= virtualKey && 0x5A >= virtualKey))
  460. {
  461. if (mTypingCursorPos < IME_MAX_TYPING_LEN)
  462. {
  463. mTypingString[mTypingCursorPos] = (wchar_t)virtualKey;
  464. ++mTypingCursorPos;
  465. mTypingString[mTypingCursorPos] = 0;
  466. typingChanged = true;
  467. }
  468. }
  469. if (typingChanged)
  470. {
  471. CompositionEvent event(COMPOSITION_TYPING, this);
  472. NotifyObservers(event);
  473. }
  474. #endif
  475. outResult = DefWindowProc(hwnd, msg, wParam, lParam);
  476. }
  477. else
  478. {
  479. handled = false;
  480. }
  481. break;
  482. case WM_IME_CONTROL:
  483. case WM_IME_KEYDOWN:
  484. case WM_IME_KEYUP:
  485. default:
  486. handled = false;
  487. break;
  488. }
  489. return handled;
  490. }
  491. /******************************************************************************
  492. *
  493. * NAME
  494. * IMEManager::IMENotify
  495. *
  496. * DESCRIPTION
  497. *
  498. * INPUTS
  499. *
  500. * RESULT
  501. *
  502. ******************************************************************************/
  503. LRESULT IMEManager::IMENotify(WPARAM wParam, LPARAM lParam)
  504. {
  505. switch (wParam)
  506. {
  507. // The open status of the input context has changed.
  508. case IMN_SETOPENSTATUS:
  509. WWDEBUG_SAY(("IMEManager: IMN_SETOPENSTATUS\n"));
  510. {
  511. HIMC imc = ImmGetContext(mHWND);
  512. if (imc)
  513. {
  514. IMEAction action = IME_DEACTIVATED;
  515. if (!ImmGetOpenStatus(imc))
  516. {
  517. // If the IME conversion engine is closed, we need to
  518. // erase all currently displayed composition chars and any
  519. // candidate windows.
  520. mCandidateColl.clear();
  521. ResetComposition();
  522. }
  523. else
  524. {
  525. action = IME_ACTIVATED;
  526. }
  527. ImmReleaseContext(mHWND, imc);
  528. IMEEvent event(action, this);
  529. NotifyObservers(event);
  530. }
  531. }
  532. break;
  533. // Open the status window
  534. case IMN_OPENSTATUSWINDOW:
  535. WWDEBUG_SAY(("IMEManager: IMN_OPENSTATUSWINDOW\n"));
  536. break;
  537. // Close the status window
  538. case IMN_CLOSESTATUSWINDOW:
  539. WWDEBUG_SAY(("IMEManager: IMN_CLOSESTATUSWINDOW\n"));
  540. break;
  541. // Update the position of the status window.
  542. case IMN_SETSTATUSWINDOWPOS:
  543. WWDEBUG_SAY(("IMEManager: IMN_SETSTATUSWINDOWPOS\n"));
  544. break;
  545. // The font of the input context has changed.
  546. case IMN_SETCOMPOSITIONFONT:
  547. WWDEBUG_SAY(("IMEManager: IMN_SETCOMPOSITIONFONT\n"));
  548. break;
  549. // The style or position of the composition window has changed.
  550. case IMN_SETCOMPOSITIONWINDOW:
  551. WWDEBUG_SAY(("IMEManager: IMN_SETCOMPOSITIONWINDOW\n"));
  552. break;
  553. // The conversion mode of the input context has changed.
  554. case IMN_SETCONVERSIONMODE:
  555. WWDEBUG_SAY(("IMEManager: IMN_SETCONVERSIONMODE\n"));
  556. #ifdef WWDEBUG
  557. {
  558. HIMC imc = ImmGetContext(mHWND);
  559. if (imc)
  560. {
  561. DWORD conversionMode = 0;
  562. DWORD sentenceMode = 0;
  563. BOOL okay = ImmGetConversionStatus(imc, &conversionMode, &sentenceMode);
  564. if (okay)
  565. {
  566. WWDEBUG_SAY(("IMEManager: ConversionMode - "));
  567. static struct convmodestruct {long flag; const char* ondesc; const char* offdesc;} _convModes[] =
  568. {
  569. {IME_CMODE_CHARCODE, "CharCode:On", "CharCode:Off"},
  570. {IME_CMODE_EUDC, " EUDC:On", " EUDC:Off"},
  571. {IME_CMODE_FIXED, " Fixed:On", " Fixed:Off"},
  572. {IME_CMODE_FULLSHAPE, " FullShape", " HalfShape"},
  573. {IME_CMODE_HANJACONVERT, " Hanja:On", " Hanja:Off"},
  574. {IME_CMODE_KATAKANA, " Katakana", " Hiragana"},
  575. {IME_CMODE_NATIVE, " Native", " Alphanumberic"},
  576. {IME_CMODE_NOCONVERSION, " Conversion:Off", " Conversion:On"},
  577. {IME_CMODE_ROMAN, " Roman:On", " Roman:Off"},
  578. {IME_CMODE_SOFTKBD, " SoftKbd:On", " SoftKbd:Off"},
  579. {IME_CMODE_SYMBOL, " Symbol:On", " Symbol:Off"},
  580. {0, ""}
  581. };
  582. int flgidx = 0;
  583. while (_convModes[flgidx].flag)
  584. {
  585. if (conversionMode & _convModes[flgidx].flag)
  586. {
  587. WWDEBUG_SAY((_convModes[flgidx].ondesc));
  588. }
  589. else
  590. {
  591. WWDEBUG_SAY((_convModes[flgidx].offdesc));
  592. }
  593. flgidx++;
  594. }
  595. WWDEBUG_SAY(("\n"));
  596. }
  597. ImmReleaseContext(mHWND, imc);
  598. }
  599. }
  600. #endif
  601. break;
  602. // The sentence mode has changed.
  603. case IMN_SETSENTENCEMODE:
  604. WWDEBUG_SAY(("IMEManager: IMN_SETSENTENCEMODE\n"));
  605. #ifdef WWDEBUG
  606. {
  607. HIMC imc = ImmGetContext(mHWND);
  608. if (imc)
  609. {
  610. DWORD conversionMode = 0;
  611. DWORD sentenceMode = 0;
  612. BOOL okay = ImmGetConversionStatus(imc, &conversionMode, &sentenceMode);
  613. if (okay)
  614. {
  615. WWDEBUG_SAY(("IMEManager: SentenceMode - "));
  616. static struct smodestruct {long flag; const char* ondesc; const char* offdesc;} _sModes[] =
  617. {
  618. {IME_SMODE_AUTOMATIC, "Automatic:On", "Automatic:Off"},
  619. {IME_SMODE_NONE, " SentenceInfo:Off", " SentenceInfo:On"},
  620. {IME_SMODE_PHRASEPREDICT, " PhrasePredict:On", " PhrasePredict:Off"},
  621. {IME_SMODE_PLAURALCLAUSE, " PluralClause:On", " PluralClause:Off"},
  622. {IME_SMODE_SINGLECONVERT, " SingleConvert:On", " SingleConvert:Off"},
  623. {IME_SMODE_CONVERSATION, " Conversation:On", " Conversation:Off"},
  624. {0, ""}
  625. };
  626. int flgidx = 0;
  627. while (_sModes[flgidx].flag)
  628. {
  629. if (sentenceMode & _sModes[flgidx].flag)
  630. {
  631. WWDEBUG_SAY((_sModes[flgidx].ondesc));
  632. }
  633. else
  634. {
  635. WWDEBUG_SAY((_sModes[flgidx].offdesc));
  636. }
  637. flgidx++;
  638. }
  639. WWDEBUG_SAY(("\n"));
  640. }
  641. ImmReleaseContext(mHWND, imc);
  642. }
  643. }
  644. #endif
  645. break;
  646. // Open the candidate window (lParam = candidate flags)
  647. case IMN_OPENCANDIDATE:
  648. OpenCandidate(lParam);
  649. break;
  650. // Close the candidate window. (lParam = candidate flags)
  651. case IMN_CLOSECANDIDATE:
  652. CloseCandidate(lParam);
  653. break;
  654. // Changing the contents of the candidate window (lParam = candidate flags)
  655. case IMN_CHANGECANDIDATE:
  656. ChangeCandidate(lParam);
  657. break;
  658. // Candidate processing is finished; moving the candidate window
  659. case IMN_SETCANDIDATEPOS:
  660. WWDEBUG_SAY(("IMEManager: IMN_SETCANDIDATEPOS\n"));
  661. break;
  662. // Show error message or other information
  663. case IMN_GUIDELINE:
  664. WWDEBUG_SAY(("IMEManager: IMN_GUIDELINE\n"));
  665. {
  666. IMEEvent event(IME_GUIDELINE, this);
  667. NotifyObservers(event);
  668. }
  669. break;
  670. default:
  671. break;
  672. }
  673. return TRUE;
  674. }
  675. /******************************************************************************
  676. *
  677. * NAME
  678. * IMEManager::InputLanguageChangeRequest
  679. *
  680. * DESCRIPTION
  681. *
  682. * INPUTS
  683. *
  684. * RESULT
  685. *
  686. ******************************************************************************/
  687. HKL IMEManager::InputLanguageChangeRequest(HKL hkl)
  688. {
  689. WWDEBUG_SAY(("IMEManager: Input language change request\n"));
  690. // Get the number of Keyboard layouts available to the system
  691. UINT numLayouts = GetKeyboardLayoutList(0, NULL);
  692. if (numLayouts)
  693. {
  694. // Get the list of layouts
  695. std::vector<HKL> layoutList(numLayouts);
  696. layoutList.resize(numLayouts);
  697. numLayouts = GetKeyboardLayoutList(numLayouts, layoutList.begin());
  698. // Find the position in the list of the layout which has been requested.
  699. std::vector<HKL>::iterator iter = std::find(layoutList.begin(), layoutList.end(), hkl);
  700. if (iter != layoutList.end())
  701. {
  702. // Rotate the list so the requested layout is at the head.
  703. std::rotate(layoutList.begin(), iter, layoutList.end());
  704. // Look for the layout that doesn't have the AT_CARET or SPECIAL_UI properties
  705. iter = layoutList.begin();
  706. while (iter != layoutList.end())
  707. {
  708. DWORD property = ImmGetProperty(*iter, IGP_PROPERTY);
  709. if ((property & (IME_PROP_AT_CARET | IME_PROP_SPECIAL_UI)) == (IME_PROP_AT_CARET | IME_PROP_SPECIAL_UI))
  710. {
  711. iter++;
  712. }
  713. else
  714. {
  715. return *iter;
  716. }
  717. }
  718. }
  719. }
  720. return NULL;
  721. }
  722. /******************************************************************************
  723. *
  724. * NAME
  725. * IMEManager::InputLanguageChanged
  726. *
  727. * DESCRIPTION
  728. *
  729. * INPUTS
  730. *
  731. * RESULT
  732. *
  733. ******************************************************************************/
  734. void IMEManager::InputLanguageChanged(HKL hkl)
  735. {
  736. mLangID = LOWORD(hkl);
  737. DWORD lcid = MAKELCID(mLangID, SORT_DEFAULT);
  738. // Get the default codepage for this input language
  739. char localeData[8];
  740. GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, localeData, sizeof(localeData));
  741. // Set the codepage for character conversion
  742. mCodePage = atoi(localeData);
  743. // Get properties
  744. mIMEProperties = ImmGetProperty(hkl, IGP_PROPERTY);
  745. mStartCandListFrom1 = ((mIMEProperties & IME_PROP_CANDLIST_START_FROM_1) == IME_PROP_CANDLIST_START_FROM_1);
  746. mUseUnicode = (mOSCanUnicode && (mIMEProperties & IME_PROP_UNICODE));
  747. // Get IME description
  748. if (mUseUnicode)
  749. {
  750. UINT descSize = ImmGetDescriptionW(hkl, NULL, 0);
  751. ++descSize;
  752. wchar_t* descPtr = mIMEDescription.Get_Buffer(descSize);
  753. ImmGetDescriptionW(hkl, descPtr, descSize);
  754. }
  755. else
  756. {
  757. UINT descSize = ImmGetDescription(hkl, NULL, 0);
  758. ++descSize;
  759. StringClass desc((int)descSize, true);
  760. char* descPtr = desc.Get_Buffer(descSize);
  761. ImmGetDescription(hkl, descPtr, descSize);
  762. mIMEDescription = desc;
  763. }
  764. #if(0)
  765. mHilite = true;
  766. static const wchar_t _TradChImeName[] = {0x6CE8,0x97F3,0x8F38,0x5165,0x6CD5,0x0020,0x0034,0x002E,0x0031,0x0020,0x7248,0x0000};
  767. if (mIMEDescription.Compare(_TradChImeName) == 0)
  768. {
  769. mHilite = false;
  770. }
  771. #endif
  772. WWDEBUG_SAY(("IMEManager: Language Changed - LangID = %04X, CodePage = %d, Description: '%S'\n",
  773. mLangID, mCodePage, mIMEDescription));
  774. WWDEBUG_SAY(("IMEManager: Properties - %s%s%s%s%s\n",
  775. mIMEProperties & IME_PROP_AT_CARET ? "At Caret" : "",
  776. mIMEProperties & IME_PROP_SPECIAL_UI ? ", SpecialUI" : "",
  777. mIMEProperties & IME_PROP_CANDLIST_START_FROM_1 ? ", CandlistFrom1" : "",
  778. mIMEProperties & IME_PROP_UNICODE ? ", Unicode" : "",
  779. mIMEProperties & IME_PROP_COMPLETE_ON_UNSELECT ? ", Complete on Unselect" : ""));
  780. IMEEvent action(IME_LANGUAGECHANGED, this);
  781. NotifyObservers(action);
  782. }
  783. /******************************************************************************
  784. *
  785. * NAME
  786. * IMEManager::ResetComposition
  787. *
  788. * DESCRIPTION
  789. *
  790. * INPUTS
  791. *
  792. * RESULT
  793. *
  794. ******************************************************************************/
  795. void IMEManager::ResetComposition(void)
  796. {
  797. mInComposition = false;
  798. #ifdef SHOW_IME_TYPING
  799. mTypingString[0] = 0;
  800. mTypingCursorPos = 0;
  801. #endif
  802. mCompositionString[0] = 0;
  803. memset(mCompositionAttr, 0, sizeof(mCompositionAttr));
  804. memset(mCompositionClause, 0, sizeof(mCompositionClause));
  805. mCompositionCursorPos = 0;
  806. mReadingString[0] = 0;
  807. }
  808. /******************************************************************************
  809. *
  810. * NAME
  811. * IMEManager::StartComposition
  812. *
  813. * DESCRIPTION
  814. *
  815. * INPUTS
  816. *
  817. * RESULT
  818. *
  819. ******************************************************************************/
  820. void IMEManager::StartComposition(void)
  821. {
  822. WWDEBUG_SAY(("IMEManager: StartComposition\n"));
  823. mInComposition = true;
  824. mResultString[0] = 0;
  825. CompositionEvent event(COMPOSITION_START, this);
  826. NotifyObservers(event);
  827. }
  828. /******************************************************************************
  829. *
  830. * NAME
  831. * IMEManager::DoComposition
  832. *
  833. * DESCRIPTION
  834. * Handle composition message.
  835. *
  836. * INPUTS
  837. *
  838. * RESULT
  839. *
  840. ******************************************************************************/
  841. void IMEManager::DoComposition(unsigned int dbcs, long compFlags)
  842. {
  843. WWDEBUG_SAY(("IMEManager: DoComposition\n"));
  844. #if(0)
  845. #ifdef _DEBUG
  846. static struct flagstruct {long flag; const char* desc;} _gcsFlags[] =
  847. {
  848. {GCS_COMPATTR, "GCS_COMPATTR"},
  849. {GCS_COMPCLAUSE, " GCS_COMPCLAUSE"},
  850. {GCS_COMPREADATTR, " GCS_COMPREADATTR"},
  851. {GCS_COMPREADCLAUSE, " GCS_COMPREADCLAUSE"},
  852. {GCS_DELTASTART, " GCS_DELTASTART"},
  853. {GCS_RESULTCLAUSE, " GCS_RESULTCLAUSE"},
  854. {GCS_RESULTREADCLAUSE, " GCS_RESULTREADCLAUSE"},
  855. {GCS_RESULTREADSTR, " GCS_RESULTREADSTR"},
  856. {CS_INSERTCHAR, " CS_INSERTCHAR"},
  857. {CS_NOMOVECARET, " CS_NOMOVECARET"},
  858. {0, ""}
  859. };
  860. WWDEBUG_SAY(("Flags: "));
  861. int flgidx = 0;
  862. while (_gcsFlags[flgidx].flag)
  863. {
  864. if (compFlags & _gcsFlags[flgidx].flag)
  865. {
  866. WWDEBUG_SAY((_gcsFlags[flgidx].desc));
  867. }
  868. flgidx++;
  869. }
  870. WWDEBUG_SAY(("\n"));
  871. #endif
  872. #endif
  873. #ifdef SHOW_IME_TYPING
  874. // Reset the typing string
  875. mTypingString[0] = 0;
  876. mTypingCursorPos = 0;
  877. CompositionEvent event(COMPOSITION_TYPING, this);
  878. NotifyObservers(event);
  879. #endif
  880. HIMC imc = ImmGetContext(mHWND);
  881. if (imc)
  882. {
  883. if (compFlags == 0)
  884. {
  885. ResetComposition();
  886. CompositionEvent event(COMPOSITION_CANCEL, this);
  887. NotifyObservers(event);
  888. }
  889. else if (compFlags & GCS_RESULTSTR)
  890. {
  891. // Retrieve the result string
  892. if (ReadCompositionString(imc, GCS_RESULTSTR, mResultString, sizeof(mResultString)))
  893. {
  894. WWDEBUG_SAY(("Result string '%S'\n", mResultString));
  895. CompositionEvent event(COMPOSITION_RESULT, this);
  896. NotifyObservers(event);
  897. }
  898. }
  899. else
  900. {
  901. CompositionAction action = COMPOSITION_INVALID;
  902. // Update reading string.
  903. if (compFlags & GCS_COMPREADSTR)
  904. {
  905. if (ReadCompositionString(imc, GCS_COMPREADSTR, mReadingString, sizeof(mReadingString)))
  906. {
  907. WWDEBUG_SAY(("Reading string '%S'\n", (const wchar_t*)mReadingString));
  908. action = COMPOSITION_CHANGE;
  909. }
  910. }
  911. #ifdef WWDEBUG
  912. if (compFlags & GCS_COMPREADATTR)
  913. {
  914. unsigned char attr[IME_MAX_STRING_LEN * 2];
  915. long size = ReadReadingAttr(imc, attr, sizeof(attr));
  916. WWDEBUG_SAY(("ReadAttr: "));
  917. for (long index = 0; index < size; ++index)
  918. {
  919. WWDEBUG_SAY(("%01x", (int)attr[index]));
  920. }
  921. WWDEBUG_SAY(("\n"));
  922. }
  923. #endif
  924. // Update composition string.
  925. if (compFlags & GCS_COMPSTR)
  926. {
  927. if (ReadCompositionString(imc, GCS_COMPSTR, mCompositionString, sizeof(mCompositionString)))
  928. {
  929. WWDEBUG_SAY(("Composition string '%S'\n", (const wchar_t*)mCompositionString));
  930. action = COMPOSITION_CHANGE;
  931. }
  932. }
  933. if (compFlags & GCS_COMPATTR)
  934. {
  935. long size = ReadCompositionAttr(imc, mCompositionAttr, sizeof(mCompositionAttr));
  936. #ifdef WWDEBUG
  937. WWDEBUG_SAY(("CompAttr: "));
  938. for (long index = 0; index < size; ++index)
  939. {
  940. WWDEBUG_SAY(("%01x", (int)mCompositionAttr[index]));
  941. }
  942. WWDEBUG_SAY(("\n"));
  943. #endif
  944. }
  945. if (compFlags & GCS_COMPCLAUSE)
  946. {
  947. mCompositionClause[0] = 0;
  948. long size = ReadCompositionClause(imc, mCompositionClause, sizeof(mCompositionClause));
  949. #ifdef WWDEBUG
  950. WWDEBUG_SAY(("CompClause: "));
  951. const int count = (size / sizeof(unsigned long));
  952. for (int index = 0; index < count; ++index)
  953. {
  954. WWDEBUG_SAY(("%u ", mCompositionClause[index]));
  955. }
  956. WWDEBUG_SAY(("\n"));
  957. #endif
  958. }
  959. if (compFlags & GCS_CURSORPOS)
  960. {
  961. mCompositionCursorPos = ReadCursorPos(imc);
  962. action = COMPOSITION_CHANGE;
  963. }
  964. if (action != COMPOSITION_INVALID)
  965. {
  966. CompositionEvent event(action, this);
  967. NotifyObservers(event);
  968. }
  969. }
  970. ImmReleaseContext(mHWND, imc);
  971. }
  972. }
  973. /******************************************************************************
  974. *
  975. * NAME
  976. * IMEManager::EndComposition
  977. *
  978. * DESCRIPTION
  979. *
  980. * INPUTS
  981. *
  982. * RESULT
  983. *
  984. ******************************************************************************/
  985. void IMEManager::EndComposition(void)
  986. {
  987. WWDEBUG_SAY(("IMEManager: EndComposition\n"));
  988. HIMC imc = ImmGetContext(mHWND);
  989. if (imc)
  990. {
  991. ReadCompositionString(imc, GCS_COMPSTR, mResultString, sizeof(mResultString));
  992. ImmReleaseContext(mHWND, imc);
  993. }
  994. ResetComposition();
  995. CompositionEvent event(COMPOSITION_END, this);
  996. NotifyObservers(event);
  997. }
  998. /******************************************************************************
  999. *
  1000. * NAME
  1001. * IMEManager::ReadCompositionString
  1002. *
  1003. * DESCRIPTION
  1004. *
  1005. * INPUTS
  1006. *
  1007. * RESULT
  1008. *
  1009. ******************************************************************************/
  1010. bool IMEManager::ReadCompositionString(HIMC imc, unsigned long flag, wchar_t* buffer, int length)
  1011. {
  1012. if (mUseUnicode)
  1013. {
  1014. LONG size = ImmGetCompositionStringW(imc, flag, buffer, length);
  1015. if (size < 0)
  1016. {
  1017. buffer[0] = 0;
  1018. return false;
  1019. }
  1020. // Terminate string
  1021. buffer[(size / sizeof(wchar_t))] = 0;
  1022. }
  1023. else
  1024. {
  1025. // Read the string as multibyte ANSI
  1026. unsigned char string[IME_MAX_STRING_LEN];
  1027. LONG size = ImmGetCompositionString(imc, flag, string, sizeof(string));
  1028. if (size < 0)
  1029. {
  1030. buffer[0] = 0;
  1031. return false;
  1032. }
  1033. // Terminate the string
  1034. string[size] = 0;
  1035. // Convert to Unicode
  1036. MultiByteToWideChar(mCodePage, 0, (const char*)string, -1, buffer, (length / sizeof(wchar_t)));
  1037. }
  1038. return true;
  1039. }
  1040. /******************************************************************************
  1041. *
  1042. * NAME
  1043. * IMEManager::
  1044. *
  1045. * DESCRIPTION
  1046. *
  1047. * INPUTS
  1048. *
  1049. * RESULT
  1050. *
  1051. ******************************************************************************/
  1052. long IMEManager::ReadReadingAttr(HIMC imc, unsigned char* attr, int length)
  1053. {
  1054. if (mUseUnicode)
  1055. {
  1056. LONG size = ImmGetCompositionStringW(imc, GCS_COMPREADATTR, attr, length);
  1057. return (size / sizeof(wchar_t));
  1058. }
  1059. // Read the string as multibyte ANSI
  1060. unsigned char string[IME_MAX_STRING_LEN];
  1061. LONG size = ImmGetCompositionString(imc, GCS_COMPREADSTR, string, sizeof(string));
  1062. if (size <= 0)
  1063. {
  1064. return 0;
  1065. }
  1066. // Terminate the string
  1067. string[size] = 0;
  1068. LONG attrSize = ImmGetCompositionString(imc, GCS_COMPREADATTR, attr, length);
  1069. WWASSERT(size == attrSize);
  1070. if (attrSize <= size)
  1071. {
  1072. return 0;
  1073. }
  1074. return ConvertAttrForUnicode(string, attr);
  1075. }
  1076. /******************************************************************************
  1077. *
  1078. * NAME
  1079. * IMEManager::
  1080. *
  1081. * DESCRIPTION
  1082. *
  1083. * INPUTS
  1084. *
  1085. * RESULT
  1086. *
  1087. ******************************************************************************/
  1088. long IMEManager::ReadReadingClause(HIMC imc, unsigned long* clause, int length)
  1089. {
  1090. if (mUseUnicode)
  1091. {
  1092. LONG size = ImmGetCompositionStringW(imc, GCS_COMPREADCLAUSE, clause, length);
  1093. return (size / sizeof(wchar_t));
  1094. }
  1095. // Read the string as multibyte ANSI
  1096. unsigned char string[IME_MAX_STRING_LEN];
  1097. LONG size = ImmGetCompositionString(imc, GCS_COMPREADSTR, string, sizeof(string));
  1098. if (size <= 0)
  1099. {
  1100. return 0;
  1101. }
  1102. // Terminate the string
  1103. string[size] = 0;
  1104. LONG clauseSize = ImmGetCompositionString(imc, GCS_COMPREADCLAUSE, clause, length);
  1105. if (clauseSize <= 0)
  1106. {
  1107. return 0;
  1108. }
  1109. return ConvertClauseForUnicode(string, size, clause);
  1110. }
  1111. /******************************************************************************
  1112. *
  1113. * NAME
  1114. * IMEManager::
  1115. *
  1116. * DESCRIPTION
  1117. *
  1118. * INPUTS
  1119. *
  1120. * RESULT
  1121. *
  1122. ******************************************************************************/
  1123. long IMEManager::ReadCompositionAttr(HIMC imc, unsigned char* attr, int length)
  1124. {
  1125. if (mUseUnicode)
  1126. {
  1127. return ImmGetCompositionStringW(imc, GCS_COMPATTR, attr, length);
  1128. }
  1129. // Read the string as multibyte ANSI
  1130. unsigned char string[IME_MAX_STRING_LEN];
  1131. LONG size = ImmGetCompositionString(imc, GCS_COMPSTR, string, sizeof(string));
  1132. if (size <= 0)
  1133. {
  1134. return size;
  1135. }
  1136. // Terminate the string
  1137. string[size] = 0;
  1138. LONG attrSize = ImmGetCompositionString(imc, GCS_COMPATTR, attr, length);
  1139. WWASSERT(size == attrSize);
  1140. if (attrSize <= size)
  1141. {
  1142. return 0;
  1143. }
  1144. return ConvertAttrForUnicode(string, attr);
  1145. }
  1146. /******************************************************************************
  1147. *
  1148. * NAME
  1149. * IMEManager::
  1150. *
  1151. * DESCRIPTION
  1152. *
  1153. * INPUTS
  1154. *
  1155. * RESULT
  1156. *
  1157. ******************************************************************************/
  1158. long IMEManager::ReadCompositionClause(HIMC imc, unsigned long* clause, int length)
  1159. {
  1160. if (mUseUnicode)
  1161. {
  1162. return ImmGetCompositionStringW(imc, GCS_COMPCLAUSE, clause, length);
  1163. }
  1164. // Read the string as multibyte ANSI
  1165. unsigned char string[IME_MAX_STRING_LEN];
  1166. LONG size = ImmGetCompositionString(imc, GCS_COMPSTR, string, sizeof(string));
  1167. if (size <= 0)
  1168. {
  1169. return size;
  1170. }
  1171. // Terminate the string
  1172. string[size] = 0;
  1173. LONG clauseSize = ImmGetCompositionString(imc, GCS_COMPCLAUSE, clause, length);
  1174. if (clauseSize <= 0)
  1175. {
  1176. return 0;
  1177. }
  1178. return ConvertClauseForUnicode(string, size, clause);
  1179. }
  1180. /******************************************************************************
  1181. *
  1182. * NAME
  1183. * IMEManager::ReadCursorPos
  1184. *
  1185. * DESCRIPTION
  1186. * Read the composition string cursor position.
  1187. *
  1188. * INPUTS
  1189. *
  1190. * RESULT
  1191. *
  1192. ******************************************************************************/
  1193. long IMEManager::ReadCursorPos(HIMC imc)
  1194. {
  1195. if (mUseUnicode)
  1196. {
  1197. long cursorPos = ImmGetCompositionStringW(imc, GCS_CURSORPOS, NULL, 0);
  1198. return (cursorPos & 0x0000FFFF);
  1199. }
  1200. // Get the multibyte string
  1201. char string[IME_MAX_STRING_LEN];
  1202. LONG size = ImmGetCompositionString(imc, GCS_COMPSTR, string, sizeof(string));
  1203. if (size < 0)
  1204. {
  1205. return 0;
  1206. }
  1207. string[size] = 0;
  1208. long cursorPos = ImmGetCompositionString(imc, GCS_CURSORPOS, NULL, 0);
  1209. cursorPos = (cursorPos & 0x0000FFFF);
  1210. // Convert multibyte character position in unicode position.
  1211. return _mbsnccnt((unsigned char*)string, cursorPos);
  1212. }
  1213. /******************************************************************************
  1214. *
  1215. * NAME
  1216. * IMEManager::GetTargetClause
  1217. *
  1218. * DESCRIPTION
  1219. * Get the composition string conversion target range. This is the characters
  1220. * that are currently be considered for conversion.
  1221. *
  1222. * INPUTS
  1223. *
  1224. * RESULT
  1225. *
  1226. ******************************************************************************/
  1227. void IMEManager::GetTargetClause(unsigned long& start, unsigned long& end)
  1228. {
  1229. int index = 0;
  1230. const unsigned long compLength = wcslen(mCompositionString);
  1231. while (mCompositionClause[index] < compLength)
  1232. {
  1233. unsigned long offset = mCompositionClause[index];
  1234. if (ATTR_TARGET_CONVERTED == mCompositionAttr[offset])
  1235. {
  1236. start = offset;
  1237. end = mCompositionClause[index + 1];
  1238. return;
  1239. }
  1240. ++index;
  1241. }
  1242. start = 0;
  1243. end = 0;
  1244. }
  1245. /******************************************************************************
  1246. *
  1247. * NAME
  1248. * IMEManager::GetCompositionFont
  1249. *
  1250. * DESCRIPTION
  1251. *
  1252. * INPUTS
  1253. *
  1254. * RESULT
  1255. *
  1256. ******************************************************************************/
  1257. bool IMEManager::GetCompositionFont(LPLOGFONT lpFont)
  1258. {
  1259. BOOL success = FALSE;
  1260. HIMC imc = ImmGetContext(mHWND);
  1261. if (imc)
  1262. {
  1263. success = ImmGetCompositionFont(imc, lpFont);
  1264. ImmReleaseContext(mHWND, imc);
  1265. }
  1266. return (success == TRUE);
  1267. }
  1268. /******************************************************************************
  1269. *
  1270. * NAME
  1271. * IMEManager::OpenCandidate
  1272. *
  1273. * DESCRIPTION
  1274. *
  1275. * INPUTS
  1276. *
  1277. * RESULT
  1278. *
  1279. ******************************************************************************/
  1280. void IMEManager::OpenCandidate(unsigned long candList)
  1281. {
  1282. WWDEBUG_SAY(("IMEManager: OpenCandidate\n"));
  1283. for (int index = 0; index < 32; index++)
  1284. {
  1285. if (candList & (1 << index))
  1286. {
  1287. IMECandidate& candidate = mCandidateColl[index];
  1288. candidate.Open(index, mHWND, mCodePage, mUseUnicode, mStartCandListFrom1);
  1289. candidate.Read();
  1290. CandidateEvent event(CANDIDATE_OPEN, &candidate);
  1291. NotifyObservers(event);
  1292. }
  1293. }
  1294. }
  1295. /******************************************************************************
  1296. *
  1297. * NAME
  1298. * IMEManager::ChangeCandidate
  1299. *
  1300. * DESCRIPTION
  1301. * The contents of the candidate list has changed.
  1302. *
  1303. * INPUTS
  1304. * Changed - Bitfield of the candidate lists that have changed.
  1305. *
  1306. * RESULT
  1307. * NONE
  1308. *
  1309. ******************************************************************************/
  1310. void IMEManager::ChangeCandidate(unsigned long candList)
  1311. {
  1312. WWDEBUG_SAY(("IMEManager: ChangeCandidate\n"));
  1313. for (int index = 0; index < 32; index++)
  1314. {
  1315. if (candList & (1 << index))
  1316. {
  1317. IMECandidate& candidate = mCandidateColl[index];
  1318. candidate.Read();
  1319. CandidateEvent event(CANDIDATE_CHANGE, &candidate);
  1320. NotifyObservers(event);
  1321. }
  1322. }
  1323. }
  1324. /******************************************************************************
  1325. *
  1326. * NAME
  1327. * IMEManager::CloseCandidate
  1328. *
  1329. * DESCRIPTION
  1330. *
  1331. * INPUTS
  1332. *
  1333. * RESULT
  1334. *
  1335. ******************************************************************************/
  1336. void IMEManager::CloseCandidate(unsigned long candList)
  1337. {
  1338. WWDEBUG_SAY(("IMEManager: CloseCandidate\n"));
  1339. for (int index = 0; index < 32; index++)
  1340. {
  1341. if (candList & (1 << index))
  1342. {
  1343. IMECandidate& candidate = mCandidateColl[index];
  1344. CandidateEvent event(CANDIDATE_CLOSE, &candidate);
  1345. NotifyObservers(event);
  1346. candidate.Close();
  1347. }
  1348. }
  1349. }
  1350. /******************************************************************************
  1351. *
  1352. * NAME
  1353. * IMEManager::GetGuideLine
  1354. *
  1355. * DESCRIPTION
  1356. *
  1357. * INPUTS
  1358. *
  1359. * RESULT
  1360. *
  1361. ******************************************************************************/
  1362. unsigned long IMEManager::GetGuideline(wchar_t* outString, int length)
  1363. {
  1364. unsigned long level = GL_LEVEL_NOGUIDELINE;
  1365. HIMC imc = ImmGetContext(mHWND);
  1366. if (imc)
  1367. {
  1368. level = ImmGetGuideLine(imc, GGL_LEVEL, NULL, 0);
  1369. if ((GL_LEVEL_NOGUIDELINE != level) && outString)
  1370. {
  1371. if (mUseUnicode)
  1372. {
  1373. DWORD size = ImmGetGuideLineW(imc, GGL_STRING, outString, (length * sizeof(wchar_t)));
  1374. WWASSERT(size <= (DWORD)length);
  1375. outString[size / sizeof(wchar_t)] = 0;
  1376. }
  1377. else
  1378. {
  1379. char temp[512];
  1380. DWORD size = ImmGetGuideLine(imc, GGL_STRING, temp, sizeof(temp));
  1381. temp[size] = 0;
  1382. MultiByteToWideChar(mCodePage, 0, temp, -1, outString, length);
  1383. outString[length] = 0;
  1384. }
  1385. }
  1386. ImmReleaseContext(mHWND, imc);
  1387. }
  1388. return level;
  1389. }
  1390. /******************************************************************************
  1391. *
  1392. * NAME
  1393. * IMEManager::IMECharHandler
  1394. *
  1395. * DESCRIPTION
  1396. *
  1397. * INPUTS
  1398. *
  1399. * RESULT
  1400. * True if character converted.
  1401. *
  1402. ******************************************************************************/
  1403. bool IMEManager::IMECharHandler(unsigned short dbcs)
  1404. {
  1405. unsigned long mbcs = dbcs;
  1406. // If this char has a lead byte then it is double byte. Swap the bytes
  1407. // for generate string order
  1408. if (dbcs & 0xFF00)
  1409. {
  1410. mbcs = (((dbcs & 0xFF) << 8) | (dbcs >> 8));
  1411. }
  1412. // Convert char to unicode
  1413. wchar_t unicode = 0;
  1414. MultiByteToWideChar(mCodePage, 0, (const char*)&mbcs, -1, &unicode, 1);
  1415. UnicodeChar event(unicode);
  1416. NotifyObservers(event);
  1417. return true;
  1418. }
  1419. /******************************************************************************
  1420. *
  1421. * NAME
  1422. * IMEManager::CharHandler
  1423. *
  1424. * DESCRIPTION
  1425. *
  1426. * INPUTS
  1427. *
  1428. * RESULT
  1429. * True if character handled.
  1430. *
  1431. ******************************************************************************/
  1432. bool IMEManager::CharHandler(unsigned short ch)
  1433. {
  1434. // Because DBCS characters are usually generated by IMEs (as two PostMessages),
  1435. // if a lead byte comes in, the trail byte should arrive very soon after.
  1436. // We wait here for the trail byte and store them into the text buffer together.
  1437. if (!IsDBCSLeadByte((unsigned char)ch))
  1438. {
  1439. return false;
  1440. }
  1441. // Wait an arbitrary amount of time for the trail byte to arrive.
  1442. // If it doesn't, then discard the lead byte. This could happen if the IME
  1443. // screwed up. Or, more likely, the user generated the lead byte through ALT-numpad.
  1444. MSG msg;
  1445. int i = 10;
  1446. while (!PeekMessage(&msg, mHWND, WM_CHAR, WM_CHAR, PM_REMOVE))
  1447. {
  1448. if (--i == 0)
  1449. {
  1450. return true;
  1451. }
  1452. Sleep(0);
  1453. }
  1454. // Convert char to unicode.
  1455. unsigned long dbcs = (unsigned long)(((unsigned)msg.wParam << 8) | ch);
  1456. wchar_t unicode = 0;
  1457. MultiByteToWideChar(mCodePage, 0, (const char*)&dbcs, 2, &unicode, 1);
  1458. UnicodeChar event(unicode);
  1459. NotifyObservers(event);
  1460. return true;
  1461. }
  1462. /******************************************************************************
  1463. *
  1464. * NAME
  1465. * IMEManager::
  1466. *
  1467. * DESCRIPTION
  1468. *
  1469. * INPUTS
  1470. *
  1471. * RESULT
  1472. *
  1473. ******************************************************************************/
  1474. long IMEManager::ConvertAttrForUnicode(unsigned char* mbcs, unsigned char* attr)
  1475. {
  1476. // Scale the attributes for unicode string length
  1477. unsigned char* mbsPtr = mbcs;
  1478. unsigned char* attrPtr = attr;
  1479. while (*mbsPtr != 0)
  1480. {
  1481. *attrPtr = attr[mbsPtr - mbcs];
  1482. ++attrPtr;
  1483. mbsPtr = _mbsinc(mbsPtr);
  1484. }
  1485. return (attrPtr - attr);
  1486. }
  1487. /******************************************************************************
  1488. *
  1489. * NAME
  1490. * IMEManager::
  1491. *
  1492. * DESCRIPTION
  1493. *
  1494. * INPUTS
  1495. *
  1496. * RESULT
  1497. *
  1498. ******************************************************************************/
  1499. long IMEManager::ConvertClauseForUnicode(unsigned char* mbcs, long length, unsigned long* clause)
  1500. {
  1501. //---------------------------------------------------------------------------
  1502. // Scale the clause offsets for unicode string
  1503. //---------------------------------------------------------------------------
  1504. unsigned char* mbsPtr = mbcs;
  1505. unsigned long offset = 0;
  1506. // The first clause is always zero so there is no need to adjust it.
  1507. int index = 1;
  1508. // The clause is terminated with the size of the string
  1509. while (clause[index] < (unsigned long)length)
  1510. {
  1511. // Count the number of characters in this clause
  1512. unsigned char* mbsStop = (mbcs + clause[index]);
  1513. while (mbsPtr < mbsStop)
  1514. {
  1515. ++offset;
  1516. mbsPtr = _mbsinc(mbsPtr);
  1517. }
  1518. clause[index] = offset;
  1519. ++index;
  1520. }
  1521. // Terminate the unicode adjusted clause with the string length
  1522. clause[index] = _mbslen(mbcs);
  1523. ++index;
  1524. return (&clause[index] - clause);
  1525. }
  1526. } // namespace IME