diff options
-rw-r--r-- | Widgets/AbstractScreen.h | 3 | ||||
-rw-r--r-- | Widgets/Makefile | 1 | ||||
-rw-r--r-- | Widgets/MsgBox.cpp | 59 | ||||
-rw-r--r-- | Widgets/MsgBox.h | 8 | ||||
-rw-r--r-- | Widgets/Widget.cpp | 5 | ||||
-rw-r--r-- | Widgets/Widget.h | 4 | ||||
-rw-r--r-- | Widgets/Window.cpp | 4 | ||||
-rw-r--r-- | Widgets/Window.h | 2 | ||||
-rw-r--r-- | Widgets/test.cpp | 3 |
9 files changed, 89 insertions, 0 deletions
diff --git a/Widgets/AbstractScreen.h b/Widgets/AbstractScreen.h index fdf6bd33ca..840d94720f 100644 --- a/Widgets/AbstractScreen.h +++ b/Widgets/AbstractScreen.h @@ -1,6 +1,7 @@ #pragma once #include "Object.h" +#include "Rect.h" class AbstractScreen : public Object { public: @@ -11,6 +12,8 @@ public: static AbstractScreen& the(); + Rect rect() const { return { 0, 0, width(), height() }; } + protected: AbstractScreen(unsigned width, unsigned height); diff --git a/Widgets/Makefile b/Widgets/Makefile index 8f92a19d53..52835f08f5 100644 --- a/Widgets/Makefile +++ b/Widgets/Makefile @@ -27,6 +27,7 @@ VFS_OBJS = \ CheckBox.o \ ListBox.o \ TextBox.o \ + MsgBox.o \ test.o OBJS = $(AK_OBJS) $(VFS_OBJS) diff --git a/Widgets/MsgBox.cpp b/Widgets/MsgBox.cpp new file mode 100644 index 0000000000..4827a5fca9 --- /dev/null +++ b/Widgets/MsgBox.cpp @@ -0,0 +1,59 @@ +#include "MsgBox.h" +#include "Font.h" +#include "AbstractScreen.h" +#include "Window.h" +#include "Label.h" +#include "Button.h" + +void MsgBox(Window* owner, String&& text) +{ + Font& font = Font::defaultFont(); + auto screenRect = AbstractScreen::the().rect(); + + unsigned textWidth = text.length() * font.glyphWidth() + 8; + unsigned textHeight = font.glyphHeight() + 8; + unsigned horizontalPadding = 16; + unsigned verticalPadding = 16; + unsigned buttonWidth = 60; + unsigned buttonHeight = 20; + unsigned windowWidth = textWidth + horizontalPadding * 2; + unsigned windowHeight = textHeight + buttonHeight + verticalPadding * 3; + + Rect windowRect( + screenRect.center().x() - windowWidth / 2, + screenRect.center().y() - windowHeight / 2, + windowWidth, + windowHeight + ); + + Rect buttonRect( + windowWidth / 2 - buttonWidth / 2, + windowHeight - verticalPadding - buttonHeight, + buttonWidth, + buttonHeight + ); + + auto* window = new Window; + window->setTitle("MsgBox"); + window->setRect(windowRect); + auto* widget = new Widget; + widget->setWindowRelativeRect({ 0, 0, windowWidth, windowHeight }); + widget->setFillWithBackgroundColor(true); + window->setMainWidget(widget); + auto* label = new Label(widget); + label->setWindowRelativeRect({ + horizontalPadding, + verticalPadding, + textWidth, + textHeight + }); + label->setText(std::move(text)); + auto* button = new Button(widget); + button->setCaption("OK"); + button->setWindowRelativeRect(buttonRect); + button->onClick = [] (Button& button) { + printf("MsgBox button pressed, closing MsgBox :)\n"); + button.window()->close(); + }; +} + diff --git a/Widgets/MsgBox.h b/Widgets/MsgBox.h new file mode 100644 index 0000000000..fa746ac86b --- /dev/null +++ b/Widgets/MsgBox.h @@ -0,0 +1,8 @@ +#pragma once + +#include <AK/String.h> + +class Window; + +void MsgBox(Window* owner, String&&); + diff --git a/Widgets/Widget.cpp b/Widgets/Widget.cpp index a4d98499c4..07b09b6583 100644 --- a/Widgets/Widget.cpp +++ b/Widgets/Widget.cpp @@ -3,6 +3,7 @@ #include "EventLoop.h" #include "WindowManager.h" #include "Window.h" +#include "Painter.h" #include <AK/Assertions.h> Widget::Widget(Widget* parent) @@ -64,6 +65,10 @@ void Widget::event(Event& event) void Widget::paintEvent(PaintEvent& event) { //printf("Widget::paintEvent :)\n"); + if (fillWithBackgroundColor()) { + Painter painter(*this); + painter.fillRect(rect(), backgroundColor()); + } for (auto* ch : children()) { auto* child = (Widget*)ch; child->paintEvent(event); diff --git a/Widgets/Widget.h b/Widgets/Widget.h index e025dc3aed..c0e9b428f2 100644 --- a/Widgets/Widget.h +++ b/Widgets/Widget.h @@ -76,6 +76,9 @@ public: Widget* parentWidget() { return static_cast<Widget*>(parent()); } const Widget* parentWidget() const { return static_cast<const Widget*>(parent()); } + void setFillWithBackgroundColor(bool b) { m_fillWithBackgroundColor = b; } + bool fillWithBackgroundColor() const { return m_fillWithBackgroundColor; } + private: Window* m_window { nullptr }; @@ -84,4 +87,5 @@ private: Color m_foregroundColor; bool m_hasPendingPaintEvent { false }; + bool m_fillWithBackgroundColor { false }; }; diff --git a/Widgets/Window.cpp b/Widgets/Window.cpp index f521b94c4e..c0ba0a2351 100644 --- a/Widgets/Window.cpp +++ b/Widgets/Window.cpp @@ -111,3 +111,7 @@ void Window::setFocusedWidget(Widget* widget) previouslyFocusedWidget->repaint(Rect()); } +void Window::close() +{ +} + diff --git a/Widgets/Window.h b/Widgets/Window.h index ceb7bd3433..cf9d1006b8 100644 --- a/Widgets/Window.h +++ b/Widgets/Window.h @@ -44,6 +44,8 @@ public: const Widget* focusedWidget() const { return m_focusedWidget.ptr(); } void setFocusedWidget(Widget*); + void close(); + private: String m_title; Rect m_rect; diff --git a/Widgets/test.cpp b/Widgets/test.cpp index 1c17229137..054c7e343b 100644 --- a/Widgets/test.cpp +++ b/Widgets/test.cpp @@ -10,6 +10,7 @@ #include "CheckBox.h" #include "ListBox.h" #include "TextBox.h" +#include "MsgBox.h" #include <cstdio> int main(int argc, char** argv) @@ -103,5 +104,7 @@ int main(int argc, char** argv) clockWin->setRect({ 500, 50, 100, 40 }); clockWin->setMainWidget(new ClockWidget); + MsgBox(nullptr, "This is a message box!"); + return loop.exec(); } |