Commit 70cd557f authored by Clinton Stimpson's avatar Clinton Stimpson
Browse files

ENH: Add printing support

ENH: Allow a vtk render window created by a vtkImageViewer to be given to vtkMFCWindow
ENH: Easier setup for parent windows
ENH: Support for double click events
parent 5455054c
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(VTK_STD_AFX_H)
#define VTK_STD_AFX_H
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#include "vtkConfigure.h"
#if defined(VTK_BUILD_SHARED_LIBS)
# if defined(vtkMFC_EXPORTS)
# define VTK_MFC_EXPORT __declspec( dllexport )
# else
# define VTK_MFC_EXPORT __declspec( dllimport )
# endif
#else
# define VTK_MFC_EXPORT
#endif
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(VTK_STD_AFX_H)
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMFCWindow.cpp
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifndef WINVER // Allow use of features specific to Windows 95 and Windows NT 4 or later.
#define WINVER 0x0400
#endif
#include "vtkMFCWindow.h"
#include "vtkWin32OpenGLRenderWindow.h"
#include "vtkWin32RenderWindowInteractor.h"
#include "vtkInteractorStyleSwitch.h"
#ifdef _DEBUG
#define new DEBUG_NEW
......@@ -26,186 +15,187 @@
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// vtkMFCView
IMPLEMENT_DYNCREATE(vtkMFCWindow, CWnd)
BEGIN_MESSAGE_MAP(vtkMFCWindow, CWnd)
//{{AFX_MSG_MAP(vtkMFCWindow)
ON_WM_SIZE()
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_CREATE()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// vtkMFCView construction/destruction
vtkMFCWindow::vtkMFCWindow()
#ifdef _DEBUG
void vtkMFCWindow::AssertValid() const
{
this->renWin = vtkWin32OpenGLRenderWindow::New();
this->iren = vtkWin32RenderWindowInteractor::New();
this->iren->SetInstallMessageProc(0);
CWnd::AssertValid();
}
vtkMFCWindow::~vtkMFCWindow()
void vtkMFCWindow::Dump(CDumpContext& dc) const
{
iren->Delete();
renWin->Delete();
CWnd::Dump(dc);
}
#endif //_DEBUG
BOOL vtkMFCWindow::PreCreateWindow(CREATESTRUCT& cs)
vtkMFCWindow::vtkMFCWindow(CWnd *pcWnd)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
this->pvtkWin32OpenGLRW = NULL;
// create self as a child of passed in parent
DWORD style = WS_VISIBLE | WS_CLIPSIBLINGS;
if(pcWnd)
style |= WS_CHILD;
BOOL bCreated = CWnd::Create(NULL, _T("VTK-MFC Window"),
style, CRect(0, 0, 1, 1),
pcWnd, (UINT)IDC_STATIC);
SUCCEEDED(bCreated);
// create a default vtk window
vtkWin32OpenGLRenderWindow* win = vtkWin32OpenGLRenderWindow::New();
this->SetRenderWindow(win);
win->Delete();
return CWnd::PreCreateWindow(cs);
}
/* finalize the window when we destroy */
void vtkMFCWindow::OnDestroy()
vtkMFCWindow::~vtkMFCWindow()
{
this->renWin->Finalize();
this->SetRenderWindow(NULL);
}
/*!
* connect window to vtk window
*/
int vtkMFCWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
void vtkMFCWindow::OnDestroy()
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// setup the parent window
this->renWin->SetWindowId(this->GetSafeHwnd());
this->iren->SetRenderWindow(this->renWin);
this->iren->Initialize();
if(this->pvtkWin32OpenGLRW && this->pvtkWin32OpenGLRW->GetMapped())
this->pvtkWin32OpenGLRW->Finalize();
CWnd::OnDestroy();
}
void vtkMFCWindow::SetRenderWindow(vtkWin32OpenGLRenderWindow* win)
{
// make a default interactor style if one doesn't exist
if(!this->iren->GetInteractorStyle())
if(this->pvtkWin32OpenGLRW)
{
vtkInteractorStyle* style = vtkInteractorStyleSwitch::New();
iren->SetInteractorStyle(style);
style->Delete();
if(this->pvtkWin32OpenGLRW->GetMapped())
this->pvtkWin32OpenGLRW->Finalize();
this->pvtkWin32OpenGLRW->UnRegister(NULL);
}
return 0;
}
this->pvtkWin32OpenGLRW = win;
if(this->pvtkWin32OpenGLRW)
{
this->pvtkWin32OpenGLRW->Register(NULL);
vtkWin32RenderWindowInteractor* iren = vtkWin32RenderWindowInteractor::New();
iren->SetInstallMessageProc(0);
// setup the parent window
this->pvtkWin32OpenGLRW->SetWindowId(this->GetSafeHwnd());
this->pvtkWin32OpenGLRW->SetParentId(::GetParent(this->GetSafeHwnd()));
iren->SetRenderWindow(this->pvtkWin32OpenGLRW);
iren->Initialize();
// update size
CRect cRect = CRect(0,0,1,1);
if(this->GetParent())
this->GetParent()->GetClientRect(&cRect);
if (iren->GetInitialized())
iren->UpdateSize(cRect.Width(), cRect.Height());
// release our hold on interactor
iren->Delete();
}
}
vtkWin32OpenGLRenderWindow* vtkMFCWindow::GetRenderWindow()
{
return this->renWin;
return this->pvtkWin32OpenGLRW;
}
/////////////////////////////////////////////////////////////////////////////
// vtkMFCWindow drawing
/*!
* Based on a comment from Klaus Nowikow in vtkusers list.
* <http://public.kitware.com/pipermail/vtkusers/1999-August/001852.html>
*
*
* @return void :
*/
vtkRenderWindowInteractor* vtkMFCWindow::GetInteractor()
{
if(!this->pvtkWin32OpenGLRW)
return NULL;
return this->pvtkWin32OpenGLRW->GetInteractor();
}
void vtkMFCWindow::OnPaint()
{
CPaintDC dc(this);
this->renWin->Render();
if (this->GetInteractor() && this->GetInteractor()->GetInitialized())
{
this->GetInteractor()->Render();
}
}
void vtkMFCWindow::DrawDC(CDC* pDC)
{
// Obtain the size of the printer page in pixels.
int cxPage = pDC->GetDeviceCaps(HORZRES);
int cyPage = pDC->GetDeviceCaps(VERTRES);
// Get the size of the window in pixels.
int *size = this->pvtkWin32OpenGLRW->GetSize();
int cxWindow = size[0];
int cyWindow = size[1];
float fx = float(cxPage) / float(cxWindow);
float fy = float(cyPage) / float(cyWindow);
float scale = min(fx,fy);
int x = int(scale * float(cxWindow));
int y = int(scale * float(cyWindow));
this->pvtkWin32OpenGLRW->SetupMemoryRendering(cxWindow, cyWindow, pDC->GetSafeHdc());
this->pvtkWin32OpenGLRW->Render();
HDC memDC = this->pvtkWin32OpenGLRW->GetMemoryDC();
StretchBlt(pDC->GetSafeHdc(),0,0,x,y,memDC,0,0,cxWindow,cyWindow,SRCCOPY);
this->pvtkWin32OpenGLRW->ResumeScreenRendering();
}
/////////////////////////////////////////////////////////////////////////////
// vtkMFCWindow message handlers
/*!
* Resize the render window to that of the view window.
*
* @param nType :
* @param cx :
* @param cy :
*
* @return void :
*/
void vtkMFCWindow::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
this->renWin->vtkRenderWindow::SetSize(cx,cy);
this->iren->SetSize(cx,cy);
if (this->GetInteractor() && this->GetInteractor()->GetInitialized())
this->GetInteractor()->UpdateSize(cx, cy);
}
BOOL vtkMFCWindow::OnEraseBkgnd(CDC*)
{
// don't erase background
return TRUE;
}
/*!
* Allow the interactor to interact with the keyboard.
*
* @param message :
* @param wParam :
* @param lParam :
*
* @return LRESULT :
*/
LRESULT vtkMFCWindow::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
// grab focus on mouse downs
this->SetFocus();
// follow through
case WM_MBUTTONUP:
case WM_RBUTTONUP:
case WM_LBUTTONUP:
case WM_MOUSEMOVE:
case WM_MOUSEWHEEL:
case WM_CHAR:
case WM_KEYDOWN:
case WM_KEYUP:
case WM_TIMER:
if (this->iren->GetInitialized())
case WM_LBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
// grab focus on mouse downs
this->SetFocus();
// follow through
case WM_MBUTTONUP:
case WM_RBUTTONUP:
case WM_LBUTTONUP:
case WM_MOUSEMOVE:
case WM_MOUSEWHEEL:
case WM_CHAR:
case WM_KEYDOWN:
case WM_KEYUP:
case WM_TIMER:
if (this->GetInteractor()->GetInitialized())
{
return vtkHandleMessage2(this->m_hWnd, message, wParam, lParam, this->iren);
return vtkHandleMessage2(this->m_hWnd, message, wParam, lParam,
static_cast<vtkWin32RenderWindowInteractor*>(this->GetInteractor()));
}
break;
break;
}
return CWnd::WindowProc(message, wParam, lParam);
}
/////////////////////////////////////////////////////////////////////////////
// vtkMFCView diagnostics
#ifdef _DEBUG
void vtkMFCWindow::AssertValid() const
{
CWnd::AssertValid();
}
void vtkMFCWindow::Dump(CDumpContext& dc) const
{
CWnd::Dump(dc);
return CWnd::WindowProc(message, wParam, lParam);
}
#endif //_DEBUG
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMFCWindow.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#if !defined(C_VTK_MFC_WINDOW)
#ifndef C_VTK_MFC_WINDOW
#define C_VTK_MFC_WINDOW
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "afxwin.h"
#include "vtkMFCStdAfx.h"
// Include the required header files for the vtk classes we are using
class vtkWin32OpenGLRenderWindow;
class vtkWin32RenderWindowInteractor;
class vtkRenderWindowInteractor;
#include "vtkConfigure.h"
#if defined(VTK_BUILD_SHARED_LIBS)
# if defined(vtkMFC_EXPORTS)
# define VTK_MFC_EXPORT __declspec( dllexport )
# else
# define VTK_MFC_EXPORT __declspec( dllimport )
# endif
#else
# define VTK_MFC_EXPORT
#endif
//! class to display a VTK window in an MFC window
class VTK_MFC_EXPORT vtkMFCWindow : public CWnd
{
public:
vtkMFCWindow();
//! constructor requires a parent
vtkMFCWindow(CWnd *pcWnd);
//! destructor
virtual ~vtkMFCWindow();
DECLARE_DYNCREATE(vtkMFCWindow)
// Operations
public:
vtkWin32OpenGLRenderWindow* GetRenderWindow();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(vtkMFCWindow)
public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
//}}AFX_VIRTUAL
// Implementation
protected:
vtkWin32OpenGLRenderWindow *renWin;
vtkWin32RenderWindowInteractor *iren;
public:
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
//! draw to a device context
void DrawDC(CDC* pDC);
//! give an instance of a vtk render window to the mfc window
virtual void SetRenderWindow(vtkWin32OpenGLRenderWindow*);
//! get the render window
virtual vtkWin32OpenGLRenderWindow* GetRenderWindow();
//! get the interactor
virtual vtkRenderWindowInteractor* GetInteractor();
// Generated message map functions
protected:
//{{AFX_MSG(vtkMFCWindow)
//! windows procedure for this class
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
//! handle size events
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnPaint(); // overridden to draw this view
BOOL OnEraseBkgnd(CDC* pDC);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//! handle paint events
afx_msg void OnPaint();
//! handle destroy events
afx_msg void OnDestroy();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//! don't clear background
BOOL OnEraseBkgnd(CDC* pDC);
//! the vtk window
vtkWin32OpenGLRenderWindow* pvtkWin32OpenGLRW;
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
DECLARE_MESSAGE_MAP()
};
#endif // !defined(C_VTK_MFC_VIEW)
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment