From b0315b76beaf7a7c7ab019624ecd96c9784275fd Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Tue, 5 Jul 2022 20:07:09 +0300 Subject: [PATCH] add scrolling widgets --- meson.build | 1 + src/Box.cpp | 5 +++++ src/Box.hpp | 2 ++ src/Events.hpp | 8 +++++++- src/Styles.cpp | 13 ++++++++++++- src/Styles.hpp | 1 + src/Widget.cpp | 18 ++++++++---------- src/Widget.hpp | 3 +-- src/Window.cpp | 4 ++-- src/main.cpp | 23 ++++++++++++----------- 10 files changed, 51 insertions(+), 27 deletions(-) diff --git a/meson.build b/meson.build index d4e6e84..36e45e4 100644 --- a/meson.build +++ b/meson.build @@ -11,6 +11,7 @@ executable( './src/Painter.cpp', './src/Window.cpp', './src/Widget.cpp', + './src/ScrollContainer.cpp', './src/Button.cpp', './src/DocumentLayout.cpp', './src/HorizontalBoxLayout.cpp', diff --git a/src/Box.cpp b/src/Box.cpp index aa5e0ae..af68180 100644 --- a/src/Box.cpp +++ b/src/Box.cpp @@ -58,6 +58,11 @@ void Box::set_height(double height) { } } +void Box::update() { + set_width(m_width); + set_height(m_height); +} + Box Box::max_geometry() { auto max_width = m_max_width == -1 ? m_width : m_max_width; auto max_height = m_max_height == -1 ? m_height : m_max_height; diff --git a/src/Box.hpp b/src/Box.hpp index 920816e..056faba 100644 --- a/src/Box.hpp +++ b/src/Box.hpp @@ -56,6 +56,8 @@ public: void set_min_width(double min_width) { m_min_width = min_width; set_width(m_width); } void set_min_height(double min_height) { m_min_height = min_height; set_height(m_height); } + void update(); + bool contains_point(double x, double y) const; bool contains_point(const Point &point) const; bool contains_box(const Box &other) const; diff --git a/src/Events.hpp b/src/Events.hpp index 7f4770e..bcc3743 100644 --- a/src/Events.hpp +++ b/src/Events.hpp @@ -31,11 +31,15 @@ class MouseButtonEvent : public Event { private: bool m_was_left_button_pressed; bool m_was_right_button_pressed; + bool m_did_scroll_up; + bool m_did_scroll_down; Point m_point; public: - MouseButtonEvent(bool was_left_button_pressed, bool was_right_button_pressed, Point point) + MouseButtonEvent(bool was_left_button_pressed, bool was_right_button_pressed, bool did_scroll_up, bool did_scroll_down, Point point) : m_was_left_button_pressed(was_left_button_pressed) , m_was_right_button_pressed(was_right_button_pressed) + , m_did_scroll_up(did_scroll_up) + , m_did_scroll_down(did_scroll_down) , m_point(point) {} EventType type() { return EventType::MouseButton; } @@ -43,6 +47,8 @@ public: bool was_left_button_pressed() { return m_was_left_button_pressed; } bool was_right_button_pressed() { return m_was_right_button_pressed; } + bool did_scroll_up() { return m_did_scroll_up; } + bool did_scroll_down() { return m_did_scroll_down; } Point &point() { return m_point; } }; diff --git a/src/Styles.cpp b/src/Styles.cpp index 1880fb6..07e4978 100644 --- a/src/Styles.cpp +++ b/src/Styles.cpp @@ -47,6 +47,17 @@ GenericStyle accent_widget_style { false }; +GenericStyle clear_widget_style { + pango_font_description_from_string("sans-serif"), + black6, + white2, + white2, + white2, + 0.0, + false, + false +}; + GenericStyle default_button_style { pango_font_description_from_string("sans-serif"), black6, @@ -64,7 +75,7 @@ GenericStyle accent_button_style { accent2, accent1, accent0, - 5.0, + 0.0, true, true }; diff --git a/src/Styles.hpp b/src/Styles.hpp index 6a79de0..d9ef9a6 100644 --- a/src/Styles.hpp +++ b/src/Styles.hpp @@ -26,6 +26,7 @@ extern RGB accent2; extern GenericStyle default_widget_style; extern GenericStyle accent_widget_style; +extern GenericStyle clear_widget_style; extern GenericStyle default_button_style; extern GenericStyle accent_button_style; extern GenericStyle default_label_style; diff --git a/src/Widget.cpp b/src/Widget.cpp index a98ac42..2ca6073 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -67,14 +67,6 @@ void Widget::move_to(double x, double y) { reflow(); } -void Widget::do_layout() { - if (m_layout) { - m_layout->run(); - } - - m_window_relative = compute_window_relative(); -} - void Widget::set_layout(std::shared_ptr layout) { m_layout = layout; m_layout->bind_to(this); @@ -182,10 +174,16 @@ void Widget::handle_repaint_rect(RepaintRectEvent &event) { } void Widget::handle_relayout_subtree(RelayoutSubtreeEvent &event) { + on_layout(); // hack for (auto child : m_children) { child->dispatch_event(event); } - do_layout(); + + if (m_layout) { + m_layout->run(); + } + + m_window_relative = compute_window_relative(); } void Widget::handle_mouse_move_event(MouseMoveEvent &event) { @@ -246,7 +244,7 @@ void Widget::handle_mouse_button_event(MouseButtonEvent &event) { if (!m_consumes_hits) { // translate the event's point to our coordinate space because the position of all children is relative to the origin of their parent - auto local_event = MouseButtonEvent(event.was_left_button_pressed(), event.was_right_button_pressed(), Point( + auto local_event = MouseButtonEvent(event.was_left_button_pressed(), event.was_right_button_pressed(), event.did_scroll_up(), event.did_scroll_down(), Point( event.point().x() - m_rect.x(), event.point().y() - m_rect.y() )); diff --git a/src/Widget.hpp b/src/Widget.hpp index fc6b626..669560d 100644 --- a/src/Widget.hpp +++ b/src/Widget.hpp @@ -113,12 +113,11 @@ protected: virtual void on_focus_update(FocusUpdateEvent &event) {} virtual void on_activation_update(ActivationUpdateEvent &event) {} virtual void on_paint() {} + virtual void on_layout() {} void set_did_init(bool did_init) { m_did_init = did_init; } Point compute_window_relative(); - void do_layout(); - void repaint(); void reflow(); private: diff --git a/src/Window.cpp b/src/Window.cpp index 00896e7..bcb30fe 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -172,13 +172,13 @@ void Window::run(bool block) { } case ButtonRelease: { auto point = Point(e.xbutton.x, e.xbutton.y); - auto event = MouseButtonEvent(false, false, point); + auto event = MouseButtonEvent(false, false, false, false, point); dispatch_to_main_widget(event); break; } case ButtonPress: { auto point = Point(e.xbutton.x, e.xbutton.y); - auto event = MouseButtonEvent(e.xbutton.button == Button1, e.xbutton.button == Button2, point); + auto event = MouseButtonEvent(e.xbutton.button == Button1, e.xbutton.button == Button2, e.xbutton.button == Button4, e.xbutton.button == Button5, point); dispatch_to_main_widget(event); break; } diff --git a/src/main.cpp b/src/main.cpp index be0d1f6..6e2b53e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include "DocumentLayout.hpp" #include "Events.hpp" #include "src/HorizontalBoxLayout.hpp" +#include "src/ScrollContainer.hpp" #include "src/Styles.hpp" #include "src/VerticalBoxLayout.hpp" #include @@ -22,18 +23,18 @@ int main() { auto main_widget = window.set_main_widget(); main_widget->set_layout(6.0); - for (int i = 0; i < 12; i++) { - auto row = main_widget->add(); - row->set_layout(6.0); - for (int i = 0; i < 12; i++) { - auto button = row->add("0"); + auto scroll_container = main_widget->add(); + scroll_container->set_scroll(Raven::Point(0, 0)); + scroll_container->resize_fixed(100, 100); + scroll_container->set_style(&Raven::accent_widget_style); - button->on_click = [button]() { - int number = std::stoi(button->text()); - button->set_text(std::to_string(++number)); - }; - } - } + auto target = scroll_container->make_target(); + target->set_layout(6.0); + + auto hello_button = target->add("hello"); + hello_button->set_style(&Raven::accent_button_style); + auto bye_button = target->add("bye"); + bye_button->set_style(&Raven::accent_button_style); window.run(true); return 0;