Small cleanups and simplifications. Removed duplicated code

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15209 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2005-11-29 10:34:06 +00:00
parent a76b13ce62
commit a4368fc50d
3 changed files with 48 additions and 80 deletions
+3 -6
View File
@@ -27,15 +27,12 @@
#include <SupportDefs.h> #include <SupportDefs.h>
#include "TextViewSupportBuffer.h" #include "TextViewSupportBuffer.h"
// It's important that this struct remains as is,
// as it's the way it is in BeOS, and if we change it,
// it would lead to some issues as long as we use "mixed" libraries.
typedef struct STELine { typedef struct STELine {
long offset; // offset of first character of line long offset; // offset of first character of line
float origin; // pixel position of top of line float origin; // pixel position of top of line
float ascent; // maximum ascent for line float ascent; // maximum ascent for line
float width; // not used for now, but could be float width; // not used for now, but could be
} STELine, *STELinePtr; } STELine;
// _BLineBuffer_ class --------------------------------------------------------- // _BLineBuffer_ class ---------------------------------------------------------
@@ -56,7 +53,7 @@ virtual ~_BLineBuffer_();
void BumpOffset(int32 delta, int32 index); void BumpOffset(int32 delta, int32 index);
long NumLines() const; long NumLines() const;
const STELinePtr operator[](int32 index) const; STELine * operator[](int32 index) const;
}; };
@@ -67,7 +64,7 @@ _BLineBuffer_::NumLines() const
} }
inline const STELinePtr inline STELine *
_BLineBuffer_::operator[](int32 index) const _BLineBuffer_::operator[](int32 index) const
{ {
return &fBuffer[index]; return &fBuffer[index];
+14 -22
View File
@@ -143,11 +143,8 @@ _BStyleRecordBuffer_::MatchRecord(const BFont *inFont,
const rgb_color *inColor, int32 *outIndex) const rgb_color *inColor, int32 *outIndex)
{ {
for (int32 i = 0; i < fItemCount; i++) { for (int32 i = 0; i < fItemCount; i++) {
if (*inFont == fBuffer[i].style.font if (*inFont == fBuffer[i].style.font
&& inColor->red == fBuffer[i].style.color.red && *inColor == fBuffer[i].style.color) {
&& inColor->green == fBuffer[i].style.color.green
&& inColor->blue == fBuffer[i].style.color.blue
&& inColor->alpha == fBuffer[i].style.color.alpha) {
*outIndex = i; *outIndex = i;
return true; return true;
} }
@@ -185,7 +182,7 @@ _BStyleBuffer_::IsValidNullStyle() const
void void
_BStyleBuffer_::SyncNullStyle(int32 offset) _BStyleBuffer_::SyncNullStyle(int32 offset)
{ {
if ((fValidNullStyle) || (fStyleRunDesc.ItemCount() < 1)) if (fValidNullStyle || fStyleRunDesc.ItemCount() < 1)
return; return;
int32 index = OffsetToRun(offset); int32 index = OffsetToRun(offset);
@@ -199,7 +196,7 @@ void
_BStyleBuffer_::SetNullStyle(uint32 inMode, const BFont *inFont, _BStyleBuffer_::SetNullStyle(uint32 inMode, const BFont *inFont,
const rgb_color *inColor, int32 offset) const rgb_color *inColor, int32 offset)
{ {
if ((fValidNullStyle) || (fStyleRunDesc.ItemCount() < 1)) if (fValidNullStyle || fStyleRunDesc.ItemCount() < 1)
SetStyle(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color); SetStyle(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color);
else { else {
int32 index = OffsetToRun(offset - 1); int32 index = OffsetToRun(offset - 1);
@@ -329,16 +326,15 @@ _BStyleBuffer_::GetStyle(int32 inOffset, BFont *outFont, rgb_color *outColor) co
STEStyleRange* STEStyleRange*
_BStyleBuffer_::GetStyleRange(int32 startOffset, int32 endOffset) const _BStyleBuffer_::GetStyleRange(int32 startOffset, int32 endOffset) const
{ {
STEStyleRange* result = NULL;
int32 startIndex = OffsetToRun(startOffset); int32 startIndex = OffsetToRun(startOffset);
int32 endIndex = OffsetToRun(endOffset); int32 endIndex = OffsetToRun(endOffset);
int32 numStyles = endIndex - startIndex + 1; int32 numStyles = endIndex - startIndex + 1;
numStyles = (numStyles < 1) ? 1 : numStyles; if (numStyles < 1)
numStyles = 1;
result = (STEStyleRange*)malloc(sizeof(int32) STEStyleRange* result = (STEStyleRange *)malloc(sizeof(int32)
+ sizeof(STEStyleRun) * numStyles); + sizeof(STEStyleRun) * numStyles);
if (!result) if (!result)
return NULL; return NULL;
@@ -348,7 +344,8 @@ _BStyleBuffer_::GetStyleRange(int32 startOffset, int32 endOffset) const
for (int32 index = 0; index < numStyles; index++) { for (int32 index = 0; index < numStyles; index++) {
*run = (*this)[startIndex + index]; *run = (*this)[startIndex + index];
run->offset -= startOffset; run->offset -= startOffset;
run->offset = (run->offset < 0) ? 0 : run->offset; if (run->offset < 0)
run->offset = 0;
run++; run++;
} }
@@ -394,9 +391,8 @@ _BStyleBuffer_::RemoveStyleRange(int32 fromOffset, int32 toOffset)
void void
_BStyleBuffer_::RemoveStyles(int32 index, int32 count) _BStyleBuffer_::RemoveStyles(int32 index, int32 count)
{ {
for (int32 i = index; i < (index + count); i++) { for (int32 i = index; i < (index + count); i++)
fStyleRecord.RemoveRecord(fStyleRunDesc[i]->index); fStyleRecord.RemoveRecord(fStyleRunDesc[i]->index);
}
fStyleRunDesc.RemoveDescs(index, count); fStyleRunDesc.RemoveDescs(index, count);
} }
@@ -449,9 +445,8 @@ _BStyleBuffer_::BumpOffset(int32 delta, int32 index)
void void
_BStyleBuffer_::SetStyle(uint32 mode, const BFont *fromFont, _BStyleBuffer_::SetStyle(uint32 mode, const BFont *fromFont, BFont *toFont,
BFont *toFont, const rgb_color *fromColor, const rgb_color *fromColor, rgb_color *toColor)
rgb_color *toColor)
{ {
if (mode & B_FONT_FAMILY_AND_STYLE) if (mode & B_FONT_FAMILY_AND_STYLE)
toFont->SetFamilyAndStyle(fromFont->FamilyAndStyle()); toFont->SetFamilyAndStyle(fromFont->FamilyAndStyle());
@@ -545,10 +540,7 @@ _BStyleBuffer_::ContinuousGetStyle(BFont *outFont, uint32 *ioMode,
mode &= ~B_FONT_SHEAR; mode &= ~B_FONT_SHEAR;
} }
if (theStyle.color.red != style->color.red if (theStyle.color != style->color)
|| theStyle.color.green != style->color.green
|| theStyle.color.blue != style->color.blue
|| theStyle.color.alpha != style->color.alpha)
oneColor = false; oneColor = false;
// TODO: Finish this: handle B_FONT_FACE, B_FONT_FLAGS, etc. // TODO: Finish this: handle B_FONT_FACE, B_FONT_FLAGS, etc.
+31 -52
View File
@@ -615,7 +615,6 @@ void
BTextView::MouseMoved(BPoint where, uint32 code, const BMessage *message) BTextView::MouseMoved(BPoint where, uint32 code, const BMessage *message)
{ {
// Check if it's a "click'n'move // Check if it's a "click'n'move
// TODO: Currently always returns false
if (PerformMouseMoved(where, code)) if (PerformMouseMoved(where, code))
return; return;
@@ -1542,22 +1541,7 @@ void
BTextView::SetFontAndColor(const BFont *inFont, uint32 inMode, BTextView::SetFontAndColor(const BFont *inFont, uint32 inMode,
const rgb_color *inColor) const rgb_color *inColor)
{ {
CALLED(); SetFontAndColor(fSelStart, fSelEnd, inFont, inMode, inColor);
CancelInputMethod();
BFont newFont = *inFont;
NormalizeFont(&newFont);
// add the style to the style buffer
fStyles->SetStyleRange(fSelStart, fSelEnd, fText->Length(),
inMode, &newFont, inColor);
if (inMode & B_FONT_FAMILY_AND_STYLE || inMode & B_FONT_SIZE)
// recalc the line breaks and redraw with new style
Refresh(fSelStart, fSelEnd, fSelStart != fSelEnd, false);
else
// the line breaks wont change, simply redraw
DrawLines(LineAt(fSelStart), LineAt(fSelEnd), fSelStart, true);
} }
@@ -1586,8 +1570,7 @@ BTextView::SetFontAndColor(int32 startOffset, int32 endOffset,
void void
BTextView::GetFontAndColor(int32 inOffset, BFont *outFont, BTextView::GetFontAndColor(int32 inOffset, BFont *outFont, rgb_color *outColor) const
rgb_color *outColor) const
{ {
CALLED(); CALLED();
fStyles->GetStyle(inOffset, outFont, outColor); fStyles->GetStyle(inOffset, outFont, outColor);
@@ -1595,36 +1578,42 @@ BTextView::GetFontAndColor(int32 inOffset, BFont *outFont,
void void
BTextView::GetFontAndColor(BFont *outFont, uint32 *outMode, BTextView::GetFontAndColor(BFont *outFont, uint32 *outMode, rgb_color *outColor, bool *outEqColor) const
rgb_color *outColor, bool *outEqColor) const
{ {
CALLED(); CALLED();
fStyles->ContinuousGetStyle(outFont, outMode, outColor, outEqColor, fStyles->ContinuousGetStyle(outFont, outMode, outColor, outEqColor, fSelStart, fSelEnd);
fSelStart, fSelEnd);
} }
void void
BTextView::SetRunArray(int32 startOffset, int32 endOffset, BTextView::SetRunArray(int32 startOffset, int32 endOffset, const text_run_array *inRuns)
const text_run_array *inRuns)
{ {
CALLED(); CALLED();
if (startOffset > endOffset)
return;
CancelInputMethod(); CancelInputMethod();
int32 textLength = fText->Length();
// pin offsets at reasonable values // pin offsets at reasonable values
startOffset = (startOffset < 0) ? 0 : startOffset; if (startOffset < 0)
endOffset = (endOffset < 0) ? 0 : endOffset; startOffset = 0;
endOffset = (endOffset > fText->Length()) ? fText->Length() : endOffset; else if (startOffset > textLength)
startOffset = textLength;
if (endOffset < 0)
endOffset = 0;
else if (endOffset > textLength)
endOffset = textLength;
long numStyles = inRuns->count; long numStyles = inRuns->count;
if (numStyles > 0) { if (numStyles > 0) {
int32 textLength = fText->Length();
const text_run *theRun = &inRuns->runs[0]; const text_run *theRun = &inRuns->runs[0];
for (long index = 0; index < numStyles; index++) { for (long index = 0; index < numStyles; index++) {
long fromOffset = theRun->offset + startOffset; long fromOffset = theRun->offset + startOffset;
long toOffset = endOffset; long toOffset = endOffset;
if ((index + 1) < numStyles) { if (index + 1 < numStyles) {
toOffset = (theRun + 1)->offset + startOffset; toOffset = (theRun + 1)->offset + startOffset;
toOffset = (toOffset > endOffset) ? endOffset : toOffset; toOffset = (toOffset > endOffset) ? endOffset : toOffset;
} }
@@ -2027,10 +2016,9 @@ BTextView::LineWidth(int32 lineNum) const
{ {
if (lineNum < 0 || lineNum >= fLines->NumLines()) if (lineNum < 0 || lineNum >= fLines->NumLines())
return 0; return 0;
else {
STELine* line = (*fLines)[lineNum]; STELine* line = (*fLines)[lineNum];
return StyledWidth(line->offset, (line + 1)->offset - line->offset); return StyledWidth(line->offset, (line + 1)->offset - line->offset);
}
} }
@@ -2062,9 +2050,7 @@ BTextView::TextHeight(int32 startLine, int32 endLine) const
if (endLine == numLines - 1 && (*fText)[fText->Length() - 1] == '\n') if (endLine == numLines - 1 && (*fText)[fText->Length() - 1] == '\n')
height += (*fLines)[endLine + 1]->origin - (*fLines)[endLine]->origin; height += (*fLines)[endLine + 1]->origin - (*fLines)[endLine]->origin;
height = ceil(height); return ceil(height);
return height;
} }
@@ -2779,8 +2765,7 @@ BTextView::UndoState(bool *isRedo) const
void void
BTextView::GetDragParameters(BMessage *drag, BBitmap **bitmap, BTextView::GetDragParameters(BMessage *drag, BBitmap **bitmap, BPoint *point, BHandler **handler)
BPoint *point, BHandler **handler)
{ {
CALLED(); CALLED();
if (drag == NULL) if (drag == NULL)
@@ -3542,9 +3527,7 @@ BTextView::FindLineBreak(int32 fromOffset, float *outAscent,
} }
} }
offset = min_c(offset, limit); return min_c(offset, limit);
return offset;
} }
@@ -3616,16 +3599,14 @@ BTextView::DoInsertText(const char *inText, int32 inLength, int32 inOffset,
void void
BTextView::DoDeleteText(int32 fromOffset, int32 toOffset, BTextView::DoDeleteText(int32 fromOffset, int32 toOffset, _BTextChangeResult_ *outResult)
_BTextChangeResult_ *outResult)
{ {
CALLED(); CALLED();
} }
void void
BTextView::DrawLines(int32 startLine, int32 endLine, int32 startOffset, BTextView::DrawLines(int32 startLine, int32 endLine, int32 startOffset, bool erase)
bool erase)
{ {
// clip the text // clip the text
BRect clipRect = Bounds() & fTextRect; BRect clipRect = Bounds() & fTextRect;
@@ -3955,10 +3936,8 @@ void
BTextView::TrackDrag(BPoint where) BTextView::TrackDrag(BPoint where)
{ {
CALLED(); CALLED();
if (Bounds().Contains(where)) { if (Bounds().Contains(where))
int32 offset = OffsetAt(where); DragCaret(OffsetAt(where));
DragCaret(offset);
}
} }
@@ -4460,7 +4439,7 @@ BTextView::HandleInputMethodLocationRequest()
return; return;
int32 offset = fInline->Offset(); int32 offset = fInline->Offset();
int32 limit = offset + fInline->Length(); const int32 limit = offset + fInline->Length();
BMessage message(B_INPUT_METHOD_EVENT); BMessage message(B_INPUT_METHOD_EVENT);
message.AddInt32("be:opcode", B_INPUT_METHOD_LOCATION_REQUEST); message.AddInt32("be:opcode", B_INPUT_METHOD_LOCATION_REQUEST);