GWidget.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "GWidget.h"
  2. #include "GEvent.h"
  3. #include "GEventLoop.h"
  4. #include "GWindow.h"
  5. #include <AK/Assertions.h>
  6. #include <SharedGraphics/GraphicsBitmap.h>
  7. #include <SharedGraphics/Painter.h>
  8. GWidget::GWidget(GWidget* parent)
  9. : GObject(parent)
  10. {
  11. setFont(nullptr);
  12. m_backgroundColor = Color::White;
  13. m_foregroundColor = Color::Black;
  14. }
  15. GWidget::~GWidget()
  16. {
  17. }
  18. void GWidget::setWindowRelativeRect(const Rect& rect, bool should_update)
  19. {
  20. // FIXME: Make some kind of event loop driven ResizeEvent?
  21. m_relativeRect = rect;
  22. if (should_update)
  23. update();
  24. }
  25. void GWidget::repaint(const Rect& rect)
  26. {
  27. // FIXME: Implement.
  28. }
  29. void GWidget::event(GEvent& event)
  30. {
  31. switch (event.type()) {
  32. case GEvent::Paint:
  33. m_hasPendingPaintEvent = false;
  34. return paintEvent(static_cast<GPaintEvent&>(event));
  35. case GEvent::Show:
  36. return showEvent(static_cast<GShowEvent&>(event));
  37. case GEvent::Hide:
  38. return hideEvent(static_cast<GHideEvent&>(event));
  39. case GEvent::KeyDown:
  40. return keyDownEvent(static_cast<GKeyEvent&>(event));
  41. case GEvent::KeyUp:
  42. return keyUpEvent(static_cast<GKeyEvent&>(event));
  43. case GEvent::MouseMove:
  44. return mouseMoveEvent(static_cast<GMouseEvent&>(event));
  45. case GEvent::MouseDown:
  46. // FIXME: Focus self if needed.
  47. return mouseDownEvent(static_cast<GMouseEvent&>(event));
  48. case GEvent::MouseUp:
  49. return mouseUpEvent(static_cast<GMouseEvent&>(event));
  50. default:
  51. return GObject::event(event);
  52. }
  53. }
  54. void GWidget::paintEvent(GPaintEvent& event)
  55. {
  56. if (fillWithBackgroundColor()) {
  57. Painter painter(*this);
  58. painter.fill_rect(rect(), backgroundColor());
  59. }
  60. for (auto* ch : children()) {
  61. auto* child = (GWidget*)ch;
  62. child->event(event);
  63. }
  64. }
  65. void GWidget::showEvent(GShowEvent&)
  66. {
  67. }
  68. void GWidget::hideEvent(GHideEvent&)
  69. {
  70. }
  71. void GWidget::keyDownEvent(GKeyEvent&)
  72. {
  73. }
  74. void GWidget::keyUpEvent(GKeyEvent&)
  75. {
  76. }
  77. void GWidget::mouseDownEvent(GMouseEvent&)
  78. {
  79. }
  80. void GWidget::mouseUpEvent(GMouseEvent&)
  81. {
  82. }
  83. void GWidget::mouseMoveEvent(GMouseEvent&)
  84. {
  85. }
  86. void GWidget::update()
  87. {
  88. auto* w = window();
  89. if (!w)
  90. return;
  91. if (m_hasPendingPaintEvent)
  92. return;
  93. m_hasPendingPaintEvent = true;
  94. GEventLoop::main().post_event(w, make<GPaintEvent>(relativeRect()));
  95. }
  96. GWidget::HitTestResult GWidget::hitTest(int x, int y)
  97. {
  98. // FIXME: Care about z-order.
  99. for (auto* ch : children()) {
  100. auto* child = (GWidget*)ch;
  101. if (child->relativeRect().contains(x, y)) {
  102. return child->hitTest(x - child->relativeRect().x(), y - child->relativeRect().y());
  103. }
  104. }
  105. return { this, x, y };
  106. }
  107. void GWidget::set_window(GWindow* window)
  108. {
  109. if (m_window == window)
  110. return;
  111. m_window = window;
  112. }
  113. bool GWidget::isFocused() const
  114. {
  115. // FIXME: Implement.
  116. return false;
  117. }
  118. void GWidget::setFocus(bool focus)
  119. {
  120. if (focus == isFocused())
  121. return;
  122. // FIXME: Implement.
  123. }
  124. void GWidget::setFont(RetainPtr<Font>&& font)
  125. {
  126. if (!font)
  127. m_font = Font::default_font();
  128. else
  129. m_font = move(font);
  130. }
  131. GraphicsBitmap* GWidget::backing()
  132. {
  133. if (auto* w = window())
  134. return w->backing();
  135. return nullptr;
  136. }