]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: uiaction.cpp | |
3 | // Purpose: wxUIActionSimulator sample | |
4 | // Author: Kevin Ollivier | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // Copyright: (c) Kevin Ollivier, Steven Lamerton | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx/wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | // for all others, include the necessary headers (this file is usually all you | |
27 | // need because it includes almost all "standard" wxWidgets headers) | |
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/wx.h" | |
30 | #endif | |
31 | ||
32 | #if wxUSE_UIACTIONSIMULATOR | |
33 | #include "wx/uiaction.h" | |
34 | #endif | |
35 | ||
36 | // ---------------------------------------------------------------------------- | |
37 | // resources | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | // the application icon (under Windows and OS/2 it is in resources and even | |
41 | // though we could still include the XPM here it would be unused) | |
42 | #ifndef wxHAS_IMAGES_IN_RESOURCES | |
43 | #include "../sample.xpm" | |
44 | #endif | |
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // constants | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | // IDs for the controls and the menu commands | |
51 | enum | |
52 | { | |
53 | // menu items | |
54 | RunSimulation = 1, | |
55 | SimulateText | |
56 | }; | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // private classes | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | // Define a new application type, each program should derive a class from wxApp | |
63 | class MyApp : public wxApp | |
64 | { | |
65 | public: | |
66 | virtual bool OnInit(); | |
67 | }; | |
68 | ||
69 | #if wxUSE_UIACTIONSIMULATOR | |
70 | ||
71 | // Define a new frame type: this is going to be our main frame | |
72 | class MyFrame : public wxFrame | |
73 | { | |
74 | public: | |
75 | // ctor(s) | |
76 | MyFrame(const wxString& title); | |
77 | ||
78 | void OnButtonPressed(wxCommandEvent& event); | |
79 | void OnRunSimulation(wxCommandEvent& event); | |
80 | void OnSimulateText(wxCommandEvent& event); | |
81 | void OnExit(wxCommandEvent& WXUNUSED(event)) { Close(); } | |
82 | ||
83 | private: | |
84 | wxButton* m_button; | |
85 | wxTextCtrl* m_text; | |
86 | ||
87 | DECLARE_EVENT_TABLE() | |
88 | }; | |
89 | ||
90 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
91 | EVT_BUTTON(wxID_ANY, MyFrame::OnButtonPressed) | |
92 | EVT_MENU(RunSimulation, MyFrame::OnRunSimulation) | |
93 | EVT_MENU(SimulateText, MyFrame::OnSimulateText) | |
94 | EVT_MENU(wxID_EXIT, MyFrame::OnExit) | |
95 | END_EVENT_TABLE() | |
96 | ||
97 | #endif // wxUSE_UIACTIONSIMULATOR | |
98 | ||
99 | // ============================================================================ | |
100 | // implementation | |
101 | // ============================================================================ | |
102 | ||
103 | // ---------------------------------------------------------------------------- | |
104 | // the application class | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
107 | IMPLEMENT_APP(MyApp) | |
108 | ||
109 | bool MyApp::OnInit() | |
110 | { | |
111 | if ( !wxApp::OnInit() ) | |
112 | return false; | |
113 | ||
114 | #if wxUSE_UIACTIONSIMULATOR | |
115 | MyFrame *frame = new MyFrame("wxUIActionSimulator sample application"); | |
116 | frame->Show(true); | |
117 | ||
118 | return true; | |
119 | #else // !wxUSE_UIACTIONSIMULATOR | |
120 | wxLogError("wxUSE_UIACTIONSIMULATOR must be 1 for this sample"); | |
121 | return false; | |
122 | #endif // wxUSE_UIACTIONSIMULATOR/!wxUSE_UIACTIONSIMULATOR | |
123 | } | |
124 | ||
125 | // ---------------------------------------------------------------------------- | |
126 | // main frame | |
127 | // ---------------------------------------------------------------------------- | |
128 | ||
129 | #if wxUSE_UIACTIONSIMULATOR | |
130 | ||
131 | // frame constructor | |
132 | MyFrame::MyFrame(const wxString& title) | |
133 | : wxFrame(NULL, wxID_ANY, title) | |
134 | { | |
135 | SetIcon(wxICON(sample)); | |
136 | ||
137 | #if wxUSE_MENUS | |
138 | // create a menu bar | |
139 | wxMenu *fileMenu = new wxMenu; | |
140 | ||
141 | fileMenu->Append(wxID_NEW, "&New File...", "Open a new file"); | |
142 | fileMenu->Append(RunSimulation, "&Run Simulation", | |
143 | "Run predefined UI action simulation"); | |
144 | fileMenu->Append(SimulateText, "Simulate &text input...", | |
145 | "Enter text to simulate"); | |
146 | fileMenu->AppendSeparator(); | |
147 | ||
148 | fileMenu->Append(wxID_EXIT, "E&xit\tAlt-X", "Quit this program"); | |
149 | ||
150 | wxMenuBar *menuBar = new wxMenuBar(); | |
151 | menuBar->Append(fileMenu, "&File"); | |
152 | ||
153 | SetMenuBar(menuBar); | |
154 | #endif // wxUSE_MENUS | |
155 | ||
156 | wxPanel *panel = new wxPanel(this); | |
157 | ||
158 | wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); | |
159 | panel->SetSizer(sizer); | |
160 | ||
161 | m_button = new wxButton(panel, wxID_ANY, "&Button"); | |
162 | sizer->Add(m_button, wxSizerFlags().Centre().Border()); | |
163 | ||
164 | m_text = new wxTextCtrl(panel, wxID_ANY, "", | |
165 | wxDefaultPosition, wxDefaultSize, | |
166 | wxTE_MULTILINE); | |
167 | sizer->Add(m_text, wxSizerFlags(1).Expand().Border()); | |
168 | } | |
169 | ||
170 | ||
171 | // event handlers | |
172 | ||
173 | void MyFrame::OnRunSimulation(wxCommandEvent& WXUNUSED(event)) | |
174 | { | |
175 | wxUIActionSimulator sim; | |
176 | ||
177 | // Add some extra distance to take account of window decorations | |
178 | sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10)); | |
179 | sim.MouseClick(wxMOUSE_BTN_LEFT); | |
180 | ||
181 | // Process the resulting button event | |
182 | wxYield(); | |
183 | ||
184 | m_text->SetFocus(); | |
185 | sim.Char('A'); | |
186 | sim.Char('A', wxMOD_SHIFT); | |
187 | sim.Char(WXK_RETURN); | |
188 | sim.Char('Z'); | |
189 | sim.Char('Z', wxMOD_SHIFT); | |
190 | sim.Char(WXK_RETURN); | |
191 | sim.Text("aAbBcC"); | |
192 | sim.Char(WXK_RETURN); | |
193 | sim.Text("1 234.57e-8"); | |
194 | sim.Char(WXK_RETURN); | |
195 | ||
196 | } | |
197 | ||
198 | void MyFrame::OnSimulateText(wxCommandEvent& WXUNUSED(event)) | |
199 | { | |
200 | static wxString s_text; | |
201 | const wxString text = wxGetTextFromUser | |
202 | ( | |
203 | "Enter text to simulate: ", | |
204 | "wxUIActionSimulator wxWidgets Sample", | |
205 | s_text, | |
206 | this | |
207 | ); | |
208 | if ( text.empty() ) | |
209 | return; | |
210 | ||
211 | s_text = text; | |
212 | ||
213 | wxUIActionSimulator sim; | |
214 | m_text->SetFocus(); | |
215 | sim.Text(s_text.c_str()); | |
216 | } | |
217 | ||
218 | void MyFrame::OnButtonPressed(wxCommandEvent& WXUNUSED(event)) | |
219 | { | |
220 | m_text->AppendText("Button pressed.\n"); | |
221 | } | |
222 | ||
223 | #endif // wxUSE_UIACTIONSIMULATOR |