forked from EasyIME/libIME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.cpp
99 lines (84 loc) · 2.69 KB
/
Window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// 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 "Window.h"
namespace Ime {
static TCHAR g_imeWindowClassName[] = _T("LibImeWindow");
static HINSTANCE g_hinstance = NULL;
std::map<HWND, Window*> Window::hwndMap_;
Window::Window():
hwnd_(NULL) {
}
Window::~Window() {
if(hwnd_)
DestroyWindow(hwnd_);
}
bool Window::create(HWND parent, DWORD style, DWORD exStyle) {
hwnd_ = CreateWindowEx(exStyle, g_imeWindowClassName, NULL, style,
0, 0, 0, 0, parent, NULL, g_hinstance, NULL);
if(hwnd_) {
// associate this object with the hwnd
hwndMap_[hwnd_] = this;
return true;
}
return false;
}
void Window::destroy(void) {
if( hwnd_ )
DestroyWindow(hwnd_);
hwnd_ = NULL;
}
// static
LRESULT Window::_wndProc(HWND hwnd , UINT msg, WPARAM wp , LPARAM lp) {
// get object pointer from the hwnd
Window* window = (Window*)hwndMap_[hwnd];
// FIXME: we cannot handle WM_CREATE in Window::wndProc member function
// because the message is sent before CreateWindow returns, and
// we do SetWindowLongPtr() only after CreateWindow().
if(window) {
LRESULT result = window->wndProc(msg, wp, lp);
if(msg == WM_NCDESTROY)
hwndMap_.erase(hwnd);
return result;
}
return ::DefWindowProc(hwnd, msg, wp, lp);
}
LRESULT Window::wndProc(UINT msg, WPARAM wp , LPARAM lp) {
return ::DefWindowProc(hwnd_, msg, wp, lp);
}
bool Window::registerClass(HINSTANCE hinstance) {
if(!g_hinstance)
g_hinstance = hinstance;
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_IME; // FIXME: is this needed?
wc.lpfnWndProc = (WNDPROC)Window::_wndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = NULL;
wc.lpszMenuName = (LPTSTR)NULL;
wc.lpszClassName = g_imeWindowClassName;
wc.hbrBackground = NULL;
wc.hIconSm = NULL;
if(!::RegisterClassEx((LPWNDCLASSEX)&wc))
return false;
return true;
}
} // namespace Ime