blob: daa7688554863a26d772e36738e93f87c825fefc [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
nisseede5da42017-01-12 05:15:36 -080011#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012#include "webrtc/base/common.h"
13#include "webrtc/base/logging.h"
14#include "webrtc/base/win32window.h"
15
16namespace rtc {
17
18///////////////////////////////////////////////////////////////////////////////
19// Win32Window
20///////////////////////////////////////////////////////////////////////////////
21
22static const wchar_t kWindowBaseClassName[] = L"WindowBaseClass";
23HINSTANCE Win32Window::instance_ = NULL;
24ATOM Win32Window::window_class_ = 0;
25
26Win32Window::Win32Window() : wnd_(NULL) {
27}
28
29Win32Window::~Win32Window() {
nisseede5da42017-01-12 05:15:36 -080030 RTC_DCHECK(NULL == wnd_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031}
32
33bool Win32Window::Create(HWND parent, const wchar_t* title, DWORD style,
34 DWORD exstyle, int x, int y, int cx, int cy) {
35 if (wnd_) {
36 // Window already exists.
37 return false;
38 }
39
40 if (!window_class_) {
41 if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
42 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
43 reinterpret_cast<LPCWSTR>(&Win32Window::WndProc),
44 &instance_)) {
45 LOG_GLE(LS_ERROR) << "GetModuleHandleEx failed";
46 return false;
47 }
48
49 // Class not registered, register it.
50 WNDCLASSEX wcex;
51 memset(&wcex, 0, sizeof(wcex));
52 wcex.cbSize = sizeof(wcex);
53 wcex.hInstance = instance_;
54 wcex.lpfnWndProc = &Win32Window::WndProc;
55 wcex.lpszClassName = kWindowBaseClassName;
56 window_class_ = ::RegisterClassEx(&wcex);
57 if (!window_class_) {
58 LOG_GLE(LS_ERROR) << "RegisterClassEx failed";
59 return false;
60 }
61 }
62 wnd_ = ::CreateWindowEx(exstyle, kWindowBaseClassName, title, style,
63 x, y, cx, cy, parent, NULL, instance_, this);
64 return (NULL != wnd_);
65}
66
67void Win32Window::Destroy() {
68 VERIFY(::DestroyWindow(wnd_) != FALSE);
69}
70
71void Win32Window::Shutdown() {
72 if (window_class_) {
73 ::UnregisterClass(MAKEINTATOM(window_class_), instance_);
74 window_class_ = 0;
75 }
76}
77
78bool Win32Window::OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam,
79 LRESULT& result) {
80 switch (uMsg) {
81 case WM_CLOSE:
82 if (!OnClose()) {
83 result = 0;
84 return true;
85 }
86 break;
87 }
88 return false;
89}
90
91LRESULT Win32Window::WndProc(HWND hwnd, UINT uMsg,
92 WPARAM wParam, LPARAM lParam) {
93 Win32Window* that = reinterpret_cast<Win32Window*>(
94 ::GetWindowLongPtr(hwnd, GWLP_USERDATA));
95 if (!that && (WM_CREATE == uMsg)) {
96 CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lParam);
97 that = static_cast<Win32Window*>(cs->lpCreateParams);
98 that->wnd_ = hwnd;
99 ::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(that));
100 }
101 if (that) {
102 LRESULT result;
103 bool handled = that->OnMessage(uMsg, wParam, lParam, result);
104 if (WM_DESTROY == uMsg) {
105 for (HWND child = ::GetWindow(hwnd, GW_CHILD); child;
106 child = ::GetWindow(child, GW_HWNDNEXT)) {
107 LOG(LS_INFO) << "Child window: " << static_cast<void*>(child);
108 }
109 }
110 if (WM_NCDESTROY == uMsg) {
111 ::SetWindowLongPtr(hwnd, GWLP_USERDATA, NULL);
112 that->wnd_ = NULL;
113 that->OnNcDestroy();
114 }
115 if (handled) {
116 return result;
117 }
118 }
119 return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
120}
121
122} // namespace rtc