Win32MainWindow.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // Filename: Win32MainWindow.cpp
3 // Author: Florian Hecht
4 // Date: 2008
5 // ****************************************************************************
6 
7 
8 #include "Win32MainWindow.h"
10 
11 #include <windows.h>
12 #include <Windowsx.h>
13 #include <commctrl.h>
14 
15 #include "Image/ByteImage.h"
16 
17 
19 {
20  eImage = 0,
28 };
29 
31 {
33  int m_index; // index into the widgets array
34  HWND m_hWnd; // window handle
35 };
36 
38 {
39  HDC m_hDC; // device context of the widget
40  HDC m_hDCMem; // device context to store the current image
41  HBITMAP m_hBmp; // the bitmap with the image
42  HBITMAP m_hBmpOld; // the previous bitmap (to restore the state)
43  unsigned char *m_pPixels; // pointer to an upside-down BGR buffer
44  int m_iWidth; // width of the current bitmap
45  int m_iHeight; // height of the current bitmap
46 
47  const CByteImage *m_pImage; // the currently displayed image
48  unsigned char *m_buffer; // stores a copy of the upside-down BGR buffer
49  // it is used to draw the selection rectangle
50 
51  // mouse interaction
52  bool mouse_down;
53  unsigned int mouse_start_x;
54  unsigned int mouse_start_y;
55  unsigned int mouse_current_x;
56  unsigned int mouse_current_y;
57 };
58 
59 struct Win32GLWidget : public Win32Widget
60 {
61  HDC m_hDC; // device context of the widget
62  int m_iWidth; // width of the current bitmap
63  int m_iHeight; // height of the current bitmap
64  HGLRC m_hGLRC; // OpenGL context
65 };
66 
67 
68 // helper function
69 void DrawRect(unsigned char *img, int width, int height, int x0, int y0, int x1, int y1)
70 {
71  const int width_ = width % 2 == 0 ? width : width + 1;
72  int base0 = 3*(height - y0 - 1)*width_;
73  int base1 = 3*(height - y1)*width_;
74 
75  if (x1 >= width)
76  x1 = width-1;
77  if (y1 >= height)
78  y1 = height-1;
79 
80  for (int x = x0; x < x1; x++)
81  {
82  img[base0 + 3*x + 0] = 255;
83  img[base0 + 3*x + 1] = 0;
84  img[base0 + 3*x + 2] = 0;
85 
86  img[base1 + 3*x + 0] = 255;
87  img[base1 + 3*x + 1] = 0;
88  img[base1 + 3*x + 2] = 0;
89  }
90 
91  for (int y = (height - y1); y < (height - y0 - 1); y++)
92  {
93  img[3*y*width_ + 3*x0 + 0] = 255;
94  img[3*y*width_ + 3*x0 + 1] = 0;
95  img[3*y*width_ + 3*x0 + 2] = 0;
96 
97  img[3*y*width_ + 3*(x1-1) + 0] = 255;
98  img[3*y*width_ + 3*(x1-1) + 1] = 0;
99  img[3*y*width_ + 3*(x1-1) + 2] = 0;
100  }
101 }
102 
103 
104 #define DEFAULT_CLASS_NAME "MainWindow"
105 #define IMAGE_WINDOW_CLASS_NAME "ImageWindow"
106 
107 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
108 {
109  if (uMsg == WM_CREATE)
110  {
111  // extract the pointer to the object that created the window
112  CREATESTRUCT *cs = (CREATESTRUCT*)lParam;
113  CWin32MainWindow *ptr = (CWin32MainWindow*)cs->lpCreateParams;
114 
115  // store it in the user data of the window
116  SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) ptr);
117 
118  return 0;
119  }
120  else
121  {
122  // get the pointer to the object from the user data
123  CWin32MainWindow *ptr = (CWin32MainWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
124 
125  // if we got a valid pointer, call the WindowProc method of that object
126  if (ptr)
127  return ptr->WindowProc(hWnd, uMsg, wParam, lParam);
128  else
129  return DefWindowProc(hWnd, uMsg, wParam, lParam);
130  }
131 }
132 
133 
134 LRESULT CALLBACK CWin32MainWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
135 {
136  Win32Widget *w = FindWidget(hWnd);
137 
138  switch (uMsg)
139  {
140  case WM_ACTIVATE:
141  // check minimization state
142  //bActive = !HIWORD(wParam);
143  return 0;
144 
145  /*case WM_SYSCOMMAND:
146  switch (wParam)
147  {
148  case SC_SCREENSAVE:
149  case SC_MONITORPOWER:
150  return 0;
151  }
152  break;*/
153 
154  case WM_CLOSE:
155  m_quit_count++;
156  m_did_quit = true;
157 
158  if (m_quit_count >= m_ref_count)
159  PostQuitMessage(0);
160  else
161  ShowWindow(m_hWnd, SW_HIDE);
162 
163  return 0;
164 
165  case WM_LBUTTONDOWN:
166  {
167  if (w != NULL && w->m_eType == eImage)
168  {
170 
171  unsigned int x = GET_X_LPARAM(lParam);
172  unsigned int y = GET_Y_LPARAM(lParam);
173 
174  if (iw->mouse_down)
175  {
176  iw->mouse_down = false;
177  }
178  else
179  {
180  iw->mouse_start_x = x;
181  iw->mouse_start_y = y;
182  iw->mouse_current_x = iw->mouse_start_x;
183  iw->mouse_current_y = iw->mouse_start_y;
184 
185  if (x < (unsigned int) iw->m_iWidth && y < (unsigned int) iw->m_iHeight)
186  iw->mouse_down = true;
187  }
188  }
189 
190  if (w != NULL && (w->m_eType == eImage || w->m_eType == eGLWidget))
191  {
192  if (m_event_callback != NULL)
193  {
194  m_event_callback->MouseDown((WIDGET_HANDLE)w, IVT_LEFT_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
195  }
196 
197  SetFocus(hWnd);
198  }
199  }
200  break;
201 
202  case WM_RBUTTONDOWN:
203  {
204  if (w != NULL && (w->m_eType == eImage || w->m_eType == eGLWidget))
205  {
206  if (m_event_callback != NULL)
207  {
208  m_event_callback->MouseDown((WIDGET_HANDLE)w, IVT_RIGHT_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
209  }
210 
211  SetFocus(hWnd);
212  }
213  }
214  break;
215 
216  case WM_MBUTTONDOWN:
217  {
218  if (w != NULL && (w->m_eType == eImage || w->m_eType == eGLWidget))
219  {
220  if (m_event_callback != NULL)
221  {
222  m_event_callback->MouseDown((WIDGET_HANDLE)w, IVT_MIDDLE_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
223  }
224 
225  SetFocus(hWnd);
226  }
227  }
228  break;
229 
230  case WM_MOUSEMOVE:
231  {
232  if (w != NULL && w->m_eType == eImage)
233  {
235 
236  if (iw->mouse_down)
237  {
238  unsigned int old_curr_x = iw->mouse_current_x;
239  unsigned int old_curr_y = iw->mouse_current_y;
240 
241  unsigned int x = GET_X_LPARAM(lParam);
242  unsigned int y = GET_Y_LPARAM(lParam);
243 
244  if (x < (unsigned int) iw->m_iWidth && y < (unsigned int) iw->m_iHeight)
245  {
246  iw->mouse_current_x = x;
247  iw->mouse_current_y = y;
248 
249  // tell the window it has to redraw the area
250  RECT WindowRect;
251 
252  unsigned int x0 = iw->mouse_start_x;
253  unsigned int y0 = iw->mouse_start_y;
254  unsigned int x1 = iw->mouse_current_x;
255  unsigned int y1 = iw->mouse_current_y;
256 
257  if (x0 > x1)
258  {
259  unsigned int swap = x0;
260  x0 = x1;
261  x1 = swap;
262  }
263  if (y0 > y1)
264  {
265  unsigned int swap = y0;
266  y0 = y1;
267  y1 = swap;
268  }
269 
270  WindowRect.left = x0;
271  WindowRect.right = x1;
272  WindowRect.top = y0;
273  WindowRect.bottom = y1;
274 
275  InvalidateRect(iw->m_hWnd, &WindowRect, false);
276 
277  // also invalidate the previous rect
278  x0 = iw->mouse_start_x;
279  y0 = iw->mouse_start_y;
280  x1 = old_curr_x;
281  y1 = old_curr_y;
282 
283  if (x0 > x1)
284  {
285  unsigned int swap = x0;
286  x0 = x1;
287  x1 = swap;
288  }
289  if (y0 > y1)
290  {
291  unsigned int swap = y0;
292  y0 = y1;
293  y1 = swap;
294  }
295 
296  WindowRect.left = x0;
297  WindowRect.right = x1;
298  WindowRect.top = y0;
299  WindowRect.bottom = y1;
300 
301  InvalidateRect(iw->m_hWnd, &WindowRect, false);
302  }
303  }
304  }
305 
306  if (w != NULL && (w->m_eType == eImage || w->m_eType == eGLWidget))
307  {
308  if (m_event_callback != NULL)
309  {
310  m_event_callback->MouseMove((WIDGET_HANDLE)w, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
311  }
312  }
313  }
314 
315  break;
316 
317  case WM_LBUTTONUP:
318  {
319  if (w != NULL && w->m_eType == eImage)
320  {
322 
323  if (iw->mouse_down)
324  {
325  iw->mouse_down = false;
326 
327  if (m_event_callback != NULL)
328  {
329  if (iw->mouse_start_x == iw->mouse_current_x && iw->mouse_start_y == iw->mouse_current_y)
330  {
331  m_event_callback->PointClicked((WIDGET_HANDLE)w, iw->mouse_start_x, iw->mouse_start_y);
332  }
333  else
334  {
335  unsigned int x0 = iw->mouse_start_x;
336  unsigned int y0 = iw->mouse_start_y;
337  unsigned int x1 = iw->mouse_current_x;
338  unsigned int y1 = iw->mouse_current_y;
339 
340  if (x0 > x1)
341  {
342  unsigned int swap = x0;
343  x0 = x1;
344  x1 = swap;
345  }
346  if (y0 > y1)
347  {
348  unsigned int swap = y0;
349  y0 = y1;
350  y1 = swap;
351  }
352 
353  m_event_callback->RectSelected((WIDGET_HANDLE)w, x0, y0, x1, y1);
354  }
355  }
356 
357  memcpy(iw->m_pPixels, iw->m_buffer, 3*iw->m_iWidth*iw->m_iHeight);
358  InvalidateRect(iw->m_hWnd, NULL, false);
359  }
360  }
361 
362  if (w != NULL && (w->m_eType == eImage || w->m_eType == eGLWidget))
363  {
364  if (m_event_callback != NULL)
365  {
366  m_event_callback->MouseUp((WIDGET_HANDLE)w, IVT_LEFT_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
367  }
368  }
369  }
370  break;
371 
372  case WM_RBUTTONUP:
373  {
374  if (w != NULL && (w->m_eType == eImage || w->m_eType == eGLWidget))
375  {
376  if (m_event_callback != NULL)
377  {
378  m_event_callback->MouseUp((WIDGET_HANDLE)w, IVT_RIGHT_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
379  }
380  }
381  }
382  break;
383 
384  case WM_MBUTTONUP:
385  {
386  if (w != NULL && (w->m_eType == eImage || w->m_eType == eGLWidget))
387  {
388  if (m_event_callback != NULL)
389  {
390  m_event_callback->MouseUp((WIDGET_HANDLE)w, IVT_MIDDLE_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
391  }
392  }
393  }
394  break;
395 
396  case WM_KEYDOWN:
397  {
398  if (w != NULL && (w->m_eType == eImage || w->m_eType == eGLWidget))
399  {
400  if (m_event_callback != NULL)
401  {
402  char key[2] = {0, 0};
403  BYTE keys[256];
404 
405  GetKeyboardState(keys);
406 
407  int c = ToAscii((unsigned int) wParam, MapVirtualKey((unsigned int) wParam, 0), keys, (unsigned short*)key, 0);
408  if (c == 1)
409  m_event_callback->KeyDown((WIDGET_HANDLE)w, key[0]);
410  else
411  m_event_callback->KeyDown((WIDGET_HANDLE)w, 0);
412  }
413  }
414  }
415  break;
416 
417  case WM_KEYUP:
418  {
419  if (w != NULL && (w->m_eType == eImage || w->m_eType == eGLWidget))
420  {
421  if (m_event_callback != NULL)
422  {
423  char key[2] = {0, 0};
424  BYTE keys[256];
425 
426  GetKeyboardState(keys);
427 
428  int c = ToAscii((unsigned int) wParam, MapVirtualKey((unsigned int) wParam, 0), keys, (unsigned short*)key, 0);
429  if (c == 1)
430  m_event_callback->KeyUp((WIDGET_HANDLE)w, key[0]);
431  else
432  m_event_callback->KeyUp((WIDGET_HANDLE)w, 0);
433  }
434  }
435  }
436  break;
437 
438  case WM_COMMAND:
439  {
440  int idx = LOWORD(wParam); // the index into the widgets array
441  int cmd = HIWORD(wParam); // the index into the widgets array
442 
443  // make sure it's a valid index
444  const int nSize = (int) m_widgets.size();
445  if (idx != 0 && idx > 0 && idx <= nSize)
446  {
447  w = m_widgets[idx-1];
448 
449  if (w != NULL)
450  {
451  if (w->m_eType == eButton)
452  {
453  if (m_event_callback != NULL)
454  {
455  m_event_callback->ButtonPushed((WIDGET_HANDLE)w);
456  }
457 
458  return 0;
459  }
460  else if (w->m_eType == eCheckBox)
461  {
462  if (m_event_callback != NULL)
463  {
464  BOOL checked = IsDlgButtonChecked(m_hWnd, idx);
465 
466  m_event_callback->ValueChanged((WIDGET_HANDLE)w, (checked ? 1 : 0));
467  }
468 
469  return 0;
470  }
471  else if (w->m_eType == eTextEdit && cmd == EN_CHANGE)
472  {
473  if (m_event_callback != NULL)
474  {
475  m_event_callback->ValueChanged((WIDGET_HANDLE)w, -1);
476  }
477 
478  return 0;
479  }
480  else if (w->m_eType == eComboBox && cmd == CBN_SELCHANGE)
481  {
482  if (m_event_callback != NULL)
483  {
484  int value = (int) SendMessage(w->m_hWnd, CB_GETCURSEL, 0, 0);
485 
486  m_event_callback->ValueChanged((WIDGET_HANDLE)w, value);
487  }
488 
489  return 0;
490  }
491  }
492  }
493  }
494  break;
495 
496  case WM_NOTIFY:
497  {
498  int idx = (int) wParam; // the index into the widgets array
499  NMHDR *nmhdr = (LPNMHDR)lParam;
500 
501  // make sure it's a valid index
502  const int nSize = (int) m_widgets.size();
503  if (idx != 0 && idx > 0 && idx <= nSize)
504  {
505  w = m_widgets[idx-1];
506 
507  if (w != NULL)
508  {
509  if (w->m_eType == eSlider)
510  {
511  if (m_event_callback != NULL && nmhdr->code == -12 /* code for dragging */)
512  {
513  int value = (int) SendMessage(w->m_hWnd, TBM_GETPOS, 0, 0);
514 
515  m_event_callback->ValueChanged((WIDGET_HANDLE)w, value);
516  }
517 
518  return 0;
519  }
520  }
521  }
522  }
523  break;
524 
525  case WM_SIZE:
526  break;
527 
528  case WM_PAINT:
529  {
530  if (w != NULL && w->m_eType == eImage)
531  {
533 
534  if (iw->m_hBmp != 0)
535  {
536  PAINTSTRUCT ps;
537  HDC dc = BeginPaint(iw->m_hWnd, &ps);
538 
539  if (iw->mouse_down && (iw->mouse_start_x != iw->mouse_current_x || iw->mouse_start_y != iw->mouse_current_y))
540  {
541  unsigned int x0 = iw->mouse_start_x;
542  unsigned int y0 = iw->mouse_start_y;
543  unsigned int x1 = iw->mouse_current_x;
544  unsigned int y1 = iw->mouse_current_y;
545 
546  if (x0 > x1)
547  {
548  unsigned int swap = x0;
549  x0 = x1;
550  x1 = swap;
551  }
552  if (y0 > y1)
553  {
554  unsigned int swap = y0;
555  y0 = y1;
556  y1 = swap;
557  }
558 
559  memcpy(iw->m_pPixels, iw->m_buffer, 3*iw->m_iWidth*iw->m_iHeight);
560  DrawRect(iw->m_pPixels, iw->m_iWidth, iw->m_iHeight, x0, y0, x1, y1);
561  }
562 
563 
564  BitBlt(dc, 0, 0, iw->m_iWidth, iw->m_iHeight, iw->m_hDCMem, 0, 0, SRCCOPY);
565 
566  EndPaint(w->m_hWnd, &ps);
567 
568  return 0;
569  }
570  }
571  }
572 
573  break;
574  }
575 
576  // pass all unhandled messages to DefWindowProc
577  return DefWindowProc(hWnd, uMsg, wParam, lParam);
578 }
579 
582 
583 CWin32MainWindow::CWin32MainWindow(int x, int y, int width, int height, const char *title)
584 {
585  m_did_quit = false;
586 
587  WNDCLASS wc; // Windows Class Structure
588  DWORD dwExStyle; // Window Extended Style
589  DWORD dwStyle; // Window Style
590  RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
591 
592  WindowRect.left = x;
593  WindowRect.right = x + width;
594  WindowRect.top = y;
595  WindowRect.bottom = y + height;
596 
597  m_hInstance = GetModuleHandle(NULL);
598 
599  if (m_ref_count == 0)
600  {
601  wc.style = CS_HREDRAW | CS_VREDRAW; // | CS_OWNDC; // Redraw On Size, And Own DC For Window.
602  wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
603  wc.cbClsExtra = 0; // No Extra Class Data
604  wc.cbWndExtra = 8; // 4 bytes of Extra Window Data
605  wc.hInstance = m_hInstance; // Set The Instance
606  wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
607  wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
608  wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); // Background
609  wc.lpszMenuName = NULL; // We Don't Want A Menu
610  wc.lpszClassName = DEFAULT_CLASS_NAME; // Set The Class Name
611 
612  if (!RegisterClass(&wc))
613  {
614  MessageBox(NULL,"Failed To Register The MainWindow Class.", "ERROR", MB_OK|MB_ICONEXCLAMATION);
615  return;
616  }
617 
618 
619  wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
620  wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
621  wc.cbClsExtra = 0; // No Extra Class Data
622  wc.cbWndExtra = 8; // 4 bytes of Extra Window Data
623  wc.hInstance = m_hInstance; // Set The Instance
624  wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
625  wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
626  wc.hbrBackground = NULL; //GetSysColorBrush(COLOR_BTNFACE); // No Background Required For GL
627  wc.lpszMenuName = NULL; // We Don't Want A Menu
628  wc.lpszClassName = IMAGE_WINDOW_CLASS_NAME; // Set The Class Name
629 
630  if (!RegisterClass(&wc))
631  {
632  MessageBox(NULL,"Failed To Register The Image Window Class.", "ERROR", MB_OK|MB_ICONEXCLAMATION);
633  return;
634  }
635  }
636 
637  m_ref_count++;
638 
639  dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
640  dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
641 
642  AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
643 
644  // make sure the window is positioned within the desktop
645  if (WindowRect.top < 0)
646  {
647  WindowRect.bottom -= WindowRect.top;
648  WindowRect.top = 0;
649  }
650  if (WindowRect.left < 0)
651  {
652  WindowRect.right -= WindowRect.left;
653  WindowRect.left = 0;
654  }
655 
656  // Create The Window
657  if (!(m_hWnd = CreateWindowEx(dwExStyle, // Extended Style For The Window
658  DEFAULT_CLASS_NAME, // Class Name
659  title, // Window Title
660  dwStyle | // Defined Window Style
661  WS_CLIPSIBLINGS | // Required Window Style
662  WS_CLIPCHILDREN, // Required Window Style
663  WindowRect.left, WindowRect.top, // Window Position
664  WindowRect.right-WindowRect.left, // Calculate Window Width
665  WindowRect.bottom-WindowRect.top, // Calculate Window Height
666  NULL, // No Parent Window
667  NULL, // No Menu
668  m_hInstance, // Instance
669  this))) // Pass this pointer to WM_CREATE
670  {
671  MessageBox(NULL, "Couldn't create window.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
672  return;
673  }
674 
675  m_event_callback = NULL;
676 
677  //ShowWindow(m_hWnd, SW_SHOW);
678  SetForegroundWindow(m_hWnd);
679  SetFocus(m_hWnd);
680 }
681 
683 {
684  int i, c = (int) m_widgets.size();
685 
686  for (i = c - 1; i >= 0; i--)
687  {
688  Win32Widget *w = m_widgets[i];
689 
690  if (w->m_eType == eImage)
691  {
693 
694  if (iw->m_buffer != NULL)
695  {
696  delete [] iw->m_buffer;
697  iw->m_buffer = NULL;
698  }
699 
700  if (iw->m_hBmp != NULL)
701  {
702  SelectObject(iw->m_hDCMem, iw->m_hBmpOld);
703  DeleteObject(iw->m_hBmp);
704 
705  iw->m_hBmp = 0;
706  iw->m_hBmpOld = 0;
707  }
708 
709  if (iw->m_hDCMem && !DeleteDC(iw->m_hDCMem))
710  {
711  MessageBox(NULL, "Release Compatible Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
712  iw->m_hDCMem = NULL;
713  }
714 
715  if (iw->m_hDC && !ReleaseDC(iw->m_hWnd, iw->m_hDC))
716  {
717  MessageBox(NULL, "Release Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
718  iw->m_hDC = NULL;
719  }
720  }
721 
722  if (w->m_eType == eGLWidget)
723  {
724  Win32GLWidget *glw = (Win32GLWidget*)w;
725 
726  wglMakeCurrent(NULL, NULL);
727 
728  if (glw->m_hGLRC && !wglDeleteContext(glw->m_hGLRC))
729  {
730  MessageBox(NULL, "Release Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
731  glw->m_hGLRC = NULL;
732  }
733 
734  if (glw->m_hDC && !ReleaseDC(glw->m_hWnd, glw->m_hDC))
735  {
736  MessageBox(NULL, "Release Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
737  glw->m_hDC = NULL;
738  }
739  }
740 
741  if (w->m_hWnd && !DestroyWindow(w->m_hWnd))
742  {
743  MessageBox(NULL,"Could Not Release hWnd.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
744  w->m_hWnd = NULL;
745  }
746 
747  delete w;
748  }
749 
750  m_widgets.clear();
751 
752  if (m_hWnd && !DestroyWindow(m_hWnd))
753  {
754  MessageBox(NULL, "Could Not Release hWnd.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
755  m_hWnd = NULL;
756  }
757 
758  m_ref_count--;
759  if (m_did_quit)
760  m_quit_count--;
761 
762  if (m_ref_count == 0 && m_hInstance)
763  {
764  if (!UnregisterClass(DEFAULT_CLASS_NAME, m_hInstance) || !UnregisterClass(IMAGE_WINDOW_CLASS_NAME, m_hInstance))
765  {
766  MessageBox(NULL, "Could Not Unregister Class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
767  m_hInstance = NULL;
768  }
769  }
770 }
771 
772 // create widgets
774 {
775  Win32Widget *p = (Win32Widget*)parent;
776 
778 
779  iw->m_eType = eImage;
780  iw->m_hWnd = 0;
781  iw->m_hDC = 0;
782  iw->m_hDCMem = 0;
783  iw->m_hBmp = 0;
784  iw->m_hBmpOld = 0;
785  iw->m_pPixels = NULL;
786  iw->m_iWidth = 0;
787  iw->m_iHeight = 0;
788  iw->m_pImage = NULL;
789  iw->m_buffer = NULL;
790 
791  iw->mouse_down = false;
792  iw->mouse_start_x = 0;
793  iw->mouse_start_y = 0;
794  iw->mouse_current_x = 0;
795  iw->mouse_current_y = 0;
796 
797  // Create The Window
798  if (!(iw->m_hWnd = CreateWindowEx(0, // Extended Style For The Window
799  IMAGE_WINDOW_CLASS_NAME, // Class Name
800  "", // Window Title
801  WS_CLIPSIBLINGS | WS_CHILD | WS_VISIBLE, // Required Window Style
802  x, y, // Window Position
803  width, // Calculate Window Width
804  height, // Calculate Window Height
805  (p != 0 ? p->m_hWnd : m_hWnd), // Parent Window
806  NULL, // No Menu
807  m_hInstance, // Instance
808  this))) // Pass pointer to struct to WM_CREATE
809  {
810  MessageBox(NULL, "Couldn't create image window.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
811  delete iw;
812  return 0;
813  }
814 
815  SetWindowPos(iw->m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
816 
817  if (!(iw->m_hDC = GetDC(iw->m_hWnd)))
818  {
819  MessageBox(NULL, "Couldn't create device context." , "ERROR", MB_OK | MB_ICONEXCLAMATION);
820  delete iw;
821  return 0;
822  }
823 
824  if (!(iw->m_hDCMem = CreateCompatibleDC(iw->m_hDC)))
825  {
826  MessageBox(NULL, "Couldn't create compatible device context." , "ERROR", MB_OK | MB_ICONEXCLAMATION);
827  delete iw;
828  return 0;
829  }
830 
831  iw->m_index = (int) m_widgets.size();
832  m_widgets.push_back(iw);
833 
834  return (WIDGET_HANDLE) iw;
835 }
836 
837 WIDGET_HANDLE CWin32MainWindow::AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
838 {
839  Win32Widget *p = (Win32Widget*)parent;
840 
841  Win32Widget *w = new Win32Widget;
842 
843  w->m_eType = eButton;
844  w->m_hWnd = 0;
845 
846  // Create The Window
847  if (!(w->m_hWnd = CreateWindowEx(0, // Extended Style For The Window
848  "button", // Class Name
849  text, // Window Title
850  WS_CLIPSIBLINGS | WS_CHILD | WS_VISIBLE, // Required Window Style
851  x, y, // Window Position
852  width, // Calculate Window Width
853  height, // Calculate Window Height
854  (p != 0 ? p->m_hWnd : m_hWnd), // Parent Window
855  (HMENU)(m_widgets.size()+1), // The index into the widgets array is stored as the button id
856  m_hInstance, // Instance
857  this))) // Pass pointer to struct to WM_CREATE
858  {
859  MessageBox(NULL, "Couldn't create button.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
860 
861  delete w;
862 
863  return 0;
864  }
865 
866  SetWindowPos(w->m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
867 
868  w->m_index = (int) m_widgets.size();
869  m_widgets.push_back(w);
870 
871  return (WIDGET_HANDLE)w;
872 }
873 
874 WIDGET_HANDLE CWin32MainWindow::AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
875 {
876  Win32Widget *p = (Win32Widget*)parent;
877 
878  Win32Widget *w = new Win32Widget;
879 
880  w->m_eType = eLabel;
881  w->m_hWnd = 0;
882 
883  // Create The Window
884  if (!(w->m_hWnd = CreateWindowEx(0, // Extended Style For The Window
885  "STATIC", // Class Name
886  text, // Window Title
887  WS_CLIPSIBLINGS | WS_CHILD | WS_VISIBLE, // Required Window Style
888  x, y, // Window Position
889  width, // Calculate Window Width
890  height, // Calculate Window Height
891  (p != 0 ? p->m_hWnd : m_hWnd), // Parent Window
892  (HMENU)(m_widgets.size()+1), // The index into the widgets array is stored as the button id
893  m_hInstance, // Instance
894  this))) // Pass pointer to struct to WM_CREATE
895  {
896  MessageBox(NULL, "Couldn't create label.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
897 
898  delete w;
899 
900  return 0;
901  }
902 
903  SetWindowPos(w->m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
904 
905  w->m_index = (int) m_widgets.size();
906  m_widgets.push_back(w);
907 
908  return (WIDGET_HANDLE)w;
909 }
910 
911 WIDGET_HANDLE CWin32MainWindow::AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent)
912 {
913  Win32Widget *p = (Win32Widget*)parent;
914 
915  Win32Widget *w = new Win32Widget;
916 
917  w->m_eType = eCheckBox;
918  w->m_hWnd = 0;
919 
920  // Create The Window
921  if (!(w->m_hWnd = CreateWindowEx(0, // Extended Style For The Window
922  "button", // Class Name
923  text, // Window Title
924  WS_CHILD | WS_VISIBLE | BS_CHECKBOX | BS_AUTOCHECKBOX,// Required Window Style
925  x, y, // Window Position
926  width, // Calculate Window Width
927  height, // Calculate Window Height
928  (p != 0 ? p->m_hWnd : m_hWnd), // Parent Window
929  (HMENU)(m_widgets.size()+1), // The index into the widgets array is stored as the button id
930  m_hInstance, // Instance
931  this))) // Pass pointer to struct to WM_CREATE
932  {
933  MessageBox(NULL, "Couldn't create check box.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
934 
935  delete w;
936 
937  return 0;
938  }
939 
940  SetWindowPos(w->m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
941 
942  w->m_index = (int) m_widgets.size();
943  m_widgets.push_back(w);
944 
945  if (checked)
946  CheckDlgButton(m_hWnd, w->m_index+1, BST_CHECKED);
947  else
948  CheckDlgButton(m_hWnd, w->m_index+1, BST_UNCHECKED);
949 
950  return (WIDGET_HANDLE)w;
951 }
952 
953 WIDGET_HANDLE CWin32MainWindow::AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
954 {
955  Win32Widget *p = (Win32Widget*)parent;
956 
957  Win32Widget *w = new Win32Widget;
958 
959  w->m_eType = eTextEdit;
960  w->m_hWnd = 0;
961 
962  // Create The Window
963  if (!(w->m_hWnd = CreateWindowEx(WS_EX_STATICEDGE, // Extended Style For The Window
964  "Edit", // Class Name
965  text, // Window Title
966  WS_CHILD | WS_VISIBLE | WS_BORDER, // Required Window Style
967  x, y, // Window Position
968  width, // Calculate Window Width
969  height, // Calculate Window Height
970  (p != 0 ? p->m_hWnd : m_hWnd), // Parent Window
971  (HMENU)(m_widgets.size()+1), // The index into the widgets array is stored as the button id
972  m_hInstance, // Instance
973  this))) // Pass pointer to struct to WM_CREATE
974  {
975  MessageBox(NULL, "Couldn't create text edit.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
976 
977  delete w;
978 
979  return 0;
980  }
981 
982  SetWindowPos(w->m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
983 
984  w->m_index = (int) m_widgets.size();
985  m_widgets.push_back(w);
986 
987  return (WIDGET_HANDLE)w;
988 }
989 
990 WIDGET_HANDLE CWin32MainWindow::AddSlider(int x, int y, int width, int height, int min_value, int max_value, int step, int value, WIDGET_HANDLE parent)
991 {
992  Win32Widget *p = (Win32Widget*)parent;
993 
994  Win32Widget *w = new Win32Widget;
995 
996  w->m_eType = eSlider;
997  w->m_hWnd = 0;
998 
999  // Create The Window
1000  if (!(w->m_hWnd = CreateWindowEx(0, // Extended Style For The Window
1001  TRACKBAR_CLASS, // Class Name
1002  "Trackbar", // Window Title
1003  WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS, // Required Window Style
1004  x, y, // Window Position
1005  width, // Calculate Window Width
1006  height, // Calculate Window Height
1007  (p != 0 ? p->m_hWnd : m_hWnd), // Parent Window
1008  (HMENU)(m_widgets.size()+1), // The index into the widgets array is stored as the button id
1009  m_hInstance, // Instance
1010  this))) // Pass pointer to struct to WM_CREATE
1011  {
1012  MessageBox(NULL, "Couldn't create text edit.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
1013 
1014  delete w;
1015 
1016  return 0;
1017  }
1018 
1019  SendMessage(w->m_hWnd, TBM_SETRANGE, TRUE, MAKELONG(min_value, max_value));
1020  SendMessage(w->m_hWnd, TBM_SETPAGESIZE, 0, step);
1021  SendMessage(w->m_hWnd, TBM_SETTICFREQ, step, 0);
1022  SendMessage(w->m_hWnd, TBM_SETPOS, TRUE, value);
1023 
1024  SetWindowPos(w->m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
1025 
1026  w->m_index = (int) m_widgets.size();
1027  m_widgets.push_back(w);
1028 
1029  return (WIDGET_HANDLE)w;
1030 }
1031 
1032 WIDGET_HANDLE CWin32MainWindow::AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent)
1033 {
1034  Win32Widget *p = (Win32Widget*)parent;
1035 
1036  Win32Widget *w = new Win32Widget;
1037 
1038  w->m_eType = eComboBox;
1039  w->m_hWnd = 0;
1040 
1041  // Create The Window
1042  if (!(w->m_hWnd = CreateWindowEx(0, // Extended Style For The Window
1043  "combobox", // Class Name
1044  "ComboBox", // Window Title
1045  WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST, // Required Window Style
1046  x, y, // Window Position
1047  width, // Calculate Window Width
1048  2*width, // HACK: in win32 the height should incorporate the extended drop down list
1049  (p != 0 ? p->m_hWnd : m_hWnd), // Parent Window
1050  (HMENU)(m_widgets.size()+1), // The index into the widgets array is stored as the button id
1051  m_hInstance, // Instance
1052  this))) // Pass pointer to struct to WM_CREATE
1053  {
1054  MessageBox(NULL, "Couldn't create text edit.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
1055 
1056  delete w;
1057 
1058  return 0;
1059  }
1060 
1061  for (int i = 0; i < num_entries; i++)
1062  {
1063  SendMessage(w->m_hWnd, CB_ADDSTRING, 0, (LPARAM)entries[i]);
1064  }
1065 
1066  SendMessage(w->m_hWnd, CB_SETCURSEL, current_entry, 0);
1067 
1068  SetWindowPos(w->m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
1069 
1070  w->m_index = (int) m_widgets.size();
1071  m_widgets.push_back(w);
1072 
1073  return (WIDGET_HANDLE)w;
1074 }
1075 
1077 {
1078  Win32Widget *p = (Win32Widget*)parent;
1079 
1080  Win32GLWidget *glw = new Win32GLWidget;
1081 
1082  glw->m_eType = eGLWidget;
1083  glw->m_hWnd = 0;
1084  glw->m_hDC = 0;
1085  glw->m_iWidth = width;
1086  glw->m_iHeight = height;
1087 
1088  // Create The Window
1089  if (!(glw->m_hWnd = CreateWindowEx(0, // Extended Style For The Window
1090  IMAGE_WINDOW_CLASS_NAME, // Class Name (re-use the image window class
1091  "", // Window Title
1092  WS_CLIPSIBLINGS | WS_CHILD | WS_VISIBLE, // Required Window Style
1093  x, y, // Window Position
1094  width, // Calculate Window Width
1095  height, // Calculate Window Height
1096  (p != 0 ? p->m_hWnd : m_hWnd), // Parent Window
1097  NULL, // No Menu
1098  m_hInstance, // Instance
1099  this))) // Pass pointer to struct to WM_CREATE
1100  {
1101  MessageBox(NULL, "Couldn't create image window.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
1102 
1103  delete glw;
1104 
1105  return 0;
1106  }
1107 
1108  SetWindowPos(glw->m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
1109 
1110  if (!(glw->m_hDC = GetDC(glw->m_hWnd)))
1111  {
1112  MessageBox(NULL, "Couldn't create device context." , "ERROR", MB_OK | MB_ICONEXCLAMATION);
1113 
1114  delete glw;
1115 
1116  return 0;
1117  }
1118 
1119  PIXELFORMATDESCRIPTOR pfd = {
1120  sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1121  1, // version number
1122  PFD_DRAW_TO_WINDOW | // support window
1123  PFD_SUPPORT_OPENGL | // support OpenGL
1124  PFD_DOUBLEBUFFER, // double buffered
1125  PFD_TYPE_RGBA, // RGBA type
1126  24, // 24-bit color depth
1127  0, 0, 0, 0, 0, 0, // color bits ignored
1128  0, // no alpha buffer
1129  0, // shift bit ignored
1130  0, // no accumulation buffer
1131  0, 0, 0, 0, // accum bits ignored
1132  32, // 32-bit z-buffer
1133  0, // no stencil buffer
1134  0, // no auxiliary buffer
1135  PFD_MAIN_PLANE, // main layer
1136  0, // reserved
1137  0, 0, 0 // layer masks ignored
1138  };
1139 
1140  int iPixelFormat;
1141 
1142  // get the best available match of pixel format for the device context
1143  iPixelFormat = ChoosePixelFormat(glw->m_hDC, &pfd);
1144 
1145  // make that the pixel format of the device context
1146  BOOL bResult = SetPixelFormat(glw->m_hDC, iPixelFormat, &pfd);
1147  if (!bResult)
1148  {
1149  MessageBox(NULL, "ERROR: SetPixelFormat failed", "ERROR", MB_OK | MB_ICONEXCLAMATION);
1150 
1151  delete glw;
1152 
1153  return NULL;
1154  }
1155 
1156  glw->m_hGLRC = wglCreateContext(glw->m_hDC);
1157  if (!glw->m_hGLRC)
1158  {
1159  MessageBox(NULL, "ERROR: couldn't allocate HGLRC", "ERROR", MB_OK | MB_ICONEXCLAMATION);
1160 
1161  glw;
1162 
1163  return NULL;
1164  }
1165 
1166  wglMakeCurrent(glw->m_hDC, glw->m_hGLRC);
1167 
1168  glw->m_index = (int) m_widgets.size();
1169  m_widgets.push_back(glw);
1170 
1171  return (WIDGET_HANDLE)glw;
1172 }
1173 
1174 
1175 
1176 // access to widget attributes
1177 bool CWin32MainWindow::GetText(WIDGET_HANDLE widget, char *text, int len)
1178 {
1179  Win32Widget *w = (Win32Widget*)widget;
1180 
1181  if (w->m_eType == eButton || w->m_eType == eCheckBox)
1182  {
1183  Button_GetText(w->m_hWnd, text, len);
1184  }
1185  else if (w->m_eType == eLabel)
1186  {
1187  Static_GetText(w->m_hWnd, text, len);
1188  }
1189  else if (w->m_eType == eTextEdit)
1190  {
1191  GetWindowText(w->m_hWnd, text, len);
1192  }
1193  else
1194  return false;
1195 
1196  return true;
1197 }
1198 bool CWin32MainWindow::SetText(WIDGET_HANDLE widget, const char *text)
1199 {
1200  Win32Widget *w = (Win32Widget*)widget;
1201 
1202  if (w->m_eType == eButton || w->m_eType == eCheckBox)
1203  {
1204  Button_SetText(w->m_hWnd, text);
1205  }
1206  else if (w->m_eType == eLabel)
1207  {
1208  Static_SetText(w->m_hWnd, text);
1209  }
1210  else if (w->m_eType == eTextEdit)
1211  {
1212  SetWindowText(w->m_hWnd, text);
1213  }
1214  else
1215  return false;
1216 
1217  return true;
1218 }
1219 
1221 {
1222  Win32Widget *w = (Win32Widget*)widget;
1223 
1224  if (w->m_eType == eImage)
1225  {
1227 
1228  iw->m_pImage = pImage;
1229 
1230  if (iw->m_iWidth != pImage->width || iw->m_iHeight != pImage->height)
1231  {
1232  if (iw->m_hBmp != 0)
1233  {
1234  SelectObject(iw->m_hDCMem, iw->m_hBmpOld);
1235  DeleteObject(iw->m_hBmp);
1236 
1237  iw->m_hBmp = 0;
1238  iw->m_hBmpOld = 0;
1239  }
1240 
1241  if (iw->m_buffer != NULL)
1242  {
1243  delete [] iw->m_buffer;
1244  iw->m_buffer = NULL;
1245  }
1246 
1247  BITMAPINFO bminfo;
1248  // create a bitmap with rgb pixels
1249  bminfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1250  bminfo.bmiHeader.biWidth = pImage->width;
1251  bminfo.bmiHeader.biHeight = pImage->height;
1252  bminfo.bmiHeader.biPlanes = 1;
1253  bminfo.bmiHeader.biBitCount = 24;
1254  bminfo.bmiHeader.biCompression = BI_RGB;
1255  bminfo.bmiHeader.biSizeImage = 0; // it's OK for RGB images
1256  bminfo.bmiHeader.biXPelsPerMeter = 1;
1257  bminfo.bmiHeader.biYPelsPerMeter = 1;
1258  bminfo.bmiHeader.biClrUsed = 0;
1259  bminfo.bmiHeader.biClrImportant = 0;
1260 
1261  iw->m_hBmp = CreateDIBSection(iw->m_hDC, &bminfo, DIB_RGB_COLORS, (void**)&iw->m_pPixels, NULL, 0);
1262 
1263  if (!iw->m_hBmp)
1264  {
1265  MessageBox(NULL, "Couldn't create DIB." , "ERROR", MB_OK | MB_ICONEXCLAMATION);
1266  }
1267 
1268  iw->m_hBmpOld = (HBITMAP)SelectObject(iw->m_hDCMem, iw->m_hBmp);
1269 
1270  if (!iw->m_hBmpOld)
1271  {
1272  MessageBox(NULL, "Couldn't Select Object." , "ERROR", MB_OK | MB_ICONEXCLAMATION);
1273  }
1274 
1275  iw->m_iWidth = pImage->width;
1276  iw->m_iHeight = pImage->height;
1277 
1278  int padding = 4 - (3 * pImage->width) % 4;
1279  if (padding == 4) padding = 0;
1280  iw->m_buffer = new unsigned char[( 3 * pImage->width + padding) * pImage->height];
1281  }
1282 
1283  // upload the image into the DIB section
1284  const int width = pImage->width;
1285  const int height = pImage->height;
1286  int padding = 4 - (3 * width) % 4;
1287  if (padding == 4) padding = 0;
1288 
1289  if (pImage->type == CByteImage::eGrayScale)
1290  {
1291  const int out_offset = 6 * width + padding;
1292 
1293  unsigned char *out = iw->m_buffer + (3 * width + padding) * (height - 1);
1294  unsigned char *in = pImage->pixels;
1295 
1296  for (int y = 0; y < height; y++, out -= out_offset)
1297  {
1298  for (int x = 0; x < width; x++, out += 3, in++)
1299  {
1300  const unsigned int c = *in;
1301  out[0] = c;
1302  out[1] = c;
1303  out[2] = c;
1304  }
1305  }
1306  }
1307  else
1308  {
1309  const int out_offset = 6 * width + padding;
1310 
1311  unsigned char *out = iw->m_buffer + (3 * width + padding) * (height - 1);
1312  unsigned char *in = pImage->pixels;
1313 
1314  for (int y = 0; y < height; y++, out -= out_offset)
1315  {
1316  for (int x = 0; x < width; x++, out += 3, in += 3)
1317  {
1318  out[0] = in[2];
1319  out[1] = in[1];
1320  out[2] = in[0];
1321  }
1322  }
1323  }
1324 
1325  memcpy(iw->m_pPixels, iw->m_buffer, (3 * width + padding) * height);
1326 
1327  // tell the window it has to redraw the area
1328  InvalidateRect(iw->m_hWnd, NULL, false);
1329 
1330  return true;
1331  }
1332 
1333  return false;
1334 }
1335 
1337 {
1338  Win32Widget *w = (Win32Widget*)widget;
1339 
1340  if (w->m_eType == eCheckBox)
1341  {
1342  BOOL checked = IsDlgButtonChecked(m_hWnd, w->m_index+1);
1343 
1344  value = (checked ? 1 : 0);
1345 
1346  return true;
1347  }
1348  else if (w->m_eType == eSlider)
1349  {
1350  value = (int) SendMessage(w->m_hWnd, TBM_GETPOS, 0, 0);
1351 
1352  return true;
1353  }
1354  else if (w->m_eType == eComboBox)
1355  {
1356  value = (int) SendMessage(w->m_hWnd, CB_GETCURSEL, 0, 0);
1357 
1358  return true;
1359  }
1360 
1361  return false;
1362 }
1364 {
1365  Win32Widget *w = (Win32Widget*)widget;
1366 
1367  if (w->m_eType == eCheckBox)
1368  {
1369  if (value != 0)
1370  CheckDlgButton(m_hWnd, w->m_index+1, BST_CHECKED);
1371  else
1372  CheckDlgButton(m_hWnd, w->m_index+1, BST_UNCHECKED);
1373 
1374  return true;
1375  }
1376  else if (w->m_eType == eSlider)
1377  {
1378  SendMessage(w->m_hWnd, TBM_SETPOS, TRUE, value);
1379 
1380  return true;
1381  }
1382  else if (w->m_eType == eComboBox)
1383  {
1384  SendMessage(w->m_hWnd, CB_SETCURSEL, value, 0);
1385 
1386  return true;
1387  }
1388 
1389  return false;
1390 }
1391 
1393 {
1394  Win32Widget *w = (Win32Widget*)widget;
1395 
1396  if (w->m_eType == eGLWidget)
1397  {
1398  Win32GLWidget *glw = (Win32GLWidget*)w;
1399 
1400  SwapBuffers(glw->m_hDC);
1401 
1402  return true;
1403  }
1404 
1405  return false;
1406 }
1407 
1409 {
1410  Win32Widget *w = (Win32Widget*)widget;
1411 
1412  if (w->m_eType == eGLWidget)
1413  {
1414  Win32GLWidget *glw = (Win32GLWidget*)w;
1415 
1416  wglMakeCurrent(glw->m_hDC, glw->m_hGLRC);
1417 
1418  return true;
1419  }
1420 
1421  return false;
1422 }
1423 
1424 
1425 // window control
1427 {
1428  Win32Widget *w = (Win32Widget*)widget;
1429 
1430  if (w == NULL)
1431  {
1432  ShowWindow(m_hWnd, SW_SHOW);
1433  InvalidateRect(m_hWnd, NULL, false);
1434  }
1435  else
1436  {
1437  ShowWindow(w->m_hWnd, SW_SHOW);
1438  InvalidateRect(w->m_hWnd, NULL, false);
1439  }
1440 }
1442 {
1443  Win32Widget *w = (Win32Widget*)widget;
1444 
1445  if (w == NULL)
1446  {
1447  ShowWindow(m_hWnd, SW_HIDE);
1448  }
1449  else
1450  {
1451  ShowWindow(w->m_hWnd, SW_HIDE);
1452  }
1453 }
1454 
1456 {
1457  Win32Widget *w = (Win32Widget *) widget;
1458 
1459  if (w)
1460  {
1461  SetWindowPos(w->m_hWnd, HWND_TOP, 0, 0, width, height, SWP_NOMOVE);
1462  }
1463  else
1464  {
1465  SetWindowPos(m_hWnd, HWND_TOP, 0, 0, width, height, SWP_NOMOVE);
1466  }
1467 }
1468 
1470 {
1471  int result = 0;
1472 
1473  result |= GetAsyncKeyState(VK_SHIFT) < 0 ? IVT_SHIFT_KEY : 0;
1474  result |= GetAsyncKeyState(VK_MENU) < 0 ? IVT_ALT_KEY : 0;
1475  result |= GetAsyncKeyState(VK_CONTROL) < 0 ? IVT_CONTROL_KEY : 0;
1476 
1477  return result;
1478 }
1479 
1481 {
1482  int i, c = (int) m_widgets.size();
1483 
1484  for (i = 0; i < c; i++)
1485  {
1486  Win32Widget *w = m_widgets[i];
1487 
1488  if (w->m_hWnd == handle)
1489  return w;
1490  }
1491 
1492  return NULL;
1493 }
WIDGET_HANDLE AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
bool SetImage(WIDGET_HANDLE widget, const CByteImage *pImage)
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
WIDGET_HANDLE AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
#define IVT_SHIFT_KEY
Win32Widget * FindWidget(HWND handle)
int width
The width of the image in pixels.
Definition: ByteImage.h:257
#define IMAGE_WINDOW_CLASS_NAME
unsigned int mouse_current_y
CWin32MainWindow(int x, int y, int width, int height, const char *title)
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
WIDGET_HANDLE AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent=0)
#define IVT_LEFT_BUTTON
GLenum GLsizei len
Definition: glext.h:3940
bool GetText(WIDGET_HANDLE widget, char *text, int len)
void Hide(WIDGET_HANDLE widget=0)
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
bool MakeCurrentGLWidget(WIDGET_HANDLE widget)
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
bool SetValue(WIDGET_HANDLE widget, int value)
const CByteImage * m_pImage
#define IVT_CONTROL_KEY
GLenum GLint x
Definition: glext.h:3125
bool SetText(WIDGET_HANDLE widget, const char *text)
const GLubyte * c
Definition: glext.h:5181
void * WIDGET_HANDLE
GLint GLvoid * img
Definition: glext.h:3254
bool SwapBuffersGLWidget(WIDGET_HANDLE widget)
void DrawRect(unsigned char *img, int width, int height, int x0, int y0, int x1, int y1)
GLsizei const GLfloat * value
Definition: glext.h:3538
WIDGET_HANDLE AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent=0)
unsigned int mouse_start_x
unsigned char * m_pPixels
#define IVT_RIGHT_BUTTON
int height
The height of the image in pixels.
Definition: ByteImage.h:264
static int m_ref_count
unsigned int mouse_current_x
bool GetValue(WIDGET_HANDLE widget, int &value)
GLenum GLsizei width
Definition: glext.h:3122
GLenum GLsizei GLsizei height
Definition: glext.h:3132
#define IVT_MIDDLE_BUTTON
GLuint in
Definition: glext.h:5892
Win32WidgetType
ImageType type
The type of the image.
Definition: ByteImage.h:292
WIDGET_HANDLE AddImage(int x, int y, int width, int height, WIDGET_HANDLE parent=0)
GLenum GLint GLint y
Definition: glext.h:3125
static int m_quit_count
#define DEFAULT_CLASS_NAME
void SetSize(int width, int height, WIDGET_HANDLE widget=0)
unsigned char * m_buffer
GLfloat GLfloat p
Definition: glext.h:5178
void Show(WIDGET_HANDLE widget=0)
WIDGET_HANDLE AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
WIDGET_HANDLE AddSlider(int x, int y, int width, int height, int min_value, int max_value, int step, int value, WIDGET_HANDLE parent=0)
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3571
Win32WidgetType m_eType
unsigned int mouse_start_y
#define IVT_ALT_KEY
WIDGET_HANDLE AddGLWidget(int x, int y, int width, int height, WIDGET_HANDLE parent=0)


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28