WSWindow.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "WSWindow.h"
  2. #include "WSWindowManager.h"
  3. #include "WSEvent.h"
  4. #include "WSEventLoop.h"
  5. #include "Process.h"
  6. WSWindow::WSWindow(Process& process, int window_id)
  7. : m_process(process)
  8. , m_window_id(window_id)
  9. {
  10. WSWindowManager::the().add_window(*this);
  11. }
  12. WSWindow::~WSWindow()
  13. {
  14. WSWindowManager::the().remove_window(*this);
  15. }
  16. void WSWindow::set_title(String&& title)
  17. {
  18. if (m_title == title)
  19. return;
  20. m_title = move(title);
  21. WSWindowManager::the().notify_title_changed(*this);
  22. }
  23. void WSWindow::set_rect(const Rect& rect)
  24. {
  25. if (m_rect == rect)
  26. return;
  27. auto oldRect = m_rect;
  28. m_rect = rect;
  29. m_backing = GraphicsBitmap::create(m_process, m_rect.size());
  30. WSWindowManager::the().notify_rect_changed(*this, oldRect, m_rect);
  31. }
  32. // FIXME: Just use the same types.
  33. static GUI_MouseButton to_api(MouseButton button)
  34. {
  35. switch (button) {
  36. case MouseButton::None: return GUI_MouseButton::NoButton;
  37. case MouseButton::Left: return GUI_MouseButton::Left;
  38. case MouseButton::Right: return GUI_MouseButton::Right;
  39. case MouseButton::Middle: return GUI_MouseButton::Middle;
  40. }
  41. }
  42. void WSWindow::event(WSEvent& event)
  43. {
  44. GUI_Event gui_event;
  45. gui_event.window_id = window_id();
  46. switch (event.type()) {
  47. case WSEvent::Paint:
  48. gui_event.type = GUI_Event::Type::Paint;
  49. gui_event.paint.rect = static_cast<PaintEvent&>(event).rect();
  50. break;
  51. case WSEvent::MouseMove:
  52. gui_event.type = GUI_Event::Type::MouseMove;
  53. gui_event.mouse.position = static_cast<MouseEvent&>(event).position();
  54. gui_event.mouse.button = GUI_MouseButton::NoButton;
  55. break;
  56. case WSEvent::MouseDown:
  57. gui_event.type = GUI_Event::Type::MouseDown;
  58. gui_event.mouse.position = static_cast<MouseEvent&>(event).position();
  59. gui_event.mouse.button = to_api(static_cast<MouseEvent&>(event).button());
  60. break;
  61. case WSEvent::MouseUp:
  62. gui_event.type = GUI_Event::Type::MouseUp;
  63. gui_event.mouse.position = static_cast<MouseEvent&>(event).position();
  64. gui_event.mouse.button = to_api(static_cast<MouseEvent&>(event).button());
  65. break;
  66. case WSEvent::KeyDown:
  67. gui_event.type = GUI_Event::Type::KeyDown;
  68. gui_event.key.character = static_cast<KeyEvent&>(event).text()[0];
  69. break;
  70. case WSEvent::WM_Invalidate:
  71. WSWindowManager::the().invalidate(*this, event.rect());
  72. return;
  73. case WSEvent::WindowActivated:
  74. gui_event.type = GUI_Event::Type::WindowActivated;
  75. break;
  76. case WSEvent::WindowDeactivated:
  77. gui_event.type = GUI_Event::Type::WindowDeactivated;
  78. break;
  79. }
  80. if (gui_event.type == GUI_Event::Type::Invalid)
  81. return;
  82. {
  83. LOCKER(m_process.gui_events_lock());
  84. m_process.gui_events().append(move(gui_event));
  85. }
  86. }