summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/browser_bubble_gtk.cc
blob: bf905ed5bee5d62664e7c10867a124aff7bfe22d (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) 2011 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/ui/views/browser_bubble.h"

#include <vector>

#include "chrome/browser/ui/views/bubble/border_contents.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "views/widget/root_view.h"
#include "views/widget/widget_gtk.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/wm_ipc.h"
#include "third_party/cros/chromeos_wm_ipc_enums.h"
#endif

using std::vector;

namespace {

class BubbleWidget : public views::WidgetGtk {
 public:
  BubbleWidget(BrowserBubble* bubble, const gfx::Insets& content_margins)
      : bubble_(bubble),
        border_contents_(new BorderContents) {
    border_contents_->Init();
    border_contents_->set_content_margins(content_margins);
  }

  void ShowAndActivate(bool activate) {
    // TODO: honor activate.
    views::WidgetGtk::Show();
  }

  virtual void Close() {
    if (!bubble_)
      return;  // We have already been closed.
    if (IsActive()) {
      BrowserBubble::Delegate* delegate = bubble_->delegate();
      if (delegate)
        delegate->BubbleLostFocus(bubble_, false);
    }
    views::WidgetGtk::Close();
    bubble_ = NULL;
  }

  virtual void Hide() {
    if (IsActive()&& bubble_) {
      BrowserBubble::Delegate* delegate = bubble_->delegate();
      if (delegate)
        delegate->BubbleLostFocus(bubble_, false);
    }
    views::WidgetGtk::Hide();
  }

  virtual void IsActiveChanged() {
    if (IsActive() || !bubble_)
      return;
    BrowserBubble::Delegate* delegate = bubble_->delegate();
    if (!delegate) {
      bubble_->DetachFromBrowser();
      delete bubble_;
      return;
    }

    // TODO(jcampan): http://crbugs.com/29131 Check if the window we are losing
    //                focus to is a child window and pass that to the call
    //                below.
    delegate->BubbleLostFocus(bubble_, false);
  }

  virtual gboolean OnFocusIn(GtkWidget* widget, GdkEventFocus* event) {
    if (bubble_ && bubble_->delegate())
      bubble_->delegate()->BubbleGotFocus(bubble_);
    return views::WidgetGtk::OnFocusIn(widget, event);
  }

  BorderContents* border_contents() {
    return border_contents_;
  }

 private:
  BrowserBubble* bubble_;
  BorderContents* border_contents_;  // Owned by root view of this widget.

  DISALLOW_COPY_AND_ASSIGN(BubbleWidget);
};

}  // namespace

void BrowserBubble::InitPopup(const gfx::Insets& content_margins) {
  // TODO(port)
  BubbleWidget* pop = new BubbleWidget(this, content_margins);
  pop->MakeTransparent();
  pop->make_transient_to_parent();
  views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
  params.parent = frame_->GetNativeView();
  pop->Init(params);
#if defined(OS_CHROMEOS)
  {
    vector<int> params;
    params.push_back(0);  // don't show while screen is locked
    chromeos::WmIpc::instance()->SetWindowType(
        pop->GetNativeView(),
        chromeos::WM_IPC_WINDOW_CHROME_INFO_BUBBLE,
        &params);
  }
#endif

  views::View* contents_view = new views::View;

  // We add |contents_view| to ourselves before the AddChildView() call below so
  // that when |contents| gets added, it will already have a widget, and thus
  // any NativeButtons it creates in ViewHierarchyChanged() will be functional
  // (e.g. calling SetChecked() on checkboxes is safe).
  pop->SetContentsView(contents_view);

  // Added border_contents before |view_| so it will paint under it.
  contents_view->AddChildView(pop->border_contents());
  contents_view->AddChildView(view_);

  popup_ = pop;

  ResizeToView();
  Reposition();
  AttachToBrowser();
}

void BrowserBubble::MovePopup(int x, int y, int w, int h) {
  views::WidgetGtk* pop = static_cast<views::WidgetGtk*>(popup_);
  pop->SetBounds(gfx::Rect(x, y, w, h));
}

void BrowserBubble::Show(bool activate) {
  if (visible_)
    return;
  static_cast<BubbleWidget*>(popup_)->ShowAndActivate(activate);
  visible_ = true;
}

void BrowserBubble::Hide() {
  if (!visible_)
    return;
  views::WidgetGtk* pop = static_cast<views::WidgetGtk*>(popup_);
  pop->Hide();
  visible_ = false;
}

void BrowserBubble::ResizeToView() {
  BorderContents* border_contents =
      static_cast<BubbleWidget*>(popup_)->border_contents();

  // Calculate and set the bounds for all windows and views.
  gfx::Rect window_bounds;
  gfx::Rect contents_bounds;
  border_contents->SizeAndGetBounds(GetAbsoluteRelativeTo(),
      arrow_location_, false, view_->size(),
      &contents_bounds, &window_bounds);

  border_contents->SetBoundsRect(gfx::Rect(gfx::Point(), window_bounds.size()));
  view_->SetBoundsRect(contents_bounds);

  SetAbsoluteBounds(window_bounds);
}