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).
This commit is contained in:
Vadim Zeitlin
2019-11-29 04:57:59 +01:00
parent eadee05729
commit abc8841f0b
4 changed files with 32 additions and 41 deletions

View File

@@ -1007,16 +1007,16 @@ private:
wxGridDataTypeInfoArray m_typeinfo; wxGridDataTypeInfoArray m_typeinfo;
}; };
// Returns the rectangle for showing a check box in a cell with the given // Returns the rectangle for showing something of the given size in a cell with
// alignment. // the given alignment.
// //
// The function is used by wxGridCellBoolEditor and wxGridCellBoolRenderer to // The function is used by wxGridCellBoolEditor and wxGridCellBoolRenderer to
// draw a check mark and position wxCheckBox respectively. // draw a check mark and position wxCheckBox respectively.
wxRect wxRect
wxGetGridCheckBoxRect(wxWindow* win, wxGetContentRect(wxSize contentSize,
const wxRect& cellRect, const wxRect& cellRect,
int hAlign, int hAlign,
int vAlign); int vAlign);
#endif // wxUSE_GRID #endif // wxUSE_GRID
#endif // _WX_GENERIC_GRID_PRIVATE_H_ #endif // _WX_GENERIC_GRID_PRIVATE_H_

View File

@@ -10332,57 +10332,55 @@ wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
return editor; return editor;
} }
wxRect wxGetGridCheckBoxRect(wxWindow* win, wxRect
const wxRect& cellRect, wxGetContentRect(wxSize contentSize,
int hAlign, const wxRect& cellRect,
int vAlign) 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 // 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. // the available space, even if it's smaller than the standard size.
const wxCoord minSize = wxMin(cellRect.width, cellRect.height); 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. // It must still have positive size, however.
const int fittingSize = wxMax(1, minSize - 2*GRID_CELL_CHECKBOX_MARGIN); const int fittingSize = wxMax(1, minSize - 2*GRID_CELL_CHECKBOX_MARGIN);
checkBoxSize.x = contentSize.x =
checkBoxSize.y = fittingSize; contentSize.y = fittingSize;
} }
wxRect checkBoxRect(checkBoxSize); wxRect contentRect(contentSize);
if ( hAlign & wxALIGN_CENTER_HORIZONTAL ) if ( hAlign & wxALIGN_CENTER_HORIZONTAL )
{ {
checkBoxRect = checkBoxRect.CentreIn(cellRect, wxHORIZONTAL); contentRect = contentRect.CentreIn(cellRect, wxHORIZONTAL);
} }
else if ( hAlign & wxALIGN_RIGHT ) else if ( hAlign & wxALIGN_RIGHT )
{ {
checkBoxRect.SetX(cellRect.x + cellRect.width contentRect.SetX(cellRect.x + cellRect.width
- checkBoxSize.x - GRID_CELL_CHECKBOX_MARGIN); - contentSize.x - GRID_CELL_CHECKBOX_MARGIN);
} }
else // ( hAlign == wxALIGN_LEFT ) and invalid alignment value 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 ) if ( vAlign & wxALIGN_CENTER_VERTICAL )
{ {
checkBoxRect = checkBoxRect.CentreIn(cellRect, wxVERTICAL); contentRect = contentRect.CentreIn(cellRect, wxVERTICAL);
} }
else if ( vAlign & wxALIGN_BOTTOM ) else if ( vAlign & wxALIGN_BOTTOM )
{ {
checkBoxRect.SetY(cellRect.y + cellRect.height contentRect.SetY(cellRect.y + cellRect.height
- checkBoxRect.y - GRID_CELL_CHECKBOX_MARGIN); - contentRect.y - GRID_CELL_CHECKBOX_MARGIN);
} }
else // wxALIGN_TOP 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 #endif // wxUSE_GRID

View File

@@ -937,11 +937,8 @@ wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
// compute it only once (no locks for MT safeness in GUI thread...) // compute it only once (no locks for MT safeness in GUI thread...)
if ( !ms_sizeCheckMark.x ) 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 = ms_sizeCheckMark =
wxGetGridCheckBoxRect(&grid, r, wxALIGN_LEFT, wxALIGN_TOP).GetSize(); wxRendererNative::Get().GetCheckBoxSize(&grid, wxCONTROL_CELL);
} }
return ms_sizeCheckMark; return ms_sizeCheckMark;
@@ -960,8 +957,9 @@ void wxGridCellBoolRenderer::Draw(wxGrid& grid,
int vAlign = wxALIGN_CENTRE_VERTICAL; int vAlign = wxALIGN_CENTRE_VERTICAL;
attr.GetNonDefaultAlignment(&hAlign, &vAlign); attr.GetNonDefaultAlignment(&hAlign, &vAlign);
const wxRect const wxRect checkBoxRect =
checkBoxRect = wxGetGridCheckBoxRect(&grid, rect, hAlign, vAlign); wxGetContentRect(GetBestSize(grid, attr, dc, row, col),
rect, hAlign, vAlign);
bool value; bool value;
if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )

View File

@@ -1223,16 +1223,11 @@ void wxGridCellBoolEditor::SetSize(const wxRect& r)
if (GetCellAttr()) if (GetCellAttr())
GetCellAttr()->GetNonDefaultAlignment(&hAlign, &vAlign); GetCellAttr()->GetNonDefaultAlignment(&hAlign, &vAlign);
const wxRect const wxRect checkBoxRect =
checkBoxRect = wxGetGridCheckBoxRect(GetWindow(), r, hAlign, vAlign); wxGetContentRect(m_control->GetSize(), r, hAlign, vAlign);
// resize the control if required // Don't resize the checkbox, it should have its default (and fitting)
if ( m_control->GetSize() != checkBoxRect.GetSize() ) // size, but do move it to the right position.
{
m_control->SetSize(checkBoxRect.GetSize());
}
// and move it
m_control->Move(checkBoxRect.GetPosition()); m_control->Move(checkBoxRect.GetPosition());
} }