henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/win32window.h" |
| 12 | #include "rtc_base/checks.h" |
| 13 | #include "rtc_base/logging.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 14 | |
| 15 | namespace rtc { |
| 16 | |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | // Win32Window |
| 19 | /////////////////////////////////////////////////////////////////////////////// |
| 20 | |
| 21 | static const wchar_t kWindowBaseClassName[] = L"WindowBaseClass"; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 22 | HINSTANCE Win32Window::instance_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | ATOM Win32Window::window_class_ = 0; |
| 24 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 25 | Win32Window::Win32Window() : wnd_(nullptr) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | |
| 27 | Win32Window::~Win32Window() { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 28 | RTC_DCHECK(nullptr == wnd_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | bool Win32Window::Create(HWND parent, const wchar_t* title, DWORD style, |
| 32 | DWORD exstyle, int x, int y, int cx, int cy) { |
| 33 | if (wnd_) { |
| 34 | // Window already exists. |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | if (!window_class_) { |
| 39 | if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 40 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 41 | reinterpret_cast<LPCWSTR>(&Win32Window::WndProc), |
| 42 | &instance_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 43 | RTC_LOG_GLE(LS_ERROR) << "GetModuleHandleEx failed"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | |
| 47 | // Class not registered, register it. |
| 48 | WNDCLASSEX wcex; |
| 49 | memset(&wcex, 0, sizeof(wcex)); |
| 50 | wcex.cbSize = sizeof(wcex); |
| 51 | wcex.hInstance = instance_; |
| 52 | wcex.lpfnWndProc = &Win32Window::WndProc; |
| 53 | wcex.lpszClassName = kWindowBaseClassName; |
| 54 | window_class_ = ::RegisterClassEx(&wcex); |
| 55 | if (!window_class_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 56 | RTC_LOG_GLE(LS_ERROR) << "RegisterClassEx failed"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | } |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 60 | wnd_ = ::CreateWindowEx(exstyle, kWindowBaseClassName, title, style, x, y, cx, |
| 61 | cy, parent, nullptr, instance_, this); |
| 62 | return (nullptr != wnd_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void Win32Window::Destroy() { |
nisse | c16fa5e | 2017-02-07 07:18:43 -0800 | [diff] [blame] | 66 | const bool success = ::DestroyWindow(wnd_); |
| 67 | RTC_DCHECK(success); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void Win32Window::Shutdown() { |
| 71 | if (window_class_) { |
| 72 | ::UnregisterClass(MAKEINTATOM(window_class_), instance_); |
| 73 | window_class_ = 0; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | bool Win32Window::OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, |
| 78 | LRESULT& result) { |
| 79 | switch (uMsg) { |
| 80 | case WM_CLOSE: |
| 81 | if (!OnClose()) { |
| 82 | result = 0; |
| 83 | return true; |
| 84 | } |
| 85 | break; |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 90 | bool Win32Window::OnClose() { |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | void Win32Window::OnNcDestroy() { |
| 95 | // Do nothing. } |
| 96 | } |
| 97 | |
| 98 | LRESULT Win32Window::WndProc(HWND hwnd, |
| 99 | UINT uMsg, |
| 100 | WPARAM wParam, |
| 101 | LPARAM lParam) { |
| 102 | Win32Window* that = |
| 103 | reinterpret_cast<Win32Window*>(::GetWindowLongPtr(hwnd, GWLP_USERDATA)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | if (!that && (WM_CREATE == uMsg)) { |
| 105 | CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lParam); |
| 106 | that = static_cast<Win32Window*>(cs->lpCreateParams); |
| 107 | that->wnd_ = hwnd; |
| 108 | ::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(that)); |
| 109 | } |
| 110 | if (that) { |
| 111 | LRESULT result; |
| 112 | bool handled = that->OnMessage(uMsg, wParam, lParam, result); |
| 113 | if (WM_DESTROY == uMsg) { |
| 114 | for (HWND child = ::GetWindow(hwnd, GW_CHILD); child; |
| 115 | child = ::GetWindow(child, GW_HWNDNEXT)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 116 | RTC_LOG(LS_INFO) << "Child window: " << static_cast<void*>(child); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | if (WM_NCDESTROY == uMsg) { |
| 120 | ::SetWindowLongPtr(hwnd, GWLP_USERDATA, NULL); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 121 | that->wnd_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 122 | that->OnNcDestroy(); |
| 123 | } |
| 124 | if (handled) { |
| 125 | return result; |
| 126 | } |
| 127 | } |
| 128 | return ::DefWindowProc(hwnd, uMsg, wParam, lParam); |
| 129 | } |
| 130 | |
| 131 | } // namespace rtc |