From 4f09f835975df7673cb46ed5d9bba69a4d8a9adb Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Mon, 16 Sep 2019 00:43:43 +0200 Subject: [PATCH 1/4] Make wxGenericColourButton DPI aware --- include/wx/generic/clrpickerg.h | 2 ++ src/generic/clrpickerg.cpp | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/include/wx/generic/clrpickerg.h b/include/wx/generic/clrpickerg.h index 9f8327b422..2194d72c14 100644 --- a/include/wx/generic/clrpickerg.h +++ b/include/wx/generic/clrpickerg.h @@ -71,6 +71,8 @@ protected: void UpdateColour() wxOVERRIDE; + void OnDPIChanged(wxDPIChangedEvent& event); + // the colour data shown in wxColourPickerCtrlGeneric // controls. This member is static so that all colour pickers // in the program share the same set of custom colours. diff --git a/src/generic/clrpickerg.cpp b/src/generic/clrpickerg.cpp index d0f8434168..68dadc7d65 100644 --- a/src/generic/clrpickerg.cpp +++ b/src/generic/clrpickerg.cpp @@ -29,6 +29,10 @@ #include "wx/colordlg.h" #include "wx/dcmemory.h" +namespace // anonymous namespace +{ +const wxSize defaultBitmapSize(60, 13); +} // ============================================================================ // implementation @@ -46,8 +50,6 @@ bool wxGenericColourButton::Create( wxWindow *parent, wxWindowID id, const wxSize &size, long style, const wxValidator& validator, const wxString &name) { - m_bitmap = wxBitmap( 60, 13 ); - // create this button if (!wxBitmapButton::Create( parent, id, m_bitmap, pos, size, style, validator, name )) @@ -59,11 +61,14 @@ bool wxGenericColourButton::Create( wxWindow *parent, wxWindowID id, // and handle user clicks on it Bind(wxEVT_BUTTON, &wxGenericColourButton::OnButtonClick, this, GetId()); + m_bitmap = wxBitmap(FromDIP(defaultBitmapSize)); m_colour = col; UpdateColour(); InitColourData(); ms_data.SetChooseAlpha((style & wxCLRP_SHOW_ALPHA) != 0); + Bind(wxEVT_DPI_CHANGED, &wxGenericColourButton::OnDPIChanged, this); + return true; } @@ -118,6 +123,12 @@ void wxGenericColourButton::OnColourChanged(wxColourDialogEvent& ev) parent->ProcessWindowEvent(event); } +void wxGenericColourButton::OnDPIChanged(wxDPIChangedEvent&WXUNUSED(event)) +{ + m_bitmap = wxBitmap(FromDIP(defaultBitmapSize)); + UpdateColour(); +} + void wxGenericColourButton::UpdateColour() { wxMemoryDC dc(m_bitmap); @@ -130,7 +141,12 @@ void wxGenericColourButton::UpdateColour() wxColour col( ~m_colour.Red(), ~m_colour.Green(), ~m_colour.Blue() ); dc.SetTextForeground( col ); dc.SetFont( GetFont() ); - dc.DrawText( m_colour.GetAsString(wxC2S_HTML_SYNTAX), 0, 0 ); + + const wxString text = m_colour.GetAsString(wxC2S_HTML_SYNTAX); + const wxSize textSize = dc.GetTextExtent(text); + const int x = (m_bitmap.GetWidth() - textSize.GetWidth()) / 2; + const int y = (m_bitmap.GetHeight() - textSize.GetHeight()) / 2; + dc.DrawText(text, x, y); } dc.SelectObject( wxNullBitmap ); From a54b4073f99074d18a4fde2de45d58fa06a313b1 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Fri, 31 Jul 2020 00:06:27 +0200 Subject: [PATCH 2/4] Remove unused members from wxGenericColourDialog --- include/wx/generic/colrdlgg.h | 5 ----- src/generic/colrdlgg.cpp | 4 ---- 2 files changed, 9 deletions(-) diff --git a/include/wx/generic/colrdlgg.h b/include/wx/generic/colrdlgg.h index fbe8844df7..771d48cfbc 100644 --- a/include/wx/generic/colrdlgg.h +++ b/include/wx/generic/colrdlgg.h @@ -122,11 +122,6 @@ protected: wxStaticBitmap *m_customColoursBmp[16]; #endif // wxCLRDLGG_USE_PREVIEW_WITH_ALPHA - int m_buttonY; - - int m_okButtonX; - int m_customButtonX; - // static bool colourDialogCancelled; wxDECLARE_EVENT_TABLE(); diff --git a/src/generic/colrdlgg.cpp b/src/generic/colrdlgg.cpp index 3c3919c40d..f7c09f2b5c 100644 --- a/src/generic/colrdlgg.cpp +++ b/src/generic/colrdlgg.cpp @@ -263,10 +263,6 @@ void wxGenericColourDialog::CalculateMeasurements() m_singleCustomColourRect.x = m_customColoursRect.width + m_customColoursRect.x + m_sectionSpacing; m_singleCustomColourRect.y = 80; m_singleCustomColourRect.SetSize(customRectangleSize); - - m_okButtonX = 10; - m_customButtonX = m_singleCustomColourRect.x ; - m_buttonY = m_customColoursRect.y + m_customColoursRect.height + 10; } void wxGenericColourDialog::CreateWidgets() From 9e2615ac06a146060a872e596b6ec78f3b3af3ff Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Fri, 31 Jul 2020 00:06:35 +0200 Subject: [PATCH 3/4] Move creating custom bitmaps of wxGenericColourDialog into separate function --- include/wx/generic/colrdlgg.h | 1 + src/generic/colrdlgg.cpp | 61 +++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/include/wx/generic/colrdlgg.h b/include/wx/generic/colrdlgg.h index 771d48cfbc..7c4a07ce62 100644 --- a/include/wx/generic/colrdlgg.h +++ b/include/wx/generic/colrdlgg.h @@ -79,6 +79,7 @@ public: void OnCloseWindow(wxCloseEvent& event); #if wxCLRDLGG_USE_PREVIEW_WITH_ALPHA + virtual void CreateCustomBitmaps(); void DoPreviewBitmap(wxBitmap& bmp, const wxColour& colour); #endif // wxCLRDLGG_USE_PREVIEW_WITH_ALPHA diff --git a/src/generic/colrdlgg.cpp b/src/generic/colrdlgg.cpp index f7c09f2b5c..9eb6bec3c5 100644 --- a/src/generic/colrdlgg.cpp +++ b/src/generic/colrdlgg.cpp @@ -162,6 +162,12 @@ bool wxGenericColourDialog::Create(wxWindow *parent, const wxColourData *data) if (data) m_colourData = *data; +#if wxCLRDLGG_USE_PREVIEW_WITH_ALPHA + m_customColourBmp = NULL; + for ( unsigned i = 0; i < WXSIZEOF(m_customColoursBmp); i++ ) + m_customColoursBmp[i] = NULL; +#endif + InitializeColours(); CalculateMeasurements(); CreateWidgets(); @@ -225,6 +231,34 @@ void wxGenericColourDialog::OnCustomColourMouseClick(wxMouseEvent& event) event.Skip(); } + +void wxGenericColourDialog::CreateCustomBitmaps() +{ + // Bitmap to preview selected colour (with alpha channel) + wxBitmap customColourBmp(m_singleCustomColourRect.GetSize(), 32); + customColourBmp.UseAlpha(); + DoPreviewBitmap(customColourBmp, m_colourData.GetColour()); + m_customColourBmp = new wxStaticBitmap(this, wxID_ANY, customColourBmp, + m_singleCustomColourRect.GetLeftTop(), + m_singleCustomColourRect.GetSize(), + wxBORDER_SUNKEN); + + // 16 bitmaps to preview custom colours (with alpha channel) + for ( unsigned i = 0; i < WXSIZEOF(m_customColoursBmp); i++ ) + { + int x = ((i % 8) * (m_smallRectangleSize.x + m_gridSpacing)) + m_customColoursRect.x; + int y = ((i / 8) * (m_smallRectangleSize.y + m_gridSpacing)) + m_customColoursRect.y; + + wxBitmap bmp(m_smallRectangleSize, 32); + bmp.UseAlpha(); + DoPreviewBitmap(bmp, m_customColours[i]); + m_customColoursBmp[i] = new wxStaticBitmap(this, wxID_ANY, bmp, + wxPoint(x, y), m_smallRectangleSize); + m_customColoursBmp[i]->Bind(wxEVT_LEFT_DOWN, + &wxGenericColourDialog::OnCustomColourMouseClick, this); + + } +} #endif // wxCLRDLGG_USE_PREVIEW_WITH_ALPHA void wxGenericColourDialog::OnPaint(wxPaintEvent& WXUNUSED(event)) @@ -270,31 +304,8 @@ void wxGenericColourDialog::CreateWidgets() wxBeginBusyCursor(); #if wxCLRDLGG_USE_PREVIEW_WITH_ALPHA - // Bitmap to preview selected colour (with alpha channel) - wxBitmap customColourBmp(m_singleCustomColourRect.GetSize(), 32); - customColourBmp.UseAlpha(); - DoPreviewBitmap(customColourBmp, m_colourData.GetColour()); - m_customColourBmp = new wxStaticBitmap(this, wxID_ANY, customColourBmp, - m_singleCustomColourRect.GetLeftTop(), - m_singleCustomColourRect.GetSize(), - wxBORDER_SUNKEN); - - // 16 bitmaps to preview custom colours (with alpha channel) - for (unsigned i = 0; i < WXSIZEOF(m_customColoursBmp); i++) - { - int x = ((i % 8)*(m_smallRectangleSize.x+m_gridSpacing)) + m_customColoursRect.x; - int y = ((i / 8)*(m_smallRectangleSize.y+m_gridSpacing)) + m_customColoursRect.y; - - wxBitmap bmp(m_smallRectangleSize, 32); - bmp.UseAlpha(); - DoPreviewBitmap(bmp, m_customColours[i]); - m_customColoursBmp[i] = new wxStaticBitmap(this, wxID_ANY, bmp, - wxPoint(x, y), m_smallRectangleSize); - m_customColoursBmp[i]->Bind(wxEVT_LEFT_DOWN, - &wxGenericColourDialog::OnCustomColourMouseClick, this); - - } -#endif // wxCLRDLGG_USE_PREVIEW_WITH_ALPHA + CreateCustomBitmaps(); +#endif wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL ); From b10ba803f5f844d5bb0277d64ae27c7561cbae62 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Fri, 31 Jul 2020 00:06:46 +0200 Subject: [PATCH 4/4] Make wxGenericColourDialog DPI aware --- include/wx/generic/colrdlgg.h | 1 + src/generic/colrdlgg.cpp | 54 +++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/include/wx/generic/colrdlgg.h b/include/wx/generic/colrdlgg.h index 7c4a07ce62..98fe616a4b 100644 --- a/include/wx/generic/colrdlgg.h +++ b/include/wx/generic/colrdlgg.h @@ -49,6 +49,7 @@ public: // Internal functions void OnMouseEvent(wxMouseEvent& event); void OnPaint(wxPaintEvent& event); + void OnDPIChanged(wxDPIChangedEvent& event); #if wxCLRDLGG_USE_PREVIEW_WITH_ALPHA void OnCustomColourMouseClick(wxMouseEvent& event); #endif // wxCLRDLGG_USE_PREVIEW_WITH_ALPHA diff --git a/src/generic/colrdlgg.cpp b/src/generic/colrdlgg.cpp index 9eb6bec3c5..31e6d56968 100644 --- a/src/generic/colrdlgg.cpp +++ b/src/generic/colrdlgg.cpp @@ -60,6 +60,7 @@ wxBEGIN_EVENT_TABLE(wxGenericColourDialog, wxDialog) EVT_SLIDER(wxID_GREEN_SLIDER, wxGenericColourDialog::OnGreenSlider) EVT_SLIDER(wxID_BLUE_SLIDER, wxGenericColourDialog::OnBlueSlider) #endif + EVT_DPI_CHANGED(wxGenericColourDialog::OnDPIChanged) EVT_PAINT(wxGenericColourDialog::OnPaint) EVT_MOUSE_EVENTS(wxGenericColourDialog::OnMouseEvent) EVT_CLOSE(wxGenericColourDialog::OnCloseWindow) @@ -155,8 +156,7 @@ void wxGenericColourDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) bool wxGenericColourDialog::Create(wxWindow *parent, const wxColourData *data) { if ( !wxDialog::Create(GetParentForModalDialog(parent, 0), wxID_ANY, - _("Choose colour"), - wxPoint(0, 0), wxSize(900, 900)) ) + _("Choose colour")) ) return false; if (data) @@ -238,10 +238,13 @@ void wxGenericColourDialog::CreateCustomBitmaps() wxBitmap customColourBmp(m_singleCustomColourRect.GetSize(), 32); customColourBmp.UseAlpha(); DoPreviewBitmap(customColourBmp, m_colourData.GetColour()); - m_customColourBmp = new wxStaticBitmap(this, wxID_ANY, customColourBmp, - m_singleCustomColourRect.GetLeftTop(), - m_singleCustomColourRect.GetSize(), - wxBORDER_SUNKEN); + + if ( !m_customColourBmp ) + { + m_customColourBmp = new wxStaticBitmap(this, wxID_ANY, customColourBmp); + } + m_customColourBmp->SetSize(m_singleCustomColourRect); + m_customColourBmp->SetBitmap(customColourBmp); // 16 bitmaps to preview custom colours (with alpha channel) for ( unsigned i = 0; i < WXSIZEOF(m_customColoursBmp); i++ ) @@ -252,11 +255,15 @@ void wxGenericColourDialog::CreateCustomBitmaps() wxBitmap bmp(m_smallRectangleSize, 32); bmp.UseAlpha(); DoPreviewBitmap(bmp, m_customColours[i]); - m_customColoursBmp[i] = new wxStaticBitmap(this, wxID_ANY, bmp, - wxPoint(x, y), m_smallRectangleSize); - m_customColoursBmp[i]->Bind(wxEVT_LEFT_DOWN, - &wxGenericColourDialog::OnCustomColourMouseClick, this); + if ( !m_customColoursBmp[i] ) + { + m_customColoursBmp[i] = new wxStaticBitmap(this, wxID_ANY, bmp); + m_customColoursBmp[i]->Bind(wxEVT_LEFT_DOWN, + &wxGenericColourDialog::OnCustomColourMouseClick, this); + } + m_customColoursBmp[i]->SetSize(x, y, m_smallRectangleSize.x, m_smallRectangleSize.y); + m_customColoursBmp[i]->SetBitmap(bmp); } } #endif // wxCLRDLGG_USE_PREVIEW_WITH_ALPHA @@ -274,28 +281,37 @@ void wxGenericColourDialog::OnPaint(wxPaintEvent& WXUNUSED(event)) PaintHighlight(dc, true); } +void wxGenericColourDialog::OnDPIChanged(wxDPIChangedEvent& WXUNUSED(event)) +{ + CalculateMeasurements(); + +#if wxCLRDLGG_USE_PREVIEW_WITH_ALPHA + CreateCustomBitmaps(); +#endif +} + void wxGenericColourDialog::CalculateMeasurements() { // For single customizable colour - const wxSize customRectangleSize(40, 40); + const wxSize customRectangleSize = FromDIP(wxSize(40, 40)); - m_smallRectangleSize.Set(18, 14); + m_smallRectangleSize = FromDIP(wxSize(18, 14)); - m_gridSpacing = 6; - m_sectionSpacing = 15; + m_gridSpacing = FromDIP(6); + m_sectionSpacing = FromDIP(15); - m_standardColoursRect.x = 10; - m_standardColoursRect.y = 15; + m_standardColoursRect.x = FromDIP(10); + m_standardColoursRect.y = FromDIP(15); m_standardColoursRect.width = (8*m_smallRectangleSize.x) + (7*m_gridSpacing); m_standardColoursRect.height = (6*m_smallRectangleSize.y) + (5*m_gridSpacing); m_customColoursRect.x = m_standardColoursRect.x; - m_customColoursRect.y = m_standardColoursRect.y + m_standardColoursRect.height + 20; + m_customColoursRect.y = m_standardColoursRect.y + m_standardColoursRect.height + FromDIP(20); m_customColoursRect.width = (8*m_smallRectangleSize.x) + (7*m_gridSpacing); m_customColoursRect.height = (2*m_smallRectangleSize.y) + (1*m_gridSpacing); m_singleCustomColourRect.x = m_customColoursRect.width + m_customColoursRect.x + m_sectionSpacing; - m_singleCustomColourRect.y = 80; + m_singleCustomColourRect.y = FromDIP(80); m_singleCustomColourRect.SetSize(customRectangleSize); } @@ -309,7 +325,7 @@ void wxGenericColourDialog::CreateWidgets() wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL ); - const int sliderHeight = 160; + const int sliderHeight = FromDIP(160); // first sliders #if wxUSE_SLIDER