Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / samples / taborder / taborder.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: taborder.cpp
3 // Purpose: Sample for testing TAB navigation
4 // Author: Vadim Zeitlin
5 // Copyright: (c) 2007 Vadim Zeitlin
6 // Licence: wxWindows licence
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
24 #include "wx/app.h"
25 #include "wx/log.h"
26 #include "wx/frame.h"
27 #include "wx/menu.h"
28 #include "wx/sizer.h"
29
30 #include "wx/panel.h"
31 #include "wx/msgdlg.h"
32
33 #include "wx/button.h"
34 #include "wx/listbox.h"
35 #include "wx/stattext.h"
36 #include "wx/textctrl.h"
37 #endif
38
39 #include "wx/notebook.h"
40
41 #ifndef wxHAS_IMAGES_IN_RESOURCES
42 #include "../sample.xpm"
43 #endif
44
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // menu commands and controls ids
51 enum
52 {
53 // file menu
54 TabOrder_Quit = wxID_EXIT,
55 TabOrder_About = wxID_ABOUT,
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
66 enum
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
78 class MyApp : public wxApp
79 {
80 public:
81 virtual bool OnInit();
82 };
83
84 // and the main sample window
85 class MyFrame : public wxFrame
86 {
87 public:
88 MyFrame();
89
90 private:
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
99 void DoNavigate(int flags)
100 {
101 if ( m_panel->NavigateIn(flags) )
102 {
103 wxLogStatus(this, wxT("Navigation event processed"));
104 }
105 else
106 {
107 wxLogStatus(this, wxT("Navigation event ignored"));
108 }
109 }
110
111 wxPanel *m_panel;
112
113 DECLARE_EVENT_TABLE()
114 };
115
116 // and the panel taking up MyFrame client area
117 class MyPanel : public wxPanel
118 {
119 public:
120 MyPanel(wxWindow *parent);
121
122 private:
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
129 class MyTabTextCtrl : public wxTextCtrl
130 {
131 public:
132 MyTabTextCtrl(wxWindow *parent, const wxString& value, int flags = 0)
133 : wxTextCtrl(parent, wxID_ANY, value,
134 wxDefaultPosition, wxDefaultSize,
135 flags)
136 {
137 Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MyTabTextCtrl::OnKeyDown));
138 }
139
140 private:
141 void OnKeyDown(wxKeyEvent& event)
142 {
143 if ( event.GetKeyCode() == WXK_TAB &&
144 wxMessageBox
145 (
146 wxT("Let the Tab be used for navigation?"),
147 wxT("wxWidgets TabOrder sample: Tab key pressed"),
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 }
159 };
160
161 // ============================================================================
162 // implementation
163 // ============================================================================
164
165 // ----------------------------------------------------------------------------
166 // MyApp
167 // ----------------------------------------------------------------------------
168
169 IMPLEMENT_APP(MyApp)
170
171 bool 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
186 BEGIN_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)
194 END_EVENT_TABLE()
195
196 MyFrame::MyFrame()
197 : wxFrame(NULL, wxID_ANY, wxT("TabOrder wxWidgets Sample"),
198 wxDefaultPosition, wxSize(700, 450))
199 {
200 SetIcon(wxICON(sample));
201
202 wxMenu *menuFile = new wxMenu;
203 menuFile->Append(TabOrder_About);
204 menuFile->AppendSeparator();
205 menuFile->Append(TabOrder_Quit);
206
207 wxMenu *menuNav = new wxMenu;
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"));
212
213 wxMenuBar *mbar = new wxMenuBar;
214 mbar->Append(menuFile, wxT("&File"));
215 mbar->Append(menuNav, wxT("&Navigate"));
216
217 SetMenuBar(mbar);
218
219 m_panel = new MyPanel(this);
220
221 CreateStatusBar(StatusPane_Max);
222 }
223
224 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
225 {
226 Close(true);
227 }
228
229 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
230 {
231 wxMessageBox(wxT("Tab navigation sample\n(c) 2007 Vadim Zeitlin"),
232 wxT("About TabOrder wxWidgets Sample"), wxOK, this);
233 }
234
235 void MyFrame::OnTabForward(wxCommandEvent& WXUNUSED(event))
236 {
237 DoNavigate(wxNavigationKeyEvent::IsForward | wxNavigationKeyEvent::FromTab);
238 }
239
240 void MyFrame::OnTabBackward(wxCommandEvent& WXUNUSED(event))
241 {
242 DoNavigate(wxNavigationKeyEvent::IsBackward | wxNavigationKeyEvent::FromTab);
243 }
244
245 void 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 {
257 msg.Printf(wxT("Focus is at %s"), s_windowFocus->GetName().c_str());
258 }
259 else
260 {
261 msg = wxT("No focus");
262 }
263
264 SetStatusText(msg, StatusPane_Focus);
265 }
266 }
267
268 // ----------------------------------------------------------------------------
269 // MyPanel
270 // ----------------------------------------------------------------------------
271
272 MyPanel::MyPanel(wxWindow *parent)
273 : wxPanel(parent, wxID_ANY)
274 {
275 wxNotebook *notebook = new wxNotebook(this, wxID_ANY);
276 notebook->AddPage(CreateButtonPage(notebook), wxT("Button"));
277 notebook->AddPage(CreateTextPage(notebook), wxT("Text"));
278
279 wxSizer *sizerV = new wxBoxSizer(wxVERTICAL);
280 sizerV->Add(notebook, wxSizerFlags(1).Expand());
281
282 wxListBox *lbox = new wxListBox(this, wxID_ANY);
283 lbox->AppendString(wxT("Just a"));
284 lbox->AppendString(wxT("simple"));
285 lbox->AppendString(wxT("listbox"));
286 sizerV->Add(lbox, wxSizerFlags(1).Expand());
287
288 SetSizerAndFit(sizerV);
289 }
290
291 wxWindow *MyPanel::CreateButtonPage(wxWindow *parent)
292 {
293 wxSizerFlags flagsBorder = wxSizerFlags().Border().Centre();
294
295 wxPanel *page = new wxPanel(parent);
296 wxSizer *sizerPage = new wxBoxSizer(wxHORIZONTAL);
297 sizerPage->Add(new wxButton(page, wxID_ANY, wxT("&First")), flagsBorder);
298 sizerPage->Add(new wxStaticText(page, wxID_ANY, wxT("[st&atic]")),
299 flagsBorder);
300 sizerPage->Add(new wxButton(page, wxID_ANY, wxT("&Second")), flagsBorder);
301
302 page->SetSizer(sizerPage);
303
304 return page;
305 }
306
307 wxWindow *MyPanel::CreateTextPage(wxWindow *parent)
308 {
309 wxSizerFlags flagsBorder = wxSizerFlags().Border();
310
311 wxSizer *sizerPage = new wxBoxSizer(wxVERTICAL);
312 wxPanel *page = new wxPanel(parent);
313
314 wxSizer *sizerH = new wxBoxSizer(wxHORIZONTAL);
315 sizerH->Add(new wxStaticText(page, wxID_ANY, wxT("&Label:")), flagsBorder);
316 sizerH->Add(new MyTabTextCtrl(page, wxT("TAB ignored here")), flagsBorder);
317 sizerPage->Add(sizerH, wxSizerFlags(1).Expand());
318
319 sizerH = new wxBoxSizer(wxHORIZONTAL);
320 sizerH->Add(new wxStaticText(page, wxID_ANY, wxT("&Another one:")),
321 flagsBorder);
322 sizerH->Add(new MyTabTextCtrl(page, wxT("press Tab here"), wxTE_PROCESS_TAB),
323 flagsBorder);
324 sizerPage->Add(sizerH, wxSizerFlags(1).Expand());
325
326 page->SetSizer(sizerPage);
327
328 return page;
329 }
330