GWidget.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. if (auto* win = window()) {
  35. if (!win->is_visible())
  36. return;
  37. }
  38. return paintEvent(static_cast<GPaintEvent&>(event));
  39. case GEvent::Show:
  40. return showEvent(static_cast<GShowEvent&>(event));
  41. case GEvent::Hide:
  42. return hideEvent(static_cast<GHideEvent&>(event));
  43. case GEvent::KeyDown:
  44. return keyDownEvent(static_cast<GKeyEvent&>(event));
  45. case GEvent::KeyUp:
  46. return keyUpEvent(static_cast<GKeyEvent&>(event));
  47. case GEvent::MouseMove:
  48. return mouseMoveEvent(static_cast<GMouseEvent&>(event));
  49. case GEvent::MouseDown:
  50. // FIXME: Focus self if needed.
  51. return mouseDownEvent(static_cast<GMouseEvent&>(event));
  52. case GEvent::MouseUp:
  53. return mouseUpEvent(static_cast<GMouseEvent&>(event));
  54. default:
  55. return GObject::event(event);
  56. }
  57. }
  58. void GWidget::paintEvent(GPaintEvent& event)
  59. {
  60. //printf("GWidget::paintEvent :)\n");
  61. if (fillWithBackgroundColor()) {
  62. Painter painter(*this);
  63. painter.fill_rect(rect(), backgroundColor());
  64. }
  65. for (auto* ch : children()) {
  66. auto* child = (GWidget*)ch;
  67. child->event(event);
  68. }
  69. }
  70. void GWidget::showEvent(GShowEvent&)
  71. {
  72. }
  73. void GWidget::hideEvent(GHideEvent&)
  74. {
  75. }
  76. void GWidget::keyDownEvent(GKeyEvent&)
  77. {
  78. }
  79. void GWidget::keyUpEvent(GKeyEvent&)
  80. {
  81. }
  82. void GWidget::mouseDownEvent(GMouseEvent&)
  83. {
  84. }
  85. void GWidget::mouseUpEvent(GMouseEvent&)
  86. {
  87. }
  88. void GWidget::mouseMoveEvent(GMouseEvent&)
  89. {
  90. }
  91. void GWidget::update()
  92. {
  93. auto* w = window();
  94. if (!w)
  95. return;
  96. if (m_hasPendingPaintEvent)
  97. return;
  98. m_hasPendingPaintEvent = true;
  99. GEventLoop::main().post_event(w, make<GPaintEvent>(relativeRect()));
  100. }
  101. GWidget::HitTestResult GWidget::hitTest(int x, int y)
  102. {
  103. // FIXME: Care about z-order.
  104. for (auto* ch : children()) {
  105. auto* child = (GWidget*)ch;
  106. if (child->relativeRect().contains(x, y)) {
  107. return child->hitTest(x - child->relativeRect().x(), y - child->relativeRect().y());
  108. }
  109. }
  110. return { this, x, y };
  111. }
  112. void GWidget::setWindow(GWindow* window)
  113. {
  114. if (m_window == window)
  115. return;
  116. m_window = window;
  117. }
  118. bool GWidget::isFocused() const
  119. {
  120. // FIXME: Implement.
  121. return false;
  122. }
  123. void GWidget::setFocus(bool focus)
  124. {
  125. if (focus == isFocused())
  126. return;
  127. // FIXME: Implement.
  128. }
  129. void GWidget::setFont(RetainPtr<Font>&& font)
  130. {
  131. if (!font)
  132. m_font = Font::default_font();
  133. else
  134. m_font = move(font);
  135. }
  136. GraphicsBitmap* GWidget::backing()
  137. {
  138. if (auto* w = window())
  139. return w->backing();
  140. return nullptr;
  141. }