1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_VIEWS_ACCESSIBILITY_NATIVE_VIEW_ACCESSIBILITY_WIN_H_
6 #define UI_VIEWS_ACCESSIBILITY_NATIVE_VIEW_ACCESSIBILITY_WIN_H_
12 #include <UIAutomationCore.h>
16 #include "third_party/iaccessible2/ia2_api_all.h"
17 #include "ui/base/accessibility/accessible_view_state.h"
18 #include "ui/views/accessibility/native_view_accessibility.h"
19 #include "ui/views/controls/native/native_view_host.h"
20 #include "ui/views/view.h"
23 enum TextBoundaryDirection
;
24 enum TextBoundaryType
;
29 ////////////////////////////////////////////////////////////////////////////////
31 // NativeViewAccessibilityWin
33 // Class implementing the MSAA IAccessible COM interface for a generic View,
34 // providing accessibility to be used by screen readers and other assistive
37 ////////////////////////////////////////////////////////////////////////////////
38 class __declspec(uuid("26f5641a-246d-457b-a96d-07f3fae6acf2"))
39 NativeViewAccessibilityWin
40 : public CComObjectRootEx
<CComMultiThreadModel
>,
41 public IDispatchImpl
<IAccessible2
, &IID_IAccessible2
,
42 &LIBID_IAccessible2Lib
>,
43 public IAccessibleText
,
44 public IServiceProvider
,
46 public IRawElementProviderSimple
,
47 public NativeViewAccessibility
{
49 BEGIN_COM_MAP(NativeViewAccessibilityWin
)
50 COM_INTERFACE_ENTRY2(IDispatch
, IAccessible2
)
51 COM_INTERFACE_ENTRY2(IAccessible
, IAccessible2
)
52 COM_INTERFACE_ENTRY(IAccessible2
)
53 COM_INTERFACE_ENTRY(IAccessibleText
)
54 COM_INTERFACE_ENTRY(IServiceProvider
)
55 COM_INTERFACE_ENTRY(IAccessibleEx
)
56 COM_INTERFACE_ENTRY(IRawElementProviderSimple
)
59 virtual ~NativeViewAccessibilityWin();
61 // NativeViewAccessibility.
62 virtual void NotifyAccessibilityEvent(
63 ui::AccessibilityTypes::Event event_type
) OVERRIDE
;
64 virtual gfx::NativeViewAccessible
GetNativeObject() OVERRIDE
;
65 virtual void Destroy() OVERRIDE
;
67 void set_view(views::View
* view
) { view_
= view
; }
69 // Supported IAccessible methods.
71 // Retrieves the child element or child object at a given point on the screen.
72 virtual STDMETHODIMP
accHitTest(LONG x_left
, LONG y_top
, VARIANT
* child
);
74 // Performs the object's default action.
75 STDMETHODIMP
accDoDefaultAction(VARIANT var_id
);
77 // Retrieves the specified object's current screen location.
78 STDMETHODIMP
accLocation(LONG
* x_left
,
84 // Traverses to another UI element and retrieves the object.
85 STDMETHODIMP
accNavigate(LONG nav_dir
, VARIANT start
, VARIANT
* end
);
87 // Retrieves an IDispatch interface pointer for the specified child.
88 virtual STDMETHODIMP
get_accChild(VARIANT var_child
, IDispatch
** disp_child
);
90 // Retrieves the number of accessible children.
91 virtual STDMETHODIMP
get_accChildCount(LONG
* child_count
);
93 // Retrieves a string that describes the object's default action.
94 STDMETHODIMP
get_accDefaultAction(VARIANT var_id
, BSTR
* default_action
);
96 // Retrieves the tooltip description.
97 STDMETHODIMP
get_accDescription(VARIANT var_id
, BSTR
* desc
);
99 // Retrieves the object that has the keyboard focus.
100 STDMETHODIMP
get_accFocus(VARIANT
* focus_child
);
102 // Retrieves the specified object's shortcut.
103 STDMETHODIMP
get_accKeyboardShortcut(VARIANT var_id
, BSTR
* access_key
);
105 // Retrieves the name of the specified object.
106 STDMETHODIMP
get_accName(VARIANT var_id
, BSTR
* name
);
108 // Retrieves the IDispatch interface of the object's parent.
109 STDMETHODIMP
get_accParent(IDispatch
** disp_parent
);
111 // Retrieves information describing the role of the specified object.
112 STDMETHODIMP
get_accRole(VARIANT var_id
, VARIANT
* role
);
114 // Retrieves the current state of the specified object.
115 STDMETHODIMP
get_accState(VARIANT var_id
, VARIANT
* state
);
117 // Retrieve or set the string value associated with the specified object.
118 // Setting the value is not typically used by screen readers, but it's
119 // used frequently by automation software.
120 STDMETHODIMP
get_accValue(VARIANT var_id
, BSTR
* value
);
121 STDMETHODIMP
put_accValue(VARIANT var_id
, BSTR new_value
);
123 // Selections not applicable to views.
124 STDMETHODIMP
get_accSelection(VARIANT
* selected
);
125 STDMETHODIMP
accSelect(LONG flags_sel
, VARIANT var_id
);
127 // Help functions not supported.
128 STDMETHODIMP
get_accHelp(VARIANT var_id
, BSTR
* help
);
129 STDMETHODIMP
get_accHelpTopic(BSTR
* help_file
,
133 // Deprecated functions, not implemented here.
134 STDMETHODIMP
put_accName(VARIANT var_id
, BSTR put_name
);
140 STDMETHODIMP
role(LONG
* role
);
142 STDMETHODIMP
get_states(AccessibleStates
* states
);
144 STDMETHODIMP
get_uniqueID(LONG
* unique_id
);
146 STDMETHODIMP
get_windowHandle(HWND
* window_handle
);
149 // IAccessible2 methods not implemented.
152 STDMETHODIMP
get_attributes(BSTR
* attributes
) {
155 STDMETHODIMP
get_indexInParent(LONG
* index_in_parent
) {
158 STDMETHODIMP
get_extendedRole(BSTR
* extended_role
) {
161 STDMETHODIMP
get_nRelations(LONG
* n_relations
) {
164 STDMETHODIMP
get_relation(LONG relation_index
,
165 IAccessibleRelation
** relation
) {
168 STDMETHODIMP
get_relations(LONG max_relations
,
169 IAccessibleRelation
** relations
,
173 STDMETHODIMP
scrollTo(enum IA2ScrollType scroll_type
) {
176 STDMETHODIMP
scrollToPoint(
177 enum IA2CoordinateType coordinate_type
,
182 STDMETHODIMP
get_groupPosition(LONG
* group_level
,
183 LONG
* similar_items_in_group
,
184 LONG
* position_in_group
) {
187 STDMETHODIMP
get_localizedExtendedRole(
188 BSTR
* localized_extended_role
) {
191 STDMETHODIMP
get_nExtendedStates(LONG
* n_extended_states
) {
194 STDMETHODIMP
get_extendedStates(LONG max_extended_states
,
195 BSTR
** extended_states
,
196 LONG
* n_extended_states
) {
199 STDMETHODIMP
get_localizedExtendedStates(
200 LONG max_localized_extended_states
,
201 BSTR
** localized_extended_states
,
202 LONG
* n_localized_extended_states
) {
205 STDMETHODIMP
get_locale(IA2Locale
* locale
) {
210 // IAccessibleText methods.
213 STDMETHODIMP
get_nCharacters(LONG
* n_characters
);
215 STDMETHODIMP
get_caretOffset(LONG
* offset
);
217 STDMETHODIMP
get_nSelections(LONG
* n_selections
);
219 STDMETHODIMP
get_selection(LONG selection_index
,
223 STDMETHODIMP
get_text(LONG start_offset
, LONG end_offset
, BSTR
* text
);
225 STDMETHODIMP
get_textAtOffset(LONG offset
,
226 enum IA2TextBoundaryType boundary_type
,
227 LONG
* start_offset
, LONG
* end_offset
,
230 STDMETHODIMP
get_textBeforeOffset(LONG offset
,
231 enum IA2TextBoundaryType boundary_type
,
232 LONG
* start_offset
, LONG
* end_offset
,
235 STDMETHODIMP
get_textAfterOffset(LONG offset
,
236 enum IA2TextBoundaryType boundary_type
,
237 LONG
* start_offset
, LONG
* end_offset
,
240 STDMETHODIMP
get_offsetAtPoint(LONG x
, LONG y
,
241 enum IA2CoordinateType coord_type
,
245 // IAccessibleText methods not implemented.
248 STDMETHODIMP
get_newText(IA2TextSegment
* new_text
) {
251 STDMETHODIMP
get_oldText(IA2TextSegment
* old_text
) {
254 STDMETHODIMP
addSelection(LONG start_offset
, LONG end_offset
) {
257 STDMETHODIMP
get_attributes(LONG offset
,
260 BSTR
* text_attributes
) {
263 STDMETHODIMP
get_characterExtents(LONG offset
,
264 enum IA2CoordinateType coord_type
,
266 LONG
* width
, LONG
* height
) {
269 STDMETHODIMP
removeSelection(LONG selection_index
) {
272 STDMETHODIMP
setCaretOffset(LONG offset
) {
275 STDMETHODIMP
setSelection(LONG selection_index
,
280 STDMETHODIMP
scrollSubstringTo(LONG start_index
,
282 enum IA2ScrollType scroll_type
) {
285 STDMETHODIMP
scrollSubstringToPoint(LONG start_index
,
287 enum IA2CoordinateType coordinate_type
,
293 // IServiceProvider methods.
296 STDMETHODIMP
QueryService(REFGUID guidService
, REFIID riid
, void** object
);
299 // IAccessibleEx methods not implemented.
301 STDMETHODIMP
GetObjectForChild(long child_id
, IAccessibleEx
** ret
) {
305 STDMETHODIMP
GetIAccessiblePair(IAccessible
** acc
, long* child_id
) {
309 STDMETHODIMP
GetRuntimeId(SAFEARRAY
** runtime_id
) {
313 STDMETHODIMP
ConvertReturnedElement(IRawElementProviderSimple
* element
,
314 IAccessibleEx
** acc
) {
319 // IRawElementProviderSimple methods.
321 // The GetPatternProvider/GetPropertyValue methods need to be implemented for
322 // the on-screen keyboard to show up in Windows 8 metro.
323 STDMETHODIMP
GetPatternProvider(PATTERNID id
, IUnknown
** provider
);
324 STDMETHODIMP
GetPropertyValue(PROPERTYID id
, VARIANT
* ret
);
327 // IRawElementProviderSimple methods not implemented.
329 STDMETHODIMP
get_ProviderOptions(enum ProviderOptions
* ret
) {
333 STDMETHODIMP
get_HostRawElementProvider(
334 IRawElementProviderSimple
** provider
) {
340 // Returns a conversion from the event (as defined in accessibility_types.h)
342 static int32
MSAAEvent(ui::AccessibilityTypes::Event event
);
344 // Returns a conversion from the Role (as defined in accessibility_types.h)
346 static int32
MSAARole(ui::AccessibilityTypes::Role role
);
348 // Returns a conversion from the State (as defined in accessibility_types.h)
349 // to MSAA states set.
350 static int32
MSAAState(ui::AccessibilityTypes::State state
);
353 NativeViewAccessibilityWin();
355 const View
* view() const { return view_
; }
358 // Determines navigation direction for accNavigate, based on left, up and
359 // previous being mapped all to previous and right, down, next being mapped
360 // to next. Returns true if navigation direction is next, false otherwise.
361 bool IsNavDirNext(int nav_dir
) const;
363 // Determines if the navigation target is within the allowed bounds. Returns
364 // true if it is, false otherwise.
365 bool IsValidNav(int nav_dir
,
368 int upper_bound
) const;
370 // Determines if the child id variant is valid.
371 bool IsValidId(const VARIANT
& child
) const;
373 // Helper function which sets applicable states of view.
374 void SetState(VARIANT
* msaa_state
, View
* view
);
376 // Return the text to use for IAccessibleText.
377 string16
TextForIAccessibleText();
379 // If offset is a member of IA2TextSpecialOffsets this function updates the
380 // value of offset and returns, otherwise offset remains unchanged.
381 void HandleSpecialTextOffset(const string16
& text
, LONG
* offset
);
383 // Convert from a IA2TextBoundaryType to a ui::TextBoundaryType.
384 ui::TextBoundaryType
IA2TextBoundaryToTextBoundary(IA2TextBoundaryType type
);
386 // Search forwards (direction == 1) or backwards (direction == -1)
387 // from the given offset until the given boundary is found, and
388 // return the offset of that boundary.
389 LONG
FindBoundary(const string16
& text
,
390 IA2TextBoundaryType ia2_boundary
,
392 ui::TextBoundaryDirection direction
);
394 // Populates the given vector with all widgets that are either a child
395 // or are owned by this view's widget, and who are not contained in a
397 void PopulateChildWidgetVector(std::vector
<Widget
*>* child_widgets
);
399 // Give CComObject access to the class constructor.
400 template <class Base
> friend class CComObject
;
402 // Member View needed for view-specific calls.
405 // A unique id for each object, needed for IAccessible2.
408 // Next unique id to assign.
409 static long next_unique_id_
;
411 // Circular queue size.
412 static const int kMaxViewStorageIds
= 20;
414 // Circular queue of view storage ids corresponding to child ids
415 // used to post notifications using NotifyWinEvent.
416 static int view_storage_ids_
[kMaxViewStorageIds
];
418 // Next index into |view_storage_ids_| to use.
419 static int next_view_storage_id_index_
;
421 DISALLOW_COPY_AND_ASSIGN(NativeViewAccessibilityWin
);
426 #endif // UI_VIEWS_ACCESSIBILITY_NATIVE_VIEW_ACCESSIBILITY_WIN_H_