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