notebook.cpp

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,435 行 · 第 1/3 页

CPP
1,435
字号
/////////////////////////////////////////////////////////////////////////////
// Name:        univ/notebook.cpp
// Purpose:     wxNotebook implementation
// Author:      Vadim Zeitlin
// Modified by:
// Created:     01.02.01
// RCS-ID:      $Id: notebook.cpp,v 1.35 2005/02/27 10:36:58 JS Exp $
// Copyright:   (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
    #pragma implementation "univnotebook.h"
#endif

#ifdef __VMS
#pragma message disable unscomzer
#endif

#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#if wxUSE_NOTEBOOK

#include "wx/imaglist.h"
#include "wx/notebook.h"
#include "wx/spinbutt.h"
#include "wx/dcmemory.h"

#include "wx/univ/renderer.h"

// ----------------------------------------------------------------------------
// macros
// ----------------------------------------------------------------------------

#if 0
// due to unsigned type nPage is always >= 0
#define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((size_t(nPage)) < GetPageCount()))
#else
#define IS_VALID_PAGE(nPage) (((size_t)nPage) < GetPageCount())
#endif

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

static const size_t INVALID_PAGE = (size_t)-1;

DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)

// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------

class wxNotebookSpinBtn : public wxSpinButton
{
public:
    wxNotebookSpinBtn(wxNotebook *nb)
        : wxSpinButton(nb, wxID_ANY,
                       wxDefaultPosition, wxDefaultSize,
                       nb->IsVertical() ? wxSP_VERTICAL : wxSP_HORIZONTAL)
    {
        m_nb = nb;
    }

protected:
    void OnSpin(wxSpinEvent& event)
    {
        m_nb->PerformAction(wxACTION_NOTEBOOK_GOTO, event.GetPosition());
    }

private:
    wxNotebook *m_nb;

    DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(wxNotebookSpinBtn, wxSpinButton)
    EVT_SPIN(wxID_ANY, wxNotebookSpinBtn::OnSpin)
END_EVENT_TABLE()

// ============================================================================
// implementation
// ============================================================================

IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)

// ----------------------------------------------------------------------------
// wxNotebook creation
// ----------------------------------------------------------------------------

void wxNotebook::Init()
{
    m_sel = INVALID_PAGE;

    m_heightTab =
    m_widthMax = 0;

    m_firstVisible =
    m_lastVisible =
    m_lastFullyVisible = 0;

    m_offset = 0;

    m_spinbtn = NULL;
}

bool wxNotebook::Create(wxWindow *parent,
                        wxWindowID id,
                        const wxPoint& pos,
                        const wxSize& size,
                        long style,
                        const wxString& name)
{
    if ( !wxControl::Create(parent, id, pos, size, style,
                            wxDefaultValidator, name) )
        return false;

    m_sizePad = GetRenderer()->GetTabPadding();

    SetBestSize(size);

    CreateInputHandler(wxINP_HANDLER_NOTEBOOK);

    return true;
}

// ----------------------------------------------------------------------------
// wxNotebook page titles and images
// ----------------------------------------------------------------------------

wxString wxNotebook::GetPageText(size_t nPage) const
{
    wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, _T("invalid notebook page") );

    return m_titles[nPage];
}

bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
{
    wxCHECK_MSG( IS_VALID_PAGE(nPage), false, _T("invalid notebook page") );

    if ( strText != m_titles[nPage] )
    {
        m_accels[nPage] = FindAccelIndex(strText, &m_titles[nPage]);

        if ( FixedSizeTabs() )
        {
            // it's enough to just reresh this one
            RefreshTab(nPage);
        }
        else // var width tabs
        {
            // we need to resize the tab to fit the new string
            ResizeTab(nPage);
        }
    }

    return true;
}

int wxNotebook::GetPageImage(size_t nPage) const
{
    wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("invalid notebook page") );

    return m_images[nPage];
}

bool wxNotebook::SetPageImage(size_t nPage, int nImage)
{
    wxCHECK_MSG( IS_VALID_PAGE(nPage), false, _T("invalid notebook page") );

    wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), false,
                 _T("invalid image index in SetPageImage()") );

    if ( nImage != m_images[nPage] )
    {
        // if the item didn't have an icon before or, on the contrary, did have
        // it but has lost it now, its size will change - but if the icon just
        // changes, it won't
        bool tabSizeChanges = nImage == -1 || m_images[nPage] == -1;
        m_images[nPage] = nImage;

        if ( tabSizeChanges )
            RefreshAllTabs();
        else
            RefreshTab(nPage);
    }

    return true;
}

wxNotebook::~wxNotebook()
{
}

// ----------------------------------------------------------------------------
// wxNotebook page switching
// ----------------------------------------------------------------------------

int wxNotebook::SetSelection(size_t nPage)
{
    wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("invalid notebook page") );

    if ( (size_t)nPage == m_sel )
    {
        // don't do anything if there is nothing to do
        return m_sel;
    }

    // event handling
    wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
    event.SetSelection(nPage);
    event.SetOldSelection(m_sel);
    event.SetEventObject(this);
    if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
    {
        // program doesn't allow the page change
        return m_sel;
    }

    // we need to change m_sel first, before calling RefreshTab() below as
    // otherwise the previously selected tab wouldn't be redrawn properly under
    // wxGTK which calls Refresh() immediately and not during the next event
    // loop iteration as wxMSW does and as it should
    size_t selOld = m_sel;

    m_sel = nPage;

    if ( selOld != INVALID_PAGE )
    {
        RefreshTab(selOld, true /* this tab was selected */);

        m_pages[selOld]->Hide();
    }

    if ( m_sel != INVALID_PAGE ) // this is impossible - but test nevertheless
    {
        if ( HasSpinBtn() )
        {
            // keep it in sync
            m_spinbtn->SetValue(m_sel);
        }

        if ( m_sel < m_firstVisible )
        {
            // selection is to the left of visible part of tabs
            ScrollTo(m_sel);
        }
        else if ( m_sel > m_lastFullyVisible )
        {
            // selection is to the right of visible part of tabs
            ScrollLastTo(m_sel);
        }
        else // we already see this tab
        {
            // no need to scroll
            RefreshTab(m_sel);
        }

        m_pages[m_sel]->SetSize(GetPageRect());
        m_pages[m_sel]->Show();
    }

    // event handling
    event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
    GetEventHandler()->ProcessEvent(event);

    return selOld;
}

// ----------------------------------------------------------------------------
// wxNotebook pages adding/deleting
// ----------------------------------------------------------------------------

bool wxNotebook::InsertPage(size_t nPage,
                            wxNotebookPage *pPage,
                            const wxString& strText,
                            bool bSelect,
                            int imageId)
{
    size_t nPages = GetPageCount();
    wxCHECK_MSG( nPage == nPages || IS_VALID_PAGE(nPage), false,
                 _T("invalid notebook page in InsertPage()") );

    // modify the data
    m_pages.Insert(pPage, nPage);

    wxString label;
    m_accels.Insert(FindAccelIndex(strText, &label), nPage);
    m_titles.Insert(label, nPage);

    m_images.Insert(imageId, nPage);

    // cache the tab geometry here
    wxSize sizeTab = CalcTabSize(nPage);

    if ( sizeTab.y > m_heightTab )
        m_heightTab = sizeTab.y;

    if ( FixedSizeTabs() && sizeTab.x > m_widthMax )
        m_widthMax = sizeTab.x;

    m_widths.Insert(sizeTab.x, nPage);

    // spin button may appear if we didn't have it before - but even if we did,
    // its range should change, so update it unconditionally
    UpdateSpinBtn();

    // if the tab has just appeared, we have to relayout everything, otherwise
    // it's enough to just redraw the tabs
    if ( nPages == 0 )
    {
        // always select the first tab to have at least some selection
        bSelect = true;

        Relayout();
        Refresh();
    }
    else // not the first tab
    {
        RefreshAllTabs();
    }

    if ( bSelect )
    {
        SetSelection(nPage);
    }
    else // pages added to the notebook are initially hidden
    {
        pPage->Hide();
    }

    return true;
}

bool wxNotebook::DeleteAllPages()
{
    if ( !wxNotebookBase::DeleteAllPages() )
        return false;

    // clear the other arrays as well
    m_titles.Clear();
    m_images.Clear();
    m_accels.Clear();
    m_widths.Clear();

    // it is not valid any longer
    m_sel = INVALID_PAGE;

    // spin button is not needed any more
    UpdateSpinBtn();

    Relayout();

    return true;
}

wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
{
    wxCHECK_MSG( IS_VALID_PAGE(nPage), NULL, _T("invalid notebook page") );

    wxNotebookPage *page = m_pages[nPage];
    m_pages.RemoveAt(nPage);
    m_titles.RemoveAt(nPage);
    m_accels.RemoveAt(nPage);
    m_widths.RemoveAt(nPage);
    m_images.RemoveAt(nPage);

    // the spin button might not be needed any more
    // 2002-08-12 'if' commented out by JACS on behalf
    // of Hans Van Leemputten <Hansvl@softhome.net> who
    // points out that UpdateSpinBtn should always be called,
    // to ensure m_lastVisible is up to date.
    // if ( HasSpinBtn() )
    {
        UpdateSpinBtn();
    }

    size_t count = GetPageCount();
    if ( count )
    {
        if ( m_sel == (size_t)nPage )
        {
            // avoid sending event to this page which doesn't exist in the
            // notebook any more
            m_sel = INVALID_PAGE;

            SetSelection(nPage == count ? nPage - 1 : nPage);
        }
        else if ( m_sel > (size_t)nPage )
        {
            // no need to change selection, just adjust the index
            m_sel--;
        }
    }
    else // no more tabs left
    {
        m_sel = INVALID_PAGE;
    }

    // have to refresh everything
    Relayout();

    return page;
}

// ----------------------------------------------------------------------------
// wxNotebook drawing
// ----------------------------------------------------------------------------

void wxNotebook::RefreshCurrent()
{
    if ( m_sel != INVALID_PAGE )
    {
        RefreshTab(m_sel);
    }
}

void wxNotebook::RefreshTab(int page, bool forceSelected)
{
    wxCHECK_RET( IS_VALID_PAGE(page), _T("invalid notebook page") );

    wxRect rect = GetTabRect(page);
    if ( forceSelected || ((size_t)page == m_sel) )
    {
        const wxSize indent = GetRenderer()->GetTabIndent();
        rect.Inflate(indent.x, indent.y);
    }

    RefreshRect(rect);
}

void wxNotebook::RefreshAllTabs()
{
    wxRect rect = GetAllTabsRect();
    if ( rect.width || rect.height )
    {
        RefreshRect(rect);
    }
    //else: we don't have tabs at all
}

void wxNotebook::DoDrawTab(wxDC& dc, const wxRect& rect, size_t n)
{
    wxBitmap bmp;
    if ( HasImage(n) )
    {
        int image = m_images[n];

        // Not needed now that wxGenericImageList is being
        // used for wxUniversal under MSW
#if 0 // def __WXMSW__    // FIXME
        int w, h;
        m_imageList->GetSize(n, w, h);
        bmp.Create(w, h);
        wxMemoryDC dc;
        dc.SelectObject(bmp);
        dc.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID));
        m_imageList->Draw(image, dc, 0, 0, wxIMAGELIST_DRAW_NORMAL, true);
        dc.SelectObject(wxNullBitmap);
#else
        bmp = m_imageList->GetBitmap(image);
#endif
    }

⌨️ 快捷键说明

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