X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/304969053696308d0d68a063c999309c618dad19..6f026b5b63fe7ccb025e84509886f74772b9df13:/src/generic/grid.cpp diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 86612f914d..38c1b9f92c 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -4,7 +4,6 @@ // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn) // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios // Created: 1/08/1999 -// RCS-ID: $Id$ // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au) // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -146,6 +145,7 @@ wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE, wxGridSizeEvent ); wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE, wxGridSizeEvent ); +wxDEFINE_EVENT( wxEVT_GRID_COL_AUTO_SIZE, wxGridSizeEvent ); wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_COL_SORT, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT, wxGridRangeSelectEvent ); @@ -155,6 +155,7 @@ wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED, wxGridEditorCreatedEvent ); +wxDEFINE_EVENT( wxEVT_GRID_TABBING, wxGridEvent ); // ---------------------------------------------------------------------------- // private helpers @@ -2157,6 +2158,7 @@ BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow ) EVT_KEY_UP( wxGrid::OnKeyUp ) EVT_CHAR ( wxGrid::OnChar ) EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground ) + EVT_COMMAND(wxID_ANY, wxEVT_GRID_HIDE_EDITOR, wxGrid::OnHideEditor ) END_EVENT_TABLE() bool wxGrid::Create(wxWindow *parent, wxWindowID id, @@ -2413,6 +2415,8 @@ wxGrid::SetTable(wxGridTableBase *table, m_created = true; } + InvalidateBestSize(); + return m_created; } @@ -2514,6 +2518,8 @@ void wxGrid::Init() // now anyhow, so just set the parameters directly m_xScrollPixelsPerLine = GRID_SCROLL_LINE_X; m_yScrollPixelsPerLine = GRID_SCROLL_LINE_Y; + + m_tabBehaviour = Tab_Stop; } // ---------------------------------------------------------------------------- @@ -2877,7 +2883,6 @@ bool wxGrid::Redimension( wxGridTableMessage& msg ) if ( !m_colAt.IsEmpty() ) { //Shift the column IDs - int i; for ( i = 0; i < m_numCols - numCols; i++ ) { if ( m_colAt[i] >= (int)pos ) @@ -2939,15 +2944,12 @@ bool wxGrid::Redimension( wxGridTableMessage& msg ) 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; @@ -2973,6 +2975,12 @@ bool wxGrid::Redimension( wxGridTableMessage& msg ) } } + // Notice that this must be called after updating m_colWidths above + // as the native grid control will check whether the new columns + // are shown which results in accessing m_colWidths array. + if ( m_useNativeHeader ) + GetGridColHeader()->SetColumnCount(m_numCols); + if ( m_currentCellCoords == wxGridNoCellCoords ) { // if we have just inserted cols into an empty grid the current @@ -3067,6 +3075,8 @@ bool wxGrid::Redimension( wxGridTableMessage& msg ) break; } + InvalidateBestSize(); + if (result && !GetBatchCount() ) m_gridWin->Refresh(); @@ -3531,9 +3541,8 @@ void wxGrid::DoUpdateResizeColWidth(int w) void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) { - int x, y; - wxPoint pos( event.GetPosition() ); - CalcUnscrolledPosition( pos.x, pos.y, &x, &y ); + int x; + CalcUnscrolledPosition( event.GetPosition().x, 0, &x, NULL ); int col = XToCol(x); if ( event.Dragging() ) @@ -3649,14 +3658,13 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) // else if ( event.LeftDown() ) { - int col = XToEdgeOfCol(x); - if ( col != wxNOT_FOUND && CanDragColSize(col) ) + int colEdge = XToEdgeOfCol(x); + if ( colEdge != wxNOT_FOUND && CanDragColSize(colEdge) ) { 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 ) ) { @@ -3717,7 +3725,8 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) // adjust column width depending on label text // // TODO: generate RESIZING event, see #10754 - AutoSizeColLabelSize( colEdge ); + if ( !SendGridSizeEvent(wxEVT_GRID_COL_AUTO_SIZE, -1, colEdge, event) ) + AutoSizeColLabelSize( colEdge ); SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, colEdge, event); @@ -4415,14 +4424,14 @@ bool wxGrid::DoEndDragResizeLine(const wxGridOperations& oper) oper.SelectSize(rect) = oper.Select(size); int subtractLines = 0; - const int lineStart = doper.PosToLine(this, posLineStart); - if ( lineStart >= 0 ) + int line = doper.PosToLine(this, posLineStart); + if ( line >= 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++ ) + for ( ; line < lineEnd; line++ ) { int cellLines = oper.Select( GetCellSize(oper.MakeCoords(m_dragRowOrCol, line))); @@ -4639,7 +4648,7 @@ wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend)(size_t), // event generation helpers // ---------------------------------------------------------------------------- -void +bool wxGrid::SendGridSizeEvent(wxEventType type, int row, int col, const wxMouseEvent& mouseEv) @@ -4654,7 +4663,7 @@ wxGrid::SendGridSizeEvent(wxEventType type, mouseEv.GetY() + GetColLabelSize(), mouseEv); - GetEventHandler()->ProcessEvent(gridEvt); + return GetEventHandler()->ProcessEvent(gridEvt); } // Generate a grid event based on a mouse event and return: @@ -4937,30 +4946,18 @@ void wxGrid::OnKeyDown( wxKeyEvent& event ) break; case WXK_TAB: - if (event.ShiftDown()) - { - if ( GetGridCursorCol() > 0 ) - { - MoveCursorLeft( false ); - } - else - { - // at left of grid - DisableCellEditControl(); - } - } - else { - if ( GetGridCursorCol() < GetNumberCols() - 1 ) + // send an event to the grid's parents for custom handling + wxGridEvent gridEvt(GetId(), wxEVT_GRID_TABBING, this, + GetGridCursorRow(), GetGridCursorCol(), + -1, -1, false, event); + if ( ProcessWindowEvent(gridEvt) ) { - MoveCursorRight( false ); - } - else - { - // at right of grid - DisableCellEditControl(); + // the event has been handled so no need for more processing + break; } } + DoGridProcessTab( event ); break; case WXK_HOME: @@ -5092,6 +5089,69 @@ void wxGrid::OnEraseBackground(wxEraseEvent&) { } +void wxGrid::DoGridProcessTab(wxKeyboardState& kbdState) +{ + const bool isForwardTab = !kbdState.ShiftDown(); + + // TAB processing only changes when we are at the borders of the grid, so + // let's first handle the common behaviour when we are not at the border. + if ( isForwardTab ) + { + if ( GetGridCursorCol() < GetNumberCols() - 1 ) + { + MoveCursorRight( false ); + return; + } + } + else // going back + { + if ( GetGridCursorCol() ) + { + MoveCursorLeft( false ); + return; + } + } + + + // We only get here if the cursor is at the border of the grid, apply the + // configured behaviour. + switch ( m_tabBehaviour ) + { + case Tab_Stop: + // Nothing special to do, we remain at the current cell. + break; + + case Tab_Wrap: + // Go to the beginning of the next or the end of the previous row. + if ( isForwardTab ) + { + if ( GetGridCursorRow() < GetNumberRows() - 1 ) + { + GoToCell( GetGridCursorRow() + 1, 0 ); + return; + } + } + else + { + if ( GetGridCursorRow() > 0 ) + { + GoToCell( GetGridCursorRow() - 1, GetNumberCols() - 1 ); + return; + } + } + break; + + case Tab_Leave: + if ( Navigate( isForwardTab ? wxNavigationKeyEvent::IsForward + : wxNavigationKeyEvent::IsBackward ) ) + return; + break; + } + + // If we remain in this cell, stop editing it if we were doing so. + DisableCellEditControl(); +} + bool wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) { if ( SendEvent(wxEVT_GRID_SELECT_CELL, coords) == -1 ) @@ -5508,7 +5568,7 @@ void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) // implicitly, causing this out-of order render. #if !defined(__WXMAC__) wxGridCellEditor *editor = attr->GetEditor(this, row, col); - editor->PaintBackground(rect, attr); + editor->PaintBackground(dc, rect, *attr); editor->DecRef(); #endif } @@ -6497,6 +6557,11 @@ void wxGrid::SaveEditControlValue() } } +void wxGrid::OnHideEditor(wxCommandEvent& WXUNUSED(event)) +{ + DisableCellEditControl(); +} + // // ------ Grid location functions // Note that all of these functions work with the logical coordinates of @@ -6609,7 +6674,7 @@ int wxGrid::XToPos(int x) const return PosToLinePos(x, true /* clip */, wxGridColumnOperations()); } -// return the row number such that the y coord is near the edge of, or -1 if +// return the row/col number such that the pos is near the edge of, or -1 if // not near an edge. // // notice that position can only possibly be near an edge if the row/column is @@ -6618,7 +6683,8 @@ int wxGrid::XToPos(int x) const // _never_ be considered to be near the edge). int wxGrid::PosToEdgeOfLine(int pos, const wxGridOperations& oper) const { - const int line = oper.PosToLine(this, pos, true); + // Get the bottom or rightmost line that could match. + int line = oper.PosToLine(this, pos, true); if ( oper.GetLineSize(this, line) > WXGRID_LABEL_EDGE_ZONE ) { @@ -6630,7 +6696,16 @@ int wxGrid::PosToEdgeOfLine(int pos, const wxGridOperations& oper) const pos - oper.GetLineStartPos(this, line) < WXGRID_LABEL_EDGE_ZONE ) { - return oper.GetLineBefore(this, line); + // We need to find the previous visible line, so skip all the + // hidden (of size 0) ones. + do + { + line = oper.GetLineBefore(this, line); + } + while ( line >= 0 && oper.GetLineSize(this, line) == 0 ); + + // It can possibly be -1 here. + return line; } } @@ -7067,6 +7142,7 @@ void wxGrid::SetRowLabelSize( int width ) } m_rowLabelWidth = width; + InvalidateBestSize(); CalcWindowSizes(); wxScrolledWindow::Refresh( true ); } @@ -7096,6 +7172,7 @@ void wxGrid::SetColLabelSize( int height ) } m_colLabelHeight = height; + InvalidateBestSize(); CalcWindowSizes(); wxScrolledWindow::Refresh( true ); } @@ -8050,8 +8127,11 @@ int UpdateRowOrColSize(int& sizeCurrent, int sizeNew) // We're showing back a previously hidden row/column. wxASSERT_MSG( sizeNew == -1, wxS("New size must be positive or -1.") ); - wxASSERT_MSG( sizeCurrent < 0, wxS("May only show back if hidden.") ); + // If it's already visible, simply do nothing. + if ( sizeCurrent >= 0 ) + return 0; + // Otherwise show it by restoring its old size. sizeCurrent = -sizeCurrent; // This is positive which is correct. @@ -8060,8 +8140,13 @@ int UpdateRowOrColSize(int& sizeCurrent, int sizeNew) else if ( sizeNew == 0 ) { // We're hiding a row/column. - wxASSERT_MSG( sizeCurrent > 0, wxS("Can't hide if already hidden.") ); + // If it's already hidden, simply do nothing. + if ( sizeCurrent <= 0 ) + return 0; + + // Otherwise hide it and also remember the shown size to be able to + // restore it later. sizeCurrent = -sizeCurrent; // This is negative which is correct. @@ -8087,7 +8172,8 @@ void wxGrid::SetRowSize( int row, int height ) return; // The value of -1 is special and means to fit the height to the row label. - if ( height == -1 ) + // As with the columns, ignore attempts to auto-size the hidden rows. + if ( height == -1 && GetRowHeight(row) != 0 ) { long w, h; wxArrayString lines; @@ -8121,6 +8207,8 @@ void wxGrid::DoSetRowSize( int row, int height ) m_rowBottoms[i] += diff; } + InvalidateBestSize(); + if ( !GetBatchCount() ) { CalcDimensions(); @@ -8157,7 +8245,12 @@ void wxGrid::SetColSize( int col, int width ) return; // The value of -1 is special and means to fit the width to the column label. - if ( width == -1 ) + // + // Notice that we currently don't support auto-sizing hidden columns (we + // could, but it's not clear whether this is really needed and it would + // make the code more complex), and for them passing -1 simply means to + // show the column back using its old size. + if ( width == -1 && GetColWidth(col) != 0 ) { long w, h; wxArrayString lines; @@ -8199,6 +8292,8 @@ void wxGrid::DoSetColSize( int col, int width ) m_colRights[GetColAt(colPos)] += diff; } + InvalidateBestSize(); + if ( !GetBatchCount() ) { CalcDimensions(); @@ -8275,6 +8370,19 @@ wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction) { const bool column = direction == wxGRID_COLUMN; + // We don't support auto-sizing hidden rows or columns, this doesn't seem + // to make much sense. + if ( column ) + { + if ( GetColWidth(colOrRow) == 0 ) + return; + } + else + { + if ( GetRowHeight(colOrRow) == 0 ) + return; + } + wxClientDC dc(m_gridWin); // cancel editing of cell @@ -8305,7 +8413,7 @@ wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction) row = rowOrCol; col = colOrRow; } - else + else { row = colOrRow; col = rowOrCol; @@ -8595,11 +8703,6 @@ wxSize wxGrid::DoGetBestSize() const wxSize size(self->SetOrCalcColumnSizes(true) - m_rowLabelWidth + m_extraWidth, self->SetOrCalcRowSizes(true) - m_colLabelHeight + m_extraHeight); - // NOTE: This size should be cached, but first we need to add calls to - // InvalidateBestSize everywhere that could change the results of this - // calculation. - // CacheBestSize(size); - return wxSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight) + GetWindowBorderSize(); }