HRESULT STDMETHODCALLTYPE WebKitUIDelegate::webViewPrintingMarginRect(
    /* [in] */ IWebView *webView,
    /* [retval][out] */ RECT *rect)
{

    if (!webView || !rect)
        return E_POINTER;

    IWebFrame* mainFrame = 0;
    if (FAILED(webView->mainFrame(&mainFrame)))
        return E_FAIL;

    IWebFramePrivate* framePrivate = 0;
    if (FAILED(mainFrame->QueryInterface(&framePrivate))) {
        mainFrame->Release();
        return E_FAIL;
    }

    framePrivate->frameBounds(rect);
    rect->left += MARGIN;
    rect->top += MARGIN;
    rect->right -= MARGIN;
    rect->bottom -= MARGIN;

    framePrivate->Release();
    mainFrame->Release();

    return S_OK;
}
Exemple #2
0
HRESULT PrintWebUIDelegate::webViewPrintingMarginRect(IWebView* view, RECT* rect)
{
    if (!view || !rect)
        return E_POINTER;

    IWebFrame* mainFrame = 0;
    if (FAILED(view->mainFrame(&mainFrame)))
        return E_FAIL;

    IWebFramePrivate* privateFrame = 0;
    if (FAILED(mainFrame->QueryInterface(&privateFrame))) {
        mainFrame->Release();
        return E_FAIL;
    }

    privateFrame->frameBounds(rect);

    rect->left += MARGIN;
    rect->top += MARGIN;
    HDC dc = ::GetDC(0);
    rect->right = (::GetDeviceCaps(dc, LOGPIXELSX) * 6.5) - MARGIN;
    rect->bottom = (::GetDeviceCaps(dc, LOGPIXELSY) * 11) - MARGIN;
    ::ReleaseDC(0, dc);

    privateFrame->Release();
    mainFrame->Release();

    return S_OK;
}
void PrintView(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC printDC = getPrinterDC();
    if (!printDC) {
        ::MessageBox(0, _T("Error creating printing DC"), _T("Error"), MB_APPLMODAL | MB_OK);
        return;
    }

    if (::SetAbortProc(printDC, AbortProc) == SP_ERROR) {
        ::MessageBox(0, _T("Error setting up AbortProc"), _T("Error"), MB_APPLMODAL | MB_OK);
        return;
    }

    IWebFrame* frame = 0;
    if (FAILED(gWebView->mainFrame(&frame)))
        goto exit;

    IWebFramePrivate* framePrivate = 0;
    if (FAILED(frame->QueryInterface(&framePrivate)))
        goto exit;

    framePrivate->setInPrintingMode(TRUE, printDC);

    UINT pageCount = 0;
    framePrivate->getPrintedPageCount(printDC, &pageCount);

    DOCINFO di;
    initDocStruct(&di, _T("WebKit Doc"));
    ::StartDoc(printDC, &di);

    // FIXME: Need CoreGraphics implementation
    void* graphicsContext = 0;
    for (size_t page = 1; page <= pageCount; ++page) {
        ::StartPage(printDC);
        framePrivate->spoolPages(printDC, page, page, graphicsContext);
        ::EndPage(printDC);
    }

    framePrivate->setInPrintingMode(FALSE, printDC);

    ::EndDoc(printDC);
    ::DeleteDC(printDC);

exit:
    if (frame)
        frame->Release();
    if (framePrivate)
        framePrivate->Release();
}
HRESULT STDMETHODCALLTYPE WebKitUIDelegate::printFrame(
    /* [in] */ IWebView *webView,
    /* [in] */ IWebFrame *frame)
{
    // This code is originally based on the PrintView function in
    // the WinLauncher code of WebKit. We should periodically check
    // there to ensure that this doesn't become obsolete.

    // Open a printing dialog to fetch the HDC of the desired printer.
    PRINTDLG dialog;
    ZeroMemory(&dialog, sizeof(PRINTDLG));
    dialog.lStructSize = sizeof(PRINTDLG);
    dialog.Flags = PD_PRINTSETUP | PD_RETURNDC;
    BOOL dialogResult = ::PrintDlg(&dialog);

    if (!dialogResult) // Error or cancel.
    {
        DWORD reason = CommDlgExtendedError();
        if (!reason) // User cancelled.
            return S_OK;

        logger->Error("Could not print page, dialog error code: %i",
            reason);
        return E_FAIL;
    }

    HDC hdc = dialog.hDC;
    if (!hdc)
    {
        logger->Error("Could not fetch printer HDC.");
        return E_FAIL;
    }

    if (::SetAbortProc(hdc, AbortProc) == SP_ERROR)
    {
        logger->Error("Could not set printer AbortProc.");
        return E_FAIL;
    }
    
    IWebFrame* mainFrame = 0;
    if (FAILED(webView->mainFrame(&mainFrame)))
    {
        return E_POINTER;
    }

    IWebFramePrivate* framePrivate = 0;
    if (FAILED(mainFrame->QueryInterface(&framePrivate)))
    {
        mainFrame->Release();
        return E_POINTER;
    }

    framePrivate->setInPrintingMode(TRUE, hdc);
    UINT pageCount = 0;
    framePrivate->getPrintedPageCount(hdc, &pageCount);

    DOCINFO docInfo;
    ZeroMemory(&docInfo, sizeof(DOCINFO));
    docInfo.cbSize = sizeof(DOCINFO);
    docInfo.lpszDocName = _T("Titanium Document");
    ::StartDoc(hdc, &docInfo);

    void* graphicsContext = 0;
    for (size_t page = 1; page <= pageCount; page++)
    {
        ::StartPage(hdc);
        framePrivate->spoolPages(hdc, page, page, graphicsContext);
        ::EndPage(hdc);
    }

    framePrivate->setInPrintingMode(FALSE, hdc);

    ::EndDoc(hdc);
    ::DeleteDC(hdc);

    if (mainFrame)
        mainFrame->Release();
    if (framePrivate)
        framePrivate->Release();

    return S_OK;

}