home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2240.zip / wxWindows-2.4.0 / samples / widgets / button.cpp next >
C/C++ Source or Header  |  2002-09-02  |  10KB  |  373 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program:     wxWindows Widgets Sample
  3. // Name:        button.cpp
  4. // Purpose:     Part of the widgets sample showing wxButton
  5. // Author:      Vadim Zeitlin
  6. // Created:     10.04.01
  7. // Id:          $Id: button.cpp,v 1.7 2002/09/02 12:29:44 VZ Exp $
  8. // Copyright:   (c) 2001 Vadim Zeitlin
  9. // License:     wxWindows license
  10. /////////////////////////////////////////////////////////////////////////////
  11.  
  12. // ============================================================================
  13. // declarations
  14. // ============================================================================
  15.  
  16. // ----------------------------------------------------------------------------
  17. // headers
  18. // ----------------------------------------------------------------------------
  19.  
  20. // for compilers that support precompilation, includes "wx/wx.h".
  21. #include "wx/wxprec.h"
  22.  
  23. #ifdef __BORLANDC__
  24.     #pragma hdrstop
  25. #endif
  26.  
  27. // for all others, include the necessary headers
  28. #ifndef WX_PRECOMP
  29.     #include "wx/app.h"
  30.     #include "wx/log.h"
  31.  
  32.     #include "wx/button.h"
  33.     #include "wx/checkbox.h"
  34.     #include "wx/radiobox.h"
  35.     #include "wx/statbox.h"
  36.     #include "wx/textctrl.h"
  37. #endif
  38.  
  39. #include "wx/artprov.h"
  40. #include "wx/sizer.h"
  41.  
  42. #include "widgets.h"
  43.  
  44. #include "icons/button.xpm"
  45.  
  46. // ----------------------------------------------------------------------------
  47. // constants
  48. // ----------------------------------------------------------------------------
  49.  
  50. // control ids
  51. enum
  52. {
  53.     ButtonPage_Reset = 100,
  54.     ButtonPage_ChangeLabel,
  55.     ButtonPage_Button
  56. };
  57.  
  58. // radio boxes
  59. enum
  60. {
  61.     ButtonHAlign_Left,
  62.     ButtonHAlign_Centre,
  63.     ButtonHAlign_Right
  64. };
  65.  
  66. enum
  67. {
  68.     ButtonVAlign_Top,
  69.     ButtonVAlign_Centre,
  70.     ButtonVAlign_Bottom
  71. };
  72.  
  73. // ----------------------------------------------------------------------------
  74. // ButtonWidgetsPage
  75. // ----------------------------------------------------------------------------
  76.  
  77. class ButtonWidgetsPage : public WidgetsPage
  78. {
  79. public:
  80.     ButtonWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
  81.     virtual ~ButtonWidgetsPage();
  82.  
  83. protected:
  84.     // event handlers
  85.     void OnCheckOrRadioBox(wxCommandEvent& event);
  86.  
  87.     void OnButton(wxCommandEvent& event);
  88.     void OnButtonReset(wxCommandEvent& event);
  89.     void OnButtonChangeLabel(wxCommandEvent& event);
  90.  
  91.     // reset the wxButton parameters
  92.     void Reset();
  93.  
  94.     // (re)create the wxButton
  95.     void CreateButton();
  96.  
  97.     // the controls
  98.     // ------------
  99.  
  100.     // the check/radio boxes for styles
  101.     wxCheckBox *m_chkImage,
  102.                *m_chkFit,
  103.                *m_chkDefault;
  104.  
  105.     wxRadioBox *m_radioHAlign,
  106.                *m_radioVAlign;
  107.  
  108.     // the gauge itself and the sizer it is in
  109.     wxButton *m_button;
  110.     wxSizer *m_sizerButton;
  111.  
  112.     // the text entries for command parameters
  113.     wxTextCtrl *m_textLabel;
  114.  
  115. private:
  116.     DECLARE_EVENT_TABLE()
  117.     DECLARE_WIDGETS_PAGE(ButtonWidgetsPage)
  118. };
  119.  
  120. // ----------------------------------------------------------------------------
  121. // event tables
  122. // ----------------------------------------------------------------------------
  123.  
  124. BEGIN_EVENT_TABLE(ButtonWidgetsPage, WidgetsPage)
  125.     EVT_BUTTON(ButtonPage_Button, ButtonWidgetsPage::OnButton)
  126.  
  127.     EVT_BUTTON(ButtonPage_Reset, ButtonWidgetsPage::OnButtonReset)
  128.     EVT_BUTTON(ButtonPage_ChangeLabel, ButtonWidgetsPage::OnButtonChangeLabel)
  129.  
  130.     EVT_CHECKBOX(-1, ButtonWidgetsPage::OnCheckOrRadioBox)
  131.     EVT_RADIOBOX(-1, ButtonWidgetsPage::OnCheckOrRadioBox)
  132. END_EVENT_TABLE()
  133.  
  134. // ============================================================================
  135. // implementation
  136. // ============================================================================
  137.  
  138. IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, _T("Button"));
  139.  
  140. ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook,
  141.                                        wxImageList *imaglist)
  142.                   : WidgetsPage(notebook)
  143. {
  144.     imaglist->Add(wxBitmap(button_xpm));
  145.  
  146.     // init everything
  147.     m_chkImage =
  148.     m_chkFit =
  149.     m_chkDefault = (wxCheckBox *)NULL;
  150.  
  151.     m_radioHAlign =
  152.     m_radioVAlign = (wxRadioBox *)NULL;
  153.  
  154.     m_textLabel = (wxTextCtrl *)NULL;
  155.  
  156.     m_button = (wxButton *)NULL;
  157.     m_sizerButton = (wxSizer *)NULL;
  158.  
  159.     wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
  160.  
  161.     // left pane
  162.     wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
  163.  
  164.     wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
  165.  
  166.     m_chkImage = CreateCheckBoxAndAddToSizer(sizerLeft, _T("With &image"));
  167.     m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit exactly"));
  168.     m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Default"));
  169.  
  170. #ifndef __WXUNIVERSAL__
  171.     // only wxUniv currently supoprts buttons with images
  172.     m_chkImage->Disable();
  173. #endif // !wxUniv
  174.  
  175.     sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
  176.  
  177.     // should be in sync with enums Button[HV]Align!
  178.     static const wxString halign[] =
  179.     {
  180.         _T("left"),
  181.         _T("centre"),
  182.         _T("right"),
  183.     };
  184.  
  185.     static const wxString valign[] =
  186.     {
  187.         _T("top"),
  188.         _T("centre"),
  189.         _T("bottom"),
  190.     };
  191.  
  192.     m_radioHAlign = new wxRadioBox(this, -1, _T("&Horz alignment"),
  193.                                    wxDefaultPosition, wxDefaultSize,
  194.                                    WXSIZEOF(halign), halign);
  195.     m_radioVAlign = new wxRadioBox(this, -1, _T("&Vert alignment"),
  196.                                    wxDefaultPosition, wxDefaultSize,
  197.                                    WXSIZEOF(valign), valign);
  198.  
  199.     sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
  200.     sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
  201.  
  202.     sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
  203.  
  204.     wxButton *btn = new wxButton(this, ButtonPage_Reset, _T("&Reset"));
  205.     sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  206.  
  207.     // middle pane
  208.     wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Operations"));
  209.     wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
  210.  
  211.     wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
  212.                                                      _T("Change label"),
  213.                                                      -1,
  214.                                                      &m_textLabel);
  215.  
  216.     sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
  217.  
  218.     // right pane
  219.     wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
  220.     m_button = new wxButton(this, ButtonPage_Button, _T("&Press me!"));
  221.     sizerRight->Add(0, 0, 1, wxCENTRE);
  222.     sizerRight->Add(m_button, 1, wxCENTRE);
  223.     sizerRight->Add(0, 0, 1, wxCENTRE);
  224.     sizerRight->SetMinSize(150, 0);
  225.     m_sizerButton = sizerRight; // save it to modify it later
  226.  
  227.     // the 3 panes panes compose the window
  228.     sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
  229.     sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
  230.     sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
  231.  
  232.     // final initializations
  233.     Reset();
  234.  
  235.     SetAutoLayout(TRUE);
  236.     SetSizer(sizerTop);
  237.  
  238.     sizerTop->Fit(this);
  239. }
  240.  
  241. ButtonWidgetsPage::~ButtonWidgetsPage()
  242. {
  243. }
  244.  
  245. // ----------------------------------------------------------------------------
  246. // operations
  247. // ----------------------------------------------------------------------------
  248.  
  249. void ButtonWidgetsPage::Reset()
  250. {
  251.     m_chkFit->SetValue(TRUE);
  252.     m_chkImage->SetValue(FALSE);
  253.     m_chkDefault->SetValue(FALSE);
  254.  
  255.     m_radioHAlign->SetSelection(ButtonHAlign_Centre);
  256.     m_radioVAlign->SetSelection(ButtonVAlign_Centre);
  257. }
  258.  
  259. void ButtonWidgetsPage::CreateButton()
  260. {
  261.     wxString label;
  262.     if ( m_button )
  263.     {
  264.         label = m_button->GetLabel();
  265.  
  266.         size_t count = m_sizerButton->GetChildren().GetCount();
  267.         for ( size_t n = 0; n < count; n++ )
  268.         {
  269.             m_sizerButton->Remove(0);
  270.         }
  271.  
  272.         delete m_button;
  273.     }
  274.     else
  275.     {
  276.         label = _T("&Press me!");
  277.     }
  278.  
  279.     int flags = 0;
  280.     switch ( m_radioHAlign->GetSelection() )
  281.     {
  282.         case ButtonHAlign_Left:
  283.             flags |= wxBU_LEFT;
  284.             break;
  285.  
  286.         default:
  287.             wxFAIL_MSG(_T("unexpected radiobox selection"));
  288.             // fall through
  289.  
  290.         case ButtonHAlign_Centre:
  291.             break;
  292.  
  293.         case ButtonHAlign_Right:
  294.             flags |= wxBU_RIGHT;
  295.             break;
  296.     }
  297.  
  298.     switch ( m_radioVAlign->GetSelection() )
  299.     {
  300.         case ButtonVAlign_Top:
  301.             flags |= wxBU_TOP;
  302.             break;
  303.  
  304.         default:
  305.             wxFAIL_MSG(_T("unexpected radiobox selection"));
  306.             // fall through
  307.  
  308.         case ButtonVAlign_Centre:
  309.             flags |= wxALIGN_CENTRE_VERTICAL;
  310.             break;
  311.  
  312.         case ButtonVAlign_Bottom:
  313.             flags |= wxBU_BOTTOM;
  314.             break;
  315.     }
  316.  
  317.     m_button = new wxButton(this, ButtonPage_Button, label,
  318.                             wxDefaultPosition, wxDefaultSize,
  319.                             flags);
  320.  
  321. #ifdef __WXUNIVERSAL__
  322.     if ( m_chkImage->GetValue() )
  323.     {
  324.         m_button->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION));
  325.     }
  326. #endif // wxUniv
  327.  
  328.     if ( m_chkDefault->GetValue() )
  329.     {
  330.         m_button->SetDefault();
  331.     }
  332.  
  333.     if ( m_chkFit->GetValue() )
  334.     {
  335.         m_sizerButton->Add(0, 0, 1, wxCENTRE);
  336.         m_sizerButton->Add(m_button, 1, wxCENTRE);
  337.         m_sizerButton->Add(0, 0, 1, wxCENTRE);
  338.     }
  339.     else
  340.     {
  341.         m_sizerButton->Add(m_button, 1, wxGROW | wxALL, 5);
  342.     }
  343.  
  344.     m_sizerButton->Layout();
  345. }
  346.  
  347. // ----------------------------------------------------------------------------
  348. // event handlers
  349. // ----------------------------------------------------------------------------
  350.  
  351. void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  352. {
  353.     Reset();
  354.  
  355.     CreateButton();
  356. }
  357.  
  358. void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
  359. {
  360.     CreateButton();
  361. }
  362.  
  363. void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
  364. {
  365.     m_button->SetLabel(m_textLabel->GetValue());
  366. }
  367.  
  368. void ButtonWidgetsPage::OnButton(wxCommandEvent& event)
  369. {
  370.     wxLogMessage(_T("Test button clicked."));
  371. }
  372.  
  373.