20#include "KoTextView.h"
21#include "KoTextParag.h"
22#include "KoParagCounter.h"
23#include "KoTextObject.h"
24#include "KoTextViewIface.h"
25#include "KoStyleCollection.h"
26#include "KoBgSpellCheck.h"
27#include "KoVariable.h"
30#include <kstandarddirs.h>
31#include <tdestdaccel.h>
33#include <tdeinstance.h>
36#include <tdemessagebox.h>
38#include <kbookmarkmanager.h>
42#include <tqapplication.h>
44#include <tqclipboard.h>
46class KoTextView::KoTextViewPrivate
51 m_currentUnicodeNumber = 0;
55 void appendDigit(
int digit ) { m_currentUnicodeNumber = 10 * m_currentUnicodeNumber + digit; }
56 int currentUnicodeNumber()
const {
return m_currentUnicodeNumber; }
57 void clearCurrentUnicodeNumber() { m_currentUnicodeNumber = 0; }
59 KoBgSpellCheck* m_backSpeller;
62 int m_currentUnicodeNumber;
67 d =
new KoTextViewPrivate;
71 connect( m_textobj, TQ_SIGNAL( hideCursor() ),
this, TQ_SLOT( hideCursor() ) );
72 connect( m_textobj, TQ_SIGNAL( showCursor() ),
this, TQ_SLOT( showCursor() ) );
73 connect( m_textobj, TQ_SIGNAL( setCursor( KoTextCursor * ) ),
this, TQ_SLOT( setCursor( KoTextCursor * ) ) );
74 connect( m_textobj, TQ_SIGNAL(
updateUI(
bool,
bool) ),
this, TQ_SLOT(
updateUI(
bool,
bool) ) );
75 connect( m_textobj, TQ_SIGNAL( showCurrentFormat() ),
this, TQ_SLOT( showCurrentFormat() ) );
76 connect( m_textobj, TQ_SIGNAL( ensureCursorVisible() ),
this, TQ_SLOT( ensureCursorVisible() ) );
78 m_cursor =
new KoTextCursor( m_textobj->
textDocument() );
80 m_cursorVisible =
false;
83 blinkTimer =
new TQTimer(
this );
84 connect( blinkTimer, TQ_SIGNAL( timeout() ),
85 this, TQ_SLOT( blinkCursor() ) );
86 if ( TQApplication::cursorFlashTime() > 0 )
87 blinkTimer->start( TQApplication::cursorFlashTime() / 2 );
89 dragStartTimer =
new TQTimer(
this );
90 connect( dragStartTimer, TQ_SIGNAL( timeout() ),
93 m_textobj->formatMore( 2 );
95 blinkCursorVisible =
false;
96 inDoubleClick =
false;
97 mightStartDrag =
false;
98 possibleTripleClick =
false;
99 afterTripleClick =
false;
101 m_variablePosition =-1;
102 m_overwriteMode =
false;
106KoTextView::~KoTextView()
112 delete dragStartTimer;
115KoTextViewIface* KoTextView::dcopObject()
118 dcop =
new KoTextViewIface(
this );
126 if ( removeselection && textDocument()->removeSelection( KoTextDocument::Standard ) )
127 textObject()->selectionChangedNotify();
131void KoTextView::deleteWordRight()
133 if ( textObject()->hasSelection() ) {
137 textDocument()->setSelectionStart( KoTextDocument::Standard, m_cursor );
140 m_cursor->gotoRight();
141 }
while ( !m_cursor->atParagEnd()
142 && !m_cursor->parag()->at( m_cursor->index() )->c.isSpace() );
143 textDocument()->setSelectionEnd( KoTextDocument::Standard, m_cursor );
144 textObject()->
removeSelectedText( m_cursor, KoTextDocument::Standard, i18n(
"Remove Word") );
147void KoTextView::deleteWordLeft()
149 if ( textObject()->hasSelection() ) {
153 textDocument()->setSelectionStart( KoTextDocument::Standard, m_cursor );
156 m_cursor->gotoLeft();
157 }
while ( !m_cursor->atParagStart()
158 && !m_cursor->parag()->at( m_cursor->index()-1 )->c.isSpace() );
159 textDocument()->setSelectionEnd( KoTextDocument::Standard, m_cursor );
160 textObject()->
removeSelectedText( m_cursor, KoTextDocument::Standard, i18n(
"Remove Word") );
164void KoTextView::handleKeyPressEvent( TQKeyEvent * e, TQWidget *widget,
const TQPoint &pos)
166 textObject()->typingStarted();
177 bool clearUndoRedoInfo =
true;
178 if ( TDEShortcut( KKey( e ) ) == TDEStdAccel::deleteWordBack() )
180 if ( m_cursor->parag()->string()->isRightToLeft() )
184 clearUndoRedoInfo =
true;
185 }
else if ( TDEShortcut( KKey( e ) ) == TDEStdAccel::deleteWordForward() )
187 if ( m_cursor->parag()->string()->isRightToLeft() )
191 clearUndoRedoInfo =
true;
194 switch ( e->key() ) {
197 if (!doToolTipCompletion(m_cursor, m_cursor->parag(), m_cursor->index() - 1, e->key()) )
202 if ( m_cursor->parag()->string()->isRightToLeft() == (e->key() == Key_Right) )
203 a = e->state() & ControlButton ? MoveWordBackward : MoveBackward;
205 a = e->state() & ControlButton ? MoveWordForward : MoveForward;
206 moveCursor( a, e->state() & ShiftButton );
211 moveCursor( e->state() & ControlButton ? MoveParagUp : MoveUp, e->state() & ShiftButton );
214 moveCursor( e->state() & ControlButton ? MoveParagDown : MoveDown, e->state() & ShiftButton );
217 moveCursor( e->state() & ControlButton ? MoveHome : MoveLineStart, e->state() & ShiftButton );
220 if (!doToolTipCompletion(m_cursor, m_cursor->parag(), m_cursor->index() - 1, e->key()) )
221 moveCursor( e->state() & ControlButton ? MoveEnd : MoveLineEnd, e->state() & ShiftButton );
224 moveCursor( e->state() & ControlButton ? MovePgUp : MoveViewportUp, e->state() & ShiftButton );
227 moveCursor( e->state() & ControlButton ? MovePgDown : MoveViewportDown, e->state() & ShiftButton );
229 case Key_Return:
case Key_Enter:
231 if (!doToolTipCompletion(m_cursor, m_cursor->parag(), m_cursor->index() - 1, e->key()) )
232 if ( (e->state() & (ShiftButton|ControlButton)) == 0 )
234 if ( textObject()->hasSelection() )
236 clearUndoRedoInfo =
false;
237 textObject()->
doKeyboardAction( m_cursor, m_currentFormat, KoTextObject::ActionReturn );
238 Q_ASSERT( m_cursor->parag()->prev() );
239 if ( m_cursor->parag()->prev() )
241 m_cursor->parag()->prev()->length() - 1,
'\n' );
243 clearUndoRedoInfo =
true;
246 if ( textObject()->hasSelection() ) {
251 textObject()->
doKeyboardAction( m_cursor, m_currentFormat, KoTextObject::ActionDelete );
253 clearUndoRedoInfo =
false;
256 if (e->state() & ShiftButton && m_cursor->parag() && m_cursor->atParagStart() && m_cursor->parag()->counter() && textDecreaseIndent())
260 if ( textObject()->hasSelection() ) {
264 textObject()->
doKeyboardAction( m_cursor, m_currentFormat, KoTextObject::ActionBackspace );
266 clearUndoRedoInfo =
false;
277 case Key_Direction_L: {
278 if ( m_cursor->parag() && m_cursor->parag()->direction() != TQChar::DirL )
280 KCommand* cmd = textObject()->setParagDirectionCommand( m_cursor, TQChar::DirL );
281 textObject()->emitNewCommand( cmd );
285 case Key_Direction_R: {
286 if ( m_cursor->parag() && m_cursor->parag()->direction() != TQChar::DirR )
288 KCommand* cmd = textObject()->setParagDirectionCommand( m_cursor, TQChar::DirR );
289 textObject()->emitNewCommand( cmd );
295 if (e->key() == TQt::Key_Tab)
297 if (doToolTipCompletion(m_cursor, m_cursor->parag(), m_cursor->index() - 1, e->key()) )
299 if ( m_cursor->parag() && m_cursor->atParagStart() && m_cursor->parag()->counter() )
301 textIncreaseIndent();
306 if ( e->key() == TQt::Key_Space )
308 if (doToolTipCompletion(m_cursor, m_cursor->parag(), m_cursor->index() - 1, e->key()) )
311 if ( e->text().length() &&
312 ( !e->ascii() || e->ascii() >= 32 ) ||
313 ( e->text() ==
"\t" && !( e->state() & ControlButton ) ) ) {
314 clearUndoRedoInfo =
false;
315 TQString text = e->text();
317 if ( d->m_backSpeller ) {
318 d->m_backSpeller->setIntraWordEditing( m_cursor->parag(), m_cursor->index() );
322 if ( ( e->state() & AltButton ) && text[0].isDigit() )
324 while ( text[0].isDigit() ) {
325 d->appendDigit( text[0].digitValue() );
329 if ( !text.isEmpty() )
332 KoTextParag *p = m_cursor->parag();
333 if ( p && p->string() && p->string()->isRightToLeft() ) {
334 TQChar *c = (TQChar *)text.unicode();
335 int l = text.length();
338 *c = c->mirroredChar();
343 if( !doIgnoreDoubleSpace( p, m_cursor->index()-1, text[ text.length() - 1 ] ) )
350 doAutoFormat( m_cursor, m_cursor->parag(), m_cursor->index() - 1, text[ text.length() - 1 ] );
352 showToolTipBox(m_cursor->parag(), m_cursor->index()-1, widget,pos);
355 removeToolTipCompletion();
363 if ( e->state() & ControlButton ) {
370 moveCursor( MoveLineStart, e->state() & ShiftButton );
373 moveCursor( MoveLineEnd, e->state() & ShiftButton );
376 textObject()->
doKeyboardAction( m_cursor, m_currentFormat, KoTextObject::ActionKill );
382 insertNonbreakingSpace();
385 clearUndoRedoInfo =
false;
391 clearUndoRedoInfo =
false;
398 if ( clearUndoRedoInfo ) {
400 if ( d->m_backSpeller )
401 d->m_backSpeller->setIntraWordEditing( 0, 0 );
404 textObject()->typingDone();
407void KoTextView::setOverwriteMode(
bool overwriteMode )
409 m_overwriteMode = overwriteMode;
414 int insertFlags = KoTextObject::DefaultInsertFlags;
415 if ( m_overwriteMode )
417 textObject()->
insert( m_cursor, m_currentFormat, text, i18n(
"Insert Text"), KoTextDocument::Standard, insertFlags );
420void KoTextView::newParagraph()
422 textObject()->
insert( m_cursor, m_currentFormat,
"\n", i18n(
"Insert Text"), KoTextDocument::Standard, KoTextObject::CheckNewLine );
425void KoTextView::handleKeyReleaseEvent( TQKeyEvent * e )
427 if ( e->key() == Key_Alt && d->currentUnicodeNumber() >= 32 )
429 TQString text = TQChar( d->currentUnicodeNumber() );
430 d->clearCurrentUnicodeNumber();
433 m_cursor->index() - 1, text[ text.length() - 1 ] );
437void KoTextView::handleImStartEvent( TQIMEvent * )
442void KoTextView::handleImComposeEvent( TQIMEvent * e )
445 if ( textDocument()->hasSelection( KoTextDocument::Standard ) )
446 textDocument()->removeSelection( KoTextDocument::Standard );
447 if ( textDocument()->hasSelection( KoTextDocument::InputMethodPreedit ) )
448 textDocument()->removeSelectedText( KoTextDocument::InputMethodPreedit, m_cursor );
451 int preeditStartIdx = m_cursor->index();
452 textDocument()->setSelectionStart( KoTextDocument::InputMethodPreedit, m_cursor );
453 textObject()->
insert( m_cursor, m_currentFormat, e->text(), i18n(
"Insert Text"),
454 KoTextDocument::Standard,
456 textDocument()->setSelectionEnd( KoTextDocument::InputMethodPreedit, m_cursor );
459 int preeditSelStart = preeditStartIdx + e->cursorPos();
460 int preeditSelEnd = preeditSelStart + e->selectionLength();
461 m_cursor->setIndex( preeditSelStart );
462 textDocument()->setSelectionStart( KoTextDocument::Standard, m_cursor );
463 m_cursor->setIndex( preeditSelEnd );
464 textDocument()->setSelectionEnd( KoTextDocument::Standard, m_cursor );
467 m_cursor->setIndex( preeditSelStart );
469 textObject()->emitUpdateUI(
true );
470 textObject()->emitShowCursor();
471 textObject()->selectionChangedNotify();
474void KoTextView::handleImEndEvent( TQIMEvent * e )
477 if ( textDocument()->hasSelection( KoTextDocument::Standard ) )
478 textDocument()->removeSelection( KoTextDocument::Standard );
479 if ( textDocument()->hasSelection( KoTextDocument::InputMethodPreedit ) )
480 textDocument()->removeSelectedText( KoTextDocument::InputMethodPreedit, m_cursor );
484 textObject()->emitUpdateUI(
true );
485 textObject()->emitShowCursor();
486 textObject()->selectionChangedNotify();
489void KoTextView::completion()
491 (void) doCompletion(m_cursor, m_cursor->parag(),
492 m_cursor->index() - 1);
495void KoTextView::moveCursor( CursorAction action,
bool select )
498 bool cursorMoved =
false;
500 if ( !textDocument()->hasSelection( KoTextDocument::Standard ) )
501 textDocument()->setSelectionStart( KoTextDocument::Standard, m_cursor );
502 cursorMoved = moveCursor( action );
503 if ( textDocument()->setSelectionEnd( KoTextDocument::Standard, m_cursor ) ) {
504 textObject()->selectionChangedNotify();
507 bool redraw = textDocument()->removeSelection( KoTextDocument::Standard );
508 cursorMoved = moveCursor( action );
510 textObject()->selectionChangedNotify();
516 ensureCursorVisible();
522bool KoTextView::moveCursor( CursorAction action )
524 bool cursorMoved =
true;
527 m_cursor->gotoPreviousLetter();
529 case MoveWordBackward:
530 m_cursor->gotoPreviousWord();
533 m_cursor->gotoNextLetter();
535 case MoveWordForward:
536 m_cursor->gotoNextWord();
542 m_cursor->gotoDown();
547 case MoveViewportDown:
557 m_cursor->gotoLineStart();
560 m_cursor->gotoHome();
563 m_cursor->gotoLineEnd();
570 KoTextParag * parag = m_cursor->parag()->prev();
571 if ( m_cursor->index()==0 && parag )
573 m_cursor->setParag( parag );
574 m_cursor->setIndex( 0 );
576 else m_cursor->setIndex( 0 );
578 case MoveParagDown: {
579 KoTextParag * parag = m_cursor->parag()->next();
582 m_cursor->setParag( parag );
583 m_cursor->setIndex( 0 );
592KoTextCursor KoTextView::selectWordUnderCursor(
const KoTextCursor& cursor,
int selectionId )
594 KoTextCursor c1 = cursor;
595 KoTextCursor c2 = cursor;
596 if ( cursor.index() > 0 && !cursor.parag()->at( cursor.index()-1 )->c.isSpace() )
598 if ( !cursor.parag()->at( cursor.index() )->c.isSpace() && !cursor.atParagEnd() )
604 KoTextString *s = cursor.parag()->string();
605 bool beginFound =
false;
606 for (
int i = c1.index(); i< c2.index(); i++)
608 const TQChar ch = s->at(i).c;
611 const bool isWordDelimiter = ch.isSpace()
612 || ch.category() == TQChar::Punctuation_Open
613 || ch.category() == TQChar::Punctuation_Close
614 || ch.category() == TQChar::Punctuation_Other
617 if( !beginFound && !isWordDelimiter )
622 else if ( beginFound && isWordDelimiter )
629 textDocument()->setSelectionStart( selectionId, &c1 );
630 textDocument()->setSelectionEnd( selectionId, &c2 );
634KoTextCursor KoTextView::selectParagUnderCursor(
const KoTextCursor& cursor,
int selectionId,
bool copyAndNotify )
636 KoTextCursor c1 = cursor;
637 KoTextCursor c2 = cursor;
639 c2.setIndex(c1.parag()->string()->length() - 1);
640 textDocument()->setSelectionStart( selectionId, &c1 );
641 textDocument()->setSelectionEnd( selectionId, &c2 );
644 textObject()->selectionChangedNotify();
646 TQApplication::clipboard()->setSelectionMode(
true );
648 TQApplication::clipboard()->setSelectionMode(
false );
653void KoTextView::extendParagraphSelection(
const TQPoint& iPoint )
656 KoTextCursor oldCursor = *m_cursor;
657 placeCursor( iPoint );
660 if ( textDocument()->hasSelection( KoTextDocument::Standard ) )
662 redraw = textDocument()->setSelectionEnd( KoTextDocument::Standard, m_cursor );
663 if ( textDocument()->isSelectionSwapped( KoTextDocument::Standard ) )
664 m_cursor->setIndex( 0 );
666 m_cursor->setIndex( m_cursor->parag()->string()->length() - 1 );
667 textDocument()->setSelectionEnd( KoTextDocument::Standard, m_cursor );
673 textObject()->selectionChangedNotify(
false );
678TQString KoTextView::wordUnderCursor(
const KoTextCursor& cursor )
680 selectWordUnderCursor( cursor, KoTextDocument::Temp );
681 TQString text = textObject()->
selectedText( KoTextDocument::Temp );
683 textDocument()->removeSelection( KoTextDocument::Temp );
684 if( !hasCustomItems )
689bool KoTextView::handleMousePressEvent( TQMouseEvent *e,
const TQPoint &iPoint,
bool canStartDrag,
bool insertDirectCursor )
691 bool addParag =
false;
692 mightStartDrag =
false;
695 if (possibleTripleClick)
697 handleMouseTripleClickEvent( e, iPoint );
701 KoTextCursor oldCursor = *m_cursor;
702 addParag = placeCursor( iPoint, insertDirectCursor&&
isReadWrite() );
703 ensureCursorVisible();
705 if ( e->button() != TQt::LeftButton )
711 KoLinkVariable* lv = linkVariable();
717 KoTextDocument * textdoc = textDocument();
718 if ( canStartDrag && textdoc->inSelection( KoTextDocument::Standard, iPoint ) ) {
719 mightStartDrag =
true;
720 m_textobj->emitShowCursor();
721 dragStartTimer->start( TQApplication::startDragTime(),
true );
722 dragStartPos = e->pos();
727 if ( textdoc->hasSelection( KoTextDocument::Standard ) ) {
728 if ( !( e->state() & ShiftButton ) ) {
729 redraw = textdoc->removeSelection( KoTextDocument::Standard );
730 textdoc->setSelectionStart( KoTextDocument::Standard, m_cursor );
732 redraw = textdoc->setSelectionEnd( KoTextDocument::Standard, m_cursor ) || redraw;
735 if ( !( e->state() & ShiftButton ) ) {
736 textdoc->setSelectionStart( KoTextDocument::Standard, m_cursor );
738 textdoc->setSelectionStart( KoTextDocument::Standard, &oldCursor );
739 redraw = textdoc->setSelectionEnd( KoTextDocument::Standard, m_cursor ) || redraw;
747 textObject()->selectionChangedNotify();
752void KoTextView::handleMouseMoveEvent( TQMouseEvent*,
const TQPoint& iPoint )
755 KoTextCursor oldCursor = *m_cursor;
756 placeCursor( iPoint );
759 if ( inDoubleClick ) {
760 KoTextCursor cl = *m_cursor;
762 KoTextCursor cr = *m_cursor;
765 int diff = TQABS( oldCursor.parag()->at( oldCursor.index() )->x - iPoint.x() );
766 int ldiff = TQABS( cl.parag()->at( cl.index() )->x - iPoint.x() );
767 int rdiff = TQABS( cr.parag()->at( cr.index() )->x - iPoint.x() );
769 if ( m_cursor->parag()->lineStartOfChar( m_cursor->index() ) !=
770 oldCursor.parag()->lineStartOfChar( oldCursor.index() ) )
773 if ( rdiff < diff && rdiff < ldiff )
775 else if ( ldiff < diff && ldiff < rdiff )
778 *m_cursor = oldCursor;
782 if ( textDocument()->hasSelection( KoTextDocument::Standard ) )
783 redraw = textDocument()->setSelectionEnd( KoTextDocument::Standard, m_cursor ) || redraw;
785 textDocument()->setSelectionStart( KoTextDocument::Standard, m_cursor );
788 textObject()->selectionChangedNotify(
false );
793void KoTextView::handleMouseReleaseEvent()
795 if ( dragStartTimer->isActive() )
796 dragStartTimer->stop();
797 if ( mightStartDrag ) {
798 textObject()->selectAll(
false );
799 mightStartDrag =
false;
803 if ( textDocument()->selectionStartCursor( KoTextDocument::Standard ) == textDocument()->selectionEndCursor( KoTextDocument::Standard ) )
805 textDocument()->removeSelection( KoTextDocument::Standard );
808 textObject()->selectionChangedNotify();
811 TQApplication::clipboard()->setSelectionMode(
true );
813 TQApplication::clipboard()->setSelectionMode(
false );
816 inDoubleClick =
false;
817 m_textobj->emitShowCursor();
820void KoTextView::handleMouseDoubleClickEvent( TQMouseEvent*ev,
const TQPoint& i )
826 handleMousePressEvent( ev, i );
830 inDoubleClick =
true;
831 *m_cursor = selectWordUnderCursor( *m_cursor );
832 textObject()->selectionChangedNotify();
834 TQApplication::clipboard()->setSelectionMode(
true );
836 TQApplication::clipboard()->setSelectionMode(
false );
838 possibleTripleClick=
true;
840 TQTimer::singleShot(TQApplication::doubleClickInterval(),
this,TQ_SLOT(tripleClickTimeout()));
843void KoTextView::tripleClickTimeout()
845 possibleTripleClick=
false;
848void KoTextView::handleMouseTripleClickEvent( TQMouseEvent*ev,
const TQPoint& )
850 if ( ev->button() != TQt::LeftButton)
855 afterTripleClick=
true;
856 inDoubleClick =
false;
857 *m_cursor = selectParagUnderCursor( *m_cursor );
858 TQTimer::singleShot(TQApplication::doubleClickInterval(),
this,TQ_SLOT(afterTripleClickTimeout()));
861void KoTextView::afterTripleClickTimeout()
863 afterTripleClick=
false;
866bool KoTextView::maybeStartDrag( TQMouseEvent* e )
868 if ( mightStartDrag ) {
869 dragStartTimer->stop();
870 if ( ( e->pos() - dragStartPos ).manhattanLength() > TQApplication::startDragDistance() )
877bool KoTextView::insertParagraph(
const TQPoint &pos)
879 KoTextParag *last = textDocument()->lastParag();
883 int diff = (pos.y()- textDocument()->height());
884 f = last->at( last->length()-1 )->format();
885 int height =f->height();
886 int nbParag = (diff / height);
888 for (
int i = 0; i < nbParag ;i++)
890 KoTextParag *s=textDocument()->createParag( textDocument(), last );
891 s->setFormat( 0, 1, f,
true );
893 s->setStyle( style );
894 s->setCounter( counter );
897 bool createParag = (nbParag > 0 );
900 if ( pos.x() + f->
width(
' ') >= textDocument()->width())
904 last->setAlignment( TQt::AlignRight );
908 int nbSpace = pos.x()/f->
width(
' ');
910 for (
int i = 0; i< nbSpace; i++)
914 last->insert( 0, tmp );
921bool KoTextView::placeCursor(
const TQPoint &pos,
bool insertDirectCursor )
923 bool addParag =
false;
924 if ( insertDirectCursor && (pos.y()>textDocument()->height()) )
925 addParag = insertParagraph(pos);
928 s = textDocument()->lastParag();
930 s = textDocument()->firstParag();
931 m_cursor->place( pos, s,
false, &m_variablePosition );
932 if ( m_variablePosition != -1 )
933 kdDebug() << k_funcinfo <<
" m_variablePosition set to " << m_variablePosition << endl;
938void KoTextView::blinkCursor()
942 if ( !m_cursorVisible )
944 bool cv = m_cursorVisible;
945 blinkCursorVisible = !blinkCursorVisible;
947 m_cursorVisible = cv;
952 m_cursorVisible = visible;
956void KoTextView::focusInEvent()
958 if ( TQApplication::cursorFlashTime() > 0 )
959 blinkTimer->start( TQApplication::cursorFlashTime() / 2 );
963void KoTextView::focusOutEvent()
976 return textObject()->
setFormatCommand( m_cursor, &m_currentFormat, newFormat, flags, zoomFont );
979void KoTextView::dragStarted()
981 mightStartDrag =
false;
982 inDoubleClick =
false;
985void KoTextView::applyStyle(
const KoParagStyle * style )
1000 int i = cursor()->index();
1005 kdDebug(32500) <<
"KoTextView::updateUI old currentFormat=" <<
currentFormat()
1007 <<
" parag format=" << cursor()->parag()->at( i )->format()->key() << endl;
1009 kdDebug(32500) <<
"KoTextView::updateUI old currentFormat=0" << endl;
1016 kdDebug(32500) <<
"Setting currentFormat from format " << cursor()->parag()->at( i )->format()
1017 <<
" ( character " << i <<
" in paragraph " << cursor()->parag()->paragId() <<
" )" << endl;
1019 setCurrentFormat( textDocument()->formatCollection()->format( cursor()->parag()->at( i )->format() ) );
1022 fNoMisspelled.setMisspelled(
false );
1024 setCurrentFormat( textDocument()->formatCollection()->format( &fNoMisspelled ) );
1026 showCurrentFormat();
1031void KoTextView::showCurrentFormat()
1041 return textObject()->setCounterCommand( m_cursor, counter );
1043KCommand * KoTextView::setAlignCommand(
int align )
1045 return textObject()->setAlignCommand( m_cursor, align );
1049 return textObject()->setLineSpacingCommand( m_cursor, spacing, _type);
1053 return textObject()->setBordersCommand( m_cursor, leftBorder, rightBorder, bottomBorder, topBorder );
1055KCommand * KoTextView::setJoinBordersCommand(
bool join )
1057 return textObject()->setJoinBordersCommand( m_cursor, join );
1059KCommand * KoTextView::setMarginCommand( TQStyleSheetItem::Margin m,
double margin )
1061 return textObject()->setMarginCommand( m_cursor, m, margin );
1063KCommand * KoTextView::setTabListCommand(
const KoTabulatorList & tabList )
1065 return textObject()->setTabListCommand( m_cursor, tabList );
1067KCommand * KoTextView::setBackgroundColorCommand(
const TQColor & color )
1069 return textObject()->setBackgroundColorCommand( m_cursor, color );
1072KoTextDocument * KoTextView::textDocument()
const
1079 if ( m_variablePosition < 0 )
1085KoLinkVariable * KoTextView::linkVariable()
1087 return dynamic_cast<KoLinkVariable *
>(variable());
1092 m_singleWord =
false;
1093 m_wordUnderCursor = TQString();
1095 if ( textObject()->hasSelection() )
1098 if ( text.find(
' ') == -1 && text.find(
'\t') == -1 && text.find(KoTextObject::customItemChar()) == -1 )
1100 m_singleWord =
true;
1104 m_singleWord =
false;
1106 if( text.find(KoTextObject::customItemChar())!=-1)
1112 if ( !word.isEmpty() )
1114 m_singleWord =
true;
1115 m_wordUnderCursor = word;
1120 if ( text.isEmpty() || textObject()->protectContent())
1121 return TQPtrList<TDEAction>();
1124 TQValueList<KDataToolInfo> tools;
1125 tools +=KDataToolInfo::query(
"TQString",
"text/plain", instance );
1131 tools += KDataToolInfo::query(
"TQString",
"application/x-singleword", instance );
1134 tools += KDataToolInfo::query(
"KoTextString",
"application/x-qrichtext", instance );
1136 return KDataToolAction::dataToolActionList( tools,
this, TQ_SLOT( slotToolActivated(
const KDataToolInfo &,
const TQString & ) ) );
1139TQString KoTextView::currentWordOrSelection()
const
1141 if ( textObject()->hasSelection() )
1144 return m_wordUnderCursor;
1147void KoTextView::slotToolActivated(
const KDataToolInfo & info,
const TQString & command )
1149 KDataTool* tool = info.createTool( );
1152 kdWarning() <<
"Could not create Tool !" << endl;
1156 kdDebug(32500) <<
"KWTextFrameSetEdit::slotToolActivated command=" << command
1157 <<
" dataType=" << info.dataType() << endl;
1160 if ( textObject()->hasSelection() )
1163 text = m_wordUnderCursor;
1166 TQString mimetype =
"application/x-qrichtext";
1167 TQString datatype =
"KoTextString";
1169 if ( !info.mimeTypes().contains( mimetype ) )
1171 mimetype =
"text/plain";
1172 datatype =
"TQString";
1175 if ( !info.mimeTypes().contains( mimetype ) && m_singleWord )
1176 mimetype =
"application/x-singleword";
1178 kdDebug(32500) <<
"Running tool with datatype=" << datatype <<
" mimetype=" << mimetype << endl;
1180 TQString origText = text;
1181 if ( tool->run( command, &text, datatype, mimetype) )
1183 kdDebug(32500) <<
"Tool ran. Text is now " << text << endl;
1184 if ( origText != text )
1186 if ( !textObject()->hasSelection() )
1189 selectWordUnderCursor( *m_cursor );
1192 textObject()->emitNewCommand( textObject()->replaceSelectionCommand(
1193 cursor(), text, i18n(
"Replace Word") ));
1201 kdDebug() << k_funcinfo << variable->url() << endl;
1202 KURL url( variable->url() );
1205 (void)
new KRun( url );
1216void KoTextView::insertSoftHyphen()
1219 i18n(
"Insert Soft Hyphen") );
1222void KoTextView::insertLineBreak()
1225 i18n(
"Insert Line Break") );
1228void KoTextView::insertNonbreakingSpace()
1231 i18n(
"Insert Non-Breaking Space") );
1234void KoTextView::insertNonbreakingHyphen()
1237 i18n(
"Insert Non-Breaking Hyphen") );
1240void KoTextView::insertSpecialChar(TQChar _c,
const TQString& font)
1243 newFormat->setFamily( font );
1244 if ( textObject()->hasSelection() )
1252 macroCmd->
addCommand( textObject()->replaceSelectionCommand(
1253 cursor(), _c, TQString()) );
1254 textObject()->emitNewCommand( macroCmd );
1258 textObject()->
insert( cursor(), newFormat, _c, i18n(
"Insert Special Char"));
1265 KoTextParag * parag = m_cursor->parag();
1266 return &(parag->paragLayout());
1269bool KoTextView::rtl()
const
1271 return m_cursor->parag()->string()->isRightToLeft();
1276 return textObject()->setParagLayoutCommand( m_cursor, *newLayout, KoTextDocument::Standard,
1277 flags, marginIndex,
true );
1281void KoTextView::increaseNumberingLevel(
const KoStyleCollection* styleCollection )
1288 level = counter->
depth() + 1;
1289 if ( m_cursor->parag()->style()->isOutline() )
1291 TQValueVector<KoParagStyle *> outlineStyles = styleCollection->
outlineStyles();
1292 while ( level < 10 && !style ) {
1293 style = outlineStyles[ level ];
1307 c.setDepth( level );
1311 c.setNumbering(KoParagCounter::NUM_LIST);
1312 c.setStyle(KoParagCounter::STYLE_NUM);
1314 KCommand* command = textObject()->setCounterCommand( m_cursor, c );
1315 textObject()->emitNewCommand( command );
1323void KoTextView::decreaseNumberingLevel(
const KoStyleCollection* styleCollection )
1329 level = counter->
depth() - 1;
1331 if ( m_cursor->parag()->style()->isOutline() || !counter )
1335 TQValueVector<KoParagStyle *> outlineStyles = styleCollection->
outlineStyles();
1336 while ( level >= 0 && !style ) {
1337 style = outlineStyles[ level ];
1350 c.setDepth( level );
1354 KCommand* command = textObject()->setCounterCommand( m_cursor, c );
1355 textObject()->emitNewCommand( command );
1363KCommand *KoTextView::setChangeCaseOfTextCommand(KoChangeCaseDia::TypeOfCase _type)
1366 if ( textObject()->hasSelection() )
1369 return textObject()->changeCaseOfText(cursor(), _type);
1376 Q_ASSERT( textDocument()->hasSelection( KoTextDocument::Standard ) );
1378 KoTextCursor startSel = textDocument()->selectionStartCursor( KoTextDocument::Standard );
1379 KoTextCursor endSel = textDocument()->selectionEndCursor( KoTextDocument::Standard );
1380 bool inSelection =
false;
1381 if ( startSel.parag() == endSel.parag() )
1382 inSelection = dropCursor.parag() == startSel.parag()
1383 && dropCursor.index() >= startSel.index()
1384 && dropCursor.index() <= endSel.index();
1388 inSelection = dropCursor.parag() == startSel.parag() && dropCursor.index() >= startSel.index();
1392 KoTextParag *p = startSel.parag()->next();
1393 while ( !inSelection && p && p != endSel.parag() )
1395 inSelection = ( p == dropCursor.parag() );
1400 inSelection = dropCursor.parag() == endSel.parag() && dropCursor.index() <= endSel.index();
1403 if ( inSelection || m_textobj->protectContent() )
1405 textDocument()->removeSelection( KoTextDocument::Standard );
1406 textObject()->selectionChangedNotify();
1408 *cursor() = dropCursor;
1410 ensureCursorVisible();
1413 if ( textObject()->protectContent() )
1415 textDocument()->removeSelection( KoTextDocument::Standard );
1416 textObject()->selectionChangedNotify();
1421 if ( endSel.parag() == dropCursor.parag() )
1424 if ( startSel.parag() != dropCursor.parag() || startSel.index() < dropCursor.index() )
1429 int dropIndex = dropCursor.index();
1430 dropCursor.setParag( startSel.parag() );
1432 dropCursor.setIndex( dropIndex - TQMIN( endSel.index(), dropIndex ) + startSel.index() );
1434 kdDebug(32500) <<
"dropCursor: parag=" << dropCursor.parag()->paragId() <<
" index=" << dropCursor.index() << endl;
1436 KCommand* cmd = textObject()->removeSelectedTextCommand( cursor(), KoTextDocument::Standard );
1439 *cursor() = dropCursor;
1446void KoTextView::copyTextOfComment()
1448 KoNoteVariable *var =
dynamic_cast<KoNoteVariable *
>( variable() );
1452 lst.append( var->note() );
1453 TQApplication::clipboard()->setSelectionMode(
true);
1454 TQApplication::clipboard()->setData(
new KURLDrag(lst, 0, 0) );
1455 TQApplication::clipboard()->setSelectionMode(
false);
1456 TQApplication::clipboard()->setData(
new KURLDrag(lst, 0, 0) );
1460void KoTextView::removeComment()
1462 KoNoteVariable *var =
dynamic_cast<KoNoteVariable *
>( variable() );
1465 m_cursor->setIndex( m_variablePosition );
1466 textDocument()->setSelectionStart( KoTextDocument::Temp, m_cursor );
1467 m_cursor->setIndex( m_variablePosition + 1 );
1468 textDocument()->setSelectionEnd( KoTextDocument::Temp, m_cursor );
1469 textObject()->
removeSelectedText( m_cursor, KoTextDocument::Temp, i18n(
"Remove Comment") );
1473KoParagStyle * KoTextView::createStyleFromSelection(
const TQString & name)
1475 KoTextCursor cursor = *m_cursor;
1476 if ( textDocument()->hasSelection( KoTextDocument::Standard ) )
1477 cursor = textDocument()->selectionStartCursor( KoTextDocument::Standard );
1480 layout.style = style;
1481 style->setFollowingStyle( style );
1482 style->
format() = *(cursor.parag()->at(cursor.index())->format());
1484 style->paragLayout() = layout;
1486 cursor.parag()->setParagLayout( style->paragLayout() );
1490void KoTextView::updateStyleFromSelection(
KoParagStyle* style )
1492 KoTextCursor cursor = *m_cursor;
1493 if ( textDocument()->hasSelection( KoTextDocument::Standard ) )
1494 cursor = textDocument()->selectionStartCursor( KoTextDocument::Standard );
1496 style->paragLayout() = cursor.parag()->paragLayout();
1497 style->paragLayout().style = style;
1498 style->
format() = *(cursor.parag()->at(cursor.index())->format());
1501void KoTextView::addBookmarks(
const TQString &url)
1503 TQString filename = locateLocal(
"data", TQString::fromLatin1(
"konqueror/bookmarks.xml") );
1504 KBookmarkManager *bookManager = KBookmarkManager::managerForFile( filename,
false );
1505 KBookmarkGroup group = bookManager->root();
1506 group.addBookmark( bookManager, url, KURL( url));
1507 bookManager->save();
1511void KoTextView::copyLink()
1513 KoLinkVariable * var=linkVariable();
1517 lst.append( var->url() );
1518 TQApplication::clipboard()->setSelectionMode(
true);
1519 TQApplication::clipboard()->setData(
new KURLDrag(lst, 0, 0) );
1520 TQApplication::clipboard()->setSelectionMode(
false);
1521 TQApplication::clipboard()->setData(
new KURLDrag(lst, 0, 0) );
1525void KoTextView::removeLink()
1527 KoLinkVariable * var=linkVariable();
1530 KoTextCursor c1 = *m_cursor;
1531 KoTextCursor c2 = *m_cursor;
1532 c1.setIndex(var->index());
1533 c2.setIndex(var->index()+1);
1534 textDocument()->setSelectionStart( KoTextDocument::Temp, &c1 );
1535 textDocument()->setSelectionEnd( KoTextDocument::Temp, &c2 );
1536 KCommand *cmd=textObject()->replaceSelectionCommand( &c1, var->value(),
1537 i18n(
"Remove Link"), KoTextDocument::Temp );
1539 textObject()->emitNewCommand( cmd );
1543void KoTextView::setBackSpeller( KoBgSpellCheck* backSpeller )
1545 d->m_backSpeller = backSpeller;
1548#include "KoTextView.moc"
1549class KoBgSpellCheck;
void addCommand(KCommand *command)
static void sorry(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
This class represents a border - for anything, like paragraph, or frame.
const KoTextFormat & format() const
Return a format.
This is the structure associated with a paragraph (KoTextParag), to define the bullet or numbering of...
unsigned int depth() const
The level of the numbering.
int displayLevels() const
Number of levels whose numbers are displayed at the current level.
This class holds the paragraph-specific formatting information It's separated from KoTextParag so tha...
SpacingType
Line spacing values Assuming h is the height of the biggest char in the line, and v is the linespacin...
A paragraph style is a combination of a character style and paragraph-layout attributes,...
TODO rename to KoParagStyleCollection - or should char styles be part of it too?
TQValueVector< KoParagStyle * > outlineStyles() const
KoParagStyle * defaultStyle() const
KoParagStyle * numberedStyleForLevel(int level) const
Each character (KoTextStringChar) points to a KoTextFormat that defines the formatting of that charac...
const TQFontMetrics & refFontMetrics() const
int width(const TQChar &c) const
Return the width of one char (from a string, not necessarily from a paragraph) in LU pixels.
The KoTextObject is the high-level object that contains a KoTextDocument (the list of paragraphs),...
void applyStyle(KoTextCursor *cursor, const KoParagStyle *style, KoTextDocument::SelectionId selectionId=KoTextDocument::Standard, int paragLayoutFlags=KoParagLayout::All, int formatFlags=KoTextFormat::Format, bool createUndoRedo=true, bool interactive=true)
Apply a KoParagStyle to a selection.
void clearUndoRedoInfo()
Terminate our current undo/redo info, to start with a new one.
bool selectionHasCustomItems(KoTextDocument::SelectionId selectionId=KoTextDocument::Standard) const
returns true if the given selection has any custom item in it
void insert(KoTextCursor *cursor, KoTextFormat *currentFormat, const TQString &text, const TQString &commandName, KoTextDocument::SelectionId selectionId=KoTextDocument::Standard, int insertFlags=DefaultInsertFlags, CustomItemsMap customItemsMap=CustomItemsMap())
The main "insert" method, including undo/redo creation/update.
void doKeyboardAction(KoTextCursor *cursor, KoTextFormat *¤tFormat, KeyboardAction action)
Executes keyboard action action.
@ OverwriteMode
< if true, the text to be inserted is checked for '\n' (as a paragraph delimiter)
@ DoNotRepaint
usually we repaint in insert(), this allows to turn it off
void removeSelectedText(KoTextCursor *cursor, KoTextDocument::SelectionId selectionId=KoTextDocument::Standard, const TQString &cmdName=TQString(), bool createUndoRedo=true)
Remove the text currently selected, including undo/redo creation/update.
TQString selectedText(KoTextDocument::SelectionId selectionId=KoTextDocument::Standard) const
returns the selected text [without formatting] if hasSelection()
KoVariable * variableAtPosition(KoTextParag *parag, int index) const
Return the variable at the given position, if any.
KoTextDocument * textDocument() const
Return the text document contained in this KoTextObject.
void ensureFormatted(KoTextParag *parag, bool emitAfterFormatting=true)
Make sure that parag is formatted.
KCommand * setFormatCommand(const KoTextFormat *format, int flags, bool zoomFont=false)
Implementation of setFormatCommand from KoTextFormatInterface - apply change to the whole document.
virtual void ctrlPgUpKeyPressed()
Reimplement this to handle CTRL+PageUp.
virtual KoTextFormat * currentFormat() const
Interface for accessing the current format.
virtual void showFormat(KoTextFormat *format)=0
Show the settings of this format in the GUI.
bool isReadWrite() const
Return true if the view is allowed to modify the text object.
virtual void updateUI(bool updateFormat, bool force=false)
Show the current settings (those for the paragraph and character under the cursor),...
virtual KCommand * setParagLayoutFormatCommand(KoParagLayout *newLayout, int flags, int marginIndex=-1)
Interface for changing the paragraph layout.
KoTextView(KoTextObject *textobj)
Constructor.
virtual void ctrlPgDownKeyPressed()
Reimplement this to handle CTRL+PageDown.
virtual bool openLink(KoLinkVariable *linkVariable)
Called when clicking on a link.
void insertText(const TQString &text)
This is a slot so that it's accessible from DCOP.
TQPtrList< TDEAction > dataToolActionList(TDEInstance *instance, const TQString &word, bool &_singleWord)
Return the list of actions from data-tools.
virtual bool pgDownKeyPressed()=0
Reimplement this to handle PageDown.
virtual bool pgUpKeyPressed()=0
Reimplement this to handle PageUp.
void terminate(bool removeselection=true)
Call this before deleting.
virtual const KoParagLayout * currentParagLayoutFormat() const
Use this format for displaying the properties (Align/counter/...) of the object.
virtual KCommand * setFormatCommand(const KoTextFormat *newFormat, int flags, bool zoomFont=false)
Implement the KoTextFormatInterface.
virtual void doAutoFormat(KoTextCursor *, KoTextParag *, int, TQChar)
Called when a character (ch) has been inserted into parag, at the given index.
KCommand * prepareDropMove(KoTextCursor dropCursor)
A "drop move" will move the selected text to the given cursor position.
virtual void startDrag()=0
Start a drag.
virtual void drawCursor(bool b)
Draws the cursor (or hides it if b is false).
A KoVariable is a custom item, i.e.