PrintWebUIDelegate.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include "stdafx.h"
  27. #include "PrintWebUIDelegate.h"
  28. #include <WebKit/WebKitCOMAPI.h>
  29. #include <commctrl.h>
  30. #include <commdlg.h>
  31. #include <objbase.h>
  32. #include <shlwapi.h>
  33. #include <wininet.h>
  34. static const int MARGIN = 20;
  35. HRESULT PrintWebUIDelegate::QueryInterface(REFIID riid, void** ppvObject)
  36. {
  37. *ppvObject = 0;
  38. if (IsEqualIID(riid, IID_IUnknown))
  39. *ppvObject = static_cast<IWebUIDelegate*>(this);
  40. else if (IsEqualIID(riid, IID_IWebUIDelegate))
  41. *ppvObject = static_cast<IWebUIDelegate*>(this);
  42. else
  43. return E_NOINTERFACE;
  44. AddRef();
  45. return S_OK;
  46. }
  47. ULONG PrintWebUIDelegate::AddRef(void)
  48. {
  49. return ++m_refCount;
  50. }
  51. ULONG PrintWebUIDelegate::Release(void)
  52. {
  53. ULONG newRef = --m_refCount;
  54. if (!newRef)
  55. delete this;
  56. return newRef;
  57. }
  58. HRESULT PrintWebUIDelegate::webViewPrintingMarginRect(IWebView* view, RECT* rect)
  59. {
  60. if (!view || !rect)
  61. return E_POINTER;
  62. IWebFrame* mainFrame = 0;
  63. if (FAILED(view->mainFrame(&mainFrame)))
  64. return E_FAIL;
  65. IWebFramePrivate* privateFrame = 0;
  66. if (FAILED(mainFrame->QueryInterface(&privateFrame))) {
  67. mainFrame->Release();
  68. return E_FAIL;
  69. }
  70. privateFrame->frameBounds(rect);
  71. rect->left += MARGIN;
  72. rect->top += MARGIN;
  73. HDC dc = ::GetDC(0);
  74. rect->right = (::GetDeviceCaps(dc, LOGPIXELSX) * 6.5) - MARGIN;
  75. rect->bottom = (::GetDeviceCaps(dc, LOGPIXELSY) * 11) - MARGIN;
  76. ::ReleaseDC(0, dc);
  77. privateFrame->Release();
  78. mainFrame->Release();
  79. return S_OK;
  80. }
  81. HRESULT PrintWebUIDelegate::webViewHeaderHeight(IWebView* webView, float* height)
  82. {
  83. if (!webView || !height)
  84. return E_POINTER;
  85. HDC dc = ::GetDC(0);
  86. TEXTMETRIC textMetric;
  87. ::GetTextMetrics(dc, &textMetric);
  88. ::ReleaseDC(0, dc);
  89. *height = 1.1 * textMetric.tmHeight;
  90. return S_OK;
  91. }
  92. HRESULT PrintWebUIDelegate::webViewFooterHeight(IWebView* webView, float* height)
  93. {
  94. if (!webView || !height)
  95. return E_POINTER;
  96. HDC dc = ::GetDC(0);
  97. TEXTMETRIC textMetric;
  98. ::GetTextMetrics(dc, &textMetric);
  99. ::ReleaseDC(0, dc);
  100. *height = 1.1 * textMetric.tmHeight;
  101. return S_OK;
  102. }
  103. HRESULT PrintWebUIDelegate::drawHeaderInRect(
  104. /* [in] */ IWebView* webView,
  105. /* [in] */ RECT* rect,
  106. /* [in] */ OLE_HANDLE drawingContext)
  107. {
  108. if (!webView || !rect)
  109. return E_POINTER;
  110. // Turn off header for now.
  111. HDC dc = reinterpret_cast<HDC>(drawingContext);
  112. HGDIOBJ hFont = ::GetStockObject(ANSI_VAR_FONT);
  113. HGDIOBJ hOldFont = ::SelectObject(dc, hFont);
  114. LPCWSTR header = L"[Sample Header]";
  115. size_t length = wcslen(header);
  116. int rc = ::DrawTextW(dc, header, length, rect, DT_LEFT | DT_NOCLIP | DT_VCENTER | DT_SINGLELINE);
  117. ::SelectObject(dc, hOldFont);
  118. if (!rc)
  119. return E_FAIL;
  120. ::MoveToEx(dc, rect->left, rect->bottom, 0);
  121. HGDIOBJ hPen = ::GetStockObject(BLACK_PEN);
  122. HGDIOBJ hOldPen = ::SelectObject(dc, hPen);
  123. ::LineTo(dc, rect->right, rect->bottom);
  124. ::SelectObject(dc, hOldPen);
  125. return S_OK;
  126. }
  127. HRESULT PrintWebUIDelegate::drawFooterInRect(
  128. /* [in] */ IWebView* webView,
  129. /* [in] */ RECT* rect,
  130. /* [in] */ OLE_HANDLE drawingContext,
  131. /* [in] */ UINT pageIndex,
  132. /* [in] */ UINT pageCount)
  133. {
  134. if (!webView || !rect)
  135. return E_POINTER;
  136. HDC dc = reinterpret_cast<HDC>(drawingContext);
  137. HGDIOBJ hFont = ::GetStockObject(ANSI_VAR_FONT);
  138. HGDIOBJ hOldFont = ::SelectObject(dc, hFont);
  139. LPCWSTR footer = L"[Sample Footer]";
  140. size_t length = wcslen(footer);
  141. // Add a line, 1/10th inch above the footer text from left margin to right margin.
  142. ::MoveToEx(dc, rect->left, rect->top, 0);
  143. HGDIOBJ hPen = ::GetStockObject(BLACK_PEN);
  144. HGDIOBJ hOldPen = ::SelectObject(dc, hPen);
  145. ::LineTo(dc, rect->right, rect->top);
  146. ::SelectObject(dc, hOldPen);
  147. int rc = ::DrawTextW(dc, footer, length, rect, DT_LEFT | DT_NOCLIP | DT_VCENTER | DT_SINGLELINE);
  148. ::SelectObject(dc, hOldFont);
  149. if (!rc)
  150. return E_FAIL;
  151. return S_OK;
  152. }