forked from EasyIME/libIME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextService.h
318 lines (249 loc) · 9.96 KB
/
TextService.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// Copyright (C) 2013 Hong Jen Yee (PCMan) <[email protected]>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301, USA.
//
#ifndef IME_TEXT_SERVICE_H
#define IME_TEXT_SERVICE_H
#include "libIME.h"
#include <msctf.h>
#include "EditSession.h"
#include "KeyEvent.h"
#include "ComPtr.h"
#include "DisplayAttributeInfo.h"
#include <vector>
#include <list>
#include <string>
// for Windows 8 support
#ifndef TF_TMF_IMMERSIVEMODE // this is defined in Win 8 SDK
#define TF_TMF_IMMERSIVEMODE 0x40000000
#endif
namespace Ime {
class ImeModule;
class LangBarButton;
class TextService:
// TSF interfaces
public ITfTextInputProcessorEx,
// event sinks
public ITfThreadMgrEventSink,
public ITfTextEditSink,
public ITfKeyEventSink,
public ITfCompositionSink,
public ITfCompartmentEventSink,
public ITfLangBarEventSink,
public ITfActiveLanguageProfileNotifySink {
public:
enum CommandType { // used in onCommand()
COMMAND_LEFT_CLICK,
COMMAND_RIGHT_CLICK,
COMMAND_MENU
};
TextService(ImeModule* module);
// public methods
ImeModule* imeModule() const;
ITfThreadMgr* threadMgr() const;
TfClientId clientId() const;
ITfContext* currentContext();
bool isActivated() const {
return (threadMgr() != NULL);
}
DWORD activateFlags() const {
return activateFlags_;
}
// running in Windows 8 app mode
bool isImmersive() const {
return (activateFlags_ & TF_TMF_IMMERSIVEMODE) != 0;
}
// alias of isImmersive()
bool isMetroApp() const {
return isImmersive();
}
// UI less mode is enabled (for ex: in fullscreen games)
bool isUiLess() const {
return (activateFlags_ & TF_TMF_UIELEMENTENABLEDONLY) != 0;
}
// is in console mode
bool isConsole() const {
return (activateFlags_ & TF_TMF_CONSOLE) != 0;
}
DWORD langBarStatus() const;
// language bar buttons
void addButton(LangBarButton* button);
void removeButton(LangBarButton* button);
// preserved keys
void addPreservedKey(UINT keyCode, UINT modifiers, const GUID& guid);
void removePreservedKey(const GUID& guid);
// text composition handling
bool isComposing();
// is keyboard disabled for the context (NULL means current context)
bool isKeyboardDisabled(ITfContext* context = NULL);
// is keyboard opened for the whole thread
bool isKeyboardOpened();
void setKeyboardOpen(bool open);
bool isInsertionAllowed(EditSession* session);
void startComposition(ITfContext* context);
void endComposition(ITfContext* context);
bool compositionRect(EditSession* session, RECT* rect);
bool selectionRect(EditSession* session, RECT* rect);
HWND compositionWindow(EditSession* session);
std::wstring compositionString(EditSession* session);
void setCompositionString(EditSession* session, const wchar_t* str, int len);
void setCompositionCursor(EditSession* session, int pos);
// compartment handling
ComPtr<ITfCompartment> globalCompartment(const GUID& key);
ComPtr<ITfCompartment> threadCompartment(const GUID& key);
ComPtr<ITfCompartment> contextCompartment(const GUID& key, ITfContext* context = NULL);
DWORD globalCompartmentValue(const GUID& key);
DWORD threadCompartmentValue(const GUID& key);
DWORD contextCompartmentValue(const GUID& key, ITfContext* context = NULL);
void setGlobalCompartmentValue(const GUID& key, DWORD value);
void setThreadCompartmentValue(const GUID& key, DWORD value);
void setContextCompartmentValue(const GUID& key, DWORD value, ITfContext* context = NULL);
// manage sinks to global or thread compartment (context specific compartment is not used)
void addCompartmentMonitor(const GUID key, bool isGlobal = false);
void removeCompartmentMonitor(const GUID key);
// virtual functions that IME implementors may need to override
virtual void onActivate();
virtual void onDeactivate();
virtual void onSetFocus();
virtual void onKillFocus();
virtual bool filterKeyDown(KeyEvent& keyEvent);
virtual bool onKeyDown(KeyEvent& keyEvent, EditSession* session);
virtual bool filterKeyUp(KeyEvent& keyEvent);
virtual bool onKeyUp(KeyEvent& keyEvent, EditSession* session);
virtual bool onPreservedKey(const GUID& guid);
// called when a language button or menu item is clicked
virtual bool onCommand(UINT id, CommandType type);
// called when a value in the global or thread compartment changed.
virtual void onCompartmentChanged(const GUID& key);
virtual void onLangBarStatusChanged(int newStatus);
// called when the keyboard is opened or closed
virtual void onKeyboardStatusChanged(bool opened);
// called just before current composition is terminated for doing cleanup.
// if forced is true, the composition is terminated by others, such as
// the input focus is grabbed by another application.
// if forced is false, the composition is terminated gracefully by endComposition().
virtual void onCompositionTerminated(bool forced);
// called when a language profile is activated (only useful for text services that supports multiple language profiles)
virtual void onLangProfileActivated(REFGUID guidProfile);
// called when a language profile is deactivated
virtual void onLangProfileDeactivated(REFGUID guidProfile);
// COM related stuff
public:
friend class DisplayAttributeInfoEnum;
// IUnknown
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObj);
STDMETHODIMP_(ULONG) AddRef(void);
STDMETHODIMP_(ULONG) Release(void);
// ITfTextInputProcessor
STDMETHODIMP Activate(ITfThreadMgr *pThreadMgr, TfClientId tfClientId);
STDMETHODIMP Deactivate();
// ITfTextInputProcessorEx
STDMETHODIMP ActivateEx(ITfThreadMgr *ptim, TfClientId tid, DWORD dwFlags);
// ITfThreadMgrEventSink
STDMETHODIMP OnInitDocumentMgr(ITfDocumentMgr *pDocMgr);
STDMETHODIMP OnUninitDocumentMgr(ITfDocumentMgr *pDocMgr);
STDMETHODIMP OnSetFocus(ITfDocumentMgr *pDocMgrFocus, ITfDocumentMgr *pDocMgrPrevFocus);
STDMETHODIMP OnPushContext(ITfContext *pContext);
STDMETHODIMP OnPopContext(ITfContext *pContext);
// ITfTextEditSink
STDMETHODIMP OnEndEdit(ITfContext *pContext, TfEditCookie ecReadOnly, ITfEditRecord *pEditRecord);
// ITfKeyEventSink
STDMETHODIMP OnSetFocus(BOOL fForeground);
STDMETHODIMP OnTestKeyDown(ITfContext *pContext, WPARAM wParam, LPARAM lParam, BOOL *pfEaten);
STDMETHODIMP OnKeyDown(ITfContext *pContext, WPARAM wParam, LPARAM lParam, BOOL *pfEaten);
STDMETHODIMP OnTestKeyUp(ITfContext *pContext, WPARAM wParam, LPARAM lParam, BOOL *pfEaten);
STDMETHODIMP OnKeyUp(ITfContext *pContext, WPARAM wParam, LPARAM lParam, BOOL *pfEaten);
STDMETHODIMP OnPreservedKey(ITfContext *pContext, REFGUID rguid, BOOL *pfEaten);
// ITfCompositionSink
STDMETHODIMP OnCompositionTerminated(TfEditCookie ecWrite, ITfComposition *pComposition);
// ITfCompartmentEventSink
STDMETHODIMP OnChange(REFGUID rguid);
// ITfLangBarEventSink
STDMETHODIMP OnSetFocus(DWORD dwThreadId);
STDMETHODIMP OnThreadTerminate(DWORD dwThreadId);
STDMETHODIMP OnThreadItemChange(DWORD dwThreadId);
STDMETHODIMP OnModalInput(DWORD dwThreadId, UINT uMsg, WPARAM wParam, LPARAM lParam);
STDMETHODIMP ShowFloating(DWORD dwFlags);
STDMETHODIMP GetItemFloatingRect(DWORD dwThreadId, REFGUID rguid, RECT *prc);
// ITfActiveLanguageProfileNotifySink
STDMETHODIMP OnActivated(REFCLSID clsid, REFGUID guidProfile, BOOL fActivated);
protected:
// edit session classes, used with TSF
class KeyEditSession: public EditSession {
public:
KeyEditSession(TextService* service, ITfContext* context, KeyEvent& keyEvent):
EditSession(service, context),
keyEvent_(keyEvent),
result_(false) {
}
STDMETHODIMP DoEditSession(TfEditCookie ec);
KeyEvent keyEvent_;
bool result_;
};
class StartCompositionEditSession: public EditSession {
public:
StartCompositionEditSession(TextService* service, ITfContext* context):
EditSession(service, context){
}
STDMETHODIMP DoEditSession(TfEditCookie ec);
};
class EndCompositionEditSession: public EditSession {
public:
EndCompositionEditSession(TextService* service, ITfContext* context):
EditSession(service, context){
}
STDMETHODIMP DoEditSession(TfEditCookie ec);
};
HRESULT doKeyEditSession(TfEditCookie cookie, KeyEditSession* session);
HRESULT doStartCompositionEditSession(TfEditCookie cookie, StartCompositionEditSession* session);
HRESULT doEndCompositionEditSession(TfEditCookie cookie, EndCompositionEditSession* session);
struct PreservedKey : public TF_PRESERVEDKEY {
GUID guid;
};
struct CompartmentMonitor {
GUID guid;
DWORD cookie;
bool isGlobal;
bool operator == (const GUID& other) const {
return bool(::IsEqualGUID(guid, other));
}
};
protected: // COM object should not be deleted directly. calling Release() instead.
virtual ~TextService(void);
private:
ComPtr<ImeModule> module_;
ComPtr<ITfThreadMgr> threadMgr_;
TfClientId clientId_;
DWORD activateFlags_;
bool isKeyboardOpened_;
// event sink cookies
DWORD threadMgrEventSinkCookie_;
DWORD textEditSinkCookie_;
DWORD compositionSinkCookie_;
DWORD keyboardOpenEventSinkCookie_;
DWORD globalCompartmentEventSinkCookie_;
DWORD langBarSinkCookie_;
DWORD activateLanguageProfileNotifySinkCookie_;
ITfComposition* composition_; // acquired when starting composition, released when ending composition
ComPtr<ITfLangBarMgr> langBarMgr_;
std::vector<ComPtr<LangBarButton>> langBarButtons_;
std::vector<PreservedKey> preservedKeys_;
std::vector<CompartmentMonitor> compartmentMonitors_;
long refCount_; // reference counting
};
}
#endif