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