22 #include <tqdragobject.h>
23 #include <tqpopupmenu.h>
24 #include <tqtextstream.h>
27 #include <tdeapplication.h>
30 #include <kcmenumngr.h>
31 #include <tdefontdialog.h>
33 #include <tdemessagebox.h>
34 #include <tdestdaccel.h>
38 #include "keditcl.moc"
40 class KEdit::KEditPrivate
43 bool overwriteEnabled:1;
50 : TQMultiLineEdit(_parent, name)
53 d->overwriteEnabled =
false;
61 line_pos = col_pos = 0;
70 connect(
this, TQ_SIGNAL(cursorPositionChanged(
int,
int)),
71 this, TQ_SLOT(slotCursorPositionChanged()));
81 KEdit::setAutoUpdate(
bool b)
91 getCursorPosition(&line, &col);
102 int oldUndoDepth = undoDepth();
107 TQIODevice *dev=stream->device();
108 if (dev && dev->size()>(1024*1024)) {
112 for (i=0; i<5000; i++) {
113 TQString line=stream->readLine();
114 if (line.isNull())
break;
117 insertAt(textLine, line, col);
123 textLine = stream->read();
124 insertAt( textLine, line, col);
126 setUndoDepth( oldUndoDepth );
128 setCursorPosition(saveline, savecol);
150 d->autoUpdate =
false;
151 if (!hasMarkedText())
153 TQString oldText = markedText();
155 TQStringList lines = TQStringList::split(
'\n', oldText,
true);
156 bool addSpace =
false;
157 bool firstLine =
true;
158 TQChar lastChar = oldText[oldText.length()-1];
159 TQChar firstChar = oldText[0];
160 for(TQStringList::Iterator it = lines.begin();
163 TQString line = (*it).simplifyWhiteSpace();
167 newText += TQString::fromLatin1(
"\n\n");
170 if (firstChar.isSpace())
182 if (firstChar.isSpace())
189 it = lines.remove(it);
193 if (lastChar ==
'\n')
195 else if (lastChar.isSpace())
199 if (oldText == newText)
202 d->autoUpdate =
true;
206 if (wordWrap() == NoWrap)
211 TQMultiLineEdit *we =
new TQMultiLineEdit();
212 we->setWordWrap(FixedColumnWidth);
213 we->setWrapColumnOrWidth(78);
214 we->setText(newText);
215 newText = TQString::null;
216 for(
int i = 0; i < we->numLines(); i++)
218 TQString line = we->textLine(i);
219 if (line.right(1) !=
"\n")
227 d->autoUpdate =
true;
244 int line_count = numLines()-1;
248 if (softWrap || (wordWrap() == NoWrap))
250 for(
int i = 0; i < line_count; i++)
252 (*stream) << textLine(i) <<
'\n';
254 (*stream) << textLine(line_count);
258 for(
int i = 0; i <= line_count; i++)
260 int lines_in_parag = linesOfParagraph(i);
261 if (lines_in_parag == 1)
263 (*stream) << textLine(i);
267 TQString parag_text = textLine(i);
270 int current_line = 0;
272 while(lineOfChar(i, pos) == current_line) pos++;
273 (*stream) << parag_text.mid(first_pos, pos - first_pos - 1) <<
'\n';
276 if (current_line+1 == lines_in_parag)
279 (*stream) << parag_text.mid(pos);
303 void KEdit::slotCursorPositionChanged()
309 void KEdit::computePosition()
311 if (!d->posDirty)
return;
316 getCursorPosition(&line,&col);
320 if (wordWrap() == NoWrap)
326 for(
int i = 0; i < line; i++)
327 line_pos += linesOfParagraph(i);
330 int line_offset = lineOfChar(line, col);
331 line_pos += line_offset;
334 TQString linetext = textLine(line);
335 int start_of_line = 0;
339 while(lineOfChar(line, --start_of_line) == line_offset);
351 int coltemp = col-start_of_line;
355 bool found_one =
false;
361 while(find >=0 && find <= coltemp- 1 ){
362 find = linetext.find(
'\t', find+start_of_line,
true )-start_of_line;
363 if( find >=0 && find <= coltemp - 1 ){
365 pos = pos +
find - mem;
366 pos = pos + 8 - pos % 8;
372 pos = pos + coltemp - mem;
383 void KEdit::keyPressEvent ( TQKeyEvent *e)
386 if ( e->key() == Key_Return && e->state() == ControlButton ) {
392 int keyQt =
key.keyCodeQt();
394 if ( keyQt == CTRL+Key_K ){
401 killbufferstring =
"";
403 lastwasanewline =
false;
408 getCursorPosition(&line,&col);
409 killstring = textLine(line);
410 killstring = killstring.mid(col,killstring.length());
413 if(!killbufferstring.isEmpty() && !killtrue && !lastwasanewline){
414 killbufferstring +=
'\n';
417 if( (killstring.length() == 0) && !killtrue){
418 killbufferstring +=
'\n';
419 lastwasanewline =
true;
422 if(killstring.length() > 0){
424 killbufferstring += killstring;
425 lastwasanewline =
false;
430 lastwasanewline =
false;
431 killtrue = !killtrue;
437 if(killbufferstring.isEmpty() && !killtrue && !lastwasanewline){
445 TQMultiLineEdit::keyPressEvent(e);
449 else if ( keyQt == CTRL+Key_Y ){
454 getCursorPosition(&line,&col);
456 TQString tmpstring = killbufferstring;
460 insertAt(tmpstring,line,col);
471 else if ( isReadOnly() )
472 TQMultiLineEdit::keyPressEvent( e );
474 else if ( !(
key.keyCodeQt() & (CTRL | ALT)) && !e->text().isEmpty() && TQString(e->text()).unicode()->isPrint() )
475 TQMultiLineEdit::keyPressEvent( e );
479 slotCursorPositionChanged();
484 slotCursorPositionChanged();
489 slotCursorPositionChanged();
494 slotCursorPositionChanged();
497 moveCursor(MoveWordBackward,
true);
498 if (hasSelectedText())
501 slotCursorPositionChanged();
504 moveCursor(MoveWordForward,
true);
505 if (hasSelectedText())
508 slotCursorPositionChanged();
511 CursorAction
action = MoveWordBackward;
513 getCursorPosition( ¶, & index );
514 if (text(para).isRightToLeft())
516 moveCursor(action,
false );
517 slotCursorPositionChanged();
520 CursorAction
action = MoveWordForward;
522 getCursorPosition( ¶, & index );
523 if (text(para).isRightToLeft())
524 action = MoveWordBackward;
525 moveCursor( action,
false );
526 slotCursorPositionChanged();
529 moveCursor( MovePgDown,
false );
530 slotCursorPositionChanged();
533 moveCursor( MovePgUp,
false );
534 slotCursorPositionChanged();
537 moveCursor( MoveHome,
false );
538 slotCursorPositionChanged();
541 moveCursor( MoveEnd,
false );
542 slotCursorPositionChanged();
545 moveCursor( MoveLineStart,
false);
546 slotCursorPositionChanged();
549 moveCursor( MoveLineEnd,
false);
550 slotCursorPositionChanged();
552 else if ( key == Key_Insert ) {
553 if (d->overwriteEnabled)
555 this->setOverwriteMode(!this->isOverwriteMode());
560 TQMultiLineEdit::keyPressEvent(e);
569 TQFont font = this->font();
578 gotodialog =
new KEdGotoLine( parent,
"gotodialog" );
585 if( gotodialog->result() != KEdGotoLine::Accepted)
587 int target_line = gotodialog->getLineNumber()-1;
588 if (wordWrap() == NoWrap)
590 setCursorPosition( target_line, 0 );
595 int max_parag = paragraphs();
599 int lines_in_parag = 0;
600 while ((++parag < max_parag) && (line + lines_in_parag < target_line))
602 line += lines_in_parag;
603 lines_in_parag = linesOfParagraph(parag);
607 if (parag >= max_parag)
609 target_line = line + lines_in_parag - 1;
613 while(1+line+lineOfChar(parag,col) < target_line) col++;
614 setCursorPosition( parag, col );
619 void KEdit::dragMoveEvent(TQDragMoveEvent* e) {
621 if(KURLDrag::canDecode(e))
623 else if(TQTextDrag::canDecode(e))
624 TQMultiLineEdit::dragMoveEvent(e);
627 void KEdit::contentsDragMoveEvent(TQDragMoveEvent* e) {
629 if(KURLDrag::canDecode(e))
631 else if(TQTextDrag::canDecode(e))
632 TQMultiLineEdit::contentsDragMoveEvent(e);
635 void KEdit::dragEnterEvent(TQDragEnterEvent* e) {
638 e->accept(KURLDrag::canDecode(e) || TQTextDrag::canDecode(e));
641 void KEdit::contentsDragEnterEvent(TQDragEnterEvent* e) {
643 kdDebug() <<
"KEdit::contentsDragEnterEvent()" <<
endl;
644 e->accept(KURLDrag::canDecode(e) || TQTextDrag::canDecode(e));
648 void KEdit::dropEvent(TQDropEvent* e) {
652 if(KURLDrag::canDecode(e)) {
655 else if(TQTextDrag::canDecode(e))
656 TQMultiLineEdit::dropEvent(e);
659 void KEdit::contentsDropEvent(TQDropEvent* e) {
663 if(KURLDrag::canDecode(e)) {
666 else if(TQTextDrag::canDecode(e))
667 TQMultiLineEdit::contentsDropEvent(e);
672 d->overwriteEnabled = b;
678 TQMultiLineEdit::create(
id, initializeWindow, destroyOldWindow );
687 TQMultiLineEdit::ensureCursorVisible();
690 void KEdit::setCursor(
const TQCursor &c )
695 TQMultiLineEdit::setCursor(c);
698 void KEdit::viewportPaintEvent( TQPaintEvent*pe )
703 TQMultiLineEdit::viewportPaintEvent(pe);
707 void KEdGotoLine::virtual_hook(
int id,
void* data )
708 { KDialogBase::virtual_hook(
id, data ); }
710 void KEdFind::virtual_hook(
int id,
void* data )
711 { KDialogBase::virtual_hook(
id, data ); }
713 void KEdReplace::virtual_hook(
int id,
void* data )
714 { KDialogBase::virtual_hook(
id, data ); }
716 void KEdit::virtual_hook(
int,
void* )
static void setAutoHideCursor(TQWidget *w, bool enable)
Sets auto-hiding the cursor for widget w.
void CursorPositionChanged()
This signal is emitted whenever the cursor position changes.
void installRBPopup(TQPopupMenu *)
Install a context menu for KEdit.
virtual void ensureCursorVisible()
Reimplemented for internal reasons, the API is not affected.
void toggle_overwrite_signal()
This signal is emitted if the user toggles from insert to overwrite mode or vice versa.
virtual void create(WId=0, bool initializeWindow=true, bool destroyOldWindow=true)
Reimplemented for internal reasons, the API is not affected.
void insertText(TQTextStream *)
Insert text from the text stream into the edit widget.
int currentColumn()
Retrieve the actual column number the cursor is on.
void selectFont()
Let the user select a font and set the font of the textwidget to that selected font.
void saveText(TQTextStream *, bool softWrap)
Save text from the edit widget to a text stream.
void gotUrlDrop(TQDropEvent *e)
This signal is emitted if the user dropped a URL over the text editor TQMultiLineEdit widget.
void cleanWhiteSpace()
Clean up redundant whitespace from selected text.
void doGotoLine()
Present a "Goto Line" dialog to the user.
KEdit(TQWidget *_parent=NULL, const char *name=NULL)
The usual constructor.
int currentLine()
Retrieve the current line number.
void setOverwriteEnabled(bool b)
Allow the user to toggle between insert mode and overwrite mode with the "Insert" key.
static int getFont(TQFont &theFont, bool onlyFixed=false, TQWidget *parent=0L, bool makeFrame=true, TQButton::ToggleState *sizeIsRelativeState=0L)
Creates a modal font dialog, lets the user choose a font, and returns when the dialog is closed.
kndbgstream & endl(kndbgstream &s)
kdbgstream kdDebug(int area=0)
const TDEShortcut & copy()
const TDEShortcut & endOfLine()
const TDEShortcut & next()
const TDEShortcut & paste()
const TDEShortcut & prior()
const TDEShortcut & cut()
const TDEShortcut & end()
const TDEShortcut & redo()
const TDEShortcut & forwardWord()
const TDEShortcut & beginningOfLine()
const TDEShortcut & deleteWordBack()
const TDEShortcut & find()
const TDEShortcut & undo()
const TDEShortcut & home()
TQString action(StdAccel id)
const TDEShortcut & backwardWord()
const TDEShortcut & deleteWordForward()