KeyButton.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  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 "KeyButton.h"
  27. #include <LibGUI/Button.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/Window.h>
  30. #include <LibGfx/Font.h>
  31. #include <LibGfx/Palette.h>
  32. KeyButton::~KeyButton()
  33. {
  34. }
  35. void KeyButton::paint_event(GUI::PaintEvent& event)
  36. {
  37. GUI::Painter painter(*this);
  38. painter.add_clip_rect(event.rect());
  39. auto cont_rect = rect();
  40. auto& font = this->font();
  41. Color color;
  42. if (m_pressed) {
  43. color = Color::Cyan;
  44. } else if (!is_enabled()) {
  45. color = Color::LightGray;
  46. } else {
  47. color = Color::White;
  48. }
  49. painter.fill_rect(cont_rect, Color::Black);
  50. painter.fill_rect({ cont_rect.x() + 1, cont_rect.y() + 1, cont_rect.width() - 2, cont_rect.height() - 2 }, Color::from_rgb(0x999999));
  51. painter.fill_rect({ cont_rect.x() + 6, cont_rect.y() + 3, cont_rect.width() - 12, cont_rect.height() - 12 }, Color::from_rgb(0x8C7272));
  52. painter.fill_rect({ cont_rect.x() + 7, cont_rect.y() + 4, cont_rect.width() - 14, cont_rect.height() - 14 }, color);
  53. if (!text().is_empty()) {
  54. Gfx::IntRect text_rect { 0, 0, font.width(text()), font.glyph_height() };
  55. text_rect.align_within({ cont_rect.x() + 7, cont_rect.y() + 4, cont_rect.width() - 14, cont_rect.height() - 14 }, Gfx::TextAlignment::Center);
  56. painter.draw_text(text_rect, text(), font, Gfx::TextAlignment::Center, palette().button_text(), Gfx::TextElision::Right);
  57. if (is_focused())
  58. painter.draw_rect(text_rect.inflated(6, 4), palette().focus_outline());
  59. }
  60. }
  61. void KeyButton::click(unsigned)
  62. {
  63. if (on_click)
  64. on_click();
  65. }
  66. void KeyButton::mousemove_event(GUI::MouseEvent& event)
  67. {
  68. if (!is_enabled())
  69. return;
  70. Gfx::IntRect c = { rect().x() + 7, rect().y() + 4, rect().width() - 14, rect().height() - 14 };
  71. if (c.contains(event.position())) {
  72. window()->set_override_cursor(GUI::StandardCursor::Hand);
  73. return;
  74. }
  75. window()->set_override_cursor(GUI::StandardCursor::Arrow);
  76. AbstractButton::mousemove_event(event);
  77. }
  78. void KeyButton::leave_event(Core::Event& event)
  79. {
  80. window()->set_override_cursor(GUI::StandardCursor::Arrow);
  81. AbstractButton::leave_event(event);
  82. }