File indexing completed on 2024-04-28 15:23:58

0001 /*
0002  * Copyright (C) 2004 Apple Computer, Inc.  All rights reserved.
0003  * Copyright (C) 2005 Zack Rusin <zack@kde.org>
0004  * Copyright (C) 2007 Maksim Orlovich <maksim@kde.org>
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
0016  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0018  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
0019  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0021  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0022  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
0023  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0025  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 //#define DEBUG_LAYOUT
0029 
0030 #include "render_canvasimage.h"
0031 #include "render_canvas.h"
0032 
0033 #include <QDebug>
0034 
0035 #include <css/csshelper.h>
0036 #include <misc/helper.h>
0037 #include <html/html_formimpl.h>
0038 #include <html/html_canvasimpl.h>
0039 #include <xml/dom2_eventsimpl.h>
0040 #include <html/html_documentimpl.h>
0041 #include <imload/canvasimage.h>
0042 
0043 #include <math.h>
0044 
0045 using namespace DOM;
0046 using namespace khtml;
0047 
0048 // -------------------------------------------------------------------------
0049 
0050 RenderCanvasImage::RenderCanvasImage(DOM::HTMLCanvasElementImpl *canvasEl)
0051     : RenderReplaced(canvasEl), imagePainter(canvasEl->getCanvasImage())
0052 {
0053     setIntrinsicWidth(element()->width());
0054     setIntrinsicHeight(element()->height());
0055 }
0056 
0057 void RenderCanvasImage::paint(PaintInfo &i, int _tx, int _ty)
0058 {
0059     int x = _tx + m_x;
0060     int y = _ty + m_y;
0061 
0062     if (shouldPaintBackgroundOrBorder() && i.phase != PaintActionOutline) {
0063         paintBoxDecorations(i, x, y);
0064     }
0065 
0066     QPainter *p = i.p;
0067 
0068     if (i.phase == PaintActionOutline && style()->outlineWidth() && style()->visibility() == VISIBLE) {
0069         paintOutline(p, x, y, width(), height(), style());
0070     }
0071 
0072     if (i.phase != PaintActionForeground && i.phase != PaintActionSelection) {
0073         return;
0074     }
0075 
0076     //bool isPrinting = (i.p->device()->devType() == QInternal::Printer);
0077     //bool drawSelectionTint = (selectionState() != SelectionNone) && !isPrinting;
0078     if (i.phase == PaintActionSelection) {
0079         if (selectionState() == SelectionNone) {
0080             return;
0081         }
0082         //drawSelectionTint = false;
0083     }
0084 
0085     int cWidth = contentWidth();
0086     int cHeight = contentHeight();
0087     if (!cWidth) {
0088         cWidth = 300;
0089     }
0090     if (!cHeight) {
0091         cHeight = 150;
0092     }
0093     int leftBorder = borderLeft();
0094     int topBorder = borderTop();
0095     int leftPad = paddingLeft();
0096     int topPad = paddingTop();
0097 
0098     x += leftBorder + leftPad;
0099     y += topBorder + topPad;
0100 
0101     element()->getContext2D()->commit(); // Make sure everything is up-to-date
0102     imagePainter.setSize(QSize(cWidth, cHeight));
0103     imagePainter.paint(x, y, p, 0, 0);
0104 
0105     // if (drawSelectionTint) {
0106 //         QBrush brush(selectionColor(p));
0107 //         QRect selRect(selectionRect());
0108 //         p->fillRect(selRect.x(), selRect.y(), selRect.width(), selRect.height(), brush);
0109 //     }
0110 }
0111 
0112 void RenderCanvasImage::layout()
0113 {
0114     KHTMLAssert(needsLayout());
0115     KHTMLAssert(minMaxKnown());
0116 
0117     calcWidth();
0118     calcHeight();
0119 
0120     setNeedsLayout(false);
0121 }
0122 
0123 void RenderCanvasImage::updateFromElement()
0124 {
0125     int newWidth  = element()->width();
0126     int newHeight = element()->height();
0127     if (intrinsicHeight() != newHeight || intrinsicWidth()  != newWidth) {
0128         setIntrinsicWidth(newWidth);
0129         setIntrinsicHeight(newHeight);
0130         setNeedsLayoutAndMinMaxRecalc();
0131     }
0132 
0133     if (!needsLayout()) {
0134         repaint();
0135     }
0136 }
0137