]> git.saurik.com Git - wxWidgets.git/blob - samples/uiaction/uiaction.cpp
Add wxTreeCtrl::{Clear,Set}FocusedItem().
[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 #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 #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 #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&);
80 void OnRunSimulation(wxCommandEvent&);
81
82 bool ButtonPressed() const { return m_buttonPressed; }
83 bool MenuSelected() const { return m_menuSelected; }
84
85 private:
86 bool m_buttonPressed;
87 bool m_menuSelected;
88
89 DECLARE_EVENT_TABLE()
90 };
91
92 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
93 EVT_BUTTON(TheButton, MyFrame::OnButtonPressed)
94 EVT_MENU(RunSimulation, MyFrame::OnRunSimulation)
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 m_buttonPressed = false;
138 m_menuSelected = false;
139
140 #if wxUSE_MENUS
141 // create a menu bar
142 wxMenu *fileMenu = new wxMenu;
143
144 fileMenu->Append(wxID_NEW, "&New File...", "Open a new file");
145 fileMenu->Append(RunSimulation, "&Run Simulation...", "Run the UI action simulation");
146
147 fileMenu->Append(wxID_EXIT, "E&xit\tAlt-X", "Quit this program");
148
149 wxMenuBar *menuBar = new wxMenuBar();
150 menuBar->Append(fileMenu, "&File");
151
152 SetMenuBar(menuBar);
153 #endif // wxUSE_MENUS
154
155 wxButton* button = new wxButton(this, TheButton, "Button");
156 button->SetName("TheButton");
157 }
158
159
160 // event handlers
161
162 void MyFrame::OnRunSimulation(wxCommandEvent&)
163 {
164 wxUIActionSimulator sim;
165 wxWindow* button = FindWindow(wxString("TheButton"));
166 wxPoint globalPoint = button->ClientToScreen(wxPoint(20, 10));
167 sim.MouseMove(globalPoint.x, globalPoint.y);
168 sim.MouseClick(wxMOUSE_BTN_LEFT);
169
170 wxYield();
171
172 if (ButtonPressed())
173 wxMessageBox("Button automagically pressed!");
174 }
175
176 void MyFrame::OnButtonPressed(wxCommandEvent&)
177 {
178 m_buttonPressed = true;
179 }
180
181 #endif // wxUSE_UIACTIONSIMULATOR