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