Make wxGenericColourButton and wxGenericColourDialog DPI aware.

See https://github.com/wxWidgets/wxWidgets/pull/2006
This commit is contained in:
Vadim Zeitlin
2020-08-03 00:49:56 +02:00
4 changed files with 86 additions and 48 deletions

View File

@@ -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.

View File

@@ -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
@@ -79,6 +80,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
@@ -122,11 +124,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();

View File

@@ -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 );

View File

@@ -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,13 +156,18 @@ 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)
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,41 @@ 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());
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++ )
{
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]);
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
void wxGenericColourDialog::OnPaint(wxPaintEvent& WXUNUSED(event))
@@ -240,33 +281,38 @@ 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);
m_okButtonX = 10;
m_customButtonX = m_singleCustomColourRect.x ;
m_buttonY = m_customColoursRect.y + m_customColoursRect.height + 10;
}
void wxGenericColourDialog::CreateWidgets()
@@ -274,35 +320,12 @@ 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 );
const int sliderHeight = 160;
const int sliderHeight = FromDIP(160);
// first sliders
#if wxUSE_SLIDER