ButtonBox.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Event.h>
  27. #include <LibGfx/Font.h>
  28. #include <LibGfx/Painter.h>
  29. #include <LibGfx/StylePainter.h>
  30. #include <LibWeb/DOM/Document.h>
  31. #include <LibWeb/Layout/ButtonBox.h>
  32. #include <LibWeb/Layout/Label.h>
  33. #include <LibWeb/Page/Frame.h>
  34. namespace Web::Layout {
  35. ButtonBox::ButtonBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> style)
  36. : LabelableNode(document, element, move(style))
  37. {
  38. }
  39. ButtonBox::~ButtonBox()
  40. {
  41. }
  42. void ButtonBox::prepare_for_replaced_layout()
  43. {
  44. set_intrinsic_width(font().width(dom_node().value()) + 20);
  45. set_has_intrinsic_width(true);
  46. set_intrinsic_height(20);
  47. set_has_intrinsic_height(true);
  48. }
  49. void ButtonBox::paint(PaintContext& context, PaintPhase phase)
  50. {
  51. if (!is_visible())
  52. return;
  53. LabelableNode::paint(context, phase);
  54. if (phase == PaintPhase::Foreground) {
  55. bool hovered = document().hovered_node() == &dom_node();
  56. if (!hovered)
  57. hovered = Label::is_associated_label_hovered(*this);
  58. Gfx::StylePainter::paint_button(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::ButtonStyle::Normal, m_being_pressed, hovered, dom_node().checked(), dom_node().enabled());
  59. auto text_rect = enclosing_int_rect(absolute_rect());
  60. if (m_being_pressed)
  61. text_rect.move_by(1, 1);
  62. context.painter().draw_text(text_rect, dom_node().value(), font(), Gfx::TextAlignment::Center, context.palette().button_text());
  63. }
  64. }
  65. void ButtonBox::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned)
  66. {
  67. if (button != GUI::MouseButton::Left || !dom_node().enabled())
  68. return;
  69. m_being_pressed = true;
  70. set_needs_display();
  71. m_tracking_mouse = true;
  72. frame().event_handler().set_mouse_event_tracking_layout_node(this);
  73. }
  74. void ButtonBox::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  75. {
  76. if (!m_tracking_mouse || button != GUI::MouseButton::Left || !dom_node().enabled())
  77. return;
  78. // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
  79. NonnullRefPtr protected_this = *this;
  80. NonnullRefPtr protected_frame = frame();
  81. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  82. if (!is_inside_node_or_label)
  83. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  84. if (is_inside_node_or_label)
  85. dom_node().did_click_button({});
  86. m_being_pressed = false;
  87. m_tracking_mouse = false;
  88. protected_frame->event_handler().set_mouse_event_tracking_layout_node(nullptr);
  89. }
  90. void ButtonBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  91. {
  92. if (!m_tracking_mouse || !dom_node().enabled())
  93. return;
  94. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  95. if (!is_inside_node_or_label)
  96. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  97. if (m_being_pressed == is_inside_node_or_label)
  98. return;
  99. m_being_pressed = is_inside_node_or_label;
  100. set_needs_display();
  101. }
  102. void ButtonBox::handle_associated_label_mousedown(Badge<Label>)
  103. {
  104. m_being_pressed = true;
  105. set_needs_display();
  106. }
  107. void ButtonBox::handle_associated_label_mouseup(Badge<Label>)
  108. {
  109. // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
  110. NonnullRefPtr protected_this = *this;
  111. NonnullRefPtr protected_frame = frame();
  112. dom_node().did_click_button({});
  113. m_being_pressed = false;
  114. }
  115. void ButtonBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label)
  116. {
  117. if (m_being_pressed == is_inside_node_or_label)
  118. return;
  119. m_being_pressed = is_inside_node_or_label;
  120. set_needs_display();
  121. }
  122. }