Home | History | Annotate | Download | only in bubble
      1 // Copyright (c) 2012 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_GTK_BUBBLE_BUBBLE_GTK_H_
      6 #define CHROME_BROWSER_UI_GTK_BUBBLE_BUBBLE_GTK_H_
      7 
      8 #include <gtk/gtk.h>
      9 
     10 #include <vector>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/gtest_prod_util.h"
     15 #include "content/public/browser/notification_observer.h"
     16 #include "content/public/browser/notification_registrar.h"
     17 #include "ui/base/gtk/gtk_signal.h"
     18 #include "ui/base/gtk/gtk_signal_registrar.h"
     19 #include "ui/gfx/point.h"
     20 #include "ui/gfx/rect.h"
     21 
     22 class BubbleGtk;
     23 class GtkThemeService;
     24 
     25 namespace gfx {
     26 class Rect;
     27 }
     28 
     29 class BubbleDelegateGtk {
     30  public:
     31   // Called when the bubble is closing and is about to be deleted.
     32   // |closed_by_escape| is true if the close is the result of pressing escape.
     33   virtual void BubbleClosing(BubbleGtk* bubble, bool closed_by_escape) = 0;
     34 
     35   // NOTE: The Views interface has CloseOnEscape, except I can't find a place
     36   // where it ever returns false, so we always allow you to close via escape.
     37 
     38  protected:
     39   virtual ~BubbleDelegateGtk() {}
     40 };
     41 
     42 // This is the GTK implementation of Bubbles. Bubbles are like dialogs, but they
     43 // can point to a given element on the screen. You should call BubbleGtk::Show,
     44 // which will create and display a bubble. The object is self deleting, when the
     45 // bubble is closed, you will be notified via
     46 // BubbleDelegateGtk::BubbleClosing(). Then the widgets and the underlying
     47 // object will be destroyed. You can also close and destroy the bubble by
     48 // calling Close().
     49 class BubbleGtk : public content::NotificationObserver {
     50  public:
     51   // The style of the frame of the bubble (includes positioning and arrow).
     52   enum FrameStyle {
     53     ANCHOR_TOP_LEFT,
     54     ANCHOR_TOP_MIDDLE,
     55     ANCHOR_TOP_RIGHT,
     56     ANCHOR_BOTTOM_LEFT,
     57     ANCHOR_BOTTOM_MIDDLE,
     58     ANCHOR_BOTTOM_RIGHT,
     59     FLOAT_BELOW_RECT,  // No arrow. Positioned under the supplied rect.
     60     CENTER_OVER_RECT,  // No arrow. Centered over the supplied rect.
     61     FIXED_TOP_LEFT,  // No arrow. Shown at top left of |toplevel_window_|.
     62     FIXED_TOP_RIGHT,  // No arrow. Shown at top right of |toplevel_window_|.
     63   };
     64 
     65   enum BubbleAttribute {
     66     NONE = 0,
     67     MATCH_SYSTEM_THEME = 1 << 0,  // Matches system colors/themes when possible.
     68     POPUP_WINDOW = 1 << 1,  // Displays as popup instead of top-level window.
     69     GRAB_INPUT = 1 << 2,  // Causes bubble to grab keyboard/pointer input.
     70     NO_ACCELERATORS = 1 << 3, // Does not register any of the default bubble
     71                               // accelerators.
     72   };
     73 
     74   // Show a bubble, pointing at the area |rect| (in coordinates relative to
     75   // |anchor_widget|'s origin). A bubble will try to fit on the screen, so it
     76   // can point to any edge of |rect|. If |rect| is NULL, the widget's entire
     77   // area will be used. The bubble will host the |content| widget. Its arrow
     78   // will be drawn according to |frame_style| if possible, and will be
     79   // automatically flipped in RTL locales. The |delegate| will be notified when
     80   // the bubble is closed. The bubble will perform an X grab of the pointer and
     81   // keyboard, and will close itself if a click is received outside of the
     82   // bubble.
     83   static BubbleGtk* Show(GtkWidget* anchor_widget,
     84                          const gfx::Rect* rect,
     85                          GtkWidget* content,
     86                          FrameStyle frame_style,
     87                          int attribute_flags,
     88                          GtkThemeService* provider,
     89                          BubbleDelegateGtk* delegate);
     90 
     91   // Close the bubble if it's open.  This will delete the widgets and object,
     92   // so you shouldn't hold a BubbleGtk pointer after calling Close().
     93   void Close();
     94 
     95   // Move the window to the new anchor rectangle.
     96   void SetPositionRelativeToAnchor(const gfx::Rect* rect);
     97 
     98   // content::NotificationObserver implementation.
     99   virtual void Observe(int type,
    100                        const content::NotificationSource& source,
    101                        const content::NotificationDetails& details) OVERRIDE;
    102 
    103   // Change an input-grabbing bubble into a non-input-grabbing bubble. This
    104   // allows a window to change from auto closing when it loses to focus to being
    105   // a window that does not auto close, and is useful if an auto closing window
    106   // starts being inspected.
    107   void StopGrabbingInput();
    108 
    109   GtkWindow* GetNativeWindow();
    110 
    111   GtkWidget* anchor_widget() { return anchor_widget_; }
    112 
    113  private:
    114   FRIEND_TEST_ALL_PREFIXES(BubbleGtkTest, ArrowLocation);
    115   FRIEND_TEST_ALL_PREFIXES(BubbleGtkTest, NoArrow);
    116 
    117   enum FrameType {
    118     FRAME_MASK,
    119     FRAME_STROKE,
    120   };
    121 
    122   BubbleGtk(GtkThemeService* provider,
    123             FrameStyle frame_style,
    124             int attribute_flags);
    125   virtual ~BubbleGtk();
    126 
    127   // Creates the Bubble.
    128   void Init(GtkWidget* anchor_widget,
    129             const gfx::Rect* rect,
    130             GtkWidget* content,
    131             int attribute_flags);
    132 
    133   // Make the points for our polygon frame, either for fill (the mask), or for
    134   // when we stroke the border.
    135   static std::vector<GdkPoint> MakeFramePolygonPoints(
    136       FrameStyle frame_style,
    137       int width,
    138       int height,
    139       FrameType type);
    140 
    141   // Get the allowed frame style (which is a function of the preferred style and
    142   // of the direction that the bubble should be facing to fit onscreen).
    143   // |arrow_x| (or |arrow_y|) is the X component (or Y component) in screen
    144   // coordinates of the point at which the bubble's arrow should be aimed,
    145   // respectively.  |width| (or |height|) is the bubble's width (or height).
    146   static FrameStyle GetAllowedFrameStyle(FrameStyle preferred_location,
    147                                          int arrow_x,
    148                                          int arrow_y,
    149                                          int width,
    150                                          int height);
    151 
    152   // Updates the frame style based on the toplevel window's current position and
    153   // the bubble's size.  If the |force_move_and_reshape| is true or the location
    154   // changes, moves and reshapes the window and returns true.
    155   bool UpdateFrameStyle(bool force_move_and_reshape);
    156 
    157   // Reshapes the window and updates |mask_region_|.
    158   void UpdateWindowShape();
    159 
    160   // Calculate the current screen position for the bubble's window (per
    161   // |toplevel_window_|'s position as of its most-recent ConfigureNotify event
    162   // and |rect_|) and move it there.
    163   void MoveWindow();
    164 
    165   // Restack the bubble's window directly above |toplevel_window_|.
    166   void StackWindow();
    167 
    168   // Sets the delegate.
    169   void set_delegate(BubbleDelegateGtk* delegate) { delegate_ = delegate; }
    170 
    171   // Grab (in the X sense) the pointer and keyboard.  This is needed to make
    172   // sure that we have the input focus.
    173   void GrabPointerAndKeyboard();
    174 
    175   // Ungrab (in the X sense) the pointer and keyboard.  This is needed to make
    176   // sure that we release the input focus, e.g. when an extension popup
    177   // is inspected by the DevTools.
    178   void UngrabPointerAndKeyboard();
    179 
    180   CHROMEG_CALLBACK_3(BubbleGtk, gboolean, OnGtkAccelerator, GtkAccelGroup*,
    181                      GObject*, guint, GdkModifierType);
    182 
    183   CHROMEGTK_CALLBACK_1(BubbleGtk, gboolean, OnExpose, GdkEventExpose*);
    184   CHROMEGTK_CALLBACK_1(BubbleGtk, void, OnSizeAllocate, GtkAllocation*);
    185   CHROMEGTK_CALLBACK_1(BubbleGtk, gboolean, OnButtonPress, GdkEventButton*);
    186   CHROMEGTK_CALLBACK_0(BubbleGtk, gboolean, OnDestroy);
    187   CHROMEGTK_CALLBACK_0(BubbleGtk, void, OnHide);
    188   CHROMEGTK_CALLBACK_1(BubbleGtk, gboolean, OnGrabBroken, GdkEventGrabBroken*);
    189   CHROMEGTK_CALLBACK_0(BubbleGtk, void, OnForeshadowWidgetHide);
    190   CHROMEGTK_CALLBACK_1(BubbleGtk, gboolean, OnToplevelConfigure,
    191                        GdkEventConfigure*);
    192   CHROMEGTK_CALLBACK_1(BubbleGtk, gboolean, OnToplevelUnmap, GdkEvent*);
    193   CHROMEGTK_CALLBACK_1(BubbleGtk, void, OnAnchorAllocate, GtkAllocation*);
    194   CHROMEGTK_CALLBACK_0(BubbleGtk, void, OnAnchorDestroy);
    195   CHROMEGTK_CALLBACK_0(BubbleGtk, void, OnToplevelDestroy);
    196 
    197   // The caller supplied delegate, can be NULL.
    198   BubbleDelegateGtk* delegate_;
    199 
    200   // Our GtkWindow popup window, we don't technically "own" the widget, since
    201   // it deletes us when it is destroyed.
    202   GtkWidget* window_;
    203 
    204   // Provides colors and stuff.
    205   GtkThemeService* theme_service_;
    206 
    207   // The accel group attached to |window_|, to handle closing with escape.
    208   GtkAccelGroup* accel_group_;
    209 
    210   // The window for which we're being shown (and to which |rect_| is relative).
    211   // Note that it's possible for |toplevel_window_| to be NULL if the
    212   // window is destroyed before this object is destroyed, so it's important
    213   // to check for that case.
    214   GtkWidget* toplevel_window_;
    215 
    216   // The widget that we use to relatively position the popup window.
    217   GtkWidget* anchor_widget_;
    218 
    219   // Provides an offset from |anchor_widget_|'s origin for MoveWindow() to
    220   // use.
    221   gfx::Rect rect_;
    222 
    223   // The current shape of |window_| (used to test whether clicks fall in it or
    224   // not).
    225   GdkRegion* mask_region_;
    226 
    227   // The frame style given to |Show()| that will attempt to be used. It will be
    228   // flipped in RTL. If there's not enough screen space for the given
    229   // FrameStyle, this may be changed and differ from |actual_frame_style_|.
    230   FrameStyle requested_frame_style_;
    231 
    232   // The currently used frame style given screen size and directionality.
    233   FrameStyle actual_frame_style_;
    234 
    235   // Whether the background should match the system theme, when the system theme
    236   // is being used. For example, the bookmark bubble does, but extension popups
    237   // do not.
    238   bool match_system_theme_;
    239 
    240   // If true, the popup owns all X input for the duration of its existence.
    241   // This will usually be true, the exception being when inspecting extension
    242   // popups with dev tools.
    243   bool grab_input_;
    244 
    245   bool closed_by_escape_;
    246 
    247   content::NotificationRegistrar registrar_;
    248 
    249   ui::GtkSignalRegistrar signals_;
    250 
    251   DISALLOW_COPY_AND_ASSIGN(BubbleGtk);
    252 };
    253 
    254 #endif  // CHROME_BROWSER_UI_GTK_BUBBLE_BUBBLE_GTK_H_
    255