MsgBox.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "MsgBox.h"
  2. #include "Font.h"
  3. #include "AbstractScreen.h"
  4. #include "Window.h"
  5. #include "Label.h"
  6. #include "Button.h"
  7. void MsgBox(Window* owner, String&& text)
  8. {
  9. Font& font = Font::defaultFont();
  10. auto screenRect = AbstractScreen::the().rect();
  11. int textWidth = text.length() * font.glyphWidth() + 8;
  12. int textHeight = font.glyphHeight() + 8;
  13. int horizontalPadding = 16;
  14. int verticalPadding = 16;
  15. int buttonWidth = 60;
  16. int buttonHeight = 20;
  17. int windowWidth = textWidth + horizontalPadding * 2;
  18. int windowHeight = textHeight + buttonHeight + verticalPadding * 3;
  19. Rect windowRect(
  20. screenRect.center().x() - windowWidth / 2,
  21. screenRect.center().y() - windowHeight / 2,
  22. windowWidth,
  23. windowHeight
  24. );
  25. Rect buttonRect(
  26. windowWidth / 2 - buttonWidth / 2,
  27. windowHeight - verticalPadding - buttonHeight,
  28. buttonWidth,
  29. buttonHeight
  30. );
  31. auto* window = new Window;
  32. window->setTitle("MsgBox");
  33. window->setRect(windowRect);
  34. auto* widget = new Widget;
  35. widget->setWindowRelativeRect({ 0, 0, windowWidth, windowHeight });
  36. widget->setFillWithBackgroundColor(true);
  37. window->setMainWidget(widget);
  38. auto* label = new Label(widget);
  39. label->setWindowRelativeRect({
  40. horizontalPadding,
  41. verticalPadding,
  42. textWidth,
  43. textHeight
  44. });
  45. label->setText(move(text));
  46. auto* button = new Button(widget);
  47. button->setCaption("OK");
  48. button->setWindowRelativeRect(buttonRect);
  49. button->onClick = [] (Button& button) {
  50. printf("MsgBox button pressed, closing MsgBox :)\n");
  51. button.window()->close();
  52. };
  53. }