00001
00025
00026
00027 #include "render_image.h"
00028 #include "render_root.h"
00029
00030 #include <qdrawutil.h>
00031 #include <qpainter.h>
00032
00033 #include <kapplication.h>
00034 #include <kdebug.h>
00035
00036 #include "css/csshelper.h"
00037 #include "misc/helper.h"
00038 #include "misc/htmlattrs.h"
00039 #include "misc/htmltags.h"
00040 #include "html/html_formimpl.h"
00041 #include "html/html_imageimpl.h"
00042 #include "html/dtd.h"
00043 #include "xml/dom2_eventsimpl.h"
00044 #include "html/html_documentimpl.h"
00045 #include "html/html_objectimpl.h"
00046 #include <math.h>
00047
00048 using namespace DOM;
00049 using namespace khtml;
00050
00051
00052
00053 RenderImage::RenderImage(HTMLElementImpl *_element)
00054 : RenderReplaced(_element)
00055 {
00056 image = 0;
00057 berrorPic = false;
00058 loadEventSent = false;
00059
00060 setIntrinsicWidth( 0 );
00061 setIntrinsicHeight( 0 );
00062 }
00063
00064 RenderImage::~RenderImage()
00065 {
00066 if(image) image->deref(this);
00067 }
00068
00069 void RenderImage::setStyle(RenderStyle* _style)
00070 {
00071 RenderReplaced::setStyle(_style);
00072
00073 setInline( style()->display()==INLINE );
00074
00075 setSpecialObjects(true);
00076
00077 CachedObject* co = style()->contentObject();
00078 if (co && image != co ) {
00079 if (image) image->deref(this);
00080 image = static_cast<CachedImage*>(style()->contentObject());
00081 if (image) image->ref(this);
00082 }
00083 }
00084
00085 void RenderImage::setPixmap( const QPixmap &p, const QRect& r, CachedImage *o)
00086 {
00087 if(o != image) {
00088 RenderReplaced::setPixmap(p, r, o);
00089 return;
00090 }
00091
00092 bool iwchanged = false;
00093
00094 if(o->isErrorImage()) {
00095 int iw = p.width() + 8;
00096 int ih = p.height() + 8;
00097
00098
00099 if ( element() && !alt.isEmpty() && !element()->getAttribute( ATTR_ALT ).isNull()) {
00100 const QFontMetrics &fm = style()->fontMetrics();
00101 QRect br = fm.boundingRect ( 0, 0, 1024, 256, Qt::AlignAuto|Qt::WordBreak, alt.string() );
00102 if ( br.width() > iw )
00103 iw = br.width();
00104 if ( br.height() > ih )
00105 ih = br.height();
00106 }
00107
00108 if ( iw != intrinsicWidth() ) {
00109 setIntrinsicWidth( iw );
00110 iwchanged = true;
00111 }
00112 if ( ih != intrinsicHeight() ) {
00113 setIntrinsicHeight( ih );
00114 iwchanged = true;
00115 }
00116 if ( element()->id() == ID_OBJECT ) {
00117 static_cast<HTMLObjectElementImpl*>( element() )->renderAlternative();
00118 return;
00119 }
00120 }
00121 berrorPic = o->isErrorImage();
00122
00123 bool needlayout = false;
00124
00125
00126 if( o->pixmap_size().width() != intrinsicWidth() ||
00127 o->pixmap_size().height() != intrinsicHeight() || iwchanged )
00128 {
00129
00130
00131
00132
00133 if(!o->isErrorImage()) {
00134 setIntrinsicWidth( o->pixmap_size().width() );
00135 setIntrinsicHeight( o->pixmap_size().height() );
00136 }
00137
00138
00139 int oldwidth = m_width;
00140 int oldheight = m_height;
00141 calcWidth();
00142 calcHeight();
00143
00144 if(iwchanged || m_width != oldwidth || m_height != oldheight)
00145 needlayout = true;
00146
00147 m_width = oldwidth;
00148 m_height = oldheight;
00149 }
00150
00151 pix = p;
00152
00153 if(needlayout)
00154 {
00155 setLayouted(false);
00156 setMinMaxKnown(false);
00157
00158
00159
00160 }
00161 else
00162 {
00163 bool completeRepaint = !resizeCache.isNull();
00164 int cHeight = contentHeight();
00165 int scaledHeight = intrinsicHeight() ? ((o->valid_rect().height()*cHeight)/intrinsicHeight()) : 0;
00166
00167
00168 if(completeRepaint && cHeight >= 5 && o->valid_rect().height() < intrinsicHeight() &&
00169 (scaledHeight / (cHeight/5) == resizeCache.height() / (cHeight/5)))
00170 return;
00171
00172 resizeCache = QPixmap();
00173 if(completeRepaint)
00174 repaintRectangle(borderLeft()+paddingLeft(), borderTop()+paddingTop(), contentWidth(), contentHeight());
00175 else
00176 {
00177 repaintRectangle(r.x() + borderLeft() + paddingLeft(), r.y() + borderTop() + paddingTop(),
00178 r.width(), r.height());
00179 }
00180 }
00181 }
00182
00183 void RenderImage::paintObject(QPainter *p, int , int , int , int , int _tx, int _ty)
00184 {
00185
00186 if(isRelPositioned())
00187 relativePositionOffset(_tx, _ty);
00188
00189 int cWidth = contentWidth();
00190 int cHeight = contentHeight();
00191 int leftBorder = borderLeft();
00192 int topBorder = borderTop();
00193 int leftPad = paddingLeft();
00194 int topPad = paddingTop();
00195
00196 if (khtml::printpainter && !root()->paintImages())
00197 return;
00198
00199
00200 if ( pix.isNull() || berrorPic)
00201 {
00202 if(cWidth > 2 && cHeight > 2)
00203 {
00204 if ( !berrorPic ) {
00205
00206 qDrawShadePanel( p, _tx + leftBorder + leftPad, _ty + topBorder + topPad, cWidth, cHeight,
00207 KApplication::palette().inactive(), true, 1 );
00208 }
00209 if(berrorPic && !pix.isNull() && (cWidth >= pix.width()+4) && (cHeight >= pix.height()+4) )
00210 {
00211 QRect r(pix.rect());
00212 r = r.intersect(QRect(0, 0, cWidth-4, cHeight-4));
00213 p->drawPixmap( QPoint( _tx + leftBorder + leftPad+2, _ty + topBorder + topPad+2), pix, r );
00214 }
00215 if(!alt.isEmpty()) {
00216 QString text = alt.string();
00217 p->setFont(style()->font());
00218 p->setPen( style()->color() );
00219 int ax = _tx + leftBorder + leftPad + 2;
00220 int ay = _ty + topBorder + topPad + 2;
00221 const QFontMetrics &fm = style()->fontMetrics();
00222 if (cWidth>5 && cHeight>=fm.height())
00223 p->drawText(ax, ay+1, cWidth - 4, cHeight - 4, Qt::WordBreak, text );
00224 }
00225 }
00226 }
00227 else if (image && !image->isTransparent())
00228 {
00229 if ( (cWidth != intrinsicWidth() || cHeight != intrinsicHeight()) &&
00230 pix.width() > 0 && pix.height() > 0 && image->valid_rect().isValid())
00231 {
00232 if (resizeCache.isNull() && cWidth > 0 && cHeight > 0 && intrinsicWidth() && intrinsicHeight()
00233 && cWidth < 4096 && cHeight < 4096)
00234 {
00235 QRect scaledrect(image->valid_rect());
00236
00237
00238
00239
00240 QWMatrix matrix;
00241 matrix.scale( (float)(cWidth)/intrinsicWidth(),
00242 (float)(cHeight)/intrinsicHeight() );
00243 resizeCache = pix.xForm( matrix );
00244 scaledrect.setWidth( ( cWidth*scaledrect.width() ) / intrinsicWidth() );
00245 scaledrect.setHeight( ( cHeight*scaledrect.height() ) / intrinsicHeight() );
00246
00247
00248
00249
00250
00251
00252
00253
00254 QSize s(scaledrect.size());
00255 if(image->valid_rect().size() == QSize( intrinsicWidth(), intrinsicHeight() ))
00256 s = QSize(cWidth, cHeight);
00257 if(QABS(s.width() - cWidth) < 2)
00258 s.setWidth(cWidth);
00259 if(resizeCache.size() != s)
00260 resizeCache.resize(s);
00261
00262 p->drawPixmap( QPoint( _tx + leftBorder + leftPad, _ty + topBorder + topPad),
00263 resizeCache, scaledrect );
00264 }
00265 else
00266 p->drawPixmap( QPoint( _tx + leftBorder + leftPad, _ty + topBorder + topPad), resizeCache );
00267 }
00268 else
00269 {
00270
00271
00272
00273
00274 QRect rect(image->valid_rect().isValid() ? image->valid_rect()
00275 : QRect(0, 0, intrinsicWidth(), intrinsicHeight()));
00276
00277 QPoint offs( _tx + leftBorder + leftPad, _ty + topBorder + topPad);
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 p->drawPixmap(offs, pix, rect);
00290
00291 }
00292 }
00293 if(style()->outlineWidth())
00294 paintOutline(p, _tx, _ty, width(), height(), style());
00295 }
00296
00297 void RenderImage::layout()
00298 {
00299 KHTMLAssert(!layouted());
00300 KHTMLAssert( minMaxKnown() );
00301
00302 short oldwidth = m_width;
00303 int oldheight = m_height;
00304
00305
00306 m_height = image && image->isErrorImage() ? intrinsicHeight() : 0;
00307
00308 calcWidth();
00309 calcHeight();
00310
00311
00312
00313 if ( m_width > 2048 && !style()->width().isFixed() )
00314 m_width = intrinsicWidth();
00315 if ( m_height > 2048 && !style()->height().isFixed() )
00316 m_height = intrinsicHeight();
00317
00318 if ( m_width * m_height > 2048*2048 ) {
00319 float scale = sqrt( m_width*m_height / ( 2048.*2048. ) );
00320 m_width = (int) (m_width/scale);
00321 m_height = (int) (m_height/scale);
00322 }
00323
00324 if ( m_width != oldwidth || m_height != oldheight )
00325 resizeCache = QPixmap();
00326
00327 setLayouted();
00328 }
00329
00330 void RenderImage::notifyFinished(CachedObject *finishedObj)
00331 {
00332 if (image == finishedObj && !loadEventSent && element()) {
00333 loadEventSent = true;
00334 element()->dispatchHTMLEvent(EventImpl::LOAD_EVENT,false,false);
00335 }
00336 }
00337
00338 bool RenderImage::nodeAtPoint(NodeInfo& info, int _x, int _y, int _tx, int _ty)
00339 {
00340 bool inside = RenderReplaced::nodeAtPoint(info, _x, _y, _tx, _ty);
00341
00342 if (inside && element()) {
00343 int tx = _tx + m_x;
00344 int ty = _ty + m_y;
00345 if (isRelPositioned())
00346 relativePositionOffset(tx, ty);
00347
00348 HTMLImageElementImpl* i = element()->id() == ID_IMG ? static_cast<HTMLImageElementImpl*>(element()) : 0;
00349 HTMLMapElementImpl* map;
00350 if (i && i->getDocument()->isHTMLDocument() &&
00351 (map = static_cast<HTMLDocumentImpl*>(i->getDocument())->getMap(i->imageMap()))) {
00352
00353 inside = map->mapMouseEvent(_x - tx, _y - ty, contentWidth(), contentHeight(), info);
00354 }
00355 }
00356
00357 return inside;
00358 }
00359
00360 void RenderImage::updateFromElement()
00361 {
00362 DOMString u = element()->id() == ID_OBJECT ?
00363 element()->getAttribute(ATTR_DATA) : element()->getAttribute(ATTR_SRC);
00364
00365 if (!u.isEmpty()) {
00366 CachedImage *new_image = element()->getDocument()->docLoader()->
00367 requestImage(khtml::parseURL(u));
00368
00369 if(new_image && new_image != image && (!style() || !style()->contentObject())) {
00370 loadEventSent = false;
00371 CachedImage* oldimage = image;
00372 image = new_image;
00373 image->ref(this);
00374 if(oldimage) oldimage->deref(this);
00375 berrorPic = image->isErrorImage();
00376 }
00377 }
00378
00379 if (element()->id() == ID_INPUT)
00380 alt = static_cast<HTMLInputElementImpl*>(element())->altText();
00381 else if (element()->id() == ID_IMG)
00382 alt = static_cast<HTMLImageElementImpl*>(element())->altText();
00383 }
00384
00385 bool RenderImage::complete() const
00386 {
00387
00388
00389 return !pix.isNull() && layouted();
00390 }
00391
00392 short RenderImage::calcReplacedWidth() const
00393 {
00394 const Length w = style()->width();
00395
00396 if (w.isVariable()) {
00397 const Length h = style()->height();
00398 if ( m_intrinsicHeight > 0 && ( h.isPercent() || h.isFixed() ) )
00399 return ( ( h.isPercent() ? calcReplacedHeight() : h.value )*intrinsicWidth() ) / m_intrinsicHeight;
00400 }
00401
00402 return RenderReplaced::calcReplacedWidth();
00403 }
00404
00405 int RenderImage::calcReplacedHeight() const
00406 {
00407 const Length h = style()->height();
00408
00409 if (h.isVariable()) {
00410 const Length w = style()->width();
00411 if( m_intrinsicWidth > 0 && ( w.isFixed() || w.isPercent() ))
00412 return (( w.isPercent() ? calcReplacedWidth() : w.value ) * intrinsicHeight()) / m_intrinsicWidth;
00413
00414 }
00415
00416 return RenderReplaced::calcReplacedHeight();
00417 }