📄 render_list.cpp
字号:
/** * This file is part of the DOM implementation for KDE. * * Copyright (C) 1999 Lars Knoll (knoll@kde.org) * (C) 1999 Antti Koivisto (koivisto@kde.org) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * $Id: render_list.cpp,v 1.2 2002/01/28 04:31:02 leon Exp $ */#include "render_interface.h"#include "mgpoint.h"#include "mgcolor.h"#include "mgpen.h"#include "mgbrush.h"#include "mgsize.h"#include "mgrect.h"#include "mgpixmap.h"#include "mgfontmetrics.h"#include "mgpainter.h"#include "mghtmlview.h"#include "mghtml_part.h"#include "render_list.h"#include "render_style.h"#include <qpointarray.h>#include "kdebug.h"using namespace khtml;static QString toRoman( int number, bool upper ){ QString roman; QChar ldigits[] = { 'i', 'v', 'x', 'l', 'c', 'd', 'm' }; QChar udigits[] = { 'I', 'V', 'X', 'L', 'C', 'D', 'M' }; QChar *digits = upper ? udigits : ldigits; int i, d = 0; do { int num = number % 10; if ( num % 5 < 4 ) for ( i = num % 5; i > 0; i-- ) roman.insert( 0, digits[ d ] ); if ( num >= 4 && num <= 8) roman.insert( 0, digits[ d+1 ] ); if ( num == 9 ) roman.insert( 0, digits[ d+2 ] ); if ( num % 5 == 4 ) roman.insert( 0, digits[ d ] ); number /= 10; d += 2; } while ( number ); return roman;}// -------------------------------------------------------------------------RenderListItem::RenderListItem() : RenderFlow(){ // init RenderObject attributes m_inline = false; // our object is not Inline predefVal = -1; m_marker = 0;}void RenderListItem::setStyle(RenderStyle *style){ RenderFlow::setStyle(style); RenderStyle *newStyle = new RenderStyle(style); if(newStyle->direction() == LTR) newStyle->setFloating(FLEFT); else newStyle->setFloating(FRIGHT); if(!m_marker && m_style->listStyleType() != LNONE) { m_marker = new RenderListMarker(); m_marker->setPart(m_part); m_marker->setStyle(newStyle); addChild(m_marker); } else if ( m_marker ) { m_marker->setPart(m_part); m_marker->setStyle(newStyle); }}RenderListItem::~RenderListItem(){}void RenderListItem::calcListValue(){ if( !m_marker ) return; if(predefVal != -1) m_marker->val = predefVal; else if(!m_previous) m_marker->val = 1; else { RenderObject *o = m_previous; while ( o && (!o->isListItem() || o->style()->listStyleType() == LNONE) ) o = o->previousSibling(); if( o && o->isListItem() && o->style()->listStyleType() != LNONE ) { RenderListItem *item = static_cast<RenderListItem *>(o); m_marker->val = item->value() + 1; } else m_marker->val = 1; }}void RenderListItem::layout( ){ if ( !checkChildren() ) { m_height = 0; //kdDebug(0) << "empty item" << endl; return; } calcListValue(); if ( m_marker ) m_marker->layout(); RenderFlow::layout();}// this function checks if there is other rendered contents in the list item than a marker. If not, the whole// list item will not get printed.bool RenderListItem::checkChildren() const{ //kdDebug(0) << " checkCildren" << endl; if(!m_first) return false; RenderObject *o = m_first; while(o->firstChild()) o = o->firstChild(); while (!o->nextSibling() && o->parent() != static_cast<const RenderObject*>(this)) o = o->parent(); o = o->nextSibling(); while( o ) { //kdDebug(0) << "checking " << renderName() << endl; if ( o->isText() || o->isReplaced() ) { //kdDebug(0) << "found" << endl; return true; } RenderObject *next = o->firstChild(); if ( !next ) next = o->nextSibling(); while ( !next && o->parent() != static_cast<const RenderObject*>(this) ) { o = o->parent(); next = o->nextSibling(); } if( !next ) break; o = next; } //kdDebug(0) << "not found" << endl; return false;}void RenderListItem::print(MGPainter *p, int _x, int _y, int _w, int _h, int _tx, int _ty){ if ( !m_height ) return;#ifdef DEBUG_LAYOUT kdDebug( 6040 ) << nodeName().string() << "(LI)::print()" << endl;#endif RenderFlow::print(p, _x, _y, _w, _h, _tx, _ty);}void RenderListItem::printObject(MGPainter *p, int _x, int _y, int _w, int _h, int _tx, int _ty){ // ### this should scale with the font size in the body... possible? //m_marker->printIcon(p, _tx, _ty); RenderFlow::printObject(p, _x, _y, _w, _h, _tx, _ty);}// -----------------------------------------------------------RenderListMarker::RenderListMarker() : RenderBox(){ // init RenderObject attributes m_inline = true; // our object is Inline m_replaced = true; // pretend to be replaced val = -1; listImage = 0;}RenderListMarker::~RenderListMarker(){ if(listImage) listImage->deref(); delete m_style;}void RenderListMarker::setStyle(RenderStyle *s){ RenderBox::setStyle(s); if ( listImage != m_style->listStyleImage() ) { if(listImage) listImage->deref(); listImage = m_style->listStyleImage(); if(listImage) listImage->ref(); }}void RenderListMarker::print(MGPainter *p, int _x, int _y, int _w, int _h, int _tx, int _ty){ printObject(p, _x, _y, _w, _h, _tx, _ty);}void RenderListMarker::printObject(MGPainter *p, int, int, int, int, int _tx, int _ty){ if( !m_visible ) return;#ifdef DEBUG_LAYOUT kdDebug( 6040 ) << nodeName().string() << "(ListMarker)::printObject(" << _tx << ", " << _ty << ")" << endl;#endif p->setFont(m_style->font()); p->setPen(m_style->color()); MGFontMetrics fm = p->fontMetrics(); int offset = fm.ascent()*2/3;#ifdef BOX_DEBUG p->setPen( MGColor("red") ); QCOORD points[] = { _tx,_ty, _tx+offset,_ty, _tx+offset,_ty+offset, _tx,_ty+offset, _tx,_ty }; QPointArray a( 5, points ); p->drawPolyline( a );#endif int xoff = 0; int yoff = fm.ascent() - offset; if(m_style->listStylePosition() != INSIDE) { xoff = -7 - offset; if(m_style->direction() == RTL) xoff = -xoff + m_parent->width(); } if ( listImage ) { p->drawPixmap( MGPoint( _tx + xoff, _ty ), listImage->pixmap()); return; } MGColor color( style()->color() ); p->setPen( MGPen( color ) ); switch(m_style->listStyleType()) { case DISC: p->fillRect ( _tx + xoff, _ty + yoff, offset, offset, color); return; case CIRCLE:#if 0 p->setBrush( MGBrush( color ) ); p->drawArc( _tx + xoff, _ty + yoff, offset, offset, 0, 16*360 );#endif p->setBrush( MGBrush( color ) ); p->drawEllipse( _tx + xoff, _ty + yoff, offset, offset ); return; case SQUARE: { int xp = _tx + xoff; int yp = _ty + fm.ascent() - offset + 1;#if 0 p->setBrush( MGBrush( color ) ); QCOORD points[] = { xp,yp, xp+offset,yp, xp+offset,yp+offset, xp,yp+offset, xp,yp }; QPointArray a( 5, points ); p->drawPolyline( a );#else p->drawRect (xp, yp, offset, offset);#endif return; } case LNONE: return; default: if(item != QString::null) { //_ty += fm.ascent() - fm.height()/2 + 1; if(m_style->listStylePosition() == INSIDE) { if(m_style->direction() == LTR) p->drawText(_tx, _ty, 0, 0, MG::AlignLeft|MG::DontClip, item); else p->drawText(_tx, _ty, 0, 0, MG::AlignRight|MG::DontClip, item); } else { if(m_style->direction() == LTR) p->drawText(_tx-offset/2, _ty, 0, 0, MG::AlignRight|MG::DontClip, item); else p->drawText(_tx+offset/2 + m_parent->width(), _ty, 0, 0, MG::AlignLeft|MG::DontClip, item); } } }}void RenderListMarker::layout(){ calcMinMaxWidth();}void RenderListMarker::setPixmap( const MGPixmap &p, const MGRect& r, MGCachedImage *o, bool *manualUpdate ){ if (manualUpdate && *manualUpdate) { updateSize(); repaintRectangle(0, 0, m_width, m_height); //should not be needed! return; } if(o != listImage) RenderBox::setPixmap(p, r, o); if(m_width != listImage->pixmap_size().width() || m_height != listImage->pixmap_size().height()) { setLayouted(false); setMinMaxKnown(false); layout(); // the updateSize() call should trigger a repaint too if (manualUpdate) { *manualUpdate = true; } else { updateSize(); repaintRectangle(0, 0, m_width, m_height); //should not be needed! } } else { repaintRectangle(0, 0, m_width, m_height); }}void RenderListMarker::calcMinMaxWidth(){ m_width = 0; if(listImage) { if(m_style->listStylePosition() == INSIDE) m_width = listImage->pixmap().width() + 5; m_height = listImage->pixmap().height(); return; } switch(m_style->listStyleType()) { case DISC: case CIRCLE: case SQUARE: { MGFontMetrics fm(m_style->font(), m_part); if(m_style->listStylePosition() == INSIDE) { m_width = fm.ascent(); } else m_width = 0; m_height = fm.ascent(); } goto end; case HEBREW: case ARMENIAN: case GEORGIAN: case CJK_IDEOGRAPHIC: case HIRAGANA: case KATAKANA: case HIRAGANA_IROHA: case KATAKANA_IROHA: case DECIMAL_LEADING_ZERO: // ### unsupported, we use decimal instead case LDECIMAL: item.sprintf( "%2ld", val ); break; case LOWER_ROMAN: item = toRoman( val, false ); break; case UPPER_ROMAN: item = toRoman( val, true ); break; case LOWER_GREEK: // ### unsupported, use low-alpha instead case LOWER_ALPHA: case LOWER_LATIN: item = (QChar) ((int)('a' + val - 1)); break; case UPPER_ALPHA: case UPPER_LATIN: item = (QChar) ((int)('A' + val - 1)); break; case LNONE: break; } item += QString::fromLatin1(". "); { MGFontMetrics fm(m_style->font(), m_part); if(m_style->listStylePosition() != INSIDE) m_width = 0; else m_width = fm.width(item); m_height = fm.ascent(); } end: m_minWidth = m_width; m_maxWidth = m_width;}short RenderListMarker::baselineOffset() const{ return MGFontMetrics(m_style->font(), m_part).ascent();}short RenderListMarker::verticalPositionHint() const{ return MGFontMetrics(m_style->font(), m_part).ascent();}void RenderListMarker::calcWidth(){ RenderBox::calcWidth();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -