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