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