LinkLabel.cpp 3.0 KB

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