]>
git.saurik.com Git - wxWidgets.git/blob - samples/widgets/searchctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: searchctrl.cpp
4 // Purpose: Shows wxSearchCtrl
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
28 // for all others, include the necessary headers
32 #include "wx/radiobox.h"
33 #include "wx/statbox.h"
36 #include "wx/artprov.h"
38 #include "wx/stattext.h"
39 #include "wx/checkbox.h"
42 #include "wx/srchctrl.h"
46 #include "icons/text.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 ID_SEARCH_CB
= wxID_HIGHEST
,
63 // ----------------------------------------------------------------------------
64 // ColourPickerWidgetsPage
65 // ----------------------------------------------------------------------------
67 class SearchCtrlWidgetsPage
: public WidgetsPage
70 SearchCtrlWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
71 virtual ~SearchCtrlWidgetsPage(){};
73 virtual wxControl
*GetWidget() const { return m_srchCtrl
; }
74 virtual wxTextEntryBase
*GetTextEntry() const { return m_srchCtrl
; }
75 virtual void RecreateWidget();
77 // lazy creation of the content
78 virtual void CreateContent();
82 void OnToggleSearchButton(wxCommandEvent
&);
83 void OnToggleCancelButton(wxCommandEvent
&);
84 void OnToggleSearchMenu(wxCommandEvent
&);
86 void OnSearch(wxCommandEvent
& event
);
87 void OnSearchCancel(wxCommandEvent
& event
);
89 wxMenu
* CreateTestMenu();
91 // (re)create the control
95 wxSearchCtrl
* m_srchCtrl
;
96 wxCheckBox
* m_searchBtnCheck
;
97 wxCheckBox
* m_cancelBtnCheck
;
98 wxCheckBox
* m_menuBtnCheck
;
101 DECLARE_EVENT_TABLE()
102 DECLARE_WIDGETS_PAGE(SearchCtrlWidgetsPage
)
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
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
)
114 EVT_SEARCHCTRL_SEARCH_BTN(wxID_ANY
, SearchCtrlWidgetsPage::OnSearch
)
115 EVT_SEARCHCTRL_CANCEL_BTN(wxID_ANY
, SearchCtrlWidgetsPage::OnSearchCancel
)
118 // ============================================================================
120 // ============================================================================
122 #if defined(__WXMAC__)
123 #define FAMILY_CTRLS NATIVE_CTRLS
125 #define FAMILY_CTRLS GENERIC_CTRLS
128 IMPLEMENT_WIDGETS_PAGE(SearchCtrlWidgetsPage
, wxT("SearchCtrl"),
129 FAMILY_CTRLS
| EDITABLE_CTRLS
| ALL_CTRLS
);
131 SearchCtrlWidgetsPage::SearchCtrlWidgetsPage(WidgetsBookCtrl
*book
,
132 wxImageList
*imaglist
)
133 : WidgetsPage(book
, imaglist
, text_xpm
)
137 void SearchCtrlWidgetsPage::CreateContent()
144 wxSizer
* box
= new wxStaticBoxSizer(
145 new wxStaticBox(this, -1, wxT("Options")),
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"));
152 m_searchBtnCheck
->SetValue(true);
154 box
->Add(m_searchBtnCheck
, 0, wxALL
, 5);
155 box
->Add(m_cancelBtnCheck
, 0, wxALL
, 5);
156 box
->Add(m_menuBtnCheck
, 0, wxALL
, 5);
158 wxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
159 sizer
->Add(box
, 0, wxALL
|wxEXPAND
, 15);
160 sizer
->Add(m_srchCtrl
, 0, wxALL
|wxALIGN_CENTER
, 15);
165 void SearchCtrlWidgetsPage::CreateControl()
168 m_srchCtrl
->Destroy();
172 m_srchCtrl
= new wxSearchCtrl(this, -1, wxEmptyString
, wxDefaultPosition
,
173 wxSize(150, -1), style
);
176 void SearchCtrlWidgetsPage::RecreateWidget()
180 GetSizer()->Add(m_srchCtrl
, wxSizerFlags().Centre().TripleBorder());
185 wxMenu
* SearchCtrlWidgetsPage::CreateTestMenu()
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
++ )
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
);
199 // ID_SEARCHMENU+SEARCH_MENU_SIZE,
201 // wxCommandEventHandler(MySearchCtrl::OnSearchMenu)
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 void SearchCtrlWidgetsPage::OnToggleSearchButton(wxCommandEvent
&)
213 m_srchCtrl
->ShowSearchButton( m_searchBtnCheck
->GetValue() );
216 void SearchCtrlWidgetsPage::OnToggleCancelButton(wxCommandEvent
&)
218 m_srchCtrl
->ShowCancelButton( m_cancelBtnCheck
->GetValue() );
222 void SearchCtrlWidgetsPage::OnToggleSearchMenu(wxCommandEvent
&)
224 if ( m_menuBtnCheck
->GetValue() )
225 m_srchCtrl
->SetMenu( CreateTestMenu() );
227 m_srchCtrl
->SetMenu(NULL
);
230 void SearchCtrlWidgetsPage::OnSearch(wxCommandEvent
& event
)
232 wxLogMessage("Search button: search for \"%s\".", event
.GetString());
235 void SearchCtrlWidgetsPage::OnSearchCancel(wxCommandEvent
& event
)
237 wxLogMessage("Cancel button pressed.");
242 #endif // wxUSE_SEARCHCTRL