summaryrefslogtreecommitdiff
path: root/Widgets/MsgBox.cpp
blob: 4827a5fca92d0d9bdd0fe0ba8334a0e794acf28f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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();
    };
}