KeyButton.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "KeyButton.h"
  7. #include <LibGUI/Painter.h>
  8. #include <LibGUI/Window.h>
  9. #include <LibGfx/Font.h>
  10. #include <LibGfx/Palette.h>
  11. KeyButton::~KeyButton()
  12. {
  13. }
  14. void KeyButton::paint_event(GUI::PaintEvent& event)
  15. {
  16. GUI::Painter painter(*this);
  17. painter.add_clip_rect(event.rect());
  18. auto cont_rect = rect();
  19. auto& font = this->font();
  20. Color color;
  21. if (m_pressed) {
  22. color = Color::Cyan;
  23. } else if (!is_enabled()) {
  24. color = Color::LightGray;
  25. } else {
  26. color = Color::White;
  27. }
  28. painter.fill_rect(cont_rect, Color::Black);
  29. painter.fill_rect({ cont_rect.x() + 1, cont_rect.y() + 1, cont_rect.width() - 2, cont_rect.height() - 2 }, Color::from_rgb(0x999999));
  30. painter.fill_rect({ cont_rect.x() + 6, cont_rect.y() + 3, cont_rect.width() - 12, cont_rect.height() - 12 }, Color::from_rgb(0x8C7272));
  31. painter.fill_rect({ cont_rect.x() + 7, cont_rect.y() + 4, cont_rect.width() - 14, cont_rect.height() - 14 }, color);
  32. if (!text().is_empty()) {
  33. Gfx::IntRect text_rect { 0, 0, font.width(text()), font.glyph_height() };
  34. text_rect.align_within({ cont_rect.x() + 7, cont_rect.y() + 4, cont_rect.width() - 14, cont_rect.height() - 14 }, Gfx::TextAlignment::Center);
  35. painter.draw_text(text_rect, text(), font, Gfx::TextAlignment::Center, Color::Black, Gfx::TextElision::Right);
  36. if (is_focused())
  37. painter.draw_rect(text_rect.inflated(6, 4), palette().focus_outline());
  38. }
  39. }
  40. void KeyButton::click(unsigned)
  41. {
  42. if (on_click)
  43. on_click();
  44. }
  45. void KeyButton::mousemove_event(GUI::MouseEvent& event)
  46. {
  47. if (!is_enabled())
  48. return;
  49. Gfx::IntRect c = { rect().x() + 7, rect().y() + 4, rect().width() - 14, rect().height() - 14 };
  50. if (c.contains(event.position())) {
  51. window()->set_cursor(Gfx::StandardCursor::Hand);
  52. return;
  53. }
  54. window()->set_cursor(Gfx::StandardCursor::Arrow);
  55. AbstractButton::mousemove_event(event);
  56. }
  57. void KeyButton::leave_event(Core::Event& event)
  58. {
  59. window()->set_cursor(Gfx::StandardCursor::Arrow);
  60. AbstractButton::leave_event(event);
  61. }