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