| 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/validation_message_bubble.h" | 5 #include "chrome/browser/ui/views/validation_message_bubble_view.h" |
| 6 | 6 |
| 7 #include "chrome/browser/platform_util.h" | |
| 8 #include "chrome/browser/ui/views/validation_message_bubble_delegate.h" | |
| 9 #include "content/public/browser/render_widget_host.h" | 7 #include "content/public/browser/render_widget_host.h" |
| 10 #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" |
| 11 #include "ui/views/widget/widget.h" | 10 #include "ui/views/widget/widget.h" |
| 12 | 11 |
| 13 namespace { | 12 ValidationMessageBubbleView::ValidationMessageBubbleView( |
| 14 | 13 content::WebContents* web_contents, |
| 15 // A ValidationMessageBubble implementation for Views. | 14 const gfx::Rect& anchor_in_root_view, |
| 16 class ValidationMessageBubbleImpl | |
| 17 : public chrome::ValidationMessageBubble, | |
| 18 public ValidationMessageBubbleDelegate::Observer { | |
| 19 public: | |
| 20 ValidationMessageBubbleImpl(content::RenderWidgetHost* widget_host, | |
| 21 const gfx::Rect& anchor_in_screen, | |
| 22 const base::string16& main_text, | |
| 23 const base::string16& sub_text); | |
| 24 | |
| 25 ~ValidationMessageBubbleImpl() override { | |
| 26 if (delegate_ != NULL) | |
| 27 delegate_->Close(); | |
| 28 } | |
| 29 | |
| 30 void SetPositionRelativeToAnchor( | |
| 31 content::RenderWidgetHost* widget_host, | |
| 32 const gfx::Rect& anchor_in_root_view) override { | |
| 33 if (!delegate_) | |
| 34 return; | |
| 35 delegate_->SetPositionRelativeToAnchor(anchor_in_root_view + | |
| 36 widget_host->GetView()->GetViewBounds().origin().OffsetFromOrigin()); | |
| 37 } | |
| 38 | |
| 39 // ValidationMessageBubbleDelegate::Observer override: | |
| 40 void WindowClosing() override { delegate_ = NULL; } | |
| 41 | |
| 42 private: | |
| 43 ValidationMessageBubbleDelegate* delegate_; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(ValidationMessageBubbleImpl); | |
| 46 }; | |
| 47 | |
| 48 ValidationMessageBubbleImpl::ValidationMessageBubbleImpl( | |
| 49 content::RenderWidgetHost* widget_host, | |
| 50 const gfx::Rect& anchor_in_screen, | |
| 51 const base::string16& main_text, | 15 const base::string16& main_text, |
| 52 const base::string16& sub_text) { | 16 const base::string16& sub_text) { |
| 17 content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView(); |
| 18 const gfx::Rect anchor_in_screen = |
| 19 anchor_in_root_view + rwhv->GetViewBounds().origin().OffsetFromOrigin(); |
| 53 delegate_ = new ValidationMessageBubbleDelegate( | 20 delegate_ = new ValidationMessageBubbleDelegate( |
| 54 anchor_in_screen, main_text, sub_text, this); | 21 anchor_in_screen, main_text, sub_text, this); |
| 55 delegate_->set_parent_window(platform_util::GetTopLevel( | 22 delegate_->set_parent_window(rwhv->GetNativeView()); |
| 56 widget_host->GetView()->GetNativeView())); | |
| 57 views::BubbleDelegateView::CreateBubble(delegate_); | 23 views::BubbleDelegateView::CreateBubble(delegate_); |
| 58 delegate_->GetWidget()->ShowInactive(); | 24 delegate_->GetWidget()->ShowInactive(); |
| 59 } | 25 } |
| 60 | 26 |
| 61 } // namespace | 27 ValidationMessageBubbleView::~ValidationMessageBubbleView() { |
| 62 | 28 if (delegate_) |
| 63 namespace chrome { | 29 delegate_->Close(); |
| 64 | |
| 65 scoped_ptr<ValidationMessageBubble> ValidationMessageBubble::CreateAndShow( | |
| 66 content::RenderWidgetHost* widget_host, | |
| 67 const gfx::Rect& anchor_in_root_view, | |
| 68 const base::string16& main_text, | |
| 69 const base::string16& sub_text) { | |
| 70 const gfx::Rect anchor_in_screen = anchor_in_root_view | |
| 71 + widget_host->GetView()->GetViewBounds().origin().OffsetFromOrigin(); | |
| 72 scoped_ptr<ValidationMessageBubble> bubble(new ValidationMessageBubbleImpl( | |
| 73 widget_host, anchor_in_screen, main_text, sub_text)); | |
| 74 return bubble.Pass(); | |
| 75 } | 30 } |
| 76 | 31 |
| 77 } // namespace chrome | 32 void ValidationMessageBubbleView::SetPositionRelativeToAnchor( |
| 33 content::RenderWidgetHost* widget_host, |
| 34 const gfx::Rect& anchor_in_root_view) { |
| 35 if (!delegate_) |
| 36 return; |
| 37 delegate_->SetPositionRelativeToAnchor( |
| 38 anchor_in_root_view + |
| 39 widget_host->GetView()->GetViewBounds().origin().OffsetFromOrigin()); |
| 40 } |
| 41 |
| 42 void ValidationMessageBubbleView::WindowClosing() { |
| 43 delegate_ = NULL; |
| 44 } |
| OLD | NEW |