GWidget.cpp 13 KB

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