]> git.saurik.com Git - wxWidgets.git/blame - samples/taborder/taborder.cpp
merge wxEditableListBox sample in widgets sample (it was a 60-lines sample of a singl...
[wxWidgets.git] / samples / taborder / taborder.cpp
CommitLineData
d63f7562
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: taborder.cpp
3// Purpose: Sample for testing TAB navigation
4// Author: Vadim Zeitlin
5// RCS-ID: $Id$
6// Copyright: (c) 2007 Vadim Zeitlin
7// Licence: wxWindows license
8/////////////////////////////////////////////////////////////////////////////
9
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21 #pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
8af53fb4 25 #include "wx/app.h"
147adf0a 26 #include "wx/log.h"
8af53fb4
VZ
27 #include "wx/frame.h"
28 #include "wx/menu.h"
147adf0a 29 #include "wx/sizer.h"
8af53fb4
VZ
30
31 #include "wx/panel.h"
147adf0a 32 #include "wx/msgdlg.h"
8af53fb4
VZ
33
34 #include "wx/button.h"
35 #include "wx/listbox.h"
147adf0a 36 #include "wx/stattext.h"
8af53fb4 37 #include "wx/textctrl.h"
d63f7562
VZ
38#endif
39
8af53fb4
VZ
40#include "wx/notebook.h"
41
d63f7562
VZ
42// ----------------------------------------------------------------------------
43// constants
44// ----------------------------------------------------------------------------
45
46// menu commands and controls ids
47enum
48{
49 // file menu
8af53fb4
VZ
50 TabOrder_Quit = wxID_EXIT,
51 TabOrder_About = wxID_ABOUT,
d63f7562
VZ
52
53 // navigation menu
54 TabOrder_TabForward = 200,
55 TabOrder_TabBackward,
56
57 TabOrder_Max
58};
59
60// status panes: first one is for temporary messages, the second one shows
61// current focus
62enum
63{
64 StatusPane_Default,
65 StatusPane_Focus,
66 StatusPane_Max
67};
68
69// ----------------------------------------------------------------------------
70// declarations of the classes used in this sample
71// ----------------------------------------------------------------------------
72
73// the main application class
74class MyApp : public wxApp
75{
76public:
77 virtual bool OnInit();
78};
79
80// and the main sample window
81class MyFrame : public wxFrame
82{
83public:
84 MyFrame();
85
86private:
87 void OnAbout(wxCommandEvent& event);
88 void OnQuit(wxCommandEvent& event);
89
90 void OnTabForward(wxCommandEvent& event);
91 void OnTabBackward(wxCommandEvent& event);
92
93 void OnIdle(wxIdleEvent& event);
94
2a1cf3e9 95 void DoNavigate(int flags)
d63f7562 96 {
2a1cf3e9 97 if ( m_panel->NavigateIn(flags) )
d63f7562
VZ
98 wxLogStatus(this, _T("Navigation event processed"));
99 else
100 wxLogStatus(this, _T("Navigation event ignored"));
101 }
102
b348c1da
VZ
103 wxPanel *m_panel;
104
d63f7562
VZ
105 DECLARE_EVENT_TABLE()
106};
107
108// and the panel taking up MyFrame client area
109class MyPanel : public wxPanel
110{
111public:
112 MyPanel(wxWindow *parent);
8af53fb4
VZ
113
114private:
115 wxWindow *CreateButtonPage(wxWindow *parent);
116 wxWindow *CreateTextPage(wxWindow *parent);
117};
118
119// a text control which checks if processing Tab presses in controls with
120// wxTE_PROCESS_TAB style really works
121class MyTabTextCtrl : public wxTextCtrl
122{
123public:
124 MyTabTextCtrl(wxWindow *parent, const wxString& value, int flags = 0)
125 : wxTextCtrl(parent, wxID_ANY, value,
126 wxDefaultPosition, wxDefaultSize,
147adf0a 127 flags)
8af53fb4
VZ
128 {
129 Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MyTabTextCtrl::OnKeyDown));
130 }
131
132private:
133 void OnKeyDown(wxKeyEvent& event)
134 {
135 if ( event.GetKeyCode() == WXK_TAB &&
136 wxMessageBox
137 (
138 _T("Let the Tab be used for navigation?"),
139 _T("wxWidgets TabOrder sample: Tab key pressed"),
140 wxICON_QUESTION | wxYES_NO,
141 this
142 ) != wxYES )
143 {
144 // skip Skip() below: we consume the Tab press ourselves and so the
145 // focus shouldn't change
146 return;
147 }
148
149 event.Skip();
150 }
d63f7562
VZ
151};
152
153// ============================================================================
154// implementation
155// ============================================================================
156
157// ----------------------------------------------------------------------------
158// MyApp
159// ----------------------------------------------------------------------------
160
161IMPLEMENT_APP(MyApp)
162
163bool MyApp::OnInit()
164{
165 if ( !wxApp::OnInit() )
166 return false;
167
168 MyFrame *frame = new MyFrame;
169 frame->Show(true);
170
171 return true;
172}
173
174// ----------------------------------------------------------------------------
175// MyFrame
176// ----------------------------------------------------------------------------
177
178BEGIN_EVENT_TABLE(MyFrame, wxFrame)
179 EVT_MENU(TabOrder_Quit, MyFrame::OnQuit)
180 EVT_MENU(TabOrder_About, MyFrame::OnAbout)
181
182 EVT_MENU(TabOrder_TabForward, MyFrame::OnTabForward)
183 EVT_MENU(TabOrder_TabBackward, MyFrame::OnTabBackward)
184
185 EVT_IDLE(MyFrame::OnIdle)
186END_EVENT_TABLE()
187
188MyFrame::MyFrame()
189 : wxFrame(NULL, wxID_ANY, _T("TabOrder wxWidgets Sample"),
190 wxDefaultPosition, wxSize(700, 450))
191{
192 wxMenu *menuFile = new wxMenu;
8af53fb4 193 menuFile->Append(TabOrder_About);
d63f7562 194 menuFile->AppendSeparator();
8af53fb4 195 menuFile->Append(TabOrder_Quit);
d63f7562
VZ
196
197 wxMenu *menuNav = new wxMenu;
198 menuNav->Append(TabOrder_TabForward, _T("Tab &forward\tCtrl-F"),
199 _T("Emulate a <Tab> press"));
200 menuNav->Append(TabOrder_TabBackward, _T("Tab &backward\tCtrl-B"),
201 _T("Emulate a <Shift-Tab> press"));
202
203 wxMenuBar *mbar = new wxMenuBar;
204 mbar->Append(menuFile, _T("&File"));
205 mbar->Append(menuNav, _T("&Navigate"));
206
207 SetMenuBar(mbar);
208
b348c1da 209 m_panel = new MyPanel(this);
d63f7562
VZ
210
211 CreateStatusBar(StatusPane_Max);
212}
213
214void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
215{
216 Close(true);
217}
218
219void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
220{
221 wxMessageBox(_T("Tab navigation sample\n(c) 2007 Vadim Zeitlin"),
222 _T("About TabOrder wxWidgets Sample"), wxOK, this);
223}
224
225void MyFrame::OnTabForward(wxCommandEvent& WXUNUSED(event))
226{
227 DoNavigate(wxNavigationKeyEvent::IsForward | wxNavigationKeyEvent::FromTab);
228}
229
230void MyFrame::OnTabBackward(wxCommandEvent& WXUNUSED(event))
231{
232 DoNavigate(wxNavigationKeyEvent::IsBackward | wxNavigationKeyEvent::FromTab);
233}
234
235void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
236{
237 // track the window which has the focus in the status bar
238 static wxWindow *s_windowFocus = NULL;
239 wxWindow *focus = wxWindow::FindFocus();
240 if ( focus != s_windowFocus )
241 {
242 s_windowFocus = focus;
243
244 wxString msg;
245 if ( focus )
246 {
247 msg.Printf(_T("Focus is at %s"), s_windowFocus->GetName().c_str());
248 }
249 else
250 {
251 msg = _T("No focus");
252 }
253
254 SetStatusText(msg, StatusPane_Focus);
255 }
256}
257
258// ----------------------------------------------------------------------------
259// MyPanel
260// ----------------------------------------------------------------------------
261
262MyPanel::MyPanel(wxWindow *parent)
263 : wxPanel(parent, wxID_ANY)
8af53fb4
VZ
264{
265 wxNotebook *notebook = new wxNotebook(this, wxID_ANY);
266 notebook->AddPage(CreateButtonPage(notebook), _T("Button"));
267 notebook->AddPage(CreateTextPage(notebook), _T("Text"));
268
269 wxSizer *sizerV = new wxBoxSizer(wxVERTICAL);
270 sizerV->Add(notebook, wxSizerFlags(1).Expand());
271
272 wxListBox *lbox = new wxListBox(this, wxID_ANY);
273 lbox->AppendString(_T("Just a"));
274 lbox->AppendString(_T("simple"));
275 lbox->AppendString(_T("listbox"));
276 sizerV->Add(lbox, wxSizerFlags(1).Expand());
277
278 SetSizerAndFit(sizerV);
279}
280
281wxWindow *MyPanel::CreateButtonPage(wxWindow *parent)
282{
283 wxSizerFlags flagsBorder = wxSizerFlags().Border().Centre();
284
285 wxPanel *page = new wxPanel(parent);
286 wxSizer *sizerPage = new wxBoxSizer(wxHORIZONTAL);
287 sizerPage->Add(new wxButton(page, wxID_ANY, _T("&First")), flagsBorder);
288 sizerPage->Add(new wxStaticText(page, wxID_ANY, _T("[st&atic]")),
289 flagsBorder);
290 sizerPage->Add(new wxButton(page, wxID_ANY, _T("&Second")), flagsBorder);
291
292 page->SetSizer(sizerPage);
293
294 return page;
295}
296
297wxWindow *MyPanel::CreateTextPage(wxWindow *parent)
d63f7562
VZ
298{
299 wxSizerFlags flagsBorder = wxSizerFlags().Border();
300
8af53fb4
VZ
301 wxSizer *sizerPage = new wxBoxSizer(wxVERTICAL);
302 wxPanel *page = new wxPanel(parent);
303
d63f7562 304 wxSizer *sizerH = new wxBoxSizer(wxHORIZONTAL);
8af53fb4
VZ
305 sizerH->Add(new wxStaticText(page, wxID_ANY, _T("&Label:")), flagsBorder);
306 sizerH->Add(new MyTabTextCtrl(page, _T("TAB ignored here")), flagsBorder);
307 sizerPage->Add(sizerH, wxSizerFlags(1).Expand());
d63f7562 308
8af53fb4 309 sizerH = new wxBoxSizer(wxHORIZONTAL);
147adf0a
VZ
310 sizerH->Add(new wxStaticText(page, wxID_ANY, _T("&Another one:")),
311 flagsBorder);
8af53fb4
VZ
312 sizerH->Add(new MyTabTextCtrl(page, _T("press Tab here"), wxTE_PROCESS_TAB),
313 flagsBorder);
314 sizerPage->Add(sizerH, wxSizerFlags(1).Expand());
315
316 page->SetSizer(sizerPage);
317
318 return page;
d63f7562
VZ
319}
320