side_tab.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 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 "chrome/browser/ui/views/tabs/side_tab.h"
6
7#include "base/utf_string_conversions.h"
8#include "grit/app_resources.h"
9#include "grit/theme_resources.h"
10#include "ui/base/resource/resource_bundle.h"
11#include "ui/gfx/canvas_skia.h"
12#include "ui/gfx/favicon_size.h"
13#include "ui/gfx/path.h"
14#include "ui/gfx/skia_util.h"
15#include "views/controls/button/image_button.h"
16
17namespace {
18const int kVerticalTabHeight = 27;
19const int kTitleCloseSpacing = 4;
20const SkScalar kRoundRectRadius = 4;
21const SkColor kTabBackgroundColor = SK_ColorWHITE;
22const SkColor kTextColor = SK_ColorBLACK;
23
24// Padding between the edge and the icon.
25const int kIconLeftPadding = 5;
26
27// Location the title starts at.
28const int kTitleX = kIconLeftPadding + kFavIconSize + 5;
29};
30
31////////////////////////////////////////////////////////////////////////////////
32// SideTab, public:
33
34SideTab::SideTab(TabController* controller)
35    : BaseTab(controller) {
36  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
37  close_button()->SetBackground(kTextColor,
38                                rb.GetBitmapNamed(IDR_TAB_CLOSE),
39                                rb.GetBitmapNamed(IDR_TAB_CLOSE_MASK));
40}
41
42SideTab::~SideTab() {
43}
44
45// static
46int SideTab::GetPreferredHeight() {
47  return 27;
48}
49
50////////////////////////////////////////////////////////////////////////////////
51// SideTab, views::View overrides:
52
53void SideTab::Layout() {
54  if (ShouldShowIcon()) {
55    int icon_x = kIconLeftPadding;
56    int icon_y = (height() - kFavIconSize) / 2;
57    int icon_size =
58        !data().favicon.empty() ? data().favicon.width() : kFavIconSize;
59    if (icon_size != kFavIconSize) {
60      icon_x -= (icon_size - kFavIconSize) / 2;
61      icon_y -= (icon_size - kFavIconSize) / 2;
62    }
63    icon_bounds_.SetRect(icon_x, icon_y, icon_size, icon_size);
64  } else {
65    icon_bounds_ = gfx::Rect();
66  }
67
68  gfx::Size ps = close_button()->GetPreferredSize();
69  int close_y = (height() - ps.height()) / 2;
70  close_button()->SetBounds(
71      std::max(0, width() - ps.width() -
72               (GetPreferredHeight() - ps.height()) / 2),
73      close_y,
74      ps.width(),
75      ps.height());
76
77  int title_y = (height() - font_height()) / 2;
78  title_bounds_.SetRect(
79      kTitleX,
80      title_y,
81      std::max(0, close_button()->x() - kTitleCloseSpacing - kTitleX),
82      font_height());
83}
84
85void SideTab::Paint(gfx::Canvas* canvas) {
86  if (ShouldPaintHighlight()) {
87    SkPaint paint;
88    paint.setColor(kTabBackgroundColor);
89    paint.setAntiAlias(true);
90    SkRect border_rect = { SkIntToScalar(0), SkIntToScalar(0),
91                           SkIntToScalar(width()), SkIntToScalar(height()) };
92    canvas->AsCanvasSkia()->drawRoundRect(border_rect,
93                                          SkIntToScalar(kRoundRectRadius),
94                                          SkIntToScalar(kRoundRectRadius),
95                                          paint);
96  }
97
98  if (ShouldShowIcon())
99    PaintIcon(canvas, icon_bounds_.x(), icon_bounds_.y());
100
101  PaintTitle(canvas, kTextColor);
102}
103
104gfx::Size SideTab::GetPreferredSize() {
105  return gfx::Size(0, GetPreferredHeight());
106}
107
108bool SideTab::ShouldPaintHighlight() const {
109  return IsSelected() || !controller();
110}
111
112bool SideTab::ShouldShowIcon() const {
113  return data().mini || data().show_icon;
114}
115