📄 htmlobj.cpp
字号:
/* This file is part of the KDE libraries Copyright (C) 1997 Martin Jones (mjones@kde.org) (C) 1997 Torben Weis (weis@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.*///-----------------------------------------------------------------------------//// KDE HTML Widget#include <kurl.h>#include <kapp.h>#include "htmlchain.h"#include "htmlobj.h"#include "html.h"#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include <string.h>#include <strings.h>#include <errno.h>#include <sys/stat.h>#include <qfile.h>#include <qimage.h>#include <qdrawutil.h>#include <qmovie.h>#include <qregexp.h>#include <qdict.h>#include <qbitmap.h>#include "htmlobj.h"// This will be constructed once and NEVER deleted.QDict<HTMLCachedImage>* HTMLImage::pCache = 0L;int HTMLObject::objCount = 0;//-----------------------------------------------------------------------------HTMLObject::HTMLObject(){ flags = 0; setFixedWidth( true ); max_width = 0; width = 0; ascent = 0; descent = 0; percent = 0; objCount++; nextObj = 0; x = 0; y = 0;}HTMLObject* HTMLObject::checkPoint( int _x, int _y ){ if ( _x >= x && _x < x + width ) if ( _y > y - ascent && _y < y + descent + 1 ) return this; return 0L;}void HTMLObject::select( KHTMLWidget *_htmlw, HTMLChain *_chain, QRect &_rect, int _tx, int _ty ){ QRect r( x + _tx, y - ascent + _ty, width, ascent + descent ); bool s = isSelected(); bool s2; if ( _rect.contains( r ) ) s2 = TRUE; else s2 = FALSE; if ( s != s2 ) select( _htmlw, _chain, s2, _tx, _ty );}void HTMLObject::getSelected( QStrList &_list ){ if ( &_list == 0L ) { return; } const char *u = getURL(); if ( u != 0 && *u != '\0' && isSelected() ) { char *s; for ( s = _list.first(); s != 0L; s = _list.next() ) if ( strcmp( u, s ) == 0 ) return; _list.append( u ); }}void HTMLObject::selectByURL( KHTMLWidget *_htmlw, HTMLChain *_chain, const char *_url, bool _select, int _tx, int _ty ){ const char *u = getURL(); if ( u == 0 ) return; if ( strcmp( _url, u ) == 0 ) select( _htmlw, _chain, _select, _tx, _ty );}void HTMLObject::select( KHTMLWidget *_htmlw, HTMLChain *_chain, QRegExp& _pattern, bool _select, int _tx, int _ty ){ const char *u = getURL(); if ( u == 0 || *u == '\0' ) return; KURL ku( u ); QString filename = ku.filename(); if ( filename.find( _pattern ) != -1 ) select( _htmlw, _chain, _select, _tx, _ty );}void HTMLObject::select( KHTMLWidget *_htmlw, HTMLChain *_chain, bool _select, int _tx, int _ty ){ const char *u = getURL(); if ( (u == 0) || (*u == '\0') || (_select == isSelected()) ) return; setSelected( _select ); _chain->push( this ); _htmlw->paint(_chain, x + _tx, y - ascent + _ty, width, ascent+descent); _chain->pop();}bool HTMLObject::selectText( KHTMLWidget *_htmlw, HTMLChain *_chain, int _x1, int _y1, int _x2, int _y2, int _tx, int _ty ){ bool selectIt = false; if ( _y1 >= y + descent || _y2 <= y - ascent ) selectIt = false; // start and end are on this line else if ( _y1 >= y - ascent && _y2 <= y + descent ) { if ( _x1 > _x2 ) { int tmp = _x1; _x1 = _x2; _x2 = tmp; } if ( _x1 < x + width/2 && _x2 > x + width/2 && _x2 - _x1 > width/2 ) selectIt = true; } // starts on this line and extends past it. else if ( _y1 >= y - ascent && _y2 > y + descent ) { if ( _x1 < x + width/2 ) selectIt = true; } // starts before this line and ends on it. else if ( _y1 < y - ascent && _y2 <= y + descent ) { if ( _x2 > x + width/2 ) selectIt = true; } // starts before and ends after this line else if ( _y1 < y - ascent && _y2 > y + descent ) { selectIt = true; } if ( selectIt != isSelected() ) { setSelected( selectIt ); _chain->push( this ); _htmlw->paint(_chain, x + _tx, y - ascent + _ty, width, ascent+descent); _chain->pop(); } return selectIt;}int HTMLObject::findPageBreak( int _y ){ if ( _y < y + descent ) return ( y - ascent ); return -1;}bool HTMLObject::getObjectPosition( const HTMLObject *obj, int &xp, int &yp ){ if ( obj == this ) { xp += x; yp += y; return true; } return false;}//const char *HTMLObject::getExec()//{// printf("The actual function in HTMLObject\n");// return(exec);//}//-----------------------------------------------------------------------------HTMLVSpace::HTMLVSpace( int _vspace, Clear c ) : HTMLObject(){ setNewline( true ); ascent = _vspace; descent = 0; width = 1; cl = c;}void HTMLVSpace::getSelectedText( QString &_str ){ if ( isSelected() ) _str += '\n';}//-----------------------------------------------------------------------------HTMLHSpace::HTMLHSpace( const HTMLFont * _font, QPainter *_painter, bool hidden ) : HTMLObject(){ font = _font; ascent = _painter->fontMetrics().ascent(); descent = _painter->fontMetrics().descent()+1; if ( !hidden) width = _painter->fontMetrics().width( ' ' ); else width = 0; setSeparator( true ); setHidden( hidden );}void HTMLHSpace::recalcBaseSize( QPainter *_painter ){ const QFont &oldFont = _painter->font(); _painter->setFont( *font ); ascent = _painter->fontMetrics().ascent(); descent = _painter->fontMetrics().descent() + 1; width = _painter->fontMetrics().width( ' ' ); _painter->setFont( oldFont );}void HTMLHSpace::getSelectedText( QString &_str ){ if ( !isHidden() && isSelected() ) { _str += ' '; }}bool HTMLHSpace::print( QPainter *_painter, int, int _y, int, int _height, int _tx, int _ty, bool toPrinter ){ if ( _y + _height < y - getAscent() || _y > y + getDescent() ) return false; if ( toPrinter ) { if ( _y + _height < y + descent ) return true; if ( isPrinted() ) return false; setPrinted( true ); } print( _painter, _tx, _ty ); return false;}void HTMLHSpace::print( QPainter *_painter, int _tx, int _ty ){ if (isHidden()) return; _painter->setFont( *font ); if ( isSelected() && _painter->device()->devType() != PDT_PRINTER ) { _painter->fillRect( x + _tx, y - ascent + _ty, width, ascent + descent, kapp->selectColor ); _painter->setPen( kapp->selectTextColor ); } else { _painter->setPen( font->textColor() ); } _painter->drawText( x + _tx, y + _ty, " ", 1);}//-----------------------------------------------------------------------------HTMLText::HTMLText(const char* _text, const HTMLFont *_font, QPainter *_painter) : HTMLObject(){ text = _text; font = _font; ascent = _painter->fontMetrics().ascent(); descent = _painter->fontMetrics().descent()+1; width = _painter->fontMetrics().width( (const char*)_text ); selStart = 0; selEnd = 0;}HTMLText::HTMLText( const HTMLFont *_font, QPainter *_painter ) : HTMLObject(){ text = ""; font = _font; ascent = _painter->fontMetrics().ascent(); descent = _painter->fontMetrics().descent() + 1; width = 0; setSeparator( true ); selStart = 0; selEnd = 0;}HTMLText::~HTMLText() { } bool HTMLText::selectText( KHTMLWidget *_htmlw, HTMLChain *_chain, int _x1, int _y1, int _x2, int _y2, int _tx, int _ty ){ bool selectIt = false; int oldSelStart = selStart; int oldSelEnd = selEnd; if ( _y1 >= y + descent || _y2 <= y - ascent ) selectIt = false; // start and end are on this line else if ( _y1 >= y - ascent && _y2 <= y + descent ) { if ( _x1 > _x2 ) { int tmp = _x1; _x1 = _x2; _x2 = tmp; } if ( _x1 < x + width && _x2 > x ) { selectIt = true; selStart = 0; if ( _x1 > x ) selStart = getCharIndex( _x1 - x ); selEnd = strlen( text ); if ( _x2 < x + width ) selEnd = getCharIndex( _x2 - x ); } } // starts on this line and extends past it. else if ( _y1 >= y - ascent && _y2 > y + descent ) { if ( _x1 < x + width ) { selectIt = true; selStart = 0; if ( _x1 > x ) selStart = getCharIndex( _x1 - x ); selEnd = strlen( text ); } } // starts before this line and ends on it. else if ( _y1 < y - ascent && _y2 <= y + descent ) { if ( _x2 > x ) { selectIt = true; selStart = 0; selEnd = strlen( text ); if ( _x2 < x + width ) selEnd = getCharIndex( _x2 - x ); } } // starts before and ends after this line else if ( _y1 < y - ascent && _y2 > y + descent ) { selectIt = true; selStart = 0; selEnd = strlen( text ); } if ( selectIt && (selStart == selEnd) ) selectIt = false; if ( (selectIt != isSelected()) || (oldSelStart != selStart) || (oldSelEnd != selEnd) ) { setSelected( selectIt ); _chain->push( this ); _htmlw->paint(_chain, x + _tx, y - ascent + _ty, width, ascent+descent); _chain->pop(); } return selectIt;}bool HTMLText::selectText( const QRegExp &exp ){ int len; int pos = exp.match( text, 0, &len ); if ( pos >= 0 ) { selStart = pos; selEnd = pos + len; setSelected( true ); } return ( pos != -1 );}// get the index of the character at _xpos.//int HTMLText::getCharIndex( int _xpos ){ int charWidth, index = 0, xp = 0, len = strlen( text ); QFontMetrics fm( *font ); while ( index < len ) { charWidth = fm.width( text[ index ] ); if ( xp + charWidth/2 >= _xpos ) break; xp += charWidth; index++; } return index;}void HTMLText::getSelectedText( QString &_str ){ if ( isSelected() ) { if ( isNewline() ) _str += '\n'; else { int i = selStart; // skip white space at the start of a line. if ( !_str.isEmpty() && _str[ _str.length() - 1 ] == '\n' ) { while ( text[ i ] == ' ' ) i++; } while ( i < selEnd ) { _str += text[ i ]; i++; } } }}void HTMLText::select( KHTMLWidget *_htmlw, HTMLChain *_chain, bool _select, int _tx, int _ty ){ const char *u = getURL(); if ( (u == 0) || (*u == '\0') || (_select == isSelected()) ) return; setSelected( _select ); selStart = 0; selEnd = strlen( text ); _chain->push( this ); _htmlw->paint(_chain, x + _tx, y - ascent + _ty, width, ascent+descent); _chain->pop();}void HTMLText::recalcBaseSize( QPainter *_painter ){ const QFont &oldFont = _painter->font(); _painter->setFont( *font ); ascent = _painter->fontMetrics().ascent(); descent = _painter->fontMetrics().descent() + 1; width = _painter->fontMetrics().width( (const char*)text ); _painter->setFont( oldFont );}bool HTMLText::print( QPainter *_painter, int, int _y, int, int _height, int _tx, int _ty, bool toPrinter ){ if ( _y + _height < y - getAscent() || _y > y + getDescent() ) return false; if ( toPrinter ) { if ( _y + _height < y + descent ) return true; if ( isPrinted() ) return false; setPrinted( true ); } print( _painter, _tx, _ty );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -