simple_form.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef _FY_SIMPLE_FORM_
  2. #define _FY_SIMPLE_FORM_
  3. #include <string>
  4. #include <vector>
  5. #include "control/ZKListView.h"
  6. namespace fy {
  7. class simple_form {
  8. public:
  9. typedef std::string (*string_getter)(int index, void* user_data);
  10. typedef void (*click_listener)(int index, void* user_data);
  11. struct form_item {
  12. std::string title;
  13. string_getter get_value;
  14. click_listener on_click;
  15. void* user_data;
  16. };
  17. void add_item(const std::string &title, string_getter get_value, click_listener on_click, void* user_data) {
  18. form_item item;
  19. item.title = title;
  20. item.get_value = get_value;
  21. item.on_click = on_click;
  22. item.user_data = user_data;
  23. items_.push_back(item);
  24. }
  25. int size() { return items_.size();}
  26. void clear() { items_.clear();}
  27. const form_item* find(int index) {
  28. if (index >= (int)items_.size()) {
  29. return NULL;
  30. }
  31. return (form_item*)&(*(items_.begin() + index));
  32. }
  33. void obtain(ZKListView::ZKListItem *pListItem, int index, int subitem_id) {
  34. const form_item* item = find(index);
  35. if (item == NULL) {
  36. return;
  37. }
  38. if (pListItem) {
  39. pListItem->setText(item->title);
  40. }
  41. ZKListView::ZKListSubItem* value = pListItem->findSubItemByID(subitem_id);
  42. if (value == NULL) {
  43. return;
  44. }
  45. if (item->get_value == NULL) {
  46. value->setText("");
  47. return;
  48. }
  49. value->setText(item->get_value(index, item->user_data));
  50. }
  51. void click(int index) {
  52. const form_item* item = find(index);
  53. if (item == NULL) {
  54. return;
  55. }
  56. if (item->on_click == NULL) {
  57. return;
  58. }
  59. item->on_click(index, item->user_data);
  60. }
  61. private:
  62. std::vector<simple_form::form_item> items_;
  63. };
  64. } /* namespace fy */
  65. #endif