TextPaintable.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 const* label = layout_node().first_ancestor_of_type<Layout::Label>())
  28. return label->dom_node().control().ptr();
  29. return nullptr;
  30. }
  31. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mousedown(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  32. {
  33. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  34. if (!label)
  35. return DispatchEventOfSameName::No;
  36. const_cast<Layout::Label*>(label)->handle_mousedown_on_label({}, position, button);
  37. const_cast<HTML::BrowsingContext&>(browsing_context()).event_handler().set_mouse_event_tracking_paintable(this);
  38. return DispatchEventOfSameName::Yes;
  39. }
  40. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mouseup(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  41. {
  42. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  43. if (!label)
  44. return DispatchEventOfSameName::No;
  45. const_cast<Layout::Label*>(label)->handle_mouseup_on_label({}, position, button);
  46. const_cast<HTML::BrowsingContext&>(browsing_context()).event_handler().set_mouse_event_tracking_paintable(nullptr);
  47. return DispatchEventOfSameName::Yes;
  48. }
  49. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mousemove(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  50. {
  51. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  52. if (!label)
  53. return DispatchEventOfSameName::No;
  54. const_cast<Layout::Label*>(label)->handle_mousemove_on_label({}, position, button);
  55. return DispatchEventOfSameName::Yes;
  56. }
  57. }