1 // Copyright 2014 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 #include "ui/views/accessibility/ax_view_obj_wrapper.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/accessibility/ax_node_data.h"
9 #include "ui/accessibility/ax_view_state.h"
10 #include "ui/events/event_utils.h"
11 #include "ui/views/accessibility/ax_aura_obj_cache.h"
12 #include "ui/views/view.h"
13 #include "ui/views/widget/widget.h"
17 AXViewObjWrapper::AXViewObjWrapper(View
* view
) : view_(view
) {
18 if (view
->GetWidget())
19 AXAuraObjCache::GetInstance()->GetOrCreate(view
->GetWidget());
22 AXViewObjWrapper::~AXViewObjWrapper() {}
24 AXAuraObjWrapper
* AXViewObjWrapper::GetParent() {
25 AXAuraObjCache
* cache
= AXAuraObjCache::GetInstance();
27 return cache
->GetOrCreate(view_
->parent());
29 if (view_
->GetWidget())
30 return cache
->GetOrCreate(view_
->GetWidget());
35 void AXViewObjWrapper::GetChildren(
36 std::vector
<AXAuraObjWrapper
*>* out_children
) {
37 // TODO(dtseng): Need to handle |Widget| child of |View|.
38 for (int i
= 0; i
< view_
->child_count(); ++i
) {
39 AXAuraObjWrapper
* child
=
40 AXAuraObjCache::GetInstance()->GetOrCreate(view_
->child_at(i
));
41 out_children
->push_back(child
);
45 void AXViewObjWrapper::Serialize(ui::AXNodeData
* out_node_data
) {
46 ui::AXViewState view_data
;
47 view_
->GetAccessibleState(&view_data
);
49 out_node_data
->id
= GetID();
50 out_node_data
->role
= view_data
.role
;
52 out_node_data
->state
= view_data
.state();
53 if (view_
->HasFocus())
54 out_node_data
->state
|= 1 << ui::AX_STATE_FOCUSED
;
55 if (view_
->IsFocusable())
56 out_node_data
->state
|= 1 << ui::AX_STATE_FOCUSABLE
;
58 out_node_data
->location
= view_
->GetBoundsInScreen();
60 out_node_data
->AddStringAttribute(
61 ui::AX_ATTR_NAME
, base::UTF16ToUTF8(view_data
.name
));
62 out_node_data
->AddStringAttribute(
63 ui::AX_ATTR_VALUE
, base::UTF16ToUTF8(view_data
.value
));
65 if (view_data
.selection_start
> -1 && view_data
.selection_end
> -1) {
66 out_node_data
->AddIntAttribute(ui::AX_ATTR_TEXT_SEL_START
,
67 view_data
.selection_start
);
69 out_node_data
->AddIntAttribute(ui::AX_ATTR_TEXT_SEL_END
,
70 view_data
.selection_end
);
74 int32
AXViewObjWrapper::GetID() {
75 return AXAuraObjCache::GetInstance()->GetID(view_
);
78 void AXViewObjWrapper::DoDefault() {
79 gfx::Rect rect
= view_
->GetLocalBounds();
80 gfx::Point center
= rect
.CenterPoint();
81 view_
->OnMousePressed(ui::MouseEvent(
82 ui::ET_MOUSE_PRESSED
, center
, center
, ui::EventTimeForNow(),
83 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
));
84 view_
->OnMouseReleased(ui::MouseEvent(
85 ui::ET_MOUSE_RELEASED
, center
, center
, ui::EventTimeForNow(),
86 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
));
89 void AXViewObjWrapper::Focus() {
90 view_
->RequestFocus();
93 void AXViewObjWrapper::MakeVisible() {
94 // TODO(dtseng): Implement.
97 void AXViewObjWrapper::SetSelection(int32 start
, int32 end
) {
98 // TODO(dtseng): Implement.
101 void AXViewObjWrapper::ShowContextMenu() {
102 view_
->ShowContextMenu(view_
->bounds().CenterPoint(),
103 ui::MENU_SOURCE_KEYBOARD
);