Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / shell / browser / shell_web_contents_view_delegate_gtk.cc
blob4c982573298deb3ceda79250d31e289a6cfb1a27
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 WebKit::WebContextMenuData;
27 namespace content {
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() {
41 floating_.Destroy();
44 void ShellWebContentsViewDelegate::ShowContextMenu(
45 const ContextMenuParams& params) {
46 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
47 return;
49 GtkWidget* menu = gtk_menu_new();
51 params_ = params;
52 bool has_link = !params_.unfiltered_link_url.is_empty();
53 bool has_selection = !params_.selection_text.empty();
55 if (params_.media_type == WebContextMenuData::MediaTypeNone &&
56 !has_link &&
57 !has_selection &&
58 !params_.is_editable) {
59 GtkWidget* back_menu = gtk_menu_item_new_with_label("Back");
60 gtk_menu_append(GTK_MENU(menu), back_menu);
61 g_signal_connect(back_menu,
62 "activate",
63 G_CALLBACK(OnBackMenuActivatedThunk),
64 this);
65 gtk_widget_set_sensitive(back_menu,
66 web_contents_->GetController().CanGoBack());
68 GtkWidget* forward_menu = gtk_menu_item_new_with_label("Forward");
69 gtk_menu_append(GTK_MENU(menu), forward_menu);
70 g_signal_connect(forward_menu,
71 "activate",
72 G_CALLBACK(OnForwardMenuActivatedThunk),
73 this);
74 gtk_widget_set_sensitive(forward_menu,
75 web_contents_->GetController().CanGoForward());
77 GtkWidget* reload_menu = gtk_menu_item_new_with_label("Reload");
78 gtk_menu_append(GTK_MENU(menu), reload_menu);
79 g_signal_connect(reload_menu,
80 "activate",
81 G_CALLBACK(OnReloadMenuActivatedThunk),
82 this);
84 GtkWidget* navigate_separator = gtk_separator_menu_item_new();
85 gtk_menu_append(GTK_MENU(menu), navigate_separator);
88 if (has_link) {
89 GtkWidget* open_menu = gtk_menu_item_new_with_label("Open in New Window");
90 gtk_menu_append(GTK_MENU(menu), open_menu);
91 g_signal_connect(open_menu,
92 "activate",
93 G_CALLBACK(OnOpenURLMenuActivatedThunk),
94 this);
96 GtkWidget* link_separator = gtk_separator_menu_item_new();
97 gtk_menu_append(GTK_MENU(menu), link_separator);
100 if (params_.is_editable) {
101 GtkWidget* cut_menu = gtk_menu_item_new_with_label("Cut");
102 gtk_menu_append(GTK_MENU(menu), cut_menu);
103 g_signal_connect(cut_menu,
104 "activate",
105 G_CALLBACK(OnCutMenuActivatedThunk),
106 this);
107 gtk_widget_set_sensitive(
108 cut_menu,
109 params_.edit_flags & WebContextMenuData::CanCut);
111 GtkWidget* copy_menu = gtk_menu_item_new_with_label("Copy");
112 gtk_menu_append(GTK_MENU(menu), copy_menu);
113 g_signal_connect(copy_menu,
114 "activate",
115 G_CALLBACK(OnCopyMenuActivatedThunk),
116 this);
117 gtk_widget_set_sensitive(
118 copy_menu,
119 params_.edit_flags & WebContextMenuData::CanCopy);
121 GtkWidget* paste_menu = gtk_menu_item_new_with_label("Paste");
122 gtk_menu_append(GTK_MENU(menu), paste_menu);
123 g_signal_connect(paste_menu,
124 "activate",
125 G_CALLBACK(OnPasteMenuActivatedThunk),
126 this);
127 gtk_widget_set_sensitive(
128 paste_menu,
129 params_.edit_flags & WebContextMenuData::CanPaste);
131 GtkWidget* delete_menu = gtk_menu_item_new_with_label("Delete");
132 gtk_menu_append(GTK_MENU(menu), delete_menu);
133 g_signal_connect(delete_menu,
134 "activate",
135 G_CALLBACK(OnDeleteMenuActivatedThunk),
136 this);
137 gtk_widget_set_sensitive(
138 delete_menu,
139 params_.edit_flags & WebContextMenuData::CanDelete);
141 GtkWidget* edit_separator = gtk_separator_menu_item_new();
142 gtk_menu_append(GTK_MENU(menu), edit_separator);
143 } else if (has_selection) {
144 GtkWidget* copy_menu = gtk_menu_item_new_with_label("Copy");
145 gtk_menu_append(GTK_MENU(menu), copy_menu);
146 g_signal_connect(copy_menu,
147 "activate",
148 G_CALLBACK(OnCopyMenuActivatedThunk),
149 this);
151 GtkWidget* copy_separator = gtk_separator_menu_item_new();
152 gtk_menu_append(GTK_MENU(menu), copy_separator);
155 GtkWidget* inspect_menu = gtk_menu_item_new_with_label("Inspect...");
156 gtk_menu_append(GTK_MENU(menu), inspect_menu);
157 g_signal_connect(inspect_menu,
158 "activate",
159 G_CALLBACK(OnInspectMenuActivatedThunk),
160 this);
162 gtk_widget_show_all(menu);
164 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, GDK_CURRENT_TIME);
167 WebDragDestDelegate* ShellWebContentsViewDelegate::GetDragDestDelegate() {
168 return NULL;
171 void ShellWebContentsViewDelegate::Initialize(GtkWidget* expanded_container,
172 ui::FocusStoreGtk* focus_store) {
173 expanded_container_ = expanded_container;
175 gtk_container_add(GTK_CONTAINER(floating_.get()), expanded_container_);
176 gtk_widget_show(floating_.get());
179 gfx::NativeView ShellWebContentsViewDelegate::GetNativeView() const {
180 return floating_.get();
183 void ShellWebContentsViewDelegate::Focus() {
184 GtkWidget* widget = web_contents_->GetView()->GetContentNativeView();
185 if (widget)
186 gtk_widget_grab_focus(widget);
189 gboolean ShellWebContentsViewDelegate::OnNativeViewFocusEvent(
190 GtkWidget* widget,
191 GtkDirectionType type,
192 gboolean* return_value) {
193 return false;
196 void ShellWebContentsViewDelegate::OnBackMenuActivated(GtkWidget* widget) {
197 web_contents_->GetController().GoToOffset(-1);
198 web_contents_->GetView()->Focus();
201 void ShellWebContentsViewDelegate::OnForwardMenuActivated(GtkWidget* widget) {
202 web_contents_->GetController().GoToOffset(1);
203 web_contents_->GetView()->Focus();
206 void ShellWebContentsViewDelegate::OnReloadMenuActivated(GtkWidget* widget) {
207 web_contents_->GetController().Reload(false);
208 web_contents_->GetView()->Focus();
211 void ShellWebContentsViewDelegate::OnOpenURLMenuActivated(GtkWidget* widget) {
212 ShellBrowserContext* browser_context =
213 ShellContentBrowserClient::Get()->browser_context();
214 Shell::CreateNewWindow(browser_context,
215 params_.link_url,
216 NULL,
217 MSG_ROUTING_NONE,
218 gfx::Size());
221 void ShellWebContentsViewDelegate::OnCutMenuActivated(GtkWidget* widget) {
222 web_contents_->GetRenderViewHost()->Cut();
225 void ShellWebContentsViewDelegate::OnCopyMenuActivated(GtkWidget* widget) {
226 web_contents_->GetRenderViewHost()->Copy();
229 void ShellWebContentsViewDelegate::OnPasteMenuActivated(GtkWidget* widget) {
230 web_contents_->GetRenderViewHost()->Paste();
233 void ShellWebContentsViewDelegate::OnDeleteMenuActivated(GtkWidget* widget) {
234 web_contents_->GetRenderViewHost()->Delete();
237 void ShellWebContentsViewDelegate::OnInspectMenuActivated(GtkWidget* widget) {
238 ShellDevToolsFrontend::Show(web_contents_);
241 } // namespace content