22 #include <QtGui/QApplication>
23 #include <QtGui/QClipboard>
24 #include <Qt3Support/Q3PopupMenu>
35 class K3TextEdit::K3TextEditPrivate
39 : customPalette( false ),
44 ~K3TextEditPrivate() {
56 QWidget *parent,
const char *name )
59 d =
new K3TextEditPrivate();
66 d =
new K3TextEditPrivate();
79 if ( KStandardShortcut::copy().contains( key ) ) {
84 else if ( KStandardShortcut::paste().contains( key ) ) {
89 else if ( KStandardShortcut::cut().contains( key ) ) {
94 else if ( KStandardShortcut::undo().contains( key ) ) {
99 else if ( KStandardShortcut::redo().contains( key ) ) {
104 else if ( KStandardShortcut::deleteWordBack().contains( key ) )
110 else if ( KStandardShortcut::deleteWordForward().contains( key ) )
116 else if ( KStandardShortcut::backwardWord().contains( key ) )
118 CursorAction action = MoveWordBackward;
120 getCursorPosition( ¶, & index );
121 if (text(para).isRightToLeft())
122 action = MoveWordForward;
123 moveCursor(action,
false );
127 else if ( KStandardShortcut::forwardWord().contains( key ) )
129 CursorAction action = MoveWordForward;
131 getCursorPosition( ¶, & index );
132 if (text(para).isRightToLeft())
133 action = MoveWordBackward;
134 moveCursor( action,
false );
138 else if ( KStandardShortcut::next().contains( key ) )
140 moveCursor( MovePgDown,
false );
144 else if ( KStandardShortcut::prior().contains( key ) )
146 moveCursor( MovePgUp,
false );
150 else if ( KStandardShortcut::begin().contains( key ) )
152 moveCursor( MoveHome,
false );
156 else if ( KStandardShortcut::end().contains( key ) )
158 moveCursor( MoveEnd,
false );
162 else if ( KStandardShortcut::beginningOfLine().contains( key ) )
164 moveCursor( MoveLineStart,
false );
168 else if ( KStandardShortcut::endOfLine().contains( key ) )
170 moveCursor(MoveLineEnd,
false);
174 else if ( KStandardShortcut::pasteSelection().contains( key ) )
176 QString text = QApplication::clipboard()->text( QClipboard::Selection);
177 if ( !text.isEmpty() )
184 else if ( e->state() == Qt::ControlModifier &&
185 (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) &&
186 topLevelWidget()->inherits(
"KDialog" ) )
192 Q3TextEdit::keyPressEvent( e );
198 moveCursor( MoveWordBackward,
true );
199 removeSelectedText();
205 moveCursor( MoveWordForward,
true );
206 removeSelectedText();
209 void K3TextEdit::slotAllowTab()
211 setTabChangesFocus(!tabChangesFocus());
216 enum { IdUndo, IdRedo, IdSep1, IdCut, IdCopy, IdPaste, IdClear, IdSep2, IdSelectAll };
218 Q3PopupMenu *menu = Q3TextEdit::createPopupMenu( pos );
221 menu->changeItem( menu->idAt(0),
KIcon(
"edit-copy"), menu->text( menu->idAt(0) ) );
223 int id = menu->idAt(0);
224 menu->changeItem(
id - IdUndo,
KIcon(
"edit-undo"), menu->text(
id - IdUndo) );
225 menu->changeItem(
id - IdRedo,
KIcon(
"edit-redo"), menu->text(
id - IdRedo) );
226 menu->changeItem(
id - IdCut,
KIcon(
"edit-cut"), menu->text(
id - IdCut) );
227 menu->changeItem(
id - IdCopy,
KIcon(
"edit-copy"), menu->text(
id - IdCopy) );
228 menu->changeItem(
id - IdPaste,
KIcon(
"edit-paste"), menu->text(
id - IdPaste) );
229 menu->changeItem(
id - IdClear,
KIcon(
"edit-clear"), menu->text(
id - IdClear) );
231 menu->insertSeparator();
232 id = menu->insertItem(
KIcon(
"tools-check-spelling" ),
i18n(
"Check Spelling..." ),
235 if( text().isEmpty() )
236 menu->setItemEnabled(
id,
false );
238 id = menu->insertItem(
i18n(
"Auto Spell Check" ),
239 this, SLOT(toggleAutoSpellCheck()) );
240 menu->setItemChecked(
id, d->checkSpellingEnabled);
241 menu->insertSeparator();
242 id=menu->insertItem(
i18n(
"Allow Tabulations"),
this,SLOT(slotAllowTab()));
243 menu->setItemChecked(
id, !tabChangesFocus());
251 return Q3TextEdit::createPopupMenu();
257 Q3TextEdit::contentsWheelEvent( e );
259 Q3ScrollView::contentsWheelEvent( e );
264 Q3TextEdit::setPalette( palette );
267 d->customPalette = ownPalette();
270 void K3TextEdit::toggleAutoSpellCheck()
277 if ( check == d->checkSpellingEnabled )
284 d->checkSpellingEnabled = check;
292 delete d->highlighter;
299 if ( d->checkSpellingEnabled && !d->highlighter )
302 Q3TextEdit::focusInEvent( e );
307 return d->checkSpellingEnabled;
312 if ( readOnly == isReadOnly() )
317 bool custom = ownPalette();
318 QPalette p = palette();
319 QColor color = p.color(QPalette::Disabled, QPalette::Background);
320 p.setColor(QPalette::Base, color);
321 p.setColor(QPalette::Background, color);
323 d->customPalette = custom;
327 if ( d->customPalette )
329 QPalette p = palette();
330 QColor color = p.color(QPalette::Normal, QPalette::Base);
331 p.setColor(QPalette::Base, color);
332 p.setColor(QPalette::Background, color);
339 Q3TextEdit::setReadOnly (readOnly);
348 d->spell =
new K3Spell(
this,
i18n(
"Spell Checking" ),
349 this, SLOT(slotSpellCheckReady(
K3Spell*)), 0,
true,
true);
351 connect( d->spell, SIGNAL(death()),
352 this, SLOT(spellCheckerFinished()) );
361 void K3TextEdit::spellCheckerMisspelling(
const QString &text,
const QStringList &,
unsigned int pos )
366 void K3TextEdit::spellCheckerCorrected(
const QString &oldWord,
const QString &newWord,
unsigned int pos )
369 unsigned int cnt = 0;
370 if ( oldWord != newWord ) {
371 posToRowCol( pos, l, cnt );
372 setSelection( l, cnt, l, cnt + oldWord.length() );
373 removeSelectedText();
378 void K3TextEdit::posToRowCol(
unsigned int pos,
unsigned int &line,
unsigned int &col)
380 for ( line = 0; line < static_cast<uint>( lines() ) && col <= pos; line++ )
381 col += paragraphLength( line ) + 1;
384 col = pos - col + paragraphLength( line ) + 1;
387 void K3TextEdit::spellCheckerFinished()
393 void K3TextEdit::slotSpellCheckReady(
K3Spell *s )
396 connect( s, SIGNAL(done(
QString)),
this, SLOT(slotSpellCheckDone(
QString)) );
399 void K3TextEdit::slotSpellCheckDone(
const QString &s )
409 unsigned int cnt = 0;
410 posToRowCol( pos, l, cnt );
411 setSelection( l, cnt, l, cnt + length );
414 #include "k3textedit.moc"