mdig.cpp

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 820 行 · 第 1/2 页

CPP
820
字号
    if (pParentFrame != NULL)
    {
        bool bActive = false;
        if (pParentFrame->GetActiveChild() == this)
        {
            pParentFrame->SetActiveChild((wxGenericMDIChildFrame*) NULL);
            pParentFrame->SetChildMenuBar((wxGenericMDIChildFrame*) NULL);
            bActive = true;
        }

        wxGenericMDIClientWindow *pClientWindow = pParentFrame->GetClientWindow();

        // Remove page if still there
        size_t pos;
        for (pos = 0; pos < pClientWindow->GetPageCount(); pos++)
        {
            if (pClientWindow->GetPage(pos) == this)
            {
                if (pClientWindow->RemovePage(pos))
                    pClientWindow->Refresh();
                break;
            }
        }

        if (bActive)
        {
            // Set the new selection to the a remaining page
            if (pClientWindow->GetPageCount() > pos)
            {
                pClientWindow->SetSelection(pos);
            }
            else
            {
                if ((int)pClientWindow->GetPageCount() - 1 >= 0)
                    pClientWindow->SetSelection(pClientWindow->GetPageCount() - 1);
            }
        }
    }

#if wxUSE_MENUS
    wxDELETE(m_pMenuBar);
#endif // wxUSE_MENUS
}

bool wxGenericMDIChildFrame::Create( wxGenericMDIParentFrame *parent,
      wxWindowID id, const wxString& title,
      const wxPoint& WXUNUSED(pos), const wxSize& size,
      long style, const wxString& name )
{
    wxGenericMDIClientWindow* pClientWindow = parent->GetClientWindow();

    wxASSERT_MSG((pClientWindow != (wxWindow*) NULL), wxT("Missing MDI client window.") );

    wxPanel::Create(pClientWindow, id, wxDefaultPosition, size, style, name);

    SetMDIParentFrame(parent);

    // This is the currently active child
    parent->SetActiveChild(this);

    m_Title = title;

    pClientWindow->AddPage(this, title, true);
    ApplyMDIChildFrameRect();   // Ok confirme the size change!
    pClientWindow->Refresh();

    return true;
}

#if wxUSE_MENUS
void wxGenericMDIChildFrame::SetMenuBar( wxMenuBar *menu_bar )
{
    wxMenuBar *pOldMenuBar = m_pMenuBar;
    m_pMenuBar = menu_bar;

    if (m_pMenuBar)
    {
        wxGenericMDIParentFrame *pParentFrame = GetMDIParentFrame();

        if (pParentFrame != NULL)
        {
            m_pMenuBar->SetParent(pParentFrame);

            if (pParentFrame->GetActiveChild() == this)
            {
                // Replace current menu bars
                if (pOldMenuBar)
                    pParentFrame->SetChildMenuBar((wxGenericMDIChildFrame*) NULL);
                pParentFrame->SetChildMenuBar((wxGenericMDIChildFrame*) this);
            }
        }
    }
}

wxMenuBar *wxGenericMDIChildFrame::GetMenuBar() const
{
    return m_pMenuBar;
}
#endif // wxUSE_MENUS

void wxGenericMDIChildFrame::SetTitle(const wxString& title)
{
    m_Title = title;

    wxGenericMDIParentFrame *pParentFrame = GetMDIParentFrame();

    if (pParentFrame != NULL)
    {
        wxGenericMDIClientWindow * pClientWindow = pParentFrame->GetClientWindow();

        if (pClientWindow != NULL)
        {
            size_t pos;
            for (pos = 0; pos < pClientWindow->GetPageCount(); pos++)
            {
                if (pClientWindow->GetPage(pos) == this)
                {
                    pClientWindow->SetPageText(pos, m_Title);
                    break;
                }
            }
        }
    }
}

wxString wxGenericMDIChildFrame::GetTitle() const
{
    return m_Title;
}

void wxGenericMDIChildFrame::Activate()
{
    wxGenericMDIParentFrame *pParentFrame = GetMDIParentFrame();

    if (pParentFrame != NULL)
    {
        wxGenericMDIClientWindow * pClientWindow = pParentFrame->GetClientWindow();

        if (pClientWindow != NULL)
        {
            size_t pos;
            for (pos = 0; pos < pClientWindow->GetPageCount(); pos++)
            {
                if (pClientWindow->GetPage(pos) == this)
                {
                    pClientWindow->SetSelection(pos);
                    break;
                }
            }
        }
    }
}

void wxGenericMDIChildFrame::OnMenuHighlight(wxMenuEvent& event)
{
#if wxUSE_STATUSBAR
    if ( m_pMDIParentFrame)
    {
        // we don't have any help text for this item,
        // but may be the MDI frame does?
        m_pMDIParentFrame->OnMenuHighlight(event);
    }
#else
    wxUnusedVar(event);
#endif // wxUSE_STATUSBAR
}

void wxGenericMDIChildFrame::OnActivate(wxActivateEvent& WXUNUSED(event))
{
    // Do mothing.
}

/*** Copied from top level..! ***/
// default resizing behaviour - if only ONE subwindow, resize to fill the
// whole client area
void wxGenericMDIChildFrame::OnSize(wxSizeEvent& WXUNUSED(event))
{
    // if we're using constraints or sizers - do use them
    if ( GetAutoLayout() )
    {
        Layout();
    }
    else
    {
        // do we have _exactly_ one child?
        wxWindow *child = (wxWindow *)NULL;
        for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
              node;
              node = node->GetNext() )
        {
            wxWindow *win = node->GetData();

            // exclude top level and managed windows (status bar isn't
            // currently in the children list except under wxMac anyhow, but
            // it makes no harm to test for it)
            if ( !win->IsTopLevel() /*&& !IsOneOfBars(win)*/ )
            {
                if ( child )
                {
                    return;     // it's our second subwindow - nothing to do
                }

                child = win;
            }
        }

        // do we have any children at all?
        if ( child )
        {
            // exactly one child - set it's size to fill the whole frame
            int clientW, clientH;
            DoGetClientSize(&clientW, &clientH);

            // for whatever reasons, wxGTK wants to have a small offset - it
            // probably looks better with it?
#ifdef __WXGTK__
            static const int ofs = 1;
#else
            static const int ofs = 0;
#endif

            child->SetSize(ofs, ofs, clientW - 2*ofs, clientH - 2*ofs);
        }
    }
}

/*** Copied from top level..! ***/
// The default implementation for the close window event.
void wxGenericMDIChildFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
{
    Destroy();
}

void wxGenericMDIChildFrame::SetMDIParentFrame(wxGenericMDIParentFrame* parentFrame)
{
    m_pMDIParentFrame = parentFrame;
}

wxGenericMDIParentFrame* wxGenericMDIChildFrame::GetMDIParentFrame() const
{
    return m_pMDIParentFrame;
}

void wxGenericMDIChildFrame::Init()
{
    m_pMDIParentFrame = (wxGenericMDIParentFrame *) NULL;
#if wxUSE_MENUS
    m_pMenuBar = (wxMenuBar *) NULL;
#endif // wxUSE_MENUS
}

void wxGenericMDIChildFrame::DoMoveWindow(int x, int y, int width, int height)
{
    m_MDIRect = wxRect(x, y, width, height);
}

void wxGenericMDIChildFrame::ApplyMDIChildFrameRect()
{
    wxPanel::DoMoveWindow(m_MDIRect.x, m_MDIRect.y, m_MDIRect.width, m_MDIRect.height);
}

//-----------------------------------------------------------------------------
// wxGenericMDIClientWindow
//-----------------------------------------------------------------------------

#define wxID_NOTEBOOK_CLIENT_AREA wxID_HIGHEST + 100

IMPLEMENT_DYNAMIC_CLASS(wxGenericMDIClientWindow, wxNotebook)

BEGIN_EVENT_TABLE(wxGenericMDIClientWindow, wxNotebook)
    EVT_NOTEBOOK_PAGE_CHANGED(wxID_NOTEBOOK_CLIENT_AREA, wxGenericMDIClientWindow::OnPageChanged)
    EVT_SIZE(wxGenericMDIClientWindow::OnSize)
END_EVENT_TABLE()


wxGenericMDIClientWindow::wxGenericMDIClientWindow()
{
}

wxGenericMDIClientWindow::wxGenericMDIClientWindow( wxGenericMDIParentFrame *parent, long style )
{
    CreateClient( parent, style );
}

wxGenericMDIClientWindow::~wxGenericMDIClientWindow()
{
    DestroyChildren();
}

bool wxGenericMDIClientWindow::CreateClient( wxGenericMDIParentFrame *parent, long style )
{
    SetWindowStyleFlag(style);

    bool success = wxNotebook::Create(parent, wxID_NOTEBOOK_CLIENT_AREA, wxPoint(0,0), wxSize(100, 100), 0);
    if (success)
    {
        /*
        wxFont font(10, wxSWISS, wxNORMAL, wxNORMAL);
        wxFont selFont(10, wxSWISS, wxNORMAL, wxBOLD);
        GetTabView()->SetTabFont(font);
        GetTabView()->SetSelectedTabFont(selFont);
        GetTabView()->SetTabSize(120, 18);
        GetTabView()->SetTabSelectionHeight(20);
        */
        return true;
    }
    else
        return false;
}

int wxGenericMDIClientWindow::SetSelection(size_t nPage)
{
    int oldSelection = wxNotebook::SetSelection(nPage);

#if !defined(__WXMSW__) // No need to do this for wxMSW as wxNotebook::SetSelection()
                        // will already cause this to be done!
    // Handle the page change.
    PageChanged(oldSelection, nPage);
#endif

    return oldSelection;
}

void wxGenericMDIClientWindow::PageChanged(int OldSelection, int newSelection)
{
    // Don't do to much work, only when something realy should change!
    if (OldSelection == newSelection)
        return;
    // Again check if we realy need to do this...
    if (newSelection != -1)
    {
        wxGenericMDIChildFrame* child = (wxGenericMDIChildFrame *)GetPage(newSelection);

        if (child->GetMDIParentFrame()->GetActiveChild() == child)
            return;
    }

    // Notify old active child that it has been deactivated
    if (OldSelection != -1)
    {
        wxGenericMDIChildFrame* oldChild = (wxGenericMDIChildFrame *)GetPage(OldSelection);
        if (oldChild)
        {
            wxActivateEvent event(wxEVT_ACTIVATE, false, oldChild->GetId());
            event.SetEventObject( oldChild );
            oldChild->GetEventHandler()->ProcessEvent(event);
        }
    }

    // Notify new active child that it has been activated
    if (newSelection != -1)
    {
        wxGenericMDIChildFrame* activeChild = (wxGenericMDIChildFrame *)GetPage(newSelection);
        if (activeChild)
        {
            wxActivateEvent event(wxEVT_ACTIVATE, true, activeChild->GetId());
            event.SetEventObject( activeChild );
            activeChild->GetEventHandler()->ProcessEvent(event);

            if (activeChild->GetMDIParentFrame())
            {
                activeChild->GetMDIParentFrame()->SetActiveChild(activeChild);
                activeChild->GetMDIParentFrame()->SetChildMenuBar(activeChild);
            }
        }
    }
}

void wxGenericMDIClientWindow::OnPageChanged(wxNotebookEvent& event)
{
    PageChanged(event.GetOldSelection(), event.GetSelection());

    event.Skip();
}

void wxGenericMDIClientWindow::OnSize(wxSizeEvent& event)
{
    wxNotebook::OnSize(event);

    size_t pos;
    for (pos = 0; pos < GetPageCount(); pos++)
    {
        ((wxGenericMDIChildFrame *)GetPage(pos))->ApplyMDIChildFrameRect();
    }
}


/*
 * Define normal wxMDI classes based on wxGenericMDI
 */

#if wxUSE_GENERIC_MDI_AS_NATIVE

wxMDIChildFrame * wxMDIParentFrame::GetActiveChild() const
    {
        wxGenericMDIChildFrame *pGFrame = wxGenericMDIParentFrame::GetActiveChild();
        wxMDIChildFrame *pFrame = wxDynamicCast(pGFrame, wxMDIChildFrame);

        wxASSERT_MSG(!(pFrame == NULL && pGFrame != NULL), wxT("Active frame is class not derived from wxMDIChildFrame!"));

        return pFrame;
    }

IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame, wxGenericMDIParentFrame)
IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame, wxGenericMDIChildFrame)
IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow, wxGenericMDIClientWindow)

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?