Add a simple MsgBox() :^)

This commit is contained in:
Andreas Kling 2018-10-14 00:21:42 +02:00
parent e9e7f7a714
commit 3ebea05996
Notes: sideshowbarker 2024-07-19 18:48:37 +09:00
9 changed files with 89 additions and 0 deletions

View file

@ -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);

View file

@ -27,6 +27,7 @@ VFS_OBJS = \
CheckBox.o \
ListBox.o \
TextBox.o \
MsgBox.o \
test.o
OBJS = $(AK_OBJS) $(VFS_OBJS)

59
Widgets/MsgBox.cpp Normal file
View file

@ -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();
};
}

8
Widgets/MsgBox.h Normal file
View file

@ -0,0 +1,8 @@
#pragma once
#include <AK/String.h>
class Window;
void MsgBox(Window* owner, String&&);

View file

@ -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);

View file

@ -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 };
};

View file

@ -111,3 +111,7 @@ void Window::setFocusedWidget(Widget* widget)
previouslyFocusedWidget->repaint(Rect());
}
void Window::close()
{
}

View file

@ -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;

View file

@ -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();
}