]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/searchctrl.cpp
Fix wrong in wxListCtrl::SetItemColumnImage() in r74716.
[wxWidgets.git] / samples / widgets / searchctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: searchctrl.cpp
4 // Purpose: Shows wxSearchCtrl
5 // Author: Robin Dunn
6 // Created: 9-Dec-2006
7 // Copyright: (c) 2006
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_SEARCHCTRL
27
28 // for all others, include the necessary headers
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/log.h"
32 #include "wx/radiobox.h"
33 #include "wx/statbox.h"
34 #endif
35
36 #include "wx/artprov.h"
37 #include "wx/sizer.h"
38 #include "wx/stattext.h"
39 #include "wx/checkbox.h"
40 #include "wx/menu.h"
41
42 #include "wx/srchctrl.h"
43
44 #include "widgets.h"
45
46 #include "icons/text.xpm"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // control ids
53 enum
54 {
55 ID_SEARCH_CB = wxID_HIGHEST,
56 ID_CANCEL_CB,
57 ID_MENU_CB,
58
59 ID_SEARCHMENU
60 };
61
62
63 // ----------------------------------------------------------------------------
64 // ColourPickerWidgetsPage
65 // ----------------------------------------------------------------------------
66
67 class SearchCtrlWidgetsPage : public WidgetsPage
68 {
69 public:
70 SearchCtrlWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
71 virtual ~SearchCtrlWidgetsPage(){};
72
73 virtual wxControl *GetWidget() const { return m_srchCtrl; }
74 virtual wxTextEntryBase *GetTextEntry() const { return m_srchCtrl; }
75 virtual void RecreateWidget();
76
77 // lazy creation of the content
78 virtual void CreateContent();
79
80 protected:
81
82 void OnToggleSearchButton(wxCommandEvent&);
83 void OnToggleCancelButton(wxCommandEvent&);
84 void OnToggleSearchMenu(wxCommandEvent&);
85
86 void OnSearch(wxCommandEvent& event);
87 void OnSearchCancel(wxCommandEvent& event);
88
89 wxMenu* CreateTestMenu();
90
91 // (re)create the control
92 void CreateControl();
93
94
95 wxSearchCtrl* m_srchCtrl;
96 wxCheckBox* m_searchBtnCheck;
97 wxCheckBox* m_cancelBtnCheck;
98 wxCheckBox* m_menuBtnCheck;
99
100 private:
101 DECLARE_EVENT_TABLE()
102 DECLARE_WIDGETS_PAGE(SearchCtrlWidgetsPage)
103 };
104
105 // ----------------------------------------------------------------------------
106 // event tables
107 // ----------------------------------------------------------------------------
108
109 BEGIN_EVENT_TABLE(SearchCtrlWidgetsPage, WidgetsPage)
110 EVT_CHECKBOX(ID_SEARCH_CB, SearchCtrlWidgetsPage::OnToggleSearchButton)
111 EVT_CHECKBOX(ID_CANCEL_CB, SearchCtrlWidgetsPage::OnToggleCancelButton)
112 EVT_CHECKBOX(ID_MENU_CB, SearchCtrlWidgetsPage::OnToggleSearchMenu)
113
114 EVT_SEARCHCTRL_SEARCH_BTN(wxID_ANY, SearchCtrlWidgetsPage::OnSearch)
115 EVT_SEARCHCTRL_CANCEL_BTN(wxID_ANY, SearchCtrlWidgetsPage::OnSearchCancel)
116 END_EVENT_TABLE()
117
118 // ============================================================================
119 // implementation
120 // ============================================================================
121
122 #if defined(__WXMAC__)
123 #define FAMILY_CTRLS NATIVE_CTRLS
124 #else
125 #define FAMILY_CTRLS GENERIC_CTRLS
126 #endif
127
128 IMPLEMENT_WIDGETS_PAGE(SearchCtrlWidgetsPage, wxT("SearchCtrl"),
129 FAMILY_CTRLS | EDITABLE_CTRLS | ALL_CTRLS);
130
131 SearchCtrlWidgetsPage::SearchCtrlWidgetsPage(WidgetsBookCtrl *book,
132 wxImageList *imaglist)
133 : WidgetsPage(book, imaglist, text_xpm)
134 {
135 }
136
137 void SearchCtrlWidgetsPage::CreateContent()
138 {
139 m_srchCtrl = NULL;
140
141 CreateControl();
142
143
144 wxSizer* box = new wxStaticBoxSizer(
145 new wxStaticBox(this, -1, wxT("Options")),
146 wxVERTICAL);
147
148 m_searchBtnCheck = new wxCheckBox(this, ID_SEARCH_CB, wxT("Search button"));
149 m_cancelBtnCheck = new wxCheckBox(this, ID_CANCEL_CB, wxT("Cancel button"));
150 m_menuBtnCheck = new wxCheckBox(this, ID_MENU_CB, wxT("Search menu"));
151
152 m_searchBtnCheck->SetValue(true);
153
154 box->Add(m_searchBtnCheck, 0, wxALL, 5);
155 box->Add(m_cancelBtnCheck, 0, wxALL, 5);
156 box->Add(m_menuBtnCheck, 0, wxALL, 5);
157
158 wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
159 sizer->Add(box, 0, wxALL|wxEXPAND, 15);
160 sizer->Add(m_srchCtrl, 0, wxALL|wxALIGN_CENTER, 15);
161
162 SetSizer(sizer);
163 }
164
165 void SearchCtrlWidgetsPage::CreateControl()
166 {
167 if (m_srchCtrl)
168 m_srchCtrl->Destroy();
169
170 int style = 0;
171
172 m_srchCtrl = new wxSearchCtrl(this, -1, wxEmptyString, wxDefaultPosition,
173 wxSize(150, -1), style);
174 }
175
176 void SearchCtrlWidgetsPage::RecreateWidget()
177 {
178 CreateControl();
179
180 GetSizer()->Add(m_srchCtrl, wxSizerFlags().Centre().TripleBorder());
181
182 Layout();
183 }
184
185 wxMenu* SearchCtrlWidgetsPage::CreateTestMenu()
186 {
187 wxMenu* menu = new wxMenu;
188 const int SEARCH_MENU_SIZE = 5;
189 wxMenuItem* menuItem = menu->Append(wxID_ANY, wxT("Recent Searches"), wxT(""), wxITEM_NORMAL);
190 menuItem->Enable(false);
191 for ( int i = 0; i < SEARCH_MENU_SIZE; i++ )
192 {
193 wxString itemText = wxString::Format(wxT("item %i"),i);
194 wxString tipText = wxString::Format(wxT("tip %i"),i);
195 menu->Append(ID_SEARCHMENU+i, itemText, tipText, wxITEM_NORMAL);
196 }
197 // target->Connect(
198 // ID_SEARCHMENU,
199 // ID_SEARCHMENU+SEARCH_MENU_SIZE,
200 // wxEVT_MENU,
201 // wxCommandEventHandler(MySearchCtrl::OnSearchMenu)
202 // );
203 return menu;
204 }
205
206
207 // ----------------------------------------------------------------------------
208 // event handlers
209 // ----------------------------------------------------------------------------
210
211 void SearchCtrlWidgetsPage::OnToggleSearchButton(wxCommandEvent&)
212 {
213 m_srchCtrl->ShowSearchButton( m_searchBtnCheck->GetValue() );
214 }
215
216 void SearchCtrlWidgetsPage::OnToggleCancelButton(wxCommandEvent&)
217 {
218 m_srchCtrl->ShowCancelButton( m_cancelBtnCheck->GetValue() );
219
220 }
221
222 void SearchCtrlWidgetsPage::OnToggleSearchMenu(wxCommandEvent&)
223 {
224 if ( m_menuBtnCheck->GetValue() )
225 m_srchCtrl->SetMenu( CreateTestMenu() );
226 else
227 m_srchCtrl->SetMenu(NULL);
228 }
229
230 void SearchCtrlWidgetsPage::OnSearch(wxCommandEvent& event)
231 {
232 wxLogMessage("Search button: search for \"%s\".", event.GetString());
233 }
234
235 void SearchCtrlWidgetsPage::OnSearchCancel(wxCommandEvent& event)
236 {
237 wxLogMessage("Cancel button pressed.");
238
239 event.Skip();
240 }
241
242 #endif // wxUSE_SEARCHCTRL