Skip to content

Commit

Permalink
Text framework: Added TextEditor::Replace()
Browse files Browse the repository at this point in the history
Call Replace() on TextDocument, so that there are not two edits later
on once UndoableEdit stuff is supported.
  • Loading branch information
stippi committed Sep 6, 2015
1 parent 41bd20b commit 7d8d778
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/apps/haikudepot/textview/TextEditor.cpp
Expand Up @@ -240,11 +240,7 @@ TextEditor::KeyDown(KeyEvent event)
// Handle null-termintating the string
BString text(event.bytes, event.length);

// Remove selection, if any
if (HasSelection())
Remove(SelectionStart(), SelectionLength());

Insert(fSelection.Caret(), text);
Replace(SelectionStart(), SelectionLength(), text);
}
break;
}
Expand All @@ -260,9 +256,6 @@ TextEditor::Insert(int32 offset, const BString& string)
status_t ret = fDocument->Insert(offset, string, fStyleAtCaret);

if (ret == B_OK) {
// TODO: Via listener, and only affected paragraphs
fLayout->Invalidate();

_SetCaretOffset(offset + string.CountChars(), true, false, true);

fDocument->PrintToStream();
Expand All @@ -281,9 +274,6 @@ TextEditor::Remove(int32 offset, int32 length)
status_t ret = fDocument->Remove(offset, length);

if (ret == B_OK) {
// TODO: Via listener, and only affected paragraphs
fLayout->Invalidate();

_SetCaretOffset(offset, true, false, true);

fDocument->PrintToStream();
Expand All @@ -293,6 +283,24 @@ TextEditor::Remove(int32 offset, int32 length)
}


status_t
TextEditor::Replace(int32 offset, int32 length, const BString& string)
{
if (!fEditingEnabled || fDocument.Get() == NULL)
return B_ERROR;

status_t ret = fDocument->Replace(offset, length, string);

if (ret == B_OK) {
_SetCaretOffset(offset + string.CountChars(), true, false, true);

fDocument->PrintToStream();
}

return ret;
}


// #pragma mark -


Expand Down
2 changes: 2 additions & 0 deletions src/apps/haikudepot/textview/TextEditor.h
Expand Up @@ -61,6 +61,8 @@ class TextEditor : public BReferenceable {

virtual status_t Insert(int32 offset, const BString& string);
virtual status_t Remove(int32 offset, int32 length);
virtual status_t Replace(int32 offset, int32 length,
const BString& string);

void LineUp(bool select);
void LineDown(bool select);
Expand Down

0 comments on commit 7d8d778

Please sign in to comment.