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.
5 #include "chrome/browser/ui/gtk/fullscreen_exit_bubble_gtk.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/gtk/gtk_chrome_link_button.h"
11 #include "chrome/browser/ui/gtk/gtk_theme_service.h"
12 #include "chrome/browser/ui/gtk/gtk_util.h"
13 #include "chrome/browser/ui/gtk/rounded_window.h"
14 #include "content/public/browser/notification_source.h"
15 #include "content/public/browser/render_widget_host_view.h"
16 #include "grit/generated_resources.h"
17 #include "grit/ui_strings.h"
18 #include "ui/base/gtk/gtk_floating_container.h"
19 #include "ui/base/gtk/gtk_hig_constants.h"
20 #include "ui/base/l10n/l10n_util.h"
24 const GdkColor kBackgroundColor = GDK_COLOR_RGB(0xff, 0xff, 0xff);
25 const GdkColor kFrameColor = GDK_COLOR_RGB(0x63, 0x63, 0x63);
29 FullscreenExitBubbleGtk::FullscreenExitBubbleGtk(
30 GtkFloatingContainer* container,
33 FullscreenExitBubbleType bubble_type)
34 : FullscreenExitBubble(browser, url, bubble_type),
37 container_(container) {
41 FullscreenExitBubbleGtk::~FullscreenExitBubbleGtk() {
44 void FullscreenExitBubbleGtk::UpdateContent(
46 FullscreenExitBubbleType bubble_type) {
47 if (bubble_type == FEB_TYPE_NONE) {
49 bubble_type = FEB_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION;
53 bubble_type_ = bubble_type;
55 gtk_label_set_text(GTK_LABEL(message_label_),
56 UTF16ToUTF8(GetCurrentMessageText()).c_str());
57 if (fullscreen_bubble::ShowButtonsForType(bubble_type)) {
58 gtk_widget_hide(link_);
59 gtk_widget_hide(instruction_label_);
60 gtk_widget_show(allow_button_);
61 gtk_button_set_label(GTK_BUTTON(deny_button_),
62 UTF16ToUTF8(GetCurrentDenyButtonText()).c_str());
63 gtk_widget_show(deny_button_);
65 bool link_visible = true;
67 if (bubble_type == FEB_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION ||
69 FEB_TYPE_BROWSER_EXTENSION_FULLSCREEN_EXIT_INSTRUCTION) {
70 accelerator = l10n_util::GetStringUTF16(IDS_APP_F11_KEY);
71 } else if (bubble_type == FEB_TYPE_FULLSCREEN_EXIT_INSTRUCTION) {
72 accelerator = l10n_util::GetStringUTF16(IDS_APP_ESC_KEY);
77 std::string exit_link_text(
78 l10n_util::GetStringUTF8(IDS_EXIT_FULLSCREEN_MODE) + " " +
79 l10n_util::GetStringFUTF8(IDS_EXIT_FULLSCREEN_MODE_ACCELERATOR,
81 gtk_chrome_link_button_set_label(GTK_CHROME_LINK_BUTTON(link_),
82 exit_link_text.c_str());
83 gtk_widget_show(link_);
84 gtk_widget_hide(instruction_label_);
86 gtk_widget_hide(link_);
87 gtk_widget_show(instruction_label_);
89 gtk_widget_hide(allow_button_);
90 gtk_widget_hide(deny_button_);
95 StartWatchingMouseIfNecessary();
98 void FullscreenExitBubbleGtk::InitWidgets() {
99 theme_service_ = GtkThemeService::GetFrom(browser_->profile());
101 hbox_ = gtk_hbox_new(false, ui::kControlSpacing);
103 message_label_ = theme_service_->BuildLabel(GetMessage(url_).c_str(),
105 gtk_box_pack_start(GTK_BOX(hbox_), message_label_, FALSE, FALSE, 0);
107 allow_button_ = gtk_button_new_with_label(
108 l10n_util::GetStringUTF8(IDS_FULLSCREEN_ALLOW).c_str());
109 gtk_widget_set_can_focus(allow_button_, FALSE);
110 gtk_widget_set_no_show_all(allow_button_, FALSE);
111 gtk_box_pack_start(GTK_BOX(hbox_), allow_button_, FALSE, FALSE, 0);
113 deny_button_ = gtk_button_new_with_label(
114 l10n_util::GetStringUTF8(IDS_FULLSCREEN_DENY).c_str());
115 gtk_widget_set_can_focus(deny_button_, FALSE);
116 gtk_widget_set_no_show_all(deny_button_, FALSE);
117 gtk_box_pack_start(GTK_BOX(hbox_), deny_button_, FALSE, FALSE, 0);
119 link_ = gtk_chrome_link_button_new("");
120 gtk_widget_set_can_focus(link_, FALSE);
121 gtk_widget_set_no_show_all(link_, FALSE);
122 gtk_chrome_link_button_set_use_gtk_theme(GTK_CHROME_LINK_BUTTON(link_),
124 gtk_box_pack_start(GTK_BOX(hbox_), link_, FALSE, FALSE, 0);
126 instruction_label_ = gtk_label_new(UTF16ToUTF8(GetInstructionText()).c_str());
127 gtk_widget_set_no_show_all(instruction_label_, FALSE);
128 gtk_box_pack_start(GTK_BOX(hbox_), instruction_label_, FALSE, FALSE, 0);
130 bubble_ = gtk_util::CreateGtkBorderBin(
131 hbox_, &ui::kGdkWhite,
132 kPaddingPx, kPaddingPx, kPaddingPx, kPaddingPx);
133 gtk_util::ActAsRoundedWindow(bubble_, kFrameColor, 3,
134 gtk_util::ROUNDED_ALL, gtk_util::BORDER_ALL);
135 GtkWidget* alignment = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
136 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), 5, 0, 0, 0);
137 gtk_container_add(GTK_CONTAINER(alignment), bubble_);
138 ui_container_.Own(alignment);
140 slide_widget_.reset(new SlideAnimatorGtk(ui_container_.get(),
141 SlideAnimatorGtk::DOWN, kSlideOutDurationMs, false, false, NULL));
142 gtk_widget_set_name(widget(), "exit-fullscreen-bubble");
143 gtk_widget_show_all(ui_container_.get());
144 gtk_widget_show(widget());
145 slide_widget_->OpenWithoutAnimation();
147 gtk_floating_container_add_floating(GTK_FLOATING_CONTAINER(container_),
150 signals_.Connect(container_, "set-floating-position",
151 G_CALLBACK(OnSetFloatingPositionThunk), this);
152 signals_.Connect(link_, "clicked", G_CALLBACK(OnLinkClickedThunk), this);
153 signals_.Connect(allow_button_, "clicked",
154 G_CALLBACK(&OnAllowClickedThunk), this);
155 signals_.Connect(deny_button_, "clicked",
156 G_CALLBACK(&OnDenyClickedThunk), this);
158 UpdateContent(url_, bubble_type_);
160 theme_service_->InitThemesFor(this);
161 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
162 content::Source<ThemeService>(theme_service_));
165 std::string FullscreenExitBubbleGtk::GetMessage(const GURL& url) {
167 return l10n_util::GetStringUTF8(IDS_FULLSCREEN_USER_ENTERED_FULLSCREEN);
169 if (url.SchemeIsFile())
170 return l10n_util::GetStringUTF8(IDS_FULLSCREEN_ENTERED_FULLSCREEN);
171 return l10n_util::GetStringFUTF8(IDS_FULLSCREEN_SITE_ENTERED_FULLSCREEN,
172 UTF8ToUTF16(url.host()));
175 gfx::Rect FullscreenExitBubbleGtk::GetPopupRect(
176 bool ignore_animation_state) const {
177 GtkRequisition bubble_size;
178 if (ignore_animation_state) {
179 gtk_widget_size_request(ui_container_.get(), &bubble_size);
181 gtk_widget_size_request(widget(), &bubble_size);
183 return gfx::Rect(bubble_size.width, bubble_size.height);
186 gfx::Point FullscreenExitBubbleGtk::GetCursorScreenPoint() {
187 GdkDisplay* display = gtk_widget_get_display(widget());
189 // Get cursor position.
190 // TODO: this hits the X server, so we may want to consider decreasing
191 // kPositionCheckHz if we detect that we're running remotely.
193 gdk_display_get_pointer(display, NULL, &x, &y, NULL);
195 return gfx::Point(x, y);
198 bool FullscreenExitBubbleGtk::WindowContainsPoint(gfx::Point pos) {
199 GtkWindow* window = GTK_WINDOW(
200 gtk_widget_get_ancestor(widget(), GTK_TYPE_WINDOW));
201 int width, height, x, y;
202 gtk_window_get_size(window, &width, &height);
203 gtk_window_get_position(window, &x, &y);
204 return gfx::Rect(x, y, width, height).Contains(pos);
207 bool FullscreenExitBubbleGtk::IsWindowActive() {
208 if (!gtk_widget_get_parent(widget()))
210 GtkWindow* window = GTK_WINDOW(
211 gtk_widget_get_ancestor(widget(), GTK_TYPE_WINDOW));
212 return gtk_window_is_active(window);
215 void FullscreenExitBubbleGtk::Hide() {
216 slide_widget_->Close();
219 void FullscreenExitBubbleGtk::Show() {
220 slide_widget_->Open();
223 bool FullscreenExitBubbleGtk::IsAnimating() {
224 return slide_widget_->IsAnimating();
227 bool FullscreenExitBubbleGtk::CanMouseTriggerSlideIn() const {
231 void FullscreenExitBubbleGtk::StartWatchingMouseIfNecessary() {
232 if (!fullscreen_bubble::ShowButtonsForType(bubble_type_))
233 StartWatchingMouse();
236 void FullscreenExitBubbleGtk::OnSetFloatingPosition(
237 GtkWidget* floating_container,
238 GtkAllocation* allocation) {
239 GtkRequisition bubble_size;
240 gtk_widget_size_request(widget(), &bubble_size);
242 // Position the bubble at the top center of the screen.
243 GValue value = { 0, };
244 g_value_init(&value, G_TYPE_INT);
245 g_value_set_int(&value, (allocation->width - bubble_size.width) / 2);
246 gtk_container_child_set_property(GTK_CONTAINER(floating_container),
247 widget(), "x", &value);
249 g_value_set_int(&value, 0);
250 gtk_container_child_set_property(GTK_CONTAINER(floating_container),
251 widget(), "y", &value);
252 g_value_unset(&value);
255 void FullscreenExitBubbleGtk::OnLinkClicked(GtkWidget* link) {
259 void FullscreenExitBubbleGtk::OnAllowClicked(GtkWidget* button) {
263 void FullscreenExitBubbleGtk::OnDenyClicked(GtkWidget* button) {
267 void FullscreenExitBubbleGtk::Observe(
269 const content::NotificationSource& source,
270 const content::NotificationDetails& details) {
271 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_THEME_CHANGED);
272 if (theme_service_->UsingNativeTheme())
273 gtk_widget_modify_bg(bubble_, GTK_STATE_NORMAL, NULL);
275 gtk_widget_modify_bg(bubble_, GTK_STATE_NORMAL, &kBackgroundColor);