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.
5 #include "content/shell/browser/shell_web_contents_view_delegate.h"
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"
25 using blink::WebContextMenuData;
29 WebContentsViewDelegate* CreateShellWebContentsViewDelegate(
30 WebContents* web_contents) {
31 return new ShellWebContentsViewDelegate(web_contents);
34 ShellWebContentsViewDelegate::ShellWebContentsViewDelegate(
35 WebContents* web_contents)
36 : web_contents_(web_contents),
37 floating_(gtk_floating_container_new()) {
40 ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {
44 void ShellWebContentsViewDelegate::ShowContextMenu(
45 RenderFrameHost* render_frame_host,
46 const ContextMenuParams& params) {
47 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
50 GtkWidget* menu = gtk_menu_new();
53 bool has_link = !params_.unfiltered_link_url.is_empty();
54 bool has_selection = !params_.selection_text.empty();
56 if (params_.media_type == WebContextMenuData::MediaTypeNone &&
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,
64 G_CALLBACK(OnBackMenuActivatedThunk),
66 gtk_widget_set_sensitive(back_menu,
67 web_contents_->GetController().CanGoBack());
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,
73 G_CALLBACK(OnForwardMenuActivatedThunk),
75 gtk_widget_set_sensitive(forward_menu,
76 web_contents_->GetController().CanGoForward());
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,
82 G_CALLBACK(OnReloadMenuActivatedThunk),
85 GtkWidget* navigate_separator = gtk_separator_menu_item_new();
86 gtk_menu_append(GTK_MENU(menu), navigate_separator);
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,
94 G_CALLBACK(OnOpenURLMenuActivatedThunk),
97 GtkWidget* link_separator = gtk_separator_menu_item_new();
98 gtk_menu_append(GTK_MENU(menu), link_separator);
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,
106 G_CALLBACK(OnCutMenuActivatedThunk),
108 gtk_widget_set_sensitive(
110 params_.edit_flags & WebContextMenuData::CanCut);
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,
116 G_CALLBACK(OnCopyMenuActivatedThunk),
118 gtk_widget_set_sensitive(
120 params_.edit_flags & WebContextMenuData::CanCopy);
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,
126 G_CALLBACK(OnPasteMenuActivatedThunk),
128 gtk_widget_set_sensitive(
130 params_.edit_flags & WebContextMenuData::CanPaste);
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,
136 G_CALLBACK(OnDeleteMenuActivatedThunk),
138 gtk_widget_set_sensitive(
140 params_.edit_flags & WebContextMenuData::CanDelete);
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,
149 G_CALLBACK(OnCopyMenuActivatedThunk),
152 GtkWidget* copy_separator = gtk_separator_menu_item_new();
153 gtk_menu_append(GTK_MENU(menu), copy_separator);
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,
160 G_CALLBACK(OnInspectMenuActivatedThunk),
163 gtk_widget_show_all(menu);
165 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, GDK_CURRENT_TIME);
168 WebDragDestDelegate* ShellWebContentsViewDelegate::GetDragDestDelegate() {
172 void ShellWebContentsViewDelegate::Initialize(GtkWidget* expanded_container,
173 ui::FocusStoreGtk* focus_store) {
174 expanded_container_ = expanded_container;
176 gtk_container_add(GTK_CONTAINER(floating_.get()), expanded_container_);
177 gtk_widget_show(floating_.get());
180 gfx::NativeView ShellWebContentsViewDelegate::GetNativeView() const {
181 return floating_.get();
184 void ShellWebContentsViewDelegate::Focus() {
185 GtkWidget* widget = web_contents_->GetView()->GetContentNativeView();
187 gtk_widget_grab_focus(widget);
190 gboolean ShellWebContentsViewDelegate::OnNativeViewFocusEvent(
192 GtkDirectionType type,
193 gboolean* return_value) {
197 void ShellWebContentsViewDelegate::OnBackMenuActivated(GtkWidget* widget) {
198 web_contents_->GetController().GoToOffset(-1);
199 web_contents_->GetView()->Focus();
202 void ShellWebContentsViewDelegate::OnForwardMenuActivated(GtkWidget* widget) {
203 web_contents_->GetController().GoToOffset(1);
204 web_contents_->GetView()->Focus();
207 void ShellWebContentsViewDelegate::OnReloadMenuActivated(GtkWidget* widget) {
208 web_contents_->GetController().Reload(false);
209 web_contents_->GetView()->Focus();
212 void ShellWebContentsViewDelegate::OnOpenURLMenuActivated(GtkWidget* widget) {
213 ShellBrowserContext* browser_context =
214 ShellContentBrowserClient::Get()->browser_context();
215 Shell::CreateNewWindow(browser_context,
222 void ShellWebContentsViewDelegate::OnCutMenuActivated(GtkWidget* widget) {
223 web_contents_->GetRenderViewHost()->Cut();
226 void ShellWebContentsViewDelegate::OnCopyMenuActivated(GtkWidget* widget) {
227 web_contents_->GetRenderViewHost()->Copy();
230 void ShellWebContentsViewDelegate::OnPasteMenuActivated(GtkWidget* widget) {
231 web_contents_->GetRenderViewHost()->Paste();
234 void ShellWebContentsViewDelegate::OnDeleteMenuActivated(GtkWidget* widget) {
235 web_contents_->GetRenderViewHost()->Delete();
238 void ShellWebContentsViewDelegate::OnInspectMenuActivated(GtkWidget* widget) {
239 ShellDevToolsFrontend::Show(web_contents_);
242 } // namespace content