2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "WebNodeHighlight.h"
33 #include <WebCore/BitmapInfo.h>
34 #include <WebCore/Color.h>
35 #include <WebCore/GraphicsContext.h>
36 #include <WebCore/HWndDC.h>
37 #include <WebCore/InspectorController.h>
38 #include <WebCore/Page.h>
39 #include <WebCore/WindowMessageBroadcaster.h>
40 #include <wtf/HashSet.h>
41 #include <wtf/OwnPtr.h>
43 using namespace WebCore;
45 static LPCTSTR kOverlayWindowClassName = TEXT("WebNodeHighlightWindowClass");
46 static ATOM registerOverlayClass();
47 static LPCTSTR kWebNodeHighlightPointerProp = TEXT("WebNodeHighlightPointer");
49 WebNodeHighlight::WebNodeHighlight(WebView* webView)
52 m_inspectedWebView(webView),
53 #endif // ENABLE(INSPECTOR)
56 , m_showsWhileWebViewIsVisible(false)
59 m_inspectedWebView->viewWindow(reinterpret_cast<OLE_HANDLE*>(&m_inspectedWebViewWindow));
60 #endif // ENABLE(INSPECTOR)
63 WebNodeHighlight::~WebNodeHighlight()
66 WindowMessageBroadcaster::removeListener(m_observedWindow, this);
67 if (m_inspectedWebViewWindow)
68 WindowMessageBroadcaster::removeListener(m_inspectedWebViewWindow, this);
71 ::DestroyWindow(m_overlay);
74 void WebNodeHighlight::setShowsWhileWebViewIsVisible(bool shows)
76 if (m_showsWhileWebViewIsVisible == shows)
78 m_showsWhileWebViewIsVisible = shows;
80 if (!m_showsWhileWebViewIsVisible) {
85 bool webViewVisible = isWebViewVisible();
87 if (isShowing() == webViewVisible)
96 bool WebNodeHighlight::isWebViewVisible() const
98 if (!m_inspectedWebViewWindow)
101 return IsWindowVisible(m_inspectedWebViewWindow);
104 void WebNodeHighlight::show()
107 registerOverlayClass();
109 m_overlay = ::CreateWindowEx(WS_EX_LAYERED | WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT, kOverlayWindowClassName, 0, WS_POPUP,
111 m_inspectedWebViewWindow, 0, 0, 0);
115 ::SetProp(m_overlay, kWebNodeHighlightPointerProp, reinterpret_cast<HANDLE>(this));
117 m_observedWindow = GetAncestor(m_inspectedWebViewWindow, GA_ROOT);
118 WindowMessageBroadcaster::addListener(m_observedWindow, this);
119 WindowMessageBroadcaster::addListener(m_inspectedWebViewWindow, this);
122 ASSERT(m_showsWhileWebViewIsVisible);
125 SetWindowPos(m_overlay, 0, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
128 void WebNodeHighlight::hide()
131 ::ShowWindow(m_overlay, SW_HIDE);
134 bool WebNodeHighlight::isShowing() const
136 return m_overlay && ::IsWindowVisible(m_overlay);
139 void WebNodeHighlight::update()
143 HDC hdc = ::CreateCompatibleDC(HWndDC(m_overlay));
148 ::GetWindowRect(m_inspectedWebViewWindow, &webViewRect);
151 size.cx = webViewRect.right - webViewRect.left;
152 size.cy = webViewRect.bottom - webViewRect.top;
154 if (!size.cx || !size.cy)
157 BitmapInfo bitmapInfo = BitmapInfo::createBottomUp(IntSize(size));
160 OwnPtr<HBITMAP> hbmp = adoptPtr(::CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, &pixels, 0, 0));
161 ASSERT_WITH_MESSAGE(hbmp, "::CreateDIBSection failed with error %lu", ::GetLastError());
163 ::SelectObject(hdc, hbmp.get());
165 GraphicsContext context(hdc);
166 #if ENABLE(INSPECTOR)
167 m_inspectedWebView->page()->inspectorController()->drawHighlight(context);
168 #endif // ENABLE(INSPECTOR)
171 bf.BlendOp = AC_SRC_OVER;
173 bf.SourceConstantAlpha = 255;
174 bf.AlphaFormat = AC_SRC_ALPHA;
181 dstPoint.x = webViewRect.left;
182 dstPoint.y = webViewRect.top;
184 ::UpdateLayeredWindow(m_overlay, HWndDC(0), &dstPoint, &size, hdc, &srcPoint, 0, &bf, ULW_ALPHA);
188 void WebNodeHighlight::placeBehindWindow(HWND window)
191 SetWindowPos(m_overlay, window, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
194 static ATOM registerOverlayClass()
196 static bool haveRegisteredWindowClass = false;
198 if (haveRegisteredWindowClass)
203 wcex.cbSize = sizeof(WNDCLASSEX);
206 wcex.lpfnWndProc = OverlayWndProc;
211 wcex.hCursor = LoadCursor(0, IDC_ARROW);
212 wcex.hbrBackground = 0;
213 wcex.lpszMenuName = 0;
214 wcex.lpszClassName = kOverlayWindowClassName;
217 haveRegisteredWindowClass = true;
219 return ::RegisterClassEx(&wcex);
222 LRESULT CALLBACK OverlayWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
224 WebNodeHighlight* highlight = reinterpret_cast<WebNodeHighlight*>(::GetProp(hwnd, kWebNodeHighlightPointerProp));
226 return ::DefWindowProc(hwnd, msg, wParam, lParam);
228 return ::DefWindowProc(hwnd, msg, wParam, lParam);
231 void WebNodeHighlight::onWebViewShowWindow(bool showing)
233 if (!m_showsWhileWebViewIsVisible)
236 if (isShowing() == showing)
245 void WebNodeHighlight::onWebViewWindowPosChanged(WINDOWPOS* windowPos)
247 bool sizing = !(windowPos->flags & SWP_NOSIZE);
258 void WebNodeHighlight::onRootWindowPosChanged(WINDOWPOS* windowPos)
260 bool moving = !(windowPos->flags & SWP_NOMOVE);
261 bool sizing = !(windowPos->flags & SWP_NOSIZE);
266 // Size changes are handled by onWebViewWindowPosChanged.
276 void WebNodeHighlight::windowReceivedMessage(HWND window, UINT msg, WPARAM wParam, LPARAM lParam)
278 if (window == m_inspectedWebViewWindow) {
281 onWebViewShowWindow(wParam);
283 case WM_WINDOWPOSCHANGED:
284 onWebViewWindowPosChanged(reinterpret_cast<WINDOWPOS*>(lParam));
293 ASSERT(window == m_observedWindow);
295 case WM_WINDOWPOSCHANGED:
296 onRootWindowPosChanged(reinterpret_cast<WINDOWPOS*>(lParam));