GWidget.cpp 17 KB

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