|
| 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 |
0 commit comments