GWindow.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "GWindow.h"
  2. #include "GEvent.h"
  3. #include "GEventLoop.h"
  4. #include "GWidget.h"
  5. #include <SharedGraphics/GraphicsBitmap.h>
  6. #include <LibC/gui.h>
  7. #include <LibC/stdio.h>
  8. #include <LibC/stdlib.h>
  9. #include <LibC/unistd.h>
  10. #include <AK/HashMap.h>
  11. static HashMap<int, GWindow*>* s_windows;
  12. static HashMap<int, GWindow*>& windows()
  13. {
  14. if (!s_windows)
  15. s_windows = new HashMap<int, GWindow*>;
  16. return *s_windows;
  17. }
  18. GWindow* GWindow::from_window_id(int window_id)
  19. {
  20. auto it = windows().find(window_id);
  21. if (it != windows().end())
  22. return (*it).value;
  23. return nullptr;
  24. }
  25. GWindow::GWindow(GObject* parent)
  26. : GObject(parent)
  27. {
  28. GUI_WindowParameters wparams;
  29. wparams.rect = { { 100, 400 }, { 140, 140 } };
  30. wparams.background_color = 0xffc0c0;
  31. strcpy(wparams.title, "GWindow");
  32. m_window_id = gui_create_window(&wparams);
  33. if (m_window_id < 0) {
  34. perror("gui_create_window");
  35. exit(1);
  36. }
  37. GUI_WindowBackingStoreInfo backing;
  38. int rc = gui_get_window_backing_store(m_window_id, &backing);
  39. if (rc < 0) {
  40. perror("gui_get_window_backing_store");
  41. exit(1);
  42. }
  43. m_backing = GraphicsBitmap::create_wrapper(backing.size, backing.pixels);
  44. windows().set(m_window_id, this);
  45. }
  46. GWindow::~GWindow()
  47. {
  48. }
  49. void GWindow::set_title(String&& title)
  50. {
  51. dbgprintf("GWindow::set_title \"%s\"\n", title.characters());
  52. GUI_WindowParameters params;
  53. int rc = gui_get_window_parameters(m_window_id, &params);
  54. ASSERT(rc == 0);
  55. strcpy(params.title, title.characters());;
  56. rc = gui_set_window_parameters(m_window_id, &params);
  57. ASSERT(rc == 0);
  58. m_title = move(title);
  59. }
  60. void GWindow::set_rect(const Rect& rect)
  61. {
  62. // FIXME: This is a hack to fudge the race with WSWindowManager trying to display @ old rect.
  63. sleep(10);
  64. dbgprintf("GWindow::set_rect %d,%d %dx%d\n", m_rect.x(), m_rect.y(), m_rect.width(), m_rect.height());
  65. GUI_WindowParameters params;
  66. int rc = gui_get_window_parameters(m_window_id, &params);
  67. ASSERT(rc == 0);
  68. params.rect = rect;
  69. rc = gui_set_window_parameters(m_window_id, &params);
  70. ASSERT(rc == 0);
  71. m_rect = rect;
  72. GUI_WindowBackingStoreInfo backing;
  73. rc = gui_get_window_backing_store(m_window_id, &backing);
  74. if (rc < 0) {
  75. perror("gui_get_window_backing_store");
  76. exit(1);
  77. }
  78. m_backing = GraphicsBitmap::create_wrapper(backing.size, backing.pixels);
  79. }
  80. void GWindow::event(GEvent& event)
  81. {
  82. if (event.is_mouse_event()) {
  83. if (!m_main_widget)
  84. return;
  85. auto& mouse_event = static_cast<GMouseEvent&>(event);
  86. if (m_main_widget) {
  87. auto result = m_main_widget->hit_test(mouse_event.x(), mouse_event.y());
  88. auto local_event = make<GMouseEvent>(event.type(), Point { result.localX, result.localY }, mouse_event.buttons(), mouse_event.button());
  89. ASSERT(result.widget);
  90. return result.widget->event(*local_event);
  91. }
  92. }
  93. if (event.is_paint_event()) {
  94. if (!m_main_widget)
  95. return;
  96. auto& paint_event = static_cast<GPaintEvent&>(event);
  97. auto rect = paint_event.rect();
  98. if (rect.is_empty())
  99. rect = m_main_widget->rect();
  100. m_main_widget->event(*make<GPaintEvent>(rect));
  101. GUI_Rect gui_rect = rect;
  102. int rc = gui_invalidate_window(m_window_id, &gui_rect);
  103. ASSERT(rc == 0);
  104. }
  105. return GObject::event(event);
  106. }
  107. bool GWindow::is_visible() const
  108. {
  109. return false;
  110. }
  111. void GWindow::close()
  112. {
  113. }
  114. void GWindow::show()
  115. {
  116. }
  117. void GWindow::update()
  118. {
  119. GEventLoop::main().post_event(this, make<GPaintEvent>());
  120. }
  121. void GWindow::set_main_widget(GWidget* widget)
  122. {
  123. if (m_main_widget == widget)
  124. return;
  125. m_main_widget = widget;
  126. if (widget)
  127. widget->set_window(this);
  128. update();
  129. }