diff --git a/src/kits/interface/TextView.cpp b/src/kits/interface/TextView.cpp index 8f7b91699c..53f34056a2 100644 --- a/src/kits/interface/TextView.cpp +++ b/src/kits/interface/TextView.cpp @@ -3661,11 +3661,9 @@ BTextView::DrawLines(int32 startLine, int32 endLine, int32 startOffset, bool era view->PopState(); } - const char *string = fText->GetString(offset, tabChars); - // TODO: GetString() could return less or more bytes than requested - // Improve the backend api so it's more robust and pass back the returned - // bytes - view->DrawString(string, tabChars); + int32 returnedBytes = 0; + const char *string = fText->GetString(offset, tabChars, &returnedBytes); + view->DrawString(string, returnedBytes); if (foundTab) { float penPos = PenLocation().x - fTextRect.left; diff --git a/src/kits/interface/textview_support/TextGapBuffer.cpp b/src/kits/interface/textview_support/TextGapBuffer.cpp index 80ff05906c..582340403f 100644 --- a/src/kits/interface/textview_support/TextGapBuffer.cpp +++ b/src/kits/interface/textview_support/TextGapBuffer.cpp @@ -169,15 +169,15 @@ _BTextGapBuffer_::SizeGapTo(long inCount) const char * -_BTextGapBuffer_::GetString(int32 fromOffset, int32 numBytes) +_BTextGapBuffer_::GetString(int32 fromOffset, int32 numBytes, int32 *returnedBytes) { - // numBytes won't necessarily be honored. This function could return more - // bytes than specified (for example when in password mode) - // TODO: Fix this, it's not very nice. char *result = ""; - if (numBytes < 1) + if (numBytes < 1) { + if (returnedBytes != NULL) + *returnedBytes = 0; return result; + } bool isStartBeforeGap = (fromOffset < fGapIndex); bool isEndBeforeGap = ((fromOffset + numBytes - 1) < fGapIndex); @@ -198,7 +198,10 @@ _BTextGapBuffer_::GetString(int32 fromOffset, int32 numBytes) result = fScratchBuffer; } - + + if (returnedBytes != NULL) + *returnedBytes = numBytes; + // TODO: this could be improved. We are overwriting what we did some lines ago, // we could just avoid to do that. if (fPasswordMode) { @@ -216,7 +219,9 @@ _BTextGapBuffer_::GetString(int32 fromOffset, int32 numBytes) memcpy(scratchPtr, B_UTF8_BULLET, charLen); scratchPtr += charLen; } - scratchPtr = '\0'; + scratchPtr = '\0'; + if (returnedBytes != NULL) + *returnedBytes = newSize - 1; } return result; diff --git a/src/kits/interface/textview_support/TextGapBuffer.h b/src/kits/interface/textview_support/TextGapBuffer.h index 9d8dc21cbf..0ddce9b989 100644 --- a/src/kits/interface/textview_support/TextGapBuffer.h +++ b/src/kits/interface/textview_support/TextGapBuffer.h @@ -13,7 +13,6 @@ class BFile; -// _BTextGapBuffer_ class ------------------------------------------------------ class _BTextGapBuffer_ { public: @@ -27,32 +26,31 @@ virtual ~_BTextGapBuffer_(); void MoveGapTo(int32 toIndex); void SizeGapTo(int32 inCount); - const char *GetString(int32 fromOffset, int32 numChars); bool FindChar(char inChar, int32 fromIndex, int32 *ioDelta); - const char *Text(); + const char *Text(); int32 Length() const; char operator[](int32 index) const; -// char *RealText(); - void GetString(int32 offset, int32 length, char *buffer); -// void GetString(int32, int32 *); +// char *RealText(); + const char *GetString(int32 fromOffset, int32 numBytes, int32 *returnedBytes); + void GetString(int32 offset, int32 length, char *buffer); - char RealCharAt(int32 offset) const; + char RealCharAt(int32 offset) const; - bool PasswordMode() const; - void SetPasswordMode(bool); + bool PasswordMode() const; + void SetPasswordMode(bool); -// void Resize(int32 size); +// void Resize(int32 size); protected: int32 fExtraCount; // when realloc()-ing - int32 fItemCount; // logical count - char *fBuffer; // allocated memory + int32 fItemCount; // logical count + char *fBuffer; // allocated memory int32 fBufferCount; // physical count - int32 fGapIndex; // gap position - int32 fGapCount; // gap count + int32 fGapIndex; // gap position + int32 fGapCount; // gap count char *fScratchBuffer; // for GetString int32 fScratchSize; // scratch size bool fPasswordMode;