From 75ee0a2911a4942882ed31ed659b71cd0ebca300 Mon Sep 17 00:00:00 2001 From: Stefano Ceccherini Date: Fri, 18 Jan 2008 12:01:31 +0000 Subject: [PATCH] Respect the maximum bytes set by SetMaxBytes() in Insert() and on keydown. Fixed SetMaxBytes() so that it respect multibyte characters (it removes the whole character in case it doesn't fit). This can be seen in BColorControls, where you can't write numbers with more than 3 characters anymore. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23605 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/interface/TextView.cpp | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/kits/interface/TextView.cpp b/src/kits/interface/TextView.cpp index faa543dfe0..3beb2c3798 100644 --- a/src/kits/interface/TextView.cpp +++ b/src/kits/interface/TextView.cpp @@ -2166,8 +2166,16 @@ BTextView::SetMaxBytes(int32 max) const int32 textLength = fText->Length(); fMaxBytes = max; - if (fMaxBytes < textLength) - Delete(fMaxBytes, textLength); + if (fMaxBytes < textLength) { + int32 offset = fMaxBytes; + // Delete the text after fMaxBytes, but + // respect multibyte characters boundaries. + const int32 previousInitial = _PreviousInitialByte(offset); + if (_NextInitialByte(previousInitial) != offset) + offset = previousInitial; + + Delete(offset, textLength); + } } @@ -3013,7 +3021,7 @@ BTextView::_HandlePageKey(uint32 inPageKey) */ void BTextView::_HandleAlphaKey(const char *bytes, int32 numBytes) -{ +{ // TODO: block input if not editable (Andrew) if (fUndo) { _BTypingUndoBuffer_ *undoBuffer = dynamic_cast<_BTypingUndoBuffer_ *>(fUndo); @@ -3042,18 +3050,16 @@ BTextView::_HandleAlphaKey(const char *bytes, int32 numBytes) offset++; if (start != offset) - InsertText(Text() + start, offset - start, fSelStart, NULL); + _DoInsertText(Text() + start, offset - start, fSelStart, NULL, NULL); - InsertText(bytes, numBytes, fSelStart, NULL); - numBytes += offset - start; + _DoInsertText(bytes, numBytes, fSelStart, NULL, NULL); } else - InsertText(bytes, numBytes, fSelStart, NULL); + _DoInsertText(bytes, numBytes, fSelStart, NULL, NULL); - fClickOffset = fSelEnd = fSelStart = fSelStart + numBytes; + fClickOffset = fSelEnd; - if (Window()) - _Refresh(saveStart, fSelEnd, erase, true); + ScrollToOffset(fClickOffset); } @@ -3473,6 +3479,9 @@ BTextView::_DoInsertText(const char *inText, int32 inLength, int32 inOffset, { _CancelInputMethod(); + if (TextLength() + inLength > MaxBytes()) + return; + // Don't do any check, the public methods will have adjusted // eventual bogus values...