1 /****************************************************************************
2 ** $Id: qt/qtextedit.h 3.3.8 edited Jan 11 14:39 $
3 **
4 ** Definition of the QTextEdit class
5 **
6 ** Created : 990101
7 **
8 ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
9 **
10 ** This file is part of the widgets module of the Qt GUI Toolkit.
11 **
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech ASA of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
15 **
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
20 **
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses may use this file in accordance with the Qt Commercial License
23 ** Agreement provided with the Software.
24 **
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 **
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 ** information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
32 **
33 ** Contact info@trolltech.com if any conditions of this licensing are
34 ** not clear to you.
35 **
36 **********************************************************************/
37
38 #ifndef QTEXTEDIT_H
39 #define QTEXTEDIT_H
40
41 #ifndef QT_H
42 #include "qscrollview.h"
43 #include "qstylesheet.h"
44 #include "qptrvector.h"
45 #include "qvaluelist.h"
46 #include "qptrlist.h"
47 #endif // QT_H
48
49 #ifndef QT_NO_TEXTEDIT
50 // uncomment below to enable optimization mode - also uncomment the
51 // optimDoAutoScroll() private slot since moc ignores #ifdefs..
52 #define QT_TEXTEDIT_OPTIMIZATION
53
54 class QPainter;
55 class QTextDocument;
56 class QTextCursor;
57 class QKeyEvent;
58 class QResizeEvent;
59 class QMouseEvent;
60 class QTimer;
61 class QTextString;
62 class QTextCommand;
63 class QTextParagraph;
64 class QTextFormat;
65 class QFont;
66 class QColor;
67 class QTextEdit;
68 class QTextBrowser;
69 class QTextString;
70 struct QUndoRedoInfoPrivate;
71 class QPopupMenu;
72 class QTextEditPrivate;
73 class QSyntaxHighlighter;
74
75 #ifdef QT_TEXTEDIT_OPTIMIZATION
76 class QTextEditOptimPrivate
77 {
78 public:
79 // Note: no left-tag has any value for leftTag or parent, and
80 // no right-tag has any formatting flags set.
81 enum TagType { Color = 0, Format = 1 };
82 struct Tag {
83 TagType type:2;
84 bool bold:1;
85 bool italic:1;
86 bool underline:1;
87 int line;
88 int index;
89 Tag * leftTag; // ptr to left-tag in a left-right tag pair
90 Tag * parent; // ptr to parent left-tag in a nested tag
91 Tag * prev;
92 Tag * next;
93 QString tag;
94 };
QTextEditOptimPrivate()95 QTextEditOptimPrivate()
96 {
97 len = numLines = maxLineWidth = 0;
98 selStart.line = selStart.index = -1;
99 selEnd.line = selEnd.index = -1;
100 search.line = search.index = 0;
101 tags = lastTag = 0;
102 }
clearTags()103 void clearTags()
104 {
105 Tag * itr = tags;
106 while ( tags ) {
107 itr = tags;
108 tags = tags->next;
109 delete itr;
110 }
111 tags = lastTag = 0;
112 tagIndex.clear();
113 }
~QTextEditOptimPrivate()114 ~QTextEditOptimPrivate()
115 {
116 clearTags();
117 }
118 int len;
119 int numLines;
120 int maxLineWidth;
121 struct Selection {
122 int line;
123 int index;
124 };
125 Selection selStart, selEnd, search;
126 Tag * tags, * lastTag;
127 QMap<int, QString> lines;
128 QMap<int, Tag *> tagIndex;
129 };
130 #endif
131
132 class Q_EXPORT QTextEdit : public QScrollView
133 {
134 friend class QTextBrowser;
135 friend class QSyntaxHighlighter;
136
137 Q_OBJECT
138 Q_ENUMS( WordWrap WrapPolicy )
139 Q_SETS( AutoFormatting )
140 Q_PROPERTY( TextFormat textFormat READ textFormat WRITE setTextFormat )
141 Q_PROPERTY( QString text READ text WRITE setText )
142 Q_PROPERTY( QBrush paper READ paper WRITE setPaper )
143 Q_PROPERTY( bool linkUnderline READ linkUnderline WRITE setLinkUnderline )
144 Q_PROPERTY( QString documentTitle READ documentTitle )
145 Q_PROPERTY( int length READ length )
146 Q_PROPERTY( WordWrap wordWrap READ wordWrap WRITE setWordWrap )
147 Q_PROPERTY( int wrapColumnOrWidth READ wrapColumnOrWidth WRITE setWrapColumnOrWidth )
148 Q_PROPERTY( WrapPolicy wrapPolicy READ wrapPolicy WRITE setWrapPolicy )
149 Q_PROPERTY( bool hasSelectedText READ hasSelectedText )
150 Q_PROPERTY( QString selectedText READ selectedText )
151 Q_PROPERTY( int undoDepth READ undoDepth WRITE setUndoDepth )
152 Q_PROPERTY( bool overwriteMode READ isOverwriteMode WRITE setOverwriteMode )
153 Q_PROPERTY( bool modified READ isModified WRITE setModified DESIGNABLE false )
154 Q_PROPERTY( bool readOnly READ isReadOnly WRITE setReadOnly )
155 Q_PROPERTY( bool undoRedoEnabled READ isUndoRedoEnabled WRITE setUndoRedoEnabled )
156 Q_PROPERTY( int tabStopWidth READ tabStopWidth WRITE setTabStopWidth )
157 Q_PROPERTY( bool tabChangesFocus READ tabChangesFocus WRITE setTabChangesFocus )
158 Q_PROPERTY( AutoFormatting autoFormatting READ autoFormatting WRITE setAutoFormatting )
159
160 public:
161 enum WordWrap {
162 NoWrap,
163 WidgetWidth,
164 FixedPixelWidth,
165 FixedColumnWidth
166 };
167
168 enum WrapPolicy {
169 AtWordBoundary,
170 AtWhiteSpace = AtWordBoundary, // AtWhiteSpace is deprecated
171 Anywhere,
172 AtWordOrDocumentBoundary
173 };
174
175 enum AutoFormatting {
176 AutoNone = 0,
177 AutoBulletList = 0x00000001,
178 AutoAll = 0xffffffff
179 };
180
181 enum KeyboardAction {
182 ActionBackspace,
183 ActionDelete,
184 ActionReturn,
185 ActionKill,
186 ActionWordBackspace,
187 ActionWordDelete
188 };
189
190 enum CursorAction {
191 MoveBackward,
192 MoveForward,
193 MoveWordBackward,
194 MoveWordForward,
195 MoveUp,
196 MoveDown,
197 MoveLineStart,
198 MoveLineEnd,
199 MoveHome,
200 MoveEnd,
201 MovePgUp,
202 MovePgDown
203 };
204
205 enum VerticalAlignment {
206 AlignNormal,
207 AlignSuperScript,
208 AlignSubScript
209 };
210
211 enum TextInsertionFlags {
212 RedoIndentation = 0x0001,
213 CheckNewLines = 0x0002,
214 RemoveSelected = 0x0004
215 };
216
217 QTextEdit( const QString& text, const QString& context = QString::null,
218 QWidget* parent=0, const char* name=0);
219 QTextEdit( QWidget* parent=0, const char* name=0 );
220 virtual ~QTextEdit();
221 void setPalette( const QPalette & );
222
223 QString text() const;
224 QString text( int para ) const;
225 TextFormat textFormat() const;
226 QString context() const;
227 QString documentTitle() const;
228
229 void getSelection( int *paraFrom, int *indexFrom,
230 int *paraTo, int *indexTo, int selNum = 0 ) const;
231 virtual bool find( const QString &expr, bool cs, bool wo, bool forward = TRUE,
232 int *para = 0, int *index = 0 );
233
234 int paragraphs() const;
235 int lines() const;
236 int linesOfParagraph( int para ) const;
237 int lineOfChar( int para, int chr );
238 int length() const;
239 QRect paragraphRect( int para ) const;
240 int paragraphAt( const QPoint &pos ) const;
241 int charAt( const QPoint &pos, int *para ) const;
242 int paragraphLength( int para ) const;
243
244 QStyleSheet* styleSheet() const;
245 #ifndef QT_NO_MIME
246 QMimeSourceFactory* mimeSourceFactory() const;
247 #endif
248 QBrush paper() const;
249 bool linkUnderline() const;
250
251 int heightForWidth( int w ) const;
252
253 bool hasSelectedText() const;
254 QString selectedText() const;
255 bool isUndoAvailable() const;
256 bool isRedoAvailable() const;
257
258 WordWrap wordWrap() const;
259 int wrapColumnOrWidth() const;
260 WrapPolicy wrapPolicy() const;
261
262 int tabStopWidth() const;
263
264 QString anchorAt( const QPoint& pos );
265 QString anchorAt( const QPoint& pos, AnchorAttribute a );
266
267 QSize sizeHint() const;
268
isReadOnly()269 bool isReadOnly() const { return readonly; }
270
271 void getCursorPosition( int *parag, int *index ) const;
272
273 bool isModified() const;
274 bool italic() const;
275 bool bold() const;
276 bool underline() const;
277 QString family() const;
278 int pointSize() const;
279 QColor color() const;
280 QFont font() const;
281 QFont currentFont() const;
282 int alignment() const;
283 int undoDepth() const;
284
285 // do not use, will go away
286 virtual bool getFormat( int para, int index, QFont *font, QColor *color, VerticalAlignment *verticalAlignment );
287 // do not use, will go away
288 virtual bool getParagraphFormat( int para, QFont *font, QColor *color,
289 VerticalAlignment *verticalAlignment, int *alignment,
290 QStyleSheetItem::DisplayMode *displayMode,
291 QStyleSheetItem::ListStyle *listStyle,
292 int *listDepth );
293
294
isOverwriteMode()295 bool isOverwriteMode() const { return overWrite; }
296 QColor paragraphBackgroundColor( int para ) const;
297
298 bool isUndoRedoEnabled() const;
299 bool eventFilter( QObject *o, QEvent *e );
300 bool tabChangesFocus() const;
301
302 void setAutoFormatting( uint features );
303 uint autoFormatting() const;
304 QSyntaxHighlighter *syntaxHighlighter() const;
305
306 public slots:
307 void setEnabled( bool );
308 #ifndef QT_NO_MIME
309 virtual void setMimeSourceFactory( QMimeSourceFactory* factory );
310 #endif
311 virtual void setStyleSheet( QStyleSheet* styleSheet );
312 virtual void scrollToAnchor( const QString& name );
313 virtual void setPaper( const QBrush& pap );
314 virtual void setLinkUnderline( bool );
315
316 virtual void setWordWrap( WordWrap mode );
317 virtual void setWrapColumnOrWidth( int );
318 virtual void setWrapPolicy( WrapPolicy policy );
319
320 virtual void copy();
321 virtual void append( const QString& text );
322
setText(const QString & txt)323 void setText( const QString &txt ) { setText( txt, QString::null ); }
324 virtual void setText( const QString &txt, const QString &context );
325 virtual void setTextFormat( TextFormat f );
326
327 virtual void selectAll( bool select = TRUE );
328 virtual void setTabStopWidth( int ts );
329 virtual void zoomIn( int range );
zoomIn()330 virtual void zoomIn() { zoomIn( 1 ); }
331 virtual void zoomOut( int range );
zoomOut()332 virtual void zoomOut() { zoomOut( 1 ); }
333 virtual void zoomTo( int size );
334
335 virtual void sync();
336 virtual void setReadOnly( bool b );
337
338 virtual void undo();
339 virtual void redo();
340 virtual void cut();
341 virtual void paste();
342 #ifndef QT_NO_CLIPBOARD
343 virtual void pasteSubType( const QCString &subtype );
344 #endif
345 virtual void clear();
346 virtual void del();
347 virtual void indent();
348 virtual void setItalic( bool b );
349 virtual void setBold( bool b );
350 virtual void setUnderline( bool b );
351 virtual void setFamily( const QString &f );
352 virtual void setPointSize( int s );
353 virtual void setColor( const QColor &c );
354 virtual void setFont( const QFont &f );
355 virtual void setVerticalAlignment( VerticalAlignment a );
356 virtual void setAlignment( int a );
357
358 // do not use, will go away
359 virtual void setParagType( QStyleSheetItem::DisplayMode dm, QStyleSheetItem::ListStyle listStyle );
360
361 virtual void setCursorPosition( int parag, int index );
362 virtual void setSelection( int parag_from, int index_from, int parag_to, int index_to, int selNum = 0 );
363 virtual void setSelectionAttributes( int selNum, const QColor &back, bool invertText );
364 virtual void setModified( bool m );
365 virtual void resetFormat();
366 virtual void setUndoDepth( int d );
367 virtual void setFormat( QTextFormat *f, int flags );
368 virtual void ensureCursorVisible();
369 virtual void placeCursor( const QPoint &pos, QTextCursor *c = 0 );
370 virtual void moveCursor( CursorAction action, bool select );
371 virtual void doKeyboardAction( KeyboardAction action );
372 virtual void removeSelectedText( int selNum = 0 );
373 virtual void removeSelection( int selNum = 0 );
374 virtual void setCurrentFont( const QFont &f );
setOverwriteMode(bool b)375 virtual void setOverwriteMode( bool b ) { overWrite = b; }
376
377 virtual void scrollToBottom();
378
379 void insert( const QString &text, uint insertionFlags = CheckNewLines | RemoveSelected ); // ## virtual in 4.0
380
381 // obsolete
382 virtual void insert( const QString &text, bool, bool = TRUE, bool = TRUE );
383
384 virtual void insertAt( const QString &text, int para, int index );
385 virtual void removeParagraph( int para );
386 virtual void insertParagraph( const QString &text, int para );
387
388 virtual void setParagraphBackgroundColor( int para, const QColor &bg );
389 virtual void clearParagraphBackground( int para );
390
391 virtual void setUndoRedoEnabled( bool b );
392 void setTabChangesFocus( bool b ); // ### make virtual in 4.0
393
394 #ifdef QT_TEXTEDIT_OPTIMIZATION
395 void polish();
396 void setMaxLogLines( int numLines );
397 int maxLogLines();
398 #endif
399
400 signals:
401 void textChanged();
402 void selectionChanged();
403 void copyAvailable( bool );
404 void undoAvailable( bool yes );
405 void redoAvailable( bool yes );
406 void currentFontChanged( const QFont &f );
407 void currentColorChanged( const QColor &c );
408 void currentAlignmentChanged( int a );
409 void currentVerticalAlignmentChanged( VerticalAlignment a );
410 void cursorPositionChanged( QTextCursor *c );
411 void cursorPositionChanged( int para, int pos );
412 void returnPressed();
413 void modificationChanged( bool m );
414 void clicked( int parag, int index );
415 void doubleClicked( int parag, int index );
416
417 protected:
418 void repaintChanged();
419 void updateStyles();
420 void drawContents( QPainter *p, int cx, int cy, int cw, int ch );
421 bool event( QEvent *e );
422 void keyPressEvent( QKeyEvent *e );
423 void resizeEvent( QResizeEvent *e );
424 void viewportResizeEvent( QResizeEvent* );
425 void contentsMousePressEvent( QMouseEvent *e );
426 void contentsMouseMoveEvent( QMouseEvent *e );
427 void contentsMouseReleaseEvent( QMouseEvent *e );
428 void contentsMouseDoubleClickEvent( QMouseEvent *e );
429 #ifndef QT_NO_WHEELEVENT
430 void contentsWheelEvent( QWheelEvent *e );
431 #endif
432 void imStartEvent( QIMEvent * );
433 void imComposeEvent( QIMEvent * );
434 void imEndEvent( QIMEvent * );
435 #ifndef QT_NO_DRAGANDDROP
436 void contentsDragEnterEvent( QDragEnterEvent *e );
437 void contentsDragMoveEvent( QDragMoveEvent *e );
438 void contentsDragLeaveEvent( QDragLeaveEvent *e );
439 void contentsDropEvent( QDropEvent *e );
440 #endif
441 void contentsContextMenuEvent( QContextMenuEvent *e );
442 bool focusNextPrevChild( bool next );
443 QTextDocument *document() const;
444 QTextCursor *textCursor() const;
445 void setDocument( QTextDocument *doc );
446 virtual QPopupMenu *createPopupMenu( const QPoint& pos );
447 virtual QPopupMenu *createPopupMenu();
448 void drawCursor( bool visible );
449
450 void windowActivationChange( bool );
451
452 protected slots:
453 virtual void doChangeInterval();
454 void sliderReleased(); // ### make virtual in 4.0
455 #if (QT_VERSION >= 0x040000)
456 #error "Some functions need to be changed to virtual for Qt 4.0"
457 #endif
458
459 private slots:
460 void formatMore();
461 void doResize();
462 void autoScrollTimerDone();
463 void blinkCursor();
464 void setModified();
465 void startDrag();
466 void documentWidthChanged( int w );
467 void clipboardChanged();
468
469 private:
470 struct Q_EXPORT UndoRedoInfo {
471 enum Type { Invalid, Insert, Delete, Backspace, Return, RemoveSelected, Format, Style, IME };
472
473 UndoRedoInfo( QTextDocument *dc );
474 ~UndoRedoInfo();
475 void clear();
476 bool valid() const;
477
478 QUndoRedoInfoPrivate *d;
479 int id;
480 int index;
481 int eid;
482 int eindex;
483 QTextFormat *format;
484 int flags;
485 Type type;
486 QTextDocument *doc;
487 QByteArray styleInformation;
488 };
489
490 private:
491 void updateCursor( const QPoint & pos );
492 void handleMouseMove( const QPoint& pos );
493 void drawContents( QPainter * );
linksEnabled()494 virtual bool linksEnabled() const { return FALSE; }
495 void init();
496 void checkUndoRedoInfo( UndoRedoInfo::Type t );
497 void updateCurrentFormat();
498 bool handleReadOnlyKeyEvent( QKeyEvent *e );
499 void makeParagVisible( QTextParagraph *p );
500 void normalCopy();
501 void copyToClipboard();
502 #ifndef QT_NO_MIME
503 QCString pickSpecial(QMimeSource* ms, bool always_ask, const QPoint&);
504 QTextDrag *dragObject( QWidget *parent = 0 ) const;
505 #endif
506 #ifndef QT_NO_MIMECLIPBOARD
507 void pasteSpecial(const QPoint&);
508 #endif
509 void setFontInternal( const QFont &f );
510
emitHighlighted(const QString &)511 virtual void emitHighlighted( const QString & ) {}
emitLinkClicked(const QString &)512 virtual void emitLinkClicked( const QString & ) {}
513
514 void readFormats( QTextCursor &c1, QTextCursor &c2, QTextString &text, bool fillStyles = FALSE );
515 void clearUndoRedo();
516 void paintDocument( bool drawAll, QPainter *p, int cx = -1, int cy = -1, int cw = -1, int ch = -1 );
517 void moveCursor( CursorAction action );
518 void ensureFormatted( QTextParagraph *p );
519 void placeCursor( const QPoint &pos, QTextCursor *c, bool link );
520 void updateMicroFocusHint();
521
522 #ifdef QT_TEXTEDIT_OPTIMIZATION
523 bool checkOptimMode();
524 QString optimText() const;
525 void optimSetText( const QString &str );
526 void optimAppend( const QString &str );
527 void optimInsert( const QString &str, int line, int index );
528 void optimDrawContents( QPainter * p, int cx, int cy, int cw, int ch );
529 void optimMousePressEvent( QMouseEvent * e );
530 void optimMouseReleaseEvent( QMouseEvent * e );
531 void optimMouseMoveEvent( QMouseEvent * e );
532 int optimCharIndex( const QString &str, int mx ) const;
533 void optimSelectAll();
534 void optimRemoveSelection();
535 void optimSetSelection( int startLine, int startIdx, int endLine,
536 int endIdx );
537 bool optimHasSelection() const;
538 QString optimSelectedText() const;
539 bool optimFind( const QString & str, bool, bool, bool, int *, int * );
540 void optimParseTags( QString * str, int lineNo = -1, int indexOffset = 0 );
541 QTextEditOptimPrivate::Tag * optimPreviousLeftTag( int line );
542 void optimSetTextFormat( QTextDocument *, QTextCursor *, QTextFormat * f,
543 int, int, QTextEditOptimPrivate::Tag * t );
544 QTextEditOptimPrivate::Tag * optimAppendTag( int index, const QString & tag );
545 QTextEditOptimPrivate::Tag * optimInsertTag( int line, int index, const QString & tag );
546 void optimCheckLimit( const QString& str );
547 bool optimHasBoldMetrics( int line );
548
549 private slots:
550 void optimDoAutoScroll();
551 #endif // QT_TEXTEDIT_OPTIMIZATION
552
553 private:
554 #ifndef QT_NO_CLIPBOARD
555 void pasteSubType( const QCString &subtype, QMimeSource *m );
556 #endif
557
558 private:
559 QTextDocument *doc;
560 QTextCursor *cursor;
561 QTimer *formatTimer, *scrollTimer, *changeIntervalTimer, *blinkTimer, *dragStartTimer;
562 QTextParagraph *lastFormatted;
563 int interval;
564 UndoRedoInfo undoRedoInfo;
565 QTextFormat *currentFormat;
566 int currentAlignment;
567 QPoint oldMousePos, mousePos;
568 QPoint dragStartPos;
569 QString onLink;
570 WordWrap wrapMode;
571 WrapPolicy wPolicy;
572 int wrapWidth;
573 QString pressedLink;
574 QTextEditPrivate *d;
575 bool inDoubleClick : 1;
576 bool mousePressed : 1;
577 bool cursorVisible : 1;
578 bool blinkCursorVisible : 1;
579 bool readOnly : 1;
580 bool modified : 1;
581 bool mightStartDrag : 1;
582 bool inDnD : 1;
583 bool readonly : 1;
584 bool undoEnabled : 1;
585 bool overWrite : 1;
586
587 private: // Disabled copy constructor and operator=
588 #if defined(Q_DISABLE_COPY)
589 QTextEdit( const QTextEdit & );
590 QTextEdit &operator=( const QTextEdit & );
591 #endif
592 };
593
document()594 inline QTextDocument *QTextEdit::document() const
595 {
596 return doc;
597 }
598
textCursor()599 inline QTextCursor *QTextEdit::textCursor() const
600 {
601 return cursor;
602 }
603
setCurrentFont(const QFont & f)604 inline void QTextEdit::setCurrentFont( const QFont &f )
605 {
606 QTextEdit::setFontInternal( f );
607 }
608
609 #endif //QT_NO_TEXTEDIT
610 #endif //QTEXTVIEW_H
611