1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Sample for testing TAB navigation
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2007 Vadim Zeitlin
7 // Licence: wxWindows license
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 #include "wx/wxprec.h"
31 #include "wx/button.h"
32 #include "wx/listbox.h"
33 #include "wx/textctrl.h"
36 #include "wx/notebook.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // menu commands and controls ids
46 TabOrder_Quit
= wxID_EXIT
,
47 TabOrder_About
= wxID_ABOUT
,
50 TabOrder_TabForward
= 200,
56 // status panes: first one is for temporary messages, the second one shows
65 // ----------------------------------------------------------------------------
66 // declarations of the classes used in this sample
67 // ----------------------------------------------------------------------------
69 // the main application class
70 class MyApp
: public wxApp
73 virtual bool OnInit();
76 // and the main sample window
77 class MyFrame
: public wxFrame
83 void OnAbout(wxCommandEvent
& event
);
84 void OnQuit(wxCommandEvent
& event
);
86 void OnTabForward(wxCommandEvent
& event
);
87 void OnTabBackward(wxCommandEvent
& event
);
89 void OnIdle(wxIdleEvent
& event
);
91 void DoNavigate(long flags
)
93 wxNavigationKeyEvent event
;
94 event
.SetFlags(flags
);
95 if ( m_panel
->ProcessEvent(event
) )
96 wxLogStatus(this, _T("Navigation event processed"));
98 wxLogStatus(this, _T("Navigation event ignored"));
103 DECLARE_EVENT_TABLE()
106 // and the panel taking up MyFrame client area
107 class MyPanel
: public wxPanel
110 MyPanel(wxWindow
*parent
);
113 wxWindow
*CreateButtonPage(wxWindow
*parent
);
114 wxWindow
*CreateTextPage(wxWindow
*parent
);
117 // a text control which checks if processing Tab presses in controls with
118 // wxTE_PROCESS_TAB style really works
119 class MyTabTextCtrl
: public wxTextCtrl
122 MyTabTextCtrl(wxWindow
*parent
, const wxString
& value
, int flags
= 0)
123 : wxTextCtrl(parent
, wxID_ANY
, value
,
124 wxDefaultPosition
, wxDefaultSize
,
127 Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyTabTextCtrl
::OnKeyDown
));
131 void OnKeyDown(wxKeyEvent
& event
)
133 if ( event
.GetKeyCode() == WXK_TAB
&&
136 _T("Let the Tab be used for navigation?"),
137 _T("wxWidgets TabOrder sample: Tab key pressed"),
138 wxICON_QUESTION
| wxYES_NO
,
142 // skip Skip() below: we consume the Tab press ourselves and so the
143 // focus shouldn't change
151 // ============================================================================
153 // ============================================================================
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
163 if ( !wxApp
::OnInit() )
166 MyFrame
*frame
= new MyFrame
;
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
177 EVT_MENU(TabOrder_Quit
, MyFrame
::OnQuit
)
178 EVT_MENU(TabOrder_About
, MyFrame
::OnAbout
)
180 EVT_MENU(TabOrder_TabForward
, MyFrame
::OnTabForward
)
181 EVT_MENU(TabOrder_TabBackward
, MyFrame
::OnTabBackward
)
183 EVT_IDLE(MyFrame
::OnIdle
)
187 : wxFrame(NULL
, wxID_ANY
, _T("TabOrder wxWidgets Sample"),
188 wxDefaultPosition
, wxSize(700, 450))
190 wxMenu
*menuFile
= new wxMenu
;
191 menuFile
->Append(TabOrder_About
);
192 menuFile
->AppendSeparator();
193 menuFile
->Append(TabOrder_Quit
);
195 wxMenu
*menuNav
= new wxMenu
;
196 menuNav
->Append(TabOrder_TabForward
, _T("Tab &forward\tCtrl-F"),
197 _T("Emulate a <Tab> press"));
198 menuNav
->Append(TabOrder_TabBackward
, _T("Tab &backward\tCtrl-B"),
199 _T("Emulate a <Shift-Tab> press"));
201 wxMenuBar
*mbar
= new wxMenuBar
;
202 mbar
->Append(menuFile
, _T("&File"));
203 mbar
->Append(menuNav
, _T("&Navigate"));
207 m_panel
= new MyPanel(this);
209 CreateStatusBar(StatusPane_Max
);
212 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
217 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
219 wxMessageBox(_T("Tab navigation sample\n(c) 2007 Vadim Zeitlin"),
220 _T("About TabOrder wxWidgets Sample"), wxOK
, this);
223 void MyFrame
::OnTabForward(wxCommandEvent
& WXUNUSED(event
))
225 DoNavigate(wxNavigationKeyEvent
::IsForward
| wxNavigationKeyEvent
::FromTab
);
228 void MyFrame
::OnTabBackward(wxCommandEvent
& WXUNUSED(event
))
230 DoNavigate(wxNavigationKeyEvent
::IsBackward
| wxNavigationKeyEvent
::FromTab
);
233 void MyFrame
::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
235 // track the window which has the focus in the status bar
236 static wxWindow
*s_windowFocus
= NULL
;
237 wxWindow
*focus
= wxWindow
::FindFocus();
238 if ( focus
!= s_windowFocus
)
240 s_windowFocus
= focus
;
245 msg
.Printf(_T("Focus is at %s"), s_windowFocus
->GetName().c_str());
249 msg
= _T("No focus");
252 SetStatusText(msg
, StatusPane_Focus
);
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 MyPanel
::MyPanel(wxWindow
*parent
)
261 : wxPanel(parent
, wxID_ANY
)
263 wxNotebook
*notebook
= new wxNotebook(this, wxID_ANY
);
264 notebook
->AddPage(CreateButtonPage(notebook
), _T("Button"));
265 notebook
->AddPage(CreateTextPage(notebook
), _T("Text"));
267 wxSizer
*sizerV
= new wxBoxSizer(wxVERTICAL
);
268 sizerV
->Add(notebook
, wxSizerFlags(1).Expand());
270 wxListBox
*lbox
= new wxListBox(this, wxID_ANY
);
271 lbox
->AppendString(_T("Just a"));
272 lbox
->AppendString(_T("simple"));
273 lbox
->AppendString(_T("listbox"));
274 sizerV
->Add(lbox
, wxSizerFlags(1).Expand());
276 SetSizerAndFit(sizerV
);
279 wxWindow
*MyPanel
::CreateButtonPage(wxWindow
*parent
)
281 wxSizerFlags flagsBorder
= wxSizerFlags().Border().Centre();
283 wxPanel
*page
= new wxPanel(parent
);
284 wxSizer
*sizerPage
= new wxBoxSizer(wxHORIZONTAL
);
285 sizerPage
->Add(new wxButton(page
, wxID_ANY
, _T("&First")), flagsBorder
);
286 sizerPage
->Add(new wxStaticText(page
, wxID_ANY
, _T("[st&atic]")),
288 sizerPage
->Add(new wxButton(page
, wxID_ANY
, _T("&Second")), flagsBorder
);
290 page
->SetSizer(sizerPage
);
295 wxWindow
*MyPanel
::CreateTextPage(wxWindow
*parent
)
297 wxSizerFlags flagsBorder
= wxSizerFlags().Border();
299 wxSizer
*sizerPage
= new wxBoxSizer(wxVERTICAL
);
300 wxPanel
*page
= new wxPanel(parent
);
302 wxSizer
*sizerH
= new wxBoxSizer(wxHORIZONTAL
);
303 sizerH
->Add(new wxStaticText(page
, wxID_ANY
, _T("&Label:")), flagsBorder
);
304 sizerH
->Add(new MyTabTextCtrl(page
, _T("TAB ignored here")), flagsBorder
);
305 sizerPage
->Add(sizerH
, wxSizerFlags(1).Expand());
307 sizerH
= new wxBoxSizer(wxHORIZONTAL
);
308 sizerH
->Add(new wxStaticText(page
, wxID_ANY
, _T("&Another one::")), flagsBorder
);
309 sizerH
->Add(new MyTabTextCtrl(page
, _T("press Tab here"), wxTE_PROCESS_TAB
),
311 sizerPage
->Add(sizerH
, wxSizerFlags(1).Expand());
313 page
->SetSizer(sizerPage
);