~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/qt3support/text/q3multilineedit.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the Qt 3 compatibility classes of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
#include <qplatformdefs.h>
 
29
#include "q3multilineedit.h"
 
30
#ifndef QT_NO_MULTILINEEDIT
 
31
#include "qpainter.h"
 
32
#include "qscrollbar.h"
 
33
#include "qcursor.h"
 
34
#include "qclipboard.h"
 
35
#include "qpixmap.h"
 
36
#include "qregexp.h"
 
37
#include "qapplication.h"
 
38
#include "q3dragobject.h"
 
39
#include "qtimer.h"
 
40
#include <private/q3richtext_p.h>
 
41
 
 
42
 
 
43
/*!
 
44
  \class Q3MultiLineEdit qmultilineedit.h
 
45
 
 
46
  \brief The Q3MultiLineEdit widget is a simple editor for inputting text.
 
47
 
 
48
  \compat
 
49
 
 
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
 
54
  applications working.
 
55
 
 
56
  If you implement something new with Q3MultiLineEdit, we suggest using
 
57
  \l Q3TextEdit instead and call Q3TextEdit::setTextFormat(Qt::PlainText).
 
58
 
 
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.
 
66
 
 
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.
 
70
 
 
71
  \sa Q3TextEdit
 
72
*/
 
73
 
 
74
/*!
 
75
    \fn bool Q3MultiLineEdit::autoUpdate() const
 
76
*/
 
77
 
 
78
/*!
 
79
    \fn virtual void Q3MultiLineEdit::setAutoUpdate(bool b)
 
80
 
 
81
    \internal
 
82
*/
 
83
 
 
84
/*!
 
85
    \fn int Q3MultiLineEdit::totalWidth() const
 
86
*/
 
87
 
 
88
/*!
 
89
    \fn int Q3MultiLineEdit::totalHeight() const
 
90
*/
 
91
 
 
92
/*!
 
93
    \fn int Q3MultiLineEdit::maxLines() const
 
94
*/
 
95
 
 
96
/*!
 
97
    \fn void Q3MultiLineEdit::setMaxLines(int max)
 
98
 
 
99
    Sets the maximum number of lines this Q3MultiLineEdit will hold to
 
100
    \a max.
 
101
*/
 
102
 
 
103
/*!
 
104
    \fn void Q3MultiLineEdit::deselect()
 
105
*/
 
106
 
 
107
 
 
108
class Q3MultiLineEditData
 
109
{
 
110
};
 
111
 
 
112
 
 
113
/*!
 
114
  Constructs a new, empty, Q3MultiLineEdit with parent \a parent called
 
115
  \a name.
 
116
*/
 
117
 
 
118
Q3MultiLineEdit::Q3MultiLineEdit(QWidget *parent , const char *name)
 
119
    : Q3TextEdit(parent, name)
 
120
{
 
121
    d = new Q3MultiLineEditData;
 
122
    setTextFormat(Qt::PlainText);
 
123
}
 
124
 
 
125
/*! \property Q3MultiLineEdit::numLines
 
126
  \brief the number of paragraphs in the editor
 
127
 
 
128
  The count includes any empty paragraph at top and bottom, so for an
 
129
  empty editor this method returns 1.
 
130
*/
 
131
 
 
132
int Q3MultiLineEdit::numLines() const
 
133
{
 
134
    return document()->lastParagraph()->paragId() + 1;
 
135
}
 
136
 
 
137
/*! \property Q3MultiLineEdit::atEnd
 
138
  \brief whether the cursor is placed at the end of the text
 
139
 
 
140
  \sa atBeginning
 
141
*/
 
142
 
 
143
bool Q3MultiLineEdit::atEnd() const
 
144
{
 
145
    return textCursor()->paragraph() == document()->lastParagraph() && textCursor()->atParagEnd();
 
146
}
 
147
 
 
148
 
 
149
/*! \property Q3MultiLineEdit::atBeginning
 
150
  \brief whether the cursor is placed at the beginning of the text
 
151
 
 
152
  \sa atEnd
 
153
*/
 
154
 
 
155
bool Q3MultiLineEdit::atBeginning() const
 
156
{
 
157
    return textCursor()->paragraph() == document()->firstParagraph() && textCursor()->atParagStart();
 
158
}
 
159
 
 
160
/*!  Returns the number of characters at paragraph number \a row. If
 
161
  \a row is out of range, -1 is returned.
 
162
*/
 
163
 
 
164
int Q3MultiLineEdit::lineLength(int row) const
 
165
{
 
166
    if (row < 0 || row > numLines())
 
167
        return -1;
 
168
    return document()->paragAt(row)->length() - 1;
 
169
}
 
170
 
 
171
 
 
172
/*! Destructor. */
 
173
 
 
174
Q3MultiLineEdit::~Q3MultiLineEdit()
 
175
{
 
176
    delete d;
 
177
}
 
178
 
 
179
/*!
 
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.
 
183
 */
 
184
bool Q3MultiLineEdit::getMarkedRegion(int *line1, int *col1,
 
185
                                      int *line2, int *col2) const
 
186
{
 
187
    int p1,c1, p2, c2;
 
188
    getSelection(&p1, &c1, &p2, &c2);
 
189
    if (p1 == -1 && c1 == -1 && p2 == -1 && c2 == -1)
 
190
        return false;
 
191
    if (line1)
 
192
        *line1 = p1;
 
193
    if (col1)
 
194
        *col1 = c1;
 
195
    if (line2)
 
196
        *line2 = p2;
 
197
    if (col2)
 
198
        *col2 = c2;
 
199
    return true;
 
200
}
 
201
 
 
202
 
 
203
/*!
 
204
  Returns true if there is selected text.
 
205
*/
 
206
 
 
207
bool Q3MultiLineEdit::hasMarkedText() const
 
208
{
 
209
    return hasSelectedText();
 
210
}
 
211
 
 
212
 
 
213
/*!
 
214
  Returns a copy of the selected text.
 
215
*/
 
216
 
 
217
QString Q3MultiLineEdit::markedText() const
 
218
{
 
219
    return selectedText();
 
220
}
 
221
 
 
222
/*!
 
223
  Moves the cursor one page down.  If \a mark is true, the text
 
224
  is selected.
 
225
*/
 
226
 
 
227
void Q3MultiLineEdit::pageDown(bool mark)
 
228
{
 
229
    moveCursor(MoveDown, mark);
 
230
}
 
231
 
 
232
 
 
233
/*!
 
234
  Moves the cursor one page up.  If \a mark is true, the text
 
235
  is selected.
 
236
*/
 
237
 
 
238
void Q3MultiLineEdit::pageUp(bool mark)
 
239
{
 
240
    moveCursor(MovePgUp, mark);
 
241
}
 
242
 
 
243
 
 
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.
 
248
 
 
249
  The cursor position is not changed.
 
250
*/
 
251
 
 
252
void Q3MultiLineEdit::insertLine(const QString &txt, int line)
 
253
{
 
254
    insertParagraph(txt, line);
 
255
}
 
256
 
 
257
/*!  Deletes the paragraph at paragraph number \a paragraph. If \a
 
258
  paragraph is less than zero or larger than the number of paragraphs,
 
259
  nothing is deleted.
 
260
*/
 
261
 
 
262
void Q3MultiLineEdit::removeLine(int paragraph)
 
263
{
 
264
    removeParagraph(paragraph);
 
265
}
 
266
 
 
267
/*!  Inserts \a str at the current cursor position and selects the
 
268
  text if \a mark is true.
 
269
*/
 
270
 
 
271
void Q3MultiLineEdit::insertAndMark(const QString& str, bool mark)
 
272
{
 
273
    insert(str);
 
274
    if (mark)
 
275
        document()->setSelectionEnd(Q3TextDocument::Standard, *textCursor());
 
276
}
 
277
 
 
278
/*!  Splits the paragraph at the current cursor position.
 
279
*/
 
280
 
 
281
void Q3MultiLineEdit::newLine()
 
282
{
 
283
    insert("\n");
 
284
}
 
285
 
 
286
 
 
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
 
291
  del()
 
292
*/
 
293
 
 
294
void Q3MultiLineEdit::backspace()
 
295
{
 
296
    if (document()->hasSelection(Q3TextDocument::Standard)) {
 
297
        removeSelectedText();
 
298
        return;
 
299
    }
 
300
 
 
301
    if (!textCursor()->paragraph()->prev() &&
 
302
         textCursor()->atParagStart())
 
303
        return;
 
304
 
 
305
    doKeyboardAction(ActionBackspace);
 
306
}
 
307
 
 
308
 
 
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.
 
312
 
 
313
  \sa end()
 
314
*/
 
315
 
 
316
void Q3MultiLineEdit::home(bool mark)
 
317
{
 
318
    moveCursor(MoveLineStart, mark);
 
319
}
 
320
 
 
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.
 
324
 
 
325
  \sa home()
 
326
*/
 
327
 
 
328
void Q3MultiLineEdit::end(bool mark)
 
329
{
 
330
    moveCursor(MoveLineEnd, mark);
 
331
}
 
332
 
 
333
 
 
334
/*!
 
335
  \fn void Q3MultiLineEdit::setCursorPosition(int line, int col)
 
336
  \reimp
 
337
*/
 
338
 
 
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
 
341
  range.
 
342
 
 
343
  If \a mark is false, the selection is cleared. otherwise it is extended.
 
344
 
 
345
*/
 
346
 
 
347
void Q3MultiLineEdit::setCursorPosition(int line, int col, bool mark)
 
348
{
 
349
    if (!mark)
 
350
        selectAll(false);
 
351
    Q3TextEdit::setCursorPosition(line, col);
 
352
    if (mark)
 
353
        document()->setSelectionEnd(Q3TextDocument::Standard, *textCursor());
 
354
}
 
355
 
 
356
/*!  Returns the top center point where the cursor is drawn.
 
357
*/
 
358
 
 
359
QPoint Q3MultiLineEdit::cursorPoint() const
 
360
{
 
361
    return QPoint(textCursor()->x(), textCursor()->y() + textCursor()->paragraph()->rect().y());
 
362
}
 
363
 
 
364
/*!  \property Q3MultiLineEdit::alignment
 
365
  \brief The editor's paragraph alignment
 
366
 
 
367
  Sets the alignment to flag, which must be \c Qt::AlignLeft, \c
 
368
  Qt::AlignHCenter or \c Qt::AlignRight.
 
369
 
 
370
  If flag is an illegal flag, nothing happens.
 
371
*/
 
372
void Q3MultiLineEdit::setAlignment(Qt::Alignment flag)
 
373
{
 
374
    if (flag == Qt::AlignCenter)
 
375
        flag = Qt::AlignHCenter;
 
376
    if (flag != Qt::AlignLeft && flag != Qt::AlignRight && flag != Qt::AlignHCenter)
 
377
        return;
 
378
    Q3TextParagraph *p = document()->firstParagraph();
 
379
    while (p) {
 
380
        p->setAlignment(flag);
 
381
        p = p->next();
 
382
    }
 
383
}
 
384
 
 
385
Qt::Alignment Q3MultiLineEdit::alignment() const
 
386
{
 
387
    return QFlag(document()->firstParagraph()->alignment());
 
388
}
 
389
 
 
390
 
 
391
void Q3MultiLineEdit::setEdited(bool e)
 
392
{
 
393
    setModified(e);
 
394
}
 
395
 
 
396
/*!  \property Q3MultiLineEdit::edited
 
397
  \brief whether the document has been edited by the user
 
398
 
 
399
  This is the same as Q3TextEdit's "modifed" property.
 
400
*/
 
401
bool Q3MultiLineEdit::edited() const
 
402
{
 
403
    return isModified();
 
404
}
 
405
 
 
406
/*!  Moves the cursor one word to the right.  If \a mark is true, the text
 
407
  is selected.
 
408
 
 
409
  \sa cursorWordBackward()
 
410
*/
 
411
void Q3MultiLineEdit::cursorWordForward(bool mark)
 
412
{
 
413
    moveCursor(MoveWordForward, mark);
 
414
}
 
415
 
 
416
/*!  Moves the cursor one word to the left.  If \a mark is true, the
 
417
  text is selected.
 
418
 
 
419
  \sa cursorWordForward()
 
420
*/
 
421
void Q3MultiLineEdit::cursorWordBackward(bool mark)
 
422
{
 
423
    moveCursor(MoveWordBackward, mark);
 
424
}
 
425
 
 
426
/*!
 
427
  \fn Q3MultiLineEdit::insertAt(const QString &s, int line, int col)
 
428
  \reimp
 
429
*/
 
430
 
 
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.
 
435
 
 
436
  The cursor position is adjusted.
 
437
 */
 
438
 
 
439
void Q3MultiLineEdit::insertAt(const QString &s, int line, int col, bool mark)
 
440
{
 
441
    Q3TextEdit::insertAt(s, line, col);
 
442
    if (mark)
 
443
        setSelection(line, col, line, col + s.length());
 
444
}
 
445
 
 
446
// ### reggie - is this documentation correct?
 
447
 
 
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.)
 
450
*/
 
451
 
 
452
void Q3MultiLineEdit::killLine()
 
453
{
 
454
    doKeyboardAction(ActionKill);
 
455
}
 
456
 
 
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.
 
460
 
 
461
  \sa cursorRight() cursorUp() cursorDown()
 
462
*/
 
463
 
 
464
void Q3MultiLineEdit::cursorLeft(bool mark, bool)
 
465
{
 
466
    moveCursor(MoveBackward, mark);
 
467
}
 
468
 
 
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.
 
472
 
 
473
  \sa cursorLeft() cursorUp() cursorDown()
 
474
*/
 
475
 
 
476
void Q3MultiLineEdit::cursorRight(bool mark, bool)
 
477
{
 
478
    moveCursor(MoveForward, mark);
 
479
}
 
480
 
 
481
/*!  Moves the cursor up one line.  If \a mark is true, the text is
 
482
  selected.
 
483
 
 
484
  \sa cursorDown() cursorLeft() cursorRight()
 
485
*/
 
486
 
 
487
void Q3MultiLineEdit::cursorUp(bool mark)
 
488
{
 
489
    moveCursor(MoveUp, mark);
 
490
}
 
491
 
 
492
/*!
 
493
  Moves the cursor one line down.  If \a mark is true, the text
 
494
  is selected.
 
495
  \sa cursorUp() cursorLeft() cursorRight()
 
496
*/
 
497
 
 
498
void Q3MultiLineEdit::cursorDown(bool mark)
 
499
{
 
500
    moveCursor(MoveDown, mark);
 
501
}
 
502
 
 
503
 
 
504
/*!  Returns the text at line number \a line (possibly the empty
 
505
  string), or a null if \a line is invalid.
 
506
*/
 
507
 
 
508
QString Q3MultiLineEdit::textLine(int line) const
 
509
{
 
510
    if (line < 0 || line >= numLines())
 
511
        return QString();
 
512
    QString str = document()->paragAt(line)->string()->toString();
 
513
    str.truncate(str.length() - 1);
 
514
    return str;
 
515
}
 
516
 
 
517
#endif