]>
git.saurik.com Git - wxWidgets.git/blob - samples/taborder/taborder.cpp
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"
43 #include "../sample.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // menu commands and controls ids
55 TabOrder_Quit
= wxID_EXIT
,
56 TabOrder_About
= wxID_ABOUT
,
59 TabOrder_TabForward
= 200,
65 // status panes: first one is for temporary messages, the second one shows
74 // ----------------------------------------------------------------------------
75 // declarations of the classes used in this sample
76 // ----------------------------------------------------------------------------
78 // the main application class
79 class MyApp
: public wxApp
82 virtual bool OnInit();
85 // and the main sample window
86 class MyFrame
: public wxFrame
92 void OnAbout(wxCommandEvent
& event
);
93 void OnQuit(wxCommandEvent
& event
);
95 void OnTabForward(wxCommandEvent
& event
);
96 void OnTabBackward(wxCommandEvent
& event
);
98 void OnIdle(wxIdleEvent
& event
);
100 void DoNavigate(int flags
)
102 if ( m_panel
->NavigateIn(flags
) )
103 wxLogStatus(this, _T("Navigation event processed"));
105 wxLogStatus(this, _T("Navigation event ignored"));
110 DECLARE_EVENT_TABLE()
113 // and the panel taking up MyFrame client area
114 class MyPanel
: public wxPanel
117 MyPanel(wxWindow
*parent
);
120 wxWindow
*CreateButtonPage(wxWindow
*parent
);
121 wxWindow
*CreateTextPage(wxWindow
*parent
);
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
129 MyTabTextCtrl(wxWindow
*parent
, const wxString
& value
, int flags
= 0)
130 : wxTextCtrl(parent
, wxID_ANY
, value
,
131 wxDefaultPosition
, wxDefaultSize
,
134 Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyTabTextCtrl::OnKeyDown
));
138 void OnKeyDown(wxKeyEvent
& event
)
140 if ( event
.GetKeyCode() == WXK_TAB
&&
143 _T("Let the Tab be used for navigation?"),
144 _T("wxWidgets TabOrder sample: Tab key pressed"),
145 wxICON_QUESTION
| wxYES_NO
,
149 // skip Skip() below: we consume the Tab press ourselves and so the
150 // focus shouldn't change
158 // ============================================================================
160 // ============================================================================
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
170 if ( !wxApp::OnInit() )
173 MyFrame
*frame
= new MyFrame
;
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
184 EVT_MENU(TabOrder_Quit
, MyFrame::OnQuit
)
185 EVT_MENU(TabOrder_About
, MyFrame::OnAbout
)
187 EVT_MENU(TabOrder_TabForward
, MyFrame::OnTabForward
)
188 EVT_MENU(TabOrder_TabBackward
, MyFrame::OnTabBackward
)
190 EVT_IDLE(MyFrame::OnIdle
)
194 : wxFrame(NULL
, wxID_ANY
, _T("TabOrder wxWidgets Sample"),
195 wxDefaultPosition
, wxSize(700, 450))
197 SetIcon(wxICON(sample
));
199 wxMenu
*menuFile
= new wxMenu
;
200 menuFile
->Append(TabOrder_About
);
201 menuFile
->AppendSeparator();
202 menuFile
->Append(TabOrder_Quit
);
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"));
210 wxMenuBar
*mbar
= new wxMenuBar
;
211 mbar
->Append(menuFile
, _T("&File"));
212 mbar
->Append(menuNav
, _T("&Navigate"));
216 m_panel
= new MyPanel(this);
218 CreateStatusBar(StatusPane_Max
);
221 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
226 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
228 wxMessageBox(_T("Tab navigation sample\n(c) 2007 Vadim Zeitlin"),
229 _T("About TabOrder wxWidgets Sample"), wxOK
, this);
232 void MyFrame::OnTabForward(wxCommandEvent
& WXUNUSED(event
))
234 DoNavigate(wxNavigationKeyEvent::IsForward
| wxNavigationKeyEvent::FromTab
);
237 void MyFrame::OnTabBackward(wxCommandEvent
& WXUNUSED(event
))
239 DoNavigate(wxNavigationKeyEvent::IsBackward
| wxNavigationKeyEvent::FromTab
);
242 void MyFrame::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
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
)
249 s_windowFocus
= focus
;
254 msg
.Printf(_T("Focus is at %s"), s_windowFocus
->GetName().c_str());
258 msg
= _T("No focus");
261 SetStatusText(msg
, StatusPane_Focus
);
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 MyPanel::MyPanel(wxWindow
*parent
)
270 : wxPanel(parent
, wxID_ANY
)
272 wxNotebook
*notebook
= new wxNotebook(this, wxID_ANY
);
273 notebook
->AddPage(CreateButtonPage(notebook
), _T("Button"));
274 notebook
->AddPage(CreateTextPage(notebook
), _T("Text"));
276 wxSizer
*sizerV
= new wxBoxSizer(wxVERTICAL
);
277 sizerV
->Add(notebook
, wxSizerFlags(1).Expand());
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());
285 SetSizerAndFit(sizerV
);
288 wxWindow
*MyPanel::CreateButtonPage(wxWindow
*parent
)
290 wxSizerFlags flagsBorder
= wxSizerFlags().Border().Centre();
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]")),
297 sizerPage
->Add(new wxButton(page
, wxID_ANY
, _T("&Second")), flagsBorder
);
299 page
->SetSizer(sizerPage
);
304 wxWindow
*MyPanel::CreateTextPage(wxWindow
*parent
)
306 wxSizerFlags flagsBorder
= wxSizerFlags().Border();
308 wxSizer
*sizerPage
= new wxBoxSizer(wxVERTICAL
);
309 wxPanel
*page
= new wxPanel(parent
);
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());
316 sizerH
= new wxBoxSizer(wxHORIZONTAL
);
317 sizerH
->Add(new wxStaticText(page
, wxID_ANY
, _T("&Another one:")),
319 sizerH
->Add(new MyTabTextCtrl(page
, _T("press Tab here"), wxTE_PROCESS_TAB
),
321 sizerPage
->Add(sizerH
, wxSizerFlags(1).Expand());
323 page
->SetSizer(sizerPage
);