GWidget.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. #include <AK/Assertions.h>
  2. #include <AK/JsonObject.h>
  3. #include <LibDraw/GraphicsBitmap.h>
  4. #include <LibGUI/GAction.h>
  5. #include <LibGUI/GApplication.h>
  6. #include <LibGUI/GEvent.h>
  7. #include <LibGUI/GLayout.h>
  8. #include <LibGUI/GMenu.h>
  9. #include <LibGUI/GPainter.h>
  10. #include <LibGUI/GWidget.h>
  11. #include <LibGUI/GWindow.h>
  12. #include <LibGUI/GWindowServerConnection.h>
  13. #include <unistd.h>
  14. #include <LibGUI/GButton.h>
  15. #include <LibGUI/GCheckBox.h>
  16. #include <LibGUI/GGroupBox.h>
  17. #include <LibGUI/GLabel.h>
  18. #include <LibGUI/GRadioButton.h>
  19. #include <LibGUI/GScrollBar.h>
  20. #include <LibGUI/GSlider.h>
  21. #include <LibGUI/GSpinBox.h>
  22. #include <LibGUI/GTextBox.h>
  23. REGISTER_GWIDGET(GButton)
  24. REGISTER_GWIDGET(GCheckBox)
  25. REGISTER_GWIDGET(GGroupBox)
  26. REGISTER_GWIDGET(GLabel)
  27. REGISTER_GWIDGET(GRadioButton)
  28. REGISTER_GWIDGET(GScrollBar)
  29. REGISTER_GWIDGET(GSlider)
  30. REGISTER_GWIDGET(GSpinBox)
  31. REGISTER_GWIDGET(GTextBox)
  32. REGISTER_GWIDGET(GWidget)
  33. static HashMap<String, GWidgetClassRegistration*>& widget_classes()
  34. {
  35. static HashMap<String, GWidgetClassRegistration*>* map;
  36. if (!map)
  37. map = new HashMap<String, GWidgetClassRegistration*>;
  38. return *map;
  39. }
  40. GWidgetClassRegistration::GWidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<GWidget>(GWidget*)> factory)
  41. : m_class_name(class_name)
  42. , m_factory(move(factory))
  43. {
  44. widget_classes().set(class_name, this);
  45. }
  46. GWidgetClassRegistration::~GWidgetClassRegistration()
  47. {
  48. ASSERT_NOT_REACHED();
  49. }
  50. void GWidgetClassRegistration::for_each(Function<void(const GWidgetClassRegistration&)> callback)
  51. {
  52. for (auto& it : widget_classes()) {
  53. callback(*it.value);
  54. }
  55. }
  56. const GWidgetClassRegistration* GWidgetClassRegistration::find(const String& class_name)
  57. {
  58. return widget_classes().get(class_name).value_or(nullptr);
  59. }
  60. GWidget::GWidget(GWidget* parent)
  61. : CObject(parent, true)
  62. , m_font(Font::default_font())
  63. {
  64. m_background_color = Color::WarmGray;
  65. m_foreground_color = Color::Black;
  66. }
  67. GWidget::~GWidget()
  68. {
  69. }
  70. void GWidget::child_event(CChildEvent& event)
  71. {
  72. if (event.type() == GEvent::ChildAdded) {
  73. if (event.child() && is<GWidget>(*event.child()) && layout()) {
  74. if (event.insertion_before_child() && event.insertion_before_child()->is_widget())
  75. layout()->insert_widget_before(to<GWidget>(*event.child()), to<GWidget>(*event.insertion_before_child()));
  76. else
  77. layout()->add_widget(to<GWidget>(*event.child()));
  78. }
  79. }
  80. if (event.type() == GEvent::ChildRemoved) {
  81. if (layout()) {
  82. if (event.child() && is<GWidget>(*event.child()))
  83. layout()->remove_widget(to<GWidget>(*event.child()));
  84. else
  85. invalidate_layout();
  86. }
  87. update();
  88. }
  89. return CObject::child_event(event);
  90. }
  91. void GWidget::set_relative_rect(const Rect& a_rect)
  92. {
  93. Rect rect = a_rect.is_empty() ? Rect() : a_rect;
  94. if (rect == m_relative_rect)
  95. return;
  96. auto old_rect = m_relative_rect;
  97. bool size_changed = m_relative_rect.size() != rect.size();
  98. m_relative_rect = rect;
  99. if (size_changed) {
  100. GResizeEvent resize_event(m_relative_rect.size(), rect.size());
  101. event(resize_event);
  102. }
  103. if (auto* parent = parent_widget())
  104. parent->update(old_rect);
  105. update();
  106. }
  107. void GWidget::event(CEvent& event)
  108. {
  109. switch (event.type()) {
  110. case GEvent::Paint:
  111. return handle_paint_event(static_cast<GPaintEvent&>(event));
  112. case GEvent::Resize:
  113. return handle_resize_event(static_cast<GResizeEvent&>(event));
  114. case GEvent::FocusIn:
  115. return focusin_event(event);
  116. case GEvent::FocusOut:
  117. return focusout_event(event);
  118. case GEvent::Show:
  119. return show_event(static_cast<GShowEvent&>(event));
  120. case GEvent::Hide:
  121. return hide_event(static_cast<GHideEvent&>(event));
  122. case GEvent::KeyDown:
  123. return keydown_event(static_cast<GKeyEvent&>(event));
  124. case GEvent::KeyUp:
  125. return keyup_event(static_cast<GKeyEvent&>(event));
  126. case GEvent::MouseMove:
  127. return mousemove_event(static_cast<GMouseEvent&>(event));
  128. case GEvent::MouseDown:
  129. return handle_mousedown_event(static_cast<GMouseEvent&>(event));
  130. case GEvent::MouseDoubleClick:
  131. return handle_mousedoubleclick_event(static_cast<GMouseEvent&>(event));
  132. case GEvent::MouseUp:
  133. return handle_mouseup_event(static_cast<GMouseEvent&>(event));
  134. case GEvent::MouseWheel:
  135. return mousewheel_event(static_cast<GMouseEvent&>(event));
  136. case GEvent::Enter:
  137. return handle_enter_event(event);
  138. case GEvent::Leave:
  139. return handle_leave_event(event);
  140. case GEvent::EnabledChange:
  141. return change_event(static_cast<GEvent&>(event));
  142. default:
  143. return CObject::event(event);
  144. }
  145. }
  146. void GWidget::handle_paint_event(GPaintEvent& event)
  147. {
  148. ASSERT(is_visible());
  149. if (fill_with_background_color()) {
  150. GPainter painter(*this);
  151. painter.fill_rect(event.rect(), background_color());
  152. } else {
  153. #ifdef DEBUG_WIDGET_UNDERDRAW
  154. // FIXME: This is a bit broken.
  155. // If the widget is not opaque, let's not mess it up with debugging color.
  156. GPainter painter(*this);
  157. painter.fill_rect(rect(), Color::Red);
  158. #endif
  159. }
  160. paint_event(event);
  161. for_each_child_widget([&](auto& child) {
  162. if (!child.is_visible())
  163. return IterationDecision::Continue;
  164. if (child.relative_rect().intersects(event.rect())) {
  165. GPaintEvent local_event(event.rect().intersected(child.relative_rect()).translated(-child.relative_position()));
  166. child.dispatch_event(local_event, this);
  167. }
  168. return IterationDecision::Continue;
  169. });
  170. second_paint_event(event);
  171. }
  172. void GWidget::set_layout(OwnPtr<GLayout>&& layout)
  173. {
  174. if (m_layout)
  175. m_layout->notify_disowned({}, *this);
  176. m_layout = move(layout);
  177. if (m_layout) {
  178. m_layout->notify_adopted({}, *this);
  179. do_layout();
  180. } else {
  181. update();
  182. }
  183. }
  184. void GWidget::do_layout()
  185. {
  186. for_each_child_widget([&](auto& child) {
  187. child.do_layout();
  188. return IterationDecision::Continue;
  189. });
  190. custom_layout();
  191. if (!m_layout)
  192. return;
  193. m_layout->run(*this);
  194. update();
  195. }
  196. void GWidget::notify_layout_changed(Badge<GLayout>)
  197. {
  198. invalidate_layout();
  199. }
  200. void GWidget::handle_resize_event(GResizeEvent& event)
  201. {
  202. do_layout();
  203. return resize_event(event);
  204. }
  205. void GWidget::handle_mouseup_event(GMouseEvent& event)
  206. {
  207. mouseup_event(event);
  208. }
  209. void GWidget::handle_mousedown_event(GMouseEvent& event)
  210. {
  211. if (accepts_focus())
  212. set_focus(true);
  213. mousedown_event(event);
  214. if (event.button() == GMouseButton::Right) {
  215. GContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
  216. context_menu_event(c_event);
  217. }
  218. }
  219. void GWidget::handle_mousedoubleclick_event(GMouseEvent& event)
  220. {
  221. doubleclick_event(event);
  222. }
  223. void GWidget::handle_enter_event(CEvent& event)
  224. {
  225. if (has_tooltip())
  226. GApplication::the().show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2));
  227. enter_event(event);
  228. }
  229. void GWidget::handle_leave_event(CEvent& event)
  230. {
  231. GApplication::the().hide_tooltip();
  232. leave_event(event);
  233. }
  234. void GWidget::click_event(GMouseEvent&)
  235. {
  236. }
  237. void GWidget::doubleclick_event(GMouseEvent&)
  238. {
  239. }
  240. void GWidget::resize_event(GResizeEvent&)
  241. {
  242. }
  243. void GWidget::paint_event(GPaintEvent&)
  244. {
  245. }
  246. void GWidget::second_paint_event(GPaintEvent&)
  247. {
  248. }
  249. void GWidget::show_event(GShowEvent&)
  250. {
  251. }
  252. void GWidget::hide_event(GHideEvent&)
  253. {
  254. }
  255. void GWidget::keydown_event(GKeyEvent& event)
  256. {
  257. if (!event.alt() && !event.ctrl() && !event.logo() && event.key() == KeyCode::Key_Tab) {
  258. if (event.shift())
  259. focus_previous_widget();
  260. else
  261. focus_next_widget();
  262. event.accept();
  263. return;
  264. }
  265. event.ignore();
  266. }
  267. void GWidget::keyup_event(GKeyEvent&)
  268. {
  269. }
  270. void GWidget::mousedown_event(GMouseEvent&)
  271. {
  272. }
  273. void GWidget::mouseup_event(GMouseEvent&)
  274. {
  275. }
  276. void GWidget::mousemove_event(GMouseEvent&)
  277. {
  278. }
  279. void GWidget::mousewheel_event(GMouseEvent&)
  280. {
  281. }
  282. void GWidget::context_menu_event(GContextMenuEvent&)
  283. {
  284. }
  285. void GWidget::focusin_event(CEvent&)
  286. {
  287. }
  288. void GWidget::focusout_event(CEvent&)
  289. {
  290. }
  291. void GWidget::enter_event(CEvent&)
  292. {
  293. }
  294. void GWidget::leave_event(CEvent&)
  295. {
  296. }
  297. void GWidget::change_event(GEvent&)
  298. {
  299. }
  300. void GWidget::update()
  301. {
  302. if (rect().is_empty())
  303. return;
  304. update(rect());
  305. }
  306. void GWidget::update(const Rect& rect)
  307. {
  308. if (!is_visible())
  309. return;
  310. if (!updates_enabled())
  311. return;
  312. GWindow* window = m_window;
  313. GWidget* parent = parent_widget();
  314. while (parent) {
  315. if (!parent->updates_enabled())
  316. return;
  317. window = parent->m_window;
  318. parent = parent->parent_widget();
  319. }
  320. if (window)
  321. window->update(rect.translated(window_relative_rect().location()));
  322. }
  323. Rect GWidget::window_relative_rect() const
  324. {
  325. auto rect = relative_rect();
  326. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  327. rect.move_by(parent->relative_position());
  328. }
  329. return rect;
  330. }
  331. Rect GWidget::screen_relative_rect() const
  332. {
  333. return window_relative_rect().translated(window()->position());
  334. }
  335. GWidget* GWidget::child_at(const Point& point) const
  336. {
  337. for (int i = children().size() - 1; i >= 0; --i) {
  338. if (!is<GWidget>(children()[i]))
  339. continue;
  340. auto& child = to<GWidget>(children()[i]);
  341. if (!child.is_visible())
  342. continue;
  343. if (child.relative_rect().contains(point))
  344. return const_cast<GWidget*>(&child);
  345. }
  346. return nullptr;
  347. }
  348. GWidget::HitTestResult GWidget::hit_test(const Point& position, ShouldRespectGreediness should_respect_greediness)
  349. {
  350. if (should_respect_greediness == ShouldRespectGreediness::Yes && is_greedy_for_hits())
  351. return { this, position };
  352. if (auto* child = child_at(position))
  353. return child->hit_test(position - child->relative_position());
  354. return { this, position };
  355. }
  356. void GWidget::set_window(GWindow* window)
  357. {
  358. if (m_window == window)
  359. return;
  360. m_window = window;
  361. }
  362. bool GWidget::is_focused() const
  363. {
  364. auto* win = window();
  365. if (!win)
  366. return false;
  367. if (!win->is_active())
  368. return false;
  369. return win->focused_widget() == this;
  370. }
  371. void GWidget::set_focus(bool focus)
  372. {
  373. auto* win = window();
  374. if (!win)
  375. return;
  376. if (focus) {
  377. win->set_focused_widget(this);
  378. } else {
  379. if (win->focused_widget() == this)
  380. win->set_focused_widget(nullptr);
  381. }
  382. }
  383. void GWidget::set_font(const Font* font)
  384. {
  385. if (m_font.ptr() == font)
  386. return;
  387. if (!font)
  388. m_font = Font::default_font();
  389. else
  390. m_font = *font;
  391. did_change_font();
  392. update();
  393. }
  394. void GWidget::set_global_cursor_tracking(bool enabled)
  395. {
  396. auto* win = window();
  397. if (!win)
  398. return;
  399. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  400. }
  401. bool GWidget::global_cursor_tracking() const
  402. {
  403. auto* win = window();
  404. if (!win)
  405. return false;
  406. return win->global_cursor_tracking_widget() == this;
  407. }
  408. void GWidget::set_preferred_size(const Size& size)
  409. {
  410. if (m_preferred_size == size)
  411. return;
  412. m_preferred_size = size;
  413. invalidate_layout();
  414. }
  415. void GWidget::set_size_policy(Orientation orientation, SizePolicy policy)
  416. {
  417. if (orientation == Orientation::Horizontal)
  418. set_size_policy(policy, m_vertical_size_policy);
  419. else
  420. set_size_policy(m_horizontal_size_policy, policy);
  421. }
  422. void GWidget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
  423. {
  424. if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
  425. return;
  426. m_horizontal_size_policy = horizontal_policy;
  427. m_vertical_size_policy = vertical_policy;
  428. invalidate_layout();
  429. }
  430. void GWidget::invalidate_layout()
  431. {
  432. if (window())
  433. window()->schedule_relayout();
  434. }
  435. void GWidget::set_visible(bool visible)
  436. {
  437. if (visible == m_visible)
  438. return;
  439. m_visible = visible;
  440. if (auto* parent = parent_widget())
  441. parent->invalidate_layout();
  442. if (m_visible)
  443. update();
  444. if (m_visible) {
  445. GShowEvent e;
  446. event(e);
  447. } else {
  448. GHideEvent e;
  449. event(e);
  450. }
  451. }
  452. bool GWidget::spans_entire_window_horizontally() const
  453. {
  454. auto* w = window();
  455. if (!w)
  456. return false;
  457. auto* main_widget = w->main_widget();
  458. if (!main_widget)
  459. return false;
  460. if (main_widget == this)
  461. return true;
  462. auto wrr = window_relative_rect();
  463. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  464. }
  465. void GWidget::set_enabled(bool enabled)
  466. {
  467. if (m_enabled == enabled)
  468. return;
  469. m_enabled = enabled;
  470. GEvent e(GEvent::EnabledChange);
  471. event(e);
  472. update();
  473. }
  474. void GWidget::move_to_front()
  475. {
  476. auto* parent = parent_widget();
  477. if (!parent)
  478. return;
  479. if (parent->children().size() == 1)
  480. return;
  481. parent->children().remove_first_matching([this](auto& entry) {
  482. return entry == this;
  483. });
  484. parent->children().append(*this);
  485. parent->update();
  486. }
  487. void GWidget::move_to_back()
  488. {
  489. auto* parent = parent_widget();
  490. if (!parent)
  491. return;
  492. if (parent->children().size() == 1)
  493. return;
  494. parent->children().remove_first_matching([this](auto& entry) {
  495. return entry == this;
  496. });
  497. parent->children().prepend(*this);
  498. parent->update();
  499. }
  500. bool GWidget::is_frontmost() const
  501. {
  502. auto* parent = parent_widget();
  503. if (!parent)
  504. return true;
  505. return &parent->children().last() == this;
  506. }
  507. bool GWidget::is_backmost() const
  508. {
  509. auto* parent = parent_widget();
  510. if (!parent)
  511. return true;
  512. return &parent->children().first() == this;
  513. }
  514. GAction* GWidget::action_for_key_event(const GKeyEvent& event)
  515. {
  516. auto it = m_local_shortcut_actions.find(GShortcut(event.modifiers(), (KeyCode)event.key()));
  517. if (it == m_local_shortcut_actions.end())
  518. return nullptr;
  519. return (*it).value;
  520. }
  521. void GWidget::register_local_shortcut_action(Badge<GAction>, GAction& action)
  522. {
  523. m_local_shortcut_actions.set(action.shortcut(), &action);
  524. }
  525. void GWidget::unregister_local_shortcut_action(Badge<GAction>, GAction& action)
  526. {
  527. m_local_shortcut_actions.remove(action.shortcut());
  528. }
  529. void GWidget::set_updates_enabled(bool enabled)
  530. {
  531. if (m_updates_enabled == enabled)
  532. return;
  533. m_updates_enabled = enabled;
  534. if (enabled)
  535. update();
  536. }
  537. void GWidget::focus_previous_widget()
  538. {
  539. auto focusable_widgets = window()->focusable_widgets();
  540. for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
  541. if (focusable_widgets[i] != this)
  542. continue;
  543. if (i > 0)
  544. focusable_widgets[i - 1]->set_focus(true);
  545. else
  546. focusable_widgets.last()->set_focus(true);
  547. }
  548. }
  549. void GWidget::focus_next_widget()
  550. {
  551. auto focusable_widgets = window()->focusable_widgets();
  552. for (int i = 0; i < focusable_widgets.size(); ++i) {
  553. if (focusable_widgets[i] != this)
  554. continue;
  555. if (i < focusable_widgets.size() - 1)
  556. focusable_widgets[i + 1]->set_focus(true);
  557. else
  558. focusable_widgets.first()->set_focus(true);
  559. }
  560. }
  561. void GWidget::set_backcolor(const StringView& color_string)
  562. {
  563. auto color = Color::from_string(color_string);
  564. if (!color.has_value())
  565. return;
  566. set_background_color(color.value());
  567. }
  568. void GWidget::set_forecolor(const StringView& color_string)
  569. {
  570. auto color = Color::from_string(color_string);
  571. if (!color.has_value())
  572. return;
  573. set_foreground_color(color.value());
  574. }
  575. void GWidget::save_to(AK::JsonObject& json)
  576. {
  577. json.set("relative_rect", relative_rect().to_string());
  578. json.set("fill_with_background_color", fill_with_background_color());
  579. json.set("tooltip", tooltip());
  580. json.set("visible", is_visible());
  581. json.set("focused", is_focused());
  582. json.set("enabled", is_enabled());
  583. json.set("background_color", background_color().to_string());
  584. json.set("foreground_color", foreground_color().to_string());
  585. json.set("preferred_size", preferred_size().to_string());
  586. json.set("size_policy", String::format("[%s,%s]", to_string(horizontal_size_policy()), to_string(vertical_size_policy())));
  587. CObject::save_to(json);
  588. }