⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 te_key.c

📁 用BC++3.1编的C语言文本编辑器
💻 C
字号:
#include "te.h"


void clrkey(void)     /*清除键盘缓冲区的按键*/
{
while(keypress())getkey(NULL);
}

unsigned int getkey(KEYCODE *pkc)      /*读键,KEYCODE在te.h中定义*/
{
 union REGS regkey;
 regkey.h.ah=0x10;
 int86(0x16,&regkey,&regkey);
 if(pkc)
 {
  pkc->code=regkey.h.ah;
  pkc->c  =regkey.h.al;
  pkc->cup=toupper((int)regkey.h.al);
  pkc->clo=tolower((int)regkey.h.al);
 }
 return (int)regkey.h.ah;
}

char  keypress(void)       /*C语言的kbhit()会被Ctrl+Break中断,所以自己编写*/
{  int far *key_start,*key_next;
key_start=(int far *)MK_FP(0x40,0x1a);
key_next=(int far *)MK_FP(0x40,0x1c);
if(*key_start!=*key_next)return(1);
else return(0);
}

unsigned int chkkey(unsigned int t) /*简查Shift,atl,ctrl的状态*/
{unsigned char x;
 x=bioskey(2);
 if(t==1){if(x&1||x&2)return(1);
	      else return(0);}
 else return(x&t);
}


int DealAltKey(KEYCODE *pkc,int *pshow)
{
  if(!chkkey(ALT))return pkc->code;
  *pshow=0;
  switch(pkc->code)
    {
    case KEY_F:
       cur_menu_x=0;
       is_showpop=1;
       DriveMenu(pkc);
       break;
    case KEY_S:
       cur_menu_x=1;
       is_showpop=1;
       DriveMenu(pkc);
       break;
    case KEY_O:
       cur_menu_x=2;
       is_showpop=1;
       DriveMenu(pkc);
       break;
    case KEY_H:
       cur_menu_x=3;
       is_showpop=1;
       DriveMenu(pkc);
       break;
    case KEY_X:
       pkc->code=0X100+'X';
   }
return pkc->code;
}

int SearchString(int is_ask)
{
 static char szFind[100]={0};
 TEXTLINE *ptx=pcurtx,*ptx1;
 int len1,len2,i,j,is_ret=0;
 if(is_ask||strlen(szFind)==0)
 {
  if(AskFileName(szFind,"Looking for String:",1))
    return 0;
 }
 len1=slen(szFind);
 if(len1==0)return 0;
 while(ptx&&!is_ret)
 {len2=slen(ptx->str);
  if(len1)
   {
    for(i=0;i<len2;i++)
     if(strnicmp(szFind,ptx->str+i,len1)==0)
      {is_ret=1;
       while(pcurtx!=ptx)GoDown();
       if(i>screen_max_x)
	{
	 offset_x=i-screen_max_x;
	 cur_x=i-offset_x;
	}
       else cur_x=i;
	ptx1=pindex;
	cur(0);
	clr();
	for(i=0;i<screen_max_y;i++)
	{
	 if(ptx1==NULL)break;
	 if(ptx1->str&&offset_x<slen(ptx1->str))
	 if(ptx1==ptx)
	 {
	  outsn(2,i+3,wincolor,ptx1->str+offset_x,cur_x);
	  len1=(cur_x+len1>screen_max_x?screen_max_x-cur_x:len1);
	  outsn(2+cur_x,i+3,0x2E,ptx1->str+offset_x+cur_x,len1);
	  outsn(2+cur_x+len1,i+3,wincolor,ptx1->str+offset_x+cur_x+len1,
	   screen_max_x-cur_x-len1);
	  j=i;
	 }
	 else
	 outsn(2,i+3,wincolor,ptx1->str+offset_x,screen_max_x);
	 ptx1=ptx1->pnext;
	}
	gotoxy(cur_x+1,cur_y+1);
	clrkey();
	getkey(NULL);
	cur(cur_type);
	outsn(2,j+3,wincolor,ptx->str+offset_x,screen_max_x);
	return 0;
      }
   }
  ptx=ptx->pnext;
 }
 MsgBox("\r\t\x5 Search finished!"," \xfe Search",2,30,0x5e54,0,NULL);
 return 0;
}


int DealCtrlKey(KEYCODE *pkc,int *pshow)
{
 if(!chkkey(CTRL))return pkc->code;
 *pshow=0;
 switch(pkc->code)
    {
      case KEY_F:
	*pshow=SearchString(1);
	return 0;
    }
return pkc->code;
}



int DealOtherKey(KEYCODE *pkc,int *pshow)
{
TEXTLINE *ptx;
char szTemp[MAXPATH];
int i;
*pshow=0;
switch(pkc->code)
 {
   case 0x400+'H':
   case F1:
       help();
       return 0;
   case 0x100+'N':
       if(isModify)
	{
	 i=SaveFile(1);
	 if(i==ESC||i==KEY_C)break;
	}
       NewFile();
       *pshow=1;
       mainmenu(1);
       break;
   case 0x100+'O':
   case F2:
       if(isModify)
	{
	 i=SaveFile(1);
	 if(i==ESC||i==KEY_C)break;
	}
       if(AskFileName(szTemp,"Input the file path and name:",0))
       return 1;
       NewFile();
       strcpy(szFilePath,szTemp);
       LoadFile();
       mainmenu(1);
       *pshow=1;
       return 1;
   case 0x100+'A':
   case F4:
       if(AskFileName(szFilePath,"Save current file to:",1))
	  return 0;

   case 0x100+'S':
   case F3:
       SaveFile(0);
       mainmenu(0);
       return 0;
   case F5:
	*pshow=SearchString(0);
	break;
   case 0x200+'F':
	*pshow=SearchString(1);
       break;
   case 0x300+'B':
   case F8:
       if((wincolor>>4)<0xf)wincolor+=0x10;
       else wincolor=wincolor&0xf;
       *pshow=1;
       return 0;
   case 0x300+'T':
   case F7:
       if((wincolor&0xf)<0xf)wincolor++;
       else wincolor=wincolor&0xf0;
       *pshow=1;
       return 0;
   case 0x400+'A':
       MsgBox("\r\t\x5 Text editor Ver1.0\r\r\t\x8 -- 2000.4 --\0",
	" \xfe About",3,30,0x3e34,0,NULL);
       break;
   case CTRLHOME:
       *pshow=1;
       pindex=pcurtx=ptext;
       offset_x=0;
       offset_y=0;
       cur_x=0;
       cur_y=0;
       return 0;
   case CTRLEND:
       GoBottom();
       *pshow=1;
       return 0;
   case F10:
       is_showpop=0;
       DriveMenu(pkc);
       break;
  }
return pkc->code;
}

/*以下是光标控制*/
int GoHome(void)
{
  int ret=0;
  if(offset_x)ret=1;
  cur_x=0;
  offset_x=0;
  return ret;
}

int GoEnd(void)
{
 if(offset_x+screen_max_x>slen(pcurtx->str))
	cur_x=slen(pcurtx->str)%screen_max_x;
      else
	{cur_x=screen_max_x-1;
	 offset_x=slen(pcurtx->str)-cur_x;
	 return 1;
	}
return 0;
}

int GoDown(void)
{
 if(pcurtx->pnext==NULL)return 0;
 if(cur_y<screen_max_y-1)
  {
   pcurtx=pcurtx->pnext;
   cur_y++;
  }
 else if(pcurtx->pnext)
  {
   offset_y++;
   pindex=pindex->pnext;
   pcurtx=pcurtx->pnext;
   return 1;
  }
return 0;
}

int GoPageUp(void)
{
int i,j;
if(pcurtx->plast==NULL)return 0;
j=cur_y;
for(i=0;i<screen_max_y*2+j;i++)
 {
  if(pcurtx->plast==NULL)return 1;
  GoUp();
 }
for(i=0;i<j;i++)
 GoDown();
return 1;
}


int GoPageDown(void)
{
int i,j;
if(pcurtx->pnext==NULL)return 0;
j=cur_y;
for(i=0;i<screen_max_y*2-j-1;i++)
 {
  if(pcurtx->pnext==NULL)return 1;
  GoDown();
 }
for(i=0;i<screen_max_y-j-1;i++)
 GoUp();
return 1;
}

int GoLeft(void)
{
 if(cur_x>0)cur_x--;
 else
  if(cur_x==0&&offset_x)
   {
     offset_x--;
     return 1;
   }
return 0;
}

int GoRight(void)
{
 if(cur_x<screen_max_x-1)cur_x++;
 else
  if(offset_x<1000)
  {
    offset_x++;
    return 1;
  }
return 0;
}

int GoBottom(void)
{
TEXTLINE *ptx=ptext;
int i;
unsigned long all=0;
offset_y=0;
while(ptx->pnext)
 {
  ptx=ptx->pnext;
  all++;
 }
pcurtx=ptx;
offset_y=all;
cur_y=0;
for(i=0;i<screen_max_y-1;i++)
 { if(ptx->plast==NULL)break;
   ptx=ptx->plast;
   offset_y--;
  }
cur_y=all-offset_y;
pindex=ptx;
return 1;
}

int GoUp(void)
{
   if(cur_y>0)
      {
       cur_y--;
       pcurtx=pcurtx->plast;
      }
   else
     if(offset_y)
      {pcurtx=pcurtx->plast;
       pindex=pindex->plast;
       offset_y--;
       return 1;
      }
return 0;
}


int InsChar(TEXTLINE *ptx,int offset,char c)
{
 char *ptemp1,*ptemp2,ptemp3[2]={0,0};
 int len,i;
 len=slen(ptx->str);
 if(len==1000)
  {
   bell();
   return 0;
  }
 if(len<=offset)
   {
    ptemp1=(char *)farmalloc(offset+3);
    memset(ptemp1,0,offset+3);
    if(!ptemp1)
     {
      MsgBox("\t\x4 Not enough memory!\r\t\x4 Any key return...",
	" \xfe Error",1,30,0x3e34,0,NULL);
      return 0;
     }
    if(ptx->pnext==NULL)
     {
      ptx->pnext=(TEXTLINE *)farmalloc(sizeof(TEXTLINE));
      memset(ptx->pnext,0,sizeof(TEXTLINE));
      ptx->pnext->plast=ptx;
     }
    if(ptx->str)scpy(ptemp1,ptx->str);
    for(i=len;i<offset;i++)
     ptemp1[i]=' ';
     ptemp1[i++]=c;
     ptemp1[i]=0;
    ptemp2=ptx->str;
    ptx->str=ptemp1;
    if(ptemp2)farfree(ptemp2);
   }
 else
 if(isInsert)
 {
  ptemp1=(char *)farmalloc(len+2);
  memset(ptemp1,0,len+2);
  if(ptemp1==NULL)
   NeedMem(1);
  sncpy(ptemp1,ptx->str,offset);
  ptemp3[0]=c;
  scat(ptemp1,ptemp3);
  scat(ptemp1,ptx->str+offset);

  ptemp2=ptx->str;
  ptx->str=ptemp1;
  farfree(ptemp2);
 }
 else
  ptx->str[offset]=c;
 return 0;
}

int DelChar(TEXTLINE *ptx,int offset)
{
char *ptemp1,*ptemp2;
int len1,len2,i;
TEXTLINE *ptx1;
len1=slen(ptx->str);
if(offset<len1)
 {
  scpy(ptx->str+offset,pcurtx->str+offset+1);
  return 0;
 }
else
 {if(ptx->pnext==NULL||ptx->pnext->str==NULL&&ptx->pnext->pnext==NULL)
     return 0;
  ptx1=ptx->pnext;
  len2=slen(ptx1->str);
  if(len2)
   {
    ptemp1=(char *)farmalloc(offset+len2+1);
    if(!ptemp1)
     {MsgBox("\t\x4 Not enough memory!\r\t\x4 Any key return...",
	" \xfe Error",1,30,0x3e34,0,NULL);
      return 0;
     }
    memset(ptemp1,0,offset+len2+1);
    if(ptx->str)scpy(ptemp1,ptx->str);
    for(i=len1;i<offset;i++)
      ptemp1[i]=' ';
    scat(ptemp1,ptx1->str);
    farfree(ptx->str);
    ptx->str=ptemp1;
   }
   ptx->pnext=ptx1->pnext;
   if(ptx1->pnext)
     ptx1->pnext->plast=ptx;
   farfree(ptx1);
 }
return 1;
}

int EnterChar(TEXTLINE *pkc,int offset)
{
int len=slen(pkc->str);
TEXTLINE *ptx;
char *ptemp1,*ptemp2;
ptemp1=NULL;
if(len>offset)
{
  ptemp1=(char *)farmalloc(len-offset+1);
  if(ptemp1==NULL)
    NeedMem(1);
  scpy(ptemp1,pkc->str+offset);
  pkc->str[offset]=0;
}

ptx=(TEXTLINE *)farmalloc(sizeof(TEXTLINE));
if(ptx==NULL)
  NeedMem(1);
ptx->str=ptemp1;
ptx->plast=pcurtx;
ptx->pnext=pcurtx->pnext;
if(ptx->pnext)ptx->pnext->plast=ptx;
pcurtx->pnext=ptx;
GoDown();
GoHome();
return 1;
}





int DealInput(KEYCODE *pkc)
{
 int i,ret=0;
 char *ptemp1,*ptemp2,szTemp[100];
 if(isReadOnly)return 0;
 isModify=1;
 switch(pkc->code)
 {
  case BACKSPACE:
   if(offset_x+cur_x>slen(pcurtx->str))
    {ret=GoLeft();
     return ret;
    }
   if(offset_x+cur_x)
    {
     ret=DelChar(pcurtx,offset_x+cur_x-1);
     if(cur_x)cur_x--;
     else {
	   offset_x--;
	   return 1;
	  }
    }
   else
    if(pcurtx->plast)
     {
      GoUp();
      GoEnd();
      ret=DelChar(pcurtx,slen(pcurtx->str));
     }
   if(ret)return 1;
   break;
  case DEL:
   ret=DelChar(pcurtx,offset_x+cur_x);
   if(ret)return 1;
   break;
  case ENTER:
   ret=EnterChar(pcurtx,offset_x+cur_x);
   if(ret)return 1;
   break;
  default:
   InsChar(pcurtx,offset_x+cur_x,pkc->c);
   if(cur_x<screen_max_x)cur_x++;
   else {
	 offset_x++;
	 return 1;
	}
 }
if(pcurtx->str)
  {
   for(i=2;i<=screen_max_x+1;i++)
   outxy(i,cur_y+3,wincolor,' ');
   outsn(2,cur_y+3,wincolor,pcurtx->str+offset_x,screen_max_x);
  }
return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -