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