]>
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" 
  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(int flags
) 
  97         if ( m_panel
->NavigateIn(flags
) ) 
  98             wxLogStatus(this, _T("Navigation event processed")); 
 100             wxLogStatus(this, _T("Navigation event ignored")); 
 105     DECLARE_EVENT_TABLE() 
 108 // and the panel taking up MyFrame client area 
 109 class MyPanel 
: public wxPanel
 
 112     MyPanel(wxWindow 
*parent
); 
 115     wxWindow 
*CreateButtonPage(wxWindow 
*parent
); 
 116     wxWindow 
*CreateTextPage(wxWindow 
*parent
); 
 119 // a text control which checks if processing Tab presses in controls with 
 120 // wxTE_PROCESS_TAB style really works 
 121 class MyTabTextCtrl 
: public wxTextCtrl
 
 124     MyTabTextCtrl(wxWindow 
*parent
, const wxString
& value
, int flags 
= 0) 
 125         : wxTextCtrl(parent
, wxID_ANY
, value
, 
 126                      wxDefaultPosition
, wxDefaultSize
, 
 129         Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyTabTextCtrl::OnKeyDown
)); 
 133     void OnKeyDown(wxKeyEvent
& event
) 
 135         if ( event
.GetKeyCode() == WXK_TAB 
&& 
 138                     _T("Let the Tab be used for navigation?"), 
 139                     _T("wxWidgets TabOrder sample: Tab key pressed"), 
 140                     wxICON_QUESTION 
| wxYES_NO
, 
 144             // skip Skip() below: we consume the Tab press ourselves and so the 
 145             // focus shouldn't change 
 153 // ============================================================================ 
 155 // ============================================================================ 
 157 // ---------------------------------------------------------------------------- 
 159 // ---------------------------------------------------------------------------- 
 165     if ( !wxApp::OnInit() ) 
 168     MyFrame 
*frame 
= new MyFrame
; 
 174 // ---------------------------------------------------------------------------- 
 176 // ---------------------------------------------------------------------------- 
 178 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
) 
 179     EVT_MENU(TabOrder_Quit
,   MyFrame::OnQuit
) 
 180     EVT_MENU(TabOrder_About
,  MyFrame::OnAbout
) 
 182     EVT_MENU(TabOrder_TabForward
, MyFrame::OnTabForward
) 
 183     EVT_MENU(TabOrder_TabBackward
, MyFrame::OnTabBackward
) 
 185     EVT_IDLE(MyFrame::OnIdle
) 
 189        : wxFrame(NULL
, wxID_ANY
, _T("TabOrder wxWidgets Sample"), 
 190                  wxDefaultPosition
, wxSize(700, 450)) 
 192     wxMenu 
*menuFile 
= new wxMenu
; 
 193     menuFile
->Append(TabOrder_About
); 
 194     menuFile
->AppendSeparator(); 
 195     menuFile
->Append(TabOrder_Quit
); 
 197     wxMenu 
*menuNav 
= new wxMenu
; 
 198     menuNav
->Append(TabOrder_TabForward
, _T("Tab &forward\tCtrl-F"), 
 199                     _T("Emulate a <Tab> press")); 
 200     menuNav
->Append(TabOrder_TabBackward
, _T("Tab &backward\tCtrl-B"), 
 201                     _T("Emulate a <Shift-Tab> press")); 
 203     wxMenuBar 
*mbar 
= new wxMenuBar
; 
 204     mbar
->Append(menuFile
, _T("&File")); 
 205     mbar
->Append(menuNav
, _T("&Navigate")); 
 209     m_panel 
= new MyPanel(this); 
 211     CreateStatusBar(StatusPane_Max
); 
 214 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
)) 
 219 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
)) 
 221     wxMessageBox(_T("Tab navigation sample\n(c) 2007 Vadim Zeitlin"), 
 222                  _T("About TabOrder wxWidgets Sample"), wxOK
, this); 
 225 void MyFrame::OnTabForward(wxCommandEvent
& WXUNUSED(event
)) 
 227     DoNavigate(wxNavigationKeyEvent::IsForward 
| wxNavigationKeyEvent::FromTab
); 
 230 void MyFrame::OnTabBackward(wxCommandEvent
& WXUNUSED(event
)) 
 232     DoNavigate(wxNavigationKeyEvent::IsBackward 
| wxNavigationKeyEvent::FromTab
); 
 235 void MyFrame::OnIdle( wxIdleEvent
& WXUNUSED(event
) ) 
 237     // track the window which has the focus in the status bar 
 238     static wxWindow 
*s_windowFocus 
= NULL
; 
 239     wxWindow 
*focus 
= wxWindow::FindFocus(); 
 240     if ( focus 
!= s_windowFocus 
) 
 242         s_windowFocus 
= focus
; 
 247             msg
.Printf(_T("Focus is at %s"), s_windowFocus
->GetName().c_str()); 
 251             msg 
= _T("No focus"); 
 254         SetStatusText(msg
, StatusPane_Focus
); 
 258 // ---------------------------------------------------------------------------- 
 260 // ---------------------------------------------------------------------------- 
 262 MyPanel::MyPanel(wxWindow 
*parent
) 
 263        : wxPanel(parent
, wxID_ANY
) 
 265     wxNotebook 
*notebook 
= new wxNotebook(this, wxID_ANY
); 
 266     notebook
->AddPage(CreateButtonPage(notebook
), _T("Button")); 
 267     notebook
->AddPage(CreateTextPage(notebook
), _T("Text")); 
 269     wxSizer 
*sizerV 
= new wxBoxSizer(wxVERTICAL
); 
 270     sizerV
->Add(notebook
, wxSizerFlags(1).Expand()); 
 272     wxListBox 
*lbox 
= new wxListBox(this, wxID_ANY
); 
 273     lbox
->AppendString(_T("Just a")); 
 274     lbox
->AppendString(_T("simple")); 
 275     lbox
->AppendString(_T("listbox")); 
 276     sizerV
->Add(lbox
, wxSizerFlags(1).Expand()); 
 278     SetSizerAndFit(sizerV
); 
 281 wxWindow 
*MyPanel::CreateButtonPage(wxWindow 
*parent
) 
 283     wxSizerFlags flagsBorder 
= wxSizerFlags().Border().Centre(); 
 285     wxPanel 
*page 
= new wxPanel(parent
); 
 286     wxSizer 
*sizerPage 
= new wxBoxSizer(wxHORIZONTAL
); 
 287     sizerPage
->Add(new wxButton(page
, wxID_ANY
, _T("&First")), flagsBorder
); 
 288     sizerPage
->Add(new wxStaticText(page
, wxID_ANY
, _T("[st&atic]")), 
 290     sizerPage
->Add(new wxButton(page
, wxID_ANY
, _T("&Second")), flagsBorder
); 
 292     page
->SetSizer(sizerPage
); 
 297 wxWindow 
*MyPanel::CreateTextPage(wxWindow 
*parent
) 
 299     wxSizerFlags flagsBorder 
= wxSizerFlags().Border(); 
 301     wxSizer 
*sizerPage 
= new wxBoxSizer(wxVERTICAL
); 
 302     wxPanel 
*page 
= new wxPanel(parent
); 
 304     wxSizer 
*sizerH 
= new wxBoxSizer(wxHORIZONTAL
); 
 305     sizerH
->Add(new wxStaticText(page
, wxID_ANY
, _T("&Label:")), flagsBorder
); 
 306     sizerH
->Add(new MyTabTextCtrl(page
, _T("TAB ignored here")), flagsBorder
); 
 307     sizerPage
->Add(sizerH
, wxSizerFlags(1).Expand()); 
 309     sizerH 
= new wxBoxSizer(wxHORIZONTAL
); 
 310     sizerH
->Add(new wxStaticText(page
, wxID_ANY
, _T("&Another one:")), 
 312     sizerH
->Add(new MyTabTextCtrl(page
, _T("press Tab here"), wxTE_PROCESS_TAB
), 
 314     sizerPage
->Add(sizerH
, wxSizerFlags(1).Expand()); 
 316     page
->SetSizer(sizerPage
);