OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 // | |
5 // Originally from libjingle. Minor alterations to compile it in Chrome. | |
6 | |
7 #include "talk/base/common.h" | |
8 #include "talk/base/logging.h" | |
9 #include "talk/base/win32window.h" | |
10 | |
11 namespace talk_base { | |
12 | |
13 /////////////////////////////////////////////////////////////////////////////// | |
14 // Win32Window | |
15 /////////////////////////////////////////////////////////////////////////////// | |
16 | |
17 static const wchar_t kWindowBaseClassName[] = L"WindowBaseClass"; | |
18 HINSTANCE instance_ = GetModuleHandle(NULL); | |
19 ATOM window_class_ = 0; | |
20 | |
21 Win32Window::Win32Window() : wnd_(NULL) { | |
22 } | |
23 | |
24 Win32Window::~Win32Window() { | |
25 ASSERT(NULL == wnd_); | |
26 } | |
27 | |
28 bool Win32Window::Create(HWND parent, const wchar_t* title, DWORD style, | |
29 DWORD exstyle, int x, int y, int cx, int cy) { | |
30 if (wnd_) { | |
31 // Window already exists. | |
32 return false; | |
33 } | |
34 | |
35 if (!window_class_) { | |
36 // Class not registered, register it. | |
37 WNDCLASSEX wcex; | |
38 memset(&wcex, 0, sizeof(wcex)); | |
39 wcex.cbSize = sizeof(wcex); | |
40 wcex.hInstance = instance_; | |
41 wcex.lpfnWndProc = &Win32Window::WndProc; | |
42 wcex.lpszClassName = kWindowBaseClassName; | |
43 window_class_ = ::RegisterClassEx(&wcex); | |
44 if (!window_class_) { | |
45 LOG_GLE(LS_ERROR) << "RegisterClassEx failed"; | |
46 return false; | |
47 } | |
48 } | |
49 wnd_ = ::CreateWindowEx(exstyle, kWindowBaseClassName, title, style, | |
50 x, y, cx, cy, parent, NULL, instance_, this); | |
51 return (NULL != wnd_); | |
52 } | |
53 | |
54 void Win32Window::Destroy() { | |
55 VERIFY(::DestroyWindow(wnd_) != FALSE); | |
56 } | |
57 | |
58 #if 0 | |
59 void Win32Window::SetInstance(HINSTANCE instance) { | |
60 instance_ = instance; | |
61 } | |
62 | |
63 void Win32Window::Shutdown() { | |
64 if (window_class_) { | |
65 ::UnregisterClass(MAKEINTATOM(window_class_), instance_); | |
66 window_class_ = 0; | |
67 } | |
68 } | |
69 #endif | |
70 | |
71 bool Win32Window::OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, | |
72 LRESULT& result) { | |
73 switch (uMsg) { | |
74 case WM_CLOSE: | |
75 if (!OnClose()) { | |
76 result = 0; | |
77 return true; | |
78 } | |
79 break; | |
80 } | |
81 return false; | |
82 } | |
83 | |
84 LRESULT Win32Window::WndProc(HWND hwnd, UINT uMsg, | |
85 WPARAM wParam, LPARAM lParam) { | |
86 Win32Window* that = reinterpret_cast<Win32Window*>( | |
87 ::GetWindowLongPtr(hwnd, GWL_USERDATA)); | |
88 if (!that && (WM_CREATE == uMsg)) { | |
89 CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lParam); | |
90 that = static_cast<Win32Window*>(cs->lpCreateParams); | |
91 that->wnd_ = hwnd; | |
92 ::SetWindowLongPtr(hwnd, GWL_USERDATA, reinterpret_cast<LONG_PTR>(that)); | |
93 } | |
94 if (that) { | |
95 LRESULT result; | |
96 bool handled = that->OnMessage(uMsg, wParam, lParam, result); | |
97 if (WM_DESTROY == uMsg) { | |
98 for (HWND child = ::GetWindow(hwnd, GW_CHILD); child; | |
99 child = ::GetWindow(child, GW_HWNDNEXT)) { | |
100 LOG(LS_INFO) << "Child window: " << static_cast<void*>(child); | |
101 } | |
102 } | |
103 if (WM_NCDESTROY == uMsg) { | |
104 ::SetWindowLongPtr(hwnd, GWL_USERDATA, NULL); | |
105 that->wnd_ = NULL; | |
106 that->OnDestroyed(); | |
107 } | |
108 if (handled) { | |
109 return result; | |
110 } | |
111 } | |
112 return ::DefWindowProc(hwnd, uMsg, wParam, lParam); | |
113 } | |
114 | |
115 } // namespace talk_base | |
OLD | NEW |