Adding wxUIActionSimulator, a class for programmatically controlling the mouse and...
[wxWidgets.git] / samples / uiaction / uiaction.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: uiaction.cpp
3 // Purpose: wxUIActionSimulator sample
4 // Author: Kevin Ollivier
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Kevin Ollivier
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #include "wx/uiaction.h"
34
35 // ----------------------------------------------------------------------------
36 // resources
37 // ----------------------------------------------------------------------------
38
39 // the application icon (under Windows and OS/2 it is in resources and even
40 // though we could still include the XPM here it would be unused)
41 #if !defined(__WXMSW__) && !defined(__WXPM__)
42 #include "../sample.xpm"
43 #endif
44
45 // ----------------------------------------------------------------------------
46 // private classes
47 // ----------------------------------------------------------------------------
48
49 // Define a new application type, each program should derive a class from wxApp
50 class MyApp : public wxApp
51 {
52 public:
53 virtual bool OnInit();
54 };
55
56 // Define a new frame type: this is going to be our main frame
57 class MyFrame : public wxFrame
58 {
59 public:
60 // ctor(s)
61 MyFrame(const wxString& title);
62
63 void OnButtonPressed(wxCommandEvent&);
64 void OnRunSimulation(wxCommandEvent&);
65
66 bool ButtonPressed() const { return m_buttonPressed; }
67 bool MenuSelected() const { return m_menuSelected; }
68
69 private:
70 bool m_buttonPressed;
71 bool m_menuSelected;
72 };
73
74 // ----------------------------------------------------------------------------
75 // constants
76 // ----------------------------------------------------------------------------
77
78 // IDs for the controls and the menu commands
79 enum
80 {
81 // menu items
82 TheButton = wxID_ANY,
83 RunSimulation = wxID_ANY
84 };
85
86 IMPLEMENT_APP(MyApp)
87
88 // ============================================================================
89 // implementation
90 // ============================================================================
91
92 // ----------------------------------------------------------------------------
93 // the application class
94 // ----------------------------------------------------------------------------
95
96 bool MyApp::OnInit()
97 {
98 if ( !wxApp::OnInit() )
99 return false;
100
101 MyFrame *frame = new MyFrame("wxUIActionSimulator sample application");
102 frame->Show(true);
103
104 return true;
105 }
106
107 // ----------------------------------------------------------------------------
108 // main frame
109 // ----------------------------------------------------------------------------
110
111 // frame constructor
112 MyFrame::MyFrame(const wxString& title)
113 : wxFrame(NULL, wxID_ANY, title)
114 {
115 SetIcon(wxICON(sample));
116
117 m_buttonPressed = false;
118 m_menuSelected = false;
119
120 #if wxUSE_MENUS
121 // create a menu bar
122 wxMenu *fileMenu = new wxMenu;
123
124 fileMenu->Append(wxID_NEW, "&New File...", "Open a new file");
125 fileMenu->Append(RunSimulation, "&Run Simulation...", "Run the UI action simulation");
126
127 fileMenu->Append(wxID_EXIT, "E&xit\tAlt-X", "Quit this program");
128
129 wxMenuBar *menuBar = new wxMenuBar();
130 menuBar->Append(fileMenu, "&File");
131
132 SetMenuBar(menuBar);
133 #endif // wxUSE_MENUS
134
135 wxButton* button = new wxButton(this, TheButton, "Button");
136 button->SetName("TheButton");
137 Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyFrame::OnButtonPressed, this, button->GetId());
138 Bind(wxEVT_COMMAND_MENU_SELECTED, &MyFrame::OnRunSimulation, this, RunSimulation);
139 }
140
141
142 // event handlers
143
144 void MyFrame::OnRunSimulation(wxCommandEvent&)
145 {
146 wxUIActionSimulator sim;
147 wxWindow* button = FindWindow(wxString("TheButton"));
148 wxPoint globalPoint = button->ClientToScreen(wxPoint(20, 10));
149 sim.MouseMove(globalPoint.x, globalPoint.y);
150 sim.MouseClick(wxMOUSE_BTN_LEFT);
151
152 wxYield();
153
154 if (ButtonPressed())
155 wxMessageBox("Button automagically pressed!");
156 }
157
158 void MyFrame::OnButtonPressed(wxCommandEvent&)
159 {
160 m_buttonPressed = true;
161 }