Add wxUSE_UIACTIONSIMULATOR and turn it off by default.
[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 #if wxUSE_UIACTIONSIMULATOR
28
29 // for all others, include the necessary headers (this file is usually all you
30 // need because it includes almost all "standard" wxWidgets headers)
31 #ifndef WX_PRECOMP
32 #include "wx/wx.h"
33 #endif
34
35 #include "wx/uiaction.h"
36
37 // ----------------------------------------------------------------------------
38 // resources
39 // ----------------------------------------------------------------------------
40
41 // the application icon (under Windows and OS/2 it is in resources and even
42 // though we could still include the XPM here it would be unused)
43 #if !defined(__WXMSW__) && !defined(__WXPM__)
44 #include "../sample.xpm"
45 #endif
46
47 // ----------------------------------------------------------------------------
48 // constants
49 // ----------------------------------------------------------------------------
50
51 // IDs for the controls and the menu commands
52 enum
53 {
54 // menu items
55 TheButton = 100,
56 RunSimulation
57 };
58
59 // ----------------------------------------------------------------------------
60 // private classes
61 // ----------------------------------------------------------------------------
62
63 // Define a new application type, each program should derive a class from wxApp
64 class MyApp : public wxApp
65 {
66 public:
67 virtual bool OnInit();
68 };
69
70 // Define a new frame type: this is going to be our main frame
71 class MyFrame : public wxFrame
72 {
73 public:
74 // ctor(s)
75 MyFrame(const wxString& title);
76
77 void OnButtonPressed(wxCommandEvent&);
78 void OnRunSimulation(wxCommandEvent&);
79
80 bool ButtonPressed() const { return m_buttonPressed; }
81 bool MenuSelected() const { return m_menuSelected; }
82
83 private:
84 bool m_buttonPressed;
85 bool m_menuSelected;
86
87 DECLARE_EVENT_TABLE()
88 };
89
90 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
91 EVT_BUTTON(TheButton, MyFrame::OnButtonPressed)
92 EVT_MENU(RunSimulation, MyFrame::OnRunSimulation)
93 END_EVENT_TABLE()
94
95 // ============================================================================
96 // implementation
97 // ============================================================================
98
99 // ----------------------------------------------------------------------------
100 // the application class
101 // ----------------------------------------------------------------------------
102
103 IMPLEMENT_APP(MyApp)
104
105 bool MyApp::OnInit()
106 {
107 if ( !wxApp::OnInit() )
108 return false;
109
110 MyFrame *frame = new MyFrame("wxUIActionSimulator sample application");
111 frame->Show(true);
112
113 return true;
114 }
115
116 // ----------------------------------------------------------------------------
117 // main frame
118 // ----------------------------------------------------------------------------
119
120 // frame constructor
121 MyFrame::MyFrame(const wxString& title)
122 : wxFrame(NULL, wxID_ANY, title)
123 {
124 SetIcon(wxICON(sample));
125
126 m_buttonPressed = false;
127 m_menuSelected = false;
128
129 #if wxUSE_MENUS
130 // create a menu bar
131 wxMenu *fileMenu = new wxMenu;
132
133 fileMenu->Append(wxID_NEW, "&New File...", "Open a new file");
134 fileMenu->Append(RunSimulation, "&Run Simulation...", "Run the UI action simulation");
135
136 fileMenu->Append(wxID_EXIT, "E&xit\tAlt-X", "Quit this program");
137
138 wxMenuBar *menuBar = new wxMenuBar();
139 menuBar->Append(fileMenu, "&File");
140
141 SetMenuBar(menuBar);
142 #endif // wxUSE_MENUS
143
144 wxButton* button = new wxButton(this, TheButton, "Button");
145 button->SetName("TheButton");
146 }
147
148
149 // event handlers
150
151 void MyFrame::OnRunSimulation(wxCommandEvent&)
152 {
153 wxUIActionSimulator sim;
154 wxWindow* button = FindWindow(wxString("TheButton"));
155 wxPoint globalPoint = button->ClientToScreen(wxPoint(20, 10));
156 sim.MouseMove(globalPoint.x, globalPoint.y);
157 sim.MouseClick(wxMOUSE_BTN_LEFT);
158
159 wxYield();
160
161 if (ButtonPressed())
162 wxMessageBox("Button automagically pressed!");
163 }
164
165 void MyFrame::OnButtonPressed(wxCommandEvent&)
166 {
167 m_buttonPressed = true;
168 }
169
170 #endif // wxUSE_UIACTIONSIMULATOR