Skip to content

Commit

Permalink
* Fix key code conversion stuff in KeyEvent.
Browse files Browse the repository at this point in the history
* Add a tooltip class which will later be used to show messages to the users.
  • Loading branch information
PCMan authored and PCMan committed Sep 19, 2013
1 parent 1830c6c commit ac5644f
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 6 deletions.
14 changes: 8 additions & 6 deletions ChewingTextService/ChewingTextService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,15 @@ bool TextService::onKeyDown(Ime::KeyEvent& keyEvent, Ime::EditSession* session)
if(isComposing()) {
setCompositionCursor(session, ::chewing_cursor_Current(chewingContext_));
}
/*
Ime::ComQIPtr<ITfInsertAtSelection> sel = session->context();
if(sel) {
Ime::ComPtr<ITfRange> range;
sel->InsertTextAtSelection(session->editCookie(), 0, L"TEXT", 4, &range);

// show aux info
if(::chewing_aux_Check(chewingContext_)) {
char* str = ::chewing_aux_String(chewingContext_);
wchar_t* wstr = utf8ToUtf16(str, NULL);
::chewing_free(str);
// TODO: show the message to the user
delete []wstr;
}
*/
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions libIME/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ add_library(libIME_static STATIC
${PROJECT_SOURCE_DIR}/PropertyPage.h
${PROJECT_SOURCE_DIR}/ImeWindow.cpp
${PROJECT_SOURCE_DIR}/ImeWindow.h
${PROJECT_SOURCE_DIR}/Tooltip.cpp
${PROJECT_SOURCE_DIR}/Tooltip.h
${PROJECT_SOURCE_DIR}/CandidateWindow.h
${PROJECT_SOURCE_DIR}/CandidateWindow.cpp
)
Expand Down
6 changes: 6 additions & 0 deletions libIME/KeyEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ KeyEvent::KeyEvent(UINT type, WPARAM wp, LPARAM lp):
::memset(keyStates_, 0, sizeof(keyStates_));

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

KeyEvent::KeyEvent(const KeyEvent& other):
Expand Down
112 changes: 112 additions & 0 deletions libIME/Tooltip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// 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 "Tooltip.h"
#include "DrawUtils.h"

namespace Ime {

Tooltip::Tooltip(void):
autoDestroy_(true),
timerId_(0) {
}

Tooltip::~Tooltip(void) {
if(timerId_)
KillTimer(hwnd_, timerId_);
}

LRESULT Tooltip::wndProc(UINT msg, WPARAM wp, LPARAM lp) {
switch(msg) {
case WM_PAINT: {
PAINTSTRUCT ps;
BeginPaint(hwnd_, &ps);
onPaint(ps);
EndPaint(hwnd_, &ps);
}
break;
case WM_TIMER:
hideTip();
if(autoDestroy_) {
::DestroyWindow(hwnd_);
}
break;
case WM_NCDESTROY:
if(autoDestroy_) {
delete this;
return 0;
}
case WM_MOUSEACTIVATE:
return MA_NOACTIVATE;
default:
return ImeWindow::wndProc(msg, wp, lp);
}
return 0;
}

void Tooltip::onPaint(PAINTSTRUCT& ps) {
int len = text.length();
RECT rc, textrc = {0};
GetClientRect(hwnd_, &rc);
::FillSolidRect(ps.hdc, &rc, ::GetSysColor(COLOR_INFOBK));
Draw3DBorder(ps.hdc, &rc, GetSysColor(COLOR_BTNFACE), GetSysColor(COLOR_3DDKSHADOW), 1);

SetBkMode(ps.hdc, TRANSPARENT);
SetTextColor(ps.hdc, GetSysColor(COLOR_INFOTEXT));
HGDIOBJ old_font = SelectObject(ps.hdc, GetStockObject(DEFAULT_GUI_FONT));

SIZE size;
GetTextExtentPoint32W(ps.hdc, text.c_str(), len, &size);
rc.top += (rc.bottom - size.cy)/2;
rc.left += (rc.right - size.cx)/2;
ExtTextOutW(ps.hdc, rc.left, rc.top, 0, &textrc, text.c_str(), len, NULL);

SelectObject(ps.hdc, old_font);
}

void Tooltip::showTip(int x, int y, std::wstring tip_text, int duration) {
text = tip_text;
SIZE size = {0};
HDC dc = GetDC(hwnd_);
HGDIOBJ old_font = SelectObject(dc, GetStockObject(DEFAULT_GUI_FONT));
GetTextExtentPointW(dc, text.c_str(), text.length(), &size);
SelectObject(dc, old_font);
ReleaseDC(hwnd_, dc);

SetWindowPos(hwnd_, HWND_TOPMOST, x, y, size.cx + 4, size.cy + 4, SWP_NOACTIVATE);
if(IsWindowVisible(hwnd_))
InvalidateRect(hwnd_, NULL, TRUE);
else
ShowWindow(hwnd_, SW_SHOWNA);
if(duration > 0) {
if(timerId_)
KillTimer(hwnd_, timerId_);
timerId_ = SetTimer(hwnd_, 1, duration, NULL);
}
}

void Tooltip::hideTip(void) {
if(timerId_) {
KillTimer(hwnd_, timerId_);
timerId_ = 0;
}
hide();
}

} // namespace Ime
53 changes: 53 additions & 0 deletions libIME/Tooltip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// 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.
//

// FIXME: this class is taken from old ChewingIME and the
// interface really needs some redesign. It's not ready for use yet.

#ifndef IME_TOOLTIP_H
#define IME_TOOLTIP_H

#include "imewindow.h"
#include <string>

namespace Ime {

class Tooltip : public ImeWindow {
public:
Tooltip(void);
virtual ~Tooltip(void);
void showTip(int x, int y, std::wstring tip_text, int duration = 0);
void hideTip(void);
void setAutoDestroy(bool autoDestroy = true) {
autoDestroy_ = autoDestroy;
}

protected:
LRESULT wndProc(UINT msg, WPARAM wp, LPARAM lp);
void onPaint(PAINTSTRUCT& ps);

private:
UINT timerId_;
std::wstring text;
bool autoDestroy_;
};

}

#endif

0 comments on commit ac5644f

Please sign in to comment.