ListBox.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /********************************************************************************
  2. * File Name: ListBox.c
  3. * Function Describe: ListBox 控件
  4. * Relate Module: GUI.c
  5. * Writer: Shliangwen
  6. * Date: 2016-1-8
  7. *******************************************************************************/
  8. #include "Includes.h"
  9. #define N 100
  10. void ListBoxShow(struct SUT_LIST_BOX *p);
  11. SUT_LIST_BOX ListBox;
  12. /*************************************************************************
  13. ListBoxInit
  14. ***************************************************************************/
  15. void ListBoxInit(struct SUT_LIST_BOX *p,char **itemlist,char unicode,const char **iconlist,char *features)
  16. {
  17. unsigned int kk;
  18. ///////////////////////
  19. unsigned char i,ch;
  20. //统计item
  21. p->item = itemlist;
  22. i = 0;
  23. while(i<LIST_ITEM_NUM_MAX)
  24. {
  25. ch=p->item[i][0];
  26. if(ch==0)break;
  27. i ++;
  28. }
  29. i=i-1;
  30. p->itemnum = i+1;
  31. p->pgaenum=i/LIST_ROW;
  32. if(p->pgaenum%LIST_ROW)p->pgaenum+=1;
  33. //统计icon
  34. i=0;
  35. p->icon=iconlist;
  36. while(i<LIST_ITEM_NUM_MAX)
  37. {
  38. if(iconlist==NULL)break;
  39. ch=p->icon[i][0];
  40. if(ch==0)break;
  41. i ++;
  42. }
  43. p->iconnum=i;
  44. //特性
  45. for(i=0;i<LIST_ITEM_NUM_MAX;i++){
  46. if(i<p->itemnum){
  47. p->features[i]=features[i];
  48. }else{
  49. p->features[i]=0;
  50. }
  51. }
  52. //unicode
  53. p->unicode=unicode;
  54. //默认项
  55. p->page = 0;
  56. p->handle = 0;
  57. //显示
  58. ListBoxShow(p);
  59. }
  60. /******************************************************************************
  61. 指定允许的长度截取字符串,如果源长度超过指定长度,则截取之并将最后字符改为".."或"..."
  62. 同时处理中文字符
  63. *******************************************************************************/
  64. void StrIntercept(char *des,char *src,u16 len)
  65. {
  66. int srclen;
  67. unsigned char d1,d2;
  68. unsigned char *p;
  69. int i;
  70. srclen=strlen(src);
  71. des[len-1]='\0';
  72. strncpy(des,src,len-1);
  73. //if(srclen=len-1 && len>5){
  74. if(srclen>=len-1 && len>5){//防止最后一条没显示..
  75. //将最后两个字符转为".."
  76. des[len-2]='.';
  77. des[len-3]='.';
  78. //判断倒数第3个字符是否为半个中文字符,如果是则将其转为‘.’,避免显示乱码
  79. p=(unsigned char *)des;
  80. i=0;
  81. while(i<(len-4)){
  82. if(*p>0x9f){
  83. p+=2;
  84. i+=2;
  85. }else{
  86. p+=1;
  87. i+=1;
  88. }
  89. }
  90. if(i==(len-4) && *p>0x9f){//半个汉字
  91. des[len-4]='.';
  92. }
  93. }
  94. }
  95. /********************************************************************************
  96. ListBoxShowItem
  97. *******************************************************************************/
  98. void ListBoxShowItem(struct SUT_LIST_BOX *p,unsigned short handle)
  99. {
  100. u16 len;
  101. u16 x,y,hand;
  102. unsigned char buf[LIST_ITEM_TEXT_LEN_MAX+3];
  103. char str[LIST_ITEM_TEXT_LEN_MAX+1];
  104. char f;
  105. hand=handle % LIST_ROW;
  106. x=LIST_TOPX;
  107. y=LIST_TOPY+hand*16;
  108. #if 1 //换页时清理干净
  109. GuiClearArea(x,y,LIST_BAR_LEN+16,16);
  110. #else
  111. GuiClearArea(x,y,LIST_BAR_LEN,16);
  112. #endif
  113. memset(buf,0,sizeof(buf));
  114. if(p->unicode){
  115. StrUnicodeToAnsi(buf,sizeof(buf),p->item[handle]);
  116. buf[LIST_ITEM_TEXT_LEN_MAX+2]=0;
  117. }else{
  118. strncpy((char *)buf,p->item[handle],sizeof(buf));
  119. buf[LIST_ITEM_TEXT_LEN_MAX+2]=0;
  120. }
  121. //截取并适当补..
  122. StrIntercept(str,(char *)buf,LIST_ITEM_TEXT_LEN_MAX);
  123. if(p->icon!=NULL){//有图标
  124. f= p->features[handle];
  125. GuiShowBmp(x,y,p->icon[f]);
  126. GuiShowStr(x+16,y,str,0x01);
  127. }else{//无图标
  128. GuiShowStr(x,y,str,0x01);
  129. }
  130. }
  131. /********************************************************************************
  132. * Function:ListBoxShowBar
  133. * display a bar
  134. *******************************************************************************/
  135. void ListBoxShowBar(struct SUT_LIST_BOX *p)
  136. {
  137. unsigned char x,y,h,handle;
  138. char *str;
  139. x=LIST_TOPX;
  140. if(p->icon!=NULL){//有图标
  141. x+=16;
  142. }
  143. handle=p->handle%LIST_ROW;
  144. y=LIST_TOPY+handle*16;
  145. if(sutUIstatus.Status==UIS_SET_QUICK_GROUP && handle==0)
  146. {
  147. x+=72;
  148. if(!QuickGroupSet)GuiReverseRect(x,y,64,16);
  149. else {
  150. GuiClearArea(x,y,64,16);
  151. if(sutProductPara.QuickGroup==0){
  152. GuiShowStr(x+16,y,"关闭",0x01);
  153. GuiReverseRect(x,y,64,16);
  154. }
  155. else {
  156. GuiShowStr(x+16,y,"开启",0x01);
  157. GuiReverseRect(x,y,64,16);
  158. }
  159. }
  160. }
  161. else if(sutUIstatus.Status==UIS_SET_QUICK_GROUP)
  162. {
  163. x+=32;
  164. GuiReverseRect(x,y,LIST_BAR_LEN,16);
  165. }
  166. else{
  167. GuiReverseRect(x,y,LIST_BAR_LEN,16);
  168. }
  169. }
  170. /********************************************************************************
  171. * Function:ListBoxShow
  172. * display ListBox
  173. *******************************************************************************/
  174. void ListBoxShow(struct SUT_LIST_BOX *p)
  175. {
  176. unsigned char i,j;
  177. u16 x,y;
  178. if(p->itemnum==0)return;
  179. j=p->page*LIST_ROW;
  180. for(i = j ; i <j+LIST_ROW ; i ++)
  181. {
  182. if(i >= p->itemnum){
  183. y=LIST_TOPY+(i % LIST_ROW)*16;
  184. GuiClearArea(LIST_TOPX,y,LIST_BAR_LEN+18,16);
  185. }else{
  186. ListBoxShowItem(p,i);
  187. }
  188. }
  189. ListBoxShowBar(p);
  190. if(sutUIstatus.Status!=UIS_SET_QUICK_GROUP){
  191. //显示箭头
  192. x=LCD_WIDTH-17;
  193. if(p->page>0){
  194. GuiShowArrow(x+8,LIST_TOPY,8,3);//向上箭头
  195. }else{
  196. GuiClearArea(x,LIST_TOPY,16,8);
  197. }
  198. y=LIST_TOPY+LIST_ROW*16;
  199. if(p->page<(p->pgaenum-1)){
  200. GuiShowArrow(x+8,y,8,4);//向下箭头
  201. }else{
  202. GuiClearArea(x,y-8,16,8);
  203. }
  204. }
  205. }
  206. /********************************************************************************
  207. ListBoxResponse
  208. ListBox控件的键盘响应处理 底层
  209. *******************************************************************************/
  210. unsigned long ListBoxResponse(struct SUT_LIST_BOX *p)
  211. {
  212. unsigned char temp;
  213. if(p->itemnum==0)return g_ulKeyValue;
  214. // SUT_PHONE_NUM PhoneNum[N];
  215. // int i = 0;
  216. switch(g_ulKeyValue)
  217. {
  218. case KEY_PANEL_UP:
  219. if(QuickGroupSet)sutProductPara.QuickGroup=!sutProductPara.QuickGroup;
  220. ListBoxShowBar(p);
  221. #if 0
  222. if(p->handle==0)
  223. {
  224. p->handle=(p->itemnum-1);
  225. //清预览位置
  226. GuiClearArea(LIST_TOPX,LIST_TOPY,LIST_BAR_LEN,16*LIST_ROW);
  227. }
  228. #else
  229. if(p->handle==0&&!QuickGroupSet)p->handle=(p->itemnum-1);
  230. #endif
  231. else if(!QuickGroupSet)p->handle --;
  232. temp=p->page;
  233. p->page=p->handle/LIST_ROW;
  234. if(temp!=p->page)ListBoxShow(p);
  235. else ListBoxShowBar(p);
  236. break;
  237. case KEY_PANEL_DOWN:
  238. if(QuickGroupSet)sutProductPara.QuickGroup=!sutProductPara.QuickGroup;
  239. ListBoxShowBar(p);
  240. if(!QuickGroupSet)p->handle ++;
  241. #if 0
  242. if(p->handle>=p-> itemnum)
  243. {
  244. p->handle=0;
  245. //清预览位置
  246. GuiClearArea(LIST_TOPX,LIST_TOPY,LIST_BAR_LEN+16,16*LIST_ROW);
  247. }
  248. #else
  249. if(p->handle>=p->itemnum&&!QuickGroupSet)p->handle=0;
  250. #endif
  251. temp = p->page;
  252. p->page=p->handle/LIST_ROW;
  253. if(temp!=p->page)ListBoxShow(p);
  254. else ListBoxShowBar(p);
  255. break;
  256. default:
  257. return g_ulKeyValue;
  258. break;
  259. }
  260. return 0;
  261. }
  262. /********************************************************************************
  263. ListBoxGetHandle
  264. 获取ListBox当前手柄
  265. *******************************************************************************/
  266. unsigned short ListBoxGetHandle(struct SUT_LIST_BOX *p)
  267. {
  268. return p->handle;
  269. }