CheckBox.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "CheckBox.h"
  2. #include "Painter.h"
  3. #include "CBitmap.h"
  4. #include <cstdio>
  5. CheckBox::CheckBox(Widget* parent)
  6. : Widget(parent)
  7. {
  8. }
  9. CheckBox::~CheckBox()
  10. {
  11. }
  12. void CheckBox::setCaption(String&& caption)
  13. {
  14. if (caption == m_caption)
  15. return;
  16. m_caption = std::move(caption);
  17. update();
  18. }
  19. void CheckBox::setIsChecked(bool b)
  20. {
  21. if (m_isChecked == b)
  22. return;
  23. m_isChecked = b;
  24. update();
  25. }
  26. static const char* uncheckedBitmap = {
  27. "###########"
  28. "# #"
  29. "# #"
  30. "# #"
  31. "# #"
  32. "# #"
  33. "# #"
  34. "# #"
  35. "# #"
  36. "# #"
  37. "###########"
  38. };
  39. #if 0
  40. static const char* checkedBitmap = {
  41. "############"
  42. "# #"
  43. "# ## #"
  44. "# ## #"
  45. "# ## #"
  46. "# ## #"
  47. "# ## #"
  48. "# ## ## #"
  49. "# ## ## #"
  50. "# ### #"
  51. "# #"
  52. "############"
  53. };
  54. #endif
  55. static const char* checkedBitmap = {
  56. "###########"
  57. "## ##"
  58. "# # # #"
  59. "# # # #"
  60. "# # # #"
  61. "# # #"
  62. "# # # #"
  63. "# # # #"
  64. "# # # #"
  65. "## ##"
  66. "###########"
  67. };
  68. void CheckBox::paintEvent(PaintEvent&)
  69. {
  70. Painter painter(*this);
  71. auto bitmap = CBitmap::createFromASCII(isChecked() ? checkedBitmap : uncheckedBitmap, 11, 11);
  72. auto textRect = rect();
  73. textRect.setLeft(bitmap->width() + 4);
  74. textRect.setTop(height() / 2 - font().glyphHeight() / 2);
  75. Point bitmapPosition;
  76. bitmapPosition.setX(2);
  77. bitmapPosition.setY(height() / 2 - bitmap->height() / 2 - 1);
  78. painter.fillRect(rect(), backgroundColor());
  79. painter.drawBitmap(bitmapPosition, *bitmap, foregroundColor());
  80. if (!caption().is_empty()) {
  81. painter.drawText(textRect, caption(), Painter::TextAlignment::TopLeft, foregroundColor());
  82. }
  83. }
  84. void CheckBox::mouseDownEvent(MouseEvent& event)
  85. {
  86. printf("CheckBox::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  87. setIsChecked(!isChecked());
  88. }