Click to See Complete Forum and Search --> : How to get the active doc template?
quickhands
May 28th, 1999, 04:44 PM
I wonder if there is a way to get the currently active doc template in an MDI app.
There must be a pointer to the active doc template somewhere.. can somebody tell me?
thanks
jonas
krishna TS
May 29th, 1999, 04:46 AM
I think U can get Active doc template or active frame or active view through this.
CMDIFrameWnd* pMDI=
(CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
CMDIChildWnd* pChild=
(CMDIChildWnd*)pMDI->GetActiveFrame();
CMyDocument* pDoc =
(CMyDocument*)pMDI->GetActiveDocument();
CMyView* pMyView=
(CMyView*)pChild->GetActiveView();
Good answer but,
Those methods would work fine if you knew in advance which class to use in the type casting. My application uses different doc templates each with there own unique run time classes, so if I used GetActiveFrame() for instance, I could not know how to cast the return pointer and therefore I can not access
"My Class" variables.
Dan B.
krishna TS
June 2nd, 1999, 11:42 PM
After getting active view
in Cview* pView;
Check it for ur view,like
pView->Iskindof(RUNTIME_CLASS(CMyView));
I think that will work.
Bye.
Ivan Zhakov
June 8th, 1999, 07:06 AM
This is simple:
CMDIFrameWnd * pFrameWnd = STATIC_DOWNCAST(CMDIFrameWnd, AfxGetMainWnd());
CDocTemplate * pDocTemplate = NULL;
if ( pFrameWnd != NULL )
{
CMDIChildWnd * pChildWnd = pFrameWnd->MDIGetActive();
if (pChildWnd != NULL)
{
CDocument * pDoc = pChildWnd->GetActiveDocument();
if (pDoc != NULL)
pDocTemplate = pDoc->GetDocTemplate();
}
}
[ .. Use pDocTemplate variable ..]
Dave Lorde
June 8th, 1999, 07:55 AM
If the problem is knowing what type to cast to, use dynamic casting. A dynamic cast will return 0 if you try to cast to the wrong type:
CMyDocTemplate* pDT = dynamic_cast<CMyDocTemplate*>(pDoc->GetDocTemplate());
if (pDT)
{
// do stuff for CMyDocTemplate
return;
}
CMyOtherDocTemplate* pODT = dynamic_cast<CMyOtherDocTemplate*>(pDoc->GetDocTemplate());
if (pODT)
{
// do stuff for CMyOtherDocTemplate
return;
}
It's not very elegant, and you need to enable RTTI, but it does the job.
The best answer is to design your application so that this problem doesn't arise.
It's often difficult to change it once this sort of difficulty is programmed in, but there are partial solutions short of complete redesign, such as deriving your specialised classes from an intermediary class that defines a virtual function for the Visitor pattern:
class VDocTemplate : pubic CDocTemplate
{
...
virtual void accept(Visitor&) = 0;
};
class CMyDocTemplate : public VDocTemplate
{
...
void accept(Visitor& visitor) { visitor.visit(this); }
};
// Now the Visitor base class for various derived operations:
class Visitor
{
...
virtual void visit(CMyDocTemplate* pMDT);
virtual void visit(CMyOtherDocTemplate* pMDT);
protected:
Visitor();
};
// Derive classes from Visitor to do the various operations required, e.g.
class ResetDocTemplateVisitor : public Visitor
{
...
virtual void visit(CMyDocTemplate* pMDT) { pMDT->SetFooVar(0); }
virtual void visit(CMyOtherDocTemplate* pMODT) { pMODT->SetBarVar(0); }
};
// Use the visitors like this:
VDocTemplate* pVDT = dynamic_cast<VDocTemplate*>(pDocument->GetDocTemplate());
if (pVDT = 0)
return MajorError;
ResetDocTemplateVisitor rdtv;
pVDT->accept(rdtv); // this will perform the appropriate reset function
Hmmm, personally, I'd rather rethink the application design :-)
Dave
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.