mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
Add a simple MsgBox() :^)
This commit is contained in:
parent
e9e7f7a714
commit
3ebea05996
Notes:
sideshowbarker
2024-07-19 18:48:37 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/3ebea059961
9 changed files with 89 additions and 0 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -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
59
Widgets/MsgBox.cpp
Normal 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
8
Widgets/MsgBox.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
|
||||
class Window;
|
||||
|
||||
void MsgBox(Window* owner, String&&);
|
||||
|
|
@ -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);
|
||||
|
|
|
@ -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 };
|
||||
};
|
||||
|
|
|
@ -111,3 +111,7 @@ void Window::setFocusedWidget(Widget* widget)
|
|||
previouslyFocusedWidget->repaint(Rect());
|
||||
}
|
||||
|
||||
void Window::close()
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue