Home | History | Annotate | Download | only in views
      1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_UI_VIEWS_BROWSER_BUBBLE_H_
      6 #define CHROME_BROWSER_UI_VIEWS_BROWSER_BUBBLE_H_
      7 #pragma once
      8 
      9 #include "chrome/browser/ui/views/bubble/bubble_border.h"
     10 #include "views/view.h"
     11 #include "views/widget/widget.h"
     12 
     13 class BrowserBubbleHost;
     14 
     15 // A class for creating a floating window that is "attached" to a particular
     16 // Browser.  If you don't install a delegate, the bubble will hide
     17 // automatically when the browser moves.  The bubble is only shown manually.
     18 // Users are expected to delete the bubble when finished with it.
     19 // Class assumes that RTL related mirroring is done by the view.
     20 class BrowserBubble {
     21  public:
     22   // Delegate to browser bubble events.
     23   class Delegate {
     24    public:
     25     // Called when the Browser Window that this bubble is attached to moves.
     26     virtual void BubbleBrowserWindowMoved(BrowserBubble* bubble) {}
     27 
     28     // Called with the Browser Window that this bubble is attached to is
     29     // about to close.
     30     virtual void BubbleBrowserWindowClosing(BrowserBubble* bubble) {}
     31 
     32     // Called when the bubble became active / got focus.
     33     virtual void BubbleGotFocus(BrowserBubble* bubble) {}
     34 
     35     // Called when the bubble became inactive / lost focus.
     36     // |lost_focus_to_child| is true when a child window became active.
     37     virtual void BubbleLostFocus(BrowserBubble* bubble,
     38                                  bool lost_focus_to_child) {}
     39   };
     40 
     41   // Note that the bubble will size itself to the preferred size of |view| plus
     42   // insets of bubble border. |view| is the embedded view, |frame| is widget
     43   // that the bubble is being positioned relative to, |relative_to| is the
     44   // location that the bubble is showing relative to in screen coordinates,
     45   // e.g. if the buuble is showing for a toolbar button, |relative_to| usually
     46   // would be the bounds of the toolbar button in screen coordiates,
     47   // |arrow_location| is the location where the arrow should on the bubble.
     48   BrowserBubble(views::View* view,
     49                 views::Widget* frame,
     50                 const gfx::Rect& relative_to,
     51                 BubbleBorder::ArrowLocation arrow_location);
     52   virtual ~BrowserBubble();
     53 
     54   // Call manually if you need to detach the bubble from tracking the browser's
     55   // position.  Note that you must call this manually before deleting this
     56   // object since it can't be safely called from the destructor.
     57   void DetachFromBrowser();
     58 
     59   // Normally called automatically during construction, but if DetachFromBrowser
     60   // has been called manually, then this call will reattach.
     61   void AttachToBrowser();
     62   bool attached() const { return attached_; }
     63 
     64   // Get/Set the delegate.
     65   Delegate* delegate() const { return delegate_; }
     66   void set_delegate(Delegate* del) { delegate_ = del; }
     67 
     68   // Notifications from BrowserBubbleHost.
     69   // With no delegate, both of these default to Hiding the bubble.
     70   virtual void BrowserWindowMoved();
     71   virtual void BrowserWindowClosing();
     72 
     73   // Show or hide the bubble.
     74   virtual void Show(bool activate);
     75   virtual void Hide();
     76   bool visible() const { return visible_; }
     77 
     78   // The contained view.
     79   views::View* view() const { return view_; }
     80 
     81   // Set the bounds of the bubble relative to the browser window.
     82   void SetBounds(int x, int y, int w, int h);
     83   void MoveTo(int x, int y);
     84   int width() { return bounds_.width(); }
     85   int height() { return bounds_.height(); }
     86   const gfx::Rect& bounds() const { return bounds_; }
     87 
     88   // Reposition the bubble - as we are using a WS_POPUP for the bubble,
     89   // we have to manually position it when the browser window moves.
     90   void Reposition();
     91 
     92   // Resize the bubble to fit the view.
     93   void ResizeToView();
     94 
     95   // Returns the NativeView containing that popup.
     96   gfx::NativeView native_view() const { return popup_->GetNativeView(); }
     97 
     98  protected:
     99   // Create the popup widget.
    100   virtual void InitPopup();
    101 
    102   // Get |relative_to_| rect in screen coordinates.
    103   gfx::Rect GetAbsoluteRelativeTo();
    104 
    105   // Set bounds using screen coordinates.
    106   void SetAbsoluteBounds(const gfx::Rect& window_bounds);
    107 
    108   // Move the popup to an absolute position.
    109   void MovePopup(int x, int y, int w, int h);
    110 
    111   // The widget that this bubble is in.
    112   views::Widget* popup_;
    113 
    114   // The frame that this bubble is attached to.
    115   views::Widget* frame_;
    116 
    117  private:
    118   // The view that is displayed in this bubble.
    119   views::View* view_;
    120 
    121   // Anchor rect that this bubble is shown relative to, in frame coordinates.
    122   gfx::Rect relative_to_;
    123 
    124   // Arrow location of this bubble.
    125   BubbleBorder::ArrowLocation arrow_location_;
    126 
    127   // The bounds relative to the frame.
    128   gfx::Rect bounds_;
    129 
    130   // Current visibility.
    131   bool visible_;
    132 
    133   // The delegate isn't owned by the bubble.
    134   Delegate* delegate_;
    135 
    136   // Is the bubble attached to a Browser window.
    137   bool attached_;
    138 
    139   // Non-owning pointer to the host of this bubble.
    140   BrowserBubbleHost* bubble_host_;
    141 
    142   DISALLOW_COPY_AND_ASSIGN(BrowserBubble);
    143 };
    144 
    145 #endif  // CHROME_BROWSER_UI_VIEWS_BROWSER_BUBBLE_H_
    146