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

📄 def.h

📁 本人上程序设计课时
💻 H
📖 第 1 页 / 共 3 页
字号:
	{
		MessageBox("File already is being edited.",0);
        return -1;/*文件已打开*/
	}

    if(bNeedSave)
    {
        if(MessageBox("Do you want to save the file?",1))
            SaveFile(0);
    }
    if((flag=OpenTheFile(name))==-1)
        return -1;
    
    if(0==flag)
    {
         MessageBox("No enough memory,so only part of file are read.",0);
         readonly=1;
    }else if(-1==flag)
    {
        MessageBox("Sorry,can't open the file!",0);
        return 0;
    }
    txtGB.x=1;
    txtGB.y=1;

    DrawView();
    setTextWindow();
}

int SaveFile(int SaveAs)/*保存文件*/
{
    FILE *file;
    struct FileLine *tp;
    char savename[MAXPATH+1];
    memset(savename,0,MAXPATH+1);

    if(!strlen(path)||SaveAs)  /*新建文件,未保存||另存为*/
    {
		if(SaveAs)
		{
			strcpy(savename,path);
		}else{
			current_directory(savename);
			if(savename[strlen(savename)]!='\\')
				strcat(savename,"\\");
			strcat(savename,"noname.txt");
		}
		if(0==InputMessageBox(savename,MAXPATH,"Input save as filename"))
			return 0;
		strcpy(path,savename);
	}

    file=fopen(path,"w");
    if(!file)
    {
    MessageBox("Cann't cteate file!",0);
    }else{
		tp=top;
		while(tp)
		{
			fputs(tp->lineNR,file);
			fputs("\n",file);
			tp=tp->next;
		}
    }
    bNeedSave=0;
    fclose(file);
}

int DrawSelectLine(struct FileLine *theline,int col1,int col2,int y)
{
    char lc[79];
    memset(lc,0,79);
    if(col1<iStartCol)
    col1=iStartCol;
    if(col2>iStartCol+78)
    col2=iStartCol+78;
    if(!theline->lineNR)
		return 0;

    memcpy(lc,theline->lineNR+col1,col2-col1+1);
    gotoxy(col1-iStartCol+1,y);
    cputs(lc);
}
/*txtSelect.sCol从1(txtGB.x+iStartCol)开始*/
int DrawSelectText()
{
    int cCol,sLine,x1,x2,y,i,n;
    int first_isSeen=1,last_isSeen=1;
    char str[79];
    struct FileLine *start_temp;

    if(!txtSelect.isSelect)
    return 0;

    DrawView();
    textbackground(WHITE);
    textcolor(BLACK);
    n=abs(txtSelect.sLine-txtGB.y-iStartLine)+1;  /*行数*/

    if(1==n)/*只一行内部分被选*/
    {

       if(txtGB.x+iStartCol-1<txtSelect.sCol)
        DrawSelectLine(current,txtGB.x+iStartCol-1,txtSelect.sCol,txtGB.y);
    else DrawSelectLine(current,txtSelect.sCol,txtGB.x+iStartCol-1,txtGB.y);
    return 0;
    }
    /*有多行被选时*/
    if((txtGB.y+iStartLine)>txtSelect.sLine)  /*↑下选*/
    {
    sLine=txtSelect.sLine-iStartLine;
    start_temp=txtSelect.pline;
    while(sLine<1)
    {
        start_temp=start_temp->next;
        sLine++;                       /*纵坐标加一*/
        first_isSeen=0;
    }

    if(n>txtGB.y) n=txtGB.y;
    }else{
    sLine=txtGB.y;
    start_temp=current;
    if(txtSelect.sLine>iStartLine+21)
    {
        last_isSeen=0;
    }
    if(n>21-txtGB.y+1) n=21-txtGB.y+1;
   }
    for(i=0;i<n;i++)
    {
    if(i==0&&first_isSeen)
    {
        if(txtSelect.sLine<txtGB.y+iStartLine)/*↓下选*/
         x1=txtSelect.sCol;
        else x1=txtGB.x+iStartCol-1;              /*↑上选*/
        x2=(strlen(start_temp->lineNR)>iStartCol+78)?iStartCol+78:(strlen(start_temp->lineNR)-iStartCol);
        DrawSelectLine(start_temp,x1,x2,sLine+i);
    }
    else if(i==n-1&&last_isSeen)
    {
        x1=iStartCol;
        if(txtSelect.sLine<txtGB.y+iStartLine)  /*↓下选*/
        x2=txtGB.x+iStartCol-1;
        else x2=txtSelect.sCol;
        DrawSelectLine(start_temp,x1,x2,sLine+i);
    }else{
        x1=iStartCol;
        x2=iStartCol+78;
        DrawSelectLine(start_temp,x1,x2,sLine+i);
    }
    start_temp=start_temp->next;
    }
    gotoxy(txtGB.x,txtGB.y);
    textbackground(BLUE);
    textcolor(WHITE);
    return 1;
}
/*设置为选择状态*/
int setSelectState(int shiftkey)
{
    int k=0;
    k=shiftkey&0x03;
    if(0==k)
    {
    if(txtSelect.isSelect==1)
    DrawView();

    txtSelect.isSelect=0;
    return 0;
    }
    if(1==txtSelect.isSelect)
    return 0;

    txtSelect.isSelect=1;
    txtSelect.sLine=txtGB.y+iStartLine;
    txtSelect.sCol=txtGB.x+iStartCol-1;
    txtSelect.pline=current;
    return 0;
}

int deleteline(struct FileLine *bd)
{
    bd->pro->next=bd->next;
    bd->next->pro=bd->pro;
    if(bd->num)
    free(bd->lineNR);
    free(bd);
    iTotalLine--;


}
int swap(int *n1,int *n2)
{
    int temp=*n1;
    *n1=*n2;
    *n2=temp;
}
int DeleteSelectText()/*删除所选择的内容*/
{
    struct FileLine *temp,*delend,*delfirst,*bd;
    char *linenr;
    int ny=0,nx,delnum,tempn;
    ny=abs(txtGB.y+iStartLine-txtSelect.sLine)+1;/*行数*/
    delnum=abs(ny)-2;
    if(1==ny)                        /*一行内选择*/
    {
        if(txtGB.x+iStartCol-1==txtSelect.sCol)
        {
          OnDeleteCh();
          txtSelect.isSelect=0;
          return 0;
        }else if((txtGB.x+iStartCol)>txtSelect.sCol)
        {
            strcpy(current->lineNR+txtSelect.sCol,current->lineNR+txtGB.x+iStartCol);
            txtGB.x=txtSelect.sCol+1;
        }
        else
        {
            strcpy(current->lineNR+txtGB.x+iStartCol-1,current->lineNR+txtSelect.sCol);
            txtGB.x=txtGB.x+iStartCol;
        }

        txtSelect.isSelect=0;
        DrawView();
        return 0;
    }
    if(txtGB.y+iStartLine<txtSelect.sLine)    /*↑转为↓第一行删除后半,最后一行删除前半,中间各行删除*/
    {
        tempn=txtGB.y+iStartLine;
        swap(&tempn,&txtSelect.sLine);
        txtGB.y=tempn-iStartLine;

        tempn=txtGB.x+iStartCol-1;
        swap(&tempn,&txtSelect.sCol);
        txtGB.x=tempn-iStartCol+1;

        temp=txtSelect.pline;
        txtSelect.pline=current;
        current=temp;
    }

    delend=current;

    delfirst=temp=txtSelect.pline;

    memset(delfirst->lineNR+txtSelect.sCol,0,strlen(delfirst->lineNR)-txtSelect.sCol+1);
    temp=temp->next;
    while(temp!=delend)/*2~n-1*/
        {
            bd=temp;
            temp=temp->next;
            deleteline(bd);
        }
    if(delfirst->num<(strlen(delfirst->lineNR)+strlen(temp->lineNR+txtGB.x+iStartCol)))/*合并1和n行*/
    {
        linenr=(char*)malloc(strlen(delfirst->lineNR)+strlen(temp->lineNR+txtGB.x+iStartCol)+1);
    strcpy(linenr,delfirst->lineNR);
    free(delfirst->lineNR);
    delfirst->lineNR=linenr;
     }
     strcat(delfirst->lineNR,temp->lineNR+txtGB.x+iStartCol);
     deleteline(temp);


    while(delnum>0&&top->pro)
    {
        top=top->pro;
        iStartLine--;
        delnum--;
     }
    temp=head; /*重新计算 iStartLine*/
    iStartLine=0;
    while(temp!=top)
    {
          iStartLine++;
          temp=temp->next;
    }

    temp=top;/*重新计算 txtGB.y*/
    txtGB.x=txtSelect.sCol-iStartCol+1;
    txtGB.y=1;
    current=txtSelect.pline;
    while(temp!=current)
    {
        txtGB.y++;
        temp=temp->next;
    }

   txtSelect.isSelect=0;
   DrawView();
}
int OnCopy()
{
    struct FileLine *start,*end,*temp;
    int scol,ecol,num;
	if(!txtSelect.isSelect)
	{
		MessageBox("No text be selected.",0);
		return 0;
	}

    if(txtCopy)
    {
        free(txtCopy);
        txtCopy=0;
    }

    if(current==txtSelect.pline)
    {
		if(!current->lineNR)
			return 0;
		if(txtGB.x+iStartCol-1>txtSelect.sCol)
		{
			scol=txtSelect.sCol;
			ecol=txtGB.x+iStartCol-1;
		}else{
			ecol=txtSelect.sCol;
			scol=txtGB.x+iStartCol-1;
			}
		txtCopy=malloc(ecol-scol+2);
		memset(txtCopy,0,ecol-scol+2);
		memcpy(txtCopy,current->lineNR+scol,ecol-scol+1);
		return 0;
    }
    if(txtGB.y+iStartLine>txtSelect.sLine)
    {
		start=txtSelect.pline;
		scol=txtSelect.sCol;
		ecol=txtGB.x+iStartCol-1;
		end=current;
    }else{
		start=current;
		end=txtSelect.pline;
		scol=txtGB.x+iStartCol-1;
		ecol=txtSelect.sCol;
    }
    /*计算所需内存*/
    temp=start;
	if(!temp->lineNR)
		num=1;
	else
	    num=strlen(temp->lineNR+scol)+1;/*符加'\n'1个字节*/

    temp=temp->next;
    while(temp!=end)
    {
	if(!temp->lineNR)
		num+=1;
	else num+=strlen(temp->lineNR)+1;

	temp=temp->next;
    }
    num+=(ecol+1)+1;/*最后一行不含'\n',含'\0'*/
    if(NULL==(txtCopy=(char*)malloc(num)))
    {
        MessageBox("No memory!",0); /*no memory*/
        return 0;
    }

    /*memset(txtCopy,0,num);*/
    if(start->lineNR)
    strcpy(txtCopy,start->lineNR+scol);
    else strcpy(txtCopy,"");

    strcat(txtCopy,"\n");
    start=start->next;
    while(start!=end)
    {
		if(start->lineNR)
			strcat(txtCopy,start->lineNR);
		strcat(txtCopy,"\n");
		start=start->next;
    }
	if(start->lineNR)
	  memcpy(txtCopy+strlen(txtCopy),start->lineNR,ecol+1);

	txtCopy[num-1]='\0';

    return 0;
}
int OnPaste()
{
    int n,num;
    if(!txtCopy)
    return 0;
	txtSelect.isSelect=0;
    num=strlen(txtCopy);
    for(n=0;n<num;n++)
    {
    if(txtCopy[n]=='\n')
       TranslateKey(Key_ENTER,0);
    else
    if(!OnModify(txtCopy[n],0))
        return 0;
    }
    DrawView();
    return 0;
}
/*return NULL未找到,找到返回指针位置*/
char * FindInLine(char *sourse,char *dsd)
{
    char *p=strchr(sourse,dsd[0]);
    int i;
    if(p&&p<sourse+strlen(sourse)-strlen(dsd)+1)
    {
        if(strncmpi(p,dsd,strlen(dsd)))
        {
            FindInLine(p+1,dsd);
        }else return (p);
    }
    return NULL;
}
/*NULL未找到,1找到*/
int FindNext(char *dsd,int flag)
{
    char *p,*gbp;
    int i,line,col;
    struct FileLine *temp;


    /**/
    line=iStartLine+txtGB.y;
    temp=current;
    gbp=current->lineNR+iStartCol+txtGB.x-1;
    while(NULL==(p=FindInLine(gbp,dsd)))
    {
        if(current->next)
        {
            current=current->next;
            gbp=current->lineNR;
            line++;
        }else
        {
            if(flag)
                MessageBox("Can't find the string,",0);

            current=temp;
            return NULL;
        }
    }
    txtSelect.isSelect=1;
    txtSelect.sCol=p-current->lineNR;
    txtSelect.sLine=line;
    txtSelect.pline=current;

    
    for(i=line;i-iStartLine>21;)
    {
        /*向上滚动*/
        iStartLine++;
        top=top->next;
        
    }
    for(i=txtSelect.sCol+strlen(dsd)-iStartCol;i<0;)
    {
        iStartCol--;
    }
    for(i=txtSelect.sCol+strlen(dsd)-iStartCol;i>78;)
    {
         iStartCol++;
    }
    txtGB.y=txtSelect.sLine-iStartLine;
    txtGB.x=txtSelect.sCol-iStartCol+strlen(dsd);

    DrawSelectText();
    return 1;
}
int OnFind()
{
    char search[61];

    /*用户输入查找内容*/
    memset(search,0,61);
    if(!InputMessageBox(search,60,"search"))
    {
        return NULL;
    }
    strcpy(txtSearch,search);
    FindNext(search,1);
}
int GetReplaceTxt(char *fp,char *rp,int num)
{
    /*画窗口*/
    char buf[41*8*2],ch;
    char *s="Find    what:";/*13n*/
    char *r="Replace with:";
    char *ent="* Enter to replace all the rest *";
    char *can="* Esc to canel -----------------*";
    int  *windowflag=0,n1,n2,i;/*windowflag==0为find what*/
    gettext(20,7,60,14,buf);
    window(20,7,60,14);
    textbackground(LIGHTGRAY);
    clrscr();
    textcolor(BLACK);
    gotoxy(2,2);
    cputs(s);
    gotoxy(2,5);
    cputs(r);
    gotoxy(2,7);
    cputs(ent);
    gotoxy(2,8);
    cputs(can);

    box(16,1,25,3);/*find what input window*/
    box(16,4,25,3);/*replace with input window*/
    window(37,11,58,11);
    textbackground(LIGHTGRAY);
    textcolor(BLACK);
    clrscr();
    n2=strlen(rp);
    gotoxy(1,1);
    cputs(rp);

    window(37,8,58,8);
    textbackground(LIGHTGRAY);
    textcolor(BLACK);
    clrscr();
    n1=strlen(fp);
    gotoxy(1,1);
    cputs(fp);

    while((ch=getch())!=13)
    {
        if(27==ch) /*ESC*/
        {puttext(20,7,60,14,buf); return 0;}        
        if(9==ch) /*tab*/
        {
            windowflag=!windowflag;
            if(!windowflag)
              window(37,8,58,8);
            else
          window(37,11,58,11);
            continue;
        }
        
        if(ch!=8&&ch<32)
            continue;

        if(!windowflag)
        {
            if(n1==num-1&&ch!=8)/*8 is backspace*/
            { continue; }

            if(ch==8)
            {
                n1--;
                if(n1<0)
                    n1=0;
                fp[n1]='\0';
            }else{
                fp[n1++]=ch;
            }
            clrscr();
            gotoxy(1,1);
            i=n1<22?0:n1-21;
            cputs(fp+i);
        }else{
            if(n2==num-1&&ch!=8)/*8 is backspace*/
                {continue;}

            if(ch==8)
            {
                n2--;
                if(n2<0)
                    n2=0;
                rp[n2]='\0';
            }else{
                rp[n2++]=ch;
            }
            clrscr();
            gotoxy(1,1);
            i=n2<22?0:n2-21;
            cputs(rp+i);
        }
    }
    puttext(20,7,60,14,buf);
    return 1;
}
int OnReplace()
{
    char fp[61]="",rp[61]="";
    char *oldcopytxt=NULL,meg[30];
    int i=0;

    if(0==GetReplaceTxt(fp,rp,60))
        return 0;
    if(txtCopy)
    {
        oldcopytxt=(char*)malloc(strlen(txtCopy)+1);
		if(!oldcopytxt)
		{
			MessageBox("No memory!",0);
			return 0;
		}
        strcpy(oldcopytxt,txtCopy);
        free(txtCopy);
    }
    txtCopy=(char*)malloc(strlen(rp)+1);
 	if(!txtCopy)
	{
		MessageBox("No memory!",0);
		return 0;
	}
    strcpy(txtCopy,rp);

    while(FindNext(fp,0))
    {
		if(coreleft()<strlen(txtCopy)+1)
		{
			MessageBox("No memory!",0);
			return 0;
		}
		
        TranslateKey(Key_DEL,0);
        TranslateKey(Key_CTRL_V,0);
        i++;
    }
    free(txtCopy);
    txtCopy=NULL;
    if(oldcopytxt)
    {
        txtCopy=(char*)malloc(strlen(oldcopytxt)+1);
		if(txtCopy)
		{
		    strcpy(txtCopy,oldcopytxt);
		}
    }
    if(i!=0)
    {
        sprintf(meg,"Total replaced %d times.",i);
        MessageBox(meg,2);
    }else{
            MessageBox("Can't find this string",0);
    }
    return 1;
}
int OnAbout()
{
    char buf[41*6*2];
    int key;
    gettext(20,8,60,13,buf);
    window(22,9,60,13);
    textbackground(BLACK);
    clrscr();

    window(20,8,58,12);
    textbackground(LIGHTGRAY);
    clrscr();
    textcolor(BLACK);
    gotoxy(4,2);
    cputs("MYEDIT  version 1.0");
    gotoxy(4,3);
    cputs("Copyright@ 2005.11 dingjianzhong");
    gotoxy(4,5);
    textcolor(BLUE);
    cputs("mailto:djzbj@163.com");
    while(1)
    {
    key=bioskey(0);
    if(key==Key_ESC)
    break;
    }
    puttext(20,8,60,13,buf);
    setTextWindow();
    return 0;
}

⌨️ 快捷键说明

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