summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/location_bar/icon_label_bubble_view.cc
blob: c40acffacf00fc674f0c5d8691fc3fc588b5353a (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
// Copyright (c) 2010 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/views/location_bar/icon_label_bubble_view.h"

#include "app/resource_bundle.h"
#include "chrome/browser/views/location_bar/location_bar_view.h"
#include "gfx/canvas.h"
#include "views/controls/image_view.h"
#include "views/controls/label.h"

// Amount to offset the image.
static const int kImageOffset = 1;

// Amount to offset the label from the image.
static const int kLabelOffset = 3;

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

IconLabelBubbleView::IconLabelBubbleView(const int background_images[],
                                         int contained_image,
                                         const SkColor& color)
    : background_painter_(background_images) {
  image_ = new views::ImageView();
  AddChildView(image_);
  image_->SetImage(
      ResourceBundle::GetSharedInstance().GetBitmapNamed(contained_image));
  label_ = new views::Label();
  AddChildView(label_);
  label_->SetColor(color);
}

IconLabelBubbleView::~IconLabelBubbleView() {
}

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

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

void IconLabelBubbleView::Paint(gfx::Canvas* canvas) {
  int y_offset = (GetParent()->height() - height()) / 2;
  canvas->TranslateInt(0, y_offset);
  background_painter_.Paint(width(), height(), canvas);
  canvas->TranslateInt(0, -y_offset);
}

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

void IconLabelBubbleView::Layout() {
  image_->SetBounds(kImageOffset, 0, image_->GetPreferredSize().width(),
                    height());
  gfx::Size label_size(label_->GetPreferredSize());
  label_->SetBounds(image_->x() + image_->width() + kLabelOffset,
                    (height() - label_size.height()) / 2, label_size.width(),
                    label_size.height());
}

gfx::Size IconLabelBubbleView::GetNonLabelSize() {
  return gfx::Size(kImageOffset + image_->GetPreferredSize().width() +
      kLabelOffset + kLabelPadding, background_painter_.height());
}