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