Skip to content

Commit

Permalink
修复上一版本修改的interval==-1的场景,格式化代码。
Browse files Browse the repository at this point in the history
  • Loading branch information
setoutsoft committed Sep 19, 2023
1 parent c605c33 commit 0c37aae
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 130 deletions.
2 changes: 1 addition & 1 deletion SOUI/src/core/shostwnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ int SHostWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
m_szAppSetted.cy = lpCreateStruct->cy;
if (!OnLoadLayoutFromResourceID(m_strXmlLayout))
return -1;
GetRoot()->RequestRelayout();
GetRoot()->RequestRelayout();
m_pTipCtrl = CreateTooltip();
if (m_pTipCtrl && m_hostAttr.m_bHasMsgLoop)
GetMsgLoop()->AddMessageFilter(m_pTipCtrl);
Expand Down
128 changes: 70 additions & 58 deletions SOUI/src/layout/SGridLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,14 @@ SIZE SGridLayout::MeasureChildren(const IWindow *pParent, int nWidth, int nHeigh
delete[] pCellsSize;
delete[] pCellsOccupy;

szRet.cx += m_xInterval.toPixelSize(pParent->GetScale()) * (nCols - 1);
szRet.cy += m_yInterval.toPixelSize(pParent->GetScale()) * (nRows - 1);
int xInter = m_xInterval.toPixelSize(pParent->GetScale());
int yInter = m_yInterval.toPixelSize(pParent->GetScale());
if (xInter < 0)
xInter = 0;
if (yInter < 0)
yInter = 0;
szRet.cx += xInter * (nCols - 1);
szRet.cy += yInter * (nRows - 1);
return szRet;
}

Expand Down Expand Up @@ -517,63 +523,69 @@ void SGridLayout::LayoutChildren(IWindow *pParent)
delete[] pCellsSpanFlagY;
delete[] pCellsSpanFlagX;

int xInter = m_xInterval.toPixelSize(pParent->GetScale());
int yInter = m_yInterval.toPixelSize(pParent->GetScale());
int xInter = m_xInterval.toPixelSize(pParent->GetScale());
int yInter = m_yInterval.toPixelSize(pParent->GetScale());
//分配weight
if(totalColsWeight > 0.0f){
int netParentWid = rcParent.Width() - (nCols - 1) * (xInter>0?xInter:0);
if (nTotalWidth < netParentWid)
{
int nRemain = netParentWid - nTotalWidth;
for (int i = 0; i < nCols; i++)
{ //采用逐行4舍5入的方式解决不能整除的问题.
if (SLayoutSize::fequal(totalColsWeight, 0.0f))
break;
int extra = int(nRemain * pColsWeight[i] / totalColsWeight + 0.5f);
pCellsWidth[i] += extra;
nRemain -= extra;
totalColsWeight -= pColsWeight[i];
}
}
}else if(xInter<0){
int nRemain = rcParent.Width() - nTotalWidth;
if(xInter==SIZE_WRAP_CONTENT && nCols>1)
xInter=nRemain/(nCols-1);
else if(xInter==SIZE_MATCH_PARENT)
{
xInter=nRemain/(nCols+1);
rcParent.DeflateRect(xInter,0,xInter,0);
}
else
xInter=0;
}
if(totalRowsWeight > 0.0f){
int netParentHei = rcParent.Height() - (nRows - 1) * (yInter>0?yInter:0);
if (nTotalHeight < netParentHei)
{
int nRemain = netParentHei - nTotalHeight;
for (int i = 0; i < nRows; i++)
{ //采用逐行4舍5入的方式解决不能整除的问题.
if (SLayoutSize::fequal(totalRowsWeight, 0.0f))
break;
int extra = int(nRemain * pRowsWeight[i] / totalRowsWeight + 0.5f);
pCellsHeight[i] += extra;
nRemain -= extra;
totalRowsWeight -= pRowsWeight[i];
}
}
}else if(yInter<0){
int nRemain = rcParent.Height() - nTotalHeight;
if(yInter==SIZE_WRAP_CONTENT && nRows>1)
yInter=nRemain/(nRows-1);
else if(yInter==SIZE_MATCH_PARENT)
{
yInter=nRemain/(nRows+1);
rcParent.DeflateRect(0,yInter,0,yInter);
}
else
yInter=0;
}
if (totalColsWeight > 0.0f)
{
int netParentWid = rcParent.Width() - (nCols - 1) * (xInter > 0 ? xInter : 0);
if (nTotalWidth < netParentWid)
{
int nRemain = netParentWid - nTotalWidth;
for (int i = 0; i < nCols; i++)
{ //采用逐行4舍5入的方式解决不能整除的问题.
if (SLayoutSize::fequal(totalColsWeight, 0.0f))
break;
int extra = int(nRemain * pColsWeight[i] / totalColsWeight + 0.5f);
pCellsWidth[i] += extra;
nRemain -= extra;
totalColsWeight -= pColsWeight[i];
}
}
}
else if (xInter < 0)
{
int nRemain = rcParent.Width() - nTotalWidth;
if (xInter == SIZE_WRAP_CONTENT && nCols > 1)
xInter = nRemain / (nCols - 1);
else if (xInter == SIZE_MATCH_PARENT)
{
xInter = nRemain / (nCols + 1);
rcParent.DeflateRect(xInter, 0, xInter, 0);
}
else
xInter = 0;
}
if (totalRowsWeight > 0.0f)
{
int netParentHei = rcParent.Height() - (nRows - 1) * (yInter > 0 ? yInter : 0);
if (nTotalHeight < netParentHei)
{
int nRemain = netParentHei - nTotalHeight;
for (int i = 0; i < nRows; i++)
{ //采用逐行4舍5入的方式解决不能整除的问题.
if (SLayoutSize::fequal(totalRowsWeight, 0.0f))
break;
int extra = int(nRemain * pRowsWeight[i] / totalRowsWeight + 0.5f);
pCellsHeight[i] += extra;
nRemain -= extra;
totalRowsWeight -= pRowsWeight[i];
}
}
}
else if (yInter < 0)
{
int nRemain = rcParent.Height() - nTotalHeight;
if (yInter == SIZE_WRAP_CONTENT && nRows > 1)
yInter = nRemain / (nRows - 1);
else if (yInter == SIZE_MATCH_PARENT)
{
yInter = nRemain / (nRows + 1);
rcParent.DeflateRect(0, yInter, 0, yInter);
}
else
yInter = 0;
}

delete[] pColsWeight;
delete[] pRowsWeight;
Expand Down
145 changes: 74 additions & 71 deletions SOUI/src/layout/SLinearLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,76 +281,79 @@ void SLinearLayout::LayoutChildren(IWindow *pParent)
}

int size = m_orientation == Vert ? rcParent.Height() : rcParent.Width();
int interval = m_interval.toPixelSize(pParent->GetScale());
if(interval>0){
offset += (nChilds-1)*interval;
}
int interval = m_interval.toPixelSize(pParent->GetScale());
if (interval > 0)
{
offset += (nChilds - 1) * interval;
}
ORIENTATION orienOther = m_orientation == Vert ? Horz : Vert;
if(size > offset)
{
if (fWeight > 0.0f)
{ // assign size by weight
int nRemain = size - offset;

for (int iChild = 0; iChild < nChilds; iChild++)
{
if (SLayoutSize::fequal(fWeight, 0.0f))
break;
IWindow *pChild = pChilds[iChild];
SLinearLayoutParam *pLinearLayoutParam = (SLinearLayoutParam *)pChild->GetLayoutParam();
int nScale = pChild->GetScale();
if (pLinearLayoutParam->weight > 0.0f)
{
int extra = int(nRemain * pLinearLayoutParam->weight / fWeight + 0.5f);
LONG &szChild = m_orientation == Vert ? pSize[iChild].cy : pSize[iChild].cx;
szChild += extra;
nRemain -= extra;
fWeight -= pLinearLayoutParam->weight;

if (pLinearLayoutParam->IsWrapContent(orienOther))
{
ILayoutParam *backup = pLinearLayoutParam->Clone();
pLinearLayoutParam->SetSpecifiedSize(m_orientation, SLayoutSize((float)szChild, SLayoutSize::dp));
int nWid = pSize[iChild].cx, nHei = pSize[iChild].cy;

if (orienOther == Vert)
nHei = SIZE_WRAP_CONTENT;
else
nWid = SIZE_WRAP_CONTENT;

CSize szCalc;
pChild->GetDesiredSize(&szCalc, nWid, nHei);
if (orienOther == Vert)
{
szCalc.cy += pLinearLayoutParam->extend_top.toPixelSize(nScale) + pLinearLayoutParam->extend_bottom.toPixelSize(nScale);
pSize[iChild].cy = szCalc.cy;
}
else
{
szCalc.cx += pLinearLayoutParam->extend_left.toPixelSize(nScale) + pLinearLayoutParam->extend_right.toPixelSize(nScale);
pSize[iChild].cx = szCalc.cx;
}
pChild->SetLayoutParam(backup);
backup->Release();
}
}
}
}else if(interval<0){
//handle interval==-1 or interval==-2
if(interval==SIZE_WRAP_CONTENT && nChilds>1)
interval=(size-offset)/(nChilds-1);
else if(interval==SIZE_MATCH_PARENT)
{
interval = (size-offset)/(nChilds+1);
if(m_orientation == Vert)
rcParent.DeflateRect(0,interval,0,interval);
else
rcParent.DeflateRect(interval,0,interval,0);
}
else
interval = 0;
}
}
if (size > offset)
{
if (fWeight > 0.0f)
{ // assign size by weight
int nRemain = size - offset;

for (int iChild = 0; iChild < nChilds; iChild++)
{
if (SLayoutSize::fequal(fWeight, 0.0f))
break;
IWindow *pChild = pChilds[iChild];
SLinearLayoutParam *pLinearLayoutParam = (SLinearLayoutParam *)pChild->GetLayoutParam();
int nScale = pChild->GetScale();
if (pLinearLayoutParam->weight > 0.0f)
{
int extra = int(nRemain * pLinearLayoutParam->weight / fWeight + 0.5f);
LONG &szChild = m_orientation == Vert ? pSize[iChild].cy : pSize[iChild].cx;
szChild += extra;
nRemain -= extra;
fWeight -= pLinearLayoutParam->weight;

if (pLinearLayoutParam->IsWrapContent(orienOther))
{
ILayoutParam *backup = pLinearLayoutParam->Clone();
pLinearLayoutParam->SetSpecifiedSize(m_orientation, SLayoutSize((float)szChild, SLayoutSize::dp));
int nWid = pSize[iChild].cx, nHei = pSize[iChild].cy;

if (orienOther == Vert)
nHei = SIZE_WRAP_CONTENT;
else
nWid = SIZE_WRAP_CONTENT;

CSize szCalc;
pChild->GetDesiredSize(&szCalc, nWid, nHei);
if (orienOther == Vert)
{
szCalc.cy += pLinearLayoutParam->extend_top.toPixelSize(nScale) + pLinearLayoutParam->extend_bottom.toPixelSize(nScale);
pSize[iChild].cy = szCalc.cy;
}
else
{
szCalc.cx += pLinearLayoutParam->extend_left.toPixelSize(nScale) + pLinearLayoutParam->extend_right.toPixelSize(nScale);
pSize[iChild].cx = szCalc.cx;
}
pChild->SetLayoutParam(backup);
backup->Release();
}
}
}
}
else if (interval < 0)
{
// handle interval==-1 or interval==-2
if (interval == SIZE_WRAP_CONTENT && nChilds > 1)
interval = (size - offset) / (nChilds - 1);
else if (interval == SIZE_MATCH_PARENT)
{
interval = (size - offset) / (nChilds + 1);
if (m_orientation == Vert)
rcParent.DeflateRect(0, interval, 0, interval);
else
rcParent.DeflateRect(interval, 0, interval, 0);
}
else
interval = 0;
}
}
{ // assign position
offset = 0;
for (int iChild = 0; iChild < nChilds; iChild++)
Expand Down Expand Up @@ -521,8 +524,8 @@ SIZE SLinearLayout::MeasureChildren(const IWindow *pParent, int nWidth, int nHei

int size = m_orientation == Vert ? nHeight : nWidth;
int nInterval = m_interval.toPixelSize(pParent->GetScale());
if(nInterval<0)
nInterval = 0;
if (nInterval < 0)
nInterval = 0;
if (!SLayoutSize::fequal(fWeight, 0.0f) && size != SIZE_WRAP_CONTENT)
{ // assign weight for elements. and calc size of other orientation again.
int offset = 0;
Expand Down

0 comments on commit 0c37aae

Please sign in to comment.