CheckBox.cpp 3.3 KB

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