ViSP  2.9.0
vpWin32Window.cpp
1 /****************************************************************************
2  *
3  * $Id: vpWin32Window.cpp 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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 #include <visp/vpConfig.h>
43 #include <iostream>
44 #include <visp/vpWin32API.h>
45 
46 #if ( defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) )
47 
48 #define MAX_SEM_COUNT 2147483647
49 
50 #ifndef DOXYGEN_SHOULD_SKIP_THIS
51 
52 #include <visp/vpWin32Window.h>
53 
54 //Should be already defined ...
55 #ifndef GET_X_LPARAM
56 # define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
57 #endif
58 
59 #ifndef GET_Y_LPARAM
60 # define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
61 #endif
62 
63 //declares the window as thread local
64 //allows multiple displays
65 _declspec(thread) vpWin32Window * window;
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 
83  switch (message)
84  {
85  case vpWM_DISPLAY:
86  //redraw the whole window
87  InvalidateRect(window->getHWnd(), NULL, TRUE);
88  UpdateWindow(window->getHWnd());
89  break;
90 
91  case vpWM_DISPLAY_ROI:
92  {
93  RECT rect;
94  typedef struct _half_rect_t{
95  unsigned short left_top;
96  unsigned short right_bottom;
97  } half_rect_t;
98 
99  half_rect_t hr1;
100  half_rect_t hr2;
101 
102  hr1 = *((half_rect_t*)(&wParam));
103  hr2 = *((half_rect_t*)(&lParam));
104 
105  rect.left = hr1.left_top;
106  rect.right = hr1.right_bottom;
107 
108  rect.top = hr2.left_top;
109  rect.bottom = hr2.right_bottom;
110 
111  InvalidateRect(window->getHWnd(), &rect, TRUE);
112  UpdateWindow(window->getHWnd());
113  }
114  break;
115 
116  case WM_LBUTTONDOWN:
117  {
118  window->clickX = GET_X_LPARAM(lParam);
119  window->clickY = GET_Y_LPARAM(lParam);
120 
121  window->clickButton = vpMouseButton::button1;
122  vpReleaseSemaphore(window->semaClick,1,NULL);
123  }
124  break;
125 
126  case WM_MBUTTONDOWN:
127  {
128  window->clickX = GET_X_LPARAM(lParam);
129  window->clickY = GET_Y_LPARAM(lParam);
130 
131  window->clickButton = vpMouseButton::button2;
132  vpReleaseSemaphore(window->semaClick,1,NULL);
133  }
134  break;
135 
136  case WM_RBUTTONDOWN:
137  {
138  window->clickX = GET_X_LPARAM(lParam);
139  window->clickY = GET_Y_LPARAM(lParam);
140 
141  window->clickButton = vpMouseButton::button3;
142  vpReleaseSemaphore(window->semaClick,1,NULL);
143  }
144  break;
145 
146  case WM_LBUTTONUP:
147  {
148  window->clickXUp = GET_X_LPARAM(lParam);
149  window->clickYUp = GET_Y_LPARAM(lParam);
150 
151  window->clickButtonUp = vpMouseButton::button1;
152  vpReleaseSemaphore(window->semaClickUp,1,NULL);
153  }
154  break;
155 
156  case WM_MBUTTONUP:
157  {
158  window->clickXUp = GET_X_LPARAM(lParam);
159  window->clickYUp = GET_Y_LPARAM(lParam);
160 
161  window->clickButtonUp = vpMouseButton::button2;
162  vpReleaseSemaphore(window->semaClickUp,1,NULL);
163  }
164  break;
165 
166  case WM_RBUTTONUP:
167  {
168  window->clickXUp = GET_X_LPARAM(lParam);
169  window->clickYUp = GET_Y_LPARAM(lParam);
170 
171  window->clickButtonUp = vpMouseButton::button3;
172  vpReleaseSemaphore(window->semaClickUp,1,NULL);
173  }
174  break;
175  case WM_MOUSEMOVE:
176  {
177  window->coordX = GET_X_LPARAM(lParam);
178  window->coordY = GET_Y_LPARAM(lParam);
179  vpReleaseSemaphore(window->semaMove,1,NULL);
180  }
181  break;
182 
183  case WM_SYSKEYDOWN:
184  // case WM_SYSKEYUP:
185  case WM_KEYDOWN:
186  //case WM_KEYUP:
187  {
188  GetKeyNameText((LONG)lParam, window->lpString, 10); // 10 is the size of lpString
189  //window->key = MapVirtualKey(wParam, MAPVK_VK_TO_CHAR);
190  vpReleaseSemaphore(window->semaKey,1,NULL);
191  break;
192  }
193 
194  case WM_COMMAND:
195 
196  break;
197 
198  //we must prevent the window from erasing the background each time a
199  //repaint is needed
200  case WM_ERASEBKGND:
201  return (LRESULT)1;
202 
203  case WM_PAINT:
204  //render the display
205  window->renderer->render();
206  break;
207 
208  case vpWM_CLOSEDISPLAY:
209  //cleanup code here, if needed
210  //destroys the window
211  DestroyWindow(hWnd);
212  break;
213 
214  case WM_DESTROY:
215  PostQuitMessage(0);
216  break;
217  default:
218  return DefWindowProc(hWnd, message, wParam, lParam);
219  }
220  return 0;
221 }
222 
226 vpWin32Window::vpWin32Window(vpWin32Renderer * rend): initialized(false)
227 {
228  renderer = rend;
229 
230  //registered is static member class and is initialized at the beginning of this file (registered = false)
231 
232  //creates the semaphores
233  semaInit = CreateSemaphore(NULL,0,1,NULL);
234  semaClick = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
235  semaClickUp = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
236  semaKey = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
237  semaMove = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
238 
239 }
240 
244 vpWin32Window::~vpWin32Window()
245 {
246  delete renderer;
247  CloseHandle(semaInit);
248  CloseHandle(semaClick);
249  CloseHandle(semaClickUp);
250  CloseHandle(semaKey);
251  CloseHandle(semaMove);
252 }
253 
254 
265 void vpWin32Window::initWindow(const char* title, int posx, int posy, unsigned int w, unsigned int h)
266 {
267  //the window's style
268  DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
269  const char g_szClassName[] = "ViSPWindowClass";
270 
271  RECT rect;
272  rect.left=0;
273  rect.right=static_cast<int>(w);
274  rect.top=0;
275  rect.bottom=static_cast<int>(h);
276 
277  //now we register the window's class
278  WNDCLASSEX wcex;
279 
280  wcex.cbSize = sizeof(WNDCLASSEX);
281 
282  wcex.style = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE ;
283  wcex.lpfnWndProc = (WNDPROC)WndProc;
284  wcex.cbClsExtra = 0;
285  wcex.cbWndExtra = 0;
286  wcex.hInstance = hInst;
287  wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
288  wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
289  wcex.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
290  wcex.lpszMenuName = NULL;
291  wcex.lpszClassName = g_szClassName;
292  wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
293 
294  RegisterClassEx(&wcex);
295 
296  AdjustWindowRectEx(&rect, style, false, style);
297  // std::cout << "win client size (orig)(w,h): " << rect.left << " " << rect.top << " " << rect.right << " " << rect.bottom << std::endl;
298 
299  //creates the window
300  hWnd = CreateWindowEx(WS_EX_APPWINDOW, g_szClassName, title, style,
301  posx, posy, rect.right-rect.left, rect.bottom-rect.top, NULL, NULL, hInst, NULL);
302  if (hWnd == NULL)
303  {
304  DWORD err= GetLastError();
305  std::cout << "err CreateWindowEx=" << err << std::endl;
307  "Can't create the window!");
308  }
309  SetWindowPos(hWnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
310 
311  //needed if we want to access it from the callback method (message handler)
312  window = this;
313 
314  //initialize the renderer
315  renderer->init(hWnd, w, h);
316 
317  //displays the window
318  ShowWindow(hWnd, SW_SHOWDEFAULT);
319  //ShowWindow(hWnd, SW_SHOW);
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.