ListBox.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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(newPara.QuickGroup==0)GuiShowStr(x+16,y,"关闭",0x01);
  152. else GuiShowStr(x+16,y,"开启",0x01);
  153. GuiReverseRect(x,y,64,16);
  154. }
  155. }
  156. else if(sutUIstatus.Status==UIS_SET_QUICK_GROUP)
  157. {
  158. x+=32;
  159. GuiReverseRect(x,y,LIST_BAR_LEN,16);
  160. }
  161. else{
  162. GuiReverseRect(x,y,LIST_BAR_LEN,16);
  163. }
  164. }
  165. /********************************************************************************
  166. * Function:ListBoxShow
  167. * display ListBox
  168. *******************************************************************************/
  169. void ListBoxShow(struct SUT_LIST_BOX *p)
  170. {
  171. unsigned char i,j;
  172. u16 x,y;
  173. if(p->itemnum==0)return;
  174. j=p->page*LIST_ROW;
  175. for(i = j ; i <j+LIST_ROW ; i ++)
  176. {
  177. if(i >= p->itemnum){
  178. y=LIST_TOPY+(i % LIST_ROW)*16;
  179. GuiClearArea(LIST_TOPX,y,LIST_BAR_LEN+18,16);
  180. }else{
  181. ListBoxShowItem(p,i);
  182. }
  183. }
  184. ListBoxShowBar(p);
  185. if(sutUIstatus.Status!=UIS_SET_QUICK_GROUP){
  186. //显示箭头
  187. x=LCD_WIDTH-17;
  188. if(p->page>0){
  189. GuiShowArrow(x+8,LIST_TOPY,8,3);//向上箭头
  190. }else{
  191. GuiClearArea(x,LIST_TOPY,16,8);
  192. }
  193. y=LIST_TOPY+LIST_ROW*16;
  194. if(p->page<(p->pgaenum-1)){
  195. GuiShowArrow(x+8,y,8,4);//向下箭头
  196. }else{
  197. GuiClearArea(x,y-8,16,8);
  198. }
  199. }
  200. }
  201. /********************************************************************************
  202. ListBoxResponse
  203. ListBox控件的键盘响应处理 底层
  204. *******************************************************************************/
  205. unsigned long ListBoxResponse(struct SUT_LIST_BOX *p)
  206. {
  207. unsigned char temp;
  208. if(p->itemnum==0)return g_ulKeyValue;
  209. // SUT_PHONE_NUM PhoneNum[N];
  210. // int i = 0;
  211. switch(g_ulKeyValue)
  212. {
  213. case KEY_PANEL_UP:
  214. if(QuickGroupSet)newPara.QuickGroup=!newPara.QuickGroup;
  215. ListBoxShowBar(p);
  216. #if 0
  217. if(p->handle==0)
  218. {
  219. p->handle=(p->itemnum-1);
  220. //清预览位置
  221. GuiClearArea(LIST_TOPX,LIST_TOPY,LIST_BAR_LEN,16*LIST_ROW);
  222. }
  223. #else
  224. if(p->handle==0&&!QuickGroupSet)p->handle=(p->itemnum-1);
  225. #endif
  226. else if(!QuickGroupSet) p->handle --;
  227. temp=p->page;
  228. p->page=p->handle/LIST_ROW;
  229. if(temp!=p->page)ListBoxShow(p);
  230. else ListBoxShowBar(p);
  231. break;
  232. case KEY_PANEL_DOWN:
  233. if(QuickGroupSet)newPara.QuickGroup=!newPara.QuickGroup;
  234. ListBoxShowBar(p);
  235. if(!QuickGroupSet)p->handle ++;
  236. #if 0
  237. if(p->handle>=p-> itemnum)
  238. {
  239. p->handle=0;
  240. //清预览位置
  241. GuiClearArea(LIST_TOPX,LIST_TOPY,LIST_BAR_LEN+16,16*LIST_ROW);
  242. }
  243. #else
  244. if(p->handle>=p-> itemnum&&!QuickGroupSet)p->handle=0;
  245. #endif
  246. temp = p->page;
  247. p->page=p->handle/LIST_ROW;
  248. if(temp!=p->page)ListBoxShow(p);
  249. else ListBoxShowBar(p);
  250. break;
  251. default:
  252. return g_ulKeyValue;
  253. break;
  254. }
  255. return 0;
  256. }
  257. /********************************************************************************
  258. ListBoxGetHandle
  259. 获取ListBox当前手柄
  260. *******************************************************************************/
  261. unsigned short ListBoxGetHandle(struct SUT_LIST_BOX *p)
  262. {
  263. return p->handle;
  264. }