lib

KoTextObject.h
1/* This file is part of the KDE project
2 Copyright (C) 2001-2006 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 kotextobject_h
21#define kotextobject_h
22
23#include <KoRichText.h>
24#include "KoChangeCaseDia.h"
25#include "KoStyleCollection.h"
26#include "KoTextDocument.h"
27#include <koffice_export.h>
28
29class KoSavingContext;
30class KCommand;
31class KoTextFormat;
32class TQProgressDialog;
33class KoLinkVariable;
34class KoVariable;
35
36//#define TIMING_FORMAT
37//#include <tqdatetime.h>
38
48class KOTEXT_EXPORT KoTextFormatInterface
49{
50public:
51 KoTextFormatInterface() {}
52 virtual ~KoTextFormatInterface() {}
53
55 virtual KoTextFormat * currentFormat() const = 0;
56
57 virtual bool rtl() const = 0;
58
65 virtual KCommand *setFormatCommand( const KoTextFormat *format, int flags, bool zoomFont = false ) = 0;
66
68 virtual const KoParagLayout * currentParagLayoutFormat() const = 0;
69
75 virtual KCommand *setParagLayoutFormatCommand( KoParagLayout *newLayout, int flags, int marginIndex=-1) = 0;
76
77 virtual KCommand *setChangeCaseOfTextCommand(KoChangeCaseDia::TypeOfCase _type)=0;
78
79 KoTextDocCommand *deleteTextCommand( KoTextDocument *textdoc, int id, int index, const TQMemArray<KoTextStringChar> & str, const CustomItemsMap & customItemsMap, const TQValueList<KoParagLayout> & oldParagLayouts );
80
81 void setParagLayoutFormat( KoParagLayout *newLayout,int flags, int marginIndex=-1);
82 void setFormat( KoTextFormat * newFormat, int flags, bool zoomFont = false );
83
84 // Warning: use the methods that return a command! The others just leak the commands away
85 //void setBold(bool on);
86 KCommand *setBoldCommand(bool on);
87 //void setItalic(bool on);
88 KCommand *setItalicCommand(bool on);
89 //void setUnderline(bool on);
90 KCommand *setUnderlineCommand(bool on);
91 //void setStrikeOut(bool on);
92 KCommand *setDoubleUnderlineCommand( bool on );
93 KCommand *setUnderlineColorCommand( const TQColor &color );
94 KCommand *setStrikeOutCommand(bool on);
95 //void setTextColor(const TQColor &color);
96 KCommand *setTextColorCommand(const TQColor &color);
97 //void setPointSize( int s );
98 KCommand *setPointSizeCommand( int s );
99 //void setFamily(const TQString &font);
100 KCommand *setFamilyCommand(const TQString &font);
101 //void setTextSubScript(bool on);
102 KCommand *setTextSubScriptCommand(bool on);
103 //void setTextSuperScript(bool on);
104 KCommand *setTextSuperScriptCommand(bool on);
105
106 //void setDefaultFormat();
107 KCommand *setDefaultFormatCommand();
108
109 //void setTextBackgroundColor(const TQColor &);
110 KCommand *setTextBackgroundColorCommand(const TQColor &);
111
112 //void setAlign(int align);
113 KCommand *setAlignCommand(int align);
114
115 //void setMargin(TQStyleSheetItem::Margin m, double margin);
116 KCommand *setMarginCommand(TQStyleSheetItem::Margin m, double margin);
117
118 //void setTabList(const KoTabulatorList & tabList );
119 KCommand *setTabListCommand(const KoTabulatorList & tabList );
120
121 //void setBackgroundColor(const TQColor & color );
122 KCommand *setBackgroundColorCommand(const TQColor & color );
123
124 //void setCounter(const KoParagCounter & counter );
125 KCommand *setCounterCommand(const KoParagCounter & counter );
126
127 KCommand *setLanguageCommand(const TQString &);
128
129 KCommand *setShadowTextCommand( double shadowDistanceX, double shadowDistanceY, const TQColor& shadowColor );
130
131 KCommand *setHyphenationCommand( bool _b );
132
133
134 KCommand *setFontAttributeCommand( KoTextFormat::AttributeStyle _att);
135
136 KCommand *setRelativeTextSizeCommand( double _size );
137
138 KCommand *setOffsetFromBaseLineCommand( int _offset );
139
140 KCommand *setWordByWordCommand( bool _b );
141
142
143 TQColor textColor() const;
144 TQFont textFont() const;
145 TQString textFontFamily()const;
146 TQString language() const;
147 TQColor textBackgroundColor()const;
148 TQColor textUnderlineColor()const;
149
150 KoTextFormat::UnderlineType underlineType()const;
151 KoTextFormat::StrikeOutType strikeOutType()const;
152 KoTextFormat::UnderlineStyle underlineStyle()const;
153 KoTextFormat::StrikeOutStyle strikeOutStyle()const;
154
155
156
157 bool textUnderline()const;
158 bool textDoubleUnderline()const;
159
160 bool textBold()const;
161 bool textStrikeOut()const;
162 bool textItalic() const;
163 bool textSubScript() const;
164 bool textSuperScript() const;
165 double shadowDistanceX() const;
166 double shadowDistanceY() const;
167 TQColor shadowColor() const;
168 KoTextFormat::AttributeStyle fontAttribute() const;
169 double relativeTextSize() const;
170 int offsetFromBaseLine()const;
171 bool wordByWord()const;
172 bool hyphenation()const;
173};
174
181class KOTEXT_EXPORT KoTextObject : public TQObject, public KoTextFormatInterface
182{
183 TQ_OBJECT
184
185public:
199 KoTextObject( KoTextZoomHandler *zh, const TQFont& defaultFont, const TQString &defaultLanguage,
200 bool defaultHyphenation, KoParagStyle* defaultStyle, int tabStopWidth = -1,
201 TQObject* parent = 0, const char *name = 0 );
202
212 KoTextObject( KoTextDocument *textdoc, KoParagStyle* defaultStyle,
213 TQObject* parent = 0, const char *name = 0 );
214
215 virtual ~KoTextObject();
216
217
218 static const char * acceptSelectionMimeType();
221 static TQCString providesOasis( TQMimeSource* mime );
222
223 void setNeedSpellCheck(bool b);
224 bool needSpellCheck() const { return m_needsSpellCheck;}
225 void setProtectContent(bool b) { m_protectContent = b; }
226 bool protectContent() const{ return m_protectContent;}
230 KoTextDocument *textDocument() const { return textdoc; }
231
232 void setAvailableHeight( int avail ) { m_availableHeight = avail; }
233 int availableHeight() const;
234
235 void undo();
236 void redo();
238 void clearUndoRedoInfo();
239
241 bool hasSelection() const { return textdoc->hasSelection( KoTextDocument::Standard, true ); }
243 TQString selectedText( KoTextDocument::SelectionId selectionId = KoTextDocument::Standard ) const {
244 return textdoc->selectedText( selectionId );
245 }
246
247 bool selectionHasCustomItems( KoTextDocument::SelectionId selectionId = KoTextDocument::Standard ) const;
248
250 DefaultInsertFlags = 0,
251 CheckNewLine = 1,
255 };
256
268 void insert( KoTextCursor * cursor, KoTextFormat * currentFormat, const TQString &text,
269 const TQString & commandName,
270 KoTextDocument::SelectionId selectionId = KoTextDocument::Standard,
271 int insertFlags = DefaultInsertFlags, // KDE4: TODO use TQFlags
272 CustomItemsMap customItemsMap = CustomItemsMap() );
273
281 void removeSelectedText( KoTextCursor * cursor, KoTextDocument::SelectionId selectionId = KoTextDocument::Standard,
282 const TQString & cmdName = TQString(), bool createUndoRedo=true );
283
284 KCommand * replaceSelectionCommand( KoTextCursor * cursor, const TQString & replacement,
285 const TQString & cmdName,
286 KoTextDocument::SelectionId selectionId = KoTextDocument::Standard,
287 int insertFlags = DefaultInsertFlags, // KDE4: TODO use TQFlags
288 CustomItemsMap customItemsMap = CustomItemsMap() );
289 KCommand * removeSelectedTextCommand( KoTextCursor * cursor, KoTextDocument::SelectionId selectionId, bool repaint = true );
290 KCommand* insertParagraphCommand( KoTextCursor * cursor );
291
304 void pasteText( KoTextCursor * cursor, const TQString & text, KoTextFormat * currentFormat,
305 bool removeSelected );
306 void selectAll( bool select );
307
311 void highlightPortion( KoTextParag * parag, int index, int length, bool repaint );
312 void removeHighlight( bool repaint );
313
315 KCommand *setFormatCommand( const KoTextFormat *format, int flags, bool zoomFont = false );
316
319 KCommand *setFormatCommand( KoTextCursor * cursor, KoTextFormat ** currentFormat, const KoTextFormat *format, int flags, bool zoomFont = false, KoTextDocument::SelectionId selectionId = KoTextDocument::Standard );
320
321 enum KeyboardAction { // keep in sync with TQTextEdit
322 ActionBackspace,
323 ActionDelete,
324 ActionReturn,
325 ActionKill
326 };
329 void doKeyboardAction( KoTextCursor * cursor, KoTextFormat * & currentFormat, KeyboardAction action );
330
331 // -- Paragraph settings --
332 KCommand * setCounterCommand( KoTextCursor * cursor, const KoParagCounter & counter, KoTextDocument::SelectionId selectionId = KoTextDocument::Standard );
333 KCommand * setAlignCommand( KoTextCursor * cursor, int align , KoTextDocument::SelectionId selectionId = KoTextDocument::Standard);
334 KCommand * setLineSpacingCommand( KoTextCursor * cursor, double spacing, KoParagLayout::SpacingType _type,KoTextDocument::SelectionId selectionId = KoTextDocument::Standard );
335 KCommand * setBordersCommand( KoTextCursor * cursor, const KoBorder& leftBorder, const KoBorder& rightBorder, const KoBorder& topBorder, const KoBorder& bottomBorder, KoTextDocument::SelectionId selectionId = KoTextDocument::Standard );
336 KCommand * setJoinBordersCommand( KoTextCursor * cursor, bool join, KoTextDocument::SelectionId selectionId = KoTextDocument::Standard );
337 KCommand * setMarginCommand( KoTextCursor * cursor, TQStyleSheetItem::Margin m, double margin, KoTextDocument::SelectionId selectionId = KoTextDocument::Standard);
338 KCommand* setTabListCommand( KoTextCursor * cursor,const KoTabulatorList & tabList , KoTextDocument::SelectionId selectionId = KoTextDocument::Standard );
339 KCommand* setBackgroundColorCommand( KoTextCursor * cursor,const TQColor & color , KoTextDocument::SelectionId selectionId = KoTextDocument::Standard );
340
341 KCommand * setParagDirectionCommand( KoTextCursor * cursor, TQChar::Direction d, KoTextDocument::SelectionId selectionId = KoTextDocument::Standard );
342
353 void applyStyle( KoTextCursor * cursor, const KoParagStyle * style,
354 KoTextDocument::SelectionId selectionId = KoTextDocument::Standard,
355 int paragLayoutFlags = KoParagLayout::All, int formatFlags = KoTextFormat::Format,
356 bool createUndoRedo = true, bool interactive = true );
357
363 KCommand* applyStyleCommand( KoTextCursor * cursor, const KoParagStyle * style,
364 KoTextDocument::SelectionId selectionId = KoTextDocument::Standard,
365 int paragLayoutFlags = KoParagLayout::All, int formatFlags = KoTextFormat::Format,
366 bool createUndoRedo = true, bool interactive = true );
367
368
373 void applyStyleChange( KoStyleChangeDefMap changed );
376 void setFormat( KoTextCursor * cursor, KoTextFormat ** currentFormat, KoTextFormat *format, int flags, bool zoomFont = false );
377
378
384 virtual KoTextFormat * currentFormat() const;
385
389 virtual const KoParagLayout * currentParagLayoutFormat() const;
390
391 virtual bool rtl() const;
392
396 virtual KCommand *setParagLayoutFormatCommand( KoParagLayout *newLayout, int flags, int marginIndex=-1);
397
398 // common for setParagLayoutFormatCommand above and KoTextView::setParagLayoutFormatCommand
399 KCommand *setParagLayoutCommand( KoTextCursor * cursor, const KoParagLayout& paragLayout,
400 KoTextDocument::SelectionId selectionId, int paragLayoutFlags,
401 int marginIndex, bool createUndoRedo );
405 virtual void setFormat( KoTextFormat * newFormat, int flags, bool zoomFont = false );
406
408 int docFontSize( KoTextFormat * format ) const;
410 int zoomedFontSize( int docFontSize ) const;
411
413 void setViewArea( TQWidget* w, int maxY );
415 void ensureFormatted( KoTextParag * parag, bool emitAfterFormatting = true );
416 void setLastFormattedParag( KoTextParag *parag );
417
418 static TQChar customItemChar() { return TQChar( s_customItemChar ); }
419
420 // TQt should really have support for public signals
421 void emitHideCursor() { emit hideCursor(); }
422 void emitShowCursor() { emit showCursor(); }
423 void emitEnsureCursorVisible() { emit ensureCursorVisible(); }
424 void emitUpdateUI( bool updateFormat, bool force = false ) { emit updateUI( updateFormat, force ); }
425
426 void typingStarted();
427 void typingDone();
428
435 void abortFormatting();
436
437 void selectionChangedNotify( bool enableActions = true );
438
439 void emitNewCommand(KCommand *cmd);
440
441 virtual KCommand *setChangeCaseOfTextCommand(KoChangeCaseDia::TypeOfCase _type);
442
443 KCommand *changeCaseOfText(KoTextCursor *cursor, KoChangeCaseDia::TypeOfCase _type);
444 TQString textChangedCase(const TQString& _text, KoChangeCaseDia::TypeOfCase _type);
445 KCommand *changeCaseOfTextParag(int cursorPosStart, int cursorPosEnd,KoChangeCaseDia::TypeOfCase _type,KoTextCursor *cursor, KoTextParag *parag);
446
447 void loadOasisContent( const TQDomElement &bodyElem, KoOasisContext& context, KoStyleCollection * styleColl );
448 void saveOasisContent( KoXmlWriter& writer, KoSavingContext& context ) const;
449
450 // Similar to KoTextDocument::loadOasisText but there's no newline inserted before the first paragraph
451 // or after the last one - so it's possible to paste just a few chars.
452 // It also handles m_lastFormatted
453 KoTextCursor pasteOasisText( const TQDomElement &bodyElem, KoOasisContext& context,
454 KoTextCursor& cursor, KoStyleCollection * styleColl );
455
456#ifndef NDEBUG
457 void printRTDebug(int);
458#endif
459
460 bool statistics( TQProgressDialog *progress, ulong & charsWithSpace, ulong & charsWithoutSpace, ulong & words, ulong & sentences, ulong & syllables, ulong & lines, bool selected );
461 int numberOfparagraphLineSelected( KoTextParag *parag);
462
466 KoVariable* variableAtPoint( const TQPoint& iPoint ) const;
467
473 KoVariable* variableAtPosition( KoTextParag* parag, int index ) const;
474
475 enum ParagModifyType { AddChar = 0, RemoveChar = 1, ChangeFormat = 2 };
476
477signals:
481
485 void afterFormatting( int bottom, KoTextParag* m_lastFormatted, bool* abort );
486
491 void chapterParagraphFormatted( KoTextParag* parag );
492
496
501 void newCommand( KCommand *cmd );
502
505
506 void hideCursor();
507 void showCursor();
509 void setCursor( KoTextCursor * cursor );
512 void updateUI( bool updateFormat, bool force = false );
519
520 void showFormatObject(const KoTextFormat &);
521
522 // Keeping track of text modifications - not emitted during loading/closing.
523 void paragraphCreated( KoTextParag* parag );
524 void paragraphModified( KoTextParag* parag, int /*ParagModifyType*/, int pos, int length );
525 void paragraphDeleted( KoTextParag* parag );
526
527public slots:
528 // The default arguments are those used by the formatTimer.
529 // The return value is used by ensureFormatted
530 bool formatMore( int count = 10, bool emitAfterFormatting = true );
531
532 void emitRepaintChanged() { emit repaintChanged( this ); }
533
534public: // made public for KWTextFrameSet...
535
539 void storeParagUndoRedoInfo( KoTextCursor * cursor, KoTextDocument::SelectionId selectionId = KoTextDocument::Standard );
541 void copyCharFormatting( KoTextParag *parag, int position, int index /*in text*/, bool moveCustomItems );
542 void readFormats( KoTextCursor &c1, KoTextCursor &c2, bool copyParagLayouts = false, bool moveCustomItems = false );
543
553 struct KOTEXT_EXPORT UndoRedoInfo { // borrowed from TQTextEdit
554 enum Type { Invalid, Insert, Delete, Return, RemoveSelected };
555 UndoRedoInfo( KoTextObject* textobj );
556 ~UndoRedoInfo() {}
557 void clear();
558 bool valid() const;
559
560 KoTextString text; // storage for formatted text
561 int id; // id of first parag
562 int eid; // id of last parag
563 int index; // index (for insertion/deletion)
564 Type type; // type of command
565 KoTextObject* textobj; // parent
566 CustomItemsMap customItemsMap; // character position -> qtextcustomitem
567 TQValueList<KoParagLayout> oldParagLayouts;
568 KoParagLayout newParagLayout;
569 KoTextCursor *cursor; // basically a "mark" of the view that started this undo/redo info
570 // If the view changes, the next call to checkUndoRedoInfo will terminate the previous view's edition
571 KMacroCommand *placeHolderCmd;
572 };
573
579 void newPlaceHolderCommand( const TQString & name );
580 void checkUndoRedoInfo( KoTextCursor * cursor, UndoRedoInfo::Type t );
581
583 UndoRedoInfo & undoRedoInfoStruct() { return undoRedoInfo; }
584
585 void setVisible(bool vis) { m_visible=vis; }
586 bool isVisible() const { return m_visible; }
587
588private slots:
589 void doChangeInterval();
594 void slotAfterUndoRedo();
595 void slotParagraphModified(KoTextParag *, int, int , int);
596 void slotParagraphCreated(KoTextParag *);
597 void slotParagraphDeleted(KoTextParag *);
598private:
599 void init();
600
601private:
602 class KoTextObjectPrivate;
603 KoTextObjectPrivate* d;
605 KoTextDocument *textdoc;
606
609 KoParagStyle* m_defaultStyle;
610
611 bool m_visible;
612
614 UndoRedoInfo undoRedoInfo;
615
619 KoTextParag *m_lastFormatted;
621 TQTimer *formatTimer, *changeIntervalTimer;
622 int interval;
623
625 int m_availableHeight;
627 TQMap<TQWidget *, int> m_mapViewAreas;
628
629 //TQPtrDict<int> m_origFontSizes; // Format -> doc font size.
630
631 bool m_highlightSelectionAdded;
632
633#ifdef TIMING_FORMAT
634 TQTime m_time;
635#endif
636
637 static const char s_customItemChar;
638 bool m_needsSpellCheck;
639 bool m_protectContent;
640};
641
642#endif
A CustomItemsMap associates a custom item to an index Used in the undo/redo info for insert/delete te...
This is the structure associated with a paragraph (KoTextParag), to define the bullet or numbering of...
This class holds the paragraph-specific formatting information It's separated from KoTextParag so tha...
SpacingType
Line spacing values Assuming h is the height of the biggest char in the line, and v is the linespacin...
A paragraph style is a combination of a character style and paragraph-layout attributes,...
Used during saving to Oasis format (and discarded at the end of the saving).
virtual KCommand * setFormatCommand(const KoTextFormat *format, int flags, bool zoomFont=false)=0
Interface for setting the modified format.
virtual const KoParagLayout * currentParagLayoutFormat() const =0
Interface for accessing the current parag layout.
virtual KoTextFormat * currentFormat() const =0
Interface for accessing the current format.
virtual KCommand * setParagLayoutFormatCommand(KoParagLayout *newLayout, int flags, int marginIndex=-1)=0
Interface for changing the paragraph layout.
Each character (KoTextStringChar) points to a KoTextFormat that defines the formatting of that charac...
void newPlaceHolderCommand(const TQString &name)
Creates a place holder for a command that will be completed later on.
bool hasSelection() const
return true if some text is selected
void repaintChanged(KoTextObject *)
Tell the world that we'd like some repainting to happen.
void selectionChanged(bool hasSelection)
Tell the views that the selection changed (for cut/copy...).
void showCurrentFormat()
Same thing, when the current format (of the edit object) was changed.
void availableHeightNeeded()
Emitted by availableHeight() when the available height hasn't been calculated yet or is invalid.
void formattingFirstParag()
Emitted by formatMore() when formatting the first paragraph.
void setCursor(KoTextCursor *cursor)
Special hack for undo/redo - used by KoTextView.
@ OverwriteMode
< if true, the text to be inserted is checked for '\n' (as a paragraph delimiter)
@ DoNotRemoveSelected
whether to remove selected text before
@ DoNotRepaint
usually we repaint in insert(), this allows to turn it off
void afterFormatting(int bottom, KoTextParag *m_lastFormatted, bool *abort)
Emitted by formatMore() after formatting a bunch of paragraphs.
UndoRedoInfo & undoRedoInfoStruct()
for KWTextFrameSet
void chapterParagraphFormatted(KoTextParag *parag)
Emitted by formatMore() when formatting a "Head 1" paragraph.
TQString selectedText(KoTextDocument::SelectionId selectionId=KoTextDocument::Standard) const
returns the selected text [without formatting] if hasSelection()
KoTextObject(KoTextZoomHandler *zh, const TQFont &defaultFont, const TQString &defaultLanguage, bool defaultHyphenation, KoParagStyle *defaultStyle, int tabStopWidth=-1, TQObject *parent=0, const char *name=0)
Constructor.
KoTextDocument * textDocument() const
Return the text document contained in this KoTextObject.
void ensureCursorVisible()
The views should make sure the cursor is visible.
void updateUI(bool updateFormat, bool force=false)
Emitted when the formatting under the cursor may have changed.
static TQCString providesOasis(TQMimeSource *mime)
Check if the mimesource mime provides one of the OASIS mimetypes, and if so, return it.
void newCommand(KCommand *cmd)
Emitted when a new command has been created and should be added to the main list of commands (usually...
This class extends KoZoomHandler to add support for WYSIWYG text layouting.
A KoVariable is a custom item, i.e.
Definition KoVariable.h:364
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)
The undo-redo structure holds the temporary information for the current undo/redo command.