Adding Splitter Windows Makes Mainframe Unable to Get Current Document
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 43

Thread: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

  1. #1
    sunnysky Guest

    Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    Hello,

    I created two split views like this:

    BOOL CMainFrame::OnCreateClient( LPCREATESTRUCT lpcs, CCreateContext* pContext )
    {
    CFrameWnd::OnCreateClient(lpcs, pContext);

    if (!m_wndSplitter.CreateStatic (this, 2, 1) ||
    !m_wndSplitter.CreateView (0, 0, RUNTIME_CLASS (CFirstView),
    CSize (0, 256), pContext) ||
    !m_wndSplitter.CreateView (1, 0, RUNTIME_CLASS (CSecondView),
    CSize (0, 0), pContext))
    return FALSE;

    return TRUE;
    }

    Then I added a menu item and get it handled:

    void CMainFrame::OnAlohaHowAreYou()
    {
    m_pDoc = (CAlohaDoc*)GetActiveDocument();

    m_pDoc->SayHowAreYou();
    }

    When I step into CMainFrame::OnAlohaHowAreYou(), I found m_pDoc is NULL. If I remove the split views and make CMainFrame::OnCreateClient() like below, then there is no problem.

    BOOL CMainFrame::OnCreateClient( LPCREATESTRUCT lpcs, CCreateContext* pContext )
    {
    CFrameWnd::OnCreateClient(lpcs, pContext);

    return TRUE;
    }

    What could be wrong?

    Thanks,
    Brian

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    How and what from is this OnAlohaHowAreYou called?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,024

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    Quote Originally Posted by sunnysky View Post
    When I step into CMainFrame::OnAlohaHowAreYou(), I found m_pDoc is NULL. If I remove the split views and make CMainFrame::OnCreateClient() like below, then there is no problem.
    Code:
    CDocument* CFrameWnd::GetActiveDocument()
    {
        ASSERT_VALID(this);
        CView* pView = GetActiveView();
        if (pView != NULL)
            return pView->GetDocument();
        return NULL;
    }
    Your frame is not aware of active view. You have to explicitly access some of your views to get its corresponding document. Or set active view information otherwise.
    Best regards,
    Igor

  4. #4
    sunnysky Guest

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    Quote Originally Posted by VictorN View Post
    How and what from is this OnAlohaHowAreYou called?
    It's a message handler.

    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    ON_WM_CREATE()
    ON_COMMAND(ID_FILE_OPEN, &CMainFrame::OnFileOpen)
    ON_COMMAND(ID_ALOHA_HOWAREYOU, &CMainFrame::OnAlohaHowAreYou)
    END_MESSAGE_MAP()

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    Quote Originally Posted by sunnysky View Post
    I created two split views like this:
    ...
    When I step into CMainFrame::OnAlohaHowAreYou(), I found m_pDoc is NULL. If I remove the split views and make CMainFrame::OnCreateClient() like below, then there is no problem.
    ...
    What could be wrong?
    You seem to have created your splitter from scratch without careful reading the docs and playing with samples like viewex.
    Thus you missed to activate a view as Igor already mentioned.
    However if you had looked at the viewex code you would see this important line:
    Code:
    BOOL CSplitterFrame::OnCreateClient(LPCREATESTRUCT,
    	 CCreateContext* pContext)
    {
    	// create a splitter with 1 row, 2 columns
    	if (!m_wndSplitter.CreateStatic(this, 1, 2))
    	{
    		TRACE0("Failed to CreateStaticSplitter\n");
    		return FALSE;
    	}
    
    	// add the first splitter pane - the default view in column 0
    	if (!m_wndSplitter.CreateView(0, 0,
    		pContext->m_pNewViewClass, CSize(130, 50), pContext))
    	{
    		TRACE0("Failed to create first pane\n");
    		return FALSE;
    	}
    
    	// add the second splitter pane - an input view in column 1
    	if (!m_wndSplitter.CreateView(0, 1,
    		RUNTIME_CLASS(CInputView), CSize(0, 0), pContext))
    	{
    		TRACE0("Failed to create second pane\n");
    		return FALSE;
    	}
    
    	// activate the input view
    	SetActiveView((CView*)m_wndSplitter.GetPane(0,1));
    
    	return TRUE;
    }
    Victor Nijegorodov

  6. #6
    sunnysky Guest

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    Thanks Victor. But I think it is not enough. I added "SetActiveView((CView*)m_wndSplitter.GetPane(0, 0));" before "return TRUE;" and it still doesn't work.

    I stepped into CFrameWnd::GetActiveDocument() and found "CView* pView = GetActiveView();" still set pView to be NULL. There must be something else missed.

    Also I think I need to set the member m_pDocument of my CFirstView and CSecondView objects to be the created CDocument object, but I haven't found how to do that either.

    I will study the viewex sample to try to get more hints. I really don't find MSDN documents very helpful.

    Quote Originally Posted by VictorN View Post
    You seem to have created your splitter from scratch without careful reading the docs and playing with samples like viewex.
    Thus you missed to activate a view as Igor already mentioned.
    However if you had looked at the viewex code you would see this important line:
    Code:
    BOOL CSplitterFrame::OnCreateClient(LPCREATESTRUCT,
    	 CCreateContext* pContext)
    {
    	// create a splitter with 1 row, 2 columns
    	if (!m_wndSplitter.CreateStatic(this, 1, 2))
    	{
    		TRACE0("Failed to CreateStaticSplitter\n");
    		return FALSE;
    	}
    
    	// add the first splitter pane - the default view in column 0
    	if (!m_wndSplitter.CreateView(0, 0,
    		pContext->m_pNewViewClass, CSize(130, 50), pContext))
    	{
    		TRACE0("Failed to create first pane\n");
    		return FALSE;
    	}
    
    	// add the second splitter pane - an input view in column 1
    	if (!m_wndSplitter.CreateView(0, 1,
    		RUNTIME_CLASS(CInputView), CSize(0, 0), pContext))
    	{
    		TRACE0("Failed to create second pane\n");
    		return FALSE;
    	}
    
    	// activate the input view
    	SetActiveView((CView*)m_wndSplitter.GetPane(0,1));
    
    	return TRUE;
    }

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    Quote Originally Posted by sunnysky View Post
    Thanks Victor. But I think it is not enough. I added "SetActiveView((CView*)m_wndSplitter.GetPane(0, 0));" before "return TRUE;" and it still doesn't work.

    I stepped into CFrameWnd::GetActiveDocument() and found "CView* pView = GetActiveView();" still set pView to be NULL. There must be something else missed.
    Could you post a very small test project (in zip archive noty including Debug/Release folders nor .aps, .res, .opt, .ncb files) reproducing the problem?
    Victor Nijegorodov

  8. #8
    sunnysky Guest

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    Hello Victor,

    Here it is.

    Thanks,
    Brian
    Attached Files Attached Files

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    The problem seems to be in the base class of your CSecondView class.
    Since there is no any tab then the CTabView::OnInitialUpdate() method calls
    Code:
    	OnChangeActiveTab(m_nFirstActiveTab, 0);
    passing m_nFirstActiveTab being initialized to -1.
    It causes to set the active view to BULL here:
    Code:
    LRESULT CTabView::OnChangeActiveTab(WPARAM wp, LPARAM)
    {
    	if (!m_bIsReady)
    	{
    		m_nFirstActiveTab = (int) wp;
    		return 0;
    	}
    
    	CFrameWnd* pFrame = AFXGetParentFrame(this);
    	ASSERT_VALID(pFrame);
    
    	int iTabNum = (int) wp;
    	if (iTabNum >= 0)
    	{
    		CView* pView = DYNAMIC_DOWNCAST(CView, m_wndTabs.GetTabWnd(iTabNum));
    		ASSERT_VALID(pView);
    
    		pFrame->SetActiveView(pView);
    
    		OnActivateView(pView);
    	}
    	else
    	{
    		pFrame->SetActiveView(NULL);
    
    		OnActivateView(NULL);
    	}
    
    	return 0;
    }
    Besides, you do not provide the GetDocument methods in your view classes. Why?
    Victor Nijegorodov

  10. #10
    sunnysky Guest

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    Thanks Victor.

    I created these view classes with the help of wizard and didn't add any code. My problem is that other than application specific members, I don't know which methods should be implemented by me and which ones are already provided by the framework.

  11. #11
    sunnysky Guest

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    For instance, the examples I saw don't call "CFrameWnd::OnCreateClient(lpcs, pContext)" in "CMainFrame::OnCreateClient( LPCREATESTRUCT lpcs, CCreateContext* pContext )", but I found that if I don't do that, I will have problems.

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    You could start with creating using the AppWizard a new SDI project with "Windows Explorer" style. Then remove unndeeded code, add/change what you need and use it!
    Victor Nijegorodov

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    Quote Originally Posted by sunnysky View Post
    the examples I saw don't call "CFrameWnd::OnCreateClient(lpcs, pContext)" in "CMainFrame::OnCreateClient( LPCREATESTRUCT lpcs, CCreateContext* pContext )", but I found that if I don't do that, I will have problems.
    What problems will you have?
    Victor Nijegorodov

  14. #14
    sunnysky Guest

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    But that will sacrifice flexibility. The Wizard only provide CListView and CTreeView.

    Quote Originally Posted by VictorN View Post
    You could start with creating using the AppWizard a new SDI project with "Windows Explorer" style. Then remove unndeeded code, add/change what you need and use it!

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Adding Splitter Windows Makes Mainframe Unable to Get Current Document

    But you will see all the needed methods/calls/initializations!
    Then compare with your own code... Perhaps you will want to add something tha your code does not have...
    Victor Nijegorodov

Page 1 of 3 123 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center