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.

138 lines
4.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/common.h"
  28. #include "talk/base/logging.h"
  29. #include "talk/base/win32window.h"
  30. namespace talk_base {
  31. ///////////////////////////////////////////////////////////////////////////////
  32. // Win32Window
  33. ///////////////////////////////////////////////////////////////////////////////
  34. static const wchar_t kWindowBaseClassName[] = L"WindowBaseClass";
  35. HINSTANCE Win32Window::instance_ = NULL;
  36. ATOM Win32Window::window_class_ = 0;
  37. Win32Window::Win32Window() : wnd_(NULL) {
  38. }
  39. Win32Window::~Win32Window() {
  40. ASSERT(NULL == wnd_);
  41. }
  42. bool Win32Window::Create(HWND parent, const wchar_t* title, DWORD style,
  43. DWORD exstyle, int x, int y, int cx, int cy) {
  44. if (wnd_) {
  45. // Window already exists.
  46. return false;
  47. }
  48. if (!window_class_) {
  49. if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
  50. GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
  51. reinterpret_cast<LPCWSTR>(&Win32Window::WndProc),
  52. &instance_)) {
  53. LOG_GLE(LS_ERROR) << "GetModuleHandleEx failed";
  54. return false;
  55. }
  56. // Class not registered, register it.
  57. WNDCLASSEX wcex;
  58. memset(&wcex, 0, sizeof(wcex));
  59. wcex.cbSize = sizeof(wcex);
  60. wcex.hInstance = instance_;
  61. wcex.lpfnWndProc = &Win32Window::WndProc;
  62. wcex.lpszClassName = kWindowBaseClassName;
  63. window_class_ = ::RegisterClassEx(&wcex);
  64. if (!window_class_) {
  65. LOG_GLE(LS_ERROR) << "RegisterClassEx failed";
  66. return false;
  67. }
  68. }
  69. wnd_ = ::CreateWindowEx(exstyle, kWindowBaseClassName, title, style,
  70. x, y, cx, cy, parent, NULL, instance_, this);
  71. return (NULL != wnd_);
  72. }
  73. void Win32Window::Destroy() {
  74. VERIFY(::DestroyWindow(wnd_) != FALSE);
  75. }
  76. void Win32Window::Shutdown() {
  77. if (window_class_) {
  78. ::UnregisterClass(MAKEINTATOM(window_class_), instance_);
  79. window_class_ = 0;
  80. }
  81. }
  82. bool Win32Window::OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam,
  83. LRESULT& result) {
  84. switch (uMsg) {
  85. case WM_CLOSE:
  86. if (!OnClose()) {
  87. result = 0;
  88. return true;
  89. }
  90. break;
  91. }
  92. return false;
  93. }
  94. LRESULT Win32Window::WndProc(HWND hwnd, UINT uMsg,
  95. WPARAM wParam, LPARAM lParam) {
  96. Win32Window* that = reinterpret_cast<Win32Window*>(
  97. ::GetWindowLongPtr(hwnd, GWLP_USERDATA));
  98. if (!that && (WM_CREATE == uMsg)) {
  99. CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lParam);
  100. that = static_cast<Win32Window*>(cs->lpCreateParams);
  101. that->wnd_ = hwnd;
  102. ::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(that));
  103. }
  104. if (that) {
  105. LRESULT result;
  106. bool handled = that->OnMessage(uMsg, wParam, lParam, result);
  107. if (WM_DESTROY == uMsg) {
  108. for (HWND child = ::GetWindow(hwnd, GW_CHILD); child;
  109. child = ::GetWindow(child, GW_HWNDNEXT)) {
  110. LOG(LS_INFO) << "Child window: " << static_cast<void*>(child);
  111. }
  112. }
  113. if (WM_NCDESTROY == uMsg) {
  114. ::SetWindowLongPtr(hwnd, GWLP_USERDATA, NULL);
  115. that->wnd_ = NULL;
  116. that->OnNcDestroy();
  117. }
  118. if (handled) {
  119. return result;
  120. }
  121. }
  122. return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
  123. }
  124. } // namespace talk_base