diff --git a/ChewingTextService/ChewingTextService.cpp b/ChewingTextService/ChewingTextService.cpp index 29005fc82..6a45a1bc2 100644 --- a/ChewingTextService/ChewingTextService.cpp +++ b/ChewingTextService/ChewingTextService.cpp @@ -351,13 +351,15 @@ bool TextService::onKeyDown(Ime::KeyEvent& keyEvent, Ime::EditSession* session) if(isComposing()) { setCompositionCursor(session, ::chewing_cursor_Current(chewingContext_)); } -/* - Ime::ComQIPtr sel = session->context(); - if(sel) { - Ime::ComPtr 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; } diff --git a/libIME/CMakeLists.txt b/libIME/CMakeLists.txt index f868a959d..b19037a35 100644 --- a/libIME/CMakeLists.txt +++ b/libIME/CMakeLists.txt @@ -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 ) diff --git a/libIME/KeyEvent.cpp b/libIME/KeyEvent.cpp index 39c053996..267125858 100644 --- a/libIME/KeyEvent.cpp +++ b/libIME/KeyEvent.cpp @@ -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): diff --git a/libIME/Tooltip.cpp b/libIME/Tooltip.cpp new file mode 100644 index 000000000..b68302b22 --- /dev/null +++ b/libIME/Tooltip.cpp @@ -0,0 +1,112 @@ +// +// Copyright (C) 2013 Hong Jen Yee (PCMan) +// +// 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 diff --git a/libIME/Tooltip.h b/libIME/Tooltip.h new file mode 100644 index 000000000..3ca7bec30 --- /dev/null +++ b/libIME/Tooltip.h @@ -0,0 +1,53 @@ +// +// Copyright (C) 2013 Hong Jen Yee (PCMan) +// +// 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 + +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