// TabbedMDIChildWnd.cpp : implementation of the CTabbedMDIChildWnd class
//
//
//Copyright (c) 2004-2008 Ernesto Perales Soto
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this
//software and associated documentation files (the "Software"), to deal in the Software
//without restriction, including without limitation the rights to use, copy,modify, merge,
//publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
//to whom the Software is furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all copies or
//substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
//INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
//FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
//OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//DEALINGS IN THE SOFTWARE.
// 20040611 v 1.1
// Fixed memory leak, CTabbedMDIChildWnd::~CTabItem was missing
// (Thanks to J.H.Park)
// 20080311 v 1.2
// Forced repaint on reposition window to fix blank tab problem
// changed CTabItem constructors to make them Unicode compliant
// (Thanks to John Slagel)
// Replaced delete with free
// (Thanks to Jabes)
#include "stdafx.h"
#include "TabbedMDIChildWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CTabbedMDIChildWnd::CTabItem
CTabbedMDIChildWnd::CTabItem::CTabItem(const CTabItem& ti)
: TCITEM(ti)
{
pszText=_tcsdup(ti.pszText);
m_prcView=ti.m_prcView;
}
CTabbedMDIChildWnd::CTabItem::CTabItem(LPCTSTR szTabName, CRuntimeClass* prcView)
{
mask=TCIF_TEXT;
pszText=_tcsdup(szTabName);
m_prcView=prcView;
}
CTabbedMDIChildWnd::CTabItem::~CTabItem()
{
free(pszText);
}
// CTabbedMDIChildWnd
IMPLEMENT_DYNCREATE(CTabbedMDIChildWnd, CMDIChildWnd)
BEGIN_MESSAGE_MAP(CTabbedMDIChildWnd, CMDIChildWnd)
ON_WM_SIZE()
ON_NOTIFY(TCN_SELCHANGE,AFX_IDW_PANE_FIRST,OnTabChange)
ON_NOTIFY(TCN_SELCHANGING,AFX_IDW_PANE_FIRST,OnTabChanging)
END_MESSAGE_MAP()
// CTabbedMDIChildWnd construction/destruction
CTabbedMDIChildWnd::~CTabbedMDIChildWnd()
{
}
// CTabbedMDIChildWnd diagnostics
#ifdef _DEBUG
void CTabbedMDIChildWnd::AssertValid() const
{
CMDIChildWnd::AssertValid();
}
void CTabbedMDIChildWnd::Dump(CDumpContext& dc) const
{
dc << "m_vectorTabItems size = " << m_vectorTabItems.size();
dc << "\n";
for(std::vector<CTabItem>::const_iterator i=m_vectorTabItems.begin();i!=m_vectorTabItems.end();i++)
{
dc << "m_vectorTabItems item = " << i->m_prcView;
dc << "\n";
}
CMDIChildWnd::Dump(dc);
}
#endif //_DEBUG
void
CTabbedMDIChildWnd::DefineTab(const CTabItem& ti)
{
m_vectorTabItems.push_back(ti);
}
void
CTabbedMDIChildWnd::RepositionWindows()
{
if(NULL==m_wndTabs.m_hWnd)
return;
CView* p = GetActiveView();
if(NULL==p)
return;
if(NULL==p->m_hWnd)
return;
CRect r;
m_wndTabs.GetClientRect(r);
m_wndTabs.AdjustRect(FALSE,r);
m_wndTabs.ClientToScreen(r);
ScreenToClient(r);
p->MoveWindow(r,true); // JAS: Change to "true" to force a repaint.
}
void
CTabbedMDIChildWnd::ActivateFrame(int nCmdShow)
{
CMDIChildWnd::ActivateFrame(nCmdShow);
RepositionWindows();
}
// CTabbedMDIChildWnd message handlers
BOOL
CTabbedMDIChildWnd::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// Crate tab control
if(!m_wndTabs.Create(WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),this,AFX_IDW_PANE_FIRST))
return false;
int nOrder=0;
CRuntimeClass* prcOriginalView=pContext->m_pNewViewClass;
for(std::vector<CTabItem>::iterator i=m_vectorTabItems.begin();i!=m_vectorTabItems.end();i++,nOrder++)
{
if(-1==m_wndTabs.InsertItem(nOrder,&(*i)))
return false;
pContext->m_pNewViewClass=i->m_prcView;
CWnd* pView=CreateView(pContext,AFX_IDW_PANE_FIRST+nOrder+1);
if(NULL==pView)
{
TRACE("CTabbedMDIChildWnd::OnCreateClient -E- can not create view\r");
return false;
}
if(prcOriginalView==i->m_prcView)
SetActiveView(DYNAMIC_DOWNCAST(CView,pView));
else
pView->ShowWindow(SW_HIDE);
}
return true;
}
void
CTabbedMDIChildWnd::OnSize(UINT nType, int cx, int cy)
{
CMDIChildWnd::OnSize(nType, cx, cy);
RepositionWindows();
}
void
CTabbedMDIChildWnd::OnTabChanging(NMHDR * pNotifyStruct, LRESULT * result )
{
int nCurSel=m_wndTabs.GetCurSel();
if(-1==nCurSel)
return;
CDocument* pDocument=GetActiveDocument();
if(!pDocument)
return;
POSITION pos = pDocument->GetFirstViewPosition();
while (pos != NULL)
{
CView* pView = pDocument->GetNextView(pos);
if(m_vectorTabItems[nCurSel].m_prcView!=pView->GetRuntimeClass())
continue;
pView->ShowWindow(SW_HIDE);
}
}
void
CTabbedMDIChildWnd::OnTabChange(NMHDR * pNotifyStruct, LRESULT * result )
{
int nCurSel=m_wndTabs.GetCurSel();
if(-1==nCurSel)
return;
CDocument* pDocument=GetActiveDocument();
if(!pDocument)
return;
POSITION pos = pDocument->GetFirstViewPosition();
while (pos != NULL)
{
CView* pView = pDocument->GetNextView(pos);
if(m_vectorTabItems[nCurSel].m_prcView!=pView->GetRuntimeClass())
continue;
pView->ShowWindow(SW_SHOW);
SetActiveView(pView);
RepositionWindows();
}
}
- 1
- 2
前往页