- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / gtk / fullscreen_exit_bubble_gtk.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 "chrome/browser/ui/gtk/fullscreen_exit_bubble_gtk.h"
6
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"
21
22 namespace {
23
24 const GdkColor kBackgroundColor = GDK_COLOR_RGB(0xff, 0xff, 0xff);
25 const GdkColor kFrameColor = GDK_COLOR_RGB(0x63, 0x63, 0x63);
26
27 }  // namespace
28
29 FullscreenExitBubbleGtk::FullscreenExitBubbleGtk(
30     GtkFloatingContainer* container,
31     Browser* browser,
32     const GURL& url,
33     FullscreenExitBubbleType bubble_type)
34     : FullscreenExitBubble(browser, url, bubble_type),
35       theme_service_(NULL),
36       bubble_(NULL),
37       container_(container) {
38   InitWidgets();
39 }
40
41 FullscreenExitBubbleGtk::~FullscreenExitBubbleGtk() {
42 }
43
44 void FullscreenExitBubbleGtk::UpdateContent(
45     const GURL& url,
46     FullscreenExitBubbleType bubble_type) {
47   if (bubble_type == FEB_TYPE_NONE) {
48     NOTREACHED();
49     bubble_type = FEB_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION;
50   }
51
52   url_ = url;
53   bubble_type_ = bubble_type;
54
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_);
64   } else {
65     bool link_visible = true;
66     string16 accelerator;
67     if (bubble_type == FEB_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION ||
68         bubble_type ==
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);
73     } else {
74       link_visible = false;
75     }
76     if (link_visible) {
77       std::string exit_link_text(
78           l10n_util::GetStringUTF8(IDS_EXIT_FULLSCREEN_MODE) + " " +
79           l10n_util::GetStringFUTF8(IDS_EXIT_FULLSCREEN_MODE_ACCELERATOR,
80               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_);
85     } else {
86       gtk_widget_hide(link_);
87       gtk_widget_show(instruction_label_);
88     }
89     gtk_widget_hide(allow_button_);
90     gtk_widget_hide(deny_button_);
91   }
92
93   Show();
94   StopWatchingMouse();
95   StartWatchingMouseIfNecessary();
96 }
97
98 void FullscreenExitBubbleGtk::InitWidgets() {
99   theme_service_ = GtkThemeService::GetFrom(browser_->profile());
100
101   hbox_ = gtk_hbox_new(false, ui::kControlSpacing);
102
103   message_label_ = theme_service_->BuildLabel(GetMessage(url_).c_str(),
104                                               ui::kGdkBlack);
105   gtk_box_pack_start(GTK_BOX(hbox_), message_label_, FALSE, FALSE, 0);
106
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);
112
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);
118
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_),
123                                            FALSE);
124   gtk_box_pack_start(GTK_BOX(hbox_), link_, FALSE, FALSE, 0);
125
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);
129
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);
139
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();
146
147   gtk_floating_container_add_floating(GTK_FLOATING_CONTAINER(container_),
148                                       widget());
149
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);
157
158   UpdateContent(url_, bubble_type_);
159
160   theme_service_->InitThemesFor(this);
161   registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
162                  content::Source<ThemeService>(theme_service_));
163 }
164
165 std::string FullscreenExitBubbleGtk::GetMessage(const GURL& url) {
166   if (url.is_empty())
167     return l10n_util::GetStringUTF8(IDS_FULLSCREEN_USER_ENTERED_FULLSCREEN);
168
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()));
173 }
174
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);
180   } else {
181     gtk_widget_size_request(widget(), &bubble_size);
182   }
183   return gfx::Rect(bubble_size.width, bubble_size.height);
184 }
185
186 gfx::Point FullscreenExitBubbleGtk::GetCursorScreenPoint() {
187   GdkDisplay* display = gtk_widget_get_display(widget());
188
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.
192   int x, y;
193   gdk_display_get_pointer(display, NULL, &x, &y, NULL);
194
195   return gfx::Point(x, y);
196 }
197
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);
205 }
206
207 bool FullscreenExitBubbleGtk::IsWindowActive() {
208   if (!gtk_widget_get_parent(widget()))
209     return false;
210   GtkWindow* window = GTK_WINDOW(
211       gtk_widget_get_ancestor(widget(), GTK_TYPE_WINDOW));
212   return gtk_window_is_active(window);
213 }
214
215 void FullscreenExitBubbleGtk::Hide() {
216   slide_widget_->Close();
217 }
218
219 void FullscreenExitBubbleGtk::Show() {
220   slide_widget_->Open();
221 }
222
223 bool FullscreenExitBubbleGtk::IsAnimating() {
224   return slide_widget_->IsAnimating();
225 }
226
227 bool FullscreenExitBubbleGtk::CanMouseTriggerSlideIn() const {
228   return true;
229 }
230
231 void FullscreenExitBubbleGtk::StartWatchingMouseIfNecessary() {
232   if (!fullscreen_bubble::ShowButtonsForType(bubble_type_))
233     StartWatchingMouse();
234 }
235
236 void FullscreenExitBubbleGtk::OnSetFloatingPosition(
237     GtkWidget* floating_container,
238     GtkAllocation* allocation) {
239   GtkRequisition bubble_size;
240   gtk_widget_size_request(widget(), &bubble_size);
241
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);
248
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);
253 }
254
255 void FullscreenExitBubbleGtk::OnLinkClicked(GtkWidget* link) {
256   ToggleFullscreen();
257 }
258
259 void FullscreenExitBubbleGtk::OnAllowClicked(GtkWidget* button) {
260   Accept();
261 }
262
263 void FullscreenExitBubbleGtk::OnDenyClicked(GtkWidget* button) {
264   Cancel();
265 }
266
267 void FullscreenExitBubbleGtk::Observe(
268     int type,
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);
274   else
275     gtk_widget_modify_bg(bubble_, GTK_STATE_NORMAL, &kBackgroundColor);
276 }