Visual Servoing Platform  version 3.0.0
vpWin32Window.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Windows 32 display's window class
32  *
33  * Authors:
34  * Bruno Renier
35  * Anthony Saunier
36  *
37  *****************************************************************************/
38 #include <visp3/core/vpConfig.h>
39 #include <iostream>
40 #include <visp3/gui/vpWin32API.h>
41 
42 #if ( defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) )
43 
44 #define MAX_SEM_COUNT 2147483647
45 
46 #ifndef DOXYGEN_SHOULD_SKIP_THIS
47 
48 #include <visp3/gui/vpWin32Window.h>
49 
50 //Should be already defined ...
51 #ifndef GET_X_LPARAM
52 # define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
53 #endif
54 
55 #ifndef GET_Y_LPARAM
56 # define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
57 #endif
58 
59 //declares the window as thread local
60 //allows multiple displays
61 #ifdef __MINGW32__
62 vpWin32Window * window;
63 #else
64 _declspec(thread) vpWin32Window * window;
65 #endif
66 
67 bool vpWin32Window::registered = false;
71 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
72 {
73  //the first time this callback is executed, put the window in initialized state
74  if (window != NULL)
75  {
76  if (!window->isInitialized())
77  {
78  window->initialized = true;
79  vpReleaseSemaphore(window->semaInit,1,NULL);
80  }
81  }
82  switch (message)
83  {
84  case vpWM_DISPLAY:
85  //redraw the whole window
86  InvalidateRect(window->getHWnd(), NULL, TRUE);
87  UpdateWindow(window->getHWnd());
88  break;
89 
90  case vpWM_DISPLAY_ROI:
91  {
92  RECT rect;
93 
94  rect.left = LOWORD(wParam);
95  rect.right = HIWORD(wParam);
96 
97  rect.top = LOWORD(lParam);
98  rect.bottom = HIWORD(lParam);
99 
100  InvalidateRect(window->getHWnd(), &rect, TRUE);
101  UpdateWindow(window->getHWnd());
102  }
103  break;
104 
105  case WM_LBUTTONDOWN:
106  {
107  window->clickX = GET_X_LPARAM(lParam);
108  window->clickY = GET_Y_LPARAM(lParam);
109 
110  window->clickButton = vpMouseButton::button1;
111  vpReleaseSemaphore(window->semaClick,1,NULL);
112  }
113  break;
114 
115  case WM_MBUTTONDOWN:
116  {
117  window->clickX = GET_X_LPARAM(lParam);
118  window->clickY = GET_Y_LPARAM(lParam);
119 
120  window->clickButton = vpMouseButton::button2;
121  vpReleaseSemaphore(window->semaClick,1,NULL);
122  }
123  break;
124 
125  case WM_RBUTTONDOWN:
126  {
127  window->clickX = GET_X_LPARAM(lParam);
128  window->clickY = GET_Y_LPARAM(lParam);
129 
130  window->clickButton = vpMouseButton::button3;
131  vpReleaseSemaphore(window->semaClick,1,NULL);
132  }
133  break;
134 
135  case WM_LBUTTONUP:
136  {
137  window->clickXUp = GET_X_LPARAM(lParam);
138  window->clickYUp = GET_Y_LPARAM(lParam);
139 
140  window->clickButtonUp = vpMouseButton::button1;
141  vpReleaseSemaphore(window->semaClickUp,1,NULL);
142  }
143  break;
144 
145  case WM_MBUTTONUP:
146  {
147  window->clickXUp = GET_X_LPARAM(lParam);
148  window->clickYUp = GET_Y_LPARAM(lParam);
149 
150  window->clickButtonUp = vpMouseButton::button2;
151  vpReleaseSemaphore(window->semaClickUp,1,NULL);
152  }
153  break;
154 
155  case WM_RBUTTONUP:
156  {
157  window->clickXUp = GET_X_LPARAM(lParam);
158  window->clickYUp = GET_Y_LPARAM(lParam);
159 
160  window->clickButtonUp = vpMouseButton::button3;
161  vpReleaseSemaphore(window->semaClickUp,1,NULL);
162  }
163  break;
164  case WM_MOUSEMOVE:
165  {
166  window->coordX = GET_X_LPARAM(lParam);
167  window->coordY = GET_Y_LPARAM(lParam);
168  vpReleaseSemaphore(window->semaMove,1,NULL);
169  }
170  break;
171 
172  case WM_SYSKEYDOWN:
173  // case WM_SYSKEYUP:
174  case WM_KEYDOWN:
175  //case WM_KEYUP:
176  {
177  GetKeyNameText((LONG)lParam, window->lpString, 10); // 10 is the size of lpString
178  //window->key = MapVirtualKey(wParam, MAPVK_VK_TO_CHAR);
179  vpReleaseSemaphore(window->semaKey,1,NULL);
180  break;
181  }
182 
183  case WM_COMMAND:
184 
185  break;
186 
187  //we must prevent the window from erasing the background each time a
188  //repaint is needed
189  case WM_ERASEBKGND:
190  return (LRESULT)1;
191 
192  case WM_PAINT:
193  //render the display
194  window->renderer->render();
195  break;
196 
197  case vpWM_CLOSEDISPLAY:
198  //cleanup code here, if needed
199  //destroys the window
200  DestroyWindow(hWnd);
201  break;
202 
203  case WM_DESTROY:
204  PostQuitMessage(0);
205  break;
206  default:
207  return DefWindowProc(hWnd, message, wParam, lParam);
208  }
209  return 0;
210 }
211 
215 vpWin32Window::vpWin32Window(vpWin32Renderer * rend): initialized(false)
216 {
217  renderer = rend;
218 
219  //registered is static member class and is initialized at the beginning of this file (registered = false)
220 
221  //creates the semaphores
222  semaInit = CreateSemaphore(NULL,0,1,NULL);
223  semaClick = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
224  semaClickUp = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
225  semaKey = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
226  semaMove = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
227 
228 }
229 
233 vpWin32Window::~vpWin32Window()
234 {
235  delete renderer;
236  CloseHandle(semaInit);
237  CloseHandle(semaClick);
238  CloseHandle(semaClickUp);
239  CloseHandle(semaKey);
240  CloseHandle(semaMove);
241 }
242 
243 
254 void vpWin32Window::initWindow(const char* title, int posx, int posy, unsigned int w, unsigned int h)
255 {
256  //the window's style
257  DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
258  const char g_szClassName[] = "ViSPWindowClass";
259 
260  RECT rect;
261  rect.left=0;
262  rect.right=static_cast<int>(w);
263  rect.top=0;
264  rect.bottom=static_cast<int>(h);
265 
266  //now we register the window's class
267  WNDCLASSEX wcex;
268 
269  wcex.cbSize = sizeof(WNDCLASSEX);
270 
271  wcex.style = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE ;
272  wcex.lpfnWndProc = (WNDPROC)WndProc;
273  wcex.cbClsExtra = 0;
274  wcex.cbWndExtra = 0;
275  wcex.hInstance = hInst;
276  wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
277  wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
278  wcex.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
279  wcex.lpszMenuName = NULL;
280  wcex.lpszClassName = g_szClassName;
281  wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
282 
283  RegisterClassEx(&wcex);
284 
285  AdjustWindowRectEx(&rect, style, false, style);
286  // std::cout << "win client size (orig)(w,h): " << rect.left << " " << rect.top << " " << rect.right << " " << rect.bottom << std::endl;
287 
288  //creates the window
289  hWnd = CreateWindowEx(WS_EX_APPWINDOW, g_szClassName, title, style,
290  posx, posy, rect.right-rect.left, rect.bottom-rect.top, NULL, NULL, hInst, NULL);
291  if (hWnd == NULL)
292  {
293  DWORD err= GetLastError();
294  std::cout << "err CreateWindowEx=" << err << std::endl;
296  "Can't create the window!");
297  }
298  SetWindowPos(hWnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
299 
300  //needed if we want to access it from the callback method (message handler)
301  window = this;
302 
303  //initialize the renderer
304  renderer->init(hWnd, w, h);
305 
306  //displays the window
307  ShowWindow(hWnd, SW_SHOWDEFAULT);
308  //ShowWindow(hWnd, SW_SHOW);
309  UpdateWindow(hWnd);
310 
311  MSG msg;
312 
313  //starts the message loop
314  while (true)
315  {
316  BOOL val = GetMessage(&msg, NULL, 0, 0);
317  if(val==-1){
318  std::cout << "GetMessage error:" << GetLastError() << std::endl;
319  break;
320  }else if(val==0){
321  break;
322  }else{
323  if (!TranslateAccelerator(msg.hwnd, NULL, &msg))
324  {
325  TranslateMessage(&msg);
326  DispatchMessage(&msg);
327  }
328  }
329  }
330 }
331 
332 #endif
333 #elif !defined(VISP_BUILD_SHARED_LIBS)
334 // Work arround to avoid warning: libvisp_core.a(vpWin32Window.cpp.o) has no symbols
335 void dummy_vpWin32Window() {};
336 #endif
vpWin32Window window
The window.
Error that can be emited by the vpDisplay class and its derivates.