editlbox.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: editlbox.cpp
  4. // Purpose: Part of the widgets sample showing wxEditableListbox
  5. // Author: Francesco Montorsi
  6. // Created: 8/2/2009
  7. // Copyright: (c) 2009 Francesco Montorsi
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // for compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #if wxUSE_EDITABLELISTBOX
  22. // for all others, include the necessary headers
  23. #ifndef WX_PRECOMP
  24. #include "wx/log.h"
  25. #include "wx/bitmap.h"
  26. #include "wx/button.h"
  27. #include "wx/checkbox.h"
  28. #include "wx/combobox.h"
  29. #include "wx/listbox.h"
  30. #include "wx/radiobox.h"
  31. #include "wx/statbox.h"
  32. #include "wx/textctrl.h"
  33. #endif
  34. #include "wx/sizer.h"
  35. #include "wx/editlbox.h"
  36. #include "wx/listctrl.h"
  37. #include "itemcontainer.h"
  38. #include "widgets.h"
  39. #include "icons/listbox.xpm"
  40. // ----------------------------------------------------------------------------
  41. // constants
  42. // ----------------------------------------------------------------------------
  43. // control ids
  44. enum
  45. {
  46. EditableListboxPage_Reset = wxID_HIGHEST,
  47. EditableListboxPage_Listbox,
  48. EditableListboxPage_ContainerTests
  49. };
  50. // ----------------------------------------------------------------------------
  51. // EditableListboxWidgetsPage
  52. // ----------------------------------------------------------------------------
  53. class EditableListboxWidgetsPage : public WidgetsPage
  54. {
  55. public:
  56. EditableListboxWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  57. virtual wxControl *GetWidget() const { return m_lbox->GetListCtrl(); }
  58. virtual void RecreateWidget() { CreateLbox(); }
  59. // lazy creation of the content
  60. virtual void CreateContent();
  61. protected:
  62. // event handlers
  63. void OnButtonReset(wxCommandEvent& event);
  64. void OnCheckBox(wxCommandEvent& event);
  65. // reset the listbox parameters
  66. void Reset();
  67. // (re)create the listbox
  68. void CreateLbox();
  69. // the checkboxes
  70. wxCheckBox *m_chkAllowNew,
  71. *m_chkAllowEdit,
  72. *m_chkAllowDelete,
  73. *m_chkAllowNoReorder;
  74. wxEditableListBox
  75. *m_lbox;
  76. wxSizer *m_sizerLbox;
  77. private:
  78. wxDECLARE_EVENT_TABLE();
  79. DECLARE_WIDGETS_PAGE(EditableListboxWidgetsPage)
  80. };
  81. // ----------------------------------------------------------------------------
  82. // event tables
  83. // ----------------------------------------------------------------------------
  84. wxBEGIN_EVENT_TABLE(EditableListboxWidgetsPage, WidgetsPage)
  85. EVT_BUTTON(EditableListboxPage_Reset, EditableListboxWidgetsPage::OnButtonReset)
  86. EVT_CHECKBOX(wxID_ANY, EditableListboxWidgetsPage::OnCheckBox)
  87. wxEND_EVENT_TABLE()
  88. // ============================================================================
  89. // implementation
  90. // ============================================================================
  91. IMPLEMENT_WIDGETS_PAGE(EditableListboxWidgetsPage, wxT("EditableListbox"), GENERIC_CTRLS);
  92. EditableListboxWidgetsPage::EditableListboxWidgetsPage(WidgetsBookCtrl *book,
  93. wxImageList *imaglist)
  94. : WidgetsPage(book, imaglist, listbox_xpm)
  95. {
  96. }
  97. void EditableListboxWidgetsPage::CreateContent()
  98. {
  99. /*
  100. What we create here is a frame having 2 panes: style pane is the
  101. leftmost one and the pane containing the listbox itself to the right
  102. */
  103. wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
  104. // left pane
  105. wxStaticBox *box = new wxStaticBox(this, wxID_ANY,
  106. wxT("&Set listbox parameters"));
  107. wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
  108. m_chkAllowNew = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Allow new items"));
  109. m_chkAllowEdit = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Allow editing items"));
  110. m_chkAllowDelete = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Allow deleting items"));
  111. m_chkAllowNoReorder = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Block user reordering"));
  112. wxButton *btn = new wxButton(this, EditableListboxPage_Reset, wxT("&Reset"));
  113. sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  114. // right pane
  115. wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
  116. m_lbox = new wxEditableListBox(this, EditableListboxPage_Listbox,
  117. _("Match these wildcards:"),
  118. wxDefaultPosition, wxDefaultSize, 0);
  119. sizerRight->Add(m_lbox, 1, wxGROW | wxALL, 5);
  120. sizerRight->SetMinSize(150, 0);
  121. m_sizerLbox = sizerRight; // save it to modify it later
  122. // the 3 panes panes compose the window
  123. sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
  124. sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
  125. // final initializations
  126. Reset();
  127. SetSizer(sizerTop);
  128. }
  129. // ----------------------------------------------------------------------------
  130. // operations
  131. // ----------------------------------------------------------------------------
  132. void EditableListboxWidgetsPage::Reset()
  133. {
  134. m_chkAllowNew->SetValue(false);
  135. m_chkAllowEdit->SetValue(false);
  136. m_chkAllowDelete->SetValue(false);
  137. m_chkAllowNoReorder->SetValue(false);
  138. }
  139. void EditableListboxWidgetsPage::CreateLbox()
  140. {
  141. int flags = 0;
  142. if ( m_chkAllowNew->GetValue() )
  143. flags |= wxEL_ALLOW_NEW;
  144. if ( m_chkAllowEdit->GetValue() )
  145. flags |= wxEL_ALLOW_EDIT;
  146. if ( m_chkAllowDelete->GetValue() )
  147. flags |= wxEL_ALLOW_DELETE;
  148. if ( m_chkAllowNoReorder->GetValue() )
  149. flags |= wxEL_NO_REORDER;
  150. wxArrayString items;
  151. if ( m_lbox )
  152. {
  153. m_lbox->GetStrings(items);
  154. m_sizerLbox->Detach( m_lbox );
  155. delete m_lbox;
  156. }
  157. m_lbox = new wxEditableListBox(this, EditableListboxPage_Listbox,
  158. _("Match these wildcards:"),
  159. wxDefaultPosition, wxDefaultSize,
  160. flags);
  161. m_lbox->SetStrings(items);
  162. m_sizerLbox->Add(m_lbox, 1, wxGROW | wxALL, 5);
  163. m_sizerLbox->Layout();
  164. }
  165. // ----------------------------------------------------------------------------
  166. // event handlers
  167. // ----------------------------------------------------------------------------
  168. void EditableListboxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  169. {
  170. Reset();
  171. CreateLbox();
  172. }
  173. void EditableListboxWidgetsPage::OnCheckBox(wxCommandEvent& WXUNUSED(event))
  174. {
  175. CreateLbox();
  176. }
  177. #endif // wxUSE_EDITABLELISTBOX