FrameWin.cpp 6.24 KB
Newer Older
1
/*
darin@apple.com's avatar
darin@apple.com committed
2
 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "FrameWin.h"

aroben's avatar
aroben committed
29 30 31 32 33
#include <winsock2.h>
#include <windows.h>

#include "AffineTransform.h"
#include "FloatRect.h"
darin's avatar
darin committed
34
#include "Document.h"
35 36
#include "EditorClient.h"
#include "FrameLoader.h"
weinig's avatar
weinig committed
37
#include "FrameLoadRequest.h"
darin's avatar
darin committed
38
#include "FramePrivate.h"
39
#include "FrameView.h"
aroben's avatar
aroben committed
40 41 42 43 44
#include "HTMLIFrameElement.h"
#include "HTMLNames.h"
#include "HTMLTableCellElement.h"
#include "KeyboardEvent.h"
#include "NP_jsobject.h"
darin@apple.com's avatar
darin@apple.com committed
45
#include "NotImplemented.h"
aroben's avatar
aroben committed
46
#include "Page.h"
mjs's avatar
mjs committed
47
#include "Plugin.h"
aroben's avatar
aroben committed
48 49 50
#include "PluginDatabaseWin.h"
#include "PluginViewWin.h"
#include "RegularExpression.h"
darin's avatar
darin committed
51
#include "RenderFrame.h"
aroben's avatar
aroben committed
52 53
#include "RenderTableCell.h"
#include "RenderView.h"
mjs's avatar
mjs committed
54
#include "ResourceHandle.h"
aroben's avatar
aroben committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include "TextResourceDecoder.h"
#include "kjs_proxy.h"
#include "kjs_window.h"
#include "npruntime_impl.h"
#include "runtime_root.h"
#include "GraphicsContext.h"
#include "Settings.h"

#if PLATFORM(CG)
#include <CoreGraphics/CoreGraphics.h>
#endif

using std::min;
using namespace KJS::Bindings;
mjs's avatar
mjs committed
69

70 71
namespace WebCore {

aroben's avatar
aroben committed
72 73
using namespace HTMLNames;

bdash's avatar
bdash committed
74
void Frame::clearPlatformScriptObjects()
75 76 77
{
}

aroben's avatar
aroben committed
78
KJS::Bindings::Instance* Frame::createScriptInstanceForWidget(Widget* widget)
79
{
aroben's avatar
aroben committed
80 81 82 83 84
    // FIXME: Ideally we'd have an isPluginView() here but we can't add that to the open source tree right now.
    if (widget->isFrameView())
        return 0;

    return static_cast<PluginViewWin*>(widget)->bindingInstance();
85 86
}

aroben's avatar
aroben committed
87

88
void computePageRectsForFrame(Frame* frame, const IntRect& printRect, float headerHeight, float footerHeight, float userScaleFactor,Vector<IntRect>& pages, int& outPageHeight)
89
{
aroben's avatar
aroben committed
90 91
    ASSERT(frame);

92
    pages.clear();
93
    outPageHeight = 0;
aroben's avatar
aroben committed
94

95 96
    if (!frame->document() || !frame->view() || !frame->document()->renderer())
        return;
aroben's avatar
aroben committed
97 98 99 100 101
 
    RenderView* root = static_cast<RenderView *>(frame->document()->renderer());

    if (!root) {
        LOG_ERROR("document to be printed has no renderer");
102
        return;
aroben's avatar
aroben committed
103 104 105 106
    }

    if (userScaleFactor <= 0) {
        LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor);
107
        return;
aroben's avatar
aroben committed
108 109 110 111 112 113
    }
    
    float ratio = (float)printRect.height() / (float)printRect.width();
 
    float pageWidth  = (float) root->docWidth();
    float pageHeight = pageWidth * ratio;
114
    outPageHeight = (int) pageHeight;   // this is the height of the page adjusted by margins
115
    pageHeight -= (headerHeight + footerHeight);
aroben's avatar
aroben committed
116 117 118

    if (pageHeight <= 0) {
        LOG_ERROR("pageHeight has bad value %.2f", pageHeight);
119
        return;
aroben's avatar
aroben committed
120 121 122 123 124 125
    }

    float currPageHeight = pageHeight / userScaleFactor;
    float docHeight      = root->layer()->height();
    float docWidth       = root->layer()->width();
    float currPageWidth  = pageWidth / userScaleFactor;
126

aroben's avatar
aroben committed
127
    
128 129 130 131 132 133
    // always return at least one page, since empty files should print a blank page
    float printedPagesHeight = 0.0;
    do {
        float proposedBottom = min(docHeight, printedPagesHeight + pageHeight);
        frame->adjustPageHeight(&proposedBottom, printedPagesHeight, proposedBottom, printedPagesHeight);
        currPageHeight = max(1.0f, proposedBottom - printedPagesHeight);
aroben's avatar
aroben committed
134
       
135 136 137
        pages.append(IntRect(0,printedPagesHeight,currPageWidth,currPageHeight));
        printedPagesHeight += currPageHeight;
    } while (printedPagesHeight < docHeight);
138 139
}

aroben's avatar
aroben committed
140
static void drawRectIntoContext(IntRect rect, FrameView* view, GraphicsContext* gc)
141
{
aroben's avatar
aroben committed
142 143 144 145 146 147 148
    IntSize offset = view->scrollOffset();
    rect.move(-offset.width(), -offset.height());
    rect = view->convertToContainingWindow(rect);

    gc->concatCTM(AffineTransform().translate(-rect.x(), -rect.y()));

    view->paint(gc, rect);
149 150
}

151
HBITMAP imageFromSelection(Frame* frame, bool forceBlackText)
152
{
153
    frame->setPaintRestriction(forceBlackText ? PaintRestrictionSelectionOnlyBlackText : PaintRestrictionSelectionOnly);
aroben's avatar
aroben committed
154 155
    FloatRect fr = frame->selectionRect();
    IntRect ir((int)fr.x(), (int)fr.y(),(int)fr.width(),(int)fr.height());
rwlbuis's avatar
rwlbuis committed
156

aroben's avatar
aroben committed
157 158 159 160 161
    void* bits;
    HDC hdc = CreateCompatibleDC(0);
    int w = ir.width();
    int h = ir.height();
    BITMAPINFO bmp = { { sizeof(BITMAPINFOHEADER), w, h, 1, 32 } };
162

aroben's avatar
aroben committed
163 164 165 166 167 168 169 170 171 172
    HBITMAP hbmp = CreateDIBSection(0, &bmp, DIB_RGB_COLORS, (void**)&bits, 0, 0);
    HBITMAP hbmpOld = (HBITMAP)SelectObject(hdc, hbmp);
    CGColorSpaceRef deviceRGB = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate((void*)bits, w, h,
        8, w * sizeof(RGBQUAD), deviceRGB, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGColorSpaceRelease(deviceRGB);
    CGContextSaveGState(context);

    GraphicsContext gc(context);

173
    frame->document()->updateLayout();
aroben's avatar
aroben committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    drawRectIntoContext(ir, frame->view(), &gc);

    CGContextRelease(context);
    SelectObject(hdc, hbmpOld);
    DeleteDC(hdc);

    frame->setPaintRestriction(PaintRestrictionNone);

    return hbmp;
}

DragImageRef Frame::dragImageForSelection()
{    
    if (selectionController()->isRange())
        return imageFromSelection(this, false);
189

aroben's avatar
aroben committed
190
    return 0;
191 192
}

aroben's avatar
aroben committed
193
void Frame::dashboardRegionsChanged()
194 195 196
{
}

weinig's avatar
weinig committed
197
} // namespace WebCore