Text framework: Add TextListener in TextDocumentLayout...
... 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.
This commit is contained in:
parent
4a96bcdafa
commit
41bd20b06b
|
@ -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.
|
||||
*/
|
||||
|
||||
|
@ -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()
|
||||
{
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -297,6 +337,9 @@ TextDocumentLayout::_Init()
|
|||
{
|
||||
fParagraphLayouts.Clear();
|
||||
|
||||
if (fDocument.Get() == NULL)
|
||||
return;
|
||||
|
||||
const ParagraphList& paragraphs = fDocument->Paragraphs();
|
||||
|
||||
int paragraphCount = paragraphs.CountItems();
|
||||
|
|
|
@ -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
|
||||
|
@ -124,6 +124,7 @@ private:
|
|||
bool fLayoutValid;
|
||||
|
||||
TextDocumentRef fDocument;
|
||||
TextListenerRef fTextListener;
|
||||
ParagraphLayoutList fParagraphLayouts;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue