uiaction.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: uiaction.cpp
  3. // Purpose: wxUIActionSimulator sample
  4. // Author: Kevin Ollivier
  5. // Modified by:
  6. // Created: 04/01/98
  7. // Copyright: (c) Kevin Ollivier, Steven Lamerton
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers (this file is usually all you
  22. // need because it includes almost all "standard" wxWidgets headers)
  23. #ifndef WX_PRECOMP
  24. #include "wx/wx.h"
  25. #endif
  26. #if wxUSE_UIACTIONSIMULATOR
  27. #include "wx/uiaction.h"
  28. #endif
  29. // ----------------------------------------------------------------------------
  30. // resources
  31. // ----------------------------------------------------------------------------
  32. // the application icon (under Windows and OS/2 it is in resources and even
  33. // though we could still include the XPM here it would be unused)
  34. #ifndef wxHAS_IMAGES_IN_RESOURCES
  35. #include "../sample.xpm"
  36. #endif
  37. // ----------------------------------------------------------------------------
  38. // constants
  39. // ----------------------------------------------------------------------------
  40. // IDs for the controls and the menu commands
  41. enum
  42. {
  43. // menu items
  44. RunSimulation = 1,
  45. SimulateText
  46. };
  47. // ----------------------------------------------------------------------------
  48. // private classes
  49. // ----------------------------------------------------------------------------
  50. // Define a new application type, each program should derive a class from wxApp
  51. class MyApp : public wxApp
  52. {
  53. public:
  54. virtual bool OnInit();
  55. };
  56. #if wxUSE_UIACTIONSIMULATOR
  57. // Define a new frame type: this is going to be our main frame
  58. class MyFrame : public wxFrame
  59. {
  60. public:
  61. // ctor(s)
  62. MyFrame(const wxString& title);
  63. void OnButtonPressed(wxCommandEvent& event);
  64. void OnRunSimulation(wxCommandEvent& event);
  65. void OnSimulateText(wxCommandEvent& event);
  66. void OnExit(wxCommandEvent& WXUNUSED(event)) { Close(); }
  67. private:
  68. wxButton* m_button;
  69. wxTextCtrl* m_text;
  70. wxDECLARE_EVENT_TABLE();
  71. };
  72. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  73. EVT_BUTTON(wxID_ANY, MyFrame::OnButtonPressed)
  74. EVT_MENU(RunSimulation, MyFrame::OnRunSimulation)
  75. EVT_MENU(SimulateText, MyFrame::OnSimulateText)
  76. EVT_MENU(wxID_EXIT, MyFrame::OnExit)
  77. wxEND_EVENT_TABLE()
  78. #endif // wxUSE_UIACTIONSIMULATOR
  79. // ============================================================================
  80. // implementation
  81. // ============================================================================
  82. // ----------------------------------------------------------------------------
  83. // the application class
  84. // ----------------------------------------------------------------------------
  85. IMPLEMENT_APP(MyApp)
  86. bool MyApp::OnInit()
  87. {
  88. if ( !wxApp::OnInit() )
  89. return false;
  90. #if wxUSE_UIACTIONSIMULATOR
  91. MyFrame *frame = new MyFrame("wxUIActionSimulator sample application");
  92. frame->Show(true);
  93. return true;
  94. #else // !wxUSE_UIACTIONSIMULATOR
  95. wxLogError("wxUSE_UIACTIONSIMULATOR must be 1 for this sample");
  96. return false;
  97. #endif // wxUSE_UIACTIONSIMULATOR/!wxUSE_UIACTIONSIMULATOR
  98. }
  99. // ----------------------------------------------------------------------------
  100. // main frame
  101. // ----------------------------------------------------------------------------
  102. #if wxUSE_UIACTIONSIMULATOR
  103. // frame constructor
  104. MyFrame::MyFrame(const wxString& title)
  105. : wxFrame(NULL, wxID_ANY, title)
  106. {
  107. SetIcon(wxICON(sample));
  108. #if wxUSE_MENUS
  109. // create a menu bar
  110. wxMenu *fileMenu = new wxMenu;
  111. fileMenu->Append(wxID_NEW, "&New File...", "Open a new file");
  112. fileMenu->Append(RunSimulation, "&Run Simulation",
  113. "Run predefined UI action simulation");
  114. fileMenu->Append(SimulateText, "Simulate &text input...",
  115. "Enter text to simulate");
  116. fileMenu->AppendSeparator();
  117. fileMenu->Append(wxID_EXIT, "E&xit\tAlt-X", "Quit this program");
  118. wxMenuBar *menuBar = new wxMenuBar();
  119. menuBar->Append(fileMenu, "&File");
  120. SetMenuBar(menuBar);
  121. #endif // wxUSE_MENUS
  122. wxPanel *panel = new wxPanel(this);
  123. wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
  124. panel->SetSizer(sizer);
  125. m_button = new wxButton(panel, wxID_ANY, "&Button");
  126. sizer->Add(m_button, wxSizerFlags().Centre().Border());
  127. m_text = new wxTextCtrl(panel, wxID_ANY, "",
  128. wxDefaultPosition, wxDefaultSize,
  129. wxTE_MULTILINE);
  130. sizer->Add(m_text, wxSizerFlags(1).Expand().Border());
  131. }
  132. // event handlers
  133. void MyFrame::OnRunSimulation(wxCommandEvent& WXUNUSED(event))
  134. {
  135. wxUIActionSimulator sim;
  136. // Add some extra distance to take account of window decorations
  137. sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
  138. sim.MouseClick(wxMOUSE_BTN_LEFT);
  139. // Process the resulting button event
  140. wxYield();
  141. m_text->SetFocus();
  142. sim.Char('A');
  143. sim.Char('A', wxMOD_SHIFT);
  144. sim.Char(WXK_RETURN);
  145. sim.Char('Z');
  146. sim.Char('Z', wxMOD_SHIFT);
  147. sim.Char(WXK_RETURN);
  148. sim.Text("aAbBcC");
  149. sim.Char(WXK_RETURN);
  150. sim.Text("1 234.57e-8");
  151. sim.Char(WXK_RETURN);
  152. }
  153. void MyFrame::OnSimulateText(wxCommandEvent& WXUNUSED(event))
  154. {
  155. static wxString s_text;
  156. const wxString text = wxGetTextFromUser
  157. (
  158. "Enter text to simulate: ",
  159. "wxUIActionSimulator wxWidgets Sample",
  160. s_text,
  161. this
  162. );
  163. if ( text.empty() )
  164. return;
  165. s_text = text;
  166. wxUIActionSimulator sim;
  167. m_text->SetFocus();
  168. sim.Text(s_text.c_str());
  169. }
  170. void MyFrame::OnButtonPressed(wxCommandEvent& WXUNUSED(event))
  171. {
  172. m_text->AppendText("Button pressed.\n");
  173. }
  174. #endif // wxUSE_UIACTIONSIMULATOR