1 /*
2 * Copyright (C) 2009 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2009 Brent Fulgham. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 *
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.
25 */
26
27 #include "stdafx.h"
28 #include "PrintWebUIDelegate.h"
29
30 #include <WebKit/WebKitCOMAPI.h>
31 #include <commctrl.h>
32 #include <commdlg.h>
33 #include <objbase.h>
34 #include <shlwapi.h>
35 #include <wininet.h>
36
37 static const int MARGIN = 20;
38
QueryInterface(REFIID riid,void ** ppvObject)39 HRESULT PrintWebUIDelegate::QueryInterface(REFIID riid, void** ppvObject)
40 {
41 *ppvObject = 0;
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);
46 else
47 return E_NOINTERFACE;
48
49 AddRef();
50 return S_OK;
51 }
52
AddRef(void)53 ULONG PrintWebUIDelegate::AddRef(void)
54 {
55 return ++m_refCount;
56 }
57
Release(void)58 ULONG PrintWebUIDelegate::Release(void)
59 {
60 ULONG newRef = --m_refCount;
61 if (!newRef)
62 delete this;
63
64 return newRef;
65 }
66
webViewPrintingMarginRect(IWebView * view,RECT * rect)67 HRESULT PrintWebUIDelegate::webViewPrintingMarginRect(IWebView* view, RECT* rect)
68 {
69 if (!view || !rect)
70 return E_POINTER;
71
72 IWebFrame* mainFrame = 0;
73 if (FAILED(view->mainFrame(&mainFrame)))
74 return E_FAIL;
75
76 IWebFramePrivate* privateFrame = 0;
77 if (FAILED(mainFrame->QueryInterface(&privateFrame))) {
78 mainFrame->Release();
79 return E_FAIL;
80 }
81
82 privateFrame->frameBounds(rect);
83
84 rect->left += MARGIN;
85 rect->top += MARGIN;
86 HDC dc = ::GetDC(0);
87 rect->right = (::GetDeviceCaps(dc, LOGPIXELSX) * 6.5) - MARGIN;
88 rect->bottom = (::GetDeviceCaps(dc, LOGPIXELSY) * 11) - MARGIN;
89 ::ReleaseDC(0, dc);
90
91 privateFrame->Release();
92 mainFrame->Release();
93
94 return S_OK;
95 }
96
webViewHeaderHeight(IWebView * webView,float * height)97 HRESULT PrintWebUIDelegate::webViewHeaderHeight(IWebView* webView, float* height)
98 {
99 if (!webView || !height)
100 return E_POINTER;
101
102 HDC dc = ::GetDC(0);
103
104 TEXTMETRIC textMetric;
105 ::GetTextMetrics(dc, &textMetric);
106 ::ReleaseDC(0, dc);
107
108 *height = 1.1 * textMetric.tmHeight;
109
110 return S_OK;
111 }
112
webViewFooterHeight(IWebView * webView,float * height)113 HRESULT PrintWebUIDelegate::webViewFooterHeight(IWebView* webView, float* height)
114 {
115 if (!webView || !height)
116 return E_POINTER;
117
118 HDC dc = ::GetDC(0);
119
120 TEXTMETRIC textMetric;
121 ::GetTextMetrics(dc, &textMetric);
122 ::ReleaseDC(0, dc);
123
124 *height = 1.1 * textMetric.tmHeight;
125
126 return S_OK;
127 }
128
drawHeaderInRect(IWebView * webView,RECT * rect,OLE_HANDLE drawingContext)129 HRESULT PrintWebUIDelegate::drawHeaderInRect(
130 /* [in] */ IWebView* webView,
131 /* [in] */ RECT* rect,
132 /* [in] */ OLE_HANDLE drawingContext)
133 {
134 if (!webView || !rect)
135 return E_POINTER;
136
137 // Turn off header for now.
138 HDC dc = reinterpret_cast<HDC>(drawingContext);
139
140 HGDIOBJ hFont = ::GetStockObject(ANSI_VAR_FONT);
141 HGDIOBJ hOldFont = ::SelectObject(dc, hFont);
142
143 LPCWSTR header = L"[Sample Header]";
144 size_t length = wcslen(header);
145
146 int rc = ::DrawTextW(dc, header, length, rect, DT_LEFT | DT_NOCLIP | DT_VCENTER | DT_SINGLELINE);
147 ::SelectObject(dc, hOldFont);
148
149 if (!rc)
150 return E_FAIL;
151
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);
157
158 return S_OK;
159 }
160
drawFooterInRect(IWebView * webView,RECT * rect,OLE_HANDLE drawingContext,UINT pageIndex,UINT pageCount)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)
167 {
168 if (!webView || !rect)
169 return E_POINTER;
170
171 HDC dc = reinterpret_cast<HDC>(drawingContext);
172
173 HGDIOBJ hFont = ::GetStockObject(ANSI_VAR_FONT);
174 HGDIOBJ hOldFont = ::SelectObject(dc, hFont);
175
176 LPCWSTR footer = L"[Sample Footer]";
177 size_t length = wcslen(footer);
178
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);
185
186 int rc = ::DrawTextW(dc, footer, length, rect, DT_LEFT | DT_NOCLIP | DT_VCENTER | DT_SINGLELINE);
187 ::SelectObject(dc, hOldFont);
188
189 if (!rc)
190 return E_FAIL;
191
192 return S_OK;
193 }
194