147265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org// Copyright (c) 2012 The Chromium Authors. All rights reserved.
247265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org// Use of this source code is governed by a BSD-style license that can be
347265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org// found in the LICENSE file.
447265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
547265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#include "ui/views/bubble/bubble_delegate.h"
647265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
747265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#include "ui/accessibility/ax_view_state.h"
847265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#include "ui/base/resource/resource_bundle.h"
947265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#include "ui/gfx/color_utils.h"
1047265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#include "ui/gfx/rect.h"
1147265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#include "ui/native_theme/native_theme.h"
1247265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#include "ui/views/bubble/bubble_frame_view.h"
1347265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#include "ui/views/focus/view_storage.h"
1447265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#include "ui/views/widget/widget.h"
1547265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#include "ui/views/widget/widget_observer.h"
1653a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org
1747265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#if defined(OS_WIN)
1853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org#include "ui/base/win/shell.h"
1947265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#endif
2053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org
2147265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org// The defaut margin between the content and the inside border, in pixels.
2253a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.orgstatic const int kDefaultMargin = 6;
2347265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
2447265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.orgnamespace views {
2553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org
2647265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.orgnamespace {
2753a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org
2847265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org// Create a widget to host the bubble.
2953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.orgWidget* CreateBubbleWidget(BubbleDelegateView* bubble) {
3047265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  Widget* bubble_widget = new Widget();
3147265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  Widget::InitParams bubble_params(Widget::InitParams::TYPE_BUBBLE);
3253a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  bubble_params.delegate = bubble;
3353a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  bubble_params.opacity = Widget::InitParams::TRANSLUCENT_WINDOW;
3453a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  bubble_params.accept_events = bubble->accept_events();
3553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  if (bubble->parent_window())
3653a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    bubble_params.parent = bubble->parent_window();
3753a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  else if (bubble->anchor_widget())
3853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    bubble_params.parent = bubble->anchor_widget()->GetNativeView();
3953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  bubble_params.activatable = bubble->CanActivate() ?
4053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org      Widget::InitParams::ACTIVATABLE_YES : Widget::InitParams::ACTIVATABLE_NO;
4147265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  bubble->OnBeforeBubbleWidgetInit(&bubble_params, bubble_widget);
4247265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  bubble_widget->Init(bubble_params);
4347265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  return bubble_widget;
4447265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org}
4547265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
4647265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org}  // namespace
4747265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
4847265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.orgBubbleDelegateView::BubbleDelegateView()
4953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    : close_on_esc_(true),
5047265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      close_on_deactivate_(true),
5147265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
5247265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      anchor_widget_(NULL),
5347265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      arrow_(BubbleBorder::TOP_LEFT),
5447265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      shadow_(BubbleBorder::SMALL_SHADOW),
5553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org      color_explicitly_set_(false),
5647265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
5747265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      accept_events_(true),
5847265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      border_accepts_events_(true),
5947265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      adjust_if_offscreen_(true),
6047265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      parent_window_(NULL) {
6153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
6247265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  UpdateColorsFromTheme(GetNativeTheme());
6347265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org}
6447265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
6547265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.orgBubbleDelegateView::BubbleDelegateView(
6647265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org    View* anchor_view,
6753a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    BubbleBorder::Arrow arrow)
6847265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org    : close_on_esc_(true),
6947265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      close_on_deactivate_(true),
7047265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
7147265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      anchor_widget_(NULL),
7247265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      arrow_(arrow),
7347265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      shadow_(BubbleBorder::SMALL_SHADOW),
7447265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      color_explicitly_set_(false),
7553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org      margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
7647265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org      accept_events_(true),
7753a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org      border_accepts_events_(true),
7853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org      adjust_if_offscreen_(true),
7953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org      parent_window_(NULL) {
8053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  SetAnchorView(anchor_view);
8153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
8247265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  UpdateColorsFromTheme(GetNativeTheme());
8353a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org}
8453a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org
8553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.orgBubbleDelegateView::~BubbleDelegateView() {
8653a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  if (GetWidget())
8753a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    GetWidget()->RemoveObserver(this);
8847265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  SetLayoutManager(NULL);
8953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  SetAnchorView(NULL);
9053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org}
9153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org
9253a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org// static
9353a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.orgWidget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) {
9447265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  bubble_delegate->Init();
9553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  // Get the latest anchor widget from the anchor view at bubble creation time.
9653a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  bubble_delegate->SetAnchorView(bubble_delegate->GetAnchorView());
9753a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  Widget* bubble_widget = CreateBubbleWidget(bubble_delegate);
9853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org
9953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org#if defined(OS_WIN)
10047265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  // If glass is enabled, the bubble is allowed to extend outside the bounds of
10147265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  // the parent frame and let DWM handle compositing.  If not, then we don't
10247265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  // want to allow the bubble to extend the frame because it will be clipped.
10353a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  bubble_delegate->set_adjust_if_offscreen(ui::win::IsAeroGlassEnabled());
10453a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org#elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
10553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  // Linux clips bubble windows that extend outside their parent window bounds.
10647265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  bubble_delegate->set_adjust_if_offscreen(false);
10747265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org#endif
10847265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
10947265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  bubble_delegate->SizeToContents();
11095aa45d73048f952dcaad0037429cc6751b34f2fjohannkoenig@google.com  bubble_widget->AddObserver(bubble_delegate);
11195aa45d73048f952dcaad0037429cc6751b34f2fjohannkoenig@google.com  return bubble_widget;
11295aa45d73048f952dcaad0037429cc6751b34f2fjohannkoenig@google.com}
11395aa45d73048f952dcaad0037429cc6751b34f2fjohannkoenig@google.com
11495aa45d73048f952dcaad0037429cc6751b34f2fjohannkoenig@google.comBubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() {
11595aa45d73048f952dcaad0037429cc6751b34f2fjohannkoenig@google.com  return this;
11695aa45d73048f952dcaad0037429cc6751b34f2fjohannkoenig@google.com}
117ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org
118ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.orgbool BubbleDelegateView::ShouldShowCloseButton() const {
119ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org  return false;
120ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org}
121ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org
122ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.orgView* BubbleDelegateView::GetContentsView() {
123ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org  return this;
124ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org}
12553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org
12653a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.orgNonClientFrameView* BubbleDelegateView::CreateNonClientFrameView(
12753a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    Widget* widget) {
12853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  BubbleFrameView* frame = new BubbleFrameView(margins());
12953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  // Note: In CreateBubble, the call to SizeToContents() will cause
13053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  // the relayout that this call requires.
13153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  frame->SetTitleFontList(GetTitleFontList());
13253a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  BubbleBorder::Arrow adjusted_arrow = arrow();
13353a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  if (base::i18n::IsRTL())
13453a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    adjusted_arrow = BubbleBorder::horizontal_mirror(adjusted_arrow);
13553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  frame->SetBubbleBorder(scoped_ptr<BubbleBorder>(
13653a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org      new BubbleBorder(adjusted_arrow, shadow(), color())));
13753a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  return frame;
13853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org}
13953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org
14053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.orgvoid BubbleDelegateView::GetAccessibleState(ui::AXViewState* state) {
14147265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  state->role = ui::AX_ROLE_DIALOG;
14253a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org}
143d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org
144d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.orgvoid BubbleDelegateView::OnWidgetDestroying(Widget* widget) {
145d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org  if (anchor_widget() == widget)
146d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org    SetAnchorView(NULL);
147d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org}
148d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org
149d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.orgvoid BubbleDelegateView::OnWidgetVisibilityChanging(Widget* widget,
150d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org                                                    bool visible) {
151d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org#if defined(OS_WIN)
152d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org  // On Windows we need to handle this before the bubble is visible or hidden.
153d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org  // Please see the comment on the OnWidgetVisibilityChanging function. On
154d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org  // other platforms it is fine to handle it after the bubble is shown/hidden.
155d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org  HandleVisibilityChanged(widget, visible);
156d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org#endif
157d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org}
158d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org
159d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.orgvoid BubbleDelegateView::OnWidgetVisibilityChanged(Widget* widget,
160d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org                                                   bool visible) {
161d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org#if !defined(OS_WIN)
162d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org  HandleVisibilityChanged(widget, visible);
163d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org#endif
164}
165
166void BubbleDelegateView::OnWidgetActivationChanged(Widget* widget,
167                                                   bool active) {
168  if (close_on_deactivate() && widget == GetWidget() && !active)
169    GetWidget()->Close();
170}
171
172void BubbleDelegateView::OnWidgetBoundsChanged(Widget* widget,
173                                               const gfx::Rect& new_bounds) {
174  if (anchor_widget() == widget)
175    SizeToContents();
176}
177
178View* BubbleDelegateView::GetAnchorView() const {
179  return ViewStorage::GetInstance()->RetrieveView(anchor_view_storage_id_);
180}
181
182gfx::Rect BubbleDelegateView::GetAnchorRect() const {
183  if (!GetAnchorView())
184    return anchor_rect_;
185
186  anchor_rect_ = GetAnchorView()->GetBoundsInScreen();
187  anchor_rect_.Inset(anchor_view_insets_);
188  return anchor_rect_;
189}
190
191void BubbleDelegateView::OnBeforeBubbleWidgetInit(Widget::InitParams* params,
192                                                  Widget* widget) const {
193}
194
195void BubbleDelegateView::SetAlignment(BubbleBorder::BubbleAlignment alignment) {
196  GetBubbleFrameView()->bubble_border()->set_alignment(alignment);
197  SizeToContents();
198}
199
200void BubbleDelegateView::SetArrowPaintType(
201    BubbleBorder::ArrowPaintType paint_type) {
202  GetBubbleFrameView()->bubble_border()->set_paint_arrow(paint_type);
203  SizeToContents();
204}
205
206void BubbleDelegateView::OnAnchorBoundsChanged() {
207  SizeToContents();
208}
209
210bool BubbleDelegateView::AcceleratorPressed(
211    const ui::Accelerator& accelerator) {
212  if (!close_on_esc() || accelerator.key_code() != ui::VKEY_ESCAPE)
213    return false;
214  GetWidget()->Close();
215  return true;
216}
217
218void BubbleDelegateView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
219  UpdateColorsFromTheme(theme);
220}
221
222void BubbleDelegateView::Init() {}
223
224void BubbleDelegateView::SetAnchorView(View* anchor_view) {
225  // When the anchor view gets set the associated anchor widget might
226  // change as well.
227  if (!anchor_view || anchor_widget() != anchor_view->GetWidget()) {
228    if (anchor_widget()) {
229      anchor_widget_->RemoveObserver(this);
230      anchor_widget_ = NULL;
231    }
232    if (anchor_view) {
233      anchor_widget_ = anchor_view->GetWidget();
234      if (anchor_widget_)
235        anchor_widget_->AddObserver(this);
236    }
237  }
238
239  // Remove the old storage item and set the new (if there is one).
240  ViewStorage* view_storage = ViewStorage::GetInstance();
241  if (view_storage->RetrieveView(anchor_view_storage_id_))
242    view_storage->RemoveView(anchor_view_storage_id_);
243  if (anchor_view)
244    view_storage->StoreView(anchor_view_storage_id_, anchor_view);
245
246  // Do not update anchoring for NULL views; this could indicate that our
247  // NativeWindow is being destroyed, so it would be dangerous for us to update
248  // our anchor bounds at that point. (It's safe to skip this, since if we were
249  // to update the bounds when |anchor_view| is NULL, the bubble won't move.)
250  if (anchor_view && GetWidget())
251    OnAnchorBoundsChanged();
252}
253
254void BubbleDelegateView::SetAnchorRect(const gfx::Rect& rect) {
255  anchor_rect_ = rect;
256  if (GetWidget())
257    OnAnchorBoundsChanged();
258}
259
260void BubbleDelegateView::SizeToContents() {
261  GetWidget()->SetBounds(GetBubbleBounds());
262}
263
264BubbleFrameView* BubbleDelegateView::GetBubbleFrameView() const {
265  const NonClientView* view =
266      GetWidget() ? GetWidget()->non_client_view() : NULL;
267  return view ? static_cast<BubbleFrameView*>(view->frame_view()) : NULL;
268}
269
270gfx::Rect BubbleDelegateView::GetBubbleBounds() {
271  // The argument rect has its origin at the bubble's arrow anchor point;
272  // its size is the preferred size of the bubble's client view (this view).
273  bool anchor_minimized = anchor_widget() && anchor_widget()->IsMinimized();
274  return GetBubbleFrameView()->GetUpdatedWindowBounds(GetAnchorRect(),
275      GetPreferredSize(), adjust_if_offscreen_ && !anchor_minimized);
276}
277
278const gfx::FontList& BubbleDelegateView::GetTitleFontList() const {
279  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
280  return rb.GetFontList(ui::ResourceBundle::MediumFont);
281}
282
283
284void BubbleDelegateView::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
285  if (!color_explicitly_set_)
286    color_ = theme->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground);
287  set_background(Background::CreateSolidBackground(color()));
288  BubbleFrameView* frame_view = GetBubbleFrameView();
289  if (frame_view)
290    frame_view->bubble_border()->set_background_color(color());
291}
292
293void BubbleDelegateView::HandleVisibilityChanged(Widget* widget, bool visible) {
294  if (widget == GetWidget() && anchor_widget() &&
295      anchor_widget()->GetTopLevelWidget()) {
296    if (visible)
297      anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering();
298    else
299      anchor_widget()->GetTopLevelWidget()->EnableInactiveRendering();
300  }
301}
302
303}  // namespace views
304