X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b5e97a17b1afb7cea4c03def51e830b5cd6d92d6..38274d1de33ff44ec96dbb2c7d6942cb85882a82:/src/generic/grid.cpp?ds=inline diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 7ca5c3cdd9..494b74f2cc 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -72,8 +72,8 @@ const char wxGridNameStr[] = "grid"; // Required for wxIs... functions #include -WX_DECLARE_HASH_SET_WITH_DECL(int, wxIntegerHash, wxIntegerEqual, - wxGridFixedIndicesSet, class WXDLLIMPEXP_ADV); +WX_DECLARE_HASH_SET_WITH_DECL_PTR(int, ::wxIntegerHash, ::wxIntegerEqual, + wxGridFixedIndicesSet, class WXDLLIMPEXP_ADV); // ---------------------------------------------------------------------------- @@ -156,6 +156,23 @@ wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED, wxGridEditorCreatedEvent ); +// ---------------------------------------------------------------------------- +// private helpers +// ---------------------------------------------------------------------------- + +namespace +{ + + // ensure that first is less or equal to second, swapping the values if + // necessary + void EnsureFirstLessThanSecond(int& first, int& second) + { + if ( first > second ) + wxSwap(first, second); + } + +} // anonymous namespace + // ============================================================================ // implementation // ============================================================================ @@ -170,6 +187,8 @@ END_EVENT_TABLE() BEGIN_EVENT_TABLE(wxGridHeaderCtrl, wxHeaderCtrl) EVT_HEADER_CLICK(wxID_ANY, wxGridHeaderCtrl::OnClick) + EVT_HEADER_DCLICK(wxID_ANY, wxGridHeaderCtrl::OnDoubleClick) + EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxGridHeaderCtrl::OnRightClick) EVT_HEADER_BEGIN_RESIZE(wxID_ANY, wxGridHeaderCtrl::OnBeginResize) EVT_HEADER_RESIZING(wxID_ANY, wxGridHeaderCtrl::OnResizing) @@ -400,7 +419,7 @@ void wxGridCellAttr::SetSize(int num_rows, int num_cols) wxASSERT_MSG( (!((num_rows > 0) && (num_cols <= 0)) || !((num_rows <= 0) && (num_cols > 0)) || !((num_rows == 0) && (num_cols == 0))), - wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values")); + wxT("wxGridCellAttr::SetSize only takes two positive values or negative/zero values")); m_sizeRows = num_rows; m_sizeCols = num_cols; @@ -1515,9 +1534,9 @@ bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) // m_colLabels stores just as many elements as it needs, e.g. if only // the label of the first column had been set it would have only one // element and not numCols, so account for it - int nToRm = m_colLabels.size() - colID; - if ( nToRm > 0 ) - m_colLabels.RemoveAt( colID, nToRm ); + int numRemaining = m_colLabels.size() - colID; + if (numRemaining > 0) + m_colLabels.RemoveAt( colID, wxMin(numCols, numRemaining) ); } if ( numCols >= curNumCols ) @@ -1837,58 +1856,6 @@ void wxGridWindow::OnFocus(wxFocusEvent& event) ///////////////////////////////////////////////////////////////////// -#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 ) @@ -1911,7 +1878,6 @@ bool wxGrid::Create(wxWindow *parent, wxWindowID id, Create(); SetInitialSize(size); - SetScrollRate(m_scrollLineX, m_scrollLineY); CalcDimensions(); return true; @@ -2100,8 +2066,7 @@ wxGrid::SetTable(wxGridTableBase *table, m_table = NULL; } - delete m_selection; - m_selection = NULL; + wxDELETE(m_selection); m_ownTable = false; m_numRows = 0; @@ -2250,8 +2215,11 @@ void wxGrid::Init() m_extraWidth = m_extraHeight = 0; - m_scrollLineX = GRID_SCROLL_LINE_X; - m_scrollLineY = GRID_SCROLL_LINE_Y; + // 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; } // ---------------------------------------------------------------------------- @@ -3101,7 +3069,7 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow()); m_dragLastPos = -1; } - else // not on row separator or it's not resizeable + else // not on row separator or it's not resizable { row = YToRow(y); if ( row >=0 && @@ -3469,7 +3437,34 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) } else { - DoEndMoveCol(XToPos(x)); + // 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; @@ -3653,13 +3648,15 @@ void wxGrid::ChangeCursorMode(CursorMode mode, // grid mouse event processing // ---------------------------------------------------------------------------- -void +bool wxGrid::DoGridCellDrag(wxMouseEvent& event, const wxGridCellCoords& coords, bool isFirstDrag) { + bool performDefault = true ; + if ( coords == wxGridNoCellCoords ) - return; // we're outside any valid cell + return performDefault; // we're outside any valid cell // Hide the edit control, so it won't interfere with drag-shrinking. if ( IsCellEditControlShown() ) @@ -3670,7 +3667,7 @@ wxGrid::DoGridCellDrag(wxMouseEvent& event, switch ( event.GetModifiers() ) { - case wxMOD_CMD: + case wxMOD_CONTROL: if ( m_selectedBlockCorner == wxGridNoCellCoords) m_selectedBlockCorner = coords; UpdateBlockBeingSelected(m_selectedBlockCorner, coords); @@ -3684,8 +3681,11 @@ wxGrid::DoGridCellDrag(wxMouseEvent& event, if ( m_selectedBlockCorner == wxGridNoCellCoords) m_selectedBlockCorner = coords; - SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG, coords, event); - return; + // if event is handled by user code, no further processing + if ( SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG, coords, event) != 0 ) + performDefault = false; + + return performDefault; } } @@ -3696,6 +3696,8 @@ wxGrid::DoGridCellDrag(wxMouseEvent& event, // we don't handle the other key modifiers event.Skip(); } + + return performDefault; } void wxGrid::DoGridLineDrag(wxMouseEvent& event, const wxGridOperations& oper) @@ -3748,7 +3750,9 @@ void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords switch ( m_cursorMode ) { case WXGRID_CURSOR_SELECT_CELL: - DoGridCellDrag(event, coords, isFirstDrag); + // no further handling if handled by user + if ( DoGridCellDrag(event, coords, isFirstDrag) == false ) + return; break; case WXGRID_CURSOR_RESIZE_ROW: @@ -3812,6 +3816,29 @@ wxGrid::DoGridCellLeftDown(wxMouseEvent& event, } 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 ); @@ -4079,13 +4106,13 @@ bool wxGrid::DoEndDragResizeLine(const wxGridOperations& oper) oper.SelectSize(rect) = oper.Select(size); int subtractLines = 0; - const int lineStart = oper.PosToLine(this, posLineStart); + 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 = oper.PosToLine(this, posLineEnd, true); + const int lineEnd = doper.PosToLine(this, posLineEnd, true); for ( int line = lineStart; line < lineEnd; line++ ) { int cellLines = oper.Select( @@ -4379,6 +4406,14 @@ wxGrid::SendEvent(const wxEventType type, mouseEv.GetY() + GetColLabelSize(), false, mouseEv); + + if ( type == wxEVT_GRID_CELL_BEGIN_DRAG ) + { + // by default the dragging is not supported, the user code must + // explicitly allow the event for it to take place + gridEvt.Veto(); + } + claimed = GetEventHandler()->ProcessEvent(gridEvt); vetoed = !gridEvt.IsAllowed(); } @@ -4620,25 +4655,15 @@ void wxGrid::OnKeyDown( wxKeyEvent& event ) break; case WXK_HOME: - if ( event.ControlDown() ) - { - GoToCell(0, 0); - } - else - { - event.Skip(); - } + GoToCell(event.ControlDown() ? 0 + : m_currentCellCoords.GetRow(), + 0); break; case WXK_END: - if ( event.ControlDown() ) - { - GoToCell(m_numRows - 1, m_numCols - 1); - } - else - { - event.Skip(); - } + GoToCell(event.ControlDown() ? m_numRows - 1 + : m_currentCellCoords.GetRow(), + m_numCols - 1); break; case WXK_PAGEUP: @@ -4816,6 +4841,9 @@ void wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol, int bottomRow, int rightCol) { + MakeCellVisible(m_selectedBlockCorner); + m_selectedBlockCorner = wxGridCellCoords(bottomRow, rightCol); + if ( m_selection ) { switch ( m_selection->GetSelectionMode() ) @@ -4852,9 +4880,6 @@ wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol, } } - m_selectedBlockCorner = wxGridCellCoords(bottomRow, rightCol); - MakeCellVisible(m_selectedBlockCorner); - EnsureFirstLessThanSecond(topRow, bottomRow); EnsureFirstLessThanSecond(leftCol, rightCol); @@ -5849,8 +5874,8 @@ void wxGrid::EnableCellEditControl( bool enable ) bool wxGrid::IsCurrentCellReadOnly() const { - // const_cast - wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords); + wxGridCellAttr* + attr = const_cast(this)->GetCellAttr(m_currentCellCoords); bool readonly = attr->IsReadOnly(); attr->DecRef(); @@ -6238,7 +6263,9 @@ int wxGrid::PosToEdgeOfLine(int pos, const wxGridOperations& oper) const else if ( line > 0 && pos - oper.GetLineStartPos(this, line) < WXGRID_LABEL_EDGE_ZONE ) - return line - 1; + { + return oper.GetLineBefore(this, line); + } } return -1; @@ -6366,7 +6393,7 @@ void wxGrid::MakeCellVisible( int row, int col ) // // Sometimes GRID_SCROLL_LINE / 2 is not enough, // so just add a full scroll unit... - ypos += m_scrollLineY; + ypos += m_yScrollPixelsPerLine; } // special handling for wide cells - show always left part of the cell! @@ -6385,15 +6412,15 @@ void wxGrid::MakeCellVisible( int row, int col ) xpos = x0 + (right - cw); // see comment for ypos above - xpos += m_scrollLineX; + xpos += m_xScrollPixelsPerLine; } if ( xpos != -1 || ypos != -1 ) { if ( xpos != -1 ) - xpos /= m_scrollLineX; + xpos /= m_xScrollPixelsPerLine; if ( ypos != -1 ) - ypos /= m_scrollLineY; + ypos /= m_yScrollPixelsPerLine; Scroll( xpos, ypos ); AdjustScrollbars(); } @@ -6908,7 +6935,7 @@ void wxGrid::SetCellHighlightPenWidth(int width) m_cellHighlightPenWidth = width; // Just redrawing the cell highlight is not enough since that won't - // make any visible change if the the thickness is getting smaller. + // make any visible change if the thickness is getting smaller. int row = m_currentCellCoords.GetRow(); int col = m_currentCellCoords.GetCol(); if ( row == -1 || col == -1 || GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 ) @@ -6926,7 +6953,7 @@ void wxGrid::SetCellHighlightROPenWidth(int width) m_cellHighlightROPenWidth = width; // Just redrawing the cell highlight is not enough since that won't - // make any visible change if the the thickness is getting smaller. + // make any visible change if the thickness is getting smaller. int row = m_currentCellCoords.GetRow(); int col = m_currentCellCoords.GetCol(); if ( row == -1 || col == -1 || @@ -7216,11 +7243,18 @@ void wxGrid::ClearAttrCache() } } +void wxGrid::RefreshAttr(int row, int col) +{ + if ( m_attrCache.row == row && m_attrCache.col == col ) + ClearAttrCache(); +} + + void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const { if ( attr != NULL ) { - wxGrid *self = (wxGrid *)this; // const_cast + wxGrid * const self = const_cast(this); self->ClearAttrCache(); self->m_attrCache.row = row; @@ -7648,7 +7682,7 @@ void wxGrid::SetRowSize( int row, int height ) } // See comment in SetColSize - if ( height < GetRowMinimalAcceptableHeight()) + if ( height > 0 && height < GetRowMinimalAcceptableHeight()) return; if ( m_rowHeights.IsEmpty() ) @@ -7667,7 +7701,10 @@ void wxGrid::SetRowSize( int row, int height ) } if ( !GetBatchCount() ) + { CalcDimensions(); + Refresh(); + } } void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols ) @@ -7816,24 +7853,29 @@ wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction) HideCellEditControl(); SaveEditControlValue(); - // initialize both of them just to avoid compiler warnings - int row = -1, + // initialize both of them just to avoid compiler warnings even if only + // really needs to be initialized here + int row, + col; + if ( column ) + { + row = -1; + col = colOrRow; + } + else + { + row = colOrRow; col = -1; + } wxCoord extent, extentMax = 0; int max = column ? m_numRows : m_numCols; for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ ) { if ( column ) - { row = rowOrCol; - col = colOrRow; - } else - { - row = colOrRow; col = rowOrCol; - } // we need to account for the cells spanning multiple columns/rows: // while they may need a lot of space, they don't need all of it in @@ -8071,10 +8113,8 @@ void wxGrid::AutoSize() // we know that we're not going to have scrollbars so disable them now to // avoid trouble in SetClientSize() which can otherwise set the correct // client size but also leave space for (not needed any more) scrollbars - SetScrollbars(0, 0, 0, 0, 0, 0, true); - - // restore the scroll rate parameters overwritten by SetScrollbars() - SetScrollRate(m_scrollLineX, m_scrollLineY); + SetScrollbars(m_xScrollPixelsPerLine, m_yScrollPixelsPerLine, + 0, 0, 0, 0, true); SetClientSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight); } @@ -8111,7 +8151,7 @@ void wxGrid::AutoSizeColLabelSize( int col ) wxSize wxGrid::DoGetBestSize() const { - wxGrid *self = (wxGrid *)this; // const_cast + wxGrid * const self = const_cast(this); // we do the same as in AutoSize() here with the exception that we don't // change the column/row sizes, only calculate them @@ -8132,10 +8172,12 @@ void wxGrid::Fit() AutoSize(); } +#if WXWIN_COMPATIBILITY_2_8 wxPen& wxGrid::GetDividerPen() const { return wxNullPen; } +#endif // WXWIN_COMPATIBILITY_2_8 // ---------------------------------------------------------------------------- // cell value accessor functions