2 * Copyright (C) 2010 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "WebInspectorProxy.h"
31 #include "WebKitBundle.h"
32 #include "WebPageProxy.h"
33 #include "WebProcessProxy.h"
35 #include <WebCore/InspectorFrontendClientLocal.h>
36 #include <WebCore/WebCoreInstanceHandle.h>
37 #include <WebCore/WindowMessageBroadcaster.h>
38 #include <wtf/PassRefPtr.h>
39 #include <wtf/RetainPtr.h>
40 #include <wtf/text/StringConcatenate.h>
41 #include <wtf/text/WTFString.h>
43 using namespace WebCore;
47 static const LPCWSTR kWebKit2InspectorWindowClassName = L"WebKit2InspectorWindowClass";
49 bool WebInspectorProxy::registerInspectorViewWindowClass()
51 static bool haveRegisteredWindowClass = false;
52 if (haveRegisteredWindowClass)
54 haveRegisteredWindowClass = true;
58 wcex.cbSize = sizeof(WNDCLASSEX);
59 wcex.style = CS_DBLCLKS;
60 wcex.lpfnWndProc = WebInspectorProxy::InspectorViewWndProc;
62 wcex.cbWndExtra = sizeof(WebInspectorProxy*);
63 wcex.hInstance = instanceHandle();
65 wcex.hCursor = ::LoadCursor(0, IDC_ARROW);
66 wcex.hbrBackground = 0;
67 wcex.lpszMenuName = 0;
68 wcex.lpszClassName = kWebKit2InspectorWindowClassName;
71 return !!::RegisterClassEx(&wcex);
74 LRESULT CALLBACK WebInspectorProxy::InspectorViewWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
76 LONG_PTR longPtr = ::GetWindowLongPtr(hWnd, 0);
78 if (WebInspectorProxy* inspectorView = reinterpret_cast<WebInspectorProxy*>(longPtr))
79 return inspectorView->wndProc(hWnd, message, wParam, lParam);
81 if (message == WM_CREATE) {
82 LPCREATESTRUCT createStruct = reinterpret_cast<LPCREATESTRUCT>(lParam);
84 // Associate the WebInspectorProxy with the window.
85 ::SetWindowLongPtr(hWnd, 0, (LONG_PTR)createStruct->lpCreateParams);
89 return ::DefWindowProc(hWnd, message, wParam, lParam);
92 void WebInspectorProxy::windowReceivedMessage(HWND, UINT msg, WPARAM wParam, LPARAM lParam)
97 case WM_WINDOWPOSCHANGING:
98 onWebViewWindowPosChangingEvent(wParam, lParam);
105 LRESULT WebInspectorProxy::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
112 lResult = onSizeEvent(hWnd, message, wParam, lParam, handled);
114 case WM_GETMINMAXINFO:
115 lResult = onMinMaxInfoEvent(hWnd, message, wParam, lParam, handled);
118 lResult = onSetFocusEvent(hWnd, message, wParam, lParam, handled);
121 lResult = onCloseEvent(hWnd, message, wParam, lParam, handled);
129 lResult = ::DefWindowProc(hWnd, message, wParam, lParam);
134 LRESULT WebInspectorProxy::onSizeEvent(HWND, UINT, WPARAM, LPARAM, bool&)
137 ::GetClientRect(m_inspectorWindow, &rect);
139 ::SetWindowPos(m_inspectorView->window(), 0, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER);
144 LRESULT WebInspectorProxy::onSetFocusEvent(HWND, UINT, WPARAM, LPARAM lParam, bool&)
146 ::SetFocus(m_inspectorView->window());
151 LRESULT WebInspectorProxy::onMinMaxInfoEvent(HWND, UINT, WPARAM, LPARAM lParam, bool&)
153 MINMAXINFO* info = reinterpret_cast<MINMAXINFO*>(lParam);
154 POINT size = {minimumWindowWidth, minimumWindowHeight};
155 info->ptMinTrackSize = size;
160 LRESULT WebInspectorProxy::onCloseEvent(HWND, UINT, WPARAM, LPARAM, bool&)
162 ::ShowWindow(m_inspectorWindow, SW_HIDE);
168 void WebInspectorProxy::onWebViewWindowPosChangingEvent(WPARAM wParam, LPARAM lParam)
170 WINDOWPOS* windowPos = reinterpret_cast<WINDOWPOS*>(lParam);
172 if (windowPos->flags & SWP_NOSIZE)
175 HWND inspectorWindow = m_inspectorView->window();
178 ::GetClientRect(inspectorWindow, &inspectorRect);
179 unsigned inspectorHeight = inspectorRect.bottom - inspectorRect.top;
182 ::GetClientRect(::GetParent(inspectorWindow), &parentRect);
183 inspectorHeight = InspectorFrontendClientLocal::constrainedAttachedWindowHeight(inspectorHeight, parentRect.bottom - parentRect.top);
185 windowPos->cy -= inspectorHeight;
187 ::SetWindowPos(inspectorWindow, 0, windowPos->x, windowPos->y + windowPos->cy, windowPos->cx, inspectorHeight, SWP_NOZORDER);
190 WebPageProxy* WebInspectorProxy::platformCreateInspectorPage()
192 ASSERT(!m_inspectorView);
193 ASSERT(!m_inspectorWindow);
195 RECT initialRect = { 0, 0, initialWindowWidth, initialWindowHeight };
196 m_inspectorView = WebView::create(initialRect, m_page->process()->context(), inspectorPageGroup(), 0);
198 return m_inspectorView->page();
201 void WebInspectorProxy::platformOpen()
203 registerInspectorViewWindowClass();
205 m_inspectorWindow = ::CreateWindowEx(0, kWebKit2InspectorWindowClassName, 0, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
206 0, 0, initialWindowWidth, initialWindowHeight, 0, 0, instanceHandle(), this);
207 ASSERT(::IsWindow(m_inspectorWindow));
209 m_inspectorView->setParentWindow(m_inspectorWindow);
214 ::ShowWindow(m_inspectorWindow, SW_SHOW);
217 void WebInspectorProxy::platformDidClose()
219 ASSERT(!m_isAttached);
220 ASSERT(!m_isVisible || m_inspectorWindow);
221 ASSERT(!m_isVisible || m_inspectorView);
223 if (m_inspectorWindow) {
224 ASSERT(::IsWindow(m_inspectorWindow));
225 ::DestroyWindow(m_inspectorWindow);
228 m_inspectorWindow = 0;
232 void WebInspectorProxy::platformBringToFront()
234 // FIXME: this will not bring a background tab in Safari to the front, only its window.
235 HWND parentWindow = m_isAttached ? ::GetAncestor(m_page->nativeWindow(), GA_ROOT) : m_inspectorWindow;
239 ASSERT(::IsWindow(parentWindow));
240 ::SetWindowPos(parentWindow, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
243 void WebInspectorProxy::platformInspectedURLChanged(const String& urlString)
245 // FIXME: this should be made localizable once WebKit2 supports it. <rdar://problem/8728860>
246 String title = makeString("Web Inspector ", static_cast<UChar>(0x2014), ' ', urlString);
247 ::SetWindowTextW(m_inspectorWindow, title.charactersWithNullTermination());
250 unsigned WebInspectorProxy::platformInspectedWindowHeight()
252 HWND inspectedWindow = m_page->nativeWindow();
254 RECT inspectedWindowRect;
255 ::GetWindowRect(inspectedWindow, &inspectedWindowRect);
257 return static_cast<unsigned>(inspectedWindowRect.bottom - inspectedWindowRect.top);
260 void WebInspectorProxy::platformAttach()
262 HWND webViewWindow = m_page->nativeWindow();
263 HWND parentWindow = ::GetParent(webViewWindow);
265 WindowMessageBroadcaster::addListener(webViewWindow, this);
266 m_inspectorView->setParentWindow(parentWindow);
267 ::ShowWindow(m_inspectorWindow, SW_HIDE);
269 ::PostMessage(parentWindow, WM_SIZE, 0, 0);
272 void WebInspectorProxy::platformDetach()
274 HWND webViewWindow = m_page->nativeWindow();
275 WindowMessageBroadcaster::removeListener(webViewWindow, this);
277 m_inspectorView->setParentWindow(m_inspectorWindow);
280 ::ShowWindow(m_inspectorWindow, SW_SHOW);
282 // Send the detached inspector window and the WebView's parent window WM_SIZE messages
283 // to have them re-layout correctly.
284 ::PostMessage(m_inspectorWindow, WM_SIZE, 0, 0);
285 ::PostMessage(::GetParent(webViewWindow), WM_SIZE, 0, 0);
288 void WebInspectorProxy::platformSetAttachedWindowHeight(unsigned height)
293 HWND inspectedWindow = m_page->nativeWindow();
294 HWND parentWindow = ::GetParent(inspectedWindow);
296 RECT parentWindowRect;
297 ::GetWindowRect(parentWindow, &parentWindowRect);
299 RECT inspectedWindowRect;
300 ::GetWindowRect(inspectedWindow, &inspectedWindowRect);
302 int totalHeight = parentWindowRect.bottom - parentWindowRect.top;
303 int webViewWidth = inspectedWindowRect.right - inspectedWindowRect.left;
305 POINT inspectedWindowOrigin = { inspectedWindowRect.left, inspectedWindowRect.top };
306 ::ScreenToClient(parentWindow, &inspectedWindowOrigin);
308 HWND inspectorWindow = m_inspectorView->window();
309 ::SetWindowPos(inspectorWindow, 0, inspectedWindowOrigin.x, totalHeight - height, webViewWidth, height, SWP_NOZORDER);
311 // We want to set the inspected web view height to the totalHeight, because the height adjustment
312 // of the inspected WebView happens in onWindowPosChanging, not here.
313 ::SetWindowPos(inspectedWindow, 0, inspectedWindowOrigin.x, inspectedWindowOrigin.y, webViewWidth, totalHeight, SWP_NOZORDER);
315 ::RedrawWindow(inspectorWindow, 0, 0, RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_UPDATENOW);
316 ::RedrawWindow(inspectedWindow, 0, 0, RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_UPDATENOW);
319 String WebInspectorProxy::inspectorPageURL() const
321 RetainPtr<CFURLRef> htmlURLRef(AdoptCF, CFBundleCopyResourceURL(webKitBundle(), CFSTR("inspector"), CFSTR("html"), CFSTR("inspector")));
325 return String(CFURLGetString(htmlURLRef.get()));
328 } // namespace WebKit
330 #endif // ENABLE(INSPECTOR)