• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/memory/weak_ptr.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "ui/base/gtk/gtk_signal.h"
19 #include "ui/base/gtk/gtk_signal_registrar.h"
20 #include "ui/gfx/point.h"
21 #include "ui/gfx/rect.h"
22 
23 class BubbleGtk;
24 class GtkThemeService;
25 
26 namespace gfx {
27 class Rect;
28 }
29 
30 class BubbleDelegateGtk {
31  public:
32   // Called when the bubble is closing and is about to be deleted.
33   // |closed_by_escape| is true if the close is the result of pressing escape.
34   virtual void BubbleClosing(BubbleGtk* bubble, bool closed_by_escape) = 0;
35 
36   // NOTE: The Views interface has CloseOnEscape, except I can't find a place
37   // where it ever returns false, so we always allow you to close via escape.
38 
39  protected:
~BubbleDelegateGtk()40   virtual ~BubbleDelegateGtk() {}
41 };
42 
43 // This is the GTK implementation of Bubbles. Bubbles are like dialogs, but they
44 // can point to a given element on the screen. You should call BubbleGtk::Show,
45 // which will create and display a bubble. The object is self deleting, when the
46 // bubble is closed, you will be notified via
47 // BubbleDelegateGtk::BubbleClosing(). Then the widgets and the underlying
48 // object will be destroyed. You can also close and destroy the bubble by
49 // calling Close().
50 class BubbleGtk : public content::NotificationObserver {
51  public:
52   // The style of the frame of the bubble (includes positioning and arrow).
53   enum FrameStyle {
54     ANCHOR_TOP_LEFT,
55     ANCHOR_TOP_MIDDLE,
56     ANCHOR_TOP_RIGHT,
57     ANCHOR_BOTTOM_LEFT,
58     ANCHOR_BOTTOM_MIDDLE,
59     ANCHOR_BOTTOM_RIGHT,
60     FLOAT_BELOW_RECT,  // No arrow. Positioned under the supplied rect.
61     CENTER_OVER_RECT,  // No arrow. Centered over the supplied rect.
62     FIXED_TOP_LEFT,  // No arrow. Shown at top left of |toplevel_window_|.
63     FIXED_TOP_RIGHT,  // No arrow. Shown at top right of |toplevel_window_|.
64   };
65 
66   enum BubbleAttribute {
67     NONE = 0,
68     MATCH_SYSTEM_THEME = 1 << 0,  // Matches system colors/themes when possible.
69     POPUP_WINDOW = 1 << 1,  // Displays as popup instead of top-level window.
70     GRAB_INPUT = 1 << 2,  // Causes bubble to grab keyboard/pointer input.
71     NO_ACCELERATORS = 1 << 3, // Does not register any of the default bubble
72                               // accelerators.
73   };
74 
75   // Show a bubble, pointing at the area |rect| (in coordinates relative to
76   // |anchor_widget|'s origin). A bubble will try to fit on the screen, so it
77   // can point to any edge of |rect|. If |rect| is NULL, the widget's entire
78   // area will be used. The bubble will host the |content| widget. Its arrow
79   // will be drawn according to |frame_style| if possible, and will be
80   // automatically flipped in RTL locales. The |delegate| will be notified when
81   // the bubble is closed. The bubble will perform an X grab of the pointer and
82   // keyboard, and will close itself if a click is received outside of the
83   // bubble.
84   static BubbleGtk* Show(GtkWidget* anchor_widget,
85                          const gfx::Rect* rect,
86                          GtkWidget* content,
87                          FrameStyle frame_style,
88                          int attribute_flags,
89                          GtkThemeService* provider,
90                          BubbleDelegateGtk* delegate);
91 
92   // Close the bubble if it's open.  This will delete the widgets and object,
93   // so you shouldn't hold a BubbleGtk pointer after calling Close().
94   void Close();
95 
96   // Move the window to the new anchor rectangle.
97   void SetPositionRelativeToAnchor(const gfx::Rect* rect);
98 
99   // content::NotificationObserver implementation.
100   virtual void Observe(int type,
101                        const content::NotificationSource& source,
102                        const content::NotificationDetails& details) OVERRIDE;
103 
104   // Change an input-grabbing bubble into a non-input-grabbing bubble. This
105   // allows a window to change from auto closing when it loses to focus to being
106   // a window that does not auto close, and is useful if an auto closing window
107   // starts being inspected.
108   void StopGrabbingInput();
109 
110   GtkWindow* GetNativeWindow();
111 
anchor_widget()112   GtkWidget* anchor_widget() { return anchor_widget_; }
113 
114  private:
115   FRIEND_TEST_ALL_PREFIXES(BubbleGtkTest, ArrowLocation);
116   FRIEND_TEST_ALL_PREFIXES(BubbleGtkTest, NoArrow);
117 
118   enum FrameType {
119     FRAME_MASK,
120     FRAME_STROKE,
121   };
122 
123   BubbleGtk(GtkThemeService* provider,
124             FrameStyle frame_style,
125             int attribute_flags);
126   virtual ~BubbleGtk();
127 
128   // Creates the Bubble.
129   void Init(GtkWidget* anchor_widget,
130             const gfx::Rect* rect,
131             GtkWidget* content,
132             int attribute_flags);
133 
134   // Make the points for our polygon frame, either for fill (the mask), or for
135   // when we stroke the border.
136   static std::vector<GdkPoint> MakeFramePolygonPoints(
137       FrameStyle frame_style,
138       int width,
139       int height,
140       FrameType type);
141 
142   // Get the allowed frame style (which is a function of the preferred style and
143   // of the direction that the bubble should be facing to fit onscreen).
144   // |arrow_x| (or |arrow_y|) is the X component (or Y component) in screen
145   // coordinates of the point at which the bubble's arrow should be aimed,
146   // respectively.  |width| (or |height|) is the bubble's width (or height).
147   static FrameStyle GetAllowedFrameStyle(FrameStyle preferred_location,
148                                          int arrow_x,
149                                          int arrow_y,
150                                          int width,
151                                          int height);
152 
153   // Updates the frame style based on the toplevel window's current position and
154   // the bubble's size.  If the |force_move_and_reshape| is true or the location
155   // changes, moves and reshapes the window and returns true.
156   bool UpdateFrameStyle(bool force_move_and_reshape);
157 
158   // Reshapes the window and updates |mask_region_|.
159   void UpdateWindowShape();
160 
161   // Calculate the current screen position for the bubble's window (per
162   // |toplevel_window_|'s position as of its most-recent ConfigureNotify event
163   // and |rect_|) and move it there.
164   void MoveWindow();
165 
166   // Restack the bubble's window directly above |toplevel_window_|.
167   void StackWindow();
168 
169   // Sets the delegate.
set_delegate(BubbleDelegateGtk * delegate)170   void set_delegate(BubbleDelegateGtk* delegate) { delegate_ = delegate; }
171 
172   // Grab (in the X sense) the pointer and keyboard.  This is needed to make
173   // sure that we have the input focus.
174   void GrabPointerAndKeyboard();
175 
176   // Ungrab (in the X sense) the pointer and keyboard.  This is needed to make
177   // sure that we release the input focus, e.g. when an extension popup
178   // is inspected by the DevTools.
179   void UngrabPointerAndKeyboard();
180 
181   CHROMEG_CALLBACK_3(BubbleGtk, gboolean, OnGtkAccelerator, GtkAccelGroup*,
182                      GObject*, guint, GdkModifierType);
183 
184   CHROMEGTK_CALLBACK_1(BubbleGtk, gboolean, OnExpose, GdkEventExpose*);
185   CHROMEGTK_CALLBACK_1(BubbleGtk, void, OnSizeAllocate, GtkAllocation*);
186   CHROMEGTK_CALLBACK_1(BubbleGtk, gboolean, OnButtonPress, GdkEventButton*);
187   CHROMEGTK_CALLBACK_0(BubbleGtk, gboolean, OnDestroy);
188   CHROMEGTK_CALLBACK_0(BubbleGtk, void, OnHide);
189   CHROMEGTK_CALLBACK_1(BubbleGtk, gboolean, OnGrabBroken, GdkEventGrabBroken*);
190   CHROMEGTK_CALLBACK_0(BubbleGtk, void, OnForeshadowWidgetHide);
191   CHROMEGTK_CALLBACK_1(BubbleGtk, gboolean, OnToplevelConfigure,
192                        GdkEventConfigure*);
193   CHROMEGTK_CALLBACK_1(BubbleGtk, gboolean, OnToplevelUnmap, GdkEvent*);
194   CHROMEGTK_CALLBACK_1(BubbleGtk, void, OnAnchorAllocate, GtkAllocation*);
195   CHROMEGTK_CALLBACK_0(BubbleGtk, void, OnAnchorDestroy);
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   base::WeakPtrFactory<BubbleGtk> weak_ptr_factory_;
252 
253   DISALLOW_COPY_AND_ASSIGN(BubbleGtk);
254 };
255 
256 #endif  // CHROME_BROWSER_UI_GTK_BUBBLE_BUBBLE_GTK_H_
257