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