Use event tables instead of Bind() in uiaction sample.
[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 // constants
47 // ----------------------------------------------------------------------------
48
49 // IDs for the controls and the menu commands
50 enum
51 {
52 // menu items
53 TheButton = 100,
54 RunSimulation
55 };
56
57 // ----------------------------------------------------------------------------
58 // private classes
59 // ----------------------------------------------------------------------------
60
61 // Define a new application type, each program should derive a class from wxApp
62 class MyApp : public wxApp
63 {
64 public:
65 virtual bool OnInit();
66 };
67
68 // Define a new frame type: this is going to be our main frame
69 class MyFrame : public wxFrame
70 {
71 public:
72 // ctor(s)
73 MyFrame(const wxString& title);
74
75 void OnButtonPressed(wxCommandEvent&);
76 void OnRunSimulation(wxCommandEvent&);
77
78 bool ButtonPressed() const { return m_buttonPressed; }
79 bool MenuSelected() const { return m_menuSelected; }
80
81 private:
82 bool m_buttonPressed;
83 bool m_menuSelected;
84
85 DECLARE_EVENT_TABLE()
86 };
87
88 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
89 EVT_BUTTON(TheButton, MyFrame::OnButtonPressed)
90 EVT_MENU(RunSimulation, MyFrame::OnRunSimulation)
91 END_EVENT_TABLE()
92
93 // ============================================================================
94 // implementation
95 // ============================================================================
96
97 // ----------------------------------------------------------------------------
98 // the application class
99 // ----------------------------------------------------------------------------
100
101 IMPLEMENT_APP(MyApp)
102
103 bool MyApp::OnInit()
104 {
105 if ( !wxApp::OnInit() )
106 return false;
107
108 MyFrame *frame = new MyFrame("wxUIActionSimulator sample application");
109 frame->Show(true);
110
111 return true;
112 }
113
114 // ----------------------------------------------------------------------------
115 // main frame
116 // ----------------------------------------------------------------------------
117
118 // frame constructor
119 MyFrame::MyFrame(const wxString& title)
120 : wxFrame(NULL, wxID_ANY, title)
121 {
122 SetIcon(wxICON(sample));
123
124 m_buttonPressed = false;
125 m_menuSelected = false;
126
127 #if wxUSE_MENUS
128 // create a menu bar
129 wxMenu *fileMenu = new wxMenu;
130
131 fileMenu->Append(wxID_NEW, "&New File...", "Open a new file");
132 fileMenu->Append(RunSimulation, "&Run Simulation...", "Run the UI action simulation");
133
134 fileMenu->Append(wxID_EXIT, "E&xit\tAlt-X", "Quit this program");
135
136 wxMenuBar *menuBar = new wxMenuBar();
137 menuBar->Append(fileMenu, "&File");
138
139 SetMenuBar(menuBar);
140 #endif // wxUSE_MENUS
141
142 wxButton* button = new wxButton(this, TheButton, "Button");
143 button->SetName("TheButton");
144 }
145
146
147 // event handlers
148
149 void MyFrame::OnRunSimulation(wxCommandEvent&)
150 {
151 wxUIActionSimulator sim;
152 wxWindow* button = FindWindow(wxString("TheButton"));
153 wxPoint globalPoint = button->ClientToScreen(wxPoint(20, 10));
154 sim.MouseMove(globalPoint.x, globalPoint.y);
155 sim.MouseClick(wxMOUSE_BTN_LEFT);
156
157 wxYield();
158
159 if (ButtonPressed())
160 wxMessageBox("Button automagically pressed!");
161 }
162
163 void MyFrame::OnButtonPressed(wxCommandEvent&)
164 {
165 m_buttonPressed = true;
166 }