CheckBox.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Event.h>
  7. #include <LibGfx/Font.h>
  8. #include <LibGfx/Painter.h>
  9. #include <LibGfx/StylePainter.h>
  10. #include <LibWeb/HTML/BrowsingContext.h>
  11. #include <LibWeb/HTML/HTMLInputElement.h>
  12. #include <LibWeb/Layout/CheckBox.h>
  13. #include <LibWeb/Layout/Label.h>
  14. #include <LibWeb/Painting/CheckBoxPaintable.h>
  15. namespace Web::Layout {
  16. CheckBox::CheckBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> style)
  17. : LabelableNode(document, element, move(style))
  18. {
  19. set_intrinsic_width(13);
  20. set_intrinsic_height(13);
  21. }
  22. CheckBox::~CheckBox()
  23. {
  24. }
  25. void CheckBox::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned)
  26. {
  27. if (button != GUI::MouseButton::Primary || !dom_node().enabled())
  28. return;
  29. m_being_pressed = true;
  30. set_needs_display();
  31. m_tracking_mouse = true;
  32. browsing_context().event_handler().set_mouse_event_tracking_layout_node(this);
  33. }
  34. void CheckBox::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  35. {
  36. if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !dom_node().enabled())
  37. return;
  38. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  39. NonnullRefPtr protect = *this;
  40. bool is_inside_node_or_label = enclosing_int_rect(paint_box()->absolute_rect()).contains(position);
  41. if (!is_inside_node_or_label)
  42. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  43. if (is_inside_node_or_label) {
  44. dom_node().did_click_checkbox({});
  45. dom_node().set_checked(!dom_node().checked(), HTML::HTMLInputElement::ChangeSource::User);
  46. }
  47. m_being_pressed = false;
  48. m_tracking_mouse = false;
  49. browsing_context().event_handler().set_mouse_event_tracking_layout_node(nullptr);
  50. }
  51. void CheckBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  52. {
  53. if (!m_tracking_mouse || !dom_node().enabled())
  54. return;
  55. bool is_inside_node_or_label = enclosing_int_rect(paint_box()->absolute_rect()).contains(position);
  56. if (!is_inside_node_or_label)
  57. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  58. if (m_being_pressed == is_inside_node_or_label)
  59. return;
  60. m_being_pressed = is_inside_node_or_label;
  61. set_needs_display();
  62. }
  63. void CheckBox::handle_associated_label_mousedown(Badge<Label>)
  64. {
  65. if (!dom_node().enabled())
  66. return;
  67. m_being_pressed = true;
  68. set_needs_display();
  69. }
  70. void CheckBox::handle_associated_label_mouseup(Badge<Label>)
  71. {
  72. if (!dom_node().enabled())
  73. return;
  74. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  75. NonnullRefPtr protect = *this;
  76. dom_node().did_click_checkbox({});
  77. dom_node().set_checked(!dom_node().checked(), HTML::HTMLInputElement::ChangeSource::User);
  78. m_being_pressed = false;
  79. }
  80. void CheckBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label)
  81. {
  82. if (m_being_pressed == is_inside_node_or_label || !dom_node().enabled())
  83. return;
  84. m_being_pressed = is_inside_node_or_label;
  85. set_needs_display();
  86. }
  87. RefPtr<Painting::Paintable> CheckBox::create_paintable() const
  88. {
  89. return Painting::CheckBoxPaintable::create(*this);
  90. }
  91. }