Make wxGenericColourButton DPI aware

This commit is contained in:
Maarten Bent
2019-09-16 00:43:43 +02:00
parent 8df97af5fe
commit 4f09f83597
2 changed files with 21 additions and 3 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

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