Menu.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /******************************************************************
  10. 返回串str在LCD显示所占的x坐标长度
  11. *******************************************************************/
  12. int GetStrXLen(char *str)
  13. {
  14. unsigned char ch;
  15. unsigned short len;
  16. len=0;
  17. while ((ch=*str++)){
  18. if(ch=='M')len+=16;
  19. else len+=8;
  20. if(len>LCD_WIDTH-1){//191
  21. len=LCD_WIDTH-1;
  22. break;
  23. }
  24. }
  25. return (unsigned char )len;
  26. }
  27. /*************************************************************************
  28. *居中显示
  29. ***************************************************************************/
  30. void ShowCaption(const char *str,unsigned char y)
  31. {
  32. char buf[30];
  33. int len;
  34. len=GetStrXLen((char *)str);
  35. GuiShowStr((LCD_WIDTH-len)/2,y,str,0x01);
  36. }
  37. /*************************************************************************
  38. MenuInit
  39. ***************************************************************************/
  40. void MenuInit(struct SUT_MENU *p,const char **item)
  41. {
  42. unsigned char i,ch;
  43. //calculate menu item number
  44. p->item = item;
  45. i = 0;
  46. for(;;)
  47. {
  48. ch=p->item[i][0];
  49. if(ch==0)break;
  50. i ++;
  51. }
  52. i=i-1;
  53. p->itemnum = i+1;
  54. p->pgaenum=i/MENU_ROW;
  55. if(p->pgaenum%MENU_ROW)p->pgaenum+=1;
  56. //
  57. p->page = 0;
  58. p->handle = 0;
  59. MenuShow(p);
  60. }
  61. /********************************************************************************
  62. * Function:MenuShowItem
  63. * display menu item
  64. *******************************************************************************/
  65. void MenuShowItem(struct SUT_MENU *p,unsigned char handle)
  66. {
  67. unsigned char y,hand;
  68. hand=handle % MENU_ROW;
  69. y=MENU_TOPY+hand*(16+MENU_SPACE);
  70. GuiClearArea((LCD_WIDTH-MENU_BAR_LEN)/2,y-1,MENU_BAR_LEN,18);
  71. ShowCaption(p->item[handle],y);
  72. }
  73. /********************************************************************************
  74. * Function:MenuShowBar
  75. * display a bar
  76. *******************************************************************************/
  77. void MenuShowBar(struct SUT_MENU *p)
  78. {
  79. unsigned char x,y,h,handle;
  80. char *str;
  81. x=(LCD_WIDTH-MENU_BAR_LEN)/2;
  82. handle=p->handle%MENU_ROW;
  83. y=MENU_TOPY+handle*(16+MENU_SPACE);
  84. GuiReverseRect(x,y-1,MENU_BAR_LEN,18);
  85. }
  86. /********************************************************************************
  87. * Function:MenuShow
  88. * display menu
  89. *******************************************************************************/
  90. void MenuShow(struct SUT_MENU *p)
  91. {
  92. unsigned char i,j;
  93. u16 x,y;
  94. j=p->page*MENU_ROW;
  95. for(i = j ; i <j+MENU_ROW ; i ++)
  96. {
  97. if(i >= p->itemnum){
  98. y=MENU_TOPY+(i % MENU_ROW)*(16+MENU_SPACE);
  99. GuiClearArea((LCD_WIDTH-MENU_BAR_LEN)/2,y-1,MENU_BAR_LEN,18);
  100. }else{
  101. MenuShowItem(p,i);
  102. }
  103. }
  104. MenuShowBar(p);
  105. //显示箭头
  106. x=MENU_BAR_LEN+(LCD_WIDTH-MENU_BAR_LEN)/2;
  107. if(p->page>0){
  108. GuiShowArrow(x+10,MENU_TOPY,8,3);//向上箭头
  109. }else{
  110. GuiClearArea(x,MENU_TOPY,18,8);
  111. }
  112. y=MENU_TOPY+MENU_ROW*(16+MENU_SPACE);
  113. if(p->page<(p->pgaenum-1)){
  114. GuiShowArrow(x+10,y,8,4);//向下箭头
  115. }else{
  116. GuiClearArea(x,y-8,18,8);
  117. }
  118. }
  119. /********************************************************************************
  120. * Function:MenuResponse
  121. *******************************************************************************/
  122. unsigned long MenuResponse(struct SUT_MENU *p)
  123. {
  124. unsigned char temp;
  125. switch(g_ulKeyValue)
  126. {
  127. case KEY_PANEL_UP:
  128. MenuShowBar(p);
  129. if(p->handle==0)p->handle=(p->itemnum-1);
  130. else p->handle --;
  131. temp=p->page;
  132. p->page=p->handle/MENU_ROW;
  133. if(temp!=p->page)MenuShow(p);
  134. else MenuShowBar(p);
  135. break;
  136. case KEY_PANEL_DOWN:
  137. MenuShowBar(p);
  138. p->handle ++;
  139. if(p->handle>=p-> itemnum)p->handle=0;
  140. temp = p->page;
  141. p->page=p->handle/MENU_ROW;
  142. if(temp!=p->page)MenuShow(p);
  143. else MenuShowBar(p);
  144. break;
  145. // case KEY_PANEL_OK:
  146. // return MENU_KEY_ENTER;
  147. //
  148. // case KEY_PANEL_EXIT:
  149. // return MENU_KEY_BACK;
  150. //
  151. default:
  152. return g_ulKeyValue;
  153. break;
  154. }
  155. return 0;
  156. }
  157. /********************************************************************************
  158. * Function:MenuGetHandle
  159. * get menu 's handle
  160. *******************************************************************************/
  161. unsigned char MenuGetHandle(struct SUT_MENU *p)
  162. {
  163. unsigned char handle;
  164. handle = p->handle;
  165. /*
  166. for(i = 0 ; i <= p->handle ; i ++)
  167. {
  168. if(p->item[i][0] == 0)handle --;
  169. }
  170. */
  171. return handle;
  172. }