KeyButton.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 face_color;
  21. if (m_pressed) {
  22. face_color = Color::Cyan;
  23. } else if (!is_enabled()) {
  24. face_color = Color::LightGray;
  25. } else {
  26. face_color = Color::White;
  27. }
  28. Gfx::IntRect key_cap_side_rect = { cont_rect.x() + 1, cont_rect.y() + 1, cont_rect.width() - 2, cont_rect.height() - 2 };
  29. Gfx::IntRect key_cap_face_border_rect = { cont_rect.x() + 6, cont_rect.y() + 3, cont_rect.width() - 12, cont_rect.height() - 12 };
  30. Gfx::IntRect key_cap_face_rect = { cont_rect.x() + 7, cont_rect.y() + 4, cont_rect.width() - 14, cont_rect.height() - 14 };
  31. painter.draw_rect(cont_rect, Color::Black); // Key cap border
  32. painter.fill_rect(key_cap_side_rect, Color::from_rgb(0x999999));
  33. painter.draw_rect(key_cap_face_border_rect, Color::from_rgb(0x8C7272), false);
  34. painter.fill_rect(key_cap_face_rect, face_color);
  35. if (!text().is_empty()) {
  36. Gfx::IntRect text_rect { 0, 0, font.width(text()), font.glyph_height() };
  37. text_rect.align_within(key_cap_face_rect, Gfx::TextAlignment::Center);
  38. painter.draw_text(text_rect, text(), font, Gfx::TextAlignment::Center, Color::Black, Gfx::TextElision::Right);
  39. if (is_focused())
  40. painter.draw_rect(text_rect.inflated(6, 4), palette().focus_outline());
  41. }
  42. }
  43. void KeyButton::click(unsigned)
  44. {
  45. if (on_click)
  46. on_click();
  47. }
  48. void KeyButton::mousemove_event(GUI::MouseEvent& event)
  49. {
  50. if (!is_enabled())
  51. return;
  52. Gfx::IntRect key_cap_face_rect = { rect().x() + 7, rect().y() + 4, rect().width() - 14, rect().height() - 14 };
  53. if (key_cap_face_rect.contains(event.position())) {
  54. window()->set_cursor(Gfx::StandardCursor::Hand);
  55. return;
  56. }
  57. window()->set_cursor(Gfx::StandardCursor::Arrow);
  58. AbstractButton::mousemove_event(event);
  59. }
  60. void KeyButton::leave_event(Core::Event& event)
  61. {
  62. window()->set_cursor(Gfx::StandardCursor::Arrow);
  63. AbstractButton::leave_event(event);
  64. }