xref: /reactos/sdk/lib/cicero/cicimc.h (revision f53f1334)
1fc3eeb61SKatayama Hirofumi MZ /*
2fc3eeb61SKatayama Hirofumi MZ  * PROJECT:     ReactOS Cicero
3fc3eeb61SKatayama Hirofumi MZ  * LICENSE:     LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4fc3eeb61SKatayama Hirofumi MZ  * PURPOSE:     Locking and Unlocking IMC and IMCC handles
5fc3eeb61SKatayama Hirofumi MZ  * COPYRIGHT:   Copyright 2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6fc3eeb61SKatayama Hirofumi MZ  */
7fc3eeb61SKatayama Hirofumi MZ 
8fc3eeb61SKatayama Hirofumi MZ #pragma once
9fc3eeb61SKatayama Hirofumi MZ 
10fc3eeb61SKatayama Hirofumi MZ // struct CTFIMECONTEXT;
11fc3eeb61SKatayama Hirofumi MZ // class CIC_IMCC_LOCK<T_DATA>;
12fc3eeb61SKatayama Hirofumi MZ // class CicIMCCLock<T_DATA>;
13fc3eeb61SKatayama Hirofumi MZ // class CIC_IMC_LOCK;
14fc3eeb61SKatayama Hirofumi MZ // class CicIMCLock;
15fc3eeb61SKatayama Hirofumi MZ 
16fc3eeb61SKatayama Hirofumi MZ class CicInputContext;
17fc3eeb61SKatayama Hirofumi MZ 
18fc3eeb61SKatayama Hirofumi MZ typedef struct tagCTFIMECONTEXT
19fc3eeb61SKatayama Hirofumi MZ {
20fc3eeb61SKatayama Hirofumi MZ     CicInputContext *m_pCicIC;
21fc3eeb61SKatayama Hirofumi MZ     DWORD m_dwCicFlags;
22fc3eeb61SKatayama Hirofumi MZ } CTFIMECONTEXT, *PCTFIMECONTEXT;
23fc3eeb61SKatayama Hirofumi MZ 
24fc3eeb61SKatayama Hirofumi MZ template <typename T_DATA>
25fc3eeb61SKatayama Hirofumi MZ class CIC_IMCC_LOCK
26fc3eeb61SKatayama Hirofumi MZ {
27fc3eeb61SKatayama Hirofumi MZ protected:
28fc3eeb61SKatayama Hirofumi MZ     T_DATA *m_pIMCC;
29fc3eeb61SKatayama Hirofumi MZ 
30fc3eeb61SKatayama Hirofumi MZ public:
31fc3eeb61SKatayama Hirofumi MZ     HIMCC m_hIMCC;
32fc3eeb61SKatayama Hirofumi MZ     HRESULT m_hr;
33fc3eeb61SKatayama Hirofumi MZ 
CIC_IMCC_LOCK(HIMCC hIMCC)34fc3eeb61SKatayama Hirofumi MZ     CIC_IMCC_LOCK(HIMCC hIMCC)
35fc3eeb61SKatayama Hirofumi MZ     {
36fc3eeb61SKatayama Hirofumi MZ         m_pIMCC = NULL;
37fc3eeb61SKatayama Hirofumi MZ         m_hr = S_OK;
38fc3eeb61SKatayama Hirofumi MZ         m_hIMCC = hIMCC;
39fc3eeb61SKatayama Hirofumi MZ     }
40fc3eeb61SKatayama Hirofumi MZ };
41fc3eeb61SKatayama Hirofumi MZ 
42fc3eeb61SKatayama Hirofumi MZ template <typename T_DATA>
43fc3eeb61SKatayama Hirofumi MZ class CicIMCCLock : public CIC_IMCC_LOCK<T_DATA>
44fc3eeb61SKatayama Hirofumi MZ {
45fc3eeb61SKatayama Hirofumi MZ public:
CicIMCCLock(HIMCC hIMCC)46fc3eeb61SKatayama Hirofumi MZ     CicIMCCLock(HIMCC hIMCC) : CIC_IMCC_LOCK<T_DATA>(hIMCC)
47fc3eeb61SKatayama Hirofumi MZ     {
48*f53f1334SKatayama Hirofumi MZ         this->m_hr = _LockIMCC(this->m_hIMCC, &this->m_pIMCC);
49fc3eeb61SKatayama Hirofumi MZ     }
~CicIMCCLock()50fc3eeb61SKatayama Hirofumi MZ     ~CicIMCCLock()
51fc3eeb61SKatayama Hirofumi MZ     {
52fc3eeb61SKatayama Hirofumi MZ         unlock();
53fc3eeb61SKatayama Hirofumi MZ     }
54fc3eeb61SKatayama Hirofumi MZ 
unlock()55fc3eeb61SKatayama Hirofumi MZ     void unlock()
56fc3eeb61SKatayama Hirofumi MZ     {
57fc3eeb61SKatayama Hirofumi MZ         if (this->m_pIMCC)
58fc3eeb61SKatayama Hirofumi MZ         {
59fc3eeb61SKatayama Hirofumi MZ             _UnlockIMCC(this->m_hIMCC);
60fc3eeb61SKatayama Hirofumi MZ             this->m_pIMCC = NULL;
61fc3eeb61SKatayama Hirofumi MZ         }
62fc3eeb61SKatayama Hirofumi MZ     }
63fc3eeb61SKatayama Hirofumi MZ 
64fc3eeb61SKatayama Hirofumi MZ     operator T_DATA*() const
65fc3eeb61SKatayama Hirofumi MZ     {
66fc3eeb61SKatayama Hirofumi MZ         return this->m_pIMCC;
67fc3eeb61SKatayama Hirofumi MZ     }
get()68fc3eeb61SKatayama Hirofumi MZ     T_DATA& get() const
69fc3eeb61SKatayama Hirofumi MZ     {
70fc3eeb61SKatayama Hirofumi MZ         return *this->m_pIMCC;
71fc3eeb61SKatayama Hirofumi MZ     }
72fc3eeb61SKatayama Hirofumi MZ 
73fc3eeb61SKatayama Hirofumi MZ protected:
_LockIMCC(HIMCC hIMCC,T_DATA ** pptr)74fc3eeb61SKatayama Hirofumi MZ     HRESULT _LockIMCC(HIMCC hIMCC, T_DATA **pptr)
75fc3eeb61SKatayama Hirofumi MZ     {
76fc3eeb61SKatayama Hirofumi MZ         if (!hIMCC)
77fc3eeb61SKatayama Hirofumi MZ             return E_INVALIDARG;
78fc3eeb61SKatayama Hirofumi MZ         *pptr = (T_DATA*)::ImmLockIMCC(hIMCC);
79fc3eeb61SKatayama Hirofumi MZ         return (*pptr ? S_OK : E_FAIL);
80fc3eeb61SKatayama Hirofumi MZ     }
_UnlockIMCC(HIMCC hIMCC)81fc3eeb61SKatayama Hirofumi MZ     HRESULT _UnlockIMCC(HIMCC hIMCC)
82fc3eeb61SKatayama Hirofumi MZ     {
83fc3eeb61SKatayama Hirofumi MZ         if (!::ImmUnlockIMCC(hIMCC))
84fc3eeb61SKatayama Hirofumi MZ             return (::GetLastError() ? E_FAIL : S_OK);
85fc3eeb61SKatayama Hirofumi MZ         return S_OK;
86fc3eeb61SKatayama Hirofumi MZ     }
87fc3eeb61SKatayama Hirofumi MZ };
88fc3eeb61SKatayama Hirofumi MZ 
89fc3eeb61SKatayama Hirofumi MZ class CIC_IMC_LOCK
90fc3eeb61SKatayama Hirofumi MZ {
91fc3eeb61SKatayama Hirofumi MZ protected:
92fc3eeb61SKatayama Hirofumi MZ     LPINPUTCONTEXTDX m_pIC;
93fc3eeb61SKatayama Hirofumi MZ 
94fc3eeb61SKatayama Hirofumi MZ public:
95fc3eeb61SKatayama Hirofumi MZ     HIMC m_hIMC;
96fc3eeb61SKatayama Hirofumi MZ     HRESULT m_hr;
97fc3eeb61SKatayama Hirofumi MZ     DWORD m_dw3;
98fc3eeb61SKatayama Hirofumi MZ 
CIC_IMC_LOCK(HIMC hIMC)99fc3eeb61SKatayama Hirofumi MZ     CIC_IMC_LOCK(HIMC hIMC)
100fc3eeb61SKatayama Hirofumi MZ     {
101fc3eeb61SKatayama Hirofumi MZ         m_pIC = NULL;
102fc3eeb61SKatayama Hirofumi MZ         m_hIMC = hIMC;
103fc3eeb61SKatayama Hirofumi MZ         m_hr = S_OK;
104fc3eeb61SKatayama Hirofumi MZ         m_dw3 = 0;
105fc3eeb61SKatayama Hirofumi MZ     }
106fc3eeb61SKatayama Hirofumi MZ 
Invalid()107fc3eeb61SKatayama Hirofumi MZ     BOOL Invalid() const
108fc3eeb61SKatayama Hirofumi MZ     {
109fc3eeb61SKatayama Hirofumi MZ         return (!m_pIC || m_hr != S_OK);
110fc3eeb61SKatayama Hirofumi MZ     }
111fc3eeb61SKatayama Hirofumi MZ };
112fc3eeb61SKatayama Hirofumi MZ 
113fc3eeb61SKatayama Hirofumi MZ class CicIMCLock : public CIC_IMC_LOCK
114fc3eeb61SKatayama Hirofumi MZ {
115fc3eeb61SKatayama Hirofumi MZ public:
CicIMCLock(HIMC hIMC)116fc3eeb61SKatayama Hirofumi MZ     CicIMCLock(HIMC hIMC) : CIC_IMC_LOCK(hIMC)
117fc3eeb61SKatayama Hirofumi MZ     {
118fc3eeb61SKatayama Hirofumi MZ         m_hr = _LockIMC(hIMC, &m_pIC);
119fc3eeb61SKatayama Hirofumi MZ     }
~CicIMCLock()120fc3eeb61SKatayama Hirofumi MZ     ~CicIMCLock()
121fc3eeb61SKatayama Hirofumi MZ     {
122fc3eeb61SKatayama Hirofumi MZ         unlock();
123fc3eeb61SKatayama Hirofumi MZ     }
124fc3eeb61SKatayama Hirofumi MZ 
unlock()125fc3eeb61SKatayama Hirofumi MZ     void unlock()
126fc3eeb61SKatayama Hirofumi MZ     {
127fc3eeb61SKatayama Hirofumi MZ         if (m_pIC)
128fc3eeb61SKatayama Hirofumi MZ         {
129fc3eeb61SKatayama Hirofumi MZ             _UnlockIMC(m_hIMC);
130fc3eeb61SKatayama Hirofumi MZ             m_pIC = NULL;
131fc3eeb61SKatayama Hirofumi MZ         }
132fc3eeb61SKatayama Hirofumi MZ     }
133fc3eeb61SKatayama Hirofumi MZ 
InitContext()134fc3eeb61SKatayama Hirofumi MZ     void InitContext()
135fc3eeb61SKatayama Hirofumi MZ     {
136fc3eeb61SKatayama Hirofumi MZ         if (!(m_pIC->fdwInit & INIT_COMPFORM))
137fc3eeb61SKatayama Hirofumi MZ             m_pIC->cfCompForm.dwStyle = 0;
138fc3eeb61SKatayama Hirofumi MZ         for (UINT i = 0; i < 4; ++i)
139fc3eeb61SKatayama Hirofumi MZ             m_pIC->cfCandForm[i].dwStyle = 0;
140fc3eeb61SKatayama Hirofumi MZ     }
141fc3eeb61SKatayama Hirofumi MZ 
ValidCompositionString()142fc3eeb61SKatayama Hirofumi MZ     BOOL ValidCompositionString()
143fc3eeb61SKatayama Hirofumi MZ     {
144fc3eeb61SKatayama Hirofumi MZ         if (ImmGetIMCCSize(m_pIC->hCompStr) < sizeof(COMPOSITIONSTRING))
145fc3eeb61SKatayama Hirofumi MZ             return FALSE;
146fc3eeb61SKatayama Hirofumi MZ 
147fc3eeb61SKatayama Hirofumi MZ         CicIMCCLock<COMPOSITIONSTRING> imccLock(m_pIC->hCompStr);
148fc3eeb61SKatayama Hirofumi MZ         if (!imccLock)
149fc3eeb61SKatayama Hirofumi MZ             return FALSE;
150fc3eeb61SKatayama Hirofumi MZ 
151fc3eeb61SKatayama Hirofumi MZ         return imccLock.get().dwCompStrLen > 0;
152fc3eeb61SKatayama Hirofumi MZ     }
153fc3eeb61SKatayama Hirofumi MZ 
154fc3eeb61SKatayama Hirofumi MZ     BOOL ClearCand();
155fc3eeb61SKatayama Hirofumi MZ 
UseVerticalCompWindow()156fc3eeb61SKatayama Hirofumi MZ     BOOL UseVerticalCompWindow() const
157fc3eeb61SKatayama Hirofumi MZ     {
158fc3eeb61SKatayama Hirofumi MZ         return m_pIC->cfCompForm.dwStyle && ((m_pIC->lfFont.A.lfEscapement / 900) % 4 == 3);
159fc3eeb61SKatayama Hirofumi MZ     }
160fc3eeb61SKatayama Hirofumi MZ 
161fc3eeb61SKatayama Hirofumi MZ     operator INPUTCONTEXTDX*() const
162fc3eeb61SKatayama Hirofumi MZ     {
163fc3eeb61SKatayama Hirofumi MZ         return m_pIC;
164fc3eeb61SKatayama Hirofumi MZ     }
get()165fc3eeb61SKatayama Hirofumi MZ     INPUTCONTEXTDX& get() const
166fc3eeb61SKatayama Hirofumi MZ     {
167fc3eeb61SKatayama Hirofumi MZ         return *m_pIC;
168fc3eeb61SKatayama Hirofumi MZ     }
169fc3eeb61SKatayama Hirofumi MZ 
170fc3eeb61SKatayama Hirofumi MZ protected:
_LockIMC(HIMC hIMC,LPINPUTCONTEXTDX * ppIC)171fc3eeb61SKatayama Hirofumi MZ     HRESULT _LockIMC(HIMC hIMC, LPINPUTCONTEXTDX *ppIC)
172fc3eeb61SKatayama Hirofumi MZ     {
173fc3eeb61SKatayama Hirofumi MZ         if (!hIMC)
174fc3eeb61SKatayama Hirofumi MZ             return E_INVALIDARG;
175fc3eeb61SKatayama Hirofumi MZ 
176fc3eeb61SKatayama Hirofumi MZ         LPINPUTCONTEXTDX pIC = (LPINPUTCONTEXTDX)ImmLockIMC(hIMC);
177fc3eeb61SKatayama Hirofumi MZ         *ppIC = pIC;
178fc3eeb61SKatayama Hirofumi MZ         return (pIC ? S_OK : E_FAIL);
179fc3eeb61SKatayama Hirofumi MZ     }
_UnlockIMC(HIMC hIMC)180fc3eeb61SKatayama Hirofumi MZ     HRESULT _UnlockIMC(HIMC hIMC)
181fc3eeb61SKatayama Hirofumi MZ     {
182fc3eeb61SKatayama Hirofumi MZ         return ::ImmUnlockIMC(hIMC) ? S_OK : E_FAIL;
183fc3eeb61SKatayama Hirofumi MZ     }
184fc3eeb61SKatayama Hirofumi MZ };
185