OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/validation_message_bubble_view.h" | 5 #include "chrome/browser/ui/views/validation_message_bubble_view.h" |
6 | 6 |
7 #include "content/public/browser/render_widget_host.h" | 7 #include "content/public/browser/render_widget_host.h" |
8 #include "content/public/browser/render_widget_host_view.h" | 8 #include "content/public/browser/render_widget_host_view.h" |
9 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
| 10 #include "grit/theme_resources.h" |
| 11 #include "ui/base/resource/resource_bundle.h" |
| 12 #include "ui/views/controls/image_view.h" |
| 13 #include "ui/views/controls/label.h" |
10 #include "ui/views/widget/widget.h" | 14 #include "ui/views/widget/widget.h" |
11 | 15 |
| 16 // static |
| 17 const int ValidationMessageBubbleView::kWindowMinWidth = 64; |
| 18 // static |
| 19 const int ValidationMessageBubbleView::kWindowMaxWidth = 256; |
| 20 static const int kPadding = 0; |
| 21 static const int kIconTextMargin = 8; |
| 22 static const int kTextVerticalMargin = 4; |
| 23 |
12 ValidationMessageBubbleView::ValidationMessageBubbleView( | 24 ValidationMessageBubbleView::ValidationMessageBubbleView( |
13 content::WebContents* web_contents, | 25 content::WebContents* web_contents, |
14 const gfx::Rect& anchor_in_root_view, | 26 const gfx::Rect& anchor_in_root_view, |
15 const base::string16& main_text, | 27 const base::string16& main_text, |
16 const base::string16& sub_text) { | 28 const base::string16& sub_text) |
| 29 : width_(0), height_(0) { |
17 content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView(); | 30 content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView(); |
| 31 set_parent_window(rwhv->GetNativeView()); |
| 32 |
| 33 set_can_activate(false); |
| 34 set_arrow(views::BubbleBorder::TOP_LEFT); |
18 const gfx::Rect anchor_in_screen = | 35 const gfx::Rect anchor_in_screen = |
19 anchor_in_root_view + rwhv->GetViewBounds().origin().OffsetFromOrigin(); | 36 anchor_in_root_view + rwhv->GetViewBounds().origin().OffsetFromOrigin(); |
20 delegate_ = new ValidationMessageBubbleDelegate( | 37 SetAnchorRect(anchor_in_screen); |
21 anchor_in_screen, main_text, sub_text, this); | 38 |
22 delegate_->set_parent_window(rwhv->GetNativeView()); | 39 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
23 views::BubbleDelegateView::CreateBubble(delegate_); | 40 views::ImageView* icon = new views::ImageView(); |
24 delegate_->GetWidget()->ShowInactive(); | 41 icon->SetImage(*bundle.GetImageSkiaNamed(IDR_INPUT_ALERT)); |
| 42 gfx::Size size = icon->GetPreferredSize(); |
| 43 icon->SetBounds(kPadding, kPadding, size.width(), size.height()); |
| 44 AddChildView(icon); |
| 45 |
| 46 views::Label* label = new views::Label( |
| 47 main_text, bundle.GetFontList(ui::ResourceBundle::MediumFont)); |
| 48 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 49 int text_start_x = kPadding + size.width() + kIconTextMargin; |
| 50 int min_available = kWindowMinWidth - text_start_x - kPadding; |
| 51 int max_available = kWindowMaxWidth - text_start_x - kPadding; |
| 52 int label_width = label->GetPreferredSize().width(); |
| 53 label->SetMultiLine(true); |
| 54 AddChildView(label); |
| 55 |
| 56 views::Label* sub_label = NULL; |
| 57 if (!sub_text.empty()) { |
| 58 sub_label = new views::Label(sub_text); |
| 59 sub_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 60 label_width = std::max(label_width, sub_label->GetPreferredSize().width()); |
| 61 sub_label->SetMultiLine(true); |
| 62 AddChildView(sub_label); |
| 63 } |
| 64 |
| 65 label_width = std::min(std::max(label_width, min_available), max_available); |
| 66 label->SetBounds(text_start_x, kPadding, |
| 67 label_width, label->GetHeightForWidth(label_width)); |
| 68 int content_bottom = kPadding + label->height(); |
| 69 |
| 70 if (sub_label) { |
| 71 sub_label->SetBounds(text_start_x, |
| 72 content_bottom + kTextVerticalMargin, |
| 73 label_width, |
| 74 sub_label->GetHeightForWidth(label_width)); |
| 75 content_bottom += kTextVerticalMargin + sub_label->height(); |
| 76 } |
| 77 |
| 78 width_ = text_start_x + label_width + kPadding; |
| 79 height_ = content_bottom + kPadding; |
| 80 |
| 81 views::BubbleDialogDelegateView::CreateBubble(this)->ShowInactive(); |
25 } | 82 } |
26 | 83 |
27 ValidationMessageBubbleView::~ValidationMessageBubbleView() { | 84 ValidationMessageBubbleView::~ValidationMessageBubbleView() { |
28 if (delegate_) | 85 } |
29 delegate_->Close(); | 86 |
| 87 gfx::Size ValidationMessageBubbleView::GetPreferredSize() const { |
| 88 return gfx::Size(width_, height_); |
| 89 } |
| 90 |
| 91 int ValidationMessageBubbleView::GetDialogButtons() const { |
| 92 return ui::DIALOG_BUTTON_NONE; |
30 } | 93 } |
31 | 94 |
32 void ValidationMessageBubbleView::SetPositionRelativeToAnchor( | 95 void ValidationMessageBubbleView::SetPositionRelativeToAnchor( |
33 content::RenderWidgetHost* widget_host, | 96 content::RenderWidgetHost* widget_host, |
34 const gfx::Rect& anchor_in_root_view) { | 97 const gfx::Rect& anchor_in_root_view) { |
35 if (!delegate_) | 98 SetAnchorRect(anchor_in_root_view + |
36 return; | |
37 delegate_->SetPositionRelativeToAnchor( | |
38 anchor_in_root_view + | |
39 widget_host->GetView()->GetViewBounds().origin().OffsetFromOrigin()); | 99 widget_host->GetView()->GetViewBounds().origin().OffsetFromOrigin()); |
40 } | 100 } |
41 | 101 |
42 void ValidationMessageBubbleView::WindowClosing() { | 102 void ValidationMessageBubbleView::CloseValidationMessage() { |
43 delegate_ = NULL; | 103 GetWidget()->Close(); |
44 } | 104 } |
OLD | NEW |