forked from EasyIME/libIME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LangBarButton.cpp
367 lines (316 loc) · 9.44 KB
/
LangBarButton.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//
// 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.
//
#include "LangBarButton.h"
#include "TextService.h"
#include "ImeModule.h"
#include <OleCtl.h>
#include <assert.h>
#include <stdlib.h>
namespace Ime {
LangBarButton::LangBarButton(TextService* service, const GUID& guid, UINT commandId, const wchar_t* text, DWORD style):
textService_(service),
tooltip_(),
commandId_(commandId),
menu_(NULL),
icon_(NULL),
status_(0),
refCount_(1) {
assert(service && service->imeModule());
textService_->AddRef();
info_.clsidService = service->imeModule()->textServiceClsid();
info_.guidItem = guid;
info_.dwStyle = style;
info_.ulSort = 0;
setText(text);
}
LangBarButton::~LangBarButton(void) {
if(textService_)
textService_->Release();
if(menu_)
::DestroyMenu(menu_);
}
const wchar_t* LangBarButton::text() const {
return info_.szDescription;
}
void LangBarButton::setText(const wchar_t* text) {
if (text && text[0] != '\0') {
wcsncpy(info_.szDescription, text, TF_LBI_DESC_MAXLEN - 1);
info_.szDescription[TF_LBI_DESC_MAXLEN - 1] = 0;
}
else {
// NOTE: The language button text should NOT be empty.
// Otherwise, when the button status or icon is changed after its creation,
// the button will disappear temporarily in Windows 10 for unknown reason.
// This can be considered a bug of Windows 10 and there does not seem to be a way to fix it.
// So we need to avoid empty button text otherwise the language button won't work properly.
// Here we use a space character to make the text non-empty to workaround the problem.
wcscpy(info_.szDescription, L" ");
}
update(TF_LBI_TEXT);
}
void LangBarButton::setText(UINT stringId) {
const wchar_t* str;
int len = ::LoadStringW(textService_->imeModule()->hInstance(), stringId, (LPTSTR)&str, 0);
if(str) {
if(len > (TF_LBI_DESC_MAXLEN - 1))
len = TF_LBI_DESC_MAXLEN - 1;
wcsncpy(info_.szDescription, str, len);
info_.szDescription[len] = 0;
update(TF_LBI_TEXT);
}
}
// public methods
const wchar_t* LangBarButton::tooltip() const {
return tooltip_.c_str();
}
void LangBarButton::setTooltip(const wchar_t* tooltip) {
tooltip_ = tooltip;
update(TF_LBI_TOOLTIP);
}
void LangBarButton::setTooltip(UINT tooltipId) {
const wchar_t* str;
// If this parameter is 0, then lpBuffer receives a read-only pointer to the resource itself.
auto len = ::LoadStringW(textService_->imeModule()->hInstance(), tooltipId, (LPTSTR)&str, 0);
if(str) {
tooltip_ = std::wstring(str, len);
update(TF_LBI_TOOLTIP);
}
}
HICON LangBarButton::icon() const {
return icon_;
}
// The language button does not take owner ship of the icon
// That means, when the button is destroyed, it will not destroy
// the icon automatically.
void LangBarButton::setIcon(HICON icon) {
icon_ = icon;
update(TF_LBI_ICON);
}
void LangBarButton::setIcon(UINT iconId) {
HICON icon = ::LoadIconW(textService_->imeModule()->hInstance(), (LPCTSTR)iconId);
if(icon)
setIcon(icon);
}
UINT LangBarButton::commandId() const {
return commandId_;
}
void LangBarButton::setCommandId(UINT id) {
commandId_ = id;
}
HMENU LangBarButton::menu() const {
return menu_;
}
void LangBarButton::setMenu(HMENU menu) {
if(menu_) {
::DestroyMenu(menu_);
}
menu_ = menu;
// FIXME: how to handle toggle buttons?
if(menu)
info_.dwStyle = TF_LBI_STYLE_BTN_MENU;
else
info_.dwStyle = TF_LBI_STYLE_BTN_BUTTON;
}
bool LangBarButton::enabled() const {
return !(status_ & TF_LBI_STATUS_DISABLED);
}
void LangBarButton::setEnabled(bool enable) {
if(enabled() != enable) {
if(enable)
status_ &= ~TF_LBI_STATUS_DISABLED;
else
status_ |= TF_LBI_STATUS_DISABLED;
update(TF_LBI_STATUS);
}
}
// need to create the button with TF_LBI_STYLE_BTN_TOGGLE style
bool LangBarButton::toggled() const {
return (status_ & TF_LBI_STATUS_BTN_TOGGLED) ? true : false;
}
void LangBarButton::setToggled(bool toggle) {
if(toggled() != toggle) {
if(toggle)
status_ |= TF_LBI_STATUS_BTN_TOGGLED;
else
status_ &= ~TF_LBI_STATUS_BTN_TOGGLED;
update(TF_LBI_STATUS);
}
}
DWORD LangBarButton::style() const {
return info_.dwStyle;
}
void LangBarButton::setStyle(DWORD style) {
info_.dwStyle = style;
}
// COM stuff
// IUnknown
STDMETHODIMP LangBarButton::QueryInterface(REFIID riid, void **ppvObj) {
if (ppvObj == NULL)
return E_INVALIDARG;
if(IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_ITfLangBarItem) || IsEqualIID(riid, IID_ITfLangBarItemButton))
*ppvObj = (ITfLangBarItemButton*)this;
else if(IsEqualIID(riid, IID_ITfSource))
*ppvObj = (ITfSource*)this;
else
*ppvObj = NULL;
if(*ppvObj) {
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
// IUnknown implementation
STDMETHODIMP_(ULONG) LangBarButton::AddRef(void) {
return ++refCount_;
}
STDMETHODIMP_(ULONG) LangBarButton::Release(void) {
assert(refCount_ > 0);
const ULONG newCount = --refCount_;
if (0 == refCount_)
delete this;
return newCount;
}
// ITfLangBarItem
STDMETHODIMP LangBarButton::GetInfo(TF_LANGBARITEMINFO *pInfo) {
*pInfo = info_;
return S_OK;
}
STDMETHODIMP LangBarButton::GetStatus(DWORD *pdwStatus) {
*pdwStatus = status_;
return S_OK;
}
STDMETHODIMP LangBarButton::Show(BOOL fShow) {
return E_NOTIMPL;
}
STDMETHODIMP LangBarButton::GetTooltipString(BSTR *pbstrToolTip) {
*pbstrToolTip = ::SysAllocString(tooltip_.c_str());
return *pbstrToolTip ? S_OK : E_FAIL;
}
// ITfLangBarItemButton
STDMETHODIMP LangBarButton::OnClick(TfLBIClick click, POINT pt, const RECT *prcArea) {
TextService::CommandType type;
if(click == TF_LBI_CLK_RIGHT)
type = TextService::COMMAND_RIGHT_CLICK;
else
type = TextService::COMMAND_LEFT_CLICK;
textService_->onCommand(commandId_, type);
return S_OK;
}
STDMETHODIMP LangBarButton::InitMenu(ITfMenu *pMenu) {
if(!menu_)
return E_FAIL;
buildITfMenu(pMenu, menu_);
return S_OK;
}
STDMETHODIMP LangBarButton::OnMenuSelect(UINT wID) {
textService_->onCommand(wID, TextService::COMMAND_MENU);
return S_OK;
}
STDMETHODIMP LangBarButton::GetIcon(HICON *phIcon) {
// https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms628718%28v=vs.85%29.aspx
// The caller will delete the icon when it's no longer needed.
// However, we might still need it. So let's return a copy here.
*phIcon = (HICON)CopyImage(icon_, IMAGE_ICON, 0, 0, 0);
return S_OK;
}
STDMETHODIMP LangBarButton::GetText(BSTR *pbstrText) {
*pbstrText = ::SysAllocString(info_.szDescription);
return *pbstrText ? S_OK : S_FALSE;
}
// ITfSource
STDMETHODIMP LangBarButton::AdviseSink(REFIID riid, IUnknown *punk, DWORD *pdwCookie) {
if(IsEqualIID(riid, IID_ITfLangBarItemSink)) {
ITfLangBarItemSink* langBarItemSink;
if(punk->QueryInterface(IID_ITfLangBarItemSink, (void **)&langBarItemSink) == S_OK) {
*pdwCookie = (DWORD)rand();
sinks_[*pdwCookie] = langBarItemSink;
return S_OK;
}
else
return E_NOINTERFACE;
}
return CONNECT_E_CANNOTCONNECT;
}
STDMETHODIMP LangBarButton::UnadviseSink(DWORD dwCookie) {
std::map<DWORD, ITfLangBarItemSink*>::iterator it = sinks_.find(dwCookie);
if(it != sinks_.end()) {
ITfLangBarItemSink* langBarItemSink = (ITfLangBarItemSink*)it->second;
langBarItemSink->Release();
sinks_.erase(it);
return S_OK;
}
return CONNECT_E_NOCONNECTION;
}
// build ITfMenu according to the content of HMENU
void LangBarButton::buildITfMenu(ITfMenu* menu, HMENU templ) {
int n = ::GetMenuItemCount(templ);
for(int i = 0; i < n; ++i) {
MENUITEMINFO mi;
wchar_t textBuffer[256];
memset(&mi, 0, sizeof(mi));
mi.cbSize = sizeof(mi);
mi.dwTypeData = (LPTSTR)textBuffer;
mi.cch = 255;
mi.fMask = MIIM_FTYPE|MIIM_ID|MIIM_STATE|MIIM_STRING|MIIM_SUBMENU;
if(::GetMenuItemInfoW(templ, i, TRUE, &mi)) {
UINT flags = 0;
wchar_t* text = nullptr;
ULONG textLen = 0;
ITfMenu* subMenu = NULL;
ITfMenu** pSubMenu = NULL;
if(mi.hSubMenu) { // has submenu
pSubMenu = &subMenu;
flags |= TF_LBMENUF_SUBMENU;
}
if(mi.fType == MFT_STRING) { // text item
text = (wchar_t*)mi.dwTypeData;
textLen = mi.cch;
}
else if(mi.fType == MFT_SEPARATOR) { // separator item
flags |= TF_LBMENUF_SEPARATOR;
}
else // other types are not supported
continue;
if(mi.fState & MFS_CHECKED) // checked
flags |= TF_LBMENUF_CHECKED;
if(mi.fState & (MFS_GRAYED|MFS_DISABLED)) // disabled
flags |= TF_LBMENUF_GRAYED;
if(menu->AddMenuItem(mi.wID, flags, NULL, 0, text, textLen, pSubMenu) == S_OK) {
if(subMenu) {
buildITfMenu(subMenu, mi.hSubMenu);
subMenu->Release();
}
}
}
else {
DWORD error = ::GetLastError();
}
}
}
// call all sinks to generate update notifications
void LangBarButton::update(DWORD flags) {
if(!sinks_.empty()) {
std::map<DWORD, ITfLangBarItemSink*>::iterator it;
for(it = sinks_.begin(); it != sinks_.end(); ++it) {
ITfLangBarItemSink* sink = it->second;
sink->OnUpdate(flags);
}
}
}
} // namespace Ime