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