Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
MDIWindow.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include "StdAfx.h"
33 
34 #include <vlMFC/MDIWindow.hpp>
35 #include <vlWin32/Win32Window.hpp>
36 #include <vlCore/Log.hpp>
37 #include <vlCore/Say.hpp>
38 #include <vlCore/Time.hpp>
39 #include <shellapi.h>
40 
41 using namespace vl;
42 using namespace vlMFC;
43 
44 //-----------------------------------------------------------------------------
45 BEGIN_MESSAGE_MAP(MDIWindow, CView)
46  ON_WM_CHAR()
47  ON_WM_CLOSE()
48  ON_WM_CREATE()
49  ON_WM_KEYDOWN()
50  ON_WM_KEYUP()
51  ON_WM_LBUTTONDBLCLK()
52  ON_WM_LBUTTONDOWN()
53  ON_WM_LBUTTONUP()
54  ON_WM_MBUTTONDBLCLK()
55  ON_WM_MBUTTONDOWN()
56  ON_WM_MBUTTONUP()
57  ON_WM_MOUSEMOVE()
58  ON_WM_MOUSEWHEEL()
59  ON_WM_PAINT()
60  ON_WM_RBUTTONDBLCLK()
61  ON_WM_RBUTTONDOWN()
62  ON_WM_RBUTTONUP()
63  ON_WM_SIZE()
64  ON_WM_TIMER()
65  ON_WM_DROPFILES()
66  ON_WM_DESTROY()
67 END_MESSAGE_MAP()
68 /*
69  WM_SYSKEYDOWN
70  WM_SYSKEYUP
71  WM_GETICON
72  WM_SETCURSOR
73  WM_SETICON
74  WM_CAPTURECHANGED
75  WM_MOUSEFIRST
76 */
77 BOOL MDIWindow::PreCreateWindow(CREATESTRUCT & cs)
78 {
79  cs.dwExStyle |= WS_EX_ACCEPTFILES;
80  cs.style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
81  return CView::PreCreateWindow(cs);
82 }
83 //-----------------------------------------------------------------------------
84 MDIWindow::~MDIWindow()
85 {
86  dispatchDestroyEvent();
87 }
88 //-----------------------------------------------------------------------------
89 void MDIWindow::destroyGLContext()
90 {
91  // wglMakeCurrent(NULL, NULL) not needed
92  if (hwnd())
93  {
94  if (mHGLRC)
95  {
96  if ( wglDeleteContext(mHGLRC) == FALSE )
97  {
98  MessageBox( L"OpenGL context creation failed.\n"
99  L"The handle either doesn't specify a valid context or the context is being used by another thread.", L" MDIWindow::destroyGLContext() error!", MB_OK);
100  }
101  mHGLRC = NULL;
102  }
103 
104  if (mHDC)
105  {
106  DeleteDC(mHDC);
107  mHDC = NULL;
108  }
109  }
110 }
111 //-----------------------------------------------------------------------------
112 void MDIWindow::OnDestroy()
113 {
114  dispatchDestroyEvent();
115  destroyGLContext();
116 }
117 //-----------------------------------------------------------------------------
118 void MDIWindow::OnPaint()
119 {
120  if (hwnd() && hdc() && hglrc())
121  dispatchUpdateEvent();
122  ValidateRect(NULL);
123 }
124 //-----------------------------------------------------------------------------
125 /*void MDIWindow::OnDraw(CDC *pDC)
126 {
127 }*/
128 //-----------------------------------------------------------------------------
129 /*void MDIWindow::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
130 {
131  unsigned short unicode_out = 0;
132  vl::EKey key_out = Key_None;
133  vlWin32::translateKeyEvent(nChar, nFlags, unicode_out, key_out);
134  dispatchKeyPressEvent(unicode_out, key_out);
135 }*/
136 //-----------------------------------------------------------------------------
137 void MDIWindow::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
138 {
139  unsigned short unicode_out = 0;
140  vl::EKey key_out = Key_None;
141  vlWin32::translateKeyEvent(nChar, nFlags, unicode_out, key_out);
142  dispatchKeyPressEvent(unicode_out, key_out);
143 }
144 //-----------------------------------------------------------------------------
145 void MDIWindow::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
146 {
147  unsigned short unicode_out = 0;
148  vl::EKey key_out = Key_None;
149  vlWin32::translateKeyEvent(nChar, nFlags, unicode_out, key_out);
150  dispatchKeyReleaseEvent(unicode_out, key_out);
151 }
152 //-----------------------------------------------------------------------------
153 void MDIWindow::countAndCapture()
154 {
155  mMouseDownCount++;
156  if (mMouseDownCount == 1)
157  ::SetCapture(hwnd());
158 }
159 //-----------------------------------------------------------------------------
160 void MDIWindow::countAndRelease()
161 {
162  mMouseDownCount--;
163  if (mMouseDownCount <= 0)
164  {
165  ReleaseCapture();
166  mMouseDownCount = 0;
167  }
168 }
169 //-----------------------------------------------------------------------------
170 void MDIWindow::OnLButtonDblClk(UINT nFlags, CPoint point)
171 {
172  countAndCapture();
173  dispatchMouseDownEvent( LeftButton, point.x, point.y );
174 }
175 //-----------------------------------------------------------------------------
176 void MDIWindow::OnLButtonDown(UINT nFlags, CPoint point)
177 {
178  countAndCapture();
179  dispatchMouseDownEvent( LeftButton, point.x, point.y );
180 }
181 //-----------------------------------------------------------------------------
182 void MDIWindow::OnLButtonUp(UINT nFlags, CPoint point)
183 {
184  countAndRelease();
185  dispatchMouseUpEvent( LeftButton, point.x, point.y );
186 }
187 //-----------------------------------------------------------------------------
188 void MDIWindow::OnMButtonDblClk(UINT nFlags, CPoint point)
189 {
190  countAndCapture();
191  dispatchMouseDownEvent( MiddleButton, point.x, point.y );
192 }
193 //-----------------------------------------------------------------------------
194 void MDIWindow::OnMButtonDown(UINT nFlags, CPoint point)
195 {
196  countAndCapture();
197  dispatchMouseDownEvent( MiddleButton, point.x, point.y );
198 }
199 //-----------------------------------------------------------------------------
200 void MDIWindow::OnMButtonUp(UINT nFlags, CPoint point)
201 {
202  countAndRelease();
203  dispatchMouseUpEvent( MiddleButton, point.x, point.y );
204 }
205 //-----------------------------------------------------------------------------
206 void MDIWindow::OnMouseMove(UINT nFlags, CPoint point)
207 {
208  dispatchMouseMoveEvent( point.x, point.y );
209 }
210 //-----------------------------------------------------------------------------
211 BOOL MDIWindow::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
212 {
213  dispatchMouseWheelEvent (zDelta/120);
214  return FALSE;
215 }
216 //-----------------------------------------------------------------------------
217 void MDIWindow::OnRButtonDblClk(UINT nFlags, CPoint point)
218 {
219  countAndCapture();
220  dispatchMouseDownEvent( RightButton, point.x, point.y );
221 }
222 //-----------------------------------------------------------------------------
223 void MDIWindow::OnRButtonDown(UINT nFlags, CPoint point)
224 {
225  countAndCapture();
226  dispatchMouseDownEvent( RightButton, point.x, point.y );
227 }
228 //-----------------------------------------------------------------------------
229 void MDIWindow::OnRButtonUp(UINT nFlags, CPoint point)
230 {
231  countAndRelease();
232  dispatchMouseUpEvent( RightButton, point.x, point.y );
233 }
234 //-----------------------------------------------------------------------------
235 void MDIWindow::OnDropFiles(HDROP hDrop)
236 {
237  int count = DragQueryFile(hDrop, 0xFFFFFFFF, 0, 0);
238  const int char_count = 1024;
239  std::vector<String> files;
240  for(int i=0; i<count; ++i)
241  {
242  wchar_t file_path[char_count];
243  memset(file_path, 0, char_count);
244  DragQueryFile(hDrop,i,file_path,char_count);
245  files.push_back(file_path);
246  }
247  dispatchFileDroppedEvent(files);
248 }
249 //-----------------------------------------------------------------------------
250 void MDIWindow::OnSize (UINT nType, int cx, int cy)
251 {
252  CWnd::OnSize(nType, cx, cy);
253 
254  if (0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED)
255  return;
256 
257  framebuffer()->setWidth(cx);
258  framebuffer()->setHeight(cy);
259  dispatchResizeEvent(cx, cy);
260 }
261 //-----------------------------------------------------------------------------
Visualization Library main namespace.
#define NULL
Definition: OpenGLDefs.hpp:81
VLWIN32_EXPORT void translateKeyEvent(WPARAM wParam, LPARAM lParam, unsigned short &unicode_out, vl::EKey &key_out)
The MFC bindings namespace.
The MDIWindow class is an MFC CView with the functionalities of a Win32Context (experimental).
Definition: MDIWindow.hpp:48