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 UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_ 6 #define UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_ 7 8 #include "base/gtest_prod_util.h" 9 #include "ui/views/bubble/bubble_border.h" 10 #include "ui/views/widget/widget.h" 11 #include "ui/views/widget/widget_delegate.h" 12 #include "ui/views/widget/widget_observer.h" 13 14 namespace gfx { 15 class FontList; 16 class Rect; 17 } 18 19 namespace views { 20 21 class BubbleFrameView; 22 23 // BubbleDelegateView creates frame and client views for bubble Widgets. 24 // BubbleDelegateView itself is the client's contents view. 25 class VIEWS_EXPORT BubbleDelegateView : public WidgetDelegateView, 26 public WidgetObserver { 27 public: 28 BubbleDelegateView(); 29 BubbleDelegateView(View* anchor_view, BubbleBorder::Arrow arrow); 30 virtual ~BubbleDelegateView(); 31 32 // Create and initialize the bubble Widget(s) with proper bounds. 33 static Widget* CreateBubble(BubbleDelegateView* bubble_delegate); 34 35 // WidgetDelegateView overrides: 36 virtual BubbleDelegateView* AsBubbleDelegate() OVERRIDE; 37 virtual bool CanActivate() const OVERRIDE; 38 virtual bool ShouldShowCloseButton() const OVERRIDE; 39 virtual View* GetContentsView() OVERRIDE; 40 virtual NonClientFrameView* CreateNonClientFrameView(Widget* widget) OVERRIDE; 41 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; 42 43 // WidgetObserver overrides: 44 virtual void OnWidgetDestroying(Widget* widget) OVERRIDE; 45 virtual void OnWidgetVisibilityChanging(Widget* widget, bool visible) 46 OVERRIDE; 47 virtual void OnWidgetVisibilityChanged(Widget* widget, bool visible) 48 OVERRIDE; 49 virtual void OnWidgetActivationChanged(Widget* widget, bool active) OVERRIDE; 50 virtual void OnWidgetBoundsChanged(Widget* widget, 51 const gfx::Rect& new_bounds) OVERRIDE; 52 close_on_esc()53 bool close_on_esc() const { return close_on_esc_; } set_close_on_esc(bool close_on_esc)54 void set_close_on_esc(bool close_on_esc) { close_on_esc_ = close_on_esc; } 55 close_on_deactivate()56 bool close_on_deactivate() const { return close_on_deactivate_; } set_close_on_deactivate(bool close)57 void set_close_on_deactivate(bool close) { close_on_deactivate_ = close; } 58 59 View* GetAnchorView() const; anchor_widget()60 Widget* anchor_widget() const { return anchor_widget_; } 61 62 // The anchor rect is used in the absence of an assigned anchor view. anchor_rect()63 const gfx::Rect& anchor_rect() const { return anchor_rect_; } 64 arrow()65 BubbleBorder::Arrow arrow() const { return arrow_; } set_arrow(BubbleBorder::Arrow arrow)66 void set_arrow(BubbleBorder::Arrow arrow) { arrow_ = arrow; } 67 shadow()68 BubbleBorder::Shadow shadow() const { return shadow_; } set_shadow(BubbleBorder::Shadow shadow)69 void set_shadow(BubbleBorder::Shadow shadow) { shadow_ = shadow; } 70 color()71 SkColor color() const { return color_; } set_color(SkColor color)72 void set_color(SkColor color) { 73 color_ = color; 74 color_explicitly_set_ = true; 75 } 76 margins()77 const gfx::Insets& margins() const { return margins_; } set_margins(const gfx::Insets & margins)78 void set_margins(const gfx::Insets& margins) { margins_ = margins; } 79 anchor_view_insets()80 const gfx::Insets& anchor_view_insets() const { return anchor_view_insets_; } set_anchor_view_insets(const gfx::Insets & i)81 void set_anchor_view_insets(const gfx::Insets& i) { anchor_view_insets_ = i; } 82 parent_window()83 gfx::NativeView parent_window() const { return parent_window_; } set_parent_window(gfx::NativeView window)84 void set_parent_window(gfx::NativeView window) { parent_window_ = window; } 85 use_focusless()86 bool use_focusless() const { return use_focusless_; } set_use_focusless(bool focusless)87 void set_use_focusless(bool focusless) { use_focusless_ = focusless; } 88 accept_events()89 bool accept_events() const { return accept_events_; } set_accept_events(bool accept_events)90 void set_accept_events(bool accept_events) { accept_events_ = accept_events; } 91 border_accepts_events()92 bool border_accepts_events() const { return border_accepts_events_; } set_border_accepts_events(bool event)93 void set_border_accepts_events(bool event) { border_accepts_events_ = event; } 94 adjust_if_offscreen()95 bool adjust_if_offscreen() const { return adjust_if_offscreen_; } set_adjust_if_offscreen(bool adjust)96 void set_adjust_if_offscreen(bool adjust) { adjust_if_offscreen_ = adjust; } 97 98 // Get the arrow's anchor rect in screen space. 99 virtual gfx::Rect GetAnchorRect() const; 100 101 // Allows delegates to provide custom parameters before widget initialization. 102 virtual void OnBeforeBubbleWidgetInit(Widget::InitParams* params, 103 Widget* widget) const; 104 105 // Sets the bubble alignment relative to the anchor. This may only be called 106 // after calling CreateBubble. 107 void SetAlignment(BubbleBorder::BubbleAlignment alignment); 108 109 // Sets the bubble arrow paint type. 110 void SetArrowPaintType(BubbleBorder::ArrowPaintType paint_type); 111 112 // Call this method when the anchor bounds have changed to reposition the 113 // bubble. The bubble is automatically repositioned when the anchor view 114 // bounds change as a result of the widget's bounds changing. 115 void OnAnchorBoundsChanged(); 116 117 protected: 118 // Get bubble bounds from the anchor rect and client view's preferred size. 119 virtual gfx::Rect GetBubbleBounds(); 120 121 // Return a FontList to use for the title of the bubble. 122 // (The default is MediumFont). 123 virtual const gfx::FontList& GetTitleFontList() const; 124 125 // View overrides: 126 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; 127 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE; 128 129 // Perform view initialization on the contents for bubble sizing. 130 virtual void Init(); 131 132 // Sets the anchor view or rect and repositions the bubble. Note that if a 133 // valid view gets passed, the anchor rect will get ignored. If the view gets 134 // deleted, but no new view gets set, the last known anchor postion will get 135 // returned. 136 void SetAnchorView(View* anchor_view); 137 void SetAnchorRect(const gfx::Rect& rect); 138 139 // Resize and potentially move the bubble to fit the content's preferred size. 140 void SizeToContents(); 141 142 BubbleFrameView* GetBubbleFrameView() const; 143 144 private: 145 friend class BubbleBorderDelegate; 146 friend class BubbleWindowTargeter; 147 148 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest, CreateDelegate); 149 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest, NonClientHitTest); 150 151 // Update the bubble color from |theme|, unless it was explicitly set. 152 void UpdateColorsFromTheme(const ui::NativeTheme* theme); 153 154 // Handles widget visibility changes. 155 void HandleVisibilityChanged(Widget* widget, bool visible); 156 157 // Flags controlling bubble closure on the escape key and deactivation. 158 bool close_on_esc_; 159 bool close_on_deactivate_; 160 161 // The view and widget to which this bubble is anchored. Since an anchor view 162 // can be deleted without notice, we store it in the ViewStorage and retrieve 163 // it from there. It will make sure that the view is still valid. 164 const int anchor_view_storage_id_; 165 Widget* anchor_widget_; 166 167 // The anchor rect used in the absence of an anchor view. 168 mutable gfx::Rect anchor_rect_; 169 170 // The arrow's location on the bubble. 171 BubbleBorder::Arrow arrow_; 172 173 // Bubble border shadow to use. 174 BubbleBorder::Shadow shadow_; 175 176 // The background color of the bubble; and flag for when it's explicitly set. 177 SkColor color_; 178 bool color_explicitly_set_; 179 180 // The margins between the content and the inside of the border. 181 gfx::Insets margins_; 182 183 // Insets applied to the |anchor_view_| bounds. 184 gfx::Insets anchor_view_insets_; 185 186 // If true, the bubble does not take focus on display; default is false. 187 bool use_focusless_; 188 189 // Specifies whether the bubble (or its border) handles mouse events, etc. 190 bool accept_events_; 191 bool border_accepts_events_; 192 193 // If true (defaults to true), the arrow may be mirrored and moved to fit the 194 // bubble on screen better. It would be a no-op if the bubble has no arrow. 195 bool adjust_if_offscreen_; 196 197 // Parent native window of the bubble. 198 gfx::NativeView parent_window_; 199 200 DISALLOW_COPY_AND_ASSIGN(BubbleDelegateView); 201 }; 202 203 } // namespace views 204 205 #endif // UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_ 206