//CODED by Veliant, Leency. GNU GPL licence.

#startaddress 0
#code32 TRUE

byte   os_name[8]   = {'M','E','N','U','E','T','0','1'};
dword  os_version   = 0x00000001;
dword  start_addr   = #main;
dword  final_addr   = #stop+32;
dword  alloc_mem    = 0x00100000;
dword  x86esp_reg   = 0x00100000;   // 0x0007fff0; 
dword  I_Param      = #param;
dword  I_Icon       = 0x0;
char param[256]="";

//Events
#define evMouse		6
#define evButton	3
#define evKey		2
#define evReDraw	1

//Button options
#define BT_DEL		0x80000000
#define BT_HIDE		0x40000000
#define BT_NOFRAME	0x20000000

#define OLD			-1
#define true		1
#define false		0



struct mouse{
 int x,y,lkm,pkm,hor,vert;
 void get();
};

void mouse::get()
{
	EAX = 37;
	EBX = 1;
	$int	0x40
	$mov	ebx, eax
	$shr	eax, 16
	$and	ebx,0x0000FFFF
	x = EAX;
	y = EBX;
	EAX = 37;
	EBX = 2;
	$int	0x40
	$mov	ebx, eax
	$and	eax, 0x00000001
	$shr	ebx, 1
	$and	ebx, 0x00000001
	lkm = EAX;
	pkm = EBX;
	EAX = 37; //скролл
	EBX = 7;
	$int	0x40
	$mov	ebx, eax
	$shr	eax, 16
	$and	ebx,0x0000FFFF
	//hor = EAX;
	vert = EBX;
}

//---------------------------------------------------------------------------
struct proc_info{
	dword	use_cpu;
	word	pos_in_stack,num_slot,rezerv1;
	char	name[11];
	char	rezerv2;
	dword	adress,use_memory,ID,left,top,width,height;
	word	status_slot,rezerv3;
	dword	work_left,work_top,work_width,work_height;
	char	status_window;
	void	GetInfo(dword ECX);
#define SelfInfo -1
};

void proc_info::GetInfo(dword ECX)
{
	EAX = 9;
	EBX = #use_cpu;
	$int  0x40
}
//-------------------------------------------------------------------------------

inline fastcall dword WaitEvent(){
 EAX = 10;              // wait here for event
 $int 0x40
}

inline fastcall void SetEventMask(dword EBX)
{
 EAX = 40;
 $int 0x40
}

inline fastcall word GetKey(){
 EAX = 2;              // just read this key from buffer
 $int  0x40
 EAX = EAX >> 8;
}

inline fastcall word GetButtonID(){
 EAX = 17;            // Get ID
 $int  0x40
 EAX = EAX >> 8;
}

inline fastcall void ExitProcess(){
 EAX = -1;            // close this program
 $int 0x40
}

inline fastcall void Pause(dword EBX){				
	$mov eax, 5       //Пауза, в сотых долях секунды
	$int 0x40
}

//------------------------------------------------------------------------------
void DefineAndDrawWindow(dword x,y,sizeX,sizeY,byte mainAreaType,dword
	mainAreaColor,byte headerType,dword headerColor,EDI)
{
	EBX = x << 16 + sizeX;
	ECX = y << 16 + sizeY;
	EDX = mainAreaType << 24 | mainAreaColor;
	ESI = headerType << 24 | headerColor;
	$xor eax,eax
	$int 0x40
}

inline fastcall void CreateThread(dword ECX,EDX)
{
	EAX = 51;
	EBX = 1;
	$int 0x40
}

inline fastcall void DrawTitle(dword ECX){
	EAX = 71;
	EBX = 1;
	$int 0x40;
}


inline fastcall void WindowRedrawStatus(dword EBX){
	EAX = 12;              //tell os about windowdraw
	$int 0x40
}

inline fastcall dword GetSkinWidth(){
	EAX = 48;
	EBX = 4;
	$int 0x40
}

inline fastcall void MoveSize(dword EBX,ECX,EDX,ESI){
	EAX = 67;
	$int 0x40
}

//------------------------------------------------------------------------------

inline fastcall dword strlen(dword EDI){
	EAX=0;
	ECX=-1;
	$REPNE $SCASB
	EAX-=2+ECX;
}


inline fastcall copystr(dword ESI,EDI)
{
	$cld
l1:
	$lodsb
	$stosb
	$test al,al
	$jnz l1
}


byte fastcall TestBit(EAX, CL)
{
	$shr eax,cl
	$and eax,1
}


char buffer[11]="";
inline fastcall dword IntToStr(dword ESI)
{
     $mov     edi, #buffer
     $mov     ecx, 10
     $test     esi, esi
     $jns     f1
     $mov     al, '-'
     $stosb
     $neg     esi
f1:
     $mov     eax, esi
     $push     -'0'
f2:
     $xor     edx, edx
     $div     ecx
     $push     edx
     $test     eax, eax
     $jnz     f2
f3:
     $pop     eax
     $add     al, '0'
     $stosb
     $jnz     f3
     $mov     eax, #buffer
     $ret
}


dword StrToCol(char* htmlcolor)
{
  dword j=1, color=0; char ch=0x00;
  IF (ESBYTE[htmlcolor]<>'#') RETURN;
  FOR (;j<7;j++)
  {
    ch=ESBYTE[htmlcolor+j];
    IF ((ch>='0') && (ch<='9')) ch -= '0';
    IF ((ch>='A') && (ch<='F')) ch -= 'A'-10;
    IF ((ch>='a') && (ch<='f')) ch -= 'a'-10;
    color = color*0x10 + ch;
  }
  EAX=color;
}


inline fastcall int strcmp(ESI, EDI)
{
	loop()
	{
		IF (DSBYTE[ESI]<DSBYTE[EDI]) RETURN -1;
		IF (DSBYTE[ESI]>DSBYTE[EDI]) RETURN 1;
		IF (DSBYTE[ESI]=='\0') RETURN 0;
		ESI++;
		EDI++;
	}
}


inline fastcall unsigned int find_symbol(ESI,BL)
{
	int jj=0, last=-1;
	do{
		jj++;
		$lodsb
		IF(AL==BL) last=jj;
	} while(AL!=0);
	return last;
}


inline fastcall memmov( EDI, ESI, ECX)
{
  asm {
    MOV EAX, ECX
    CMP EDI, ESI
    JG L1
    JE L2
    SAR ECX, 2
    JS L2
    REP MOVSD
    MOV ECX, EAX
    AND ECX, 3
    REP MOVSB
    JMP SHORT L2
L1: LEA ESI, DSDWORD[ ESI+ECX-4]
    LEA EDI, DSDWORD[ EDI+ECX-4]
    SAR ECX, 2
    JS L2
    STD
    REP MOVSD
    MOV ECX, EAX
    AND ECX, 3
    ADD ESI, 3
    ADD EDI, 3
    REP MOVSB
    CLD
L2:
  }
}


inline fastcall ChangeCase(dword EDX)
{
	AL=DSBYTE[EDX];
	IF(AL>='a')&&(AL<='z')DSBYTE[EDX]=AL&0x5f;
	IF (AL>=160) && (AL<=175) DSBYTE[EDX] = AL - 32;	//р-я
	IF (AL>=224) && (AL<=239) DSBYTE[EDX] = AL - 80;	//р-я
	do{
		EDX++;
		AL=DSBYTE[EDX];
		IF(AL>='A')&&(AL<='Z'){DSBYTE[EDX]=AL|0x20; continue;}
		IF(AL>='А')&&(AL<='П')DSBYTE[EDX]=AL|0x20; //а-п
		IF (AL>=144) && (AL<=159) DSBYTE[EDX] = AL + 80;	//р-я
	}while(AL!=0);
}


//------------------------------------------------------------------------------
inline fastcall void PutPixel(dword EBX,ECX,EDX){
  EAX=1;
  $int 0x40
}

void DefineButton(dword x,y,w,h,EDX,ESI){
	EAX = 8;
	EBX = x<<16+w;
	ECX = y<<16+h;
	$int 0x40
}

inline fastcall void DeleteButton(dword EDX)
{
	EAX = 8;
	EDX += BT_DEL;
	$int 0x40;
}

void WriteText(dword x,y,byte fontType, dword color, EDX, ESI)
{
	EAX = 4;
	EBX = x<<16+y;
	ECX = fontType<<24+color;
	$int 0x40;
}

void DrawBar(dword x,y,w,h,EDX)
{
	#speed
	EAX = 13;
	EBX = x<<16+w;
	ECX = y<<16+h;
 	$int 0x40
	#codesize
}

void DrawRegion_3D(dword x,y,width,height,color1,color2)
{
	DrawBar(x,y,width+1,1,color1); //полоса гор сверху
	DrawBar(x,y+1,1,height-1,color1); //полоса слева
	DrawBar(x+width,y+1,1,height,color2); //полоса справа
	DrawBar(x,y+height,width,1,color2); //полоса гор снизу
}

void DrawFlatButton(dword x,y,width,height,id,color,text)
{
	DrawRegion_3D(x,y,width,height,0x94AECE,0x94AECE);
	DrawRegion_3D(x+1,y+1,width-2,height-2,0xFFFFFF,0xC7C7C7);
	DrawBar(x+2,y+2,width-3,height-3,color); //заливка
	IF (id<>0)	DefineButton(x,y,width,height,id+BT_HIDE,0xEFEBEF); //кнопка
	WriteText(-strlen(text)*6+width/2+x+1,height/2-3+y,0x80,0,text,0);
}

void PutImage(dword EBX,w,h,x,y)
{
	EAX = 7;
	ECX = w<<16+h;
	EDX = x<<16+y;
	$int 0x40
}

void PutPaletteImage(dword EBX,w,h,x,y, EDI)
{
	EAX = 65;
	ECX = w<<16+h;
	EDX = x<<16+y;
	ESI = 8;
	EBP = 0;
	$int 0x40
}  

//------------------------------------------------------------------------------
inline fastcall dword WriteDebug(dword EDX)
{
	$push ebx
	$push ecx
	$mov eax, 63
	$mov ebx, 1
next_char:
	$mov ecx, DSDWORD[edx]
	$or	 cl, cl
	$jz  done
	$int 0x40
	$inc edx
	$jmp next_char
done:
	$mov cl, 13
	$int 0x40
	$mov cl, 10
	$int 0x40
	$pop ecx
	$pop ebx
}
