• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/location_bar/icon_label_bubble_view.h"
6 
7 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
8 #include "grit/theme_resources.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/canvas.h"
11 #include "views/controls/image_view.h"
12 #include "views/controls/label.h"
13 
14 // Amount of padding at the edges of the bubble.
15 static const int kBubbleOuterPadding = LocationBarView::kEdgeItemPadding -
16     LocationBarView::kBubbleHorizontalPadding;
17 
18 // Amount of padding after the label.
19 static const int kLabelPadding = 5;
20 
IconLabelBubbleView(const int background_images[],int contained_image,const SkColor & color)21 IconLabelBubbleView::IconLabelBubbleView(const int background_images[],
22                                          int contained_image,
23                                          const SkColor& color)
24     : background_painter_(background_images),
25       is_extension_icon_(false) {
26   image_ = new views::ImageView();
27   AddChildView(image_);
28   image_->SetImage(
29       ResourceBundle::GetSharedInstance().GetBitmapNamed(contained_image));
30   label_ = new views::Label();
31   AddChildView(label_);
32   label_->SetColor(color);
33 }
34 
~IconLabelBubbleView()35 IconLabelBubbleView::~IconLabelBubbleView() {
36 }
37 
SetFont(const gfx::Font & font)38 void IconLabelBubbleView::SetFont(const gfx::Font& font) {
39   label_->SetFont(font);
40 }
41 
SetLabel(const std::wstring & label)42 void IconLabelBubbleView::SetLabel(const std::wstring& label) {
43   label_->SetText(label);
44 }
45 
SetImage(const SkBitmap & bitmap)46 void IconLabelBubbleView::SetImage(const SkBitmap& bitmap) {
47   image_->SetImage(bitmap);
48 }
49 
OnPaint(gfx::Canvas * canvas)50 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) {
51   background_painter_.Paint(width(), height(), canvas);
52 }
53 
GetPreferredSize()54 gfx::Size IconLabelBubbleView::GetPreferredSize() {
55   gfx::Size size(GetNonLabelSize());
56   size.Enlarge(label_->GetPreferredSize().width(), 0);
57   return size;
58 }
59 
Layout()60 void IconLabelBubbleView::Layout() {
61   image_->SetBounds(kBubbleOuterPadding +
62       (is_extension_icon_ ? LocationBarView::kIconInternalPadding : 0), 0,
63       image_->GetPreferredSize().width(), height());
64   const int label_height = label_->GetPreferredSize().height();
65   label_->SetBounds(GetPreLabelWidth(), (height() - label_height) / 2,
66                     width() - GetNonLabelWidth(), label_height);
67 }
68 
SetElideInMiddle(bool elide_in_middle)69 void IconLabelBubbleView::SetElideInMiddle(bool elide_in_middle) {
70   label_->SetElideInMiddle(elide_in_middle);
71 }
72 
GetNonLabelSize() const73 gfx::Size IconLabelBubbleView::GetNonLabelSize() const {
74   return gfx::Size(GetNonLabelWidth(), background_painter_.height());
75 }
76 
GetPreLabelWidth() const77 int IconLabelBubbleView::GetPreLabelWidth() const {
78   return kBubbleOuterPadding + ResourceBundle::GetSharedInstance().
79       GetBitmapNamed(IDR_OMNIBOX_SEARCH)->width() +
80       LocationBarView::kItemPadding;
81 }
82 
GetNonLabelWidth() const83 int IconLabelBubbleView::GetNonLabelWidth() const {
84   return GetPreLabelWidth() + kBubbleOuterPadding;
85 }
86