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