X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a02a5cfcf33bb2c0edae246c1b19e286bf86d422..fa699cbaaf217af186cd04dd10d6ec67c8667136:/samples/uiaction/uiaction.cpp diff --git a/samples/uiaction/uiaction.cpp b/samples/uiaction/uiaction.cpp index a9eeb43519..6fd518e299 100644 --- a/samples/uiaction/uiaction.cpp +++ b/samples/uiaction/uiaction.cpp @@ -5,7 +5,7 @@ // Modified by: // Created: 04/01/98 // RCS-ID: $Id$ -// Copyright: (c) Kevin Ollivier +// Copyright: (c) Kevin Ollivier, Steven Lamerton // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -16,10 +16,10 @@ // ---------------------------------------------------------------------------- // headers // ---------------------------------------------------------------------------- - + // For compilers that support precompilation, includes "wx/wx.h". #include "wx/wxprec.h" - + #ifdef __BORLANDC__ #pragma hdrstop #endif @@ -30,7 +30,9 @@ #include "wx/wx.h" #endif -#include "wx/uiaction.h" +#if wxUSE_UIACTIONSIMULATOR + #include "wx/uiaction.h" +#endif // ---------------------------------------------------------------------------- // resources @@ -38,10 +40,22 @@ // the application icon (under Windows and OS/2 it is in resources and even // though we could still include the XPM here it would be unused) -#if !defined(__WXMSW__) && !defined(__WXPM__) +#ifndef wxHAS_IMAGES_IN_RESOURCES #include "../sample.xpm" #endif +// ---------------------------------------------------------------------------- +// constants +// ---------------------------------------------------------------------------- + +// IDs for the controls and the menu commands +enum +{ + // menu items + RunSimulation = 1, + SimulateText +}; + // ---------------------------------------------------------------------------- // private classes // ---------------------------------------------------------------------------- @@ -53,6 +67,8 @@ public: virtual bool OnInit(); }; +#if wxUSE_UIACTIONSIMULATOR + // Define a new frame type: this is going to be our main frame class MyFrame : public wxFrame { @@ -60,30 +76,26 @@ public: // ctor(s) MyFrame(const wxString& title); - void OnButtonPressed(wxCommandEvent&); - void OnRunSimulation(wxCommandEvent&); - - bool ButtonPressed() const { return m_buttonPressed; } - bool MenuSelected() const { return m_menuSelected; } - -private: - bool m_buttonPressed; - bool m_menuSelected; -}; + void OnButtonPressed(wxCommandEvent& event); + void OnRunSimulation(wxCommandEvent& event); + void OnSimulateText(wxCommandEvent& event); + void OnExit(wxCommandEvent& WXUNUSED(event)) { Close(); } -// ---------------------------------------------------------------------------- -// constants -// ---------------------------------------------------------------------------- +private: + wxButton* m_button; + wxTextCtrl* m_text; -// IDs for the controls and the menu commands -enum -{ - // menu items - TheButton = wxID_ANY, - RunSimulation = wxID_ANY + DECLARE_EVENT_TABLE() }; -IMPLEMENT_APP(MyApp) +BEGIN_EVENT_TABLE(MyFrame, wxFrame) + EVT_BUTTON(wxID_ANY, MyFrame::OnButtonPressed) + EVT_MENU(RunSimulation, MyFrame::OnRunSimulation) + EVT_MENU(SimulateText, MyFrame::OnSimulateText) + EVT_MENU(wxID_EXIT, MyFrame::OnExit) +END_EVENT_TABLE() + +#endif // wxUSE_UIACTIONSIMULATOR // ============================================================================ // implementation @@ -93,36 +105,46 @@ IMPLEMENT_APP(MyApp) // the application class // ---------------------------------------------------------------------------- +IMPLEMENT_APP(MyApp) + bool MyApp::OnInit() { if ( !wxApp::OnInit() ) return false; +#if wxUSE_UIACTIONSIMULATOR MyFrame *frame = new MyFrame("wxUIActionSimulator sample application"); frame->Show(true); - + return true; +#else // !wxUSE_UIACTIONSIMULATOR + wxLogError("wxUSE_UIACTIONSIMULATOR must be 1 for this sample"); + return false; +#endif // wxUSE_UIACTIONSIMULATOR/!wxUSE_UIACTIONSIMULATOR } // ---------------------------------------------------------------------------- // main frame // ---------------------------------------------------------------------------- +#if wxUSE_UIACTIONSIMULATOR + // frame constructor MyFrame::MyFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title) { SetIcon(wxICON(sample)); - m_buttonPressed = false; - m_menuSelected = false; - #if wxUSE_MENUS // create a menu bar wxMenu *fileMenu = new wxMenu; fileMenu->Append(wxID_NEW, "&New File...", "Open a new file"); - fileMenu->Append(RunSimulation, "&Run Simulation...", "Run the UI action simulation"); + fileMenu->Append(RunSimulation, "&Run Simulation", + "Run predefined UI action simulation"); + fileMenu->Append(SimulateText, "Simulate &text input...", + "Enter text to simulate"); + fileMenu->AppendSeparator(); fileMenu->Append(wxID_EXIT, "E&xit\tAlt-X", "Quit this program"); @@ -132,30 +154,71 @@ MyFrame::MyFrame(const wxString& title) SetMenuBar(menuBar); #endif // wxUSE_MENUS - wxButton* button = new wxButton(this, TheButton, "Button"); - button->SetName("TheButton"); - Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyFrame::OnButtonPressed, this, button->GetId()); - Bind(wxEVT_COMMAND_MENU_SELECTED, &MyFrame::OnRunSimulation, this, RunSimulation); + wxPanel *panel = new wxPanel(this); + + wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); + panel->SetSizer(sizer); + + m_button = new wxButton(panel, wxID_ANY, "&Button"); + sizer->Add(m_button, wxSizerFlags().Centre().Border()); + + m_text = new wxTextCtrl(panel, wxID_ANY, "", + wxDefaultPosition, wxDefaultSize, + wxTE_MULTILINE); + sizer->Add(m_text, wxSizerFlags(1).Expand().Border()); } // event handlers -void MyFrame::OnRunSimulation(wxCommandEvent&) +void MyFrame::OnRunSimulation(wxCommandEvent& WXUNUSED(event)) { wxUIActionSimulator sim; - wxWindow* button = FindWindow(wxString("TheButton")); - wxPoint globalPoint = button->ClientToScreen(wxPoint(20, 10)); - sim.MouseMove(globalPoint.x, globalPoint.y); + + // Add some extra distance to take account of window decorations + sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10)); sim.MouseClick(wxMOUSE_BTN_LEFT); - + + // Process the resulting button event wxYield(); - - if (ButtonPressed()) - wxMessageBox("Button automagically pressed!"); + + m_text->SetFocus(); + sim.Char('A'); + sim.Char('A', wxMOD_SHIFT); + sim.Char(WXK_RETURN); + sim.Char('Z'); + sim.Char('Z', wxMOD_SHIFT); + sim.Char(WXK_RETURN); + sim.Text("aAbBcC"); + sim.Char(WXK_RETURN); + sim.Text("1 234.57e-8"); + sim.Char(WXK_RETURN); + } -void MyFrame::OnButtonPressed(wxCommandEvent&) +void MyFrame::OnSimulateText(wxCommandEvent& WXUNUSED(event)) { - m_buttonPressed = true; + static wxString s_text; + const wxString text = wxGetTextFromUser + ( + "Enter text to simulate: ", + "wxUIActionSimulator wxWidgets Sample", + s_text, + this + ); + if ( text.empty() ) + return; + + s_text = text; + + wxUIActionSimulator sim; + m_text->SetFocus(); + sim.Text(s_text.c_str()); } + +void MyFrame::OnButtonPressed(wxCommandEvent& WXUNUSED(event)) +{ + m_text->AppendText("Button pressed.\n"); +} + +#endif // wxUSE_UIACTIONSIMULATOR