GWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #include "GWidget.h"
  2. #include "GEvent.h"
  3. #include "GEventLoop.h"
  4. #include "GWindow.h"
  5. #include <LibGUI/GLayout.h>
  6. #include <AK/Assertions.h>
  7. #include <SharedGraphics/GraphicsBitmap.h>
  8. #include <LibGUI/GPainter.h>
  9. #include <LibGUI/GApplication.h>
  10. #include <LibGUI/GMenu.h>
  11. #include <unistd.h>
  12. GWidget::GWidget(GWidget* parent)
  13. : CObject(parent)
  14. {
  15. set_font(nullptr);
  16. m_background_color = Color::LightGray;
  17. m_foreground_color = Color::Black;
  18. }
  19. GWidget::~GWidget()
  20. {
  21. }
  22. void GWidget::child_event(CChildEvent& event)
  23. {
  24. if (event.type() == GEvent::ChildAdded) {
  25. if (event.child() && event.child()->is_widget() && layout())
  26. layout()->add_widget(static_cast<GWidget&>(*event.child()));
  27. }
  28. if (event.type() == GEvent::ChildRemoved) {
  29. if (layout()) {
  30. if (event.child() && event.child()->is_widget())
  31. layout()->remove_widget(static_cast<GWidget&>(*event.child()));
  32. else
  33. invalidate_layout();
  34. }
  35. }
  36. return CObject::child_event(event);
  37. }
  38. void GWidget::set_relative_rect(const Rect& rect)
  39. {
  40. if (rect == m_relative_rect)
  41. return;
  42. auto old_rect = m_relative_rect;
  43. bool size_changed = m_relative_rect.size() != rect.size();
  44. m_relative_rect = rect;
  45. if (size_changed) {
  46. GResizeEvent resize_event(m_relative_rect.size(), rect.size());
  47. event(resize_event);
  48. }
  49. if (auto* parent = parent_widget())
  50. parent->update(old_rect);
  51. update();
  52. }
  53. void GWidget::event(CEvent& event)
  54. {
  55. switch (event.type()) {
  56. case GEvent::Paint:
  57. return handle_paint_event(static_cast<GPaintEvent&>(event));
  58. case GEvent::Resize:
  59. return handle_resize_event(static_cast<GResizeEvent&>(event));
  60. case GEvent::FocusIn:
  61. return focusin_event(event);
  62. case GEvent::FocusOut:
  63. return focusout_event(event);
  64. case GEvent::Show:
  65. return show_event(static_cast<GShowEvent&>(event));
  66. case GEvent::Hide:
  67. return hide_event(static_cast<GHideEvent&>(event));
  68. case GEvent::KeyDown:
  69. return keydown_event(static_cast<GKeyEvent&>(event));
  70. case GEvent::KeyUp:
  71. return keyup_event(static_cast<GKeyEvent&>(event));
  72. case GEvent::MouseMove:
  73. return mousemove_event(static_cast<GMouseEvent&>(event));
  74. case GEvent::MouseDown:
  75. return handle_mousedown_event(static_cast<GMouseEvent&>(event));
  76. case GEvent::MouseUp:
  77. return handle_mouseup_event(static_cast<GMouseEvent&>(event));
  78. case GEvent::Enter:
  79. return handle_enter_event(event);
  80. case GEvent::Leave:
  81. return handle_leave_event(event);
  82. default:
  83. return CObject::event(event);
  84. }
  85. }
  86. void GWidget::handle_paint_event(GPaintEvent& event)
  87. {
  88. ASSERT(is_visible());
  89. if (fill_with_background_color()) {
  90. GPainter painter(*this);
  91. painter.fill_rect(event.rect(), background_color());
  92. } else {
  93. #ifdef DEBUG_WIDGET_UNDERDRAW
  94. // FIXME: This is a bit broken.
  95. // If the widget is not opaque, let's not mess it up with debugging color.
  96. GPainter painter(*this);
  97. painter.fill_rect(rect(), Color::Red);
  98. #endif
  99. }
  100. paint_event(event);
  101. for (auto* ch : children()) {
  102. auto* child = (GWidget*)ch;
  103. if (!child->is_visible())
  104. continue;
  105. if (child->relative_rect().intersects(event.rect())) {
  106. GPaintEvent local_event(event.rect().intersected(child->relative_rect()).translated(-child->relative_position()));
  107. child->event(local_event);
  108. }
  109. }
  110. second_paint_event(event);
  111. }
  112. void GWidget::set_layout(OwnPtr<GLayout>&& layout)
  113. {
  114. if (m_layout)
  115. m_layout->notify_disowned(Badge<GWidget>(), *this);
  116. m_layout = move(layout);
  117. if (m_layout) {
  118. m_layout->notify_adopted(Badge<GWidget>(), *this);
  119. do_layout();
  120. } else {
  121. update();
  122. }
  123. }
  124. void GWidget::do_layout()
  125. {
  126. if (!m_layout)
  127. return;
  128. m_layout->run(*this);
  129. update();
  130. }
  131. void GWidget::notify_layout_changed(Badge<GLayout>)
  132. {
  133. do_layout();
  134. }
  135. void GWidget::handle_resize_event(GResizeEvent& event)
  136. {
  137. if (layout())
  138. do_layout();
  139. return resize_event(event);
  140. }
  141. void GWidget::handle_mouseup_event(GMouseEvent& event)
  142. {
  143. mouseup_event(event);
  144. if (!rect().contains(event.position()))
  145. return;
  146. // It's a click.. but is it a doubleclick?
  147. // FIXME: This needs improvement.
  148. if (m_click_clock.is_valid()) {
  149. int elapsed_since_last_click = m_click_clock.elapsed();
  150. #if 0
  151. dbgprintf("Click clock elapsed: %d\n", m_click_clock.elapsed());
  152. #endif
  153. if (elapsed_since_last_click < 250) {
  154. doubleclick_event(event);
  155. } else {
  156. m_click_clock.start();
  157. }
  158. }
  159. }
  160. void GWidget::handle_mousedown_event(GMouseEvent& event)
  161. {
  162. if (accepts_focus())
  163. set_focus(true);
  164. // FIXME: Maybe the click clock should be per-button.
  165. if (!m_click_clock.is_valid())
  166. m_click_clock.start();
  167. mousedown_event(event);
  168. if (event.button() == GMouseButton::Right) {
  169. GContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
  170. context_menu_event(c_event);
  171. }
  172. }
  173. void GWidget::handle_enter_event(CEvent& event)
  174. {
  175. if (has_tooltip())
  176. GApplication::the().show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2));
  177. enter_event(event);
  178. }
  179. void GWidget::handle_leave_event(CEvent& event)
  180. {
  181. GApplication::the().hide_tooltip();
  182. leave_event(event);
  183. }
  184. void GWidget::click_event(GMouseEvent&)
  185. {
  186. }
  187. void GWidget::doubleclick_event(GMouseEvent&)
  188. {
  189. }
  190. void GWidget::resize_event(GResizeEvent&)
  191. {
  192. }
  193. void GWidget::paint_event(GPaintEvent&)
  194. {
  195. }
  196. void GWidget::second_paint_event(GPaintEvent&)
  197. {
  198. }
  199. void GWidget::show_event(GShowEvent&)
  200. {
  201. }
  202. void GWidget::hide_event(GHideEvent&)
  203. {
  204. }
  205. void GWidget::keydown_event(GKeyEvent&)
  206. {
  207. }
  208. void GWidget::keyup_event(GKeyEvent&)
  209. {
  210. }
  211. void GWidget::mousedown_event(GMouseEvent&)
  212. {
  213. }
  214. void GWidget::mouseup_event(GMouseEvent&)
  215. {
  216. }
  217. void GWidget::mousemove_event(GMouseEvent&)
  218. {
  219. }
  220. void GWidget::context_menu_event(GContextMenuEvent&)
  221. {
  222. }
  223. void GWidget::focusin_event(CEvent&)
  224. {
  225. }
  226. void GWidget::focusout_event(CEvent&)
  227. {
  228. }
  229. void GWidget::enter_event(CEvent&)
  230. {
  231. }
  232. void GWidget::leave_event(CEvent&)
  233. {
  234. }
  235. void GWidget::update()
  236. {
  237. update(rect());
  238. }
  239. void GWidget::update(const Rect& rect)
  240. {
  241. if (!is_visible())
  242. return;
  243. auto* w = window();
  244. if (!w)
  245. return;
  246. w->update(rect.translated(window_relative_rect().location()));
  247. }
  248. Rect GWidget::window_relative_rect() const
  249. {
  250. auto rect = relative_rect();
  251. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  252. rect.move_by(parent->relative_position());
  253. }
  254. return rect;
  255. }
  256. Rect GWidget::screen_relative_rect() const
  257. {
  258. return window_relative_rect().translated(window()->position());
  259. }
  260. GWidget* GWidget::child_at(const Point& point) const
  261. {
  262. for (int i = children().size() - 1; i >= 0; --i) {
  263. if (!children()[i]->is_widget())
  264. continue;
  265. auto& child = *(GWidget*)children()[i];
  266. if (!child.is_visible())
  267. continue;
  268. if (child.relative_rect().contains(point))
  269. return &child;
  270. }
  271. return nullptr;
  272. }
  273. GWidget::HitTestResult GWidget::hit_test(const Point& position)
  274. {
  275. if (is_greedy_for_hits())
  276. return { this, position };
  277. if (auto* child = child_at(position))
  278. return child->hit_test(position - child->relative_position());
  279. return { this, position };
  280. }
  281. void GWidget::set_window(GWindow* window)
  282. {
  283. if (m_window == window)
  284. return;
  285. m_window = window;
  286. }
  287. bool GWidget::is_focused() const
  288. {
  289. auto* win = window();
  290. if (!win)
  291. return false;
  292. if (!win->is_active())
  293. return false;
  294. return win->focused_widget() == this;
  295. }
  296. void GWidget::set_focus(bool focus)
  297. {
  298. auto* win = window();
  299. if (!win)
  300. return;
  301. if (focus) {
  302. win->set_focused_widget(this);
  303. } else {
  304. if (win->focused_widget() == this)
  305. win->set_focused_widget(nullptr);
  306. }
  307. }
  308. void GWidget::set_font(RetainPtr<Font>&& font)
  309. {
  310. if (!font)
  311. m_font = Font::default_font();
  312. else
  313. m_font = move(font);
  314. update();
  315. }
  316. void GWidget::set_global_cursor_tracking(bool enabled)
  317. {
  318. auto* win = window();
  319. if (!win)
  320. return;
  321. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  322. }
  323. bool GWidget::global_cursor_tracking() const
  324. {
  325. auto* win = window();
  326. if (!win)
  327. return false;
  328. return win->global_cursor_tracking_widget() == this;
  329. }
  330. void GWidget::set_preferred_size(const Size& size)
  331. {
  332. if (m_preferred_size == size)
  333. return;
  334. m_preferred_size = size;
  335. invalidate_layout();
  336. }
  337. void GWidget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
  338. {
  339. if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
  340. return;
  341. m_horizontal_size_policy = horizontal_policy;
  342. m_vertical_size_policy = vertical_policy;
  343. invalidate_layout();
  344. }
  345. void GWidget::invalidate_layout()
  346. {
  347. auto* w = window();
  348. if (!w)
  349. return;
  350. if (!w->main_widget())
  351. return;
  352. do_layout();
  353. w->main_widget()->do_layout();
  354. }
  355. void GWidget::set_visible(bool visible)
  356. {
  357. if (visible == m_visible)
  358. return;
  359. m_visible = visible;
  360. if (auto* parent = parent_widget())
  361. parent->invalidate_layout();
  362. if (m_visible)
  363. update();
  364. }
  365. bool GWidget::spans_entire_window_horizontally() const
  366. {
  367. auto* w = window();
  368. if (!w)
  369. return false;
  370. auto* main_widget = w->main_widget();
  371. if (!main_widget)
  372. return false;
  373. if (main_widget == this)
  374. return true;
  375. auto wrr = window_relative_rect();
  376. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  377. }
  378. void GWidget::set_enabled(bool enabled)
  379. {
  380. if (m_enabled == enabled)
  381. return;
  382. m_enabled = enabled;
  383. update();
  384. }
  385. void GWidget::move_to_front()
  386. {
  387. auto* parent = parent_widget();
  388. if (!parent)
  389. return;
  390. if (parent->children().size() == 1)
  391. return;
  392. parent->children().remove_first_matching([this] (auto& entry) {
  393. return entry == this;
  394. });
  395. parent->children().append(this);
  396. parent->update();
  397. }
  398. void GWidget::move_to_back()
  399. {
  400. auto* parent = parent_widget();
  401. if (!parent)
  402. return;
  403. if (parent->children().size() == 1)
  404. return;
  405. parent->children().remove_first_matching([this] (auto& entry) {
  406. return entry == this;
  407. });
  408. parent->children().prepend(this);
  409. parent->update();
  410. }
  411. bool GWidget::is_frontmost() const
  412. {
  413. auto* parent = parent_widget();
  414. if (!parent)
  415. return true;
  416. return parent->children().last() == this;
  417. }
  418. bool GWidget::is_backmost() const
  419. {
  420. auto* parent = parent_widget();
  421. if (!parent)
  422. return true;
  423. return parent->children().first() == this;
  424. }