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"
32 #include "wx/msgdlg.h"
34 #include "wx/button.h"
35 #include "wx/listbox.h"
36 #include "wx/stattext.h"
37 #include "wx/textctrl.h"
40 #include "wx/notebook.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // menu commands and controls ids
50 TabOrder_Quit
= wxID_EXIT
,
51 TabOrder_About
= wxID_ABOUT
,
54 TabOrder_TabForward
= 200,
60 // status panes: first one is for temporary messages, the second one shows
69 // ----------------------------------------------------------------------------
70 // declarations of the classes used in this sample
71 // ----------------------------------------------------------------------------
73 // the main application class
74 class MyApp
: public wxApp
77 virtual bool OnInit();
80 // and the main sample window
81 class MyFrame
: public wxFrame
87 void OnAbout(wxCommandEvent
& event
);
88 void OnQuit(wxCommandEvent
& event
);
90 void OnTabForward(wxCommandEvent
& event
);
91 void OnTabBackward(wxCommandEvent
& event
);
93 void OnIdle(wxIdleEvent
& event
);
95 void DoNavigate(long flags
)
97 wxNavigationKeyEvent event
;
98 event
.SetFlags(flags
);
99 if ( m_panel
->ProcessEvent(event
) )
100 wxLogStatus(this, _T("Navigation event processed"));
102 wxLogStatus(this, _T("Navigation event ignored"));
107 DECLARE_EVENT_TABLE()
110 // and the panel taking up MyFrame client area
111 class MyPanel
: public wxPanel
114 MyPanel(wxWindow
*parent
);
117 wxWindow
*CreateButtonPage(wxWindow
*parent
);
118 wxWindow
*CreateTextPage(wxWindow
*parent
);
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
126 MyTabTextCtrl(wxWindow
*parent
, const wxString
& value
, int flags
= 0)
127 : wxTextCtrl(parent
, wxID_ANY
, value
,
128 wxDefaultPosition
, wxDefaultSize
,
131 Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyTabTextCtrl::OnKeyDown
));
135 void OnKeyDown(wxKeyEvent
& event
)
137 if ( event
.GetKeyCode() == WXK_TAB
&&
140 _T("Let the Tab be used for navigation?"),
141 _T("wxWidgets TabOrder sample: Tab key pressed"),
142 wxICON_QUESTION
| wxYES_NO
,
146 // skip Skip() below: we consume the Tab press ourselves and so the
147 // focus shouldn't change
155 // ============================================================================
157 // ============================================================================
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
167 if ( !wxApp::OnInit() )
170 MyFrame
*frame
= new MyFrame
;
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
181 EVT_MENU(TabOrder_Quit
, MyFrame::OnQuit
)
182 EVT_MENU(TabOrder_About
, MyFrame::OnAbout
)
184 EVT_MENU(TabOrder_TabForward
, MyFrame::OnTabForward
)
185 EVT_MENU(TabOrder_TabBackward
, MyFrame::OnTabBackward
)
187 EVT_IDLE(MyFrame::OnIdle
)
191 : wxFrame(NULL
, wxID_ANY
, _T("TabOrder wxWidgets Sample"),
192 wxDefaultPosition
, wxSize(700, 450))
194 wxMenu
*menuFile
= new wxMenu
;
195 menuFile
->Append(TabOrder_About
);
196 menuFile
->AppendSeparator();
197 menuFile
->Append(TabOrder_Quit
);
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"));
205 wxMenuBar
*mbar
= new wxMenuBar
;
206 mbar
->Append(menuFile
, _T("&File"));
207 mbar
->Append(menuNav
, _T("&Navigate"));
211 m_panel
= new MyPanel(this);
213 CreateStatusBar(StatusPane_Max
);
216 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
221 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
223 wxMessageBox(_T("Tab navigation sample\n(c) 2007 Vadim Zeitlin"),
224 _T("About TabOrder wxWidgets Sample"), wxOK
, this);
227 void MyFrame::OnTabForward(wxCommandEvent
& WXUNUSED(event
))
229 DoNavigate(wxNavigationKeyEvent::IsForward
| wxNavigationKeyEvent::FromTab
);
232 void MyFrame::OnTabBackward(wxCommandEvent
& WXUNUSED(event
))
234 DoNavigate(wxNavigationKeyEvent::IsBackward
| wxNavigationKeyEvent::FromTab
);
237 void MyFrame::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
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
)
244 s_windowFocus
= focus
;
249 msg
.Printf(_T("Focus is at %s"), s_windowFocus
->GetName().c_str());
253 msg
= _T("No focus");
256 SetStatusText(msg
, StatusPane_Focus
);
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
264 MyPanel::MyPanel(wxWindow
*parent
)
265 : wxPanel(parent
, wxID_ANY
)
267 wxNotebook
*notebook
= new wxNotebook(this, wxID_ANY
);
268 notebook
->AddPage(CreateButtonPage(notebook
), _T("Button"));
269 notebook
->AddPage(CreateTextPage(notebook
), _T("Text"));
271 wxSizer
*sizerV
= new wxBoxSizer(wxVERTICAL
);
272 sizerV
->Add(notebook
, wxSizerFlags(1).Expand());
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());
280 SetSizerAndFit(sizerV
);
283 wxWindow
*MyPanel::CreateButtonPage(wxWindow
*parent
)
285 wxSizerFlags flagsBorder
= wxSizerFlags().Border().Centre();
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]")),
292 sizerPage
->Add(new wxButton(page
, wxID_ANY
, _T("&Second")), flagsBorder
);
294 page
->SetSizer(sizerPage
);
299 wxWindow
*MyPanel::CreateTextPage(wxWindow
*parent
)
301 wxSizerFlags flagsBorder
= wxSizerFlags().Border();
303 wxSizer
*sizerPage
= new wxBoxSizer(wxVERTICAL
);
304 wxPanel
*page
= new wxPanel(parent
);
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());
311 sizerH
= new wxBoxSizer(wxHORIZONTAL
);
312 sizerH
->Add(new wxStaticText(page
, wxID_ANY
, _T("&Another one:")),
314 sizerH
->Add(new MyTabTextCtrl(page
, _T("press Tab here"), wxTE_PROCESS_TAB
),
316 sizerPage
->Add(sizerH
, wxSizerFlags(1).Expand());
318 page
->SetSizer(sizerPage
);