73 lines
1.9 KiB
C++
73 lines
1.9 KiB
C++
|
/**
|
||
|
* @file Layout.cpp
|
||
|
* @author apio (cloudapio.eu)
|
||
|
* @brief Layout widgets to organize content.
|
||
|
*
|
||
|
* @copyright Copyright (c) 2023, the Luna authors.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
#include <ui/Layout.h>
|
||
|
|
||
|
namespace ui
|
||
|
{
|
||
|
HorizontalLayout::HorizontalLayout(AdjustHeight adjust_height, AdjustWidth adjust_width)
|
||
|
: m_adjust_height(adjust_height), m_adjust_width(adjust_width)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
Result<EventResult> HorizontalLayout::handle_mousemove(Point position)
|
||
|
{
|
||
|
for (auto widget : m_widgets)
|
||
|
{
|
||
|
if (widget->rect().contains(position)) return widget->handle_mousemove(position);
|
||
|
}
|
||
|
|
||
|
return ui::EventResult::DidNotHandle;
|
||
|
}
|
||
|
|
||
|
Result<void> HorizontalLayout::draw(Canvas& canvas)
|
||
|
{
|
||
|
for (auto widget : m_widgets)
|
||
|
{
|
||
|
ui::Rect rect = { m_rect.relative(widget->rect().pos), widget->rect().width, widget->rect().height };
|
||
|
auto subcanvas = canvas.subcanvas(rect);
|
||
|
TRY(widget->draw(subcanvas));
|
||
|
}
|
||
|
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
Result<void> HorizontalLayout::add_widget(Widget& widget)
|
||
|
{
|
||
|
TRY(m_widgets.try_append(&widget));
|
||
|
|
||
|
if (m_adjust_width == AdjustWidth::No)
|
||
|
{
|
||
|
widget.rect().pos.x = m_rect.pos.x + m_used_width;
|
||
|
m_used_width += widget.rect().width;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
int used_width = 0;
|
||
|
div_t result = div(m_rect.width, (int)m_widgets.size());
|
||
|
for (auto w : m_widgets)
|
||
|
{
|
||
|
w->rect().pos.x = m_rect.pos.x + used_width;
|
||
|
w->rect().width = result.quot;
|
||
|
used_width += result.quot;
|
||
|
}
|
||
|
m_widgets[m_widgets.size() - 1]->rect().width += result.rem;
|
||
|
}
|
||
|
|
||
|
widget.rect().pos.y = m_rect.pos.y;
|
||
|
|
||
|
if (m_adjust_height == AdjustHeight::Yes) { widget.rect().height = m_rect.height; }
|
||
|
|
||
|
widget.set_parent(this);
|
||
|
|
||
|
return {};
|
||
|
}
|
||
|
}
|