X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3379ed3789e405f874d9db757d95346f1d378795..d8eff331e23435d9d8d6483a40f6fd9997a13f87:/samples/widgets/button.cpp diff --git a/samples/widgets/button.cpp b/samples/widgets/button.cpp index eee4376195..af70f76ea0 100644 --- a/samples/widgets/button.cpp +++ b/samples/widgets/button.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Program: wxWindows Widgets Sample +// Program: wxWidgets Widgets Sample // Name: button.cpp // Purpose: Part of the widgets sample showing wxButton // Author: Vadim Zeitlin @@ -29,6 +29,7 @@ #include "wx/app.h" #include "wx/log.h" + #include "wx/bmpbuttn.h" #include "wx/button.h" #include "wx/checkbox.h" #include "wx/radiobox.h" @@ -36,7 +37,9 @@ #include "wx/textctrl.h" #endif +#include "wx/artprov.h" #include "wx/sizer.h" +#include "wx/dcmemory.h" #include "widgets.h" @@ -49,12 +52,20 @@ // control ids enum { - ButtonPage_Reset = 100, + ButtonPage_Reset = wxID_HIGHEST, ButtonPage_ChangeLabel, ButtonPage_Button }; // radio boxes +enum +{ + ButtonImagePos_Left, + ButtonImagePos_Right, + ButtonImagePos_Top, + ButtonImagePos_Bottom +}; + enum { ButtonHAlign_Left, @@ -76,8 +87,14 @@ enum class ButtonWidgetsPage : public WidgetsPage { public: - ButtonWidgetsPage(wxNotebook *notebook, wxImageList *imaglist); - virtual ~ButtonWidgetsPage(); + ButtonWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist); + virtual ~ButtonWidgetsPage(){}; + + virtual wxControl *GetWidget() const { return m_button; } + virtual void RecreateWidget() { CreateButton(); } + + // lazy creation of the content + virtual void CreateContent(); protected: // event handlers @@ -93,18 +110,36 @@ protected: // (re)create the wxButton void CreateButton(); + // add m_button to m_sizerButton using current value of m_chkFit + void AddButtonToSizer(); + + // helper function: create a bitmap for wxBitmapButton + wxBitmap CreateBitmap(const wxString& label); + + // the controls // ------------ // the check/radio boxes for styles - wxCheckBox *m_chkImage, + wxCheckBox *m_chkBitmapOnly, + *m_chkTextAndBitmap, *m_chkFit, + *m_chkAuthNeeded, *m_chkDefault; + // more checkboxes for wxBitmapButton only + wxCheckBox *m_chkUsePressed, + *m_chkUseFocused, + *m_chkUseCurrent, + *m_chkUseDisabled; + + // and an image position choice used if m_chkTextAndBitmap is on + wxRadioBox *m_radioImagePos; + wxRadioBox *m_radioHAlign, *m_radioVAlign; - // the gauge itself and the sizer it is in + // the button itself and the sizer it is in wxButton *m_button; wxSizer *m_sizerButton; @@ -112,8 +147,8 @@ protected: wxTextCtrl *m_textLabel; private: - DECLARE_EVENT_TABLE(); - DECLARE_WIDGETS_PAGE(ButtonWidgetsPage); + DECLARE_EVENT_TABLE() + DECLARE_WIDGETS_PAGE(ButtonWidgetsPage) }; // ---------------------------------------------------------------------------- @@ -126,27 +161,38 @@ BEGIN_EVENT_TABLE(ButtonWidgetsPage, WidgetsPage) EVT_BUTTON(ButtonPage_Reset, ButtonWidgetsPage::OnButtonReset) EVT_BUTTON(ButtonPage_ChangeLabel, ButtonWidgetsPage::OnButtonChangeLabel) - EVT_CHECKBOX(-1, ButtonWidgetsPage::OnCheckOrRadioBox) - EVT_RADIOBOX(-1, ButtonWidgetsPage::OnCheckOrRadioBox) + EVT_CHECKBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox) + EVT_RADIOBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox) END_EVENT_TABLE() // ============================================================================ // implementation // ============================================================================ -IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, _T("Button")); +#if defined(__WXUNIVERSAL__) + #define FAMILY_CTRLS UNIVERSAL_CTRLS +#else + #define FAMILY_CTRLS NATIVE_CTRLS +#endif -ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook, - wxImageList *imaglist) - : WidgetsPage(notebook) -{ - imaglist->Add(wxBitmap(button_xpm)); +IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, wxT("Button"), FAMILY_CTRLS ); +ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl *book, + wxImageList *imaglist) + : WidgetsPage(book, imaglist, button_xpm) +{ // init everything - m_chkImage = + m_chkBitmapOnly = + m_chkTextAndBitmap = m_chkFit = - m_chkDefault = (wxCheckBox *)NULL; - + m_chkAuthNeeded = + m_chkDefault = + m_chkUsePressed = + m_chkUseFocused = + m_chkUseCurrent = + m_chkUseDisabled = (wxCheckBox *)NULL; + + m_radioImagePos = m_radioHAlign = m_radioVAlign = (wxRadioBox *)NULL; @@ -154,44 +200,69 @@ ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook, m_button = (wxButton *)NULL; m_sizerButton = (wxSizer *)NULL; +} +void ButtonWidgetsPage::CreateContent() +{ wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); // left pane - wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style")); + wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style")); wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); - m_chkImage = CreateCheckBoxAndAddToSizer(sizerLeft, _T("With &image")); - m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit exactly")); - m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Default")); - -#ifndef __WXUNIVERSAL__ - // only wxUniv currently supoprts buttons with images - m_chkImage->Disable(); -#endif // !wxUniv - - sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer + m_chkBitmapOnly = CreateCheckBoxAndAddToSizer(sizerLeft, "&Bitmap only"); + m_chkTextAndBitmap = CreateCheckBoxAndAddToSizer(sizerLeft, "Text &and bitmap"); + m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Fit exactly")); + m_chkAuthNeeded = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Require a&uth")); + m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Default")); + + sizerLeft->AddSpacer(5); + + wxSizer *sizerUseLabels = + new wxStaticBoxSizer(wxVERTICAL, this, + "&Use the following bitmaps in addition to the normal one?"); + m_chkUsePressed = CreateCheckBoxAndAddToSizer(sizerUseLabels, + "&Pressed (small help icon)"); + m_chkUseFocused = CreateCheckBoxAndAddToSizer(sizerUseLabels, + "&Focused (small error icon)"); + m_chkUseCurrent = CreateCheckBoxAndAddToSizer(sizerUseLabels, + "&Current (small warning icon)"); + m_chkUseDisabled = CreateCheckBoxAndAddToSizer(sizerUseLabels, + "&Disabled (broken image icon)"); + sizerLeft->Add(sizerUseLabels, wxSizerFlags().Expand().Border()); + + sizerLeft->AddSpacer(10); + + static const wxString dirs[] = + { + "left", "right", "top", "bottom", + }; + m_radioImagePos = new wxRadioBox(this, wxID_ANY, "Image &position", + wxDefaultPosition, wxDefaultSize, + WXSIZEOF(dirs), dirs); + sizerLeft->Add(m_radioImagePos, 0, wxGROW | wxALL, 5); + sizerLeft->AddSpacer(15); // should be in sync with enums Button[HV]Align! static const wxString halign[] = { - _T("left"), - _T("centre"), - _T("right"), + wxT("left"), + wxT("centre"), + wxT("right"), }; static const wxString valign[] = { - _T("top"), - _T("centre"), - _T("bottom"), + wxT("top"), + wxT("centre"), + wxT("bottom"), }; - m_radioHAlign = new wxRadioBox(this, -1, _T("&Horz alignment"), + m_radioHAlign = new wxRadioBox(this, wxID_ANY, wxT("&Horz alignment"), wxDefaultPosition, wxDefaultSize, WXSIZEOF(halign), halign); - m_radioVAlign = new wxRadioBox(this, -1, _T("&Vert alignment"), + m_radioVAlign = new wxRadioBox(this, wxID_ANY, wxT("&Vert alignment"), wxDefaultPosition, wxDefaultSize, WXSIZEOF(valign), valign); @@ -200,45 +271,35 @@ ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook, sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer - wxButton *btn = new wxButton(this, ButtonPage_Reset, _T("&Reset")); + wxButton *btn = new wxButton(this, ButtonPage_Reset, wxT("&Reset")); sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); // middle pane - wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Operations")); + wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, wxT("&Operations")); wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL); wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel, - _T("Change label"), - -1, + wxT("Change label"), + wxID_ANY, &m_textLabel); + m_textLabel->SetValue(wxT("&Press me!")); sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); // right pane - wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL); - m_button = new wxButton(this, ButtonPage_Button, _T("&Press me!")); - sizerRight->Add(0, 0, 1, wxCENTRE); - sizerRight->Add(m_button, 1, wxCENTRE); - sizerRight->Add(0, 0, 1, wxCENTRE); - sizerRight->SetMinSize(250, 0); - m_sizerButton = sizerRight; // save it to modify it later + m_sizerButton = new wxBoxSizer(wxHORIZONTAL); + m_sizerButton->SetMinSize(150, 0); // the 3 panes panes compose the window sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10); sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10); - sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); + sizerTop->Add(m_sizerButton, 1, wxGROW | (wxALL & ~wxRIGHT), 10); - // final initializations + // do create the main control Reset(); + CreateButton(); - SetAutoLayout(TRUE); SetSizer(sizerTop); - - sizerTop->Fit(this); -} - -ButtonWidgetsPage::~ButtonWidgetsPage() -{ } // ---------------------------------------------------------------------------- @@ -247,10 +308,18 @@ ButtonWidgetsPage::~ButtonWidgetsPage() void ButtonWidgetsPage::Reset() { - m_chkFit->SetValue(TRUE); - m_chkImage->SetValue(FALSE); - m_chkDefault->SetValue(FALSE); - + m_chkBitmapOnly->SetValue(false); + m_chkFit->SetValue(true); + m_chkAuthNeeded->SetValue(false); + m_chkTextAndBitmap->SetValue(false); + m_chkDefault->SetValue(false); + + m_chkUsePressed->SetValue(true); + m_chkUseFocused->SetValue(true); + m_chkUseCurrent->SetValue(true); + m_chkUseDisabled->SetValue(true); + + m_radioImagePos->SetSelection(ButtonImagePos_Left); m_radioHAlign->SetSelection(ButtonHAlign_Centre); m_radioVAlign->SetSelection(ButtonVAlign_Centre); } @@ -265,83 +334,131 @@ void ButtonWidgetsPage::CreateButton() size_t count = m_sizerButton->GetChildren().GetCount(); for ( size_t n = 0; n < count; n++ ) { - m_sizerButton->Remove(0); + m_sizerButton->Remove( 0 ); } delete m_button; } - else + + if ( label.empty() ) { - label = _T("&Press me!"); + // creating for the first time or recreating a button after bitmap + // button + label = m_textLabel->GetValue(); } - int flags = 0; + int flags = ms_defaultFlags; switch ( m_radioHAlign->GetSelection() ) { case ButtonHAlign_Left: - flags |= wxALIGN_LEFT; + flags |= wxBU_LEFT; break; default: - wxFAIL_MSG(_T("unexpected radiobox selection")); + wxFAIL_MSG(wxT("unexpected radiobox selection")); // fall through case ButtonHAlign_Centre: - flags |= wxALIGN_CENTRE_HORIZONTAL; break; case ButtonHAlign_Right: - flags |= wxALIGN_RIGHT; + flags |= wxBU_RIGHT; break; } switch ( m_radioVAlign->GetSelection() ) { case ButtonVAlign_Top: - flags |= wxALIGN_TOP; + flags |= wxBU_TOP; break; default: - wxFAIL_MSG(_T("unexpected radiobox selection")); + wxFAIL_MSG(wxT("unexpected radiobox selection")); // fall through case ButtonVAlign_Centre: - flags |= wxALIGN_CENTRE_VERTICAL; + // centre vertical alignment is the default (no style) break; case ButtonVAlign_Bottom: - flags |= wxALIGN_BOTTOM; + flags |= wxBU_BOTTOM; break; } - m_button = new wxButton(this, ButtonPage_Button, label, - wxDefaultPosition, wxDefaultSize, - flags); + bool showsBitmap = false; + if ( m_chkBitmapOnly->GetValue() ) + { + showsBitmap = true; + + wxBitmapButton *bbtn = new wxBitmapButton(this, ButtonPage_Button, + CreateBitmap(wxT("normal"))); + if ( m_chkUsePressed->GetValue() ) + bbtn->SetBitmapPressed(CreateBitmap(wxT("pushed"))); + if ( m_chkUseFocused->GetValue() ) + bbtn->SetBitmapFocus(CreateBitmap(wxT("focused"))); + if ( m_chkUseCurrent->GetValue() ) + bbtn->SetBitmapCurrent(CreateBitmap(wxT("hover"))); + if ( m_chkUseDisabled->GetValue() ) + bbtn->SetBitmapDisabled(CreateBitmap(wxT("disabled"))); + m_button = bbtn; + } + else // normal button + { + m_button = new wxButton(this, ButtonPage_Button, label, + wxDefaultPosition, wxDefaultSize, + flags); + } -#ifdef __WXUNIVERSAL__ - if ( m_chkImage->GetValue() ) + if ( !showsBitmap && m_chkTextAndBitmap->GetValue() ) { - m_button->SetImageLabel(wxTheApp->GetStdIcon(wxICON_INFORMATION)); + showsBitmap = true; + + static const wxDirection positions[] = + { + wxLEFT, wxRIGHT, wxTOP, wxBOTTOM + }; + + m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION), + positions[m_radioImagePos->GetSelection()]); + + if ( m_chkUsePressed->GetValue() ) + m_button->SetBitmapPressed(wxArtProvider::GetIcon(wxART_HELP)); + if ( m_chkUseFocused->GetValue() ) + m_button->SetBitmapFocus(wxArtProvider::GetIcon(wxART_ERROR)); + if ( m_chkUseCurrent->GetValue() ) + m_button->SetBitmapCurrent(wxArtProvider::GetIcon(wxART_WARNING)); + if ( m_chkUseDisabled->GetValue() ) + m_button->SetBitmapDisabled(wxArtProvider::GetIcon(wxART_MISSING_IMAGE)); } -#endif // wxUniv + + m_chkUsePressed->Enable(showsBitmap); + m_chkUseFocused->Enable(showsBitmap); + m_chkUseCurrent->Enable(showsBitmap); + m_chkUseDisabled->Enable(showsBitmap); + + if ( m_chkAuthNeeded->GetValue() ) + m_button->SetAuthNeeded(); if ( m_chkDefault->GetValue() ) - { m_button->SetDefault(); - } + AddButtonToSizer(); + + m_sizerButton->Layout(); +} + +void ButtonWidgetsPage::AddButtonToSizer() +{ if ( m_chkFit->GetValue() ) { - m_sizerButton->Add(0, 0, 1, wxCENTRE); - m_sizerButton->Add(m_button, 1, wxCENTRE); - m_sizerButton->Add(0, 0, 1, wxCENTRE); + m_sizerButton->AddStretchSpacer(1); + m_sizerButton->Add(m_button, wxSizerFlags(0).Centre().Border()); + m_sizerButton->AddStretchSpacer(1); } - else + else // take up the entire space { - m_sizerButton->Add(m_button, 1, wxGROW | wxALL, 5); + m_sizerButton->Add(m_button, wxSizerFlags(1).Expand().Border()); } - - m_sizerButton->Layout(); } // ---------------------------------------------------------------------------- @@ -355,7 +472,7 @@ void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) CreateButton(); } -void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event) +void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event)) { CreateButton(); } @@ -363,10 +480,33 @@ void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event) void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event)) { m_button->SetLabel(m_textLabel->GetValue()); + + m_sizerButton->Layout(); } -void ButtonWidgetsPage::OnButton(wxCommandEvent& event) +void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event)) +{ + wxLogMessage(wxT("Test button clicked.")); +} + +// ---------------------------------------------------------------------------- +// bitmap button stuff +// ---------------------------------------------------------------------------- + +wxBitmap ButtonWidgetsPage::CreateBitmap(const wxString& label) { - wxLogMessage(_T("Test button clicked.")); + wxBitmap bmp(180, 70); // shouldn't hardcode but it's simpler like this + wxMemoryDC dc; + dc.SelectObject(bmp); + dc.SetBackground(wxBrush(*wxCYAN)); + dc.Clear(); + dc.SetTextForeground(*wxBLACK); + dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + wxT("\n") + wxT("(") + label + wxT(" state)"), + wxArtProvider::GetBitmap(wxART_INFORMATION), + wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20), + wxALIGN_CENTRE); + + return bmp; }