]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/widgets/searchctrl.cpp
Support using GetTextExtent() with empty string to get descent in wxOSX.
[wxWidgets.git] / samples / widgets / searchctrl.cpp
... / ...
CommitLineData
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// Licence: wxWindows licence
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
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 wxTextEntryBase *GetTextEntry() const { return m_srchCtrl; }
76 virtual void RecreateWidget();
77
78 // lazy creation of the content
79 virtual void CreateContent();
80
81protected:
82
83 void OnToggleSearchButton(wxCommandEvent&);
84 void OnToggleCancelButton(wxCommandEvent&);
85 void OnToggleSearchMenu(wxCommandEvent&);
86
87 void OnSearch(wxCommandEvent& event);
88 void OnSearchCancel(wxCommandEvent& event);
89
90 wxMenu* CreateTestMenu();
91
92 // (re)create the control
93 void CreateControl();
94
95
96 wxSearchCtrl* m_srchCtrl;
97 wxCheckBox* m_searchBtnCheck;
98 wxCheckBox* m_cancelBtnCheck;
99 wxCheckBox* m_menuBtnCheck;
100
101private:
102 DECLARE_EVENT_TABLE()
103 DECLARE_WIDGETS_PAGE(SearchCtrlWidgetsPage)
104};
105
106// ----------------------------------------------------------------------------
107// event tables
108// ----------------------------------------------------------------------------
109
110BEGIN_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
115 EVT_SEARCHCTRL_SEARCH_BTN(wxID_ANY, SearchCtrlWidgetsPage::OnSearch)
116 EVT_SEARCHCTRL_CANCEL_BTN(wxID_ANY, SearchCtrlWidgetsPage::OnSearchCancel)
117END_EVENT_TABLE()
118
119// ============================================================================
120// implementation
121// ============================================================================
122
123#if defined(__WXMAC__)
124 #define FAMILY_CTRLS NATIVE_CTRLS
125#else
126 #define FAMILY_CTRLS GENERIC_CTRLS
127#endif
128
129IMPLEMENT_WIDGETS_PAGE(SearchCtrlWidgetsPage, wxT("SearchCtrl"),
130 FAMILY_CTRLS | EDITABLE_CTRLS | ALL_CTRLS);
131
132SearchCtrlWidgetsPage::SearchCtrlWidgetsPage(WidgetsBookCtrl *book,
133 wxImageList *imaglist)
134 : WidgetsPage(book, imaglist, text_xpm)
135{
136}
137
138void SearchCtrlWidgetsPage::CreateContent()
139{
140 m_srchCtrl = NULL;
141
142 CreateControl();
143
144
145 wxSizer* box = new wxStaticBoxSizer(
146 new wxStaticBox(this, -1, wxT("Options")),
147 wxVERTICAL);
148
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"));
152
153 m_searchBtnCheck->SetValue(true);
154
155 box->Add(m_searchBtnCheck, 0, wxALL, 5);
156 box->Add(m_cancelBtnCheck, 0, wxALL, 5);
157 box->Add(m_menuBtnCheck, 0, wxALL, 5);
158
159 wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
160 sizer->Add(box, 0, wxALL|wxEXPAND, 15);
161 sizer->Add(m_srchCtrl, 0, wxALL|wxALIGN_CENTER, 15);
162
163 SetSizer(sizer);
164}
165
166void SearchCtrlWidgetsPage::CreateControl()
167{
168 if (m_srchCtrl)
169 m_srchCtrl->Destroy();
170
171 int style = 0;
172
173 m_srchCtrl = new wxSearchCtrl(this, -1, wxEmptyString, wxDefaultPosition,
174 wxSize(150, -1), style);
175}
176
177void SearchCtrlWidgetsPage::RecreateWidget()
178{
179 CreateControl();
180
181 GetSizer()->Add(m_srchCtrl, wxSizerFlags().Centre().TripleBorder());
182
183 Layout();
184}
185
186wxMenu* SearchCtrlWidgetsPage::CreateTestMenu()
187{
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++ )
193 {
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);
197 }
198// target->Connect(
199// ID_SEARCHMENU,
200// ID_SEARCHMENU+SEARCH_MENU_SIZE,
201// wxEVT_MENU,
202// wxCommandEventHandler(MySearchCtrl::OnSearchMenu)
203// );
204 return menu;
205}
206
207
208// ----------------------------------------------------------------------------
209// event handlers
210// ----------------------------------------------------------------------------
211
212void SearchCtrlWidgetsPage::OnToggleSearchButton(wxCommandEvent&)
213{
214 m_srchCtrl->ShowSearchButton( m_searchBtnCheck->GetValue() );
215}
216
217void SearchCtrlWidgetsPage::OnToggleCancelButton(wxCommandEvent&)
218{
219 m_srchCtrl->ShowCancelButton( m_cancelBtnCheck->GetValue() );
220
221}
222
223void SearchCtrlWidgetsPage::OnToggleSearchMenu(wxCommandEvent&)
224{
225 if ( m_menuBtnCheck->GetValue() )
226 m_srchCtrl->SetMenu( CreateTestMenu() );
227 else
228 m_srchCtrl->SetMenu(NULL);
229}
230
231void SearchCtrlWidgetsPage::OnSearch(wxCommandEvent& event)
232{
233 wxLogMessage("Search button: search for \"%s\".", event.GetString());
234}
235
236void SearchCtrlWidgetsPage::OnSearchCancel(wxCommandEvent& event)
237{
238 wxLogMessage("Cancel button pressed.");
239
240 event.Skip();
241}
242
243#endif // wxUSE_SEARCHCTRL