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