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