validation_message_bubble_delegate.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright (c) 2013 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/validation_message_bubble_delegate.h"
6
7#include "grit/theme_resources.h"
8#include "ui/base/resource/resource_bundle.h"
9#include "ui/views/controls/image_view.h"
10#include "ui/views/controls/label.h"
11#include "ui/views/widget/widget.h"
12
13// static
14const int ValidationMessageBubbleDelegate::kWindowMinWidth = 64;
15// static
16const int ValidationMessageBubbleDelegate::kWindowMaxWidth = 256;
17static const int kPadding = 0;
18static const int kIconTextMargin = 8;
19static const int kTextVerticalMargin = 4;
20
21ValidationMessageBubbleDelegate::ValidationMessageBubbleDelegate(
22    const gfx::Rect& anchor_in_screen,
23    const base::string16& main_text,
24    const base::string16& sub_text,
25    Observer* observer)
26    : observer_(observer), width_(0), height_(0) {
27  set_can_activate(false);
28  set_arrow(views::BubbleBorder::TOP_LEFT);
29  SetAnchorRect(anchor_in_screen);
30
31  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
32  views::ImageView* icon = new views::ImageView();
33  icon->SetImage(*bundle.GetImageSkiaNamed(IDR_INPUT_ALERT));
34  gfx::Size size = icon->GetPreferredSize();
35  icon->SetBounds(kPadding, kPadding, size.width(), size.height());
36  AddChildView(icon);
37
38  views::Label* label = new views::Label(
39      main_text, bundle.GetFontList(ui::ResourceBundle::MediumFont));
40  label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
41  int text_start_x = kPadding + size.width() + kIconTextMargin;
42  int min_available = kWindowMinWidth - text_start_x - kPadding;
43  int max_available = kWindowMaxWidth - text_start_x - kPadding;
44  int label_width = label->GetPreferredSize().width();
45  label->SetMultiLine(true);
46  AddChildView(label);
47
48  views::Label* sub_label = NULL;
49  if (!sub_text.empty()) {
50    sub_label = new views::Label(sub_text);
51    sub_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
52    label_width = std::max(label_width, sub_label->GetPreferredSize().width());
53    sub_label->SetMultiLine(true);
54    AddChildView(sub_label);
55  }
56
57  if (label_width < min_available)
58    label_width = min_available;
59  else if (label_width > max_available)
60    label_width = max_available;
61  label->SetBounds(text_start_x, kPadding,
62                   label_width, label->GetHeightForWidth(label_width));
63  int content_bottom = kPadding + label->height();
64
65  if (sub_label) {
66    sub_label->SetBounds(text_start_x,
67                         content_bottom + kTextVerticalMargin,
68                         label_width,
69                         sub_label->GetHeightForWidth(label_width));
70    content_bottom += kTextVerticalMargin + sub_label->height();
71  }
72
73  width_ = text_start_x + label_width + kPadding;
74  height_ = content_bottom + kPadding;
75}
76
77ValidationMessageBubbleDelegate::~ValidationMessageBubbleDelegate() {}
78
79void ValidationMessageBubbleDelegate::Close() {
80  GetWidget()->Close();
81  observer_ = NULL;
82}
83
84void ValidationMessageBubbleDelegate::SetPositionRelativeToAnchor(
85    const gfx::Rect& anchor_in_screen) {
86  SetAnchorRect(anchor_in_screen);
87}
88
89gfx::Size ValidationMessageBubbleDelegate::GetPreferredSize() const {
90  return gfx::Size(width_, height_);
91}
92
93void ValidationMessageBubbleDelegate::DeleteDelegate() {
94  delete this;
95}
96
97void ValidationMessageBubbleDelegate::WindowClosing() {
98  if (observer_ != NULL)
99    observer_->WindowClosing();
100}
101