You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.0 KiB

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