Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 00005 /* */ 00006 /* Copyright (c) 2005-2010, Michele Bosi */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without modification, */ 00010 /* are permitted provided that the following conditions are met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, this */ 00013 /* list of conditions and the following disclaimer. */ 00014 /* */ 00015 /* - Redistributions in binary form must reproduce the above copyright notice, this */ 00016 /* list of conditions and the following disclaimer in the documentation and/or */ 00017 /* other materials provided with the distribution. */ 00018 /* */ 00019 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */ 00020 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ 00021 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ 00022 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */ 00023 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ 00024 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */ 00025 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ 00026 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ 00027 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ 00028 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00029 /* */ 00030 /**************************************************************************************/ 00031 00032 #include "StdAfx.h" 00033 00034 #include <vlMFC/MFCWindow.hpp> 00035 #include <vlWin32/Win32Window.hpp> 00036 #include <vlCore/Log.hpp> 00037 #include <vlCore/Say.hpp> 00038 #include <vlCore/Time.hpp> 00039 #include <shellapi.h> 00040 00041 using namespace vl; 00042 using namespace vlMFC; 00043 00044 //----------------------------------------------------------------------------- 00045 // MFCWindow 00046 //----------------------------------------------------------------------------- 00047 CString MFCWindow::mClassName; 00048 //----------------------------------------------------------------------------- 00049 BEGIN_MESSAGE_MAP(MFCWindow, CWnd) 00050 ON_WM_CHAR() 00051 ON_WM_CLOSE() 00052 ON_WM_CREATE() 00053 ON_WM_KEYDOWN() 00054 ON_WM_KEYUP() 00055 ON_WM_LBUTTONDBLCLK() 00056 ON_WM_LBUTTONDOWN() 00057 ON_WM_LBUTTONUP() 00058 ON_WM_MBUTTONDBLCLK() 00059 ON_WM_MBUTTONDOWN() 00060 ON_WM_MBUTTONUP() 00061 ON_WM_MOUSEMOVE() 00062 ON_WM_MOUSEWHEEL() 00063 ON_WM_PAINT() 00064 ON_WM_RBUTTONDBLCLK() 00065 ON_WM_RBUTTONDOWN() 00066 ON_WM_RBUTTONUP() 00067 ON_WM_SIZE() 00068 ON_WM_TIMER() 00069 ON_WM_DROPFILES() 00070 ON_WM_DESTROY() 00071 END_MESSAGE_MAP() 00072 /* 00073 WM_SYSKEYDOWN 00074 WM_SYSKEYUP 00075 WM_GETICON 00076 WM_SETCURSOR 00077 WM_SETICON 00078 WM_CAPTURECHANGED 00079 WM_MOUSEFIRST 00080 */ 00081 //----------------------------------------------------------------------------- 00082 MFCWindow::~MFCWindow() 00083 { 00084 dispatchDestroyEvent(); 00085 } 00086 //----------------------------------------------------------------------------- 00087 int MFCWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) 00088 { 00089 if (CWnd::OnCreate(lpCreateStruct) == -1) 00090 return -1; 00091 return 0; 00092 } 00093 //----------------------------------------------------------------------------- 00094 bool MFCWindow::initMFCWindow(CWnd* parent, MFCWindow* share_context, const vl::String& title, const vl::OpenGLContextFormat& fmt, int x, int y, int width, int height) 00095 { 00096 destroyGLContext(); 00097 00098 // register the class only once 00099 if (mClassName.IsEmpty()) 00100 mClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS, LoadCursor(NULL, IDC_ARROW), NULL, LoadIcon(NULL, IDI_WINLOGO)); 00101 00102 CreateEx(WS_EX_APPWINDOW|WS_EX_ACCEPTFILES, 00103 mClassName, L"MFCWindow", 00104 (parent?WS_CHILD:WS_OVERLAPPEDWINDOW) | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 00105 x, y, width, height, 00106 parent?parent->m_hWnd:NULL, NULL, NULL); 00107 00108 return initWin32GLContext(share_context?share_context->hglrc():NULL, title, fmt, x, y, width, height); 00109 } 00110 //----------------------------------------------------------------------------- 00111 void MFCWindow::destroyGLContext() 00112 { 00113 // wglMakeCurrent(NULL, NULL) not needed 00114 if (hwnd()) 00115 { 00116 if (mHGLRC) 00117 { 00118 if ( wglDeleteContext(mHGLRC) == FALSE ) 00119 { 00120 MessageBox( L"OpenGL context creation failed.\n" 00121 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); 00122 } 00123 mHGLRC = NULL; 00124 } 00125 00126 if (mHDC) 00127 { 00128 DeleteDC(mHDC); 00129 mHDC = NULL; 00130 } 00131 } 00132 } 00133 //----------------------------------------------------------------------------- 00134 void MFCWindow::OnDestroy() 00135 { 00136 dispatchDestroyEvent(); 00137 destroyGLContext(); 00138 } 00139 //----------------------------------------------------------------------------- 00140 void MFCWindow::OnPaint() 00141 { 00142 if (hwnd() && hdc() && hglrc()) 00143 dispatchRunEvent(); 00144 ValidateRect(NULL); 00145 } 00146 //----------------------------------------------------------------------------- 00147 /*void MFCWindow::OnDraw(CDC *pDC) 00148 { 00149 }*/ 00150 //----------------------------------------------------------------------------- 00151 /*void MFCWindow::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 00152 { 00153 unsigned short unicode_out = 0; 00154 vl::EKey key_out = Key_None; 00155 vlWin32::translateKeyEvent(nChar, nFlags, unicode_out, key_out); 00156 dispatchKeyPressEvent(unicode_out, key_out); 00157 }*/ 00158 //----------------------------------------------------------------------------- 00159 void MFCWindow::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 00160 { 00161 unsigned short unicode_out = 0; 00162 vl::EKey key_out = Key_None; 00163 vlWin32::translateKeyEvent(nChar, nFlags, unicode_out, key_out); 00164 dispatchKeyPressEvent(unicode_out, key_out); 00165 } 00166 //----------------------------------------------------------------------------- 00167 void MFCWindow::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 00168 { 00169 unsigned short unicode_out = 0; 00170 vl::EKey key_out = Key_None; 00171 vlWin32::translateKeyEvent(nChar, nFlags, unicode_out, key_out); 00172 dispatchKeyReleaseEvent(unicode_out, key_out); 00173 } 00174 //----------------------------------------------------------------------------- 00175 void MFCWindow::countAndCapture() 00176 { 00177 mMouseDownCount++; 00178 if (mMouseDownCount == 1) 00179 ::SetCapture(hwnd()); 00180 } 00181 //----------------------------------------------------------------------------- 00182 void MFCWindow::countAndRelease() 00183 { 00184 mMouseDownCount--; 00185 if (mMouseDownCount <= 0) 00186 { 00187 ReleaseCapture(); 00188 mMouseDownCount = 0; 00189 } 00190 } 00191 //----------------------------------------------------------------------------- 00192 void MFCWindow::OnLButtonDblClk(UINT nFlags, CPoint point) 00193 { 00194 countAndCapture(); 00195 dispatchMouseDownEvent( LeftButton, point.x, point.y ); 00196 } 00197 //----------------------------------------------------------------------------- 00198 void MFCWindow::OnLButtonDown(UINT nFlags, CPoint point) 00199 { 00200 countAndCapture(); 00201 dispatchMouseDownEvent( LeftButton, point.x, point.y ); 00202 } 00203 //----------------------------------------------------------------------------- 00204 void MFCWindow::OnLButtonUp(UINT nFlags, CPoint point) 00205 { 00206 countAndRelease(); 00207 dispatchMouseUpEvent( LeftButton, point.x, point.y ); 00208 } 00209 //----------------------------------------------------------------------------- 00210 void MFCWindow::OnMButtonDblClk(UINT nFlags, CPoint point) 00211 { 00212 countAndCapture(); 00213 dispatchMouseDownEvent( MiddleButton, point.x, point.y ); 00214 } 00215 //----------------------------------------------------------------------------- 00216 void MFCWindow::OnMButtonDown(UINT nFlags, CPoint point) 00217 { 00218 countAndCapture(); 00219 dispatchMouseDownEvent( MiddleButton, point.x, point.y ); 00220 } 00221 //----------------------------------------------------------------------------- 00222 void MFCWindow::OnMButtonUp(UINT nFlags, CPoint point) 00223 { 00224 countAndRelease(); 00225 dispatchMouseUpEvent( MiddleButton, point.x, point.y ); 00226 } 00227 //----------------------------------------------------------------------------- 00228 void MFCWindow::OnMouseMove(UINT nFlags, CPoint point) 00229 { 00230 dispatchMouseMoveEvent( point.x, point.y ); 00231 } 00232 //----------------------------------------------------------------------------- 00233 BOOL MFCWindow::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 00234 { 00235 dispatchMouseWheelEvent (zDelta/120); 00236 return FALSE; 00237 } 00238 //----------------------------------------------------------------------------- 00239 void MFCWindow::OnRButtonDblClk(UINT nFlags, CPoint point) 00240 { 00241 countAndCapture(); 00242 dispatchMouseDownEvent( RightButton, point.x, point.y ); 00243 } 00244 //----------------------------------------------------------------------------- 00245 void MFCWindow::OnRButtonDown(UINT nFlags, CPoint point) 00246 { 00247 countAndCapture(); 00248 dispatchMouseDownEvent( RightButton, point.x, point.y ); 00249 } 00250 //----------------------------------------------------------------------------- 00251 void MFCWindow::OnRButtonUp(UINT nFlags, CPoint point) 00252 { 00253 countAndRelease(); 00254 dispatchMouseUpEvent( RightButton, point.x, point.y ); 00255 } 00256 //----------------------------------------------------------------------------- 00257 void MFCWindow::OnDropFiles(HDROP hDrop) 00258 { 00259 int count = DragQueryFile(hDrop, 0xFFFFFFFF, 0, 0); 00260 const int char_count = 1024; 00261 std::vector<String> files; 00262 for(int i=0; i<count; ++i) 00263 { 00264 wchar_t file_path[char_count]; 00265 memset(file_path, 0, char_count); 00266 DragQueryFile(hDrop,i,file_path,char_count); 00267 files.push_back(file_path); 00268 } 00269 dispatchFileDroppedEvent(files); 00270 } 00271 //----------------------------------------------------------------------------- 00272 void MFCWindow::OnSize (UINT nType, int cx, int cy) 00273 { 00274 CWnd::OnSize(nType, cx, cy); 00275 00276 if (0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED) 00277 return; 00278 00279 framebuffer()->setWidth(cx); 00280 framebuffer()->setHeight(cy); 00281 dispatchResizeEvent(cx, cy); 00282 } 00283 //-----------------------------------------------------------------------------