ktextedit.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "ktextedit.h" 00021 00022 #include <tqapplication.h> 00023 #include <tqclipboard.h> 00024 #include <tqpopupmenu.h> 00025 00026 #include <ksyntaxhighlighter.h> 00027 #include <tdespell.h> 00028 #include <kcursor.h> 00029 #include <tdeglobalsettings.h> 00030 #include <tdestdaccel.h> 00031 #include <kiconloader.h> 00032 #include <tdelocale.h> 00033 00034 class KTextEdit::KTextEditPrivate 00035 { 00036 public: 00037 KTextEditPrivate() 00038 : customPalette( false ), 00039 checkSpellingEnabled( false ), 00040 highlighter( 0 ), 00041 spell( 0 ) 00042 {} 00043 ~KTextEditPrivate() { 00044 delete highlighter; 00045 delete spell; 00046 } 00047 00048 bool customPalette; 00049 bool checkSpellingEnabled; 00050 KDictSpellingHighlighter *highlighter; 00051 KSpell *spell; 00052 }; 00053 00054 KTextEdit::KTextEdit( const TQString& text, const TQString& context, 00055 TQWidget *parent, const char *name ) 00056 : TQTextEdit ( text, context, parent, name ) 00057 { 00058 d = new KTextEditPrivate(); 00059 KCursor::setAutoHideCursor( this, true, false ); 00060 } 00061 00062 KTextEdit::KTextEdit( TQWidget *parent, const char *name ) 00063 : TQTextEdit ( parent, name ) 00064 { 00065 d = new KTextEditPrivate(); 00066 KCursor::setAutoHideCursor( this, true, false ); 00067 } 00068 00069 KTextEdit::~KTextEdit() 00070 { 00071 delete d; 00072 } 00073 00074 void KTextEdit::keyPressEvent( TQKeyEvent *e ) 00075 { 00076 KKey key( e ); 00077 00078 if ( TDEStdAccel::copy().contains( key ) ) { 00079 copy(); 00080 e->accept(); 00081 return; 00082 } 00083 else if ( TDEStdAccel::paste().contains( key ) ) { 00084 paste(); 00085 e->accept(); 00086 return; 00087 } 00088 else if ( TDEStdAccel::cut().contains( key ) ) { 00089 cut(); 00090 e->accept(); 00091 return; 00092 } 00093 else if ( TDEStdAccel::undo().contains( key ) ) { 00094 undo(); 00095 e->accept(); 00096 return; 00097 } 00098 else if ( TDEStdAccel::redo().contains( key ) ) { 00099 redo(); 00100 e->accept(); 00101 return; 00102 } 00103 else if ( TDEStdAccel::deleteWordBack().contains( key ) ) 00104 { 00105 deleteWordBack(); 00106 e->accept(); 00107 return; 00108 } 00109 else if ( TDEStdAccel::deleteWordForward().contains( key ) ) 00110 { 00111 deleteWordForward(); 00112 e->accept(); 00113 return; 00114 } 00115 else if ( TDEStdAccel::backwardWord().contains( key ) ) 00116 { 00117 CursorAction action = MoveWordBackward; 00118 int para, index; 00119 getCursorPosition( ¶, & index ); 00120 if (text(para).isRightToLeft()) 00121 action = MoveWordForward; 00122 moveCursor(action, false ); 00123 e->accept(); 00124 return; 00125 } 00126 else if ( TDEStdAccel::forwardWord().contains( key ) ) 00127 { 00128 CursorAction action = MoveWordForward; 00129 int para, index; 00130 getCursorPosition( ¶, & index ); 00131 if (text(para).isRightToLeft()) 00132 action = MoveWordBackward; 00133 moveCursor( action, false ); 00134 e->accept(); 00135 return; 00136 } 00137 else if ( TDEStdAccel::next().contains( key ) ) 00138 { 00139 moveCursor( MovePgDown, false ); 00140 e->accept(); 00141 return; 00142 } 00143 else if ( TDEStdAccel::prior().contains( key ) ) 00144 { 00145 moveCursor( MovePgUp, false ); 00146 e->accept(); 00147 return; 00148 } 00149 else if ( TDEStdAccel::home().contains( key ) ) 00150 { 00151 moveCursor( MoveHome, false ); 00152 e->accept(); 00153 return; 00154 } 00155 else if ( TDEStdAccel::end().contains( key ) ) 00156 { 00157 moveCursor( MoveEnd, false ); 00158 e->accept(); 00159 return; 00160 } 00161 else if ( TDEStdAccel::beginningOfLine().contains( key ) ) 00162 { 00163 moveCursor( MoveLineStart, false ); 00164 e->accept(); 00165 return; 00166 } 00167 else if ( TDEStdAccel::endOfLine().contains( key ) ) 00168 { 00169 moveCursor(MoveLineEnd, false); 00170 e->accept(); 00171 return; 00172 } 00173 else if ( TDEStdAccel::pasteSelection().contains( key ) ) 00174 { 00175 TQString text = TQApplication::clipboard()->text( TQClipboard::Selection); 00176 if ( !text.isEmpty() ) 00177 insert( text ); 00178 e->accept(); 00179 return; 00180 } 00181 00182 // ignore Ctrl-Return so that KDialogs can close the dialog 00183 else if ( e->state() == ControlButton && 00184 (e->key() == Key_Return || e->key() == Key_Enter) && 00185 topLevelWidget()->inherits( "KDialog" ) ) 00186 { 00187 e->ignore(); 00188 return; 00189 } 00190 00191 TQTextEdit::keyPressEvent( e ); 00192 } 00193 00194 void KTextEdit::deleteWordBack() 00195 { 00196 removeSelection(); 00197 moveCursor( MoveWordBackward, true ); 00198 removeSelectedText(); 00199 } 00200 00201 void KTextEdit::deleteWordForward() 00202 { 00203 removeSelection(); 00204 moveCursor( MoveWordForward, true ); 00205 removeSelectedText(); 00206 } 00207 00208 void KTextEdit::slotAllowTab() 00209 { 00210 setTabChangesFocus(!tabChangesFocus()); 00211 } 00212 00213 TQPopupMenu *KTextEdit::createPopupMenu( const TQPoint &pos ) 00214 { 00215 enum { IdUndo, IdRedo, IdSep1, IdCut, IdCopy, IdPaste, IdClear, IdSep2, IdSelectAll }; 00216 00217 TQPopupMenu *menu = TQTextEdit::createPopupMenu( pos ); 00218 00219 if ( isReadOnly() ) 00220 menu->changeItem( menu->idAt(0), SmallIconSet("edit-copy"), menu->text( menu->idAt(0) ) ); 00221 else { 00222 int id = menu->idAt(0); 00223 menu->changeItem( id - IdUndo, SmallIconSet("edit-undo"), menu->text( id - IdUndo) ); 00224 menu->changeItem( id - IdRedo, SmallIconSet("edit-redo"), menu->text( id - IdRedo) ); 00225 menu->changeItem( id - IdCut, SmallIconSet("edit-cut"), menu->text( id - IdCut) ); 00226 menu->changeItem( id - IdCopy, SmallIconSet("edit-copy"), menu->text( id - IdCopy) ); 00227 menu->changeItem( id - IdPaste, SmallIconSet("edit-paste"), menu->text( id - IdPaste) ); 00228 menu->changeItem( id - IdClear, SmallIconSet("edit-clear"), menu->text( id - IdClear) ); 00229 00230 menu->insertSeparator(); 00231 id = menu->insertItem( SmallIconSet( "tools-check-spelling" ), i18n( "Check Spelling..." ), 00232 this, TQT_SLOT( checkSpelling() ) ); 00233 00234 if( text().isEmpty() ) 00235 menu->setItemEnabled( id, false ); 00236 00237 id = menu->insertItem( i18n( "Auto Spell Check" ), 00238 this, TQT_SLOT( toggleAutoSpellCheck() ) ); 00239 menu->setItemChecked(id, d->checkSpellingEnabled); 00240 menu->insertSeparator(); 00241 id=menu->insertItem(i18n("Allow Tabulations"),this,TQT_SLOT(slotAllowTab())); 00242 menu->setItemChecked(id, !tabChangesFocus()); 00243 } 00244 00245 return menu; 00246 } 00247 00248 TQPopupMenu *KTextEdit::createPopupMenu() 00249 { 00250 return TQTextEdit::createPopupMenu(); 00251 } 00252 00253 void KTextEdit::contentsWheelEvent( TQWheelEvent *e ) 00254 { 00255 if ( TDEGlobalSettings::wheelMouseZooms() ) 00256 TQTextEdit::contentsWheelEvent( e ); 00257 else // thanks, we don't want to zoom, so skip QTextEdit's impl. 00258 TQScrollView::contentsWheelEvent( e ); 00259 } 00260 00261 void KTextEdit::setPalette( const TQPalette& palette ) 00262 { 00263 TQTextEdit::setPalette( palette ); 00264 // unsetPalette() is not virtual and calls setPalette() as well 00265 // so we can use ownPalette() to find out about unsetting 00266 d->customPalette = ownPalette(); 00267 } 00268 00269 void KTextEdit::toggleAutoSpellCheck() 00270 { 00271 setCheckSpellingEnabled( !d->checkSpellingEnabled ); 00272 } 00273 00274 void KTextEdit::setCheckSpellingEnabled( bool check ) 00275 { 00276 if ( check == d->checkSpellingEnabled ) 00277 return; 00278 00279 // From the above statment we know know that if we're turning checking 00280 // on that we need to create a new highlighter and if we're turning it 00281 // off we should remove the old one. 00282 00283 d->checkSpellingEnabled = check; 00284 if ( check ) 00285 { 00286 if (hasFocus()) 00287 d->highlighter = new KDictSpellingHighlighter( this ); 00288 } 00289 else 00290 { 00291 delete d->highlighter; 00292 d->highlighter = 0; 00293 } 00294 } 00295 00296 void KTextEdit::focusInEvent( TQFocusEvent *e ) 00297 { 00298 if ( d->checkSpellingEnabled && !isReadOnly() && !d->highlighter ) 00299 d->highlighter = new KDictSpellingHighlighter( this ); 00300 00301 TQTextEdit::focusInEvent( e ); 00302 } 00303 00304 bool KTextEdit::checkSpellingEnabled() const 00305 { 00306 return d->checkSpellingEnabled; 00307 } 00308 00309 void KTextEdit::setReadOnly(bool readOnly) 00310 { 00311 if ( !readOnly && hasFocus() && d->checkSpellingEnabled && !d->highlighter ) 00312 d->highlighter = new KDictSpellingHighlighter( this ); 00313 00314 if ( readOnly == isReadOnly() ) 00315 return; 00316 00317 if (readOnly) 00318 { 00319 delete d->highlighter; 00320 d->highlighter = 0; 00321 00322 bool custom = ownPalette(); 00323 TQPalette p = palette(); 00324 TQColor color = p.color(TQPalette::Disabled, TQColorGroup::Background); 00325 p.setColor(TQColorGroup::Base, color); 00326 p.setColor(TQColorGroup::Background, color); 00327 setPalette(p); 00328 d->customPalette = custom; 00329 } 00330 else 00331 { 00332 if ( d->customPalette ) 00333 { 00334 TQPalette p = palette(); 00335 TQColor color = p.color(TQPalette::Normal, TQColorGroup::Base); 00336 p.setColor(TQColorGroup::Base, color); 00337 p.setColor(TQColorGroup::Background, color); 00338 setPalette( p ); 00339 } 00340 else 00341 unsetPalette(); 00342 } 00343 00344 TQTextEdit::setReadOnly (readOnly); 00345 } 00346 00347 void KTextEdit::virtual_hook( int, void* ) 00348 { /*BASE::virtual_hook( id, data );*/ } 00349 00350 void KTextEdit::checkSpelling() 00351 { 00352 delete d->spell; 00353 d->spell = new KSpell( this, i18n( "Spell Checking" ), 00354 TQT_TQOBJECT(this), TQT_SLOT( slotSpellCheckReady( KSpell *) ), 0, true, true); 00355 00356 connect( d->spell, TQT_SIGNAL( death() ), 00357 this, TQT_SLOT( spellCheckerFinished() ) ); 00358 00359 connect( d->spell, TQT_SIGNAL( misspelling( const TQString &, const TQStringList &, unsigned int ) ), 00360 this, TQT_SLOT( spellCheckerMisspelling( const TQString &, const TQStringList &, unsigned int ) ) ); 00361 00362 connect( d->spell, TQT_SIGNAL( corrected( const TQString &, const TQString &, unsigned int ) ), 00363 this, TQT_SLOT( spellCheckerCorrected( const TQString &, const TQString &, unsigned int ) ) ); 00364 } 00365 00366 void KTextEdit::spellCheckerMisspelling( const TQString &text, const TQStringList &, unsigned int pos ) 00367 { 00368 highLightWord( text.length(), pos ); 00369 } 00370 00371 void KTextEdit::spellCheckerCorrected( const TQString &oldWord, const TQString &newWord, unsigned int pos ) 00372 { 00373 unsigned int l = 0; 00374 unsigned int cnt = 0; 00375 if ( oldWord != newWord ) { 00376 posToRowCol( pos, l, cnt ); 00377 setSelection( l, cnt, l, cnt + oldWord.length() ); 00378 removeSelectedText(); 00379 insert( newWord ); 00380 } 00381 } 00382 00383 void KTextEdit::posToRowCol(unsigned int pos, unsigned int &line, unsigned int &col) 00384 { 00385 for ( line = 0; line < static_cast<uint>( lines() ) && col <= pos; line++ ) 00386 col += paragraphLength( line ) + 1; 00387 00388 line--; 00389 col = pos - col + paragraphLength( line ) + 1; 00390 } 00391 00392 void KTextEdit::spellCheckerFinished() 00393 { 00394 delete d->spell; 00395 d->spell = 0L; 00396 } 00397 00398 void KTextEdit::slotSpellCheckReady( KSpell *s ) 00399 { 00400 s->check( text() ); 00401 connect( s, TQT_SIGNAL( done( const TQString & ) ), this, TQT_SLOT( slotSpellCheckDone( const TQString & ) ) ); 00402 } 00403 00404 void KTextEdit::slotSpellCheckDone( const TQString &s ) 00405 { 00406 if ( s != text() ) 00407 setText( s ); 00408 } 00409 00410 00411 void KTextEdit::highLightWord( unsigned int length, unsigned int pos ) 00412 { 00413 unsigned int l = 0; 00414 unsigned int cnt = 0; 00415 posToRowCol( pos, l, cnt ); 00416 setSelection( l, cnt, l, cnt + length ); 00417 } 00418 00419 #include "ktextedit.moc"