]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/grid.cpp
Account for largest item in wxGenericListCtrl::GetBestSize().
[wxWidgets.git] / src / generic / grid.cpp
index 2ce9f06c7040de5dd2c04d857240d81632d8b84a..887ba87cf0526fbf5597d17e8eefc7a7e10c1df8 100644 (file)
@@ -80,12 +80,28 @@ WX_DECLARE_HASH_SET_WITH_DECL(int, wxIntegerHash, wxIntegerEqual,
 // globals
 // ----------------------------------------------------------------------------
 
+namespace
+{
+
 //#define DEBUG_ATTR_CACHE
 #ifdef DEBUG_ATTR_CACHE
     static size_t gs_nAttrCacheHits = 0;
     static size_t gs_nAttrCacheMisses = 0;
 #endif
 
+// this struct simply combines together the default header renderers
+//
+// as the renderers ctors are trivial, there is no problem with making them
+// globals
+struct DefaultHeaderRenderers
+{
+    wxGridColumnHeaderRendererDefault colRenderer;
+    wxGridRowHeaderRendererDefault rowRenderer;
+    wxGridCornerHeaderRendererDefault cornerRenderer;
+} gs_defaultHeaderRenderers;
+
+} // anonymous namespace
+
 // ----------------------------------------------------------------------------
 // constants
 // ----------------------------------------------------------------------------
@@ -191,6 +207,90 @@ wxGridCellWorker::~wxGridCellWorker()
 {
 }
 
+// ----------------------------------------------------------------------------
+// wxGridHeaderLabelsRenderer and related classes
+// ----------------------------------------------------------------------------
+
+void wxGridHeaderLabelsRenderer::DrawLabel(const wxGrid& grid,
+                                           wxDC& dc,
+                                           const wxString& value,
+                                           const wxRect& rect,
+                                           int horizAlign,
+                                           int vertAlign,
+                                           int textOrientation) const
+{
+    dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
+    dc.SetTextForeground(grid.GetLabelTextColour());
+    dc.SetFont(grid.GetLabelFont());
+    grid.DrawTextRectangle(dc, value, rect, horizAlign, vertAlign, textOrientation);
+}
+
+
+void wxGridRowHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid),
+                                                wxDC& dc,
+                                                wxRect& rect) const
+{
+    dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
+    dc.DrawLine(rect.GetRight(), rect.GetTop(),
+                rect.GetRight(), rect.GetBottom());
+    dc.DrawLine(rect.GetLeft(), rect.GetTop(),
+                rect.GetLeft(), rect.GetBottom());
+    dc.DrawLine(rect.GetLeft(), rect.GetBottom(),
+                rect.GetRight() + 1, rect.GetBottom());
+
+    dc.SetPen(*wxWHITE_PEN);
+    dc.DrawLine(rect.GetLeft() + 1, rect.GetTop(),
+                rect.GetLeft() + 1, rect.GetBottom());
+    dc.DrawLine(rect.GetLeft() + 1, rect.GetTop(),
+                rect.GetRight(), rect.GetTop());
+
+    rect.Deflate(2);
+}
+
+void wxGridColumnHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid),
+                                                   wxDC& dc,
+                                                   wxRect& rect) const
+{
+    dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
+    dc.DrawLine(rect.GetRight(), rect.GetTop(),
+                rect.GetRight(), rect.GetBottom());
+    dc.DrawLine(rect.GetLeft(), rect.GetTop(),
+                rect.GetRight(), rect.GetTop());
+    dc.DrawLine(rect.GetLeft(), rect.GetBottom(),
+                rect.GetRight() + 1, rect.GetBottom());
+
+    dc.SetPen(*wxWHITE_PEN);
+    dc.DrawLine(rect.GetLeft(), rect.GetTop() + 1,
+                rect.GetLeft(), rect.GetBottom());
+    dc.DrawLine(rect.GetLeft(), rect.GetTop() + 1,
+                rect.GetRight(), rect.GetTop() + 1);
+
+    rect.Deflate(2);
+}
+
+void wxGridCornerHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid),
+                                                   wxDC& dc,
+                                                   wxRect& rect) const
+{
+    dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
+    dc.DrawLine(rect.GetRight() - 1, rect.GetBottom() - 1,
+                rect.GetRight() - 1, rect.GetTop());
+    dc.DrawLine(rect.GetRight() - 1, rect.GetBottom() - 1,
+                rect.GetLeft(), rect.GetBottom() - 1);
+    dc.DrawLine(rect.GetLeft(), rect.GetTop(),
+                rect.GetRight(), rect.GetTop());
+    dc.DrawLine(rect.GetLeft(), rect.GetTop(),
+                rect.GetLeft(), rect.GetBottom());
+
+    dc.SetPen(*wxWHITE_PEN);
+    dc.DrawLine(rect.GetLeft() + 1, rect.GetTop() + 1,
+                rect.GetRight() - 1, rect.GetTop() + 1);
+    dc.DrawLine(rect.GetLeft() + 1, rect.GetTop() + 1,
+                rect.GetLeft() + 1, rect.GetBottom() - 1);
+
+    rect.Deflate(2);
+}
+
 // ----------------------------------------------------------------------------
 // wxGridCellAttr
 // ----------------------------------------------------------------------------
@@ -376,6 +476,15 @@ void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
     }
 }
 
+void wxGridCellAttr::GetNonDefaultAlignment(int *hAlign, int *vAlign) const
+{
+    if ( hAlign && m_hAlign != wxALIGN_INVALID )
+        *hAlign = m_hAlign;
+
+    if ( vAlign && m_vAlign != wxALIGN_INVALID )
+        *vAlign = m_vAlign;
+}
+
 void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const
 {
     if ( num_rows )
@@ -859,6 +968,23 @@ void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
     }
 }
 
+const wxGridColumnHeaderRenderer&
+wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col))
+{
+    return gs_defaultHeaderRenderers.colRenderer;
+}
+
+const wxGridRowHeaderRenderer&
+wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row))
+{
+    return gs_defaultHeaderRenderers.rowRenderer;
+}
+
+const wxGridCornerHeaderRenderer& wxGridCellAttrProvider::GetCornerRenderer()
+{
+    return gs_defaultHeaderRenderers.cornerRenderer;
+}
+
 // ----------------------------------------------------------------------------
 // wxGridTableBase
 // ----------------------------------------------------------------------------
@@ -1974,8 +2100,7 @@ wxGrid::SetTable(wxGridTableBase *table,
             m_table = NULL;
         }
 
-        delete m_selection;
-        m_selection = NULL;
+        wxDELETE(m_selection);
 
         m_ownTable = false;
         m_numRows = 0;
@@ -3343,7 +3468,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;
 
@@ -3456,7 +3608,7 @@ void wxGrid::ChangeCursorMode(CursorMode mode,
                               bool captureMouse)
 {
 #if wxUSE_LOG_TRACE
-    static const wxChar *cursorModes[] =
+    static const wxChar *const cursorModes[] =
     {
         wxT("SELECT_CELL"),
         wxT("RESIZE_ROW"),
@@ -3639,6 +3791,8 @@ void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords
 
     if ( isFirstDrag )
     {
+        wxASSERT_MSG( !m_winCapture, "shouldn't capture the mouse twice" );
+
         m_winCapture = m_gridWin;
         m_winCapture->CaptureMouse();
     }
@@ -3684,6 +3838,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 );
@@ -3818,6 +3995,14 @@ wxGrid::DoGridMouseMoveEvent(wxMouseEvent& WXUNUSED(event),
 
 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
@@ -3843,17 +4028,6 @@ void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event)
     m_isDragging = false;
     m_startDragPos = wxDefaultPosition;
 
-    // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
-    //     immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
-    //     wxGTK
-#if 0
-    if ( event.Entering() || event.Leaving() )
-    {
-        ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
-        m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
-    }
-#endif // 0
-
     // deal with various button presses
     if ( event.IsButton() )
     {
@@ -5324,32 +5498,25 @@ void wxGrid::DrawRowLabel( wxDC& dc, int row )
     if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
         return;
 
-    wxRect rect;
-
-    int rowTop = GetRowTop(row),
-        rowBottom = GetRowBottom(row) - 1;
-
-    dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
-    dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom );
-    dc.DrawLine( 0, rowTop, 0, rowBottom );
-    dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
+    wxGridCellAttrProvider * const
+        attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
 
-    dc.SetPen( *wxWHITE_PEN );
-    dc.DrawLine( 1, rowTop, 1, rowBottom );
-    dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop );
+    // notice that an explicit static_cast is needed to avoid a compilation
+    // error with VC7.1 which, for some reason, tries to instantiate (abstract)
+    // wxGridRowHeaderRenderer class without it
+    const wxGridRowHeaderRenderer&
+        rend = attrProvider ? attrProvider->GetRowHeaderRenderer(row)
+                            : static_cast<const wxGridRowHeaderRenderer&>
+                                (gs_defaultHeaderRenderers.rowRenderer);
 
-    dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT );
-    dc.SetTextForeground( GetLabelTextColour() );
-    dc.SetFont( GetLabelFont() );
+    wxRect rect(0, GetRowTop(row), m_rowLabelWidth, GetRowHeight(row));
+    rend.DrawBorder(*this, dc, rect);
 
     int hAlign, vAlign;
-    GetRowLabelAlignment( &hAlign, &vAlign );
+    GetRowLabelAlignment(&hAlign, &vAlign);
 
-    rect.SetX( 2 );
-    rect.SetY( GetRowTop(row) + 2 );
-    rect.SetWidth( m_rowLabelWidth - 4 );
-    rect.SetHeight( GetRowHeight(row) - 4 );
-    DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
+    rend.DrawLabel(*this, dc, GetRowLabelValue(row),
+                   rect, hAlign, vAlign, wxHORIZONTAL);
 }
 
 void wxGrid::UseNativeColHeader(bool native)
@@ -5397,26 +5564,27 @@ void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
 
 void wxGrid::DrawCornerLabel(wxDC& dc)
 {
+    wxRect rect(wxSize(m_rowLabelWidth, m_colLabelHeight));
+
     if ( m_nativeColumnLabels )
     {
-        wxRect rect(wxSize(m_rowLabelWidth, m_colLabelHeight));
         rect.Deflate(1);
 
         wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin, dc, rect, 0);
     }
     else
     {
-        dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
-        dc.DrawLine( m_rowLabelWidth - 1, m_colLabelHeight - 1,
-                     m_rowLabelWidth - 1, 0 );
-        dc.DrawLine( m_rowLabelWidth - 1, m_colLabelHeight - 1,
-                     0, m_colLabelHeight - 1 );
-        dc.DrawLine( 0, 0, m_rowLabelWidth, 0 );
-        dc.DrawLine( 0, 0, 0, m_colLabelHeight );
+        rect.width++;
+        rect.height++;
 
-        dc.SetPen( *wxWHITE_PEN );
-        dc.DrawLine( 1, 1, m_rowLabelWidth - 1, 1 );
-        dc.DrawLine( 1, 1, 1, m_colLabelHeight - 1 );
+        wxGridCellAttrProvider * const
+            attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
+        const wxGridCornerHeaderRenderer&
+            rend = attrProvider ? attrProvider->GetCornerRenderer()
+                                : static_cast<wxGridCornerHeaderRenderer&>
+                                    (gs_defaultHeaderRenderers.cornerRenderer);
+
+        rend.DrawBorder(*this, dc, rect);
     }
 }
 
@@ -5428,6 +5596,12 @@ void wxGrid::DrawColLabel(wxDC& dc, int col)
     int colLeft = GetColLeft(col);
 
     wxRect rect(colLeft, 0, GetColWidth(col), m_colLabelHeight);
+    wxGridCellAttrProvider * const
+        attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
+    const wxGridColumnHeaderRenderer&
+        rend = attrProvider ? attrProvider->GetColumnHeaderRenderer(col)
+                            : static_cast<wxGridColumnHeaderRenderer&>
+                                (gs_defaultHeaderRenderers.colRenderer);
 
     if ( m_nativeColumnLabels )
     {
@@ -5443,34 +5617,23 @@ void wxGrid::DrawColLabel(wxDC& dc, int col)
                                             : wxHDR_SORT_ICON_DOWN
                                         : wxHDR_SORT_ICON_NONE
                                 );
+        rect.Deflate(2);
     }
     else
     {
-        int colRight = GetColRight(col) - 1;
-
-        dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
-        dc.DrawLine( colRight, 0,
-                     colRight, m_colLabelHeight - 1 );
-        dc.DrawLine( colLeft, 0,
-                     colRight, 0 );
-        dc.DrawLine( colLeft, m_colLabelHeight - 1,
-                     colRight + 1, m_colLabelHeight - 1 );
+        // It is reported that we need to erase the background to avoid display
+        // artefacts, see #12055.
+        wxDCBrushChanger setBrush(dc, m_colWindow->GetBackgroundColour());
+        dc.DrawRectangle(rect);
 
-        dc.SetPen( *wxWHITE_PEN );
-        dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight - 1 );
-        dc.DrawLine( colLeft, 1, colRight, 1 );
+        rend.DrawBorder(*this, dc, rect);
     }
 
-    dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT );
-    dc.SetTextForeground( GetLabelTextColour() );
-    dc.SetFont( GetLabelFont() );
-
     int hAlign, vAlign;
-    GetColLabelAlignment( &hAlign, &vAlign );
+    GetColLabelAlignment(&hAlign, &vAlign);
     const int orient = GetColLabelTextOrientation();
 
-    rect.Deflate(2);
-    DrawTextRectangle(dc, GetColLabelValue(col), rect, hAlign, vAlign, orient);
+    rend.DrawLabel(*this, dc, GetColLabelValue(col), rect, hAlign, vAlign, orient);
 }
 
 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
@@ -5480,7 +5643,7 @@ void wxGrid::DrawTextRectangle( wxDC& dc,
                                 const wxRect& rect,
                                 int horizAlign,
                                 int vertAlign,
-                                int textOrientation )
+                                int textOrientation ) const
 {
     wxArrayString lines;
 
@@ -5494,7 +5657,7 @@ void wxGrid::DrawTextRectangle(wxDC& dc,
                                const wxRect& rect,
                                int horizAlign,
                                int vertAlign,
-                               int textOrientation)
+                               int textOrientation) const
 {
     if ( lines.empty() )
         return;
@@ -7029,11 +7192,21 @@ bool wxGrid::GetCellOverflow( int row, int col ) const
     return allow;
 }
 
-void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
+wxGrid::CellSpan
+wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
 {
     wxGridCellAttr *attr = GetCellAttr(row, col);
     attr->GetSize( num_rows, num_cols );
     attr->DecRef();
+
+    if ( *num_rows == 1 && *num_cols == 1 )
+        return CellSpan_None; // just a normal cell
+
+    if ( *num_rows < 0 || *num_cols < 0 )
+        return CellSpan_Inside; // covered by a multi-span cell
+
+    // this cell spans multiple cells to its right/bottom
+    return CellSpan_Main;
 }
 
 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const
@@ -7692,22 +7865,40 @@ wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction)
     HideCellEditControl();
     SaveEditControlValue();
 
-    // init both of them to avoid compiler warnings, even if we only need one
+    // initialize both of them just to avoid compiler warnings
     int row = -1,
         col = -1;
-    if ( column )
-        col = colOrRow;
-    else
-        row = colOrRow;
 
     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
+        // this column/row
+        int numRows, numCols;
+        const CellSpan span = GetCellSize(row, col, &numRows, &numCols);
+        if ( span == CellSpan_Inside )
+        {
+            // we need to get the size of the main cell, not of a cell hidden
+            // by it
+            row += numRows;
+            col += numCols;
+
+            // get the size of the main cell too
+            GetCellSize(row, col, &numRows, &numCols);
+        }
 
         wxGridCellAttr *attr = GetCellAttr(row, col);
         wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
@@ -7715,6 +7906,19 @@ wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction)
         {
             wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
             extent = column ? size.x : size.y;
+
+            if ( span != CellSpan_None )
+            {
+                // we spread the size of a spanning cell over all the cells it
+                // covers evenly -- this is probably not ideal but we can't
+                // really do much better here
+                //
+                // notice that numCols and numRows are never 0 as they
+                // correspond to the size of the main cell of the span and not
+                // of the cell inside it
+                extent /= column ? numCols : numRows;
+            }
+
             if ( extent > extentMax )
                 extentMax = extent;