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

#include <vcl.h>
#include <stdio.h>
#pragma hdrstop

#include "GForm.h"
#include "main.h"
#include "OFDform.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TGenForm *GenForm;

bool ask;
bool ShittyKeil=true;

//---------------------------------------------------------------------------
__fastcall TGenForm::TGenForm(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TGenForm::BtnOkClick(TObject *Sender)
{
	ask=CbAsk->Checked;

	SFDpath=genpath;
	if(SFD->ShowModal()==mrOk)
	{
		genpath=SFDpath;

		if(CbGenFont->Checked==false || GenerateFonts()==0)
		{
			if(CbGenScr->Checked==false || GenerateScreens()==0)
			{
				if(CbGenLCD->Checked==false || GenerateLCD()==0)
				{
					if(CbGenTxtGrph->Checked==false || GenerateTxtGrph()==0)
					{
						if(CbGenGUI->Checked==false || GenerateGUI()==0)
						{
							if(CbGenMODBUS->Checked==false || GenerateMODBUS()==0)
							{
								if(CbGenMain->Checked==false || GenerateMODBUS()==0)
								{

								}
							}
						}
					}
				}
			}
		}
	}
}
//---------------------------------------------------------------------------

int CopyFileEh(AnsiString fname, AnsiString msghead)
{
	if(FileExists(apppath+"\\sources\\"+fname)==true)
	{
		if(ask==false || FileExists(genpath+"\\"+fname)==false || (FileExists(genpath+"\\"+fname)==true && MessageBox(GenForm->Handle,("Rewrite current file \""+fname+"\"").c_str(),("Generating "+msghead+" ...").c_str(),MB_ICONQUESTION|MB_YESNO)==mrYes))
		{
			// input file found
			if(CopyFile((apppath+"\\sources\\"+fname).c_str(),(genpath+"\\"+fname).c_str(),false)==0)
			{
				// no input files
				if(MessageBox(GenForm->Handle,("Can't generate \""+fname+"\" file!\nContinue?").c_str(),("Generating "+msghead+" ...").c_str(),MB_ICONQUESTION|MB_YESNO)==mrNo)
					return(1);
			}
		}
		else
			return(2);
	}
	else
	{
		// missing input file
		if(MessageBox(GenForm->Handle,("Missing input file \""+fname+"\"!\nContinue?").c_str(),("Generating "+msghead+" ...").c_str(),MB_ICONQUESTION|MB_YESNO)==mrNo)
			return(3);
	}
	return(0);
}


int GenerateMain(void)
{
	int ret;

	if((ret=CopyFileEh("main.c","main"))!=0) return(ret);
	if((ret=CopyFileEh("main_routines.a51","main"))!=0) return(ret);
	if((ret=CopyFileEh("main.h","main"))!=0) return(ret);

	return(0);
}

int GenerateMODBUS(void)
{
	int ret;

	if((ret=CopyFileEh("MODBUS.c","MODBUS functions"))!=0) return(ret);
	if((ret=CopyFileEh("MODBUS_routines.a51","MODBUS functions"))!=0) return(ret);
	if((ret=CopyFileEh("MODBUS.h","MODBUS functions"))!=0) return(ret);

	return(0);
}

int GenerateGUI(void)
{
	int ret;

	if((ret=CopyFileEh("buttons.c","GUI functions"))!=0) return(ret);
	if((ret=CopyFileEh("buttons.h","GUI functions"))!=0) return(ret);

	return(0);
}

int GenerateTxtGrph(void)
{
	int ret;

	if((ret=CopyFileEh("text_graphics_functions.c","text/graphics functions"))!=0) return(ret);
	if((ret=CopyFileEh("text_graphics_routines.a51","text/graphics functions"))!=0) return(ret);
	if((ret=CopyFileEh("text_graphics.h","text/graphics functions"))!=0) return(ret);

	return(0);
}

int GenerateLCD(void)
{
	int ret;

	if((ret=CopyFileEh("LCD_functions.c","LCD functions"))!=0) return(ret);
	if((ret=CopyFileEh("LCD_routines.a51","LCD functions"))!=0) return(ret);
	if((ret=CopyFileEh("LCD_functions.h","LCD functions"))!=0) return(ret);

	return(0);
}

int GenerateScreens(void)
{
	FILE *fw;
	int i,j,k,m,n;
	int icon;
	int events;
	int icb;
	void *obj;
	char *str;

	if(Project->screens>0)
	{
		// C source file
		if(ask==false || FileExists(genpath+"\\screens.c")==false || (FileExists(genpath+"\\screens.c")==true && MessageBox(GenForm->Handle,"Rewrite current file \"screens.c\"","Generating screens ...",MB_ICONQUESTION|MB_YESNO)==mrYes))
		{
			fw=fopen((genpath+"\\screens.c").c_str(),"wt");
			if(fw!=NULL)
			{
				fprintf(fw,"#include <AT89C51CC03.h>\n");
				fprintf(fw,"#include <string.h>\n");
				fprintf(fw,"#include <stdio.h>\n\n");

				fprintf(fw,"#include \"screens.h\"\n");
				fprintf(fw,"#include \"main.h\"\n");
				fprintf(fw,"#include \"LCD_functions.h\"\n");
				fprintf(fw,"#include \"text_graphics.h\"\n");
				fprintf(fw,"#include \"buttons.h\"\n\n");

				icon=0;
				for(i=0;i<Project->screens;i++)
					for(j=0;j<Project->scr[i].objects;j++)
						if(Project->scr[i].obj[j].type==obIcon) icon++;

				if(icon>0)
				{
					fprintf(fw,"\n");
					fprintf(fw,"// ==========================\n");
					fprintf(fw,"// ====== icon bitmaps ======\n");
					fprintf(fw,"// ==========================\n");

					icon=0;
					for(i=0;i<Project->screens;i++)
					{
						for(j=0;j<Project->scr[i].objects;j++)
						{
							obj=Project->scr[i].obj[j].objptr;
							if(Project->scr[i].obj[j].type==obIcon)
							{
								fprintf(fw,"code unsigned char icon_%d[%d]={\n\t",icon++,((((TObjIcon*)obj)->xr/8) + ((((TObjIcon*)obj)->xr%8!=0)?1:0))*((TObjIcon*)obj)->yr);

								m=0;
								icb=0;
								n=0;
								for(k=0;k<((TObjIcon*)obj)->xr*((TObjIcon*)obj)->yr;k++)
								{
									if(((TObjIcon*)obj)->destimg[k]==0) icb|=1<<m;
									if((++m)==8 || (k+1)==((TObjIcon*)obj)->xr*((TObjIcon*)obj)->yr || (k%((TObjIcon*)obj)->xr)==((TObjIcon*)obj)->xr-1)
									{
										fprintf(fw,"0x%02X",icb);
										m=0;
										icb=0;
										if((k+1)!=((TObjIcon*)obj)->xr*((TObjIcon*)obj)->yr)
										{
											fputc(',',fw);
											n++;
											if(n==16)
											{
												fprintf(fw,"\n\t");
												n=0;
											}
										}
										else
											fprintf(fw,"};\n\n");
									}
								}
							}
						}
					}
				}
				fprintf(fw,"\n\n");

				icon=0;
				for(i=0;i<Project->screens;i++)
				{
					fprintf(fw,"//=====================================================================================================\n");
					fprintf(fw,"// Screen name: %s\n",Project->scr[i].name);
					fprintf(fw,"// Objects:     %d\n",Project->scr[i].objects);
					fprintf(fw,"//=====================================================================================================\n");
					fprintf(fw,"void %s(char events)\n{\n",Project->scr[i].name);

					fprintf(fw,"\tthis_scr=%s;\n\n",Project->scr[i].name);

					// check for events
					events=0;
					for(j=0;j<Project->scr[i].objects;j++)
					{
						obj=Project->scr[i].obj[j].objptr;
						switch(Project->scr[i].obj[j].type)
						{
							case obLabel:
								if(((TObjLabel*)obj)->btn>0)
									events++;
								break;
						}
					}

					// create actions
					if(events>0)
					{
						fprintf(fw,"\t// ------ create actions ------\n");
						fprintf(fw,"\tif(events==true)\n\t{\n");
						fprintf(fw,"\t\tClearActions();\n");

						for(j=0;j<Project->scr[i].objects;j++)
						{
							obj=Project->scr[i].obj[j].objptr;
							switch(Project->scr[i].obj[j].type)
							{
								case obLabel:
									if(((TObjLabel*)obj)->btn>0)
									{
										fprintf(fw,"\t\tAddAction(%d,",((TObjLabel*)obj)->btn);
										switch(((TObjLabel*)obj)->btnact)
										{
											case atNone: fprintf(fw,"atNone,NULL,"); break;
											case atNewScr: fprintf(fw,"atNewScr,(void*)%s,",((TObjLabel*)obj)->btnactscr==NULL?"NULL":((TObjLabel*)obj)->btnactscr); break;
											case atPrevScr: fprintf(fw,"atPrevScr,NULL,"); break;
											case atAddChar: fprintf(fw,"atAddChar,NULL,"); break;
											case atAddInt: fprintf(fw,"atAddInt,NULL,"); break;
											case atCall: fprintf(fw,"atCall,NULL,"); break;
											default: fprintf(fw,"atNone,NULL,"); break;
										}
										fprintf(fw,"0,0,0);\n");
									}
									break;
							}
						}
						fprintf(fw,"\t}\n\n");
					}


					fprintf(fw,"\t// ------ screen objects ------\n");
					for(j=0;j<Project->scr[i].objects;j++)
					{
						obj=Project->scr[i].obj[j].objptr;
						switch(Project->scr[i].obj[j].type)
						{
							case obLine:
								fprintf(fw,"\tDrawLine(%d,%d,%d,%d);\n",((TObjLine*)obj)->Ax,((TObjLine*)obj)->Ay,((TObjLine*)obj)->Bx,((TObjLine*)obj)->By);
								break;
							case obFrame:
								fprintf(fw,"\tDrawFrame(%d,%d,%d,%d,",((TObjFrame*)obj)->Ax,((TObjFrame*)obj)->Ay,((TObjFrame*)obj)->Bx,((TObjFrame*)obj)->By);
								switch(((TObjFrame*)obj)->style)
								{
									case fstSingle: fprintf(fw,"fstSingle);\n"); break;
									case fstDouble: fprintf(fw,"fstDouble);\n"); break;
									case fstFilled: fprintf(fw,"fstFilled);\n"); break;
								}
								break;
							case obLabel:
								fprintf(fw,"\tDrawString(%d,%d,%d,%d,\"",((TObjLabel*)obj)->Ax,((TObjLabel*)obj)->Ay,((TObjLabel*)obj)->font,((TObjLabel*)obj)->btnanim==bcaYes?((TObjLabel*)obj)->btn:0);
								str=((TObjLabel*)obj)->str;
								//GenStr2File4shittyKail(str,fw);
								while(*str!='\0')
								{
									switch(*str)
									{
										case 'ý':
											if(ShittyKeil)
												fprintf(fw,"\\375");
											else
												fputc('ý',fw);
											break;
										case '\\':
											fprintf(fw,"\\\\");
											break;
										case '\"':
											fprintf(fw,"\\\"");
											break;
										case '\'':
											fprintf(fw,"\\\'");
											break;
										default:
											fputc(*str,fw);
											break;
									}
									str++;
								}

								fprintf(fw,"\");\n");
								break;
							case obIcon:
								fprintf(fw,"\tDrawIcon(%d,%d,%d,%d,icon_%d);\n",((TObjIcon*)obj)->Ax,((TObjIcon*)obj)->Ay,((TObjIcon*)obj)->xr,((TObjIcon*)obj)->yr,icon++);
								break;
						}
					}
					fprintf(fw,"}\n\n",Project->scr[i].name);
				}

				fclose(fw);
			}
			else
			{
				if(MessageBox(GenForm->Handle,"Can't create fonts file \"screens.c\"!Continue?","Generating screens ...",MB_ICONQUESTION|MB_YESNO)==mrYes)
					return(1);
			}
		}
		else
			return(2);


		// H source file
		if(ask==false || FileExists(genpath+"\\screens.h")==false || (FileExists(genpath+"\\screens.h")==true && MessageBox(GenForm->Handle,"Rewrite current file \"screens.h\"","Generating screens ...",MB_ICONQUESTION|MB_YESNO)==mrYes))
		{
			fw=fopen((genpath+"\\screens.h").c_str(),"wt");
			if(fw!=NULL)
			{
				fprintf(fw,"#ifndef screensH\n");
				fprintf(fw,"#define screensH\n\n");

				fprintf(fw,"// =================================\n");
				fprintf(fw,"// ====== function prototypes ======\n");
				fprintf(fw,"// =================================\n");

				for(i=0;i<Project->screens;i++)
				{
					fprintf(fw,"extern void %s(char events);\n",Project->scr[i].name);
				}

				fprintf(fw,"\n#endif\n");

				fclose(fw);
			}
			else
			{
				if(MessageBox(GenForm->Handle,"Can't create fonts file \"screens.c\"!Continue?","Generating screens ...",MB_ICONQUESTION|MB_YESNO)==mrYes)
					return(1);
			}
		}
		else
			return(2);
	}
	return(0);
}

int GenerateFonts(void)
{
	FILE *fw;
	int i,j,k,m;

	if(Fonts->fonts>0)
	{
		if(ask==false || FileExists(genpath+"\\fonts.a51")==false || (FileExists(genpath+"\\fonts.a51")==true && MessageBox(GenForm->Handle,"Rewrite current file \"fonts.a51\"","Generating fonts ...",MB_ICONQUESTION|MB_YESNO)==mrYes))
		{
			fw=fopen((genpath+"\\fonts.a51").c_str(),"wt");
			if(fw!=NULL)
			{
				fprintf(fw,"\tNAME\tfonts\n\n");
				fprintf(fw,"codeseg\tSEGMENT CODE\n");
				fprintf(fw,"\tRSEG\tcodeseg\n\n");
				fprintf(fw,"\tPUBLIC\tFontList\n\n");

				fprintf(fw,";******************************************************************************************************\n");
				fprintf(fw,";******************************************************************************************************\n");
				fprintf(fw,";\tFonts\n");
				fprintf(fw,";******************************************************************************************************\n");
				fprintf(fw,";******************************************************************************************************\n");

				fprintf(fw,";======================================================================================================\n");
				fprintf(fw,";\tFonts list\n");
				fprintf(fw,";======================================================================================================\n");

				fprintf(fw,"FontList:");
				for(i=0;i<Fonts->fonts;i++)
				{
					fprintf(fw,"\tdw\t%s\n",Fonts->font[i].prefix);
					fprintf(fw,"\tdb\t%d\n",Fonts->font[i].yres);
				}

				fprintf(fw,"\n");

				for(i=0;i<Fonts->fonts;i++)
				{
					fprintf(fw,";======================================================================================================\n");
					fprintf(fw,";\tFont name: %s\n",Fonts->font[i].prefix);
					fprintf(fw,";\tHeight:    %d\n",Fonts->font[i].yres);
					fprintf(fw,";======================================================================================================\n");

					fprintf(fw,"%s:",Fonts->font[i].prefix);
					for(j=0;j<256;j++)
					{
						if(Fonts->font[i].data[j].name[0]!='\0')
							fprintf(fw,"\tdw\t%s_%s\n",Fonts->font[i].prefix,Fonts->font[i].data[j].name);
						else
							fprintf(fw,"\tdw\t%s_%s\n",Fonts->font[i].prefix,Fonts->font[i].data[256].name);
					}
					fprintf(fw,"\n");

					for(j=0;j<257;j++)
					{
						if(Fonts->font[i].data[j].name[0]!='\0')
						{
							fprintf(fw,"%s_%s:\tdb\t%d\n",Fonts->font[i].prefix,Fonts->font[i].data[j].name,Fonts->font[i].data[j].xres);
							for(k=0;k<Fonts->font[i].yres;k++)
							{
								fprintf(fw,"\tdb\t");
								for(m=7;m>=0;m--)
								{
									fprintf(fw,"%c",(Fonts->font[i].data[j].img[m][k]==0) ? '0' : '1');
								}
								fprintf(fw,"b\n");
							}
						}
					}
					fprintf(fw,"\n");
				}

				fprintf(fw,"\n\tEND\n");

				fclose(fw);
			}
			else
			{
				if(MessageBox(GenForm->Handle,"Can't create fonts file \"fonts.a51\"!Continue?","Generating fonts ...",MB_ICONQUESTION|MB_YESNO)==mrYes)
					return(1);
			}
		}
		else
			return(2);
	}
	return(0);
}



void __fastcall TGenForm::CbGenScrShittyKeilClick(TObject *Sender)
{
	ShittyKeil=CbGenScrShittyKeil->Checked;
}
//---------------------------------------------------------------------------

