]>
git.saurik.com Git - wxWidgets.git/blob - samples/widgets/searchctrl.cpp
4c8e900c406ab74759fbb8769864683c2b536c34
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: searchctrl.cpp
4 // Purpose: Shows wxSearchCtrl
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
29 // for all others, include the necessary headers
33 #include "wx/radiobox.h"
34 #include "wx/statbox.h"
37 #include "wx/artprov.h"
39 #include "wx/stattext.h"
40 #include "wx/checkbox.h"
43 #include "wx/srchctrl.h"
47 #include "icons/text.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 ID_SEARCH_CB
= wxID_HIGHEST
,
64 // ----------------------------------------------------------------------------
65 // ColourPickerWidgetsPage
66 // ----------------------------------------------------------------------------
68 class SearchCtrlWidgetsPage
: public WidgetsPage
71 SearchCtrlWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
72 virtual ~SearchCtrlWidgetsPage(){};
74 virtual wxControl
*GetWidget() const { return m_srchCtrl
; }
75 virtual wxTextEntryBase
*GetTextEntry() const { return m_srchCtrl
; }
76 virtual void RecreateWidget();
78 // lazy creation of the content
79 virtual void CreateContent();
83 void OnToggleSearchButton(wxCommandEvent
&);
84 void OnToggleCancelButton(wxCommandEvent
&);
85 void OnToggleSearchMenu(wxCommandEvent
&);
87 void OnSearch(wxCommandEvent
& event
);
88 void OnSearchCancel(wxCommandEvent
& event
);
90 wxMenu
* CreateTestMenu();
92 // (re)create the control
96 wxSearchCtrl
* m_srchCtrl
;
97 wxCheckBox
* m_searchBtnCheck
;
98 wxCheckBox
* m_cancelBtnCheck
;
99 wxCheckBox
* m_menuBtnCheck
;
102 DECLARE_EVENT_TABLE()
103 DECLARE_WIDGETS_PAGE(SearchCtrlWidgetsPage
)
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
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
)
115 EVT_SEARCHCTRL_SEARCH_BTN(wxID_ANY
, SearchCtrlWidgetsPage::OnSearch
)
116 EVT_SEARCHCTRL_CANCEL_BTN(wxID_ANY
, SearchCtrlWidgetsPage::OnSearchCancel
)
119 // ============================================================================
121 // ============================================================================
123 #if defined(__WXMAC__)
124 #define FAMILY_CTRLS NATIVE_CTRLS
126 #define FAMILY_CTRLS GENERIC_CTRLS
129 IMPLEMENT_WIDGETS_PAGE(SearchCtrlWidgetsPage
, wxT("SearchCtrl"),
130 FAMILY_CTRLS
| EDITABLE_CTRLS
| ALL_CTRLS
);
132 SearchCtrlWidgetsPage::SearchCtrlWidgetsPage(WidgetsBookCtrl
*book
,
133 wxImageList
*imaglist
)
134 : WidgetsPage(book
, imaglist
, text_xpm
)
138 void SearchCtrlWidgetsPage::CreateContent()
145 wxSizer
* box
= new wxStaticBoxSizer(
146 new wxStaticBox(this, -1, wxT("Options")),
149 m_searchBtnCheck
= new wxCheckBox(this, ID_SEARCH_CB
, wxT("Search button"));
150 m_cancelBtnCheck
= new wxCheckBox(this, ID_CANCEL_CB
, wxT("Cancel button"));
151 m_menuBtnCheck
= new wxCheckBox(this, ID_MENU_CB
, wxT("Search menu"));
153 m_searchBtnCheck
->SetValue(true);
155 box
->Add(m_searchBtnCheck
, 0, wxALL
, 5);
156 box
->Add(m_cancelBtnCheck
, 0, wxALL
, 5);
157 box
->Add(m_menuBtnCheck
, 0, wxALL
, 5);
159 wxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
160 sizer
->Add(box
, 0, wxALL
|wxEXPAND
, 15);
161 sizer
->Add(m_srchCtrl
, 0, wxALL
|wxALIGN_CENTER
, 15);
166 void SearchCtrlWidgetsPage::CreateControl()
169 m_srchCtrl
->Destroy();
173 m_srchCtrl
= new wxSearchCtrl(this, -1, wxEmptyString
, wxDefaultPosition
,
174 wxSize(150, -1), style
);
177 void SearchCtrlWidgetsPage::RecreateWidget()
181 GetSizer()->Add(m_srchCtrl
, wxSizerFlags().Centre().TripleBorder());
186 wxMenu
* SearchCtrlWidgetsPage::CreateTestMenu()
188 wxMenu
* menu
= new wxMenu
;
189 const int SEARCH_MENU_SIZE
= 5;
190 wxMenuItem
* menuItem
= menu
->Append(wxID_ANY
, wxT("Recent Searches"), wxT(""), wxITEM_NORMAL
);
191 menuItem
->Enable(false);
192 for ( int i
= 0; i
< SEARCH_MENU_SIZE
; i
++ )
194 wxString itemText
= wxString::Format(wxT("item %i"),i
);
195 wxString tipText
= wxString::Format(wxT("tip %i"),i
);
196 menu
->Append(ID_SEARCHMENU
+i
, itemText
, tipText
, wxITEM_NORMAL
);
200 // ID_SEARCHMENU+SEARCH_MENU_SIZE,
201 // wxEVT_COMMAND_MENU_SELECTED,
202 // wxCommandEventHandler(MySearchCtrl::OnSearchMenu)
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 void SearchCtrlWidgetsPage::OnToggleSearchButton(wxCommandEvent
&)
214 m_srchCtrl
->ShowSearchButton( m_searchBtnCheck
->GetValue() );
217 void SearchCtrlWidgetsPage::OnToggleCancelButton(wxCommandEvent
&)
219 m_srchCtrl
->ShowCancelButton( m_cancelBtnCheck
->GetValue() );
223 void SearchCtrlWidgetsPage::OnToggleSearchMenu(wxCommandEvent
&)
225 if ( m_menuBtnCheck
->GetValue() )
226 m_srchCtrl
->SetMenu( CreateTestMenu() );
228 m_srchCtrl
->SetMenu(NULL
);
231 void SearchCtrlWidgetsPage::OnSearch(wxCommandEvent
& event
)
233 wxLogMessage("Search button: search for \"%s\".", event
.GetString());
236 void SearchCtrlWidgetsPage::OnSearchCancel(wxCommandEvent
& event
)
238 wxLogMessage("Cancel button pressed.");
243 #endif // wxUSE_SEARCHCTRL