RadioButton.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Event.h>
  7. #include <LibGfx/Painter.h>
  8. #include <LibGfx/StylePainter.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/Layout/Label.h>
  11. #include <LibWeb/Layout/RadioButton.h>
  12. #include <LibWeb/Page/BrowsingContext.h>
  13. namespace Web::Layout {
  14. RadioButton::RadioButton(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> style)
  15. : LabelableNode(document, element, move(style))
  16. {
  17. set_has_intrinsic_width(true);
  18. set_has_intrinsic_height(true);
  19. set_intrinsic_width(12);
  20. set_intrinsic_height(12);
  21. }
  22. RadioButton::~RadioButton()
  23. {
  24. }
  25. void RadioButton::paint(PaintContext& context, PaintPhase phase)
  26. {
  27. if (!is_visible())
  28. return;
  29. LabelableNode::paint(context, phase);
  30. if (phase == PaintPhase::Foreground) {
  31. Gfx::StylePainter::paint_radio_button(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), dom_node().checked(), m_being_pressed);
  32. }
  33. }
  34. void RadioButton::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned)
  35. {
  36. if (button != GUI::MouseButton::Left || !dom_node().enabled())
  37. return;
  38. m_being_pressed = true;
  39. set_needs_display();
  40. m_tracking_mouse = true;
  41. browsing_context().event_handler().set_mouse_event_tracking_layout_node(this);
  42. }
  43. void RadioButton::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  44. {
  45. if (!m_tracking_mouse || button != GUI::MouseButton::Left || !dom_node().enabled())
  46. return;
  47. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  48. NonnullRefPtr protect = *this;
  49. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  50. if (!is_inside_node_or_label)
  51. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  52. if (is_inside_node_or_label)
  53. set_checked_within_group();
  54. m_being_pressed = false;
  55. m_tracking_mouse = false;
  56. browsing_context().event_handler().set_mouse_event_tracking_layout_node(nullptr);
  57. }
  58. void RadioButton::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  59. {
  60. if (!m_tracking_mouse || !dom_node().enabled())
  61. return;
  62. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  63. if (!is_inside_node_or_label)
  64. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  65. if (m_being_pressed == is_inside_node_or_label)
  66. return;
  67. m_being_pressed = is_inside_node_or_label;
  68. set_needs_display();
  69. }
  70. void RadioButton::handle_associated_label_mousedown(Badge<Label>)
  71. {
  72. m_being_pressed = true;
  73. set_needs_display();
  74. }
  75. void RadioButton::handle_associated_label_mouseup(Badge<Label>)
  76. {
  77. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  78. NonnullRefPtr protect = *this;
  79. set_checked_within_group();
  80. m_being_pressed = false;
  81. }
  82. void RadioButton::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label)
  83. {
  84. if (m_being_pressed == is_inside_node_or_label)
  85. return;
  86. m_being_pressed = is_inside_node_or_label;
  87. set_needs_display();
  88. }
  89. void RadioButton::set_checked_within_group()
  90. {
  91. if (dom_node().checked())
  92. return;
  93. dom_node().set_checked(true);
  94. String name = dom_node().name();
  95. document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
  96. if (element.checked() && (element.layout_node() != this) && (element.name() == name))
  97. element.set_checked(false);
  98. return IterationDecision::Continue;
  99. });
  100. }
  101. }