ListBox.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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) //预期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. buf[LIST_ITEM_TEXT_LEN_MAX+2]=0;
  116. }else{
  117. strncpy((char *)buf,p->item[handle],sizeof(buf));
  118. buf[LIST_ITEM_TEXT_LEN_MAX+2]=0;
  119. }
  120. //截取并适当补..
  121. StrIntercept(str,(char *)buf,LIST_ITEM_TEXT_LEN_MAX);
  122. if(p->icon!=NULL){//有图标
  123. f= p->features[handle];
  124. GuiShowBmp(x,y,p->icon[f]);
  125. GuiShowStr(x+16,y,str,0x01,0);
  126. }else{//无图标
  127. GuiShowStr(x,y,str,0x01,0);
  128. }
  129. }
  130. /********************************************************************************
  131. * Function:ListBoxShowBar
  132. * display a bar
  133. *******************************************************************************/
  134. void ListBoxShowBar(struct SUT_LIST_BOX *p,char rev)
  135. {
  136. unsigned char x,y,h,handle;
  137. char str[LIST_ITEM_TEXT_LEN_MAX+1];
  138. x=LIST_TOPX;
  139. if(p->icon!=NULL){//有图标
  140. x+=16;
  141. }
  142. handle=p->handle%LIST_ROW;
  143. y=LIST_TOPY+handle*16;
  144. //截取并适当补..
  145. StrIntercept(str,p->item[p->handle],LIST_ITEM_TEXT_LEN_MAX);
  146. if(rev)GuiFillRect(x,y,140,y+14,COLOR_SELECT_BLUE);
  147. else GuiFillRect(x,y,140,y+14,COLOR_WHITE_BACK);
  148. //GuiReverseRect(x,y,LIST_BAR_LEN,16);
  149. GuiShowStr(x, y,str , 1, rev);
  150. }
  151. /********************************************************************************
  152. * Function:ListBoxShow
  153. * display ListBox
  154. *******************************************************************************/
  155. void ListBoxShow(struct SUT_LIST_BOX *p)
  156. {
  157. unsigned char i,j;
  158. u16 x,y;
  159. if(p->itemnum==0)return;
  160. j=p->page*LIST_ROW;
  161. for(i = j ; i <j+LIST_ROW ; i ++)
  162. {
  163. if(i >= p->itemnum){
  164. //y=MENU_TOPY+(i % MENU_ROW)*(12+MENU_SPACE);//16
  165. y=LIST_TOPY+(i % LIST_ROW)*16;
  166. GuiClearArea(LIST_TOPX,y,LIST_BAR_LEN+18,16);
  167. }else{
  168. ListBoxShowItem(p,i);
  169. }
  170. }
  171. ListBoxShowBar(p,1);
  172. // //显示箭头
  173. // x=LCD_WIDTH-17;
  174. // if(p->page>0){
  175. // GuiShowArrow(x+8,LIST_TOPY,8,3);//向上箭头
  176. // }else{
  177. // GuiClearArea(x,LIST_TOPY,16,8);
  178. // }
  179. // y=LIST_TOPY+LIST_ROW*16;
  180. // if(p->page<(p->pgaenum-1)){
  181. // GuiShowArrow(x+8,y,8,4);//向下箭头
  182. // }else{
  183. // GuiClearArea(x,y-8,16,8);
  184. // }
  185. }
  186. /********************************************************************************
  187. ListBoxResponse
  188. ListBox控件的键盘响应处理 底层
  189. *******************************************************************************/
  190. unsigned long ListBoxResponse(struct SUT_LIST_BOX *p)
  191. {
  192. unsigned char temp;
  193. unsigned char buf[10];
  194. if(p->itemnum==0)return g_ulKeyValue;
  195. switch(g_ulKeyValue)
  196. {
  197. case KEY_PANEL_UP:
  198. ListBoxShowBar(p,0);
  199. #if 0
  200. if(p->handle==0)
  201. {
  202. p->handle=(p->itemnum-1);
  203. //清预览位置
  204. GuiClearArea(LIST_TOPX,LIST_TOPY,LIST_BAR_LEN,16*LIST_ROW);
  205. }
  206. #else
  207. if(p->handle==0)p->handle=(p->itemnum-1);
  208. #endif
  209. else p->handle --;
  210. temp=p->page;
  211. p->page=p->handle/LIST_ROW;
  212. if(temp!=p->page)ListBoxShow(p);
  213. else ListBoxShowBar(p,1);
  214. if(sutUIstatus.Status==UIS_MENU_GROUP_SEL||sutUIstatus.Status==UIS_MENU_USER_SEL){
  215. snprintf(buf,sizeof(buf),"%03d/%03d",sutPocStatus.ListCurOnlineNum,(sutPocStatus.handleIndex+1)*5-p->handle);
  216. GuiShowStr(68,STATUS_BAR_HEIGH,buf,0x01,0);
  217. }
  218. break;
  219. case KEY_PANEL_DOWN:
  220. ListBoxShowBar(p,0);
  221. p->handle ++;
  222. #if 0
  223. if(p->handle>=p-> itemnum)
  224. {
  225. p->handle=0;
  226. //清预览位置
  227. GuiClearArea(LIST_TOPX,LIST_TOPY,LIST_BAR_LEN+16,16*LIST_ROW);
  228. }
  229. #else
  230. if(p->handle>=p-> itemnum)p->handle=0;
  231. #endif
  232. temp = p->page;
  233. p->page=p->handle/LIST_ROW;
  234. if(temp!=p->page)ListBoxShow(p);
  235. else ListBoxShowBar(p,1);
  236. if(sutUIstatus.Status==UIS_MENU_GROUP_SEL||sutUIstatus.Status==UIS_MENU_USER_SEL){
  237. snprintf(buf,sizeof(buf),"%03d/%03d",sutPocStatus.ListCurOnlineNum,sutPocStatus.handleIndex*5+p->handle+1);
  238. printf("handleIndex==%d,p->handle===%d\r\n",sutPocStatus.handleIndex,p->handle);
  239. GuiShowStr(68,STATUS_BAR_HEIGH,buf,0x01,0);
  240. }
  241. break;
  242. default:
  243. return g_ulKeyValue;
  244. break;
  245. }
  246. return 0;
  247. }
  248. /********************************************************************************
  249. ListBoxGetHandle
  250. 获取ListBox当前手柄
  251. *******************************************************************************/
  252. unsigned short ListBoxGetHandle(struct SUT_LIST_BOX *p)
  253. {
  254. return p->handle;
  255. }