]>
git.saurik.com Git - wxWidgets.git/blob - samples/taborder/taborder.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Sample for testing TAB navigation
4 // Author: Vadim Zeitlin
5 // Copyright: (c) 2007 Vadim Zeitlin
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 #include "wx/wxprec.h"
31 #include "wx/msgdlg.h"
33 #include "wx/button.h"
34 #include "wx/listbox.h"
35 #include "wx/stattext.h"
36 #include "wx/textctrl.h"
39 #include "wx/notebook.h"
41 #ifndef wxHAS_IMAGES_IN_RESOURCES
42 #include "../sample.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // menu commands and controls ids
54 TabOrder_Quit
= wxID_EXIT
,
55 TabOrder_About
= wxID_ABOUT
,
58 TabOrder_TabForward
= 200,
64 // status panes: first one is for temporary messages, the second one shows
73 // ----------------------------------------------------------------------------
74 // declarations of the classes used in this sample
75 // ----------------------------------------------------------------------------
77 // the main application class
78 class MyApp
: public wxApp
81 virtual bool OnInit();
84 // and the main sample window
85 class MyFrame
: public wxFrame
91 void OnAbout(wxCommandEvent
& event
);
92 void OnQuit(wxCommandEvent
& event
);
94 void OnTabForward(wxCommandEvent
& event
);
95 void OnTabBackward(wxCommandEvent
& event
);
97 void OnIdle(wxIdleEvent
& event
);
99 void DoNavigate(int flags
)
101 if ( m_panel
->NavigateIn(flags
) )
103 wxLogStatus(this, wxT("Navigation event processed"));
107 wxLogStatus(this, wxT("Navigation event ignored"));
113 DECLARE_EVENT_TABLE()
116 // and the panel taking up MyFrame client area
117 class MyPanel
: public wxPanel
120 MyPanel(wxWindow
*parent
);
123 wxWindow
*CreateButtonPage(wxWindow
*parent
);
124 wxWindow
*CreateTextPage(wxWindow
*parent
);
127 // a text control which checks if processing Tab presses in controls with
128 // wxTE_PROCESS_TAB style really works
129 class MyTabTextCtrl
: public wxTextCtrl
132 MyTabTextCtrl(wxWindow
*parent
, const wxString
& value
, int flags
= 0)
133 : wxTextCtrl(parent
, wxID_ANY
, value
,
134 wxDefaultPosition
, wxDefaultSize
,
137 Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyTabTextCtrl::OnKeyDown
));
141 void OnKeyDown(wxKeyEvent
& event
)
143 if ( event
.GetKeyCode() == WXK_TAB
&&
146 wxT("Let the Tab be used for navigation?"),
147 wxT("wxWidgets TabOrder sample: Tab key pressed"),
148 wxICON_QUESTION
| wxYES_NO
,
152 // skip Skip() below: we consume the Tab press ourselves and so the
153 // focus shouldn't change
161 // ============================================================================
163 // ============================================================================
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
173 if ( !wxApp::OnInit() )
176 MyFrame
*frame
= new MyFrame
;
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
187 EVT_MENU(TabOrder_Quit
, MyFrame::OnQuit
)
188 EVT_MENU(TabOrder_About
, MyFrame::OnAbout
)
190 EVT_MENU(TabOrder_TabForward
, MyFrame::OnTabForward
)
191 EVT_MENU(TabOrder_TabBackward
, MyFrame::OnTabBackward
)
193 EVT_IDLE(MyFrame::OnIdle
)
197 : wxFrame(NULL
, wxID_ANY
, wxT("TabOrder wxWidgets Sample"),
198 wxDefaultPosition
, wxSize(700, 450))
200 SetIcon(wxICON(sample
));
202 wxMenu
*menuFile
= new wxMenu
;
203 menuFile
->Append(TabOrder_About
);
204 menuFile
->AppendSeparator();
205 menuFile
->Append(TabOrder_Quit
);
207 wxMenu
*menuNav
= new wxMenu
;
208 menuNav
->Append(TabOrder_TabForward
, wxT("Tab &forward\tCtrl-F"),
209 wxT("Emulate a <Tab> press"));
210 menuNav
->Append(TabOrder_TabBackward
, wxT("Tab &backward\tCtrl-B"),
211 wxT("Emulate a <Shift-Tab> press"));
213 wxMenuBar
*mbar
= new wxMenuBar
;
214 mbar
->Append(menuFile
, wxT("&File"));
215 mbar
->Append(menuNav
, wxT("&Navigate"));
219 m_panel
= new MyPanel(this);
221 CreateStatusBar(StatusPane_Max
);
224 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
229 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
231 wxMessageBox(wxT("Tab navigation sample\n(c) 2007 Vadim Zeitlin"),
232 wxT("About TabOrder wxWidgets Sample"), wxOK
, this);
235 void MyFrame::OnTabForward(wxCommandEvent
& WXUNUSED(event
))
237 DoNavigate(wxNavigationKeyEvent::IsForward
| wxNavigationKeyEvent::FromTab
);
240 void MyFrame::OnTabBackward(wxCommandEvent
& WXUNUSED(event
))
242 DoNavigate(wxNavigationKeyEvent::IsBackward
| wxNavigationKeyEvent::FromTab
);
245 void MyFrame::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
247 // track the window which has the focus in the status bar
248 static wxWindow
*s_windowFocus
= NULL
;
249 wxWindow
*focus
= wxWindow::FindFocus();
250 if ( focus
!= s_windowFocus
)
252 s_windowFocus
= focus
;
257 msg
.Printf(wxT("Focus is at %s"), s_windowFocus
->GetName().c_str());
261 msg
= wxT("No focus");
264 SetStatusText(msg
, StatusPane_Focus
);
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 MyPanel::MyPanel(wxWindow
*parent
)
273 : wxPanel(parent
, wxID_ANY
)
275 wxNotebook
*notebook
= new wxNotebook(this, wxID_ANY
);
276 notebook
->AddPage(CreateButtonPage(notebook
), wxT("Button"));
277 notebook
->AddPage(CreateTextPage(notebook
), wxT("Text"));
279 wxSizer
*sizerV
= new wxBoxSizer(wxVERTICAL
);
280 sizerV
->Add(notebook
, wxSizerFlags(1).Expand());
282 wxListBox
*lbox
= new wxListBox(this, wxID_ANY
);
283 lbox
->AppendString(wxT("Just a"));
284 lbox
->AppendString(wxT("simple"));
285 lbox
->AppendString(wxT("listbox"));
286 sizerV
->Add(lbox
, wxSizerFlags(1).Expand());
288 SetSizerAndFit(sizerV
);
291 wxWindow
*MyPanel::CreateButtonPage(wxWindow
*parent
)
293 wxSizerFlags flagsBorder
= wxSizerFlags().Border().Centre();
295 wxPanel
*page
= new wxPanel(parent
);
296 wxSizer
*sizerPage
= new wxBoxSizer(wxHORIZONTAL
);
297 sizerPage
->Add(new wxButton(page
, wxID_ANY
, wxT("&First")), flagsBorder
);
298 sizerPage
->Add(new wxStaticText(page
, wxID_ANY
, wxT("[st&atic]")),
300 sizerPage
->Add(new wxButton(page
, wxID_ANY
, wxT("&Second")), flagsBorder
);
302 page
->SetSizer(sizerPage
);
307 wxWindow
*MyPanel::CreateTextPage(wxWindow
*parent
)
309 wxSizerFlags flagsBorder
= wxSizerFlags().Border();
311 wxSizer
*sizerPage
= new wxBoxSizer(wxVERTICAL
);
312 wxPanel
*page
= new wxPanel(parent
);
314 wxSizer
*sizerH
= new wxBoxSizer(wxHORIZONTAL
);
315 sizerH
->Add(new wxStaticText(page
, wxID_ANY
, wxT("&Label:")), flagsBorder
);
316 sizerH
->Add(new MyTabTextCtrl(page
, wxT("TAB ignored here")), flagsBorder
);
317 sizerPage
->Add(sizerH
, wxSizerFlags(1).Expand());
319 sizerH
= new wxBoxSizer(wxHORIZONTAL
);
320 sizerH
->Add(new wxStaticText(page
, wxID_ANY
, wxT("&Another one:")),
322 sizerH
->Add(new MyTabTextCtrl(page
, wxT("press Tab here"), wxTE_PROCESS_TAB
),
324 sizerPage
->Add(sizerH
, wxSizerFlags(1).Expand());
326 page
->SetSizer(sizerPage
);