2 * Copyright (c) 2005 Alexander Gottwald
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
22 * Except as contained in this notice, the name(s) of the above copyright
23 * holders shall not be used in advertising or otherwise to promote the sale,
24 * use or other dealings in this Software without prior written authorization.
32 CWindow::CWindowClass
CWindow::windowClass("CWINDOWCLASS", DefWindowProc
);
34 CWindow::CWindowClass::CWindowClass(const char *_name
, WNDPROC _wndproc
) :
35 wndproc(_wndproc
), atom(0), classname(_name
)
40 CWindow::CWindowClass::~CWindowClass()
42 UnregisterClass(classname
.c_str(), GetModuleHandle(NULL
));
45 void CWindow::CWindowClass::Register()
48 memset(&wndclass
, 0, sizeof(wndclass
));
49 wndclass
.cbSize
= sizeof(wndclass
);
51 wndclass
.lpfnWndProc
= wndproc
;
52 wndclass
.cbClsExtra
= 0;
53 wndclass
.cbWndExtra
= 0;
54 wndclass
.hInstance
= GetModuleHandle(NULL
);
55 wndclass
.hIcon
= LoadIcon(NULL
, IDI_APPLICATION
);
56 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
57 wndclass
.hbrBackground
= (HBRUSH
)(1 + COLOR_BTNFACE
);
58 wndclass
.lpszMenuName
= NULL
;
59 wndclass
.lpszClassName
= classname
.c_str();
60 wndclass
.hIconSm
= NULL
;
61 atom
= RegisterClassEx(&wndclass
);
63 throw win32_error("RegisterClassEx failed");
66 CWindow::CWindow(const char *_title
) : title(_title
), hwnd(NULL
), parent(NULL
), bounds(), owndproc(NULL
), showing(FALSE
)
72 HWND
CWindow::CreateWindowHandle()
74 HWND ret
= CreateWindowEx(
85 GetModuleHandle(NULL
),
89 throw win32_error("CreateWindowEx failed");
93 void CWindow::Create()
97 hwnd
= CreateWindowHandle();
99 throw win32_error("Could not create window");
101 // Reset the error code
105 // Attach the object reference to the window handle
106 SetWindowLongPtr(hwnd
, GWLP_USERDATA
, (LONG_PTR
)this);
107 err
= GetLastError();
109 throw win32_error("SetWindowLongPtr failed",err
);
111 // Set the window proc
112 owndproc
= (WNDPROC
)SetWindowLongPtr(hwnd
, GWLP_WNDPROC
, (LONG_PTR
)WindowProc
);
113 err
= GetLastError();
115 throw win32_error("SetWindowLongPtr failed",err
);
118 const char *CWindow::GetClassName()
120 return windowClass
.GetClassName();
123 LRESULT CALLBACK
CWindow::WindowProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
125 MessageDebug::debug(hwnd
, uMsg
, wParam
, lParam
, __FUNCTION__
);
126 CWindow
* window
= (CWindow
*)GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
128 return window
->Dispatch(hwnd
, uMsg
, wParam
, lParam
);
129 return DefWindowProc(hwnd
, uMsg
, wParam
, lParam
);
132 LRESULT
CWindow::Dispatch(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
137 bounds
.width
= LOWORD(lParam
);
138 bounds
.height
= LOWORD(lParam
);
141 bounds
.left
= LOWORD(lParam
);
142 bounds
.top
= LOWORD(lParam
);
149 return CallWindowProc(owndproc
, hwnd
, uMsg
, wParam
, lParam
);
151 return DefWindowProc(hwnd
, uMsg
, wParam
, lParam
);
158 ShowWindow(hwnd
, SW_SHOWNORMAL
);
161 int CWindow::ShowModal()
168 while( showing
&& (bRet
= GetMessage( &msg
, NULL
, 0, 0 )) != 0)
172 // handle the error and possibly exit
176 TranslateMessage(&msg
);
177 DispatchMessage(&msg
);
183 void CWindow::SetLeft(int left
)
187 if (!SetWindowPos(hwnd
, NULL
,
188 bounds
.left
, bounds
.top
,
190 SWP_NOZORDER
| SWP_NOSIZE
))
191 throw win32_error("SetWindowPos failed");
194 void CWindow::SetTop(int top
)
198 if (!SetWindowPos(hwnd
, NULL
,
199 bounds
.left
, bounds
.top
,
201 SWP_NOZORDER
| SWP_NOSIZE
))
202 throw win32_error("SetWindowPos failed");
205 void CWindow::SetWidth(int width
)
207 bounds
.width
= width
;
209 if (!SetWindowPos(hwnd
, NULL
,
211 bounds
.width
, bounds
.height
,
212 SWP_NOZORDER
| SWP_NOMOVE
))
213 throw win32_error("SetWindowPos failed");
215 void CWindow::SetHeight(int height
)
217 bounds
.height
= height
;
219 if (!SetWindowPos(hwnd
, NULL
,
221 bounds
.width
, bounds
.height
,
222 SWP_NOZORDER
| SWP_NOMOVE
))
223 throw win32_error("SetWindowPos failed");
226 void CWindow::SetBounds(int left
, int top
, int width
, int height
)
228 bounds
= CBoundary(left
, top
, width
, height
);
230 if (!SetWindowPos(hwnd
, NULL
,
231 bounds
.left
, bounds
.top
,
232 bounds
.width
, bounds
.height
,
234 throw win32_error("SetWindowPos failed");
237 void CWindow::SetBounds(const RECT
&rect
)
241 if (!SetWindowPos(hwnd
, NULL
,
242 bounds
.left
, bounds
.top
,
243 bounds
.width
, bounds
.height
,
245 throw win32_error("SetWindowPos failed");
248 HWND
CWindow::GetHandle()
255 void CWindow::SetParent(CWindow
*window
)
257 parent
= window
->GetHandle();
259 if (::SetParent(hwnd
, parent
) == NULL
)
260 throw win32_error("SetParent failed");
264 void CWindow::SetStyle(DWORD style
)
269 SetWindowLong(hwnd
, GWL_STYLE
, style
);
270 int err
= GetLastError();
272 throw win32_error("SetWindowLong failed", err
);
275 void CWindow::SetExStyle(DWORD exstyle
)
277 this->exstyle
= exstyle
;
280 SetWindowLong(hwnd
, GWL_EXSTYLE
, exstyle
);
281 int err
= GetLastError();
283 throw win32_error("SetWindowWLong failed", err
);