lib

KoTextDocument.h
1/* This file is part of the KDE project
2 Copyright (C) 2001 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#ifndef KOTEXTDOCUMENT_H
21#define KOTEXTDOCUMENT_H
22
23#include "KoRichText.h"
24#include <koffice_export.h>
25#if defined(TQ_TEMPLATEDLL)
26// MOC_SKIP_BEGIN
27template class TQ_EXPORT TQMap<int, TQColor>;
28template class TQ_EXPORT TQMap<int, bool>;
29template class TQ_EXPORT TQMap<int, KoTextDocumentSelection>;
30template class TQ_EXPORT TQPtrList<KoTextDocument>;
31// MOC_SKIP_END
32#endif
33
35class KoXmlWriter;
36class KoGenStyles;
38class KoTextFormatCollection;
39class KoParagVisitor;
40class KoTextFormatter;
41class KoTextParag;
42class CustomItemsMap;
43
44class KOTEXT_EXPORT KoTextDocument : public TQObject
45{
46 TQ_OBJECT
47
48
49 friend class KoTextCursor;
50 friend class KoTextParag;
51
52public:
54 enum SelectionId {
55 Standard = 0,
56 InputMethodPreedit = 1,
57 HighlightSelection = 2, // used to highlight during search/replace
58 Temp = 32000 // This selection must not be drawn, it's used e.g. by undo/redo to
59 // remove multiple lines with removeSelectedText()
60 };
61
62 //KoTextDocument( KoTextDocument *p );
63 //KoTextDocument( KoTextDocument *d, KoTextFormatCollection *f );
64 // see below for constructor
65 virtual ~KoTextDocument();
66
67 //KoTextDocument *parent() const { return par; }
68
69 void setText( const TQString &text, const TQString &context );
70
71 //TQString text() const;
72 //TQString text( int parag ) const;
73 //TQString originalText() const;
74
75 int x() const;
76 int y() const;
77 int width() const;
78 //int widthUsed() const;
79 //int visibleWidth() const;
80 int height() const;
81 void setWidth( int w );
82 //int minimumWidth() const;
83 //virtual bool setMinimumWidth( int w, KoTextParag *parag );
84
85 void setY( int y );
86 int leftMargin() const;
87 void setLeftMargin( int lm );
88 int rightMargin() const;
89 void setRightMargin( int rm );
90
91 KoTextParag *firstParag() const;
92 KoTextParag *lastParag() const;
93 void setFirstParag( KoTextParag *p );
94 void setLastParag( KoTextParag *p );
95
96 void invalidate();
97
98 //void setPreProcessor( KoTextPreProcessor *sh );
99 //KoTextPreProcessor *preProcessor() const;
100
101 void setFormatter( KoTextFormatterBase *f );
102 KoTextFormatterBase *formatter() const;
103
104 TQColor selectionColor( int id ) const;
105 bool invertSelectionText( int id ) const;
106 void setSelectionColor( int id, const TQColor &c );
107 void setInvertSelectionText( int id, bool b );
108 bool hasSelection( int id, bool visible = false ) const;
109 bool isSelectionSwapped( int id );
110 void setSelectionStart( int id, KoTextCursor *cursor );
111 bool setSelectionEnd( int id, KoTextCursor *cursor );
112 void selectAll( int id );
113 bool removeSelection( int id );
114 void selectionStart( int id, int &paragId, int &index );
115 KoTextCursor selectionStartCursor( int id );
116 KoTextCursor selectionEndCursor( int id );
117 void selectionEnd( int id, int &paragId, int &index );
118 void setFormat( int id, const KoTextFormat *f, int flags );
119 KoTextParag *selectionStart( int id );
120 KoTextParag *selectionEnd( int id );
121 int numSelections() const { return nSelections; }
122 void addSelection( int id );
123
124 TQString selectedText( int id, bool withCustom = true ) const;
125 //void copySelectedText( int id );
126 void removeSelectedText( int id, KoTextCursor *cursor );
127
128 KoTextParag *paragAt( int i ) const;
129
130 void addCommand( KoTextDocCommand *cmd );
131 KoTextCursor *undo( KoTextCursor *c = 0 );
132 KoTextCursor *redo( KoTextCursor *c = 0 );
133 KoTextDocCommandHistory *commands() const { return commandHistory; }
134
135 KoTextFormatCollection *formatCollection() const;
136
137 bool find( const TQString &expr, bool cs, bool wo, bool forward, int *parag, int *index, KoTextCursor *cursor );
138
139 //void setTextFormat( TQt::TextFormat f );
140 //TQt::TextFormat textFormat() const;
141
142 bool inSelection( int selId, const TQPoint &pos ) const;
143
144 void setUnderlineLinks( bool b ) { underlLinks = b; }
145 bool underlineLinks() const { return underlLinks; }
146
147 void setPaper( TQBrush *brush ) { if ( backBrush ) delete backBrush; backBrush = brush; }
148 TQBrush *paper() const { return backBrush; }
149
150 //void doLayout( TQPainter *p, int w );
151#if 0 // see KoTextDocument
152 void draw( TQPainter *p, const TQRect& rect, const TQColorGroup &cg, const TQBrush *paper = 0 );
153 void drawParag( TQPainter *p, KoTextParag *parag, int cx, int cy, int cw, int ch,
154 TQPixmap *&doubleBuffer, const TQColorGroup &cg,
155 bool drawCursor, KoTextCursor *cursor, bool resetChanged = true );
156 KoTextParag *draw( TQPainter *p, int cx, int cy, int cw, int ch, const TQColorGroup &cg,
157 bool onlyChanged = false, bool drawCursor = false, KoTextCursor *cursor = 0,
158 bool resetChanged = true );
159#endif
160
161 //void setDefaultFont( const TQFont &f );
162
163 void registerCustomItem( KoTextCustomItem *i, KoTextParag *p );
164 void unregisterCustomItem( KoTextCustomItem *i, KoTextParag *p );
165 const TQPtrList<KoTextCustomItem> & allCustomItems() const { return customItems; }
166
167 void setFlow( KoTextFlow *f );
168 void takeFlow();
169 KoTextFlow *flow() const { return flow_; }
170 bool isPageBreakEnabled() const { return m_pageBreakEnabled; }
171 void setPageBreakEnabled( bool b ) { m_pageBreakEnabled = b; }
172
173 void setWithoutDoubleBuffer( bool b ) { withoutDoubleBuffer = b; }
174 bool isWithoutDoubleBuffer() const { return withoutDoubleBuffer; } // added for KWTextDocument
175
176 void setUseFormatCollection( bool b ) { useFC = b; }
177 bool useFormatCollection() const { return useFC; }
178
179#ifdef TQTEXTTABLE_AVAILABLE
180 KoTextTableCell *tableCell() const { return tc; }
181 void setTableCell( KoTextTableCell *c ) { tc = c; }
182#endif
183
184 void setPlainText( const TQString &text );
185 //void setRichText( const TQString &text, const TQString &context );
186 //TQString richText( KoTextParag *p = 0 ) const;
187 TQString plainText() const;
188
189 //bool focusNextPrevChild( bool next );
190
191 int alignment() const;
192 void setAlignment( int a );
193
194 int *tabArray() const;
195 int tabStopWidth() const;
196 void setTabArray( int *a );
197 void setTabStops( int tw );
198
199 void setUndoDepth( int d ) { commandHistory->setUndoDepth( d ); }
200 int undoDepth() const { return commandHistory->undoDepth(); }
201
202 int length() const;
203 void clear( bool createEmptyParag = false );
204
205 KoTextParag* loadList( const TQDomElement& list, KoOasisContext& context, KoTextParag* lastParagraph, KoStyleCollection * styleColl, KoTextParag* nextParagraph );
206
207 // For normal loading nextParagraph and pos are 0.
208 KoTextParag* loadOasisText( const TQDomElement &bodyElem, KoOasisContext& context, KoTextParag* lastParagraph, KoStyleCollection * styleColl, KoTextParag* nextParagraph );
209
210 TQString copySelection( KoXmlWriter& writer, KoSavingContext& context, int selectionId );
211
212 void saveOasisContent( KoXmlWriter& writer, KoSavingContext& context ) const;
213
214 virtual KoTextParag *createParag( KoTextDocument *d, KoTextParag *pr = 0, KoTextParag *nx = 0, bool updateIds = true );
215
216 // Whether margins are added or max'ed.
217 int addMargins() const { return true; }
218
219 void informParagraphDeleted( KoTextParag* parag );
220
221signals:
222 //void minimumWidthChanged( int );
223
225 void paragraphDeleted( KoTextParag* parag );
226
227private:
228 void init();
229 TQPixmap *bufferPixmap( const TQSize &s );
230
232
233public:
247 KoTextDocument( KoTextZoomHandler *zoomHandler,
248 KoTextFormatCollection *fc, KoTextFormatter *formatter = 0L,
249 bool createInitialParag = true );
250
253 KoTextZoomHandler * formattingZoomHandler() const { return m_zoomHandler; }
254
262 KoTextZoomHandler * paintingZoomHandler() const { return m_zoomHandler; }
263
264
267 bool visitSelection( int selectionId, KoParagVisitor *visitor, bool forward = true );
268
271 bool visitDocument( KoParagVisitor *visitor, bool forward = true );
272
275 bool visitFromTo( KoTextParag *firstParag, int firstIndex, KoTextParag* lastParag, int lastIndex, KoParagVisitor* visitor, bool forw = true );
276
280 bool isDestroying() const { return m_bDestroying; }
281
285 enum DrawingFlags {
286 DrawMisspelledLine = 1,
287 DrawFormattingChars = 2,
288 DrawSelections = 4,
289 DontDrawNoteVariable = 8,
290 TransparentBackground = 16
291 };
294 KoTextParag *drawWYSIWYG( TQPainter *p, int cx, int cy, int cw, int ch, const TQColorGroup &cg,
295 KoTextZoomHandler* zoomHandler, bool onlyChanged = false,
296 bool drawCursor = false, KoTextCursor *cursor = 0,
297 bool resetChanged = true, uint drawingFlags = KoTextDocument::DrawSelections );
298
301 void drawParagWYSIWYG( TQPainter *p, KoTextParag *parag, int cx, int cy, int cw, int ch,
302 TQPixmap *&doubleBuffer, const TQColorGroup &cg,
303 KoTextZoomHandler* zoomHandler,
304 bool drawCursor, KoTextCursor *cursor,
305 bool resetChanged = true,
306 uint drawingFlags = KoTextDocument::DrawSelections );
307
309 bool drawFormattingChars() const { return (m_drawingFlags & DrawFormattingChars); }
311 bool drawingMissingSpellLine() const { return (m_drawingFlags & DrawMisspelledLine); }
312
314 bool dontDrawingNoteVariable() const { return (m_drawingFlags & DontDrawNoteVariable); }
315
316 virtual KoTextDocCommand *deleteTextCommand( KoTextDocument *textdoc, int id, int index, const TQMemArray<KoTextStringChar> & str, const CustomItemsMap & customItemsMap, const TQValueList<KoParagLayout> & oldParagLayouts );
317
318 void emitNewCommand(KCommand *cmd) {
319 emit newCommand( cmd );
320 }
321 void emitRepaintChanged() {
322 emit repaintChanged();
323 }
324signals:
330 void newCommand( KCommand *cmd );
335 void repaintChanged();
336
337protected:
338 void drawWithoutDoubleBuffer( TQPainter *p, const TQRect &rect, const TQColorGroup &cg,
339 KoTextZoomHandler* zoomHandler, const TQBrush *paper = 0 );
340
346 virtual bool loadOasisBodyTag( const TQDomElement& /*tag*/, KoOasisContext& /*context*/,
347 KoTextParag* & /*lastParagraph*/, KoStyleCollection* /*styleColl*/,
348 KoTextParag* /*nextParagraph*/ ) {
349 return false;
350 }
351
359 virtual bool loadSpanTag( const TQDomElement& /*tag*/, KoOasisContext& /*context*/,
360 KoTextParag* /*parag*/, uint /*pos*/,
361 TQString& /*textData*/, KoTextCustomItem* & /*customItem*/ ) {
362 return false;
363 }
364
365private:
366 // The zoom handler used when formatting
367 // (due to the pixelx/pixelww stuff in KoTextFormatter)
368 KoTextZoomHandler * m_zoomHandler;
369 bool m_bDestroying;
370 uint m_drawingFlags;
371
373
374private:
375 /*struct TQ_EXPORT Focus {
376 KoTextParag *parag;
377 int start, len;
378 TQString href;
379 };*/
380
381 int cx, cy; //, cw, vw;
382 KoTextParag *fParag, *lParag;
383 //KoTextPreProcessor *pProcessor;
384 TQMap<int, TQColor> selectionColors;
385 TQMap<int, KoTextDocumentSelection> selections;
386 TQMap<int, bool> selectionText;
387 KoTextDocCommandHistory *commandHistory;
388 KoTextFormatterBase *pFormatter;
389 KoTextFormatCollection *fCollection;
390 //TQt::TextFormat txtFormat;
391 //bool preferRichText : 1;
392 bool m_pageBreakEnabled : 1;
393 bool useFC : 1;
394 bool withoutDoubleBuffer : 1;
395 bool underlLinks : 1;
396 //bool nextDoubleBuffered : 1;
397 bool addMargs : 1;
398 int nSelections;
399 KoTextFlow *flow_;
400 TQPtrList<KoTextCustomItem> customItems;
401 TQBrush *backBrush;
402 TQPixmap *buf_pixmap;
403 //Focus focusIndicator;
404 //int minw;
405 int leftmargin;
406 int rightmargin;
407 //KoTextParag *minwParag;
408 int align;
409 int *tArray;
410 int tStopWidth;
411};
412
413inline int KoTextDocument::x() const
414{
415 return cx;
416}
417
418inline int KoTextDocument::y() const
419{
420 return cy;
421}
422
423inline int KoTextDocument::width() const
424{
425 return flow_->width();
426 //return TQMAX( cw, flow_->width() );
427}
428
429//inline int KoTextDocument::visibleWidth() const
430//{
431// return vw;
432//}
433
434inline KoTextParag *KoTextDocument::firstParag() const
435{
436 return fParag;
437}
438
439inline KoTextParag *KoTextDocument::lastParag() const
440{
441 return lParag;
442}
443
444inline void KoTextDocument::setFirstParag( KoTextParag *p )
445{
446 fParag = p;
447}
448
449inline void KoTextDocument::setLastParag( KoTextParag *p )
450{
451 lParag = p;
452}
453
454inline void KoTextDocument::setWidth( int w )
455{
456 //cw = TQMAX( w, minw );
457 flow_->setWidth( w );
458 //vw = w;
459}
460
461//inline int KoTextDocument::minimumWidth() const
462//{
463// return minw;
464//}
465
466inline void KoTextDocument::setY( int y )
467{
468 cy = y;
469}
470
471inline int KoTextDocument::leftMargin() const
472{
473 return leftmargin;
474}
475
476inline void KoTextDocument::setLeftMargin( int lm )
477{
478 leftmargin = lm;
479}
480
481inline int KoTextDocument::rightMargin() const
482{
483 return rightmargin;
484}
485
486inline void KoTextDocument::setRightMargin( int rm )
487{
488 rightmargin = rm;
489}
490
491/*inline KoTextPreProcessor *KoTextDocument::preProcessor() const
492{
493 return pProcessor;
494}
495
496inline void KoTextDocument::setPreProcessor( KoTextPreProcessor * sh )
497{
498 pProcessor = sh;
499}*/
500
501inline void KoTextDocument::setFormatter( KoTextFormatterBase *f )
502{
503 delete pFormatter;
504 pFormatter = f;
505}
506
507inline KoTextFormatterBase *KoTextDocument::formatter() const
508{
509 return pFormatter;
510}
511
512inline TQColor KoTextDocument::selectionColor( int id ) const
513{
514 return selectionColors[ id ];
515}
516
517inline bool KoTextDocument::invertSelectionText( int id ) const
518{
519 return selectionText[ id ];
520}
521
522inline void KoTextDocument::setSelectionColor( int id, const TQColor &c )
523{
524 selectionColors[ id ] = c;
525}
526
527inline void KoTextDocument::setInvertSelectionText( int id, bool b )
528{
529 selectionText[ id ] = b;
530}
531
532inline KoTextFormatCollection *KoTextDocument::formatCollection() const
533{
534 return fCollection;
535}
536
537inline int KoTextDocument::alignment() const
538{
539 return align;
540}
541
542inline void KoTextDocument::setAlignment( int a )
543{
544 align = a;
545}
546
547inline int *KoTextDocument::tabArray() const
548{
549 return tArray;
550}
551
552inline int KoTextDocument::tabStopWidth() const
553{
554 return tStopWidth;
555}
556
557inline void KoTextDocument::setTabArray( int *a )
558{
559 tArray = a;
560}
561
562inline void KoTextDocument::setTabStops( int tw )
563{
564 tStopWidth = tw;
565}
566
567/*inline TQString KoTextDocument::originalText() const
568{
569 if ( oTextValid )
570 return oText;
571 return text();
572}*/
573
574inline void KoTextDocument::setFlow( KoTextFlow *f )
575{
576 if ( flow_ )
577 delete flow_;
578 flow_ = f;
579}
580
581inline void KoTextDocument::takeFlow()
582{
583 flow_ = 0L;
584}
585
592{
593protected:
596 virtual ~KoParagVisitor() {}
597public:
599 virtual bool visit( KoTextParag *parag, int start, int end ) = 0;
600};
601
602class KCommand;
603class TQDomElement;
604class KMacroCommand;
605
608class CustomItemsMap : public TQMap<int, KoTextCustomItem *>
609{
610public:
611
613 void insertItems( const KoTextCursor & startCursor, int size );
614
616 void deleteAll( KMacroCommand *macroCmd );
617};
618
619#endif
A CustomItemsMap associates a custom item to an index Used in the undo/redo info for insert/delete te...
void deleteAll(KMacroCommand *macroCmd)
Delete all the items from the map, adding their commands into macroCmd.
void insertItems(const KoTextCursor &startCursor, int size)
Insert all the items from the map, into the existing text.
Repository of styles used during saving of OASIS/OOo file.
Definition KoGenStyles.h:60
Used during loading of Oasis format (and discarded at the end of the loading).
Base class for "visitors".
KoParagVisitor()
protected since this is an abstract base class
virtual bool visit(KoTextParag *parag, int start, int end)=0
Visit the paragraph parag, from index start to index end.
Used during saving to Oasis format (and discarded at the end of the saving).
TODO rename to KoParagStyleCollection - or should char styles be part of it too?
KoTextCustomItem is the base class for custom items (i.e.
Each character (KoTextStringChar) points to a KoTextFormat that defines the formatting of that charac...
We implement our own text formatter to implement WYSIWYG: It is heavily based on KoTextFormatterBaseB...
This class extends KoZoomHandler to add support for WYSIWYG text layouting.
A class for writing out XML (to any TQIODevice), with a special attention on performance.
Definition KoXmlWriter.h:37
TDEAction * selectAll(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TDEAction * undo(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TDEAction * redo(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TDEAction * find(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TDEAction * clear(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)