CheckBox.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2020, 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/Paintable.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::paint(PaintContext& context, Painting::PaintPhase phase)
  26. {
  27. if (!is_visible())
  28. return;
  29. LabelableNode::paint(context, phase);
  30. if (phase == Painting::PaintPhase::Foreground) {
  31. Gfx::StylePainter::paint_check_box(context.painter(), enclosing_int_rect(m_paint_box->absolute_rect()), context.palette(), dom_node().enabled(), dom_node().checked(), m_being_pressed);
  32. }
  33. }
  34. void CheckBox::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned)
  35. {
  36. if (button != GUI::MouseButton::Primary || !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 CheckBox::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  44. {
  45. if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !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(m_paint_box->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. dom_node().did_click_checkbox({});
  54. dom_node().set_checked(!dom_node().checked(), HTML::HTMLInputElement::ChangeSource::User);
  55. }
  56. m_being_pressed = false;
  57. m_tracking_mouse = false;
  58. browsing_context().event_handler().set_mouse_event_tracking_layout_node(nullptr);
  59. }
  60. void CheckBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  61. {
  62. if (!m_tracking_mouse || !dom_node().enabled())
  63. return;
  64. bool is_inside_node_or_label = enclosing_int_rect(m_paint_box->absolute_rect()).contains(position);
  65. if (!is_inside_node_or_label)
  66. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  67. if (m_being_pressed == is_inside_node_or_label)
  68. return;
  69. m_being_pressed = is_inside_node_or_label;
  70. set_needs_display();
  71. }
  72. void CheckBox::handle_associated_label_mousedown(Badge<Label>)
  73. {
  74. if (!dom_node().enabled())
  75. return;
  76. m_being_pressed = true;
  77. set_needs_display();
  78. }
  79. void CheckBox::handle_associated_label_mouseup(Badge<Label>)
  80. {
  81. if (!dom_node().enabled())
  82. return;
  83. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  84. NonnullRefPtr protect = *this;
  85. dom_node().did_click_checkbox({});
  86. dom_node().set_checked(!dom_node().checked(), HTML::HTMLInputElement::ChangeSource::User);
  87. m_being_pressed = false;
  88. }
  89. void CheckBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label)
  90. {
  91. if (m_being_pressed == is_inside_node_or_label || !dom_node().enabled())
  92. return;
  93. m_being_pressed = is_inside_node_or_label;
  94. set_needs_display();
  95. }
  96. }