RadioButtonPaintable.cpp 4.4 KB

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