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