1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the Qt 3 compatibility classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
28
#include <qplatformdefs.h>
29
#include "q3multilineedit.h"
30
#ifndef QT_NO_MULTILINEEDIT
32
#include "qscrollbar.h"
34
#include "qclipboard.h"
37
#include "qapplication.h"
38
#include "q3dragobject.h"
40
#include <private/q3richtext_p.h>
44
\class Q3MultiLineEdit qmultilineedit.h
46
\brief The Q3MultiLineEdit widget is a simple editor for inputting text.
50
The Q3MultiLineEdit was a simple editor widget in former Qt versions. Qt
51
3.0 includes a new richtext engine which obsoletes Q3MultiLineEdit. It is
52
still included for compatibility reasons. It is now a subclass of
53
\l Q3TextEdit, and provides enough of the old Q3MultiLineEdit API to keep old
56
If you implement something new with Q3MultiLineEdit, we suggest using
57
\l Q3TextEdit instead and call Q3TextEdit::setTextFormat(Qt::PlainText).
59
Although most of the old Q3MultiLineEdit API is still available, there is
60
a few difference. The old Q3MultiLineEdit operated on lines, not on
61
paragraphs. As lines change all the time during wordwrap, the new
62
richtext engine uses paragraphs as basic elements in the data structure.
63
All functions (numLines(), textLine(), etc.) that operated on lines, now
64
operate on paragraphs. Further, getString() has been removed completely.
65
It revealed too much of the internal data structure.
67
Applications which made normal and reasonable use of Q3MultiLineEdit
68
should still work without problems. Some odd usage will require some
69
porting. In these cases, it may be better to use \l Q3TextEdit now.
75
\fn bool Q3MultiLineEdit::autoUpdate() const
79
\fn virtual void Q3MultiLineEdit::setAutoUpdate(bool b)
85
\fn int Q3MultiLineEdit::totalWidth() const
89
\fn int Q3MultiLineEdit::totalHeight() const
93
\fn int Q3MultiLineEdit::maxLines() const
97
\fn void Q3MultiLineEdit::setMaxLines(int max)
99
Sets the maximum number of lines this Q3MultiLineEdit will hold to
104
\fn void Q3MultiLineEdit::deselect()
108
class Q3MultiLineEditData
114
Constructs a new, empty, Q3MultiLineEdit with parent \a parent called
118
Q3MultiLineEdit::Q3MultiLineEdit(QWidget *parent , const char *name)
119
: Q3TextEdit(parent, name)
121
d = new Q3MultiLineEditData;
122
setTextFormat(Qt::PlainText);
125
/*! \property Q3MultiLineEdit::numLines
126
\brief the number of paragraphs in the editor
128
The count includes any empty paragraph at top and bottom, so for an
129
empty editor this method returns 1.
132
int Q3MultiLineEdit::numLines() const
134
return document()->lastParagraph()->paragId() + 1;
137
/*! \property Q3MultiLineEdit::atEnd
138
\brief whether the cursor is placed at the end of the text
143
bool Q3MultiLineEdit::atEnd() const
145
return textCursor()->paragraph() == document()->lastParagraph() && textCursor()->atParagEnd();
149
/*! \property Q3MultiLineEdit::atBeginning
150
\brief whether the cursor is placed at the beginning of the text
155
bool Q3MultiLineEdit::atBeginning() const
157
return textCursor()->paragraph() == document()->firstParagraph() && textCursor()->atParagStart();
160
/*! Returns the number of characters at paragraph number \a row. If
161
\a row is out of range, -1 is returned.
164
int Q3MultiLineEdit::lineLength(int row) const
166
if (row < 0 || row > numLines())
168
return document()->paragAt(row)->length() - 1;
174
Q3MultiLineEdit::~Q3MultiLineEdit()
180
If there is selected text, sets \a line1, \a col1, \a line2 and \a col2
181
to the start and end of the selected region and returns true. Returns
182
false if there is no selected text.
184
bool Q3MultiLineEdit::getMarkedRegion(int *line1, int *col1,
185
int *line2, int *col2) const
188
getSelection(&p1, &c1, &p2, &c2);
189
if (p1 == -1 && c1 == -1 && p2 == -1 && c2 == -1)
204
Returns true if there is selected text.
207
bool Q3MultiLineEdit::hasMarkedText() const
209
return hasSelectedText();
214
Returns a copy of the selected text.
217
QString Q3MultiLineEdit::markedText() const
219
return selectedText();
223
Moves the cursor one page down. If \a mark is true, the text
227
void Q3MultiLineEdit::pageDown(bool mark)
229
moveCursor(MoveDown, mark);
234
Moves the cursor one page up. If \a mark is true, the text
238
void Q3MultiLineEdit::pageUp(bool mark)
240
moveCursor(MovePgUp, mark);
244
/*! Inserts \a txt at paragraph number \a line. If \a line is less
245
than zero, or larger than the number of paragraphs, the new text is
246
put at the end. If \a txt contains newline characters, several
247
paragraphs are inserted.
249
The cursor position is not changed.
252
void Q3MultiLineEdit::insertLine(const QString &txt, int line)
254
insertParagraph(txt, line);
257
/*! Deletes the paragraph at paragraph number \a paragraph. If \a
258
paragraph is less than zero or larger than the number of paragraphs,
262
void Q3MultiLineEdit::removeLine(int paragraph)
264
removeParagraph(paragraph);
267
/*! Inserts \a str at the current cursor position and selects the
268
text if \a mark is true.
271
void Q3MultiLineEdit::insertAndMark(const QString& str, bool mark)
275
document()->setSelectionEnd(Q3TextDocument::Standard, *textCursor());
278
/*! Splits the paragraph at the current cursor position.
281
void Q3MultiLineEdit::newLine()
287
/*! Deletes the character on the left side of the text cursor and
288
moves the cursor one position to the left. If a text has been selected
289
by the user (e.g. by clicking and dragging) the cursor is put at the
290
beginning of the selected text and the selected text is removed. \sa
294
void Q3MultiLineEdit::backspace()
296
if (document()->hasSelection(Q3TextDocument::Standard)) {
297
removeSelectedText();
301
if (!textCursor()->paragraph()->prev() &&
302
textCursor()->atParagStart())
305
doKeyboardAction(ActionBackspace);
309
/*! Moves the text cursor to the left end of the line. If \a mark is
310
true, text is selected toward the first position. If it is false and the
311
cursor is moved, all selected text is unselected.
316
void Q3MultiLineEdit::home(bool mark)
318
moveCursor(MoveLineStart, mark);
321
/*! Moves the text cursor to the right end of the line. If \a mark is
322
true, text is selected toward the last position. If it is false and the
323
cursor is moved, all selected text is unselected.
328
void Q3MultiLineEdit::end(bool mark)
330
moveCursor(MoveLineEnd, mark);
335
\fn void Q3MultiLineEdit::setCursorPosition(int line, int col)
339
/*! Sets the cursor position to character number \a col in paragraph
340
number \a line. The parameters are adjusted to lie within the legal
343
If \a mark is false, the selection is cleared. otherwise it is extended.
347
void Q3MultiLineEdit::setCursorPosition(int line, int col, bool mark)
351
Q3TextEdit::setCursorPosition(line, col);
353
document()->setSelectionEnd(Q3TextDocument::Standard, *textCursor());
356
/*! Returns the top center point where the cursor is drawn.
359
QPoint Q3MultiLineEdit::cursorPoint() const
361
return QPoint(textCursor()->x(), textCursor()->y() + textCursor()->paragraph()->rect().y());
364
/*! \property Q3MultiLineEdit::alignment
365
\brief The editor's paragraph alignment
367
Sets the alignment to flag, which must be \c Qt::AlignLeft, \c
368
Qt::AlignHCenter or \c Qt::AlignRight.
370
If flag is an illegal flag, nothing happens.
372
void Q3MultiLineEdit::setAlignment(Qt::Alignment flag)
374
if (flag == Qt::AlignCenter)
375
flag = Qt::AlignHCenter;
376
if (flag != Qt::AlignLeft && flag != Qt::AlignRight && flag != Qt::AlignHCenter)
378
Q3TextParagraph *p = document()->firstParagraph();
380
p->setAlignment(flag);
385
Qt::Alignment Q3MultiLineEdit::alignment() const
387
return QFlag(document()->firstParagraph()->alignment());
391
void Q3MultiLineEdit::setEdited(bool e)
396
/*! \property Q3MultiLineEdit::edited
397
\brief whether the document has been edited by the user
399
This is the same as Q3TextEdit's "modifed" property.
401
bool Q3MultiLineEdit::edited() const
406
/*! Moves the cursor one word to the right. If \a mark is true, the text
409
\sa cursorWordBackward()
411
void Q3MultiLineEdit::cursorWordForward(bool mark)
413
moveCursor(MoveWordForward, mark);
416
/*! Moves the cursor one word to the left. If \a mark is true, the
419
\sa cursorWordForward()
421
void Q3MultiLineEdit::cursorWordBackward(bool mark)
423
moveCursor(MoveWordBackward, mark);
427
\fn Q3MultiLineEdit::insertAt(const QString &s, int line, int col)
431
/*! Inserts string \a s at paragraph number \a line, after character
432
number \a col in the paragraph. If \a s contains newline
433
characters, new lines are inserted.
434
If \a mark is true the inserted string will be selected.
436
The cursor position is adjusted.
439
void Q3MultiLineEdit::insertAt(const QString &s, int line, int col, bool mark)
441
Q3TextEdit::insertAt(s, line, col);
443
setSelection(line, col, line, col + s.length());
446
// ### reggie - is this documentation correct?
448
/*! Deletes text from the current cursor position to the end of the
449
line. (Note that this function still operates on lines, not paragraphs.)
452
void Q3MultiLineEdit::killLine()
454
doKeyboardAction(ActionKill);
457
/*! Moves the cursor one character to the left. If \a mark is true,
458
the text is selected.
459
The \a wrap parameter is currently ignored.
461
\sa cursorRight() cursorUp() cursorDown()
464
void Q3MultiLineEdit::cursorLeft(bool mark, bool)
466
moveCursor(MoveBackward, mark);
469
/*! Moves the cursor one character to the right. If \a mark is true,
470
the text is selected.
471
The \a wrap parameter is currently ignored.
473
\sa cursorLeft() cursorUp() cursorDown()
476
void Q3MultiLineEdit::cursorRight(bool mark, bool)
478
moveCursor(MoveForward, mark);
481
/*! Moves the cursor up one line. If \a mark is true, the text is
484
\sa cursorDown() cursorLeft() cursorRight()
487
void Q3MultiLineEdit::cursorUp(bool mark)
489
moveCursor(MoveUp, mark);
493
Moves the cursor one line down. If \a mark is true, the text
495
\sa cursorUp() cursorLeft() cursorRight()
498
void Q3MultiLineEdit::cursorDown(bool mark)
500
moveCursor(MoveDown, mark);
504
/*! Returns the text at line number \a line (possibly the empty
505
string), or a null if \a line is invalid.
508
QString Q3MultiLineEdit::textLine(int line) const
510
if (line < 0 || line >= numLines())
512
QString str = document()->paragAt(line)->string()->toString();
513
str.truncate(str.length() - 1);