diff --git a/src/Layout.cpp b/src/Layout.cpp index d93fc83..db8458c 100644 --- a/src/Layout.cpp +++ b/src/Layout.cpp @@ -6,14 +6,17 @@ namespace Raven { -void Layout::run_on(Widget *target) { - Point point { target->get_current_geometry().get_x(), target->get_current_geometry().get_y() }; +void Layout::run() { + if (!m_target) + return; + + Point point { m_target->get_current_geometry().get_x() + m_margin, m_target->get_current_geometry().get_y() + m_margin }; double row_largest_height = 0.0; - auto children = target->get_children(); + auto children = m_target->get_children(); for (auto& c : children) { if (c->get_control_type() == ControlWidgetType::NewRow && row_largest_height) { - point.add(0, row_largest_height); + point.add(0, row_largest_height + m_margin); row_largest_height = 0.0; point.set_x(0); } @@ -22,7 +25,7 @@ void Layout::run_on(Widget *target) { } auto new_geometry = Box{ point.get_x(), point.get_y(), c->get_current_geometry().get_width(), c->get_current_geometry().get_height() }; c->set_current_geometry(std::move(new_geometry)); - point.add(c->get_current_geometry().get_width(), 0); + point.add(c->get_current_geometry().get_width() + m_margin, 0); } } diff --git a/src/Layout.hpp b/src/Layout.hpp index 3ec74e7..cfba785 100644 --- a/src/Layout.hpp +++ b/src/Layout.hpp @@ -5,10 +5,19 @@ namespace Raven { class Layout { +private: + double m_margin { 0.0 }; + Widget *m_target { nullptr }; public: Layout() {} - void run_on(Widget *target); + void run(); + + double get_margin() { return m_margin; } + void set_margin(double margin) { m_margin = margin; run(); } + + void bind_to(Widget *target) { m_target = target; } + Widget *get_target() { return m_target; } }; } \ No newline at end of file diff --git a/src/Widget.cpp b/src/Widget.cpp index b84ccb6..4dfcfb0 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -8,9 +8,13 @@ namespace Raven { void Widget::do_layout() { - if (m_layout) { - m_layout->run_on(this); - } + if (m_layout) + m_layout->run(); +} + +void Widget::set_layout(Layout *layout) { + m_layout = layout; + m_layout->bind_to(this); } void Widget::set_window(Window *window) { diff --git a/src/Widget.hpp b/src/Widget.hpp index 3afcae3..fd00efe 100644 --- a/src/Widget.hpp +++ b/src/Widget.hpp @@ -83,7 +83,7 @@ public: bool get_accepts_events() { return m_accepts_events; } void set_accepts_events(bool accepts_events) { m_accepts_events = accepts_events; } - void set_layout(Layout *layout) { m_layout = layout; } + void set_layout(Layout *layout); Layout *get_layout() { return m_layout; } WidgetType get_type() { return m_type; } diff --git a/src/main.cpp b/src/main.cpp index 618d13f..0a0cfd9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,6 +23,8 @@ int main() { impostor_button.set_current_geometry(Raven::Box(0, 0, 100, 30)); label.set_current_geometry(Raven::Box(0, 0, 100, 20)); + main_layout.set_margin(6.0); + main_widget.set_layout(&main_layout); window.set_main_widget(&main_widget);