summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/browser_bubble.cc
blob: 4736ad339cdb99b0d3cc8aeae6726e19720ba0dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/views/browser_bubble.h"

#include "app/l10n_util.h"
#include "chrome/browser/views/frame/browser_view.h"
#include "views/widget/root_view.h"
#include "views/window/window.h"

BrowserBubble::BrowserBubble(views::View* view, views::Widget* frame,
                             const gfx::Point& origin)
    : frame_(frame),
      view_(view),
      visible_(false),
      delegate_(NULL),
      attached_(false) {
  frame_native_view_ = frame_->GetNativeView();
  gfx::Size size = view->GetPreferredSize();
  bounds_.SetRect(origin.x(), origin.y(), size.width(), size.height());
  InitPopup();
}

BrowserBubble::~BrowserBubble() {
  DCHECK(!attached_);
  popup_->CloseNow();
  // Don't call DetachFromBrowser from here.  It needs to talk to the
  // BrowserView to deregister itself, and if BrowserBubble is owned
  // by a child of BrowserView, then it's possible that this stack frame
  // is a descendant of BrowserView's destructor, which leads to problems.
  // In that case, Detach doesn't need to get called anyway since BrowserView
  // will do the necessary cleanup.
}

void BrowserBubble::DetachFromBrowser() {
  DCHECK(attached_);
  if (!attached_)
    return;
  attached_ = false;
  BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(
      frame_->GetWindow()->GetNativeWindow());
  if (browser_view)
    browser_view->DetachBrowserBubble(this);
}

void BrowserBubble::AttachToBrowser() {
  DCHECK(!attached_);
  if (attached_)
    return;
  BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(
      frame_->GetWindow()->GetNativeWindow());
  DCHECK(browser_view);
  if (browser_view) {
    browser_view->AttachBrowserBubble(this);
    attached_ = true;
  }
}

void BrowserBubble::BrowserWindowMoved() {
  if (delegate_)
    delegate_->BubbleBrowserWindowMoved(this);
  else
    Hide();
}

void BrowserBubble::BrowserWindowClosed() {
  if (delegate_)
    delegate_->BubbleBrowserWindowClosed(this);
  else
    Hide();
}

void BrowserBubble::SetBounds(int x, int y, int w, int h) {
  // If the UI layout is RTL, we need to mirror the position of the bubble
  // relative to the parent.
  if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) {
    gfx::Rect frame_bounds;
    frame_->GetBounds(&frame_bounds, false);
    x = frame_bounds.width() - x - w;
  }
  bounds_.SetRect(x, y, w, h);
  Reposition();
}

void BrowserBubble::MoveTo(int x, int y) {
  SetBounds(x, y, bounds_.width(), bounds_.height());
}

void BrowserBubble::Reposition() {
  gfx::Point top_left;
  views::View::ConvertPointToScreen(frame_->GetRootView(), &top_left);
  MovePopup(top_left.x() + bounds_.x(),
            top_left.y() + bounds_.y(),
            bounds_.width(),
            bounds_.height());
}

void BrowserBubble::ResizeToView() {
  gfx::Size size = view_->GetPreferredSize();
  SetBounds(bounds_.x(), bounds_.y(), size.width(), size.height());
}