1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "views/controls/tabbed_pane/tabbed_pane.h"
#include "base/logging.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/keycodes/keyboard_codes.h"
#include "views/controls/native/native_view_host.h"
#include "views/controls/tabbed_pane/native_tabbed_pane_wrapper.h"
#include "views/controls/tabbed_pane/tabbed_pane_listener.h"
#include "views/widget/widget.h"
namespace views {
// static
const char TabbedPane::kViewClassName[] = "views/TabbedPane";
TabbedPane::TabbedPane() : native_tabbed_pane_(NULL), listener_(NULL) {
set_focusable(true);
}
TabbedPane::~TabbedPane() {
}
int TabbedPane::GetTabCount() {
return native_tabbed_pane_->GetTabCount();
}
int TabbedPane::GetSelectedTabIndex() {
return native_tabbed_pane_->GetSelectedTabIndex();
}
View* TabbedPane::GetSelectedTab() {
return native_tabbed_pane_->GetSelectedTab();
}
void TabbedPane::AddTab(const string16& title, View* contents) {
native_tabbed_pane_->AddTab(title, contents);
PreferredSizeChanged();
}
void TabbedPane::AddTabAtIndex(int index,
const string16& title,
View* contents,
bool select_if_first_tab) {
native_tabbed_pane_->AddTabAtIndex(index, title, contents,
select_if_first_tab);
PreferredSizeChanged();
}
View* TabbedPane::RemoveTabAtIndex(int index) {
View* tab = native_tabbed_pane_->RemoveTabAtIndex(index);
PreferredSizeChanged();
return tab;
}
void TabbedPane::SelectTabAt(int index) {
native_tabbed_pane_->SelectTabAt(index);
}
void TabbedPane::SetAccessibleName(const string16& name) {
accessible_name_ = name;
}
gfx::Size TabbedPane::GetPreferredSize() {
return native_tabbed_pane_ ?
native_tabbed_pane_->GetPreferredSize() : gfx::Size();
}
void TabbedPane::LoadAccelerators() {
// Ctrl+Shift+Tab
AddAccelerator(ui::Accelerator(ui::VKEY_TAB, true, true, false));
// Ctrl+Tab
AddAccelerator(ui::Accelerator(ui::VKEY_TAB, false, true, false));
}
void TabbedPane::Layout() {
if (native_tabbed_pane_)
native_tabbed_pane_->GetView()->SetBounds(0, 0, width(), height());
}
void TabbedPane::ViewHierarchyChanged(bool is_add, View* parent, View* child) {
if (is_add && !native_tabbed_pane_) {
// The native wrapper's lifetime will be managed by the view hierarchy after
// we call AddChildView.
native_tabbed_pane_ = NativeTabbedPaneWrapper::CreateNativeWrapper(this);
AddChildView(native_tabbed_pane_->GetView());
LoadAccelerators();
}
}
bool TabbedPane::AcceleratorPressed(const ui::Accelerator& accelerator) {
// We only accept Ctrl+Tab keyboard events.
DCHECK(accelerator.key_code() == ui::VKEY_TAB && accelerator.IsCtrlDown());
int tab_count = GetTabCount();
if (tab_count <= 1)
return false;
int selected_tab_index = GetSelectedTabIndex();
int next_tab_index = accelerator.IsShiftDown() ?
(selected_tab_index - 1) % tab_count :
(selected_tab_index + 1) % tab_count;
// Wrap around.
if (next_tab_index < 0)
next_tab_index += tab_count;
SelectTabAt(next_tab_index);
return true;
}
std::string TabbedPane::GetClassName() const {
return kViewClassName;
}
void TabbedPane::OnFocus() {
// Forward the focus to the wrapper.
if (native_tabbed_pane_) {
native_tabbed_pane_->SetFocus();
View* selected_tab = GetSelectedTab();
if (selected_tab) {
selected_tab->GetWidget()->NotifyAccessibilityEvent(
selected_tab, ui::AccessibilityTypes::EVENT_FOCUS, true);
}
} else {
View::OnFocus(); // Will focus the RootView window (so we still get
// keyboard messages).
}
}
void TabbedPane::OnPaintFocusBorder(gfx::Canvas* canvas) {
if (NativeViewHost::kRenderNativeControlFocus)
View::OnPaintFocusBorder(canvas);
}
void TabbedPane::GetAccessibleState(ui::AccessibleViewState* state) {
state->role = ui::AccessibilityTypes::ROLE_PAGETABLIST;
state->name = accessible_name_;
}
} // namespace views
|