OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/tabbed_pane/tabbed_pane.h" | 5 #include "ui/views/controls/tabbed_pane/tabbed_pane.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ui/accessibility/ax_view_state.h" | 8 #include "ui/accessibility/ax_view_state.h" |
9 #include "ui/base/resource/resource_bundle.h" | 9 #include "ui/base/resource/resource_bundle.h" |
10 #include "ui/events/keycodes/keyboard_codes.h" | 10 #include "ui/events/keycodes/keyboard_codes.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 } // namespace | 27 } // namespace |
28 | 28 |
29 namespace views { | 29 namespace views { |
30 | 30 |
31 // static | 31 // static |
32 const char TabbedPane::kViewClassName[] = "TabbedPane"; | 32 const char TabbedPane::kViewClassName[] = "TabbedPane"; |
33 | 33 |
34 // The tab view shown in the tab strip. | 34 // The tab view shown in the tab strip. |
35 class Tab : public View { | 35 class Tab : public View { |
36 public: | 36 public: |
| 37 // Internal class name. |
| 38 static const char kViewClassName[]; |
| 39 |
37 Tab(TabbedPane* tabbed_pane, const base::string16& title, View* contents); | 40 Tab(TabbedPane* tabbed_pane, const base::string16& title, View* contents); |
38 ~Tab() override; | 41 ~Tab() override; |
39 | 42 |
40 View* contents() const { return contents_; } | 43 View* contents() const { return contents_; } |
41 | 44 |
42 bool selected() const { return contents_->visible(); } | 45 bool selected() const { return contents_->visible(); } |
43 void SetSelected(bool selected); | 46 void SetSelected(bool selected); |
44 | 47 |
45 // Overridden from View: | 48 // Overridden from View: |
46 bool OnMousePressed(const ui::MouseEvent& event) override; | 49 bool OnMousePressed(const ui::MouseEvent& event) override; |
47 void OnMouseEntered(const ui::MouseEvent& event) override; | 50 void OnMouseEntered(const ui::MouseEvent& event) override; |
48 void OnMouseExited(const ui::MouseEvent& event) override; | 51 void OnMouseExited(const ui::MouseEvent& event) override; |
49 void OnGestureEvent(ui::GestureEvent* event) override; | 52 void OnGestureEvent(ui::GestureEvent* event) override; |
50 gfx::Size GetPreferredSize() const override; | 53 gfx::Size GetPreferredSize() const override; |
51 void Layout() override; | 54 void Layout() override; |
| 55 const char* GetClassName() const override; |
52 | 56 |
53 private: | 57 private: |
54 enum TabState { | 58 enum TabState { |
55 TAB_INACTIVE, | 59 TAB_INACTIVE, |
56 TAB_ACTIVE, | 60 TAB_ACTIVE, |
57 TAB_HOVERED, | 61 TAB_HOVERED, |
58 }; | 62 }; |
59 | 63 |
60 void SetState(TabState tab_state); | 64 void SetState(TabState tab_state); |
61 | 65 |
62 TabbedPane* tabbed_pane_; | 66 TabbedPane* tabbed_pane_; |
63 Label* title_; | 67 Label* title_; |
64 gfx::Size preferred_title_size_; | 68 gfx::Size preferred_title_size_; |
65 TabState tab_state_; | 69 TabState tab_state_; |
66 // The content view associated with this tab. | 70 // The content view associated with this tab. |
67 View* contents_; | 71 View* contents_; |
68 | 72 |
69 DISALLOW_COPY_AND_ASSIGN(Tab); | 73 DISALLOW_COPY_AND_ASSIGN(Tab); |
70 }; | 74 }; |
71 | 75 |
72 // The tab strip shown above the tab contents. | 76 // The tab strip shown above the tab contents. |
73 class TabStrip : public View { | 77 class TabStrip : public View { |
74 public: | 78 public: |
| 79 // Internal class name. |
| 80 static const char kViewClassName[]; |
| 81 |
75 explicit TabStrip(TabbedPane* tabbed_pane); | 82 explicit TabStrip(TabbedPane* tabbed_pane); |
76 ~TabStrip() override; | 83 ~TabStrip() override; |
77 | 84 |
78 // Overridden from View: | 85 // Overridden from View: |
79 gfx::Size GetPreferredSize() const override; | 86 gfx::Size GetPreferredSize() const override; |
80 void Layout() override; | 87 void Layout() override; |
| 88 const char* GetClassName() const override; |
81 void OnPaint(gfx::Canvas* canvas) override; | 89 void OnPaint(gfx::Canvas* canvas) override; |
82 | 90 |
83 private: | 91 private: |
84 TabbedPane* tabbed_pane_; | 92 TabbedPane* tabbed_pane_; |
85 | 93 |
86 DISALLOW_COPY_AND_ASSIGN(TabStrip); | 94 DISALLOW_COPY_AND_ASSIGN(TabStrip); |
87 }; | 95 }; |
88 | 96 |
| 97 // static |
| 98 const char Tab::kViewClassName[] = "Tab"; |
| 99 |
89 Tab::Tab(TabbedPane* tabbed_pane, const base::string16& title, View* contents) | 100 Tab::Tab(TabbedPane* tabbed_pane, const base::string16& title, View* contents) |
90 : tabbed_pane_(tabbed_pane), | 101 : tabbed_pane_(tabbed_pane), |
91 title_(new Label(title, | 102 title_(new Label(title, |
92 ui::ResourceBundle::GetSharedInstance().GetFontList( | 103 ui::ResourceBundle::GetSharedInstance().GetFontList( |
93 ui::ResourceBundle::BoldFont))), | 104 ui::ResourceBundle::BoldFont))), |
94 tab_state_(TAB_ACTIVE), | 105 tab_state_(TAB_ACTIVE), |
95 contents_(contents) { | 106 contents_(contents) { |
96 // Calculate this now while the font list is guaranteed to be bold. | 107 // Calculate this now while the font list is guaranteed to be bold. |
97 preferred_title_size_ = title_->GetPreferredSize(); | 108 preferred_title_size_ = title_->GetPreferredSize(); |
98 | 109 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 return size; | 159 return size; |
149 } | 160 } |
150 | 161 |
151 void Tab::Layout() { | 162 void Tab::Layout() { |
152 gfx::Rect bounds = GetLocalBounds(); | 163 gfx::Rect bounds = GetLocalBounds(); |
153 bounds.Inset(0, 1, 0, 0); | 164 bounds.Inset(0, 1, 0, 0); |
154 bounds.ClampToCenteredSize(preferred_title_size_); | 165 bounds.ClampToCenteredSize(preferred_title_size_); |
155 title_->SetBoundsRect(bounds); | 166 title_->SetBoundsRect(bounds); |
156 } | 167 } |
157 | 168 |
| 169 const char* Tab::GetClassName() const { |
| 170 return kViewClassName; |
| 171 } |
| 172 |
158 void Tab::SetState(TabState tab_state) { | 173 void Tab::SetState(TabState tab_state) { |
159 if (tab_state == tab_state_) | 174 if (tab_state == tab_state_) |
160 return; | 175 return; |
161 tab_state_ = tab_state; | 176 tab_state_ = tab_state; |
162 | 177 |
163 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 178 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
164 switch (tab_state) { | 179 switch (tab_state) { |
165 case TAB_INACTIVE: | 180 case TAB_INACTIVE: |
166 title_->SetEnabledColor(kTabTitleColor_Inactive); | 181 title_->SetEnabledColor(kTabTitleColor_Inactive); |
167 title_->SetFontList(rb.GetFontList(ui::ResourceBundle::BaseFont)); | 182 title_->SetFontList(rb.GetFontList(ui::ResourceBundle::BaseFont)); |
168 break; | 183 break; |
169 case TAB_ACTIVE: | 184 case TAB_ACTIVE: |
170 title_->SetEnabledColor(kTabTitleColor_Active); | 185 title_->SetEnabledColor(kTabTitleColor_Active); |
171 title_->SetFontList(rb.GetFontList(ui::ResourceBundle::BoldFont)); | 186 title_->SetFontList(rb.GetFontList(ui::ResourceBundle::BoldFont)); |
172 break; | 187 break; |
173 case TAB_HOVERED: | 188 case TAB_HOVERED: |
174 title_->SetEnabledColor(kTabTitleColor_Hovered); | 189 title_->SetEnabledColor(kTabTitleColor_Hovered); |
175 title_->SetFontList(rb.GetFontList(ui::ResourceBundle::BaseFont)); | 190 title_->SetFontList(rb.GetFontList(ui::ResourceBundle::BaseFont)); |
176 break; | 191 break; |
177 } | 192 } |
178 SchedulePaint(); | 193 SchedulePaint(); |
179 } | 194 } |
180 | 195 |
| 196 // static |
| 197 const char TabStrip::kViewClassName[] = "TabStrip"; |
| 198 |
181 TabStrip::TabStrip(TabbedPane* tabbed_pane) : tabbed_pane_(tabbed_pane) {} | 199 TabStrip::TabStrip(TabbedPane* tabbed_pane) : tabbed_pane_(tabbed_pane) {} |
182 | 200 |
183 TabStrip::~TabStrip() {} | 201 TabStrip::~TabStrip() {} |
184 | 202 |
185 gfx::Size TabStrip::GetPreferredSize() const { | 203 gfx::Size TabStrip::GetPreferredSize() const { |
186 gfx::Size size; | 204 gfx::Size size; |
187 for (int i = 0; i < child_count(); ++i) { | 205 for (int i = 0; i < child_count(); ++i) { |
188 const gfx::Size child_size = child_at(i)->GetPreferredSize(); | 206 const gfx::Size child_size = child_at(i)->GetPreferredSize(); |
189 size.SetSize(size.width() + child_size.width(), | 207 size.SetSize(size.width() + child_size.width(), |
190 std::max(size.height(), child_size.height())); | 208 std::max(size.height(), child_size.height())); |
191 } | 209 } |
192 return size; | 210 return size; |
193 } | 211 } |
194 | 212 |
195 void TabStrip::Layout() { | 213 void TabStrip::Layout() { |
196 const int kTabOffset = 9; | 214 const int kTabOffset = 9; |
197 int x = kTabOffset; // Layout tabs with an offset to the tabstrip border. | 215 int x = kTabOffset; // Layout tabs with an offset to the tabstrip border. |
198 for (int i = 0; i < child_count(); ++i) { | 216 for (int i = 0; i < child_count(); ++i) { |
199 gfx::Size ps = child_at(i)->GetPreferredSize(); | 217 gfx::Size ps = child_at(i)->GetPreferredSize(); |
200 child_at(i)->SetBounds(x, 0, ps.width(), ps.height()); | 218 child_at(i)->SetBounds(x, 0, ps.width(), ps.height()); |
201 x = child_at(i)->bounds().right(); | 219 x = child_at(i)->bounds().right(); |
202 } | 220 } |
203 } | 221 } |
204 | 222 |
| 223 const char* TabStrip::GetClassName() const { |
| 224 return kViewClassName; |
| 225 } |
| 226 |
205 void TabStrip::OnPaint(gfx::Canvas* canvas) { | 227 void TabStrip::OnPaint(gfx::Canvas* canvas) { |
206 OnPaintBackground(canvas); | 228 OnPaintBackground(canvas); |
207 | 229 |
208 // Draw the TabStrip border. | 230 // Draw the TabStrip border. |
209 SkPaint paint; | 231 SkPaint paint; |
210 paint.setColor(kTabBorderColor); | 232 paint.setColor(kTabBorderColor); |
211 paint.setStrokeWidth(kTabBorderThickness); | 233 paint.setStrokeWidth(kTabBorderThickness); |
212 SkScalar line_y = SkIntToScalar(height()) - (kTabBorderThickness / 2); | 234 SkScalar line_y = SkIntToScalar(height()) - (kTabBorderThickness / 2); |
213 SkScalar line_end = SkIntToScalar(width()); | 235 SkScalar line_end = SkIntToScalar(width()); |
214 int selected_tab_index = tabbed_pane_->selected_tab_index(); | 236 int selected_tab_index = tabbed_pane_->selected_tab_index(); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 selected_tab->NotifyAccessibilityEvent( | 388 selected_tab->NotifyAccessibilityEvent( |
367 ui::AX_EVENT_FOCUS, true); | 389 ui::AX_EVENT_FOCUS, true); |
368 } | 390 } |
369 } | 391 } |
370 | 392 |
371 void TabbedPane::GetAccessibleState(ui::AXViewState* state) { | 393 void TabbedPane::GetAccessibleState(ui::AXViewState* state) { |
372 state->role = ui::AX_ROLE_TAB_LIST; | 394 state->role = ui::AX_ROLE_TAB_LIST; |
373 } | 395 } |
374 | 396 |
375 } // namespace views | 397 } // namespace views |
OLD | NEW |