| 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.h" | 5 #include "views/controls/native/native_view_host.h" |
| 6 | 6 |
| 7 #include "base/logging.h" |
| 8 #include "app/gfx/canvas.h" |
| 9 #include "views/controls/native/native_view_host_wrapper.h" |
| 7 #include "views/widget/widget.h" | 10 #include "views/widget/widget.h" |
| 8 #include "base/logging.h" | |
| 9 | 11 |
| 10 namespace views { | 12 namespace views { |
| 11 | 13 |
| 14 // static |
| 15 const char NativeViewHost::kViewClassName[] = "views/NativeViewHost"; |
| 16 |
| 17 //////////////////////////////////////////////////////////////////////////////// |
| 18 // NativeViewHost, public: |
| 19 |
| 12 NativeViewHost::NativeViewHost() | 20 NativeViewHost::NativeViewHost() |
| 13 : native_view_(NULL), | 21 : native_view_(NULL), |
| 14 installed_clip_(false), | 22 native_wrapper_(NULL), |
| 15 fast_resize_(false), | 23 fast_resize_(false), |
| 16 focus_view_(NULL) { | 24 focus_view_(NULL) { |
| 17 // The native widget is placed relative to the root. As such, we need to | 25 // The native widget is placed relative to the root. As such, we need to |
| 18 // know when the position of any ancestor changes, or our visibility relative | 26 // know when the position of any ancestor changes, or our visibility relative |
| 19 // to other views changed as it'll effect our position relative to the root. | 27 // to other views changed as it'll effect our position relative to the root. |
| 20 SetNotifyWhenVisibleBoundsInRootChanges(true); | 28 SetNotifyWhenVisibleBoundsInRootChanges(true); |
| 21 } | 29 } |
| 22 | 30 |
| 23 NativeViewHost::~NativeViewHost() { | 31 NativeViewHost::~NativeViewHost() { |
| 24 } | 32 } |
| 25 | 33 |
| 26 gfx::Size NativeViewHost::GetPreferredSize() { | 34 void NativeViewHost::Attach(gfx::NativeView native_view) { |
| 27 return preferred_size_; | 35 DCHECK(!native_view_); |
| 36 native_view_ = native_view; |
| 37 native_wrapper_->NativeViewAttached(); |
| 38 } |
| 39 |
| 40 void NativeViewHost::Detach() { |
| 41 DCHECK(native_view_); |
| 42 native_wrapper_->NativeViewDetaching(); |
| 43 native_view_ = NULL; |
| 28 } | 44 } |
| 29 | 45 |
| 30 void NativeViewHost::SetPreferredSize(const gfx::Size& size) { | 46 void NativeViewHost::SetPreferredSize(const gfx::Size& size) { |
| 31 preferred_size_ = size; | 47 preferred_size_ = size; |
| 32 PreferredSizeChanged(); | 48 PreferredSizeChanged(); |
| 33 } | 49 } |
| 34 | 50 |
| 51 void NativeViewHost::NativeViewDestroyed() { |
| 52 // TODO(beng): figure out if this should/could call Detach instead since as it |
| 53 // stands right now this object is left in an inconsistent state. |
| 54 native_view_ = NULL; |
| 55 } |
| 56 |
| 57 //////////////////////////////////////////////////////////////////////////////// |
| 58 // NativeViewHost, View overrides: |
| 59 |
| 60 gfx::Size NativeViewHost::GetPreferredSize() { |
| 61 return preferred_size_; |
| 62 } |
| 63 |
| 35 void NativeViewHost::Layout() { | 64 void NativeViewHost::Layout() { |
| 36 if (!native_view_) | 65 if (!native_view_ || !native_wrapper_) |
| 37 return; | 66 return; |
| 38 | 67 |
| 39 // Since widgets know nothing about the View hierarchy (they are direct | 68 // Since widgets know nothing about the View hierarchy (they are direct |
| 40 // children of the Widget that hosts our View hierarchy) they need to be | 69 // children of the Widget that hosts our View hierarchy) they need to be |
| 41 // positioned in the coordinate system of the Widget, not the current | 70 // positioned in the coordinate system of the Widget, not the current |
| 42 // view. | 71 // view. |
| 43 gfx::Point top_left; | 72 gfx::Point top_left; |
| 44 ConvertPointToWidget(this, &top_left); | 73 ConvertPointToWidget(this, &top_left); |
| 45 | 74 |
| 46 gfx::Rect vis_bounds = GetVisibleBounds(); | 75 gfx::Rect vis_bounds = GetVisibleBounds(); |
| 47 bool visible = !vis_bounds.IsEmpty(); | 76 bool visible = !vis_bounds.IsEmpty(); |
| 48 | 77 |
| 49 if (visible && !fast_resize_) { | 78 if (visible && !fast_resize_ && native_wrapper_) { |
| 50 if (vis_bounds.size() != size()) { | 79 if (vis_bounds.size() != size()) { |
| 51 // Only a portion of the Widget is really visible. | 80 // Only a portion of the Widget is really visible. |
| 52 int x = vis_bounds.x(); | 81 int x = vis_bounds.x(); |
| 53 int y = vis_bounds.y(); | 82 int y = vis_bounds.y(); |
| 54 InstallClip(x, y, vis_bounds.width(), vis_bounds.height()); | 83 native_wrapper_->InstallClip(x, y, vis_bounds.width(), |
| 55 installed_clip_ = true; | 84 vis_bounds.height()); |
| 56 } else if (installed_clip_) { | 85 } else if (native_wrapper_->HasInstalledClip()) { |
| 57 // The whole widget is visible but we installed a clip on the widget, | 86 // The whole widget is visible but we installed a clip on the widget, |
| 58 // uninstall it. | 87 // uninstall it. |
| 59 UninstallClip(); | 88 native_wrapper_->UninstallClip(); |
| 60 installed_clip_ = false; | |
| 61 } | 89 } |
| 62 } | 90 } |
| 63 | 91 |
| 64 if (visible) { | 92 if (visible) |
| 65 ShowWidget(top_left.x(), top_left.y(), width(), height()); | 93 native_wrapper_->ShowWidget(top_left.x(), top_left.y(), width(), height()); |
| 66 } else { | 94 else |
| 67 HideWidget(); | 95 native_wrapper_->HideWidget(); |
| 68 } | 96 } |
| 97 |
| 98 void NativeViewHost::Paint(gfx::Canvas* canvas) { |
| 99 // The area behind our window is black, so during a fast resize (where our |
| 100 // content doesn't draw over the full size of our native view, and the native |
| 101 // view background color doesn't show up), we need to cover that blackness |
| 102 // with something so that fast resizes don't result in black flash. |
| 103 // |
| 104 // It would be nice if this used some approximation of the page's |
| 105 // current background color. |
| 106 if (native_wrapper_->HasInstalledClip()) |
| 107 canvas->FillRectInt(SK_ColorWHITE, 0, 0, width(), height()); |
| 69 } | 108 } |
| 70 | 109 |
| 71 void NativeViewHost::VisibilityChanged(View* starting_from, bool is_visible) { | 110 void NativeViewHost::VisibilityChanged(View* starting_from, bool is_visible) { |
| 72 Layout(); | 111 Layout(); |
| 73 } | 112 } |
| 74 | 113 |
| 75 void NativeViewHost::VisibleBoundsInRootChanged() { | 114 void NativeViewHost::VisibleBoundsInRootChanged() { |
| 76 Layout(); | 115 Layout(); |
| 77 } | 116 } |
| 78 | 117 |
| 118 void NativeViewHost::ViewHierarchyChanged(bool is_add, View* parent, |
| 119 View* child) { |
| 120 if (is_add && GetWidget()) { |
| 121 if (!native_wrapper_) |
| 122 native_wrapper_ = NativeViewHostWrapper::CreateWrapper(this); |
| 123 native_wrapper_->AddedToWidget(); |
| 124 } else if (!is_add) { |
| 125 native_wrapper_->RemovedFromWidget(); |
| 126 } |
| 127 } |
| 128 |
| 129 std::string NativeViewHost::GetClassName() const { |
| 130 return kViewClassName; |
| 131 } |
| 132 |
| 133 void NativeViewHost::Focus() { |
| 134 native_wrapper_->SetFocus(); |
| 135 } |
| 136 |
| 79 } // namespace views | 137 } // namespace views |
| OLD | NEW |