Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / views / bubble / bubble_delegate.cc
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 #include "ui/views/bubble/bubble_delegate.h"
6
7 #include "ui/accessibility/ax_view_state.h"
8 #include "ui/gfx/color_utils.h"
9 #include "ui/gfx/rect.h"
10 #include "ui/native_theme/native_theme.h"
11 #include "ui/views/bubble/bubble_frame_view.h"
12 #include "ui/views/focus/view_storage.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/widget/widget_observer.h"
15
16 #if defined(OS_WIN)
17 #include "ui/base/win/shell.h"
18 #endif
19
20 // The defaut margin between the content and the inside border, in pixels.
21 static const int kDefaultMargin = 6;
22
23 namespace views {
24
25 namespace {
26
27 // Create a widget to host the bubble.
28 Widget* CreateBubbleWidget(BubbleDelegateView* bubble) {
29   Widget* bubble_widget = new Widget();
30   Widget::InitParams bubble_params(Widget::InitParams::TYPE_BUBBLE);
31   bubble_params.delegate = bubble;
32   bubble_params.opacity = Widget::InitParams::TRANSLUCENT_WINDOW;
33   bubble_params.accept_events = bubble->accept_events();
34   if (bubble->parent_window())
35     bubble_params.parent = bubble->parent_window();
36   else if (bubble->anchor_widget())
37     bubble_params.parent = bubble->anchor_widget()->GetNativeView();
38   bubble_params.can_activate = bubble->CanActivate();
39   bubble->OnBeforeBubbleWidgetInit(&bubble_params, bubble_widget);
40   bubble_widget->Init(bubble_params);
41   return bubble_widget;
42 }
43
44 }  // namespace
45
46 BubbleDelegateView::BubbleDelegateView()
47     : close_on_esc_(true),
48       close_on_deactivate_(true),
49       anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
50       anchor_widget_(NULL),
51       arrow_(BubbleBorder::TOP_LEFT),
52       shadow_(BubbleBorder::SMALL_SHADOW),
53       color_explicitly_set_(false),
54       margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
55       use_focusless_(false),
56       accept_events_(true),
57       border_accepts_events_(true),
58       adjust_if_offscreen_(true),
59       parent_window_(NULL) {
60   AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
61   UpdateColorsFromTheme(GetNativeTheme());
62 }
63
64 BubbleDelegateView::BubbleDelegateView(
65     View* anchor_view,
66     BubbleBorder::Arrow arrow)
67     : close_on_esc_(true),
68       close_on_deactivate_(true),
69       anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
70       anchor_widget_(NULL),
71       arrow_(arrow),
72       shadow_(BubbleBorder::SMALL_SHADOW),
73       color_explicitly_set_(false),
74       margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
75       use_focusless_(false),
76       accept_events_(true),
77       border_accepts_events_(true),
78       adjust_if_offscreen_(true),
79       parent_window_(NULL) {
80   SetAnchorView(anchor_view);
81   AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
82   UpdateColorsFromTheme(GetNativeTheme());
83 }
84
85 BubbleDelegateView::~BubbleDelegateView() {
86   if (GetWidget())
87     GetWidget()->RemoveObserver(this);
88   SetLayoutManager(NULL);
89   SetAnchorView(NULL);
90 }
91
92 // static
93 Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) {
94   bubble_delegate->Init();
95   // Get the latest anchor widget from the anchor view at bubble creation time.
96   bubble_delegate->SetAnchorView(bubble_delegate->GetAnchorView());
97   Widget* bubble_widget = CreateBubbleWidget(bubble_delegate);
98
99 #if defined(OS_WIN)
100   // If glass is enabled, the bubble is allowed to extend outside the bounds of
101   // the parent frame and let DWM handle compositing.  If not, then we don't
102   // want to allow the bubble to extend the frame because it will be clipped.
103   bubble_delegate->set_adjust_if_offscreen(ui::win::IsAeroGlassEnabled());
104 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
105   // Linux clips bubble windows that extend outside their parent window bounds.
106   bubble_delegate->set_adjust_if_offscreen(false);
107 #endif
108
109   bubble_delegate->SizeToContents();
110   bubble_widget->AddObserver(bubble_delegate);
111   return bubble_widget;
112 }
113
114 BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() {
115   return this;
116 }
117
118 bool BubbleDelegateView::CanActivate() const {
119   return !use_focusless();
120 }
121
122 bool BubbleDelegateView::ShouldShowCloseButton() const {
123   return false;
124 }
125
126 View* BubbleDelegateView::GetContentsView() {
127   return this;
128 }
129
130 NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView(
131     Widget* widget) {
132   BubbleFrameView* frame = new BubbleFrameView(margins());
133   BubbleBorder::Arrow adjusted_arrow = arrow();
134   if (base::i18n::IsRTL())
135     adjusted_arrow = BubbleBorder::horizontal_mirror(adjusted_arrow);
136   frame->SetBubbleBorder(scoped_ptr<BubbleBorder>(
137       new BubbleBorder(adjusted_arrow, shadow(), color())));
138   return frame;
139 }
140
141 void BubbleDelegateView::GetAccessibleState(ui::AXViewState* state) {
142   state->role = ui::AX_ROLE_DIALOG;
143 }
144
145 void BubbleDelegateView::OnWidgetDestroying(Widget* widget) {
146   if (anchor_widget() == widget)
147     SetAnchorView(NULL);
148 }
149
150 void BubbleDelegateView::OnWidgetVisibilityChanging(Widget* widget,
151                                                     bool visible) {
152 #if defined(OS_WIN)
153   // On Windows we need to handle this before the bubble is visible or hidden.
154   // Please see the comment on the OnWidgetVisibilityChanging function. On
155   // other platforms it is fine to handle it after the bubble is shown/hidden.
156   HandleVisibilityChanged(widget, visible);
157 #endif
158 }
159
160 void BubbleDelegateView::OnWidgetVisibilityChanged(Widget* widget,
161                                                    bool visible) {
162 #if !defined(OS_WIN)
163   HandleVisibilityChanged(widget, visible);
164 #endif
165 }
166
167 void BubbleDelegateView::OnWidgetActivationChanged(Widget* widget,
168                                                    bool active) {
169   if (close_on_deactivate() && widget == GetWidget() && !active)
170     GetWidget()->Close();
171 }
172
173 void BubbleDelegateView::OnWidgetBoundsChanged(Widget* widget,
174                                                const gfx::Rect& new_bounds) {
175   if (anchor_widget() == widget)
176     SizeToContents();
177 }
178
179 View* BubbleDelegateView::GetAnchorView() const {
180   return ViewStorage::GetInstance()->RetrieveView(anchor_view_storage_id_);
181 }
182
183 gfx::Rect BubbleDelegateView::GetAnchorRect() {
184   if (!GetAnchorView())
185     return anchor_rect_;
186
187   anchor_rect_ = GetAnchorView()->GetBoundsInScreen();
188   anchor_rect_.Inset(anchor_view_insets_);
189   return anchor_rect_;
190 }
191
192 void BubbleDelegateView::OnBeforeBubbleWidgetInit(Widget::InitParams* params,
193                                                   Widget* widget) const {
194 }
195
196 void BubbleDelegateView::SetAlignment(BubbleBorder::BubbleAlignment alignment) {
197   GetBubbleFrameView()->bubble_border()->set_alignment(alignment);
198   SizeToContents();
199 }
200
201 void BubbleDelegateView::SetArrowPaintType(
202     BubbleBorder::ArrowPaintType paint_type) {
203   GetBubbleFrameView()->bubble_border()->set_paint_arrow(paint_type);
204   SizeToContents();
205 }
206
207 void BubbleDelegateView::OnAnchorBoundsChanged() {
208   SizeToContents();
209 }
210
211 bool BubbleDelegateView::AcceleratorPressed(
212     const ui::Accelerator& accelerator) {
213   if (!close_on_esc() || accelerator.key_code() != ui::VKEY_ESCAPE)
214     return false;
215   GetWidget()->Close();
216   return true;
217 }
218
219 void BubbleDelegateView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
220   UpdateColorsFromTheme(theme);
221 }
222
223 void BubbleDelegateView::Init() {}
224
225 void BubbleDelegateView::SetAnchorView(View* anchor_view) {
226   // When the anchor view gets set the associated anchor widget might
227   // change as well.
228   if (!anchor_view || anchor_widget() != anchor_view->GetWidget()) {
229     if (anchor_widget()) {
230       anchor_widget_->RemoveObserver(this);
231       anchor_widget_ = NULL;
232     }
233     if (anchor_view) {
234       anchor_widget_ = anchor_view->GetWidget();
235       if (anchor_widget_)
236         anchor_widget_->AddObserver(this);
237     }
238   }
239
240   // Remove the old storage item and set the new (if there is one).
241   ViewStorage* view_storage = ViewStorage::GetInstance();
242   if (view_storage->RetrieveView(anchor_view_storage_id_))
243     view_storage->RemoveView(anchor_view_storage_id_);
244   if (anchor_view)
245     view_storage->StoreView(anchor_view_storage_id_, anchor_view);
246
247   // Do not update anchoring for NULL views; this could indicate that our
248   // NativeWindow is being destroyed, so it would be dangerous for us to update
249   // our anchor bounds at that point. (It's safe to skip this, since if we were
250   // to update the bounds when |anchor_view| is NULL, the bubble won't move.)
251   if (anchor_view && GetWidget())
252     OnAnchorBoundsChanged();
253 }
254
255 void BubbleDelegateView::SetAnchorRect(const gfx::Rect& rect) {
256   anchor_rect_ = rect;
257   if (GetWidget())
258     OnAnchorBoundsChanged();
259 }
260
261 void BubbleDelegateView::SizeToContents() {
262   GetWidget()->SetBounds(GetBubbleBounds());
263 }
264
265 BubbleFrameView* BubbleDelegateView::GetBubbleFrameView() const {
266   const NonClientView* view =
267       GetWidget() ? GetWidget()->non_client_view() : NULL;
268   return view ? static_cast<BubbleFrameView*>(view->frame_view()) : NULL;
269 }
270
271 gfx::Rect BubbleDelegateView::GetBubbleBounds() {
272   // The argument rect has its origin at the bubble's arrow anchor point;
273   // its size is the preferred size of the bubble's client view (this view).
274   return GetBubbleFrameView()->GetUpdatedWindowBounds(GetAnchorRect(),
275       GetPreferredSize(), adjust_if_offscreen_);
276 }
277
278 void BubbleDelegateView::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
279   if (!color_explicitly_set_)
280     color_ = theme->GetSystemColor(ui::NativeTheme::kColorId_WindowBackground);
281   set_background(Background::CreateSolidBackground(color()));
282   BubbleFrameView* frame_view = GetBubbleFrameView();
283   if (frame_view)
284     frame_view->bubble_border()->set_background_color(color());
285 }
286
287 void BubbleDelegateView::HandleVisibilityChanged(Widget* widget, bool visible) {
288   if (widget == GetWidget() && anchor_widget() &&
289       anchor_widget()->GetTopLevelWidget()) {
290     if (visible)
291       anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering();
292     else
293       anchor_widget()->GetTopLevelWidget()->EnableInactiveRendering();
294   }
295 }
296
297 }  // namespace views