2 * Copyright (C) 2009 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2009 Brent Fulgham. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * 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.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "PrintWebUIDelegate.h"
30 #include <WebKit/WebKitCOMAPI.h>
37 static const int MARGIN = 20;
39 HRESULT PrintWebUIDelegate::QueryInterface(REFIID riid, void** ppvObject)
42 if (IsEqualIID(riid, IID_IUnknown))
43 *ppvObject = static_cast<IWebUIDelegate*>(this);
44 else if (IsEqualIID(riid, IID_IWebUIDelegate))
45 *ppvObject = static_cast<IWebUIDelegate*>(this);
53 ULONG PrintWebUIDelegate::AddRef(void)
58 ULONG PrintWebUIDelegate::Release(void)
60 ULONG newRef = --m_refCount;
67 HRESULT PrintWebUIDelegate::webViewPrintingMarginRect(IWebView* view, RECT* rect)
72 IWebFrame* mainFrame = 0;
73 if (FAILED(view->mainFrame(&mainFrame)))
76 IWebFramePrivate* privateFrame = 0;
77 if (FAILED(mainFrame->QueryInterface(&privateFrame))) {
82 privateFrame->frameBounds(rect);
87 rect->right = (::GetDeviceCaps(dc, LOGPIXELSX) * 6.5) - MARGIN;
88 rect->bottom = (::GetDeviceCaps(dc, LOGPIXELSY) * 11) - MARGIN;
91 privateFrame->Release();
97 HRESULT PrintWebUIDelegate::webViewHeaderHeight(IWebView* webView, float* height)
99 if (!webView || !height)
104 TEXTMETRIC textMetric;
105 ::GetTextMetrics(dc, &textMetric);
108 *height = 1.1 * textMetric.tmHeight;
113 HRESULT PrintWebUIDelegate::webViewFooterHeight(IWebView* webView, float* height)
115 if (!webView || !height)
120 TEXTMETRIC textMetric;
121 ::GetTextMetrics(dc, &textMetric);
124 *height = 1.1 * textMetric.tmHeight;
129 HRESULT PrintWebUIDelegate::drawHeaderInRect(
130 /* [in] */ IWebView* webView,
131 /* [in] */ RECT* rect,
132 /* [in] */ OLE_HANDLE drawingContext)
134 if (!webView || !rect)
137 // Turn off header for now.
138 HDC dc = reinterpret_cast<HDC>(drawingContext);
140 HGDIOBJ hFont = ::GetStockObject(ANSI_VAR_FONT);
141 HGDIOBJ hOldFont = ::SelectObject(dc, hFont);
143 LPCWSTR header = L"[Sample Header]";
144 size_t length = wcslen(header);
146 int rc = ::DrawTextW(dc, header, length, rect, DT_LEFT | DT_NOCLIP | DT_VCENTER | DT_SINGLELINE);
147 ::SelectObject(dc, hOldFont);
152 ::MoveToEx(dc, rect->left, rect->bottom, 0);
153 HGDIOBJ hPen = ::GetStockObject(BLACK_PEN);
154 HGDIOBJ hOldPen = ::SelectObject(dc, hPen);
155 ::LineTo(dc, rect->right, rect->bottom);
156 ::SelectObject(dc, hOldPen);
161 HRESULT PrintWebUIDelegate::drawFooterInRect(
162 /* [in] */ IWebView* webView,
163 /* [in] */ RECT* rect,
164 /* [in] */ OLE_HANDLE drawingContext,
165 /* [in] */ UINT pageIndex,
166 /* [in] */ UINT pageCount)
168 if (!webView || !rect)
171 HDC dc = reinterpret_cast<HDC>(drawingContext);
173 HGDIOBJ hFont = ::GetStockObject(ANSI_VAR_FONT);
174 HGDIOBJ hOldFont = ::SelectObject(dc, hFont);
176 LPCWSTR footer = L"[Sample Footer]";
177 size_t length = wcslen(footer);
179 // Add a line, 1/10th inch above the footer text from left margin to right margin.
180 ::MoveToEx(dc, rect->left, rect->top, 0);
181 HGDIOBJ hPen = ::GetStockObject(BLACK_PEN);
182 HGDIOBJ hOldPen = ::SelectObject(dc, hPen);
183 ::LineTo(dc, rect->right, rect->top);
184 ::SelectObject(dc, hOldPen);
186 int rc = ::DrawTextW(dc, footer, length, rect, DT_LEFT | DT_NOCLIP | DT_VCENTER | DT_SINGLELINE);
187 ::SelectObject(dc, hOldFont);