+ for ( i = n; i <= row; i++ )
+ {
+ m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
+ }
+ }
+
+ m_rowLabels[row] = value;
+}
+
+void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
+{
+ if ( col > (int)(m_colLabels.GetCount()) - 1 )
+ {
+ int n = m_colLabels.GetCount();
+ int i;
+
+ for ( i = n; i <= col; i++ )
+ {
+ m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
+ }
+ }
+
+ m_colLabels[col] = value;
+}
+
+
+//////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////
+
+BEGIN_EVENT_TABLE(wxGridSubwindow, wxWindow)
+ EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost)
+END_EVENT_TABLE()
+
+void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
+{
+ m_owner->CancelMouseCapture();
+}
+
+BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxGridSubwindow )
+ EVT_PAINT( wxGridRowLabelWindow::OnPaint )
+ EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel )
+ EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
+END_EVENT_TABLE()
+
+void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
+{
+ wxPaintDC dc(this);
+
+ // NO - don't do this because it will set both the x and y origin
+ // coords to match the parent scrolled window and we just want to
+ // set the y coord - MB
+ //
+ // m_owner->PrepareDC( dc );
+
+ int x, y;
+ m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
+ wxPoint pt = dc.GetDeviceOrigin();
+ dc.SetDeviceOrigin( pt.x, pt.y-y );
+
+ wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
+ m_owner->DrawRowLabels( dc, rows );
+}
+
+void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
+{
+ m_owner->ProcessRowLabelMouseEvent( event );
+}
+
+void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
+{
+ if (!m_owner->GetEventHandler()->ProcessEvent( event ))
+ event.Skip();
+}
+
+//////////////////////////////////////////////////////////////////////
+
+BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxGridSubwindow )
+ EVT_PAINT( wxGridColLabelWindow::OnPaint )
+ EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel )
+ EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
+END_EVENT_TABLE()
+
+void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
+{
+ wxPaintDC dc(this);
+
+ // NO - don't do this because it will set both the x and y origin
+ // coords to match the parent scrolled window and we just want to
+ // set the x coord - MB
+ //
+ // m_owner->PrepareDC( dc );
+
+ int x, y;
+ m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
+ wxPoint pt = dc.GetDeviceOrigin();
+ if (GetLayoutDirection() == wxLayout_RightToLeft)
+ dc.SetDeviceOrigin( pt.x+x, pt.y );
+ else
+ dc.SetDeviceOrigin( pt.x-x, pt.y );
+
+ wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
+ m_owner->DrawColLabels( dc, cols );
+}
+
+void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
+{
+ m_owner->ProcessColLabelMouseEvent( event );
+}
+
+void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
+{
+ if (!m_owner->GetEventHandler()->ProcessEvent( event ))
+ event.Skip();
+}
+
+//////////////////////////////////////////////////////////////////////
+
+BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxGridSubwindow )
+ EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel )
+ EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
+ EVT_PAINT( wxGridCornerLabelWindow::OnPaint )
+END_EVENT_TABLE()
+
+void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
+{
+ wxPaintDC dc(this);
+
+ m_owner->DrawCornerLabel(dc);
+}
+
+void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
+{
+ m_owner->ProcessCornerLabelMouseEvent( event );
+}
+
+void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
+{
+ if (!m_owner->GetEventHandler()->ProcessEvent(event))
+ event.Skip();
+}
+
+//////////////////////////////////////////////////////////////////////
+
+BEGIN_EVENT_TABLE( wxGridWindow, wxGridSubwindow )
+ EVT_PAINT( wxGridWindow::OnPaint )
+ EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel )
+ EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
+ EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
+ EVT_KEY_UP( wxGridWindow::OnKeyUp )
+ EVT_CHAR( wxGridWindow::OnChar )
+ EVT_SET_FOCUS( wxGridWindow::OnFocus )
+ EVT_KILL_FOCUS( wxGridWindow::OnFocus )
+ EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
+END_EVENT_TABLE()
+
+void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
+{
+ wxPaintDC dc( this );
+ m_owner->PrepareDC( dc );
+ wxRegion reg = GetUpdateRegion();
+ wxGridCellCoordsArray dirtyCells = m_owner->CalcCellsExposed( reg );
+ m_owner->DrawGridCellArea( dc, dirtyCells );
+
+ m_owner->DrawGridSpace( dc );
+
+ m_owner->DrawAllGridLines( dc, reg );
+
+ m_owner->DrawHighlight( dc, dirtyCells );
+}
+
+void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
+{
+ wxWindow::ScrollWindow( dx, dy, rect );
+ m_owner->GetGridRowLabelWindow()->ScrollWindow( 0, dy, rect );
+ m_owner->GetGridColLabelWindow()->ScrollWindow( dx, 0, rect );
+}
+
+void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
+{
+ if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this)
+ SetFocus();
+
+ m_owner->ProcessGridCellMouseEvent( event );
+}
+
+void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
+{
+ if (!m_owner->GetEventHandler()->ProcessEvent( event ))
+ event.Skip();
+}
+
+// This seems to be required for wxMotif/wxGTK otherwise the mouse
+// cursor must be in the cell edit control to get key events
+//
+void wxGridWindow::OnKeyDown( wxKeyEvent& event )
+{
+ if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
+ event.Skip();
+}
+
+void wxGridWindow::OnKeyUp( wxKeyEvent& event )
+{
+ if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
+ event.Skip();
+}
+
+void wxGridWindow::OnChar( wxKeyEvent& event )
+{
+ if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
+ event.Skip();
+}
+
+void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
+{
+}
+
+void wxGridWindow::OnFocus(wxFocusEvent& event)
+{
+ // and if we have any selection, it has to be repainted, because it
+ // uses different colour when the grid is not focused:
+ if ( m_owner->IsSelection() )
+ {
+ Refresh();
+ }
+ else
+ {
+ // NB: Note that this code is in "else" branch only because the other
+ // branch refreshes everything and so there's no point in calling
+ // Refresh() again, *not* because it should only be done if
+ // !IsSelection(). If the above code is ever optimized to refresh
+ // only selected area, this needs to be moved out of the "else"
+ // branch so that it's always executed.
+
+ // current cell cursor {dis,re}appears on focus change:
+ const wxGridCellCoords cursorCoords(m_owner->GetGridCursorRow(),
+ m_owner->GetGridCursorCol());
+ const wxRect cursor =
+ m_owner->BlockToDeviceRect(cursorCoords, cursorCoords);
+ Refresh(true, &cursor);
+ }
+
+ if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
+ event.Skip();
+}
+
+#define internalXToCol(x) XToCol(x, true)
+#define internalYToRow(y) YToRow(y, true)
+
+/////////////////////////////////////////////////////////////////////
+
+#if wxUSE_EXTENDED_RTTI
+WX_DEFINE_FLAGS( wxGridStyle )
+
+wxBEGIN_FLAGS( wxGridStyle )
+ // new style border flags, we put them first to
+ // use them for streaming out
+ wxFLAGS_MEMBER(wxBORDER_SIMPLE)
+ wxFLAGS_MEMBER(wxBORDER_SUNKEN)
+ wxFLAGS_MEMBER(wxBORDER_DOUBLE)
+ wxFLAGS_MEMBER(wxBORDER_RAISED)
+ wxFLAGS_MEMBER(wxBORDER_STATIC)
+ wxFLAGS_MEMBER(wxBORDER_NONE)
+
+ // old style border flags
+ wxFLAGS_MEMBER(wxSIMPLE_BORDER)
+ wxFLAGS_MEMBER(wxSUNKEN_BORDER)
+ wxFLAGS_MEMBER(wxDOUBLE_BORDER)
+ wxFLAGS_MEMBER(wxRAISED_BORDER)
+ wxFLAGS_MEMBER(wxSTATIC_BORDER)
+ wxFLAGS_MEMBER(wxBORDER)
+
+ // standard window styles
+ wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
+ wxFLAGS_MEMBER(wxCLIP_CHILDREN)
+ wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
+ wxFLAGS_MEMBER(wxWANTS_CHARS)
+ wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
+ wxFLAGS_MEMBER(wxALWAYS_SHOW_SB)
+ wxFLAGS_MEMBER(wxVSCROLL)
+ wxFLAGS_MEMBER(wxHSCROLL)
+
+wxEND_FLAGS( wxGridStyle )
+
+IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h")
+
+wxBEGIN_PROPERTIES_TABLE(wxGrid)
+ wxHIDE_PROPERTY( Children )
+ wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
+wxEND_PROPERTIES_TABLE()
+
+wxBEGIN_HANDLERS_TABLE(wxGrid)
+wxEND_HANDLERS_TABLE()
+
+wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
+
+/*
+ TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
+*/
+#else
+IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
+#endif
+
+BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
+ EVT_PAINT( wxGrid::OnPaint )
+ EVT_SIZE( wxGrid::OnSize )
+ EVT_KEY_DOWN( wxGrid::OnKeyDown )
+ EVT_KEY_UP( wxGrid::OnKeyUp )
+ EVT_CHAR ( wxGrid::OnChar )
+ EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
+END_EVENT_TABLE()
+
+bool wxGrid::Create(wxWindow *parent, wxWindowID id,
+ const wxPoint& pos, const wxSize& size,
+ long style, const wxString& name)
+{
+ if (!wxScrolledWindow::Create(parent, id, pos, size,
+ style | wxWANTS_CHARS, name))
+ return false;
+
+ m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE);
+ m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE);
+
+ Create();
+ SetInitialSize(size);
+ CalcDimensions();
+
+ return true;
+}
+
+wxGrid::~wxGrid()
+{
+ if ( m_winCapture )
+ m_winCapture->ReleaseMouse();
+
+ // Ensure that the editor control is destroyed before the grid is,
+ // otherwise we crash later when the editor tries to do something with the
+ // half destroyed grid
+ HideCellEditControl();
+
+ // Must do this or ~wxScrollHelper will pop the wrong event handler
+ SetTargetWindow(this);
+ ClearAttrCache();
+ wxSafeDecRef(m_defaultCellAttr);
+
+#ifdef DEBUG_ATTR_CACHE
+ size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
+ wxPrintf(wxT("wxGrid attribute cache statistics: "
+ "total: %u, hits: %u (%u%%)\n"),
+ total, gs_nAttrCacheHits,
+ total ? (gs_nAttrCacheHits*100) / total : 0);
+#endif
+
+ // if we own the table, just delete it, otherwise at least don't leave it
+ // with dangling view pointer
+ if ( m_ownTable )
+ delete m_table;
+ else if ( m_table && m_table->GetView() == this )
+ m_table->SetView(NULL);
+
+ delete m_typeRegistry;
+ delete m_selection;
+
+ delete m_setFixedRows;
+ delete m_setFixedCols;
+}
+
+//
+// ----- internal init and update functions
+//
+
+// NOTE: If using the default visual attributes works everywhere then this can
+// be removed as well as the #else cases below.
+#define _USE_VISATTR 0
+
+void wxGrid::Create()
+{
+ // create the type registry
+ m_typeRegistry = new wxGridTypeRegistry;
+
+ m_cellEditCtrlEnabled = false;
+
+ m_defaultCellAttr = new wxGridCellAttr();
+
+ // Set default cell attributes
+ m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
+ m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
+ m_defaultCellAttr->SetFont(GetFont());
+ m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
+ m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
+ m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
+
+#if _USE_VISATTR
+ wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes();
+ wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes();
+
+ m_defaultCellAttr->SetTextColour(gva.colFg);
+ m_defaultCellAttr->SetBackgroundColour(gva.colBg);
+
+#else
+ m_defaultCellAttr->SetTextColour(
+ wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
+ m_defaultCellAttr->SetBackgroundColour(
+ wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
+#endif
+
+ m_numRows = 0;
+ m_numCols = 0;
+ m_currentCellCoords = wxGridNoCellCoords;
+
+ // subwindow components that make up the wxGrid
+ m_rowLabelWin = new wxGridRowLabelWindow(this);
+ CreateColumnWindow();
+ m_cornerLabelWin = new wxGridCornerLabelWindow(this);
+ m_gridWin = new wxGridWindow( this );
+
+ SetTargetWindow( m_gridWin );
+
+#if _USE_VISATTR
+ wxColour gfg = gva.colFg;
+ wxColour gbg = gva.colBg;
+ wxColour lfg = lva.colFg;
+ wxColour lbg = lva.colBg;
+#else
+ wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
+ wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
+ wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
+ wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
+#endif
+
+ m_cornerLabelWin->SetOwnForegroundColour(lfg);
+ m_cornerLabelWin->SetOwnBackgroundColour(lbg);
+ m_rowLabelWin->SetOwnForegroundColour(lfg);
+ m_rowLabelWin->SetOwnBackgroundColour(lbg);
+ m_colWindow->SetOwnForegroundColour(lfg);
+ m_colWindow->SetOwnBackgroundColour(lbg);
+
+ m_gridWin->SetOwnForegroundColour(gfg);
+ m_gridWin->SetOwnBackgroundColour(gbg);
+
+ m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
+ m_labelTextColour = m_rowLabelWin->GetForegroundColour();
+
+ // now that we have the grid window, use its font to compute the default
+ // row height
+ m_defaultRowHeight = m_gridWin->GetCharHeight();
+#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
+ m_defaultRowHeight += 8;
+#else
+ m_defaultRowHeight += 4;
+#endif
+
+}
+
+void wxGrid::CreateColumnWindow()
+{
+ if ( m_useNativeHeader )
+ {
+ m_colWindow = new wxGridHeaderCtrl(this);
+ m_colLabelHeight = m_colWindow->GetBestSize().y;
+ }
+ else // draw labels ourselves
+ {
+ m_colWindow = new wxGridColLabelWindow(this);
+ m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
+ }
+}
+
+bool wxGrid::CreateGrid( int numRows, int numCols,
+ wxGridSelectionModes selmode )
+{
+ wxCHECK_MSG( !m_created,
+ false,
+ wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
+
+ return SetTable(new wxGridStringTable(numRows, numCols), true, selmode);
+}
+
+void wxGrid::SetSelectionMode(wxGridSelectionModes selmode)
+{
+ wxCHECK_RET( m_created,
+ wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
+
+ m_selection->SetSelectionMode( selmode );
+}
+
+wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
+{
+ wxCHECK_MSG( m_created, wxGridSelectCells,
+ wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
+
+ return m_selection->GetSelectionMode();
+}
+
+bool
+wxGrid::SetTable(wxGridTableBase *table,
+ bool takeOwnership,
+ wxGrid::wxGridSelectionModes selmode )
+{
+ bool checkSelection = false;
+ if ( m_created )
+ {
+ // stop all processing
+ m_created = false;
+
+ if (m_table)
+ {
+ m_table->SetView(0);
+ if( m_ownTable )
+ delete m_table;
+ m_table = NULL;
+ }
+
+ wxDELETE(m_selection);
+
+ m_ownTable = false;
+ m_numRows = 0;
+ m_numCols = 0;
+ checkSelection = true;
+
+ // kill row and column size arrays
+ m_colWidths.Empty();
+ m_colRights.Empty();
+ m_rowHeights.Empty();
+ m_rowBottoms.Empty();
+ }
+
+ if (table)
+ {
+ m_numRows = table->GetNumberRows();
+ m_numCols = table->GetNumberCols();
+
+ if ( m_useNativeHeader )
+ GetGridColHeader()->SetColumnCount(m_numCols);
+
+ m_table = table;
+ m_table->SetView( this );
+ m_ownTable = takeOwnership;
+ m_selection = new wxGridSelection( this, selmode );
+ if (checkSelection)
+ {
+ // If the newly set table is smaller than the
+ // original one current cell and selection regions
+ // might be invalid,
+ m_selectedBlockCorner = wxGridNoCellCoords;
+ m_currentCellCoords =
+ wxGridCellCoords(wxMin(m_numRows, m_currentCellCoords.GetRow()),
+ wxMin(m_numCols, m_currentCellCoords.GetCol()));
+ if (m_selectedBlockTopLeft.GetRow() >= m_numRows ||
+ m_selectedBlockTopLeft.GetCol() >= m_numCols)
+ {
+ m_selectedBlockTopLeft = wxGridNoCellCoords;
+ m_selectedBlockBottomRight = wxGridNoCellCoords;
+ }
+ else
+ m_selectedBlockBottomRight =
+ wxGridCellCoords(wxMin(m_numRows,
+ m_selectedBlockBottomRight.GetRow()),
+ wxMin(m_numCols,
+ m_selectedBlockBottomRight.GetCol()));
+ }
+ CalcDimensions();
+
+ m_created = true;
+ }
+
+ return m_created;
+}
+
+void wxGrid::Init()
+{
+ m_created = false;
+
+ m_cornerLabelWin = NULL;
+ m_rowLabelWin = NULL;
+ m_colWindow = NULL;
+ m_gridWin = NULL;
+
+ m_table = NULL;
+ m_ownTable = false;
+
+ m_selection = NULL;
+ m_defaultCellAttr = NULL;
+ m_typeRegistry = NULL;
+ m_winCapture = NULL;
+
+ m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
+ m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
+
+ m_setFixedRows =
+ m_setFixedCols = NULL;
+
+ // init attr cache
+ m_attrCache.row = -1;
+ m_attrCache.col = -1;
+ m_attrCache.attr = NULL;
+
+ m_labelFont = GetFont();
+ m_labelFont.SetWeight( wxBOLD );
+
+ m_rowLabelHorizAlign = wxALIGN_CENTRE;
+ m_rowLabelVertAlign = wxALIGN_CENTRE;
+
+ m_colLabelHorizAlign = wxALIGN_CENTRE;
+ m_colLabelVertAlign = wxALIGN_CENTRE;
+ m_colLabelTextOrientation = wxHORIZONTAL;
+
+ m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
+ m_defaultRowHeight = 0; // this will be initialized after creation
+
+ m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
+ m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
+
+ m_gridLineColour = wxColour( 192,192,192 );
+ m_gridLinesEnabled = true;
+ m_gridLinesClipHorz =
+ m_gridLinesClipVert = true;
+ m_cellHighlightColour = *wxBLACK;
+ m_cellHighlightPenWidth = 2;
+ m_cellHighlightROPenWidth = 1;
+
+ m_canDragColMove = false;
+
+ m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
+ m_winCapture = NULL;
+ m_canDragRowSize = true;
+ m_canDragColSize = true;
+ m_canDragGridSize = true;
+ m_canDragCell = false;
+ m_dragLastPos = -1;
+ m_dragRowOrCol = -1;
+ m_isDragging = false;
+ m_startDragPos = wxDefaultPosition;
+
+ m_sortCol = wxNOT_FOUND;
+ m_sortIsAscending = true;
+
+ m_useNativeHeader =
+ m_nativeColumnLabels = false;
+
+ m_waitForSlowClick = false;
+
+ m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
+ m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
+
+ m_currentCellCoords = wxGridNoCellCoords;
+
+ m_selectedBlockTopLeft =
+ m_selectedBlockBottomRight =
+ m_selectedBlockCorner = wxGridNoCellCoords;
+
+ m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
+ m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
+
+ m_editable = true; // default for whole grid
+
+ m_inOnKeyDown = false;
+ m_batchCount = 0;
+
+ m_extraWidth =
+ m_extraHeight = 0;
+
+ // we can't call SetScrollRate() as the window isn't created yet but OTOH
+ // we don't need to call it neither as the scroll position is (0, 0) right
+ // now anyhow, so just set the parameters directly
+ m_xScrollPixelsPerLine = GRID_SCROLL_LINE_X;
+ m_yScrollPixelsPerLine = GRID_SCROLL_LINE_Y;
+}
+
+// ----------------------------------------------------------------------------
+// the idea is to call these functions only when necessary because they create
+// quite big arrays which eat memory mostly unnecessary - in particular, if
+// default widths/heights are used for all rows/columns, we may not use these
+// arrays at all
+//
+// with some extra code, it should be possible to only store the widths/heights
+// different from default ones (resulting in space savings for huge grids) but
+// this is not done currently
+// ----------------------------------------------------------------------------
+
+void wxGrid::InitRowHeights()
+{
+ m_rowHeights.Empty();
+ m_rowBottoms.Empty();
+
+ m_rowHeights.Alloc( m_numRows );
+ m_rowBottoms.Alloc( m_numRows );
+
+ m_rowHeights.Add( m_defaultRowHeight, m_numRows );
+
+ int rowBottom = 0;
+ for ( int i = 0; i < m_numRows; i++ )
+ {
+ rowBottom += m_defaultRowHeight;
+ m_rowBottoms.Add( rowBottom );
+ }
+}
+
+void wxGrid::InitColWidths()
+{
+ m_colWidths.Empty();
+ m_colRights.Empty();
+
+ m_colWidths.Alloc( m_numCols );
+ m_colRights.Alloc( m_numCols );
+
+ m_colWidths.Add( m_defaultColWidth, m_numCols );
+
+ for ( int i = 0; i < m_numCols; i++ )
+ {
+ int colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
+ m_colRights.Add( colRight );
+ }
+}
+
+int wxGrid::GetColWidth(int col) const
+{
+ return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
+}
+
+int wxGrid::GetColLeft(int col) const
+{
+ return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth
+ : m_colRights[col] - m_colWidths[col];
+}
+
+int wxGrid::GetColRight(int col) const
+{
+ return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
+ : m_colRights[col];
+}
+
+int wxGrid::GetRowHeight(int row) const
+{
+ return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
+}
+
+int wxGrid::GetRowTop(int row) const
+{
+ return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
+ : m_rowBottoms[row] - m_rowHeights[row];
+}
+
+int wxGrid::GetRowBottom(int row) const
+{
+ return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
+ : m_rowBottoms[row];
+}
+
+void wxGrid::CalcDimensions()
+{
+ // compute the size of the scrollable area
+ int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0;
+ int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
+
+ w += m_extraWidth;
+ h += m_extraHeight;
+
+ // take into account editor if shown
+ if ( IsCellEditControlShown() )
+ {
+ int w2, h2;
+ int r = m_currentCellCoords.GetRow();
+ int c = m_currentCellCoords.GetCol();
+ int x = GetColLeft(c);
+ int y = GetRowTop(r);
+
+ // how big is the editor
+ wxGridCellAttr* attr = GetCellAttr(r, c);
+ wxGridCellEditor* editor = attr->GetEditor(this, r, c);
+ editor->GetControl()->GetSize(&w2, &h2);
+ w2 += x;
+ h2 += y;
+ if ( w2 > w )
+ w = w2;
+ if ( h2 > h )
+ h = h2;
+ editor->DecRef();
+ attr->DecRef();
+ }
+
+ // preserve (more or less) the previous position
+ int x, y;
+ GetViewStart( &x, &y );
+
+ // ensure the position is valid for the new scroll ranges
+ if ( x >= w )
+ x = wxMax( w - 1, 0 );
+ if ( y >= h )
+ y = wxMax( h - 1, 0 );
+
+ // update the virtual size and refresh the scrollbars to reflect it
+ m_gridWin->SetVirtualSize(w, h);
+ Scroll(x, y);
+ AdjustScrollbars();
+
+ // if our OnSize() hadn't been called (it would if we have scrollbars), we
+ // still must reposition the children
+ CalcWindowSizes();
+}
+
+wxSize wxGrid::GetSizeAvailableForScrollTarget(const wxSize& size)
+{
+ wxSize sizeGridWin(size);
+ sizeGridWin.x -= m_rowLabelWidth;
+ sizeGridWin.y -= m_colLabelHeight;
+
+ return sizeGridWin;
+}
+
+void wxGrid::CalcWindowSizes()
+{
+ // escape if the window is has not been fully created yet
+
+ if ( m_cornerLabelWin == NULL )
+ return;
+
+ int cw, ch;
+ GetClientSize( &cw, &ch );
+
+ // the grid may be too small to have enough space for the labels yet, don't
+ // size the windows to negative sizes in this case
+ int gw = cw - m_rowLabelWidth;
+ int gh = ch - m_colLabelHeight;
+ if (gw < 0)
+ gw = 0;
+ if (gh < 0)
+ gh = 0;
+
+ if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
+ m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
+
+ if ( m_colWindow && m_colWindow->IsShown() )
+ m_colWindow->SetSize( m_rowLabelWidth, 0, gw, m_colLabelHeight );
+
+ if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
+ m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, gh );
+
+ if ( m_gridWin && m_gridWin->IsShown() )
+ m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, gw, gh );
+}
+
+// this is called when the grid table sends a message
+// to indicate that it has been redimensioned
+//
+bool wxGrid::Redimension( wxGridTableMessage& msg )
+{
+ int i;
+ bool result = false;
+
+ // Clear the attribute cache as the attribute might refer to a different
+ // cell than stored in the cache after adding/removing rows/columns.
+ ClearAttrCache();
+
+ // By the same reasoning, the editor should be dismissed if columns are
+ // added or removed. And for consistency, it should IMHO always be
+ // removed, not only if the cell "underneath" it actually changes.
+ // For now, I intentionally do not save the editor's content as the
+ // cell it might want to save that stuff to might no longer exist.
+ HideCellEditControl();
+
+ switch ( msg.GetId() )
+ {
+ case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
+ {
+ size_t pos = msg.GetCommandInt();
+ int numRows = msg.GetCommandInt2();
+
+ m_numRows += numRows;
+
+ if ( !m_rowHeights.IsEmpty() )
+ {
+ m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
+ m_rowBottoms.Insert( 0, pos, numRows );
+
+ int bottom = 0;
+ if ( pos > 0 )
+ bottom = m_rowBottoms[pos - 1];
+
+ for ( i = pos; i < m_numRows; i++ )
+ {
+ bottom += m_rowHeights[i];
+ m_rowBottoms[i] = bottom;
+ }
+ }
+
+ if ( m_currentCellCoords == wxGridNoCellCoords )
+ {
+ // if we have just inserted cols into an empty grid the current
+ // cell will be undefined...
+ //
+ SetCurrentCell( 0, 0 );
+ }
+
+ if ( m_selection )
+ m_selection->UpdateRows( pos, numRows );
+ wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
+ if (attrProvider)
+ attrProvider->UpdateAttrRows( pos, numRows );
+
+ if ( !GetBatchCount() )
+ {
+ CalcDimensions();
+ m_rowLabelWin->Refresh();
+ }
+ }
+ result = true;
+ break;
+
+ case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
+ {
+ int numRows = msg.GetCommandInt();
+ int oldNumRows = m_numRows;
+ m_numRows += numRows;
+
+ if ( !m_rowHeights.IsEmpty() )
+ {
+ m_rowHeights.Add( m_defaultRowHeight, numRows );
+ m_rowBottoms.Add( 0, numRows );
+
+ int bottom = 0;
+ if ( oldNumRows > 0 )
+ bottom = m_rowBottoms[oldNumRows - 1];
+
+ for ( i = oldNumRows; i < m_numRows; i++ )
+ {
+ bottom += m_rowHeights[i];
+ m_rowBottoms[i] = bottom;
+ }
+ }
+
+ if ( m_currentCellCoords == wxGridNoCellCoords )
+ {
+ // if we have just inserted cols into an empty grid the current
+ // cell will be undefined...
+ //
+ SetCurrentCell( 0, 0 );
+ }
+
+ if ( !GetBatchCount() )
+ {
+ CalcDimensions();
+ m_rowLabelWin->Refresh();
+ }
+ }
+ result = true;
+ break;
+
+ case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
+ {
+ size_t pos = msg.GetCommandInt();
+ int numRows = msg.GetCommandInt2();
+ m_numRows -= numRows;
+
+ if ( !m_rowHeights.IsEmpty() )
+ {
+ m_rowHeights.RemoveAt( pos, numRows );
+ m_rowBottoms.RemoveAt( pos, numRows );
+
+ int h = 0;
+ for ( i = 0; i < m_numRows; i++ )
+ {
+ h += m_rowHeights[i];
+ m_rowBottoms[i] = h;
+ }
+ }
+
+ if ( !m_numRows )
+ {
+ m_currentCellCoords = wxGridNoCellCoords;
+ }
+ else
+ {
+ if ( m_currentCellCoords.GetRow() >= m_numRows )
+ m_currentCellCoords.Set( 0, 0 );
+ }
+
+ if ( m_selection )
+ m_selection->UpdateRows( pos, -((int)numRows) );
+ wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
+ if (attrProvider)
+ {
+ attrProvider->UpdateAttrRows( pos, -((int)numRows) );
+
+// ifdef'd out following patch from Paul Gammans
+#if 0
+ // No need to touch column attributes, unless we
+ // removed _all_ rows, in this case, we remove
+ // all column attributes.
+ // I hate to do this here, but the
+ // needed data is not available inside UpdateAttrRows.
+ if ( !GetNumberRows() )
+ attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
+#endif
+ }
+
+ if ( !GetBatchCount() )
+ {
+ CalcDimensions();
+ m_rowLabelWin->Refresh();
+ }
+ }
+ result = true;
+ break;
+
+ case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
+ {
+ size_t pos = msg.GetCommandInt();
+ int numCols = msg.GetCommandInt2();
+ m_numCols += numCols;
+
+ if ( m_useNativeHeader )
+ GetGridColHeader()->SetColumnCount(m_numCols);
+
+ if ( !m_colAt.IsEmpty() )
+ {
+ //Shift the column IDs
+ int i;
+ for ( i = 0; i < m_numCols - numCols; i++ )
+ {
+ if ( m_colAt[i] >= (int)pos )
+ m_colAt[i] += numCols;
+ }
+
+ m_colAt.Insert( pos, pos, numCols );
+
+ //Set the new columns' positions
+ for ( i = pos + 1; i < (int)pos + numCols; i++ )
+ {
+ m_colAt[i] = i;
+ }
+ }
+
+ if ( !m_colWidths.IsEmpty() )
+ {
+ m_colWidths.Insert( m_defaultColWidth, pos, numCols );
+ m_colRights.Insert( 0, pos, numCols );
+
+ int right = 0;
+ if ( pos > 0 )
+ right = m_colRights[GetColAt( pos - 1 )];
+
+ int colPos;
+ for ( colPos = pos; colPos < m_numCols; colPos++ )
+ {
+ i = GetColAt( colPos );
+
+ right += m_colWidths[i];
+ m_colRights[i] = right;
+ }
+ }
+
+ if ( m_currentCellCoords == wxGridNoCellCoords )
+ {
+ // if we have just inserted cols into an empty grid the current
+ // cell will be undefined...
+ //
+ SetCurrentCell( 0, 0 );
+ }
+
+ if ( m_selection )
+ m_selection->UpdateCols( pos, numCols );
+ wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
+ if (attrProvider)
+ attrProvider->UpdateAttrCols( pos, numCols );
+ if ( !GetBatchCount() )
+ {
+ CalcDimensions();
+ m_colWindow->Refresh();
+ }
+ }
+ result = true;
+ break;
+
+ case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
+ {
+ int numCols = msg.GetCommandInt();
+ int oldNumCols = m_numCols;
+ m_numCols += numCols;
+ if ( m_useNativeHeader )
+ GetGridColHeader()->SetColumnCount(m_numCols);
+
+ if ( !m_colAt.IsEmpty() )
+ {
+ m_colAt.Add( 0, numCols );
+
+ //Set the new columns' positions
+ int i;
+ for ( i = oldNumCols; i < m_numCols; i++ )
+ {
+ m_colAt[i] = i;
+ }
+ }
+
+ if ( !m_colWidths.IsEmpty() )
+ {
+ m_colWidths.Add( m_defaultColWidth, numCols );
+ m_colRights.Add( 0, numCols );
+
+ int right = 0;
+ if ( oldNumCols > 0 )
+ right = m_colRights[GetColAt( oldNumCols - 1 )];
+
+ int colPos;
+ for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
+ {
+ i = GetColAt( colPos );
+
+ right += m_colWidths[i];
+ m_colRights[i] = right;
+ }
+ }
+
+ if ( m_currentCellCoords == wxGridNoCellCoords )
+ {
+ // if we have just inserted cols into an empty grid the current
+ // cell will be undefined...
+ //
+ SetCurrentCell( 0, 0 );
+ }
+ if ( !GetBatchCount() )
+ {
+ CalcDimensions();
+ m_colWindow->Refresh();
+ }
+ }
+ result = true;
+ break;
+
+ case wxGRIDTABLE_NOTIFY_COLS_DELETED:
+ {
+ size_t pos = msg.GetCommandInt();
+ int numCols = msg.GetCommandInt2();
+ m_numCols -= numCols;
+ if ( m_useNativeHeader )
+ GetGridColHeader()->SetColumnCount(m_numCols);
+
+ if ( !m_colAt.IsEmpty() )
+ {
+ int colID = GetColAt( pos );
+
+ m_colAt.RemoveAt( pos, numCols );
+
+ //Shift the column IDs
+ int colPos;
+ for ( colPos = 0; colPos < m_numCols; colPos++ )
+ {
+ if ( m_colAt[colPos] > colID )
+ m_colAt[colPos] -= numCols;
+ }
+ }
+
+ if ( !m_colWidths.IsEmpty() )
+ {
+ m_colWidths.RemoveAt( pos, numCols );
+ m_colRights.RemoveAt( pos, numCols );
+
+ int w = 0;
+ int colPos;
+ for ( colPos = 0; colPos < m_numCols; colPos++ )
+ {
+ i = GetColAt( colPos );
+
+ w += m_colWidths[i];
+ m_colRights[i] = w;
+ }
+ }
+
+ if ( !m_numCols )
+ {
+ m_currentCellCoords = wxGridNoCellCoords;
+ }
+ else
+ {
+ if ( m_currentCellCoords.GetCol() >= m_numCols )
+ m_currentCellCoords.Set( 0, 0 );
+ }
+
+ if ( m_selection )
+ m_selection->UpdateCols( pos, -((int)numCols) );
+ wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
+ if (attrProvider)
+ {
+ attrProvider->UpdateAttrCols( pos, -((int)numCols) );
+
+// ifdef'd out following patch from Paul Gammans
+#if 0
+ // No need to touch row attributes, unless we
+ // removed _all_ columns, in this case, we remove
+ // all row attributes.
+ // I hate to do this here, but the
+ // needed data is not available inside UpdateAttrCols.
+ if ( !GetNumberCols() )
+ attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
+#endif
+ }
+
+ if ( !GetBatchCount() )
+ {
+ CalcDimensions();
+ m_colWindow->Refresh();
+ }
+ }
+ result = true;
+ break;
+ }
+
+ if (result && !GetBatchCount() )
+ m_gridWin->Refresh();
+
+ return result;
+}
+
+wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const
+{
+ wxRegionIterator iter( reg );
+ wxRect r;
+
+ wxArrayInt rowlabels;
+
+ int top, bottom;
+ while ( iter )
+ {
+ r = iter.GetRect();
+
+ // TODO: remove this when we can...
+ // There is a bug in wxMotif that gives garbage update
+ // rectangles if you jump-scroll a long way by clicking the
+ // scrollbar with middle button. This is a work-around
+ //
+#if defined(__WXMOTIF__)
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+ if ( r.GetTop() > ch )
+ r.SetTop( 0 );
+ r.SetBottom( wxMin( r.GetBottom(), ch ) );
+#endif
+
+ // logical bounds of update region
+ //
+ int dummy;
+ CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
+ CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
+
+ // find the row labels within these bounds
+ //
+ int row;
+ for ( row = internalYToRow(top); row < m_numRows; row++ )
+ {
+ if ( GetRowBottom(row) < top )
+ continue;
+
+ if ( GetRowTop(row) > bottom )
+ break;
+
+ rowlabels.Add( row );
+ }
+
+ ++iter;
+ }
+
+ return rowlabels;
+}
+
+wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const
+{
+ wxRegionIterator iter( reg );
+ wxRect r;
+
+ wxArrayInt colLabels;
+
+ int left, right;
+ while ( iter )
+ {
+ r = iter.GetRect();
+
+ // TODO: remove this when we can...
+ // There is a bug in wxMotif that gives garbage update
+ // rectangles if you jump-scroll a long way by clicking the
+ // scrollbar with middle button. This is a work-around
+ //
+#if defined(__WXMOTIF__)
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+ if ( r.GetLeft() > cw )
+ r.SetLeft( 0 );
+ r.SetRight( wxMin( r.GetRight(), cw ) );
+#endif
+
+ // logical bounds of update region
+ //
+ int dummy;
+ CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
+ CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
+
+ // find the cells within these bounds
+ //
+ int col;
+ int colPos;
+ for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
+ {
+ col = GetColAt( colPos );
+
+ if ( GetColRight(col) < left )
+ continue;
+
+ if ( GetColLeft(col) > right )
+ break;
+
+ colLabels.Add( col );
+ }
+
+ ++iter;
+ }
+
+ return colLabels;
+}
+
+wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const
+{
+ wxRegionIterator iter( reg );
+ wxRect r;
+
+ wxGridCellCoordsArray cellsExposed;
+
+ int left, top, right, bottom;
+ while ( iter )
+ {
+ r = iter.GetRect();
+
+ // TODO: remove this when we can...
+ // There is a bug in wxMotif that gives garbage update
+ // rectangles if you jump-scroll a long way by clicking the
+ // scrollbar with middle button. This is a work-around
+ //
+#if defined(__WXMOTIF__)
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+ if ( r.GetTop() > ch ) r.SetTop( 0 );
+ if ( r.GetLeft() > cw ) r.SetLeft( 0 );
+ r.SetRight( wxMin( r.GetRight(), cw ) );
+ r.SetBottom( wxMin( r.GetBottom(), ch ) );
+#endif
+
+ // logical bounds of update region
+ //
+ CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
+ CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
+
+ // find the cells within these bounds
+ wxArrayInt cols;
+ for ( int row = internalYToRow(top); row < m_numRows; row++ )
+ {
+ if ( GetRowBottom(row) <= top )
+ continue;
+
+ if ( GetRowTop(row) > bottom )
+ break;
+
+ // add all dirty cells in this row: notice that the columns which
+ // are dirty don't depend on the row so we compute them only once
+ // for the first dirty row and then reuse for all the next ones
+ if ( cols.empty() )
+ {
+ // do determine the dirty columns
+ for ( int pos = XToPos(left); pos <= XToPos(right); pos++ )
+ cols.push_back(GetColAt(pos));
+
+ // if there are no dirty columns at all, nothing to do
+ if ( cols.empty() )
+ break;
+ }
+
+ const size_t count = cols.size();
+ for ( size_t n = 0; n < count; n++ )
+ cellsExposed.Add(wxGridCellCoords(row, cols[n]));
+ }
+
+ ++iter;
+ }
+
+ return cellsExposed;
+}
+
+
+void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
+{
+ int x, y, row;
+ wxPoint pos( event.GetPosition() );
+ CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
+
+ if ( event.Dragging() )
+ {
+ if (!m_isDragging)
+ {
+ m_isDragging = true;
+ m_rowLabelWin->CaptureMouse();
+ }
+
+ if ( event.LeftIsDown() )
+ {
+ switch ( m_cursorMode )
+ {
+ case WXGRID_CURSOR_RESIZE_ROW:
+ {
+ int cw, ch, left, dummy;
+ m_gridWin->GetClientSize( &cw, &ch );
+ CalcUnscrolledPosition( 0, 0, &left, &dummy );
+
+ wxClientDC dc( m_gridWin );
+ PrepareDC( dc );
+ y = wxMax( y,
+ GetRowTop(m_dragRowOrCol) +
+ GetRowMinimalHeight(m_dragRowOrCol) );
+ dc.SetLogicalFunction(wxINVERT);
+ if ( m_dragLastPos >= 0 )
+ {
+ dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
+ }
+ dc.DrawLine( left, y, left+cw, y );
+ m_dragLastPos = y;
+ }
+ break;
+
+ case WXGRID_CURSOR_SELECT_ROW:
+ {
+ if ( (row = YToRow( y )) >= 0 )
+ {
+ if ( m_selection )
+ m_selection->SelectRow(row, event);
+ }
+ }
+ break;
+
+ // default label to suppress warnings about "enumeration value
+ // 'xxx' not handled in switch
+ default:
+ break;
+ }
+ }
+ return;
+ }
+
+ if ( m_isDragging && (event.Entering() || event.Leaving()) )
+ return;
+
+ if (m_isDragging)
+ {
+ if (m_rowLabelWin->HasCapture())
+ m_rowLabelWin->ReleaseMouse();
+ m_isDragging = false;
+ }
+
+ // ------------ Entering or leaving the window
+ //
+ if ( event.Entering() || event.Leaving() )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
+ }
+
+ // ------------ Left button pressed
+ //
+ else if ( event.LeftDown() )
+ {
+ row = YToEdgeOfRow(y);
+ if ( row != wxNOT_FOUND && CanDragRowSize(row) )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
+ }
+ else // not a request to start resizing
+ {
+ row = YToRow(y);
+ if ( row >= 0 &&
+ !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
+ {
+ if ( !event.ShiftDown() && !event.CmdDown() )
+ ClearSelection();
+ if ( m_selection )
+ {
+ if ( event.ShiftDown() )
+ {
+ m_selection->SelectBlock
+ (
+ m_currentCellCoords.GetRow(), 0,
+ row, GetNumberCols() - 1,
+ event
+ );
+ }
+ else
+ {
+ m_selection->SelectRow(row, event);
+ }
+ }
+
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
+ }
+ }
+ }
+
+ // ------------ Left double click
+ //
+ else if (event.LeftDClick() )
+ {
+ row = YToEdgeOfRow(y);
+ if ( row != wxNOT_FOUND && CanDragRowSize(row) )
+ {
+ // adjust row height depending on label text
+ //
+ // TODO: generate RESIZING event, see #10754
+ AutoSizeRowLabelSize( row );
+
+ SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, row, -1, event);
+
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
+ m_dragLastPos = -1;
+ }
+ else // not on row separator or it's not resizeable
+ {
+ row = YToRow(y);
+ if ( row >=0 &&
+ !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+ }
+
+ // ------------ Left button released
+ //
+ else if ( event.LeftUp() )
+ {
+ if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
+ DoEndDragResizeRow(event);
+
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
+ m_dragLastPos = -1;
+ }
+
+ // ------------ Right button down
+ //
+ else if ( event.RightDown() )
+ {
+ row = YToRow(y);
+ if ( row >=0 &&
+ !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+
+ // ------------ Right double click
+ //
+ else if ( event.RightDClick() )
+ {
+ row = YToRow(y);
+ if ( row >= 0 &&
+ !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+
+ // ------------ No buttons down and mouse moving
+ //
+ else if ( event.Moving() )
+ {
+ m_dragRowOrCol = YToEdgeOfRow( y );
+ if ( m_dragRowOrCol != wxNOT_FOUND )
+ {
+ if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
+ {
+ if ( CanDragRowSize(m_dragRowOrCol) )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
+ }
+ }
+ else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
+ }
+ }
+}
+
+void wxGrid::UpdateColumnSortingIndicator(int col)
+{
+ wxCHECK_RET( col != wxNOT_FOUND, "invalid column index" );
+
+ if ( m_useNativeHeader )
+ GetGridColHeader()->UpdateColumn(col);
+ else if ( m_nativeColumnLabels )
+ m_colWindow->Refresh();
+ //else: sorting indicator display not yet implemented in grid version
+}
+
+void wxGrid::SetSortingColumn(int col, bool ascending)
+{
+ if ( col == m_sortCol )
+ {
+ // we are already using this column for sorting (or not sorting at all)
+ // but we might still change the sorting order, check for it
+ if ( m_sortCol != wxNOT_FOUND && ascending != m_sortIsAscending )
+ {
+ m_sortIsAscending = ascending;
+
+ UpdateColumnSortingIndicator(m_sortCol);
+ }
+ }
+ else // we're changing the column used for sorting
+ {
+ const int sortColOld = m_sortCol;
+
+ // change it before updating the column as we want GetSortingColumn()
+ // to return the correct new value
+ m_sortCol = col;
+
+ if ( sortColOld != wxNOT_FOUND )
+ UpdateColumnSortingIndicator(sortColOld);
+
+ if ( m_sortCol != wxNOT_FOUND )
+ {
+ m_sortIsAscending = ascending;
+ UpdateColumnSortingIndicator(m_sortCol);
+ }
+ }
+}
+
+void wxGrid::DoColHeaderClick(int col)
+{
+ // we consider that the grid was resorted if this event is processed and
+ // not vetoed
+ if ( SendEvent(wxEVT_GRID_COL_SORT, -1, col) == 1 )
+ {
+ SetSortingColumn(col, IsSortingBy(col) ? !m_sortIsAscending : true);
+ Refresh();
+ }
+}
+
+void wxGrid::DoStartResizeCol(int col)
+{
+ m_dragRowOrCol = col;
+ m_dragLastPos = -1;
+ DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol));
+}
+
+void wxGrid::DoUpdateResizeCol(int x)
+{
+ int cw, ch, dummy, top;
+ m_gridWin->GetClientSize( &cw, &ch );
+ CalcUnscrolledPosition( 0, 0, &dummy, &top );
+
+ wxClientDC dc( m_gridWin );
+ PrepareDC( dc );
+
+ x = wxMax( x, GetColLeft(m_dragRowOrCol) + GetColMinimalWidth(m_dragRowOrCol));
+ dc.SetLogicalFunction(wxINVERT);
+ if ( m_dragLastPos >= 0 )
+ {
+ dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
+ }
+ dc.DrawLine( x, top, x, top + ch );
+ m_dragLastPos = x;
+}
+
+void wxGrid::DoUpdateResizeColWidth(int w)
+{
+ DoUpdateResizeCol(GetColLeft(m_dragRowOrCol) + w);
+}
+
+void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
+{
+ int x, y;
+ wxPoint pos( event.GetPosition() );
+ CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
+
+ int col = XToCol(x);
+ if ( event.Dragging() )
+ {
+ if (!m_isDragging)
+ {
+ m_isDragging = true;
+ GetColLabelWindow()->CaptureMouse();
+
+ if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL && col != -1 )
+ DoStartMoveCol(col);
+ }
+
+ if ( event.LeftIsDown() )
+ {
+ switch ( m_cursorMode )
+ {
+ case WXGRID_CURSOR_RESIZE_COL:
+ DoUpdateResizeCol(x);
+ break;
+
+ case WXGRID_CURSOR_SELECT_COL:
+ {
+ if ( col != -1 )
+ {
+ if ( m_selection )
+ m_selection->SelectCol(col, event);
+ }
+ }
+ break;
+
+ case WXGRID_CURSOR_MOVE_COL:
+ {
+ int posNew = XToPos(x);
+ int colNew = GetColAt(posNew);
+
+ // determine the position of the drop marker
+ int markerX;
+ if ( x >= GetColLeft(colNew) + (GetColWidth(colNew) / 2) )
+ markerX = GetColRight(colNew);
+ else
+ markerX = GetColLeft(colNew);
+
+ if ( markerX != m_dragLastPos )
+ {
+ wxClientDC dc( GetColLabelWindow() );
+ DoPrepareDC(dc);
+
+ int cw, ch;
+ GetColLabelWindow()->GetClientSize( &cw, &ch );
+
+ markerX++;
+
+ //Clean up the last indicator
+ if ( m_dragLastPos >= 0 )
+ {
+ wxPen pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
+ dc.SetPen(pen);
+ dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
+ dc.SetPen(wxNullPen);
+
+ if ( XToCol( m_dragLastPos ) != -1 )
+ DrawColLabel( dc, XToCol( m_dragLastPos ) );
+ }
+
+ const wxColour *color;
+ //Moving to the same place? Don't draw a marker
+ if ( colNew == m_dragRowOrCol )
+ color = wxLIGHT_GREY;
+ else
+ color = wxBLUE;
+
+ //Draw the marker
+ wxPen pen( *color, 2 );
+ dc.SetPen(pen);
+
+ dc.DrawLine( markerX, 0, markerX, ch );
+
+ dc.SetPen(wxNullPen);
+
+ m_dragLastPos = markerX - 1;
+ }
+ }
+ break;
+
+ // default label to suppress warnings about "enumeration value
+ // 'xxx' not handled in switch
+ default:
+ break;
+ }
+ }
+ return;
+ }
+
+ if ( m_isDragging && (event.Entering() || event.Leaving()) )
+ return;
+
+ if (m_isDragging)
+ {
+ if (GetColLabelWindow()->HasCapture())
+ GetColLabelWindow()->ReleaseMouse();
+ m_isDragging = false;
+ }
+
+ // ------------ Entering or leaving the window
+ //
+ if ( event.Entering() || event.Leaving() )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
+ }
+
+ // ------------ Left button pressed
+ //
+ else if ( event.LeftDown() )
+ {
+ int col = XToEdgeOfCol(x);
+ if ( col != wxNOT_FOUND && CanDragColSize(col) )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow());
+ }
+ else // not a request to start resizing
+ {
+ col = XToCol(x);
+ if ( col >= 0 &&
+ !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
+ {
+ if ( m_canDragColMove )
+ {
+ //Show button as pressed
+ wxClientDC dc( GetColLabelWindow() );
+ int colLeft = GetColLeft( col );
+ int colRight = GetColRight( col ) - 1;
+ dc.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
+ dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
+ dc.DrawLine( colLeft, 1, colRight, 1 );
+
+ ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, GetColLabelWindow());
+ }
+ else
+ {
+ if ( !event.ShiftDown() && !event.CmdDown() )
+ ClearSelection();
+ if ( m_selection )
+ {
+ if ( event.ShiftDown() )
+ {
+ m_selection->SelectBlock
+ (
+ 0, m_currentCellCoords.GetCol(),
+ GetNumberRows() - 1, col,
+ event
+ );
+ }
+ else
+ {
+ m_selection->SelectCol(col, event);
+ }
+ }
+
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, GetColLabelWindow());
+ }
+ }
+ }
+ }
+
+ // ------------ Left double click
+ //
+ if ( event.LeftDClick() )
+ {
+ const int colEdge = XToEdgeOfCol(x);
+ if ( colEdge == -1 )
+ {
+ if ( col >= 0 &&
+ ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+ else
+ {
+ // adjust column width depending on label text
+ //
+ // TODO: generate RESIZING event, see #10754
+ AutoSizeColLabelSize( colEdge );
+
+ SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, colEdge, event);
+
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
+ m_dragLastPos = -1;
+ }
+ }
+
+ // ------------ Left button released
+ //
+ else if ( event.LeftUp() )
+ {
+ switch ( m_cursorMode )
+ {
+ case WXGRID_CURSOR_RESIZE_COL:
+ DoEndDragResizeCol(event);
+ break;
+
+ case WXGRID_CURSOR_MOVE_COL:
+ if ( m_dragLastPos == -1 || col == m_dragRowOrCol )
+ {
+ // the column didn't actually move anywhere
+ if ( col != -1 )
+ DoColHeaderClick(col);
+ m_colWindow->Refresh(); // "unpress" the column
+ }
+ else
+ {
+ // get the position of the column we're over
+ int pos = XToPos(x);
+
+ // we may need to adjust the drop position but don't bother
+ // checking for it if we can't anyhow
+ if ( pos > 1 )
+ {
+ // also find the index of the column we're over: notice
+ // that the existing "col" variable may be invalid but
+ // we need a valid one here
+ const int colValid = GetColAt(pos);
+
+ // if we're on the "near" (usually left but right in
+ // RTL case) part of the column, the actual position we
+ // should be placed in is actually the one before it
+ bool onNearPart;
+ const int middle = GetColLeft(colValid) +
+ GetColWidth(colValid)/2;
+ if ( GetLayoutDirection() == wxLayout_LeftToRight )
+ onNearPart = (x <= middle);
+ else // wxLayout_RightToLeft
+ onNearPart = (x > middle);
+
+ if ( onNearPart )
+ pos--;
+ }
+
+ DoEndMoveCol(pos);
+ }
+ break;
+
+ case WXGRID_CURSOR_SELECT_COL:
+ case WXGRID_CURSOR_SELECT_CELL:
+ case WXGRID_CURSOR_RESIZE_ROW:
+ case WXGRID_CURSOR_SELECT_ROW:
+ if ( col != -1 )
+ DoColHeaderClick(col);
+ break;
+ }
+
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
+ m_dragLastPos = -1;
+ }
+
+ // ------------ Right button down
+ //
+ else if ( event.RightDown() )
+ {
+ if ( col >= 0 &&
+ !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+
+ // ------------ Right double click
+ //
+ else if ( event.RightDClick() )
+ {
+ if ( col >= 0 &&
+ !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+
+ // ------------ No buttons down and mouse moving
+ //
+ else if ( event.Moving() )
+ {
+ m_dragRowOrCol = XToEdgeOfCol( x );
+ if ( m_dragRowOrCol >= 0 )
+ {
+ if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
+ {
+ if ( CanDragColSize(m_dragRowOrCol) )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow(), false);
+ }
+ }
+ else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow(), false);
+ }
+ }
+}
+
+void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
+{
+ if ( event.LeftDown() )
+ {
+ // indicate corner label by having both row and
+ // col args == -1
+ //
+ if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
+ {
+ SelectAll();
+ }
+ }
+ else if ( event.LeftDClick() )
+ {
+ SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
+ }
+ else if ( event.RightDown() )
+ {
+ if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+ else if ( event.RightDClick() )
+ {
+ if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+}
+
+void wxGrid::CancelMouseCapture()
+{
+ // cancel operation currently in progress, whatever it is
+ if ( m_winCapture )
+ {
+ m_isDragging = false;
+ m_startDragPos = wxDefaultPosition;
+
+ m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
+ m_winCapture->SetCursor( *wxSTANDARD_CURSOR );
+ m_winCapture = NULL;
+
+ // remove traces of whatever we drew on screen
+ Refresh();
+ }
+}
+
+void wxGrid::ChangeCursorMode(CursorMode mode,
+ wxWindow *win,
+ bool captureMouse)
+{
+#if wxUSE_LOG_TRACE
+ static const wxChar *const cursorModes[] =
+ {
+ wxT("SELECT_CELL"),
+ wxT("RESIZE_ROW"),
+ wxT("RESIZE_COL"),
+ wxT("SELECT_ROW"),
+ wxT("SELECT_COL"),
+ wxT("MOVE_COL"),
+ };
+
+ wxLogTrace(wxT("grid"),
+ wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
+ win == m_colWindow ? wxT("colLabelWin")
+ : win ? wxT("rowLabelWin")
+ : wxT("gridWin"),
+ cursorModes[m_cursorMode], cursorModes[mode]);
+#endif // wxUSE_LOG_TRACE
+
+ if ( mode == m_cursorMode &&
+ win == m_winCapture &&
+ captureMouse == (m_winCapture != NULL))
+ return;
+
+ if ( !win )
+ {
+ // by default use the grid itself
+ win = m_gridWin;
+ }
+
+ if ( m_winCapture )
+ {
+ m_winCapture->ReleaseMouse();
+ m_winCapture = NULL;
+ }
+
+ m_cursorMode = mode;
+
+ switch ( m_cursorMode )
+ {
+ case WXGRID_CURSOR_RESIZE_ROW:
+ win->SetCursor( m_rowResizeCursor );
+ break;
+
+ case WXGRID_CURSOR_RESIZE_COL:
+ win->SetCursor( m_colResizeCursor );
+ break;
+
+ case WXGRID_CURSOR_MOVE_COL:
+ win->SetCursor( wxCursor(wxCURSOR_HAND) );
+ break;
+
+ default:
+ win->SetCursor( *wxSTANDARD_CURSOR );
+ break;
+ }
+
+ // we need to capture mouse when resizing
+ bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
+ m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
+
+ if ( captureMouse && resize )
+ {
+ win->CaptureMouse();
+ m_winCapture = win;
+ }
+}
+
+// ----------------------------------------------------------------------------
+// grid mouse event processing
+// ----------------------------------------------------------------------------
+
+void
+wxGrid::DoGridCellDrag(wxMouseEvent& event,
+ const wxGridCellCoords& coords,
+ bool isFirstDrag)
+{
+ if ( coords == wxGridNoCellCoords )
+ return; // we're outside any valid cell
+
+ // Hide the edit control, so it won't interfere with drag-shrinking.
+ if ( IsCellEditControlShown() )
+ {
+ HideCellEditControl();
+ SaveEditControlValue();
+ }
+
+ switch ( event.GetModifiers() )
+ {
+ case wxMOD_CMD:
+ if ( m_selectedBlockCorner == wxGridNoCellCoords)
+ m_selectedBlockCorner = coords;
+ UpdateBlockBeingSelected(m_selectedBlockCorner, coords);
+ break;
+
+ case wxMOD_NONE:
+ if ( CanDragCell() )
+ {
+ if ( isFirstDrag )
+ {
+ if ( m_selectedBlockCorner == wxGridNoCellCoords)
+ m_selectedBlockCorner = coords;
+
+ SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG, coords, event);
+ return;
+ }
+ }
+
+ UpdateBlockBeingSelected(m_currentCellCoords, coords);
+ break;
+
+ default:
+ // we don't handle the other key modifiers
+ event.Skip();
+ }
+}
+
+void wxGrid::DoGridLineDrag(wxMouseEvent& event, const wxGridOperations& oper)
+{
+ wxClientDC dc(m_gridWin);
+ PrepareDC(dc);
+ dc.SetLogicalFunction(wxINVERT);
+
+ const wxRect rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
+ m_gridWin->GetClientSize());
+
+ // erase the previously drawn line, if any
+ if ( m_dragLastPos >= 0 )
+ oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos);
+
+ // we need the vertical position for rows and horizontal for columns here
+ m_dragLastPos = oper.Dual().Select(CalcUnscrolledPosition(event.GetPosition()));
+
+ // don't allow resizing beneath the minimal size
+ const int posMin = oper.GetLineStartPos(this, m_dragRowOrCol) +
+ oper.GetMinimalLineSize(this, m_dragRowOrCol);
+ if ( m_dragLastPos < posMin )
+ m_dragLastPos = posMin;
+
+ // and draw it at the new position
+ oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos);
+}
+
+void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords)
+{
+ if ( !m_isDragging )
+ {
+ // Don't start doing anything until the mouse has been dragged far
+ // enough
+ const wxPoint& pt = event.GetPosition();
+ if ( m_startDragPos == wxDefaultPosition )
+ {
+ m_startDragPos = pt;
+ return;
+ }
+
+ if ( abs(m_startDragPos.x - pt.x) <= DRAG_SENSITIVITY &&
+ abs(m_startDragPos.y - pt.y) <= DRAG_SENSITIVITY )
+ return;
+ }
+
+ const bool isFirstDrag = !m_isDragging;
+ m_isDragging = true;
+
+ switch ( m_cursorMode )
+ {
+ case WXGRID_CURSOR_SELECT_CELL:
+ DoGridCellDrag(event, coords, isFirstDrag);
+ break;
+
+ case WXGRID_CURSOR_RESIZE_ROW:
+ DoGridLineDrag(event, wxGridRowOperations());
+ break;
+
+ case WXGRID_CURSOR_RESIZE_COL:
+ DoGridLineDrag(event, wxGridColumnOperations());
+ break;
+
+ default:
+ event.Skip();
+ }
+
+ if ( isFirstDrag )
+ {
+ wxASSERT_MSG( !m_winCapture, "shouldn't capture the mouse twice" );
+
+ m_winCapture = m_gridWin;
+ m_winCapture->CaptureMouse();
+ }
+}
+
+void
+wxGrid::DoGridCellLeftDown(wxMouseEvent& event,
+ const wxGridCellCoords& coords,
+ const wxPoint& pos)
+{
+ if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK, coords, event) )
+ {
+ // event handled by user code, no need to do anything here
+ return;
+ }
+
+ if ( !event.CmdDown() )
+ ClearSelection();
+
+ if ( event.ShiftDown() )
+ {
+ if ( m_selection )
+ {
+ m_selection->SelectBlock(m_currentCellCoords, coords, event);
+ m_selectedBlockCorner = coords;
+ }
+ }
+ else if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 )
+ {
+ DisableCellEditControl();
+ MakeCellVisible( coords );
+
+ if ( event.CmdDown() )
+ {
+ if ( m_selection )
+ {
+ m_selection->ToggleCellSelection(coords, event);
+ }
+
+ m_selectedBlockTopLeft = wxGridNoCellCoords;
+ m_selectedBlockBottomRight = wxGridNoCellCoords;
+ m_selectedBlockCorner = coords;
+ }
+ else
+ {
+ if ( m_selection )
+ {
+ // In row or column selection mode just clicking on the cell
+ // should select the row or column containing it: this is more
+ // convenient for the kinds of controls that use such selection
+ // mode and is compatible with 2.8 behaviour (see #12062).
+ switch ( m_selection->GetSelectionMode() )
+ {
+ case wxGridSelectCells:
+ case wxGridSelectRowsOrColumns:
+ // nothing to do in these cases
+ break;
+
+ case wxGridSelectRows:
+ m_selection->SelectRow(coords.GetRow());
+ break;
+
+ case wxGridSelectColumns:
+ m_selection->SelectCol(coords.GetCol());
+ break;
+ }
+ }
+
+ m_waitForSlowClick = m_currentCellCoords == coords &&
+ coords != wxGridNoCellCoords;
+ SetCurrentCell( coords );
+ }
+ }
+}
+
+void
+wxGrid::DoGridCellLeftDClick(wxMouseEvent& event,
+ const wxGridCellCoords& coords,
+ const wxPoint& pos)
+{
+ if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 )
+ {
+ if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK, coords, event) )
+ {
+ // we want double click to select a cell and start editing
+ // (i.e. to behave in same way as sequence of two slow clicks):
+ m_waitForSlowClick = true;
+ }
+ }
+}
+
+void
+wxGrid::DoGridCellLeftUp(wxMouseEvent& event, const wxGridCellCoords& coords)
+{
+ if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
+ {
+ if (m_winCapture)
+ {
+ m_winCapture->ReleaseMouse();
+ m_winCapture = NULL;
+ }
+
+ if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
+ {
+ ClearSelection();
+ EnableCellEditControl();
+
+ wxGridCellAttr *attr = GetCellAttr(coords);
+ wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
+ editor->StartingClick();
+ editor->DecRef();
+ attr->DecRef();
+
+ m_waitForSlowClick = false;
+ }
+ else if ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
+ m_selectedBlockBottomRight != wxGridNoCellCoords )
+ {
+ if ( m_selection )
+ {
+ m_selection->SelectBlock( m_selectedBlockTopLeft,
+ m_selectedBlockBottomRight,
+ event );
+ }
+
+ m_selectedBlockTopLeft = wxGridNoCellCoords;
+ m_selectedBlockBottomRight = wxGridNoCellCoords;
+
+ // Show the edit control, if it has been hidden for
+ // drag-shrinking.
+ ShowCellEditControl();
+ }
+ }
+ else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
+ DoEndDragResizeRow(event);
+ }
+ else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
+ DoEndDragResizeCol(event);
+ }
+
+ m_dragLastPos = -1;
+}
+
+void
+wxGrid::DoGridMouseMoveEvent(wxMouseEvent& WXUNUSED(event),
+ const wxGridCellCoords& coords,
+ const wxPoint& pos)
+{
+ if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
+ {
+ // out of grid cell area
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
+ return;
+ }
+
+ int dragRow = YToEdgeOfRow( pos.y );
+ int dragCol = XToEdgeOfCol( pos.x );
+
+ // Dragging on the corner of a cell to resize in both
+ // directions is not implemented yet...
+ //
+ if ( dragRow >= 0 && dragCol >= 0 )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
+ return;
+ }
+
+ if ( dragRow >= 0 && CanDragGridSize() && CanDragRowSize(dragRow) )
+ {
+ if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
+ {
+ m_dragRowOrCol = dragRow;
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false);
+ }
+ }
+ // When using the native header window we can only resize the columns by
+ // dragging the dividers in it because we can't make it enter into the
+ // column resizing mode programmatically
+ else if ( dragCol >= 0 && !m_useNativeHeader &&
+ CanDragGridSize() && CanDragColSize(dragCol) )
+ {
+ if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
+ {
+ m_dragRowOrCol = dragCol;
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false);
+ }
+ }
+ else // Neither on a row or col edge
+ {
+ if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
+ }
+ }
+}
+
+void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event)
+{
+ if ( event.Entering() || event.Leaving() )
+ {
+ // we don't care about these events but we must not reset m_isDragging
+ // if they happen so return before anything else is done
+ event.Skip();
+ return;
+ }
+
+ const wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
+
+ // coordinates of the cell under mouse
+ wxGridCellCoords coords = XYToCell(pos);
+
+ int cell_rows, cell_cols;
+ GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
+ if ( (cell_rows < 0) || (cell_cols < 0) )
+ {
+ coords.SetRow(coords.GetRow() + cell_rows);
+ coords.SetCol(coords.GetCol() + cell_cols);
+ }
+
+ if ( event.Dragging() )
+ {
+ if ( event.LeftIsDown() )
+ DoGridDragEvent(event, coords);
+ else
+ event.Skip();
+ return;
+ }
+
+ m_isDragging = false;
+ m_startDragPos = wxDefaultPosition;
+
+ // deal with various button presses
+ if ( event.IsButton() )
+ {
+ if ( coords != wxGridNoCellCoords )
+ {
+ DisableCellEditControl();
+
+ if ( event.LeftDown() )
+ DoGridCellLeftDown(event, coords, pos);
+ else if ( event.LeftDClick() )
+ DoGridCellLeftDClick(event, coords, pos);
+ else if ( event.RightDown() )
+ SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK, coords, event);
+ else if ( event.RightDClick() )
+ SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK, coords, event);
+ }
+
+ // this one should be called even if we're not over any cell
+ if ( event.LeftUp() )
+ {
+ DoGridCellLeftUp(event, coords);
+ }
+ }
+ else if ( event.Moving() )
+ {
+ DoGridMouseMoveEvent(event, coords, pos);
+ }
+ else // unknown mouse event?
+ {
+ event.Skip();
+ }
+}
+
+// this function returns true only if the size really changed
+bool wxGrid::DoEndDragResizeLine(const wxGridOperations& oper)
+{
+ if ( m_dragLastPos == -1 )
+ return false;
+
+ const wxGridOperations& doper = oper.Dual();
+
+ const wxSize size = m_gridWin->GetClientSize();
+
+ const wxPoint ptOrigin = CalcUnscrolledPosition(wxPoint(0, 0));
+
+ // erase the last line we drew
+ wxClientDC dc(m_gridWin);
+ PrepareDC(dc);
+ dc.SetLogicalFunction(wxINVERT);
+
+ const int posLineStart = oper.Select(ptOrigin);
+ const int posLineEnd = oper.Select(ptOrigin) + oper.Select(size);
+
+ oper.DrawParallelLine(dc, posLineStart, posLineEnd, m_dragLastPos);
+
+ // temporarily hide the edit control before resizing
+ HideCellEditControl();
+ SaveEditControlValue();
+
+ // do resize the line
+ const int lineStart = oper.GetLineStartPos(this, m_dragRowOrCol);
+ const int lineSizeOld = oper.GetLineSize(this, m_dragRowOrCol);
+ oper.SetLineSize(this, m_dragRowOrCol,
+ wxMax(m_dragLastPos - lineStart,
+ oper.GetMinimalLineSize(this, m_dragRowOrCol)));
+ const bool
+ sizeChanged = oper.GetLineSize(this, m_dragRowOrCol) != lineSizeOld;
+
+ m_dragLastPos = -1;
+
+ // refresh now if we're not frozen
+ if ( !GetBatchCount() )
+ {
+ // we need to refresh everything beyond the resized line in the header
+ // window
+
+ // get the position from which to refresh in the other direction
+ wxRect rect(CellToRect(oper.MakeCoords(m_dragRowOrCol, 0)));
+ rect.SetPosition(CalcScrolledPosition(rect.GetPosition()));
+
+ // we only need the ordinate (for rows) or abscissa (for columns) here,
+ // and need to cover the entire window in the other direction
+ oper.Select(rect) = 0;
+
+ wxRect rectHeader(rect.GetPosition(),
+ oper.MakeSize
+ (
+ oper.GetHeaderWindowSize(this),
+ doper.Select(size) - doper.Select(rect)
+ ));
+
+ oper.GetHeaderWindow(this)->Refresh(true, &rectHeader);
+
+
+ // also refresh the grid window: extend the rectangle
+ if ( m_table )
+ {
+ oper.SelectSize(rect) = oper.Select(size);
+
+ int subtractLines = 0;
+ const int lineStart = doper.PosToLine(this, posLineStart);
+ if ( lineStart >= 0 )
+ {
+ // ensure that if we have a multi-cell block we redraw all of
+ // it by increasing the refresh area to cover it entirely if a
+ // part of it is affected
+ const int lineEnd = doper.PosToLine(this, posLineEnd, true);
+ for ( int line = lineStart; line < lineEnd; line++ )
+ {
+ int cellLines = oper.Select(
+ GetCellSize(oper.MakeCoords(m_dragRowOrCol, line)));
+ if ( cellLines < subtractLines )
+ subtractLines = cellLines;
+ }
+ }
+
+ int startPos =
+ oper.GetLineStartPos(this, m_dragRowOrCol + subtractLines);
+ startPos = doper.CalcScrolledPosition(this, startPos);
+
+ doper.Select(rect) = startPos;
+ doper.SelectSize(rect) = doper.Select(size) - startPos;
+
+ m_gridWin->Refresh(false, &rect);
+ }
+ }
+
+ // show the edit control back again
+ ShowCellEditControl();
+
+ return sizeChanged;
+}
+
+void wxGrid::DoEndDragResizeRow(const wxMouseEvent& event)
+{
+ // TODO: generate RESIZING event, see #10754
+
+ if ( DoEndDragResizeLine(wxGridRowOperations()) )
+ SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event);
+}
+
+void wxGrid::DoEndDragResizeCol(const wxMouseEvent& event)
+{
+ // TODO: generate RESIZING event, see #10754
+
+ if ( DoEndDragResizeLine(wxGridColumnOperations()) )
+ SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event);
+}
+
+void wxGrid::DoStartMoveCol(int col)
+{
+ m_dragRowOrCol = col;
+}
+
+void wxGrid::DoEndMoveCol(int pos)
+{
+ wxASSERT_MSG( m_dragRowOrCol != -1, "no matching DoStartMoveCol?" );
+
+ if ( SendEvent(wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol) != -1 )
+ SetColPos(m_dragRowOrCol, pos);
+ //else: vetoed by user
+
+ m_dragRowOrCol = -1;
+}
+
+void wxGrid::RefreshAfterColPosChange()
+{
+ // recalculate the column rights as the column positions have changed,
+ // unless we calculate them dynamically because all columns widths are the
+ // same and it's easy to do
+ if ( !m_colWidths.empty() )
+ {
+ int colRight = 0;
+ for ( int colPos = 0; colPos < m_numCols; colPos++ )
+ {
+ int colID = GetColAt( colPos );
+
+ colRight += m_colWidths[colID];
+ m_colRights[colID] = colRight;
+ }
+ }
+
+ // and make the changes visible
+ if ( m_useNativeHeader )
+ {
+ if ( m_colAt.empty() )
+ GetGridColHeader()->ResetColumnsOrder();
+ else
+ GetGridColHeader()->SetColumnsOrder(m_colAt);
+ }
+ else
+ {
+ m_colWindow->Refresh();
+ }
+ m_gridWin->Refresh();
+}
+
+void wxGrid::SetColumnsOrder(const wxArrayInt& order)
+{
+ m_colAt = order;
+
+ RefreshAfterColPosChange();
+}
+
+void wxGrid::SetColPos(int idx, int pos)
+{
+ // we're going to need m_colAt now, initialize it if needed
+ if ( m_colAt.empty() )
+ {
+ m_colAt.reserve(m_numCols);
+ for ( int i = 0; i < m_numCols; i++ )
+ m_colAt.push_back(i);
+ }
+
+ wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos);
+
+ RefreshAfterColPosChange();
+}
+
+void wxGrid::ResetColPos()
+{
+ m_colAt.clear();
+
+ RefreshAfterColPosChange();
+}
+
+void wxGrid::EnableDragColMove( bool enable )
+{
+ if ( m_canDragColMove == enable )
+ return;
+
+ if ( m_useNativeHeader )
+ {
+ // update all columns to make them [not] reorderable
+ GetGridColHeader()->SetColumnCount(m_numCols);
+ }
+
+ m_canDragColMove = enable;
+
+ // we use to call ResetColPos() from here if !enable but this doesn't seem
+ // right as it would mean there would be no way to "freeze" the current
+ // columns order by disabling moving them after putting them in the desired
+ // order, whereas now you can always call ResetColPos() manually if needed
+}
+
+
+//
+// ------ interaction with data model
+//
+bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
+{
+ switch ( msg.GetId() )
+ {
+ case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
+ return GetModelValues();
+
+ case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
+ return SetModelValues();
+
+ case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
+ case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
+ case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
+ case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
+ case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
+ case wxGRIDTABLE_NOTIFY_COLS_DELETED:
+ return Redimension( msg );
+
+ default:
+ return false;
+ }
+}
+
+// The behaviour of this function depends on the grid table class
+// Clear() function. For the default wxGridStringTable class the
+// behaviour is to replace all cell contents with wxEmptyString but
+// not to change the number of rows or cols.
+//
+void wxGrid::ClearGrid()
+{
+ if ( m_table )
+ {
+ if (IsCellEditControlEnabled())
+ DisableCellEditControl();
+
+ m_table->Clear();
+ if (!GetBatchCount())
+ m_gridWin->Refresh();
+ }
+}
+
+bool
+wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify)(size_t, size_t),
+ int pos, int num, bool WXUNUSED(updateLabels) )
+{
+ wxCHECK_MSG( m_created, false, "must finish creating the grid first" );
+
+ if ( !m_table )
+ return false;
+
+ if ( IsCellEditControlEnabled() )
+ DisableCellEditControl();
+
+ return (m_table->*funcModify)(pos, num);
+
+ // the table will have sent the results of the insert row
+ // operation to this view object as a grid table message
+}
+
+bool
+wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend)(size_t),
+ int num, bool WXUNUSED(updateLabels))
+{
+ wxCHECK_MSG( m_created, false, "must finish creating the grid first" );
+
+ if ( !m_table )
+ return false;
+
+ return (m_table->*funcAppend)(num);
+}
+
+// ----------------------------------------------------------------------------
+// event generation helpers
+// ----------------------------------------------------------------------------
+
+void
+wxGrid::SendGridSizeEvent(wxEventType type,
+ int row, int col,
+ const wxMouseEvent& mouseEv)
+{
+ int rowOrCol = row == -1 ? col : row;
+
+ wxGridSizeEvent gridEvt( GetId(),
+ type,
+ this,
+ rowOrCol,
+ mouseEv.GetX() + GetRowLabelSize(),
+ mouseEv.GetY() + GetColLabelSize(),
+ mouseEv);
+
+ GetEventHandler()->ProcessEvent(gridEvt);
+}
+
+// Generate a grid event based on a mouse event and return:
+// -1 if the event was vetoed
+// +1 if the event was processed (but not vetoed)
+// 0 if the event wasn't handled
+int
+wxGrid::SendEvent(const wxEventType type,
+ int row, int col,
+ const wxMouseEvent& mouseEv)
+{
+ bool claimed, vetoed;
+
+ if ( type == wxEVT_GRID_RANGE_SELECT )
+ {
+ // Right now, it should _never_ end up here!
+ wxGridRangeSelectEvent gridEvt( GetId(),
+ type,
+ this,
+ m_selectedBlockTopLeft,
+ m_selectedBlockBottomRight,
+ true,
+ mouseEv);
+
+ claimed = GetEventHandler()->ProcessEvent(gridEvt);
+ vetoed = !gridEvt.IsAllowed();
+ }
+ else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
+ type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
+ type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
+ type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
+ {
+ wxPoint pos = mouseEv.GetPosition();
+
+ if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
+ pos.y += GetColLabelSize();
+ if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
+ pos.x += GetRowLabelSize();
+
+ wxGridEvent gridEvt( GetId(),
+ type,
+ this,
+ row, col,
+ pos.x,
+ pos.y,
+ false,
+ mouseEv);
+ claimed = GetEventHandler()->ProcessEvent(gridEvt);
+ vetoed = !gridEvt.IsAllowed();
+ }
+ else
+ {
+ wxGridEvent gridEvt( GetId(),
+ type,
+ this,
+ row, col,
+ mouseEv.GetX() + GetRowLabelSize(),
+ mouseEv.GetY() + GetColLabelSize(),
+ false,
+ mouseEv);
+ claimed = GetEventHandler()->ProcessEvent(gridEvt);
+ vetoed = !gridEvt.IsAllowed();
+ }
+
+ // A Veto'd event may not be `claimed' so test this first
+ if (vetoed)
+ return -1;
+
+ return claimed ? 1 : 0;
+}
+
+// Generate a grid event of specified type, return value same as above
+//
+int
+wxGrid::SendEvent(const wxEventType type, int row, int col, const wxString& s)
+{
+ wxGridEvent gridEvt( GetId(), type, this, row, col );
+ gridEvt.SetString(s);
+
+ const bool claimed = GetEventHandler()->ProcessEvent(gridEvt);
+
+ // A Veto'd event may not be `claimed' so test this first
+ if ( !gridEvt.IsAllowed() )
+ return -1;
+
+ return claimed ? 1 : 0;
+}
+
+void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
+{
+ // needed to prevent zillions of paint events on MSW
+ wxPaintDC dc(this);
+}
+
+void wxGrid::Refresh(bool eraseb, const wxRect* rect)
+{
+ // Don't do anything if between Begin/EndBatch...
+ // EndBatch() will do all this on the last nested one anyway.
+ if ( m_created && !GetBatchCount() )
+ {
+ // Refresh to get correct scrolled position:
+ wxScrolledWindow::Refresh(eraseb, rect);
+
+ if (rect)
+ {
+ int rect_x, rect_y, rectWidth, rectHeight;
+ int width_label, width_cell, height_label, height_cell;
+ int x, y;
+
+ // Copy rectangle can get scroll offsets..
+ rect_x = rect->GetX();
+ rect_y = rect->GetY();
+ rectWidth = rect->GetWidth();
+ rectHeight = rect->GetHeight();
+
+ width_label = m_rowLabelWidth - rect_x;
+ if (width_label > rectWidth)
+ width_label = rectWidth;
+
+ height_label = m_colLabelHeight - rect_y;
+ if (height_label > rectHeight)
+ height_label = rectHeight;
+
+ if (rect_x > m_rowLabelWidth)
+ {
+ x = rect_x - m_rowLabelWidth;
+ width_cell = rectWidth;
+ }
+ else
+ {
+ x = 0;
+ width_cell = rectWidth - (m_rowLabelWidth - rect_x);
+ }
+
+ if (rect_y > m_colLabelHeight)
+ {
+ y = rect_y - m_colLabelHeight;
+ height_cell = rectHeight;
+ }
+ else
+ {
+ y = 0;
+ height_cell = rectHeight - (m_colLabelHeight - rect_y);
+ }
+
+ // Paint corner label part intersecting rect.
+ if ( width_label > 0 && height_label > 0 )
+ {
+ wxRect anotherrect(rect_x, rect_y, width_label, height_label);
+ m_cornerLabelWin->Refresh(eraseb, &anotherrect);
+ }
+
+ // Paint col labels part intersecting rect.
+ if ( width_cell > 0 && height_label > 0 )
+ {
+ wxRect anotherrect(x, rect_y, width_cell, height_label);
+ m_colWindow->Refresh(eraseb, &anotherrect);
+ }
+
+ // Paint row labels part intersecting rect.
+ if ( width_label > 0 && height_cell > 0 )
+ {
+ wxRect anotherrect(rect_x, y, width_label, height_cell);
+ m_rowLabelWin->Refresh(eraseb, &anotherrect);
+ }
+
+ // Paint cell area part intersecting rect.
+ if ( width_cell > 0 && height_cell > 0 )
+ {
+ wxRect anotherrect(x, y, width_cell, height_cell);
+ m_gridWin->Refresh(eraseb, &anotherrect);
+ }
+ }
+ else
+ {
+ m_cornerLabelWin->Refresh(eraseb, NULL);
+ m_colWindow->Refresh(eraseb, NULL);
+ m_rowLabelWin->Refresh(eraseb, NULL);
+ m_gridWin->Refresh(eraseb, NULL);
+ }
+ }
+}
+
+void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event))
+{
+ if (m_targetWindow != this) // check whether initialisation has been done
+ {
+ // reposition our children windows
+ CalcWindowSizes();
+ }
+}
+
+void wxGrid::OnKeyDown( wxKeyEvent& event )
+{
+ if ( m_inOnKeyDown )
+ {
+ // shouldn't be here - we are going round in circles...
+ //
+ wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
+ }
+
+ m_inOnKeyDown = true;
+
+ // propagate the event up and see if it gets processed
+ wxWindow *parent = GetParent();
+ wxKeyEvent keyEvt( event );
+ keyEvt.SetEventObject( parent );
+
+ if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
+ {
+ if (GetLayoutDirection() == wxLayout_RightToLeft)
+ {
+ if (event.GetKeyCode() == WXK_RIGHT)
+ event.m_keyCode = WXK_LEFT;
+ else if (event.GetKeyCode() == WXK_LEFT)
+ event.m_keyCode = WXK_RIGHT;
+ }
+
+ // try local handlers
+ switch ( event.GetKeyCode() )
+ {
+ case WXK_UP:
+ if ( event.ControlDown() )
+ MoveCursorUpBlock( event.ShiftDown() );
+ else
+ MoveCursorUp( event.ShiftDown() );
+ break;
+
+ case WXK_DOWN:
+ if ( event.ControlDown() )
+ MoveCursorDownBlock( event.ShiftDown() );
+ else
+ MoveCursorDown( event.ShiftDown() );
+ break;
+
+ case WXK_LEFT:
+ if ( event.ControlDown() )
+ MoveCursorLeftBlock( event.ShiftDown() );
+ else
+ MoveCursorLeft( event.ShiftDown() );
+ break;
+
+ case WXK_RIGHT:
+ if ( event.ControlDown() )
+ MoveCursorRightBlock( event.ShiftDown() );
+ else
+ MoveCursorRight( event.ShiftDown() );
+ break;
+
+ case WXK_RETURN:
+ case WXK_NUMPAD_ENTER:
+ if ( event.ControlDown() )
+ {
+ event.Skip(); // to let the edit control have the return
+ }
+ else
+ {
+ if ( GetGridCursorRow() < GetNumberRows()-1 )
+ {
+ MoveCursorDown( event.ShiftDown() );
+ }
+ else
+ {
+ // at the bottom of a column
+ DisableCellEditControl();
+ }
+ }
+ break;
+
+ case WXK_ESCAPE:
+ ClearSelection();
+ break;
+
+ case WXK_TAB:
+ if (event.ShiftDown())
+ {
+ if ( GetGridCursorCol() > 0 )
+ {
+ MoveCursorLeft( false );
+ }
+ else
+ {
+ // at left of grid
+ DisableCellEditControl();
+ }
+ }
+ else
+ {
+ if ( GetGridCursorCol() < GetNumberCols() - 1 )
+ {
+ MoveCursorRight( false );
+ }
+ else
+ {
+ // at right of grid
+ DisableCellEditControl();
+ }
+ }
+ break;
+
+ case WXK_HOME:
+ GoToCell(event.ControlDown() ? 0
+ : m_currentCellCoords.GetRow(),
+ 0);
+ break;
+
+ case WXK_END:
+ GoToCell(event.ControlDown() ? m_numRows - 1
+ : m_currentCellCoords.GetRow(),
+ m_numCols - 1);
+ break;
+
+ case WXK_PAGEUP:
+ MovePageUp();
+ break;
+
+ case WXK_PAGEDOWN:
+ MovePageDown();
+ break;
+
+ case WXK_SPACE:
+ // Ctrl-Space selects the current column, Shift-Space -- the
+ // current row and Ctrl-Shift-Space -- everything
+ switch ( m_selection ? event.GetModifiers() : wxMOD_NONE )
+ {
+ case wxMOD_CONTROL:
+ m_selection->SelectCol(m_currentCellCoords.GetCol());
+ break;
+
+ case wxMOD_SHIFT:
+ m_selection->SelectRow(m_currentCellCoords.GetRow());
+ break;
+
+ case wxMOD_CONTROL | wxMOD_SHIFT:
+ m_selection->SelectBlock(0, 0,
+ m_numRows - 1, m_numCols - 1);
+ break;
+
+ case wxMOD_NONE:
+ if ( !IsEditable() )
+ {
+ MoveCursorRight(false);
+ break;
+ }
+ //else: fall through
+
+ default:
+ event.Skip();
+ }
+ break;
+
+ default:
+ event.Skip();
+ break;
+ }
+ }
+
+ m_inOnKeyDown = false;
+}
+
+void wxGrid::OnKeyUp( wxKeyEvent& event )
+{
+ // try local handlers
+ //
+ if ( event.GetKeyCode() == WXK_SHIFT )
+ {
+ if ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
+ m_selectedBlockBottomRight != wxGridNoCellCoords )
+ {
+ if ( m_selection )
+ {
+ m_selection->SelectBlock(
+ m_selectedBlockTopLeft,
+ m_selectedBlockBottomRight,
+ event);
+ }
+ }
+
+ m_selectedBlockTopLeft = wxGridNoCellCoords;
+ m_selectedBlockBottomRight = wxGridNoCellCoords;
+ m_selectedBlockCorner = wxGridNoCellCoords;
+ }
+}
+
+void wxGrid::OnChar( wxKeyEvent& event )
+{
+ // is it possible to edit the current cell at all?
+ if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
+ {
+ // yes, now check whether the cells editor accepts the key
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ wxGridCellEditor *editor = attr->GetEditor(this, row, col);
+
+ // <F2> is special and will always start editing, for
+ // other keys - ask the editor itself
+ if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
+ || editor->IsAcceptedKey(event) )
+ {
+ // ensure cell is visble
+ MakeCellVisible(row, col);
+ EnableCellEditControl();
+
+ // a problem can arise if the cell is not completely
+ // visible (even after calling MakeCellVisible the
+ // control is not created and calling StartingKey will
+ // crash the app
+ if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
+ editor->StartingKey(event);
+ }
+ else
+ {
+ event.Skip();
+ }
+
+ editor->DecRef();
+ attr->DecRef();
+ }
+ else
+ {
+ event.Skip();
+ }
+}
+
+void wxGrid::OnEraseBackground(wxEraseEvent&)
+{
+}
+
+bool wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
+{
+ if ( SendEvent(wxEVT_GRID_SELECT_CELL, coords) == -1 )
+ {
+ // the event has been vetoed - do nothing
+ return false;
+ }
+
+#if !defined(__WXMAC__)
+ wxClientDC dc( m_gridWin );
+ PrepareDC( dc );
+#endif
+
+ if ( m_currentCellCoords != wxGridNoCellCoords )
+ {
+ DisableCellEditControl();
+
+ if ( IsVisible( m_currentCellCoords, false ) )
+ {
+ wxRect r;
+ r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
+ if ( !m_gridLinesEnabled )
+ {
+ r.x--;
+ r.y--;
+ r.width++;
+ r.height++;
+ }
+
+ wxGridCellCoordsArray cells = CalcCellsExposed( r );
+
+ // Otherwise refresh redraws the highlight!
+ m_currentCellCoords = coords;
+
+#if defined(__WXMAC__)
+ m_gridWin->Refresh(true /*, & r */);
+#else
+ DrawGridCellArea( dc, cells );
+ DrawAllGridLines( dc, r );
+#endif
+ }
+ }
+
+ m_currentCellCoords = coords;
+
+ wxGridCellAttr *attr = GetCellAttr( coords );
+#if !defined(__WXMAC__)
+ DrawCellHighlight( dc, attr );
+#endif
+ attr->DecRef();
+
+ return true;
+}
+
+void
+wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol,
+ int bottomRow, int rightCol)
+{
+ if ( m_selection )
+ {
+ switch ( m_selection->GetSelectionMode() )
+ {
+ default:
+ wxFAIL_MSG( "unknown selection mode" );
+ // fall through
+
+ case wxGridSelectCells:
+ // arbitrary blocks selection allowed so just use the cell
+ // coordinates as is
+ break;
+
+ case wxGridSelectRows:
+ // only full rows selection allowd, ensure that we do select
+ // full rows
+ leftCol = 0;
+ rightCol = GetNumberCols() - 1;
+ break;
+
+ case wxGridSelectColumns:
+ // same as above but for columns
+ topRow = 0;
+ bottomRow = GetNumberRows() - 1;
+ break;
+
+ case wxGridSelectRowsOrColumns:
+ // in this mode we can select only full rows or full columns so
+ // it doesn't make sense to select blocks at all (and we can't
+ // extend the block because there is no preferred direction, we
+ // could only extend it to cover the entire grid but this is
+ // not useful)
+ return;
+ }
+ }
+
+ m_selectedBlockCorner = wxGridCellCoords(bottomRow, rightCol);
+ MakeCellVisible(m_selectedBlockCorner);
+
+ EnsureFirstLessThanSecond(topRow, bottomRow);
+ EnsureFirstLessThanSecond(leftCol, rightCol);
+
+ wxGridCellCoords updateTopLeft = wxGridCellCoords(topRow, leftCol),
+ updateBottomRight = wxGridCellCoords(bottomRow, rightCol);
+
+ // First the case that we selected a completely new area
+ if ( m_selectedBlockTopLeft == wxGridNoCellCoords ||
+ m_selectedBlockBottomRight == wxGridNoCellCoords )
+ {
+ wxRect rect;
+ rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
+ wxGridCellCoords ( bottomRow, rightCol ) );
+ m_gridWin->Refresh( false, &rect );
+ }
+
+ // Now handle changing an existing selection area.
+ else if ( m_selectedBlockTopLeft != updateTopLeft ||
+ m_selectedBlockBottomRight != updateBottomRight )
+ {
+ // Compute two optimal update rectangles:
+ // Either one rectangle is a real subset of the
+ // other, or they are (almost) disjoint!
+ wxRect rect[4];
+ bool need_refresh[4];
+ need_refresh[0] =
+ need_refresh[1] =
+ need_refresh[2] =
+ need_refresh[3] = false;
+ int i;
+
+ // Store intermediate values
+ wxCoord oldLeft = m_selectedBlockTopLeft.GetCol();
+ wxCoord oldTop = m_selectedBlockTopLeft.GetRow();
+ wxCoord oldRight = m_selectedBlockBottomRight.GetCol();
+ wxCoord oldBottom = m_selectedBlockBottomRight.GetRow();