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 namespace { |
| 17 |
| 18 const int kWindowMinWidth = 64; |
| 19 const int kWindowMaxWidth = 256; |
| 20 const int kIconTextMargin = 8; |
| 21 const int kTextVerticalMargin = 4; |
| 22 |
| 23 } // namespace |
| 24 |
12 ValidationMessageBubbleView::ValidationMessageBubbleView( | 25 ValidationMessageBubbleView::ValidationMessageBubbleView( |
13 content::WebContents* web_contents, | 26 content::WebContents* web_contents, |
14 const gfx::Rect& anchor_in_root_view, | 27 const gfx::Rect& anchor_in_root_view, |
15 const base::string16& main_text, | 28 const base::string16& main_text, |
16 const base::string16& sub_text) { | 29 const base::string16& sub_text) { |
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 icon->SizeToPreferredSize(); |
| 43 AddChildView(icon); |
| 44 |
| 45 views::Label* label = new views::Label( |
| 46 main_text, bundle.GetFontList(ui::ResourceBundle::MediumFont)); |
| 47 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 48 int text_start_x = icon->bounds().right() + kIconTextMargin; |
| 49 int min_available = kWindowMinWidth - text_start_x; |
| 50 int max_available = kWindowMaxWidth - text_start_x; |
| 51 int label_width = label->GetPreferredSize().width(); |
| 52 label->SetMultiLine(true); |
| 53 AddChildView(label); |
| 54 |
| 55 views::Label* sub_label = nullptr; |
| 56 if (!sub_text.empty()) { |
| 57 sub_label = new views::Label(sub_text); |
| 58 sub_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 59 label_width = std::max(label_width, sub_label->GetPreferredSize().width()); |
| 60 sub_label->SetMultiLine(true); |
| 61 AddChildView(sub_label); |
| 62 } |
| 63 |
| 64 label_width = std::min(std::max(label_width, min_available), max_available); |
| 65 label->SetBounds(text_start_x, 0, |
| 66 label_width, label->GetHeightForWidth(label_width)); |
| 67 int content_bottom = label->height(); |
| 68 |
| 69 if (sub_label) { |
| 70 sub_label->SetBounds(text_start_x, |
| 71 content_bottom + kTextVerticalMargin, |
| 72 label_width, |
| 73 sub_label->GetHeightForWidth(label_width)); |
| 74 content_bottom += kTextVerticalMargin + sub_label->height(); |
| 75 } |
| 76 |
| 77 size_ = gfx::Size(text_start_x + label_width, content_bottom); |
| 78 |
| 79 views::BubbleDialogDelegateView::CreateBubble(this)->ShowInactive(); |
25 } | 80 } |
26 | 81 |
27 ValidationMessageBubbleView::~ValidationMessageBubbleView() { | 82 ValidationMessageBubbleView::~ValidationMessageBubbleView() { |
28 if (delegate_) | 83 } |
29 delegate_->Close(); | 84 |
| 85 gfx::Size ValidationMessageBubbleView::GetPreferredSize() const { |
| 86 return size_; |
| 87 } |
| 88 |
| 89 int ValidationMessageBubbleView::GetDialogButtons() const { |
| 90 return ui::DIALOG_BUTTON_NONE; |
30 } | 91 } |
31 | 92 |
32 void ValidationMessageBubbleView::SetPositionRelativeToAnchor( | 93 void ValidationMessageBubbleView::SetPositionRelativeToAnchor( |
33 content::RenderWidgetHost* widget_host, | 94 content::RenderWidgetHost* widget_host, |
34 const gfx::Rect& anchor_in_root_view) { | 95 const gfx::Rect& anchor_in_root_view) { |
35 if (!delegate_) | 96 SetAnchorRect(anchor_in_root_view + |
36 return; | |
37 delegate_->SetPositionRelativeToAnchor( | |
38 anchor_in_root_view + | |
39 widget_host->GetView()->GetViewBounds().origin().OffsetFromOrigin()); | 97 widget_host->GetView()->GetViewBounds().origin().OffsetFromOrigin()); |
40 } | 98 } |
41 | 99 |
42 void ValidationMessageBubbleView::WindowClosing() { | 100 void ValidationMessageBubbleView::CloseValidationMessage() { |
43 delegate_ = NULL; | 101 GetWidget()->Close(); |
44 } | 102 } |
OLD | NEW |