handle.c
上传用户:shdz666
上传日期:2007-01-03
资源大小:566k
文件大小:10k
源码类别:

输入法编程

开发平台:

Visual C++

  1. /*
  2.  * Copyright (C) 1999.4  Li ZhenChun
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License; or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that is will be useful, but
  10.  * WITHOUT ANY WARRANTY; without even the implied warranty of 
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12.  * General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 675 Mass Ave, Cambridge, M A 02139, USA.
  17.  *
  18.  * Author: Li ZhenChun  email: zhchli@163.net or zhchli@126.com
  19.  * 
  20.  */
  21. #include "freepy.h"
  22. /**********************************************************************/
  23. /*                                                                    */
  24. /* IMEKeyupHandler()                                                  */
  25. /*                                                                    */
  26. /* A function which handles WM_IMEKEYUP                               */
  27. /*                                                                    */
  28. /**********************************************************************/
  29. BOOL IMEKeyupHandler(
  30. HIMC hIMC,
  31. WPARAM wParam,
  32. LPARAM lParam,
  33. LPBYTE lpbKeyState)
  34. {
  35.     return FALSE;
  36. }
  37. /**********************************************************************/
  38. /*                                                                    */
  39. /* IMEKeydownHandler()                                                */
  40. /*                                                                    */
  41. /* A function which handles WM_IMEKEYDOWN                             */
  42. /*                                                                    */
  43. /**********************************************************************/
  44. BOOL IMEKeydownHandler(
  45. HIMC hIMC,
  46. WPARAM wParam,
  47. LPARAM lParam,
  48. LPBYTE lpbKeyState)
  49. {
  50.     WORD wVKey;
  51.     if( wVKey = ( wParam & 0x00FF ) ){
  52. if( !KeydownHandler( hIMC, wVKey, lParam, lpbKeyState ) ) {
  53. // This WM_IMEKEYDOWN has actual character code in itself.
  54. #ifdef _UNICODE
  55. CharHandler( hIMC,  HIWORD(wParam), lParam );
  56. #else
  57. CharHandler( hIMC,(WORD)((BYTE)HIBYTE(wParam)),lParam );
  58. #endif
  59. }
  60.     }
  61.     return TRUE;
  62. }
  63. /**********************************************************************/
  64. /*                                                                    */
  65. /* KeydownHandler()                                                */
  66. /*                                                                    */
  67. /* WM_KEYDOWN handler for dictionary routine                          */
  68. /*                                                                    */
  69. /* wParam                                                             */
  70. /* virtual key                                                        */
  71. /*                                                                    */
  72. /* lParam                                                             */
  73. /* differ depending on wParam                                         */
  74. /*                                                                    */
  75. /**********************************************************************/
  76. BOOL KeydownHandler(
  77. HIMC hIMC,
  78. WORD wParam,
  79. LONG lParam,
  80. LPBYTE lpbKeyState)
  81. {
  82. LPINPUTCONTEXT lpIMC;
  83. LPCOMPOSITIONSTRING lpCompStr;
  84. GENEMSG GnMsg;
  85. SHORT *lpwEditCaret;
  86. SHORT wUnConvPos;
  87. LPTSTR lpStr;
  88. lpIMC = ImmLockIMC(hIMC);
  89. lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC(lpIMC->hCompStr);
  90. lpwEditCaret = &(((LPMYCOMPSTR)lpCompStr)->FreePYComp.wEditCaret);
  91. wUnConvPos = ((LPMYCOMPSTR)lpCompStr)->FreePYComp.wUnConvPos;
  92. lpStr = GETLPCOMPSTR(lpCompStr);
  93.     switch( wParam ) 
  94.     {
  95.         case VK_ESCAPE:
  96.             MakeResultString(hIMC,FALSE); //clear all char
  97.             break;
  98. case VK_RETURN: //to PhraseToChar mode 
  99. {
  100. LPCANDIDATEINFO lpCandInfo;
  101. LPCANDIDATELIST lpCandList;
  102. LPTSTR lpPaintStr;
  103. lpCandInfo = (LPCANDIDATEINFO)ImmLockIMCC(lpIMC->hCandInfo);
  104. lpCandList = (LPCANDIDATELIST)((LPSTR)lpCandInfo  + lpCandInfo->dwOffset[0]);
  105. if( lpCandList->dwCount ){
  106. wConversionMode |= CONVERSION_MODE_PHRASETOCHAR;
  107. lpPaintStr = ((LPMYCOMPSTR)lpCompStr)->FreePYComp.szPaintCompStr;
  108. _tcscpy(lpPaintStr,PHTOCHMODEL);
  109. GnMsg.msg = WM_IME_COMPOSITION;
  110. GnMsg.wParam = 0;
  111. GnMsg.lParam = GCS_COMPSTR;
  112. GenerateMessage(hIMC, lpdwCurTransKey,(LPGENEMSG)&GnMsg);
  113. }
  114. ImmUnlockIMCC(lpIMC->hCandInfo);
  115. break;
  116. }
  117.         case VK_BACK:
  118.             DeleteCharBackward(hIMC,wParam); //delete char
  119.             break;
  120.         case VK_DELETE:
  121. DeleteCharForward(hIMC,wParam);
  122. break;
  123. case VK_HOME:
  124. if( *lpwEditCaret > 0 ) {
  125. if( wUnConvPos ){
  126. *lpwEditCaret = wUnConvPos;
  127. }
  128. else {
  129. *lpwEditCaret = 0;
  130. }
  131. GnMsg.msg = WM_IME_NOTIFY;
  132. GnMsg.wParam = IMN_SETCOMPOSITIONWINDOW;
  133. GnMsg.lParam = 0;
  134. GenerateMessage(hIMC, lpdwCurTransKey,(LPGENEMSG)&GnMsg);
  135. }
  136. else MessageBeep(0xFFFFFFFF );
  137. break;
  138. case VK_END:
  139. if( *lpwEditCaret < (SHORT)_tcslen(lpStr) ) {
  140. *lpwEditCaret = (SHORT)_tcslen(lpStr);
  141. GnMsg.msg = WM_IME_NOTIFY;
  142. GnMsg.wParam = IMN_SETCOMPOSITIONWINDOW;
  143. GnMsg.lParam = 0;
  144. GenerateMessage(hIMC, lpdwCurTransKey,(LPGENEMSG)&GnMsg);
  145. }
  146. else MessageBeep(0xFFFFFFFF );
  147. break;
  148. case VK_LEFT:
  149. case VK_UP:
  150. if( *lpwEditCaret > 0 ) {
  151. if( *lpwEditCaret == wUnConvPos) AddChar(hIMC,wParam,EDIT_BACK);
  152. else {
  153. *lpwEditCaret -= 1;
  154. GnMsg.msg = WM_IME_NOTIFY;
  155. GnMsg.wParam = IMN_SETCOMPOSITIONWINDOW;
  156. GnMsg.lParam = 0;
  157. GenerateMessage(hIMC, lpdwCurTransKey,(LPGENEMSG)&GnMsg);
  158. }
  159. }
  160. else MessageBeep(0xFFFFFFFF );
  161. break;
  162. case VK_RIGHT:
  163. case VK_DOWN:
  164. if( *lpwEditCaret < (SHORT)_tcslen(lpStr) ) {
  165. *lpwEditCaret += 1;
  166. GnMsg.msg = WM_IME_NOTIFY;
  167. GnMsg.wParam = IMN_SETCOMPOSITIONWINDOW;
  168. GnMsg.lParam = 0;
  169. GenerateMessage(hIMC, lpdwCurTransKey,(LPGENEMSG)&GnMsg);
  170. }
  171. else MessageBeep(0xFFFFFFFF );
  172. break;
  173.         case VK_SPACE:
  174. SpaceSelect(hIMC,wParam);
  175.             break;
  176.         default:
  177.             break;
  178.     }
  179. ImmUnlockIMCC(lpIMC->hCompStr);
  180. ImmUnlockIMC(hIMC);
  181.     if (( VK_0 <= wParam && VK_9 >= wParam ) ||
  182.         ( VK_A <= wParam && VK_Z >= wParam ) ||
  183.         ( VK_NUMPAD0 <= wParam && VK_NUMPAD9 >= wParam ) ||
  184.         ( VK_OEM_1 <= wParam && VK_OEM_9 >= wParam ) ||
  185.         ( VK_MULTIPLY <= wParam && VK_DIVIDE >= wParam ))
  186.     {
  187.         return( FALSE );
  188.     }
  189.     else
  190.         return( TRUE );
  191. }
  192. BOOL CharHandler(
  193. HIMC hIMC,
  194. WORD wParam,
  195. LONG lParam)
  196. {
  197.     LPINPUTCONTEXT lpIMC;
  198.     LPCOMPOSITIONSTRING lpCompStr;
  199. LPCANDIDATEINFO lpCandInfo;
  200.     DWORD dwStrLen;
  201.     GENEMSG GnMsg;
  202. LPTSTR lpConvStr;
  203.     lpIMC = ImmLockIMC(hIMC);
  204. lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC(lpIMC->hCompStr);
  205.     dwStrLen = lpCompStr->dwCompStrLen;
  206.     if (!dwStrLen)
  207.     {
  208. lpCandInfo = (LPCANDIDATEINFO)ImmLockIMCC(lpIMC->hCandInfo);
  209. ClearCompStr(lpCompStr);
  210. ClearCandidate(lpCandInfo);
  211. ImmUnlockIMCC(lpIMC->hCandInfo);
  212.         GnMsg.msg = WM_IME_STARTCOMPOSITION;
  213.         GnMsg.wParam = 0;
  214.         GnMsg.lParam = 0;
  215.         GenerateMessage(hIMC, lpdwCurTransKey,(LPGENEMSG)&GnMsg);
  216. if(wParam == _T('#') ) {
  217. ImmUnlockIMCC(lpIMC->hCompStr);
  218.     ImmUnlockIMC(hIMC);
  219. return RepeatPreResult(hIMC);
  220. }
  221. if( (wParam < _T('a') || wParam > _T('z')) &&
  222. (wParam < _T('A') || wParam > _T('Z')) &&
  223. (wParam < _T('0') || wParam > _T('9')) ){
  224. ImmUnlockIMCC(lpIMC->hCompStr);
  225.     ImmUnlockIMC(hIMC);
  226. return GeneratePunct(hIMC,wParam);
  227. }
  228. if( wParam == _T('i') ) {
  229. LPTSTR lpStr;
  230. wConversionMode |= CONVERSION_MODE_I;
  231. lpStr = GETLPCOMPSTR(lpCompStr);
  232. *lpStr = (TCHAR)wParam;
  233. *(lpStr+1) = _T('');
  234. lpCompStr->dwCompStrLen = _tcslen(lpStr);
  235. lpStr = ((LPMYCOMPSTR)lpCompStr)->FreePYComp.szPaintCompStr;
  236. _tcscpy(lpStr,IMODEL);
  237. GnMsg.msg = WM_IME_COMPOSITION;
  238. GnMsg.wParam = 0;
  239. GnMsg.lParam = GCS_COMPSTR;
  240. GenerateMessage(hIMC, lpdwCurTransKey,(LPGENEMSG)&GnMsg);
  241. goto my_exit;
  242. }
  243. if( wParam == _T('u') ) {
  244. LPTSTR lpStr;
  245. wConversionMode |= CONVERSION_MODE_U;
  246. lpStr = GETLPCOMPSTR(lpCompStr);
  247. *lpStr = (TCHAR)wParam;
  248. *(lpStr+1) = _T('');
  249. lpCompStr->dwCompStrLen = _tcslen(lpStr);
  250. lpStr = ((LPMYCOMPSTR)lpCompStr)->FreePYComp.szPaintCompStr;
  251. _tcscpy(lpStr,UMODEL);
  252. GnMsg.msg = WM_IME_COMPOSITION;
  253. GnMsg.wParam = 0;
  254. GnMsg.lParam = GCS_COMPSTR;
  255. GenerateMessage(hIMC, lpdwCurTransKey,(LPGENEMSG)&GnMsg);
  256. goto my_exit;
  257. }
  258. if( wParam == _T('v') ) {
  259. LPTSTR lpStr;
  260. wConversionMode |= CONVERSION_MODE_V;
  261. lpStr = GETLPCOMPSTR(lpCompStr);
  262. *lpStr = (TCHAR)wParam;
  263. *(lpStr+1) = _T('');
  264. lpCompStr->dwCompStrLen = _tcslen(lpStr);
  265. lpStr = ((LPMYCOMPSTR)lpCompStr)->FreePYComp.szPaintCompStr;
  266. _tcscpy(lpStr,VMODEL);
  267. GnMsg.msg = WM_IME_COMPOSITION;
  268. GnMsg.wParam = 0;
  269. GnMsg.lParam = GCS_COMPSTR;
  270. GenerateMessage(hIMC, lpdwCurTransKey,(LPGENEMSG)&GnMsg);
  271. goto my_exit;
  272. }
  273.     }
  274. if(wConversionMode & CONVERSION_MODE_PHRASETOCHAR) {
  275. if( CharHandlePhToCh(hIMC,wParam,lParam) ) goto my_exit;
  276. }
  277. else if(wConversionMode & CONVERSION_MODE_I) {
  278. if( CharHandleI(hIMC,wParam,lParam) ) goto my_exit;
  279. }
  280. else if(wConversionMode & CONVERSION_MODE_U) {
  281. if( CharHandleU(hIMC,wParam,lParam) ) goto my_exit;
  282. }
  283. else if(wConversionMode & CONVERSION_MODE_V) {
  284. if( CharHandleV(hIMC,wParam,lParam) ) goto my_exit;
  285. }
  286. else{
  287. if( CharHandleNormal(hIMC,wParam,lParam) ) goto my_exit;
  288. }
  289. if( wParam >= _T('!') && wParam <= _T('~') && lpCompStr->dwCompStrLen == 0) {
  290. lpConvStr = ((LPMYCOMPSTR)lpCompStr)->FreePYComp.szConvCompStr;
  291. if(wConversionSet & CONVERSION_SET_FULLSHAPE) {
  292. BYTE bChar[4];
  293. bChar[0] = 0xA3;
  294. bChar[1] = (BYTE)wParam + 0x80;
  295. bChar[2] = 0;
  296. bChar[3] = 0;
  297. memcpy((LPBYTE)lpConvStr,bChar,4);
  298. }
  299. else{
  300. *lpConvStr = (TCHAR)wParam;
  301. *(lpConvStr+1) = _T('');
  302. }
  303. MakeResultString(hIMC,TRUE);
  304. }
  305. my_exit:
  306. ImmUnlockIMCC(lpIMC->hCompStr);
  307.     ImmUnlockIMC(hIMC);
  308. return TRUE;
  309. }