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.
5 #ifndef CHROME_BROWSER_UI_GTK_BUBBLE_BUBBLE_GTK_H_
6 #define CHROME_BROWSER_UI_GTK_BUBBLE_BUBBLE_GTK_H_
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"
24 class GtkThemeService
;
30 class BubbleDelegateGtk
{
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;
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.
40 virtual ~BubbleDelegateGtk() {}
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
50 class BubbleGtk
: public content::NotificationObserver
{
52 // The style of the frame of the bubble (includes positioning and arrow).
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_|.
66 enum BubbleAttribute
{
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
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
84 static BubbleGtk
* Show(GtkWidget
* anchor_widget
,
85 const gfx::Rect
* rect
,
87 FrameStyle frame_style
,
89 GtkThemeService
* provider
,
90 BubbleDelegateGtk
* delegate
);
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().
96 // Move the window to the new anchor rectangle.
97 void SetPositionRelativeToAnchor(const gfx::Rect
* rect
);
99 // content::NotificationObserver implementation.
100 virtual void Observe(int type
,
101 const content::NotificationSource
& source
,
102 const content::NotificationDetails
& details
) OVERRIDE
;
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();
110 GtkWindow
* GetNativeWindow();
112 GtkWidget
* anchor_widget() { return anchor_widget_
; }
115 FRIEND_TEST_ALL_PREFIXES(BubbleGtkTest
, ArrowLocation
);
116 FRIEND_TEST_ALL_PREFIXES(BubbleGtkTest
, NoArrow
);
123 BubbleGtk(GtkThemeService
* provider
,
124 FrameStyle frame_style
,
125 int attribute_flags
);
126 virtual ~BubbleGtk();
128 // Creates the Bubble.
129 void Init(GtkWidget
* anchor_widget
,
130 const gfx::Rect
* rect
,
132 int attribute_flags
);
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
,
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
,
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
);
158 // Reshapes the window and updates |mask_region_|.
159 void UpdateWindowShape();
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.
166 // Restack the bubble's window directly above |toplevel_window_|.
169 // Sets the delegate.
170 void set_delegate(BubbleDelegateGtk
* delegate
) { delegate_
= delegate
; }
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();
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();
181 CHROMEG_CALLBACK_3(BubbleGtk
, gboolean
, OnGtkAccelerator
, GtkAccelGroup
*,
182 GObject
*, guint
, GdkModifierType
);
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
,
193 CHROMEGTK_CALLBACK_1(BubbleGtk
, gboolean
, OnToplevelUnmap
, GdkEvent
*);
194 CHROMEGTK_CALLBACK_1(BubbleGtk
, void, OnAnchorAllocate
, GtkAllocation
*);
195 CHROMEGTK_CALLBACK_0(BubbleGtk
, void, OnAnchorDestroy
);
197 // The caller supplied delegate, can be NULL.
198 BubbleDelegateGtk
* delegate_
;
200 // Our GtkWindow popup window, we don't technically "own" the widget, since
201 // it deletes us when it is destroyed.
204 // Provides colors and stuff.
205 GtkThemeService
* theme_service_
;
207 // The accel group attached to |window_|, to handle closing with escape.
208 GtkAccelGroup
* accel_group_
;
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_
;
216 // The widget that we use to relatively position the popup window.
217 GtkWidget
* anchor_widget_
;
219 // Provides an offset from |anchor_widget_|'s origin for MoveWindow() to
223 // The current shape of |window_| (used to test whether clicks fall in it or
225 GdkRegion
* mask_region_
;
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_
;
232 // The currently used frame style given screen size and directionality.
233 FrameStyle actual_frame_style_
;
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
238 bool match_system_theme_
;
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.
245 bool closed_by_escape_
;
247 content::NotificationRegistrar registrar_
;
249 ui::GtkSignalRegistrar signals_
;
251 base::WeakPtrFactory
<BubbleGtk
> weak_ptr_factory_
;
253 DISALLOW_COPY_AND_ASSIGN(BubbleGtk
);
256 #endif // CHROME_BROWSER_UI_GTK_BUBBLE_BUBBLE_GTK_H_