]> git.saurik.com Git - wxWidgets.git/blob - samples/access/accesstest.cpp
9c21331683e360619b14216d418f315d884e4546
[wxWidgets.git] / samples / access / accesstest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: accesstest.cpp
3 // Purpose: wxWindows accessibility sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2002-02-12
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
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" wxWindows headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #include "wx/access.h"
34
35 // ----------------------------------------------------------------------------
36 // resources
37 // ----------------------------------------------------------------------------
38
39 // the application icon (under Windows and OS/2 it is in resources)
40 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
41 #include "mondrian.xpm"
42 #endif
43
44 // ----------------------------------------------------------------------------
45 // private classes
46 // ----------------------------------------------------------------------------
47
48 // Define a new application type, each program should derive a class from wxApp
49 class MyApp : public wxApp
50 {
51 public:
52 // override base class virtuals
53 // ----------------------------
54
55 // this one is called on application startup and is a good place for the app
56 // initialization (doing it here and not in the ctor allows to have an error
57 // return: if OnInit() returns false, the application terminates)
58 virtual bool OnInit();
59 };
60
61 // Define a new frame type: this is going to be our main frame
62 class MyFrame : public wxFrame
63 {
64 public:
65 // ctor(s)
66 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
67 long style = wxDEFAULT_FRAME_STYLE);
68
69 // event handlers (these functions should _not_ be virtual)
70 void OnQuit(wxCommandEvent& event);
71 void OnAbout(wxCommandEvent& event);
72
73 private:
74 // any class wishing to process wxWindows events must use this macro
75 DECLARE_EVENT_TABLE()
76 };
77
78 // ----------------------------------------------------------------------------
79 // constants
80 // ----------------------------------------------------------------------------
81
82 // IDs for the controls and the menu commands
83 enum
84 {
85 // menu items
86 AccessTest_Quit = 1,
87
88 // it is important for the id corresponding to the "About" command to have
89 // this standard value as otherwise it won't be handled properly under Mac
90 // (where it is special and put into the "Apple" menu)
91 AccessTest_About = wxID_ABOUT
92 };
93
94 // ----------------------------------------------------------------------------
95 // event tables and other macros for wxWindows
96 // ----------------------------------------------------------------------------
97
98 // the event tables connect the wxWindows events with the functions (event
99 // handlers) which process them. It can be also done at run-time, but for the
100 // simple menu events like this the static method is much simpler.
101 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
102 EVT_MENU(AccessTest_Quit, MyFrame::OnQuit)
103 EVT_MENU(AccessTest_About, MyFrame::OnAbout)
104 END_EVENT_TABLE()
105
106 // Create a new application object: this macro will allow wxWindows to create
107 // the application object during program execution (it's better than using a
108 // static object for many reasons) and also declares the accessor function
109 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
110 // not wxApp)
111 IMPLEMENT_APP(MyApp)
112
113 // ============================================================================
114 // implementation
115 // ============================================================================
116
117 // ----------------------------------------------------------------------------
118 // the application class
119 // ----------------------------------------------------------------------------
120
121 // 'Main program' equivalent: the program execution "starts" here
122 bool MyApp::OnInit()
123 {
124 // create the main application window
125 MyFrame *frame = new MyFrame(_T("AccessTest wxWindows App"),
126 wxPoint(50, 50), wxSize(450, 340));
127
128 // and show it (the frames, unlike simple controls, are not shown when
129 // created initially)
130 frame->Show(TRUE);
131
132 // success: wxApp::OnRun() will be called which will enter the main message
133 // loop and the application will run. If we returned FALSE here, the
134 // application would exit immediately.
135 return TRUE;
136 }
137
138 class FrameAccessible: public wxWindowAccessible
139 {
140 public:
141 FrameAccessible(wxWindow* win): wxWindowAccessible(win) {}
142
143 // Gets the name of the specified object.
144 virtual wxAccStatus GetName(int childId, wxString* name)
145 {
146 #if 1
147 if (childId == wxACC_SELF)
148 {
149 * name = wxT("Julian's Frame");
150 return wxACC_OK;
151 }
152 else
153 #endif
154 return wxACC_NOT_IMPLEMENTED;
155 }
156 };
157
158 class ScrolledWindowAccessible: public wxWindowAccessible
159 {
160 public:
161 ScrolledWindowAccessible(wxWindow* win): wxWindowAccessible(win) {}
162
163 // Gets the name of the specified object.
164 virtual wxAccStatus GetName(int childId, wxString* name)
165 {
166 if (childId == wxACC_SELF)
167 {
168 * name = wxT("My scrolled window");
169 return wxACC_OK;
170 }
171 else
172 return wxACC_NOT_IMPLEMENTED;
173 }
174 };
175
176 // ----------------------------------------------------------------------------
177 // main frame
178 // ----------------------------------------------------------------------------
179
180 // frame constructor
181 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
182 : wxFrame(NULL, -1, title, pos, size, style)
183 {
184 SetAccessible(new FrameAccessible(this));
185
186 // set the frame icon
187 SetIcon(wxICON(mondrian));
188
189 #if wxUSE_MENUS
190 // create a menu bar
191 wxMenu *menuFile = new wxMenu;
192
193 // the "About" item should be in the help menu
194 wxMenu *helpMenu = new wxMenu;
195 helpMenu->Append(AccessTest_About, _T("&About...\tF1"), _T("Show about dialog"));
196
197 menuFile->Append(AccessTest_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
198
199 // now append the freshly created menu to the menu bar...
200 wxMenuBar *menuBar = new wxMenuBar();
201 menuBar->Append(menuFile, _T("&File"));
202 menuBar->Append(helpMenu, _T("&Help"));
203
204 // ... and attach this menu bar to the frame
205 SetMenuBar(menuBar);
206 #endif // wxUSE_MENUS
207
208 #if wxUSE_STATUSBAR
209 // create a status bar just for fun (by default with 1 pane only)
210 CreateStatusBar(2);
211 SetStatusText(_T("Welcome to wxWindows!"));
212 #endif // wxUSE_STATUSBAR
213
214 #if 1
215 wxListBox* listBox = new wxListBox(this, -1);
216 listBox->SetAccessible(new ScrolledWindowAccessible(listBox));
217 #else
218 wxScrolledWindow* scrolledWindow = new wxScrolledWindow(this, -1);
219 scrolledWindow->SetAccessible(new ScrolledWindowAccessible(scrolledWindow));
220 #endif
221 }
222
223
224 // event handlers
225
226 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
227 {
228 // TRUE is to force the frame to close
229 Close(TRUE);
230 }
231
232 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
233 {
234 wxString msg;
235 msg.Printf( _T("This is the About dialog of the AccessTest sample.\n")
236 _T("Welcome to %s"), wxVERSION_STRING);
237
238 wxMessageBox(msg, _T("About AccessTest"), wxOK | wxICON_INFORMATION, this);
239 }