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