Home | History | Annotate | Download | only in views
      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/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"
     12 
     13 namespace {
     14 
     15 // A ValidationMessageBubble implementation for Views.
     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   virtual ~ValidationMessageBubbleImpl() {
     26     if (delegate_ != NULL)
     27       delegate_->Close();
     28   }
     29 
     30   virtual 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   virtual void WindowClosing() OVERRIDE {
     41     delegate_ = NULL;
     42   }
     43 
     44  private:
     45   ValidationMessageBubbleDelegate* delegate_;
     46 
     47   DISALLOW_COPY_AND_ASSIGN(ValidationMessageBubbleImpl);
     48 };
     49 
     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();
     61 }
     62 
     63 }  // namespace
     64 
     65 namespace chrome {
     66 
     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));
     76   return bubble.Pass();
     77 }
     78 
     79 }  // namespace chrome
     80