• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
ktextedit.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "ktextedit.h"
21
22#include <tqapplication.h>
23#include <tqclipboard.h>
24#include <tqpopupmenu.h>
25
26#include <ksyntaxhighlighter.h>
27#include <tdespell.h>
28#include <kcursor.h>
29#include <tdeglobalsettings.h>
30#include <tdestdaccel.h>
31#include <kiconloader.h>
32#include <tdelocale.h>
33
34class KTextEdit::KTextEditPrivate
35{
36public:
37 KTextEditPrivate()
38 : customPalette( false ),
39 checkSpellingEnabled( false ),
40 highlighter( 0 ),
41 spell( 0 )
42 {}
43 ~KTextEditPrivate() {
44 delete highlighter;
45 delete spell;
46 }
47
48 bool customPalette;
49 bool checkSpellingEnabled;
50 KDictSpellingHighlighter *highlighter;
51 KSpell *spell;
52};
53
54KTextEdit::KTextEdit( const TQString& text, const TQString& context,
55 TQWidget *parent, const char *name )
56 : TQTextEdit ( text, context, parent, name )
57{
58 d = new KTextEditPrivate();
59 KCursor::setAutoHideCursor( this, true, false );
60}
61
62KTextEdit::KTextEdit( TQWidget *parent, const char *name )
63 : TQTextEdit ( parent, name )
64{
65 d = new KTextEditPrivate();
66 KCursor::setAutoHideCursor( this, true, false );
67}
68
69KTextEdit::~KTextEdit()
70{
71 delete d;
72}
73
74void KTextEdit::keyPressEvent( TQKeyEvent *e )
75{
76 KKey key( e );
77
78 if ( TDEStdAccel::copy().contains( key ) ) {
79 copy();
80 e->accept();
81 return;
82 }
83 else if ( TDEStdAccel::paste().contains( key ) ) {
84 paste();
85 e->accept();
86 return;
87 }
88 else if ( TDEStdAccel::cut().contains( key ) ) {
89 cut();
90 e->accept();
91 return;
92 }
93 else if ( TDEStdAccel::undo().contains( key ) ) {
94 undo();
95 e->accept();
96 return;
97 }
98 else if ( TDEStdAccel::redo().contains( key ) ) {
99 redo();
100 e->accept();
101 return;
102 }
103 else if ( TDEStdAccel::deleteWordBack().contains( key ) )
104 {
105 deleteWordBack();
106 e->accept();
107 return;
108 }
109 else if ( TDEStdAccel::deleteWordForward().contains( key ) )
110 {
111 deleteWordForward();
112 e->accept();
113 return;
114 }
115 else if ( TDEStdAccel::backwardWord().contains( key ) )
116 {
117 CursorAction action = MoveWordBackward;
118 int para, index;
119 getCursorPosition( &para, & index );
120 if (text(para).isRightToLeft())
121 action = MoveWordForward;
122 moveCursor(action, false );
123 e->accept();
124 return;
125 }
126 else if ( TDEStdAccel::forwardWord().contains( key ) )
127 {
128 CursorAction action = MoveWordForward;
129 int para, index;
130 getCursorPosition( &para, & index );
131 if (text(para).isRightToLeft())
132 action = MoveWordBackward;
133 moveCursor( action, false );
134 e->accept();
135 return;
136 }
137 else if ( TDEStdAccel::next().contains( key ) )
138 {
139 moveCursor( MovePgDown, false );
140 e->accept();
141 return;
142 }
143 else if ( TDEStdAccel::prior().contains( key ) )
144 {
145 moveCursor( MovePgUp, false );
146 e->accept();
147 return;
148 }
149 else if ( TDEStdAccel::home().contains( key ) )
150 {
151 moveCursor( MoveHome, false );
152 e->accept();
153 return;
154 }
155 else if ( TDEStdAccel::end().contains( key ) )
156 {
157 moveCursor( MoveEnd, false );
158 e->accept();
159 return;
160 }
161 else if ( TDEStdAccel::beginningOfLine().contains( key ) )
162 {
163 moveCursor( MoveLineStart, false );
164 e->accept();
165 return;
166 }
167 else if ( TDEStdAccel::endOfLine().contains( key ) )
168 {
169 moveCursor(MoveLineEnd, false);
170 e->accept();
171 return;
172 }
173 else if ( TDEStdAccel::pasteSelection().contains( key ) )
174 {
175 TQString text = TQApplication::clipboard()->text( TQClipboard::Selection);
176 if ( !text.isEmpty() )
177 insert( text );
178 e->accept();
179 return;
180 }
181
182 // ignore Ctrl-Return so that KDialogs can close the dialog
183 else if ( e->state() == ControlButton &&
184 (e->key() == Key_Return || e->key() == Key_Enter) &&
185 topLevelWidget()->inherits( "KDialog" ) )
186 {
187 e->ignore();
188 return;
189 }
190
191 TQTextEdit::keyPressEvent( e );
192}
193
194void KTextEdit::deleteWordBack()
195{
196 removeSelection();
197 moveCursor( MoveWordBackward, true );
198 removeSelectedText();
199}
200
201void KTextEdit::deleteWordForward()
202{
203 removeSelection();
204 moveCursor( MoveWordForward, true );
205 removeSelectedText();
206}
207
208void KTextEdit::slotAllowTab()
209{
210setTabChangesFocus(!tabChangesFocus());
211}
212
213TQPopupMenu *KTextEdit::createPopupMenu( const TQPoint &pos )
214{
215 enum { IdUndo, IdRedo, IdSep1, IdCut, IdCopy, IdPaste, IdClear, IdSep2, IdSelectAll };
216
217 TQPopupMenu *menu = TQTextEdit::createPopupMenu( pos );
218
219 if ( isReadOnly() )
220 menu->changeItem( menu->idAt(0), SmallIconSet("edit-copy"), menu->text( menu->idAt(0) ) );
221 else {
222 int id = menu->idAt(0);
223 menu->changeItem( id - IdUndo, SmallIconSet("edit-undo"), menu->text( id - IdUndo) );
224 menu->changeItem( id - IdRedo, SmallIconSet("edit-redo"), menu->text( id - IdRedo) );
225 menu->changeItem( id - IdCut, SmallIconSet("edit-cut"), menu->text( id - IdCut) );
226 menu->changeItem( id - IdCopy, SmallIconSet("edit-copy"), menu->text( id - IdCopy) );
227 menu->changeItem( id - IdPaste, SmallIconSet("edit-paste"), menu->text( id - IdPaste) );
228 menu->changeItem( id - IdClear, SmallIconSet("edit-clear"), menu->text( id - IdClear) );
229
230 menu->insertSeparator();
231 id = menu->insertItem( SmallIconSet( "tools-check-spelling" ), i18n( "Check Spelling..." ),
232 this, TQ_SLOT( checkSpelling() ) );
233
234 if( text().isEmpty() )
235 menu->setItemEnabled( id, false );
236
237 id = menu->insertItem( i18n( "Auto Spell Check" ),
238 this, TQ_SLOT( toggleAutoSpellCheck() ) );
239 menu->setItemChecked(id, d->checkSpellingEnabled);
240 menu->insertSeparator();
241 id=menu->insertItem(i18n("Allow Tabulations"),this,TQ_SLOT(slotAllowTab()));
242 menu->setItemChecked(id, !tabChangesFocus());
243 }
244
245 return menu;
246}
247
248TQPopupMenu *KTextEdit::createPopupMenu()
249{
250 return TQTextEdit::createPopupMenu();
251}
252
253void KTextEdit::contentsWheelEvent( TQWheelEvent *e )
254{
255 if ( TDEGlobalSettings::wheelMouseZooms() )
256 TQTextEdit::contentsWheelEvent( e );
257 else // thanks, we don't want to zoom, so skip QTextEdit's impl.
258 TQScrollView::contentsWheelEvent( e );
259}
260
261void KTextEdit::setPalette( const TQPalette& palette )
262{
263 TQTextEdit::setPalette( palette );
264 // unsetPalette() is not virtual and calls setPalette() as well
265 // so we can use ownPalette() to find out about unsetting
266 d->customPalette = ownPalette();
267}
268
269void KTextEdit::toggleAutoSpellCheck()
270{
271 setCheckSpellingEnabled( !d->checkSpellingEnabled );
272}
273
274void KTextEdit::setCheckSpellingEnabled( bool check )
275{
276 if ( check == d->checkSpellingEnabled )
277 return;
278
279 // From the above statment we know know that if we're turning checking
280 // on that we need to create a new highlighter and if we're turning it
281 // off we should remove the old one.
282
283 d->checkSpellingEnabled = check;
284 if ( check )
285 {
286 if (hasFocus())
287 d->highlighter = new KDictSpellingHighlighter( this );
288 }
289 else
290 {
291 delete d->highlighter;
292 d->highlighter = 0;
293 }
294}
295
296void KTextEdit::focusInEvent( TQFocusEvent *e )
297{
298 if ( d->checkSpellingEnabled && !isReadOnly() && !d->highlighter )
299 d->highlighter = new KDictSpellingHighlighter( this );
300
301 TQTextEdit::focusInEvent( e );
302}
303
304bool KTextEdit::checkSpellingEnabled() const
305{
306 return d->checkSpellingEnabled;
307}
308
309void KTextEdit::setReadOnly(bool readOnly)
310{
311 if ( !readOnly && hasFocus() && d->checkSpellingEnabled && !d->highlighter )
312 d->highlighter = new KDictSpellingHighlighter( this );
313
314 if ( readOnly == isReadOnly() )
315 return;
316
317 if (readOnly)
318 {
319 delete d->highlighter;
320 d->highlighter = 0;
321
322 bool custom = ownPalette();
323 TQPalette p = palette();
324 TQColor color = p.color(TQPalette::Disabled, TQColorGroup::Background);
325 p.setColor(TQColorGroup::Base, color);
326 p.setColor(TQColorGroup::Background, color);
327 setPalette(p);
328 d->customPalette = custom;
329 }
330 else
331 {
332 if ( d->customPalette )
333 {
334 TQPalette p = palette();
335 TQColor color = p.color(TQPalette::Active, TQColorGroup::Base);
336 p.setColor(TQColorGroup::Base, color);
337 p.setColor(TQColorGroup::Background, color);
338 setPalette( p );
339 }
340 else
341 unsetPalette();
342 }
343
344 TQTextEdit::setReadOnly (readOnly);
345}
346
347void KTextEdit::virtual_hook( int, void* )
348{ /*BASE::virtual_hook( id, data );*/ }
349
350void KTextEdit::checkSpelling()
351{
352 delete d->spell;
353 d->spell = new KSpell( this, i18n( "Spell Checking" ),
354 this, TQ_SLOT( slotSpellCheckReady( KSpell *) ), 0, true, true);
355
356 connect( d->spell, TQ_SIGNAL( death() ),
357 this, TQ_SLOT( spellCheckerFinished() ) );
358
359 connect( d->spell, TQ_SIGNAL( misspelling( const TQString &, const TQStringList &, unsigned int ) ),
360 this, TQ_SLOT( spellCheckerMisspelling( const TQString &, const TQStringList &, unsigned int ) ) );
361
362 connect( d->spell, TQ_SIGNAL( corrected( const TQString &, const TQString &, unsigned int ) ),
363 this, TQ_SLOT( spellCheckerCorrected( const TQString &, const TQString &, unsigned int ) ) );
364}
365
366void KTextEdit::spellCheckerMisspelling( const TQString &text, const TQStringList &, unsigned int pos )
367{
368 highLightWord( text.length(), pos );
369}
370
371void KTextEdit::spellCheckerCorrected( const TQString &oldWord, const TQString &newWord, unsigned int pos )
372{
373 unsigned int l = 0;
374 unsigned int cnt = 0;
375 if ( oldWord != newWord ) {
376 posToRowCol( pos, l, cnt );
377 setSelection( l, cnt, l, cnt + oldWord.length() );
378 removeSelectedText();
379 insert( newWord );
380 }
381}
382
383void KTextEdit::posToRowCol(unsigned int pos, unsigned int &line, unsigned int &col)
384{
385 for ( line = 0; line < static_cast<uint>( lines() ) && col <= pos; line++ )
386 col += paragraphLength( line ) + 1;
387
388 line--;
389 col = pos - col + paragraphLength( line ) + 1;
390}
391
392void KTextEdit::spellCheckerFinished()
393{
394 delete d->spell;
395 d->spell = 0L;
396}
397
398void KTextEdit::slotSpellCheckReady( KSpell *s )
399{
400 s->check( text() );
401 connect( s, TQ_SIGNAL( done( const TQString & ) ), this, TQ_SLOT( slotSpellCheckDone( const TQString & ) ) );
402}
403
404void KTextEdit::slotSpellCheckDone( const TQString &s )
405{
406 if ( s != text() )
407 setText( s );
408}
409
410
411void KTextEdit::highLightWord( unsigned int length, unsigned int pos )
412{
413 unsigned int l = 0;
414 unsigned int cnt = 0;
415 posToRowCol( pos, l, cnt );
416 setSelection( l, cnt, l, cnt + length );
417}
418
419#include "ktextedit.moc"
KCursor::setAutoHideCursor
static void setAutoHideCursor(TQWidget *w, bool enable)
Sets auto-hiding the cursor for widget w.
Definition: kcursor.cpp:218
KDictSpellingHighlighter
Dictionary sensitive text highlighter.
Definition: ksyntaxhighlighter.h:95
KKey
KSpell
KDE Spellchecker
Definition: tdespell.h:47
KSpell::check
virtual bool check(const TQString &_buffer, bool usedialog=true)
Spellchecks a buffer of many words in plain text format.
Definition: tdespell.cpp:976
KTextEdit::setPalette
virtual void setPalette(const TQPalette &palette)
Reimplemented for tracking custom palettes.
Definition: ktextedit.cpp:261
KTextEdit::checkSpellingEnabled
bool checkSpellingEnabled() const
Returns true if spell checking is enabled for this text edit.
Definition: ktextedit.cpp:304
KTextEdit::checkSpelling
void checkSpelling()
Create a modal dialog to check the spelling.
Definition: ktextedit.cpp:350
KTextEdit::setCheckSpellingEnabled
void setCheckSpellingEnabled(bool check)
Turns spell checking for this text edit on or off.
Definition: ktextedit.cpp:274
KTextEdit::keyPressEvent
virtual void keyPressEvent(TQKeyEvent *)
Reimplemented to catch "delete word" key events.
Definition: ktextedit.cpp:74
KTextEdit::KTextEdit
KTextEdit(const TQString &text, const TQString &context=TQString::null, TQWidget *parent=0, const char *name=0)
Constructs a KTextEdit object.
Definition: ktextedit.cpp:54
KTextEdit::setReadOnly
virtual void setReadOnly(bool readOnly)
Reimplemented to set a proper "deactivated" background color.
Definition: ktextedit.cpp:309
KTextEdit::deleteWordBack
virtual void deleteWordBack()
Deletes a word backwards from the current cursor position, if available.
Definition: ktextedit.cpp:194
KTextEdit::contentsWheelEvent
virtual void contentsWheelEvent(TQWheelEvent *)
Reimplemented to allow fast-wheelscrolling with Ctrl-Wheel or zoom.
Definition: ktextedit.cpp:253
KTextEdit::createPopupMenu
virtual TQPopupMenu * createPopupMenu()
This is just a reimplementation of a deprecated method from TQTextEdit and is just here to keep sourc...
Definition: ktextedit.cpp:248
KTextEdit::~KTextEdit
~KTextEdit()
Destroys the KTextEdit object.
Definition: ktextedit.cpp:69
KTextEdit::focusInEvent
virtual void focusInEvent(TQFocusEvent *)
Reimplemented to instantiate a KDictSpellingHighlighter, if spellchecking is enabled.
Definition: ktextedit.cpp:296
KTextEdit::deleteWordForward
virtual void deleteWordForward()
Deletes a word forwards from the current cursor position, if available.
Definition: ktextedit.cpp:201
TDEGlobalSettings::wheelMouseZooms
static bool wheelMouseZooms()
TDEStdAccel::copy
const TDEShortcut & copy()
TDEStdAccel::endOfLine
const TDEShortcut & endOfLine()
TDEStdAccel::next
const TDEShortcut & next()
TDEStdAccel::paste
const TDEShortcut & paste()
TDEStdAccel::prior
const TDEShortcut & prior()
TDEStdAccel::cut
const TDEShortcut & cut()
TDEStdAccel::end
const TDEShortcut & end()
TDEStdAccel::redo
const TDEShortcut & redo()
TDEStdAccel::forwardWord
const TDEShortcut & forwardWord()
TDEStdAccel::insert
const TDEShortcut & insert()
TDEStdAccel::beginningOfLine
const TDEShortcut & beginningOfLine()
TDEStdAccel::deleteWordBack
const TDEShortcut & deleteWordBack()
TDEStdAccel::undo
const TDEShortcut & undo()
TDEStdAccel::home
const TDEShortcut & home()
TDEStdAccel::backwardWord
const TDEShortcut & backwardWord()
TDEStdAccel::pasteSelection
const TDEShortcut & pasteSelection()
TDEStdAccel::deleteWordForward
const TDEShortcut & deleteWordForward()
tdelocale.h

tdeui

Skip menu "tdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.4
This website is maintained by Timothy Pearson.