| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "views/controls/native_view_host_gtk.h" | 5 #include "views/controls/native/native_view_host_gtk.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "views/controls/native/native_view_host.h" |
| 10 #include "views/widget/widget_gtk.h" | 11 #include "views/widget/widget_gtk.h" |
| 11 | 12 |
| 12 namespace views { | 13 namespace views { |
| 13 | 14 |
| 14 NativeViewHostGtk::NativeViewHostGtk() | 15 //////////////////////////////////////////////////////////////////////////////// |
| 15 : NativeViewHost(), | 16 // NativeViewHostGtk, public: |
| 17 |
| 18 NativeViewHostWin::NativeViewHostWin(NativeViewHost* host) |
| 19 : host_(host), |
| 20 installed_clip_(false), |
| 16 destroy_signal_id_(0) { | 21 destroy_signal_id_(0) { |
| 17 } | 22 } |
| 18 | 23 |
| 19 NativeViewHostGtk::~NativeViewHostGtk() { | 24 NativeViewHostGtk::~NativeViewHostGtk() { |
| 20 } | 25 } |
| 21 | 26 |
| 22 // static | 27 // static |
| 23 View* NativeViewHostGtk::GetViewForNative(GtkWidget* widget) { | 28 View* NativeViewHostGtk::GetViewForNative(GtkWidget* widget) { |
| 24 gpointer user_data = g_object_get_data(G_OBJECT(widget), "chrome-view"); | 29 gpointer user_data = g_object_get_data(G_OBJECT(widget), "chrome-view"); |
| 25 return static_cast<View*>(user_data); | 30 return static_cast<View*>(user_data); |
| 26 } | 31 } |
| 27 | 32 |
| 28 // static | 33 // static |
| 29 void NativeViewHostGtk::SetViewForNative(GtkWidget* widget, View* view) { | 34 void NativeViewHostGtk::SetViewForNative(GtkWidget* widget, View* view) { |
| 30 g_object_set_data(G_OBJECT(widget), "chrome-view", view); | 35 g_object_set_data(G_OBJECT(widget), "chrome-view", view); |
| 31 } | 36 } |
| 32 | 37 |
| 33 void NativeViewHostGtk::Attach(GtkWidget* widget) { | 38 //////////////////////////////////////////////////////////////////////////////// |
| 34 DCHECK(native_view() == NULL); | 39 // NativeViewHostGtk, NativeViewHostWrapper implementation: |
| 35 DCHECK(widget); | 40 |
| 41 void NativeViewHostGtk::NativeViewAttached() { |
| 42 DCHECK(host_->native_view()); |
| 36 | 43 |
| 37 // Adds a mapping between the GtkWidget and us. | 44 // Adds a mapping between the GtkWidget and us. |
| 38 SetViewForNative(widget, this); | 45 SetViewForNative(host_->native_view(), host_); |
| 39 | 46 |
| 40 destroy_signal_id_ = g_signal_connect(G_OBJECT(widget), "destroy", | 47 destroy_signal_id_ = g_signal_connect(G_OBJECT(host_->native_view()), |
| 41 G_CALLBACK(CallDestroy), NULL); | 48 "destroy", G_CALLBACK(CallDestroy), |
| 42 | 49 NULL); |
| 43 set_native_view(widget); | |
| 44 | 50 |
| 45 // First hide the new window. We don't want anything to draw (like sub-hwnd | 51 // First hide the new window. We don't want anything to draw (like sub-hwnd |
| 46 // borders), when we change the parent below. | 52 // borders), when we change the parent below. |
| 47 gtk_widget_hide(widget); | 53 gtk_widget_hide(host_->native_view()); |
| 48 | 54 |
| 49 // Set the parent. | 55 // Set the parent. |
| 50 static_cast<WidgetGtk*>(GetWidget())->AddChild(widget); | 56 static_cast<WidgetGtk*>(host_->GetWidget())->AddChild(host_->native_view()); |
| 51 Layout(); | 57 host_->Layout(); |
| 52 | 58 |
| 53 // TODO: figure out focus. | 59 // TODO(port): figure out focus. |
| 54 // FocusManager::InstallFocusSubclass( | 60 // FocusManager::InstallFocusSubclass( |
| 55 // hwnd, associated_focus_view()_ ? associated_focus_view() : this); | 61 // hwnd, associated_focus_view()_ ? associated_focus_view() : this); |
| 56 } | 62 } |
| 57 | 63 |
| 58 void NativeViewHostGtk::Detach() { | 64 void NativeViewHostGtk::NativeViewDetaching() { |
| 59 DCHECK(native_view()); | 65 DCHECK(host_->native_view()); |
| 60 | 66 |
| 61 g_signal_handler_disconnect(G_OBJECT(native_view()), destroy_signal_id_); | 67 g_signal_handler_disconnect(G_OBJECT(host_->native_view()), |
| 68 destroy_signal_id_); |
| 62 destroy_signal_id_ = 0; | 69 destroy_signal_id_ = 0; |
| 63 | 70 |
| 64 // TODO: focus. | 71 // TODO(port): focus. |
| 65 // FocusManager::UninstallFocusSubclass(native_view()); | 72 // FocusManager::UninstallFocusSubclass(native_view()); |
| 66 set_native_view(NULL); | 73 installed_clip_ = false; |
| 67 set_installed_clip(false); | |
| 68 } | 74 } |
| 69 | 75 |
| 70 void NativeViewHostGtk::ViewHierarchyChanged(bool is_add, View* parent, | 76 void NativeViewHostGtk::AddedToWidget() { |
| 71 View* child) { | 77 WidgetGtk* parent_widget = static_cast<WidgetGtk*>(host_->GetWidget()); |
| 72 if (!native_view()) | 78 GtkWidget* widget_parent = gtk_widget_get_parent(host_->native_view()); |
| 73 return; | 79 GtkWidget* parent_widget_widget = parent_widget->child_widget_parent(); |
| 74 | 80 if (widget_parent != parent_widget_widget) { |
| 75 WidgetGtk* parent_widget = static_cast<WidgetGtk*>(GetWidget()); | 81 g_object_ref(host_->native_view()); |
| 76 if (is_add && parent_widget) { | 82 if (widget_parent) |
| 77 GtkWidget* widget_parent = gtk_widget_get_parent(native_view()); | 83 gtk_container_remove(GTK_CONTAINER(widget_parent), host_->native_view()); |
| 78 GtkWidget* parent_widget_widget = parent_widget->child_widget_parent(); | 84 gtk_container_add(GTK_CONTAINER(parent_widget_widget), |
| 79 if (widget_parent != parent_widget_widget) { | 85 host_->native_view()); |
| 80 g_object_ref(native_view()); | 86 g_object_unref(host_->native_view()); |
| 81 if (widget_parent) | |
| 82 gtk_container_remove(GTK_CONTAINER(widget_parent), native_view()); | |
| 83 gtk_container_add(GTK_CONTAINER(parent_widget_widget), native_view()); | |
| 84 g_object_unref(native_view()); | |
| 85 } | |
| 86 if (IsVisibleInRootView()) | |
| 87 gtk_widget_show(native_view()); | |
| 88 else | |
| 89 gtk_widget_hide(native_view()); | |
| 90 Layout(); | |
| 91 } else if (!is_add) { | |
| 92 gtk_widget_hide(native_view()); | |
| 93 if (parent_widget) { | |
| 94 gtk_container_remove(GTK_CONTAINER(parent_widget->child_widget_parent()), | |
| 95 native_view()); | |
| 96 } | |
| 97 } | 87 } |
| 88 if (host_->IsVisibleInRootView()) |
| 89 gtk_widget_show(host_->native_view()); |
| 90 else |
| 91 gtk_widget_hide(host_->native_view()); |
| 92 host_->Layout(); |
| 98 } | 93 } |
| 99 | 94 |
| 100 void NativeViewHostGtk::Focus() { | 95 void NativeViewHostGtk::RemovedFromWidget() { |
| 101 NOTIMPLEMENTED(); | 96 WidgetGtk* parent_widget = static_cast<WidgetGtk*>(host_->GetWidget()); |
| 97 gtk_widget_hide(host_->native_view()); |
| 98 if (parent_widget) { |
| 99 gtk_container_remove(GTK_CONTAINER(parent_widget->child_widget_parent()), |
| 100 host_->native_view()); |
| 101 } |
| 102 } | 102 } |
| 103 | 103 |
| 104 void NativeViewHostGtk::InstallClip(int x, int y, int w, int h) { | 104 void NativeViewHostGtk::InstallClip(int x, int y, int w, int h) { |
| 105 DCHECK(w > 0 && h > 0); | 105 DCHECK(w > 0 && h > 0); |
| 106 | 106 |
| 107 bool has_window = (GTK_WIDGET_FLAGS(native_view()) & GTK_NO_WINDOW) == 0; | 107 bool has_window = |
| 108 (GTK_WIDGET_FLAGS(host_->native_view()) & GTK_NO_WINDOW) == 0; |
| 108 if (!has_window) { | 109 if (!has_window) { |
| 109 // Clip is only supported on GtkWidgets that have windows. If this becomes | 110 // Clip is only supported on GtkWidgets that have windows. If this becomes |
| 110 // an issue (as it may be in the options dialog) we'll need to wrap the | 111 // an issue (as it may be in the options dialog) we'll need to wrap the |
| 111 // widget in a GtkFixed with a window. We have to do this as not all widgets | 112 // widget in a GtkFixed with a window. We have to do this as not all widgets |
| 112 // support turning on GTK_NO_WINDOW (for example, buttons don't appear to | 113 // support turning on GTK_NO_WINDOW (for example, buttons don't appear to |
| 113 // draw anything when they have a window). | 114 // draw anything when they have a window). |
| 114 NOTREACHED(); | 115 NOTREACHED(); |
| 115 return; | 116 return; |
| 116 } | 117 } |
| 117 DCHECK(has_window); | 118 DCHECK(has_window); |
| 118 // Unset the current region. | 119 // Unset the current region. |
| 119 gdk_window_shape_combine_region(native_view()->window, NULL, 0, 0); | 120 gdk_window_shape_combine_region(host_->native_view()->window, NULL, 0, 0); |
| 120 | 121 |
| 121 // Set a new region. | 122 // Set a new region. |
| 122 // TODO: using shapes is a bit expensive. Should investigated if there is | 123 // TODO: using shapes is a bit expensive. Should investigated if there is |
| 123 // another more efficient way to accomplish this. | 124 // another more efficient way to accomplish this. |
| 124 GdkRectangle clip_rect = { x, y, w, h }; | 125 GdkRectangle clip_rect = { x, y, w, h }; |
| 125 GdkRegion* clip_region = gdk_region_rectangle(&clip_rect); | 126 GdkRegion* clip_region = gdk_region_rectangle(&clip_rect); |
| 126 gdk_window_shape_combine_region(native_view()->window, clip_region, x, y); | 127 gdk_window_shape_combine_region(host_->native_view()->window, clip_region, x, |
| 128 y); |
| 127 gdk_region_destroy(clip_region); | 129 gdk_region_destroy(clip_region); |
| 130 installed_clip_ = true; |
| 128 } | 131 } |
| 129 | 132 |
| 130 void NativeViewHostGtk::UninstallClip() { | 133 void NativeViewHostGtk::UninstallClip() { |
| 131 gtk_widget_shape_combine_mask(native_view(), NULL, 0, 0); | 134 gtk_widget_shape_combine_mask(host_->native_view(), NULL, 0, 0); |
| 135 installed_clip_ = false; |
| 132 } | 136 } |
| 133 | 137 |
| 134 void NativeViewHostGtk::ShowWidget(int x, int y, int w, int h) { | 138 void NativeViewHostGtk::ShowWidget(int x, int y, int w, int h) { |
| 135 WidgetGtk* parent = static_cast<WidgetGtk*>(GetWidget()); | 139 WidgetGtk* parent = static_cast<WidgetGtk*>(host_->GetWidget()); |
| 136 parent->PositionChild(native_view(), x, y, w, h); | 140 parent->PositionChild(host_->native_view(), x, y, w, h); |
| 137 gtk_widget_show(native_view()); | 141 gtk_widget_show(host_->native_view()); |
| 138 } | 142 } |
| 139 | 143 |
| 140 void NativeViewHostGtk::HideWidget() { | 144 void NativeViewHostGtk::HideWidget() { |
| 141 gtk_widget_hide(native_view()); | 145 gtk_widget_hide(host_->native_view()); |
| 142 } | 146 } |
| 143 | 147 |
| 144 void NativeViewHostGtk::OnDestroy() { | 148 void NativeViewHostGtk::SetFocus() { |
| 145 set_native_view(NULL); | 149 NOTIMPLEMENTED(); |
| 146 } | 150 } |
| 147 | 151 |
| 152 //////////////////////////////////////////////////////////////////////////////// |
| 153 // NativeViewHostGtk, private: |
| 154 |
| 148 // static | 155 // static |
| 149 void NativeViewHostGtk::CallDestroy(GtkObject* object) { | 156 void NativeViewHostGtk::CallDestroy(GtkObject* object) { |
| 150 View* view = GetViewForNative(GTK_WIDGET(object)); | 157 View* view = GetViewForNative(GTK_WIDGET(object)); |
| 151 if (!view) | 158 if (!view) |
| 152 return; | 159 return; |
| 153 | 160 |
| 154 return static_cast<NativeViewHostGtk*>(view)->OnDestroy(); | 161 return static_cast<NativeViewHost*>(view)->NativeViewDestroyed(); |
| 162 } |
| 163 |
| 164 //////////////////////////////////////////////////////////////////////////////// |
| 165 // NativeViewHostWrapper, public: |
| 166 |
| 167 // static |
| 168 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( |
| 169 NativeViewHost* host) { |
| 170 return new NativeViewHostGtk(host); |
| 155 } | 171 } |
| 156 | 172 |
| 157 } // namespace views | 173 } // namespace views |
| OLD | NEW |