I’ve been working a lot with ListBox lately and have come across something that has confused me. While reading the source for ListBox::getRowPosition(), I got a little confused at the intention behind the relativeToComponentTopLeft parameter. Here’s the source for reference:
Rectangle<int> ListBox::getRowPosition (int rowNumber, bool relativeToComponentTopLeft) const noexcept
{
auto y = viewport->getY() + rowHeight * rowNumber;
if (relativeToComponentTopLeft)
y -= viewport->getViewPositionY();
return { viewport->getX(), y,
viewport->getViewedComponent()->getWidth(), rowHeight };
}
Obviously, when relativeToComponentTopLeft is true, the value you get is the position of the row relative to the top-left of the ListBox component itself. However, what confuses me is the value returned if you set that argument to false. Because the returned value still incorporates viewport->getY(), you’re getting the y-coordinate of the row relative to the top of the Viewport’s scrollable child component, plus the y-coordinate of the Viewport itself relative to the ListBox?
I’m just trying to understand what use this value could be so that I can better understand the design of the class as a whole. As far as I see it, the only values you’d want are:
- The value returned when
relativeToComponentTopLeftistrue, i.e. row position relative toListBox - The position of the row relative to the top of the viewport child, i.e.
rowHeight * rowNumber
I just can’t puzzle out why you’d want the second value added to viewport->getY(). Thanks in advance for any guidance!