added taborder sample
[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/wx.h"
26 #endif
27
28 // ----------------------------------------------------------------------------
29 // constants
30 // ----------------------------------------------------------------------------
31
32 // menu commands and controls ids
33 enum
34 {
35 // file menu
36 TabOrder_Quit = 100,
37 TabOrder_About,
38
39 // navigation menu
40 TabOrder_TabForward = 200,
41 TabOrder_TabBackward,
42
43 TabOrder_Max
44 };
45
46 // status panes: first one is for temporary messages, the second one shows
47 // current focus
48 enum
49 {
50 StatusPane_Default,
51 StatusPane_Focus,
52 StatusPane_Max
53 };
54
55 // ----------------------------------------------------------------------------
56 // declarations of the classes used in this sample
57 // ----------------------------------------------------------------------------
58
59 // the main application class
60 class MyApp : public wxApp
61 {
62 public:
63 virtual bool OnInit();
64 };
65
66 // and the main sample window
67 class MyFrame : public wxFrame
68 {
69 public:
70 MyFrame();
71
72 private:
73 void OnAbout(wxCommandEvent& event);
74 void OnQuit(wxCommandEvent& event);
75
76 void OnTabForward(wxCommandEvent& event);
77 void OnTabBackward(wxCommandEvent& event);
78
79 void OnIdle(wxIdleEvent& event);
80
81 void DoNavigate(long flags)
82 {
83 wxNavigationKeyEvent event;
84 event.SetFlags(flags);
85 if ( ProcessEvent(event) )
86 wxLogStatus(this, _T("Navigation event processed"));
87 else
88 wxLogStatus(this, _T("Navigation event ignored"));
89 }
90
91 DECLARE_EVENT_TABLE()
92 };
93
94 // and the panel taking up MyFrame client area
95 class MyPanel : public wxPanel
96 {
97 public:
98 MyPanel(wxWindow *parent);
99 };
100
101 // ============================================================================
102 // implementation
103 // ============================================================================
104
105 // ----------------------------------------------------------------------------
106 // MyApp
107 // ----------------------------------------------------------------------------
108
109 IMPLEMENT_APP(MyApp)
110
111 bool MyApp::OnInit()
112 {
113 if ( !wxApp::OnInit() )
114 return false;
115
116 MyFrame *frame = new MyFrame;
117 frame->Show(true);
118
119 return true;
120 }
121
122 // ----------------------------------------------------------------------------
123 // MyFrame
124 // ----------------------------------------------------------------------------
125
126 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
127 EVT_MENU(TabOrder_Quit, MyFrame::OnQuit)
128 EVT_MENU(TabOrder_About, MyFrame::OnAbout)
129
130 EVT_MENU(TabOrder_TabForward, MyFrame::OnTabForward)
131 EVT_MENU(TabOrder_TabBackward, MyFrame::OnTabBackward)
132
133 EVT_IDLE(MyFrame::OnIdle)
134 END_EVENT_TABLE()
135
136 MyFrame::MyFrame()
137 : wxFrame(NULL, wxID_ANY, _T("TabOrder wxWidgets Sample"),
138 wxDefaultPosition, wxSize(700, 450))
139 {
140 wxMenu *menuFile = new wxMenu;
141 menuFile->Append(TabOrder_About, _T("&About...\tF1"));
142 menuFile->AppendSeparator();
143 menuFile->Append(TabOrder_Quit, _T("E&xit\tAlt-X"), _T("Quit the sample"));
144
145 wxMenu *menuNav = new wxMenu;
146 menuNav->Append(TabOrder_TabForward, _T("Tab &forward\tCtrl-F"),
147 _T("Emulate a <Tab> press"));
148 menuNav->Append(TabOrder_TabBackward, _T("Tab &backward\tCtrl-B"),
149 _T("Emulate a <Shift-Tab> press"));
150
151 wxMenuBar *mbar = new wxMenuBar;
152 mbar->Append(menuFile, _T("&File"));
153 mbar->Append(menuNav, _T("&Navigate"));
154
155 SetMenuBar(mbar);
156
157 new MyPanel(this);
158
159 CreateStatusBar(StatusPane_Max);
160 }
161
162 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
163 {
164 Close(true);
165 }
166
167 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
168 {
169 wxMessageBox(_T("Tab navigation sample\n(c) 2007 Vadim Zeitlin"),
170 _T("About TabOrder wxWidgets Sample"), wxOK, this);
171 }
172
173 void MyFrame::OnTabForward(wxCommandEvent& WXUNUSED(event))
174 {
175 DoNavigate(wxNavigationKeyEvent::IsForward | wxNavigationKeyEvent::FromTab);
176 }
177
178 void MyFrame::OnTabBackward(wxCommandEvent& WXUNUSED(event))
179 {
180 DoNavigate(wxNavigationKeyEvent::IsBackward | wxNavigationKeyEvent::FromTab);
181 }
182
183 void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
184 {
185 // track the window which has the focus in the status bar
186 static wxWindow *s_windowFocus = NULL;
187 wxWindow *focus = wxWindow::FindFocus();
188 if ( focus != s_windowFocus )
189 {
190 s_windowFocus = focus;
191
192 wxString msg;
193 if ( focus )
194 {
195 msg.Printf(_T("Focus is at %s"), s_windowFocus->GetName().c_str());
196 }
197 else
198 {
199 msg = _T("No focus");
200 }
201
202 SetStatusText(msg, StatusPane_Focus);
203 }
204 }
205
206 // ----------------------------------------------------------------------------
207 // MyPanel
208 // ----------------------------------------------------------------------------
209
210 MyPanel::MyPanel(wxWindow *parent)
211 : wxPanel(parent, wxID_ANY)
212 {
213 wxSizerFlags flagsBorder = wxSizerFlags().Border();
214
215 wxSizer *sizerH = new wxBoxSizer(wxHORIZONTAL);
216 sizerH->Add(new wxButton(this, wxID_ANY, _T("&First")), flagsBorder);
217 sizerH->Add(new wxButton(this, wxID_ANY, _T("&Second")), flagsBorder);
218
219 wxSizer *sizerV = new wxBoxSizer(wxVERTICAL);
220 sizerV->Add(sizerH, wxSizerFlags(1).Expand());
221 sizerV->Add(new wxListBox(this, wxID_ANY), wxSizerFlags(1).Expand());
222 SetSizerAndFit(sizerV);
223 }
224