ViSP  2.10.0
vpWin32Window.cpp
1 /****************************************************************************
2  *
3  * $Id: vpWin32Window.cpp 5285 2015-02-09 14:32:54Z 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 #ifdef __MINGW32__
66 vpWin32Window * window;
67 #else
68 _declspec(thread) vpWin32Window * window;
69 #endif
70 
71 bool vpWin32Window::registered = false;
75 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
76 {
77  //the first time this callback is executed, put the window in initialized state
78  if (window != NULL)
79  {
80  if (!window->isInitialized())
81  {
82  window->initialized = true;
83  vpReleaseSemaphore(window->semaInit,1,NULL);
84  }
85  }
86 
87  switch (message)
88  {
89  case vpWM_DISPLAY:
90  //redraw the whole window
91  InvalidateRect(window->getHWnd(), NULL, TRUE);
92  UpdateWindow(window->getHWnd());
93  break;
94 
95  case vpWM_DISPLAY_ROI:
96  {
97  RECT rect;
98  typedef struct _half_rect_t{
99  unsigned short left_top;
100  unsigned short right_bottom;
101  } half_rect_t;
102 
103  half_rect_t hr1;
104  half_rect_t hr2;
105 
106  hr1 = *((half_rect_t*)(&wParam));
107  hr2 = *((half_rect_t*)(&lParam));
108 
109  rect.left = hr1.left_top;
110  rect.right = hr1.right_bottom;
111 
112  rect.top = hr2.left_top;
113  rect.bottom = hr2.right_bottom;
114 
115  InvalidateRect(window->getHWnd(), &rect, TRUE);
116  UpdateWindow(window->getHWnd());
117  }
118  break;
119 
120  case WM_LBUTTONDOWN:
121  {
122  window->clickX = GET_X_LPARAM(lParam);
123  window->clickY = GET_Y_LPARAM(lParam);
124 
125  window->clickButton = vpMouseButton::button1;
126  vpReleaseSemaphore(window->semaClick,1,NULL);
127  }
128  break;
129 
130  case WM_MBUTTONDOWN:
131  {
132  window->clickX = GET_X_LPARAM(lParam);
133  window->clickY = GET_Y_LPARAM(lParam);
134 
135  window->clickButton = vpMouseButton::button2;
136  vpReleaseSemaphore(window->semaClick,1,NULL);
137  }
138  break;
139 
140  case WM_RBUTTONDOWN:
141  {
142  window->clickX = GET_X_LPARAM(lParam);
143  window->clickY = GET_Y_LPARAM(lParam);
144 
145  window->clickButton = vpMouseButton::button3;
146  vpReleaseSemaphore(window->semaClick,1,NULL);
147  }
148  break;
149 
150  case WM_LBUTTONUP:
151  {
152  window->clickXUp = GET_X_LPARAM(lParam);
153  window->clickYUp = GET_Y_LPARAM(lParam);
154 
155  window->clickButtonUp = vpMouseButton::button1;
156  vpReleaseSemaphore(window->semaClickUp,1,NULL);
157  }
158  break;
159 
160  case WM_MBUTTONUP:
161  {
162  window->clickXUp = GET_X_LPARAM(lParam);
163  window->clickYUp = GET_Y_LPARAM(lParam);
164 
165  window->clickButtonUp = vpMouseButton::button2;
166  vpReleaseSemaphore(window->semaClickUp,1,NULL);
167  }
168  break;
169 
170  case WM_RBUTTONUP:
171  {
172  window->clickXUp = GET_X_LPARAM(lParam);
173  window->clickYUp = GET_Y_LPARAM(lParam);
174 
175  window->clickButtonUp = vpMouseButton::button3;
176  vpReleaseSemaphore(window->semaClickUp,1,NULL);
177  }
178  break;
179  case WM_MOUSEMOVE:
180  {
181  window->coordX = GET_X_LPARAM(lParam);
182  window->coordY = GET_Y_LPARAM(lParam);
183  vpReleaseSemaphore(window->semaMove,1,NULL);
184  }
185  break;
186 
187  case WM_SYSKEYDOWN:
188  // case WM_SYSKEYUP:
189  case WM_KEYDOWN:
190  //case WM_KEYUP:
191  {
192  GetKeyNameText((LONG)lParam, window->lpString, 10); // 10 is the size of lpString
193  //window->key = MapVirtualKey(wParam, MAPVK_VK_TO_CHAR);
194  vpReleaseSemaphore(window->semaKey,1,NULL);
195  break;
196  }
197 
198  case WM_COMMAND:
199 
200  break;
201 
202  //we must prevent the window from erasing the background each time a
203  //repaint is needed
204  case WM_ERASEBKGND:
205  return (LRESULT)1;
206 
207  case WM_PAINT:
208  //render the display
209  window->renderer->render();
210  break;
211 
212  case vpWM_CLOSEDISPLAY:
213  //cleanup code here, if needed
214  //destroys the window
215  DestroyWindow(hWnd);
216  break;
217 
218  case WM_DESTROY:
219  PostQuitMessage(0);
220  break;
221  default:
222  return DefWindowProc(hWnd, message, wParam, lParam);
223  }
224  return 0;
225 }
226 
230 vpWin32Window::vpWin32Window(vpWin32Renderer * rend): initialized(false)
231 {
232  renderer = rend;
233 
234  //registered is static member class and is initialized at the beginning of this file (registered = false)
235 
236  //creates the semaphores
237  semaInit = CreateSemaphore(NULL,0,1,NULL);
238  semaClick = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
239  semaClickUp = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
240  semaKey = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
241  semaMove = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
242 
243 }
244 
248 vpWin32Window::~vpWin32Window()
249 {
250  delete renderer;
251  CloseHandle(semaInit);
252  CloseHandle(semaClick);
253  CloseHandle(semaClickUp);
254  CloseHandle(semaKey);
255  CloseHandle(semaMove);
256 }
257 
258 
269 void vpWin32Window::initWindow(const char* title, int posx, int posy, unsigned int w, unsigned int h)
270 {
271  //the window's style
272  DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
273  const char g_szClassName[] = "ViSPWindowClass";
274 
275  RECT rect;
276  rect.left=0;
277  rect.right=static_cast<int>(w);
278  rect.top=0;
279  rect.bottom=static_cast<int>(h);
280 
281  //now we register the window's class
282  WNDCLASSEX wcex;
283 
284  wcex.cbSize = sizeof(WNDCLASSEX);
285 
286  wcex.style = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE ;
287  wcex.lpfnWndProc = (WNDPROC)WndProc;
288  wcex.cbClsExtra = 0;
289  wcex.cbWndExtra = 0;
290  wcex.hInstance = hInst;
291  wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
292  wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
293  wcex.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
294  wcex.lpszMenuName = NULL;
295  wcex.lpszClassName = g_szClassName;
296  wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
297 
298  RegisterClassEx(&wcex);
299 
300  AdjustWindowRectEx(&rect, style, false, style);
301  // std::cout << "win client size (orig)(w,h): " << rect.left << " " << rect.top << " " << rect.right << " " << rect.bottom << std::endl;
302 
303  //creates the window
304  hWnd = CreateWindowEx(WS_EX_APPWINDOW, g_szClassName, title, style,
305  posx, posy, rect.right-rect.left, rect.bottom-rect.top, NULL, NULL, hInst, NULL);
306  if (hWnd == NULL)
307  {
308  DWORD err= GetLastError();
309  std::cout << "err CreateWindowEx=" << err << std::endl;
311  "Can't create the window!");
312  }
313  SetWindowPos(hWnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
314 
315  //needed if we want to access it from the callback method (message handler)
316  window = this;
317 
318  //initialize the renderer
319  renderer->init(hWnd, w, h);
320 
321  //displays the window
322  ShowWindow(hWnd, SW_SHOWDEFAULT);
323  //ShowWindow(hWnd, SW_SHOW);
324  UpdateWindow(hWnd);
325 
326  MSG msg;
327 
328  //starts the message loop
329  while (true)
330  {
331  BOOL val = GetMessage(&msg, NULL, 0, 0);
332  if(val==-1){
333  std::cout << "GetMessage error:" << GetLastError() << std::endl;
334  break;
335  }else if(val==0){
336  break;
337  }else{
338  if (!TranslateAccelerator(msg.hwnd, NULL, &msg))
339  {
340  TranslateMessage(&msg);
341  DispatchMessage(&msg);
342  }
343  }
344  }
345 }
346 
347 #endif
348 #endif
Error that can be emited by the vpDisplay class and its derivates.