Explorar el Código

Add a simple MsgBox() :^)

Andreas Kling hace 6 años
padre
commit
3ebea05996
Se han modificado 9 ficheros con 89 adiciones y 0 borrados
  1. 3 0
      Widgets/AbstractScreen.h
  2. 1 0
      Widgets/Makefile
  3. 59 0
      Widgets/MsgBox.cpp
  4. 8 0
      Widgets/MsgBox.h
  5. 5 0
      Widgets/Widget.cpp
  6. 4 0
      Widgets/Widget.h
  7. 4 0
      Widgets/Window.cpp
  8. 2 0
      Widgets/Window.h
  9. 3 0
      Widgets/test.cpp

+ 3 - 0
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);
 

+ 1 - 0
Widgets/Makefile

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

+ 59 - 0
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();
+    };
+}
+

+ 8 - 0
Widgets/MsgBox.h

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

+ 5 - 0
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);

+ 4 - 0
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 };
 };

+ 4 - 0
Widgets/Window.cpp

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

+ 2 - 0
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;

+ 3 - 0
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();
 }