Widget.cpp 3.4 KB

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