1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
/* This file is part of the KDE project
Copyright (C) 2001 Simon Hausmann <[email protected]>
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., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef __kstextview_h__
#define __kstextview_h__
#include <tqscrollview.h>
#include <tqpen.h>
#include <tqptrlist.h>
#include <tqvaluelist.h>
#include <tqmap.h>
#include <tqpixmap.h>
class TQTimer;
class TQDragObject;
namespace KSirc
{
class TextView;
struct StringPtr
{
StringPtr() : ptr( 0 ), len( 0 ) {}
StringPtr( const TQChar *_ptr, uint _len )
: ptr( _ptr ), len( _len ) {}
explicit StringPtr( const TQString &s ) // use with care!
: ptr( s.unicode() ), len( s.length() ) {}
inline bool isNull() const { return ptr == 0; }
// makes deep copy
inline TQString toTQString() const
{ return ( ptr && len > 0 ) ? TQString( ptr, len ) : TQString(); }
const TQChar *ptr;
uint len;
};
#define CONSTSTRING( substr ) TQConstString( substr.ptr, substr.len ).string()
inline bool operator<( const StringPtr &s1, const StringPtr &s2 )
{
return CONSTSTRING( s1 ) < CONSTSTRING( s2 );
}
inline bool operator==( const StringPtr &s1, const StringPtr &s2 )
{
return CONSTSTRING( s1 ) == CONSTSTRING( s2 );
}
inline bool operator==( const StringPtr &s1, const char *s2 )
{
return CONSTSTRING( s1 ) == s2;
}
class AttributeMap : public TQMap<StringPtr, StringPtr>
{
public:
AttributeMap() {}
AttributeMap( const AttributeMap &rhs ) : TQMap<StringPtr, StringPtr>( rhs ) {}
AttributeMap &operator=( const AttributeMap &rhs )
{ TQMap<StringPtr, StringPtr>::operator=( rhs ); return *this; }
// helper for 'const char *' key...
ConstIterator findAttribute( const char *key ) const
{
TQString qkey( key );
return find( StringPtr( qkey ) );
}
Iterator findAttribute( const char *key )
{
TQString qkey( key );
return find( StringPtr( qkey ) );
}
StringPtr operator[]( const char *key ) const
{
ConstIterator it = findAttribute( key );
if ( it == end() )
return StringPtr();
return it.data();
}
StringPtr &operator[]( const StringPtr &key )
{
return TQMap<StringPtr, StringPtr>::operator[]( key );
}
};
struct Token
{
Token() : id( -1 ) {}
enum Id { TagOpen, Text, TagClose };
int id;
StringPtr value;
AttributeMap attributes;
};
struct ItemProperties
{
ItemProperties();
ItemProperties( const TQFont &defaultFont );
ItemProperties( const ItemProperties &other,
const Token &token,
TextView *textView );
ItemProperties( const ItemProperties &rhs );
ItemProperties &operator=( const ItemProperties &rhs );
void updateFont( const TQFont &newFont );
// these three are inherited/merged
TQFont font;
TQColor color;
TQColor selColor;
TQColor bgColor;
TQColor bgSelColor;
bool reversed;
// ### todo: inherit these, too
AttributeMap attributes;
};
class TextParag;
class TextLine;
class SelectionPoint;
class Item
{
public:
enum LayoutResetStatus { DeleteItem, KeepItem };
enum SelectionStatus { SelectionStart = 0, InSelection, SelectionEnd, SelectionBoth,
NoSelection };
enum SelectionAccuracy { SelectExact, SelectFuzzy };
Item( TextParag *parag, const ItemProperties &props = ItemProperties() );
virtual ~Item();
virtual const char *type() { return "Item"; }
virtual void paint( TQPainter &painter ) = 0;
int width() const;
int minWidth() const;
int height() const;
virtual Item *breakLine( int width );
virtual LayoutResetStatus resetLayout() = 0;
virtual int calcSelectionOffset( int x );
void setSelectionStatus( SelectionStatus status ) { m_selection = status; }
SelectionStatus selectionStatus() const { return m_selection; }
void selectionOffsets( int &startOffset, int &endOffset );
int maxSelectionOffset() const;
void setLine(TextLine *line);
// ###
virtual StringPtr text() const;
virtual void setProps( const ItemProperties &props );
ItemProperties &props() { return m_props; }
static Item *create( TextParag *parag, const Token &tok,
const ItemProperties &props = ItemProperties() );
protected:
mutable bool m_extendsDirty;
mutable int m_minWidth;
mutable int m_width;
mutable int m_height;
virtual void calcExtends() const = 0;
SelectionStatus m_selection;
TextLine *m_line;
TextParag *m_parag;
ItemProperties m_props;
};
class TextChunk : public Item
{
public:
TextChunk( TextParag *parag, const StringPtr &text, const ItemProperties &props );
virtual const char *type() { return "TextChunk"; }
virtual void paint( TQPainter &painter );
virtual Item *breakLine( int width );
virtual LayoutResetStatus resetLayout();
virtual int calcSelectionOffset( int x );
virtual StringPtr text() const;
virtual void setProps( const ItemProperties &props );
protected:
virtual void calcExtends() const;
private:
StringPtr breakInTheMiddle( int width );
Item *hardBreak( const StringPtr &rightHandSide );
void paintSelection( TQPainter &p );
int paintSelection( TQPainter &p, int x, const StringPtr &text );
int paintText( TQPainter &p, int x, const StringPtr &text );
void mergeSelection( TextChunk *child, SelectionPoint *selection );
StringPtr m_text;
uint m_originalTextLength;
TQFontMetrics m_metrics;
class TextChunk *m_parent;
};
class ImageItem : public Item
{
public:
ImageItem( TextParag *parag, const TQPixmap &pixmap );
virtual const char *type() { return "Image"; }
virtual void paint( TQPainter &painter );
virtual LayoutResetStatus resetLayout();
protected:
virtual void calcExtends() const;
private:
TQPixmap m_pixmap;
};
class Tokenizer
{
public:
struct TagIndex
{
enum Type { Open, Close };
TagIndex() : index( 0 ), type( -1 ) {}
TagIndex( int _index, int _type )
: index( _index ), type( _type ) {}
uint index;
int type;
};
typedef TQValueList<TagIndex> TagIndexList;
// preprocessed string
struct PString
{
TQString data;
TagIndexList tags;
};
Tokenizer( PString &text );
static PString preprocess( const TQString &richText );
static TQString convertToRichText( const PString &ptext );
bool parseNextToken( Token &tok );
private:
void parseTag( const StringPtr &text,
StringPtr &tag,
AttributeMap &attributes );
static TagIndexList scanTagIndices( const TQString &text );
static void resolveEntities( TQString &text, TagIndexList &tags );
enum TagParsingState { ScanForName, ScanForEqual, ScanForValue };
TQString &m_text;
TagIndexList m_tags;
TagIndexList::ConstIterator m_lastTag;
bool m_textBeforeFirstTagProcessed;
bool m_done;
Tokenizer( const Tokenizer & );
Tokenizer &operator=( const Tokenizer & );
};
class SelectionPoint;
class TextLine
{
public:
enum LayoutPolicy { NoUpdate, UpdateMaxHeight };
TextLine();
// tranfers ownership of items! make sure that 'items' does not
// have autodeletion enabled!
TextLine( const TQPtrList<Item> &items );
int maxHeight() const { return m_maxHeight; }
TQString updateSelection( const SelectionPoint &start, const SelectionPoint &end );
void clearSelection();
// transfers ownership
void appendItem( Item *i, int layoutUpdatePolicy = NoUpdate );
bool isEmpty() const { return m_items.isEmpty(); }
Item *resetLayout( TQPtrList<Item> &remainingItems);
void paint( TQPainter &p, int y );
Item *itemAt( int px, SelectionPoint *selectionInfo,
Item::SelectionAccuracy accuracy = Item::SelectExact );
TQPtrListIterator<Item> iterator() const { return TQPtrListIterator<Item>( m_items ); }
TQString plainText() const;
void fontChange( const TQFont &newFont );
private:
TQPtrList<Item> m_items;
int m_maxHeight;
};
class SelectionPoint;
class TextParag
{
public:
TextParag( TextView *textView, const TQString &richText );
~TextParag();
void layout( int width );
void paint( TQPainter &p, int y, int maxY );
inline void setLayouted( bool l ) { m_layouted = l; }
inline bool isLayouted() const { return m_layouted; }
inline int minWidth() const { return m_minWidth; }
inline int height() const { return m_height; }
Item *itemAt( int px, int py, SelectionPoint *selectionInfo,
Item::SelectionAccuracy accuracy = Item::SelectExact );
TextView *textView() const { return m_textView; }
TQString updateSelection( const SelectionPoint &start, const SelectionPoint &end );
void clearSelection();
void setRichText( const TQString &richText );
Tokenizer::PString processedRichText() const { return m_processedRichText; }
TQString plainText() const;
void fontChange( const TQFont &newFont );
private:
Tokenizer::PString m_processedRichText;
TQPtrList<TextLine> m_lines;
bool m_layouted;
int m_height;
int m_minWidth;
TextView *m_textView;
struct Tag
{
Tag() {}
Tag( const StringPtr &_name, const ItemProperties &_props )
: name( _name ), props( _props ) {}
StringPtr name;
ItemProperties props;
};
TextParag( const TextParag & );
TextParag &operator=( const TextParag & );
};
struct SelectionPoint
{
SelectionPoint() : item( 0 ), line( 0 ), parag( 0 ), offset( 0 ) {}
Item *item;
TextLine *line;
TextParag *parag;
uint offset;
TQPoint pos;
};
class TextParagIterator
{
friend class TextView;
public:
TextParagIterator( const TextParagIterator &rhs )
: m_paragIt( rhs.m_paragIt ) {}
TextParagIterator &operator=( const TextParagIterator &rhs )
{ m_paragIt = rhs.m_paragIt; return *this; }
TQString richText() const;
void setRichText( const TQString &richText );
TQString plainText() const;
bool atEnd() const { return m_paragIt.current() == 0; }
TextParagIterator &operator++() { ++m_paragIt; return *this; }
TextParagIterator &operator++( int steps ) { m_paragIt += steps; return *this; }
TextParagIterator &operator--() { --m_paragIt; return *this; }
TextParagIterator &operator--( int steps ) { m_paragIt -= steps; return *this; }
protected:
TextParagIterator( const TQPtrListIterator<TextParag> ¶gIt )
: m_paragIt( paragIt ) {}
private:
TQPtrListIterator<TextParag> m_paragIt;
};
class ContentsPaintAlgorithm
{
public:
ContentsPaintAlgorithm( const TQPtrListIterator<TextParag> ¶gIt,
TQWidget *viewport, TQPixmap &paintBuffer,
TQPainter &painter, int clipX, int clipY, int clipHeight );
void paint();
private:
int goToFirstVisibleParagraph();
int paint( TQPainter &bufferedPainter, int currentY );
int adjustYAndIterator( int startY, int currentY, int nextY );
TQPtrListIterator<TextParag> m_paragIt;
TQWidget *m_viewport;
TQPixmap &m_paintBuffer;
TQPainter &m_painter;
int m_clipX, m_clipY, m_clipHeight;
int m_overshoot;
};
class TextView : public TQScrollView
{
TQ_OBJECT
friend class Item;
friend class TextChunk;
friend class TextParag;
friend class TextParagIterator;
public:
TextView( TQWidget *parent, const char *name = 0 );
virtual ~TextView();
virtual void clear();
TextParagIterator appendParag( const TQString &richText );
bool removeParag( const TextParagIterator ¶g );
void clearSelection( bool repaint = false ); // ### re-consider the repaint arg...
TQString selectedText() const { return m_selectedText; }
TextParagIterator firstParag() const;
TQString plainText() const;
TQColor linkColor() const;
void setLinkColor( const TQColor &linkColor );
void scrollToBottom( bool force = false );
signals:
void selectionChanged();
void pasteReq(const TQString&);
void linkClicked( const TQMouseEvent *ev, const TQString &url );
public slots:
void copy();
protected slots:
void scrolling(int value);
protected:
virtual void viewportResizeEvent( TQResizeEvent *ev );
virtual void drawContents( TQPainter *p, int cx, int cy, int cw, int ch );
virtual void contentsMousePressEvent( TQMouseEvent *ev );
virtual void contentsMouseMoveEvent( TQMouseEvent *ev );
virtual void contentsMouseReleaseEvent( TQMouseEvent *ev );
virtual void fontChange( const TQFont & );
virtual void startDrag();
virtual TQDragObject *dragObject( const TQString &dragURL );
private slots:
void autoScroll();
private:
void emitLinkClickedForMouseEvent( TQMouseEvent *ev );
void startAutoScroll();
void stopAutoScroll();
void selectionOffsets( int &startOffset, int &endOffset );
void updateSelectionOrder();
TQString updateSelection( const SelectionPoint &start, const SelectionPoint &end );
SelectionPoint *selectionStart();
SelectionPoint *selectionEnd();
void layout( bool force = true );
Item *itemAt( const TQPoint &pos, SelectionPoint *selectionInfo = 0,
Item::SelectionAccuracy accuracy = Item::SelectExact );
void clearSelectionInternal();
void contentsChange(int heightChange, bool force = false);
TQPtrList<TextParag> m_parags;
TQPixmap m_paintBuffer;
SelectionPoint m_selectionMaybeStart;
SelectionPoint m_selectionStart;
SelectionPoint m_selectionEnd;
bool m_selectionEndBeforeStart;
TQTimer *m_autoScrollTimer;
TQString m_selectedText;
TQPoint m_dragStartPos;
TQString m_dragURL;
bool m_mousePressed : 1;
bool m_mmbPressed : 1;
TQColor m_linkColor;
TQColor m_selectionBackgroundColor;
int m_height;
bool m_inScroll;
int m_lastScroll;
};
} // namespace KSirc
#endif
|