GButton.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2018-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 <AK/StringBuilder.h>
  27. #include <Kernel/KeyCode.h>
  28. #include <LibDraw/StylePainter.h>
  29. #include <LibGUI/GAction.h>
  30. #include <LibGUI/GActionGroup.h>
  31. #include <LibGUI/GButton.h>
  32. #include <LibGUI/GPainter.h>
  33. GButton::GButton(GWidget* parent)
  34. : GAbstractButton(parent)
  35. {
  36. }
  37. GButton::GButton(const StringView& text, GWidget* parent)
  38. : GAbstractButton(text, parent)
  39. {
  40. }
  41. GButton::~GButton()
  42. {
  43. if (m_action)
  44. m_action->unregister_button({}, *this);
  45. }
  46. void GButton::paint_event(GPaintEvent& event)
  47. {
  48. GPainter painter(*this);
  49. painter.add_clip_rect(event.rect());
  50. StylePainter::paint_button(painter, rect(), palette(), m_button_style, is_being_pressed(), is_hovered(), is_checked(), is_enabled());
  51. if (text().is_empty() && !m_icon)
  52. return;
  53. auto content_rect = rect().shrunken(8, 2);
  54. auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Point();
  55. if (m_icon && !text().is_empty())
  56. icon_location.set_x(content_rect.x());
  57. if (is_being_pressed() || is_checked())
  58. painter.translate(1, 1);
  59. if (m_icon) {
  60. if (is_enabled())
  61. painter.blit(icon_location, *m_icon, m_icon->rect());
  62. else
  63. painter.blit_dimmed(icon_location, *m_icon, m_icon->rect());
  64. }
  65. auto& font = is_checked() ? Font::default_bold_font() : this->font();
  66. if (m_icon && !text().is_empty()) {
  67. content_rect.move_by(m_icon->width() + 4, 0);
  68. content_rect.set_width(content_rect.width() - m_icon->width() - 4);
  69. }
  70. Rect text_rect { 0, 0, font.width(text()), font.glyph_height() };
  71. if (text_rect.width() > content_rect.width())
  72. text_rect.set_width(content_rect.width());
  73. text_rect.align_within(content_rect, text_alignment());
  74. paint_text(painter, text_rect, font, text_alignment());
  75. }
  76. void GButton::click()
  77. {
  78. if (!is_enabled())
  79. return;
  80. if (is_checkable()) {
  81. if (is_checked() && !is_uncheckable())
  82. return;
  83. set_checked(!is_checked());
  84. }
  85. if (on_click)
  86. on_click(*this);
  87. if (m_action)
  88. m_action->activate(this);
  89. }
  90. void GButton::set_action(GAction& action)
  91. {
  92. m_action = action.make_weak_ptr();
  93. action.register_button({}, *this);
  94. set_enabled(action.is_enabled());
  95. set_checkable(action.is_checkable());
  96. if (action.is_checkable())
  97. set_checked(action.is_checked());
  98. }
  99. void GButton::set_icon(RefPtr<GraphicsBitmap>&& icon)
  100. {
  101. if (m_icon == icon)
  102. return;
  103. m_icon = move(icon);
  104. update();
  105. }
  106. bool GButton::is_uncheckable() const
  107. {
  108. if (!m_action)
  109. return true;
  110. if (!m_action->group())
  111. return true;
  112. return m_action->group()->is_unchecking_allowed();
  113. }