TextPaintable.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/HTML/BrowsingContext.h>
  7. #include <LibWeb/Layout/Label.h>
  8. #include <LibWeb/Layout/LabelableNode.h>
  9. #include <LibWeb/Page/EventHandler.h>
  10. #include <LibWeb/Painting/TextPaintable.h>
  11. namespace Web::Painting {
  12. JS::NonnullGCPtr<TextPaintable> TextPaintable::create(Layout::TextNode const& layout_node, String const& text_for_rendering)
  13. {
  14. return layout_node.heap().allocate_without_realm<TextPaintable>(layout_node, text_for_rendering);
  15. }
  16. TextPaintable::TextPaintable(Layout::TextNode const& layout_node, String const& text_for_rendering)
  17. : Paintable(layout_node)
  18. , m_text_for_rendering(text_for_rendering)
  19. {
  20. }
  21. bool TextPaintable::wants_mouse_events() const
  22. {
  23. return layout_node().first_ancestor_of_type<Layout::Label>();
  24. }
  25. DOM::Node* TextPaintable::mouse_event_target() const
  26. {
  27. if (auto* label = layout_node().first_ancestor_of_type<Layout::Label>()) {
  28. if (auto* control = const_cast<Layout::Label*>(label)->labeled_control())
  29. return &control->dom_node();
  30. }
  31. return nullptr;
  32. }
  33. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mousedown(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  34. {
  35. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  36. if (!label)
  37. return DispatchEventOfSameName::No;
  38. const_cast<Layout::Label*>(label)->handle_mousedown_on_label({}, position, button);
  39. const_cast<HTML::BrowsingContext&>(browsing_context()).event_handler().set_mouse_event_tracking_paintable(this);
  40. return DispatchEventOfSameName::Yes;
  41. }
  42. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mouseup(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  43. {
  44. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  45. if (!label)
  46. return DispatchEventOfSameName::No;
  47. const_cast<Layout::Label*>(label)->handle_mouseup_on_label({}, position, button);
  48. const_cast<HTML::BrowsingContext&>(browsing_context()).event_handler().set_mouse_event_tracking_paintable(nullptr);
  49. return DispatchEventOfSameName::Yes;
  50. }
  51. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mousemove(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  52. {
  53. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  54. if (!label)
  55. return DispatchEventOfSameName::No;
  56. const_cast<Layout::Label*>(label)->handle_mousemove_on_label({}, position, button);
  57. return DispatchEventOfSameName::Yes;
  58. }
  59. }