Skip to content

Commit d134631

Browse files
committed
Use std::wstring to store tooltip of language buttons
1 parent 592b8d8 commit d134631

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

LangBarButton.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ LangBarButton::~LangBarButton(void) {
5050
textService_->Release();
5151
if(menu_)
5252
::DestroyMenu(menu_);
53-
if(tooltip_) // allocated by wcsdup()
54-
::free(tooltip_);
5553
}
5654

5755
const wchar_t* LangBarButton::text() const {
@@ -89,23 +87,20 @@ void LangBarButton::setText(UINT stringId) {
8987

9088
// public methods
9189
const wchar_t* LangBarButton::tooltip() const {
92-
return tooltip_;
90+
return tooltip_.c_str();
9391
}
9492

9593
void LangBarButton::setTooltip(const wchar_t* tooltip) {
96-
if(tooltip_)
97-
free(tooltip_);
98-
tooltip_ = _wcsdup(tooltip);
94+
tooltip_ = tooltip;
9995
update(TF_LBI_TOOLTIP);
10096
}
10197

10298
void LangBarButton::setTooltip(UINT tooltipId) {
10399
const wchar_t* str;
104-
int len = ::LoadStringW(textService_->imeModule()->hInstance(), tooltipId, (LPTSTR)&str, 0);
100+
// If this parameter is 0, then lpBuffer receives a read-only pointer to the resource itself.
101+
auto len = ::LoadStringW(textService_->imeModule()->hInstance(), tooltipId, (LPTSTR)&str, 0);
105102
if(str) {
106-
tooltip_ = (wchar_t*)malloc((len + 1) * sizeof(wchar_t));
107-
wcsncpy(tooltip_, str, len);
108-
tooltip_[len] = 0;
103+
tooltip_ = std::wstring(str, len);
109104
update(TF_LBI_TOOLTIP);
110105
}
111106
}
@@ -241,11 +236,8 @@ STDMETHODIMP LangBarButton::Show(BOOL fShow) {
241236
}
242237

243238
STDMETHODIMP LangBarButton::GetTooltipString(BSTR *pbstrToolTip) {
244-
if(tooltip_) {
245-
*pbstrToolTip = ::SysAllocString(tooltip_);
246-
return *pbstrToolTip ? S_OK : E_FAIL;
247-
}
248-
return E_FAIL;
239+
*pbstrToolTip = ::SysAllocString(tooltip_.c_str());
240+
return *pbstrToolTip ? S_OK : E_FAIL;
249241
}
250242

251243
// ITfLangBarItemButton
@@ -324,7 +316,7 @@ void LangBarButton::buildITfMenu(ITfMenu* menu, HMENU templ) {
324316
mi.fMask = MIIM_FTYPE|MIIM_ID|MIIM_STATE|MIIM_STRING|MIIM_SUBMENU;
325317
if(::GetMenuItemInfoW(templ, i, TRUE, &mi)) {
326318
UINT flags = 0;
327-
wchar_t* text;
319+
wchar_t* text = nullptr;
328320
ULONG textLen = 0;
329321
ITfMenu* subMenu = NULL;
330322
ITfMenu** pSubMenu = NULL;

LangBarButton.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string.h>
2525
#include <Windows.h>
2626
#include <map>
27+
#include <string>
2728

2829
namespace Ime {
2930

@@ -105,7 +106,7 @@ class LangBarButton:
105106
TextService* textService_;
106107
TF_LANGBARITEMINFO info_;
107108
UINT commandId_;
108-
wchar_t* tooltip_;
109+
std::wstring tooltip_;
109110
HICON icon_;
110111
HMENU menu_;
111112
std::map<DWORD, ITfLangBarItemSink*> sinks_;

0 commit comments

Comments
 (0)