]> git.saurik.com Git - wxWidgets.git/blob - samples/uiaction/uiaction.cpp
Support using GetTextExtent() with empty string to get descent in wxOSX.
[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, Steven Lamerton
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 #if wxUSE_UIACTIONSIMULATOR
34 #include "wx/uiaction.h"
35 #endif
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 #ifndef wxHAS_IMAGES_IN_RESOURCES
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 RunSimulation = 1,
56 SimulateText
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 #if wxUSE_UIACTIONSIMULATOR
71
72 // Define a new frame type: this is going to be our main frame
73 class MyFrame : public wxFrame
74 {
75 public:
76 // ctor(s)
77 MyFrame(const wxString& title);
78
79 void OnButtonPressed(wxCommandEvent& event);
80 void OnRunSimulation(wxCommandEvent& event);
81 void OnSimulateText(wxCommandEvent& event);
82 void OnExit(wxCommandEvent& WXUNUSED(event)) { Close(); }
83
84 private:
85 wxButton* m_button;
86 wxTextCtrl* m_text;
87
88 DECLARE_EVENT_TABLE()
89 };
90
91 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
92 EVT_BUTTON(wxID_ANY, MyFrame::OnButtonPressed)
93 EVT_MENU(RunSimulation, MyFrame::OnRunSimulation)
94 EVT_MENU(SimulateText, MyFrame::OnSimulateText)
95 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
96 END_EVENT_TABLE()
97
98 #endif // wxUSE_UIACTIONSIMULATOR
99
100 // ============================================================================
101 // implementation
102 // ============================================================================
103
104 // ----------------------------------------------------------------------------
105 // the application class
106 // ----------------------------------------------------------------------------
107
108 IMPLEMENT_APP(MyApp)
109
110 bool MyApp::OnInit()
111 {
112 if ( !wxApp::OnInit() )
113 return false;
114
115 #if wxUSE_UIACTIONSIMULATOR
116 MyFrame *frame = new MyFrame("wxUIActionSimulator sample application");
117 frame->Show(true);
118
119 return true;
120 #else // !wxUSE_UIACTIONSIMULATOR
121 wxLogError("wxUSE_UIACTIONSIMULATOR must be 1 for this sample");
122 return false;
123 #endif // wxUSE_UIACTIONSIMULATOR/!wxUSE_UIACTIONSIMULATOR
124 }
125
126 // ----------------------------------------------------------------------------
127 // main frame
128 // ----------------------------------------------------------------------------
129
130 #if wxUSE_UIACTIONSIMULATOR
131
132 // frame constructor
133 MyFrame::MyFrame(const wxString& title)
134 : wxFrame(NULL, wxID_ANY, title)
135 {
136 SetIcon(wxICON(sample));
137
138 #if wxUSE_MENUS
139 // create a menu bar
140 wxMenu *fileMenu = new wxMenu;
141
142 fileMenu->Append(wxID_NEW, "&New File...", "Open a new file");
143 fileMenu->Append(RunSimulation, "&Run Simulation",
144 "Run predefined UI action simulation");
145 fileMenu->Append(SimulateText, "Simulate &text input...",
146 "Enter text to simulate");
147 fileMenu->AppendSeparator();
148
149 fileMenu->Append(wxID_EXIT, "E&xit\tAlt-X", "Quit this program");
150
151 wxMenuBar *menuBar = new wxMenuBar();
152 menuBar->Append(fileMenu, "&File");
153
154 SetMenuBar(menuBar);
155 #endif // wxUSE_MENUS
156
157 wxPanel *panel = new wxPanel(this);
158
159 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
160 panel->SetSizer(sizer);
161
162 m_button = new wxButton(panel, wxID_ANY, "&Button");
163 sizer->Add(m_button, wxSizerFlags().Centre().Border());
164
165 m_text = new wxTextCtrl(panel, wxID_ANY, "",
166 wxDefaultPosition, wxDefaultSize,
167 wxTE_MULTILINE);
168 sizer->Add(m_text, wxSizerFlags(1).Expand().Border());
169 }
170
171
172 // event handlers
173
174 void MyFrame::OnRunSimulation(wxCommandEvent& WXUNUSED(event))
175 {
176 wxUIActionSimulator sim;
177
178 // Add some extra distance to take account of window decorations
179 sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
180 sim.MouseClick(wxMOUSE_BTN_LEFT);
181
182 // Process the resulting button event
183 wxYield();
184
185 m_text->SetFocus();
186 sim.Char('A');
187 sim.Char('A', wxMOD_SHIFT);
188 sim.Char(WXK_RETURN);
189 sim.Char('Z');
190 sim.Char('Z', wxMOD_SHIFT);
191 sim.Char(WXK_RETURN);
192 sim.Text("aAbBcC");
193 sim.Char(WXK_RETURN);
194 sim.Text("1 234.57e-8");
195 sim.Char(WXK_RETURN);
196
197 }
198
199 void MyFrame::OnSimulateText(wxCommandEvent& WXUNUSED(event))
200 {
201 static wxString s_text;
202 const wxString text = wxGetTextFromUser
203 (
204 "Enter text to simulate: ",
205 "wxUIActionSimulator wxWidgets Sample",
206 s_text,
207 this
208 );
209 if ( text.empty() )
210 return;
211
212 s_text = text;
213
214 wxUIActionSimulator sim;
215 m_text->SetFocus();
216 sim.Text(s_text.c_str());
217 }
218
219 void MyFrame::OnButtonPressed(wxCommandEvent& WXUNUSED(event))
220 {
221 m_text->AppendText("Button pressed.\n");
222 }
223
224 #endif // wxUSE_UIACTIONSIMULATOR