canvas: several steps further with box packing and size allocation

This commit is contained in:
Paul Davis 2021-08-03 23:21:55 -06:00
parent 88b95bc8f8
commit b416caf1bb
13 changed files with 75 additions and 80 deletions

View file

@ -147,12 +147,21 @@ Box::set_homogenous (bool yn)
}
void
Box::size_allocate (Rect const & alloc)
Box::_size_allocate (Rect const & alloc)
{
_position = Duple (alloc.x0, alloc.y0);
_allocation = alloc;
Rect old_alloc (_allocation);
Rectangle::_size_allocate (alloc);
reposition_children (alloc.width(), alloc.height());
bool width_shrinking = (old_alloc.width() > alloc.width());
bool height_shrinking = (old_alloc.height() > alloc.height());
reposition_children (alloc.width(), alloc.height(), width_shrinking, height_shrinking);
}
void
Box::size_allocate_children (Rect const &)
{
/* do nothing here */
}
void
@ -166,11 +175,11 @@ Box::size_request (Distance& w, Distance& h) const
if (homogenous) {
for (std::list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
Rect bb = (*i)->bounding_box();
if (bb) {
largest_height = std::max (largest_height, bb.height());
largest_width = std::max (largest_width, bb.width());
}
Distance iw, ih;
(*i)->size_request (iw, ih);
largest_height = std::max (largest_height, ih);
largest_width = std::max (largest_width, iw);
}
uniform_size = Rect (0, 0, largest_width, largest_height);
@ -205,8 +214,6 @@ Box::size_request (Distance& w, Distance& h) const
isize = Rect (previous_edge.x, previous_edge.y, previous_edge.x + width, previous_edge.y + height);
}
std::cerr << "\tset " << (*i)->whoami() << " to " << isize << std::endl;
width = isize.width();
height = isize.height();
@ -254,7 +261,7 @@ Box::size_request (Distance& w, Distance& h) const
}
void
Box::reposition_children (Distance width, Distance height)
Box::reposition_children (Distance width, Distance height, bool shrink_width, bool shrink_height)
{
Duple previous_edge = Duple (left_margin+left_padding, top_margin+top_padding);
@ -262,40 +269,39 @@ Box::reposition_children (Distance width, Distance height)
Distance largest_height = 0;
Rect uniform_size;
std::cerr << "\n\n\n\n\nREPO C WITHIN " << width << " x " << height << std::endl;
PBD::stacktrace (std::cerr, 20);
if (homogenous) {
for (std::list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
Rect bb = (*i)->bounding_box();
if (bb) {
largest_height = std::max (largest_height, bb.height());
largest_width = std::max (largest_width, bb.width());
Distance iw, ih;
(*i)->size_request (iw, ih);
if (!shrink_height) {
largest_height = std::max (largest_height, ih);
}
if (!shrink_width) {
largest_width = std::max (largest_width, iw);
}
}
const Distance contents_width = width - (left_margin + left_padding + right_margin + right_padding);
const Distance contents_height = height - (top_margin + top_padding + bottom_margin + bottom_padding);
const Distance item_width = (contents_width - ((_items.size() - 1) * spacing)) / _items.size();
const Distance item_height = (contents_height - ((_items.size() - 1) * spacing)) / _items.size();
const Distance item_width = (contents_width - ((_items.size() - 1) * spacing));
const Distance item_height = (contents_height - ((_items.size() - 1) * spacing));;
if (orientation == Vertical && (largest_width < item_width)) {
largest_width = item_width;
std::cerr << "Vertbox, use width " << width << " for largest (iw " << item_width << ")\n";
if (orientation == Vertical) {
if ((largest_width < item_width)) {
largest_width = item_width;
}
}
if (orientation == Horizontal && (largest_height < item_height)) {
largest_height = item_height;
std::cerr << "Hozbox, use height " << height << " for largest (ih " << item_height << ")\n";
if (orientation == Horizontal) {
if ((largest_height < item_height)) {
largest_height = item_height;
}
}
uniform_size = Rect (0, 0, largest_width, largest_height);
std::cerr << "\tuniform size: " << uniform_size << std::endl;
}
Rect r;
{
PBD::Unwinder<bool> uw (ignore_child_changes, true);
@ -323,15 +329,11 @@ Box::reposition_children (Distance width, Distance height)
isize = Rect (previous_edge.x, previous_edge.y, previous_edge.x + width, previous_edge.y + height);
}
std::cerr << "\tset " << (*i)->whoami() << " to " << isize << std::endl;
(*i)->size_allocate (isize);
width = isize.width();
height = isize.height();
r = r.extend (Rect (previous_edge.x, previous_edge.y, previous_edge.x + width, previous_edge.y + height));
if (orientation == Vertical) {
Distance shift = 0;
@ -364,10 +366,6 @@ Box::reposition_children (Distance width, Distance height)
}
}
}
/* left and top margins+padding already reflected in child bboxes */
r = r.expand (0, right_margin + right_padding, bottom_margin + bottom_padding, 0);
}
void
@ -400,8 +398,7 @@ Box::layout ()
Item::layout ();
if (yes_do_it) {
std::cerr << "LAYOUT with " << _allocation << std::endl;
reposition_children (_allocation.width(), _allocation.height());
reposition_children (_allocation.width(), _allocation.height(), false, false);
}
}
@ -416,7 +413,7 @@ Box::child_changed (bool bbox_changed)
Item::child_changed (bbox_changed);
reposition_children (_allocation.width(), _allocation.height());
reposition_children (_allocation.width(), _allocation.height(), false, false);
}
void
@ -424,7 +421,7 @@ Box::set_collapse_on_hide (bool yn)
{
if (collapse_on_hide != yn) {
collapse_on_hide = yn;
reposition_children (_allocation.width(), _allocation.height());
reposition_children (_allocation.width(), _allocation.height(), false, false);
}
}