From abc8841f0bbd77ea8a74d7c5bf02bbe2466f30c6 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 29 Nov 2019 04:57:59 +0100 Subject: [PATCH] Use default wxCheckBox size in wxGridCellBoolEditor Using wxRendererNative::GetCheckBoxSize() as the size of wxCheckBox just doesn't work with GTK 3, as the control has additional padding between its actual contents and the focus rectangle, which means that its actual size must be greater than the size to be passed to DrawCheckBox() in order to draw a checkbox of the same size. However it isn't really necessary to resize wxCheckBox at all, it's enough for DrawCheckBox() to produce a check mark of the same size as that shown in a default-sized wxCheckBox and this does work in wxGTK. So keep the default size of wxCheckBox to make everything work. This means wxGetGridCheckBoxRect() is not useful any more, so replace it with wxGetContentRect() which just positions a rectangle of the given size inside another one (this should probably be moved somewhere else, as it's more general than wxGrid). --- include/wx/generic/private/grid.h | 12 +++++----- src/generic/grid.cpp | 38 +++++++++++++++---------------- src/generic/gridctrl.cpp | 10 ++++---- src/generic/grideditors.cpp | 13 ++++------- 4 files changed, 32 insertions(+), 41 deletions(-) diff --git a/include/wx/generic/private/grid.h b/include/wx/generic/private/grid.h index 35225b6a52..172738df55 100644 --- a/include/wx/generic/private/grid.h +++ b/include/wx/generic/private/grid.h @@ -1007,16 +1007,16 @@ private: wxGridDataTypeInfoArray m_typeinfo; }; -// Returns the rectangle for showing a check box in a cell with the given -// alignment. +// Returns the rectangle for showing something of the given size in a cell with +// the given alignment. // // The function is used by wxGridCellBoolEditor and wxGridCellBoolRenderer to // draw a check mark and position wxCheckBox respectively. wxRect -wxGetGridCheckBoxRect(wxWindow* win, - const wxRect& cellRect, - int hAlign, - int vAlign); +wxGetContentRect(wxSize contentSize, + const wxRect& cellRect, + int hAlign, + int vAlign); #endif // wxUSE_GRID #endif // _WX_GENERIC_GRID_PRIVATE_H_ diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index bfbaf2b352..d4983e04e0 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -10332,57 +10332,55 @@ wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) return editor; } -wxRect wxGetGridCheckBoxRect(wxWindow* win, - const wxRect& cellRect, - int hAlign, - int vAlign) +wxRect +wxGetContentRect(wxSize contentSize, + const wxRect& cellRect, + int hAlign, + int vAlign) { - wxSize checkBoxSize = - wxRendererNative::Get().GetCheckBoxSize(win, wxCONTROL_CELL); - // Keep square aspect ratio for the checkbox, but ensure that it fits into // the available space, even if it's smaller than the standard size. const wxCoord minSize = wxMin(cellRect.width, cellRect.height); - if ( checkBoxSize.x >= minSize || checkBoxSize.y >= minSize ) + if ( contentSize.x >= minSize || contentSize.y >= minSize ) { // It must still have positive size, however. const int fittingSize = wxMax(1, minSize - 2*GRID_CELL_CHECKBOX_MARGIN); - checkBoxSize.x = - checkBoxSize.y = fittingSize; + contentSize.x = + contentSize.y = fittingSize; } - wxRect checkBoxRect(checkBoxSize); + wxRect contentRect(contentSize); if ( hAlign & wxALIGN_CENTER_HORIZONTAL ) { - checkBoxRect = checkBoxRect.CentreIn(cellRect, wxHORIZONTAL); + contentRect = contentRect.CentreIn(cellRect, wxHORIZONTAL); } else if ( hAlign & wxALIGN_RIGHT ) { - checkBoxRect.SetX(cellRect.x + cellRect.width - - checkBoxSize.x - GRID_CELL_CHECKBOX_MARGIN); + contentRect.SetX(cellRect.x + cellRect.width + - contentSize.x - GRID_CELL_CHECKBOX_MARGIN); } else // ( hAlign == wxALIGN_LEFT ) and invalid alignment value { - checkBoxRect.SetX(cellRect.x + GRID_CELL_CHECKBOX_MARGIN); + contentRect.SetX(cellRect.x + GRID_CELL_CHECKBOX_MARGIN); } if ( vAlign & wxALIGN_CENTER_VERTICAL ) { - checkBoxRect = checkBoxRect.CentreIn(cellRect, wxVERTICAL); + contentRect = contentRect.CentreIn(cellRect, wxVERTICAL); } else if ( vAlign & wxALIGN_BOTTOM ) { - checkBoxRect.SetY(cellRect.y + cellRect.height - - checkBoxRect.y - GRID_CELL_CHECKBOX_MARGIN); + contentRect.SetY(cellRect.y + cellRect.height + - contentRect.y - GRID_CELL_CHECKBOX_MARGIN); } else // wxALIGN_TOP { - checkBoxRect.SetY(cellRect.y + GRID_CELL_CHECKBOX_MARGIN); + contentRect.SetY(cellRect.y + GRID_CELL_CHECKBOX_MARGIN); } - return checkBoxRect; + return contentRect; } #endif // wxUSE_GRID diff --git a/src/generic/gridctrl.cpp b/src/generic/gridctrl.cpp index 29e3056da5..585a8e6341 100644 --- a/src/generic/gridctrl.cpp +++ b/src/generic/gridctrl.cpp @@ -937,11 +937,8 @@ wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid, // compute it only once (no locks for MT safeness in GUI thread...) if ( !ms_sizeCheckMark.x ) { - // Use rectangle big enough for the check box to fit into it. - const wxRect r(0, 0, 1000, 1000); - ms_sizeCheckMark = - wxGetGridCheckBoxRect(&grid, r, wxALIGN_LEFT, wxALIGN_TOP).GetSize(); + wxRendererNative::Get().GetCheckBoxSize(&grid, wxCONTROL_CELL); } return ms_sizeCheckMark; @@ -960,8 +957,9 @@ void wxGridCellBoolRenderer::Draw(wxGrid& grid, int vAlign = wxALIGN_CENTRE_VERTICAL; attr.GetNonDefaultAlignment(&hAlign, &vAlign); - const wxRect - checkBoxRect = wxGetGridCheckBoxRect(&grid, rect, hAlign, vAlign); + const wxRect checkBoxRect = + wxGetContentRect(GetBestSize(grid, attr, dc, row, col), + rect, hAlign, vAlign); bool value; if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) diff --git a/src/generic/grideditors.cpp b/src/generic/grideditors.cpp index 08abef4efa..3fb11821ea 100644 --- a/src/generic/grideditors.cpp +++ b/src/generic/grideditors.cpp @@ -1223,16 +1223,11 @@ void wxGridCellBoolEditor::SetSize(const wxRect& r) if (GetCellAttr()) GetCellAttr()->GetNonDefaultAlignment(&hAlign, &vAlign); - const wxRect - checkBoxRect = wxGetGridCheckBoxRect(GetWindow(), r, hAlign, vAlign); + const wxRect checkBoxRect = + wxGetContentRect(m_control->GetSize(), r, hAlign, vAlign); - // resize the control if required - if ( m_control->GetSize() != checkBoxRect.GetSize() ) - { - m_control->SetSize(checkBoxRect.GetSize()); - } - - // and move it + // Don't resize the checkbox, it should have its default (and fitting) + // size, but do move it to the right position. m_control->Move(checkBoxRect.GetPosition()); }