taborder.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: taborder.cpp
  3. // Purpose: Sample for testing TAB navigation
  4. // Author: Vadim Zeitlin
  5. // Copyright: (c) 2007 Vadim Zeitlin
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. // ============================================================================
  9. // declarations
  10. // ============================================================================
  11. // ----------------------------------------------------------------------------
  12. // headers
  13. // ----------------------------------------------------------------------------
  14. #include "wx/wxprec.h"
  15. #ifdef __BORLANDC__
  16. #pragma hdrstop
  17. #endif
  18. #ifndef WX_PRECOMP
  19. #include "wx/app.h"
  20. #include "wx/log.h"
  21. #include "wx/frame.h"
  22. #include "wx/menu.h"
  23. #include "wx/sizer.h"
  24. #include "wx/panel.h"
  25. #include "wx/msgdlg.h"
  26. #include "wx/button.h"
  27. #include "wx/listbox.h"
  28. #include "wx/stattext.h"
  29. #include "wx/textctrl.h"
  30. #endif
  31. #include "wx/notebook.h"
  32. #ifndef wxHAS_IMAGES_IN_RESOURCES
  33. #include "../sample.xpm"
  34. #endif
  35. // ----------------------------------------------------------------------------
  36. // constants
  37. // ----------------------------------------------------------------------------
  38. // menu commands and controls ids
  39. enum
  40. {
  41. // file menu
  42. TabOrder_Quit = wxID_EXIT,
  43. TabOrder_About = wxID_ABOUT,
  44. // navigation menu
  45. TabOrder_TabForward = 200,
  46. TabOrder_TabBackward,
  47. TabOrder_Max
  48. };
  49. // status panes: first one is for temporary messages, the second one shows
  50. // current focus
  51. enum
  52. {
  53. StatusPane_Default,
  54. StatusPane_Focus,
  55. StatusPane_Max
  56. };
  57. // ----------------------------------------------------------------------------
  58. // declarations of the classes used in this sample
  59. // ----------------------------------------------------------------------------
  60. // the main application class
  61. class MyApp : public wxApp
  62. {
  63. public:
  64. virtual bool OnInit();
  65. };
  66. // and the main sample window
  67. class MyFrame : public wxFrame
  68. {
  69. public:
  70. MyFrame();
  71. private:
  72. void OnAbout(wxCommandEvent& event);
  73. void OnQuit(wxCommandEvent& event);
  74. void OnTabForward(wxCommandEvent& event);
  75. void OnTabBackward(wxCommandEvent& event);
  76. void OnIdle(wxIdleEvent& event);
  77. void DoNavigate(int flags)
  78. {
  79. if ( m_panel->NavigateIn(flags) )
  80. {
  81. wxLogStatus(this, wxT("Navigation event processed"));
  82. }
  83. else
  84. {
  85. wxLogStatus(this, wxT("Navigation event ignored"));
  86. }
  87. }
  88. wxPanel *m_panel;
  89. wxDECLARE_EVENT_TABLE();
  90. };
  91. // and the panel taking up MyFrame client area
  92. class MyPanel : public wxPanel
  93. {
  94. public:
  95. MyPanel(wxWindow *parent);
  96. private:
  97. wxWindow *CreateButtonPage(wxWindow *parent);
  98. wxWindow *CreateTextPage(wxWindow *parent);
  99. };
  100. // a text control which checks if processing Tab presses in controls with
  101. // wxTE_PROCESS_TAB style really works
  102. class MyTabTextCtrl : public wxTextCtrl
  103. {
  104. public:
  105. MyTabTextCtrl(wxWindow *parent, const wxString& value, int flags = 0)
  106. : wxTextCtrl(parent, wxID_ANY, value,
  107. wxDefaultPosition, wxDefaultSize,
  108. flags)
  109. {
  110. Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MyTabTextCtrl::OnKeyDown));
  111. }
  112. private:
  113. void OnKeyDown(wxKeyEvent& event)
  114. {
  115. if ( event.GetKeyCode() == WXK_TAB &&
  116. wxMessageBox
  117. (
  118. wxT("Let the Tab be used for navigation?"),
  119. wxT("wxWidgets TabOrder sample: Tab key pressed"),
  120. wxICON_QUESTION | wxYES_NO,
  121. this
  122. ) != wxYES )
  123. {
  124. // skip Skip() below: we consume the Tab press ourselves and so the
  125. // focus shouldn't change
  126. return;
  127. }
  128. event.Skip();
  129. }
  130. };
  131. // ============================================================================
  132. // implementation
  133. // ============================================================================
  134. // ----------------------------------------------------------------------------
  135. // MyApp
  136. // ----------------------------------------------------------------------------
  137. IMPLEMENT_APP(MyApp)
  138. bool MyApp::OnInit()
  139. {
  140. if ( !wxApp::OnInit() )
  141. return false;
  142. MyFrame *frame = new MyFrame;
  143. frame->Show(true);
  144. return true;
  145. }
  146. // ----------------------------------------------------------------------------
  147. // MyFrame
  148. // ----------------------------------------------------------------------------
  149. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  150. EVT_MENU(TabOrder_Quit, MyFrame::OnQuit)
  151. EVT_MENU(TabOrder_About, MyFrame::OnAbout)
  152. EVT_MENU(TabOrder_TabForward, MyFrame::OnTabForward)
  153. EVT_MENU(TabOrder_TabBackward, MyFrame::OnTabBackward)
  154. EVT_IDLE(MyFrame::OnIdle)
  155. wxEND_EVENT_TABLE()
  156. MyFrame::MyFrame()
  157. : wxFrame(NULL, wxID_ANY, wxT("TabOrder wxWidgets Sample"),
  158. wxDefaultPosition, wxSize(700, 450))
  159. {
  160. SetIcon(wxICON(sample));
  161. wxMenu *menuFile = new wxMenu;
  162. menuFile->Append(TabOrder_About);
  163. menuFile->AppendSeparator();
  164. menuFile->Append(TabOrder_Quit);
  165. wxMenu *menuNav = new wxMenu;
  166. menuNav->Append(TabOrder_TabForward, wxT("Tab &forward\tCtrl-F"),
  167. wxT("Emulate a <Tab> press"));
  168. menuNav->Append(TabOrder_TabBackward, wxT("Tab &backward\tCtrl-B"),
  169. wxT("Emulate a <Shift-Tab> press"));
  170. wxMenuBar *mbar = new wxMenuBar;
  171. mbar->Append(menuFile, wxT("&File"));
  172. mbar->Append(menuNav, wxT("&Navigate"));
  173. SetMenuBar(mbar);
  174. m_panel = new MyPanel(this);
  175. CreateStatusBar(StatusPane_Max);
  176. }
  177. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  178. {
  179. Close(true);
  180. }
  181. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  182. {
  183. wxMessageBox(wxT("Tab navigation sample\n(c) 2007 Vadim Zeitlin"),
  184. wxT("About TabOrder wxWidgets Sample"), wxOK, this);
  185. }
  186. void MyFrame::OnTabForward(wxCommandEvent& WXUNUSED(event))
  187. {
  188. DoNavigate(wxNavigationKeyEvent::IsForward | wxNavigationKeyEvent::FromTab);
  189. }
  190. void MyFrame::OnTabBackward(wxCommandEvent& WXUNUSED(event))
  191. {
  192. DoNavigate(wxNavigationKeyEvent::IsBackward | wxNavigationKeyEvent::FromTab);
  193. }
  194. void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
  195. {
  196. // track the window which has the focus in the status bar
  197. static wxWindow *s_windowFocus = NULL;
  198. wxWindow *focus = wxWindow::FindFocus();
  199. if ( focus != s_windowFocus )
  200. {
  201. s_windowFocus = focus;
  202. wxString msg;
  203. if ( focus )
  204. {
  205. msg.Printf(wxT("Focus is at %s"), s_windowFocus->GetName().c_str());
  206. }
  207. else
  208. {
  209. msg = wxT("No focus");
  210. }
  211. SetStatusText(msg, StatusPane_Focus);
  212. }
  213. }
  214. // ----------------------------------------------------------------------------
  215. // MyPanel
  216. // ----------------------------------------------------------------------------
  217. MyPanel::MyPanel(wxWindow *parent)
  218. : wxPanel(parent, wxID_ANY)
  219. {
  220. wxNotebook *notebook = new wxNotebook(this, wxID_ANY);
  221. notebook->AddPage(CreateButtonPage(notebook), wxT("Button"));
  222. notebook->AddPage(CreateTextPage(notebook), wxT("Text"));
  223. wxSizer *sizerV = new wxBoxSizer(wxVERTICAL);
  224. sizerV->Add(notebook, wxSizerFlags(1).Expand());
  225. wxListBox *lbox = new wxListBox(this, wxID_ANY);
  226. lbox->AppendString(wxT("Just a"));
  227. lbox->AppendString(wxT("simple"));
  228. lbox->AppendString(wxT("listbox"));
  229. sizerV->Add(lbox, wxSizerFlags(1).Expand());
  230. SetSizerAndFit(sizerV);
  231. }
  232. wxWindow *MyPanel::CreateButtonPage(wxWindow *parent)
  233. {
  234. wxSizerFlags flagsBorder = wxSizerFlags().Border().Centre();
  235. wxPanel *page = new wxPanel(parent);
  236. wxSizer *sizerPage = new wxBoxSizer(wxHORIZONTAL);
  237. sizerPage->Add(new wxButton(page, wxID_ANY, wxT("&First")), flagsBorder);
  238. sizerPage->Add(new wxStaticText(page, wxID_ANY, wxT("[st&atic]")),
  239. flagsBorder);
  240. sizerPage->Add(new wxButton(page, wxID_ANY, wxT("&Second")), flagsBorder);
  241. page->SetSizer(sizerPage);
  242. return page;
  243. }
  244. wxWindow *MyPanel::CreateTextPage(wxWindow *parent)
  245. {
  246. wxSizerFlags flagsBorder = wxSizerFlags().Border();
  247. wxSizer *sizerPage = new wxBoxSizer(wxVERTICAL);
  248. wxPanel *page = new wxPanel(parent);
  249. wxSizer *sizerH = new wxBoxSizer(wxHORIZONTAL);
  250. sizerH->Add(new wxStaticText(page, wxID_ANY, wxT("&Label:")), flagsBorder);
  251. sizerH->Add(new MyTabTextCtrl(page, wxT("TAB ignored here")), flagsBorder);
  252. sizerPage->Add(sizerH, wxSizerFlags(1).Expand());
  253. sizerH = new wxBoxSizer(wxHORIZONTAL);
  254. sizerH->Add(new wxStaticText(page, wxID_ANY, wxT("&Another one:")),
  255. flagsBorder);
  256. sizerH->Add(new MyTabTextCtrl(page, wxT("press Tab here"), wxTE_PROCESS_TAB),
  257. flagsBorder);
  258. sizerPage->Add(sizerH, wxSizerFlags(1).Expand());
  259. page->SetSizer(sizerPage);
  260. return page;
  261. }