1/*
2 * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3 * Copyright (C) 2007-2009 Torch Mobile, Inc.
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 COMPUTER, 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 COMPUTER, 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 "config.h"
28#include "Frame.h"
29
30#include "Document.h"
31#include "FloatRect.h"
32#include "FrameSelection.h"
33#include "FrameView.h"
34#include "GraphicsContext.h"
35#include "HTMLIFrameElement.h"
36#include "HTMLNames.h"
37#include "HTMLTableCellElement.h"
38#include "KeyboardEvent.h"
39#include "NotImplemented.h"
40#include "Page.h"
41#include "RenderFrame.h"
42#include "RenderLayer.h"
43#include "RenderView.h"
44#include "ResourceHandle.h"
45
46#include <windows.h>
47
48using std::min;
49
50namespace WebCore {
51
52using namespace HTMLNames;
53
54extern HDC g_screenDC;
55
56void computePageRectsForFrame(Frame* frame, const IntRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, Vector<IntRect>& pages, int& outPageHeight)
57{
58    ASSERT(frame);
59
60    pages.clear();
61    outPageHeight = 0;
62
63    if (!frame->document() || !frame->view() || !frame->document()->renderView())
64        return;
65
66    RenderView* root = frame->document()->renderView();
67
68    if (userScaleFactor <= 0) {
69        LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor);
70        return;
71    }
72
73    float ratio = (float)printRect.height() / (float)printRect.width();
74
75    float pageWidth  = (float) root->layoutOverflowRect().maxX();
76    float pageHeight = pageWidth * ratio;
77    outPageHeight = (int) pageHeight;   // this is the height of the page adjusted by margins
78    pageHeight -= (headerHeight + footerHeight);
79
80    if (pageHeight <= 0) {
81        LOG_ERROR("pageHeight has bad value %.2f", pageHeight);
82        return;
83    }
84
85    float currPageHeight = pageHeight / userScaleFactor;
86    float docHeight      = root->layer()->size().height();
87    float currPageWidth  = pageWidth / userScaleFactor;
88
89
90    // always return at least one page, since empty files should print a blank page
91    float printedPagesHeight = 0.0;
92    do {
93        float proposedBottom = min(docHeight, printedPagesHeight + pageHeight);
94        frame->view()->adjustPageHeightDeprecated(&proposedBottom, printedPagesHeight, proposedBottom, printedPagesHeight);
95        currPageHeight = max(1.0f, proposedBottom - printedPagesHeight);
96
97        pages.append(IntRect(0, printedPagesHeight, currPageWidth, currPageHeight));
98        printedPagesHeight += currPageHeight;
99    } while (printedPagesHeight < docHeight);
100}
101
102HBITMAP imageFromSelection(Frame* frame, bool forceBlackText)
103{
104    if (!frame->view())
105        return 0;
106
107    frame->view()->setPaintBehavior(PaintBehaviorSelectionOnly | (forceBlackText ? PaintBehaviorForceBlackText : 0));
108    FloatRect fr = frame->selection()->bounds();
109    IntRect ir((int)fr.x(), (int)fr.y(), (int)fr.width(), (int)fr.height());
110    if (ir.isEmpty())
111        return 0;
112
113    int w;
114    int h;
115    FrameView* view = frame->view();
116    if (view->parent()) {
117        ir.setLocation(view->parent()->convertChildToSelf(view, ir.location()));
118        w = ir.width() * frame->pageZoomFactor() + 0.5;
119        h = ir.height() * frame->pageZoomFactor() + 0.5;
120    } else {
121        ir = view->contentsToWindow(ir);
122        w = ir.width();
123        h = ir.height();
124    }
125
126    OwnPtr<HDC> bmpDC = adoptPtr(CreateCompatibleDC(g_screenDC));
127    HBITMAP hBmp = CreateCompatibleBitmap(g_screenDC, w, h);
128    if (!hBmp)
129        return 0;
130
131    HGDIOBJ hbmpOld = SelectObject(bmpDC.get(), hBmp);
132
133    {
134        GraphicsContext gc(bmpDC.get());
135        frame->document()->updateLayout();
136        view->paint(&gc, ir);
137    }
138
139    SelectObject(bmpDC.get(), hbmpOld);
140
141    frame->view()->setPaintBehavior(PaintBehaviorNormal);
142
143    return hBmp;
144}
145
146DragImageRef Frame::nodeImage(Node*)
147{
148    notImplemented();
149    return 0;
150}
151
152DragImageRef Frame::dragImageForSelection()
153{
154    if (selection()->isRange())
155        return imageFromSelection(this, false);
156
157    return 0;
158}
159
160} // namespace WebCore
161