field.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #pragma once
  2. #include <map>
  3. #if 1
  4. #include <functional>
  5. #else
  6. #include <boost/function.hpp>
  7. #endif
  8. #include "control/ZKListView.h"
  9. #include "entry/EasyUIContext.h"
  10. #include "utils/Log.h"
  11. struct TextExtent {
  12. int w;
  13. int h;
  14. };
  15. TextExtent GetTextExtent(ZKTextView* view, const std::string& str);
  16. std::string EllipsisText(ZKTextView* view, const std::string& str);
  17. class Field : protected IMEContext::IIMETextUpdateListener{
  18. public:
  19. typedef std::map<int, ZKListView::ZKListSubItem*> ItemMap;
  20. enum Names {
  21. ICON = 100,
  22. ONLY_TITLE,
  23. TITLE,
  24. VALUE,
  25. ARRAW,
  26. SWITCH,
  27. DIVIDER,
  28. PRIMARY_BUTTON,
  29. };
  30. typedef std::function<std::string (Field* f)> TextGetter;
  31. typedef std::function<bool (Field* f)> SwitchGetter;
  32. friend class Form;
  33. Field() {
  34. arraw_ = true;
  35. switch_ = false;
  36. divider_ = true;
  37. }
  38. virtual ~Field() {}
  39. Field& Title(const char* title) {
  40. title_ = title;
  41. return self();
  42. }
  43. Field& Title(const TextGetter& title_get) {
  44. title_get_ = title_get;
  45. return self();
  46. }
  47. Field& Icon(const std::string& icon) {
  48. icon_ = icon;
  49. return self();
  50. }
  51. Field& Arraw(bool arraw) {
  52. arraw_ = arraw;
  53. if (arraw) {
  54. switch_ = false;
  55. }
  56. return self();
  57. }
  58. Field& Divider(bool divider) {
  59. divider_ = divider;
  60. return self();
  61. }
  62. Field& Switch(const SwitchGetter& get) {
  63. switch_ = true;
  64. arraw_ = false;
  65. switch_get_ = get;
  66. return self();
  67. }
  68. Field& Text(const TextGetter& get) {
  69. get_ = get;
  70. return self();
  71. }
  72. std::string Text() {
  73. if (get_ == NULL) {
  74. return "";
  75. }
  76. return get_(this);
  77. }
  78. typedef std::function<void (Field* f)> ClickCallback;
  79. Field& Click(const ClickCallback& callback) {
  80. click_callback_ = callback;
  81. return self();
  82. }
  83. typedef std::function<void (Field* f, const std::string& str)> EditCallback;
  84. Field& Edit(const EditCallback& callback) {
  85. edit_callback_ = callback;
  86. return self();
  87. }
  88. protected:
  89. Field& self() {
  90. return *dynamic_cast<Field*>(this);
  91. }
  92. void Click() {
  93. if (click_callback_ != NULL) {
  94. click_callback_(&self());
  95. return;
  96. }
  97. if (edit_callback_ != NULL) {
  98. #if 1
  99. ime_info.text = get_ == NULL ? "" : get_(this);
  100. #else
  101. ime_info.text = "";
  102. #endif
  103. ime_info.isPassword = false;
  104. ime_info.imeTextType = IMEContext::E_IME_TEXT_TYPE_ALL;
  105. EASYUICONTEXT->showIME(&ime_info, this);
  106. }
  107. }
  108. virtual void onIMETextUpdate(const std::string &text) {
  109. if (edit_callback_ == NULL) {
  110. return;
  111. }
  112. edit_callback_(this, text);
  113. }
  114. virtual void Paint(ZKListView* list_view, ZKListView::ZKListItem *item,
  115. int index, ItemMap& item_map) {
  116. if (item_map[ICON] == nullptr) {
  117. LOGE("ICON item must not be null");
  118. return;
  119. }
  120. if (item_map[TITLE] == nullptr) {
  121. LOGE("TITLE item must not be null");
  122. return;
  123. }
  124. if (item_map[ONLY_TITLE] == nullptr) {
  125. LOGE("ONLY_TITLE item must not be null");
  126. return;
  127. }
  128. if (item_map[VALUE] == nullptr) {
  129. LOGE("VALUE item must not be null");
  130. return;
  131. }
  132. if (item_map[ARRAW] == nullptr) {
  133. LOGE("ARRAW item must not be null");
  134. return;
  135. }
  136. if (item_map[SWITCH] == nullptr) {
  137. LOGE("SWITCH item must not be null");
  138. return;
  139. }
  140. item_map[ICON]->setBackgroundPic(icon_.c_str());
  141. std::string title_str = title_;
  142. if (title_get_ != NULL) {
  143. title_str = title_get_(this);
  144. }
  145. item_map[ONLY_TITLE]->setText(icon_.empty() ? title_str : "");
  146. item_map[TITLE]->setText(
  147. EllipsisText(item_map[TITLE], icon_.empty() ? "" : title_str));
  148. std::string str = get_ == NULL ? "" : get_(this);
  149. if (arraw_ & !str.empty()) {
  150. str += " ";//箭头和文字同时存在,需要为箭头空出距离
  151. }
  152. item_map[VALUE]->setText(EllipsisText(item_map[VALUE], str));
  153. item_map[ARRAW]->setBackgroundPic(arraw_ ? "ic_arraw_right.png" : "");
  154. bool is_on = switch_get_ == NULL ? false : switch_get_(this);
  155. item_map[SWITCH]->setBackgroundPic(switch_ ?
  156. (is_on ? "settings/on_2.png" : "settings/off_2.png") : "");
  157. if (item_map[DIVIDER] != nullptr) {
  158. item_map[DIVIDER]->setAlpha(
  159. (divider_ && (index != list_view->getListItemCount() -1))
  160. ? 0 : 255);
  161. // item_map[DIVIDER]->setBackgroundColor(
  162. // (divider_ && (index != list_view->getListItemCount() -1))
  163. // ? 0xff2E3035 : 0);
  164. }
  165. }
  166. protected:
  167. std::string icon_;
  168. std::string title_;
  169. bool arraw_;
  170. bool divider_;
  171. bool switch_;
  172. ClickCallback click_callback_;
  173. EditCallback edit_callback_;
  174. IMEContext::SIMETextInfo ime_info;
  175. TextGetter get_;
  176. TextGetter title_get_;
  177. SwitchGetter switch_get_;
  178. friend class UIForm;
  179. };
  180. class FieldFactory {
  181. public:
  182. static Field& Text() {
  183. Field* field = new Field();
  184. return *field;
  185. }
  186. static Field& Placeholder() {
  187. Field* field = new Field();
  188. field->Arraw(false);
  189. field->Divider(false);
  190. return *field;
  191. }
  192. static Field& Text(const std::string& title) {
  193. Field* field = new Field();
  194. field->Title(title.c_str());
  195. return *field;
  196. }
  197. };