add simple button widget

This commit is contained in:
hippoz 2022-03-09 20:16:05 +02:00
parent 529c286b8f
commit 9fe7659a0f
No known key found for this signature in database
GPG key ID: 7C52899193467641
5 changed files with 13 additions and 8 deletions

View file

@ -9,6 +9,7 @@ executable(
'./src/Box.cpp', './src/Box.cpp',
'./src/Events.cpp', './src/Events.cpp',
'./src/Widget.cpp', './src/Widget.cpp',
'./src/Button.cpp',
'./src/Painter.cpp', './src/Painter.cpp',
'./src/Window.cpp', './src/Window.cpp',
'./src/main.cpp', './src/main.cpp',

View file

@ -1,13 +1,16 @@
#include "Button.hpp" #include "Button.hpp"
#include "Box.hpp"
#include "Window.hpp" #include "Window.hpp"
namespace Raven { namespace Raven {
void Button::on_paint() { void Button::on_paint() {
auto painter = get_window()->get_painter(); auto painter = get_window()->get_painter();
auto ctx = painter.get_cairo(); auto cr = painter.get_cairo();
cr->set_source_rgb(0.866, 0.713, 0.949);
painter.rounded_rectangle(get_current_geometry(), 8);
cr->fill();
} }
} }

View file

@ -10,8 +10,8 @@ private:
std::string m_text; std::string m_text;
public: public:
Button(std::string text) Button(std::string text)
: Widget() : m_text(text)
, m_text(text) {} , Widget() {}
void set_text(std::string text) { m_text = text; wants_repaint(); } void set_text(std::string text) { m_text = text; wants_repaint(); }
std::string &get_text() { return m_text; } std::string &get_text() { return m_text; }

View file

@ -38,7 +38,7 @@ public:
virtual void on_mouse_move(MouseMoveEvent &event) {}; virtual void on_mouse_move(MouseMoveEvent &event) {};
virtual void on_paint() {}; virtual void on_paint() {};
virtual ~Widget() = default; virtual ~Widget() {};
private: private:
void process_event(Event &event); void process_event(Event &event);
void handle_repaint_requested(WidgetRepaintRequestedEvent &event); void handle_repaint_requested(WidgetRepaintRequestedEvent &event);

View file

@ -1,16 +1,17 @@
#include <iostream> #include <iostream>
#include "Window.hpp" #include "Window.hpp"
#include "Widget.hpp" #include "Widget.hpp"
#include "Button.hpp"
#include "Box.hpp" #include "Box.hpp"
int main() { int main() {
Raven::Window window {}; Raven::Window window {};
Raven::Widget main_widget {}; Raven::Button button {"click me!"};
window.spawn_window(); window.spawn_window();
main_widget.set_current_geometry(Raven::Box(10, 10, 100, 30)); button.set_current_geometry(Raven::Box(10, 10, 100, 30));
window.set_main_widget(&main_widget); window.set_main_widget(&button);
window.run(true); window.run(true);
return 0; return 0;