Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / shell / browser / shell_web_contents_view_delegate_gtk.cc
1 // Copyright 2013 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 "content/shell/browser/shell_web_contents_view_delegate.h"
6
7 #include "base/command_line.h"
8 #include "content/public/browser/render_process_host.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/browser/render_widget_host_view.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/browser/web_contents_view.h"
13 #include "content/public/common/context_menu_params.h"
14 #include "content/shell/browser/shell.h"
15 #include "content/shell/browser/shell_browser_context.h"
16 #include "content/shell/browser/shell_browser_main_parts.h"
17 #include "content/shell/browser/shell_content_browser_client.h"
18 #include "content/shell/browser/shell_devtools_frontend.h"
19 #include "content/shell/browser/shell_web_contents_view_delegate_creator.h"
20 #include "content/shell/common/shell_switches.h"
21 #include "third_party/WebKit/public/web/WebContextMenuData.h"
22 #include "ui/base/gtk/focus_store_gtk.h"
23 #include "ui/base/gtk/gtk_floating_container.h"
24
25 using blink::WebContextMenuData;
26
27 namespace content {
28
29 WebContentsViewDelegate* CreateShellWebContentsViewDelegate(
30     WebContents* web_contents) {
31   return new ShellWebContentsViewDelegate(web_contents);
32 }
33
34 ShellWebContentsViewDelegate::ShellWebContentsViewDelegate(
35     WebContents* web_contents)
36     : web_contents_(web_contents),
37       floating_(gtk_floating_container_new()) {
38 }
39
40 ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {
41   floating_.Destroy();
42 }
43
44 void ShellWebContentsViewDelegate::ShowContextMenu(
45     RenderFrameHost* render_frame_host,
46     const ContextMenuParams& params) {
47   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
48     return;
49
50   GtkWidget* menu = gtk_menu_new();
51
52   params_ = params;
53   bool has_link = !params_.unfiltered_link_url.is_empty();
54   bool has_selection = !params_.selection_text.empty();
55
56   if (params_.media_type == WebContextMenuData::MediaTypeNone &&
57       !has_link &&
58       !has_selection &&
59       !params_.is_editable) {
60     GtkWidget* back_menu = gtk_menu_item_new_with_label("Back");
61     gtk_menu_append(GTK_MENU(menu), back_menu);
62     g_signal_connect(back_menu,
63                      "activate",
64                      G_CALLBACK(OnBackMenuActivatedThunk),
65                      this);
66     gtk_widget_set_sensitive(back_menu,
67                              web_contents_->GetController().CanGoBack());
68
69     GtkWidget* forward_menu = gtk_menu_item_new_with_label("Forward");
70     gtk_menu_append(GTK_MENU(menu), forward_menu);
71     g_signal_connect(forward_menu,
72                      "activate",
73                      G_CALLBACK(OnForwardMenuActivatedThunk),
74                      this);
75     gtk_widget_set_sensitive(forward_menu,
76                              web_contents_->GetController().CanGoForward());
77
78     GtkWidget* reload_menu = gtk_menu_item_new_with_label("Reload");
79     gtk_menu_append(GTK_MENU(menu), reload_menu);
80     g_signal_connect(reload_menu,
81                      "activate",
82                      G_CALLBACK(OnReloadMenuActivatedThunk),
83                      this);
84
85     GtkWidget* navigate_separator = gtk_separator_menu_item_new();
86     gtk_menu_append(GTK_MENU(menu), navigate_separator);
87   }
88
89   if (has_link) {
90     GtkWidget* open_menu = gtk_menu_item_new_with_label("Open in New Window");
91     gtk_menu_append(GTK_MENU(menu), open_menu);
92     g_signal_connect(open_menu,
93                      "activate",
94                      G_CALLBACK(OnOpenURLMenuActivatedThunk),
95                      this);
96
97     GtkWidget* link_separator = gtk_separator_menu_item_new();
98     gtk_menu_append(GTK_MENU(menu), link_separator);
99   }
100
101   if (params_.is_editable) {
102     GtkWidget* cut_menu = gtk_menu_item_new_with_label("Cut");
103     gtk_menu_append(GTK_MENU(menu), cut_menu);
104     g_signal_connect(cut_menu,
105                      "activate",
106                      G_CALLBACK(OnCutMenuActivatedThunk),
107                      this);
108     gtk_widget_set_sensitive(
109         cut_menu,
110         params_.edit_flags & WebContextMenuData::CanCut);
111
112     GtkWidget* copy_menu = gtk_menu_item_new_with_label("Copy");
113     gtk_menu_append(GTK_MENU(menu), copy_menu);
114     g_signal_connect(copy_menu,
115                      "activate",
116                      G_CALLBACK(OnCopyMenuActivatedThunk),
117                      this);
118     gtk_widget_set_sensitive(
119         copy_menu,
120         params_.edit_flags & WebContextMenuData::CanCopy);
121
122     GtkWidget* paste_menu = gtk_menu_item_new_with_label("Paste");
123     gtk_menu_append(GTK_MENU(menu), paste_menu);
124     g_signal_connect(paste_menu,
125                      "activate",
126                      G_CALLBACK(OnPasteMenuActivatedThunk),
127                      this);
128     gtk_widget_set_sensitive(
129         paste_menu,
130         params_.edit_flags & WebContextMenuData::CanPaste);
131
132     GtkWidget* delete_menu = gtk_menu_item_new_with_label("Delete");
133     gtk_menu_append(GTK_MENU(menu), delete_menu);
134     g_signal_connect(delete_menu,
135                      "activate",
136                      G_CALLBACK(OnDeleteMenuActivatedThunk),
137                      this);
138     gtk_widget_set_sensitive(
139         delete_menu,
140         params_.edit_flags & WebContextMenuData::CanDelete);
141
142     GtkWidget* edit_separator = gtk_separator_menu_item_new();
143     gtk_menu_append(GTK_MENU(menu), edit_separator);
144   } else if (has_selection) {
145     GtkWidget* copy_menu = gtk_menu_item_new_with_label("Copy");
146     gtk_menu_append(GTK_MENU(menu), copy_menu);
147     g_signal_connect(copy_menu,
148                      "activate",
149                      G_CALLBACK(OnCopyMenuActivatedThunk),
150                      this);
151
152     GtkWidget* copy_separator = gtk_separator_menu_item_new();
153     gtk_menu_append(GTK_MENU(menu), copy_separator);
154   }
155
156   GtkWidget* inspect_menu = gtk_menu_item_new_with_label("Inspect...");
157   gtk_menu_append(GTK_MENU(menu), inspect_menu);
158   g_signal_connect(inspect_menu,
159                    "activate",
160                    G_CALLBACK(OnInspectMenuActivatedThunk),
161                    this);
162
163   gtk_widget_show_all(menu);
164
165   gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, GDK_CURRENT_TIME);
166 }
167
168 WebDragDestDelegate* ShellWebContentsViewDelegate::GetDragDestDelegate() {
169   return NULL;
170 }
171
172 void ShellWebContentsViewDelegate::Initialize(GtkWidget* expanded_container,
173                                               ui::FocusStoreGtk* focus_store) {
174   expanded_container_ = expanded_container;
175
176   gtk_container_add(GTK_CONTAINER(floating_.get()), expanded_container_);
177   gtk_widget_show(floating_.get());
178 }
179
180 gfx::NativeView ShellWebContentsViewDelegate::GetNativeView() const {
181   return floating_.get();
182 }
183
184 void ShellWebContentsViewDelegate::Focus() {
185   GtkWidget* widget = web_contents_->GetView()->GetContentNativeView();
186   if (widget)
187     gtk_widget_grab_focus(widget);
188 }
189
190 gboolean ShellWebContentsViewDelegate::OnNativeViewFocusEvent(
191     GtkWidget* widget,
192     GtkDirectionType type,
193     gboolean* return_value) {
194   return false;
195 }
196
197 void ShellWebContentsViewDelegate::OnBackMenuActivated(GtkWidget* widget) {
198   web_contents_->GetController().GoToOffset(-1);
199   web_contents_->GetView()->Focus();
200 }
201
202 void ShellWebContentsViewDelegate::OnForwardMenuActivated(GtkWidget* widget) {
203   web_contents_->GetController().GoToOffset(1);
204   web_contents_->GetView()->Focus();
205 }
206
207 void ShellWebContentsViewDelegate::OnReloadMenuActivated(GtkWidget* widget) {
208   web_contents_->GetController().Reload(false);
209   web_contents_->GetView()->Focus();
210 }
211
212 void ShellWebContentsViewDelegate::OnOpenURLMenuActivated(GtkWidget* widget) {
213   ShellBrowserContext* browser_context =
214       ShellContentBrowserClient::Get()->browser_context();
215   Shell::CreateNewWindow(browser_context,
216                          params_.link_url,
217                          NULL,
218                          MSG_ROUTING_NONE,
219                          gfx::Size());
220 }
221
222 void ShellWebContentsViewDelegate::OnCutMenuActivated(GtkWidget* widget) {
223   web_contents_->GetRenderViewHost()->Cut();
224 }
225
226 void ShellWebContentsViewDelegate::OnCopyMenuActivated(GtkWidget* widget) {
227   web_contents_->GetRenderViewHost()->Copy();
228 }
229
230 void ShellWebContentsViewDelegate::OnPasteMenuActivated(GtkWidget* widget) {
231   web_contents_->GetRenderViewHost()->Paste();
232 }
233
234 void ShellWebContentsViewDelegate::OnDeleteMenuActivated(GtkWidget* widget) {
235   web_contents_->GetRenderViewHost()->Delete();
236 }
237
238 void ShellWebContentsViewDelegate::OnInspectMenuActivated(GtkWidget* widget) {
239   ShellDevToolsFrontend::Show(web_contents_);
240 }
241
242 }  // namespace content