]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/searchctrl.cpp
2bd3905bb6690ac254ad72b1ba53a1eb1005b4bc
[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 #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 void RecreateWidget() { CreateControl(); }
75
76 // lazy creation of the content
77 virtual void CreateContent();
78
79 protected:
80
81 void OnToggleSearchButton(wxCommandEvent&);
82 void OnToggleCancelButton(wxCommandEvent&);
83 void OnToggleSearchMenu(wxCommandEvent&);
84
85 wxMenu* CreateTestMenu();
86
87 // (re)create the control
88 void CreateControl();
89
90 // reset the control parameters
91 void Reset();
92
93
94 wxSearchCtrl* m_srchCtrl;
95 wxCheckBox* m_searchBtnCheck;
96 wxCheckBox* m_cancelBtnCheck;
97 wxCheckBox* m_menuBtnCheck;
98
99 private:
100 DECLARE_EVENT_TABLE()
101 DECLARE_WIDGETS_PAGE(SearchCtrlWidgetsPage)
102 };
103
104 // ----------------------------------------------------------------------------
105 // event tables
106 // ----------------------------------------------------------------------------
107
108 BEGIN_EVENT_TABLE(SearchCtrlWidgetsPage, WidgetsPage)
109 EVT_CHECKBOX(ID_SEARCH_CB, SearchCtrlWidgetsPage::OnToggleSearchButton)
110 EVT_CHECKBOX(ID_CANCEL_CB, SearchCtrlWidgetsPage::OnToggleCancelButton)
111 EVT_CHECKBOX(ID_MENU_CB, SearchCtrlWidgetsPage::OnToggleSearchMenu)
112 END_EVENT_TABLE()
113
114 // ============================================================================
115 // implementation
116 // ============================================================================
117
118 #if defined(__WXMAC__)
119 #define FAMILY_CTRLS NATIVE_CTRLS
120 #else
121 #define FAMILY_CTRLS GENERIC_CTRLS
122 #endif
123
124 IMPLEMENT_WIDGETS_PAGE(SearchCtrlWidgetsPage, _T("SearchCtrl"),
125 FAMILY_CTRLS | ALL_CTRLS);
126
127 SearchCtrlWidgetsPage::SearchCtrlWidgetsPage(WidgetsBookCtrl *book,
128 wxImageList *imaglist)
129 : WidgetsPage(book, imaglist, text_xpm)
130 {
131 }
132
133 void SearchCtrlWidgetsPage::CreateContent()
134 {
135 m_srchCtrl = NULL;
136
137 Reset();
138 CreateControl();
139
140
141 wxSizer* box = new wxStaticBoxSizer(
142 new wxStaticBox(this, -1, wxT("Options")),
143 wxVERTICAL);
144
145 m_searchBtnCheck = new wxCheckBox(this, ID_SEARCH_CB, wxT("Search button"));
146 m_cancelBtnCheck = new wxCheckBox(this, ID_CANCEL_CB, wxT("Cancel button"));
147 m_menuBtnCheck = new wxCheckBox(this, ID_MENU_CB, wxT("Search menu"));
148
149 m_searchBtnCheck->SetValue(true);
150
151 box->Add(m_searchBtnCheck, 0, wxALL, 5);
152 box->Add(m_cancelBtnCheck, 0, wxALL, 5);
153 box->Add(m_menuBtnCheck, 0, wxALL, 5);
154
155 wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
156 sizer->Add(box, 0, wxALL|wxEXPAND, 15);
157 sizer->Add(m_srchCtrl, 0, wxALL|wxALIGN_CENTER, 15);
158
159 SetSizer(sizer);
160 }
161
162 void SearchCtrlWidgetsPage::CreateControl()
163 {
164 if (m_srchCtrl)
165 m_srchCtrl->Destroy();
166
167 int style = 0;
168
169 m_srchCtrl = new wxSearchCtrl(this, -1, wxEmptyString, wxDefaultPosition,
170 wxSize(150, -1), style);
171 }
172
173 void SearchCtrlWidgetsPage::Reset()
174 {
175 }
176
177
178 wxMenu* SearchCtrlWidgetsPage::CreateTestMenu()
179 {
180 wxMenu* menu = new wxMenu;
181 const int SEARCH_MENU_SIZE = 5;
182 wxMenuItem* menuItem = menu->Append(wxID_ANY, _T("Recent Searches"), wxT(""), wxITEM_NORMAL);
183 menuItem->Enable(false);
184 for ( int i = 0; i < SEARCH_MENU_SIZE; i++ )
185 {
186 wxString itemText = wxString::Format(wxT("item %i"),i);
187 wxString tipText = wxString::Format(wxT("tip %i"),i);
188 menu->Append(ID_SEARCHMENU+i, itemText, tipText, wxITEM_NORMAL);
189 }
190 // target->Connect(
191 // ID_SEARCHMENU,
192 // ID_SEARCHMENU+SEARCH_MENU_SIZE,
193 // wxEVT_COMMAND_MENU_SELECTED,
194 // wxCommandEventHandler(MySearchCtrl::OnSearchMenu)
195 // );
196 return menu;
197 }
198
199
200 // ----------------------------------------------------------------------------
201 // event handlers
202 // ----------------------------------------------------------------------------
203
204 void SearchCtrlWidgetsPage::OnToggleSearchButton(wxCommandEvent&)
205 {
206 m_srchCtrl->SetSearchButtonVisible( m_searchBtnCheck->GetValue() );
207 }
208
209 void SearchCtrlWidgetsPage::OnToggleCancelButton(wxCommandEvent&)
210 {
211 m_srchCtrl->SetCancelButtonVisible( m_cancelBtnCheck->GetValue() );
212
213 }
214
215 void SearchCtrlWidgetsPage::OnToggleSearchMenu(wxCommandEvent&)
216 {
217 if ( m_menuBtnCheck->GetValue() )
218 m_srchCtrl->SetMenu( CreateTestMenu() );
219 else
220 m_srchCtrl->SetMenu(NULL);
221 }
222
223
224 #endif // wxUSE_SEARCHCTRL