Accessing View and Doc From Main Application object

What are the alternatives for accessing member functions(and data) of the
View and the Document objects from the Application object?

Is there any mechanism  similar to CView::GetDocument ?

Thanks in advance


Izack Lesher
lesherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
Hi lesher,

Is it an MDI or SDI application?

There are two ways to do it, first the simple one using CView::GetDocument():

For SDI:
------------
CMyApp::AnyFunct()
{
 CMyDoc* pDoc;
 CView* pView = AfxGetMainWnd()->GetActiveView();
 if ( !pView )
 {
  // no view exist, can only happen before CWinApp::InitInstance() has terminated
  return;
 }
 pDoc = (CMyDoc*)pView->GetDocument();
 ...
}

For MDI :
------------

CMyApp::AnyFunct()
{
 CMyDoc* pDoc;
 CFrameWnd* pParent = (CMDIFrameWnd*)AfxGetMainWnd();
 ASSERT( pParent );
 CMDIChild* pChild = pParent->MDIGetActive(); // can even use GetActiveFrame() here
 if ( !pChild )
 {
  // no child exists, so no doc/view
  return;
 }
 CView* pView = pChild->GetActiveView();
 if ( !pView )
 {
  return;
 }
 pDoc = (CMyDoc*)pView->GetDocument();
 ...
}


Now the second one (same for MDI and SDI):
CMyApp::AnyFunc()
{
 POSITION templPos, docPos, viewPos;
 CDocTemplate* pDocTempl;
 CDocument* pDoc;
 CView* pView;
 
 for ( templPos = GetFirstDocTemplatePosition(); doc != NULL; )
 {
  pDocTempl = GetNextDocTemplate( templPos );
  for ( docPos = pDocTempl->GetFirstDocPosition(); docPos != NULL; )
  {
   pDoc = pDocTempl->GetNextDoc( docPos );
   // here you are once for each open document, now for example you can achieve all views for the document
   for ( viewPos = pDoc->GetFirstViewPosition(); viewPos != NULL; )
   {
    pView = pDoc->GetNextView( viewPos );
    if ( pView->GetRuntimeClass() == RUNTIME_CLASS( CMyView )
     ((CMyView*)pView)->DoAnyFunc();
   }
  }
 }
}

hope that helps,

ZOPPO
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.