]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/searchctrl.cpp
Add a test for a tooltip for a control inside a static box.
[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
ce00f59b 7// Copyright: (c) 2006
526954c5 8// Licence: wxWindows licence
3f7f284d
RD
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx/wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#if wxUSE_SEARCHCTRL
27
28// for all others, include the necessary headers
29#ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/log.h"
32 #include "wx/radiobox.h"
0bf0b6bb 33 #include "wx/statbox.h"
3f7f284d
RD
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
53enum
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
67class SearchCtrlWidgetsPage : public WidgetsPage
68{
69public:
70 SearchCtrlWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
71 virtual ~SearchCtrlWidgetsPage(){};
72
73 virtual wxControl *GetWidget() const { return m_srchCtrl; }
6a8d7937 74 virtual wxTextEntryBase *GetTextEntry() const { return m_srchCtrl; }
07d304d2 75 virtual void RecreateWidget();
3f7f284d
RD
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
ac63bc40 86 void OnSearch(wxCommandEvent& event);
639dbb6d 87 void OnSearchCancel(wxCommandEvent& event);
ac63bc40 88
3f7f284d 89 wxMenu* CreateTestMenu();
ce00f59b 90
3f7f284d
RD
91 // (re)create the control
92 void CreateControl();
93
ce00f59b 94
3f7f284d
RD
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)
ac63bc40
VZ
113
114 EVT_SEARCHCTRL_SEARCH_BTN(wxID_ANY, SearchCtrlWidgetsPage::OnSearch)
639dbb6d 115 EVT_SEARCHCTRL_CANCEL_BTN(wxID_ANY, SearchCtrlWidgetsPage::OnSearchCancel)
3f7f284d
RD
116END_EVENT_TABLE()
117
118// ============================================================================
119// implementation
120// ============================================================================
121
122#if defined(__WXMAC__)
123 #define FAMILY_CTRLS NATIVE_CTRLS
124#else
125 #define FAMILY_CTRLS GENERIC_CTRLS
126#endif
127
9a83f860 128IMPLEMENT_WIDGETS_PAGE(SearchCtrlWidgetsPage, wxT("SearchCtrl"),
8138b9a4 129 FAMILY_CTRLS | EDITABLE_CTRLS | ALL_CTRLS);
3f7f284d
RD
130
131SearchCtrlWidgetsPage::SearchCtrlWidgetsPage(WidgetsBookCtrl *book,
132 wxImageList *imaglist)
133 : WidgetsPage(book, imaglist, text_xpm)
134{
135}
136
137void SearchCtrlWidgetsPage::CreateContent()
138{
139 m_srchCtrl = NULL;
140
3f7f284d
RD
141 CreateControl();
142
143
144 wxSizer* box = new wxStaticBoxSizer(
145 new wxStaticBox(this, -1, wxT("Options")),
146 wxVERTICAL);
147
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"));
151
152 m_searchBtnCheck->SetValue(true);
ce00f59b 153
3f7f284d
RD
154 box->Add(m_searchBtnCheck, 0, wxALL, 5);
155 box->Add(m_cancelBtnCheck, 0, wxALL, 5);
156 box->Add(m_menuBtnCheck, 0, wxALL, 5);
157
158 wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
159 sizer->Add(box, 0, wxALL|wxEXPAND, 15);
160 sizer->Add(m_srchCtrl, 0, wxALL|wxALIGN_CENTER, 15);
161
162 SetSizer(sizer);
163}
164
165void SearchCtrlWidgetsPage::CreateControl()
166{
167 if (m_srchCtrl)
168 m_srchCtrl->Destroy();
169
170 int style = 0;
ce00f59b 171
3f7f284d
RD
172 m_srchCtrl = new wxSearchCtrl(this, -1, wxEmptyString, wxDefaultPosition,
173 wxSize(150, -1), style);
174}
175
07d304d2
VZ
176void SearchCtrlWidgetsPage::RecreateWidget()
177{
178 CreateControl();
179
180 GetSizer()->Add(m_srchCtrl, wxSizerFlags().Centre().TripleBorder());
181
182 Layout();
183}
3f7f284d
RD
184
185wxMenu* SearchCtrlWidgetsPage::CreateTestMenu()
186{
187 wxMenu* menu = new wxMenu;
188 const int SEARCH_MENU_SIZE = 5;
9a83f860 189 wxMenuItem* menuItem = menu->Append(wxID_ANY, wxT("Recent Searches"), wxT(""), wxITEM_NORMAL);
3f7f284d
RD
190 menuItem->Enable(false);
191 for ( int i = 0; i < SEARCH_MENU_SIZE; i++ )
192 {
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);
196 }
ce00f59b 197// target->Connect(
3f7f284d
RD
198// ID_SEARCHMENU,
199// ID_SEARCHMENU+SEARCH_MENU_SIZE,
ce7fe42e 200// wxEVT_MENU,
3f7f284d
RD
201// wxCommandEventHandler(MySearchCtrl::OnSearchMenu)
202// );
203 return menu;
204}
205
206
207// ----------------------------------------------------------------------------
208// event handlers
209// ----------------------------------------------------------------------------
210
211void SearchCtrlWidgetsPage::OnToggleSearchButton(wxCommandEvent&)
212{
ec184e32 213 m_srchCtrl->ShowSearchButton( m_searchBtnCheck->GetValue() );
3f7f284d
RD
214}
215
216void SearchCtrlWidgetsPage::OnToggleCancelButton(wxCommandEvent&)
217{
ec184e32 218 m_srchCtrl->ShowCancelButton( m_cancelBtnCheck->GetValue() );
ce00f59b 219
3f7f284d
RD
220}
221
222void SearchCtrlWidgetsPage::OnToggleSearchMenu(wxCommandEvent&)
223{
224 if ( m_menuBtnCheck->GetValue() )
225 m_srchCtrl->SetMenu( CreateTestMenu() );
226 else
227 m_srchCtrl->SetMenu(NULL);
228}
229
ac63bc40
VZ
230void SearchCtrlWidgetsPage::OnSearch(wxCommandEvent& event)
231{
232 wxLogMessage("Search button: search for \"%s\".", event.GetString());
233}
3f7f284d 234
639dbb6d
VZ
235void SearchCtrlWidgetsPage::OnSearchCancel(wxCommandEvent& event)
236{
237 wxLogMessage("Cancel button pressed.");
238
239 event.Skip();
240}
241
3f7f284d 242#endif // wxUSE_SEARCHCTRL