]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/widgets/button.cpp
fixing infinite recursion for rotated text, introduced in cleanup r57915
[wxWidgets.git] / samples / widgets / button.cpp
index ebe6d458878e6053d6b75a84bd44eebfcf317907..914fa1a08b347d7eb210c85dcb5ed7d41d34b660 100644 (file)
@@ -29,6 +29,7 @@
     #include "wx/app.h"
     #include "wx/log.h"
 
     #include "wx/app.h"
     #include "wx/log.h"
 
+    #include "wx/bmpbuttn.h"
     #include "wx/button.h"
     #include "wx/checkbox.h"
     #include "wx/radiobox.h"
     #include "wx/button.h"
     #include "wx/checkbox.h"
     #include "wx/radiobox.h"
@@ -38,6 +39,7 @@
 
 #include "wx/artprov.h"
 #include "wx/sizer.h"
 
 #include "wx/artprov.h"
 #include "wx/sizer.h"
+#include "wx/dcmemory.h"
 
 #include "widgets.h"
 
 
 #include "widgets.h"
 
@@ -50,7 +52,7 @@
 // control ids
 enum
 {
 // control ids
 enum
 {
-    ButtonPage_Reset = 100,
+    ButtonPage_Reset = wxID_HIGHEST,
     ButtonPage_ChangeLabel,
     ButtonPage_Button
 };
     ButtonPage_ChangeLabel,
     ButtonPage_Button
 };
@@ -77,9 +79,15 @@ enum
 class ButtonWidgetsPage : public WidgetsPage
 {
 public:
 class ButtonWidgetsPage : public WidgetsPage
 {
 public:
-    ButtonWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
+    ButtonWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
     virtual ~ButtonWidgetsPage(){};
 
     virtual ~ButtonWidgetsPage(){};
 
+    virtual wxControl *GetWidget() const { return m_button; }
+    virtual void RecreateWidget() { CreateButton(); }
+
+    // lazy creation of the content
+    virtual void CreateContent();
+
 protected:
     // event handlers
     void OnCheckOrRadioBox(wxCommandEvent& event);
 protected:
     // event handlers
     void OnCheckOrRadioBox(wxCommandEvent& event);
@@ -94,14 +102,28 @@ protected:
     // (re)create the wxButton
     void CreateButton();
 
     // (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
     // the controls
     // ------------
 
     // the check/radio boxes for styles
-    wxCheckBox *m_chkImage,
+    wxCheckBox *m_chkBitmap,
+               *m_chkImage,
                *m_chkFit,
                *m_chkDefault;
 
                *m_chkFit,
                *m_chkDefault;
 
+    // more checkboxes for wxBitmapButton only
+    wxCheckBox *m_chkUseSelected,
+               *m_chkUseFocused,
+               *m_chkUseHover,
+               *m_chkUseDisabled;
+
     wxRadioBox *m_radioHAlign,
                *m_radioVAlign;
 
     wxRadioBox *m_radioHAlign,
                *m_radioVAlign;
 
@@ -135,18 +157,27 @@ END_EVENT_TABLE()
 // implementation
 // ============================================================================
 
 // implementation
 // ============================================================================
 
-IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, _T("Button"));
+#if defined(__WXUNIVERSAL__)
+    #define FAMILY_CTRLS UNIVERSAL_CTRLS
+#else
+    #define FAMILY_CTRLS NATIVE_CTRLS
+#endif
+
+IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, _T("Button"), FAMILY_CTRLS );
 
 
-ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook,
-                                       wxImageList *imaglist)
-                  : WidgetsPage(notebook)
+ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl *book,
+                                     wxImageList *imaglist)
+                  : WidgetsPage(book, imaglist, button_xpm)
 {
 {
-    imaglist->Add(wxBitmap(button_xpm));
-
     // init everything
     // init everything
+    m_chkBitmap =
     m_chkImage =
     m_chkFit =
     m_chkImage =
     m_chkFit =
-    m_chkDefault = (wxCheckBox *)NULL;
+    m_chkDefault =
+    m_chkUseSelected =
+    m_chkUseFocused =
+    m_chkUseHover =
+    m_chkUseDisabled = (wxCheckBox *)NULL;
 
     m_radioHAlign =
     m_radioVAlign = (wxRadioBox *)NULL;
 
     m_radioHAlign =
     m_radioVAlign = (wxRadioBox *)NULL;
@@ -155,7 +186,10 @@ ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook,
 
     m_button = (wxButton *)NULL;
     m_sizerButton = (wxSizer *)NULL;
 
     m_button = (wxButton *)NULL;
     m_sizerButton = (wxSizer *)NULL;
+}
 
 
+void ButtonWidgetsPage::CreateContent()
+{
     wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
 
     // left pane
     wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
 
     // left pane
@@ -163,6 +197,7 @@ ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook,
 
     wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
 
 
     wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
 
+    m_chkBitmap = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Bitmap button"));
     m_chkImage = CreateCheckBoxAndAddToSizer(sizerLeft, _T("With &image"));
     m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit exactly"));
     m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Default"));
     m_chkImage = CreateCheckBoxAndAddToSizer(sizerLeft, _T("With &image"));
     m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit exactly"));
     m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Default"));
@@ -172,7 +207,17 @@ ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook,
     m_chkImage->Disable();
 #endif // !wxUniv
 
     m_chkImage->Disable();
 #endif // !wxUniv
 
-    sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
+    sizerLeft->AddSpacer(5);
+
+    wxSizer *sizerUseLabels =
+        new wxStaticBoxSizer(wxVERTICAL, this, _T("&Use the following labels?"));
+    m_chkUseSelected = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Pushed"));
+    m_chkUseFocused = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Focused"));
+    m_chkUseHover = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Hover"));
+    m_chkUseDisabled = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Disabled"));
+    sizerLeft->Add(sizerUseLabels, wxSizerFlags().Expand().Border());
+
+    sizerLeft->AddSpacer(15);
 
     // should be in sync with enums Button[HV]Align!
     static const wxString halign[] =
 
     // should be in sync with enums Button[HV]Align!
     static const wxString halign[] =
@@ -212,29 +257,24 @@ ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook,
                                                      _T("Change label"),
                                                      wxID_ANY,
                                                      &m_textLabel);
                                                      _T("Change label"),
                                                      wxID_ANY,
                                                      &m_textLabel);
+    m_textLabel->SetValue(_T("&Press me!"));
 
     sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
 
     // right pane
 
     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(150, 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);
 
     // 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();
     Reset();
+    CreateButton();
 
     SetSizer(sizerTop);
 
     SetSizer(sizerTop);
-
-    sizerTop->Fit(this);
 }
 
 // ----------------------------------------------------------------------------
 }
 
 // ----------------------------------------------------------------------------
@@ -243,10 +283,16 @@ ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook,
 
 void ButtonWidgetsPage::Reset()
 {
 
 void ButtonWidgetsPage::Reset()
 {
+    m_chkBitmap->SetValue(false);
     m_chkFit->SetValue(true);
     m_chkImage->SetValue(false);
     m_chkDefault->SetValue(false);
 
     m_chkFit->SetValue(true);
     m_chkImage->SetValue(false);
     m_chkDefault->SetValue(false);
 
+    m_chkUseSelected->SetValue(true);
+    m_chkUseFocused->SetValue(true);
+    m_chkUseHover->SetValue(true);
+    m_chkUseDisabled->SetValue(true);
+
     m_radioHAlign->SetSelection(ButtonHAlign_Centre);
     m_radioVAlign->SetSelection(ButtonVAlign_Centre);
 }
     m_radioHAlign->SetSelection(ButtonHAlign_Centre);
     m_radioVAlign->SetSelection(ButtonVAlign_Centre);
 }
@@ -266,12 +312,15 @@ void ButtonWidgetsPage::CreateButton()
 
         delete m_button;
     }
 
         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:
     switch ( m_radioHAlign->GetSelection() )
     {
         case ButtonHAlign_Left:
@@ -301,7 +350,7 @@ void ButtonWidgetsPage::CreateButton()
             // fall through
 
         case ButtonVAlign_Centre:
             // fall through
 
         case ButtonVAlign_Centre:
-            flags |= wxALIGN_CENTRE_VERTICAL;
+            // centre vertical alignment is the default (no style)
             break;
 
         case ButtonVAlign_Bottom:
             break;
 
         case ButtonVAlign_Bottom:
@@ -309,9 +358,32 @@ void ButtonWidgetsPage::CreateButton()
             break;
     }
 
             break;
     }
 
-    m_button = new wxButton(this, ButtonPage_Button, label,
-                            wxDefaultPosition, wxDefaultSize,
-                            flags);
+    const bool isBitmapButton = m_chkBitmap->GetValue();
+    if ( isBitmapButton )
+    {
+        wxBitmapButton *bbtn = new wxBitmapButton(this, ButtonPage_Button,
+                                                  CreateBitmap(_T("normal")));
+        if ( m_chkUseSelected->GetValue() )
+            bbtn->SetBitmapSelected(CreateBitmap(_T("pushed")));
+        if ( m_chkUseFocused->GetValue() )
+            bbtn->SetBitmapFocus(CreateBitmap(_T("focused")));
+        if ( m_chkUseHover->GetValue() )
+            bbtn->SetBitmapHover(CreateBitmap(_T("hover")));
+        if ( m_chkUseDisabled->GetValue() )
+            bbtn->SetBitmapDisabled(CreateBitmap(_T("disabled")));
+        m_button = bbtn;
+    }
+    else // normal button
+    {
+        m_button = new wxButton(this, ButtonPage_Button, label,
+                                wxDefaultPosition, wxDefaultSize,
+                                flags);
+    }
+
+    m_chkUseSelected->Enable(isBitmapButton);
+    m_chkUseFocused->Enable(isBitmapButton);
+    m_chkUseHover->Enable(isBitmapButton);
+    m_chkUseDisabled->Enable(isBitmapButton);
 
 #ifdef __WXUNIVERSAL__
     if ( m_chkImage->GetValue() )
 
 #ifdef __WXUNIVERSAL__
     if ( m_chkImage->GetValue() )
@@ -325,18 +397,23 @@ void ButtonWidgetsPage::CreateButton()
         m_button->SetDefault();
     }
 
         m_button->SetDefault();
     }
 
+    AddButtonToSizer();
+
+    m_sizerButton->Layout();
+}
+
+void ButtonWidgetsPage::AddButtonToSizer()
+{
     if ( m_chkFit->GetValue() )
     {
     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();
 }
 
 // ----------------------------------------------------------------------------
 }
 
 // ----------------------------------------------------------------------------
@@ -365,3 +442,24 @@ void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
     wxLogMessage(_T("Test button clicked."));
 }
 
     wxLogMessage(_T("Test button clicked."));
 }
 
+// ----------------------------------------------------------------------------
+// bitmap button stuff
+// ----------------------------------------------------------------------------
+
+wxBitmap ButtonWidgetsPage::CreateBitmap(const wxString& label)
+{
+    wxBitmap bmp(180, 70); // shouldn't hardcode but it's simpler like this
+    wxMemoryDC dc;
+    dc.SelectObject(bmp);
+    dc.SetBackground(wxBrush(*wxWHITE));
+    dc.Clear();
+    dc.SetTextForeground(*wxBLUE);
+    dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + _T("\n")
+                    _T("(") + label + _T(" state)"),
+                 wxArtProvider::GetBitmap(wxART_INFORMATION),
+                 wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20),
+                 wxALIGN_CENTRE);
+
+    return bmp;
+}
+