Make wxGenericCalendarCtrl DPI aware

Use FromDIP for the hard-coded constants, and replace some numbers with constants.

Don't use a fixed position in the calendar example.
This commit is contained in:
Maarten Bent
2025-09-05 23:50:21 +02:00
parent c5f6b33f75
commit e3bfafeb57
2 changed files with 19 additions and 16 deletions
+2 -3
View File
@@ -389,9 +389,8 @@ bool MyApp::OnInit()
wxUILocale::UseDefault();
// Create the main application window
MyFrame *frame = new MyFrame("Calendar wxWidgets sample"
,wxPoint(50, 50), wxSize(460, 340)
);
MyFrame* frame = new MyFrame("Calendar wxWidgets sample");
frame->SetSize(frame->FromDIP(wxSize(460, 340)));
frame->Show(true);
+17 -13
View File
@@ -673,8 +673,10 @@ size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const
// the same space
// the constants used for the layout
#define VERT_MARGIN 5 // distance between choice and calendar
#define HORZ_MARGIN 5 // spin
constexpr auto VERT_MARGIN = 5; // distance between choice and calendar
constexpr auto HORZ_MARGIN = 5; // distance between choice and spin
constexpr auto DAY_MARGIN = 2; // distance between day rows and column
constexpr auto WEEK_MARGIN = 4; // extra width of the week column
wxSize wxGenericCalendarCtrl::DoGetBestSize() const
{
@@ -682,7 +684,7 @@ wxSize wxGenericCalendarCtrl::DoGetBestSize() const
const_cast<wxGenericCalendarCtrl *>(this)->RecalcGeometry();
wxCoord width = 7*m_widthCol + m_calendarWeekWidth,
height = 7*m_heightRow + m_rowOffset + VERT_MARGIN;
height = 7*m_heightRow + m_rowOffset + FromDIP(VERT_MARGIN);
if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
{
@@ -690,9 +692,9 @@ wxSize wxGenericCalendarCtrl::DoGetBestSize() const
const wxSize bestSizeSpin = m_spinYear->GetBestSize();
height += wxMax(bestSizeChoice.y, bestSizeSpin.y)
+ VERT_MARGIN;
+ FromDIP(VERT_MARGIN);
wxCoord w2 = bestSizeChoice.x + HORZ_MARGIN + bestSizeSpin.x;
wxCoord w2 = bestSizeChoice.x + FromDIP(HORZ_MARGIN) + bestSizeSpin.x;
if ( width < w2 )
width = w2;
}
@@ -721,12 +723,12 @@ void wxGenericCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
m_choiceMonth->SetSize(x, y + (maxHeight - sizeChoice.y)/2, sizeChoice.x, -1);
m_staticMonth->SetSize(x, y + dy, sizeChoice.x, -1);
int xDiff = sizeChoice.x + HORZ_MARGIN;
int xDiff = sizeChoice.x + FromDIP(HORZ_MARGIN);
m_spinYear->SetSize(x + xDiff, y + (maxHeight - sizeSpin.y)/2, width - xDiff, maxHeight);
m_staticYear->SetSize(x + xDiff, y + dy, width - xDiff, sizeStatic.y);
yDiff = maxHeight + VERT_MARGIN;
yDiff = maxHeight + FromDIP(VERT_MARGIN);
}
else // no controls on the top
{
@@ -771,11 +773,11 @@ void wxGenericCalendarCtrl::RecalcGeometry()
}
m_calendarWeekWidth = HasFlag( wxCAL_SHOW_WEEK_NUMBERS )
? dc.GetTextExtent( wxString::Format( wxT( "%d" ), 42 )).GetWidth() + 4 : 0;
? dc.GetTextExtent( wxString::Format( wxT( "%d" ), 42 )).GetWidth() + FromDIP(WEEK_MARGIN) : 0;
// leave some margins
m_widthCol += 2;
m_heightRow += 2;
m_widthCol += FromDIP(DAY_MARGIN);
m_heightRow += FromDIP(DAY_MARGIN);
m_rowOffset = HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) ? m_heightRow : 0; // conditional in relation to style
}
@@ -843,10 +845,12 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
if ( AllowMonthChange() )
{
wxDateTime ldpm = wxDateTime(1,m_date.GetMonth(), m_date.GetYear()) - wxDateSpan::Day(); // last day prev month
int rectx = FromDIP(4);
int recty = FromDIP(3);
// Check if range permits change
if ( IsDateInRange(ldpm) && ( ( ldpm.GetYear() == m_date.GetYear() ) ? true : AllowYearChange() ) )
{
m_leftArrowRect = wxRect(larrowx - 3, arrowy - 3, (arrowheight / 2) + 8, (arrowheight + 6));
m_leftArrowRect = wxRect(larrowx - rectx + 1, arrowy - recty, (arrowheight / 2) + 2 * rectx, (arrowheight + 2 * recty));
dc.SetBrush(*wxBLACK_BRUSH);
dc.SetPen(*wxBLACK_PEN);
dc.DrawPolygon(3, leftarrow, larrowx , arrowy, wxWINDING_RULE);
@@ -856,7 +860,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
wxDateTime fdnm = wxDateTime(1,m_date.GetMonth(), m_date.GetYear()) + wxDateSpan::Month(); // first day next month
if ( IsDateInRange(fdnm) && ( ( fdnm.GetYear() == m_date.GetYear() ) ? true : AllowYearChange() ) )
{
m_rightArrowRect = wxRect(rarrowx - 4, arrowy - 3, (arrowheight / 2) + 8, (arrowheight + 6));
m_rightArrowRect = wxRect(rarrowx - rectx, arrowy - recty, (arrowheight / 2) + 2 * rectx, (arrowheight + 2 * recty));
dc.SetBrush(*wxBLACK_BRUSH);
dc.SetPen(*wxBLACK_PEN);
dc.DrawPolygon(3, rightarrow, rarrowx , arrowy, wxWINDING_RULE);
@@ -916,7 +920,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
wxString text = wxString::Format( wxT( "%d" ), weekNr );
wxCoord weekw, weekh;
dc.GetTextExtent(text, &weekw, &weekh);
int weekx = m_calendarWeekWidth - weekw - 2;
int weekx = m_calendarWeekWidth - weekw - (FromDIP(WEEK_MARGIN) / 2);
int weeky = (i * m_heightRow) + (y + m_heightRow / 2 - weekh / 2);
dc.DrawText(text, weekx, weeky);
date += wxDateSpan::Week();