CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 1999
    Posts
    2

    How to get the active doc template?

    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





  2. #2
    Join Date
    May 1999
    Posts
    6

    Re: How to get the active doc template?

    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();



  3. #3
    Guest

    Re: How to get the active doc template?

    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.


  4. #4
    Join Date
    May 1999
    Posts
    6

    Re: How to get the active doc template?

    After getting active view
    in Cview* pView;
    Check it for ur view,like
    pView->Iskindof(RUNTIME_CLASS(CMyView));
    I think that will work.
    Bye.


  5. #5
    Join Date
    Jun 1999
    Posts
    5

    Re: How to get the active doc template?

    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 ..]



  6. #6
    Join Date
    Apr 1999
    Posts
    383

    Re: How to get the active doc template?

    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



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured