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_tab_contents_view_wrapper_gtk.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/renderer_host/render_view_host.h" | |
12 #include "content/browser/tab_contents/tab_contents_view_gtk.h" | |
13 #include "content/public/browser/render_process_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 ChromeTabContentsViewWrapperGtk::ChromeTabContentsViewWrapperGtk() | |
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 ChromeTabContentsViewWrapperGtk::~ChromeTabContentsViewWrapperGtk() { | |
28 floating_.Destroy(); | |
29 } | |
30 | |
31 void ChromeTabContentsViewWrapperGtk::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 ChromeTabContentsViewWrapperGtk::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 ChromeTabContentsViewWrapperGtk::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 ChromeTabContentsViewWrapperGtk::GetNativeView() const { | |
59 return floating_.get(); | |
60 } | |
61 | |
62 void ChromeTabContentsViewWrapperGtk::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 ChromeTabContentsViewWrapperGtk::Focus() { | |
70 if (!constrained_window_) { | |
71 GtkWidget* widget = view_->GetContentNativeView(); | |
72 if (widget) | |
73 gtk_widget_grab_focus(widget); | |
74 } | |
75 } | |
76 | |
77 gboolean ChromeTabContentsViewWrapperGtk::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 ChromeTabContentsViewWrapperGtk::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 = RenderWidgetHost::FromIPCChannelListener(listener)->view(); | |
116 } else { | |
117 view = view_->web_contents()->GetRenderWidgetHostView(); | |
118 } | |
119 | |
120 context_menu_.reset( | |
121 new RenderViewContextMenuGtk(view_->web_contents(), params, view)); | |
122 context_menu_->Init(); | |
123 | |
124 gfx::Rect bounds; | |
125 view_->GetContainerBounds(&bounds); | |
126 gfx::Point point = bounds.origin(); | |
127 point.Offset(params.x, params.y); | |
128 context_menu_->Popup(point); | |
129 } | |
130 | |
131 void ChromeTabContentsViewWrapperGtk::OnSetFloatingPosition( | |
132 GtkWidget* floating_container, GtkAllocation* allocation) { | |
133 if (!constrained_window_) | |
134 return; | |
135 | |
136 // Place each ConstrainedWindow in the center of the view. | |
137 GtkWidget* widget = constrained_window_->widget(); | |
138 DCHECK(gtk_widget_get_parent(widget) == floating_.get()); | |
139 | |
140 GtkRequisition requisition; | |
141 gtk_widget_size_request(widget, &requisition); | |
142 | |
143 GValue value = { 0, }; | |
144 g_value_init(&value, G_TYPE_INT); | |
145 | |
146 int child_x = std::max((allocation->width - requisition.width) / 2, 0); | |
147 g_value_set_int(&value, child_x); | |
148 gtk_container_child_set_property(GTK_CONTAINER(floating_container), | |
149 widget, "x", &value); | |
150 | |
151 int child_y = std::max((allocation->height - requisition.height) / 2, 0); | |
152 g_value_set_int(&value, child_y); | |
153 gtk_container_child_set_property(GTK_CONTAINER(floating_container), | |
154 widget, "y", &value); | |
155 g_value_unset(&value); | |
156 } | |
OLD | NEW |