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 "app/gfx/canvas.h" | 8 #include "app/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/focus/focus_manager.h" |
10 #include "views/widget/widget.h" | 11 #include "views/widget/widget.h" |
11 | 12 |
12 namespace views { | 13 namespace views { |
13 | 14 |
14 // static | 15 // static |
15 const char NativeViewHost::kViewClassName[] = "views/NativeViewHost"; | 16 const char NativeViewHost::kViewClassName[] = "views/NativeViewHost"; |
16 | 17 |
17 //////////////////////////////////////////////////////////////////////////////// | 18 //////////////////////////////////////////////////////////////////////////////// |
18 // NativeViewHost, public: | 19 // NativeViewHost, public: |
19 | 20 |
20 NativeViewHost::NativeViewHost() | 21 NativeViewHost::NativeViewHost() |
21 : native_view_(NULL), | 22 : native_view_(NULL), |
22 fast_resize_(false), | 23 fast_resize_(false), |
23 focus_view_(NULL) { | 24 focus_native_view_(NULL) { |
| 25 // By default we make this view the one that should be set as the focused view |
| 26 // when the native view gets the focus. |
| 27 focus_view_ = this; |
24 // The native widget is placed relative to the root. As such, we need to | 28 // The native widget is placed relative to the root. As such, we need to |
25 // know when the position of any ancestor changes, or our visibility relative | 29 // know when the position of any ancestor changes, or our visibility relative |
26 // to other views changed as it'll effect our position relative to the root. | 30 // to other views changed as it'll effect our position relative to the root. |
27 SetNotifyWhenVisibleBoundsInRootChanges(true); | 31 SetNotifyWhenVisibleBoundsInRootChanges(true); |
28 } | 32 } |
29 | 33 |
30 NativeViewHost::~NativeViewHost() { | 34 NativeViewHost::~NativeViewHost() { |
31 } | 35 } |
32 | 36 |
33 void NativeViewHost::Attach(gfx::NativeView native_view) { | 37 void NativeViewHost::Attach(gfx::NativeView native_view) { |
34 DCHECK(!native_view_); | 38 DCHECK(!native_view_); |
35 native_view_ = native_view; | 39 native_view_ = native_view; |
36 // If set_focus_view() has not been invoked, this view is the one that should | 40 // If this NativeViewHost is the focus view, than it should obviously be |
37 // be seen as focused when the native view receives focus. | 41 // focusable. If it is not, then it acts as a container and we don't want it |
38 if (!focus_view_) | 42 // to get focus through tab-traversal, so we make it not focusable. |
39 focus_view_ = this; | 43 SetFocusable(focus_view_ == this); |
| 44 if (!focus_native_view_) |
| 45 focus_native_view_ = native_view; |
40 native_wrapper_->NativeViewAttached(); | 46 native_wrapper_->NativeViewAttached(); |
41 } | 47 } |
42 | 48 |
43 void NativeViewHost::Detach() { | 49 void NativeViewHost::Detach() { |
44 DCHECK(native_view_); | 50 DCHECK(native_view_); |
45 native_wrapper_->NativeViewDetaching(); | 51 native_wrapper_->NativeViewDetaching(); |
46 native_view_ = NULL; | 52 native_view_ = NULL; |
| 53 focus_native_view_ = NULL; |
47 } | 54 } |
48 | 55 |
49 void NativeViewHost::SetPreferredSize(const gfx::Size& size) { | 56 void NativeViewHost::SetPreferredSize(const gfx::Size& size) { |
50 preferred_size_ = size; | 57 preferred_size_ = size; |
51 PreferredSizeChanged(); | 58 PreferredSizeChanged(); |
52 } | 59 } |
53 | 60 |
54 void NativeViewHost::NativeViewDestroyed() { | 61 void NativeViewHost::NativeViewDestroyed() { |
55 // TODO(beng): figure out if this should/could call Detach instead since as it | 62 // TODO(beng): figure out if this should/could call Detach instead since as it |
56 // stands right now this object is left in an inconsistent state. | 63 // stands right now this object is left in an inconsistent state. |
57 native_view_ = NULL; | 64 native_view_ = NULL; |
58 } | 65 } |
59 | 66 |
| 67 void NativeViewHost::GotNativeFocus() { |
| 68 // Some NativeViewHost may not have an associated focus view. This is the |
| 69 // case for containers for example. They might still get the native focus, |
| 70 // if one non native view they contain gets focused. In that case, we should |
| 71 // not change the focused view. |
| 72 if (!focus_view_ || !focus_view_->IsFocusable()) |
| 73 return; |
| 74 |
| 75 FocusManager* focus_manager = GetFocusManager(); |
| 76 if (!focus_manager) { |
| 77 NOTREACHED(); |
| 78 return; |
| 79 } |
| 80 focus_manager->SetFocusedView(focus_view_); |
| 81 } |
| 82 |
60 //////////////////////////////////////////////////////////////////////////////// | 83 //////////////////////////////////////////////////////////////////////////////// |
61 // NativeViewHost, View overrides: | 84 // NativeViewHost, View overrides: |
62 | 85 |
63 gfx::Size NativeViewHost::GetPreferredSize() { | 86 gfx::Size NativeViewHost::GetPreferredSize() { |
64 return preferred_size_; | 87 return preferred_size_; |
65 } | 88 } |
66 | 89 |
67 void NativeViewHost::Layout() { | 90 void NativeViewHost::Layout() { |
68 if (!native_view_ || !native_wrapper_.get()) | 91 if (!native_view_ || !native_wrapper_.get()) |
69 return; | 92 return; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 } else if (!is_add) { | 150 } else if (!is_add) { |
128 native_wrapper_->RemovedFromWidget(); | 151 native_wrapper_->RemovedFromWidget(); |
129 } | 152 } |
130 } | 153 } |
131 | 154 |
132 std::string NativeViewHost::GetClassName() const { | 155 std::string NativeViewHost::GetClassName() const { |
133 return kViewClassName; | 156 return kViewClassName; |
134 } | 157 } |
135 | 158 |
136 void NativeViewHost::Focus() { | 159 void NativeViewHost::Focus() { |
137 native_wrapper_->SetFocus(); | 160 FocusManager* focus_manager = GetFocusManager(); |
| 161 if (!focus_manager) { |
| 162 NOTREACHED(); |
| 163 return; |
| 164 } |
| 165 focus_manager->FocusNativeView(focus_native_view_); |
138 } | 166 } |
139 | 167 |
140 } // namespace views | 168 } // namespace views |
OLD | NEW |