 #include <alloc.h>
 #include <stdlib.h>
 #include <process.h>
 #include <string.h>
 #include <graphics.h>
 #include <errno.h>

 # include "edit.h"
 # include "..\include\dialog.h"
 # include "..\include\key.h"

 void BOY_Windows(void);

 SETPATH BOYPATH = {
		     "\\BOY20\\OVL",
		     "\\BOY20\\OVL",
		     "\\BOY20\\FONT24",
		     "\\BOY20\\OVL"
		    };

 char EDIT_FILE_NAME[MaxPath];

 int main(int argc,char *argv[])
  {
    strcpy(Serial,"001-0112940001");
    if(argc>1)
	strcpy(EDIT_FILE_NAME,argv[1]);
    BOY_SETUP_ALL();
    BOY_MOUSE_HIDE();

    strcpy(DLGPATH,"\\BOY20\\EDIT");  /* 对话框窗口 DLG 文件所在的目录 */

    setfillstyle(SOLID_FILL,DARKGRAY);
    bar(0,0,639,479);
    BOY_MOUSE_VIEW();
    CREATE_BOY_CHINESE();
    BOY_Windows();
    BOY_CLOSE_ALL();
    return 1;
  }


 void BOY_Windows(void)
 {
    int FLAG = 0;
    int CASE;
    static int  BOY_TOTAL = 11;
    static TEST BOY_TEST;
    static int  BOY_TAB = 10;

    static TYPE BOY_TYPE[11] = {
				 {18,0},{10,0},{10,1},{10,2},{10,3},
				 {10,4},{2,0},{3,0},{3,1},{3,2},
				 {3,3},
				};

    static SCROLL BOY_SCROLL[1] = {
				    {477,86,495,374,15,100,0,10,1,0,1,1234,1,7},
				   };
    static LABEL BOY_LABEL[4] = {
				    {46,378,123,398,1,0,0,0,0,2,7,0,0,1,"",0,""},
				    {128,378,205,398,1,0,0,0,0,2,7,0,0,1,"",0,""},
				    {210,378,293,398,1,0,0,0,0,2,7,0,97,1,"",0,""},
				    {299,378,494,398,1,0,0,0,0,2,7,0,0,1,"",0,""},
				   };
    static BAR3D BOY_BAR3D[5] = {
				   {43,85,474,375,0,0,7},
				   {43,377,125,399,1,0,2},
				   {127,377,207,399,1,0,2},
				   {209,377,296,399,1,0,2},
				   {298,377,495,399,1,0,2},
				  };
    static MENU BOY_MENU[21] = {
				  {"F. 文件",ALT_F,1,0,0,111,40,53,1},
					  {"打开 ...        ",0,1,1,0,114,40,75,0},
					  {"保存            ",0,1,1,0,115,40,95,0},
					  {"保存为 ...      ",0,1,1,0,116,40,115,0},
					  {"                ",0,-1,1,0,0,40,135,0},
					  {"DOSSHELL        ",0,1,1,0,119,40,155,0},
					  {"退出            ",0,1,1,0,WindowsClose,40,175,0},
				  {"E. 编辑",ALT_E,1,0,0,111,112,53,1},
					  {"拷贝            ",0,1,1,0,121,112,75,0},
					  {"剪裁            ",0,1,1,0,122,112,95,0},
					  {"粘贴            ",0,1,1,0,123,112,115,0},
					  {"                ",0,-1,1,0,0,112,135,0},
					  {"跳转至 ...      ",0,1,1,0,125,112,155,0},
				  {"R. 查找",ALT_R,1,0,0,111,184,53,1},
					  {"查找 ...        ",0,1,1,0,5555,184,75,0},
					  {"继续查找        ",0,1,1,0,6666,184,85,0},
					  {"替换 ...        ",0,1,1,0,7777,184,95,0},
				  {"H. 帮助",ALT_H,1,0,0,111,256,53,1},
					  {"命令            ",0,1,1,0,111,256,75,0},
					  {"                ",0,-1,1,0,0,256,95,0},
					  {"关于            ",0,1,1,0,111,256,115,0},
				};
    static WINDOWS BOY_WINDOWS = { 31,27,506,408,"编辑器窗口",7,1,0,0,1,1,15,0,0,20,15,1,15,0,7,0,0};
    static EDIT BOY_EDIT;

    int oldv = 0,oldmx = 0,oldins = 0,oldl = 0,oldc = 0;

    BOY_EDIT.Enable=Default;
    BOY_EDIT.Active=Default;

    BOY_EDIT.GLeft = BOY_BAR3D[0].x1+2;
    BOY_EDIT.GTop  = BOY_BAR3D[0].y1+2;
    BOY_EDIT.GRight  = BOY_BAR3D[0].x2-2;
    BOY_EDIT.GBottom = BOY_BAR3D[0].y2-2;

    BOY_EDIT.TextColor = BLACK;
    BOY_EDIT.BackColor = WHITE;
    BOY_EDIT.BlockTextColor = YELLOW;
    BOY_EDIT.BlockBackColor = BLUE;
    BOY_EDIT.TextFont = Default;
    BOY_EDIT.InsertStatus = Default;
    if(EDIT_FILE_NAME[0]=='\0')
	strcpy(BOY_EDIT.EditFileName,DefaultFileName);
    else
	strcpy(BOY_EDIT.EditFileName,EDIT_FILE_NAME);


    BOY_WINDOWS.scl = BOY_SCROLL;
    BOY_WINDOWS.lab = BOY_LABEL;
    BOY_WINDOWS.b3d = BOY_BAR3D;
    BOY_WINDOWS.menu= BOY_MENU;

    CREATE_WINDOWS_ALL(&BOY_WINDOWS,&BOY_TYPE[0],BOY_TOTAL,BOY_TAB);
    if(CREATE_BOY_EDIT(&BOY_EDIT)!=1)
    {
	CLOSE_BOY_WINDOWS(&BOY_WINDOWS);
	return;
    }

    BOY_SCROLL[0].mi = 0;
    BOY_SCROLL[0].mx = BOY_EDIT.LastLineInFile;
    BOY_SCROLL[0].v  = BOY_EDIT.CurLineInFile;
    CREATE_BOY_SCROLL(&BOY_SCROLL[0]);

    // 对编辑区域下面的状态行进行初始化工作

    oldl = BOY_EDIT.CurLineInFile; // 光标所在行
    oldc = BOY_EDIT.CurColInFile;  // 光标所在的列
    itoa(BOY_EDIT.CurLineInFile+1,BOY_LABEL[0].text,10);
    itoa(BOY_EDIT.CurColInFile+1,BOY_LABEL[1].text,10);

    if(BOY_EDIT.InsertStatus == 1) // 编辑状态 (1:插入 0:覆盖)
     { // 如果为插入
      strcpy(BOY_LABEL[2].text,"插入");
     }
    else
    { // 如果为覆盖
      strcpy(BOY_LABEL[2].text,"覆盖");
    }
    //重新建立标签
    oldins = BOY_EDIT.InsertStatus; // 保存编辑状态
    strcpy(BOY_LABEL[3].text,BOY_EDIT.EditFileName); // 编辑文件名称
    CREATE_BOY_LABEL(&BOY_LABEL[0]);
    CREATE_BOY_LABEL(&BOY_LABEL[1]);
    CREATE_BOY_LABEL(&BOY_LABEL[2]);
    CREATE_BOY_LABEL(&BOY_LABEL[3]);

    while(!FLAG)
     {
       BOY_GET_TEST(&BOY_TEST);

       BOY_EDIT_TEST(&BOY_EDIT,&BOY_TEST);
       //{// 编辑区域的状态改变
       /*
	if(oldv != BOY_EDIT.CurLineInFile || oldmx != BOY_EDIT.LastLineInFile ||
	   oldc != BOY_EDIT.CurColInFile || oldl != BOY_EDIT.CurLineInFile)
	{*/ // 当前的光标位置是否同上次一致
	  // 改变滚动条的结构并重新建立之
	  if(oldv != BOY_EDIT.CurLineInFile || oldmx != BOY_EDIT.LastLineInFile)
	   {
		 BOY_SCROLL[0].mi = 0;
		 BOY_SCROLL[0].mx = BOY_EDIT.LastLineInFile;
		 BOY_SCROLL[0].v  = BOY_EDIT.CurLineInFile;
		 CREATE_BOY_SCROLL(&BOY_SCROLL[0]);
		 oldv = BOY_EDIT.CurLineInFile;
		 oldmx = BOY_EDIT.LastLineInFile;
	    }

	  if(BOY_EDIT.CurLineInFile != oldl)
	  {// 当前的行位置改动并重新建立行位置标签
	   oldl = BOY_EDIT.CurLineInFile;
	   itoa(BOY_EDIT.CurLineInFile+1,BOY_LABEL[0].text,10);
	   CREATE_BOY_LABEL(&BOY_LABEL[0]);
	  }

	  if(BOY_EDIT.CurColInFile != oldc)
	  {// 当前的列位置改动并重新建立列位置标签
	   oldc = BOY_EDIT.CurColInFile;
	   itoa(BOY_EDIT.CurColInFile+1,BOY_LABEL[1].text,10);
	   CREATE_BOY_LABEL(&BOY_LABEL[1]);
	  }
	 //}
	 if(BOY_EDIT.InsertStatus != oldins)
	 {// 判断当前的编辑状态是否改变,
	    if(BOY_EDIT.InsertStatus == 1)
	    { // 如果为插入
	      strcpy(BOY_LABEL[2].text,"插入");
	     }
	    else
	    { // 如果为覆盖
	      strcpy(BOY_LABEL[2].text,"覆盖");
	     }
	   //重新建立标签
	   oldins = BOY_EDIT.InsertStatus;
	   CREATE_BOY_LABEL(&BOY_LABEL[2]);
	  }

	  if(strcmp(BOY_EDIT.EditFileName,BOY_LABEL[3].text)!=0)
	  { // 所编辑的文件名称是否发生变化
	    strcpy(BOY_LABEL[3].text,BOY_EDIT.EditFileName);
	    CREATE_BOY_LABEL(&BOY_LABEL[3]);
	   }
       //}

       CASE=BOY_ALL_TEST(&BOY_WINDOWS,&BOY_TEST,BOY_TOTAL,&BOY_TAB,BOY_TYPE);
       switch(CASE)
       {
	 case 114:{
		    FUNC_LoadFile(&BOY_EDIT);
		   }break;
	 case 115:{
		    FUNC_SaveFile(&BOY_EDIT);
		   }break;
	 case 116:{
		    FUNC_SaveAs(&BOY_EDIT);
		   }break;
	 case 121:{
		    FUNC_CopyToClipBoard(&BOY_EDIT);
		   }break;
	 case 122:{
		    FUNC_CopyToClipBoard(&BOY_EDIT);
		    FUNC_DeleteBlock(&BOY_EDIT);
		   }break;
	 case 123:{
		    FUNC_CopyFromClipBoard(&BOY_EDIT);
		   }break;
	 case 125:{
		    FUNC_GoToLineNumber(&BOY_EDIT);
		   }break;
	 case 5555:{
		    FUNC_Find(&BOY_EDIT);
		   }break;
	 case 6666:{
		    FUNC_Research(&BOY_EDIT);
		    }break;
	 case 7777:{
		    FUNC_Replace(&BOY_EDIT);
		    }break;
	 case 119:{
		    FUNC_DosShell(&BOY_WINDOWS,BOY_TYPE,BOY_TOTAL,BOY_TAB,&BOY_EDIT);
		   }break;
	 case 97:  {
			FUNC_InsertStatus(&BOY_EDIT);
		    }break;
	 case 1234:{
		     REDRAW_EDIT(BOY_SCROLL[0].v,&BOY_EDIT);
		    }break;
	 case WindowsChange:{
			      BOY_EDIT.GLeft = BOY_BAR3D[0].x1+2;
			      BOY_EDIT.GTop  = BOY_BAR3D[0].y1+2;
			      BOY_EDIT.GRight  = BOY_BAR3D[0].x2-2;
			      BOY_EDIT.GBottom = BOY_BAR3D[0].y2-2;
			      REDRAW_EDIT(BOY_EDIT.CurLineInFile,&BOY_EDIT);
			     }break;
	 case WindowsClose:FLAG=1;break;
	}
      }
     BOY_EDIT_CLOSE_ALL(&BOY_EDIT);
     CLOSE_BOY_WINDOWS(&BOY_WINDOWS);
  }
