]>
git.saurik.com Git - wxWidgets.git/blob - samples/taborder/taborder.cpp
934dedcd3d2366c3caa0ce4f3b149c4305625f13
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Sample for testing TAB navigation
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2007 Vadim Zeitlin
7 // Licence: wxWindows licence
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 #ifndef wxHAS_IMAGES_IN_RESOURCES
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
) )
104 wxLogStatus(this, wxT("Navigation event processed"));
108 wxLogStatus(this, wxT("Navigation event ignored"));
114 DECLARE_EVENT_TABLE()
117 // and the panel taking up MyFrame client area
118 class MyPanel
: public wxPanel
121 MyPanel(wxWindow
*parent
);
124 wxWindow
*CreateButtonPage(wxWindow
*parent
);
125 wxWindow
*CreateTextPage(wxWindow
*parent
);
128 // a text control which checks if processing Tab presses in controls with
129 // wxTE_PROCESS_TAB style really works
130 class MyTabTextCtrl
: public wxTextCtrl
133 MyTabTextCtrl(wxWindow
*parent
, const wxString
& value
, int flags
= 0)
134 : wxTextCtrl(parent
, wxID_ANY
, value
,
135 wxDefaultPosition
, wxDefaultSize
,
138 Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyTabTextCtrl::OnKeyDown
));
142 void OnKeyDown(wxKeyEvent
& event
)
144 if ( event
.GetKeyCode() == WXK_TAB
&&
147 wxT("Let the Tab be used for navigation?"),
148 wxT("wxWidgets TabOrder sample: Tab key pressed"),
149 wxICON_QUESTION
| wxYES_NO
,
153 // skip Skip() below: we consume the Tab press ourselves and so the
154 // focus shouldn't change
162 // ============================================================================
164 // ============================================================================
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
174 if ( !wxApp::OnInit() )
177 MyFrame
*frame
= new MyFrame
;
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
188 EVT_MENU(TabOrder_Quit
, MyFrame::OnQuit
)
189 EVT_MENU(TabOrder_About
, MyFrame::OnAbout
)
191 EVT_MENU(TabOrder_TabForward
, MyFrame::OnTabForward
)
192 EVT_MENU(TabOrder_TabBackward
, MyFrame::OnTabBackward
)
194 EVT_IDLE(MyFrame::OnIdle
)
198 : wxFrame(NULL
, wxID_ANY
, wxT("TabOrder wxWidgets Sample"),
199 wxDefaultPosition
, wxSize(700, 450))
201 SetIcon(wxICON(sample
));
203 wxMenu
*menuFile
= new wxMenu
;
204 menuFile
->Append(TabOrder_About
);
205 menuFile
->AppendSeparator();
206 menuFile
->Append(TabOrder_Quit
);
208 wxMenu
*menuNav
= new wxMenu
;
209 menuNav
->Append(TabOrder_TabForward
, wxT("Tab &forward\tCtrl-F"),
210 wxT("Emulate a <Tab> press"));
211 menuNav
->Append(TabOrder_TabBackward
, wxT("Tab &backward\tCtrl-B"),
212 wxT("Emulate a <Shift-Tab> press"));
214 wxMenuBar
*mbar
= new wxMenuBar
;
215 mbar
->Append(menuFile
, wxT("&File"));
216 mbar
->Append(menuNav
, wxT("&Navigate"));
220 m_panel
= new MyPanel(this);
222 CreateStatusBar(StatusPane_Max
);
225 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
230 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
232 wxMessageBox(wxT("Tab navigation sample\n(c) 2007 Vadim Zeitlin"),
233 wxT("About TabOrder wxWidgets Sample"), wxOK
, this);
236 void MyFrame::OnTabForward(wxCommandEvent
& WXUNUSED(event
))
238 DoNavigate(wxNavigationKeyEvent::IsForward
| wxNavigationKeyEvent::FromTab
);
241 void MyFrame::OnTabBackward(wxCommandEvent
& WXUNUSED(event
))
243 DoNavigate(wxNavigationKeyEvent::IsBackward
| wxNavigationKeyEvent::FromTab
);
246 void MyFrame::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
248 // track the window which has the focus in the status bar
249 static wxWindow
*s_windowFocus
= NULL
;
250 wxWindow
*focus
= wxWindow::FindFocus();
251 if ( focus
!= s_windowFocus
)
253 s_windowFocus
= focus
;
258 msg
.Printf(wxT("Focus is at %s"), s_windowFocus
->GetName().c_str());
262 msg
= wxT("No focus");
265 SetStatusText(msg
, StatusPane_Focus
);
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 MyPanel::MyPanel(wxWindow
*parent
)
274 : wxPanel(parent
, wxID_ANY
)
276 wxNotebook
*notebook
= new wxNotebook(this, wxID_ANY
);
277 notebook
->AddPage(CreateButtonPage(notebook
), wxT("Button"));
278 notebook
->AddPage(CreateTextPage(notebook
), wxT("Text"));
280 wxSizer
*sizerV
= new wxBoxSizer(wxVERTICAL
);
281 sizerV
->Add(notebook
, wxSizerFlags(1).Expand());
283 wxListBox
*lbox
= new wxListBox(this, wxID_ANY
);
284 lbox
->AppendString(wxT("Just a"));
285 lbox
->AppendString(wxT("simple"));
286 lbox
->AppendString(wxT("listbox"));
287 sizerV
->Add(lbox
, wxSizerFlags(1).Expand());
289 SetSizerAndFit(sizerV
);
292 wxWindow
*MyPanel::CreateButtonPage(wxWindow
*parent
)
294 wxSizerFlags flagsBorder
= wxSizerFlags().Border().Centre();
296 wxPanel
*page
= new wxPanel(parent
);
297 wxSizer
*sizerPage
= new wxBoxSizer(wxHORIZONTAL
);
298 sizerPage
->Add(new wxButton(page
, wxID_ANY
, wxT("&First")), flagsBorder
);
299 sizerPage
->Add(new wxStaticText(page
, wxID_ANY
, wxT("[st&atic]")),
301 sizerPage
->Add(new wxButton(page
, wxID_ANY
, wxT("&Second")), flagsBorder
);
303 page
->SetSizer(sizerPage
);
308 wxWindow
*MyPanel::CreateTextPage(wxWindow
*parent
)
310 wxSizerFlags flagsBorder
= wxSizerFlags().Border();
312 wxSizer
*sizerPage
= new wxBoxSizer(wxVERTICAL
);
313 wxPanel
*page
= new wxPanel(parent
);
315 wxSizer
*sizerH
= new wxBoxSizer(wxHORIZONTAL
);
316 sizerH
->Add(new wxStaticText(page
, wxID_ANY
, wxT("&Label:")), flagsBorder
);
317 sizerH
->Add(new MyTabTextCtrl(page
, wxT("TAB ignored here")), flagsBorder
);
318 sizerPage
->Add(sizerH
, wxSizerFlags(1).Expand());
320 sizerH
= new wxBoxSizer(wxHORIZONTAL
);
321 sizerH
->Add(new wxStaticText(page
, wxID_ANY
, wxT("&Another one:")),
323 sizerH
->Add(new MyTabTextCtrl(page
, wxT("press Tab here"), wxTE_PROCESS_TAB
),
325 sizerPage
->Add(sizerH
, wxSizerFlags(1).Expand());
327 page
->SetSizer(sizerPage
);