Improve wxButton and wxToggleButton with bitmap in widgets sample

Use a valid font when creating the bitmap.
Create the bitmap with a DPI independent size.
Use different images for different button states, as described by the checkbox
options.
Add a checkbox to disable the bitmap.
Recreate the button when changing label, so the bitmap is updated.
Implement the 'fit exactly' option on ToggleButton page.
This commit is contained in:
Maarten Bent
2019-01-27 15:10:32 +01:00
parent c68e5d0617
commit a6fbfacc62
2 changed files with 64 additions and 25 deletions

View File

@@ -115,7 +115,7 @@ protected:
void AddButtonToSizer(); void AddButtonToSizer();
// helper function: create a bitmap for wxBitmapButton // helper function: create a bitmap for wxBitmapButton
wxBitmap CreateBitmap(const wxString& label); wxBitmap CreateBitmap(const wxString& label, const wxArtID& type);
// the controls // the controls
@@ -133,7 +133,8 @@ protected:
*m_chkUseMarkup, *m_chkUseMarkup,
#endif // wxUSE_MARKUP #endif // wxUSE_MARKUP
*m_chkDefault, *m_chkDefault,
*m_chkUseBitmapClass; *m_chkUseBitmapClass,
*m_chkDisable;
// more checkboxes for wxBitmapButton only // more checkboxes for wxBitmapButton only
wxCheckBox *m_chkUsePressed, wxCheckBox *m_chkUsePressed,
@@ -216,6 +217,7 @@ ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl *book,
#endif // wxUSE_MARKUP #endif // wxUSE_MARKUP
m_chkDefault = m_chkDefault =
m_chkUseBitmapClass = m_chkUseBitmapClass =
m_chkDisable =
m_chkUsePressed = m_chkUsePressed =
m_chkUseFocused = m_chkUseFocused =
m_chkUseCurrent = m_chkUseCurrent =
@@ -256,6 +258,8 @@ void ButtonWidgetsPage::CreateContent()
"Use wxBitmapButton"); "Use wxBitmapButton");
m_chkUseBitmapClass->SetValue(true); m_chkUseBitmapClass->SetValue(true);
m_chkDisable = CreateCheckBoxAndAddToSizer(sizerLeft, "Disable");
sizerLeft->AddSpacer(5); sizerLeft->AddSpacer(5);
wxSizer *sizerUseLabels = wxSizer *sizerUseLabels =
@@ -368,6 +372,7 @@ void ButtonWidgetsPage::Reset()
m_chkUseMarkup->SetValue(false); m_chkUseMarkup->SetValue(false);
#endif // wxUSE_MARKUP #endif // wxUSE_MARKUP
m_chkUseBitmapClass->SetValue(true); m_chkUseBitmapClass->SetValue(true);
m_chkDisable->SetValue(false);
m_chkUsePressed->SetValue(true); m_chkUsePressed->SetValue(true);
m_chkUseFocused->SetValue(true); m_chkUseFocused->SetValue(true);
@@ -456,22 +461,22 @@ void ButtonWidgetsPage::CreateButton()
if ( m_chkUseBitmapClass->GetValue() ) if ( m_chkUseBitmapClass->GetValue() )
{ {
bbtn = new wxBitmapButton(this, ButtonPage_Button, bbtn = new wxBitmapButton(this, ButtonPage_Button,
CreateBitmap("normal"), CreateBitmap("normal", wxART_INFORMATION),
wxDefaultPosition, wxDefaultSize, flags); wxDefaultPosition, wxDefaultSize, flags);
} }
else else
{ {
bbtn = new wxButton(this, ButtonPage_Button); bbtn = new wxButton(this, ButtonPage_Button);
bbtn->SetBitmapLabel(CreateBitmap("normal")); bbtn->SetBitmapLabel(CreateBitmap("normal", wxART_INFORMATION));
} }
if ( m_chkUsePressed->GetValue() ) if ( m_chkUsePressed->GetValue() )
bbtn->SetBitmapPressed(CreateBitmap("pushed")); bbtn->SetBitmapPressed(CreateBitmap("pushed", wxART_HELP));
if ( m_chkUseFocused->GetValue() ) if ( m_chkUseFocused->GetValue() )
bbtn->SetBitmapFocus(CreateBitmap("focused")); bbtn->SetBitmapFocus(CreateBitmap("focused", wxART_ERROR));
if ( m_chkUseCurrent->GetValue() ) if ( m_chkUseCurrent->GetValue() )
bbtn->SetBitmapCurrent(CreateBitmap("hover")); bbtn->SetBitmapCurrent(CreateBitmap("hover", wxART_WARNING));
if ( m_chkUseDisabled->GetValue() ) if ( m_chkUseDisabled->GetValue() )
bbtn->SetBitmapDisabled(CreateBitmap("disabled")); bbtn->SetBitmapDisabled(CreateBitmap("disabled", wxART_MISSING_IMAGE));
m_button = bbtn; m_button = bbtn;
#if wxUSE_COMMANDLINKBUTTON #if wxUSE_COMMANDLINKBUTTON
m_cmdLnkButton = NULL; m_cmdLnkButton = NULL;
@@ -543,6 +548,8 @@ void ButtonWidgetsPage::CreateButton()
if ( m_chkDefault->GetValue() ) if ( m_chkDefault->GetValue() )
m_button->SetDefault(); m_button->SetDefault();
m_button->Enable(!m_chkDisable->IsChecked());
AddButtonToSizer(); AddButtonToSizer();
m_sizerButton->Layout(); m_sizerButton->Layout();
@@ -597,6 +604,9 @@ void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
m_button->SetLabel(labelText); m_button->SetLabel(labelText);
} }
if ( m_chkBitmapOnly->IsChecked() )
CreateButton();
m_sizerButton->Layout(); m_sizerButton->Layout();
} }
@@ -618,17 +628,18 @@ void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
// bitmap button stuff // bitmap button stuff
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxBitmap ButtonWidgetsPage::CreateBitmap(const wxString& label) wxBitmap ButtonWidgetsPage::CreateBitmap(const wxString& label, const wxArtID& type)
{ {
wxBitmap bmp(180, 70); // shouldn't hardcode but it's simpler like this wxBitmap bmp(FromDIP(wxSize(180, 70))); // shouldn't hardcode but it's simpler like this
wxMemoryDC dc; wxMemoryDC dc;
dc.SelectObject(bmp); dc.SelectObject(bmp);
dc.SetFont(GetFont());
dc.SetBackground(*wxCYAN_BRUSH); dc.SetBackground(*wxCYAN_BRUSH);
dc.Clear(); dc.Clear();
dc.SetTextForeground(*wxBLACK); dc.SetTextForeground(*wxBLACK);
dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + "\n" dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + "\n"
"(" + label + " state)", "(" + label + " state)",
wxArtProvider::GetBitmap(wxART_INFORMATION), wxArtProvider::GetBitmap(type),
wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20), wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20),
wxALIGN_CENTRE); wxALIGN_CENTRE);

View File

@@ -108,8 +108,11 @@ protected:
// (re)create the toggle // (re)create the toggle
void CreateToggle(); void CreateToggle();
// add m_button to m_sizerButton using current value of m_chkFit
void AddButtonToSizer();
// helper function: create a bitmap for wxBitmapToggleButton // helper function: create a bitmap for wxBitmapToggleButton
wxBitmap CreateBitmap(const wxString& label); wxBitmap CreateBitmap(const wxString& label, const wxArtID& type);
// the controls // the controls
// ------------ // ------------
@@ -122,7 +125,8 @@ protected:
wxCheckBox *m_chkBitmapOnly, wxCheckBox *m_chkBitmapOnly,
*m_chkTextAndBitmap, *m_chkTextAndBitmap,
*m_chkFit, *m_chkFit,
*m_chkUseBitmapClass; *m_chkUseBitmapClass,
*m_chkDisable;
// more checkboxes for wxBitmapToggleButton only // more checkboxes for wxBitmapToggleButton only
wxCheckBox *m_chkUsePressed, wxCheckBox *m_chkUsePressed,
@@ -192,6 +196,7 @@ ToggleWidgetsPage::ToggleWidgetsPage(WidgetsBookCtrl *book,
m_chkTextAndBitmap = m_chkTextAndBitmap =
m_chkFit = m_chkFit =
m_chkUseBitmapClass = m_chkUseBitmapClass =
m_chkDisable =
m_chkUsePressed = m_chkUsePressed =
m_chkUseFocused = m_chkUseFocused =
m_chkUseCurrent = m_chkUseCurrent =
@@ -231,6 +236,8 @@ void ToggleWidgetsPage::CreateContent()
"Use wxBitmapToggleButton"); "Use wxBitmapToggleButton");
m_chkUseBitmapClass->SetValue(true); m_chkUseBitmapClass->SetValue(true);
m_chkDisable = CreateCheckBoxAndAddToSizer(sizerLeft, "Disable");
sizerLeft->AddSpacer(5); sizerLeft->AddSpacer(5);
wxSizer *sizerUseLabels = wxSizer *sizerUseLabels =
@@ -327,6 +334,7 @@ void ToggleWidgetsPage::Reset()
m_chkUseMarkup->SetValue(false); m_chkUseMarkup->SetValue(false);
#endif // wxUSE_MARKUP #endif // wxUSE_MARKUP
m_chkUseBitmapClass->SetValue(true); m_chkUseBitmapClass->SetValue(true);
m_chkDisable->SetValue(false);
m_chkUsePressed->SetValue(true); m_chkUsePressed->SetValue(true);
m_chkUseFocused->SetValue(true); m_chkUseFocused->SetValue(true);
@@ -419,22 +427,22 @@ void ToggleWidgetsPage::CreateToggle()
if ( m_chkUseBitmapClass->GetValue() ) if ( m_chkUseBitmapClass->GetValue() )
{ {
btgl = new wxBitmapToggleButton(this, TogglePage_Picker, btgl = new wxBitmapToggleButton(this, TogglePage_Picker,
CreateBitmap("normal")); CreateBitmap("normal", wxART_INFORMATION));
} }
else else
{ {
btgl = new wxToggleButton(this, TogglePage_Picker, ""); btgl = new wxToggleButton(this, TogglePage_Picker, "");
btgl->SetBitmapLabel(CreateBitmap("normal")); btgl->SetBitmapLabel(CreateBitmap("normal", wxART_INFORMATION));
} }
#ifdef wxHAS_ANY_BUTTON #ifdef wxHAS_ANY_BUTTON
if ( m_chkUsePressed->GetValue() ) if ( m_chkUsePressed->GetValue() )
btgl->SetBitmapPressed(CreateBitmap("pushed")); btgl->SetBitmapPressed(CreateBitmap("pushed", wxART_HELP));
if ( m_chkUseFocused->GetValue() ) if ( m_chkUseFocused->GetValue() )
btgl->SetBitmapFocus(CreateBitmap("focused")); btgl->SetBitmapFocus(CreateBitmap("focused", wxART_ERROR));
if ( m_chkUseCurrent->GetValue() ) if ( m_chkUseCurrent->GetValue() )
btgl->SetBitmapCurrent(CreateBitmap("hover")); btgl->SetBitmapCurrent(CreateBitmap("hover", wxART_WARNING));
if ( m_chkUseDisabled->GetValue() ) if ( m_chkUseDisabled->GetValue() )
btgl->SetBitmapDisabled(CreateBitmap("disabled")); btgl->SetBitmapDisabled(CreateBitmap("disabled", wxART_MISSING_IMAGE));
#endif // wxHAS_ANY_BUTTON #endif // wxHAS_ANY_BUTTON
m_toggle = btgl; m_toggle = btgl;
} }
@@ -473,6 +481,7 @@ void ToggleWidgetsPage::CreateToggle()
#endif // wxHAS_ANY_BUTTON #endif // wxHAS_ANY_BUTTON
m_chkUseBitmapClass->Enable(showsBitmap); m_chkUseBitmapClass->Enable(showsBitmap);
m_chkTextAndBitmap->Enable(!m_chkBitmapOnly->IsChecked());
m_chkUsePressed->Enable(showsBitmap); m_chkUsePressed->Enable(showsBitmap);
m_chkUseFocused->Enable(showsBitmap); m_chkUseFocused->Enable(showsBitmap);
@@ -480,12 +489,27 @@ void ToggleWidgetsPage::CreateToggle()
m_chkUseDisabled->Enable(showsBitmap); m_chkUseDisabled->Enable(showsBitmap);
#endif // wxHAS_BITMAPTOGGLEBUTTON #endif // wxHAS_BITMAPTOGGLEBUTTON
m_sizerToggle->Add(0, 0, 1, wxCENTRE); m_toggle->Enable(!m_chkDisable->IsChecked());
m_sizerToggle->Add(m_toggle, 1, wxCENTRE);
m_sizerToggle->Add(0, 0, 1, wxCENTRE); AddButtonToSizer();
m_sizerToggle->Layout(); m_sizerToggle->Layout();
} }
void ToggleWidgetsPage::AddButtonToSizer()
{
if ( m_chkFit->GetValue() )
{
m_sizerToggle->AddStretchSpacer(1);
m_sizerToggle->Add(m_toggle, wxSizerFlags(0).Centre().Border());
m_sizerToggle->AddStretchSpacer(1);
}
else // take up the entire space
{
m_sizerToggle->Add(m_toggle, wxSizerFlags(1).Expand().Border());
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// event handlers // event handlers
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -512,6 +536,9 @@ void ToggleWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
else else
#endif // wxUSE_MARKUP #endif // wxUSE_MARKUP
m_toggle->SetLabel(labelText); m_toggle->SetLabel(labelText);
if ( m_chkBitmapOnly->IsChecked() )
CreateToggle();
} }
#ifdef wxHAS_BITMAPTOGGLEBUTTON #ifdef wxHAS_BITMAPTOGGLEBUTTON
@@ -519,17 +546,18 @@ void ToggleWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
// bitmap toggle button stuff // bitmap toggle button stuff
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxBitmap ToggleWidgetsPage::CreateBitmap(const wxString& label) wxBitmap ToggleWidgetsPage::CreateBitmap(const wxString& label, const wxArtID& type)
{ {
wxBitmap bmp(180, 70); // shouldn't hardcode but it's simpler like this wxBitmap bmp(FromDIP(wxSize(180, 70))); // shouldn't hardcode but it's simpler like this
wxMemoryDC dc; wxMemoryDC dc;
dc.SelectObject(bmp); dc.SelectObject(bmp);
dc.SetFont(GetFont());
dc.SetBackground(*wxCYAN_BRUSH); dc.SetBackground(*wxCYAN_BRUSH);
dc.Clear(); dc.Clear();
dc.SetTextForeground(*wxBLACK); dc.SetTextForeground(*wxBLACK);
dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + "\n" dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + "\n"
"(" + label + " state)", "(" + label + " state)",
wxArtProvider::GetBitmap(wxART_INFORMATION), wxArtProvider::GetBitmap(type),
wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20), wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20),
wxALIGN_CENTRE); wxALIGN_CENTRE);