CheckBox.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/Layout/CheckBox.h>
  11. #include <LibWeb/Layout/Label.h>
  12. #include <LibWeb/Page/BrowsingContext.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_has_intrinsic_width(true);
  18. set_has_intrinsic_height(true);
  19. set_intrinsic_width(13);
  20. set_intrinsic_height(13);
  21. }
  22. CheckBox::~CheckBox()
  23. {
  24. }
  25. void CheckBox::paint(PaintContext& context, PaintPhase phase)
  26. {
  27. if (!is_visible())
  28. return;
  29. LabelableNode::paint(context, phase);
  30. if (phase == PaintPhase::Foreground) {
  31. Gfx::StylePainter::paint_check_box(context.painter(), enclosing_int_rect(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::Left || !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::Left || !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(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().set_checked(!dom_node().checked());
  54. m_being_pressed = false;
  55. m_tracking_mouse = false;
  56. browsing_context().event_handler().set_mouse_event_tracking_layout_node(nullptr);
  57. }
  58. void CheckBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  59. {
  60. if (!m_tracking_mouse || !dom_node().enabled())
  61. return;
  62. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  63. if (!is_inside_node_or_label)
  64. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  65. if (m_being_pressed == is_inside_node_or_label)
  66. return;
  67. m_being_pressed = is_inside_node_or_label;
  68. set_needs_display();
  69. }
  70. void CheckBox::handle_associated_label_mousedown(Badge<Label>)
  71. {
  72. m_being_pressed = true;
  73. set_needs_display();
  74. }
  75. void CheckBox::handle_associated_label_mouseup(Badge<Label>)
  76. {
  77. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  78. NonnullRefPtr protect = *this;
  79. dom_node().set_checked(!dom_node().checked());
  80. m_being_pressed = false;
  81. }
  82. void CheckBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label)
  83. {
  84. if (m_being_pressed == is_inside_node_or_label)
  85. return;
  86. m_being_pressed = is_inside_node_or_label;
  87. set_needs_display();
  88. }
  89. }