add scrolling widgets

This commit is contained in:
hippoz 2022-07-05 20:07:09 +03:00
parent 0b4b3b1332
commit b0315b76be
Signed by: hippoz
GPG key ID: 7C52899193467641
10 changed files with 51 additions and 27 deletions

View file

@ -11,6 +11,7 @@ executable(
'./src/Painter.cpp', './src/Painter.cpp',
'./src/Window.cpp', './src/Window.cpp',
'./src/Widget.cpp', './src/Widget.cpp',
'./src/ScrollContainer.cpp',
'./src/Button.cpp', './src/Button.cpp',
'./src/DocumentLayout.cpp', './src/DocumentLayout.cpp',
'./src/HorizontalBoxLayout.cpp', './src/HorizontalBoxLayout.cpp',

View file

@ -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() { Box Box::max_geometry() {
auto max_width = m_max_width == -1 ? m_width : m_max_width; auto max_width = m_max_width == -1 ? m_width : m_max_width;
auto max_height = m_max_height == -1 ? m_height : m_max_height; auto max_height = m_max_height == -1 ? m_height : m_max_height;

View file

@ -56,6 +56,8 @@ public:
void set_min_width(double min_width) { m_min_width = min_width; set_width(m_width); } 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 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(double x, double y) const;
bool contains_point(const Point &point) const; bool contains_point(const Point &point) const;
bool contains_box(const Box &other) const; bool contains_box(const Box &other) const;

View file

@ -31,11 +31,15 @@ class MouseButtonEvent : public Event {
private: private:
bool m_was_left_button_pressed; bool m_was_left_button_pressed;
bool m_was_right_button_pressed; bool m_was_right_button_pressed;
bool m_did_scroll_up;
bool m_did_scroll_down;
Point m_point; Point m_point;
public: 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_left_button_pressed(was_left_button_pressed)
, m_was_right_button_pressed(was_right_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) {} , m_point(point) {}
EventType type() { return EventType::MouseButton; } EventType type() { return EventType::MouseButton; }
@ -43,6 +47,8 @@ public:
bool was_left_button_pressed() { return m_was_left_button_pressed; } bool was_left_button_pressed() { return m_was_left_button_pressed; }
bool was_right_button_pressed() { return m_was_right_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; } Point &point() { return m_point; }
}; };

View file

@ -47,6 +47,17 @@ GenericStyle accent_widget_style {
false false
}; };
GenericStyle clear_widget_style {
pango_font_description_from_string("sans-serif"),
black6,
white2,
white2,
white2,
0.0,
false,
false
};
GenericStyle default_button_style { GenericStyle default_button_style {
pango_font_description_from_string("sans-serif"), pango_font_description_from_string("sans-serif"),
black6, black6,
@ -64,7 +75,7 @@ GenericStyle accent_button_style {
accent2, accent2,
accent1, accent1,
accent0, accent0,
5.0, 0.0,
true, true,
true true
}; };

View file

@ -26,6 +26,7 @@ extern RGB accent2;
extern GenericStyle default_widget_style; extern GenericStyle default_widget_style;
extern GenericStyle accent_widget_style; extern GenericStyle accent_widget_style;
extern GenericStyle clear_widget_style;
extern GenericStyle default_button_style; extern GenericStyle default_button_style;
extern GenericStyle accent_button_style; extern GenericStyle accent_button_style;
extern GenericStyle default_label_style; extern GenericStyle default_label_style;

View file

@ -67,14 +67,6 @@ void Widget::move_to(double x, double y) {
reflow(); 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> layout) { void Widget::set_layout(std::shared_ptr<Layout> layout) {
m_layout = layout; m_layout = layout;
m_layout->bind_to(this); m_layout->bind_to(this);
@ -182,10 +174,16 @@ void Widget::handle_repaint_rect(RepaintRectEvent &event) {
} }
void Widget::handle_relayout_subtree(RelayoutSubtreeEvent &event) { void Widget::handle_relayout_subtree(RelayoutSubtreeEvent &event) {
on_layout(); // hack
for (auto child : m_children) { for (auto child : m_children) {
child->dispatch_event(event); 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) { void Widget::handle_mouse_move_event(MouseMoveEvent &event) {
@ -246,7 +244,7 @@ void Widget::handle_mouse_button_event(MouseButtonEvent &event) {
if (!m_consumes_hits) { 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 // 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().x() - m_rect.x(),
event.point().y() - m_rect.y() event.point().y() - m_rect.y()
)); ));

View file

@ -113,12 +113,11 @@ protected:
virtual void on_focus_update(FocusUpdateEvent &event) {} virtual void on_focus_update(FocusUpdateEvent &event) {}
virtual void on_activation_update(ActivationUpdateEvent &event) {} virtual void on_activation_update(ActivationUpdateEvent &event) {}
virtual void on_paint() {} virtual void on_paint() {}
virtual void on_layout() {}
void set_did_init(bool did_init) { m_did_init = did_init; } void set_did_init(bool did_init) { m_did_init = did_init; }
Point compute_window_relative(); Point compute_window_relative();
void do_layout();
void repaint(); void repaint();
void reflow(); void reflow();
private: private:

View file

@ -172,13 +172,13 @@ void Window::run(bool block) {
} }
case ButtonRelease: { case ButtonRelease: {
auto point = Point(e.xbutton.x, e.xbutton.y); 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); dispatch_to_main_widget(event);
break; break;
} }
case ButtonPress: { case ButtonPress: {
auto point = Point(e.xbutton.x, e.xbutton.y); 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); dispatch_to_main_widget(event);
break; break;
} }

View file

@ -9,6 +9,7 @@
#include "DocumentLayout.hpp" #include "DocumentLayout.hpp"
#include "Events.hpp" #include "Events.hpp"
#include "src/HorizontalBoxLayout.hpp" #include "src/HorizontalBoxLayout.hpp"
#include "src/ScrollContainer.hpp"
#include "src/Styles.hpp" #include "src/Styles.hpp"
#include "src/VerticalBoxLayout.hpp" #include "src/VerticalBoxLayout.hpp"
#include <iostream> #include <iostream>
@ -22,18 +23,18 @@ int main() {
auto main_widget = window.set_main_widget<Raven::Widget>(); auto main_widget = window.set_main_widget<Raven::Widget>();
main_widget->set_layout<Raven::VerticalBoxLayout>(6.0); main_widget->set_layout<Raven::VerticalBoxLayout>(6.0);
for (int i = 0; i < 12; i++) { auto scroll_container = main_widget->add<Raven::ScrollContainer>();
auto row = main_widget->add<Raven::Widget>(); scroll_container->set_scroll(Raven::Point(0, 0));
row->set_layout<Raven::HorizontalBoxLayout>(6.0); scroll_container->resize_fixed(100, 100);
for (int i = 0; i < 12; i++) { scroll_container->set_style(&Raven::accent_widget_style);
auto button = row->add<Raven::Button>("0");
button->on_click = [button]() { auto target = scroll_container->make_target();
int number = std::stoi(button->text()); target->set_layout<Raven::DocumentLayout>(6.0);
button->set_text(std::to_string(++number));
}; auto hello_button = target->add<Raven::Button>("hello");
} hello_button->set_style(&Raven::accent_button_style);
} auto bye_button = target->add<Raven::Button>("bye");
bye_button->set_style(&Raven::accent_button_style);
window.run(true); window.run(true);
return 0; return 0;