00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _KWTEXTLINE_H_
00024 #define _KWTEXTLINE_H_
00025
00026 #include <stdlib.h>
00027
00028 #include <qstring.h>
00029 #include <qarray.h>
00030 #include <qvaluelist.h>
00031
00032 #include <ksharedptr.h>
00033
00039 template<class T>
00040 class FVPrivate
00041 {
00042 public:
00043 int curpos;
00044 typedef QValueListConstIterator<T> Iterator;
00045 Iterator curit;
00046
00047 FVPrivate() { curpos=-1; };
00048 };
00049
00050 template<class T>
00051 class FastValueList : public QValueList<T>
00052 {
00053 public:
00054 typedef QValueListIterator<T> Iterator;
00055 typedef QValueListConstIterator<T> ConstIterator;
00056 protected:
00057 FVPrivate<T> *fvp;
00058
00059 Iterator fastat( uint i ) {
00060 uint num=this->count();
00061 if (i>=num) {return this->end();}
00062 if (fvp->curpos<0) { fvp->curpos=0; fvp->curit=this->begin(); }
00063 uint curpos=(uint) fvp->curpos;
00064 Iterator curit(fvp->curit.node);
00065 if (curpos==i) return curit;
00066
00067 int diff=i-curpos;
00068 bool forward;
00069 if (diff<0) diff=-diff;
00070 if (((uint)diff < i) && ((uint)diff < num-i)) {
00071 forward=i > (uint)curpos;
00072 } else if (i < num - i) {
00073 curit=this->begin(); diff=i; forward=TRUE;
00074 } else {
00075 curit=this->fromLast(); diff=num - i - 1;
00076 if (diff<0) diff=0;
00077 forward=FALSE;
00078 }
00079 if (forward) {
00080 while(diff--) curit++;
00081 } else {
00082 while(diff--) curit--;
00083 }
00084 fvp->curpos=i; fvp->curit=curit;
00085 return curit;
00086 }
00087 ConstIterator fastat( uint i ) const {
00088 uint num=this->count();
00089 if (i>=num) {return this->end();}
00090 if (fvp->curpos<0) { fvp->curpos=0; fvp->curit=this->begin(); }
00091 uint curpos=(uint) fvp->curpos;
00092 ConstIterator curit=fvp->curit;
00093 if (curpos==i) return curit;
00094
00095 int diff=i-curpos;
00096 bool forward;
00097 if (diff<0) diff=-diff;
00098 if (((uint)diff < i) && ((uint)diff < num-i)) {
00099 forward=i > (uint)curpos;
00100 } else if (i < num - i) {
00101 curit=this->begin(); diff=i; forward=TRUE;
00102 } else {
00103 curit=this->fromLast(); diff=num - i - 1;
00104 if (diff<0) diff=0;
00105 forward=FALSE;
00106 }
00107 if (forward) {
00108 while(diff--) curit++;
00109 } else {
00110 while(diff--) curit--;
00111 }
00112 fvp->curpos=i; fvp->curit=curit;
00113 return curit;
00114 }
00115
00116 public:
00117 FastValueList() : QValueList<T>()
00118 { fvp=new FVPrivate<T>(); }
00119 FastValueList(const FastValueList<T>& l) : QValueList<T>(l)
00120 { fvp=new FVPrivate<T>(); }
00121 ~FastValueList() { delete fvp; }
00122
00123 Iterator insert( Iterator it, const T& x ) {
00124 fvp->curpos=-1; return QValueList<T>::insert(it, x);
00125 }
00126
00127 Iterator append( const T& x ) {
00128 fvp->curpos=-1; return QValueList<T>::append( x );
00129 }
00130 Iterator prepend( const T& x ) {
00131 fvp->curpos=-1; return QValueList<T>::prepend( x );
00132 }
00133
00134 Iterator remove( Iterator it ) {
00135 fvp->curpos=-1; return QValueList<T>::remove( it );
00136 }
00137 void remove( const T& x ) {
00138 fvp->curpos=-1; QValueList<T>::remove( x );
00139 }
00140
00141 T& operator[] ( uint i ) { this->detach(); return fastat(i); }
00142 const T& operator[] ( uint i ) const { return *fastat(i); }
00143 Iterator at( uint i ) { this->detach(); return fastat(i); }
00144 ConstIterator at( uint i ) const { return ConstIterator( fastat(i) ); }
00145 };
00146
00147
00155 class TextLine : public KShared
00156 {
00157 friend class KWBuffer;
00158 friend class KWBufBlock;
00159
00160 public:
00161 typedef KSharedPtr<TextLine> Ptr;
00162 typedef FastValueList<Ptr> List;
00163
00164 public:
00169 TextLine(uchar attribute = 0, int context = 0);
00170 ~TextLine();
00171
00175 uint length() const {return text.length();}
00180 void replace(uint pos, uint delLen, const QChar *insText, uint insLen, uchar *insAttribs = 0L);
00181
00185 void append(const QChar *s, uint l) {replace(text.length(), 0, s, l);}
00189 void wrap(TextLine::Ptr nextLine, uint pos);
00194 void unWrap(uint pos, TextLine::Ptr nextLine, uint len);
00198 void truncate(uint newLen) { text.truncate(newLen); attributes.resize(text.length()); }
00202 int firstChar() const;
00206 int lastChar() const;
00210 void removeSpaces();
00214 QChar getChar(uint pos) const;
00218 const QChar *getText() const {return text.unicode();};
00222 const QString getString() { return text; };
00223
00224
00225
00226
00227 const QChar *firstNonSpace();
00232 int cursorX(uint pos, uint tabChars) const;
00236 bool startingWith(QString& match);
00240 bool endingWith(QString& match);
00241
00245 void setAttribs(uchar attribute, uint start, uint end);
00249 void setAttr(uchar attribute);
00253 uchar getAttr(uint pos) const;
00257 uchar getAttr() const;
00261 uchar getRawAttr(uint pos) const;
00266 uchar getRawAttr() const;
00267
00271 void setContext(int context);
00275 int getContext() const;
00276
00280 void select(bool sel, uint start, uint end);
00285 void selectEol(bool sel, uint pos);
00289 void toggleSelect(uint start, uint end);
00294 void toggleSelectEol(uint pos);
00298 int numSelected() const;
00302 bool isSelected(uint pos) const;
00306 bool isSelected() const;
00310 int findSelected(uint pos) const;
00314 int findUnselected(uint pos) const;
00318 int findRevSelected(uint pos) const;
00322 int findRevUnselected(uint pos) const;
00323
00324 void clearMark () { myMark = 0; };
00325 void addMark ( uint m );
00326 void delMark ( uint m );
00327 uint mark() { return myMark; };
00328
00329 uchar *getAttribs() { return attributes.data(); }
00330
00331 protected:
00335 QString text;
00339 QArray<uchar> attributes;
00343 uchar attr;
00347 int ctx;
00351 uint myMark;
00352 };
00353
00354
00355 const int taSelected = 0x40;
00356 const int taAttrMask = ~taSelected & 0xFF;
00357
00358 #endif //KWTEXTLINE_H
00359