TextPaintable.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.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_DEFINE_ALLOCATOR(TextPaintable);
  13. JS::NonnullGCPtr<TextPaintable> TextPaintable::create(Layout::TextNode const& layout_node, String const& text_for_rendering)
  14. {
  15. return layout_node.heap().allocate<TextPaintable>(layout_node, text_for_rendering);
  16. }
  17. TextPaintable::TextPaintable(Layout::TextNode const& layout_node, String const& text_for_rendering)
  18. : Paintable(layout_node)
  19. , m_text_for_rendering(text_for_rendering)
  20. {
  21. }
  22. bool TextPaintable::wants_mouse_events() const
  23. {
  24. return layout_node().first_ancestor_of_type<Layout::Label>();
  25. }
  26. DOM::Node* TextPaintable::mouse_event_target() const
  27. {
  28. if (auto const* label = layout_node().first_ancestor_of_type<Layout::Label>())
  29. return label->dom_node().control().ptr();
  30. return nullptr;
  31. }
  32. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mousedown(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  33. {
  34. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  35. if (!label)
  36. return DispatchEventOfSameName::No;
  37. const_cast<Layout::Label*>(label)->handle_mousedown_on_label({}, position, button);
  38. const_cast<HTML::Navigable&>(*navigable()).event_handler().set_mouse_event_tracking_paintable(this);
  39. return DispatchEventOfSameName::Yes;
  40. }
  41. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mouseup(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  42. {
  43. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  44. if (!label)
  45. return DispatchEventOfSameName::No;
  46. const_cast<Layout::Label*>(label)->handle_mouseup_on_label({}, position, button);
  47. const_cast<HTML::Navigable&>(*navigable()).event_handler().set_mouse_event_tracking_paintable(nullptr);
  48. return DispatchEventOfSameName::Yes;
  49. }
  50. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mousemove(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  51. {
  52. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  53. if (!label)
  54. return DispatchEventOfSameName::No;
  55. const_cast<Layout::Label*>(label)->handle_mousemove_on_label({}, position, button);
  56. return DispatchEventOfSameName::Yes;
  57. }
  58. }