diff --git a/Demos/Mouse/Makefile b/Demos/Mouse/Makefile new file mode 100644 index 0000000000..b08538d561 --- /dev/null +++ b/Demos/Mouse/Makefile @@ -0,0 +1,8 @@ +OBJS = \ + main.o + +PROGRAM = Mouse + +LIB_DEPS = GUI IPC Gfx Core + +include ../../Makefile.common diff --git a/Demos/Mouse/main.cpp b/Demos/Mouse/main.cpp new file mode 100644 index 0000000000..06f3222845 --- /dev/null +++ b/Demos/Mouse/main.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include + +static unsigned s_mouse_button_state; + +class MouseButtonIndicator final : public GUI::Frame { + C_OBJECT(MouseButtonIndicator); + +public: + virtual ~MouseButtonIndicator() {} + +private: + virtual void paint_event(GUI::PaintEvent& event) override + { + Frame::paint_event(event); + + GUI::Painter painter(*this); + painter.add_clip_rect(event.rect()); + + Color background_color; + Color foreground_color; + + if (s_mouse_button_state & m_button) { + background_color = Color::Black; + foreground_color = Color::White; + } else { + background_color = Color::White; + foreground_color = Color::Black; + } + + painter.fill_rect(frame_inner_rect(), background_color); + painter.draw_text(frame_inner_rect(), m_name, Gfx::TextAlignment::Center, foreground_color); + } + + void mousedown_event(GUI::MouseEvent& event) override + { + event.ignore(); + } + + void mouseup_event(GUI::MouseEvent& event) override + { + event.ignore(); + } + + MouseButtonIndicator(const String& name, GUI::MouseButton button) + : m_name(name) + , m_button(button) + { + } + + String m_name; + GUI::MouseButton m_button; +}; + +class MainWidget final : public GUI::Widget { + C_OBJECT(MainWidget); + +public: + void mousedown_event(GUI::MouseEvent& event) override + { + s_mouse_button_state = event.buttons(); + update(); + Widget::mousedown_event(event); + } + + void mouseup_event(GUI::MouseEvent& event) override + { + s_mouse_button_state = event.buttons(); + update(); + Widget::mouseup_event(event); + } + +private: + MainWidget() {} +}; + +int main(int argc, char** argv) +{ + GUI::Application app(argc, argv); + auto window = GUI::Window::construct(); + window->set_title("Mouse button demo"); + + auto& main_widget = window->set_main_widget(); + main_widget.set_fill_with_background_color(true); + + main_widget.set_layout(); + + main_widget.add("Left", GUI::MouseButton::Left); + main_widget.add("Middle", GUI::MouseButton::Middle); + main_widget.add("Right", GUI::MouseButton::Right); + main_widget.add("Back", GUI::MouseButton::Back); + main_widget.add("Forward", GUI::MouseButton::Forward); + + window->show(); + return app.exec(); +} diff --git a/Kernel/build-root-filesystem.sh b/Kernel/build-root-filesystem.sh index 44a5983ee4..0e9002821b 100755 --- a/Kernel/build-root-filesystem.sh +++ b/Kernel/build-root-filesystem.sh @@ -150,6 +150,7 @@ cp ../Demos/WidgetGallery/WidgetGallery mnt/bin/WidgetGallery cp ../Demos/Cube/Cube mnt/bin/Cube cp ../Demos/Screensaver/Screensaver mnt/bin/Screensaver cp ../Demos/Fire/Fire mnt/bin/Fire +cp ../Demos/Mouse/Mouse mnt/bin/Mouse cp ../Demos/DynamicLink/LinkDemo/LinkDemo mnt/bin/LinkDemo cp ../DevTools/HackStudio/HackStudio mnt/bin/HackStudio cp ../DevTools/VisualBuilder/VisualBuilder mnt/bin/VisualBuilder