Menu.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /********************************************************************************
  2. * File Name: Menu.c
  3. * Function Describe: Menu 控件
  4. * Relate Module: GUI.c
  5. * Writer: Shliangwen
  6. * Date: 2016-1-8
  7. *******************************************************************************/
  8. #include "Includes.h"
  9. unsigned char fanhui;
  10. /******************************************************************
  11. 返回串str在LCD显示所占的x坐标长度
  12. *******************************************************************/
  13. int GetStrXLen(char *str)
  14. {
  15. unsigned char ch;
  16. unsigned short len;
  17. len=0;
  18. while ((ch=*str++)){
  19. if(ch=='M')len+=12;
  20. else if(ch>='A'&&ch<='Z')len+=8;
  21. else if(ch>='a'&&ch<='z')len+=8;
  22. else if(ch>='0'&&ch<='9')len+=8;
  23. else if(ch>=0x20&& ch<=0x60)len+=8; //特殊字符
  24. else len+=6;
  25. if(len>LCD_WIDTH-1){//191
  26. len=LCD_WIDTH-1;
  27. break;
  28. }
  29. }
  30. return (unsigned char )len;
  31. }
  32. /*************************************************************************
  33. *居中显示
  34. ***************************************************************************/
  35. void ShowCaption(const char *str,unsigned char y)
  36. {
  37. //char buf[30];
  38. int len;
  39. len=GetStrXLen((char *)str);
  40. GuiShowStr((LCD_WIDTH-len)/2,y,str,0x01);
  41. }
  42. /*************************************************************************
  43. *某个区域内居中显示
  44. ***************************************************************************/
  45. void ShowCaptionArea(const char *str,unsigned char x1,unsigned char x2,unsigned char y)
  46. {
  47. //char buf[30];
  48. int len;
  49. len=GetStrXLen((char *)str);
  50. GuiShowStr(x1+(x2-x1-len)/2,y,str,0x01);
  51. }
  52. /*************************************************************************
  53. MenuInit
  54. ***************************************************************************/
  55. void MenuInit(struct SUT_MENU *p,const char **item)
  56. {
  57. unsigned char i,ch;
  58. unsigned char index;
  59. //calculate menu item number
  60. p->item = item;
  61. i = 0;
  62. for(;;)
  63. {
  64. ch=p->item[i][0];
  65. if(ch==0)break;
  66. i ++;
  67. }
  68. i=i-1;
  69. p->itemnum = i+1;
  70. p->pgaenum=i/MENU_ROW;
  71. if(p->pgaenum%MENU_ROW)p->pgaenum+=1;
  72. if(g_ulKeyValue==KEY_PANEL_EXIT||fanhui==1)
  73. {
  74. index=PullUiStack(&UI_Stack);
  75. p->page=index/5;
  76. p->handle=index;
  77. fanhui=0;
  78. }else if(g_ulKeyValue==KEY_PANEL_MENU){
  79. p->page=0;
  80. p->handle=0;
  81. }
  82. MenuShow(p);
  83. }
  84. /********************************************************************************
  85. * Function:MenuShowItem
  86. * display menu item
  87. *******************************************************************************/
  88. void MenuShowItem(struct SUT_MENU *p,unsigned char handle)
  89. {
  90. unsigned char y,hand;
  91. hand=handle % MENU_ROW;
  92. y=MENU_TOPY+hand*(16+MENU_SPACE);
  93. GuiClearArea((LCD_WIDTH-MENU_BAR_LEN)/2,y-1,MENU_BAR_LEN,18);
  94. ShowCaption(p->item[handle],y);
  95. }
  96. /********************************************************************************
  97. * Function:MenuShowBar
  98. * display a bar
  99. *******************************************************************************/
  100. void MenuShowBar(struct SUT_MENU *p)
  101. {
  102. unsigned char x,y,h,handle;
  103. char *str;
  104. x=(LCD_WIDTH-MENU_BAR_LEN)/2;
  105. handle=p->handle%MENU_ROW;
  106. y=MENU_TOPY+handle*(16+MENU_SPACE);
  107. GuiReverseRect(x,y-1,MENU_BAR_LEN,14);
  108. }
  109. /********************************************************************************
  110. * Function:MenuShow
  111. * display menu
  112. *******************************************************************************/
  113. void MenuShow(struct SUT_MENU *p)
  114. {
  115. unsigned char i,j;
  116. u16 x,y;
  117. j=p->page*MENU_ROW;
  118. for(i = j ; i <j+MENU_ROW ; i ++)
  119. {
  120. if(i >= p->itemnum){
  121. y=MENU_TOPY+(i % MENU_ROW)*(16+MENU_SPACE);
  122. GuiClearArea((LCD_WIDTH-MENU_BAR_LEN)/2,y-1,MENU_BAR_LEN,18);
  123. }else{
  124. MenuShowItem(p,i);
  125. }
  126. }
  127. MenuShowBar(p);
  128. //显示箭头
  129. x=MENU_BAR_LEN+(LCD_WIDTH-MENU_BAR_LEN)/2;
  130. if(p->page>0){
  131. GuiShowArrow(x+10,MENU_TOPY,8,3);//向上箭头
  132. }else{
  133. GuiClearArea(x,MENU_TOPY,18,8);
  134. }
  135. y=MENU_TOPY+MENU_ROW*(16+MENU_SPACE);
  136. if(p->page<(p->pgaenum-1)){
  137. GuiShowArrow(x+10,y,8,4);//向下箭头
  138. }else{
  139. GuiClearArea(x,y-8,18,8);
  140. }
  141. }
  142. /********************************************************************************
  143. * Function:MenuResponse
  144. *******************************************************************************/
  145. unsigned long MenuResponse(struct SUT_MENU *p)
  146. {
  147. unsigned char temp;
  148. switch(g_ulKeyValue)
  149. {
  150. case KEY_PANEL_UP:
  151. MenuShowBar(p);
  152. if(p->handle==0)p->handle=(p->itemnum-1);
  153. else p->handle --;
  154. temp=p->page;
  155. p->page=p->handle/MENU_ROW;
  156. if(temp!=p->page)MenuShow(p);
  157. else MenuShowBar(p);
  158. break;
  159. case KEY_PANEL_DOWN:
  160. MenuShowBar(p);
  161. p->handle ++;
  162. if(p->handle>=p-> itemnum)p->handle=0;
  163. temp = p->page;
  164. p->page=p->handle/MENU_ROW;
  165. if(temp!=p->page)MenuShow(p);
  166. else MenuShowBar(p);
  167. break;
  168. // case KEY_SOS_OK:
  169. // return MENU_KEY_ENTER;
  170. //
  171. case KEY_PANEL_EXIT:
  172. sutUIstatus.LastPage=p->page;
  173. //
  174. default:
  175. return g_ulKeyValue;
  176. break;
  177. }
  178. return 0;
  179. }
  180. /********************************************************************************
  181. * Function:MenuGetHandle
  182. * get menu 's handle
  183. *******************************************************************************/
  184. unsigned char MenuGetHandle(struct SUT_MENU *p)
  185. {
  186. unsigned char handle;
  187. handle = p->handle;
  188. /*
  189. for(i = 0 ; i <= p->handle ; i ++)
  190. {
  191. if(p->item[i][0] == 0)handle --;
  192. }
  193. */
  194. return handle;
  195. }