OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import "ui/views/controls/native/native_view_host_mac.h" | |
6 | |
7 #import <Cocoa/Cocoa.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/mac/foundation_util.h" | |
11 #import "ui/views/cocoa/bridged_content_view.h" | |
12 #include "ui/views/controls/native/native_view_host.h" | |
13 #include "ui/views/widget/widget.h" | |
14 | |
15 namespace views { | |
16 namespace { | |
17 | |
18 // Reparents |native_view| to be a child of the native content view of | |
19 // |new_parent|. | |
20 void ReparentNSView(NSView* native_view, Widget* new_parent) { | |
21 DCHECK(native_view); | |
22 // Mac's NativeViewHost has no support for hosting its own child widgets. | |
23 // This check is probably overly restrictive, since the Widget containing the | |
24 // NativeViewHost _is_ allowed child Widgets. However, we don't know yet | |
25 // whether those child Widgets need to be distinguished from Widgets that code | |
26 // might want to associate with the hosted NSView instead. | |
27 { | |
28 Widget::Widgets child_widgets; | |
29 Widget::GetAllChildWidgets(native_view, &child_widgets); | |
30 CHECK_GE(1u, child_widgets.size()); // 1 (itself) or 0 if detached. | |
31 } | |
32 | |
33 if (!new_parent) { | |
34 [native_view removeFromSuperview]; | |
35 return; | |
36 } | |
37 | |
38 BridgedContentView* new_superview = | |
39 base::mac::ObjCCastStrict<BridgedContentView>( | |
40 new_parent->GetNativeView()); | |
41 DCHECK(new_superview); | |
42 [new_superview addSubview:native_view]; | |
43 } | |
44 | |
45 } // namespace | |
46 | |
47 NativeViewHostMac::NativeViewHostMac(NativeViewHost* host) : host_(host) { | |
48 } | |
49 | |
50 NativeViewHostMac::~NativeViewHostMac() { | |
51 } | |
52 | |
53 //////////////////////////////////////////////////////////////////////////////// | |
54 // NativeViewHostMac, NativeViewHostWrapper implementation: | |
55 | |
56 void NativeViewHostMac::AttachNativeView() { | |
57 DCHECK(host_->native_view()); | |
58 ReparentNSView(host_->native_view(), host_->GetWidget()); | |
59 host_->Layout(); | |
60 } | |
61 | |
62 void NativeViewHostMac::NativeViewDetaching(bool destroyed) { | |
63 RemovedFromWidget(); | |
64 } | |
65 | |
66 void NativeViewHostMac::AddedToWidget() { | |
67 if (!host_->native_view()) | |
68 return; | |
69 | |
70 AttachNativeView(); | |
71 } | |
72 | |
73 void NativeViewHostMac::RemovedFromWidget() { | |
74 if (!host_->native_view()) | |
75 return; | |
76 | |
77 ReparentNSView(host_->native_view(), NULL); | |
78 [host_->native_view() setHidden:YES]; | |
79 } | |
80 | |
81 void NativeViewHostMac::InstallClip(int x, int y, int w, int h) { | |
82 NOTIMPLEMENTED(); | |
83 } | |
84 | |
85 bool NativeViewHostMac::HasInstalledClip() { | |
86 return false; | |
87 } | |
88 | |
89 void NativeViewHostMac::UninstallClip() { | |
90 NOTIMPLEMENTED(); | |
91 } | |
92 | |
93 void NativeViewHostMac::ShowWidget(int x, int y, int w, int h) { | |
94 if (host_->fast_resize()) | |
95 NOTIMPLEMENTED(); | |
96 | |
97 // Coordinates will be from the top left of the parent Widget. The NativeView | |
98 // is already in the same NSWindow, so just flip to get Cooca coordinates and | |
99 // then convert to the containing view. | |
100 NSRect window_rect = NSMakeRect( | |
101 x, | |
102 host_->GetWidget()->GetClientAreaBoundsInScreen().height() - y - h, | |
103 w, | |
104 h); | |
105 | |
106 // Convert window coordinates to the hosted view's superview, since that's how | |
107 // coordinates of the hosted view's frame is based. | |
108 NSRect container_rect = | |
109 [[host_->native_view() superview] convertRect:window_rect fromView:nil]; | |
110 [host_->native_view() setFrame:container_rect]; | |
111 [host_->native_view() setHidden:NO]; | |
112 } | |
113 | |
114 void NativeViewHostMac::HideWidget() { | |
115 [host_->native_view() setHidden:YES]; | |
116 } | |
117 | |
118 void NativeViewHostMac::SetFocus() { | |
119 [[host_->native_view() window] makeFirstResponder:host_->native_view()]; | |
Andre
2014/09/17 20:57:13
From -[NSWindow makeFirstResponder:] documentation
tapted
2014/09/18 08:40:14
Done.
| |
120 } | |
121 | |
122 void NativeViewHostMac::SetNativeViewVisible(bool visible) { | |
123 [host_->native_view() setHidden:!visible]; | |
124 } | |
125 | |
126 gfx::NativeViewAccessible NativeViewHostMac::GetNativeViewAccessible() { | |
127 return NULL; | |
128 } | |
129 | |
130 gfx::NativeCursor NativeViewHostMac::GetCursor(int x, int y) { | |
131 NOTIMPLEMENTED(); | |
132 return gfx::kNullCursor; | |
133 } | |
134 | |
135 // static | |
136 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( | |
137 NativeViewHost* host) { | |
138 return new NativeViewHostMac(host); | |
139 } | |
140 | |
141 } // namespace views | |
OLD | NEW |