always call SetIcon() on the main frame of the sample; some small cleanup
[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 #ifndef __WXMSW__
43 #include "../sample.xpm"
44 #endif
45
46
47 // ----------------------------------------------------------------------------
48 // constants
49 // ----------------------------------------------------------------------------
50
51 // menu commands and controls ids
52 enum
53 {
54 // file menu
55 TabOrder_Quit = wxID_EXIT,
56 TabOrder_About = wxID_ABOUT,
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
100 void DoNavigate(int flags)
101 {
102 if ( m_panel->NavigateIn(flags) )
103 wxLogStatus(this, _T("Navigation event processed"));
104 else
105 wxLogStatus(this, _T("Navigation event ignored"));
106 }
107
108 wxPanel *m_panel;
109
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);
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,
132 flags)
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 }
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 {
197 SetIcon(wxICON(sample));
198
199 wxMenu *menuFile = new wxMenu;
200 menuFile->Append(TabOrder_About);
201 menuFile->AppendSeparator();
202 menuFile->Append(TabOrder_Quit);
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
216 m_panel = new MyPanel(this);
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)
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)
305 {
306 wxSizerFlags flagsBorder = wxSizerFlags().Border();
307
308 wxSizer *sizerPage = new wxBoxSizer(wxVERTICAL);
309 wxPanel *page = new wxPanel(parent);
310
311 wxSizer *sizerH = new wxBoxSizer(wxHORIZONTAL);
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());
315
316 sizerH = new wxBoxSizer(wxHORIZONTAL);
317 sizerH->Add(new wxStaticText(page, wxID_ANY, _T("&Another one:")),
318 flagsBorder);
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;
326 }
327