| 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/hwnd_view.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 "views/controls/native/native_view_host.h" |
| 9 #include "views/focus/focus_manager.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 const char kViewClassName[] = "views/HWNDView"; | 15 //////////////////////////////////////////////////////////////////////////////// |
| 16 // NativeViewHostWin, public: |
| 15 | 17 |
| 16 HWNDView::HWNDView() : NativeViewHost() { | 18 NativeViewHostWin::NativeViewHostWin(NativeViewHost* host) |
| 19 : host_(host), |
| 20 installed_clip_(false) { |
| 17 } | 21 } |
| 18 | 22 |
| 19 HWNDView::~HWNDView() { | 23 NativeViewHostWin::~NativeViewHostWin() { |
| 20 } | 24 } |
| 21 | 25 |
| 22 void HWNDView::Attach(HWND hwnd) { | 26 //////////////////////////////////////////////////////////////////////////////// |
| 23 DCHECK(native_view() == NULL); | 27 // NativeViewHostWin, NativeViewHostWrapper implementation: |
| 24 DCHECK(hwnd) << "Impossible detatched tab case; See crbug.com/6316"; | |
| 25 | 28 |
| 26 set_native_view(hwnd); | 29 void NativeViewHostWin::NativeViewAttached() { |
| 30 DCHECK(host_->native_view()) |
| 31 << "Impossible detatched tab case; See crbug.com/6316"; |
| 27 | 32 |
| 28 // First hide the new window. We don't want anything to draw (like sub-hwnd | 33 // First hide the new window. We don't want anything to draw (like sub-hwnd |
| 29 // borders), when we change the parent below. | 34 // borders), when we change the parent below. |
| 30 ShowWindow(hwnd, SW_HIDE); | 35 ShowWindow(host_->native_view(), SW_HIDE); |
| 31 | 36 |
| 32 // Need to set the HWND's parent before changing its size to avoid flashing. | 37 // Need to set the HWND's parent before changing its size to avoid flashing. |
| 33 ::SetParent(hwnd, GetWidget()->GetNativeView()); | 38 SetParent(host_->native_view(), host_->GetWidget()->GetNativeView()); |
| 34 Layout(); | 39 host_->Layout(); |
| 35 | 40 |
| 36 // Register with the focus manager so the associated view is focused when the | 41 // Register with the focus manager so the associated view is focused when the |
| 37 // native control gets the focus. | 42 // native control gets the focus. |
| 38 FocusManager::InstallFocusSubclass( | 43 View* focus_view = host_->focus_view() ? host_->focus_view() : host_; |
| 39 hwnd, associated_focus_view() ? associated_focus_view() : this); | 44 FocusManager::InstallFocusSubclass(host_->native_view(), focus_view); |
| 40 } | 45 } |
| 41 | 46 |
| 42 void HWNDView::Detach() { | 47 void NativeViewHostWin::NativeViewDetaching() { |
| 43 DCHECK(native_view()); | 48 DCHECK(host_->native_view()); |
| 44 FocusManager::UninstallFocusSubclass(native_view()); | 49 FocusManager::UninstallFocusSubclass(host_->native_view()); |
| 45 set_native_view(NULL); | 50 installed_clip_ = false; |
| 46 set_installed_clip(false); | |
| 47 } | 51 } |
| 48 | 52 |
| 49 void HWNDView::Paint(gfx::Canvas* canvas) { | 53 void NativeViewHostWin::AddedToWidget() { |
| 50 // The area behind our window is black, so during a fast resize (where our | 54 HWND parent_hwnd = GetParent(host_->native_view()); |
| 51 // content doesn't draw over the full size of our HWND, and the HWND | 55 HWND widget_hwnd = host_->GetWidget()->GetNativeView(); |
| 52 // background color doesn't show up), we need to cover that blackness with | 56 if (parent_hwnd != widget_hwnd) |
| 53 // something so that fast resizes don't result in black flash. | 57 SetParent(host_->native_view(), widget_hwnd); |
| 54 // | 58 if (host_->IsVisibleInRootView()) |
| 55 // It would be nice if this used some approximation of the page's | 59 ShowWindow(host_->native_view(), SW_SHOW); |
| 56 // current background color. | 60 else |
| 57 if (installed_clip()) | 61 ShowWindow(host_->native_view(), SW_HIDE); |
| 58 canvas->FillRectInt(SkColorSetRGB(255, 255, 255), 0, 0, width(), height()); | 62 host_->Layout(); |
| 59 } | 63 } |
| 60 | 64 |
| 61 std::string HWNDView::GetClassName() const { | 65 void NativeViewHostWin::RemovedFromWidget() { |
| 62 return kViewClassName; | 66 ShowWindow(host_->native_view(), SW_HIDE); |
| 67 SetParent(host_->native_view(), NULL); |
| 63 } | 68 } |
| 64 | 69 |
| 65 void HWNDView::ViewHierarchyChanged(bool is_add, View *parent, View *child) { | 70 void NativeViewHostWin::InstallClip(int x, int y, int w, int h) { |
| 66 if (!native_view()) | |
| 67 return; | |
| 68 | |
| 69 Widget* widget = GetWidget(); | |
| 70 if (is_add && widget) { | |
| 71 HWND parent_hwnd = ::GetParent(native_view()); | |
| 72 HWND widget_hwnd = widget->GetNativeView(); | |
| 73 if (parent_hwnd != widget_hwnd) | |
| 74 ::SetParent(native_view(), widget_hwnd); | |
| 75 if (IsVisibleInRootView()) | |
| 76 ::ShowWindow(native_view(), SW_SHOW); | |
| 77 else | |
| 78 ::ShowWindow(native_view(), SW_HIDE); | |
| 79 Layout(); | |
| 80 } else if (!is_add) { | |
| 81 ::ShowWindow(native_view(), SW_HIDE); | |
| 82 ::SetParent(native_view(), NULL); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 void HWNDView::Focus() { | |
| 87 ::SetFocus(native_view()); | |
| 88 } | |
| 89 | |
| 90 void HWNDView::InstallClip(int x, int y, int w, int h) { | |
| 91 HRGN clip_region = CreateRectRgn(x, y, x + w, y + h); | 71 HRGN clip_region = CreateRectRgn(x, y, x + w, y + h); |
| 92 // NOTE: SetWindowRgn owns the region (as well as the deleting the | 72 // NOTE: SetWindowRgn owns the region (as well as the deleting the |
| 93 // current region), as such we don't delete the old region. | 73 // current region), as such we don't delete the old region. |
| 94 SetWindowRgn(native_view(), clip_region, FALSE); | 74 SetWindowRgn(host_->native_view(), clip_region, FALSE); |
| 75 installed_clip_ = true; |
| 95 } | 76 } |
| 96 | 77 |
| 97 void HWNDView::UninstallClip() { | 78 bool NativeViewHostWin::HasInstalledClip() { |
| 98 SetWindowRgn(native_view(), 0, FALSE); | 79 return installed_clip_; |
| 99 } | 80 } |
| 100 | 81 |
| 101 void HWNDView::ShowWidget(int x, int y, int w, int h) { | 82 void NativeViewHostWin::UninstallClip() { |
| 83 SetWindowRgn(host_->native_view(), 0, FALSE); |
| 84 installed_clip_ = false; |
| 85 } |
| 86 |
| 87 void NativeViewHostWin::ShowWidget(int x, int y, int w, int h) { |
| 102 UINT swp_flags = SWP_DEFERERASE | | 88 UINT swp_flags = SWP_DEFERERASE | |
| 103 SWP_NOACTIVATE | | 89 SWP_NOACTIVATE | |
| 104 SWP_NOCOPYBITS | | 90 SWP_NOCOPYBITS | |
| 105 SWP_NOOWNERZORDER | | 91 SWP_NOOWNERZORDER | |
| 106 SWP_NOZORDER; | 92 SWP_NOZORDER; |
| 107 // Only send the SHOWWINDOW flag if we're invisible, to avoid flashing. | 93 // Only send the SHOWWINDOW flag if we're invisible, to avoid flashing. |
| 108 if (!::IsWindowVisible(native_view())) | 94 if (!IsWindowVisible(host_->native_view())) |
| 109 swp_flags = (swp_flags | SWP_SHOWWINDOW) & ~SWP_NOREDRAW; | 95 swp_flags = (swp_flags | SWP_SHOWWINDOW) & ~SWP_NOREDRAW; |
| 110 | 96 |
| 111 if (fast_resize()) { | 97 if (host_->fast_resize()) { |
| 112 // In a fast resize, we move the window and clip it with SetWindowRgn. | 98 // In a fast resize, we move the window and clip it with SetWindowRgn. |
| 113 RECT win_rect; | 99 RECT win_rect; |
| 114 GetWindowRect(native_view(), &win_rect); | 100 GetWindowRect(host_->native_view(), &win_rect); |
| 115 gfx::Rect rect(win_rect); | 101 gfx::Rect rect(win_rect); |
| 116 ::SetWindowPos(native_view(), 0, x, y, rect.width(), rect.height(), | 102 SetWindowPos(host_->native_view(), 0, x, y, rect.width(), rect.height(), |
| 117 swp_flags); | 103 swp_flags); |
| 118 | 104 |
| 119 HRGN clip_region = CreateRectRgn(0, 0, w, h); | 105 InstallClip(0, 0, w, h); |
| 120 SetWindowRgn(native_view(), clip_region, FALSE); | |
| 121 set_installed_clip(true); | |
| 122 } else { | 106 } else { |
| 123 ::SetWindowPos(native_view(), 0, x, y, w, h, swp_flags); | 107 SetWindowPos(host_->native_view(), 0, x, y, w, h, swp_flags); |
| 124 } | 108 } |
| 125 } | 109 } |
| 126 | 110 |
| 127 void HWNDView::HideWidget() { | 111 void NativeViewHostWin::HideWidget() { |
| 128 if (!::IsWindowVisible(native_view())) | 112 if (!IsWindowVisible(host_->native_view())) |
| 129 return; // Currently not visible, nothing to do. | 113 return; // Currently not visible, nothing to do. |
| 130 | 114 |
| 131 // The window is currently visible, but its clipped by another view. Hide | 115 // The window is currently visible, but its clipped by another view. Hide |
| 132 // it. | 116 // it. |
| 133 ::SetWindowPos(native_view(), 0, 0, 0, 0, 0, | 117 SetWindowPos(host_->native_view(), 0, 0, 0, 0, 0, |
| 134 SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | | 118 SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | |
| 135 SWP_NOREDRAW | SWP_NOOWNERZORDER); | 119 SWP_NOREDRAW | SWP_NOOWNERZORDER); |
| 120 } |
| 121 |
| 122 void NativeViewHostWin::SetFocus() { |
| 123 ::SetFocus(host_->native_view()); |
| 124 } |
| 125 |
| 126 //////////////////////////////////////////////////////////////////////////////// |
| 127 // NativeViewHostWrapper, public: |
| 128 |
| 129 // static |
| 130 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( |
| 131 NativeViewHost* host) { |
| 132 return new NativeViewHostWin(host); |
| 136 } | 133 } |
| 137 | 134 |
| 138 } // namespace views | 135 } // namespace views |
| OLD | NEW |