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"
36 #include <WebKit/WebKitCOMAPI.h>
38 static const int MARGIN = 20;
40 HRESULT PrintWebUIDelegate::QueryInterface(REFIID riid, void** ppvObject)
43 if (IsEqualIID(riid, IID_IUnknown))
44 *ppvObject = static_cast<IWebUIDelegate*>(this);
45 else if (IsEqualIID(riid, IID_IWebUIDelegate))
46 *ppvObject = static_cast<IWebUIDelegate*>(this);
54 ULONG PrintWebUIDelegate::AddRef(void)
59 ULONG PrintWebUIDelegate::Release(void)
61 ULONG newRef = --m_refCount;
68 HRESULT PrintWebUIDelegate::webViewPrintingMarginRect(IWebView* view, RECT* rect)
73 IWebFrame* mainFrame = 0;
74 if (FAILED(view->mainFrame(&mainFrame)))
77 IWebFramePrivate* privateFrame = 0;
78 if (FAILED(mainFrame->QueryInterface(&privateFrame))) {
83 privateFrame->frameBounds(rect);
88 rect->right = (::GetDeviceCaps(dc, LOGPIXELSX) * 6.5) - MARGIN;
89 rect->bottom = (::GetDeviceCaps(dc, LOGPIXELSY) * 11) - MARGIN;
92 privateFrame->Release();
98 HRESULT PrintWebUIDelegate::webViewHeaderHeight(IWebView* webView, float* height)
100 if (!webView || !height)
105 TEXTMETRIC textMetric;
106 ::GetTextMetrics(dc, &textMetric);
109 *height = 1.1 * textMetric.tmHeight;
114 HRESULT PrintWebUIDelegate::webViewFooterHeight(IWebView* webView, float* height)
116 if (!webView || !height)
121 TEXTMETRIC textMetric;
122 ::GetTextMetrics(dc, &textMetric);
125 *height = 1.1 * textMetric.tmHeight;
130 HRESULT PrintWebUIDelegate::drawHeaderInRect(
131 /* [in] */ IWebView *webView,
132 /* [in] */ RECT *rect,
133 /* [in] */ OLE_HANDLE drawingContext)
135 if (!webView || !rect)
138 // Turn off header for now.
139 HDC dc = reinterpret_cast<HDC>(drawingContext);
141 HFONT hFont = (HFONT)::GetStockObject(ANSI_VAR_FONT);
142 HFONT hOldFont = (HFONT)::SelectObject(dc, hFont);
144 LPCTSTR header(_T("[Sample Header]"));
145 int length = _tcslen(header);
147 int rc = ::DrawText(dc, header, length, rect, DT_LEFT | DT_NOCLIP | DT_VCENTER | DT_SINGLELINE);
148 ::SelectObject(dc, hOldFont);
153 ::MoveToEx(dc, rect->left, rect->bottom, 0);
154 HPEN hPen = (HPEN)::GetStockObject(BLACK_PEN);
155 HPEN hOldPen = (HPEN)::SelectObject(dc, hPen);
156 ::LineTo(dc, rect->right, rect->bottom);
157 ::SelectObject(dc, hOldPen);
162 HRESULT PrintWebUIDelegate::drawFooterInRect(
163 /* [in] */ IWebView *webView,
164 /* [in] */ RECT *rect,
165 /* [in] */ OLE_HANDLE drawingContext,
166 /* [in] */ UINT pageIndex,
167 /* [in] */ UINT pageCount)
169 if (!webView || !rect)
172 HDC dc = reinterpret_cast<HDC>(drawingContext);
174 HFONT hFont = (HFONT)::GetStockObject(ANSI_VAR_FONT);
175 HFONT hOldFont = (HFONT)::SelectObject(dc, hFont);
177 LPCTSTR footer(_T("[Sample Footer]"));
178 int length = _tcslen(footer);
180 // Add a line, 1/10th inch above the footer text from left margin to right margin.
181 ::MoveToEx(dc, rect->left, rect->top, 0);
182 HPEN hPen = (HPEN)::GetStockObject(BLACK_PEN);
183 HPEN hOldPen = (HPEN)::SelectObject(dc, hPen);
184 ::LineTo(dc, rect->right, rect->top);
185 ::SelectObject(dc, hOldPen);
187 int rc = ::DrawText(dc, footer, length, rect, DT_LEFT | DT_NOCLIP | DT_VCENTER | DT_SINGLELINE);
188 ::SelectObject(dc, hOldFont);