update from jack burton
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4245 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Copyright (c) 2001-2002, OpenBeOS
|
||||
// Copyright (c) 2001-2003, OpenBeOS
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
@@ -25,7 +25,6 @@
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Standard Includes -----------------------------------------------------------
|
||||
#include <string.h>
|
||||
|
||||
// System Includes -------------------------------------------------------------
|
||||
#include "LineBuffer.h"
|
||||
@@ -40,90 +39,94 @@
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
_BLineBuffer_::_BLineBuffer_()
|
||||
: fBlockSize(20),
|
||||
fItemCount(2),
|
||||
fPhysicalSize(22),
|
||||
fObjectList(NULL)
|
||||
: _BTextViewSupportBuffer_<STELine>(20, 2)
|
||||
{
|
||||
fObjectList = new STELine[fPhysicalSize];
|
||||
|
||||
memset(fObjectList, 0, fPhysicalSize * sizeof(STELine));
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
_BLineBuffer_::~_BLineBuffer_()
|
||||
{
|
||||
delete[] fObjectList;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BLineBuffer_::InsertLine(STELine *line, int32 index)
|
||||
void
|
||||
_BLineBuffer_::InsertLine(STELine *inLine, int32 index)
|
||||
{
|
||||
if (fItemCount == fPhysicalSize - 1)
|
||||
{
|
||||
STELine *new_list = new STELine[fPhysicalSize + fBlockSize];
|
||||
InsertItemsAt(1, index, inLine);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BLineBuffer_::RemoveLines(int32 index, int32 count)
|
||||
{
|
||||
RemoveItemsAt(count, index);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BLineBuffer_::RemoveLineRange(int32 fromOffset, int32 toOffset)
|
||||
{
|
||||
int32 fromLine = OffsetToLine(fromOffset);
|
||||
int32 toLine = OffsetToLine(toOffset);
|
||||
|
||||
memcpy(new_list, fObjectList, fPhysicalSize * sizeof(STELine));
|
||||
delete fObjectList;
|
||||
fObjectList = new_list;
|
||||
int32 count = toLine - fromLine;
|
||||
if (count > 0)
|
||||
RemoveLines(fromLine + 1, count);
|
||||
|
||||
fPhysicalSize += fBlockSize;
|
||||
BumpOffset(fromOffset - toOffset, fromLine + 1);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
int32
|
||||
_BLineBuffer_::OffsetToLine(int32 offset) const
|
||||
{
|
||||
int32 minIndex = 0;
|
||||
int32 maxIndex = fItemCount - 1;
|
||||
int32 index = 0;
|
||||
|
||||
while (minIndex < maxIndex) {
|
||||
index = (minIndex + maxIndex) >> 1;
|
||||
if (offset >= fBuffer[index].offset) {
|
||||
if (offset < fBuffer[index + 1].offset)
|
||||
break;
|
||||
else
|
||||
minIndex = index + 1;
|
||||
}
|
||||
else
|
||||
maxIndex = index;
|
||||
}
|
||||
|
||||
if (index < fItemCount)
|
||||
memmove(fObjectList + index + 1, fObjectList + index,
|
||||
(fItemCount - index) * sizeof(STELine));
|
||||
memcpy(fObjectList + index, line, sizeof(STELine));
|
||||
|
||||
fItemCount++;
|
||||
|
||||
return index;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BLineBuffer_::RemoveLines(int32 index, int32 count)
|
||||
int32 _BLineBuffer_::PixelToLine(float pixel) const
|
||||
{
|
||||
memmove(fObjectList + index, fObjectList + index + count,
|
||||
(fItemCount - index - count) * sizeof(STELine));
|
||||
|
||||
fItemCount -= count;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BLineBuffer_::RemoveLineRange(int32 from, int32 to)
|
||||
{
|
||||
int32 linefrom = OffsetToLine(from);
|
||||
int32 lineto = OffsetToLine(to);
|
||||
|
||||
if (linefrom < lineto)
|
||||
RemoveLines(linefrom + 1, lineto - linefrom);
|
||||
|
||||
BumpOffset(linefrom + 1, from - to);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
int32 _BLineBuffer_::OffsetToLine(int32 offset) const
|
||||
{
|
||||
if (offset == 0)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i < fItemCount; i++)
|
||||
{
|
||||
if (offset < fObjectList[i].fOffset)
|
||||
return i - 1;
|
||||
int32 minIndex = 0;
|
||||
int32 maxIndex = fItemCount - 1;
|
||||
int32 index = 0;
|
||||
|
||||
while (minIndex < maxIndex) {
|
||||
index = (minIndex + maxIndex) >> 1;
|
||||
if (pixel >= fBuffer[index].origin) {
|
||||
if (pixel < fBuffer[index + 1].origin)
|
||||
break;
|
||||
else
|
||||
minIndex = index + 1;
|
||||
}
|
||||
else
|
||||
maxIndex = index;
|
||||
}
|
||||
|
||||
return fItemCount - 2;
|
||||
|
||||
return index;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
int32 _BLineBuffer_::PixelToLine(float height) const
|
||||
{
|
||||
for (int i = 0; i < fItemCount; i++)
|
||||
{
|
||||
if (height < fObjectList[i].fHeight)
|
||||
return i - 1;
|
||||
}
|
||||
|
||||
return fItemCount - 2;
|
||||
void
|
||||
_BLineBuffer_::BumpOrigin(float delta, long index)
|
||||
{
|
||||
for (long i = index; i < fItemCount; i++)
|
||||
fBuffer[i].origin += delta;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BLineBuffer_::BumpOffset(int32 line, int32 offset)
|
||||
void
|
||||
_BLineBuffer_::BumpOffset(int32 delta, int32 index)
|
||||
{
|
||||
for (int i = line; i < fItemCount; i++)
|
||||
fObjectList[i].fOffset += offset;
|
||||
for (long i = index; i < fItemCount; i++)
|
||||
fBuffer[i].offset += delta;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Copyright (c) 2001-2002, OpenBeOS
|
||||
// Copyright (c) 2001-2003, OpenBeOS
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
@@ -27,45 +27,56 @@
|
||||
// Standard Includes -----------------------------------------------------------
|
||||
|
||||
// System Includes -------------------------------------------------------------
|
||||
#include "SupportDefs.h"
|
||||
#include <SupportDefs.h>
|
||||
#include "TextViewSupportBuffer.h"
|
||||
|
||||
// Project Includes ------------------------------------------------------------
|
||||
|
||||
// Local Includes --------------------------------------------------------------
|
||||
|
||||
// Local Defines ---------------------------------------------------------------
|
||||
struct STELine {
|
||||
int32 fOffset;
|
||||
float fHeight;
|
||||
float fLineHeight;
|
||||
float fWidth;
|
||||
};
|
||||
typedef struct STELine {
|
||||
long offset; // offset of first character of line
|
||||
float origin; // pixel position of top of line
|
||||
float ascent; // maximum ascent for line
|
||||
float width; // width of line
|
||||
} STELine, *STELinePtr;
|
||||
|
||||
// Globals ---------------------------------------------------------------------
|
||||
|
||||
// _BLineBuffer_ class ---------------------------------------------------------
|
||||
class _BLineBuffer_ {
|
||||
class _BLineBuffer_ : public _BTextViewSupportBuffer_<STELine> {
|
||||
|
||||
public:
|
||||
_BLineBuffer_();
|
||||
virtual ~_BLineBuffer_();
|
||||
_BLineBuffer_();
|
||||
virtual ~_BLineBuffer_();
|
||||
|
||||
void InsertLine(STELine *line, int32);
|
||||
void RemoveLines(int32 index, int32 count);
|
||||
void RemoveLineRange(int32 from, int32 to);
|
||||
void InsertLine(STELine *inLine, int32 index);
|
||||
void RemoveLines(int32 index, int32 count = 1);
|
||||
void RemoveLineRange(int32 fromOffset, int32 toOffset);
|
||||
|
||||
int32 OffsetToLine(int32 offset) const;
|
||||
int32 PixelToLine(float height) const;
|
||||
int32 OffsetToLine(int32 offset) const;
|
||||
int32 PixelToLine(float pixel) const;
|
||||
|
||||
void BumpOffset(int32 line, int32 offset);
|
||||
void BumpOrigin(float, int32);
|
||||
void BumpOrigin(float delta, int32 index);
|
||||
void BumpOffset(int32 delta, int32 index);
|
||||
|
||||
int32 fBlockSize;
|
||||
int32 fItemCount;
|
||||
size_t fPhysicalSize;
|
||||
STELine *fObjectList;
|
||||
long NumLines() const;
|
||||
const STELinePtr operator[](int32 index) const;
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
inline int32
|
||||
_BLineBuffer_::NumLines() const
|
||||
{
|
||||
return fItemCount - 1;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
inline const STELinePtr
|
||||
_BLineBuffer_::operator[](int32 index) const
|
||||
{
|
||||
return &fBuffer[index];
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* $Log $
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Copyright (c) 2001-2002, OpenBeOS
|
||||
// Copyright (c) 2001-2003, OpenBeOS
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
@@ -38,115 +38,536 @@
|
||||
// Globals ---------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
_BStyleBuffer_::_BStyleBuffer_(const BFont *font, const rgb_color *color)
|
||||
: fRuns(10, 10),
|
||||
fRecords(10, 10)
|
||||
{
|
||||
rgb_color black = {0, 0, 0, 255};
|
||||
|
||||
SetNullStyle(0, font ? font : be_plain_font, color ? color : &black, 0);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
_BStyleBuffer_::~_BStyleBuffer_()
|
||||
_BStyleRunDescBuffer_::_BStyleRunDescBuffer_()
|
||||
: _BTextViewSupportBuffer_<STEStyleRunDesc>(20)
|
||||
{
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BStyleBuffer_::SetNullStyle(uint32 mode, const BFont *font,
|
||||
const rgb_color *color, int32)
|
||||
void
|
||||
_BStyleRunDescBuffer_::InsertDesc(STEStyleRunDescPtr inDesc, int32 index)
|
||||
{
|
||||
// TODO: use mode.
|
||||
if (font)
|
||||
fNullStyle.fFont = *font;
|
||||
|
||||
if (color)
|
||||
fNullStyle.fColor = *color;
|
||||
InsertItemsAt(1, index, inDesc);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BStyleBuffer_::GetNullStyle(const BFont **font, const rgb_color **color) const
|
||||
void
|
||||
_BStyleRunDescBuffer_::RemoveDescs(int32 index, int32 count)
|
||||
{
|
||||
*font = &fNullStyle.fFont;
|
||||
*color = &fNullStyle.fColor;
|
||||
RemoveItemsAt(count, index);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BStyleBuffer_::SyncNullStyle(int32)
|
||||
int32
|
||||
_BStyleRunDescBuffer_::OffsetToRun(int32 offset) const
|
||||
{
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BStyleBuffer_::InvalidateNullStyle()
|
||||
{
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool _BStyleBuffer_::IsValidNullStyle() const
|
||||
{
|
||||
// TODO: why would a style be invalid?
|
||||
return true;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BStyleBuffer_::SetStyle(uint32 mode, const BFont *font, BFont *,
|
||||
const rgb_color *color, rgb_color *) const
|
||||
{
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BStyleBuffer_::GetStyle(uint32 mode, BFont *font, rgb_color *color) const
|
||||
{
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BStyleBuffer_::SetStyleRange(int32 from, int32 to, int32, uint32 mode,
|
||||
const BFont *, const rgb_color *)
|
||||
{
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
int32 _BStyleBuffer_::GetStyleRange(int32 from, int32 to) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BStyleBuffer_::ContinuousGetStyle(BFont *, uint32 *, rgb_color *, bool *,
|
||||
int32, int32) const
|
||||
{
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BStyleBuffer_::RemoveStyles(int32 from, int32 to)
|
||||
{
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BStyleBuffer_::RemoveStyleRange(int32 from, int32 to)
|
||||
{
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
int32 _BStyleBuffer_::OffsetToRun(int32 offset) const
|
||||
{
|
||||
if (offset == 0)
|
||||
if (fItemCount <= 1)
|
||||
return 0;
|
||||
|
||||
int32 minIndex = 0;
|
||||
int32 maxIndex = fItemCount;
|
||||
int32 index = 0;
|
||||
|
||||
while (minIndex < maxIndex) {
|
||||
index = (minIndex + maxIndex) >> 1;
|
||||
if (offset >= fBuffer[index].offset) {
|
||||
if (index >= (fItemCount - 1)) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
if (offset < fBuffer[index + 1].offset)
|
||||
break;
|
||||
else
|
||||
minIndex = index + 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
maxIndex = index;
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleRunDescBuffer_::BumpOffset(int32 delta, int32 index)
|
||||
{
|
||||
for (int32 i = index; i < fItemCount; i++)
|
||||
fBuffer[i].offset += delta;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
_BStyleRecordBuffer_::_BStyleRecordBuffer_()
|
||||
: _BTextViewSupportBuffer_<STEStyleRecord>()
|
||||
{
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
int32
|
||||
_BStyleRecordBuffer_::InsertRecord(const BFont *inFont,
|
||||
const rgb_color *inColor)
|
||||
{
|
||||
int32 index = 0;
|
||||
|
||||
for (int i = 0; i < fRuns.fCount; i++)
|
||||
{
|
||||
if (offset < fRuns.fItems[i].fOffset)
|
||||
return i - 1;
|
||||
// look for style in buffer
|
||||
if (MatchRecord(inFont, inColor, &index))
|
||||
return (index);
|
||||
|
||||
// style not found, add it
|
||||
font_height fh;
|
||||
inFont->GetHeight(&fh);
|
||||
|
||||
// check if there's any unused space
|
||||
for (index = 0; index < fItemCount; index++) {
|
||||
if (fBuffer[index].refs < 1) {
|
||||
fBuffer[index].refs = 0;
|
||||
fBuffer[index].ascent = fh.ascent;
|
||||
fBuffer[index].descent = fh.descent + fh.leading;
|
||||
fBuffer[index].style.font = *inFont;
|
||||
fBuffer[index].style.color = *inColor;
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
// no unused space, expand the buffer
|
||||
index = fItemCount;
|
||||
STEStyleRecord newRecord;
|
||||
newRecord.refs = 0;
|
||||
newRecord.ascent = fh.ascent;
|
||||
newRecord.descent = fh.descent + fh.leading;
|
||||
newRecord.style.font = *inFont;
|
||||
newRecord.style.color = *inColor;
|
||||
InsertItemsAt(1, index, &newRecord);
|
||||
|
||||
return index;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleRecordBuffer_::CommitRecord(
|
||||
int32 index)
|
||||
{
|
||||
fBuffer[index].refs++;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleRecordBuffer_::RemoveRecord(
|
||||
int32 index)
|
||||
{
|
||||
fBuffer[index].refs--;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
_BStyleRecordBuffer_::MatchRecord(const BFont *inFont,
|
||||
const rgb_color *inColor, int32 *outIndex)
|
||||
{
|
||||
for (int32 i = 0; i < fItemCount; i++) {
|
||||
if ( (inFont->Size() == fBuffer[i].style.font.Size()) &&
|
||||
(inFont->Shear() == fBuffer[i].style.font.Shear()) &&
|
||||
(inFont->Face() == fBuffer[i].style.font.Face()) &&
|
||||
(inColor->red == fBuffer[i].style.color.red) &&
|
||||
(inColor->green == fBuffer[i].style.color.green) &&
|
||||
(inColor->blue == fBuffer[i].style.color.blue) &&
|
||||
(inColor->alpha == fBuffer[i].style.color.alpha) ) {
|
||||
*outIndex = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return fRuns.fCount - 2;
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BStyleBuffer_::BumpOffset(int32 run, int32 offset)
|
||||
_BStyleBuffer_::_BStyleBuffer_(const BFont *inFont, const rgb_color *inColor)
|
||||
{
|
||||
for (int i = run; i < fRuns.fCount; i++)
|
||||
fRuns.fItems[i].fOffset += offset;
|
||||
fValidNullStyle = true;
|
||||
fNullStyle.font = *inFont;
|
||||
fNullStyle.color = *inColor;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/*void _BStyleBuffer_::Iterate(int32, int32, _BInlineInput_ *, const BFont **,
|
||||
const rgb_color **, float *, uint32 *) const
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleBuffer_::InvalidateNullStyle()
|
||||
{
|
||||
fValidNullStyle = false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
_BStyleBuffer_::IsValidNullStyle() const
|
||||
{
|
||||
return fValidNullStyle;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleBuffer_::SyncNullStyle(int32 offset)
|
||||
{
|
||||
if ((fValidNullStyle) || (fStyleRunDesc.ItemCount() < 1))
|
||||
return;
|
||||
|
||||
int32 index = OffsetToRun(offset);
|
||||
fNullStyle = fStyleRecord[fStyleRunDesc[index]->index]->style;
|
||||
|
||||
fValidNullStyle = true;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleBuffer_::SetNullStyle(uint32 inMode, const BFont *inFont,
|
||||
const rgb_color *inColor, int32 offset)
|
||||
{
|
||||
if ((fValidNullStyle) || (fStyleRunDesc.ItemCount() < 1))
|
||||
SetStyle(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color);
|
||||
else {
|
||||
int32 index = OffsetToRun(offset - 1);
|
||||
fNullStyle = fStyleRecord[fStyleRunDesc[index]->index]->style;
|
||||
SetStyle(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color);
|
||||
}
|
||||
|
||||
fValidNullStyle = true;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleBuffer_::GetNullStyle(const BFont **font,
|
||||
const rgb_color **color) const
|
||||
{
|
||||
if (font)
|
||||
*font = &fNullStyle.font;
|
||||
if (color)
|
||||
*color = &fNullStyle.color;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
_BStyleBuffer_::IsContinuousStyle(uint32 *ioMode, STEStylePtr outStyle,
|
||||
int32 fromOffset, int32 toOffset)
|
||||
{
|
||||
if (fStyleRunDesc.ItemCount() < 1) {
|
||||
SetStyle(*ioMode, &fNullStyle.font, &outStyle->font,
|
||||
&fNullStyle.color, &outStyle->color);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool result = true;
|
||||
int32 fromIndex = OffsetToRun(fromOffset);
|
||||
int32 toIndex = OffsetToRun(toOffset - 1);
|
||||
|
||||
if (fromIndex == toIndex) {
|
||||
int32 styleIndex = fStyleRunDesc[fromIndex]->index;
|
||||
STEStylePtr style = &fStyleRecord[styleIndex]->style;
|
||||
|
||||
SetStyle(*ioMode, &style->font, &outStyle->font, &style->color,
|
||||
&outStyle->color);
|
||||
result = true;
|
||||
}
|
||||
else {
|
||||
int32 styleIndex = fStyleRunDesc[toIndex]->index;
|
||||
STEStyle theStyle = fStyleRecord[styleIndex]->style;
|
||||
//STEStylePtr style = NULL;
|
||||
|
||||
/* for (int32 i = fromIndex; i < toIndex; i++) {
|
||||
styleIndex = fStyleRunDesc[i]->index;
|
||||
style = &fStyleRecord[styleIndex]->style;
|
||||
|
||||
if (*ioMode & doFont) {
|
||||
if (strcmp(theStyle.font, style->font) != 0) {
|
||||
*ioMode &= ~doFont;
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (*ioMode & doSize) {
|
||||
if (theStyle.size != style->size) {
|
||||
*ioMode &= ~doSize;
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (*ioMode & doShear) {
|
||||
if (theStyle.shear != style->shear) {
|
||||
*ioMode &= ~doShear;
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (*ioMode & doUnderline) {
|
||||
if (theStyle.underline != style->underline) {
|
||||
*ioMode &= ~doUnderline;
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (*ioMode & doColor) {
|
||||
if ( (theStyle.color.red != style->color.red) ||
|
||||
(theStyle.color.green != style->color.green) ||
|
||||
(theStyle.color.blue != style->color.blue) ||
|
||||
(theStyle.color.alpha != style->color.alpha) ) {
|
||||
*ioMode &= ~doColor;
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (*ioMode & doExtra) {
|
||||
if (theStyle.extra != style->extra) {
|
||||
*ioMode &= ~doExtra;
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
SetStyle(*ioMode, &theStyle.font, &outStyle->font, &theStyle.color,
|
||||
&outStyle->color);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleBuffer_::SetStyleRange(int32 fromOffset, int32 toOffset,
|
||||
int32 textLen, uint32 inMode,
|
||||
const BFont *inFont, const rgb_color *inColor)
|
||||
{
|
||||
if (inFont == NULL)
|
||||
inFont = &fNullStyle.font;
|
||||
|
||||
if (inColor == NULL)
|
||||
inColor = &fNullStyle.color;
|
||||
|
||||
if (fromOffset == toOffset) {
|
||||
SetNullStyle(inMode, inFont, inColor, fromOffset);
|
||||
return;
|
||||
}
|
||||
|
||||
if (fStyleRunDesc.ItemCount() < 1) {
|
||||
STEStyleRunDesc newDesc;
|
||||
newDesc.offset = fromOffset;
|
||||
newDesc.index = fStyleRecord.InsertRecord(inFont, inColor);
|
||||
fStyleRunDesc.InsertDesc(&newDesc, 0);
|
||||
fStyleRecord.CommitRecord(newDesc.index);
|
||||
return;
|
||||
}
|
||||
|
||||
int32 styleIndex = 0;
|
||||
int32 offset = fromOffset;
|
||||
int32 runIndex = OffsetToRun(offset);
|
||||
do {
|
||||
STEStyleRunDesc runDesc = *fStyleRunDesc[runIndex];
|
||||
int32 runEnd = textLen;
|
||||
if (runIndex < (fStyleRunDesc.ItemCount() - 1))
|
||||
runEnd = fStyleRunDesc[runIndex + 1]->offset;
|
||||
|
||||
STEStyle style = fStyleRecord[runDesc.index]->style;
|
||||
SetStyle(inMode, inFont, &style.font, inColor, &style.color);
|
||||
|
||||
styleIndex = fStyleRecord.InsertRecord(inFont, inColor);
|
||||
|
||||
if ( (runDesc.offset == offset) && (runIndex > 0) &&
|
||||
(fStyleRunDesc[runIndex - 1]->index == styleIndex) ) {
|
||||
RemoveStyles(runIndex);
|
||||
runIndex--;
|
||||
}
|
||||
|
||||
if (styleIndex != runDesc.index) {
|
||||
if (offset > runDesc.offset) {
|
||||
STEStyleRunDesc newDesc;
|
||||
newDesc.offset = offset;
|
||||
newDesc.index = styleIndex;
|
||||
fStyleRunDesc.InsertDesc(&newDesc, runIndex + 1);
|
||||
fStyleRecord.CommitRecord(newDesc.index);
|
||||
runIndex++;
|
||||
}
|
||||
else {
|
||||
fStyleRunDesc[runIndex]->index = styleIndex;
|
||||
fStyleRecord.CommitRecord(styleIndex);
|
||||
}
|
||||
|
||||
if (toOffset < runEnd) {
|
||||
STEStyleRunDesc newDesc;
|
||||
newDesc.offset = toOffset;
|
||||
newDesc.index = runDesc.index;
|
||||
fStyleRunDesc.InsertDesc(&newDesc, runIndex + 1);
|
||||
fStyleRecord.CommitRecord(newDesc.index);
|
||||
}
|
||||
}
|
||||
|
||||
runIndex++;
|
||||
offset = runEnd;
|
||||
} while (offset < toOffset);
|
||||
|
||||
if ( (offset == toOffset) && (runIndex < fStyleRunDesc.ItemCount()) &&
|
||||
(fStyleRunDesc[runIndex]->index == styleIndex) )
|
||||
RemoveStyles(runIndex);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleBuffer_::GetStyle(int32 inOffset, BFont *outFont,
|
||||
rgb_color *outColor) const
|
||||
{
|
||||
if (fStyleRunDesc.ItemCount() < 1)
|
||||
{
|
||||
if (outFont)
|
||||
*outFont = fNullStyle.font;
|
||||
if (outColor)
|
||||
*outColor = fNullStyle.color;
|
||||
return;
|
||||
}
|
||||
|
||||
int32 runIndex = OffsetToRun(inOffset);
|
||||
int32 styleIndex = fStyleRunDesc[runIndex]->index;
|
||||
|
||||
if (outFont)
|
||||
*outFont = fStyleRecord[styleIndex]->style.font;
|
||||
if (outColor)
|
||||
*outColor = fStyleRecord[styleIndex]->style.color;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
STEStyleRangePtr
|
||||
_BStyleBuffer_::GetStyleRange(int32 startOffset,
|
||||
int32 endOffset) const
|
||||
{
|
||||
STEStyleRangePtr result = NULL;
|
||||
|
||||
int32 startIndex = OffsetToRun(startOffset);
|
||||
int32 endIndex = OffsetToRun(endOffset);
|
||||
|
||||
int32 numStyles = endIndex - startIndex + 1;
|
||||
numStyles = (numStyles < 1) ? 1 : numStyles;
|
||||
result = (STEStyleRangePtr)malloc(sizeof(int32) +
|
||||
(sizeof(STEStyleRun) * numStyles));
|
||||
|
||||
result->count = numStyles;
|
||||
STEStyleRunPtr run = &result->runs[0];
|
||||
for (int32 index = 0; index < numStyles; index++) {
|
||||
*run = (*this)[startIndex + index];
|
||||
run->offset -= startOffset;
|
||||
run->offset = (run->offset < 0) ? 0 : run->offset;
|
||||
run++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleBuffer_::RemoveStyleRange(int32 fromOffset, int32 toOffset)
|
||||
{
|
||||
int32 fromIndex = fStyleRunDesc.OffsetToRun(fromOffset);
|
||||
int32 toIndex = fStyleRunDesc.OffsetToRun(toOffset) - 1;
|
||||
|
||||
int32 count = toIndex - fromIndex;
|
||||
if (count > 0) {
|
||||
RemoveStyles(fromIndex + 1, count);
|
||||
toIndex = fromIndex;
|
||||
}
|
||||
|
||||
fStyleRunDesc.BumpOffset(fromOffset - toOffset, fromIndex + 1);
|
||||
|
||||
if ((toIndex == fromIndex) && (toIndex < (fStyleRunDesc.ItemCount() - 1))) {
|
||||
STEStyleRunDescPtr runDesc = fStyleRunDesc[toIndex + 1];
|
||||
runDesc->offset = fromOffset;
|
||||
}
|
||||
|
||||
if (fromIndex < (fStyleRunDesc.ItemCount() - 1)) {
|
||||
STEStyleRunDescPtr runDesc = fStyleRunDesc[fromIndex];
|
||||
if (runDesc->offset == (runDesc + 1)->offset) {
|
||||
RemoveStyles(fromIndex);
|
||||
fromIndex--;
|
||||
}
|
||||
}
|
||||
|
||||
if ((fromIndex >= 0) && (fromIndex < (fStyleRunDesc.ItemCount() - 1))) {
|
||||
STEStyleRunDescPtr runDesc = fStyleRunDesc[fromIndex];
|
||||
if (runDesc->index == (runDesc + 1)->index)
|
||||
RemoveStyles(fromIndex + 1);
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleBuffer_::RemoveStyles(int32 index, int32 count)
|
||||
{
|
||||
for (int32 i = index; i < (index + count); i++)
|
||||
fStyleRecord.RemoveRecord(fStyleRunDesc[i]->index);
|
||||
|
||||
fStyleRunDesc.RemoveDescs(index, count);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
int32
|
||||
_BStyleBuffer_::Iterate(int32 fromOffset, int32 length, /*_BInlineInput_ *,*/
|
||||
const BFont **outFont, const rgb_color **outColor,
|
||||
float *outAscent, float *outDescent, uint32 *) const
|
||||
{
|
||||
int32 numRuns = fStyleRunDesc.ItemCount();
|
||||
if ((length < 1) || (numRuns < 1))
|
||||
return (0);
|
||||
|
||||
int32 result = length;
|
||||
int32 runIndex = fStyleRunDesc.OffsetToRun(fromOffset);
|
||||
STEStyleRunDescPtr run = fStyleRunDesc[runIndex];
|
||||
|
||||
if (outFont != NULL)
|
||||
*outFont = &fStyleRecord[run->index]->style.font;
|
||||
if (outColor != NULL)
|
||||
*outColor = &fStyleRecord[run->index]->style.color;
|
||||
if (outAscent != NULL)
|
||||
*outAscent = fStyleRecord[run->index]->ascent;
|
||||
if (outDescent != NULL)
|
||||
*outDescent = fStyleRecord[run->index]->descent;
|
||||
|
||||
if (runIndex < (numRuns - 1)) {
|
||||
int32 nextOffset = (run + 1)->offset - fromOffset;
|
||||
result = (result > nextOffset) ? nextOffset : result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
int32
|
||||
_BStyleBuffer_::OffsetToRun(int32 offset) const
|
||||
{
|
||||
return fStyleRunDesc.OffsetToRun(offset);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleBuffer_::BumpOffset(int32 delta, int32 index)
|
||||
{
|
||||
fStyleRunDesc.BumpOffset(delta, index);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
_BStyleBuffer_::SetStyle(uint32 mode, const BFont *fromFont,
|
||||
BFont *toFont, const rgb_color *fromColor,
|
||||
rgb_color *toColor)
|
||||
{
|
||||
if (mode & doFont)
|
||||
toFont->SetFamilyAndStyle(fromFont->FamilyAndStyle());
|
||||
|
||||
if (mode & doSize) {
|
||||
if (mode & addSize)
|
||||
toFont->SetSize(fromFont->Size());
|
||||
else
|
||||
toFont->SetSize(fromFont->Size());
|
||||
}
|
||||
|
||||
if (mode & doShear)
|
||||
toFont->SetShear(fromFont->Shear());
|
||||
|
||||
if (mode & doUnderline)
|
||||
toFont->SetFace(fromFont->Face());
|
||||
|
||||
if (mode & doColor)
|
||||
*toColor = *fromColor;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
STEStyleRun
|
||||
_BStyleBuffer_::operator[](int32 index) const
|
||||
{
|
||||
STEStyleRun run;
|
||||
|
||||
if (fStyleRunDesc.ItemCount() < 1) {
|
||||
run.offset = 0;
|
||||
run.style = fNullStyle;
|
||||
} else {
|
||||
STEStyleRunDescPtr runDesc = fStyleRunDesc[index];
|
||||
STEStyleRecordPtr record = fStyleRecord[runDesc->index];
|
||||
run.offset = runDesc->offset;
|
||||
run.style = record->style;
|
||||
}
|
||||
|
||||
return run;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/*void _BStyleBuffer_::ContinuousGetStyle(BFont *, uint32 *, rgb_color *, bool *,
|
||||
int32, int32) const
|
||||
{
|
||||
}*/
|
||||
//------------------------------------------------------------------------------
|
||||
STEStyleRecord *_BStyleBuffer_::operator[](int32 index)
|
||||
{
|
||||
if (index < fRecords.fCount)
|
||||
return fRecords.fItems + index;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* $Log $
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Copyright (c) 2001-2002, OpenBeOS
|
||||
// Copyright (c) 2001-2003, OpenBeOS
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
@@ -29,67 +29,171 @@
|
||||
// System Includes -------------------------------------------------------------
|
||||
#include <SupportDefs.h>
|
||||
#include <InterfaceDefs.h>
|
||||
#include "TextViewSupportBuffer.h"
|
||||
#include <Font.h>
|
||||
|
||||
// Project Includes ------------------------------------------------------------
|
||||
|
||||
// Local Includes --------------------------------------------------------------
|
||||
#include "TextViewSupportBuffer.h"
|
||||
|
||||
// Local Defines ---------------------------------------------------------------
|
||||
struct STEStyleRunDesc {
|
||||
int32 fOffset;
|
||||
int32 fStyle;
|
||||
enum {
|
||||
doFont = 0x00000001, // set font
|
||||
doSize = 0x00000002, // set size
|
||||
doShear = 0x00000004, // set shear
|
||||
doUnderline = 0x00000008, // set underline
|
||||
doColor = 0x00000010, // set color
|
||||
doExtra = 0x00000020, // set the extra field
|
||||
doAll = 0x0000003F, // set everything
|
||||
addSize = 0x00010000 // add size value
|
||||
};
|
||||
|
||||
struct STEStyleRecord {
|
||||
BFont fFont;
|
||||
rgb_color fColor;
|
||||
};
|
||||
typedef struct STEStyle {
|
||||
BFont font; // font
|
||||
rgb_color color; // pen color
|
||||
} STEStyle, *STEStylePtr;
|
||||
|
||||
typedef struct STEStyleRun {
|
||||
long offset; // byte offset of first character of run
|
||||
STEStyle style; // style info
|
||||
} STEStyleRun, *STEStyleRunPtr;
|
||||
|
||||
typedef struct STEStyleRange {
|
||||
long count; // number of style runs
|
||||
STEStyleRun runs[1]; // array of count number of runs
|
||||
} STEStyleRange, *STEStyleRangePtr;
|
||||
|
||||
typedef struct STEStyleRecord {
|
||||
long refs; // reference count for this style
|
||||
float ascent; // ascent for this style
|
||||
float descent; // descent for this style
|
||||
STEStyle style; // style info
|
||||
} STEStyleRecord, *STEStyleRecordPtr;
|
||||
|
||||
typedef struct STEStyleRunDesc {
|
||||
long offset; // byte offset of first character of run
|
||||
long index; // index of corresponding style record
|
||||
} STEStyleRunDesc, *STEStyleRunDescPtr;
|
||||
|
||||
// Globals ---------------------------------------------------------------------
|
||||
|
||||
// _BStyleRunDescBuffer_ class -------------------------------------------------
|
||||
class _BStyleRunDescBuffer_ : public _BTextViewSupportBuffer_<STEStyleRunDesc> {
|
||||
|
||||
public:
|
||||
_BStyleRunDescBuffer_();
|
||||
|
||||
void InsertDesc(STEStyleRunDescPtr inDesc, int32 index);
|
||||
void RemoveDescs(int32 index, int32 count = 1);
|
||||
|
||||
long OffsetToRun(int32 offset) const;
|
||||
void BumpOffset(int32 delta, int32 index);
|
||||
|
||||
const STEStyleRunDescPtr operator[](int32 index) const;
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
inline const
|
||||
STEStyleRunDescPtr _BStyleRunDescBuffer_::operator[](int32 index) const
|
||||
{
|
||||
return &fBuffer[index];
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// _BStyleRecordBuffer_ class --------------------------------------------------
|
||||
class _BStyleRecordBuffer_ : public _BTextViewSupportBuffer_<STEStyleRecord> {
|
||||
|
||||
public:
|
||||
_BStyleRecordBuffer_();
|
||||
|
||||
int32 InsertRecord(const BFont *inFont, const rgb_color *inColor);
|
||||
void CommitRecord(int32 index);
|
||||
void RemoveRecord(int32 index);
|
||||
|
||||
bool MatchRecord(const BFont *inFont, const rgb_color *inColor,
|
||||
int32 *outIndex);
|
||||
|
||||
const STEStyleRecordPtr operator[](int32 index) const;
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
inline const
|
||||
STEStyleRecordPtr _BStyleRecordBuffer_::operator[](int32 index) const
|
||||
{
|
||||
return &fBuffer[index];
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// _BStyleBuffer_ class --------------------------------------------------------
|
||||
class _BStyleBuffer_ {
|
||||
|
||||
public:
|
||||
_BStyleBuffer_(const BFont *, const rgb_color *);
|
||||
virtual ~_BStyleBuffer_();
|
||||
_BStyleBuffer_(const BFont *inFont,
|
||||
const rgb_color *inColor);
|
||||
|
||||
void InvalidateNullStyle();
|
||||
bool IsValidNullStyle() const;
|
||||
|
||||
void SyncNullStyle(int32 offset);
|
||||
void SetNullStyle(uint32 inMode, const BFont *inFont,
|
||||
const rgb_color *inColor, int32 offset = 0);
|
||||
void GetNullStyle(const BFont **font,
|
||||
const rgb_color **color) const;
|
||||
|
||||
bool IsContinuousStyle(uint32 *ioMode, STEStylePtr outStyle,
|
||||
int32 fromOffset, int32 toOffset);
|
||||
void SetStyleRange(int32 fromOffset, int32 toOffset,
|
||||
int32 textLen, uint32 inMode,
|
||||
const BFont *inFont, const rgb_color *inColor);
|
||||
void GetStyle(int32 inOffset, BFont *outFont,
|
||||
rgb_color *outColor) const;
|
||||
STEStyleRangePtr GetStyleRange(int32 startOffset, int32 endOffset) const;
|
||||
|
||||
void RemoveStyleRange(int32 fromOffset, int32 toOffset);
|
||||
void RemoveStyles(int32 index, int32 count = 1);
|
||||
|
||||
int32 Iterate(int32 fromOffset, int32 length, /*_BInlineInput_ *,*/
|
||||
const BFont **outFont = NULL,
|
||||
const rgb_color **outColor = NULL,
|
||||
float *outAscent = NULL,
|
||||
float *outDescen = NULL, uint32 * = NULL) const;
|
||||
|
||||
int32 OffsetToRun(int32 offset) const;
|
||||
void BumpOffset(int32 delta, int32 index);
|
||||
|
||||
void SetStyle(uint32 mode, const BFont *fromFont,
|
||||
BFont *toFont, const rgb_color *fromColor,
|
||||
rgb_color *toColor);
|
||||
|
||||
STEStyleRun operator[](int32 index) const;
|
||||
|
||||
int32 NumRuns() const;
|
||||
const _BStyleRunDescBuffer_& RunBuffer() const;
|
||||
const _BStyleRecordBuffer_& RecordBuffer() const;
|
||||
|
||||
void SetNullStyle(uint32, const BFont *, const rgb_color *, int32);
|
||||
void GetNullStyle(const BFont **, const rgb_color **) const;
|
||||
void SyncNullStyle(int32);
|
||||
void InvalidateNullStyle();
|
||||
bool IsValidNullStyle() const;
|
||||
|
||||
void SetStyle(uint32, const BFont *, BFont *, const rgb_color *,
|
||||
rgb_color *) const;
|
||||
void GetStyle(uint32, BFont *, rgb_color *) const;
|
||||
|
||||
void SetStyleRange(int32, int32, int32, uint32 mode, const BFont *,
|
||||
const rgb_color *);
|
||||
int32 GetStyleRange(int32, int32) const;
|
||||
|
||||
void ContinuousGetStyle(BFont *, uint32 *, rgb_color *, bool *,
|
||||
int32, int32) const;
|
||||
|
||||
void RemoveStyles(int32 from, int32 to);
|
||||
void RemoveStyleRange(int32 from, int32 to);
|
||||
|
||||
int32 OffsetToRun(int32 offset) const;
|
||||
|
||||
void BumpOffset(int32 run, int32 offset);
|
||||
// void Iterate(int32, int32, _BInlineInput_ *, const BFont **,
|
||||
// const rgb_color **, float *, uint32 *) const;
|
||||
|
||||
STEStyleRecord *operator[](int32);
|
||||
|
||||
private:
|
||||
_BTextViewSupportBuffer_<STEStyleRunDesc> fRuns;
|
||||
_BTextViewSupportBuffer_<STEStyleRecord> fRecords;
|
||||
STEStyleRecord fNullStyle;
|
||||
protected:
|
||||
_BStyleRunDescBuffer_ fStyleRunDesc;
|
||||
_BStyleRecordBuffer_ fStyleRecord;
|
||||
bool fValidNullStyle;
|
||||
STEStyle fNullStyle;
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
inline int32
|
||||
_BStyleBuffer_::NumRuns() const
|
||||
{
|
||||
return fStyleRunDesc.ItemCount();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
inline const
|
||||
_BStyleRunDescBuffer_ &_BStyleBuffer_::RunBuffer() const
|
||||
{
|
||||
return fStyleRunDesc;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
inline const
|
||||
_BStyleRecordBuffer_ &_BStyleBuffer_::RecordBuffer() const
|
||||
{
|
||||
return fStyleRecord;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* $Log $
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Copyright (c) 2001-2002, OpenBeOS
|
||||
// Copyright (c) 2001-2003, OpenBeOS
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
@@ -25,14 +25,15 @@
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Standard Includes -----------------------------------------------------------
|
||||
#include <string.h>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
// System Includes -------------------------------------------------------------
|
||||
#include "TextGapBuffer.h"
|
||||
|
||||
// Project Includes ------------------------------------------------------------
|
||||
|
||||
// Local Includes --------------------------------------------------------------
|
||||
#include "TextGapBuffer.h"
|
||||
|
||||
// Local Defines ---------------------------------------------------------------
|
||||
|
||||
@@ -40,94 +41,179 @@
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
_BTextGapBuffer_::_BTextGapBuffer_()
|
||||
: fText(NULL),
|
||||
fLogicalBytes(0),
|
||||
fPhysicalBytes(2048)
|
||||
: fExtraCount(2048),
|
||||
fItemCount(0),
|
||||
fBuffer(NULL),
|
||||
fBufferCount(fExtraCount + fItemCount),
|
||||
fGapIndex(fItemCount),
|
||||
fGapCount(fBufferCount - fGapIndex),
|
||||
fScratchBuffer(NULL),
|
||||
fScratchSize(0)
|
||||
{
|
||||
fText = new char[fPhysicalBytes];
|
||||
*fText = '\0';
|
||||
fBuffer = (char *)malloc(fExtraCount + fItemCount);
|
||||
fScratchBuffer = (char*)malloc(0);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
_BTextGapBuffer_::~_BTextGapBuffer_()
|
||||
{
|
||||
delete[] fText;
|
||||
free(fBuffer);
|
||||
free(fScratchBuffer);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BTextGapBuffer_::InsertText(const char *text, int32 length, int32 offset)
|
||||
void
|
||||
_BTextGapBuffer_::InsertText(const char *inText, int32 inNumItems,
|
||||
int32 inAtIndex)
|
||||
{
|
||||
// If needed, resize buffer
|
||||
if (fPhysicalBytes < fLogicalBytes + length + 1)
|
||||
Resize(fLogicalBytes + length + 1);
|
||||
|
||||
// Move text after insertion point
|
||||
memcpy(fText + offset + length, fText + offset,
|
||||
fLogicalBytes + 1 - offset);
|
||||
|
||||
// Copy new text
|
||||
memcpy(fText + offset, text, length);
|
||||
|
||||
// Update used bytes
|
||||
fLogicalBytes += length;
|
||||
if (inNumItems < 1)
|
||||
return;
|
||||
|
||||
inAtIndex = (inAtIndex > fItemCount) ? fItemCount : inAtIndex;
|
||||
inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex;
|
||||
|
||||
if (inAtIndex != fGapIndex)
|
||||
MoveGapTo(inAtIndex);
|
||||
|
||||
if (fGapCount < inNumItems)
|
||||
SizeGapTo(inNumItems + fExtraCount);
|
||||
|
||||
memcpy(fBuffer + fGapIndex, inText, inNumItems);
|
||||
|
||||
fGapCount -= inNumItems;
|
||||
fGapIndex += inNumItems;
|
||||
fItemCount += inNumItems;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BTextGapBuffer_::RemoveRange(int32 from, int32 to)
|
||||
void
|
||||
_BTextGapBuffer_::RemoveRange(int32 start, int32 end)
|
||||
{
|
||||
// Move text after deletion point
|
||||
memcpy(fText + from, fText + to, fLogicalBytes + 1 - to);
|
||||
|
||||
// Update used bytes
|
||||
fLogicalBytes -= to - from;
|
||||
long inAtIndex = start;
|
||||
long inNumItems = end - start;
|
||||
|
||||
if (inNumItems < 1)
|
||||
return;
|
||||
|
||||
inAtIndex = (inAtIndex > fItemCount - 1) ? (fItemCount - 1) : inAtIndex;
|
||||
inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex;
|
||||
|
||||
MoveGapTo(inAtIndex);
|
||||
|
||||
fGapCount += inNumItems;
|
||||
fItemCount -= inNumItems;
|
||||
|
||||
if (fGapCount > fExtraCount)
|
||||
SizeGapTo(fExtraCount);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
char *_BTextGapBuffer_::Text()
|
||||
void
|
||||
_BTextGapBuffer_::MoveGapTo(int32 toIndex)
|
||||
{
|
||||
if (fLogicalBytes == 0 || fText[fLogicalBytes - 1] != '\0')
|
||||
{
|
||||
if (fPhysicalBytes < fLogicalBytes + 1)
|
||||
{
|
||||
char *new_text = new char[fLogicalBytes + 1];
|
||||
|
||||
if (fText)
|
||||
{
|
||||
memcpy(new_text, fText, fLogicalBytes );
|
||||
delete fText;
|
||||
}
|
||||
|
||||
fText = new_text;
|
||||
}
|
||||
|
||||
fText[fLogicalBytes] = '\0';
|
||||
if (toIndex == fGapIndex)
|
||||
return;
|
||||
|
||||
long gapEndIndex = fGapIndex + fGapCount;
|
||||
long srcIndex = 0;
|
||||
long dstIndex = 0;
|
||||
long count = 0;
|
||||
if (toIndex > fGapIndex) {
|
||||
long trailGapCount = fBufferCount - gapEndIndex;
|
||||
srcIndex = toIndex + (gapEndIndex - toIndex);
|
||||
dstIndex = fGapIndex;
|
||||
count = fGapCount + (toIndex - srcIndex);
|
||||
count = (count > trailGapCount) ? trailGapCount : count;
|
||||
}
|
||||
else {
|
||||
srcIndex = toIndex;
|
||||
dstIndex = toIndex + (gapEndIndex - fGapIndex);
|
||||
count = gapEndIndex - dstIndex;
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
memmove(fBuffer + dstIndex, fBuffer + srcIndex, count);
|
||||
|
||||
return fText;
|
||||
fGapIndex = toIndex;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
char *_BTextGapBuffer_::RealText()
|
||||
void
|
||||
_BTextGapBuffer_::SizeGapTo(long inCount)
|
||||
{
|
||||
if (inCount == fGapCount)
|
||||
return;
|
||||
|
||||
fBuffer = (char *)realloc(fBuffer, fItemCount + inCount);
|
||||
memmove(fBuffer + fGapIndex + inCount,
|
||||
fBuffer + fGapIndex + fGapCount,
|
||||
fBufferCount - (fGapIndex + fGapCount));
|
||||
|
||||
fGapCount = inCount;
|
||||
fBufferCount = fItemCount + fGapCount;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
const char *
|
||||
_BTextGapBuffer_::GetString(int32 fromOffset, int32 numChars)
|
||||
{
|
||||
char *result = "";
|
||||
|
||||
if (numChars < 1)
|
||||
return (result);
|
||||
|
||||
bool isStartBeforeGap = (fromOffset < fGapIndex);
|
||||
bool isEndBeforeGap = ((fromOffset + numChars - 1) < fGapIndex);
|
||||
|
||||
if (isStartBeforeGap == isEndBeforeGap) {
|
||||
result = fBuffer + fromOffset;
|
||||
if (!isStartBeforeGap)
|
||||
result += fGapCount;
|
||||
}
|
||||
else {
|
||||
if (fScratchSize < numChars) {
|
||||
fScratchBuffer = (char *)realloc(fScratchBuffer, numChars);
|
||||
fScratchSize = numChars;
|
||||
}
|
||||
|
||||
for (long i = 0; i < numChars; i++)
|
||||
fScratchBuffer[i] = (*this)[fromOffset + i];
|
||||
|
||||
result = fScratchBuffer;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
_BTextGapBuffer_::FindChar(char inChar, long fromIndex, long *ioDelta)
|
||||
{
|
||||
long numChars = *ioDelta;
|
||||
for (long i = 0; i < numChars; i++) {
|
||||
if (((*this)[fromIndex + i] & 0xc0) == 0x80)
|
||||
continue;
|
||||
if ((*this)[fromIndex + i] == inChar) {
|
||||
*ioDelta = i;
|
||||
return (true);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
const char *
|
||||
_BTextGapBuffer_::Text()
|
||||
{
|
||||
MoveGapTo(fItemCount);
|
||||
fBuffer[fItemCount] = '\0';
|
||||
|
||||
return fBuffer;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/*char *_BTextGapBuffer_::RealText()
|
||||
{
|
||||
return fText;
|
||||
}
|
||||
}*/
|
||||
//------------------------------------------------------------------------------
|
||||
char _BTextGapBuffer_::RealCharAt(int32 offset) const
|
||||
/*char
|
||||
_BTextGapBuffer_::RealCharAt(int32 offset) const
|
||||
{
|
||||
return *(fText + offset);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void _BTextGapBuffer_::Resize(int32 size)
|
||||
{
|
||||
if (fPhysicalBytes < size)
|
||||
{
|
||||
char *text = new char[size];
|
||||
|
||||
if (fText)
|
||||
{
|
||||
memcpy(text, fText, fLogicalBytes);
|
||||
delete fText;
|
||||
}
|
||||
|
||||
fText = text;
|
||||
fPhysicalBytes = size;
|
||||
}
|
||||
}
|
||||
return *(fBuffer + offset);
|
||||
}*/
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Copyright (c) 2001-2002, OpenBeOS
|
||||
// Copyright (c) 2001-2003, OpenBeOS
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
@@ -41,34 +41,57 @@
|
||||
class _BTextGapBuffer_ {
|
||||
|
||||
public:
|
||||
_BTextGapBuffer_();
|
||||
virtual ~_BTextGapBuffer_();
|
||||
_BTextGapBuffer_();
|
||||
virtual ~_BTextGapBuffer_();
|
||||
|
||||
void InsertText(const char *text, int32 length, int32 offset);
|
||||
void RemoveRange(int32 from, int32 to);
|
||||
|
||||
char *Text();
|
||||
char *RealText();
|
||||
void GetString(int32 offset, int32 length, char *buffer);
|
||||
void GetString(int32, int32 *);
|
||||
void InsertText(const char *inText, int32 inNumItems, int32 inAtIndex);
|
||||
void RemoveRange(int32 start, int32 end);
|
||||
|
||||
char RealCharAt(int32 offset) const;
|
||||
bool FindChar(char c, int32 inOffset, int32 *outOffset);
|
||||
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();
|
||||
int32 Length() const;
|
||||
char operator[](int32 index) const;
|
||||
|
||||
|
||||
// char *RealText();
|
||||
// void GetString(int32 offset, int32 length, char *buffer);
|
||||
// void GetString(int32, int32 *);
|
||||
|
||||
// char RealCharAt(int32 offset) const;
|
||||
|
||||
void SizeGapTo(int32);
|
||||
void MoveGapTo(int32);
|
||||
void InsertText(BFile *, int32, int32, int32);
|
||||
bool PasswordMode() const;
|
||||
void SetPasswordMode(bool);
|
||||
// void InsertText(BFile *, int32, int32, int32);
|
||||
// bool PasswordMode() const;
|
||||
// void SetPasswordMode(bool);
|
||||
|
||||
void Resize(int32 size);
|
||||
// void Resize(int32 size);
|
||||
|
||||
char *fText;
|
||||
int32 fLogicalBytes;
|
||||
int32 fPhysicalBytes;
|
||||
protected:
|
||||
int32 fExtraCount; // when realloc()-ing
|
||||
int32 fItemCount; // logical count
|
||||
char *fBuffer; // allocated memory
|
||||
int32 fBufferCount; // physical count
|
||||
int32 fGapIndex; // gap position
|
||||
int32 fGapCount; // gap count
|
||||
char *fScratchBuffer; // for GetString
|
||||
int32 fScratchSize; // scratch size
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
inline int32
|
||||
_BTextGapBuffer_::Length() const
|
||||
{
|
||||
return fItemCount;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
inline char
|
||||
_BTextGapBuffer_::operator[](long index) const
|
||||
{
|
||||
return (index < fGapIndex) ? fBuffer[index] : fBuffer[index + fGapCount];
|
||||
}
|
||||
/*
|
||||
* $Log $
|
||||
*
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Copyright (c) 2001-2002, OpenBeOS
|
||||
// Copyright (c) 2001-2003, OpenBeOS
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
@@ -25,8 +25,12 @@
|
||||
// buffer classes.
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#ifndef __TEXT_VIEW_SUPPORT_BUFFER__H__
|
||||
#define __TEXT_VIEW_SUPPORT_BUFFER__H__
|
||||
|
||||
// Standard Includes -----------------------------------------------------------
|
||||
#include <malloc.h>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
// System Includes -------------------------------------------------------------
|
||||
#include "SupportDefs.h"
|
||||
@@ -44,57 +48,99 @@ template <class T>
|
||||
class _BTextViewSupportBuffer_ {
|
||||
|
||||
public:
|
||||
_BTextViewSupportBuffer_(int32 count, int32 blockSize);
|
||||
_BTextViewSupportBuffer_(long inExtraCount = 0,
|
||||
long inCount = 0);
|
||||
virtual ~_BTextViewSupportBuffer_();
|
||||
|
||||
void InsertItemsAt(int32 offset, int32 count, const T *items);
|
||||
void RemoveItemsAt(int32 offset, int32 count);
|
||||
void InsertItemsAt(int32 inNumItems, int32 inAtIndex, const T *inItem);
|
||||
void RemoveItemsAt(int32 inNumItems, int32 inAtIndex);
|
||||
|
||||
int32 fBlockSize;
|
||||
int32 fCount;
|
||||
int32 fPhysicalSize;
|
||||
T *fItems;
|
||||
int32 fReserved;
|
||||
int32 ItemCount() const;
|
||||
|
||||
//protected:
|
||||
int32 fExtraCount;
|
||||
int32 fItemCount;
|
||||
T* fBuffer;
|
||||
int32 fBufferCount;
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
_BTextViewSupportBuffer_<T>::_BTextViewSupportBuffer_(int32 count, int32 blockSize)
|
||||
:
|
||||
fBlockSize(blockSize),
|
||||
fCount(count),
|
||||
fPhysicalSize(count + blockSize),
|
||||
fItems(NULL)
|
||||
_BTextViewSupportBuffer_<T>::_BTextViewSupportBuffer_(long inExtraCount,
|
||||
long inCount)
|
||||
: fExtraCount(inExtraCount),
|
||||
fItemCount(inCount),
|
||||
fBuffer(NULL),
|
||||
fBufferCount(fExtraCount + fItemCount)
|
||||
{
|
||||
fItems = (T*)malloc(fPhysicalSize * sizeof(T));
|
||||
fBuffer = (T *)calloc(fExtraCount + fItemCount, sizeof(T));
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
_BTextViewSupportBuffer_<T>::~_BTextViewSupportBuffer_()
|
||||
{
|
||||
free(fItems);
|
||||
free(fBuffer);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void _BTextViewSupportBuffer_<T>::InsertItemsAt(int32 offset, int32 count,
|
||||
const T *items)
|
||||
void _BTextViewSupportBuffer_<T>::InsertItemsAt(int32 inNumItems,
|
||||
int32 inAtIndex,
|
||||
const T *inItem)
|
||||
{
|
||||
if (fPhysicalSize < fCount + count)
|
||||
{
|
||||
int32 new_size = (((fCount + count) / fBlockSize) + 1) * fBlockSize;
|
||||
if (inNumItems < 1)
|
||||
return;
|
||||
|
||||
inAtIndex = (inAtIndex > fItemCount) ? fItemCount : inAtIndex;
|
||||
inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex;
|
||||
|
||||
fItems = realloc(fItems, new_size);
|
||||
long delta = inNumItems * sizeof(T);
|
||||
long logSize = fItemCount * sizeof(T);
|
||||
if ((logSize + delta) >= fBufferCount) {
|
||||
fBufferCount = logSize + delta + (fExtraCount * sizeof(T));
|
||||
fBuffer = (T *)realloc(fBuffer, fBufferCount);
|
||||
}
|
||||
|
||||
memcpy(fItems + offset, items, count);
|
||||
fCount += count;
|
||||
|
||||
T *loc = fBuffer + inAtIndex;
|
||||
memmove(loc + inNumItems, loc, (fItemCount - inAtIndex) * sizeof(T));
|
||||
memcpy(loc, inItem, delta);
|
||||
|
||||
fItemCount += inNumItems;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void _BTextViewSupportBuffer_<T>::RemoveItemsAt(int32 offset, int32 count)
|
||||
void
|
||||
_BTextViewSupportBuffer_<T>::RemoveItemsAt(int32 inNumItems,
|
||||
int32 inAtIndex)
|
||||
{
|
||||
|
||||
if (inNumItems < 1)
|
||||
return;
|
||||
|
||||
inAtIndex = (inAtIndex > fItemCount - 1) ? (fItemCount - 1) : inAtIndex;
|
||||
inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex;
|
||||
|
||||
T *loc = fBuffer + inAtIndex;
|
||||
memmove(loc, loc + inNumItems,
|
||||
(fItemCount - (inNumItems + inAtIndex)) * sizeof(T));
|
||||
|
||||
long delta = inNumItems * sizeof(T);
|
||||
long logSize = fItemCount * sizeof(T);
|
||||
long extraSize = fBufferCount - (logSize - delta);
|
||||
if (extraSize > (fExtraCount * sizeof(T))) {
|
||||
fBufferCount = (logSize - delta) + (fExtraCount * sizeof(T));
|
||||
fBuffer = (T *)realloc(fBuffer, fBufferCount);
|
||||
}
|
||||
|
||||
fItemCount -= inNumItems;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
template<class T>
|
||||
inline int32
|
||||
_BTextViewSupportBuffer_<T>::ItemCount() const
|
||||
{
|
||||
return fItemCount;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#endif // __TEXT_VIEW_SUPPORT_BUFFER__H__
|
||||
|
||||
/*
|
||||
* $Log $
|
||||
|
||||
Reference in New Issue
Block a user