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.
4
5#include "ui/views/accessibility/ax_view_obj_wrapper.h"
6
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/views/accessibility/ax_aura_obj_cache.h"
11#include "ui/views/view.h"
12#include "ui/views/widget/widget.h"
13
14namespace views {
15
16AXViewObjWrapper::AXViewObjWrapper(View* view)  : view_(view) {
17  DCHECK(view->GetWidget());
18  if (view->GetWidget())
19    AXAuraObjCache::GetInstance()->GetOrCreate(view->GetWidget());
20}
21
22AXViewObjWrapper::~AXViewObjWrapper() {}
23
24AXAuraObjWrapper* AXViewObjWrapper::GetParent() {
25  AXAuraObjCache* cache = AXAuraObjCache::GetInstance();
26  if (view_->parent())
27    return cache->GetOrCreate(view_->parent());
28
29  return cache->GetOrCreate(view_->GetWidget());
30}
31
32void AXViewObjWrapper::GetChildren(
33    std::vector<AXAuraObjWrapper*>* out_children) {
34  // TODO(dtseng): Need to handle |Widget| child of |View|.
35  for (int i = 0; i < view_->child_count(); ++i) {
36    AXAuraObjWrapper* child =
37        AXAuraObjCache::GetInstance()->GetOrCreate(view_->child_at(i));
38    out_children->push_back(child);
39  }
40}
41
42void AXViewObjWrapper::Serialize(ui::AXNodeData* out_node_data) {
43  ui::AXViewState view_data;
44  view_->GetAccessibleState(&view_data);
45  out_node_data->id = GetID();
46  out_node_data->role = view_data.role;
47
48  out_node_data->state = view_data.state();
49  if (view_->HasFocus())
50    out_node_data->state |= 1 << ui::AX_STATE_FOCUSED;
51  if (view_->IsFocusable())
52    out_node_data->state |= 1 << ui::AX_STATE_FOCUSABLE;
53
54  out_node_data->location = view_->GetBoundsInScreen();
55
56  out_node_data->AddStringAttribute(
57      ui::AX_ATTR_NAME, base::UTF16ToUTF8(view_data.name));
58  out_node_data->AddStringAttribute(
59      ui::AX_ATTR_VALUE, base::UTF16ToUTF8(view_data.value));
60
61  out_node_data->AddIntAttribute(ui::AX_ATTR_TEXT_SEL_START,
62                                 view_data.selection_start);
63  out_node_data->AddIntAttribute(ui::AX_ATTR_TEXT_SEL_END,
64                                 view_data.selection_end);
65}
66
67int32 AXViewObjWrapper::GetID() {
68  return AXAuraObjCache::GetInstance()->GetID(view_);
69}
70
71void AXViewObjWrapper::DoDefault() {
72  gfx::Rect rect = view_->GetBoundsInScreen();
73  gfx::Point center = rect.CenterPoint();
74  view_->OnMousePressed(ui::MouseEvent(ui::ET_MOUSE_PRESSED, center, center,
75                                       ui::EF_LEFT_MOUSE_BUTTON,
76                                       ui::EF_LEFT_MOUSE_BUTTON));
77}
78
79void AXViewObjWrapper::Focus() {
80  view_->RequestFocus();
81}
82
83void AXViewObjWrapper::MakeVisible() {
84  // TODO(dtseng): Implement.
85}
86
87void AXViewObjWrapper::SetSelection(int32 start, int32 end) {
88  // TODO(dtseng): Implement.
89}
90
91}  // namespace views
92