AutoLayout

article removed

I would like to see more Layout class like that.
Great.

Is there a reason why you do the layout in the destructor in stead of doing it in the constructor and removing the destructor ?

[quote=“otristan”]I would like to see more Layout class like that.
Great.
Is there a reason why you do the layout in the destructor in stead of doing it in the constructor and removing the destructor ?[/quote]

Thank you. No special reason. There are a lot of variations possible. I am just working to get this little class more complicated and tough to use.
:evil:

With these lines added, you can preserve the existing height of the child components:

[code]
VerticalAutoLayout::~VerticalAutoLayout()
{
int count = parent->getNumChildComponents();
int x=gap;
int y=gap;
int w = parent->getWidth()-gap-gap;
int h = parent->getHeight()-gap-gap;

int ypos=y;
for(int i=0;i<count;i++)
{
	Component* comp = parent->getChildComponent(i);				
	if(comp)
	{
		// choose existing height or fixed height given in ctor
		int hh = child_h == -1 ? comp->getHeight() : child_h;
		comp->setBounds(x, ypos, w, hh);
		ypos += hh+gap;
	}
}

}[/code]

Just give a height of -1 in the ctor.

Why do it as a class…? Looks like a perfect candidate for a pure static function.

This derived class will align the controls and also will connect the button edges automagically. You even can mix buttons and other controls in the strip, the ConnectedEdgeFlags will be set properly.
:smiley:

Usage

[code]void CommandPanel::resized()
{
HorizontalButtonLayout layout( this);

}[/code]

H

[code]class HorizontalButtonLayout : public HorizontalAutoLayout
{
public:
HorizontalButtonLayout ( Component* parent_, int w=-1);
~HorizontalButtonLayout();

private:
	Button* getButton( int i );
};[/code]

CPP

[code]HorizontalButtonLayout::HorizontalButtonLayout ( Component* parent_, int w)
: HorizontalAutoLayout(parent_, w )
{
}

Button* HorizontalButtonLayout::getButton( int i )
{
Button* btn = NULL;
int count = parent->getNumChildComponents();
if( i>= 0 && i<count)
{
Component* comp = parent->getChildComponent(i);
if(comp)
{
btn = dynamic_cast<Button*> (comp );
}
}
return btn;
}

HorizontalButtonLayout::~HorizontalButtonLayout()
{
int count = parent->getNumChildComponents();

for(int i=0;i<count;i++)
{
	Button* btn = getButton(i);
	
	if( btn )
	{
		Button* prev_btn = getButton(i-1);
		Button* next_btn = getButton(i+1);
		int flags = 0;
		
		if(prev_btn)
			flags += Button::ConnectedOnLeft;
		
		if(next_btn)
			flags += Button::ConnectedOnRight;
		
		btn->setConnectedEdges(flags);
		
	}
	
}

}[/code]