StretchableLayoutManager() and ResizerBar problem


#1

in Constructor

[code] layoutManager = new StretchableLayoutManager();
layoutManager->setItemLayout (0,
50, 10000,
-0.66);

layoutManager->setItemLayout (1,      
	150, 150,    
	150);      

layoutManager->setItemLayout (2,          
	8,8,8);        


layoutManager->setItemLayout (3,          
	50, 10000, 
	-0.34);        

layoutBar = new StretchableLayoutResizerBar(layoutManager,2,true);
addAndMakeVisible(layoutBar);

[/code]

in resized()

Component* comps[] = { cellEditor,masterFader,layoutBar,fileBrowser  };
layoutManager->layOutComponents(comps, 4, 0, 0, getWidth(), getHeight(),false,true);

Every time i drag the ResizerBar, it jumps immediately ca. 100 pixels to the left. The more left the resizer bar is, the more will it jump. When its reached its maximum left position, i need to drag the mouse ca. 150 to the right side, bevore something is changed.

Have I made a mistake?


#2

Can’t see any obvious bloopers in there…


#3

it took me 3 hours to understand how the algorithm works, this should fix the bug :slight_smile:

[code]int StretchableLayoutManager::fitComponentsIntoSpace (const int startIndex,
const int endIndex,
const int availableSpace,
int startPos)
{
// calculate the total sizes
int i;
double totalIdealSize = 0.0;
int totalMinimums = 0;

for (i = startIndex; i < endIndex; ++i)
{
    ItemLayoutProperties* const layout = items.getUnchecked (i);

    layout->currentSize = sizeToRealSize (layout->minSize, totalSize);

    totalMinimums += layout->currentSize;
   //delete  totalIdealSize += sizeToRealSize (layout->preferredSize, availableSpace);
   /*add*/	 totalIdealSize += sizeToRealSize (layout->preferredSize, totalSize);
}

if (totalIdealSize <= 0)
    totalIdealSize = 1.0;

// now calc the best sizes..
int extraSpace = availableSpace - totalMinimums;

while (extraSpace > 0)
{
    int numWantingMoreSpace = 0;
    int numHavingTakenExtraSpace = 0;

    // first figure out how many comps want a slice of the extra space..
    for (i = startIndex; i < endIndex; ++i)
    {
        ItemLayoutProperties* const layout = items.getUnchecked (i);

//delete double sizeWanted = sizeToRealSize (layout->preferredSize, availableSpace);
/add/ double sizeWanted = sizeToRealSize (layout->preferredSize, totalSize);

        const int bestSize = jlimit (layout->currentSize,
                                     jmax (layout->currentSize,
                                           sizeToRealSize (layout->maxSize, totalSize)),
                                     roundToInt (sizeWanted * availableSpace / totalIdealSize));
					

        if (bestSize > layout->currentSize)
            ++numWantingMoreSpace;
    }

    // ..share out the extra space..
    for (i = startIndex; i < endIndex; ++i)
    {
        ItemLayoutProperties* const layout = items.getUnchecked (i);

     //delete	double sizeWanted = sizeToRealSize (layout->preferredSize, availableSpace);
	 /*add*/    double sizeWanted = sizeToRealSize (layout->preferredSize, totalSize);
			


        int bestSize = jlimit (layout->currentSize,
                               jmax (layout->currentSize, sizeToRealSize (layout->maxSize, totalSize)),
                                roundToInt (sizeWanted * availableSpace / totalIdealSize));

        const int extraWanted = bestSize - layout->currentSize;

        if (extraWanted > 0)
        {
            const int extraAllowed = jmin (extraWanted,
                                            extraSpace / jmax (1, numWantingMoreSpace));

            if (extraAllowed > 0)
            {
                ++numHavingTakenExtraSpace;
                --numWantingMoreSpace;

                layout->currentSize += extraAllowed;
                extraSpace -= extraAllowed;
            }
        }
    }

    if (numHavingTakenExtraSpace <= 0)
        break;
}

// ..and calculate the end position
for (i = startIndex; i < endIndex; ++i)
{
    ItemLayoutProperties* const layout = items.getUnchecked(i);
    startPos += layout->currentSize;
}

return startPos;

}
[/code]


#4

(That class was an absolute bastard to write, I must admit…)

I actually can’t remember or understand enough of how it works to really be confident that your suggestion’s correct, but I’ll try it out and see if it seems to break anything!