GWindow.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. //#define UPDATE_COALESCING_DEBUG
  12. static HashMap<int, GWindow*>* s_windows;
  13. static HashMap<int, GWindow*>& windows()
  14. {
  15. if (!s_windows)
  16. s_windows = new HashMap<int, GWindow*>;
  17. return *s_windows;
  18. }
  19. GWindow* GWindow::from_window_id(int window_id)
  20. {
  21. auto it = windows().find(window_id);
  22. if (it != windows().end())
  23. return (*it).value;
  24. return nullptr;
  25. }
  26. GWindow::GWindow(GObject* parent)
  27. : GObject(parent)
  28. {
  29. m_rect_when_windowless = { 100, 400, 140, 140 };
  30. m_title_when_windowless = "GWindow";
  31. }
  32. GWindow::~GWindow()
  33. {
  34. if (m_main_widget)
  35. delete m_main_widget;
  36. hide();
  37. }
  38. void GWindow::close()
  39. {
  40. // FIXME: If we exit the event loop, we're never gonna deal with the delete_later request!
  41. // This will become relevant once we support nested event loops.
  42. if (should_exit_app_on_close())
  43. GEventLoop::main().exit(0);
  44. delete_later();
  45. }
  46. void GWindow::show()
  47. {
  48. if (m_window_id)
  49. return;
  50. GUI_WindowParameters wparams;
  51. wparams.rect = m_rect_when_windowless;
  52. wparams.background_color = 0xffc0c0;
  53. strcpy(wparams.title, m_title_when_windowless.characters());
  54. m_window_id = gui_create_window(&wparams);
  55. if (m_window_id < 0) {
  56. perror("gui_create_window");
  57. exit(1);
  58. }
  59. windows().set(m_window_id, this);
  60. update();
  61. }
  62. void GWindow::hide()
  63. {
  64. if (!m_window_id)
  65. return;
  66. windows().remove(m_window_id);
  67. int rc = gui_destroy_window(m_window_id);
  68. if (rc < 0) {
  69. perror("gui_destroy_window");
  70. exit(1);
  71. }
  72. }
  73. void GWindow::set_title(String&& title)
  74. {
  75. m_title_when_windowless = title;
  76. if (m_window_id) {
  77. int rc = gui_set_window_title(m_window_id, title.characters(), title.length());
  78. if (rc < 0) {
  79. perror("gui_set_window_title");
  80. exit(1);
  81. }
  82. }
  83. }
  84. String GWindow::title() const
  85. {
  86. if (m_window_id) {
  87. char buffer[256];
  88. int rc = gui_get_window_title(m_window_id, buffer, sizeof(buffer));
  89. ASSERT(rc >= 0);
  90. return String(buffer, rc);
  91. }
  92. return m_title_when_windowless;
  93. }
  94. Rect GWindow::rect() const
  95. {
  96. if (m_window_id) {
  97. GUI_Rect buffer;
  98. int rc = gui_get_window_rect(m_window_id, &buffer);
  99. ASSERT(rc >= 0);
  100. return buffer;
  101. }
  102. return m_rect_when_windowless;
  103. }
  104. void GWindow::set_rect(const Rect& a_rect)
  105. {
  106. m_rect_when_windowless = a_rect;
  107. if (m_window_id) {
  108. GUI_Rect rect = a_rect;
  109. int rc = gui_set_window_rect(m_window_id, &rect);
  110. ASSERT(rc == 0);
  111. }
  112. }
  113. void GWindow::event(GEvent& event)
  114. {
  115. if (event.is_mouse_event()) {
  116. if (m_global_cursor_tracking_widget) {
  117. // FIXME: This won't work for widgets-within-widgets.
  118. auto& mouse_event = static_cast<GMouseEvent&>(event);
  119. Point local_point { mouse_event.x() - m_global_cursor_tracking_widget->relative_rect().x(), mouse_event.y() - m_global_cursor_tracking_widget->relative_rect().y() };
  120. auto local_event = make<GMouseEvent>(event.type(), local_point, mouse_event.buttons(), mouse_event.button());
  121. m_global_cursor_tracking_widget->event(*local_event);
  122. }
  123. if (!m_main_widget)
  124. return;
  125. auto& mouse_event = static_cast<GMouseEvent&>(event);
  126. if (m_main_widget) {
  127. auto result = m_main_widget->hit_test(mouse_event.x(), mouse_event.y());
  128. auto local_event = make<GMouseEvent>(event.type(), Point { result.localX, result.localY }, mouse_event.buttons(), mouse_event.button());
  129. ASSERT(result.widget);
  130. return result.widget->event(*local_event);
  131. }
  132. return;
  133. }
  134. if (event.is_paint_event()) {
  135. m_pending_paint_event_rects.clear();
  136. if (!m_main_widget)
  137. return;
  138. auto& paint_event = static_cast<GPaintEvent&>(event);
  139. auto rect = paint_event.rect();
  140. if (rect.is_empty())
  141. rect = m_main_widget->rect();
  142. m_main_widget->event(*make<GPaintEvent>(rect));
  143. if (m_window_id) {
  144. GUI_Rect gui_rect = rect;
  145. int rc = gui_notify_paint_finished(m_window_id, &gui_rect);
  146. ASSERT(rc == 0);
  147. }
  148. return;
  149. }
  150. if (event.is_key_event()) {
  151. if (m_focused_widget)
  152. return m_focused_widget->event(event);
  153. if (m_main_widget)
  154. return m_main_widget->event(event);
  155. return;
  156. }
  157. if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) {
  158. m_is_active = event.type() == GEvent::WindowBecameActive;
  159. if (m_main_widget)
  160. m_main_widget->event(event);
  161. if (m_focused_widget)
  162. m_focused_widget->update();
  163. return;
  164. }
  165. if (event.type() == GEvent::WindowCloseRequest) {
  166. close();
  167. return;
  168. }
  169. GObject::event(event);
  170. }
  171. bool GWindow::is_visible() const
  172. {
  173. return false;
  174. }
  175. void GWindow::update(const Rect& a_rect)
  176. {
  177. if (!m_window_id)
  178. return;
  179. for (auto& pending_rect : m_pending_paint_event_rects) {
  180. if (pending_rect.contains(a_rect)) {
  181. #ifdef UPDATE_COALESCING_DEBUG
  182. dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
  183. #endif
  184. return;
  185. }
  186. }
  187. m_pending_paint_event_rects.append(a_rect);
  188. GUI_Rect rect = a_rect;
  189. int rc = gui_invalidate_window(m_window_id, a_rect.is_null() ? nullptr : &rect);
  190. ASSERT(rc == 0);
  191. }
  192. void GWindow::set_main_widget(GWidget* widget)
  193. {
  194. if (m_main_widget == widget)
  195. return;
  196. m_main_widget = widget;
  197. if (m_main_widget) {
  198. auto new_window_rect = rect();
  199. if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
  200. new_window_rect.set_width(m_main_widget->preferred_size().width());
  201. if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
  202. new_window_rect.set_height(m_main_widget->preferred_size().height());
  203. set_rect(new_window_rect);
  204. m_main_widget->set_relative_rect({ { }, new_window_rect.size() });
  205. m_main_widget->set_window(this);
  206. if (m_main_widget->accepts_focus())
  207. m_main_widget->set_focus(true);
  208. }
  209. update();
  210. }
  211. void GWindow::set_focused_widget(GWidget* widget)
  212. {
  213. if (m_focused_widget == widget)
  214. return;
  215. if (m_focused_widget) {
  216. GEventLoop::main().post_event(m_focused_widget, make<GEvent>(GEvent::FocusOut));
  217. m_focused_widget->update();
  218. }
  219. m_focused_widget = widget;
  220. if (m_focused_widget) {
  221. GEventLoop::main().post_event(m_focused_widget, make<GEvent>(GEvent::FocusIn));
  222. m_focused_widget->update();
  223. }
  224. }
  225. void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
  226. {
  227. ASSERT(m_window_id);
  228. if (widget == m_global_cursor_tracking_widget.ptr())
  229. return;
  230. m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  231. gui_set_global_cursor_tracking_enabled(m_window_id, widget != nullptr);
  232. }