summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/location_bar/icon_label_bubble_view.cc
blob: 2df74e60abd39445cb3074ac72476c703c300fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h"

#include "base/utf_string_conversions.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"

namespace {

// Amount of padding at the edges of the bubble.
//
// This can't be statically initialized because
// LocationBarView::GetEdgeItemPadding() depends on whether we are
// using desktop or touch layout, and this in turn depends on the
// command line.
int GetBubbleOuterPadding() {
  return LocationBarView::GetEdgeItemPadding() -
      LocationBarView::kBubbleHorizontalPadding;
}

// Amount of padding after the label.
const int kLabelPadding = 5;

}  // namespace

IconLabelBubbleView::IconLabelBubbleView(const int background_images[],
                                         int contained_image,
                                         SkColor color)
    : background_painter_(background_images),
      is_extension_icon_(false) {
  image_ = new views::ImageView();
  image_->SetImage(
      ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
          contained_image));
  AddChildView(image_);

  label_ = new views::Label();
  label_->SetAutoColorReadabilityEnabled(false);
  label_->SetEnabledColor(color);
  AddChildView(label_);
}

IconLabelBubbleView::~IconLabelBubbleView() {
}

void IconLabelBubbleView::SetFont(const gfx::Font& font) {
  label_->SetFont(font);
}

void IconLabelBubbleView::SetLabel(const string16& label) {
  label_->SetText(label);
}

void IconLabelBubbleView::SetImage(const SkBitmap& bitmap) {
  image_->SetImage(bitmap);
}

void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) {
  background_painter_.Paint(canvas, size());
}

gfx::Size IconLabelBubbleView::GetPreferredSize() {
  gfx::Size size(GetNonLabelSize());
  size.Enlarge(label_->GetPreferredSize().width(), 0);
  return size;
}

void IconLabelBubbleView::Layout() {
  image_->SetBounds(GetBubbleOuterPadding() +
      (is_extension_icon_ ? LocationBarView::kIconInternalPadding : 0), 0,
      image_->GetPreferredSize().width(), height());
  const int label_height = label_->GetPreferredSize().height();
  label_->SetBounds(GetPreLabelWidth(), (height() - label_height) / 2,
                    width() - GetNonLabelWidth(), label_height);
}

void IconLabelBubbleView::SetElideInMiddle(bool elide_in_middle) {
  label_->SetElideInMiddle(elide_in_middle);
}

gfx::Size IconLabelBubbleView::GetNonLabelSize() const {
  return gfx::Size(GetNonLabelWidth(), background_painter_.height());
}

int IconLabelBubbleView::GetPreLabelWidth() const {
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  return GetBubbleOuterPadding() +
      rb.GetBitmapNamed(IDR_OMNIBOX_SEARCH)->width() +
      LocationBarView::GetItemPadding();
}

int IconLabelBubbleView::GetNonLabelWidth() const {
  return GetPreLabelWidth() + GetBubbleOuterPadding();
}