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