#include <Symbol.h>
#include <MyFrame.h>

#include "list_led.h"


CList_Led::CList_Led(CObject* pParent):CObject(pParent)
{
    m_bTab = TRUE;

    m_nForeColor =BLACK ;
    //m_nBackColor = LIGHTBLUE;
    m_nBackColor = LIGHTBLUE;
   
    m_nBoxColor =  CYAN;
    m_nSelColor=WHITE;
    
    m_pJoint=NULL;
    m_pEdit=NULL;
    m_njointnum=0;

//	  m_ncur_opt=0;
//	  m_ntop_opt=0;
//	  m_noffset=0;
//	  m_npri_opt=0;
    //===================
    //m_bState = TRUE;
}

CList_Led::~CList_Led()
{
	if(m_pJoint!=NULL)
	delete m_pJoint;
	if(m_pEdit!=NULL)
	delete m_pEdit;
}

void CList_Led::drawlist( )
{
	
  if(m_njointnum<1)
  return;
  
	int i=0,j;
  int a,b;//列表的开始列和行坐标	
	int width,height;//列表的列数和行数	
	
	a=m_ClientRect.left;
	b=m_ClientRect.top;
	
	width=(m_ClientRect.right-m_ClientRect.left)/16;
	height=(m_ClientRect.bottom-m_ClientRect.top)/16;	
	
	m_nLedLen=(m_ClientRect.right-m_ClientRect.left-LISTORDER*16)/LEDNUMBER;
	
	j=(m_nLedLen-2*LEDSPACE)/2;
	m_nLedRad=((j>6)?6:j);
	
	
	int jointnum=m_njointnum;
	//FCOLOR=white;	//list font
	if(m_bredraw)      //全部重画
	{		
    int nOldColor=::GetColor();
    //::SetColor (m_nBackColor);
    ::SetFillStyle(1, m_nBackColor);	
    ::Bar(m_ClientRect.left, m_ClientRect.top,
        m_ClientRect.right, m_ClientRect.bottom);	
		for(i=m_ntop_opt;i<m_ntop_opt+height;i++)
		{
		   if(i>=jointnum)  break;
		   j=i-m_ntop_opt;
		   //if((i==m_ncur_opt) && (m_bredraw==true))//当前菜单项变亮
		   if(i==m_ncur_opt)//当前菜单项变亮
		   {	
//         if(m_ncur_opt==m_ntop_opt)
//         AfxMessageBox("draw list()", "绘制单前菜单");
         if(m_ncur_opt==m_ntop_opt)
         ;
         //::SetColor (m_nSelColor);
         ::SetFillStyle(1, m_nSelColor);	
         ::Bar(a,b+16*j,m_ClientRect.right,b+16*j+16);
         Drawled(a,b+16*j,i); }		   
		   else
		   {
		     Drawled(a,b+16*j,i);  }
		}               //end for (i=top_opt;i<top_opt+height;i++)
		::SetColor (nOldColor);
	}                       //end if(m_bredraw)


  return;	
	
}

void CList_Led::List_Select(int nKey)
{

    int i=0,  j=0;
	  int width,height;//列表的列数和行数	
	  
	  width=(m_ClientRect.right-m_ClientRect.left)/16;
	  height=(m_ClientRect.bottom-m_ClientRect.top)/16;	
	  	
	  int last=0;			//列表总的选项数
	  int ch;		//键盘输入值
	  ch=nKey;
    
	  last=m_njointnum-1;
	  m_ny=m_ncur_opt-m_ntop_opt+1;//当前选项和列表头选项之间位移
	                          	
		if(ch==VK_DOWN && m_ny<height && m_ncur_opt<last)          	
		{                                                 	
			m_npri_opt=m_ncur_opt,m_ncur_opt++; 			
			//m_bredraw=FALSE; 
			m_bredraw=TRUE;	}                           	
		else if(ch==VK_DOWN && m_ny==height && m_ncur_opt<last)    	
		{                                                 	
			m_npri_opt=m_ncur_opt,m_ncur_opt++,m_ntop_opt++; 
			m_bredraw=TRUE; }                                           
		if(ch==VK_UP && m_ny>1)
		{                                                 	
			m_npri_opt=m_ncur_opt,m_ncur_opt--;      	
			//m_bredraw=FALSE;
			m_bredraw=TRUE; }                         	
		else if(ch==VK_UP && m_ny==1 && m_ncur_opt>0)              	
		{                                                 	
			m_npri_opt=m_ncur_opt,m_ncur_opt--;m_ntop_opt--; 
			m_bredraw=TRUE; }                          	
		if(ch==VK_PGDOWN && m_ntop_opt!=last)                     	
		{                                                 	
			m_noffset=m_ncur_opt-m_ntop_opt;                   	
			m_ntop_opt+=height;                          	
			m_ncur_opt=m_ntop_opt+m_noffset;                   	
			m_npri_opt=m_ncur_opt;                          	
			if(m_ntop_opt>last) m_ntop_opt=last;            	
			if(m_ncur_opt>last) m_ncur_opt=last;       	
			m_bredraw=TRUE; }                         	
		if(ch==VK_PGUP && m_ntop_opt!=0)                        	
		{                                                 	
			m_noffset=m_ncur_opt-m_ntop_opt;                   	
			m_ntop_opt-=height;                          	
			m_ncur_opt=m_ntop_opt+m_noffset;                   	
			if(m_ntop_opt<0){m_ntop_opt=0,m_ncur_opt=m_noffset;}  	
			m_npri_opt=m_ncur_opt;      	
			m_bredraw=TRUE; } 
			
		if(m_ntop_opt>m_njointnum-1)
		{m_ntop_opt=m_njointnum-1;m_bredraw=TRUE; }	
		if(m_ny>height)
		{m_ncur_opt=height-1+m_ntop_opt;m_bredraw=TRUE; }
		if(m_ncur_opt>m_njointnum-1)
		{m_ncur_opt=m_njointnum-1;m_bredraw=TRUE; }		
		if(m_ntop_opt>m_ncur_opt)
		{m_ntop_opt=m_ncur_opt;m_bredraw=TRUE; }

	
		
//		if((m_ncur_opt==0))	
//		{ m_ntop_opt=0;
//			m_ncur_opt=0;
//			m_bredraw=TRUE; }
			
		//m_bredraw=TRUE;	  
	  char buf[256];
	  m_ny=m_ncur_opt-m_ntop_opt+1;//当前选项和列表头选项之间位移
    sprintf(buf,"cur_opt=%d\ntop_opt=%d\noffset=%d\npri_opt=%d\ny=%d\nbredaw=%d\n",    
     m_ncur_opt,m_ntop_opt,m_noffset,m_npri_opt,m_ny,m_bredraw );
    //AfxMessageBox("list_sel()", buf);
    if(m_bredraw)
		draw(  );//画列表选项
		m_bredraw=FALSE;
		//state[3]=state[0];		//pri-opt=cur-opt
		//m_npri_opt=m_ncur_opt;
		//m_ny=m_ncur_opt-m_ntop_opt+1;//当前选项和列表头选项之间位移
}


void CList_Led::SetFocus()
{
    viewporttype mView;
    BOOL bMouse = CList_Led::StartControlView(&mView);
    //----------------------------------------------
    //int nLeft, nTop;

    int nOldColor=::GetColor();
    ::SetColor (m_nBoxColor);
    ::Rectangle(m_ClientRect.left, m_ClientRect.top,
        m_ClientRect.right, m_ClientRect.bottom);
        
    m_bredraw=TRUE;	
	  m_ncur_opt=m_ntop_opt=m_noffset=m_npri_opt=0; 
	  m_ncur_opt=0;   
	  //m_ny=m_ncur_opt-m_ntop_opt+1;//当前选项和列表头选项之间位移
		drawlist(  );//画列表选项
		m_bredraw=FALSE;
		//state[3]=state[0];		//pri-opt=cur-opt
		m_npri_opt=m_ncur_opt;
		//m_ny=m_ncur_opt-m_ntop_opt+1;//当前选项和列表头选项之间位移
		
		//::SetColor (m_nSelColor);
		::SetFillStyle(1, m_nSelColor);
    ::Bar(m_ClientRect.left,m_ClientRect.top+16*m_ncur_opt,
    	m_ClientRect.right,m_ClientRect.top+16*m_ncur_opt+16);	
       
     if(m_njointnum>0)     
     {
     	 Drawled(m_ClientRect.left,m_ClientRect.top+16*m_ncur_opt,
               m_ncur_opt);
     }
     
     ::SetColor (nOldColor);

    //AfxMessageBox("Debug", "set focus");
    //----------------------------------------------
  	CObject::EndControlView(&mView, bMouse);
    return;
}

//void CList_Led::KillFocus()
//{
//    viewporttype mView;
//    BOOL bMouse = CList_Led::StartControlView(&mView);
//    //----------------------------------------------
//    //int nLeft, nTop;
//
//    int nOldColor=::GetColor();
//    ::SetColor(m_nBackColor);
//    //::SetFillStyle(1, m_nBackColor);
//    ::Rectangle(m_ClientRect.left, m_ClientRect.top,
//        m_ClientRect.right, m_ClientRect.bottom);
//
//    ::SetFillStyle(1, m_nBackColor);
//    ::Bar(m_ClientRect.left,m_ClientRect.top+16*m_ncur_opt,
//    	m_ClientRect.right,m_ClientRect.top+16*m_ncur_opt+16);	
//       
//     Drawled(m_ClientRect.left,m_ClientRect.top+16*m_ncur_opt,
//     m_pJoint[m_ncur_opt].strorder);
//     
//     ::SetColor(nOldColor);
//    //AfxMessageBox("Debug", "kill focus");
//    //----------------------------------------------
//  	CObject::EndControlView(&mView, bMouse);
//    return;
//
//}

BOOL CList_Led::Show()
{
    viewporttype mView;
    BOOL bMouse = CList_Led::StartControlView(&mView);
    //----------------------------------------------
    //::SetFillStyle(1, m_nBackColor);
    this->EditShow();
    
    int nOldColor=::GetColor();
    ::SetColor (m_nBackColor);
    ::Rectangle(m_ClientRect.left, m_ClientRect.top,
        m_ClientRect.right, m_ClientRect.bottom);
     ::SetColor(nOldColor);
     	        
    m_bredraw=TRUE;	
	  //m_ncur_opt=m_ntop_opt=m_noffset=m_npri_opt=0;    
		drawlist(  );//画列表选项
		m_bredraw=FALSE;
		//state[3]=state[0];		//pri-opt=cur-opt
		//m_npri_opt=m_ncur_opt;
		//m_ny=m_ncur_opt-m_ntop_opt+1;//当前选项和列表头选项之间位移
		
//		::SetColor (m_nSelColor);
//    ::Bar(m_ClientRect.left,m_ClientRect.top+16*m_ncur_opt,
//    	m_ClientRect.right,m_ClientRect.top+16*m_ncur_opt+16);	
//       
//     Drawled(m_ClientRect.left,m_ClientRect.top+16*m_ncur_opt,
//     m_pJoint[m_ncur_opt].strorder); 

    this->KillFocus();
    //AfxMessageBox("Debug", "Show");
    //----------------------------------------------
  	CObject::EndControlView(&mView, bMouse);
    return (TRUE);
}

int CList_Led::Swapping()
{
    int nKey = CObject::DefaultSwapping();

    switch (nKey)
    {
        case VK_MOUSE_SCREEN_OUT:
        {
            break;
        }
        case VK_MOUSE_CLIENT_OUT:
        {
            nKey = 0;

            break;
        }
        case VK_MOUSE_DOWN_LEFT: //Mouse Left Button is Down.
        {
            int nMouseX = ::GetMouseX();
            int nMouseY = ::GetMouseY();

            //left mouse button is down?
            while (::IsMouseDown(MOUSE_LEFT))
            {
            }

            break;
        }
	      case VK_UP:
	      case VK_DOWN:
	      case VK_PGUP :
	      case VK_PGDOWN:
        {
        
            List_Select(nKey);
            //draw();
            ////AfxMessageBox("Debug", "List_Select");
            nKey = 0;
            break;        
        }       
        
//        case VK_F1:
//        {
//            AfxMessageBox("Debug", "F1 Key");
//
//            nKey = 0;
//            break;
//        }
//        case VK_0:
//        case VK_1:
//        case VK_2:
//        case VK_3:
//        case VK_4:
//        case VK_5:
//        case VK_6:
//        case VK_7:
//        case VK_8:
//        case VK_9:
//        {
//            char buf[100];
//            ::sprintf (buf, "This is Number Key");
//
//            AfxMessageBox("Debug", buf);
//
//            nKey = 0;
//            break;
//        }   
        
        case VK_ENTER:
        {
        	nKey=Input();        
        }
        
        default:
        {
            break;
        }
    }

    return (nKey);
}

/*********
显示每行led灯和文字

***********/
//void CList_Led::Drawled(int nLeft,int nTop,char *pStr)
void CList_Led::Drawled(int nLeft,int nTop,int nindex)
{
    
    int i,j,k;
    int nOldColor=::GetColor();
    ::SetColor( m_nForeColor);
    ::ChPrintf (nLeft, nTop, m_nForeColor, m_nBackColor, 1, 0, m_pJoint[nindex].strorder);
    	
    if(m_njointnum<1)
    return;
    
    for(i=0;i<LEDNUMBER;i++)
    {

      if(m_pJoint[nindex].led & (0x01<<i))
      {
         if(i%3)
         {::SetColor(WHITE);
          ::SetFillStyle(1, GREEN);
          ::FillEllipse(nLeft+LISTORDER*16+i*m_nLedLen+m_nLedRad+LEDSPACE,
            nTop+8, m_nLedRad, m_nLedRad);}
         else
         {::SetColor(WHITE);
          ::SetFillStyle(1, RED);
          ::FillEllipse(nLeft+LISTORDER*16+i*m_nLedLen+m_nLedRad+LEDSPACE,
            nTop+8, m_nLedRad, m_nLedRad);}       
      } 
      else
      {
        ::SetColor(WHITE);
        ::SetFillStyle(1, m_nBackColor);
        ::FillEllipse(nLeft+LISTORDER*16+i*m_nLedLen+m_nLedRad+LEDSPACE,
          nTop+8, m_nLedRad, m_nLedRad);  
      }   
         
         
         
    }
    ::SetColor( nOldColor );	
    	
}

/*************
各项显示均正常,但不能画一闪一闪的光标!!!
请问如何改进为好??????
****************/

void CList_Led::draw()
{
    viewporttype mView;
    BOOL bMouse = CList_Led::StartControlView(&mView);
    //----------------------------------------------
    ::SetFillStyle(1, m_nBackColor);
    this->EditShow();

    int nOldColor=::GetColor();
    ::SetColor (m_nBackColor);
    ::Rectangle(m_ClientRect.left, m_ClientRect.top,
        m_ClientRect.right, m_ClientRect.bottom);
        
    //m_bredraw=TRUE;	
	  //m_ncur_opt=m_ntop_opt=m_noffset=m_npri_opt=0;    
		drawlist(  );//画列表选项
    ::SetColor (nOldColor);
 
//    char buf[256];
//    sprintf(buf,"cur_opt=%d\ntop_opt=%d\noffset=%d\npri_opt=%d\ny=%d\nbredaw=%d\n",
//    
//     m_ncur_opt,m_ntop_opt,m_noffset,m_npri_opt,m_ny,m_bredraw   );
//    //AfxMessageBox("draw()", buf);
    //----------------------------------------------
  	CObject::EndControlView(&mView, bMouse);
    return ;
}

/*************
此输入函数可输入数字,字母,
切换至中文输入法,不可输入!!
请问如何改进为好??????
****************/

int CList_Led::Input()
{
  
  int nlef,ntop;    
  nlef=m_ClientRect.left;
  ntop=m_ClientRect.top+16*(m_ncur_opt-m_ntop_opt);
  
  if(m_pEdit==NULL)
  { m_pEdit=new CEdit();
  	m_pEdit->CreateObject(nlef, ntop, LISTORDER*8+10, 16, NULL);}
  //m_pEdit=new CEdit(this);
  //m_pEdit=new CEdit();
  //m_pEdit=new CEdit((*this)GetParent());
  if(m_pEdit==NULL)
  AfxMessageBox("input()", "mpedit=NULL内存分配错"); 
  m_pEdit->SetVisible (TRUE);
   
  //char cbuf[LISTORDER+1];
    char buf[256];
//    sprintf(buf,"nClientlef=%d\nnClienttop=%d\nnScreenlef=%d\nnScreentop=%d\nm_ncur_opt=%d\n",
//    
//     nClientlef,nClienttop,nScreenlef,nScreentop,m_ncur_opt   );
//    AfxMessageBox("edit()", buf);  

  //m_pEdit->CreateObject(nlef, ntop, LISTORDER*8+10, ntop+16, NULL);
  m_pEdit->SetLocationEx(nlef, ntop, LISTORDER*8+10, 16);
  m_pEdit->SetMaxCount(LISTORDER);
  //m_pEdit->SetNumberFlags(TRUE);
  m_pEdit->SetID(EDITBASE + 1 - 1);
  
  m_pEdit->SetBuffer(m_pJoint[m_ncur_opt].strorder);
  m_pEdit->Show(); 
  int nkey;
//  nkey= m_pEdit->Swapping();
  m_pEdit->SetFocus();
  
  while(1)
  {
    nkey= m_pEdit->Swapping();
    if(nkey)
	  {nkey=0;break;}
    //if(nkey==VK_UP||nkey==VK_DOWN||nkey==VK_PGUP||nkey== VK_PGDOWN)
	  //{nkey=0;break;}
  }
  
  //if(m_pEdit->GetModify())
  { strcpy(m_pJoint[m_ncur_opt].strorder,m_pEdit->GetBuffer()); }
  m_pEdit->SetVisible (FALSE);
//  if(m_pEdit!=NULL)
//  {delete m_pEdit;m_pEdit=NULL;} 
  
//      sprintf(buf,"m_pEdit->GetBuffer=%s\nnClientbotom=%d\nnScreenright=%d\nnScreenbot=%d\nnkey=%d\n",
//    
//     m_ClientRect.right,m_ClientRect.bottom,m_ScreenRect.right,m_ScreenRect.bottom,nkey   );
//    AfxMessageBox("edit()", buf);

  m_bredraw=TRUE;
	draw(  );//画列表选项
	m_bredraw=FALSE; 
	
//	if(nkey==VK_UP||nkey==VK_DOWN||nkey==VK_PGUP||nkey== VK_PGDOWN)
//	{nkey=0;} 
//	nkey=0;
	return (nkey);

}