• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/views/controls/tabbed_pane/tabbed_pane.h"
6 
7 #include "base/logging.h"
8 #include "ui/base/accessibility/accessible_view_state.h"
9 #include "ui/events/keycodes/keyboard_codes.h"
10 #include "ui/gfx/canvas.h"
11 #include "ui/gfx/font.h"
12 #include "ui/views/controls/label.h"
13 #include "ui/views/controls/tabbed_pane/tabbed_pane_listener.h"
14 #include "ui/views/layout/layout_manager.h"
15 #include "ui/views/widget/widget.h"
16 
17 namespace {
18 
19 // TODO(markusheintz|msw): Use NativeTheme colors.
20 const SkColor kTabTitleColor_Inactive = SkColorSetRGB(0x64, 0x64, 0x64);
21 const SkColor kTabTitleColor_Active = SK_ColorBLACK;
22 const SkColor kTabTitleColor_Hovered = SK_ColorBLACK;
23 const SkColor kTabBorderColor = SkColorSetRGB(0xC8, 0xC8, 0xC8);
24 const SkScalar kTabBorderThickness = 1.0f;
25 
26 }  // namespace
27 
28 namespace views {
29 
30 // static
31 const char TabbedPane::kViewClassName[] = "TabbedPane";
32 
33 // The tab view shown in the tab strip.
34 class Tab : public View {
35  public:
36   Tab(TabbedPane* tabbed_pane, const string16& title, View* contents);
37   virtual ~Tab();
38 
contents() const39   View* contents() const { return contents_; }
40 
selected() const41   bool selected() const { return contents_->visible(); }
42   void SetSelected(bool selected);
43 
44   // Overridden from View:
45   virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
46   virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
47   virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
48   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
49   virtual gfx::Size GetPreferredSize() OVERRIDE;
50   virtual void Layout() OVERRIDE;
51 
52  private:
53   enum TabState {
54     TAB_INACTIVE,
55     TAB_ACTIVE,
56     TAB_HOVERED,
57   };
58 
59   void SetState(TabState tab_state);
60 
61   TabbedPane* tabbed_pane_;
62   Label* title_;
63   gfx::Size preferred_title_size_;
64   TabState tab_state_;
65   // The content view associated with this tab.
66   View* contents_;
67 
68   DISALLOW_COPY_AND_ASSIGN(Tab);
69 };
70 
71 // The tab strip shown above the tab contents.
72 class TabStrip : public View {
73  public:
74   explicit TabStrip(TabbedPane* tabbed_pane);
75   virtual ~TabStrip();
76 
77   // Overridden from View:
78   virtual gfx::Size GetPreferredSize() OVERRIDE;
79   virtual void Layout() OVERRIDE;
80   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
81 
82  private:
83   TabbedPane* tabbed_pane_;
84 
85   DISALLOW_COPY_AND_ASSIGN(TabStrip);
86 };
87 
Tab(TabbedPane * tabbed_pane,const string16 & title,View * contents)88 Tab::Tab(TabbedPane* tabbed_pane, const string16& title, View* contents)
89     : tabbed_pane_(tabbed_pane),
90       title_(new Label(title, gfx::Font().DeriveFont(0, gfx::Font::BOLD))),
91       tab_state_(TAB_ACTIVE),
92       contents_(contents) {
93   // Calculate this now while the font is guaranteed to be bold.
94   preferred_title_size_ = title_->GetPreferredSize();
95 
96   SetState(TAB_INACTIVE);
97   AddChildView(title_);
98 }
99 
~Tab()100 Tab::~Tab() {}
101 
SetSelected(bool selected)102 void Tab::SetSelected(bool selected) {
103   contents_->SetVisible(selected);
104   SetState(selected ? TAB_ACTIVE : TAB_INACTIVE);
105 }
106 
OnMousePressed(const ui::MouseEvent & event)107 bool Tab::OnMousePressed(const ui::MouseEvent& event) {
108   if (event.IsOnlyLeftMouseButton() &&
109       GetLocalBounds().Contains(event.location()))
110     tabbed_pane_->SelectTab(this);
111   return true;
112 }
113 
OnMouseEntered(const ui::MouseEvent & event)114 void Tab::OnMouseEntered(const ui::MouseEvent& event) {
115   SetState(selected() ? TAB_ACTIVE : TAB_HOVERED);
116 }
117 
OnMouseExited(const ui::MouseEvent & event)118 void Tab::OnMouseExited(const ui::MouseEvent& event) {
119   SetState(selected() ? TAB_ACTIVE : TAB_INACTIVE);
120 }
121 
OnGestureEvent(ui::GestureEvent * event)122 void Tab::OnGestureEvent(ui::GestureEvent* event) {
123   switch (event->type()) {
124     case ui::ET_GESTURE_TAP_DOWN:
125       // Fallthrough.
126     case ui::ET_GESTURE_TAP:
127       // SelectTab also sets the right tab color.
128       tabbed_pane_->SelectTab(this);
129       break;
130     case ui::ET_GESTURE_TAP_CANCEL:
131       SetState(selected() ? TAB_ACTIVE : TAB_INACTIVE);
132       break;
133     default:
134       break;
135   }
136   event->SetHandled();
137 }
138 
GetPreferredSize()139 gfx::Size Tab::GetPreferredSize() {
140   gfx::Size size(preferred_title_size_);
141   size.Enlarge(21, 9);
142   const int kTabMinWidth = 54;
143   if (size.width() < kTabMinWidth)
144     size.set_width(kTabMinWidth);
145   return size;
146 }
147 
Layout()148 void Tab::Layout() {
149   gfx::Rect bounds = GetLocalBounds();
150   bounds.Inset(0, 1, 0, 0);
151   bounds.ClampToCenteredSize(preferred_title_size_);
152   title_->SetBoundsRect(bounds);
153 }
154 
SetState(TabState tab_state)155 void Tab::SetState(TabState tab_state) {
156   if (tab_state == tab_state_)
157     return;
158   tab_state_ = tab_state;
159 
160   switch (tab_state) {
161     case TAB_INACTIVE:
162       title_->SetEnabledColor(kTabTitleColor_Inactive);
163       title_->SetFont(gfx::Font());
164       break;
165     case TAB_ACTIVE:
166       title_->SetEnabledColor(kTabTitleColor_Active);
167       title_->SetFont(gfx::Font().DeriveFont(0, gfx::Font::BOLD));
168       break;
169     case TAB_HOVERED:
170       title_->SetEnabledColor(kTabTitleColor_Hovered);
171       title_->SetFont(gfx::Font());
172       break;
173   }
174   SchedulePaint();
175 }
176 
TabStrip(TabbedPane * tabbed_pane)177 TabStrip::TabStrip(TabbedPane* tabbed_pane) : tabbed_pane_(tabbed_pane) {}
178 
~TabStrip()179 TabStrip::~TabStrip() {}
180 
GetPreferredSize()181 gfx::Size TabStrip::GetPreferredSize() {
182   gfx::Size size;
183   for (int i = 0; i < child_count(); ++i) {
184     const gfx::Size child_size = child_at(i)->GetPreferredSize();
185     size.SetSize(size.width() + child_size.width(),
186                  std::max(size.height(), child_size.height()));
187   }
188   return size;
189 }
190 
Layout()191 void TabStrip::Layout() {
192   const int kTabOffset = 9;
193   int x = kTabOffset;  // Layout tabs with an offset to the tabstrip border.
194   for (int i = 0; i < child_count(); ++i) {
195     gfx::Size ps = child_at(i)->GetPreferredSize();
196     child_at(i)->SetBounds(x, 0, ps.width(), ps.height());
197     x = child_at(i)->bounds().right();
198   }
199 }
200 
OnPaint(gfx::Canvas * canvas)201 void TabStrip::OnPaint(gfx::Canvas* canvas) {
202   OnPaintBackground(canvas);
203 
204   // Draw the TabStrip border.
205   SkPaint paint;
206   paint.setColor(kTabBorderColor);
207   paint.setStrokeWidth(kTabBorderThickness);
208   SkScalar line_y = SkIntToScalar(height()) - (kTabBorderThickness / 2);
209   SkScalar line_end = SkIntToScalar(width());
210   int selected_tab_index = tabbed_pane_->selected_tab_index();
211   if (selected_tab_index >= 0) {
212     Tab* selected_tab = tabbed_pane_->GetTabAt(selected_tab_index);
213     SkPath path;
214     SkScalar tab_height =
215         SkIntToScalar(selected_tab->height()) - kTabBorderThickness;
216     SkScalar tab_width =
217         SkIntToScalar(selected_tab->width()) - kTabBorderThickness;
218     SkScalar tab_start = SkIntToScalar(selected_tab->GetMirroredX());
219     path.moveTo(0, line_y);
220     path.rLineTo(tab_start, 0);
221     path.rLineTo(0, -tab_height);
222     path.rLineTo(tab_width, 0);
223     path.rLineTo(0, tab_height);
224     path.lineTo(line_end, line_y);
225 
226     SkPaint paint;
227     paint.setStyle(SkPaint::kStroke_Style);
228     paint.setColor(kTabBorderColor);
229     paint.setStrokeWidth(kTabBorderThickness);
230     canvas->DrawPath(path, paint);
231   } else {
232     canvas->sk_canvas()->drawLine(0, line_y, line_end, line_y, paint);
233   }
234 }
235 
TabbedPane()236 TabbedPane::TabbedPane()
237   : listener_(NULL),
238     tab_strip_(new TabStrip(this)),
239     contents_(new View()),
240     selected_tab_index_(-1) {
241   SetFocusable(true);
242   AddChildView(tab_strip_);
243   AddChildView(contents_);
244 }
245 
~TabbedPane()246 TabbedPane::~TabbedPane() {}
247 
GetTabCount()248 int TabbedPane::GetTabCount() {
249   DCHECK_EQ(tab_strip_->child_count(), contents_->child_count());
250   return contents_->child_count();
251 }
252 
GetSelectedTab()253 View* TabbedPane::GetSelectedTab() {
254   return selected_tab_index() < 0 ?
255       NULL : GetTabAt(selected_tab_index())->contents();
256 }
257 
AddTab(const string16 & title,View * contents)258 void TabbedPane::AddTab(const string16& title, View* contents) {
259   AddTabAtIndex(tab_strip_->child_count(), title, contents);
260 }
261 
AddTabAtIndex(int index,const string16 & title,View * contents)262 void TabbedPane::AddTabAtIndex(int index,
263                                const string16& title,
264                                View* contents) {
265   DCHECK(index >= 0 && index <= GetTabCount());
266   contents->SetVisible(false);
267 
268   tab_strip_->AddChildViewAt(new Tab(this, title, contents), index);
269   contents_->AddChildViewAt(contents, index);
270   if (selected_tab_index() < 0)
271     SelectTabAt(index);
272 
273   PreferredSizeChanged();
274 }
275 
SelectTabAt(int index)276 void TabbedPane::SelectTabAt(int index) {
277   DCHECK(index >= 0 && index < GetTabCount());
278   if (index == selected_tab_index())
279     return;
280 
281   if (selected_tab_index() >= 0)
282     GetTabAt(selected_tab_index())->SetSelected(false);
283 
284   selected_tab_index_ = index;
285   Tab* tab = GetTabAt(index);
286   tab->SetSelected(true);
287   tab_strip_->SchedulePaint();
288 
289   FocusManager* focus_manager = tab->contents()->GetFocusManager();
290   if (focus_manager) {
291     const View* focused_view = focus_manager->GetFocusedView();
292     if (focused_view && contents_->Contains(focused_view) &&
293         !tab->contents()->Contains(focused_view))
294       focus_manager->SetFocusedView(tab->contents());
295   }
296 
297   if (listener())
298     listener()->TabSelectedAt(index);
299 }
300 
SelectTab(Tab * tab)301 void TabbedPane::SelectTab(Tab* tab) {
302   const int index = tab_strip_->GetIndexOf(tab);
303   if (index >= 0)
304     SelectTabAt(index);
305 }
306 
GetPreferredSize()307 gfx::Size TabbedPane::GetPreferredSize() {
308   gfx::Size size;
309   for (int i = 0; i < contents_->child_count(); ++i)
310     size.SetToMax(contents_->child_at(i)->GetPreferredSize());
311   size.Enlarge(0, tab_strip_->GetPreferredSize().height());
312   return size;
313 }
314 
GetTabAt(int index)315 Tab* TabbedPane::GetTabAt(int index) {
316   return static_cast<Tab*>(tab_strip_->child_at(index));
317 }
318 
Layout()319 void TabbedPane::Layout() {
320   const gfx::Size size = tab_strip_->GetPreferredSize();
321   tab_strip_->SetBounds(0, 0, width(), size.height());
322   contents_->SetBounds(0, tab_strip_->bounds().bottom(), width(),
323                        std::max(0, height() - size.height()));
324   for (int i = 0; i < contents_->child_count(); ++i)
325     contents_->child_at(i)->SetSize(contents_->size());
326 }
327 
ViewHierarchyChanged(const ViewHierarchyChangedDetails & details)328 void TabbedPane::ViewHierarchyChanged(
329     const ViewHierarchyChangedDetails& details) {
330   if (details.is_add) {
331     // Support navigating tabs by Ctrl+Tab and Ctrl+Shift+Tab.
332     AddAccelerator(ui::Accelerator(ui::VKEY_TAB,
333                                    ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN));
334     AddAccelerator(ui::Accelerator(ui::VKEY_TAB, ui::EF_CONTROL_DOWN));
335   }
336 }
337 
AcceleratorPressed(const ui::Accelerator & accelerator)338 bool TabbedPane::AcceleratorPressed(const ui::Accelerator& accelerator) {
339   // Handle Ctrl+Tab and Ctrl+Shift+Tab navigation of pages.
340   DCHECK(accelerator.key_code() == ui::VKEY_TAB && accelerator.IsCtrlDown());
341   const int tab_count = GetTabCount();
342   if (tab_count <= 1)
343     return false;
344   const int increment = accelerator.IsShiftDown() ? -1 : 1;
345   int next_tab_index = (selected_tab_index() + increment) % tab_count;
346   // Wrap around.
347   if (next_tab_index < 0)
348     next_tab_index += tab_count;
349   SelectTabAt(next_tab_index);
350   return true;
351 }
352 
GetClassName() const353 const char* TabbedPane::GetClassName() const {
354   return kViewClassName;
355 }
356 
OnFocus()357 void TabbedPane::OnFocus() {
358   View::OnFocus();
359 
360   View* selected_tab = GetSelectedTab();
361   if (selected_tab) {
362     selected_tab->NotifyAccessibilityEvent(
363         ui::AccessibilityTypes::EVENT_FOCUS, true);
364   }
365 }
366 
GetAccessibleState(ui::AccessibleViewState * state)367 void TabbedPane::GetAccessibleState(ui::AccessibleViewState* state) {
368   state->role = ui::AccessibilityTypes::ROLE_PAGETABLIST;
369 }
370 
371 }  // namespace views
372