From acedc140e545b34b46df551d79b4ef90638b36b6 Mon Sep 17 00:00:00 2001 From: Sebastian Geerken Date: Sat, 13 Sep 2014 00:23:00 +0200 Subject: OOFAwareWidgetIterator: extended and applied to TextblockIterator. --- dw/oofawarewidget.hh | 10 ++- dw/oofawarewidget_iterator.cc | 70 +++++++++++++---- dw/textblock.hh | 24 ++---- dw/textblock_iterator.cc | 174 +++++++++--------------------------------- 4 files changed, 103 insertions(+), 175 deletions(-) diff --git a/dw/oofawarewidget.hh b/dw/oofawarewidget.hh index 50332487..e21124c1 100644 --- a/dw/oofawarewidget.hh +++ b/dw/oofawarewidget.hh @@ -59,20 +59,26 @@ protected: int sectionIndex; // 0 means in flow, otherwise OOFM index + 1 int index; - int numParts (int sectionIndex); + int numParts (int sectionIndex, int numContentsInFlow = -1); void getPart (int sectionIndex, int index, core::Content *content); protected: virtual int numContentsInFlow () = 0; virtual void getContentInFlow (int index, core::Content *content) = 0; + void setValues (int sectionIndex, int index); + inline void cloneValues (OOFAwareWidgetIterator *other) + { other->setValues (sectionIndex, index); } + + inline bool inFlow () { return sectionIndex == 0; } + inline bool getInFlowIndex () { assert (inFlow ()); return index; } void highlightOOF (int start, int end, core::HighlightLayer layer); void unhighlightOOF (int direction, core::HighlightLayer layer); void getAllocationOOF (int start, int end, core::Allocation *allocation); public: OOFAwareWidgetIterator (OOFAwareWidget *widget, core::Content::Type mask, - bool atEnd); + bool atEnd, int numContentsInFlow); int compareTo(lout::object::Comparable *other); diff --git a/dw/oofawarewidget_iterator.cc b/dw/oofawarewidget_iterator.cc index f3e68af9..74b3ea53 100644 --- a/dw/oofawarewidget_iterator.cc +++ b/dw/oofawarewidget_iterator.cc @@ -31,12 +31,50 @@ namespace dw { namespace oof { -int OOFAwareWidget::OOFAwareWidgetIterator::numParts (int sectionIndex) +// "numContentsInFlow" is passed here to avoid indirectly callin the (virtual) +// method numContentsInFlow() from the constructor. +OOFAwareWidget::OOFAwareWidgetIterator::OOFAwareWidgetIterator + (OOFAwareWidget *widget, Content::Type mask, bool atEnd, + int numContentsInFlow) : + Iterator (widget, mask, atEnd) +{ + if (atEnd) { + sectionIndex = NUM_SECTIONS - 1; + while (sectionIndex >= 0 && + numParts (sectionIndex, numContentsInFlow) == 0) + sectionIndex--; + index = numParts (sectionIndex, numContentsInFlow); + } else { + sectionIndex = 0; + index = -1; + } + + content.type = atEnd ? core::Content::END : core::Content::START; +} + +void OOFAwareWidget::OOFAwareWidgetIterator::setValues (int sectionIndex, + int index) +{ + this->sectionIndex = sectionIndex; + this->index = index; + + if (sectionIndex == 0 && index < 0) + content.type = core::Content::START; + else if (sectionIndex == NUM_SECTIONS - 1 && + index >= numParts (sectionIndex)) + content.type = core::Content::END; + else + getPart (sectionIndex, index, &content); +} + +int OOFAwareWidget::OOFAwareWidgetIterator::numParts (int sectionIndex, + int numContentsInFlow) { OOFAwareWidget *widget = (OOFAwareWidget*)getWidget(); if (sectionIndex == 0) - return numContentsInFlow (); + return numContentsInFlow == -1 ? + this->numContentsInFlow () : numContentsInFlow; else return widget->outOfFlowMgr[sectionIndex - 1] ? widget->outOfFlowMgr[sectionIndex - 1]->getNumWidgets () : 0; @@ -44,14 +82,14 @@ int OOFAwareWidget::OOFAwareWidgetIterator::numParts (int sectionIndex) void OOFAwareWidget::OOFAwareWidgetIterator::getPart (int sectionIndex, int index, - core::Content *content) + Content *content) { OOFAwareWidget *widget = (OOFAwareWidget*)getWidget(); if (sectionIndex == 0) getContentInFlow (index, content); else { - content->type = core::Content::WIDGET_OOF_CONT; + content->type = Content::WIDGET_OOF_CONT; content->widget = widget->outOfFlowMgr[sectionIndex - 1]->getWidget (index); } @@ -69,7 +107,7 @@ int OOFAwareWidget::OOFAwareWidgetIterator::compareTo (Comparable *other) bool OOFAwareWidget::OOFAwareWidgetIterator::next () { - if (content.type == core::Content::END) + if (content.type == Content::END) return false; do { @@ -77,11 +115,11 @@ bool OOFAwareWidget::OOFAwareWidgetIterator::next () if (index >= numParts(sectionIndex)) { sectionIndex++; - while (sectionIndex < NUM_SECTIONS && numParts(sectionIndex) == 0) + while (sectionIndex < NUM_SECTIONS && numParts (sectionIndex) == 0) sectionIndex++; if (sectionIndex == NUM_SECTIONS) { - content.type = core::Content::END; + content.type = Content::END; return false; } else index = 0; @@ -95,7 +133,7 @@ bool OOFAwareWidget::OOFAwareWidgetIterator::next () bool OOFAwareWidget::OOFAwareWidgetIterator::prev () { - if (content.type == core::Content::START) + if (content.type == Content::START) return false; do { @@ -103,14 +141,14 @@ bool OOFAwareWidget::OOFAwareWidgetIterator::prev () if (index < 0) { sectionIndex--; - while (sectionIndex >= 0 && numParts(sectionIndex) == 0) + while (sectionIndex >= 0 && numParts (sectionIndex) == 0) sectionIndex--; if (sectionIndex < 0) { - content.type = core::Content::START; + content.type = Content::START; return false; } else - index = numParts(sectionIndex) - 1; + index = numParts (sectionIndex) - 1; } getPart (sectionIndex, index, &content); @@ -120,21 +158,21 @@ bool OOFAwareWidget::OOFAwareWidgetIterator::prev () } void OOFAwareWidget::OOFAwareWidgetIterator::highlightOOF (int start, int end, - core::HighlightLayer - layer) + HighlightLayer layer) { // TODO What about OOF widgets? } -void OOFAwareWidget::OOFAwareWidgetIterator::unhighlightOOF - (int direction, core::HighlightLayer layer) +void OOFAwareWidget::OOFAwareWidgetIterator::unhighlightOOF (int direction, + HighlightLayer + layer) { // TODO What about OOF widgets? } void OOFAwareWidget::OOFAwareWidgetIterator::getAllocationOOF (int start, int end, - core::Allocation + Allocation *allocation) { // TODO Consider start and end? diff --git a/dw/textblock.hh b/dw/textblock.hh index 82b62b2a..870a7c87 100644 --- a/dw/textblock.hh +++ b/dw/textblock.hh @@ -447,36 +447,24 @@ protected: int wordIndex; }; - class TextblockIterator: public core::Iterator + class TextblockIterator: public OOFAwareWidgetIterator { - private: - enum { NUM_SECTIONS = NUM_OOFM + 1 }; - int sectionIndex; // 0 means in flow, otherwise OOFM index + 1 - int index; - - TextblockIterator (Textblock *textblock, core::Content::Type mask, - int sectionIndex, int index); - - int numParts (int sectionIndex); - void getPart (int sectionIndex, int index, core::Content *content); + protected: + int numContentsInFlow (); + void getContentInFlow (int index, core::Content *content); public: TextblockIterator (Textblock *textblock, core::Content::Type mask, bool atEnd); - inline static TextblockIterator *createWordIndexIterator - (Textblock *textblock, core::Content::Type mask, int wordIndex) - { return new TextblockIterator (textblock, mask, 0, wordIndex); } + static TextblockIterator *createWordIndexIterator + (Textblock *textblock, core::Content::Type mask, int wordIndex); lout::object::Object *clone(); - int compareTo(lout::object::Comparable *other); - bool next (); - bool prev (); void highlight (int start, int end, core::HighlightLayer layer); void unhighlight (int direction, core::HighlightLayer layer); void getAllocation (int start, int end, core::Allocation *allocation); - void print (); }; friend class TextblockIterator; diff --git a/dw/textblock_iterator.cc b/dw/textblock_iterator.cc index 0e66faed..34a76c08 100644 --- a/dw/textblock_iterator.cc +++ b/dw/textblock_iterator.cc @@ -33,138 +33,35 @@ namespace dw { Textblock::TextblockIterator::TextblockIterator (Textblock *textblock, core::Content::Type mask, bool atEnd): - core::Iterator (textblock, mask, atEnd) + OOFAwareWidgetIterator (textblock, mask, atEnd, textblock->words->size ()) { - if (atEnd) { - sectionIndex = NUM_SECTIONS - 1; - while (sectionIndex >= 0 && numParts (sectionIndex) == 0) - sectionIndex--; - index = numParts (sectionIndex); - } else { - sectionIndex = 0; - index = -1; - } - - content.type = atEnd ? core::Content::END : core::Content::START; -} - -Textblock::TextblockIterator::TextblockIterator (Textblock *textblock, - core::Content::Type mask, - int sectionIndex, int index): - core::Iterator (textblock, mask, false) -{ - this->sectionIndex = sectionIndex; - this->index = index; - - // TODO To be completely exact, sectionIndex should be considered here. - if (index < 0) - content.type = core::Content::START; - else if (index >= textblock->words->size()) - content.type = core::Content::END; - else - content = textblock->words->getRef(index)->content; -} - -int Textblock::TextblockIterator::numParts (int sectionIndex) -{ - Textblock *textblock = (Textblock*)getWidget(); - - if (sectionIndex == 0) - return textblock->words->size (); - else - return textblock->outOfFlowMgr[sectionIndex - 1] ? - textblock->outOfFlowMgr[sectionIndex - 1]->getNumWidgets () : 0; } -void Textblock::TextblockIterator::getPart (int sectionIndex, int index, - core::Content *content) +Textblock::TextblockIterator + *Textblock::TextblockIterator::createWordIndexIterator (Textblock *textblock, + core::Content::Type + mask, + int wordIndex) { - Textblock *textblock = (Textblock*)getWidget(); - - if (sectionIndex == 0) - *content = textblock->words->getRef(index)->content; - else { - content->type = core::Content::WIDGET_OOF_CONT; - content->widget = - textblock->outOfFlowMgr[sectionIndex - 1]->getWidget (index); - } + TextblockIterator *tbIt = new TextblockIterator (textblock, mask, false); + tbIt->setValues (0, wordIndex); + return tbIt; } - object::Object *Textblock::TextblockIterator::clone() { - return new TextblockIterator ((Textblock*)getWidget(), getMask(), - sectionIndex, index); -} - -int Textblock::TextblockIterator::compareTo(object::Comparable *other) -{ - TextblockIterator *otherTI = (TextblockIterator*)other; - - if (sectionIndex != otherTI->sectionIndex) - return sectionIndex - otherTI->sectionIndex; - else - return index - otherTI->index; -} - -bool Textblock::TextblockIterator::next () -{ - if (content.type == core::Content::END) - return false; - - do { - index++; - - if (index >= numParts(sectionIndex)) { - sectionIndex++; - while (sectionIndex < NUM_SECTIONS && numParts(sectionIndex) == 0) - sectionIndex++; - - if (sectionIndex == NUM_SECTIONS) { - content.type = core::Content::END; - return false; - } else - index = 0; - } - - getPart (sectionIndex, index, &content); - } while ((content.type & getMask()) == 0); - - return true; -} - -bool Textblock::TextblockIterator::prev () -{ - if (content.type == core::Content::START) - return false; - - do { - index--; - - if (index < 0) { - sectionIndex--; - while (sectionIndex >= 0 && numParts(sectionIndex) == 0) - sectionIndex--; - - if (sectionIndex < 0) { - content.type = core::Content::START; - return false; - } else - index = numParts(sectionIndex) - 1; - } - - getPart (sectionIndex, index, &content); - } while ((content.type & getMask()) == 0); - - return true; + TextblockIterator *tbIt = + new TextblockIterator ((Textblock*)getWidget(), getMask(), false); + cloneValues (tbIt); + return tbIt; } void Textblock::TextblockIterator::highlight (int start, int end, core::HighlightLayer layer) { - if (sectionIndex == 0) { + if (inFlow ()) { Textblock *textblock = (Textblock*)getWidget(); - int index1 = index, index2 = index; + int index = getInFlowIndex (), index1 = index, index2 = index; int oldStartIndex = textblock->hlStart[layer].index; int oldStartChar = textblock->hlStart[layer].nChar; @@ -194,17 +91,16 @@ void Textblock::TextblockIterator::highlight (int start, int end, oldEndIndex != textblock->hlEnd[layer].index || oldEndChar != textblock->hlEnd[layer].nChar) textblock->queueDrawRange (index1, index2); - } - - // TODO What about OOF widgets? + } else + highlightOOF (start, end, layer); } void Textblock::TextblockIterator::unhighlight (int direction, core::HighlightLayer layer) { - if (sectionIndex == 0) { + if (inFlow ()) { Textblock *textblock = (Textblock*)getWidget(); - int index1 = index, index2 = index; + int index = getInFlowIndex (), index1 = index, index2 = index; if (textblock->hlStart[layer].index > textblock->hlEnd[layer].index) return; @@ -234,23 +130,17 @@ void Textblock::TextblockIterator::unhighlight (int direction, oldEndIndex != textblock->hlEnd[layer].index || oldEndChar != textblock->hlEnd[layer].nChar) textblock->queueDrawRange (index1, index2); - } - - // TODO What about OOF widgets? + } else + unhighlightOOF (direction, layer); } void Textblock::TextblockIterator::getAllocation (int start, int end, core::Allocation *allocation) { - Textblock *textblock = (Textblock*)getWidget(); - - if (sectionIndex > 0) { - // TODO Consider start and end? - *allocation = - *(textblock->outOfFlowMgr[sectionIndex - 1]->getWidget(index) - ->getAllocation()); - } else { - int lineIndex = textblock->findLineOfWord (index); + if (inFlow ()) { + Textblock *textblock = (Textblock*)getWidget(); + int index = getInFlowIndex (), + lineIndex = textblock->findLineOfWord (index); Line *line = textblock->lines->getRef (lineIndex); Word *word = textblock->words->getRef (index); @@ -289,13 +179,19 @@ void Textblock::TextblockIterator::getAllocation (int start, int end, } allocation->ascent = word->size.ascent; allocation->descent = word->size.descent; - } + } else + getAllocationOOF (start, end, allocation); +} + +int Textblock::TextblockIterator::numContentsInFlow () +{ + return ((Textblock*)getWidget())->words->size (); } -void Textblock::TextblockIterator::print () +void Textblock::TextblockIterator::getContentInFlow (int index, + core::Content *content) { - Iterator::print (); - printf (", sectionIndex = %d, index = %d", sectionIndex, index); + *content = ((Textblock*)getWidget())->words->getRef(index)->content; } } // namespace dw -- cgit v1.2.3