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 "chrome/browser/tab_contents/chrome_web_contents_view_gtk_delegate.h" | |
6 | |
7 #include "chrome/browser/browser_shutdown.h" | |
8 #include "chrome/browser/tab_contents/render_view_context_menu_gtk.h" | |
9 #include "chrome/browser/tab_contents/web_drag_bookmark_handler_gtk.h" | |
10 #include "chrome/browser/ui/gtk/constrained_window_gtk.h" | |
11 #include "content/browser/tab_contents/tab_contents_view_gtk.h" | |
12 #include "content/public/browser/render_process_host.h" | |
13 #include "content/public/browser/render_view_host.h" | |
14 #include "content/public/browser/render_widget_host_view.h" | |
15 #include "content/public/browser/web_contents.h" | |
16 #include "ui/base/gtk/gtk_floating_container.h" | |
17 | |
18 ChromeWebContentsViewGtkDelegate::ChromeWebContentsViewGtkDelegate() | |
19 : floating_(gtk_floating_container_new()), | |
20 view_(NULL), | |
21 constrained_window_(NULL) { | |
22 gtk_widget_set_name(floating_.get(), "chrome-tab-contents-wrapper-view"); | |
23 g_signal_connect(floating_.get(), "set-floating-position", | |
24 G_CALLBACK(OnSetFloatingPositionThunk), this); | |
25 } | |
26 | |
27 ChromeWebContentsViewGtkDelegate::~ChromeWebContentsViewGtkDelegate() { | |
28 floating_.Destroy(); | |
29 } | |
30 | |
31 void ChromeWebContentsViewGtkDelegate::AttachConstrainedWindow( | |
32 ConstrainedWindowGtk* constrained_window) { | |
33 DCHECK(constrained_window_ == NULL); | |
34 | |
35 constrained_window_ = constrained_window; | |
36 gtk_floating_container_add_floating(GTK_FLOATING_CONTAINER(floating_.get()), | |
37 constrained_window->widget()); | |
38 } | |
39 | |
40 void ChromeWebContentsViewGtkDelegate::RemoveConstrainedWindow( | |
41 ConstrainedWindowGtk* constrained_window) { | |
42 DCHECK(constrained_window == constrained_window_); | |
43 | |
44 constrained_window_ = NULL; | |
45 gtk_container_remove(GTK_CONTAINER(floating_.get()), | |
46 constrained_window->widget()); | |
47 } | |
48 | |
49 void ChromeWebContentsViewGtkDelegate::WrapView( | |
50 content::TabContentsViewGtk* view) { | |
51 view_ = view; | |
52 | |
53 gtk_container_add(GTK_CONTAINER(floating_.get()), | |
54 view_->expanded_container()); | |
55 gtk_widget_show(floating_.get()); | |
56 } | |
57 | |
58 gfx::NativeView ChromeWebContentsViewGtkDelegate::GetNativeView() const { | |
59 return floating_.get(); | |
60 } | |
61 | |
62 void ChromeWebContentsViewGtkDelegate::OnCreateViewForWidget() { | |
63 // We install a chrome specific handler to intercept bookmark drags for the | |
64 // bookmark manager/extension API. | |
65 bookmark_handler_gtk_.reset(new WebDragBookmarkHandlerGtk); | |
66 view_->SetDragDestDelegate(bookmark_handler_gtk_.get()); | |
67 } | |
68 | |
69 void ChromeWebContentsViewGtkDelegate::Focus() { | |
70 if (!constrained_window_) { | |
71 GtkWidget* widget = view_->GetContentNativeView(); | |
72 if (widget) | |
73 gtk_widget_grab_focus(widget); | |
74 } | |
75 } | |
76 | |
77 gboolean ChromeWebContentsViewGtkDelegate::OnNativeViewFocusEvent( | |
78 GtkWidget* widget, | |
79 GtkDirectionType type, | |
80 gboolean* return_value) { | |
81 // If we are showing a constrained window, don't allow the native view to take | |
82 // focus. | |
83 if (constrained_window_) { | |
84 // If we return false, it will revert to the default handler, which will | |
85 // take focus. We don't want that. But if we return true, the event will | |
86 // stop being propagated, leaving focus wherever it is currently. That is | |
87 // also bad. So we return false to let the default handler run, but take | |
88 // focus first so as to trick it into thinking the view was already focused | |
89 // and allowing the event to propagate. | |
90 gtk_widget_grab_focus(widget); | |
91 *return_value = FALSE; | |
92 return TRUE; | |
93 } | |
94 | |
95 // Let the default TabContentsViewGtk::OnFocus() behaviour run. | |
96 return FALSE; | |
97 } | |
98 | |
99 void ChromeWebContentsViewGtkDelegate::ShowContextMenu( | |
100 const content::ContextMenuParams& params) { | |
101 // Find out the RenderWidgetHostView that corresponds to the render widget on | |
102 // which this context menu is showed, so that we can retrieve the last mouse | |
103 // down event on the render widget and use it as the timestamp of the | |
104 // activation event to show the context menu. | |
105 content::RenderWidgetHostView* view = NULL; | |
106 if (params.custom_context.render_widget_id != | |
107 content::CustomContextMenuContext::kCurrentRenderWidget) { | |
108 IPC::Channel::Listener* listener = | |
109 view_->web_contents()->GetRenderProcessHost()->GetListenerByID( | |
110 params.custom_context.render_widget_id); | |
111 if (!listener) { | |
112 NOTREACHED(); | |
113 return; | |
114 } | |
115 view = | |
116 content::RenderWidgetHost::FromIPCChannelListener(listener)->GetView(); | |
117 } else { | |
118 view = view_->web_contents()->GetRenderWidgetHostView(); | |
119 } | |
120 | |
121 context_menu_.reset( | |
122 new RenderViewContextMenuGtk(view_->web_contents(), params, view)); | |
123 context_menu_->Init(); | |
124 | |
125 gfx::Rect bounds; | |
126 view_->GetContainerBounds(&bounds); | |
127 gfx::Point point = bounds.origin(); | |
128 point.Offset(params.x, params.y); | |
129 context_menu_->Popup(point); | |
130 } | |
131 | |
132 void ChromeWebContentsViewGtkDelegate::OnSetFloatingPosition( | |
133 GtkWidget* floating_container, GtkAllocation* allocation) { | |
134 if (!constrained_window_) | |
135 return; | |
136 | |
137 // Place each ConstrainedWindow in the center of the view. | |
138 GtkWidget* widget = constrained_window_->widget(); | |
139 DCHECK(gtk_widget_get_parent(widget) == floating_.get()); | |
140 | |
141 GtkRequisition requisition; | |
142 gtk_widget_size_request(widget, &requisition); | |
143 | |
144 GValue value = { 0, }; | |
145 g_value_init(&value, G_TYPE_INT); | |
146 | |
147 int child_x = std::max((allocation->width - requisition.width) / 2, 0); | |
148 g_value_set_int(&value, child_x); | |
149 gtk_container_child_set_property(GTK_CONTAINER(floating_container), | |
150 widget, "x", &value); | |
151 | |
152 int child_y = std::max((allocation->height - requisition.height) / 2, 0); | |
153 g_value_set_int(&value, child_y); | |
154 gtk_container_child_set_property(GTK_CONTAINER(floating_container), | |
155 widget, "y", &value); | |
156 g_value_unset(&value); | |
157 } | |
OLD | NEW |