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