2010-03-06 15:09:23 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: uiaction.cpp
|
|
|
|
// Purpose: wxUIActionSimulator sample
|
|
|
|
// Author: Kevin Ollivier
|
|
|
|
// Created: 04/01/98
|
2016-05-22 20:44:00 -04:00
|
|
|
// Copyright: (c) 2010 Kevin Ollivier, Steven Lamerton
|
|
|
|
// (c) 2016 Vadim Zeitlin
|
2010-03-06 15:09:23 -05:00
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
2010-08-22 18:15:42 -04:00
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
2010-08-22 18:15:42 -04:00
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
|
|
|
|
// for all others, include the necessary headers (this file is usually all you
|
|
|
|
// need because it includes almost all "standard" wxWidgets headers)
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
2010-03-22 18:56:22 -04:00
|
|
|
#if wxUSE_UIACTIONSIMULATOR
|
|
|
|
#include "wx/uiaction.h"
|
|
|
|
#endif
|
2010-03-06 15:09:23 -05:00
|
|
|
|
2016-05-21 17:04:13 -04:00
|
|
|
#include "wx/stopwatch.h"
|
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// resources
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-11-28 17:54:31 -05:00
|
|
|
// the application icon (under Windows it is in resources and even
|
2010-03-06 15:09:23 -05:00
|
|
|
// though we could still include the XPM here it would be unused)
|
2012-03-03 19:28:58 -05:00
|
|
|
#ifndef wxHAS_IMAGES_IN_RESOURCES
|
2010-03-06 15:09:23 -05:00
|
|
|
#include "../sample.xpm"
|
|
|
|
#endif
|
|
|
|
|
2010-03-13 16:58:04 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// IDs for the controls and the menu commands
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
// menu items
|
2011-12-08 10:55:40 -05:00
|
|
|
RunSimulation = 1,
|
|
|
|
SimulateText
|
2010-03-13 16:58:04 -05:00
|
|
|
};
|
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// private classes
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Define a new application type, each program should derive a class from wxApp
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
2014-03-29 20:02:23 -04:00
|
|
|
virtual bool OnInit() wxOVERRIDE;
|
2010-03-06 15:09:23 -05:00
|
|
|
};
|
|
|
|
|
2010-03-22 18:56:22 -04:00
|
|
|
#if wxUSE_UIACTIONSIMULATOR
|
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
// Define a new frame type: this is going to be our main frame
|
|
|
|
class MyFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor(s)
|
|
|
|
MyFrame(const wxString& title);
|
|
|
|
|
2010-08-22 18:15:42 -04:00
|
|
|
void OnButtonPressed(wxCommandEvent& event);
|
2016-05-22 20:44:00 -04:00
|
|
|
void OnNew(wxCommandEvent& event);
|
2010-08-22 18:15:42 -04:00
|
|
|
void OnRunSimulation(wxCommandEvent& event);
|
2011-12-08 10:55:40 -05:00
|
|
|
void OnSimulateText(wxCommandEvent& event);
|
2010-08-22 18:15:42 -04:00
|
|
|
void OnExit(wxCommandEvent& WXUNUSED(event)) { Close(); }
|
2016-06-04 18:10:13 -04:00
|
|
|
void OnAbout(wxCommandEvent& event);
|
2010-08-22 18:15:42 -04:00
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
private:
|
2010-08-22 18:15:42 -04:00
|
|
|
wxButton* m_button;
|
|
|
|
wxTextCtrl* m_text;
|
2010-03-06 15:09:23 -05:00
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxDECLARE_EVENT_TABLE();
|
2010-03-06 15:09:23 -05:00
|
|
|
};
|
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2010-08-22 18:15:42 -04:00
|
|
|
EVT_BUTTON(wxID_ANY, MyFrame::OnButtonPressed)
|
2016-05-22 20:44:00 -04:00
|
|
|
EVT_MENU(wxID_NEW, MyFrame::OnNew)
|
2010-03-13 16:58:04 -05:00
|
|
|
EVT_MENU(RunSimulation, MyFrame::OnRunSimulation)
|
2011-12-08 10:55:40 -05:00
|
|
|
EVT_MENU(SimulateText, MyFrame::OnSimulateText)
|
2010-08-22 18:15:42 -04:00
|
|
|
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
|
2016-06-04 18:10:13 -04:00
|
|
|
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
|
2014-03-30 03:07:55 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
2010-03-06 15:09:23 -05:00
|
|
|
|
2010-03-22 18:56:22 -04:00
|
|
|
#endif // wxUSE_UIACTIONSIMULATOR
|
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the application class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxIMPLEMENT_APP(MyApp);
|
2010-03-13 16:58:04 -05:00
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
2010-03-22 18:56:22 -04:00
|
|
|
#if wxUSE_UIACTIONSIMULATOR
|
2010-03-06 15:09:23 -05:00
|
|
|
MyFrame *frame = new MyFrame("wxUIActionSimulator sample application");
|
|
|
|
frame->Show(true);
|
2010-08-22 18:15:42 -04:00
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
return true;
|
2010-03-22 18:56:22 -04:00
|
|
|
#else // !wxUSE_UIACTIONSIMULATOR
|
|
|
|
wxLogError("wxUSE_UIACTIONSIMULATOR must be 1 for this sample");
|
|
|
|
return false;
|
|
|
|
#endif // wxUSE_UIACTIONSIMULATOR/!wxUSE_UIACTIONSIMULATOR
|
2010-03-06 15:09:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main frame
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2010-03-22 18:56:22 -04:00
|
|
|
#if wxUSE_UIACTIONSIMULATOR
|
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
// frame constructor
|
|
|
|
MyFrame::MyFrame(const wxString& title)
|
|
|
|
: wxFrame(NULL, wxID_ANY, title)
|
|
|
|
{
|
|
|
|
SetIcon(wxICON(sample));
|
|
|
|
|
|
|
|
#if wxUSE_MENUS
|
|
|
|
// create a menu bar
|
|
|
|
wxMenu *fileMenu = new wxMenu;
|
|
|
|
|
|
|
|
fileMenu->Append(wxID_NEW, "&New File...", "Open a new file");
|
2016-05-19 17:51:26 -04:00
|
|
|
fileMenu->Append(RunSimulation, "&Run Simulation\tCtrl-R",
|
2011-12-08 10:55:40 -05:00
|
|
|
"Run predefined UI action simulation");
|
2016-05-19 17:51:26 -04:00
|
|
|
fileMenu->Append(SimulateText, "Simulate &text input...\tCtrl-T",
|
2011-12-08 10:55:40 -05:00
|
|
|
"Enter text to simulate");
|
|
|
|
fileMenu->AppendSeparator();
|
2010-03-06 15:09:23 -05:00
|
|
|
|
|
|
|
fileMenu->Append(wxID_EXIT, "E&xit\tAlt-X", "Quit this program");
|
|
|
|
|
2016-06-04 18:10:13 -04:00
|
|
|
wxMenu* const helpMenu = new wxMenu;
|
|
|
|
helpMenu->Append(wxID_ABOUT);
|
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
wxMenuBar *menuBar = new wxMenuBar();
|
|
|
|
menuBar->Append(fileMenu, "&File");
|
2016-06-04 18:10:13 -04:00
|
|
|
menuBar->Append(helpMenu, "&Help");
|
2010-03-06 15:09:23 -05:00
|
|
|
|
|
|
|
SetMenuBar(menuBar);
|
|
|
|
#endif // wxUSE_MENUS
|
|
|
|
|
2010-08-22 18:15:42 -04:00
|
|
|
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());
|
2010-03-06 15:09:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// event handlers
|
|
|
|
|
2016-05-22 20:44:00 -04:00
|
|
|
void MyFrame::OnNew(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
m_text->AppendText("\"New\" menu item was selected\n");
|
|
|
|
}
|
|
|
|
|
2010-08-22 18:15:42 -04:00
|
|
|
void MyFrame::OnRunSimulation(wxCommandEvent& WXUNUSED(event))
|
2010-03-06 15:09:23 -05:00
|
|
|
{
|
2016-05-19 17:51:26 -04:00
|
|
|
m_text->SetValue("=== Starting the simulation "
|
|
|
|
"(release any pressed keys) ===\n");
|
|
|
|
|
|
|
|
// This sleep is needed to give the time for the currently pressed modifier
|
|
|
|
// keys, if any, to be released. Notice that Control modifier could well be
|
|
|
|
// pressed if this command was activated from the menu using accelerator
|
|
|
|
// and keeping it pressed would totally derail the test below, e.g. "A" key
|
|
|
|
// press would actually become "Ctrl+A" selecting the entire text and so on.
|
|
|
|
wxMilliSleep(500);
|
2016-05-21 17:04:13 -04:00
|
|
|
|
|
|
|
wxStopWatch sw;
|
|
|
|
|
2010-03-06 15:09:23 -05:00
|
|
|
wxUIActionSimulator sim;
|
2010-08-22 18:15:42 -04:00
|
|
|
|
|
|
|
// Add some extra distance to take account of window decorations
|
|
|
|
sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
|
2010-03-06 15:09:23 -05:00
|
|
|
sim.MouseClick(wxMOUSE_BTN_LEFT);
|
2010-08-22 18:15:42 -04:00
|
|
|
|
|
|
|
// Process the resulting button event
|
2010-03-06 15:09:23 -05:00
|
|
|
wxYield();
|
2010-08-22 18:15:42 -04:00
|
|
|
|
|
|
|
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);
|
2011-11-15 10:56:55 -05:00
|
|
|
sim.Text("1 234.57e-8");
|
|
|
|
sim.Char(WXK_RETURN);
|
|
|
|
|
2016-05-21 17:04:13 -04:00
|
|
|
// Process the resulting text events
|
|
|
|
wxYield();
|
|
|
|
|
2016-05-22 20:44:00 -04:00
|
|
|
// Emulate opening a menu from keyboard.
|
|
|
|
sim.Char('F', wxMOD_ALT);
|
|
|
|
sim.Char('N');
|
|
|
|
wxYield();
|
|
|
|
|
2016-05-21 17:04:13 -04:00
|
|
|
m_text->AppendText(wxString::Format("\n=== Done in %ldms ===\n", sw.Time()));
|
2010-03-06 15:09:23 -05:00
|
|
|
}
|
|
|
|
|
2011-12-08 10:55:40 -05:00
|
|
|
void MyFrame::OnSimulateText(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
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();
|
2012-06-20 13:54:42 -04:00
|
|
|
sim.Text(s_text.c_str());
|
2011-12-08 10:55:40 -05:00
|
|
|
}
|
|
|
|
|
2010-08-22 18:15:42 -04:00
|
|
|
void MyFrame::OnButtonPressed(wxCommandEvent& WXUNUSED(event))
|
2010-03-06 15:09:23 -05:00
|
|
|
{
|
2010-08-22 18:15:42 -04:00
|
|
|
m_text->AppendText("Button pressed.\n");
|
2010-03-06 15:09:23 -05:00
|
|
|
}
|
2010-03-22 07:40:35 -04:00
|
|
|
|
2016-06-04 18:10:13 -04:00
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxMessageBox
|
|
|
|
(
|
|
|
|
"Shows how to use wxUIActionSimulator to simulate user actions",
|
|
|
|
"About wxWidgets uiaction sample",
|
|
|
|
wxOK | wxICON_INFORMATION,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-03-22 07:40:35 -04:00
|
|
|
#endif // wxUSE_UIACTIONSIMULATOR
|