172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
24a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
34a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// found in the LICENSE file.
44a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
54a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#ifndef CHROME_BROWSER_UI_VIEWS_BROWSER_BUBBLE_H_
64a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#define CHROME_BROWSER_UI_VIEWS_BROWSER_BUBBLE_H_
74a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#pragma once
84a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
9ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/ui/views/bubble/bubble_border.h"
104a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "views/view.h"
114a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "views/widget/widget.h"
124a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
134a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdochclass BrowserBubbleHost;
144a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
154a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// A class for creating a floating window that is "attached" to a particular
164a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// Browser.  If you don't install a delegate, the bubble will hide
174a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// automatically when the browser moves.  The bubble is only shown manually.
184a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// Users are expected to delete the bubble when finished with it.
194a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// Class assumes that RTL related mirroring is done by the view.
204a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdochclass BrowserBubble {
214a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch public:
224a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Delegate to browser bubble events.
234a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  class Delegate {
244a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch   public:
254a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    // Called when the Browser Window that this bubble is attached to moves.
264a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    virtual void BubbleBrowserWindowMoved(BrowserBubble* bubble) {}
274a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
284a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    // Called with the Browser Window that this bubble is attached to is
294a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    // about to close.
304a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    virtual void BubbleBrowserWindowClosing(BrowserBubble* bubble) {}
314a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
324a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    // Called when the bubble became active / got focus.
334a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    virtual void BubbleGotFocus(BrowserBubble* bubble) {}
344a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
354a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    // Called when the bubble became inactive / lost focus.
364a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    // |lost_focus_to_child| is true when a child window became active.
374a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    virtual void BubbleLostFocus(BrowserBubble* bubble,
384a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch                                 bool lost_focus_to_child) {}
394a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  };
404a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
41ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Note that the bubble will size itself to the preferred size of |view| plus
42ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // insets of bubble border. |view| is the embedded view, |frame| is widget
43ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // that the bubble is being positioned relative to, |relative_to| is the
44ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // location that the bubble is showing relative to in screen coordinates,
45ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // e.g. if the buuble is showing for a toolbar button, |relative_to| usually
46ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // would be the bounds of the toolbar button in screen coordiates,
47ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // |arrow_location| is the location where the arrow should on the bubble.
48ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  BrowserBubble(views::View* view,
49ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                views::Widget* frame,
50ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                const gfx::Rect& relative_to,
51ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                BubbleBorder::ArrowLocation arrow_location);
524a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  virtual ~BrowserBubble();
534a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
544a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Call manually if you need to detach the bubble from tracking the browser's
554a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // position.  Note that you must call this manually before deleting this
564a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // object since it can't be safely called from the destructor.
574a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  void DetachFromBrowser();
584a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
594a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Normally called automatically during construction, but if DetachFromBrowser
604a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // has been called manually, then this call will reattach.
614a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  void AttachToBrowser();
624a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  bool attached() const { return attached_; }
634a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
644a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Get/Set the delegate.
654a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  Delegate* delegate() const { return delegate_; }
664a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  void set_delegate(Delegate* del) { delegate_ = del; }
674a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
684a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Notifications from BrowserBubbleHost.
694a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // With no delegate, both of these default to Hiding the bubble.
704a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  virtual void BrowserWindowMoved();
714a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  virtual void BrowserWindowClosing();
724a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
734a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Show or hide the bubble.
744a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  virtual void Show(bool activate);
754a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  virtual void Hide();
764a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  bool visible() const { return visible_; }
774a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
784a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // The contained view.
794a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  views::View* view() const { return view_; }
804a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
814a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Set the bounds of the bubble relative to the browser window.
824a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  void SetBounds(int x, int y, int w, int h);
834a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  void MoveTo(int x, int y);
844a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  int width() { return bounds_.width(); }
854a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  int height() { return bounds_.height(); }
864a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  const gfx::Rect& bounds() const { return bounds_; }
874a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
884a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Reposition the bubble - as we are using a WS_POPUP for the bubble,
894a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // we have to manually position it when the browser window moves.
904a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  void Reposition();
914a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
924a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Resize the bubble to fit the view.
934a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  void ResizeToView();
944a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
954a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Returns the NativeView containing that popup.
964a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  gfx::NativeView native_view() const { return popup_->GetNativeView(); }
974a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
984a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch protected:
994a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Create the popup widget.
1004a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  virtual void InitPopup();
1014a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
102ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Get |relative_to_| rect in screen coordinates.
103ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  gfx::Rect GetAbsoluteRelativeTo();
104ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
105ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Set bounds using screen coordinates.
106ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void SetAbsoluteBounds(const gfx::Rect& window_bounds);
107ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
1084a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Move the popup to an absolute position.
1094a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  void MovePopup(int x, int y, int w, int h);
1104a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
1114a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // The widget that this bubble is in.
1124a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  views::Widget* popup_;
1134a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
1144a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // The frame that this bubble is attached to.
1154a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  views::Widget* frame_;
1164a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
1174a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch private:
1184a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // The view that is displayed in this bubble.
1194a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  views::View* view_;
1204a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
121ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Anchor rect that this bubble is shown relative to, in frame coordinates.
122ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  gfx::Rect relative_to_;
123ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
124ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Arrow location of this bubble.
125ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  BubbleBorder::ArrowLocation arrow_location_;
126ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
1274a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // The bounds relative to the frame.
1284a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  gfx::Rect bounds_;
1294a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
1304a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Current visibility.
1314a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  bool visible_;
1324a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
1334a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // The delegate isn't owned by the bubble.
1344a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  Delegate* delegate_;
1354a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
1364a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Is the bubble attached to a Browser window.
1374a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  bool attached_;
1384a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
1394a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Non-owning pointer to the host of this bubble.
1404a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  BrowserBubbleHost* bubble_host_;
1414a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
1424a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(BrowserBubble);
1434a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch};
1444a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
1454a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#endif  // CHROME_BROWSER_UI_VIEWS_BROWSER_BUBBLE_H_
146