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 17 namespace { 18 const int kVerticalTabHeight = 27; 19 const int kTitleCloseSpacing = 4; 20 const SkScalar kRoundRectRadius = 4; 21 const SkColor kTabBackgroundColor = SK_ColorWHITE; 22 const SkColor kTextColor = SK_ColorBLACK; 23 24 // Padding between the edge and the icon. 25 const int kIconLeftPadding = 5; 26 27 // Location the title starts at. 28 const int kTitleX = kIconLeftPadding + kFaviconSize + 5; 29 }; 30 31 //////////////////////////////////////////////////////////////////////////////// 32 // SideTab, public: 33 34 SideTab::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 42 SideTab::~SideTab() { 43 } 44 45 // static 46 int SideTab::GetPreferredHeight() { 47 return 27; 48 } 49 50 //////////////////////////////////////////////////////////////////////////////// 51 // SideTab, views::View overrides: 52 53 void SideTab::Layout() { 54 if (ShouldShowIcon()) { 55 int icon_y = (height() - kFaviconSize) / 2; 56 icon_bounds_.SetRect(kIconLeftPadding, icon_y, kFaviconSize, kFaviconSize); 57 } else { 58 icon_bounds_ = gfx::Rect(); 59 } 60 61 gfx::Size ps = close_button()->GetPreferredSize(); 62 int close_y = (height() - ps.height()) / 2; 63 close_button()->SetBounds( 64 std::max(0, width() - ps.width() - 65 (GetPreferredHeight() - ps.height()) / 2), 66 close_y, 67 ps.width(), 68 ps.height()); 69 70 int title_y = (height() - font_height()) / 2; 71 title_bounds_.SetRect( 72 kTitleX, 73 title_y, 74 std::max(0, close_button()->x() - kTitleCloseSpacing - kTitleX), 75 font_height()); 76 } 77 78 void SideTab::OnPaint(gfx::Canvas* canvas) { 79 // TODO: should render the active tab differently. 80 if (ShouldPaintHighlight()) { 81 SkPaint paint; 82 paint.setColor(kTabBackgroundColor); 83 paint.setAntiAlias(true); 84 SkRect border_rect = { SkIntToScalar(0), SkIntToScalar(0), 85 SkIntToScalar(width()), SkIntToScalar(height()) }; 86 canvas->AsCanvasSkia()->drawRoundRect(border_rect, 87 SkIntToScalar(kRoundRectRadius), 88 SkIntToScalar(kRoundRectRadius), 89 paint); 90 } 91 92 if (ShouldShowIcon()) 93 PaintIcon(canvas); 94 95 PaintTitle(canvas, kTextColor); 96 } 97 98 gfx::Size SideTab::GetPreferredSize() { 99 return gfx::Size(0, GetPreferredHeight()); 100 } 101 102 const gfx::Rect& SideTab::GetTitleBounds() const { 103 return title_bounds_; 104 } 105 106 const gfx::Rect& SideTab::GetIconBounds() const { 107 return icon_bounds_; 108 } 109 110 bool SideTab::ShouldPaintHighlight() const { 111 return IsSelected() || !controller(); 112 } 113 114 bool SideTab::ShouldShowIcon() const { 115 return data().mini || data().show_icon; 116 } 117