]>
git.saurik.com Git - wxWidgets.git/blob - samples/access/accesstest.cpp
0d7ee018ca5d11eedbf2ce605468f280dbab161e
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: accesstest.cpp
3 // Purpose: wxWindows accessibility sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWindows headers)
33 #include "wx/access.h"
34 #include "wx/splitter.h"
41 #include "wx/msw/ole/oleutils.h"
42 #include "wx/msw/winundef.h"
45 #define OBJID_CLIENT 0xFFFFFFFC
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // the application icon (under Windows and OS/2 it is in resources)
55 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
56 #include "mondrian.xpm"
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // Define a new application type, each program should derive a class from wxApp
64 class MyApp
: public wxApp
67 // override base class virtuals
68 // ----------------------------
70 // this one is called on application startup and is a good place for the app
71 // initialization (doing it here and not in the ctor allows to have an error
72 // return: if OnInit() returns false, the application terminates)
73 virtual bool OnInit();
77 // Define a new frame type: this is going to be our main frame
78 class MyFrame
: public wxFrame
82 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
83 long style
= wxDEFAULT_FRAME_STYLE
);
85 // event handlers (these functions should _not_ be virtual)
86 void OnQuit(wxCommandEvent
& event
);
87 void OnQuery(wxCommandEvent
& event
);
88 void OnAbout(wxCommandEvent
& event
);
90 // Log messages to the text control
91 void Log(const wxString
& text
);
93 // Recursively give information about an object
94 void LogObject(int indent
, IAccessible
* obj
);
97 wxTextCtrl
* m_textCtrl
;
99 // any class wishing to process wxWindows events must use this macro
100 DECLARE_EVENT_TABLE()
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 // IDs for the controls and the menu commands
113 // query the hierarchy
116 // it is important for the id corresponding to the "About" command to have
117 // this standard value as otherwise it won't be handled properly under Mac
118 // (where it is special and put into the "Apple" menu)
119 AccessTest_About
= wxID_ABOUT
122 // ----------------------------------------------------------------------------
123 // event tables and other macros for wxWindows
124 // ----------------------------------------------------------------------------
126 // the event tables connect the wxWindows events with the functions (event
127 // handlers) which process them. It can be also done at run-time, but for the
128 // simple menu events like this the static method is much simpler.
129 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
130 EVT_MENU(AccessTest_Quit
, MyFrame::OnQuit
)
131 EVT_MENU(AccessTest_Query
, MyFrame::OnQuery
)
132 EVT_MENU(AccessTest_About
, MyFrame::OnAbout
)
135 // Create a new application object: this macro will allow wxWindows to create
136 // the application object during program execution (it's better than using a
137 // static object for many reasons) and also declares the accessor function
138 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
142 // ============================================================================
144 // ============================================================================
146 // ----------------------------------------------------------------------------
147 // the application class
148 // ----------------------------------------------------------------------------
150 // 'Main program' equivalent: the program execution "starts" here
153 // create the main application window
154 MyFrame
*frame
= new MyFrame(_T("AccessTest wxWindows App"),
155 wxPoint(50, 50), wxSize(450, 340));
157 // and show it (the frames, unlike simple controls, are not shown when
158 // created initially)
161 // success: wxApp::OnRun() will be called which will enter the main message
162 // loop and the application will run. If we returned FALSE here, the
163 // application would exit immediately.
167 class FrameAccessible
: public wxWindowAccessible
170 FrameAccessible(wxWindow
* win
): wxWindowAccessible(win
) {}
172 // Gets the name of the specified object.
173 virtual wxAccStatus
GetName(int childId
, wxString
* name
)
176 if (childId
== wxACC_SELF
)
178 * name
= wxT("Julian's Frame");
183 return wxACC_NOT_IMPLEMENTED
;
187 class ScrolledWindowAccessible
: public wxWindowAccessible
190 ScrolledWindowAccessible(wxWindow
* win
): wxWindowAccessible(win
) {}
192 // Gets the name of the specified object.
193 virtual wxAccStatus
GetName(int childId
, wxString
* name
)
195 if (childId
== wxACC_SELF
)
197 * name
= wxT("My scrolled window");
201 return wxACC_NOT_IMPLEMENTED
;
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
210 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
211 : wxFrame(NULL
, -1, title
, pos
, size
, style
)
215 SetAccessible(new FrameAccessible(this));
217 // set the frame icon
218 SetIcon(wxICON(mondrian
));
222 wxMenu
*menuFile
= new wxMenu
;
224 // the "About" item should be in the help menu
225 wxMenu
*helpMenu
= new wxMenu
;
226 helpMenu
->Append(AccessTest_About
, _T("&About...\tF1"), _T("Show about dialog"));
228 menuFile
->Append(AccessTest_Query
, _T("Query"), _T("Query the window hierarchy"));
229 menuFile
->AppendSeparator();
230 menuFile
->Append(AccessTest_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
232 // now append the freshly created menu to the menu bar...
233 wxMenuBar
*menuBar
= new wxMenuBar();
234 menuBar
->Append(menuFile
, _T("&File"));
235 menuBar
->Append(helpMenu
, _T("&Help"));
237 // ... and attach this menu bar to the frame
239 #endif // wxUSE_MENUS
241 #if 1 // wxUSE_STATUSBAR
242 // create a status bar just for fun (by default with 1 pane only)
244 SetStatusText(_T("Welcome to wxWindows!"));
245 #endif // wxUSE_STATUSBAR
247 wxSplitterWindow
* splitter
= new wxSplitterWindow(this, -1);
248 splitter
->CreateAccessible();
250 wxListBox
* listBox
= new wxListBox(splitter
, -1);
251 listBox
->CreateAccessible();
253 m_textCtrl
= new wxTextCtrl(splitter
, -1, wxT(""), wxDefaultPosition
,
254 wxDefaultSize
, wxTE_MULTILINE
);
255 m_textCtrl
->CreateAccessible();
257 splitter
->SplitHorizontally(listBox
, m_textCtrl
, 150);
261 wxListBox
* listBox
= new wxListBox(this, -1);
262 //listBox->SetAccessible(new wxAccessible(listBox));
264 wxScrolledWindow
* scrolledWindow
= new wxScrolledWindow(this, -1);
265 scrolledWindow
->SetAccessible(new ScrolledWindowAccessible(scrolledWindow
));
273 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
275 // TRUE is to force the frame to close
279 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
282 msg
.Printf( _T("This is the About dialog of the AccessTest sample.\n")
283 _T("Welcome to %s"), wxVERSION_STRING
);
285 wxMessageBox(msg
, _T("About AccessTest"), wxOK
| wxICON_INFORMATION
, this);
288 void MyFrame::OnQuery(wxCommandEvent
& WXUNUSED(event
))
291 IAccessible
* accessibleFrame
= NULL
;
292 if (S_OK
!= AccessibleObjectFromWindow((HWND
) GetHWND(), OBJID_CLIENT
,
293 IID_IAccessible
, (void**) & accessibleFrame
))
295 Log(wxT("Could not get object."));
300 Log(wxT("Got an IAccessible for the frame."));
301 LogObject(0, accessibleFrame
);
302 accessibleFrame
->Release();
306 // Log messages to the text control
307 void MyFrame::Log(const wxString
& text
)
311 wxString
text2(text
);
312 text2
.Replace(wxT("\n"), wxT(" "));
313 text2
.Replace(wxT("\r"), wxT(" "));
314 m_textCtrl
->SetInsertionPointEnd();
315 m_textCtrl
->WriteText(text2
+ wxT("\n"));
319 // Recursively give information about an object
320 void MyFrame::LogObject(int indent
, IAccessible
* obj
)
328 HRESULT hResult
= obj
->get_accName(var
, & bStrName
);
332 wxString
strName(wxConvertStringFromOle(bStrName
));
333 SysFreeString(bStrName
);
336 str
.Printf(wxT("Name: %s"), strName
.c_str());
337 str
.Pad(indent
, wxT(' '), FALSE
);
343 str
.Printf(wxT("NO NAME"));
344 str
.Pad(indent
, wxT(' '), FALSE
);
349 VariantInit(& varRole
);
351 hResult
= obj
->get_accRole(var
, & varRole
);
353 if (hResult
== S_OK
&& varRole
.vt
== VT_I4
)
356 GetRoleText(varRole
.lVal
, buf
, 256);
358 wxString
strRole(buf
);
361 str
.Printf(wxT("Role: %s"), strRole
.c_str());
362 str
.Pad(indent
, wxT(' '), FALSE
);
368 str
.Printf(wxT("NO ROLE"));
369 str
.Pad(indent
, wxT(' '), FALSE
);
374 if (S_OK
== obj
->get_accChildCount(& childCount
))
377 str
.Printf(wxT("There are %d children."), (int) childCount
);
378 str
.Pad(indent
, wxT(' '), FALSE
);
383 for (i
= 1; i
<= childCount
; i
++)
389 IDispatch
* pDisp
= NULL
;
390 IAccessible
* childObject
= NULL
;
393 HRESULT hResult
= obj
->get_accName(var
, & bStrName
);
397 wxString
strName(wxConvertStringFromOle(bStrName
));
398 SysFreeString(bStrName
);
401 str
.Printf(wxT("Name: %s"), strName
.c_str());
402 str
.Pad(indent
+4, wxT(' '), FALSE
);
408 str
.Printf(wxT("NO NAME"));
409 str
.Pad(indent
+4, wxT(' '), FALSE
);
414 VariantInit(& varRole
);
416 hResult
= obj
->get_accRole(var
, & varRole
);
418 if (hResult
== S_OK
&& varRole
.vt
== VT_I4
)
421 GetRoleText(varRole
.lVal
, buf
, 256);
423 wxString
strRole(buf
);
426 str
.Printf(wxT("Role: %s"), strRole
.c_str());
427 str
.Pad(indent
+4, wxT(' '), FALSE
);
433 str
.Printf(wxT("NO ROLE"));
434 str
.Pad(indent
+4, wxT(' '), FALSE
);
438 if (S_OK
== obj
->get_accChild(var
, & pDisp
) && pDisp
)
441 str
.Printf(wxT("This is a real object."));
442 str
.Pad(indent
+4, wxT(' '), FALSE
);
445 if (pDisp
->QueryInterface(IID_IAccessible
, (LPVOID
*) & childObject
) == S_OK
)
447 LogObject(indent
+ 4, childObject
);
448 childObject
->Release();
455 str
.Printf(wxT("This is an element."));
456 str
.Pad(indent
+4, wxT(' '), FALSE
);