gridsel.cpp

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

CPP
1,132
字号
///////////////////////////////////////////////////////////////////////////
// Name:        generic/gridsel.cpp
// Purpose:     wxGridSelection
// Author:      Stefan Neis
// Modified by:
// Created:     20/02/1999
// RCS-ID:      $Id: gridsel.cpp,v 1.22 2005/06/06 16:46:50 ABX Exp $
// Copyright:   (c) Stefan Neis (Stefan.Neis@t-online.de)
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

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

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

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

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#include "wx/defs.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#if wxUSE_GRID

#include "wx/generic/gridsel.h"

// Some explanation for the members of the class:
// m_cellSelection stores individual selected cells
//   -- this is only used if m_selectionMode == wxGridSelectCells
// m_blockSelectionTopLeft and m_blockSelectionBottomRight
//   store the upper left and lower right corner of selected Blocks
// m_rowSelection and m_colSelection store individual selected
//   rows and columns; maybe those are superfluous and should be
//   treated as blocks?

wxGridSelection::wxGridSelection( wxGrid * grid,
                                  wxGrid::wxGridSelectionModes sel )
{
    m_grid = grid;
    m_selectionMode = sel;
}

bool wxGridSelection::IsSelection()
{
  return ( m_cellSelection.GetCount() || m_blockSelectionTopLeft.GetCount() ||
           m_rowSelection.GetCount() || m_colSelection.GetCount() );
}

bool wxGridSelection::IsInSelection ( int row, int col )
{
    size_t count;

    // First check whether the given cell is individually selected
    // (if m_selectionMode is wxGridSelectCells).
    if ( m_selectionMode == wxGrid::wxGridSelectCells )
    {
        count = m_cellSelection.GetCount();
        for ( size_t n = 0; n < count; n++ )
        {
            wxGridCellCoords& coords = m_cellSelection[n];
            if ( row == coords.GetRow() && col == coords.GetCol() )
                return true;
        }
    }

    // Now check whether the given cell is
    // contained in one of the selected blocks.
    count = m_blockSelectionTopLeft.GetCount();
    for ( size_t n = 0; n < count; n++ )
    {
        wxGridCellCoords& coords1 = m_blockSelectionTopLeft[n];
        wxGridCellCoords& coords2 = m_blockSelectionBottomRight[n];
        if ( BlockContainsCell(coords1.GetRow(), coords1.GetCol(),
                               coords2.GetRow(), coords2.GetCol(),
                               row, col ) )
            return true;
    }

    // Now check whether the given cell is
    // contained in one of the selected rows
    // (unless we are in column selection mode).
    if ( m_selectionMode != wxGrid::wxGridSelectColumns )
    {
        size_t count = m_rowSelection.GetCount();
        for ( size_t n = 0; n < count; n++ )
        {
            if ( row == m_rowSelection[n] )
              return true;
        }
    }

    // Now check whether the given cell is
    // contained in one of the selected columns
    // (unless we are in row selection mode).
    if ( m_selectionMode != wxGrid::wxGridSelectRows )
    {
        size_t count = m_colSelection.GetCount();
        for ( size_t n = 0; n < count; n++ )
        {
            if ( col == m_colSelection[n] )
              return true;
        }
    }
    return false;
}

// Change the selection mode
void wxGridSelection::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
{
    // if selection mode is unchanged return immediately
    if (selmode == m_selectionMode)
        return;

    if ( m_selectionMode != wxGrid::wxGridSelectCells )
    {
        // if changing form row to column selection
        // or vice versa, clear the selection.
        if ( selmode != wxGrid::wxGridSelectCells )
            ClearSelection();

        m_selectionMode = selmode;
    }
    else
    {
        // if changing from cell selection to something else,
        // promote selected cells/blocks to whole rows/columns.
        size_t n;
        while ( ( n = m_cellSelection.GetCount() ) > 0 )
        {
            n--;
            wxGridCellCoords& coords = m_cellSelection[n];
            int row = coords.GetRow();
            int col = coords.GetCol();
            m_cellSelection.RemoveAt(n);
            if (selmode == wxGrid::wxGridSelectRows)
                SelectRow( row );
            else // selmode == wxGridSelectColumns)
                SelectCol( col );
        }

        for (n = 0; n < m_blockSelectionTopLeft.GetCount(); n++)
        // Note that m_blockSelectionTopLeft's size may be changing!
        {
            wxGridCellCoords& coords = m_blockSelectionTopLeft[n];
            int topRow = coords.GetRow();
            int leftCol = coords.GetCol();
            coords = m_blockSelectionBottomRight[n];
            int bottomRow = coords.GetRow();
            int rightCol = coords.GetCol();
            if (selmode == wxGrid::wxGridSelectRows)
            {
                if (leftCol != 0 || rightCol != m_grid->GetNumberCols() - 1 )
                {
                    m_blockSelectionTopLeft.RemoveAt(n);
                    m_blockSelectionBottomRight.RemoveAt(n);
                    SelectBlock( topRow, 0,
                                 bottomRow, m_grid->GetNumberCols() - 1,
                                 false, false, false, false, false );
                }
            }
            else // selmode == wxGridSelectColumns)
            {
                if (topRow != 0 || bottomRow != m_grid->GetNumberRows() - 1 )
                {
                    m_blockSelectionTopLeft.RemoveAt(n);
                    m_blockSelectionBottomRight.RemoveAt(n);
                    SelectBlock( 0, leftCol,
                                 m_grid->GetNumberRows() - 1, rightCol,
                                 false, false, false, false, false );
                }
            }
        }
        m_selectionMode = selmode;
    }
}

void wxGridSelection::SelectRow( int row,
                                 bool ControlDown,  bool ShiftDown,
                                 bool AltDown, bool MetaDown )
{
    if ( m_selectionMode == wxGrid::wxGridSelectColumns )
        return;
    size_t count, n;

    // Remove single cells contained in newly selected block.
    if ( m_selectionMode == wxGrid::wxGridSelectCells )
    {
        count = m_cellSelection.GetCount();
        for ( n = 0; n < count; n++ )
        {
            wxGridCellCoords& coords = m_cellSelection[n];
            if ( BlockContainsCell( row, 0, row, m_grid->GetNumberCols() - 1,
                                    coords.GetRow(), coords.GetCol() ) )
            {
                m_cellSelection.RemoveAt(n);
                n--; count--;
            }
        }
    }

    // Simplify list of selected blocks (if possible)
    count = m_blockSelectionTopLeft.GetCount();
    bool done = false;
    for ( n = 0; n < count; n++ )
    {
        wxGridCellCoords& coords1 = m_blockSelectionTopLeft[n];
        wxGridCellCoords& coords2 = m_blockSelectionBottomRight[n];

        // Remove block if it is a subset of the row
        if ( coords1.GetRow() == row && row == coords2.GetRow() )
        {
            m_blockSelectionTopLeft.RemoveAt(n);
            m_blockSelectionBottomRight.RemoveAt(n);
            n--; count--;
        }
        else if ( coords1.GetCol() == 0  &&
                  coords2.GetCol() == m_grid->GetNumberCols() - 1 )
        {
            // silently return, if row is contained in block
            if ( coords1.GetRow() <= row && row <= coords2.GetRow() )
                return;
            // expand block, if it touched row
            else if ( coords1.GetRow() == row + 1)
            {
                coords1.SetRow(row);
                done = true;
            }
            else if ( coords2.GetRow() == row - 1)
            {
                coords2.SetRow(row);
                done = true;
            }
        }
    }

    // Unless we successfully handled the row,
    // check whether row is already selected.
    if ( !done )
    {
        count = m_rowSelection.GetCount();
        for ( n = 0; n < count; n++ )
        {
            if ( row == m_rowSelection[n] )
                return;
        }

        // Add row to selection
        m_rowSelection.Add(row);
    }

    // Update View:
    if ( !m_grid->GetBatchCount() )
    {
        wxRect r = m_grid->BlockToDeviceRect( wxGridCellCoords( row, 0 ),
                                              wxGridCellCoords( row, m_grid->GetNumberCols() - 1 ) );
        ((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
    }

    // Send Event
    wxGridRangeSelectEvent gridEvt( m_grid->GetId(),
                                    wxEVT_GRID_RANGE_SELECT,
                                    m_grid,
                                    wxGridCellCoords( row, 0 ),
                                    wxGridCellCoords( row, m_grid->GetNumberCols() - 1 ),
                                    true,
                                    ControlDown,  ShiftDown,
                                    AltDown, MetaDown );

    m_grid->GetEventHandler()->ProcessEvent(gridEvt);
}

void wxGridSelection::SelectCol( int col,
                                 bool ControlDown,  bool ShiftDown,
                                 bool AltDown, bool MetaDown )
{
    if ( m_selectionMode == wxGrid::wxGridSelectRows )
        return;
    size_t count, n;

    // Remove single cells contained in newly selected block.
    if ( m_selectionMode == wxGrid::wxGridSelectCells )
    {
        count = m_cellSelection.GetCount();
        for ( n = 0; n < count; n++ )
        {
            wxGridCellCoords& coords = m_cellSelection[n];
            if ( BlockContainsCell( 0, col, m_grid->GetNumberRows() - 1, col,
                                    coords.GetRow(), coords.GetCol() ) )
            {
                m_cellSelection.RemoveAt(n);
                n--; count--;
            }
        }
    }

    // Simplify list of selected blocks (if possible)
    count = m_blockSelectionTopLeft.GetCount();
    bool done = false;
    for ( n = 0; n < count; n++ )
    {
        wxGridCellCoords& coords1 = m_blockSelectionTopLeft[n];
        wxGridCellCoords& coords2 = m_blockSelectionBottomRight[n];

        // Remove block if it is a subset of the column
        if ( coords1.GetCol() == col && col == coords2.GetCol() )
        {
            m_blockSelectionTopLeft.RemoveAt(n);
            m_blockSelectionBottomRight.RemoveAt(n);
            n--; count--;
        }
        else if ( coords1.GetRow() == 0  &&
                  coords2.GetRow() == m_grid->GetNumberRows() - 1 )
        {
            // silently return, if row is contained in block
            if ( coords1.GetCol() <= col && col <= coords2.GetCol() )
                return;
            // expand block, if it touched col
            else if ( coords1.GetCol() == col + 1)
            {
                coords1.SetCol(col);
                done = true;
            }
            else if ( coords2.GetCol() == col - 1)
            {
                coords2.SetCol(col);
                done = true;
            }
        }
    }

    // Unless we successfully handled the column,
    // Check whether col is already selected.
    if ( !done )
    {
        count = m_colSelection.GetCount();
        for ( n = 0; n < count; n++ )
        {
            if ( col == m_colSelection[n] )
                return;
        }

        // Add col to selection
        m_colSelection.Add(col);
    }

    // Update View:
    if ( !m_grid->GetBatchCount() )
    {
        wxRect r = m_grid->BlockToDeviceRect( wxGridCellCoords( 0, col ),
                                              wxGridCellCoords( m_grid->GetNumberRows() - 1, col ) );
        ((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
    }

    // Send Event
    wxGridRangeSelectEvent gridEvt( m_grid->GetId(),
                                    wxEVT_GRID_RANGE_SELECT,
                                    m_grid,
                                    wxGridCellCoords( 0, col ),
                                    wxGridCellCoords( m_grid->GetNumberRows() - 1, col ),
                                    true,
                                    ControlDown,  ShiftDown,
                                    AltDown, MetaDown );

    m_grid->GetEventHandler()->ProcessEvent(gridEvt);
}

void wxGridSelection::SelectBlock( int topRow, int leftCol,
                                   int bottomRow, int rightCol,

⌨️ 快捷键说明

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