]> git.saurik.com Git - wxWidgets.git/blob - samples/taborder/taborder.cpp
miscellaneous small enhancements
[wxWidgets.git] / samples / taborder / taborder.cpp
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
25 #include "wx/app.h"
26 #include "wx/log.h"
27 #include "wx/frame.h"
28 #include "wx/menu.h"
29 #include "wx/sizer.h"
30
31 #include "wx/panel.h"
32 #include "wx/msgdlg.h"
33
34 #include "wx/button.h"
35 #include "wx/listbox.h"
36 #include "wx/stattext.h"
37 #include "wx/textctrl.h"
38 #endif
39
40 #include "wx/notebook.h"
41
42 // ----------------------------------------------------------------------------
43 // constants
44 // ----------------------------------------------------------------------------
45
46 // menu commands and controls ids
47 enum
48 {
49 // file menu
50 TabOrder_Quit = wxID_EXIT,
51 TabOrder_About = wxID_ABOUT,
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
62 enum
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
74 class MyApp : public wxApp
75 {
76 public:
77 virtual bool OnInit();
78 };
79
80 // and the main sample window
81 class MyFrame : public wxFrame
82 {
83 public:
84 MyFrame();
85
86 private:
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
95 void DoNavigate(int flags)
96 {
97 if ( m_panel->NavigateIn(flags) )
98 wxLogStatus(this, _T("Navigation event processed"));
99 else
100 wxLogStatus(this, _T("Navigation event ignored"));
101 }
102
103 wxPanel *m_panel;
104
105 DECLARE_EVENT_TABLE()
106 };
107
108 // and the panel taking up MyFrame client area
109 class MyPanel : public wxPanel
110 {
111 public:
112 MyPanel(wxWindow *parent);
113
114 private:
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
121 class MyTabTextCtrl : public wxTextCtrl
122 {
123 public:
124 MyTabTextCtrl(wxWindow *parent, const wxString& value, int flags = 0)
125 : wxTextCtrl(parent, wxID_ANY, value,
126 wxDefaultPosition, wxDefaultSize,
127 flags)
128 {
129 Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MyTabTextCtrl::OnKeyDown));
130 }
131
132 private:
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 }
151 };
152
153 // ============================================================================
154 // implementation
155 // ============================================================================
156
157 // ----------------------------------------------------------------------------
158 // MyApp
159 // ----------------------------------------------------------------------------
160
161 IMPLEMENT_APP(MyApp)
162
163 bool 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
178 BEGIN_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)
186 END_EVENT_TABLE()
187
188 MyFrame::MyFrame()
189 : wxFrame(NULL, wxID_ANY, _T("TabOrder wxWidgets Sample"),
190 wxDefaultPosition, wxSize(700, 450))
191 {
192 wxMenu *menuFile = new wxMenu;
193 menuFile->Append(TabOrder_About);
194 menuFile->AppendSeparator();
195 menuFile->Append(TabOrder_Quit);
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
209 m_panel = new MyPanel(this);
210
211 CreateStatusBar(StatusPane_Max);
212 }
213
214 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
215 {
216 Close(true);
217 }
218
219 void 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
225 void MyFrame::OnTabForward(wxCommandEvent& WXUNUSED(event))
226 {
227 DoNavigate(wxNavigationKeyEvent::IsForward | wxNavigationKeyEvent::FromTab);
228 }
229
230 void MyFrame::OnTabBackward(wxCommandEvent& WXUNUSED(event))
231 {
232 DoNavigate(wxNavigationKeyEvent::IsBackward | wxNavigationKeyEvent::FromTab);
233 }
234
235 void 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
262 MyPanel::MyPanel(wxWindow *parent)
263 : wxPanel(parent, wxID_ANY)
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
281 wxWindow *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
297 wxWindow *MyPanel::CreateTextPage(wxWindow *parent)
298 {
299 wxSizerFlags flagsBorder = wxSizerFlags().Border();
300
301 wxSizer *sizerPage = new wxBoxSizer(wxVERTICAL);
302 wxPanel *page = new wxPanel(parent);
303
304 wxSizer *sizerH = new wxBoxSizer(wxHORIZONTAL);
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());
308
309 sizerH = new wxBoxSizer(wxHORIZONTAL);
310 sizerH->Add(new wxStaticText(page, wxID_ANY, _T("&Another one:")),
311 flagsBorder);
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;
319 }
320