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

输入法编程

开发平台:

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. BOOL WINAPI ImeInquire(LPIMEINFO lpIMEInfo,LPTSTR lpszUIClass,LPCTSTR lpszOption)
  23. {
  24. DebugLog(1,(DebugLogFile,"ImeInquiren"));
  25.     lpIMEInfo->dwPrivateDataSize = sizeof(UIEXTRA);
  26.     lpIMEInfo->fdwProperty = IME_PROP_KBD_CHAR_FIRST |
  27. #ifdef _UNICODE
  28.                              IME_PROP_UNICODE |
  29. #endif
  30.                              IME_PROP_SPECIAL_UI;
  31.     lpIMEInfo->fdwConversionCaps = IME_CMODE_FULLSHAPE |
  32. IME_CMODE_NATIVE;
  33.     lpIMEInfo->fdwSentenceCaps = IME_SMODE_NONE;
  34.     lpIMEInfo->fdwUICaps = UI_CAP_2700;
  35. lpIMEInfo->fdwSCSCaps = 0;
  36.     lpIMEInfo->fdwSelectCaps = SELECT_CAP_CONVERSION;
  37.     _tcscpy(lpszUIClass,UICLASSNAME);
  38.     return TRUE;
  39. }
  40. BOOL WINAPI ImeConfigure(HKL hKL,HWND hWnd, DWORD dwMode, LPVOID lpData)
  41. {
  42. DebugLog(1,(DebugLogFile,"ImeConfiguren"));
  43. DialogBox( hInst,MAKEINTRESOURCE(DIALOGCONFIG),hWnd,ConfigDialogProc);
  44. InvalidateRect(hWnd,NULL,FALSE);
  45. return TRUE;
  46. }
  47. DWORD WINAPI ImeConversionList(HIMC hIMC,LPCTSTR lpSource,LPCANDIDATELIST lpCandList,DWORD dwBufLen,UINT uFlag)
  48. {
  49. DebugLog(1,(DebugLogFile,"ImeConversionListn"));
  50.     return 0;
  51. }
  52. BOOL WINAPI ImeDestroy(UINT uForce)
  53. {
  54. DebugLog(1,(DebugLogFile,"ImeDestroyn"));
  55.     return FALSE;
  56. }
  57. LRESULT WINAPI ImeEscape(HIMC hIMC,UINT uSubFunc,LPVOID lpData)
  58. {
  59. DebugLog(1,(DebugLogFile,"ImeEscapen"));
  60. return FALSE;
  61. }
  62. BOOL WINAPI ImeProcessKey(HIMC hIMC,UINT vKey,LPARAM lKeyData,CONST LPBYTE lpbKeyState)
  63. {
  64.     BOOL fRet = FALSE;
  65.     BOOL fOpen;
  66.     BOOL fCompStr = FALSE;
  67.     LPINPUTCONTEXT lpIMC;
  68.     LPCOMPOSITIONSTRING lpCompStr;
  69. static BOOL fPressOther = FALSE;
  70. // static BOOL fFirst = TRUE;
  71. DebugLog(1,(DebugLogFile,"ImeProcessKeyn"));
  72.     if ((lKeyData & 0x80000000) && vKey != VK_CONTROL)
  73.         return FALSE;
  74. if (!(lKeyData & 0x80000000) && vKey == VK_CONTROL)
  75. return FALSE;
  76. if (lpbKeyState[VK_MENU] & 0x80 ) return FALSE;
  77. if(vKey != VK_CONTROL && lpbKeyState[VK_CONTROL] & 0x80 ) {
  78. fPressOther = TRUE;
  79. return FALSE;
  80. }
  81.     if (!(lpIMC = ImmLockIMC(hIMC)))
  82.         return FALSE;
  83.     fOpen = lpIMC->fOpen;
  84. if(vKey == VK_CONTROL && (lKeyData & 0x80000000) && !fPressOther){ // && !fFirst){
  85. GENEMSG GnMsg;
  86. fPressOther = FALSE;
  87. if(fOpen) {
  88. lpIMC->fOpen=FALSE;
  89. MakeResultString(hIMC,FALSE);
  90. }
  91. else lpIMC->fOpen=TRUE;
  92. GnMsg.msg = WM_IME_NOTIFY;
  93. GnMsg.wParam = IMN_SETOPENSTATUS;
  94. GnMsg.lParam = 0;
  95. GenerateMessage(hIMC, lpdwCurTransKey,(LPGENEMSG)&GnMsg);
  96. return FALSE;
  97. }
  98. fPressOther = FALSE;
  99. // if(fFirst) fFirst = FALSE;
  100. /*
  101. //if CapsLock is pressed ,then return false;
  102. if (lpbKeyState[VK_CAPITAL] & 0x01) {
  103. return FALSE;
  104. }
  105.     if (fOpen)
  106.     {
  107.         if (lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC(lpIMC->hCompStr))
  108.         {
  109.             if ((lpCompStr->dwSize > sizeof(COMPOSITIONSTRING)) && 
  110.                 (lpCompStr->dwCompStrLen))
  111.                 fCompStr = TRUE;
  112. ImmUnlockIMCC(lpIMC->hCompStr);
  113.         }
  114. if (dwConversionMode & CONVERSION_MODE_FULLSHAPE){
  115. if (lpbKeyState[VK_SHIFT] & 0x80)
  116.     {
  117.     if (fCompStr)
  118.     fRet = (BOOL)bFullCompSht[vKey];
  119. else
  120. fRet = (BOOL)bFullNoCompSht[vKey];
  121. }
  122. else 
  123. {
  124. if (fCompStr)
  125. fRet = (BOOL)bFullComp[vKey];
  126. else
  127. fRet = (BOOL)bFullNoComp[vKey];
  128. }
  129. }
  130. else{
  131. if (lpbKeyState[VK_SHIFT] & 0x80)
  132.     {
  133.     if (fCompStr)
  134.     fRet = (BOOL)bCompSht[vKey];
  135. else
  136. fRet = (BOOL)bNoCompSht[vKey];
  137. }
  138. else 
  139. {
  140. if (fCompStr)
  141. fRet = (BOOL)bComp[vKey];
  142. else
  143. fRet = (BOOL)bNoComp[vKey];
  144. }
  145. }
  146.     }
  147. */
  148.     if (fOpen)
  149.     {
  150.         if (lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC(lpIMC->hCompStr))
  151.         {
  152.             if ((lpCompStr->dwSize > sizeof(COMPOSITIONSTRING)) && 
  153.                 (lpCompStr->dwCompStrLen))
  154.                 fCompStr = TRUE;
  155. ImmUnlockIMCC(lpIMC->hCompStr);
  156.         }
  157. if (fCompStr)
  158. fRet = (BOOL)bComp[vKey];
  159. else
  160. fRet = (BOOL)bNoComp[vKey];
  161. }
  162.     ImmUnlockIMC(hIMC);
  163.     return fRet;
  164. }
  165. BOOL WINAPI ImeSelect(HIMC hIMC, BOOL fSelect)
  166. {
  167.     LPINPUTCONTEXT lpIMC;
  168. DebugLog(1,(DebugLogFile,"ImeSelectn"));
  169.     if (fSelect)
  170.         UpdateIndicIcon(hIMC);
  171.     // it's NULL context.
  172.     if (!hIMC)
  173.         return TRUE;
  174.     if (lpIMC = ImmLockIMC(hIMC))
  175.     {
  176.         if (fSelect)
  177.         {
  178.             LPCOMPOSITIONSTRING lpCompStr;
  179.             LPCANDIDATEINFO lpCandInfo;
  180. HKEY hKey;
  181. DWORD dwConvSet;
  182. DWORD dwData = sizeof(DWORD);
  183. BOOL fFlag = TRUE;
  184.             // Init the general member of IMC.
  185. lpIMC->fOpen=TRUE;
  186.             lpIMC->hCompStr = ImmReSizeIMCC(lpIMC->hCompStr,sizeof(MYCOMPSTR));
  187.             if (lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC(lpIMC->hCompStr)) 
  188.             {
  189. InitCompStr(lpCompStr);
  190.                 ImmUnlockIMCC(lpIMC->hCompStr);
  191.             }
  192.             lpIMC->hCandInfo = ImmReSizeIMCC(lpIMC->hCandInfo,sizeof(MYCAND));
  193.             if (lpCandInfo = (LPCANDIDATEINFO)ImmLockIMCC(lpIMC->hCandInfo)) 
  194.             {
  195. InitCandInfo(lpCandInfo);
  196.                 ImmUnlockIMCC(lpIMC->hCandInfo);
  197.             }
  198. if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  199. _T("Software\freepy"),
  200. 0,
  201. KEY_READ,
  202. &hKey) == ERROR_SUCCESS ){
  203. if( RegQueryValueEx( hKey,
  204. _T("ConversionSet"),
  205. NULL,
  206. NULL,
  207. (LPBYTE)&dwConvSet,
  208. &dwData) == ERROR_SUCCESS ){
  209. wConversionSet = (WORD) dwConvSet;
  210. fFlag = FALSE;
  211. }
  212. RegCloseKey(hKey);
  213. }
  214. if( fFlag) {
  215. wConversionSet = CONVERSION_SET_PUNCT;
  216. wConversionSet |= CONVERSION_SET_SORT;
  217. wConversionSet |= CONVERSION_SET_FOLLOW;
  218. wConversionSet |= CONVERSION_SET_GBK;
  219. }
  220.         }
  221.     }
  222.     ImmUnlockIMC(hIMC);
  223.     return TRUE;
  224. }
  225. BOOL WINAPI ImeSetActiveContext(HIMC hIMC,BOOL fFlag)
  226. {
  227. DebugLog(1,(DebugLogFile,"ImeSetActiveContextn"));
  228.     UpdateIndicIcon(hIMC);
  229.     return TRUE;
  230. }
  231. UINT WINAPI ImeToAsciiEx (UINT uVKey,UINT uScanCode,CONST LPBYTE lpbKeyState,LPDWORD lpdwTransKey,UINT fuState,HIMC hIMC)
  232. {
  233.     LPARAM lParam;
  234.     LPINPUTCONTEXT lpIMC;
  235.     BOOL fOpen;
  236. DebugLog(1,(DebugLogFile,"ImeToAsciiExn"));
  237.     lpdwCurTransKey = lpdwTransKey;
  238.     lParam = ((DWORD)uScanCode << 16) + 1L;
  239.     
  240.     // Init uNumTransKey here.
  241.     uNumTransKey = 0;
  242.     // if hIMC is NULL, this means DISABLE IME.
  243.     if (!hIMC)
  244.         return 0;
  245.     if (!(lpIMC = ImmLockIMC(hIMC)))
  246.         return 0;
  247.     fOpen = lpIMC->fOpen;
  248.     ImmUnlockIMC(hIMC);
  249.     // The current status of IME is "closed".
  250.     if (!fOpen)
  251.         goto my_exit;
  252.     if (uScanCode & 0x8000)
  253.         IMEKeyupHandler( hIMC, uVKey, lParam, lpbKeyState);
  254.     else
  255.         IMEKeydownHandler( hIMC, uVKey, lParam, lpbKeyState);
  256.     // Clear static value, no more generated message!
  257.     lpdwCurTransKey = NULL;
  258. my_exit:
  259.     // If trans key buffer that is allocated by USER.EXE full up,
  260.     // the return value is the negative number.
  261.     if (fOverTransKey)
  262.     {
  263.         return (int)uNumTransKey;
  264.     }
  265.     return (int)uNumTransKey;
  266. }
  267. BOOL WINAPI NotifyIME(HIMC hIMC,DWORD dwAction,DWORD dwIndex,DWORD dwValue)
  268. {
  269.     BOOL bRet = FALSE;
  270. LPINPUTCONTEXT lpIMC;
  271. DebugLog(1,(DebugLogFile,"NotifyIMEn"));
  272.     switch(dwAction)
  273.     {
  274. case NI_OPENCANDIDATE:
  275. DebugLog(1,(DebugLogFile,"NotifyIME:NI_OPENCANDIDATEn"));
  276. break;
  277. case NI_CLOSECANDIDATE:
  278. DebugLog(1,(DebugLogFile,"NotifyIME:NI_CLOSECANDIDATEn"));
  279. break;
  280. case NI_SELECTCANDIDATESTR:
  281. DebugLog(1,(DebugLogFile,"NotifyIME:NI_SELECTCANDIDATESTRn"));
  282. break;
  283. case NI_CHANGECANDIDATELIST:
  284. DebugLog(1,(DebugLogFile,"NotifyIME:NI_CHANGECANDIDATELISTn"));
  285. break;
  286. case NI_SETCANDIDATE_PAGESTART:
  287. DebugLog(1,(DebugLogFile,"NotifyIME:NI_SETCANDIDATE_PAGESTARTn"));
  288. break;
  289. case NI_SETCANDIDATE_PAGESIZE:
  290. DebugLog(1,(DebugLogFile,"NotifyIME:NI_SETCANDIDATE_PAGESIZEn"));
  291. break;
  292. case NI_CONTEXTUPDATED:
  293. DebugLog(1,(DebugLogFile,"NotifyIME:NI_CONTEXTUPDATEDn"));
  294. switch (dwValue)
  295. {
  296. case IMC_SETCONVERSIONMODE:
  297. DebugLog(1,(DebugLogFile,"NotifyIME:NI_CONTEXTUPDATED:IMC_SETCONVERSIONMODEn"));
  298. break;
  299. case IMC_SETSENTENCEMODE:
  300. DebugLog(1,(DebugLogFile,"NotifyIME:NI_CONTEXTUPDATED:IMC_SETSENTENCEMODEn"));
  301. break;
  302. case IMC_SETCANDIDATEPOS:
  303. DebugLog(1,(DebugLogFile,"NotifyIME:NI_CONTEXTUPDATED:IMC_SETCANDIDATEPOSn"));
  304. break;
  305. case IMC_SETCOMPOSITIONFONT:
  306. DebugLog(1,(DebugLogFile,"NotifyIME:NI_CONTEXTUPDATED:IMC_SETCOMPOSITIONFONTn"));
  307. break;
  308. case IMC_SETCOMPOSITIONWINDOW:
  309. DebugLog(1,(DebugLogFile,"NotifyIME:NI_CONTEXTUPDATED:IMC_SETCOMPOSITIONWINDOWn"));
  310. break;
  311. case IMC_SETOPENSTATUS:
  312. DebugLog(1,(DebugLogFile,"NotifyIME:NI_CONTEXTUPDATED:IMC_SETOPENSTATUSn"));
  313. lpIMC = ImmLockIMC(hIMC);
  314. if (lpIMC)
  315. {
  316. if (!lpIMC->fOpen && IsCompStr(hIMC))
  317. MakeResultString( hIMC,FALSE);
  318. ImmUnlockIMC(hIMC);
  319. }
  320. UpdateIndicIcon(hIMC);
  321. bRet = TRUE;
  322. break;
  323. default:
  324. break;
  325. }
  326. break;
  327. case NI_COMPOSITIONSTR:
  328. DebugLog(1,(DebugLogFile,"NotifyIME:NI_COMPOSITIONSTRn"));
  329. switch (dwIndex)
  330. {
  331. case CPS_COMPLETE:
  332. DebugLog(1,(DebugLogFile,"NotifyIME:NI_COMPOSITIONSTR:CPS_COMPLETEn"));
  333. break;
  334. case CPS_CONVERT:
  335. DebugLog(1,(DebugLogFile,"NotifyIME:NI_COMPOSITIONSTR:CPS_CONVERTn"));
  336. break;
  337. case CPS_REVERT:
  338. DebugLog(1,(DebugLogFile,"NotifyIME:NI_COMPOSITIONSTR:CPS_REVERTn"));
  339. break;
  340. case CPS_CANCEL:
  341. DebugLog(1,(DebugLogFile,"NotifyIME:NI_COMPOSITIONSTR:CPS_CANCELn"));
  342. break;
  343. default:
  344. break;
  345. }
  346. break;
  347. default:
  348. break;
  349.     }
  350.     return bRet;
  351. }
  352. BOOL WINAPI ImeRegisterWord(LPCTSTR lpRead, DWORD dw, LPCTSTR lpStr)
  353. {
  354. DebugLog(1,(DebugLogFile,"ImeRegisterWordn"));
  355.     return FALSE;
  356. }
  357. BOOL WINAPI ImeUnregisterWord(LPCTSTR lpRead, DWORD dw, LPCTSTR lpStr)
  358. {
  359. DebugLog(1,(DebugLogFile,"ImeUnregisterWordn"));
  360.     return FALSE;
  361. }
  362. UINT WINAPI ImeGetRegisterWordStyle(UINT nItem, LPSTYLEBUF lp)
  363. {
  364. DebugLog(1,(DebugLogFile,"ImeGetRegisterWordStylen"));
  365. return 0;
  366. }
  367. UINT WINAPI ImeEnumRegisterWord(REGISTERWORDENUMPROC lpfn, LPCTSTR lpRead, DWORD dw, LPCTSTR lpStr, LPVOID lpData)
  368. {
  369. DebugLog(1,(DebugLogFile,"ImeEnumRegisterWordn"));
  370. return 0;
  371. }
  372. BOOL WINAPI ImeSetCompositionString(HIMC hIMC, DWORD dwIndex, LPCVOID lpComp, DWORD dwComp, LPCVOID lpRead, DWORD dwRead)
  373. {
  374. DebugLog(1,(DebugLogFile,"ImeSetCompositionStringn"));
  375.     return FALSE;
  376. }