pm_checkbox.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * File : pm_checkbox.h
  3. * COPYRIGHT (C) 2012-2017, Shanghai Real-Thread Technology Co., Ltd
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2017-11-05 realthread the first version
  8. */
  9. #pragma once
  10. #include <vector>
  11. #include <pm_container.h>
  12. #include <pm_image.h>
  13. #include "sigslot.h"
  14. namespace Persimmon
  15. {
  16. class Checkbox : public Widget
  17. {
  18. public:
  19. Checkbox(const Rect& rect);
  20. virtual ~Checkbox();
  21. bool isSelected(void) //whether or not selected
  22. {
  23. return selected;
  24. }
  25. void enableSelected(bool sel = true) //is selected
  26. {
  27. selected = sel;
  28. }
  29. void setSelectedImg(Image *image)
  30. {
  31. if (selectedImg)
  32. delete selectedImg;
  33. selectedImg = NULL;
  34. if (image)
  35. selectedImg = image;
  36. }
  37. Image *getSelectedImg(void)
  38. {
  39. return selectedImg;
  40. }
  41. void setNormalImg(Image *image)
  42. {
  43. if (normalImg)
  44. delete normalImg;
  45. normalImg = NULL;
  46. if (image)
  47. normalImg = image;
  48. }
  49. Image *getNormalImg(void)
  50. {
  51. return normalImg;
  52. }
  53. void setCheckboxNum(int num) //Set checkbox number
  54. {
  55. checkboxNum = num;
  56. }
  57. int getCheckboxNum(void) //Get checkbox number
  58. {
  59. return checkboxNum;
  60. }
  61. Signal<int, bool> clicked; //Event transfer
  62. virtual bool handleGestureEvent(struct rtgui_event_gesture *, const struct rtgui_gesture *); //Touch gesture event
  63. virtual void render(struct rtgui_dc* dc, const Point &dcPoint = Point(),
  64. const Rect &srcRect = Rect(),
  65. RenderFlag flags = DrawNormal);
  66. private:
  67. Image *selectedImg, *normalImg;
  68. bool selected; //State
  69. int checkboxNum; //Number
  70. };
  71. class GroupBox : public Container
  72. {
  73. public:
  74. GroupBox(const Rect& rect);
  75. virtual ~GroupBox();
  76. virtual void handleClicked(int num, bool sel)
  77. {
  78. if (sel == true)
  79. {
  80. if (selCheckboxNum != num)
  81. {
  82. this->checkbox[selCheckboxNum]->enableSelected(false);
  83. this->checkbox[selCheckboxNum]->refresh();
  84. selCheckboxNum = num;
  85. clicked(selCheckboxNum);
  86. }
  87. }
  88. else
  89. {
  90. this->checkbox[selCheckboxNum]->enableSelected(true);
  91. this->checkbox[selCheckboxNum]->refresh();
  92. }
  93. }
  94. void addCheckbox(Checkbox* widget)
  95. {
  96. if (widget != NULL)
  97. {
  98. this->checkbox.push_back(widget);
  99. widget->setCheckboxNum(this->checkbox.size() - 1);
  100. widget->clicked.connect(this, &GroupBox::handleClicked);
  101. this->addChild(widget);
  102. }
  103. }
  104. void selectCheckbox(int num = 0)
  105. {
  106. if (selCheckboxNum != num)
  107. this->checkbox[selCheckboxNum]->enableSelected(false);
  108. this->checkbox[selCheckboxNum]->refresh();
  109. selCheckboxNum = num;
  110. this->checkbox[selCheckboxNum]->enableSelected();
  111. this->checkbox[selCheckboxNum]->refresh();
  112. }
  113. int getselectNum(void)
  114. {
  115. return selCheckboxNum;
  116. }
  117. Signal<int> clicked;
  118. private:
  119. std::vector<Checkbox*> checkbox;
  120. int selCheckboxNum;
  121. };
  122. }