3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "talk/base/common.h"
29
#include "talk/base/logging.h"
30
#include "talk/base/win32window.h"
34
///////////////////////////////////////////////////////////////////////////////
36
///////////////////////////////////////////////////////////////////////////////
38
static const wchar_t kWindowBaseClassName[] = L"WindowBaseClass";
39
HINSTANCE Win32Window::instance_ = GetModuleHandle(NULL);
40
ATOM Win32Window::window_class_ = 0;
42
Win32Window::Win32Window() : wnd_(NULL) {
45
Win32Window::~Win32Window() {
49
bool Win32Window::Create(HWND parent, const wchar_t* title, DWORD style,
50
DWORD exstyle, int x, int y, int cx, int cy) {
52
// Window already exists.
57
// Class not registered, register it.
59
memset(&wcex, 0, sizeof(wcex));
60
wcex.cbSize = sizeof(wcex);
61
wcex.hInstance = instance_;
62
wcex.lpfnWndProc = &Win32Window::WndProc;
63
wcex.lpszClassName = kWindowBaseClassName;
64
window_class_ = ::RegisterClassEx(&wcex);
66
LOG_GLE(LS_ERROR) << "RegisterClassEx failed";
70
wnd_ = ::CreateWindowEx(exstyle, kWindowBaseClassName, title, style,
71
x, y, cx, cy, parent, NULL, instance_, this);
72
return (NULL != wnd_);
75
void Win32Window::Destroy() {
76
VERIFY(::DestroyWindow(wnd_) != FALSE);
79
void Win32Window::SetInstance(HINSTANCE instance) {
83
void Win32Window::Shutdown() {
85
::UnregisterClass(MAKEINTATOM(window_class_), instance_);
90
bool Win32Window::OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam,
103
LRESULT Win32Window::WndProc(HWND hwnd, UINT uMsg,
104
WPARAM wParam, LPARAM lParam) {
105
Win32Window* that = reinterpret_cast<Win32Window*>(
106
::GetWindowLongPtr(hwnd, GWLP_USERDATA));
107
if (!that && (WM_CREATE == uMsg)) {
108
CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lParam);
109
that = static_cast<Win32Window*>(cs->lpCreateParams);
111
::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(that));
115
bool handled = that->OnMessage(uMsg, wParam, lParam, result);
116
if (WM_DESTROY == uMsg) {
117
for (HWND child = ::GetWindow(hwnd, GW_CHILD); child;
118
child = ::GetWindow(child, GW_HWNDNEXT)) {
119
LOG(LS_INFO) << "Child window: " << static_cast<void*>(child);
122
if (WM_NCDESTROY == uMsg) {
123
::SetWindowLongPtr(hwnd, GWLP_USERDATA, NULL);
131
return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
134
} // namespace talk_base