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 "content/shell/shell_web_contents_view_delegate.h"
7 #include "base/command_line.h"
8 #include "content/public/browser/devtools_http_handler.h"
9 #include "content/public/browser/render_process_host.h"
10 #include "content/public/browser/render_view_host.h"
11 #include "content/public/browser/render_widget_host_view.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_contents_view.h"
14 #include "content/public/common/context_menu_params.h"
15 #include "content/shell/shell.h"
16 #include "content/shell/shell_browser_context.h"
17 #include "content/shell/shell_browser_main_parts.h"
18 #include "content/shell/shell_content_browser_client.h"
19 #include "content/shell/shell_devtools_delegate.h"
20 #include "content/shell/shell_switches.h"
21 #include "content/shell/shell_web_contents_view_delegate_creator.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
23 #include "ui/base/gtk/focus_store_gtk.h"
24 #include "ui/base/gtk/gtk_floating_container.h"
26 using WebKit::WebContextMenuData
;
30 WebContentsViewDelegate
* CreateShellWebContentsViewDelegate(
31 WebContents
* web_contents
) {
32 return new ShellWebContentsViewDelegate(web_contents
);
35 ShellWebContentsViewDelegate::ShellWebContentsViewDelegate(
36 WebContents
* web_contents
)
37 : web_contents_(web_contents
),
38 floating_(gtk_floating_container_new()) {
41 ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {
45 void ShellWebContentsViewDelegate::ShowContextMenu(
46 const ContextMenuParams
& params
,
47 ContextMenuSourceType type
) {
48 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
51 GtkWidget
* menu
= gtk_menu_new();
54 bool has_link
= !params_
.unfiltered_link_url
.is_empty();
55 bool has_selection
= !params_
.selection_text
.empty();
57 if (params_
.media_type
== WebContextMenuData::MediaTypeNone
&&
60 !params_
.is_editable
) {
61 GtkWidget
* back_menu
= gtk_menu_item_new_with_label("Back");
62 gtk_menu_append(GTK_MENU(menu
), back_menu
);
63 g_signal_connect(back_menu
,
65 G_CALLBACK(OnBackMenuActivatedThunk
),
67 gtk_widget_set_sensitive(back_menu
,
68 web_contents_
->GetController().CanGoBack());
70 GtkWidget
* forward_menu
= gtk_menu_item_new_with_label("Forward");
71 gtk_menu_append(GTK_MENU(menu
), forward_menu
);
72 g_signal_connect(forward_menu
,
74 G_CALLBACK(OnForwardMenuActivatedThunk
),
76 gtk_widget_set_sensitive(forward_menu
,
77 web_contents_
->GetController().CanGoForward());
79 GtkWidget
* reload_menu
= gtk_menu_item_new_with_label("Reload");
80 gtk_menu_append(GTK_MENU(menu
), reload_menu
);
81 g_signal_connect(reload_menu
,
83 G_CALLBACK(OnReloadMenuActivatedThunk
),
86 GtkWidget
* navigate_separator
= gtk_separator_menu_item_new();
87 gtk_menu_append(GTK_MENU(menu
), navigate_separator
);
91 GtkWidget
* open_menu
= gtk_menu_item_new_with_label("Open in New Window");
92 gtk_menu_append(GTK_MENU(menu
), open_menu
);
93 g_signal_connect(open_menu
,
95 G_CALLBACK(OnOpenURLMenuActivatedThunk
),
98 GtkWidget
* link_separator
= gtk_separator_menu_item_new();
99 gtk_menu_append(GTK_MENU(menu
), link_separator
);
102 if (params_
.is_editable
) {
103 GtkWidget
* cut_menu
= gtk_menu_item_new_with_label("Cut");
104 gtk_menu_append(GTK_MENU(menu
), cut_menu
);
105 g_signal_connect(cut_menu
,
107 G_CALLBACK(OnCutMenuActivatedThunk
),
109 gtk_widget_set_sensitive(
111 params_
.edit_flags
& WebContextMenuData::CanCut
);
113 GtkWidget
* copy_menu
= gtk_menu_item_new_with_label("Copy");
114 gtk_menu_append(GTK_MENU(menu
), copy_menu
);
115 g_signal_connect(copy_menu
,
117 G_CALLBACK(OnCopyMenuActivatedThunk
),
119 gtk_widget_set_sensitive(
121 params_
.edit_flags
& WebContextMenuData::CanCopy
);
123 GtkWidget
* paste_menu
= gtk_menu_item_new_with_label("Paste");
124 gtk_menu_append(GTK_MENU(menu
), paste_menu
);
125 g_signal_connect(paste_menu
,
127 G_CALLBACK(OnPasteMenuActivatedThunk
),
129 gtk_widget_set_sensitive(
131 params_
.edit_flags
& WebContextMenuData::CanPaste
);
133 GtkWidget
* delete_menu
= gtk_menu_item_new_with_label("Delete");
134 gtk_menu_append(GTK_MENU(menu
), delete_menu
);
135 g_signal_connect(delete_menu
,
137 G_CALLBACK(OnDeleteMenuActivatedThunk
),
139 gtk_widget_set_sensitive(
141 params_
.edit_flags
& WebContextMenuData::CanDelete
);
143 GtkWidget
* edit_separator
= gtk_separator_menu_item_new();
144 gtk_menu_append(GTK_MENU(menu
), edit_separator
);
145 } else if (has_selection
) {
146 GtkWidget
* copy_menu
= gtk_menu_item_new_with_label("Copy");
147 gtk_menu_append(GTK_MENU(menu
), copy_menu
);
148 g_signal_connect(copy_menu
,
150 G_CALLBACK(OnCopyMenuActivatedThunk
),
153 GtkWidget
* copy_separator
= gtk_separator_menu_item_new();
154 gtk_menu_append(GTK_MENU(menu
), copy_separator
);
157 GtkWidget
* inspect_menu
= gtk_menu_item_new_with_label("Inspect...");
158 gtk_menu_append(GTK_MENU(menu
), inspect_menu
);
159 g_signal_connect(inspect_menu
,
161 G_CALLBACK(OnInspectMenuActivatedThunk
),
164 gtk_widget_show_all(menu
);
166 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
, 3, GDK_CURRENT_TIME
);
169 WebDragDestDelegate
* ShellWebContentsViewDelegate::GetDragDestDelegate() {
173 void ShellWebContentsViewDelegate::Initialize(GtkWidget
* expanded_container
,
174 ui::FocusStoreGtk
* focus_store
) {
175 expanded_container_
= expanded_container
;
177 gtk_container_add(GTK_CONTAINER(floating_
.get()), expanded_container_
);
178 gtk_widget_show(floating_
.get());
181 gfx::NativeView
ShellWebContentsViewDelegate::GetNativeView() const {
182 return floating_
.get();
185 void ShellWebContentsViewDelegate::Focus() {
186 GtkWidget
* widget
= web_contents_
->GetView()->GetContentNativeView();
188 gtk_widget_grab_focus(widget
);
191 gboolean
ShellWebContentsViewDelegate::OnNativeViewFocusEvent(
193 GtkDirectionType type
,
194 gboolean
* return_value
) {
198 void ShellWebContentsViewDelegate::OnBackMenuActivated(GtkWidget
* widget
) {
199 web_contents_
->GetController().GoToOffset(-1);
200 web_contents_
->Focus();
203 void ShellWebContentsViewDelegate::OnForwardMenuActivated(GtkWidget
* widget
) {
204 web_contents_
->GetController().GoToOffset(1);
205 web_contents_
->Focus();
208 void ShellWebContentsViewDelegate::OnReloadMenuActivated(GtkWidget
* widget
) {
209 web_contents_
->GetController().Reload(false);
210 web_contents_
->Focus();
213 void ShellWebContentsViewDelegate::OnOpenURLMenuActivated(GtkWidget
* widget
) {
214 ShellBrowserContext
* browser_context
=
215 static_cast<ShellContentBrowserClient
*>(
216 GetContentClient()->browser())->browser_context();
217 Shell::CreateNewWindow(browser_context
,
224 void ShellWebContentsViewDelegate::OnCutMenuActivated(GtkWidget
* widget
) {
225 web_contents_
->GetRenderViewHost()->Cut();
228 void ShellWebContentsViewDelegate::OnCopyMenuActivated(GtkWidget
* widget
) {
229 web_contents_
->GetRenderViewHost()->Copy();
232 void ShellWebContentsViewDelegate::OnPasteMenuActivated(GtkWidget
* widget
) {
233 web_contents_
->GetRenderViewHost()->Paste();
236 void ShellWebContentsViewDelegate::OnDeleteMenuActivated(GtkWidget
* widget
) {
237 web_contents_
->GetRenderViewHost()->Delete();
240 void ShellWebContentsViewDelegate::OnInspectMenuActivated(GtkWidget
* widget
) {
241 ShellContentBrowserClient
* browser_client
=
242 static_cast<ShellContentBrowserClient
*>(
243 GetContentClient()->browser());
244 ShellDevToolsDelegate
* delegate
=
245 browser_client
->shell_browser_main_parts()->devtools_delegate();
246 GURL url
= delegate
->devtools_http_handler()->GetFrontendURL(
247 web_contents_
->GetRenderViewHost());
248 Shell::CreateNewWindow(web_contents_
->GetBrowserContext(),
255 } // namespace content