Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpWin32Window.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 __thread 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 
232 vpWin32Window::~vpWin32Window()
233 {
234  delete renderer;
235  CloseHandle(semaInit);
236  CloseHandle(semaClick);
237  CloseHandle(semaClickUp);
238  CloseHandle(semaKey);
239  CloseHandle(semaMove);
240 }
241 
242 
253 void vpWin32Window::initWindow(const char* title, int posx, int posy, unsigned int w, unsigned int h)
254 {
255  //the window's style
256  DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
257  const char g_szClassName[] = "ViSPWindowClass";
258 
259  RECT rect;
260  rect.left=0;
261  rect.right=static_cast<int>(w);
262  rect.top=0;
263  rect.bottom=static_cast<int>(h);
264 
265  //now we register the window's class
266  WNDCLASSEX wcex;
267 
268  wcex.cbSize = sizeof(WNDCLASSEX);
269 
270  wcex.style = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE ;
271  wcex.lpfnWndProc = (WNDPROC)WndProc;
272  wcex.cbClsExtra = 0;
273  wcex.cbWndExtra = 0;
274  wcex.hInstance = hInst;
275  wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
276  wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
277  wcex.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
278  wcex.lpszMenuName = NULL;
279  wcex.lpszClassName = g_szClassName;
280  wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
281 
282  RegisterClassEx(&wcex);
283 
284  AdjustWindowRectEx(&rect, style, false, style);
285  // std::cout << "win client size (orig)(w,h): " << rect.left << " " << rect.top << " " << rect.right << " " << rect.bottom << std::endl;
286 
287  //creates the window
288  hWnd = CreateWindowEx(WS_EX_APPWINDOW, g_szClassName, title, style,
289  posx, posy, rect.right-rect.left, rect.bottom-rect.top, NULL, NULL, hInst, NULL);
290  if (hWnd == NULL)
291  {
292  DWORD err= GetLastError();
293  std::cout << "err CreateWindowEx=" << err << std::endl;
295  "Can't create the window!");
296  }
297  SetWindowPos(hWnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
298 
299  //needed if we want to access it from the callback method (message handler)
300  window = this;
301 
302  //initialize the renderer
303  renderer->init(hWnd, w, h);
304 
305  //displays the window
306  ShowWindow(hWnd, SW_SHOWDEFAULT);
307  //ShowWindow(hWnd, SW_SHOW);
308  UpdateWindow(hWnd);
309 
310  MSG msg;
311 
312  //starts the message loop
313  while (true)
314  {
315  BOOL val = GetMessage(&msg, NULL, 0, 0);
316  if(val==-1){
317  std::cout << "GetMessage error:" << GetLastError() << std::endl;
318  break;
319  }else if(val==0){
320  break;
321  }else{
322  if (!TranslateAccelerator(msg.hwnd, NULL, &msg))
323  {
324  TranslateMessage(&msg);
325  DispatchMessage(&msg);
326  }
327  }
328  }
329 }
330 
331 #endif
332 #elif !defined(VISP_BUILD_SHARED_LIBS)
333 // Work arround to avoid warning: libvisp_core.a(vpWin32Window.cpp.o) has no symbols
334 void dummy_vpWin32Window() {};
335 #endif
vpWin32Window window
The window.
Error that can be emited by the vpDisplay class and its derivates.