CheckBox.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. namespace Web::Layout {
  15. CheckBox::CheckBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> style)
  16. : LabelableNode(document, element, move(style))
  17. {
  18. set_intrinsic_width(13);
  19. set_intrinsic_height(13);
  20. }
  21. CheckBox::~CheckBox()
  22. {
  23. }
  24. void CheckBox::paint(PaintContext& context, PaintPhase phase)
  25. {
  26. if (!is_visible())
  27. return;
  28. LabelableNode::paint(context, phase);
  29. if (phase == PaintPhase::Foreground) {
  30. Gfx::StylePainter::paint_check_box(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), dom_node().enabled(), dom_node().checked(), m_being_pressed);
  31. }
  32. }
  33. void CheckBox::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned)
  34. {
  35. if (button != GUI::MouseButton::Primary || !dom_node().enabled())
  36. return;
  37. m_being_pressed = true;
  38. set_needs_display();
  39. m_tracking_mouse = true;
  40. browsing_context().event_handler().set_mouse_event_tracking_layout_node(this);
  41. }
  42. void CheckBox::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  43. {
  44. if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !dom_node().enabled())
  45. return;
  46. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  47. NonnullRefPtr protect = *this;
  48. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  49. if (!is_inside_node_or_label)
  50. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  51. if (is_inside_node_or_label) {
  52. dom_node().did_click_checkbox({});
  53. dom_node().set_checked(!dom_node().checked(), HTML::HTMLInputElement::ChangeSource::User);
  54. }
  55. m_being_pressed = false;
  56. m_tracking_mouse = false;
  57. browsing_context().event_handler().set_mouse_event_tracking_layout_node(nullptr);
  58. }
  59. void CheckBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  60. {
  61. if (!m_tracking_mouse || !dom_node().enabled())
  62. return;
  63. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  64. if (!is_inside_node_or_label)
  65. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  66. if (m_being_pressed == is_inside_node_or_label)
  67. return;
  68. m_being_pressed = is_inside_node_or_label;
  69. set_needs_display();
  70. }
  71. void CheckBox::handle_associated_label_mousedown(Badge<Label>)
  72. {
  73. if (!dom_node().enabled())
  74. return;
  75. m_being_pressed = true;
  76. set_needs_display();
  77. }
  78. void CheckBox::handle_associated_label_mouseup(Badge<Label>)
  79. {
  80. if (!dom_node().enabled())
  81. return;
  82. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  83. NonnullRefPtr protect = *this;
  84. dom_node().did_click_checkbox({});
  85. dom_node().set_checked(!dom_node().checked(), HTML::HTMLInputElement::ChangeSource::User);
  86. m_being_pressed = false;
  87. }
  88. void CheckBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label)
  89. {
  90. if (m_being_pressed == is_inside_node_or_label || !dom_node().enabled())
  91. return;
  92. m_being_pressed = is_inside_node_or_label;
  93. set_needs_display();
  94. }
  95. }