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