webengine/osswebengine/WebCore/bridge/win/FrameWin.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  * 1. Redistributions of source code must retain the above copyright
       
     8  *    notice, this list of conditions and the following disclaimer.
       
     9  * 2. Redistributions in binary form must reproduce the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer in the
       
    11  *    documentation and/or other materials provided with the distribution.
       
    12  *
       
    13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
       
    14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
       
    17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
       
    24  */
       
    25 
       
    26 #include "config.h"
       
    27 #include "FrameWin.h"
       
    28 
       
    29 #include <winsock2.h>
       
    30 #include <windows.h>
       
    31 
       
    32 #include "AffineTransform.h"
       
    33 #include "FloatRect.h"
       
    34 #include "Document.h"
       
    35 #include "EditorClient.h"
       
    36 #include "FrameLoader.h"
       
    37 #include "FrameLoadRequest.h"
       
    38 #include "FramePrivate.h"
       
    39 #include "FrameView.h"
       
    40 #include "HTMLIFrameElement.h"
       
    41 #include "HTMLNames.h"
       
    42 #include "HTMLTableCellElement.h"
       
    43 #include "KeyboardEvent.h"
       
    44 #include "NP_jsobject.h"
       
    45 #include "Page.h"
       
    46 #include "Plugin.h"
       
    47 #include "PluginDatabaseWin.h"
       
    48 #include "PluginViewWin.h"
       
    49 #include "RegularExpression.h"
       
    50 #include "RenderFrame.h"
       
    51 #include "RenderTableCell.h"
       
    52 #include "RenderView.h"
       
    53 #include "ResourceHandle.h"
       
    54 #include "TextResourceDecoder.h"
       
    55 #include "kjs_proxy.h"
       
    56 #include "kjs_window.h"
       
    57 #include "npruntime_impl.h"
       
    58 #include "runtime_root.h"
       
    59 #include "GraphicsContext.h"
       
    60 #include "Settings.h"
       
    61 
       
    62 #if PLATFORM(CG)
       
    63 #include <CoreGraphics/CoreGraphics.h>
       
    64 #endif
       
    65 
       
    66 using std::min;
       
    67 using namespace KJS::Bindings;
       
    68 
       
    69 namespace WebCore {
       
    70 
       
    71 using namespace HTMLNames;
       
    72 
       
    73 void Frame::clearPlatformScriptObjects()
       
    74 {
       
    75 }
       
    76 
       
    77 KJS::Bindings::Instance* Frame::createScriptInstanceForWidget(Widget* widget)
       
    78 {
       
    79     // FIXME: Ideally we'd have an isPluginView() here but we can't add that to the open source tree right now.
       
    80     if (widget->isFrameView())
       
    81         return 0;
       
    82 
       
    83     return static_cast<PluginViewWin*>(widget)->bindingInstance();
       
    84 }
       
    85 
       
    86 
       
    87 void computePageRectsForFrame(Frame* frame, const IntRect& printRect, float headerHeight, float footerHeight, float userScaleFactor,Vector<IntRect>& pages, int& outPageHeight)
       
    88 {
       
    89     ASSERT(frame);
       
    90 
       
    91     pages.clear();
       
    92     outPageHeight = 0;
       
    93 
       
    94     if (!frame->document() || !frame->view() || !frame->document()->renderer())
       
    95         return;
       
    96  
       
    97     RenderView* root = static_cast<RenderView *>(frame->document()->renderer());
       
    98 
       
    99     if (!root) {
       
   100         LOG_ERROR("document to be printed has no renderer");
       
   101         return;
       
   102     }
       
   103 
       
   104     if (userScaleFactor <= 0) {
       
   105         LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor);
       
   106         return;
       
   107     }
       
   108     
       
   109     float ratio = (float)printRect.height() / (float)printRect.width();
       
   110  
       
   111     float pageWidth  = (float) root->docWidth();
       
   112     float pageHeight = pageWidth * ratio;
       
   113     outPageHeight = (int) pageHeight;   // this is the height of the page adjusted by margins
       
   114     pageHeight -= (headerHeight + footerHeight);
       
   115 
       
   116     if (pageHeight <= 0) {
       
   117         LOG_ERROR("pageHeight has bad value %.2f", pageHeight);
       
   118         return;
       
   119     }
       
   120 
       
   121     float currPageHeight = pageHeight / userScaleFactor;
       
   122     float docHeight      = root->layer()->height();
       
   123     float docWidth       = root->layer()->width();
       
   124     float currPageWidth  = pageWidth / userScaleFactor;
       
   125 
       
   126     
       
   127     // always return at least one page, since empty files should print a blank page
       
   128     float printedPagesHeight = 0.0;
       
   129     do {
       
   130         float proposedBottom = min(docHeight, printedPagesHeight + pageHeight);
       
   131         frame->adjustPageHeight(&proposedBottom, printedPagesHeight, proposedBottom, printedPagesHeight);
       
   132         currPageHeight = max(1.0f, proposedBottom - printedPagesHeight);
       
   133        
       
   134         pages.append(IntRect(0,printedPagesHeight,currPageWidth,currPageHeight));
       
   135         printedPagesHeight += currPageHeight;
       
   136     } while (printedPagesHeight < docHeight);
       
   137 }
       
   138 
       
   139 static void drawRectIntoContext(IntRect rect, FrameView* view, GraphicsContext* gc)
       
   140 {
       
   141     IntSize offset = view->scrollOffset();
       
   142     rect.move(-offset.width(), -offset.height());
       
   143     rect = view->convertToContainingWindow(rect);
       
   144 
       
   145     gc->concatCTM(AffineTransform().translate(-rect.x(), -rect.y()));
       
   146 
       
   147     view->paint(gc, rect);
       
   148 }
       
   149 
       
   150 HBITMAP imageFromSelection(Frame* frame, bool forceBlackText)
       
   151 {
       
   152     frame->setPaintRestriction(forceBlackText ? PaintRestrictionSelectionOnlyBlackText : PaintRestrictionSelectionOnly);
       
   153     FloatRect fr = frame->selectionRect();
       
   154     IntRect ir((int)fr.x(), (int)fr.y(),(int)fr.width(),(int)fr.height());
       
   155 
       
   156     void* bits;
       
   157     HDC hdc = CreateCompatibleDC(0);
       
   158     int w = ir.width();
       
   159     int h = ir.height();
       
   160     BITMAPINFO bmp = { { sizeof(BITMAPINFOHEADER), w, h, 1, 32 } };
       
   161 
       
   162     HBITMAP hbmp = CreateDIBSection(0, &bmp, DIB_RGB_COLORS, (void**)&bits, 0, 0);
       
   163     HBITMAP hbmpOld = (HBITMAP)SelectObject(hdc, hbmp);
       
   164     CGColorSpaceRef deviceRGB = CGColorSpaceCreateDeviceRGB();
       
   165     CGContextRef context = CGBitmapContextCreate((void*)bits, w, h,
       
   166         8, w * sizeof(RGBQUAD), deviceRGB, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
       
   167     CGColorSpaceRelease(deviceRGB);
       
   168     CGContextSaveGState(context);
       
   169 
       
   170     GraphicsContext gc(context);
       
   171 
       
   172     frame->document()->updateLayout();
       
   173     drawRectIntoContext(ir, frame->view(), &gc);
       
   174 
       
   175     CGContextRelease(context);
       
   176     SelectObject(hdc, hbmpOld);
       
   177     DeleteDC(hdc);
       
   178 
       
   179     frame->setPaintRestriction(PaintRestrictionNone);
       
   180 
       
   181     return hbmp;
       
   182 }
       
   183 
       
   184 DragImageRef Frame::dragImageForSelection()
       
   185 {    
       
   186     if (selectionController()->isRange())
       
   187         return imageFromSelection(this, false);
       
   188 
       
   189     return 0;
       
   190 }
       
   191 
       
   192 void Frame::dashboardRegionsChanged()
       
   193 {
       
   194 }
       
   195 
       
   196 } // namespace WebCore