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 virtual ~ValidationMessageBubbleImpl() {
26 if (delegate_
!= NULL
)
30 virtual 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 virtual void WindowClosing() OVERRIDE
{
45 ValidationMessageBubbleDelegate
* delegate_
;
47 DISALLOW_COPY_AND_ASSIGN(ValidationMessageBubbleImpl
);
50 ValidationMessageBubbleImpl::ValidationMessageBubbleImpl(
51 content::RenderWidgetHost
* widget_host
,
52 const gfx::Rect
& anchor_in_screen
,
53 const base::string16
& main_text
,
54 const base::string16
& sub_text
) {
55 delegate_
= new ValidationMessageBubbleDelegate(
56 anchor_in_screen
, main_text
, sub_text
, this);
57 delegate_
->set_parent_window(platform_util::GetTopLevel(
58 widget_host
->GetView()->GetNativeView()));
59 views::BubbleDelegateView::CreateBubble(delegate_
);
60 delegate_
->GetWidget()->ShowInactive();
67 scoped_ptr
<ValidationMessageBubble
> ValidationMessageBubble::CreateAndShow(
68 content::RenderWidgetHost
* widget_host
,
69 const gfx::Rect
& anchor_in_root_view
,
70 const base::string16
& main_text
,
71 const base::string16
& sub_text
) {
72 const gfx::Rect anchor_in_screen
= anchor_in_root_view
73 + widget_host
->GetView()->GetViewBounds().origin().OffsetFromOrigin();
74 scoped_ptr
<ValidationMessageBubble
> bubble(new ValidationMessageBubbleImpl(
75 widget_host
, anchor_in_screen
, main_text
, sub_text
));