/********************************************************************************
 * File Name:			ListBox.c 
 * Function Describe:	ListBox �ؼ�
 * Relate Module:		GUI.c
 * Writer:		Shliangwen
 * Date:		2016-1-8
 *******************************************************************************/
#include "Includes.h" 
 
void SwitchNextPageSMS(struct SUT_LIST_BOX *p,char up_down);
void ListBoxShowBarSMS(struct SUT_LIST_BOX *p,char rev);
void ListBoxShowSMS(struct SUT_LIST_BOX *p);

void ListBoxInitSMS(SUT_LIST_BOX *p,unsigned short totalnum,const char **iconlist,char unicode)
{
	unsigned char i,ch;
	p->totalnum   = totalnum;
	p->notehandle = 1;
	p->handle = 0;
	//unicode
	p->unicode=unicode;
	//icon
	i=0;
	p->icon=iconlist;
	while(i<LIST_ITEM_NUM_MAX)
	{
		if(iconlist==NULL) break;
		ch=p->icon[i][0];
		if(ch==0) break;
		i++;
	}
	p->iconnum=i;
	p->up_down_flag='d';
	GetPagePreMessage(p,1,'d');
}

void ListBoxItemNumShowSMS(uint16_t curIndex, uint16_t total)
{
	char buf[11];
	uint8_t len1,len2;
	static uint16_t len;
	//���������������������127����Ҫ�޸ı���������
	if(curIndex < 10)
		len1=1;
	else if(curIndex < 100)
		len1=2;
	else if(curIndex < 1000)
		len1=3;
	else len1=4;
	
	if(total < 10)
		len2=1;
	else if(total < 100)
		len2=2;
	else if(total < 1000)
		len2=3;
	else len2 = 4;
	
	snprintf(buf, sizeof(buf), "%d|%d", curIndex, total);
	if(len != (len1+len2)){
		len = len1+len2;
		//GuiClearArea(160-8*(4+4+2),STATUS_BAR_HEIGH,7*8,16);  //44
		GuiFillRect(160-8*(4+4+2),STATUS_BAR_HEIGH,160,STATUS_BAR_HEIGH+12,COLOR_WARN_RED);
	}
	GuiShowStr(160-8*(len1+len2+2),STATUS_BAR_HEIGH,buf,0x01,0);
}

/********************************************************************************
ListBoxShowItem
 *******************************************************************************/
void ListBoxShowItemSMS(struct SUT_LIST_BOX *p,unsigned short handle)
{
	u16 len;
	u16 x,y,hand;
	unsigned char buf[LIST_ITEM_TEXT_LEN_MAX+3];
	char str[LIST_ITEM_TEXT_LEN_MAX+1];
	char f;

	hand=handle % LIST_ROW;
	x=LIST_TOPX;
	y=LIST_TOPY+hand*16;
	#if 1  //��ҳʱ�����ɾ�
	GuiClearArea(x,y,LIST_BAR_LEN+16,16);
  #else
	GuiClearArea(x,y,LIST_BAR_LEN,16);
	#endif
	memset(buf,0,sizeof(buf));
	
	if(p->unicode){
		buf[LIST_ITEM_TEXT_LEN_MAX+2]=0;
	}else{
		//strncpy((char *)buf,p->item[handle],sizeof(buf));
		strncpy((char *)buf,p->boxinfo[handle].item,sizeof(buf));
		buf[LIST_ITEM_TEXT_LEN_MAX+2]=0;
	}
	//��ȡ���ʵ���..
	StrIntercept(str,(char *)buf,LIST_ITEM_TEXT_LEN_MAX);
	if(p->icon!=NULL){//��ͼ��
		//f= p->features[handle];
		
		if(p->boxinfo[handle].features >= p->iconnum)
		{
			SlwTrace(INF, "LB1",1);
			f=0;
		}
		else
		  f= p->boxinfo[handle].features;
		//f=0;
		//#error "f expired!"
		GuiShowBmp(x,y,p->icon[f]);
		GuiShowStr(x+16,y,str,0x01,0);
	}else{//��ͼ��
		GuiShowStr(x,y,str,0x01,0);
	}
}

/********************************************************************************
 * Function:ListBoxShowBar
 * display a bar
 *******************************************************************************/
void ListBoxShowBarSMS(struct SUT_LIST_BOX *p,char rev)
{
	unsigned char x,y,h,handle;
	char str[LIST_ITEM_TEXT_LEN_MAX+1];
	
	x=LIST_TOPX;
	if(p->icon!=NULL){//��ͼ��
			x+=16;
	}
	handle=p->handle;
	y=LIST_TOPY+handle*16;
	if(rev)GuiFillRect(x,y,LCD_WIDTH-20,y+14,COLOR_SELECT_BLUE);//
		else GuiFillRect(x,y,LCD_WIDTH-20,y+14,COLOR_WHITE_BACK);  //113
		StrIntercept(str,p->boxinfo[p->handle].item,LIST_ITEM_TEXT_LEN_MAX);
	GuiShowStr(x, y,str, 1, rev);
	
}
/********************************************************************************
 * Function:ListBoxShow
 * display ListBox
 *******************************************************************************/
void ListBoxShowSMS(struct SUT_LIST_BOX *p)
{
	unsigned char i,j;
	u16 x,y;

	if(p->itemnum==0) return;
	for(i=0;i<LIST_ROW;i++)
	{
		if(i >= p->itemnum)
		{
			y=LIST_TOPY+i*16;
			GuiClearArea(LIST_TOPX,y,LIST_BAR_LEN+18,16);
		}else
		ListBoxShowItemSMS(p,i);
	}
	
	if(p->up_down_flag=='u')	p->handle=p->itemnum-1;
	
	  ListBoxShowBarSMS(p,1);

	x=LCD_WIDTH-17;
	if(p->totalnum > LIST_ROW)
		GuiShowArrow(x+8,LIST_TOPY,8,3);//���ϼ�ͷ
	else
		GuiClearArea(x,LIST_TOPY,16,8);
	
	y=LIST_TOPY+LIST_ROW*16;
	if(p->totalnum > LIST_ROW)
		GuiShowArrow(x+8,y,8,4);//���¼�ͷ
	else
		GuiClearArea(x,y-8,16,8);

}

/********************************************************************************
ListBoxResponse
ListBox�ؼ��ļ�����Ӧ����   �ײ�
 *******************************************************************************/
unsigned long ListBoxResponseSMS(SUT_LIST_BOX *p)
{
	unsigned char temp;
	
	if(p->itemnum==0)return g_ulKeyValue;
	
	switch(g_ulKeyValue)
	{
		case KEY_PANEL_UP:
			ListBoxShowBarSMS(p,0);//����һ��
      p->notehandle--;
		  if(p->notehandle == 0)
				p->notehandle = p->totalnum;
		  if(p->handle == 0)
			{
				if(p->totalnum > LIST_ROW)
				{
					GetPagePreMessage(p,0,'u');
					p->pageChange=1;
				}else p->handle=p->itemnum-1;
			}else
			p->handle --;
			
			if(p->pageChange)
				p->pageChange=0;
			else
			ListBoxShowBarSMS(p,1);
		
			ListBoxItemNumShowSMS(p->notehandle,p->totalnum);

			break;
		case KEY_PANEL_DOWN:
		  ListBoxShowBarSMS(p,0);//����һ��
			p->handle ++;
		  p->notehandle++;
		  if(p->notehandle > p->totalnum)
				p->notehandle=1;
		  if(p->handle>=p-> itemnum)
			{
				if(p->totalnum > LIST_ROW)
				{
					GetPagePreMessage(p,0,'d');
				  p->pageChange=1;//page change
				}else p->handle=0;
			}
			if(p->pageChange)
				p->pageChange=0;
			else
			ListBoxShowBarSMS(p,1);
			
			ListBoxItemNumShowSMS(p->notehandle,p->totalnum);

			break;
	case KEY_PANEL_EXIT:
	  return g_ulKeyValue;
	  break;
		default:
			return g_ulKeyValue;
			break;
	}
	return 0;
}