summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/validation_message_bubble_delegate.cc
blob: bee71d58c1e93e2fe0399626b5ad370dca847550 (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
102
103
// Copyright (c) 2013 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/validation_message_bubble_delegate.h"

#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/widget/widget.h"

// static
const int ValidationMessageBubbleDelegate::kWindowMinWidth = 64;
// static
const int ValidationMessageBubbleDelegate::kWindowMaxWidth = 256;
static const int kPadding = 0;
static const int kIconTextMargin = 8;
static const int kTextVerticalMargin = 4;

ValidationMessageBubbleDelegate::ValidationMessageBubbleDelegate(
    const gfx::Rect& anchor_in_screen,
    const base::string16& main_text,
    const base::string16& sub_text,
    Observer* observer)
    : observer_(observer), width_(0), height_(0) {
  set_use_focusless(true);
  set_arrow(views::BubbleBorder::TOP_LEFT);
  SetAnchorRect(anchor_in_screen);

  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  views::ImageView* icon = new views::ImageView();
  icon->SetImage(*bundle.GetImageSkiaNamed(IDR_INPUT_ALERT));
  gfx::Size size = icon->GetPreferredSize();
  icon->SetBounds(kPadding, kPadding, size.width(), size.height());
  AddChildView(icon);

  views::Label* label = new views::Label(
      main_text, bundle.GetFontList(ui::ResourceBundle::MediumFont));
  label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  label->set_directionality_mode(views::Label::AUTO_DETECT_DIRECTIONALITY);
  int text_start_x = kPadding + size.width() + kIconTextMargin;
  int min_available = kWindowMinWidth - text_start_x - kPadding;
  int max_available = kWindowMaxWidth - text_start_x - kPadding;
  int label_width = label->GetPreferredSize().width();
  label->SetMultiLine(true);
  AddChildView(label);

  views::Label* sub_label = NULL;
  if (!sub_text.empty()) {
    sub_label = new views::Label(sub_text);
    sub_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
    sub_label->set_directionality_mode(
        views::Label::AUTO_DETECT_DIRECTIONALITY);
    label_width = std::max(label_width, sub_label->GetPreferredSize().width());
    sub_label->SetMultiLine(true);
    AddChildView(sub_label);
  }

  if (label_width < min_available)
    label_width = min_available;
  else if (label_width > max_available)
    label_width = max_available;
  label->SetBounds(text_start_x, kPadding,
                   label_width, label->GetHeightForWidth(label_width));
  int content_bottom = kPadding + label->height();

  if (sub_label) {
    sub_label->SetBounds(text_start_x,
                         content_bottom + kTextVerticalMargin,
                         label_width,
                         sub_label->GetHeightForWidth(label_width));
    content_bottom += kTextVerticalMargin + sub_label->height();
  }

  width_ = text_start_x + label_width + kPadding;
  height_ = content_bottom + kPadding;
}

ValidationMessageBubbleDelegate::~ValidationMessageBubbleDelegate() {}

void ValidationMessageBubbleDelegate::Close() {
  GetWidget()->Close();
  observer_ = NULL;
}

void ValidationMessageBubbleDelegate::SetPositionRelativeToAnchor(
    const gfx::Rect& anchor_in_screen) {
  SetAnchorRect(anchor_in_screen);
}

gfx::Size ValidationMessageBubbleDelegate::GetPreferredSize() {
  return gfx::Size(width_, height_);
}

void ValidationMessageBubbleDelegate::DeleteDelegate() {
  delete this;
}

void ValidationMessageBubbleDelegate::WindowClosing() {
  if (observer_ != NULL)
    observer_->WindowClosing();
}