//Win32Form Project
#pragma option w32
#jumptomain NONE
#pragma option wsi
#stack 16384

#include "windows.h"
#include "FastPad.h--"
#include "FastPad.rc"

RECT		rect;
RECT		rectsp;
//dword 		hMenu;
char 		window_name[]="FASTPAD";
char		setting_name[]="Setting FastPad";
char 		class_name[]="FASTPADclass";
char 		class_name1[]="FASTPADclass1";
char 		class_nameS[]="FASTPADclassS";
char 		menu[]="MainMenu";
char 		optfilename[]="\\FastPad.set";
char 		txtfilename[]="\\FastPad.txt";
WNDCLASSEX 	wcex;
HINSTANCE 	hInstance;
HWND 		MAIN_HWND;
HWND		SPLASH_HWND;
HWND 		SETTING_HWND;
BOOL		YesMouse=FALSE;
DWORD		TimeOpen=20;
DWORD		TimeClose=10;
DWORD		MouseTimeCurrent=0;
BOOL		MainShow=false;
CHAR 		SettingFile[MAX_PATH];
CHAR 		TxtFile[MAX_PATH];
byte 		SaveFilter="All Files (*.*)\000*.*\000";
byte 		SaveTitle="Save text as:";
byte 		SaveFileN[260];
byte 		OpenFilter="All Files (*.*)\000*.*\000";
byte 		OpenFileN[260];
byte 		OpenTitle="Select a file:";
LOGFONTA 	progfont;
BOOL		YesDialog=false;
DWORD		YCaption=0;
BOOL 		AutoRunProg;
char 		sautokey[]="Software\\Microsoft\\Windows\\CurrentVersion\\Run";
char 		sautokeyvalue[]="FastPad";

int main(void)
{
	MSG msg;
	hInstance=GetModuleHandle(0);
	MyRegisterClass();
	InitInstance ();

	WHILE (GetMessage(#msg, NULL, 0, 0))
	{
			TranslateMessage(#msg);
			DispatchMessage(#msg);
	}
	ExitProcess(0);
	return 0;
}

void MyRegisterClass()
{
	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= #WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, ID_ICON);
	wcex.hCursor		= LoadCursor(hInstance, ID_CURSOR);
	wcex.hbrBackground	= COLOR_WINDOW+7;
	wcex.lpszMenuName	= #menu;
	wcex.lpszClassName	= #class_name;
	wcex.hIconSm		= LoadIcon(hInstance, ID_ICON);

	RegisterClassEx(#wcex);

//	wcex.cbSize = sizeof(WNDCLASSEX);

//	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= #WndProcS;
//	wcex.cbClsExtra		= 0;
//	wcex.cbWndExtra		= 0;
//	wcex.hInstance		= hInstance;
//	wcex.hIcon			= LoadIcon(hInstance, ID_ICON);
//	wcex.hCursor		= LoadCursor(hInstance, ID_CURSOR);
//	wcex.hbrBackground	= COLOR_WINDOW+7;
	wcex.lpszMenuName	= 0;
	wcex.lpszClassName	= #class_name1;
//	wcex.hIconSm		= LoadIcon(hInstance, ID_ICON);

	RegisterClassEx(#wcex);

//	wcex.cbSize = sizeof(WNDCLASSEX);

//	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= #WndProcSet;
//	wcex.cbClsExtra		= 0;
//	wcex.cbWndExtra		= 0;
//	wcex.hInstance		= hInstance;
//	wcex.hIcon			= LoadIcon(hInstance, ID_ICON);
//	wcex.hCursor		= LoadCursor(hInstance, ID_CURSOR);
//	wcex.hbrBackground	= COLOR_WINDOW+7;
//	wcex.lpszMenuName	= 0;
	wcex.lpszClassName	= #class_nameS;
//	wcex.hIconSm		= LoadIcon(hInstance, ID_ICON);

	RegisterClassEx(#wcex);

}

BOOL InitInstance ()
{
	HWND hWnd;
	hWnd = CreateWindowExA(WS_EX_TOPMOST|WS_EX_TOOLWINDOW,#class_nameS,#setting_name,WS_SIZEBOX|WS_CAPTION|WS_POPUP,
      0, 0, 200, YCaption+200, NULL, 0, hInstance, NULL);
	SETTING_HWND=hWnd;
	ShowWindow(hWnd,SW_HIDE);
	UpdateWindow(hWnd);
	hWnd = CreateWindowExA(WS_EX_TOPMOST|WS_EX_TOOLWINDOW,#class_name,#window_name,WS_SIZEBOX|WS_CAPTION|WS_POPUP,
      0, 0, 200, 200, NULL, 0, hInstance, NULL);
	MAIN_HWND=hWnd;
	ShowWindow(hWnd,SW_HIDE);
	MainShow=false;
	UpdateWindow(hWnd);
	hWnd = CreateWindowExA(WS_EX_TOOLWINDOW,#class_name1,#window_name,WS_POPUP|WS_VISIBLE,
      0, 10, 2, 100, NULL, 0, hInstance, NULL);
	SPLASH_HWND=hWnd;
	ShowWindow(hWnd,SW_SHOWNORMAL);
	UpdateWindow(hWnd);
	SetSettingFileName();
	LoadSetting();
	ReadTxt();
	SetSetting();
}
void SetSetting()
{
	HFONT hfont;
	hfont = CreateFontIndirect(#progfont);
	SendMessage(HWND_EDIT,WM_SETFONT,hfont,true);
	MoveWindow(MAIN_HWND,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE);
	MoveWindow(SETTING_HWND,rect.left,rect.top,200,200,TRUE);
	YCaption=GetSystemMetrics(SM_CYCAPTION);
}
LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	DWORD buf;
	switch (message)
	{
		CASE WM_DESTROY:
			PostQuitMessage(0);
			break;
		CASE WM_CREATE:
			CreateComponent(hWnd);
			break;
	 ˙CASE WM_MOVE:
			GetWindowRect(hWnd,#rect);
			MoveWindow(hWnd,0,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE);
			MoveWindow(SPLASH_HWND,0,rect.top,2,rect.bottom-rect.top,true);
			break;
		case WM_SIZE:
			GetClientRect(hWnd,#rect);
			MoveWindow(HWND_EDIT,0,0,rect.right,rect.bottom,TRUE);
			GetWindowRect(hWnd,#rect);
			MoveWindow(SPLASH_HWND,0,rect.top,2,rect.bottom-rect.top,true);
			break;
		case WM_COMMAND:
			EAX=wParam;
			switch (AX)
			{
				case IDM_FEXIT:
					SaveSetting();
					SaveTxt();
					DestroyWindow(hWnd);
					break;
				case IDM_FSAVE:
					SaveTxt();
					break;
				case IDM_FSAVEAS:
					SaveTxtAs();
					break;
				case IDM_FOPEN:
					OpenTxt();
					break;
				case IDM_OFONT:
					NewFont();
					break;
				case IDM_OSAVESET:
					SaveSetting();
					break;
				case IDM_HABOUT:
					YesDialog=true;
					MessageBox(0,"FastPad designed by BDEsoft\n Version 1.0 , C-- Language","BDEsoft",0);
					YesDialog=false;
					break;
				case IDM_OSETTING:
					YesDialog=true;
					EnableWindow(MAIN_HWND,false);
					ShowWindow(SETTING_HWND,SW_SHOWNORMAL);
					break;
				case IDM_EUNDO:
					SendMessage(HWND_EDIT,WM_UNDO,0,0);
					break;
				case IDM_ECUT:
					SendMessage(HWND_EDIT,WM_CUT,0,0);
					break;
				case IDM_ECOPY:
					SendMessage(HWND_EDIT,WM_COPY,0,0);
					break;
				case IDM_EPASTE:
					SendMessage(HWND_EDIT,WM_PASTE,0,0);
					break;
				case IDM_ECLEAR:
					buf=SendMessage(HWND_EDIT,WM_GETTEXTLENGTH,0,0);
					SendMessage(HWND_EDIT,EM_SETSEL,0,buf);
					SendMessage(HWND_EDIT,WM_CLEAR,0,0);
					BREAK;
				case IDM_ECOPYALL:
					buf=SendMessage(HWND_EDIT,WM_GETTEXTLENGTH,0,0);
					SendMessage(HWND_EDIT,EM_SETSEL,0,buf);
					SendMessage(HWND_EDIT,WM_COPY,0,0);
					SendMessage(HWND_EDIT,EM_SETSEL,0,0);
					BREAK;
				case IDM_ESELECTALL:
					buf=SendMessage(HWND_EDIT,WM_GETTEXTLENGTH,0,0);
					SendMessage(HWND_EDIT,EM_SETSEL,0,buf);
					BREAK;
			}
		BREAK;
		default:
			RETURN DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

void CreateComponent(HWND hWnd)
{
	HWND_EDIT=CreateWindowEx(0,"edit","",WS_VSCROLL|WS_HSCROLL|ES_AUTOHSCROLL|ES_AUTOVSCROLL|
		ES_MULTILINE|WS_VISIBLE|WS_CHILD,0,0,100,100,hWnd,0,0,0);
}

LRESULT WndProcS(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	SWITCH (message)
	{
		CASE WM_DESTROY:
			PostQuitMessage(0);
			BREAK;
		CASE WM_CREATE:
			SetTimer(hWnd,ID_TIMER,100,#WndTimer);
			BREAK;
		CASE WM_MOVE:
			GetWindowRect(hWnd,#rectsp);
		 ˙BREAK;
		CASE WM_SIZE:
			GetWindowRect(hWnd,#rectsp);
			BREAK;
		default:
			RETURN DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

LRESULT WndProcSet(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	SWITCH (message)
	{
		CASE WM_DESTROY:
			PostQuitMessage(0);
			BREAK;
		CASE WM_CREATE:
			Setting_Load(hWnd);
			BREAK;
		CASE WM_ACTIVATE:
			Setting_Show(hWnd);
			BREAK;
		CASE WM_COMMAND:
			EAX=wParam;
			SWITCH (AX)
			{
				CASE ID_SBUTCLOSE:
					ApplySetting();
					YesDialog=false;
					EnableWindow(MAIN_HWND,true);
					ShowWindow(SETTING_HWND,SW_HIDE);
					SaveSetting();
					break;
			}
			BREAK;
		default:
			RETURN DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

void WndTimer(HWND hwnd,UINT uMsg,DWORD idEvent,DWORD dwTime)
{
	POINT pt;
	IF(YesDialog)return;
	GetCursorPos(#pt);
	IF(!MainShow)
	{
		IF((pt.x>=rectsp.left) && (pt.x<=rectsp.right) && (pt.y>=rectsp.top) && (pt.y<=rectsp.bottom))
			IF(!YesMouse)
			{
				YesMouse=true;
				MouseTimeCurrent=TimeOpen;
			}
		IF((pt.x<rectsp.left) || (pt.x>rectsp.right) || (pt.y<rectsp.top) || (pt.y>rectsp.bottom))
			IF(YesMouse)
				YesMouse=false;
	}
//	ELSE
	IF(MainShow)
	{
		IF((pt.x>=rect.left) && (pt.x<=rect.right) && (pt.y>=rect.top) && (pt.y<=rect.bottom))
			YesMouse=true;
		IF((pt.x<rect.left) || (pt.x>rect.right) || (pt.y<rect.top) || (pt.y>rect.bottom))
			IF(YesMouse)
			{
				YesMouse=false;
				MouseTimeCurrent=TimeClose;
			}
	}
	IF(YesMouse)
		IF(!MainShow)
		{
			IF(MouseTimeCurrent==0)
			{
				ShowWindow(MAIN_HWND,SW_SHOWNORMAL);
				MainShow=true;
				SetFocus(MAIN_HWND);
				SetFocus(HWND_EDIT);
			}
			MouseTimeCurrent--;
		}
//	ELSE
	IF(!YesMouse)
		IF(MainShow)
		{
			IF(MouseTimeCurrent==0)
			{
				SaveTxt();
				ShowWindow(MAIN_HWND,SW_HIDE);
				MainShow=false;
			}
			MouseTimeCurrent--;
		}
}
void SetSettingFileName()
{
//DWORD szbuf,szbuf2;
char buf[MAX_PATH];
//	szbuf=MAX_PATH;
	/*szbuf2=*/GetSystemDirectoryA(#buf,MAX_PATH/*szbuf*/);
	wsprintf(#SettingFile,"%s%s",#buf,#optfilename);
/*	FOR(i=0;i<lstrlen(#optfilename);i++)
		buf[i+szbuf2]=optfilename[i];
	buf[i+szbuf2]=0;
	FOR(i=0;i<szbuf;i++)
		SettingFile[i]=buf[i];*/
	wsprintf(#TxtFile,"%s%s",#buf,#txtfilename);
/*	szbuf2=GetSystemDirectoryA(#buf,szbuf);
	FOR(i=0;i<lstrlen(#txtfilename);i++)
		buf[i+szbuf2]=txtfilename[i];
	buf[i+szbuf2]=0;
	FOR(i=0;i<szbuf;i++)
		TxtFile[i]=buf[i];*/
}
void LoadSetting()
{
	DWORD hfile;
	DWORD kol2;
	hfile=CreateFile(#SettingFile,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
	IF(hfile==INVALID_HANDLE_VALUE)
	{
		rect.left=0;
		rect.right=200;
		rect.top=50;
		rect.bottom=250;
		return;
	}
	ReadFile(hfile,#rect,sizeof(rect),#kol2,0);
	ReadFile(hfile,#progfont,sizeof(progfont),#kol2,0);
	ReadFile(hfile,#TimeOpen,sizeof(TimeOpen),#kol2,0);
	ReadFile(hfile,#TimeClose,sizeof(TimeClose),#kol2,0);
	CloseHandle(hfile);
}
void SaveSetting()
{
	DWORD hfile;
	DWORD kol2;
	hfile=CreateFile(#SettingFile,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
	WriteFile(hfile,#rect,sizeof(rect),#kol2,0);
	WriteFile(hfile,#progfont,sizeof(progfont),#kol2,0);
	WriteFile(hfile,#TimeOpen,sizeof(TimeOpen),#kol2,0);
	WriteFile(hfile,#TimeClose,sizeof(TimeClose),#kol2,0);
	CloseHandle(hfile);
}

void SaveTxt()
{
DWORD hfile;
DWORD kol,kol2;
char buf[4096];
	hfile=CreateFile(#TxtFile,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
	kol=SendMessage(HWND_EDIT,WM_GETTEXT,4096,#buf);
	WriteFile(hfile,#buf,kol+1,#kol2,0);
	CloseHandle(hfile);
}
void ReadTxt()
{
DWORD hfile;
DWORD /*kol,*/kol2;
char buf[4096];
	hfile=CreateFile(#TxtFile,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
	IF(hfile==INVALID_HANDLE_VALUE)
	{
		buf[0]=0;
		/*kol=*/SendMessage(HWND_EDIT,WM_SETTEXT,1,#buf);
		return;
	}
	ReadFile(hfile,#buf,4096,#kol2,0);
	/*kol=*/SendMessage(HWND_EDIT,WM_SETTEXT,kol2-1,#buf);
	CloseHandle(hfile);
}

void SaveTxtAs()
{
OPENFILENAME ofn;
DWORD hfile;
DWORD kol,kol2;
char buf[4096];
	EnableWindow(MAIN_HWND,false);
	YesDialog=true;
	EBX=#ofn;
	ZeroMemory(EBX,sizeof(OPENFILENAME));
	EBX.OPENFILENAME.lStructSize=sizeof OPENFILENAME;
	EBX.OPENFILENAME.hwndOwner=MAIN_HWND;
//	EBX.OPENFILENAME.hInstance=0;
	EBX.OPENFILENAME.lpstrFilter=#SaveFilter;
	EBX.OPENFILENAME.lpstrFile=#SaveFileN;
	EBX.OPENFILENAME.nMaxFile=260;
	EBX.OPENFILENAME.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_LONGNAMES|OFN_EXPLORER|OFN_HIDEREADONLY;
	EBX.OPENFILENAME.lpstrTitle=#SaveTitle;
	IF (GetSaveFileNameA(#ofn)!=TRUE)
	{
		YesDialog=false;
		EnableWindow(MAIN_HWND,true);
		return;
	}
	hfile=CreateFile(#SaveFileN,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
	kol=SendMessage(HWND_EDIT,WM_GETTEXT,4096,#buf);
	WriteFile(hfile,#buf,kol,#kol2,0);
	CloseHandle(hfile);
	YesDialog=false;
	EnableWindow(MAIN_HWND,true);
}

void OpenTxt()
{
OPENFILENAME ofn;
DWORD hfile;
DWORD /*kol,*/kol2;
char buf[4096];
	EnableWindow(MAIN_HWND,false);
	YesDialog=true;
	EBX=#ofn;
	ZeroMemory(EBX,sizeof(OPENFILENAME));
	EBX.OPENFILENAME.lStructSize=sizeof OPENFILENAME;
	EBX.OPENFILENAME.hwndOwner=MAIN_HWND;
//	EBX.OPENFILENAME.hInstance=0;
	EBX.OPENFILENAME.lpstrFilter=#OpenFilter;
	EBX.OPENFILENAME.lpstrFile=#OpenFileN;
	EBX.OPENFILENAME.nMaxFile=260;
	EBX.OPENFILENAME.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_LONGNAMES|OFN_EXPLORER|OFN_HIDEREADONLY;
	EBX.OPENFILENAME.lpstrTitle=#OpenTitle;
	IF (GetOpenFileNameA(#ofn)!=TRUE)
		{
		YesDialog=false;
		EnableWindow(MAIN_HWND,true);
		return;
	}
	hfile=CreateFile(#OpenFileN,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
	IF(hfile==INVALID_HANDLE_VALUE)
		{
		YesDialog=false;
		EnableWindow(MAIN_HWND,true);
		return;
	}
	ReadFile(hfile,#buf,4096,#kol2,0);
	/*kol=*/SendMessage(HWND_EDIT,WM_SETTEXT,kol2,#buf);
	CloseHandle(hfile);
	YesDialog=false;
	EnableWindow(MAIN_HWND,true);
}

void NewFont()
{
	CHOOSEFONT cf;
	HFONT hfont;
	DWORD rgbCurrent;
	YesDialog=true;
	EnableWindow(MAIN_HWND,false);
	ZeroMemory(#cf, sizeof(CHOOSEFONT));
	cf.lStructSize = sizeof CHOOSEFONT;
	cf.hwndOwner = MAIN_HWND;
	cf.lpLogFont = #progfont;
	cf.rgbColors = rgbCurrent;
	cf.Flags = CF_SCREENFONTS | CF_EFFECTS;
	IF (ChooseFont(#cf)!=TRUE)
		{
		YesDialog=false;
		EnableWindow(MAIN_HWND,true);
		return;
	}
	hfont = CreateFontIndirect(#progfont);
	SendMessage(HWND_EDIT,WM_SETFONT,hfont,true);
	YesDialog=false;
	EnableWindow(MAIN_HWND,true);
}
void Setting_Show(HWND hWnd)
{
	SetDlgItemInt(hWnd,ID_SEDIT1,TimeOpen/10,false);
	SetDlgItemInt(hWnd,ID_SEDIT2,TimeClose/10,false);
	SendMessage(HWND_SBUTAUTORUN,BM_SETCHECK,AutorunYES(),0);
}
void Setting_Load(HWND hWnd)
{
	CreateWindowEx(0,"static","Time for show:",WS_VISIBLE|WS_CHILD,20,7,120,120,hWnd,0,0,0);
	CreateWindowEx(0,"static","Time for hide:",WS_VISIBLE|WS_CHILD,20,32,120,20,hWnd,0,0,0);
	CreateWindowEx(0,"edit","1",WS_CHILD | WS_VISIBLE| ES_CENTER |ES_NUMBER | WS_BORDER,130,5,20,20,hWnd,ID_SEDIT1,0,0);
	CreateWindowEx(0,"edit","1",WS_CHILD | WS_VISIBLE| ES_CENTER |ES_NUMBER | WS_BORDER,130,30,20,20,hWnd,ID_SEDIT2,0,0);
	CreateWindowEx(0,"button","CLOSE",WS_VISIBLE|WS_CHILD,130,155,60,20,hWnd,ID_SBUTCLOSE,0,0);
	HWND_SBUTAUTORUN=CreateWindowEx(0,"button","Autorun(AutoRun)",WS_CHILD | WS_VISIBLE |BS_AUTOCHECKBOX,20,55,220,20,hWnd,ID_SBUTAUTORUN,0,0);
}

void ApplySetting()
{
	DWORD buf;
	BOOL b1;
	buf=GetDlgItemInt(SETTING_HWND,ID_SEDIT1,#b1,false);
	TimeOpen=buf*10;
	buf=GetDlgItemInt(SETTING_HWND,ID_SEDIT2,#b1,false);
	TimeClose=buf*10;
	AutorunSET();
}

DWORD AutorunYES()
{
DWORD bufsize=BUFSIZE;
HKEY hKey;
LONG lRet;
char szautorun[BUFSIZE];
	AutoRunProg=0;
	lRet=RegOpenKeyExA(HKEY_CURRENT_USER,#sautokey,0,KEY_READ,#hKey);
	 IF( lRet != ERROR_SUCCESS )
           return 0;
           	lRet = RegQueryValueExA(hKey,#sautokeyvalue, NULL, NULL,#szautorun, #bufsize);
    IF( (lRet != ERROR_SUCCESS) || (bufsize > BUFSIZE) )
		return 0;
	RegCloseKey(hKey);
	AutoRunProg=1;
	return 1;
}

void AutorunSET()
{
//DWORD bufsize=BUFSIZE;
HKEY hKey;
LONG lRet;
BOOL b;
char szautorun[BUFSIZE];
	b=SendMessage(HWND_SBUTAUTORUN,BM_GETCHECK,0,0);
	IF(b==AutoRunProg) return;
	IF(b)
	{
		lRet=RegOpenKeyExA(HKEY_CURRENT_USER,#sautokey,0,KEY_WRITE,#hKey);
	 ˙IF( lRet != ERROR_SUCCESS )
			return;
		GetModuleFileNameA(NULL,#szautorun,MAX_PATH);
		RegSetValueExA(hKey,#sautokeyvalue,0,REG_SZ,#szautorun,MAX_PATH);
		RegCloseKey(hKey);
		return;
	}
	lRet=RegOpenKeyExA(HKEY_CURRENT_USER,#sautokey,0,KEY_WRITE,#hKey);
	IF( lRet != ERROR_SUCCESS )
		return;
	RegDeleteValueA(hKey,#sautokeyvalue);
	RegCloseKey(hKey);
}




