]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/widgets/button.cpp
Use template class instead of template function in wxVectorSort().
[wxWidgets.git] / samples / widgets / button.cpp
index 4a17b65be1d21ad794c65e1eefb4d0edc302a05c..af70f76ea085f84a5f68a20a1776376cc6eb3cc5 100644 (file)
 // 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,
@@ -79,10 +87,14 @@ enum
 class ButtonWidgetsPage : public WidgetsPage
 {
 public:
-    ButtonWidgetsPage(wxBookCtrlBase *book, wxImageList *imaglist);
+    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
@@ -109,17 +121,21 @@ protected:
     // ------------
 
     // the check/radio boxes for styles
-    wxCheckBox *m_chkBitmap,
-               *m_chkImage,
+    wxCheckBox *m_chkBitmapOnly,
+               *m_chkTextAndBitmap,
                *m_chkFit,
+               *m_chkAuthNeeded,
                *m_chkDefault;
 
     // more checkboxes for wxBitmapButton only
-    wxCheckBox *m_chkUseSelected,
+    wxCheckBox *m_chkUsePressed,
                *m_chkUseFocused,
-               *m_chkUseHover,
+               *m_chkUseCurrent,
                *m_chkUseDisabled;
 
+    // and an image position choice used if m_chkTextAndBitmap is on
+    wxRadioBox *m_radioImagePos;
+
     wxRadioBox *m_radioHAlign,
                *m_radioVAlign;
 
@@ -153,24 +169,30 @@ 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
+
+IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, wxT("Button"), FAMILY_CTRLS );
 
-ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrlBase *book,
+ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl *book,
                                      wxImageList *imaglist)
-                  : WidgetsPage(book)
+                  : WidgetsPage(book, imaglist, button_xpm)
 {
-    imaglist->Add(wxBitmap(button_xpm));
-
     // init everything
-    m_chkBitmap =
-    m_chkImage =
+    m_chkBitmapOnly =
+    m_chkTextAndBitmap =
     m_chkFit =
+    m_chkAuthNeeded =
     m_chkDefault =
-    m_chkUseSelected =
+    m_chkUsePressed =
     m_chkUseFocused =
-    m_chkUseHover =
+    m_chkUseCurrent =
     m_chkUseDisabled = (wxCheckBox *)NULL;
 
+    m_radioImagePos =
     m_radioHAlign =
     m_radioVAlign = (wxRadioBox *)NULL;
 
@@ -178,55 +200,69 @@ ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrlBase *book,
 
     m_button = (wxButton *)NULL;
     m_sizerButton = (wxSizer *)NULL;
+}
 
+void ButtonWidgetsPage::CreateContent()
+{
     wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
 
     // left pane
-    wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
+    wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
 
     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"));
-
-#ifndef __WXUNIVERSAL__
-    // only wxUniv currently supports buttons with images
-    m_chkImage->Disable();
-#endif // !wxUniv
+    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, _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"));
+        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, wxID_ANY, _T("&Horz alignment"),
+    m_radioHAlign = new wxRadioBox(this, wxID_ANY, wxT("&Horz alignment"),
                                    wxDefaultPosition, wxDefaultSize,
                                    WXSIZEOF(halign), halign);
-    m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"),
+    m_radioVAlign = new wxRadioBox(this, wxID_ANY, wxT("&Vert alignment"),
                                    wxDefaultPosition, wxDefaultSize,
                                    WXSIZEOF(valign), valign);
 
@@ -235,18 +271,18 @@ ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrlBase *book,
 
     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, wxID_ANY, _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"),
+                                                     wxT("Change label"),
                                                      wxID_ANY,
                                                      &m_textLabel);
-    m_textLabel->SetValue(_T("&Press me!"));
+    m_textLabel->SetValue(wxT("&Press me!"));
 
     sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
 
@@ -264,8 +300,6 @@ ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrlBase *book,
     CreateButton();
 
     SetSizer(sizerTop);
-
-    sizerTop->Fit(this);
 }
 
 // ----------------------------------------------------------------------------
@@ -274,16 +308,18 @@ ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrlBase *book,
 
 void ButtonWidgetsPage::Reset()
 {
-    m_chkBitmap->SetValue(false);
+    m_chkBitmapOnly->SetValue(false);
     m_chkFit->SetValue(true);
-    m_chkImage->SetValue(false);
+    m_chkAuthNeeded->SetValue(false);
+    m_chkTextAndBitmap->SetValue(false);
     m_chkDefault->SetValue(false);
 
-    m_chkUseSelected->SetValue(true);
+    m_chkUsePressed->SetValue(true);
     m_chkUseFocused->SetValue(true);
-    m_chkUseHover->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);
 }
@@ -311,7 +347,7 @@ void ButtonWidgetsPage::CreateButton()
         label = m_textLabel->GetValue();
     }
 
-    int flags = 0;
+    int flags = ms_defaultFlags;
     switch ( m_radioHAlign->GetSelection() )
     {
         case ButtonHAlign_Left:
@@ -319,7 +355,7 @@ void ButtonWidgetsPage::CreateButton()
             break;
 
         default:
-            wxFAIL_MSG(_T("unexpected radiobox selection"));
+            wxFAIL_MSG(wxT("unexpected radiobox selection"));
             // fall through
 
         case ButtonHAlign_Centre:
@@ -337,11 +373,11 @@ void ButtonWidgetsPage::CreateButton()
             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:
@@ -349,19 +385,21 @@ void ButtonWidgetsPage::CreateButton()
             break;
     }
 
-    const bool isBitmapButton = m_chkBitmap->GetValue();
-    if ( isBitmapButton )
+    bool showsBitmap = false;
+    if ( m_chkBitmapOnly->GetValue() )
     {
+        showsBitmap = true;
+
         wxBitmapButton *bbtn = new wxBitmapButton(this, ButtonPage_Button,
-                                                  CreateBitmap(_T("normal")));
-        if ( m_chkUseSelected->GetValue() )
-            bbtn->SetBitmapSelected(CreateBitmap(_T("pushed")));
+                                                  CreateBitmap(wxT("normal")));
+        if ( m_chkUsePressed->GetValue() )
+            bbtn->SetBitmapPressed(CreateBitmap(wxT("pushed")));
         if ( m_chkUseFocused->GetValue() )
-            bbtn->SetBitmapFocus(CreateBitmap(_T("focused")));
-        if ( m_chkUseHover->GetValue() )
-            bbtn->SetBitmapHover(CreateBitmap(_T("hover")));
+            bbtn->SetBitmapFocus(CreateBitmap(wxT("focused")));
+        if ( m_chkUseCurrent->GetValue() )
+            bbtn->SetBitmapCurrent(CreateBitmap(wxT("hover")));
         if ( m_chkUseDisabled->GetValue() )
-            bbtn->SetBitmapDisabled(CreateBitmap(_T("disabled")));
+            bbtn->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
         m_button = bbtn;
     }
     else // normal button
@@ -371,22 +409,38 @@ void ButtonWidgetsPage::CreateButton()
                                 flags);
     }
 
-    m_chkUseSelected->Enable(isBitmapButton);
-    m_chkUseFocused->Enable(isBitmapButton);
-    m_chkUseHover->Enable(isBitmapButton);
-    m_chkUseDisabled->Enable(isBitmapButton);
-
-#ifdef __WXUNIVERSAL__
-    if ( m_chkImage->GetValue() )
+    if ( !showsBitmap && m_chkTextAndBitmap->GetValue() )
     {
-        m_button->SetImageLabel(wxArtProvider::GetIcon(wxART_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();
 
@@ -426,11 +480,13 @@ void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
 {
     m_button->SetLabel(m_textLabel->GetValue());
+
+    m_sizerButton->Layout();
 }
 
 void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
 {
-    wxLogMessage(_T("Test button clicked."));
+    wxLogMessage(wxT("Test button clicked."));
 }
 
 // ----------------------------------------------------------------------------
@@ -442,11 +498,11 @@ 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.SetBackground(wxBrush(*wxCYAN));
     dc.Clear();
-    dc.SetTextForeground(*wxBLUE);
-    dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + _T("\n")
-                    _T("(") + label + _T(" state)"),
+    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);