GWidget.cpp 12 KB

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