GCheckBox.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "GCheckBox.h"
  2. #include <SharedGraphics/Painter.h>
  3. #include <SharedGraphics/CharacterBitmap.h>
  4. GCheckBox::GCheckBox(GWidget* parent)
  5. : GWidget(parent)
  6. {
  7. }
  8. GCheckBox::~GCheckBox()
  9. {
  10. }
  11. void GCheckBox::set_caption(String&& caption)
  12. {
  13. if (caption == m_caption)
  14. return;
  15. m_caption = move(caption);
  16. update();
  17. }
  18. void GCheckBox::set_checked(bool b)
  19. {
  20. if (m_checked == b)
  21. return;
  22. m_checked = b;
  23. update();
  24. }
  25. static const char* uncheckedBitmap = {
  26. "###########"
  27. "# #"
  28. "# #"
  29. "# #"
  30. "# #"
  31. "# #"
  32. "# #"
  33. "# #"
  34. "# #"
  35. "# #"
  36. "###########"
  37. };
  38. #if 0
  39. static const char* checkedBitmap = {
  40. "############"
  41. "# #"
  42. "# ## #"
  43. "# ## #"
  44. "# ## #"
  45. "# ## #"
  46. "# ## #"
  47. "# ## ## #"
  48. "# ## ## #"
  49. "# ### #"
  50. "# #"
  51. "############"
  52. };
  53. #endif
  54. static const char* checkedBitmap = {
  55. "###########"
  56. "## ##"
  57. "# # # #"
  58. "# # # #"
  59. "# # # #"
  60. "# # #"
  61. "# # # #"
  62. "# # # #"
  63. "# # # #"
  64. "## ##"
  65. "###########"
  66. };
  67. void GCheckBox::paint_event(GPaintEvent&)
  68. {
  69. Painter painter(*this);
  70. auto bitmap = CharacterBitmap::create_from_ascii(is_checked() ? checkedBitmap : uncheckedBitmap, 11, 11);
  71. auto textRect = rect();
  72. textRect.set_left(bitmap->width() + 4);
  73. textRect.set_top(height() / 2 - font().glyph_height() / 2);
  74. Point bitmapPosition;
  75. bitmapPosition.set_x(2);
  76. bitmapPosition.set_y(height() / 2 - bitmap->height() / 2 - 1);
  77. painter.fill_rect(rect(), background_color());
  78. painter.draw_bitmap(bitmapPosition, *bitmap, foreground_color());
  79. if (!caption().is_empty()) {
  80. painter.draw_text(textRect, caption(), Painter::TextAlignment::TopLeft, foreground_color());
  81. }
  82. }
  83. void GCheckBox::mousedown_event(GMouseEvent& event)
  84. {
  85. dbgprintf("GCheckBox::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  86. set_checked(!is_checked());
  87. }