]>
git.saurik.com Git - wxWidgets.git/blob - samples/access/accesstest.cpp
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
);
96 // Get info for a child (id > 0) or object (id == 0)
97 void GetInfo(IAccessible
* accessible
, int id
, wxString
& name
, wxString
& role
);
99 wxTextCtrl
* m_textCtrl
;
101 // any class wishing to process wxWindows events must use this macro
102 DECLARE_EVENT_TABLE()
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // IDs for the controls and the menu commands
115 // query the hierarchy
118 // it is important for the id corresponding to the "About" command to have
119 // this standard value as otherwise it won't be handled properly under Mac
120 // (where it is special and put into the "Apple" menu)
121 AccessTest_About
= wxID_ABOUT
124 // ----------------------------------------------------------------------------
125 // event tables and other macros for wxWindows
126 // ----------------------------------------------------------------------------
128 // the event tables connect the wxWindows events with the functions (event
129 // handlers) which process them. It can be also done at run-time, but for the
130 // simple menu events like this the static method is much simpler.
131 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
132 EVT_MENU(AccessTest_Quit
, MyFrame::OnQuit
)
133 EVT_MENU(AccessTest_Query
, MyFrame::OnQuery
)
134 EVT_MENU(AccessTest_About
, MyFrame::OnAbout
)
137 // Create a new application object: this macro will allow wxWindows to create
138 // the application object during program execution (it's better than using a
139 // static object for many reasons) and also declares the accessor function
140 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
144 // ============================================================================
146 // ============================================================================
148 // ----------------------------------------------------------------------------
149 // the application class
150 // ----------------------------------------------------------------------------
152 // 'Main program' equivalent: the program execution "starts" here
155 // create the main application window
156 MyFrame
*frame
= new MyFrame(_T("AccessTest wxWindows App"),
157 wxPoint(50, 50), wxSize(450, 340));
159 // and show it (the frames, unlike simple controls, are not shown when
160 // created initially)
163 // success: wxApp::OnRun() will be called which will enter the main message
164 // loop and the application will run. If we returned FALSE here, the
165 // application would exit immediately.
169 class FrameAccessible
: public wxWindowAccessible
172 FrameAccessible(wxWindow
* win
): wxWindowAccessible(win
) {}
174 // Gets the name of the specified object.
175 virtual wxAccStatus
GetName(int childId
, wxString
* name
)
178 if (childId
== wxACC_SELF
)
180 * name
= wxT("Julian's Frame");
185 return wxACC_NOT_IMPLEMENTED
;
189 class ScrolledWindowAccessible
: public wxWindowAccessible
192 ScrolledWindowAccessible(wxWindow
* win
): wxWindowAccessible(win
) {}
194 // Gets the name of the specified object.
195 virtual wxAccStatus
GetName(int childId
, wxString
* name
)
197 if (childId
== wxACC_SELF
)
199 * name
= wxT("My scrolled window");
203 return wxACC_NOT_IMPLEMENTED
;
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
212 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
213 : wxFrame(NULL
, -1, title
, pos
, size
, style
)
217 // SetAccessible(new FrameAccessible(this));
219 // set the frame icon
220 SetIcon(wxICON(mondrian
));
224 wxMenu
*menuFile
= new wxMenu
;
226 // the "About" item should be in the help menu
227 wxMenu
*helpMenu
= new wxMenu
;
228 helpMenu
->Append(AccessTest_About
, _T("&About...\tF1"), _T("Show about dialog"));
230 menuFile
->Append(AccessTest_Query
, _T("Query"), _T("Query the window hierarchy"));
231 menuFile
->AppendSeparator();
232 menuFile
->Append(AccessTest_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
234 // now append the freshly created menu to the menu bar...
235 wxMenuBar
*menuBar
= new wxMenuBar();
236 menuBar
->Append(menuFile
, _T("&File"));
237 menuBar
->Append(helpMenu
, _T("&Help"));
239 // ... and attach this menu bar to the frame
241 #endif // wxUSE_MENUS
243 #if 0 // wxUSE_STATUSBAR
244 // create a status bar just for fun (by default with 1 pane only)
246 SetStatusText(_T("Welcome to wxWindows!"));
247 #endif // wxUSE_STATUSBAR
250 wxSplitterWindow
* splitter
= new wxSplitterWindow(this, -1);
251 splitter
->CreateAccessible();
253 wxListBox
* listBox
= new wxListBox(splitter
, -1);
254 listBox
->CreateAccessible();
256 m_textCtrl
= new wxTextCtrl(splitter
, -1, wxT(""), wxDefaultPosition
,
257 wxDefaultSize
, wxTE_MULTILINE
);
258 m_textCtrl
->CreateAccessible();
260 splitter
->SplitHorizontally(listBox
, m_textCtrl
, 150);
265 wxListBox
* listBox
= new wxListBox(this, -1);
266 //listBox->SetAccessible(new wxAccessible(listBox));
268 wxScrolledWindow
* scrolledWindow
= new wxScrolledWindow(this, -1);
269 scrolledWindow
->SetAccessible(new ScrolledWindowAccessible(scrolledWindow
));
277 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
279 // TRUE is to force the frame to close
283 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
286 msg
.Printf( _T("This is the About dialog of the AccessTest sample.\n")
287 _T("Welcome to %s"), wxVERSION_STRING
);
289 wxMessageBox(msg
, _T("About AccessTest"), wxOK
| wxICON_INFORMATION
, this);
292 void MyFrame::OnQuery(wxCommandEvent
& WXUNUSED(event
))
295 IAccessible
* accessibleFrame
= NULL
;
296 if (S_OK
!= AccessibleObjectFromWindow((HWND
) GetHWND(), OBJID_CLIENT
,
297 IID_IAccessible
, (void**) & accessibleFrame
))
299 Log(wxT("Could not get object."));
304 //Log(wxT("Got an IAccessible for the frame."));
305 LogObject(0, accessibleFrame
);
306 Log(wxT("Checking children using AccessibleChildren()..."));
308 // Now check the AccessibleChildren function works OK
310 if (S_OK
!= accessibleFrame
->get_accChildCount(& childCount
))
312 Log(wxT("Could not get number of children."));
313 accessibleFrame
->Release();
316 else if (childCount
== 0)
318 Log(wxT("No children."));
319 accessibleFrame
->Release();
325 VARIANT
*var
= new VARIANT
[childCount
];
327 for (i
= 0; i
< childCount
; i
++)
329 VariantInit(& (var
[i
]));
330 var
[i
].vt
= VT_DISPATCH
;
333 if (S_OK
== AccessibleChildren(accessibleFrame
, 0, childCount
, var
, &obtained
))
335 for (i
= 0; i
< childCount
; i
++)
337 IAccessible
* childAccessible
= NULL
;
340 if (var
[i
].pdispVal
->QueryInterface(IID_IAccessible
, (LPVOID
*) & childAccessible
) == S_OK
)
342 var
[i
].pdispVal
->Release();
345 GetInfo(childAccessible
, 0, name
, role
);
347 str
.Printf(wxT("Found child %s/%s"), name
.c_str(), role
.c_str());
349 childAccessible
->Release();
353 var
[i
].pdispVal
->Release();
360 Log(wxT("AccessibleChildren failed."));
365 accessibleFrame
->Release();
369 // Log messages to the text control
370 void MyFrame::Log(const wxString
& text
)
374 wxString
text2(text
);
375 text2
.Replace(wxT("\n"), wxT(" "));
376 text2
.Replace(wxT("\r"), wxT(" "));
377 m_textCtrl
->SetInsertionPointEnd();
378 m_textCtrl
->WriteText(text2
+ wxT("\n"));
382 // Recursively give information about an object
383 void MyFrame::LogObject(int indent
, IAccessible
* obj
)
388 GetInfo(obj
, 0, name
, role
);
391 str
.Printf(wxT("Name = %s; Role = %s"), name
.c_str(), role
.c_str());
392 str
.Pad(indent
, wxT(' '), FALSE
);
397 if (S_OK
== obj
->get_accChildCount(& childCount
))
400 str
.Printf(wxT("There are %d children."), (int) childCount
);
401 str
.Pad(indent
, wxT(' '), FALSE
);
407 for (i
= 1; i
<= childCount
; i
++)
409 GetInfo(obj
, i
, name
, role
);
412 str
.Printf(wxT("%d) Name = %s; Role = %s"), i
, name
.c_str(), role
.c_str());
413 str
.Pad(indent
, wxT(' '), FALSE
);
420 IDispatch
* pDisp
= NULL
;
421 IAccessible
* childObject
= NULL
;
423 if (S_OK
== obj
->get_accChild(var
, & pDisp
) && pDisp
)
426 str
.Printf(wxT("This is a real object."));
427 str
.Pad(indent
+4, wxT(' '), FALSE
);
430 if (pDisp
->QueryInterface(IID_IAccessible
, (LPVOID
*) & childObject
) == S_OK
)
432 LogObject(indent
+ 4, childObject
);
433 childObject
->Release();
440 str
.Printf(wxT("This is an element."));
441 str
.Pad(indent
+4, wxT(' '), FALSE
);
449 // Get info for a child (id > 0) or object (id == 0)
450 void MyFrame::GetInfo(IAccessible
* accessible
, int id
, wxString
& name
, wxString
& role
)
458 HRESULT hResult
= accessible
->get_accName(var
, & bStrName
);
462 name
= wxConvertStringFromOle(bStrName
);
463 SysFreeString(bStrName
);
467 name
= wxT("NO NAME");
471 VariantInit(& varRole
);
473 hResult
= accessible
->get_accRole(var
, & varRole
);
475 if (hResult
== S_OK
&& varRole
.vt
== VT_I4
)
478 GetRoleText(varRole
.lVal
, buf
, 256);
484 role
= wxT("NO ROLE");