diff --git a/src/apps/haikudepot/textview/TextDocumentLayout.cpp b/src/apps/haikudepot/textview/TextDocumentLayout.cpp index 6ed09b4e97e..f998e36cd35 100644 --- a/src/apps/haikudepot/textview/TextDocumentLayout.cpp +++ b/src/apps/haikudepot/textview/TextDocumentLayout.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014, Stephan Aßmus . + * Copyright 2013-2015, Stephan Aßmus . * All rights reserved. Distributed under the terms of the MIT License. */ @@ -11,12 +11,40 @@ #include +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(); diff --git a/src/apps/haikudepot/textview/TextDocumentLayout.h b/src/apps/haikudepot/textview/TextDocumentLayout.h index b7bcb86e21e..6f111e75bb6 100644 --- a/src/apps/haikudepot/textview/TextDocumentLayout.h +++ b/src/apps/haikudepot/textview/TextDocumentLayout.h @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014, Stephan Aßmus . + * Copyright 2013-2015, Stephan Aßmus . * All rights reserved. Distributed under the terms of the MIT License. */ #ifndef TEXT_DOCUMENT_LAYOUT_H @@ -124,6 +124,7 @@ class TextDocumentLayout : public BReferenceable { bool fLayoutValid; TextDocumentRef fDocument; + TextListenerRef fTextListener; ParagraphLayoutList fParagraphLayouts; };