GteMSWWindowSystem.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.1 (2016/10/12)
7 
8 #include <GTEnginePCH.h>
10 using namespace gte;
11 
13 {
14  if (mHandleMap.size() == 0 && mAtom)
15  {
16  UnregisterClass(mWindowClassName, nullptr);
17  }
18 }
19 
21  :
22  mWindowClassName(L"GTEngineWindow"),
23  mAtom(0)
24 {
25  WNDCLASS wc;
26  wc.style = CS_OWNDC;
27  wc.lpfnWndProc = WindowProcedure;
28  wc.cbClsExtra = 0;
29  wc.cbWndExtra = 0;
30  wc.hInstance = nullptr;
31  wc.hIcon = LoadIcon(0, IDI_APPLICATION);
32  wc.hCursor = LoadCursor(0, IDC_ARROW);
33  wc.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
34  wc.lpszClassName = mWindowClassName;
35  wc.lpszMenuName = nullptr;
36  mAtom = RegisterClass(&wc);
37 }
38 
39 bool MSWWindowSystem::GetWindowRectangle(int xClientSize, int yClientSize,
40  DWORD style, RECT& windowRectangle)
41 {
42  windowRectangle.left = 0;
43  windowRectangle.top = 0;
44  windowRectangle.right = static_cast<LONG>(xClientSize) - 1;
45  windowRectangle.bottom = static_cast<LONG>(yClientSize) - 1;
46  return AdjustWindowRect(&windowRectangle, style, FALSE) != FALSE;
47 }
48 
50 {
51  DWORD style;
52  if (parameters.allowResize)
53  {
54  style = WS_OVERLAPPEDWINDOW;
55  }
56  else
57  {
58  // This removes WS_THICKFRAME and WS_MAXIMIZEBOX from
59  // WS_OVERLAPPEDWINDOW, both of which allow resizing of windows.
60  style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
61  }
62 
63  if (parameters.hscrollBar)
64  {
65  style |= WS_HSCROLL;
66  }
67 
68  if (parameters.vscrollBar)
69  {
70  style |= WS_VSCROLL;
71  }
72 
73  RECT rectangle;
74  bool adjusted = GetWindowRectangle(parameters.xSize, parameters.ySize,
75  style, rectangle);
76  if (adjusted)
77  {
78  int adjustedXSize = (int)rectangle.right - (int)rectangle.left + 1;
79  int adjustedYSize = (int)rectangle.bottom - (int)rectangle.top + 1;
80  parameters.handle = CreateWindow(mWindowClassName,
81  parameters.title.c_str(), style, parameters.xOrigin,
82  parameters.yOrigin, adjustedXSize, adjustedYSize, nullptr,
83  nullptr, nullptr, nullptr);
84 
85  // AdjustWindowRect decides that scroll bars cover client space,
86  // so the adjustment reduces the requested client rectangle size.
87  // We have to detect this and recreate a window of the correct size.
88  if (parameters.hscrollBar || parameters.vscrollBar)
89  {
90  GetClientRect(parameters.handle, &rectangle);
91  int clientXSize = (int)rectangle.right - (int)rectangle.left;
92  int clientYSize = (int)rectangle.bottom - (int)rectangle.top;
93  if (clientXSize != parameters.xSize
94  || clientYSize != parameters.ySize)
95  {
96  DestroyWindow(parameters.handle);
97  adjustedXSize += parameters.xSize - clientXSize;
98  adjustedYSize += parameters.ySize - clientYSize;
99  parameters.handle = CreateWindow(mWindowClassName,
100  parameters.title.c_str(), style, parameters.xOrigin,
101  parameters.yOrigin, adjustedXSize, adjustedYSize,
102  0, 0, 0, 0);
103  GetClientRect(parameters.handle, &rectangle);
104  }
105  }
106 
107  CreateEngineAndProgramFactory(parameters);
108  }
109  else
110  {
111  LogError("AdjustWindowRect failed.");
112  parameters.handle = nullptr;
113  parameters.engine = nullptr;
114  parameters.created = false;
115  }
116 }
117 
118 void MSWWindowSystem::Extract(LPARAM lParam, int& x, int& y)
119 {
120  x = static_cast<int>(static_cast<short>(lParam & 0xFFFF));
121  y = static_cast<int>(static_cast<short>((lParam & 0xFFFF0000) >> 16));
122 }
123 
124 void MSWWindowSystem::Extract(WPARAM wParam, int& x, int& y)
125 {
126  x = static_cast<int>(static_cast<short>(wParam & 0xFFFF));
127  y = static_cast<int>(static_cast<short>((wParam & 0xFFFF0000) >> 16));
128 }
129 
130 LRESULT CALLBACK MSWWindowSystem::WindowProcedure(HWND handle, UINT message,
131  WPARAM wParam, LPARAM lParam)
132 {
133  auto iter = TheWindowSystem.mHandleMap.find(handle);
134  if (iter == TheWindowSystem.mHandleMap.end())
135  {
136  return DefWindowProc(handle, message, wParam, lParam);
137  }
138 
139  MSWWindow& window = *iter->second;
140 
141  switch (message)
142  {
143  case WM_PAINT:
144  {
145  PAINTSTRUCT ps;
146  BeginPaint(handle, &ps);
147  window.OnDisplay();
148  EndPaint(handle, &ps);
149  return 0;
150  }
151  case WM_ERASEBKGND:
152  {
153  // This tells Windows not to erase the background (and that the
154  // application is doing so).
155  return 1;
156  }
157  case WM_MOVE:
158  {
159  // Get the origin of the moved window. The y-value for window
160  // moves is left-handed.
161  int x, y;
162  Extract(lParam, x, y);
163  window.OnMove(x, y);
164  return 0;
165  }
166  case WM_SIZE:
167  {
168  // Get the new size of the window.
169  int xSize, ySize;
170  Extract(lParam, xSize, ySize);
171 
172  if (wParam == SIZE_MINIMIZED)
173  {
174  // assert: xSize == 0 and ySize == 0
175  window.OnMinimize();
176  }
177  else if (wParam == SIZE_MAXIMIZED)
178  {
179  window.OnMaximize();
180  window.OnResize(xSize, ySize);
181  }
182  else if (wParam == SIZE_RESTORED)
183  {
184 
185  window.OnResize(xSize, ySize);
186  }
187  return 0;
188  }
189  case WM_CHAR:
190  {
191  // Get thet translated key code.
192  unsigned char key = static_cast<unsigned char>(static_cast<char>(wParam));
193 
194  // Get the cursor position in client coordinates.
195  POINT point;
196  GetCursorPos(&point);
197  ScreenToClient(handle, &point);
198  int x = static_cast<int>(point.x);
199  int y = static_cast<int>(point.y);
200 
201  window.OnCharPress(key, x, y);
202  return 0;
203  }
204  case WM_KEYDOWN:
205  {
206  // Get the virtual key code.
207  int key = static_cast<int>(wParam);
208 
209  // Get the cursor position in client coordinates.
210  POINT point;
211  GetCursorPos(&point);
212  ScreenToClient(handle, &point);
213  int x = static_cast<int>(point.x);
214  int y = static_cast<int>(point.y);
215 
216  window.OnKeyDown(key, x, y);
217  return 0;
218  }
219  case WM_KEYUP:
220  {
221  // Get the virtual key code.
222  int key = static_cast<int>(wParam);
223 
224  // Get the cursor position in client coordinates.
225  POINT point;
226  GetCursorPos(&point);
227  ScreenToClient(handle, &point);
228  int x = static_cast<int>(point.x);
229  int y = static_cast<int>(point.y);
230 
231  window.OnKeyUp(key, x, y);
232  return 0;
233  }
234  case WM_LBUTTONDOWN:
235  {
236  // Get the modifier flags.
237  unsigned int modifiers = static_cast<unsigned int>(wParam);
238 
239  // Get the cursor position in client coordinates.
240  int x, y;
241  Extract(lParam, x, y);
242 
244  x, y, modifiers);
245  return 0;
246  }
247  case WM_LBUTTONUP:
248  {
249  // Get the modifier flags.
250  unsigned int modifiers = static_cast<unsigned int>(wParam);
251 
252  // Get the cursor position in client coordinates.
253  int x, y;
254  Extract(lParam, x, y);
255 
257  x, y, modifiers);
258  return 0;
259  }
260  case WM_MBUTTONDOWN:
261  {
262  // Get the modifier flags.
263  unsigned int modifiers = static_cast<unsigned int>(wParam);
264 
265  // Get the cursor position in client coordinates.
266  int x, y;
267  Extract(lParam, x, y);
268 
270  x, y, modifiers);
271  return 0;
272  }
273  case WM_MBUTTONUP:
274  {
275  // Get the modifier flags.
276  unsigned int modifiers = static_cast<unsigned int>(wParam);
277 
278  // Get the cursor position in client coordinates.
279  int x, y;
280  Extract(lParam, x, y);
281 
283  x, y, modifiers);
284  return 0;
285  }
286  case WM_RBUTTONDOWN:
287  {
288  // Get the modifier flags.
289  unsigned int modifiers = static_cast<unsigned int>(wParam);
290 
291  // Get the cursor position in client coordinates.
292  int x, y;
293  Extract(lParam, x, y);
294 
296  x, y, modifiers);
297  return 0;
298  }
299  case WM_RBUTTONUP:
300  {
301  // Get the modifier flags.
302  unsigned int modifiers = static_cast<unsigned int>(wParam);
303 
304  // Get the cursor position in client coordinates.
305  int x, y;
306  Extract(lParam, x, y);
307 
309  x, y, modifiers);
310  return 0;
311  }
312  case WM_MOUSEMOVE:
313  {
314  // Get the modifier flags.
315  unsigned int modifiers = static_cast<unsigned int>(wParam);
316 
317  // Get the cursor position in client coordinates.
318  int x, y;
319  Extract(lParam, x, y);
320 
321  int button;
322  if (wParam & MK_LBUTTON)
323  {
324  button = WindowBase::MOUSE_LEFT;
325  }
326  else if (wParam & MK_MBUTTON)
327  {
328  button = WindowBase::MOUSE_MIDDLE;
329  }
330  else if (wParam & MK_RBUTTON)
331  {
332  button = WindowBase::MOUSE_RIGHT;
333  }
334  else
335  {
336  button = WindowBase::MOUSE_NONE;
337  }
338 
339  window.OnMouseMotion(button, x, y, modifiers);
340  return 0;
341  }
342  case WM_MOUSEWHEEL:
343  {
344  // Get the modifier flags and the amount the wheel rotated.
345  int modifiers, delta;
346  Extract(wParam, modifiers, delta);
347 
348  // Get the cursor position in client coordinates.
349  POINT point;
350  GetCursorPos(&point);
351  ScreenToClient(handle, &point);
352  int x = static_cast<int>(point.x);
353  int y = static_cast<int>(point.y);
354 
355  window.OnMouseWheel(delta, x, y, (unsigned int)modifiers);
356  return 0;
357  }
358  case WM_HSCROLL: // 0x0114
359  case WM_VSCROLL: // 0x0115
360  {
361  int bar = message - WM_HSCROLL; // 0 or 1
362  switch (LOWORD(wParam))
363  {
364  case SB_LINELEFT:
365  window.OnScrollDecrementLoRes(bar);
366  break;
367  case SB_LINERIGHT:
368  window.OnScrollIncrementLoRes(bar);
369  break;
370  case SB_PAGELEFT:
371  window.OnScrollDecrementHiRes(bar);
372  break;
373  case SB_PAGERIGHT:
374  window.OnScrollIncrementHiRes(bar);
375  break;
376  case SB_THUMBPOSITION:
377  window.OnScrollEndTracking(bar);
378  break;
379  case SB_THUMBTRACK:
380  window.OnScrollTracking(bar);
381  break;
382  default:
383  // Not handled: SB_LEFT, SB_RIGHT, SB_ENDSCROLL
384  break;
385  }
386  return 0;
387  }
388  case WM_CLOSE:
389  {
390  window.OnClose();
391  return 0;
392  }
393  }
394 
395  return DefWindowProc(handle, message, wParam, lParam);
396 }
virtual void OnMaximize()
virtual void OnMove(int x, int y)
virtual bool OnMouseClick(int button, int state, int x, int y, unsigned int modifiers)
static int const MOUSE_MIDDLE
virtual void CreateEngineAndProgramFactory(MSWWindow::Parameters &parameters)=0
int OnScrollEndTracking(int bar)
std::map< HWND, std::shared_ptr< MSWWindow > > mHandleMap
virtual bool OnKeyDown(int key, int x, int y)
int OnScrollIncrementHiRes(int bar)
static LRESULT CALLBACK WindowProcedure(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
static int const MOUSE_RIGHT
GLint GLenum GLint x
Definition: glcorearb.h:404
static int const MOUSE_UP
int OnScrollDecrementHiRes(int bar)
static int const MOUSE_DOWN
static int const MOUSE_NONE
virtual void OnMinimize()
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
static bool GetWindowRectangle(int xClientSize, int yClientSize, DWORD style, RECT &windowRectangle)
#define LogError(message)
Definition: GteLogger.h:92
static void Extract(LPARAM lParam, int &x, int &y)
int OnScrollDecrementLoRes(int bar)
int OnScrollTracking(int bar)
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2538
static int const MOUSE_LEFT
virtual bool OnKeyUp(int key, int x, int y)
virtual bool OnMouseWheel(int delta, int x, int y, unsigned int modifiers)
int OnScrollIncrementLoRes(int bar)
void CreateFrom(MSWWindow::Parameters &parameters)
virtual bool OnResize(int xSize, int ySize)
virtual void OnClose() override
virtual bool OnMouseMotion(int button, int x, int y, unsigned int modifiers)
wchar_t const * mWindowClassName
virtual void OnDisplay()
typedef UINT(WINAPI *PFNWGLGETGPUIDSAMDPROC)(UINT maxCount
std::shared_ptr< BaseEngine > engine
Definition: GteWindowBase.h:34
GLint y
Definition: glcorearb.h:98
GTE_IMPEXP WindowSystem TheWindowSystem
virtual bool OnCharPress(unsigned char key, int x, int y)


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01