| 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/native_view_host.h" | 5 #include "views/controls/native/native_view_host.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "gfx/canvas.h" | 8 #include "gfx/canvas.h" |
| 9 #include "views/controls/native/native_view_host_wrapper.h" | 9 #include "views/controls/native/native_view_host_wrapper.h" |
| 10 #include "views/controls/native/native_view_host_views.h" |
| 10 #include "views/widget/root_view.h" | 11 #include "views/widget/root_view.h" |
| 11 #include "views/widget/widget.h" | 12 #include "views/widget/widget.h" |
| 12 | 13 |
| 13 namespace views { | 14 namespace views { |
| 14 | 15 |
| 15 // static | 16 // static |
| 16 const char NativeViewHost::kViewClassName[] = "views/NativeViewHost"; | 17 const char NativeViewHost::kViewClassName[] = "views/NativeViewHost"; |
| 17 | 18 |
| 18 #if defined(OS_LINUX) | 19 #if defined(OS_LINUX) |
| 19 // GTK renders the focus. | 20 // GTK renders the focus. |
| 20 // static | 21 // static |
| 21 const bool NativeViewHost::kRenderNativeControlFocus = false; | 22 const bool NativeViewHost::kRenderNativeControlFocus = false; |
| 22 #else | 23 #else |
| 23 // static | 24 // static |
| 24 const bool NativeViewHost::kRenderNativeControlFocus = true; | 25 const bool NativeViewHost::kRenderNativeControlFocus = true; |
| 25 #endif | 26 #endif |
| 26 | 27 |
| 27 //////////////////////////////////////////////////////////////////////////////// | 28 //////////////////////////////////////////////////////////////////////////////// |
| 28 // NativeViewHost, public: | 29 // NativeViewHost, public: |
| 29 | 30 |
| 30 NativeViewHost::NativeViewHost() | 31 NativeViewHost::NativeViewHost() |
| 31 : native_view_(NULL), | 32 : native_view_(NULL), |
| 33 views_view_(NULL), |
| 32 fast_resize_(false), | 34 fast_resize_(false), |
| 33 focus_view_(NULL) { | 35 focus_view_(NULL) { |
| 34 // The native widget is placed relative to the root. As such, we need to | 36 // The native widget is placed relative to the root. As such, we need to |
| 35 // know when the position of any ancestor changes, or our visibility relative | 37 // know when the position of any ancestor changes, or our visibility relative |
| 36 // to other views changed as it'll effect our position relative to the root. | 38 // to other views changed as it'll effect our position relative to the root. |
| 37 SetNotifyWhenVisibleBoundsInRootChanges(true); | 39 SetNotifyWhenVisibleBoundsInRootChanges(true); |
| 38 } | 40 } |
| 39 | 41 |
| 40 NativeViewHost::~NativeViewHost() { | 42 NativeViewHost::~NativeViewHost() { |
| 41 } | 43 } |
| 42 | 44 |
| 43 void NativeViewHost::Attach(gfx::NativeView native_view) { | 45 void NativeViewHost::Attach(gfx::NativeView native_view) { |
| 44 DCHECK(native_view); | 46 DCHECK(native_view); |
| 45 DCHECK(!native_view_); | 47 DCHECK(!native_view_); |
| 48 DCHECK(!views_view_); |
| 46 native_view_ = native_view; | 49 native_view_ = native_view; |
| 47 // If set_focus_view() has not been invoked, this view is the one that should | 50 // If set_focus_view() has not been invoked, this view is the one that should |
| 48 // be seen as focused when the native view receives focus. | 51 // be seen as focused when the native view receives focus. |
| 49 if (!focus_view_) | 52 if (!focus_view_) |
| 50 focus_view_ = this; | 53 focus_view_ = this; |
| 51 native_wrapper_->NativeViewAttached(); | 54 native_wrapper_->NativeViewAttached(); |
| 52 } | 55 } |
| 53 | 56 |
| 57 void NativeViewHost::AttachToView(View* view) { |
| 58 if (view == views_view_) |
| 59 return; |
| 60 DCHECK(view); |
| 61 DCHECK(!native_view_); |
| 62 DCHECK(!views_view_); |
| 63 native_wrapper_.reset(new NativeViewHostViews(this)); |
| 64 views_view_ = view; |
| 65 // If set_focus_view() has not been invoked, this view is the one that should |
| 66 // be seen as focused when the native view receives focus. |
| 67 if (!focus_view_) |
| 68 focus_view_ = this; |
| 69 native_wrapper_->NativeViewAttached(); |
| 70 } |
| 71 |
| 54 void NativeViewHost::Detach() { | 72 void NativeViewHost::Detach() { |
| 55 Detach(false); | 73 Detach(false); |
| 56 } | 74 } |
| 57 | 75 |
| 58 void NativeViewHost::SetPreferredSize(const gfx::Size& size) { | 76 void NativeViewHost::SetPreferredSize(const gfx::Size& size) { |
| 59 preferred_size_ = size; | 77 preferred_size_ = size; |
| 60 PreferredSizeChanged(); | 78 PreferredSizeChanged(); |
| 61 } | 79 } |
| 62 | 80 |
| 63 void NativeViewHost::NativeViewDestroyed() { | 81 void NativeViewHost::NativeViewDestroyed() { |
| 64 // Detach so we can clear our state and notify the native_wrapper_ to release | 82 // Detach so we can clear our state and notify the native_wrapper_ to release |
| 65 // ref on the native view. | 83 // ref on the native view. |
| 66 Detach(true); | 84 Detach(true); |
| 67 } | 85 } |
| 68 | 86 |
| 69 //////////////////////////////////////////////////////////////////////////////// | 87 //////////////////////////////////////////////////////////////////////////////// |
| 70 // NativeViewHost, View overrides: | 88 // NativeViewHost, View overrides: |
| 71 | 89 |
| 72 gfx::Size NativeViewHost::GetPreferredSize() { | 90 gfx::Size NativeViewHost::GetPreferredSize() { |
| 73 return preferred_size_; | 91 return preferred_size_; |
| 74 } | 92 } |
| 75 | 93 |
| 76 void NativeViewHost::Layout() { | 94 void NativeViewHost::Layout() { |
| 77 if (!native_view_ || !native_wrapper_.get()) | 95 if ((!native_view_ && !views_view_) || !native_wrapper_.get()) |
| 78 return; | 96 return; |
| 79 | 97 |
| 80 gfx::Rect vis_bounds = GetVisibleBounds(); | 98 gfx::Rect vis_bounds = GetVisibleBounds(); |
| 81 bool visible = !vis_bounds.IsEmpty(); | 99 bool visible = !vis_bounds.IsEmpty(); |
| 82 | 100 |
| 83 if (visible && !fast_resize_) { | 101 if (visible && !fast_resize_) { |
| 84 if (vis_bounds.size() != size()) { | 102 if (vis_bounds.size() != size()) { |
| 85 // Only a portion of the Widget is really visible. | 103 // Only a portion of the Widget is really visible. |
| 86 int x = vis_bounds.x(); | 104 int x = vis_bounds.x(); |
| 87 int y = vis_bounds.y(); | 105 int y = vis_bounds.y(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 return kViewClassName; | 175 return kViewClassName; |
| 158 } | 176 } |
| 159 | 177 |
| 160 void NativeViewHost::Focus() { | 178 void NativeViewHost::Focus() { |
| 161 native_wrapper_->SetFocus(); | 179 native_wrapper_->SetFocus(); |
| 162 } | 180 } |
| 163 | 181 |
| 164 bool NativeViewHost::ContainsNativeView(gfx::NativeView native_view) const { | 182 bool NativeViewHost::ContainsNativeView(gfx::NativeView native_view) const { |
| 165 if (native_view == native_view_) | 183 if (native_view == native_view_) |
| 166 return true; | 184 return true; |
| 185 if (!native_view_) |
| 186 return false; |
| 167 | 187 |
| 168 views::Widget* native_widget = | 188 views::Widget* native_widget = |
| 169 views::Widget::GetWidgetFromNativeView(native_view_); | 189 views::Widget::GetWidgetFromNativeView(native_view_); |
| 170 views::RootView* root_view = | 190 views::RootView* root_view = |
| 171 native_widget ? native_widget->GetRootView() : NULL; | 191 native_widget ? native_widget->GetRootView() : NULL; |
| 172 if (root_view && root_view->ContainsNativeView(native_view)) | 192 if (root_view && root_view->ContainsNativeView(native_view)) |
| 173 return true; | 193 return true; |
| 174 | 194 |
| 175 return View::ContainsNativeView(native_view); | 195 return View::ContainsNativeView(native_view); |
| 176 } | 196 } |
| 177 | 197 |
| 178 //////////////////////////////////////////////////////////////////////////////// | 198 //////////////////////////////////////////////////////////////////////////////// |
| 179 // NativeViewHost, private: | 199 // NativeViewHost, private: |
| 180 | 200 |
| 181 void NativeViewHost::Detach(bool destroyed) { | 201 void NativeViewHost::Detach(bool destroyed) { |
| 182 DCHECK(native_view_); | 202 DCHECK(native_view_ || views_view_); |
| 183 native_wrapper_->NativeViewDetaching(destroyed); | 203 native_wrapper_->NativeViewDetaching(destroyed); |
| 184 native_view_ = NULL; | 204 native_view_ = NULL; |
| 205 views_view_ = NULL; |
| 185 } | 206 } |
| 186 | 207 |
| 187 } // namespace views | 208 } // namespace views |
| OLD | NEW |