Skip to content

Commit 1d11cec

Browse files
PCManPCMan
authored andcommitted
* Fix key code conversion stuff in KeyEvent.
* Add a tooltip class which will later be used to show messages to the users.
1 parent caca046 commit 1d11cec

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ add_library(libIME_static STATIC
4444
${PROJECT_SOURCE_DIR}/PropertyPage.h
4545
${PROJECT_SOURCE_DIR}/ImeWindow.cpp
4646
${PROJECT_SOURCE_DIR}/ImeWindow.h
47+
${PROJECT_SOURCE_DIR}/Tooltip.cpp
48+
${PROJECT_SOURCE_DIR}/Tooltip.h
4749
${PROJECT_SOURCE_DIR}/CandidateWindow.h
4850
${PROJECT_SOURCE_DIR}/CandidateWindow.cpp
4951
)

KeyEvent.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ KeyEvent::KeyEvent(UINT type, WPARAM wp, LPARAM lp):
3030
::memset(keyStates_, 0, sizeof(keyStates_));
3131

3232
// try to convert the key event to an ASCII character
33+
// ToAscii API tries to convert Ctrl + printable characters to
34+
// ASCII 0x00 - 0x31 non-printable escape characters, which we don't want
35+
// So here is a hack: pretend that Ctrl key is not pressed
3336
WORD result[2] = {0, 0};
37+
BYTE ctrlState = keyStates_[VK_CONTROL];
38+
keyStates_[VK_CONTROL] = 0;
3439
if(::ToAscii(keyCode_, scanCode(), keyStates_, result, 0) == 1)
3540
charCode_ = (UINT)result[0];
3641
else
3742
charCode_ = 0;
43+
keyStates_[VK_CONTROL] = ctrlState;
3844
}
3945

4046
KeyEvent::KeyEvent(const KeyEvent& other):

Tooltip.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//
2+
// Copyright (C) 2013 Hong Jen Yee (PCMan) <[email protected]>
3+
//
4+
// This library is free software; you can redistribute it and/or
5+
// modify it under the terms of the GNU Library General Public
6+
// License as published by the Free Software Foundation; either
7+
// version 2 of the License, or (at your option) any later version.
8+
//
9+
// This library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
// Library General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Library General Public
15+
// License along with this library; if not, write to the
16+
// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17+
// Boston, MA 02110-1301, USA.
18+
//
19+
20+
#include "Tooltip.h"
21+
#include "DrawUtils.h"
22+
23+
namespace Ime {
24+
25+
Tooltip::Tooltip(void):
26+
autoDestroy_(true),
27+
timerId_(0) {
28+
}
29+
30+
Tooltip::~Tooltip(void) {
31+
if(timerId_)
32+
KillTimer(hwnd_, timerId_);
33+
}
34+
35+
LRESULT Tooltip::wndProc(UINT msg, WPARAM wp, LPARAM lp) {
36+
switch(msg) {
37+
case WM_PAINT: {
38+
PAINTSTRUCT ps;
39+
BeginPaint(hwnd_, &ps);
40+
onPaint(ps);
41+
EndPaint(hwnd_, &ps);
42+
}
43+
break;
44+
case WM_TIMER:
45+
hideTip();
46+
if(autoDestroy_) {
47+
::DestroyWindow(hwnd_);
48+
}
49+
break;
50+
case WM_NCDESTROY:
51+
if(autoDestroy_) {
52+
delete this;
53+
return 0;
54+
}
55+
case WM_MOUSEACTIVATE:
56+
return MA_NOACTIVATE;
57+
default:
58+
return ImeWindow::wndProc(msg, wp, lp);
59+
}
60+
return 0;
61+
}
62+
63+
void Tooltip::onPaint(PAINTSTRUCT& ps) {
64+
int len = text.length();
65+
RECT rc, textrc = {0};
66+
GetClientRect(hwnd_, &rc);
67+
::FillSolidRect(ps.hdc, &rc, ::GetSysColor(COLOR_INFOBK));
68+
Draw3DBorder(ps.hdc, &rc, GetSysColor(COLOR_BTNFACE), GetSysColor(COLOR_3DDKSHADOW), 1);
69+
70+
SetBkMode(ps.hdc, TRANSPARENT);
71+
SetTextColor(ps.hdc, GetSysColor(COLOR_INFOTEXT));
72+
HGDIOBJ old_font = SelectObject(ps.hdc, GetStockObject(DEFAULT_GUI_FONT));
73+
74+
SIZE size;
75+
GetTextExtentPoint32W(ps.hdc, text.c_str(), len, &size);
76+
rc.top += (rc.bottom - size.cy)/2;
77+
rc.left += (rc.right - size.cx)/2;
78+
ExtTextOutW(ps.hdc, rc.left, rc.top, 0, &textrc, text.c_str(), len, NULL);
79+
80+
SelectObject(ps.hdc, old_font);
81+
}
82+
83+
void Tooltip::showTip(int x, int y, std::wstring tip_text, int duration) {
84+
text = tip_text;
85+
SIZE size = {0};
86+
HDC dc = GetDC(hwnd_);
87+
HGDIOBJ old_font = SelectObject(dc, GetStockObject(DEFAULT_GUI_FONT));
88+
GetTextExtentPointW(dc, text.c_str(), text.length(), &size);
89+
SelectObject(dc, old_font);
90+
ReleaseDC(hwnd_, dc);
91+
92+
SetWindowPos(hwnd_, HWND_TOPMOST, x, y, size.cx + 4, size.cy + 4, SWP_NOACTIVATE);
93+
if(IsWindowVisible(hwnd_))
94+
InvalidateRect(hwnd_, NULL, TRUE);
95+
else
96+
ShowWindow(hwnd_, SW_SHOWNA);
97+
if(duration > 0) {
98+
if(timerId_)
99+
KillTimer(hwnd_, timerId_);
100+
timerId_ = SetTimer(hwnd_, 1, duration, NULL);
101+
}
102+
}
103+
104+
void Tooltip::hideTip(void) {
105+
if(timerId_) {
106+
KillTimer(hwnd_, timerId_);
107+
timerId_ = 0;
108+
}
109+
hide();
110+
}
111+
112+
} // namespace Ime

Tooltip.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Copyright (C) 2013 Hong Jen Yee (PCMan) <[email protected]>
3+
//
4+
// This library is free software; you can redistribute it and/or
5+
// modify it under the terms of the GNU Library General Public
6+
// License as published by the Free Software Foundation; either
7+
// version 2 of the License, or (at your option) any later version.
8+
//
9+
// This library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
// Library General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Library General Public
15+
// License along with this library; if not, write to the
16+
// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17+
// Boston, MA 02110-1301, USA.
18+
//
19+
20+
// FIXME: this class is taken from old ChewingIME and the
21+
// interface really needs some redesign. It's not ready for use yet.
22+
23+
#ifndef IME_TOOLTIP_H
24+
#define IME_TOOLTIP_H
25+
26+
#include "imewindow.h"
27+
#include <string>
28+
29+
namespace Ime {
30+
31+
class Tooltip : public ImeWindow {
32+
public:
33+
Tooltip(void);
34+
virtual ~Tooltip(void);
35+
void showTip(int x, int y, std::wstring tip_text, int duration = 0);
36+
void hideTip(void);
37+
void setAutoDestroy(bool autoDestroy = true) {
38+
autoDestroy_ = autoDestroy;
39+
}
40+
41+
protected:
42+
LRESULT wndProc(UINT msg, WPARAM wp, LPARAM lp);
43+
void onPaint(PAINTSTRUCT& ps);
44+
45+
private:
46+
UINT timerId_;
47+
std::wstring text;
48+
bool autoDestroy_;
49+
};
50+
51+
}
52+
53+
#endif

0 commit comments

Comments
 (0)