OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_win.h" | 5 #include "views/controls/native/native_view_host_win.h" |
6 | 6 |
7 #include "app/gfx/canvas.h" | 7 #include "app/gfx/canvas.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/win_util.h" |
9 #include "views/controls/native/native_view_host.h" | 10 #include "views/controls/native/native_view_host.h" |
10 #include "views/focus/focus_manager.h" | 11 #include "views/focus/focus_manager.h" |
11 #include "views/widget/widget.h" | 12 #include "views/widget/widget.h" |
12 | 13 |
| 14 const wchar_t* kNativeViewHostWinKey = L"__NATIVE_VIEW_HOST_WIN__"; |
| 15 |
13 namespace views { | 16 namespace views { |
14 | 17 |
15 //////////////////////////////////////////////////////////////////////////////// | 18 //////////////////////////////////////////////////////////////////////////////// |
16 // NativeViewHostWin, public: | 19 // NativeViewHostWin, public: |
17 | 20 |
18 NativeViewHostWin::NativeViewHostWin(NativeViewHost* host) | 21 NativeViewHostWin::NativeViewHostWin(NativeViewHost* host) |
19 : host_(host), | 22 : host_(host), |
20 installed_clip_(false) { | 23 installed_clip_(false), |
| 24 original_wndproc_(NULL) { |
21 } | 25 } |
22 | 26 |
23 NativeViewHostWin::~NativeViewHostWin() { | 27 NativeViewHostWin::~NativeViewHostWin() { |
24 } | 28 } |
25 | 29 |
26 //////////////////////////////////////////////////////////////////////////////// | 30 //////////////////////////////////////////////////////////////////////////////// |
27 // NativeViewHostWin, NativeViewHostWrapper implementation: | 31 // NativeViewHostWin, NativeViewHostWrapper implementation: |
28 | 32 |
29 void NativeViewHostWin::NativeViewAttached() { | 33 void NativeViewHostWin::NativeViewAttached() { |
30 DCHECK(host_->native_view()) | 34 DCHECK(host_->native_view()) |
31 << "Impossible detatched tab case; See crbug.com/6316"; | 35 << "Impossible detatched tab case; See crbug.com/6316"; |
32 | 36 |
33 // First hide the new window. We don't want anything to draw (like sub-hwnd | 37 // First hide the new window. We don't want anything to draw (like sub-hwnd |
34 // borders), when we change the parent below. | 38 // borders), when we change the parent below. |
35 ShowWindow(host_->native_view(), SW_HIDE); | 39 ShowWindow(host_->native_view(), SW_HIDE); |
36 | 40 |
37 // Need to set the HWND's parent before changing its size to avoid flashing. | 41 // Need to set the HWND's parent before changing its size to avoid flashing. |
38 SetParent(host_->native_view(), host_->GetWidget()->GetNativeView()); | 42 SetParent(host_->native_view(), host_->GetWidget()->GetNativeView()); |
39 host_->Layout(); | 43 host_->Layout(); |
| 44 |
| 45 // Subclass the appropriate HWND to get focus notifications. |
| 46 HWND focus_hwnd = host_->focus_native_view(); |
| 47 DCHECK(focus_hwnd == host_->native_view() || |
| 48 ::IsChild(host_->native_view(), focus_hwnd)); |
| 49 original_wndproc_ = |
| 50 win_util::SetWindowProc(focus_hwnd, |
| 51 &NativeViewHostWin::NativeViewHostWndProc); |
| 52 |
| 53 // We use a property to retrieve the NativeViewHostWin from the window |
| 54 // procedure. |
| 55 ::SetProp(focus_hwnd, kNativeViewHostWinKey, this); |
40 } | 56 } |
41 | 57 |
42 void NativeViewHostWin::NativeViewDetaching() { | 58 void NativeViewHostWin::NativeViewDetaching() { |
43 installed_clip_ = false; | 59 installed_clip_ = false; |
| 60 |
| 61 // Restore the original Windows procedure. |
| 62 DCHECK(original_wndproc_); |
| 63 WNDPROC wndproc = win_util::SetWindowProc(host_->focus_native_view(), |
| 64 original_wndproc_); |
| 65 DCHECK(wndproc == &NativeViewHostWin::NativeViewHostWndProc); |
| 66 |
| 67 // Also remove the property, it's not needed anymore. |
| 68 HANDLE h = ::RemoveProp(host_->focus_native_view(), kNativeViewHostWinKey); |
| 69 DCHECK(h == this); |
44 } | 70 } |
45 | 71 |
46 void NativeViewHostWin::AddedToWidget() { | 72 void NativeViewHostWin::AddedToWidget() { |
47 if (!IsWindow(host_->native_view())) | 73 if (!IsWindow(host_->native_view())) |
48 return; | 74 return; |
49 HWND parent_hwnd = GetParent(host_->native_view()); | 75 HWND parent_hwnd = GetParent(host_->native_view()); |
50 HWND widget_hwnd = host_->GetWidget()->GetNativeView(); | 76 HWND widget_hwnd = host_->GetWidget()->GetNativeView(); |
51 if (parent_hwnd != widget_hwnd) | 77 if (parent_hwnd != widget_hwnd) |
52 SetParent(host_->native_view(), widget_hwnd); | 78 SetParent(host_->native_view(), widget_hwnd); |
53 if (host_->IsVisibleInRootView()) | 79 if (host_->IsVisibleInRootView()) |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 if (!IsWindowVisible(host_->native_view())) | 135 if (!IsWindowVisible(host_->native_view())) |
110 return; // Currently not visible, nothing to do. | 136 return; // Currently not visible, nothing to do. |
111 | 137 |
112 // The window is currently visible, but its clipped by another view. Hide | 138 // The window is currently visible, but its clipped by another view. Hide |
113 // it. | 139 // it. |
114 SetWindowPos(host_->native_view(), 0, 0, 0, 0, 0, | 140 SetWindowPos(host_->native_view(), 0, 0, 0, 0, 0, |
115 SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | | 141 SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | |
116 SWP_NOREDRAW | SWP_NOOWNERZORDER); | 142 SWP_NOREDRAW | SWP_NOOWNERZORDER); |
117 } | 143 } |
118 | 144 |
119 void NativeViewHostWin::SetFocus() { | 145 // static |
120 ::SetFocus(host_->native_view()); | 146 LRESULT CALLBACK NativeViewHostWin::NativeViewHostWndProc(HWND window, |
| 147 UINT message, |
| 148 WPARAM w_param, |
| 149 LPARAM l_param) { |
| 150 NativeViewHostWin* native_view_host = |
| 151 static_cast<NativeViewHostWin*>(::GetProp(window, kNativeViewHostWinKey)); |
| 152 DCHECK(native_view_host); |
| 153 |
| 154 if (message == WM_SETFOCUS) |
| 155 native_view_host->host_->GotNativeFocus(); |
| 156 if (message == WM_DESTROY) |
| 157 native_view_host->host_->Detach(); |
| 158 |
| 159 return CallWindowProc(native_view_host->original_wndproc_, |
| 160 window, message, w_param, l_param); |
121 } | 161 } |
122 | 162 |
123 //////////////////////////////////////////////////////////////////////////////// | 163 //////////////////////////////////////////////////////////////////////////////// |
124 // NativeViewHostWrapper, public: | 164 // NativeViewHostWrapper, public: |
125 | 165 |
126 // static | 166 // static |
127 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( | 167 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( |
128 NativeViewHost* host) { | 168 NativeViewHost* host) { |
129 return new NativeViewHostWin(host); | 169 return new NativeViewHostWin(host); |
130 } | 170 } |
131 | 171 |
132 } // namespace views | 172 } // namespace views |
OLD | NEW |