send wxNavigationKeyEvent to the correct window
[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 ( m_panel->ProcessEvent(event) )
86 wxLogStatus(this, _T("Navigation event processed"));
87 else
88 wxLogStatus(this, _T("Navigation event ignored"));
89 }
90
91
92 wxPanel *m_panel;
93
94 DECLARE_EVENT_TABLE()
95 };
96
97 // and the panel taking up MyFrame client area
98 class MyPanel : public wxPanel
99 {
100 public:
101 MyPanel(wxWindow *parent);
102 };
103
104 // ============================================================================
105 // implementation
106 // ============================================================================
107
108 // ----------------------------------------------------------------------------
109 // MyApp
110 // ----------------------------------------------------------------------------
111
112 IMPLEMENT_APP(MyApp)
113
114 bool MyApp::OnInit()
115 {
116 if ( !wxApp::OnInit() )
117 return false;
118
119 MyFrame *frame = new MyFrame;
120 frame->Show(true);
121
122 return true;
123 }
124
125 // ----------------------------------------------------------------------------
126 // MyFrame
127 // ----------------------------------------------------------------------------
128
129 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
130 EVT_MENU(TabOrder_Quit, MyFrame::OnQuit)
131 EVT_MENU(TabOrder_About, MyFrame::OnAbout)
132
133 EVT_MENU(TabOrder_TabForward, MyFrame::OnTabForward)
134 EVT_MENU(TabOrder_TabBackward, MyFrame::OnTabBackward)
135
136 EVT_IDLE(MyFrame::OnIdle)
137 END_EVENT_TABLE()
138
139 MyFrame::MyFrame()
140 : wxFrame(NULL, wxID_ANY, _T("TabOrder wxWidgets Sample"),
141 wxDefaultPosition, wxSize(700, 450))
142 {
143 wxMenu *menuFile = new wxMenu;
144 menuFile->Append(TabOrder_About, _T("&About...\tF1"));
145 menuFile->AppendSeparator();
146 menuFile->Append(TabOrder_Quit, _T("E&xit\tAlt-X"), _T("Quit the sample"));
147
148 wxMenu *menuNav = new wxMenu;
149 menuNav->Append(TabOrder_TabForward, _T("Tab &forward\tCtrl-F"),
150 _T("Emulate a <Tab> press"));
151 menuNav->Append(TabOrder_TabBackward, _T("Tab &backward\tCtrl-B"),
152 _T("Emulate a <Shift-Tab> press"));
153
154 wxMenuBar *mbar = new wxMenuBar;
155 mbar->Append(menuFile, _T("&File"));
156 mbar->Append(menuNav, _T("&Navigate"));
157
158 SetMenuBar(mbar);
159
160 m_panel = new MyPanel(this);
161
162 CreateStatusBar(StatusPane_Max);
163 }
164
165 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
166 {
167 Close(true);
168 }
169
170 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
171 {
172 wxMessageBox(_T("Tab navigation sample\n(c) 2007 Vadim Zeitlin"),
173 _T("About TabOrder wxWidgets Sample"), wxOK, this);
174 }
175
176 void MyFrame::OnTabForward(wxCommandEvent& WXUNUSED(event))
177 {
178 DoNavigate(wxNavigationKeyEvent::IsForward | wxNavigationKeyEvent::FromTab);
179 }
180
181 void MyFrame::OnTabBackward(wxCommandEvent& WXUNUSED(event))
182 {
183 DoNavigate(wxNavigationKeyEvent::IsBackward | wxNavigationKeyEvent::FromTab);
184 }
185
186 void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
187 {
188 // track the window which has the focus in the status bar
189 static wxWindow *s_windowFocus = NULL;
190 wxWindow *focus = wxWindow::FindFocus();
191 if ( focus != s_windowFocus )
192 {
193 s_windowFocus = focus;
194
195 wxString msg;
196 if ( focus )
197 {
198 msg.Printf(_T("Focus is at %s"), s_windowFocus->GetName().c_str());
199 }
200 else
201 {
202 msg = _T("No focus");
203 }
204
205 SetStatusText(msg, StatusPane_Focus);
206 }
207 }
208
209 // ----------------------------------------------------------------------------
210 // MyPanel
211 // ----------------------------------------------------------------------------
212
213 MyPanel::MyPanel(wxWindow *parent)
214 : wxPanel(parent, wxID_ANY)
215 {
216 wxSizerFlags flagsBorder = wxSizerFlags().Border();
217
218 wxSizer *sizerH = new wxBoxSizer(wxHORIZONTAL);
219 sizerH->Add(new wxButton(this, wxID_ANY, _T("&First")), flagsBorder);
220 sizerH->Add(new wxButton(this, wxID_ANY, _T("&Second")), flagsBorder);
221
222 wxSizer *sizerV = new wxBoxSizer(wxVERTICAL);
223 sizerV->Add(sizerH, wxSizerFlags(1).Expand());
224 sizerV->Add(new wxListBox(this, wxID_ANY), wxSizerFlags(1).Expand());
225 SetSizerAndFit(sizerV);
226 }
227