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.
5 #include "chrome/browser/ui/validation_message_bubble.h"
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"
10 #include "content/public/browser/render_widget_host_view.h"
11 #include "ui/views/widget/widget.h"
15 // A ValidationMessageBubble implementation for Views.
16 class ValidationMessageBubbleImpl
17 : public chrome::ValidationMessageBubble
,
18 public ValidationMessageBubbleDelegate::Observer
{
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
);
25 ~ValidationMessageBubbleImpl() override
{
26 if (delegate_
!= NULL
)
30 void SetPositionRelativeToAnchor(
31 content::RenderWidgetHost
* widget_host
,
32 const gfx::Rect
& anchor_in_root_view
) override
{
35 delegate_
->SetPositionRelativeToAnchor(anchor_in_root_view
+
36 widget_host
->GetView()->GetViewBounds().origin().OffsetFromOrigin());
39 // ValidationMessageBubbleDelegate::Observer override:
40 void WindowClosing() override
{ delegate_
= NULL
; }
43 ValidationMessageBubbleDelegate
* delegate_
;
45 DISALLOW_COPY_AND_ASSIGN(ValidationMessageBubbleImpl
);
48 ValidationMessageBubbleImpl::ValidationMessageBubbleImpl(
49 content::RenderWidgetHost
* widget_host
,
50 const gfx::Rect
& anchor_in_screen
,
51 const base::string16
& main_text
,
52 const base::string16
& sub_text
) {
53 delegate_
= new ValidationMessageBubbleDelegate(
54 anchor_in_screen
, main_text
, sub_text
, this);
55 delegate_
->set_parent_window(platform_util::GetTopLevel(
56 widget_host
->GetView()->GetNativeView()));
57 views::BubbleDelegateView::CreateBubble(delegate_
);
58 delegate_
->GetWidget()->ShowInactive();
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
));