GWindow.cpp 3.6 KB

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