LinkLabel.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2020, Alex McGrath <amk@amk.ie>
  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 <LibGUI/LinkLabel.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/Window.h>
  30. #include <LibGfx/Font.h>
  31. #include <LibGfx/Palette.h>
  32. REGISTER_WIDGET(GUI, LinkLabel)
  33. namespace GUI {
  34. LinkLabel::LinkLabel(String text)
  35. : Label(move(text))
  36. {
  37. set_override_cursor(Gfx::StandardCursor::Hand);
  38. set_foreground_role(Gfx::ColorRole::Link);
  39. set_focus_policy(FocusPolicy::TabFocus);
  40. }
  41. void LinkLabel::mousedown_event(MouseEvent& event)
  42. {
  43. Label::mousedown_event(event);
  44. if (on_click) {
  45. on_click();
  46. }
  47. }
  48. void LinkLabel::keydown_event(KeyEvent& event)
  49. {
  50. Label::keydown_event(event);
  51. if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
  52. if (on_click)
  53. on_click();
  54. }
  55. }
  56. void LinkLabel::paint_event(PaintEvent& event)
  57. {
  58. Label::paint_event(event);
  59. GUI::Painter painter(*this);
  60. if (m_hovered)
  61. painter.draw_line({ 0, rect().bottom() }, { font().width(text()), rect().bottom() }, palette().link());
  62. if (is_focused())
  63. painter.draw_focus_rect(text_rect(), palette().focus_outline());
  64. }
  65. void LinkLabel::enter_event(Core::Event& event)
  66. {
  67. Label::enter_event(event);
  68. m_hovered = true;
  69. update();
  70. }
  71. void LinkLabel::leave_event(Core::Event& event)
  72. {
  73. Label::leave_event(event);
  74. m_hovered = false;
  75. update();
  76. }
  77. void LinkLabel::did_change_text()
  78. {
  79. Label::did_change_text();
  80. update_tooltip_if_needed();
  81. }
  82. void LinkLabel::update_tooltip_if_needed()
  83. {
  84. if (width() < font().width(text())) {
  85. set_tooltip(text());
  86. } else {
  87. set_tooltip({});
  88. }
  89. }
  90. void LinkLabel::resize_event(ResizeEvent& event)
  91. {
  92. Label::resize_event(event);
  93. update_tooltip_if_needed();
  94. }
  95. }