From c1489885decc91520542f54da489af5290e8a98c Mon Sep 17 00:00:00 2001 From: Sebastian Geerken Date: Tue, 9 Jun 2015 23:15:39 +0200 Subject: SRDOP: Some refactoring. --- dw/Makefile.am | 2 ++ dw/textblock.cc | 89 +++++++----------------------------------------- dw/textblock.hh | 5 ++- dw/tools.cc | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ dw/tools.hh | 59 ++++++++++++++++++++++++++++++++ 5 files changed, 177 insertions(+), 80 deletions(-) create mode 100644 dw/tools.cc create mode 100644 dw/tools.hh (limited to 'dw') diff --git a/dw/Makefile.am b/dw/Makefile.am index ee891234..ee779254 100644 --- a/dw/Makefile.am +++ b/dw/Makefile.am @@ -27,6 +27,8 @@ libDw_core_a_SOURCES = \ stackingcontextmgr.cc \ style.cc \ style.hh \ + tools.cc \ + tools.hh \ types.cc \ types.hh \ ui.cc \ diff --git a/dw/textblock.cc b/dw/textblock.cc index 262554c6..c573fd71 100644 --- a/dw/textblock.cc +++ b/dw/textblock.cc @@ -218,11 +218,8 @@ Textblock::Textblock (bool limitTextWidth) lastWordDrawn = -1; DBG_OBJ_SET_NUM ("lastWordDrawn", lastWordDrawn); - sizeRequestNumPos = 0; - sizeRequestReferences = NULL; - sizeRequestX = sizeRequestY = NULL; - DBG_OBJ_SET_BOOL ("sizeRequestNumPos", sizeRequestNumPos); - + DBG_SET_SIZE_PARAMS ("sizeRequestParams", sizeRequestParams); + /* * The initial sizes of lines and words should not be * too high, since this will waste much memory with tables @@ -287,14 +284,7 @@ Textblock::~Textblock () delete lines; delete words; delete anchors; - - if (sizeRequestReferences) - delete[] sizeRequestReferences; - if (sizeRequestX) - delete[] sizeRequestX; - if (sizeRequestY) - delete[] sizeRequestY; - + /* Make sure we don't own widgets anymore. Necessary before call of parent class destructor. (???) */ words = NULL; @@ -312,27 +302,8 @@ void Textblock::sizeRequestImpl (core::Requisition *requisition, int numPos, { DBG_OBJ_ENTER0 ("resize", 0, "sizeRequestImpl"); - if (sizeRequestReferences) - delete[] sizeRequestReferences; - if (sizeRequestX) - delete[] sizeRequestX; - if (sizeRequestY) - delete[] sizeRequestY; - - sizeRequestNumPos = numPos; - DBG_OBJ_SET_BOOL ("sizeRequestNumPos", sizeRequestNumPos); - - sizeRequestReferences = new Widget*[numPos]; - sizeRequestX = new int[numPos]; - sizeRequestY = new int[numPos]; - for (int i = 0; i < numPos; i++) { - sizeRequestReferences[i] = references[i]; - sizeRequestX[i] = x[i]; - sizeRequestY[i] = y[i]; - DBG_OBJ_ARRSET_PTR ("sizeRequestReferences", i, sizeRequestReferences[i]); - DBG_OBJ_ARRSET_NUM ("sizeRequestX", i, sizeRequestX[i]); - DBG_OBJ_ARRSET_NUM ("sizeRequestY", i, sizeRequestY[i]); - } + sizeRequestParams.fill (numPos, references, x, y); + DBG_SET_SIZE_PARAMS ("sizeRequestParams", sizeRequestParams); int newLineBreakWidth = getAvailWidth (true); if (newLineBreakWidth != lineBreakWidth) { @@ -2289,37 +2260,12 @@ bool Textblock::calcSizeOfWidgetInFlow (int wordIndex, Widget *widget, int lastMargin, yLine = yOffsetOfLineToBeCreated (&lastMargin); int yRel = yLine - lastMargin + max (lastMargin, widget->getStyle()->margin.top); - - int numPos = 0; - int numChildReferences = widget->numSizeRequestReferences (); - Widget **references = new Widget*[numChildReferences]; - int *x = new int[numChildReferences]; - int *y = new int[numChildReferences]; - - for (int i = 0; i < numChildReferences; i++) { - Widget *childReference = widget->sizeRequestReference (i); - if (childReference == this) { - references[numPos] = childReference; - x[numPos] = xRel; - y[numPos] = yRel; - numPos++; - } else { - for (int j = 0; j < sizeRequestNumPos; j++) { - if (childReference == sizeRequestReferences[j]) { - references[numPos] = childReference; - x[numPos] = sizeRequestX[j] + xRel; - y[numPos] = sizeRequestY[j] + yRel; - numPos++; - } - } - } - } - widget->sizeRequest (size, numPos, references, x, y); - - delete references; - delete x; - delete y; + core::SizeParams childParams; + sizeRequestParams.forChild (this, widget, xRel, yRel, &childParams); + widget->sizeRequest (size, childParams.getNumPos (), + childParams.getReferences (), childParams.getX (), + childParams.getY ()); result = true; } else { @@ -2341,19 +2287,8 @@ bool Textblock::findSizeRequestReference (Widget *reference, int *xRef, if (yRef) *yRef = 0; return true; - } else { - for (int i = 0; i < sizeRequestNumPos; i++) { - if (reference == sizeRequestReferences[i]) { - if (xRef) - *xRef = sizeRequestX[i]; - if (yRef) - *yRef = sizeRequestY[i]; - return true; - } - } - - return false; - } + } else + return sizeRequestParams.findReference (reference, xRef, yRef); } /** diff --git a/dw/textblock.hh b/dw/textblock.hh index 3decda6d..535834a8 100644 --- a/dw/textblock.hh +++ b/dw/textblock.hh @@ -4,6 +4,7 @@ #include #include "regardingborder.hh" +#include "tools.hh" #include "../lout/misc.hh" // These were used when improved line breaking and hyphenation were implemented. @@ -576,9 +577,7 @@ protected: int redrawY; int lastWordDrawn; - int sizeRequestNumPos; - Widget **sizeRequestReferences; - int *sizeRequestX, *sizeRequestY; + core::SizeParams sizeRequestParams; /* This value is currently) set by setAscent(). */ int lineBreakWidth; diff --git a/dw/tools.cc b/dw/tools.cc new file mode 100644 index 00000000..1db83306 --- /dev/null +++ b/dw/tools.cc @@ -0,0 +1,102 @@ +#include "tools.hh" + +namespace dw { +namespace core { + +SizeParams::SizeParams () +{ + init (); +} + +SizeParams::~SizeParams () +{ + cleanup (); +} + +void SizeParams::init () +{ + numPos = 0; + references = NULL; + x = y = NULL; +} + +void SizeParams::cleanup () +{ + if (references) + delete[] references; + if (x) + delete[] x; + if (y) + delete[] y; + + init (); +} + +void SizeParams::fill (int numPos, Widget **references, int *x, int *y) +{ + cleanup (); + + this->numPos = numPos; + + this->references = new Widget*[numPos]; + this->x = new int[numPos]; + this->y = new int[numPos]; + + for (int i = 0; i < numPos; i++) { + this->references[i] = references[i]; + this->x[i] = x[i]; + this->y[i] = y[i]; + } +} + +void SizeParams::forChild (Widget *parent, Widget *child, int xRel, int yRel, + SizeParams *childParams) +{ + int numChildReferences = child->numSizeRequestReferences (); + + childParams->numPos = 0; + childParams->references = new Widget*[numChildReferences]; + childParams->x = new int[numChildReferences]; + childParams->y = new int[numChildReferences]; + + for (int i = 0; i < numChildReferences; i++) { + Widget *childReference = child->sizeRequestReference (i); + if (childReference == parent) { + references[numPos] = childReference; + childParams->x[numPos] = xRel; + childParams->y[numPos] = yRel; + numPos++; + } else { + bool found = false; + for (int j = 0; !found && j < numPos; j++) { + if (childReference == references[j]) { + found = true; + references[numPos] = childReference; + childParams->x[numPos] = x[j] + xRel; + childParams->y[numPos] = y[j] + yRel; + numPos++; + } + } + } + } + +} + +bool SizeParams::findReference (Widget *reference, int *x, int *y) +{ + for (int i = 0; i < numPos; i++) { + if (reference == references[i]) { + if (x) + *x = this->x[i]; + if (y) + *y = this->y[i]; + return true; + } + } + + return false; +} + + +} // namespace core +} // namespace dw diff --git a/dw/tools.hh b/dw/tools.hh new file mode 100644 index 00000000..04084a60 --- /dev/null +++ b/dw/tools.hh @@ -0,0 +1,59 @@ +#ifndef __DW_TOOLS_HH__ +#define __DW_TOOLS_HH__ + +#include "core.hh" +#include "../lout/debug.hh" + +namespace dw { +namespace core { + +/** + * \brief Hold arguments passed to dw::core::Widget::sizeRequest and + * dw::core::Widget::getExtremes, as described in \ref dw-size-request-pos. + */ +class SizeParams +{ +private: + int numPos; + Widget **references; + int *x, *y; + + void init (); + void cleanup (); + +public: + SizeParams (); + ~SizeParams (); + + void fill (int numPos, Widget **references, int *x, int *y); + void forChild (Widget *parent, Widget *child, int xRel, int yRel, + SizeParams *childParams); + bool findReference (Widget *reference, int *x, int *y); + + inline int getNumPos () { return numPos; } + inline Widget **getReferences () { return references; } + inline int *getX () { return x; } + inline int *getY () { return y; } + inline Widget *getReference (int i) { return references[i]; } + inline int getX (int i) { return x[i]; } + inline int getY (int i) { return y[i]; } +}; + +#define DBG_SET_SIZE_PARAMS(prefix, params) \ + D_STMT_START { \ + DBG_IF_RTFL { \ + DBG_OBJ_SET_NUM (prefix ".numPos", params.getNumPos ()); \ + for (int i = 0; i < params.getNumPos (); i++) { \ + DBG_OBJ_ARRSET_PTR (prefix ".references", i, \ + params.getReference (i)); \ + DBG_OBJ_ARRSET_NUM (prefix ".x", i, params.getX (i)); \ + DBG_OBJ_ARRSET_NUM (prefix ".y", i, params.getY (i)); \ + } \ + } \ + } D_STMT_END + + +} // namespace core +} // namespace dw + +#endif // __DW_TOOLS_HH__ -- cgit v1.2.3