g4tools  5.4.0
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
tools::Windows::glarea Class Reference
Inheritance diagram for tools::Windows::glarea:
Inheritance graph
[legend]
Collaboration diagram for tools::Windows::glarea:
Collaboration graph
[legend]

Public Member Functions

virtual void resize (unsigned int, unsigned int)
 
virtual void paint (unsigned int a_w, unsigned int a_h)
 
virtual void close ()
 
virtual void left_button_up (unsigned int a_x, unsigned int a_y)
 
virtual void left_button_down (unsigned int a_x, unsigned int a_y)
 
virtual void mouse_move (unsigned int a_x, unsigned int a_y, bool)
 
 glarea (HWND a_parent)
 
virtual ~glarea ()
 
void set_client_area_size (unsigned int a_w, unsigned int a_h)
 
HWND hwnd () const
 
void post_WM_PAINT () const
 
void send_WM_PAINT () const
 
void wm_paint ()
 
void set_device_interactor (tools::sg::device_interactor *a_interactor)
 

Protected Member Functions

 glarea (const glarea &a_from)
 
glareaoperator= (const glarea &)
 
bool is_touch_event ()
 
tools::key_code convert (WPARAM a_key)
 

Static Protected Member Functions

static LRESULT CALLBACK proc (HWND a_hwnd, UINT a_msg, WPARAM a_wparam, LPARAM a_lparam)
 
static bool SetWindowPixelFormat (HDC a_HDC)
 
static void wm__destroy (HWND a_hwnd)
 

Protected Attributes

HWND m_parent
 
HWND m_hwnd
 
HGLRC m_context
 
HDC m_HDC
 
bool m_touch_available
 
tools::sg::device_interactorm_interactor
 

Detailed Description

Definition at line 35 of file glarea.

Constructor & Destructor Documentation

◆ glarea() [1/2]

tools::Windows::glarea::glarea ( HWND  a_parent)
inline

Definition at line 67 of file glarea.

68  :m_parent(a_parent)
69  ,m_hwnd(0)
70  ,m_context(0)
71  ,m_HDC(0)
72  ,m_touch_available(false)
73  ,m_interactor(0)
74  {
75  register_class();
76  // The WS_BORDER is needed. Else probleme of size at startup.
77  RECT rect;
78  ::GetClientRect(m_parent,&rect);
79  //printf("debug : glarea : ca : %d %d\n",rect.right-rect.left,rect.bottom-rect.top);
80  //inlib::log_file::get().writef("debug : glarea : ca : %d %d\n",rect.right-rect.left,rect.bottom-rect.top);
81  m_hwnd = ::CreateWindow(s_class().c_str(),
82  NULL,
83  WS_CHILD | WS_VISIBLE,
84  0,0,
85  rect.right-rect.left,
86  rect.bottom-rect.top,
87  m_parent,NULL,
88  GetWindowInstance(m_parent),
89  NULL);
90  if(!m_hwnd) return;
91  ::SetWindowLongPtr(m_hwnd,GWLP_USERDATA,LONG_PTR(this));
92 
93  // initialize OpenGL rendering :
94  m_HDC = ::GetDC(m_hwnd);
96  m_context = ::wglCreateContext(m_HDC);
97  }
98 
99 #ifdef TOOLS_WINDOWS_TOUCH
100  {BYTE digitizer_status = (BYTE)::GetSystemMetrics(SM_DIGITIZER);
101  if((digitizer_status & (0x80 + 0x40)) == 0) {
102  m_touch_available = false;
103  } else {
104  //BYTE nInputs = (BYTE)::GetSystemMetrics(SM_MAXIMUMTOUCHES);
105  m_touch_available = true;
106  if(!::RegisterTouchWindow(m_hwnd,0)) m_touch_available = false;
107  }}
108 #endif
109 
110  }

◆ ~glarea()

virtual tools::Windows::glarea::~glarea ( )
inlinevirtual

Definition at line 111 of file glarea.

111  {
112  if(::wglGetCurrentContext()!=NULL) ::wglMakeCurrent(NULL,NULL);
113  if(m_context) {
114  ::wglDeleteContext(m_context);
115  m_context = 0;
116  }
117  if(m_HDC && m_hwnd) ::ReleaseDC(m_hwnd,m_HDC);
118  if(m_hwnd) {
119  ::SetWindowLongPtr(m_hwnd,GWLP_USERDATA,LONG_PTR(NULL));
120  ::DestroyWindow(m_hwnd);
121  m_hwnd = 0;
122  }
123  }

◆ glarea() [2/2]

tools::Windows::glarea::glarea ( const glarea a_from)
inlineprotected

Definition at line 125 of file glarea.

126  :m_parent(a_from.m_parent)
127  ,m_hwnd(0)
128  ,m_context(0)
129  ,m_HDC(0)
130  ,m_touch_available(a_from.m_touch_available)
131  ,m_interactor(0)
132  {
133  if(!m_parent) return;
134  register_class();
135  RECT rect;
136  ::GetClientRect(m_parent,&rect);
137  m_hwnd = ::CreateWindow(s_class().c_str(),
138  NULL,
139  WS_CHILD | WS_VISIBLE,
140  0,0,
141  rect.right-rect.left,
142  rect.bottom-rect.top,
143  m_parent,NULL,
144  GetWindowInstance(m_parent),
145  NULL);
146  if(!m_hwnd) return;
147  ::SetWindowLongPtr(m_hwnd,GWLP_USERDATA,LONG_PTR(this));
148 
149  // initialize OpenGL rendering :
150  m_HDC = ::GetDC(m_hwnd);
151  if(m_HDC && SetWindowPixelFormat(m_HDC) ) {
152  m_context = ::wglCreateContext(m_HDC);
153  }
154 
155 #ifdef TOOLS_WINDOWS_TOUCH
156  if(a_from.m_touch_available) {
157  if(!::RegisterTouchWindow(m_hwnd,0)) m_touch_available = false;
158  }
159 #endif
160  }

Member Function Documentation

◆ close()

virtual void tools::Windows::glarea::close ( )
inlinevirtual

Definition at line 61 of file glarea.

61 {}

◆ convert()

tools::key_code tools::Windows::glarea::convert ( WPARAM  a_key)
inlineprotected

Definition at line 403 of file glarea.

403  {
404  if(a_key==VK_SHIFT) return tools::sg::key_shift();
405  return (tools::key_code)a_key;
406  }

◆ hwnd()

HWND tools::Windows::glarea::hwnd ( ) const
inline

Definition at line 169 of file glarea.

169 {return m_hwnd;}

◆ is_touch_event()

bool tools::Windows::glarea::is_touch_event ( )
inlineprotected

Definition at line 201 of file glarea.

201  {
202 #ifdef TOOLS_WINDOWS_TOUCH
203  if(!m_touch_available) return false;
204  return is_message_touch_event();
205 #else
206  return false;
207 #endif
208  }

◆ left_button_down()

virtual void tools::Windows::glarea::left_button_down ( unsigned int  a_x,
unsigned int  a_y 
)
inlinevirtual

Definition at line 64 of file glarea.

64 {}

◆ left_button_up()

virtual void tools::Windows::glarea::left_button_up ( unsigned int  a_x,
unsigned int  a_y 
)
inlinevirtual

Definition at line 63 of file glarea.

63 {}

◆ mouse_move()

virtual void tools::Windows::glarea::mouse_move ( unsigned int  a_x,
unsigned int  a_y,
bool   
)
inlinevirtual

Definition at line 65 of file glarea.

65 {}

◆ operator=()

glarea& tools::Windows::glarea::operator= ( const glarea )
inlineprotected

Definition at line 162 of file glarea.

162 {return *this;}

◆ paint()

virtual void tools::Windows::glarea::paint ( unsigned int  a_w,
unsigned int  a_h 
)
inlinevirtual

Reimplemented in tools::Windows::sg_viewer::_glarea.

Definition at line 60 of file glarea.

60 {}

◆ post_WM_PAINT()

void tools::Windows::glarea::post_WM_PAINT ( ) const
inline

Definition at line 170 of file glarea.

170  {
171  ::PostMessage(m_hwnd,WM_PAINT,(WPARAM)0,(LPARAM)0);
172  }

◆ proc()

static LRESULT CALLBACK tools::Windows::glarea::proc ( HWND  a_hwnd,
UINT  a_msg,
WPARAM  a_wparam,
LPARAM  a_lparam 
)
inlinestaticprotected

Definition at line 210 of file glarea.

210  {
211  switch (a_msg) {
212  case WM_SIZE:{
213  int width = LOWORD(a_lparam);
214  int height = HIWORD(a_lparam);
215  glarea* _this = (glarea*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
216  if(_this) {
217  _this->resize(width,height);
218  } else {
219  // CreateWindow send a WM_SIZE but GWLP_USERDATA not yet set.
220  }
221  }return 0;
222  case WM_PAINT:{
223  glarea* _this = (glarea*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
224  if(_this) _this->wm_paint();
225  }return 0;
226 
227  case WM_KEYDOWN:{
228  glarea* _this = (glarea*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
229  if(_this) {
230  if(_this->m_interactor) {
231  tools::sg::key_down_event event(_this->convert(a_wparam));
232  _this->m_interactor->key_press(event);
233  }
234  }
235  } return 0;
236  case WM_KEYUP:{
237  glarea* _this = (glarea*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
238  if(_this) {
239  if(_this->m_interactor) {
240  tools::sg::key_up_event event(_this->convert(a_wparam));
241  _this->m_interactor->key_release(event);
242  }
243  }
244  } return 0;
245 
246  case WM_LBUTTONDOWN:{
247  glarea* _this = (glarea*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
248  if(_this) {
249  if(_this->m_interactor) {
250  tools::sg::mouse_down_event event(LOWORD(a_lparam),HIWORD(a_lparam));
251  _this->m_interactor->mouse_press(event);
252  } else {
253  RECT rect;
254  ::GetClientRect(a_hwnd,&rect);
255  unsigned int h = rect.bottom-rect.top;
256  if(!_this->is_touch_event()) _this->left_button_down(LOWORD(a_lparam),h-HIWORD(a_lparam));
257  }
258  }
259  }return 0;
260  case WM_LBUTTONUP:{
261  glarea* _this = (glarea*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
262  if(_this) {
263  if(_this->m_interactor) {
264  tools::sg::mouse_up_event event(LOWORD(a_lparam),HIWORD(a_lparam));
265  _this->m_interactor->mouse_release(event);
266  } else {
267  RECT rect;
268  ::GetClientRect(a_hwnd,&rect);
269  unsigned int h = rect.bottom-rect.top;
270  if(!_this->is_touch_event()) _this->left_button_up(LOWORD(a_lparam),h-HIWORD(a_lparam));
271  }
272  }
273  } return 0;
274  case WM_MOUSEMOVE:{
275  glarea* _this = (glarea*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
276  if(_this) {
277  if(_this->m_interactor) {
278  tools::sg::mouse_move_event event(LOWORD(a_lparam),HIWORD(a_lparam),0,0,false);
279  _this->m_interactor->mouse_move(event);
280  } else {
281  WPARAM state = a_wparam;
282  bool ldown = ((state & MK_LBUTTON)==MK_LBUTTON)?true:false;
283  RECT rect;
284  ::GetClientRect(a_hwnd,&rect);
285  unsigned int h = rect.bottom-rect.top;
286  if(!_this->is_touch_event()) _this->mouse_move(LOWORD(a_lparam),h-HIWORD(a_lparam),ldown);
287  }
288  }
289 
290  }return 0;
291 
292  case WM_MOUSEWHEEL:{
293  glarea* _this = (glarea*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
294  if(_this) {
295  if(_this->m_interactor) {
296  tools::sg::wheel_rotate_event event(GET_WHEEL_DELTA_WPARAM(a_wparam));
297  _this->m_interactor->wheel_rotate(event);
298  }
299  }
300  } return 0;
301 
302 #ifdef TOOLS_WINDOWS_TOUCH
303  case WM_TOUCH:{
304  glarea* _this = (glarea*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
305  if(_this && _this->m_touch_available) {
306  RECT rect;
307  //::GetWindowRect(hwnd,&rect); ???
308  ::GetClientRect(a_hwnd,&rect);
309  unsigned int h = rect.bottom-rect.top;
310 
311  unsigned int num_inputs = (int)a_wparam;
312  //::printf("debug : WM_TOUCH : 001 : %d\n",num_inputs);
313  TOUCHINPUT* ti = new TOUCHINPUT[num_inputs];
314  POINT p;
315  if(::GetTouchInputInfo((HTOUCHINPUT)a_lparam,num_inputs,ti,sizeof(TOUCHINPUT))) {
316  for(unsigned int i=0;i<num_inputs; ++i) {
317  p.x = TOUCH_COORD_TO_PIXEL(ti[i].x);
318  p.y = TOUCH_COORD_TO_PIXEL(ti[i].y);
319  if(!::ScreenToClient(a_hwnd,&p)) {}
320  if(ti[i].dwFlags & TOUCHEVENTF_DOWN) {
321  //::printf("debug : TOUCHEVENTF_DOWN %lu %lu\n",p.x,p.y);
322  _this->left_button_down(p.x,h-p.y);
323  } else if (ti[i].dwFlags & TOUCHEVENTF_UP) {
324  //::printf("debug : TOUCHEVENTF_UP %lu %lu\n",p.x,p.y);
325  _this->left_button_up(p.x,h-p.y);
326  } else if (ti[i].dwFlags & TOUCHEVENTF_MOVE) {
327  bool ldown = true; //we assume that a TOUCHEVENT_DOWN had been done.
328  //::printf("debug : TOUCHEVENTF_MOVE %lu %lu\n",p.x,p.y);
329  _this->mouse_move(p.x,h-p.y,ldown);
330  }
331  }
332  }
333  ::CloseTouchInputHandle((HTOUCHINPUT)a_lparam);
334  delete [] ti;
335  }
336  }return 0;
337 #endif //TOOLS_WINDOWS_TOUCH
338  case WM_DESTROY:wm__destroy(a_hwnd);return 0;
339  }
340  return (DefWindowProc(a_hwnd,a_msg,a_wparam,a_lparam));
341  }

◆ resize()

virtual void tools::Windows::glarea::resize ( unsigned int  ,
unsigned int   
)
inlinevirtual

Definition at line 59 of file glarea.

59 {}

◆ send_WM_PAINT()

void tools::Windows::glarea::send_WM_PAINT ( ) const
inline

Definition at line 173 of file glarea.

173  {
174  ::SendMessage(m_hwnd,WM_PAINT,(WPARAM)0,(LPARAM)0);
175  }

◆ set_client_area_size()

void tools::Windows::glarea::set_client_area_size ( unsigned int  a_w,
unsigned int  a_h 
)
inline

Definition at line 164 of file glarea.

164  {
165  RECT rect;
166  ::GetClientRect(m_hwnd,&rect);
167  ::MoveWindow(m_hwnd,rect.left,rect.top,a_w,a_h,TRUE);
168  }

◆ set_device_interactor()

void tools::Windows::glarea::set_device_interactor ( tools::sg::device_interactor a_interactor)
inline

Definition at line 199 of file glarea.

199 {m_interactor = a_interactor;} //we do not have ownership.

◆ SetWindowPixelFormat()

static bool tools::Windows::glarea::SetWindowPixelFormat ( HDC  a_HDC)
inlinestaticprotected

Definition at line 342 of file glarea.

342  {
343  PIXELFORMATDESCRIPTOR pfd;
344  pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
345  pfd.nVersion = 1;
346  pfd.dwFlags =
347  PFD_DRAW_TO_WINDOW |
348  PFD_SUPPORT_OPENGL |
349  PFD_DOUBLEBUFFER |
350  PFD_STEREO_DONTCARE;
351  pfd.iPixelType = PFD_TYPE_RGBA;
352  pfd.cColorBits = 32;
353  pfd.cRedBits = 8;
354  pfd.cRedShift = 16;
355  pfd.cGreenBits = 8;
356  pfd.cGreenShift = 8;
357  pfd.cBlueBits = 8;
358  pfd.cBlueShift = 0;
359  pfd.cAlphaBits = 0;
360  pfd.cAlphaShift = 0;
361  pfd.cAccumBits = 64;
362  pfd.cAccumRedBits = 16;
363  pfd.cAccumGreenBits = 16;
364  pfd.cAccumBlueBits = 16;
365  pfd.cAccumAlphaBits = 0;
366  pfd.cDepthBits = 32;
367  pfd.cStencilBits = 8;
368  pfd.cAuxBuffers = 0;
369  pfd.iLayerType = PFD_MAIN_PLANE;
370  pfd.bReserved = 0;
371  pfd.dwLayerMask = 0;
372  pfd.dwVisibleMask = 0;
373  pfd.dwDamageMask = 0;
374 
375  int pixelIndex = ::ChoosePixelFormat(a_HDC,&pfd);
376  if (pixelIndex==0) {
377  // Let's choose a default index.
378  pixelIndex = 1;
379  if (::DescribePixelFormat(a_HDC,
380  pixelIndex,
381  sizeof(PIXELFORMATDESCRIPTOR),
382  &pfd)==0) {
383  return false;
384  }
385  }
386 
387  if (::SetPixelFormat(a_HDC,pixelIndex,&pfd)==FALSE) return false;
388 
389  return true;
390  }

◆ wm__destroy()

static void tools::Windows::glarea::wm__destroy ( HWND  a_hwnd)
inlinestaticprotected

Definition at line 391 of file glarea.

391  {
392  glarea* _this = (glarea*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
393  if(_this) { //How to be sure that we have a glarea* ???
394  _this->close();
395  if(_this->m_hwnd!=a_hwnd) {
396  //::printf("WinTk::Component::wm_destroy : HWND mismatch !\n");
397  }
398  _this->m_hwnd = 0;
399  }
400  ::SetWindowLongPtr(a_hwnd,GWLP_USERDATA,LONG_PTR(NULL));
401  }

◆ wm_paint()

void tools::Windows::glarea::wm_paint ( )
inline

Definition at line 176 of file glarea.

176  {
177  PAINTSTRUCT ps;
178  //HDC hDC =
179  BeginPaint(m_hwnd,&ps);
180  if(m_HDC && m_context) {
181  ::wglMakeCurrent(m_HDC,m_context);
182 
183  RECT rect;
184  ::GetClientRect(m_hwnd,&rect);
185  unsigned int w = rect.right-rect.left;
186  unsigned int h = rect.bottom-rect.top;
187 
188  //printf("debug : glarea : paint : %d %d\n",w,h);
189  //inlib::log_file::get().writef("debug : glarea : paint : %d %d\n",w,h);
190 
191  paint(w,h);
192 
193  ::SwapBuffers(m_HDC);
194  ::wglMakeCurrent(m_HDC,0);
195  }
196  EndPaint(m_hwnd,&ps);
197  }

Member Data Documentation

◆ m_context

HGLRC tools::Windows::glarea::m_context
protected

Definition at line 410 of file glarea.

◆ m_HDC

HDC tools::Windows::glarea::m_HDC
protected

Definition at line 411 of file glarea.

◆ m_hwnd

HWND tools::Windows::glarea::m_hwnd
protected

Definition at line 409 of file glarea.

◆ m_interactor

tools::sg::device_interactor* tools::Windows::glarea::m_interactor
protected

Definition at line 413 of file glarea.

◆ m_parent

HWND tools::Windows::glarea::m_parent
protected

Definition at line 408 of file glarea.

◆ m_touch_available

bool tools::Windows::glarea::m_touch_available
protected

Definition at line 412 of file glarea.


The documentation for this class was generated from the following file:
tools::Windows::glarea::paint
virtual void paint(unsigned int a_w, unsigned int a_h)
Definition: glarea:60
tools::key_code
unsigned int key_code
Definition: typedefs:131
tools::sg::key_shift
key_code key_shift()
Definition: keys:12
tools::Windows::glarea::m_touch_available
bool m_touch_available
Definition: glarea:412
tools::sg::key_up_event
Definition: event:350
tools::Windows::is_message_touch_event
bool is_message_touch_event()
Definition: glarea:26
tools::Windows::glarea::m_parent
HWND m_parent
Definition: glarea:408
tools::Windows::glarea::wm__destroy
static void wm__destroy(HWND a_hwnd)
Definition: glarea:391
tools::Windows::glarea::m_hwnd
HWND m_hwnd
Definition: glarea:409
tools::Windows::glarea::m_context
HGLRC m_context
Definition: glarea:410
tools::sg::key_down_event
Definition: event:312
tools::sg::mouse_up_event
Definition: event:150
tools::sg::mouse_down_event
Definition: event:107
tools::Windows::glarea::m_interactor
tools::sg::device_interactor * m_interactor
Definition: glarea:413
tools::Windows::glarea::glarea
glarea(HWND a_parent)
Definition: glarea:67
tools::Windows::glarea::m_HDC
HDC m_HDC
Definition: glarea:411
tools::sg::mouse_move_event
Definition: event:193
tools::Windows::glarea::SetWindowPixelFormat
static bool SetWindowPixelFormat(HDC a_HDC)
Definition: glarea:342
tools::sg::wheel_rotate_event
Definition: event:388