Skip to content

Commit

Permalink
Text framework: Add TextListener in TextDocumentLayout...
Browse files Browse the repository at this point in the history
... and invalidate based on TextChangedEvents. I am not yet sure whether
TextChangeEvent needs separate counts for removed and changed paragraphs.
It is most likely not yet correct and may either update too many paragraph
layouts or miss updating some at the end.
  • Loading branch information
stippi committed Sep 6, 2015
1 parent 4a96bcd commit 41bd20b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
59 changes: 51 additions & 8 deletions src/apps/haikudepot/textview/TextDocumentLayout.cpp
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
* Copyright 2013-2015, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/

Expand All @@ -11,12 +11,40 @@
#include <View.h>


class LayoutTextListener : public TextListener {
public:
LayoutTextListener(TextDocumentLayout* layout)
:
fLayout(layout)
{
}

virtual void TextChanging(TextChangingEvent& event)
{
}

virtual void TextChanged(const TextChangedEvent& event)
{
printf("TextChanged(%" B_PRIi32 ", %" B_PRIi32 ")\n",
event.FirstChangedParagraph(),
event.ChangedParagraphCount());
fLayout->InvalidateParagraphs(event.FirstChangedParagraph(),
event.ChangedParagraphCount());
}

private:
TextDocumentLayout* fLayout;
};



TextDocumentLayout::TextDocumentLayout()
:
fWidth(0.0f),
fLayoutValid(false),

fDocument(),
fTextListener(new(std::nothrow) LayoutTextListener(this), true),
fParagraphLayouts()
{
}
Expand All @@ -27,10 +55,11 @@ TextDocumentLayout::TextDocumentLayout(const TextDocumentRef& document)
fWidth(0.0f),
fLayoutValid(false),

fDocument(document),
fDocument(),
fTextListener(new(std::nothrow) LayoutTextListener(this), true),
fParagraphLayouts()
{
_Init();
SetTextDocument(document);
}


Expand All @@ -40,24 +69,35 @@ TextDocumentLayout::TextDocumentLayout(const TextDocumentLayout& other)
fLayoutValid(other.fLayoutValid),

fDocument(other.fDocument),
fTextListener(new(std::nothrow) LayoutTextListener(this), true),
fParagraphLayouts(other.fParagraphLayouts)
{
if (fDocument.Get() != NULL)
fDocument->AddListener(fTextListener);
}


TextDocumentLayout::~TextDocumentLayout()
{
SetTextDocument(NULL);
}


void
TextDocumentLayout::SetTextDocument(const TextDocumentRef& document)
{
if (fDocument != document) {
fDocument = document;
_Init();
fLayoutValid = false;
}
if (fDocument == document)
return;

if (fDocument.Get() != NULL)
fDocument->RemoveListener(fTextListener);

fDocument = document;
_Init();
fLayoutValid = false;

if (fDocument.Get() != NULL)
fDocument->AddListener(fTextListener);
}


Expand Down Expand Up @@ -297,6 +337,9 @@ TextDocumentLayout::_Init()
{
fParagraphLayouts.Clear();

if (fDocument.Get() == NULL)
return;

const ParagraphList& paragraphs = fDocument->Paragraphs();

int paragraphCount = paragraphs.CountItems();
Expand Down
3 changes: 2 additions & 1 deletion src/apps/haikudepot/textview/TextDocumentLayout.h
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
* Copyright 2013-2015, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#ifndef TEXT_DOCUMENT_LAYOUT_H
Expand Down Expand Up @@ -124,6 +124,7 @@ class TextDocumentLayout : public BReferenceable {
bool fLayoutValid;

TextDocumentRef fDocument;
TextListenerRef fTextListener;
ParagraphLayoutList fParagraphLayouts;
};

Expand Down

0 comments on commit 41bd20b

Please sign in to comment.