diff --git a/src/Painter.hpp b/src/Painter.hpp index 03819d0..82c96c6 100644 --- a/src/Painter.hpp +++ b/src/Painter.hpp @@ -31,6 +31,8 @@ public: bool text(Point &where, std::string &text); bool text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize); + bool can_paint() { if (m_cairo) return true; else return false; } + void source_rgb(RGB &source_rgb); void fill(); diff --git a/src/Widget.cpp b/src/Widget.cpp index 4929a08..5bb2939 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "Widget.hpp" #include "Events.hpp" #include "Window.hpp" @@ -21,13 +22,16 @@ void Widget::set_layout(std::shared_ptr layout) { } void Widget::set_window(Window *window) { + if (!window) + throw std::logic_error{"null window value for set_window"}; + m_window = window; m_styles = m_window->get_top_level_styles(); on_init(); } -bool Widget::add_child(Widget *child) { +bool Widget::add_child(std::shared_ptr child) { if (child->get_parent()) { return false; } @@ -40,7 +44,7 @@ bool Widget::add_child(Widget *child) { return true; } -void Widget::remove_child(Widget *child) { +void Widget::remove_child(std::shared_ptr child) { m_children.erase(std::remove(m_children.begin(), m_children.end(), child), m_children.end()); } @@ -97,6 +101,10 @@ void Widget::handle_repaint(WidgetRepaintRequestedEvent &event) { return; auto painter = m_window->get_painter(); + + if (!painter.can_paint()) + return; + auto cr = painter.get_cairo(); auto event_should_do_group = event.should_do_group(); diff --git a/src/Widget.hpp b/src/Widget.hpp index 1098081..6ff82eb 100644 --- a/src/Widget.hpp +++ b/src/Widget.hpp @@ -34,7 +34,7 @@ DEF_WIDGET_STYLE_PROP(background_border_radius, double, 0.0) private: Box m_current_geometry { 0, 0, 0, 0 }; - std::vector m_children; + std::vector> m_children; Widget *m_parent { nullptr }; Window *m_window { nullptr }; std::shared_ptr m_styles { nullptr }; @@ -59,9 +59,9 @@ public: void set_current_geometry(Box current_geometry) { m_current_geometry = current_geometry; wants_full_relayout(); } void set_current_geometry(Box current_geometry, bool is_pure) { m_current_geometry = current_geometry; if (!is_pure) { wants_full_relayout(); } } - std::vector &get_children() { return m_children; } - bool add_child(Widget *child); - void remove_child(Widget *child); + std::vector> &get_children() { return m_children; } + bool add_child(std::shared_ptr child); + void remove_child(std::shared_ptr child); Widget *get_parent() { return m_parent; } void set_parent(Widget *parent) { m_parent = parent; } @@ -97,11 +97,18 @@ public: template std::shared_ptr set_layout(Args&&... args) { - std::shared_ptr layout = std::make_shared(std::forward(args)...); + std::shared_ptr layout = std::make_shared(std::forward(args)...); set_layout(layout); return layout; } + template + std::shared_ptr add_child(Args&&... args) { + std::shared_ptr child = std::make_shared(std::forward(args)...); + add_child(child); + return child; + } + virtual ~Widget() {}; protected: WidgetType m_type { WidgetType::Widget }; diff --git a/src/Window.cpp b/src/Window.cpp index 1bcebed..55b2f41 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -13,7 +13,7 @@ namespace Raven { -void Window::set_main_widget(Widget *main_widget) { +void Window::set_main_widget(std::shared_ptr main_widget) { m_main_widget = main_widget; m_main_widget->set_window(this); m_main_widget->set_current_geometry(m_current_geometry); diff --git a/src/Window.hpp b/src/Window.hpp index 8d2a5bd..7494299 100644 --- a/src/Window.hpp +++ b/src/Window.hpp @@ -13,7 +13,7 @@ class Window { private: Widget *m_focused_widget { nullptr }; Widget *m_active_widget { nullptr }; - Widget *m_main_widget { nullptr }; + std::shared_ptr m_main_widget { nullptr }; Box m_current_geometry { 0, 0, 800, 600 }; Painter m_painter {}; std::shared_ptr m_top_level_styles = std::make_shared(this); @@ -31,8 +31,8 @@ public: Widget *get_active_widget() { return m_active_widget; } void set_active_widget(Widget *active_widget) { m_active_widget = active_widget; } - Widget *get_main_widget() { return m_main_widget; } - void set_main_widget(Widget *main_widget); + std::shared_ptr get_main_widget() { return m_main_widget; } + void set_main_widget(std::shared_ptr main_widget); std::shared_ptr get_top_level_styles() { return m_top_level_styles; } @@ -48,6 +48,13 @@ public: bool spawn_window(); void run(bool block); + + template + std::shared_ptr set_main_widget(Args&&... args) { + std::shared_ptr widget = std::make_shared(std::forward(args)...); + set_main_widget(widget); + return widget; + } }; } diff --git a/src/main.cpp b/src/main.cpp index 8883816..389e659 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,42 +8,34 @@ #include "src/DocumentLayout.hpp" int main() { - Raven::Widget new_row { Raven::ControlWidgetType::NewRow }; Raven::Window window {}; - Raven::Widget main_widget {}; - Raven::Button add_button {"add"}; - Raven::Button subtract_button {"subtract"}; - Raven::Label label {"0"}; + auto main_widget = window.set_main_widget(); + main_widget->set_layout(6.0); + + auto add_button = main_widget->add_child("add"); + auto subtract_button = main_widget->add_child("subtract"); + main_widget->add_child(Raven::ControlWidgetType::NewRow); + auto label = main_widget->add_child("0"); int number = 0; window.spawn_window(); - add_button.set_current_geometry(Raven::Box(0, 0, 100, 30)); - subtract_button.set_current_geometry(Raven::Box(0, 0, 100, 30)); - label.set_current_geometry(Raven::Box(0, 0, 100, 20)); + add_button->set_current_geometry(Raven::Box(0, 0, 100, 30)); + subtract_button->set_current_geometry(Raven::Box(0, 0, 100, 30)); + label->set_current_geometry(Raven::Box(0, 0, 100, 20)); - main_widget.set_layout(6.0); - - window.set_main_widget(&main_widget); - - add_button.on_click = [&]() { + add_button->on_click = [&]() { number++; - label.set_text(std::to_string(number)); + label->set_text(std::to_string(number)); }; - subtract_button.on_click = [&]() { + subtract_button->on_click = [&]() { number--; - label.set_text(std::to_string(number)); + label->set_text(std::to_string(number)); }; - main_widget.add_child(&add_button); - main_widget.add_child(&subtract_button); - main_widget.add_child(&new_row); - main_widget.add_child(&label); - - window.run(true); return 0; } \ No newline at end of file