Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

501 lines
12 KiB

  1. /*++
  2. Copyright (c) 2001, Microsoft Corporation
  3. Module Name:
  4. dimmex.cpp
  5. Abstract:
  6. This file implements the CActiveIMMAppEx Class.
  7. Author:
  8. Revision History:
  9. Notes:
  10. --*/
  11. #include "private.h"
  12. #include "msctfp.h"
  13. #include "dimmex.h"
  14. #include "list.h"
  15. #include "atom.h"
  16. #include "globals.h"
  17. #include "tls.h"
  18. #include "apcompat.h"
  19. //+---------------------------------------------------------------------------
  20. //
  21. // CGuidMapList
  22. // Allocated by Global data object !!
  23. //
  24. //----------------------------------------------------------------------------
  25. CGuidMapList *g_pGuidMapList = NULL;
  26. BOOL InitFilterList()
  27. {
  28. BOOL bRet;
  29. EnterCriticalSection(g_cs);
  30. if (!g_pGuidMapList)
  31. g_pGuidMapList = new CGuidMapList;
  32. bRet = g_pGuidMapList ? TRUE : FALSE;
  33. LeaveCriticalSection(g_cs);
  34. return bRet;
  35. }
  36. void UninitFilterList()
  37. {
  38. //
  39. // this function is called in DllMain(). Don't need to be protected by
  40. // g_cs.
  41. //
  42. // EnterCriticalSection(g_cs);
  43. if (g_pGuidMapList)
  44. delete g_pGuidMapList;
  45. g_pGuidMapList = NULL;
  46. // LeaveCriticalSection(g_cs);
  47. }
  48. //+---------------------------------------------------------------------------
  49. //
  50. // CAtomObject
  51. // Allocated by Global data object !!
  52. //
  53. //----------------------------------------------------------------------------
  54. CAtomObject *g_pAimmAtomObject = NULL;
  55. BOOL InitAimmAtom()
  56. {
  57. BOOL bRet;
  58. EnterCriticalSection(g_cs);
  59. if (! FindAtom(TF_ENABLE_PROCESS_ATOM)) {
  60. //
  61. // This process is not WinWord XP nor Cicero aware application
  62. //
  63. bRet = FALSE;
  64. }
  65. else {
  66. if (!g_pAimmAtomObject) {
  67. g_pAimmAtomObject = new CAtomObject();
  68. if (g_pAimmAtomObject) {
  69. if (g_pAimmAtomObject->_InitAtom(AIMM12_PROCESS_ATOM) != S_OK) {
  70. delete g_pAimmAtomObject;
  71. g_pAimmAtomObject = NULL;
  72. }
  73. }
  74. }
  75. bRet = g_pAimmAtomObject ? TRUE : FALSE;
  76. }
  77. LeaveCriticalSection(g_cs);
  78. return bRet;
  79. }
  80. void UninitAimmAtom()
  81. {
  82. //
  83. // this function is called in DllMain(). Don't need to be protected by
  84. // g_cs.
  85. //
  86. // EnterCriticalSection(g_cs);
  87. if (g_pAimmAtomObject)
  88. delete g_pAimmAtomObject;
  89. g_pAimmAtomObject = NULL;
  90. // LeaveCriticalSection(g_cs);
  91. }
  92. //+---------------------------------------------------------------------------
  93. //
  94. // Activate
  95. //
  96. //----------------------------------------------------------------------------
  97. STDAPI CComActiveIMMApp::Activate(BOOL fRestoreLayout)
  98. {
  99. HRESULT hr = S_OK;
  100. TLS* ptls = TLS::GetTLS();
  101. if (ptls == NULL)
  102. {
  103. return E_OUTOFMEMORY;
  104. }
  105. if (! InitAimmAtom()) {
  106. if (! FindAtom(AIMM12_PROCESS_ATOM))
  107. AddAtom(AIMM12_PROCESS_ATOM);
  108. }
  109. else {
  110. g_pAimmAtomObject->_Activate(); // Activate AIMM12 atom
  111. }
  112. int cnt = ptls->IncrementAIMMRefCnt();
  113. #ifndef KACF_DISABLECICERO
  114. #define KACF_DISABLECICERO 0x00000100 // If set. Cicero support for the current process
  115. // is disabled.
  116. #endif
  117. if (! IsOldAImm() && ! IsCUAS_ON() && ! APPCOMPATFLAG(KACF_DISABLECICERO) && cnt == 1)
  118. {
  119. hr = imm32prev::CtfAImmActivate(&m_hModCtfIme);
  120. }
  121. return hr;
  122. }
  123. //+---------------------------------------------------------------------------
  124. //
  125. // Deactivate
  126. //
  127. //----------------------------------------------------------------------------
  128. STDAPI CComActiveIMMApp::Deactivate()
  129. {
  130. HRESULT hr = S_OK;
  131. TLS* ptls = TLS::GetTLS();
  132. if (ptls == NULL)
  133. {
  134. return E_OUTOFMEMORY;
  135. }
  136. if (InitAimmAtom()) {
  137. g_pAimmAtomObject->_Deactivate(); // Deactivate AIMM12 atom
  138. }
  139. int cnt = ptls->DecrementAIMMRefCnt();
  140. if (! IsOldAImm() && ! IsCUAS_ON() && ! APPCOMPATFLAG(KACF_DISABLECICERO) && cnt == 0)
  141. {
  142. hr = imm32prev::CtfAImmDeactivate(m_hModCtfIme);
  143. }
  144. return hr;
  145. }
  146. //+---------------------------------------------------------------------------
  147. //
  148. // FilterClientWindows
  149. //
  150. //----------------------------------------------------------------------------
  151. STDAPI CComActiveIMMApp::FilterClientWindows(ATOM *aaWindowClasses, UINT uSize)
  152. {
  153. return FilterClientWindowsGUIDMap(aaWindowClasses, uSize, NULL);
  154. }
  155. STDAPI CComActiveIMMApp::FilterClientWindowsGUIDMap(ATOM *aaWindowClasses, UINT uSize, BOOL *aaGuidMap)
  156. {
  157. if (!InitFilterList())
  158. return E_OUTOFMEMORY;
  159. return g_pGuidMapList->_Update(aaWindowClasses, uSize, aaGuidMap);
  160. }
  161. //+---------------------------------------------------------------------------
  162. //
  163. // FilterClientWindowsEx
  164. //
  165. //----------------------------------------------------------------------------
  166. STDAPI CComActiveIMMApp::FilterClientWindowsEx(HWND hWnd, BOOL fGuidMap)
  167. {
  168. if (!InitFilterList())
  169. return E_OUTOFMEMORY;
  170. return g_pGuidMapList->_Update(hWnd, fGuidMap);
  171. }
  172. //+---------------------------------------------------------------------------
  173. //
  174. // UnfilterClientWindowsEx
  175. //
  176. //----------------------------------------------------------------------------
  177. STDAPI CComActiveIMMApp::UnfilterClientWindowsEx(HWND hWnd)
  178. {
  179. if (!InitFilterList())
  180. return E_OUTOFMEMORY;
  181. return g_pGuidMapList->_Remove(hWnd);
  182. }
  183. //+---------------------------------------------------------------------------
  184. //
  185. // GetGuidAtom
  186. //
  187. //----------------------------------------------------------------------------
  188. STDAPI CComActiveIMMApp::GetGuidAtom(HIMC hImc, BYTE bAttr, TfGuidAtom *pGuidAtom)
  189. {
  190. return imm32prev::CtfImmGetGuidAtom(hImc, bAttr, pGuidAtom);
  191. }
  192. //+---------------------------------------------------------------------------
  193. //
  194. // GetCodePageA
  195. //
  196. //----------------------------------------------------------------------------
  197. STDAPI CComActiveIMMApp::GetCodePageA(HKL hKL, UINT *puCodePage)
  198. {
  199. if (puCodePage == NULL)
  200. return E_INVALIDARG;
  201. TraceMsg(TF_FUNC, "CComActiveIMMApp::GetCodePageA");
  202. *puCodePage = imm32prev::GetKeyboardLayoutCP(hKL);
  203. return S_OK;
  204. }
  205. //+---------------------------------------------------------------------------
  206. //
  207. // GetLangId
  208. //
  209. //----------------------------------------------------------------------------
  210. STDAPI CComActiveIMMApp::GetLangId(HKL hKL, LANGID *plid)
  211. {
  212. if (plid == NULL)
  213. return E_INVALIDARG;
  214. TraceMsg(TF_FUNC, "CComActiveIMMApp::GetLangId");
  215. *plid = LOWORD(HandleToUlong(hKL));
  216. return S_OK;
  217. }
  218. //+---------------------------------------------------------------------------
  219. //
  220. // QueryService
  221. //
  222. //----------------------------------------------------------------------------
  223. STDAPI CComActiveIMMApp::QueryService(
  224. REFGUID guidService,
  225. REFIID riid,
  226. void **ppv
  227. )
  228. {
  229. ITfThreadMgr *ptim = NULL;
  230. ITfDocumentMgr *pdim = NULL;
  231. ITfContext *pic = NULL;
  232. HRESULT hr = E_NOINTERFACE;
  233. if (ppv == NULL) {
  234. return E_INVALIDARG;
  235. }
  236. *ppv = NULL;
  237. if (!IsEqualGUID(guidService, GUID_SERVICE_TF))
  238. return E_INVALIDARG;
  239. if (FAILED(TF_CreateThreadMgr(&ptim)))
  240. return E_FAIL;
  241. if (!ptim)
  242. return E_FAIL;
  243. if (IsEqualIID(riid, IID_ITfThreadMgr)) {
  244. *ppv = SAFECAST(ptim, ITfThreadMgr*);
  245. ptim->AddRef();
  246. hr = S_OK;
  247. }
  248. else {
  249. if (FAILED(ptim->GetFocus(&pdim)))
  250. pdim = NULL;
  251. if (IsEqualIID(riid, IID_ITfDocumentMgr)) {
  252. if (pdim) {
  253. *ppv = SAFECAST(pdim, ITfDocumentMgr*);
  254. pdim->AddRef();
  255. hr = S_OK;
  256. }
  257. }
  258. else if (IsEqualIID(riid, IID_ITfContext)) {
  259. if (pdim) {
  260. if (SUCCEEDED(pdim->GetTop(&pic)) && pic) {
  261. *ppv = SAFECAST(pic, ITfContext*);
  262. pic->AddRef();
  263. hr = S_OK;
  264. }
  265. }
  266. }
  267. }
  268. if (ptim)
  269. ptim->Release();
  270. if (pdim)
  271. pdim->Release();
  272. if (pic)
  273. pic->Release();
  274. return hr;
  275. }
  276. //+---------------------------------------------------------------------------
  277. //
  278. // SetThreadCompartmentValue
  279. //
  280. //----------------------------------------------------------------------------
  281. STDAPI CComActiveIMMApp::SetThreadCompartmentValue(
  282. REFGUID rguid,
  283. VARIANT *pvar
  284. )
  285. {
  286. if (pvar == NULL)
  287. return E_INVALIDARG;
  288. ITfThreadMgr *ptim = NULL;
  289. HRESULT hr = E_FAIL;
  290. if (FAILED(TF_CreateThreadMgr(&ptim)))
  291. return hr;
  292. if (ptim)
  293. {
  294. ITfCompartment *pComp;
  295. if (SUCCEEDED(GetCompartment((IUnknown *)ptim, rguid, &pComp)))
  296. {
  297. //
  298. // Hack to get App Client Id.
  299. //
  300. TfClientId tid;
  301. ptim->Activate(&tid);
  302. ptim->Deactivate();
  303. hr = pComp->SetValue(tid, pvar);
  304. pComp->Release();
  305. }
  306. ptim->Release();
  307. }
  308. return hr;
  309. }
  310. //+---------------------------------------------------------------------------
  311. //
  312. // GetThreadCompartmentValue
  313. //
  314. //----------------------------------------------------------------------------
  315. STDAPI CComActiveIMMApp::GetThreadCompartmentValue(
  316. REFGUID rguid,
  317. VARIANT *pvar
  318. )
  319. {
  320. if (pvar == NULL)
  321. return E_INVALIDARG;
  322. HRESULT hr = E_FAIL;
  323. ITfThreadMgr *ptim = NULL;
  324. if (FAILED(TF_CreateThreadMgr(&ptim)))
  325. return hr;
  326. QuickVariantInit(pvar);
  327. if (ptim)
  328. {
  329. ITfCompartment *pComp;
  330. if (SUCCEEDED(GetCompartment((IUnknown *)ptim, rguid, &pComp)))
  331. {
  332. hr = pComp->GetValue(pvar);
  333. pComp->Release();
  334. }
  335. ptim->Release();
  336. }
  337. return hr;
  338. }
  339. //+---------------------------------------------------------------------------
  340. //
  341. // OnDefWindowProc
  342. //
  343. //----------------------------------------------------------------------------
  344. STDAPI CComActiveIMMApp::OnDefWindowProc(
  345. HWND hWnd,
  346. UINT Msg,
  347. WPARAM wParam,
  348. LPARAM lParam,
  349. LRESULT *plResult
  350. )
  351. {
  352. *plResult = 0;
  353. BOOL fUnicode = IsWindowUnicode(hWnd);
  354. HRESULT hr = S_FALSE; // returns S_FALSE, DefWindowProc should be called.
  355. //
  356. // RE4.0 won't call DefWindowProc even returns S_FALSE.
  357. // This code recover some IME message as same old AIMM code (dimm\aime_wnd.cpp)
  358. //
  359. switch (Msg)
  360. {
  361. case WM_IME_KEYDOWN:
  362. case WM_IME_KEYUP:
  363. case WM_IME_CHAR:
  364. case WM_IME_COMPOSITION:
  365. case WM_IME_STARTCOMPOSITION:
  366. case WM_IME_ENDCOMPOSITION:
  367. case WM_IME_NOTIFY:
  368. case WM_IME_SETCONTEXT:
  369. if (fUnicode)
  370. {
  371. *plResult = ::DefWindowProcW(hWnd, Msg, wParam, lParam);
  372. }
  373. else
  374. {
  375. *plResult = ::DefWindowProcA(hWnd, Msg, wParam, lParam);
  376. }
  377. hr = S_OK;
  378. break;
  379. case WM_IME_REQUEST:
  380. switch (wParam)
  381. {
  382. case IMR_QUERYCHARPOSITION:
  383. if (fUnicode)
  384. {
  385. *plResult = ::DefWindowProcW(hWnd, Msg, wParam, lParam);
  386. }
  387. else
  388. {
  389. *plResult = ::DefWindowProcA(hWnd, Msg, wParam, lParam);
  390. }
  391. hr = S_OK;
  392. break;
  393. }
  394. break;
  395. }
  396. return hr;
  397. }