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 "EditorClient.h" 32 #include "FloatRect.h" 33 #include "FrameLoader.h" 34 #include "FrameLoadRequest.h" 35 #include "FrameView.h" 36 #include "GraphicsContext.h" 37 #include "HTMLIFrameElement.h" 38 #include "HTMLNames.h" 39 #include "HTMLTableCellElement.h" 40 #include "KeyboardEvent.h" 41 #include "NP_jsobject.h" 42 #include "npruntime_impl.h" 43 #include "Page.h" 44 #include "Plugin.h" 45 #include "RegularExpression.h" 46 #include "RenderFrame.h" 47 #include "RenderTableCell.h" 48 #include "RenderView.h" 49 #include "ResourceHandle.h" 50 #include "runtime_root.h" 51 #include "Settings.h" 52 #include "TextResourceDecoder.h" 53 54 #include <windows.h> 55 56 using std::min; 57 58 namespace WebCore { 59 60 using namespace HTMLNames; 61 62 extern HDC g_screenDC; 63 64 void computePageRectsForFrame(Frame* frame, const IntRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, Vector<IntRect>& pages, int& outPageHeight) 65 { 66 ASSERT(frame); 67 68 pages.clear(); 69 outPageHeight = 0; 70 71 if (!frame->document() || !frame->view() || !frame->document()->renderer()) 72 return; 73 74 RenderView* root = toRenderView(frame->document()->renderer()); 75 76 if (!root) { 77 LOG_ERROR("document to be printed has no renderer"); 78 return; 79 } 80 81 if (userScaleFactor <= 0) { 82 LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor); 83 return; 84 } 85 86 float ratio = (float)printRect.height() / (float)printRect.width(); 87 88 float pageWidth = (float) root->overflowWidth(); 89 float pageHeight = pageWidth * ratio; 90 outPageHeight = (int) pageHeight; // this is the height of the page adjusted by margins 91 pageHeight -= (headerHeight + footerHeight); 92 93 if (pageHeight <= 0) { 94 LOG_ERROR("pageHeight has bad value %.2f", pageHeight); 95 return; 96 } 97 98 float currPageHeight = pageHeight / userScaleFactor; 99 float docHeight = root->layer()->height(); 100 float docWidth = root->layer()->width(); 101 float currPageWidth = pageWidth / userScaleFactor; 102 103 104 // always return at least one page, since empty files should print a blank page 105 float printedPagesHeight = 0.0; 106 do { 107 float proposedBottom = min(docHeight, printedPagesHeight + pageHeight); 108 frame->view()->adjustPageHeight(&proposedBottom, printedPagesHeight, proposedBottom, printedPagesHeight); 109 currPageHeight = max(1.0f, proposedBottom - printedPagesHeight); 110 111 pages.append(IntRect(0, printedPagesHeight, currPageWidth, currPageHeight)); 112 printedPagesHeight += currPageHeight; 113 } while (printedPagesHeight < docHeight); 114 } 115 116 HBITMAP imageFromSelection(Frame* frame, bool forceBlackText) 117 { 118 if (!frame->view()) 119 return 0; 120 121 frame->view()->setPaintRestriction(forceBlackText ? PaintRestrictionSelectionOnlyBlackText : PaintRestrictionSelectionOnly); 122 FloatRect fr = frame->selectionBounds(); 123 IntRect ir((int)fr.x(), (int)fr.y(), (int)fr.width(), (int)fr.height()); 124 if (ir.isEmpty()) 125 return 0; 126 127 int w; 128 int h; 129 FrameView* view = frame->view(); 130 if (view->parent()) { 131 ir.setLocation(view->parent()->convertChildToSelf(view, ir.location())); 132 w = ir.width() * view->scale() + 0.5; 133 h = ir.height() * view->scale() + 0.5; 134 } else { 135 ir = view->contentsToWindow(ir); 136 w = ir.width(); 137 h = ir.height(); 138 } 139 140 OwnPtr<HDC> bmpDC(CreateCompatibleDC(g_screenDC)); 141 HBITMAP hBmp = MemoryManager::createCompatibleBitmap(g_screenDC, w, h); 142 if (!hBmp) 143 return 0; 144 145 HBITMAP hbmpOld = (HBITMAP)SelectObject(bmpDC.get(), hBmp); 146 147 { 148 GraphicsContext gc(bmpDC.get()); 149 frame->document()->updateLayout(); 150 view->paint(&gc, ir); 151 } 152 153 SelectObject(bmpDC.get(), hbmpOld); 154 155 frame->view()->setPaintRestriction(PaintRestrictionNone); 156 157 return hBmp; 158 } 159 160 DragImageRef Frame::dragImageForSelection() 161 { 162 if (selection()->isRange()) 163 return imageFromSelection(this, false); 164 165 return 0; 166 } 167 168 } // namespace WebCore 169