From 96a77393417131a9ef5a0882b44e63f1f6794a3b Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Wed, 15 Jun 2022 04:44:37 +0300 Subject: [PATCH] add horizontal box layout --- src/HorizontalBoxLayout.cpp | 41 +++++++++++++++++++++++++++++++++++++ src/HorizontalBoxLayout.hpp | 27 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/HorizontalBoxLayout.cpp create mode 100644 src/HorizontalBoxLayout.hpp diff --git a/src/HorizontalBoxLayout.cpp b/src/HorizontalBoxLayout.cpp new file mode 100644 index 0000000..8c09728 --- /dev/null +++ b/src/HorizontalBoxLayout.cpp @@ -0,0 +1,41 @@ +#include "HorizontalBoxLayout.hpp" +#include "Point.hpp" +#include "Widget.hpp" + +namespace Raven { + +void HorizontalBoxLayout::run() { + if (!m_target) { + return; + } + + Point current_point { m_margin, m_margin }; + double max_height_so_far = 0; + double requested_width = 0; + + auto& children = m_target->children(); + for (auto child : children) { + if (child->absolute()) { + continue; + } + + if (child->rect().height() > max_height_so_far) { + max_height_so_far = child->rect().height(); + } + + requested_width += child->rect().width() + m_margin; + + child->rect().set_x(current_point.x()); + child->rect().set_y(current_point.y()); + + //std::cout << child->rect().x() << ", " << child->rect().y() << ", " << child->rect().width() << ", " << child->rect().height() << std::endl; + + current_point.add(child->rect().width() + m_margin, 0); + } + + m_target->rect().set_width(requested_width + m_margin); + m_target->rect().set_height(max_height_so_far + m_margin * 2); +} + +} + diff --git a/src/HorizontalBoxLayout.hpp b/src/HorizontalBoxLayout.hpp new file mode 100644 index 0000000..e0685af --- /dev/null +++ b/src/HorizontalBoxLayout.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "Layout.hpp" + +namespace Raven { + +class HorizontalBoxLayout : public Layout { +private: + double m_margin { 0.0 }; +public: + HorizontalBoxLayout() + : Layout() {} + + HorizontalBoxLayout(double margin) + : Layout() + , m_margin(margin) {} + + void run(); + + double margin() { return m_margin; } + void set_margin(double margin) { m_margin = margin; run(); } + + virtual ~HorizontalBoxLayout() {} +}; + +} +