BsWin32Window.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "Win32/BsWin32Window.h"
  2. #include "Win32/BsWin32Platform.h"
  3. namespace BansheeEngine
  4. {
  5. struct Win32Window::Pimpl
  6. {
  7. HWND hWnd = nullptr;
  8. INT32 left = 0;
  9. INT32 top = 0;
  10. UINT32 width = 0;
  11. UINT32 height = 0;
  12. bool isExternal = false;
  13. DWORD style = 0;
  14. DWORD styleEx = 0;
  15. };
  16. Win32Window::Win32Window(const WINDOW_DESC& desc)
  17. {
  18. m = bs_new<Pimpl>();
  19. HMONITOR hMonitor = desc.monitor;
  20. if (!desc.external)
  21. {
  22. m->style = WS_CLIPCHILDREN;
  23. if (!desc.hidden)
  24. m->style |= WS_VISIBLE;
  25. INT32 left = desc.left;
  26. INT32 top = desc.top;
  27. // If we didn't specified the adapter index, or if we didn't find it
  28. if (hMonitor == nullptr)
  29. {
  30. POINT windowAnchorPoint;
  31. // Fill in anchor point.
  32. windowAnchorPoint.x = left;
  33. windowAnchorPoint.y = top;
  34. // Get the nearest monitor to this window.
  35. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTOPRIMARY);
  36. }
  37. // Get the target monitor info
  38. MONITORINFO monitorInfo;
  39. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  40. monitorInfo.cbSize = sizeof(MONITORINFO);
  41. GetMonitorInfo(hMonitor, &monitorInfo);
  42. UINT32 width = desc.width;
  43. UINT32 height = desc.height;
  44. // No specified top left -> Center the window in the middle of the monitor
  45. if (left == -1 || top == -1)
  46. {
  47. int screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  48. int screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  49. // clamp window dimensions to screen size
  50. int outerw = (int(width) < screenw) ? int(width) : screenw;
  51. int outerh = (int(height) < screenh) ? int(height) : screenh;
  52. if (left == -1)
  53. left = monitorInfo.rcWork.left + (screenw - outerw) / 2;
  54. else if (hMonitor != nullptr)
  55. left += monitorInfo.rcWork.left;
  56. if (top == -1)
  57. top = monitorInfo.rcWork.top + (screenh - outerh) / 2;
  58. else if (hMonitor != nullptr)
  59. top += monitorInfo.rcWork.top;
  60. }
  61. else if (hMonitor != nullptr)
  62. {
  63. left += monitorInfo.rcWork.left;
  64. top += monitorInfo.rcWork.top;
  65. }
  66. if (!desc.fullscreen)
  67. {
  68. if (desc.parent)
  69. {
  70. if (desc.toolWindow)
  71. m->styleEx = WS_EX_TOOLWINDOW;
  72. else
  73. m->style |= WS_CHILD;
  74. }
  75. if (!desc.parent || desc.toolWindow)
  76. {
  77. if (desc.border == WindowBorder::None)
  78. m->style |= WS_POPUP;
  79. else if (desc.border == WindowBorder::Fixed)
  80. m->style |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
  81. else
  82. m->style |= WS_OVERLAPPEDWINDOW;
  83. }
  84. if (!desc.outerDimensions)
  85. {
  86. // Calculate window dimensions required to get the requested client area
  87. RECT rect;
  88. SetRect(&rect, 0, 0, width, height);
  89. AdjustWindowRect(&rect, m->style, false);
  90. width = rect.right - rect.left;
  91. height = rect.bottom - rect.top;
  92. // Clamp width and height to the desktop dimensions
  93. int screenw = GetSystemMetrics(SM_CXSCREEN);
  94. int screenh = GetSystemMetrics(SM_CYSCREEN);
  95. if ((int)width > screenw)
  96. width = screenw;
  97. if ((int)height > screenh)
  98. height = screenh;
  99. if (left < 0)
  100. left = (screenw - width) / 2;
  101. if (top < 0)
  102. top = (screenh - height) / 2;
  103. }
  104. }
  105. else
  106. {
  107. m->style |= WS_POPUP;
  108. top = 0;
  109. left = 0;
  110. }
  111. UINT classStyle = 0;
  112. if (desc.enableDoubleClick)
  113. classStyle |= CS_DBLCLKS;
  114. // Register the window class
  115. WNDCLASS wc = { classStyle, Win32Platform::_win32WndProc, 0, 0, desc.module,
  116. LoadIcon(nullptr, IDI_APPLICATION), LoadCursor(nullptr, IDC_ARROW),
  117. (HBRUSH)GetStockObject(BLACK_BRUSH), 0, "Win32Wnd" };
  118. RegisterClass(&wc);
  119. // Create main window
  120. m->hWnd = CreateWindowEx(m->styleEx, "Win32Wnd", desc.title.c_str(), m->style,
  121. left, top, width, height, desc.parent, nullptr, desc.module, desc.creationParams);
  122. m->isExternal = false;
  123. }
  124. else
  125. {
  126. m->hWnd = desc.external;
  127. m->isExternal = true;
  128. }
  129. RECT rect;
  130. GetWindowRect(m->hWnd, &rect);
  131. m->top = rect.top;
  132. m->left = rect.left;
  133. GetClientRect(m->hWnd, &rect);
  134. m->width = rect.right;
  135. m->height = rect.bottom;
  136. }
  137. Win32Window::~Win32Window()
  138. {
  139. if (m->hWnd && !m->isExternal)
  140. DestroyWindow(m->hWnd);
  141. bs_delete(m);
  142. }
  143. void Win32Window::move(INT32 top, INT32 left)
  144. {
  145. if (m->hWnd)
  146. {
  147. m->top = top;
  148. m->left = left;
  149. SetWindowPos(m->hWnd, nullptr, left, top, m->width, m->height, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  150. }
  151. }
  152. void Win32Window::resize(UINT32 width, UINT32 height)
  153. {
  154. if (m->hWnd)
  155. {
  156. RECT rc = { m->top, m->left, width, height };
  157. AdjustWindowRect(&rc, GetWindowLong(m->hWnd, GWL_STYLE), false);
  158. width = rc.right - rc.left;
  159. height = rc.bottom - rc.top;
  160. m->width = width;
  161. m->height = height;
  162. SetWindowPos(m->hWnd, nullptr, m->left, m->top, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  163. }
  164. }
  165. void Win32Window::setActive(bool state)
  166. {
  167. if (m->hWnd)
  168. {
  169. if (state)
  170. ShowWindow(m->hWnd, SW_RESTORE);
  171. else
  172. ShowWindow(m->hWnd, SW_SHOWMINNOACTIVE);
  173. }
  174. }
  175. void Win32Window::setHidden(bool hidden)
  176. {
  177. if (hidden)
  178. ShowWindow(m->hWnd, SW_HIDE);
  179. else
  180. ShowWindow(m->hWnd, SW_SHOWNORMAL);
  181. }
  182. void Win32Window::minimize()
  183. {
  184. if (m->hWnd)
  185. SendMessage(m->hWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  186. }
  187. void Win32Window::maximize()
  188. {
  189. if (m->hWnd)
  190. SendMessage(m->hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  191. }
  192. void Win32Window::restore()
  193. {
  194. if (m->hWnd)
  195. SendMessage(m->hWnd, WM_SYSCOMMAND, SC_RESTORE, 0);
  196. }
  197. void Win32Window::_windowMovedOrResized()
  198. {
  199. if (!m->hWnd || IsIconic(m->hWnd))
  200. return;
  201. RECT rc;
  202. GetWindowRect(m->hWnd, &rc);
  203. m->top = rc.top;
  204. m->left = rc.left;
  205. GetClientRect(m->hWnd, &rc);
  206. m->width = rc.right - rc.left;
  207. m->height = rc.bottom - rc.top;
  208. }
  209. Vector2I Win32Window::screenToWindowPos(const Vector2I& screenPos) const
  210. {
  211. POINT pos;
  212. pos.x = screenPos.x;
  213. pos.y = screenPos.y;
  214. ScreenToClient(m->hWnd, &pos);
  215. return Vector2I(pos.x, pos.y);
  216. }
  217. Vector2I Win32Window::windowToScreenPos(const Vector2I& windowPos) const
  218. {
  219. POINT pos;
  220. pos.x = windowPos.x;
  221. pos.y = windowPos.y;
  222. ClientToScreen(m->hWnd, &pos);
  223. return Vector2I(pos.x, pos.y);
  224. }
  225. INT32 Win32Window::getLeft() const
  226. {
  227. return m->left;
  228. }
  229. INT32 Win32Window::getTop() const
  230. {
  231. return m->top;
  232. }
  233. UINT32 Win32Window::getWidth() const
  234. {
  235. return m->width;
  236. }
  237. UINT32 Win32Window::getHeight() const
  238. {
  239. return m->height;
  240. }
  241. HWND Win32Window::getHWnd() const
  242. {
  243. return m->hWnd;
  244. }
  245. DWORD Win32Window::getStyle() const
  246. {
  247. return m->style;
  248. }
  249. DWORD Win32Window::getStyleEx() const
  250. {
  251. return m->styleEx;
  252. }
  253. }