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]
MFCWindow.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/MFCWindow.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 // MFCWindow
46 //-----------------------------------------------------------------------------
47 CString MFCWindow::mClassName;
48 //-----------------------------------------------------------------------------
49 BEGIN_MESSAGE_MAP(MFCWindow, CWnd)
50  ON_WM_CHAR()
51  ON_WM_CLOSE()
52  ON_WM_CREATE()
53  ON_WM_KEYDOWN()
54  ON_WM_KEYUP()
55  ON_WM_LBUTTONDBLCLK()
56  ON_WM_LBUTTONDOWN()
57  ON_WM_LBUTTONUP()
58  ON_WM_MBUTTONDBLCLK()
59  ON_WM_MBUTTONDOWN()
60  ON_WM_MBUTTONUP()
61  ON_WM_MOUSEMOVE()
62  ON_WM_MOUSEWHEEL()
63  ON_WM_PAINT()
64  ON_WM_RBUTTONDBLCLK()
65  ON_WM_RBUTTONDOWN()
66  ON_WM_RBUTTONUP()
67  ON_WM_SIZE()
68  ON_WM_TIMER()
69  ON_WM_DROPFILES()
70  ON_WM_DESTROY()
71 END_MESSAGE_MAP()
72 /*
73  WM_SYSKEYDOWN
74  WM_SYSKEYUP
75  WM_GETICON
76  WM_SETCURSOR
77  WM_SETICON
78  WM_CAPTURECHANGED
79  WM_MOUSEFIRST
80 */
81 //-----------------------------------------------------------------------------
83 {
84  dispatchDestroyEvent();
85 }
86 //-----------------------------------------------------------------------------
87 int MFCWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
88 {
89  if (CWnd::OnCreate(lpCreateStruct) == -1)
90  return -1;
91  return 0;
92 }
93 //-----------------------------------------------------------------------------
94 bool MFCWindow::initMFCWindow(CWnd* parent, MFCWindow* share_context, const vl::String& title, const vl::OpenGLContextFormat& fmt, int x, int y, int width, int height)
95 {
96  destroyGLContext();
97 
98  // register the class only once
99  if (mClassName.IsEmpty())
100  mClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS, LoadCursor(NULL, IDC_ARROW), NULL, LoadIcon(NULL, IDI_WINLOGO));
101 
102  CreateEx(WS_EX_APPWINDOW|WS_EX_ACCEPTFILES,
103  mClassName, L"MFCWindow",
104  (parent?WS_CHILD:WS_OVERLAPPEDWINDOW) | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
105  x, y, width, height,
106  parent?parent->m_hWnd:NULL, NULL, NULL);
107 
108  return initWin32GLContext(share_context?share_context->hglrc():NULL, title, fmt, x, y, width, height);
109 }
110 //-----------------------------------------------------------------------------
111 void MFCWindow::destroyGLContext()
112 {
113  // wglMakeCurrent(NULL, NULL) not needed
114  if (hwnd())
115  {
116  if (mHGLRC)
117  {
118  if ( wglDeleteContext(mHGLRC) == FALSE )
119  {
120  MessageBox( L"OpenGL context creation failed.\n"
121  L"The handle either doesn't specify a valid context or the context is being used by another thread.", L"MFCWindow::destroyGLContext() error!", MB_OK);
122  }
123  mHGLRC = NULL;
124  }
125 
126  if (mHDC)
127  {
128  DeleteDC(mHDC);
129  mHDC = NULL;
130  }
131  }
132 }
133 //-----------------------------------------------------------------------------
134 void MFCWindow::OnDestroy()
135 {
136  dispatchDestroyEvent();
137  destroyGLContext();
138 }
139 //-----------------------------------------------------------------------------
140 void MFCWindow::OnPaint()
141 {
142  if (hwnd() && hdc() && hglrc())
143  dispatchUpdateEvent();
144  ValidateRect(NULL);
145 }
146 //-----------------------------------------------------------------------------
147 /*void MFCWindow::OnDraw(CDC *pDC)
148 {
149 }*/
150 //-----------------------------------------------------------------------------
151 /*void MFCWindow::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
152 {
153  unsigned short unicode_out = 0;
154  vl::EKey key_out = Key_None;
155  vlWin32::translateKeyEvent(nChar, nFlags, unicode_out, key_out);
156  dispatchKeyPressEvent(unicode_out, key_out);
157 }*/
158 //-----------------------------------------------------------------------------
159 void MFCWindow::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
160 {
161  unsigned short unicode_out = 0;
162  vl::EKey key_out = Key_None;
163  vlWin32::translateKeyEvent(nChar, nFlags, unicode_out, key_out);
164  dispatchKeyPressEvent(unicode_out, key_out);
165 }
166 //-----------------------------------------------------------------------------
167 void MFCWindow::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
168 {
169  unsigned short unicode_out = 0;
170  vl::EKey key_out = Key_None;
171  vlWin32::translateKeyEvent(nChar, nFlags, unicode_out, key_out);
172  dispatchKeyReleaseEvent(unicode_out, key_out);
173 }
174 //-----------------------------------------------------------------------------
175 void MFCWindow::countAndCapture()
176 {
177  mMouseDownCount++;
178  if (mMouseDownCount == 1)
179  ::SetCapture(hwnd());
180 }
181 //-----------------------------------------------------------------------------
182 void MFCWindow::countAndRelease()
183 {
184  mMouseDownCount--;
185  if (mMouseDownCount <= 0)
186  {
187  ReleaseCapture();
188  mMouseDownCount = 0;
189  }
190 }
191 //-----------------------------------------------------------------------------
192 void MFCWindow::OnLButtonDblClk(UINT nFlags, CPoint point)
193 {
194  countAndCapture();
195  dispatchMouseDownEvent( LeftButton, point.x, point.y );
196 }
197 //-----------------------------------------------------------------------------
198 void MFCWindow::OnLButtonDown(UINT nFlags, CPoint point)
199 {
200  countAndCapture();
201  dispatchMouseDownEvent( LeftButton, point.x, point.y );
202 }
203 //-----------------------------------------------------------------------------
204 void MFCWindow::OnLButtonUp(UINT nFlags, CPoint point)
205 {
206  countAndRelease();
207  dispatchMouseUpEvent( LeftButton, point.x, point.y );
208 }
209 //-----------------------------------------------------------------------------
210 void MFCWindow::OnMButtonDblClk(UINT nFlags, CPoint point)
211 {
212  countAndCapture();
213  dispatchMouseDownEvent( MiddleButton, point.x, point.y );
214 }
215 //-----------------------------------------------------------------------------
216 void MFCWindow::OnMButtonDown(UINT nFlags, CPoint point)
217 {
218  countAndCapture();
219  dispatchMouseDownEvent( MiddleButton, point.x, point.y );
220 }
221 //-----------------------------------------------------------------------------
222 void MFCWindow::OnMButtonUp(UINT nFlags, CPoint point)
223 {
224  countAndRelease();
225  dispatchMouseUpEvent( MiddleButton, point.x, point.y );
226 }
227 //-----------------------------------------------------------------------------
228 void MFCWindow::OnMouseMove(UINT nFlags, CPoint point)
229 {
230  dispatchMouseMoveEvent( point.x, point.y );
231 }
232 //-----------------------------------------------------------------------------
233 BOOL MFCWindow::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
234 {
235  dispatchMouseWheelEvent (zDelta/120);
236  return FALSE;
237 }
238 //-----------------------------------------------------------------------------
239 void MFCWindow::OnRButtonDblClk(UINT nFlags, CPoint point)
240 {
241  countAndCapture();
242  dispatchMouseDownEvent( RightButton, point.x, point.y );
243 }
244 //-----------------------------------------------------------------------------
245 void MFCWindow::OnRButtonDown(UINT nFlags, CPoint point)
246 {
247  countAndCapture();
248  dispatchMouseDownEvent( RightButton, point.x, point.y );
249 }
250 //-----------------------------------------------------------------------------
251 void MFCWindow::OnRButtonUp(UINT nFlags, CPoint point)
252 {
253  countAndRelease();
254  dispatchMouseUpEvent( RightButton, point.x, point.y );
255 }
256 //-----------------------------------------------------------------------------
257 void MFCWindow::OnDropFiles(HDROP hDrop)
258 {
259  int count = DragQueryFile(hDrop, 0xFFFFFFFF, 0, 0);
260  const int char_count = 1024;
261  std::vector<String> files;
262  for(int i=0; i<count; ++i)
263  {
264  wchar_t file_path[char_count];
265  memset(file_path, 0, char_count);
266  DragQueryFile(hDrop,i,file_path,char_count);
267  files.push_back(file_path);
268  }
269  dispatchFileDroppedEvent(files);
270 }
271 //-----------------------------------------------------------------------------
272 void MFCWindow::OnSize (UINT nType, int cx, int cy)
273 {
274  CWnd::OnSize(nType, cx, cy);
275 
276  if (0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED)
277  return;
278 
279  framebuffer()->setWidth(cx);
280  framebuffer()->setHeight(cy);
281  dispatchResizeEvent(cx, cy);
282 }
283 //-----------------------------------------------------------------------------
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
Visualization Library main namespace.
The OpenGLContextFormat class encapsulates the settings of an OpenGL rendering context.
#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 MFCWindow class is an MFC CWnd with the functionalities of a Win32Context.
Definition: MFCWindow.hpp:47