HaikuDepot: Pargraph data classes for TextView

* Added ParagraphStyleData. Contains actual style data. Incomplete.
   Supposed to be immutable,.
 * Added ParagraphStyle, references a ParagraphStyleData.
   Multiple ParagraphStyle object can refer to the same ParagrapgStyleData,
   when a style aspect is changed for ParagraphStyle, it makes its own
   copy of the previous ParagraphStyleData with the changed property.
 * Added Paragraph class, currently knows only its ParagraphStyle.
This commit is contained in:
Stephan Aßmus 2013-08-18 15:27:22 +02:00
parent 6eabc75c78
commit 13e00959ac
7 changed files with 471 additions and 0 deletions

View File

@ -26,6 +26,10 @@ Application HaikuDepot :
PackageManager.cpp
support.cpp
# textview stuff
Paragraph.cpp
ParagraphStyle.cpp
ParagraphStyleData.cpp
TextLayout.cpp
TextStyle.cpp
TextView.cpp

View File

@ -0,0 +1,58 @@
/*
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "Paragraph.h"
Paragraph::Paragraph()
:
fStyle()
{
}
Paragraph::Paragraph(const ParagraphStyle& style)
:
fStyle(style)
{
}
Paragraph::Paragraph(const Paragraph& other)
:
fStyle(other.fStyle)
{
}
Paragraph&
Paragraph::operator=(const Paragraph& other)
{
fStyle = other.fStyle;
return *this;
}
bool
Paragraph::operator==(const Paragraph& other) const
{
return fStyle == other.fStyle;
}
bool
Paragraph::operator!=(const Paragraph& other) const
{
return !(*this == other);
}
void
Paragraph::SetStyle(const ParagraphStyle& style)
{
fStyle = style;
}

View File

@ -0,0 +1,31 @@
/*
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#ifndef PARAGRAPH_H
#define PARAGRAPH_H
#include "List.h"
#include "ParagraphStyle.h"
class Paragraph {
public:
Paragraph();
Paragraph(const ParagraphStyle& style);
Paragraph(const Paragraph& other);
Paragraph& operator=(const Paragraph& other);
bool operator==(const Paragraph& other) const;
bool operator!=(const Paragraph& other) const;
void SetStyle(const ParagraphStyle& style);
inline const ParagraphStyle& Style() const
{ return fStyle; }
private:
ParagraphStyle fStyle;
};
#endif // PARAGRAPH_H

View File

@ -0,0 +1,104 @@
/*
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "ParagraphStyle.h"
ParagraphStyle::ParagraphStyle()
:
fStyleData(new ParagraphStyleData(), true)
{
}
ParagraphStyle::ParagraphStyle(const ParagraphStyle& other)
:
fStyleData(other.fStyleData)
{
}
ParagraphStyle&
ParagraphStyle::operator=(const ParagraphStyle& other)
{
if (this == &other)
return *this;
fStyleData = other.fStyleData;
return *this;
}
bool
ParagraphStyle::operator==(const ParagraphStyle& other) const
{
if (this == &other)
return true;
if (fStyleData == other.fStyleData)
return true;
if (fStyleData.Get() != NULL && other.fStyleData.Get() != NULL)
return *fStyleData.Get() == *other.fStyleData.Get();
return false;
}
bool
ParagraphStyle::operator!=(const ParagraphStyle& other) const
{
return !(*this == other);
}
bool
ParagraphStyle::SetAlignment(::Alignment alignment)
{
fStyleData.SetTo(fStyleData->SetAlignment(alignment));
return fStyleData->Alignment() == alignment;
}
bool
ParagraphStyle::SetJustify(bool justify)
{
fStyleData.SetTo(fStyleData->SetJustify(justify));
return fStyleData->Justify() == justify;
}
bool
ParagraphStyle::SetFirstLineInset(float inset)
{
fStyleData.SetTo(fStyleData->SetFirstLineInset(inset));
return fStyleData->FirstLineInset() == inset;
}
bool
ParagraphStyle::SetLineInset(float inset)
{
fStyleData.SetTo(fStyleData->SetLineInset(inset));
return fStyleData->LineInset() == inset;
}
bool
ParagraphStyle::SetSpacingTop(float spacing)
{
fStyleData.SetTo(fStyleData->SetSpacingTop(spacing));
return fStyleData->SpacingTop() == spacing;
}
bool
ParagraphStyle::SetSpacingBottom(float spacing)
{
fStyleData.SetTo(fStyleData->SetSpacingBottom(spacing));
return fStyleData->SpacingBottom() == spacing;
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#ifndef PARAGRAPH_STYLE_H
#define PARAGRAPH_STYLE_H
#include "ParagraphStyleData.h"
class ParagraphStyle {
public:
ParagraphStyle();
ParagraphStyle(const ParagraphStyle& other);
ParagraphStyle& operator=(const ParagraphStyle& other);
bool operator==(const ParagraphStyle& other) const;
bool operator!=(const ParagraphStyle& other) const;
bool SetAlignment(::Alignment alignment);
::Alignment Alignment() const;
bool SetJustify(bool justify);
bool Justify() const;
bool SetFirstLineInset(float inset);
float FirstLineInset() const;
bool SetLineInset(float inset);
float LineInset() const;
bool SetSpacingTop(float spacing);
float SpacingTop() const;
bool SetSpacingBottom(float spacing);
float SpacingBottom() const;
private:
ParagraphDataRef fStyleData;
};
#endif // PARAGRAPH_STYLE_H

View File

@ -0,0 +1,158 @@
/*
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "ParagraphStyleData.h"
#include <new>
ParagraphStyleData::ParagraphStyleData()
:
fAlignment(ALIGN_LEFT),
fJustify(false),
fFirstLineInset(0.0f),
fLineInset(0.0f),
fSpacingTop(0.0f),
fSpacingBottom(0.0f)
{
}
ParagraphStyleData::ParagraphStyleData(const ParagraphStyleData& other)
:
fAlignment(other.fAlignment),
fJustify(other.fJustify),
fFirstLineInset(other.fFirstLineInset),
fLineInset(other.fLineInset),
fSpacingTop(other.fSpacingTop),
fSpacingBottom(other.fSpacingBottom)
{
}
bool
ParagraphStyleData::operator==(const ParagraphStyleData& other) const
{
if (this == &other)
return true;
return fAlignment == other.fAlignment
&& fJustify == other.fJustify
&& fFirstLineInset == other.fFirstLineInset
&& fLineInset == other.fLineInset
&& fSpacingTop == other.fSpacingTop
&& fSpacingBottom == other.fSpacingBottom;
}
bool
ParagraphStyleData::operator!=(const ParagraphStyleData& other) const
{
return !(*this == other);
}
ParagraphStyleData*
ParagraphStyleData::SetAlignment(::Alignment alignment)
{
if (fAlignment == alignment)
return this;
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL)
return this;
ret->fAlignment = alignment;
return ret;
}
ParagraphStyleData*
ParagraphStyleData::SetJustify(bool justify)
{
if (fJustify == justify)
return this;
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL)
return this;
ret->fJustify = justify;
return ret;
}
ParagraphStyleData*
ParagraphStyleData::SetFirstLineInset(float inset)
{
if (fFirstLineInset == inset)
return this;
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL)
return this;
ret->fFirstLineInset = inset;
return ret;
}
ParagraphStyleData*
ParagraphStyleData::SetLineInset(float inset)
{
if (fLineInset == inset)
return this;
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL)
return this;
ret->fLineInset = inset;
return ret;
}
ParagraphStyleData*
ParagraphStyleData::SetSpacingTop(float spacing)
{
if (fSpacingTop == spacing)
return this;
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL)
return this;
ret->fSpacingTop = spacing;
return ret;
}
ParagraphStyleData*
ParagraphStyleData::SetSpacingBottom(float spacing)
{
if (fSpacingBottom == spacing)
return this;
ParagraphStyleData* ret = new(std::nothrow) ParagraphStyleData(*this);
if (ret == NULL)
return this;
ret->fSpacingBottom = spacing;
return ret;
}
// #pragma mark - private
ParagraphStyleData&
ParagraphStyleData::operator=(const ParagraphStyleData& other)
{
return *this;
}

View File

@ -0,0 +1,72 @@
/*
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#ifndef PARAGRAPH_STYLE_DATA_H
#define PARAGRAPH_STYLE_DATA_H
#include <Referenceable.h>
enum Alignment {
ALIGN_LEFT = 0,
ALIGN_CENTER = 1,
ALIGN_RIGHT = 2,
};
// You cannot modify a ParagraphStyleData object once it has been
// created.
class ParagraphStyleData : public BReferenceable {
public:
ParagraphStyleData();
ParagraphStyleData(
const ParagraphStyleData& other);
bool operator==(
const ParagraphStyleData& other) const;
bool operator!=(
const ParagraphStyleData& other) const;
ParagraphStyleData* SetAlignment(::Alignment alignment);
inline ::Alignment Alignment() const
{ return fAlignment; }
ParagraphStyleData* SetJustify(bool justify);
inline bool Justify() const
{ return fJustify; }
ParagraphStyleData* SetFirstLineInset(float inset);
inline float FirstLineInset() const
{ return fFirstLineInset; }
ParagraphStyleData* SetLineInset(float inset);
inline float LineInset() const
{ return fLineInset; }
ParagraphStyleData* SetSpacingTop(float spacing);
inline float SpacingTop() const
{ return fSpacingTop; }
ParagraphStyleData* SetSpacingBottom(float spacing);
inline float SpacingBottom() const
{ return fSpacingBottom; }
private:
ParagraphStyleData& operator=(const ParagraphStyleData& other);
private:
::Alignment fAlignment;
bool fJustify;
float fFirstLineInset;
float fLineInset;
float fSpacingTop;
float fSpacingBottom;
};
typedef BReference<ParagraphStyleData> ParagraphDataRef;
#endif // PARAGRAPH_STYLE_DATA_H