#include "global.h"
#include "coptions.h"
#include "resource.h"
#include <stdlib.h>
#include <commdlg.h>

COptions::COptions()
{
	//zisti a nastav cestu k datovemu suboru
	DWORD size = ::GetCurrentDirectory(0, 0);//aky velky buffer potrebujem?
	appDir = _NEW(char[size+1]);
	::GetCurrentDirectory(size, appDir);

	connection.host = NULL; connection.host_ip = NULL; connection.dbname = NULL; connection.user = NULL; connection.password = NULL;
	editor.fontName = NULL; editor.fontSize = 0;

	html.fontName = NULL; html.fontSize = 0;

	LoadOptions(true);
}

COptions::~COptions()
{
	SaveOptions();

	_DELETE(appDir);

	//pripojenie
	if (connection.host) {_DELETE(connection.host);}
	if (connection.dbname) {_DELETE(connection.dbname);}
	if (connection.host_ip) {_DELETE(connection.host_ip);}
	if (connection.password) {_DELETE(connection.password);}
	if (connection.user) {_DELETE(connection.user);}

	if (editor.fontName) {_DELETE(editor.fontName);}

	if (html.fontName) {_DELETE(html.fontName);}
}

void COptions::LoadOptions(bool error)
{
	win.sepHor = 120;
	win.sepVer = 120;

	char *fileName = CMemory::printstr("%s\\inlog.dat", appDir);
	hFile = ::CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);	_DELETE(fileName);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		//mame vypisat chybu?
		if (error)
			::MessageBox(0, "Súbor INLOG.DAT s inicializačnými dátami nebol nájdený.", "Nastavenia", MB_ICONWARNING|MB_OK);
		SetDefault();
		return;
	}
	else
	{
		//subor s datami existuje, nacitaj z neho polozky

		//spojenie
		ReadData(&connection.host, OPTIONS_STRING);
		ReadData(&connection.host_ip, OPTIONS_STRING);
		ReadData(&connection.dbname, OPTIONS_STRING);
		ReadData(&connection.user, OPTIONS_STRING);
		ReadData(&connection.port, OPTIONS_NUMBER);
		ReadData(&connection.timeout, OPTIONS_NUMBER);
		ReadData(&connection.ssl, OPTIONS_NUMBER);

		//!!!
		/*if (connection.password)
		{
			_DELETE(connection.password)
		}
		connection.password = _NEW(char[9]); strcpy(connection.password,"postgres");
		*/

		//nastavenie editora
		ReadData(&editor.fontName, OPTIONS_STRING);
		ReadData(&editor.fontSize, OPTIONS_NUMBER);
		
		//html
		ReadData(&html.fontName, OPTIONS_STRING);
		ReadData(&html.fontSize, OPTIONS_NUMBER);

		CloseHandle(hFile);
	}
}

void COptions::WriteData(void*data,int type)
{
	//do suboru hFile ulozi data

	if (!hFile)
		throw 1;

	DWORD size, written;

	if (type == OPTIONS_STRING)
	{
		if (data)
		{
			size = strlen((char*)data);
			if (!::WriteFile(hFile, &size, 4, &written, 0)) //zapis dlzku stringu
				throw 1;
			if (written != 4)
				throw 1;

			if (!::WriteFile(hFile, data, size, &written, 0)) //zapis string
				throw 1;
			if (written != size)
				throw 1;
		}
		else
		{
			size = 0;
			if (!::WriteFile(hFile, &size, 4, &written, 0)) //prazdny string, dlzka 0
				throw 1;
			if (written != 4)
				throw 1;
		}
	}
	else
	{
		DWORD i = *(DWORD*)data;
		if (!::WriteFile(hFile, &i, 4, &written, 0))
			throw 1;
		if (written != 4)
			throw 1;
	}
}

void COptions::ReadData(void *data,int type)
{
	//zo suboru hFile ulozi data

	if (!hFile)
		throw 1;

	DWORD size, readed;
	char *tmp;

	if (type == OPTIONS_STRING)
	{
		if (*(char**)data)
			{_DELETE(*(char**)data);}
		if (!::ReadFile(hFile, &size, 4, &readed, 0)) throw 1;
		if (readed != 4) throw 1;
		if (size)
		{
			tmp = _NEW(char[size+1]); ((char*)tmp)[size] = 0;//ukoncenie stringu
			if (!::ReadFile(hFile, tmp, size, &readed, 0)) throw 1;
			if (readed != size) throw 1;
		}
		else
			tmp = NULL;//ziaden string
		*((char**)data) = tmp;
	}
	else
	{
		if (!::ReadFile(hFile, (DWORD*)data, 4, &readed, 0)) throw 1;
		if (readed != 4) throw 1;
	}
}

void COptions::SaveOptions()
{
	try
	{
		char *fileName = CMemory::printstr("%s\\inlog.dat", appDir);
		hFile = ::CreateFile(fileName, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); _DELETE(fileName);
		if (hFile == INVALID_HANDLE_VALUE)
			throw 1;

		//nastavenie pripojenia
		WriteData(connection.host, OPTIONS_STRING);
		WriteData(connection.host_ip, OPTIONS_STRING);
		WriteData(connection.dbname, OPTIONS_STRING);
		WriteData(connection.user, OPTIONS_STRING);
		WriteData(&connection.port, OPTIONS_NUMBER);
		WriteData(&connection.timeout, OPTIONS_NUMBER);
		WriteData(&connection.ssl, OPTIONS_NUMBER);

		//uloz nastavenie editora
		WriteData(editor.fontName, OPTIONS_STRING);
		WriteData(&editor.fontSize, OPTIONS_NUMBER);

		//uloz html
		WriteData(html.fontName, OPTIONS_STRING);
		WriteData(&html.fontSize, OPTIONS_NUMBER);

		::CloseHandle(hFile);
	} catch (int)
	{
		::MessageBox(::GetActiveWindow(), "Nepodarilo sa uložiť konfiguračné nastavenia do dátového súboru INLOG.DAT", "Nastavenie", MB_OK|MB_ICONWARNING);
	}
}

void COptions::SetSettings()
{
	::DialogBoxParam(app.hInstance, MAKEINTRESOURCE(DLG_SETTINGS), app.mainWindow->GetHandle(), COptions::DialogProc,
		(LPARAM)this);
}

void COptions::SetDefault()
{
	if (connection.host) {_DELETE(connection.host);}
	if (connection.dbname) {_DELETE(connection.dbname);}
	if (connection.host_ip) {_DELETE(connection.host_ip);}
	if (connection.password) {_DELETE(connection.password);}
	if (connection.user) {_DELETE(connection.user);}
	if (editor.fontName) {_DELETE(editor.fontName);}
	if (html.fontName) {_DELETE(html.fontName);}

#ifdef _DEBUG
		connection.host = NULL;//_NEW(char[1]); strcpy(connection.host,"");
		connection.dbname = _NEW(char[3]); strcpy(connection.dbname,"db");
		connection.host_ip = _NEW(char[10]); strcpy(connection.host_ip,"127.0.0.1");
		connection.password = NULL;//_NEW(char[9]); strcpy(connection.password,"postgres");
		connection.port = 5432;
		connection.user = _NEW(char[9]); strcpy(connection.user,"postgres");
		connection.timeout = 20;
		connection.ssl = false;
#else
		connection.host = _NEW(char[24]); strcpy(connection.host,"cvika.dcs.fmph.uniba.sk");
		connection.dbname = NULL;
		connection.host_ip = NULL;
		connection.password = NULL;
		connection.port = 5432;
		connection.user = NULL;
		connection.timeout = 5;
		connection.ssl = false;
#endif

	//editor
	editor.fontName = _NEW(char[12]); strcpy(editor.fontName, "Courier New");
	editor.fontSize = 15;
	//html
	html.fontName = _NEW(char[12]); strcpy(html.fontName, "Times New Roman");
	html.fontSize = 1;
}

void COptions::SelectFont(HWND hwnd,DWORD type)
{
	CHOOSEFONT cf; ::ZeroMemory(&cf, sizeof(cf));
	LOGFONT lf;
	cf.lStructSize = sizeof(cf);
	cf.hwndOwner = app.mainWindow->GetHandle();
	lf.lfCharSet = ANSI_CHARSET;
	cf.lpLogFont = &lf;
	cf.iPointSize = 10;
	cf.Flags = CF_FORCEFONTEXIST|CF_SCREENFONTS|CF_SELECTSCRIPT;
	cf.nFontType = 0;
	cf.lpszStyle = NULL;
	if (::ChooseFont(&cf))
	{
		HDC hdc = ::GetDC(NULL);
		DWORD size = -::MulDiv(lf.lfHeight, 72, ::GetDeviceCaps(hdc, LOGPIXELSY));
		::ReleaseDC(NULL, hdc);

		char *str = _NEW(char[20]);

		DWORD len = strlen(lf.lfFaceName)+1;
		if (type == 0)
		{
			_DELETE(app.options->editor.fontName);
			app.options->editor.fontName = _NEW(char[len]);
			::CopyMemory(app.options->editor.fontName, lf.lfFaceName, len);
			app.options->editor.fontSize = size;
			//do okna
			::SetDlgItemText(hwnd, DLG_SETTINGS_EFONT, app.options->editor.fontName);
			itoa(app.options->editor.fontSize, str, 10);::SetDlgItemText(hwnd, DLG_SETTINGS_ESIZE, str);
		}
		else
		{
			_DELETE(app.options->html.fontName);
			app.options->html.fontName = _NEW(char[len]);
			::CopyMemory(app.options->html.fontName, lf.lfFaceName, len);
			app.options->html.fontSize = size;
			//do okna
			::SetDlgItemText(hwnd, DLG_SETTINGS_HFONT, app.options->html.fontName);
			itoa(app.options->html.fontSize, str, 10);::SetDlgItemText(hwnd, DLG_SETTINGS_HSIZE, str);
		}
		_DELETE(str);

	}
}

BOOL CALLBACK COptions::DialogProc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		{
			::SetWindowLong(hwnd, GWL_USERDATA, lp);//pointer this

			char *str = _NEW(char[200]);
			//nacitaj udaje z nastavenia
			::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_DBNAME, app.options->connection.dbname);
			//
			if (app.options->connection.host)
			{
				::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_HOST, app.options->connection.host);
				//::EnableWindow(GetDlgItem(hwnd, DLG_CONNECT_TO_DB_IP), FALSE);//ip disabluj
			}
			//
			if (app.options->connection.host_ip)
			{
				::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_IP, app.options->connection.host_ip);
				//::EnableWindow(GetDlgItem(hwnd, DLG_CONNECT_TO_DB_HOST), FALSE);//adresu disabluj
			}
			//
			::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_PASS, app.options->connection.password);
			//
			itoa(app.options->connection.port, str, 10);::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_PORT, str);
			//
			itoa(app.options->connection.timeout, str, 10);::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_TIMEOUT, str);
			//
			::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_USER, app.options->connection.user);
			//
			::CheckDlgButton(hwnd, DLG_CONNECT_TO_DB_SSL, app.options->connection.ssl ? BST_CHECKED : BST_UNCHECKED);

			//editor
			::SetDlgItemText(hwnd, DLG_SETTINGS_EFONT, app.options->editor.fontName);
			//
			itoa(app.options->editor.fontSize, str, 10);::SetDlgItemText(hwnd, DLG_SETTINGS_ESIZE, str);

			//html
			::SetDlgItemText(hwnd, DLG_SETTINGS_HFONT, app.options->html.fontName);
			//
			itoa(app.options->html.fontSize, str, 10);::SetDlgItemText(hwnd, DLG_SETTINGS_HSIZE, str);

			_DELETE(str);
			return 1;
		}
		break;
	case WM_DESTROY:
		break;
	case WM_NOTIFY:
		switch (((NMHDR FAR *) lp)->code) 
		{
		case NM_CLICK:
			/*
			if (((NMHDR*)lp)->idFrom == DLG_EDITOR_COLOR)
			{
			}
			*/
			break;
		}
		break;
	case WM_COMMAND:
		switch (LOWORD(wp))
		{
		case IDCANCEL:
			{
				//musime znova nacitat nastavenie, lebo struktura moze byt prepisana
				//ale ked neexistuje subor, nechceme chybu ->false
				COptions *_this = (COptions*)::GetWindowLong(hwnd, GWL_USERDATA);
				_this->LoadOptions(false);
				::EndDialog(hwnd, -1);
			}
			break;
		case IDOK:
			{
				COptions *_this = (COptions*)::GetWindowLong(hwnd, GWL_USERDATA);

				//over spravnost nastaveni a uloz ich do struktury
				char *host = ReadText(hwnd, DLG_CONNECT_TO_DB_HOST);
				char *ip = ReadText(hwnd, DLG_CONNECT_TO_DB_IP);
				char *dbname = ReadText(hwnd, DLG_CONNECT_TO_DB_DBNAME);
				char *user = ReadText(hwnd, DLG_CONNECT_TO_DB_USER);
				char *password = ReadText(hwnd, DLG_CONNECT_TO_DB_PASS);

				BOOL portok; DWORD port = ::GetDlgItemInt(hwnd, DLG_CONNECT_TO_DB_PORT, &portok, FALSE);

				BOOL timeoutok; DWORD timeout = ::GetDlgItemInt(hwnd, DLG_CONNECT_TO_DB_TIMEOUT, &timeoutok, FALSE);
				if (!timeout)
					timeout = 20;//default


				if (!strcmp(ip, "0.0.0.0"))
				{
					_DELETE(ip); ip = NULL;
				}

				try
				{
					if ( (!host) && (!ip) )
						throw "Musíte zadať host";
					if ( !portok || !port)
						throw "Musíte zadať port";
					if (!dbname)
						throw "Musíte zadať databázu";
					if (!user)
						throw "Musíte zadať usera";
					if (!password)
						throw "Musíte zadať heslo";
				} catch (char *err)
				{
					::MessageBox(hwnd, err, "Nastavenie", MB_OK|MB_ICONWARNING);
					if (host) {_DELETE(host);}
					if (ip) {_DELETE(ip);}
					if (dbname) {_DELETE(dbname);}
					if (user) {_DELETE(user);}
					if (password) {_DELETE(password);}
					return 0;
				}

				//uvolni staru pamat
				if (app.options->connection.host) {_DELETE(app.options->connection.host);}
				if (app.options->connection.dbname) {_DELETE(app.options->connection.dbname);}
				if (app.options->connection.host_ip) {_DELETE(app.options->connection.host_ip);}
				if (app.options->connection.password) {_DELETE(app.options->connection.password);}
				if (app.options->connection.user) {_DELETE(app.options->connection.user);}
				//zapis nove data
				app.options->connection.host = host;
				app.options->connection.dbname = dbname;
				app.options->connection.host_ip = ip;
				app.options->connection.password = password;
				app.options->connection.user = user;
				app.options->connection.port = port;
				app.options->connection.timeout = timeout;
				app.options->connection.ssl = (::IsDlgButtonChecked(hwnd, DLG_CONNECT_TO_DB_SSL) == BST_CHECKED);

				//ok, skoncime
				_this->SaveOptions();
				//zmen nastavenie
				if (app.mainWindow->GetDatalogWindow())
					app.mainWindow->GetDatalogWindow()->RefreshFont();
				::EndDialog(hwnd, 1);
			}
			break;
		case DLG_SETTINGS_EBUTTON:
			SelectFont(hwnd, 0);
			break;
		case DLG_SETTINGS_HBUTTON:
			SelectFont(hwnd, 1);
			break;
		}
		break;
	}
	return 0;
}

/*
DWORD COptions::DlgProcEditor_ClosingOk(HWND hwnd)
{
	char *fontName;
	DWORD fontSize;
	try
	{
		fontName = ReadText(hwnd, DLG_EDITOR_FONTS);
		if (!fontName)
			throw "Zadajte font!";
		BOOL fontSize_ok;
		fontSize = ::GetDlgItemInt(hwnd, DLG_EDITOR_SIZE, &fontSize_ok, FALSE);
		if (!fontSize_ok || !fontSize)
			throw "Zadajte veľkosť fontu!";

	} catch (char *err)
	{
		::MessageBox(::GetActiveWindow(), err, "Nastavenie", MB_ICONWARNING|MB_OK);
		if (fontName) {_DELETE(fontName);}
		::SetWindowLong(hwnd, DWL_MSGRESULT, TRUE);
		return 1;//nezavri sa
	}

	//ok, uloz data, zatvarame okno
	if (app.options->editor.fontName) {_DELETE(app.options->editor.fontName);}
	app.options->editor.fontName = fontName;
	app.options->editor.fontSize = fontSize;
	//uloz farby
	for (DWORD i=0; i<OPTIONS_COLOR_CATEGORIES; i++)
		app.options->editor.color[i] = ::SendDlgItemMessage(hwnd, DLG_EDITOR_CATG, CB_GETITEMDATA, i, 0);
	::SetWindowLong(hwnd, DWL_MSGRESULT, FALSE);//ok
	return 0;//ok
}

void COptions::DlgProcEditor_WMDRAWITEM(HWND dlg, DRAWITEMSTRUCT* dis)
{
	if (dis->itemAction == ODA_DRAWENTIRE)
	{
		//podla toho, aka kategoria je vybrata, nakresli farbu
		DWORD color = app.options->editor.color[::SendDlgItemMessage(dlg, DLG_EDITOR_CATG, CB_GETCURSEL, 0, 0)];
		HBRUSH hBrush = ::CreateSolidBrush(color);
		FillRect(dis->hDC, &dis->rcItem, hBrush);
		::DeleteObject(hBrush);
	}
}

BOOL CALLBACK COptions::DlgProcEditor(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		DlgProcEditor_WMINITDIALOG(hwnd);
		return 1;
	case WM_DESTROY:
		//DlgProcEditor_WMDESTROY(hwnd);//treba uvolnit pamat
		break;
	case WM_NOTIFY:
		switch (((NMHDR FAR *) lp)->code) 
		{
		case PSN_KILLACTIVE:
			return DlgProcEditor_ClosingOk(hwnd);
		case NM_CLICK:
			if (((NMHDR*)lp)->idFrom == DLG_EDITOR_COLOR)
			{
				CHOOSECOLOR cc;
				cc.lStructSize = sizeof(CHOOSECOLOR);
				cc.hwndOwner = hwnd;
				cc.lpCustColors = _NEW(DWORD[16]);
				cc.Flags = CC_ANYCOLOR|CC_FULLOPEN;
				cc.lCustData = 0;
				if (::ChooseColor(&cc))
				{
				}
				_DELETE(cc.lpCustColors);
			}
		}
		break;
	case WM_DRAWITEM:
		if ( ((DRAWITEMSTRUCT*)lp)->CtlID == DLG_EDITOR_COLOR)
			DlgProcEditor_WMDRAWITEM(hwnd, (DRAWITEMSTRUCT*)lp);
		break;
	case WM_COMMAND:
		if ((HIWORD(wp) == CBN_SELCHANGE) && (LOWORD(wp) == DLG_EDITOR_CATG))
		{
			::SendDlgItemMessage(hwnd, DLG_EDITOR_CATG, CB_GETCURSEL, 0, 0);
			::RedrawWindow(::GetDlgItem(hwnd, DLG_EDITOR_COLOR), NULL, NULL, RDW_ERASE|RDW_INVALIDATE|RDW_ERASENOW);
		}
		break;
	}
	return 0;
}

void COptions::DlgProcEditor_WMINITDIALOG(HWND hwnd)
{
	//vloz do combo boxu kategorie fontov - kazdej kategorii nastav ako data prislusnu farbu
	for (DWORD i=0; i<OPTIONS_COLOR_CATEGORIES; i++)
	{
		DWORD item = ::SendDlgItemMessage(hwnd, DLG_EDITOR_CATG, CB_ADDSTRING, 0, (LPARAM)colorNames[i]);
		::SendDlgItemMessage(hwnd, DLG_EDITOR_CATG, CB_SETITEMDATA, item, (LPARAM)app.options->editor.color[i]);
	}
	//nastav prvu polozku
	::SendDlgItemMessage(hwnd, DLG_EDITOR_CATG, CB_SETCURSEL, 0, 0);
	::RedrawWindow(::GetDlgItem(hwnd, DLG_EDITOR_COLOR), NULL, NULL, RDW_ERASE|RDW_INVALIDATE|RDW_ERASENOW);


	//do list boxu daj vsetky fonty
	LOGFONT lf;
	lf.lfCharSet = ANSI_CHARSET;
	::ZeroMemory(lf.lfFaceName, sizeof(lf.lfFaceName));//vsetky fonty (prazdny string)
	lf.lfPitchAndFamily = FIXED_PITCH;
	HDC dc = ::GetDC(hwnd);
	::EnumFontFamiliesEx(dc, &lf, (FONTENUMPROC)DlgProcEditor_EnumProc, (DWORD)hwnd, 0);
	::SendDlgItemMessage(hwnd, DLG_EDITOR_FONTS, CB_SETCURSEL, 0, 0);
	::ReleaseDC(hwnd, dc);

	//vyber do combo boxu font z nastavenia
	DWORD item = ::SendDlgItemMessage(hwnd, DLG_EDITOR_FONTS, CB_FINDSTRING, -1, (LPARAM)app.options->editor.fontName);
	if (item == CB_ERR)
		//nastav prvu polozku
		item = 0;
	::SendDlgItemMessage(hwnd, DLG_EDITOR_FONTS, CB_SETCURSEL, item, 0);

	//vyber velkost farby podla nastavenia
	::SetDlgItemInt(hwnd, DLG_EDITOR_SIZE, app.options->editor.fontSize, FALSE);

	//alokuj docasnu pamat pre nastavenie farieb
}
int CALLBACK COptions::DlgProcEditor_EnumProc(ENUMLOGFONTEX *lpelfe, NEWTEXTMETRICEX *lpntme, DWORD FontType, LPARAM lParam)
{
	if (lpelfe->elfFullName)
	{
		DWORD item = ::SendDlgItemMessage((HWND)lParam, DLG_EDITOR_FONTS, CB_ADDSTRING, 0, (LPARAM)lpelfe->elfFullName);
		return 1;
	}
	else
		return 0;//koniec
}*/
//
char *COptions::ReadText(HWND hwnd, DWORD controlID)
{
	char *tmp = _NEW(char[1024]);//TODO ohranicenie
	if (::GetDlgItemText(hwnd, controlID, tmp, 1024))
	{
		char *ret = _NEW(char[strlen(tmp)+1]);
		strcpy(ret, tmp);
		_DELETE(tmp);
		return ret;
	}
	else
	{
		_DELETE(tmp);
		return NULL;
	}
}
/*
DWORD COptions::DlgProcDB_ClosingOk(HWND hwnd)
{
	//over spravnost nastaveni a uloz ich do struktury

	char *host = ReadText(hwnd, DLG_CONNECT_TO_DB_HOST);
	char *ip = ReadText(hwnd, DLG_CONNECT_TO_DB_IP);
	char *dbname = ReadText(hwnd, DLG_CONNECT_TO_DB_DBNAME);
	char *user = ReadText(hwnd, DLG_CONNECT_TO_DB_USER);
	char *password = ReadText(hwnd, DLG_CONNECT_TO_DB_PASS);

	BOOL portok;
	DWORD port = ::GetDlgItemInt(hwnd, DLG_CONNECT_TO_DB_PORT, &portok, FALSE);

	BOOL timeoutok;
	DWORD timeout = ::GetDlgItemInt(hwnd, DLG_CONNECT_TO_DB_TIMEOUT, &timeoutok, FALSE);

	if (!strcmp(ip, "0.0.0.0"))
	{
		_DELETE(ip); ip = NULL;
	}

	try
	{
		if ( (!host) && (!ip) )
			throw "Musíte zadať host";
		if ( !portok || !port)
			throw "Musíte zadať port";
		if (!dbname)
			throw "Musíte zadať databázu";
		if (!user)
			throw "Musíte zadať usera";
		if (!password)
			throw "Musíte zadať heslo";
	} catch (char *err)
	{
		::MessageBox(hwnd, err, "Nastavenie", MB_OK|MB_ICONWARNING);
		::SetWindowLong(hwnd, DWL_MSGRESULT, TRUE);//treba opravit, nepusti dalej..
		if (host) {_DELETE(host);}
		if (ip) {_DELETE(ip);}
		if (dbname) {_DELETE(dbname);}
		if (user) {_DELETE(user);}
		if (password) {_DELETE(password);}
		return 1;//nezavri sa, zostan aktivne
	}

	::SetWindowLong(hwnd, DWL_MSGRESULT, FALSE);//ok
	//uvolni staru pamat
	if (app.options->connection.host) {_DELETE(app.options->connection.host);}
	if (app.options->connection.dbname) {_DELETE(app.options->connection.dbname);}
	if (app.options->connection.host_ip) {_DELETE(app.options->connection.host_ip);}
	if (app.options->connection.password) {_DELETE(app.options->connection.password);}
	if (app.options->connection.user) {_DELETE(app.options->connection.user);}
	//zapis nove data
	app.options->connection.host = host;
	app.options->connection.dbname = dbname;
	app.options->connection.host_ip = ip;
	app.options->connection.password = password;
	app.options->connection.user = user;
	app.options->connection.ssl = (::IsDlgButtonChecked(hwnd, DLG_CONNECT_TO_DB_SSL) == BST_CHECKED);

	return 0;//zavri sa
}

BOOL CALLBACK COptions::DlgProcDB(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		{
			char *str = _NEW(char[200]);
			::SetWindowLong(hwnd,GWL_USERDATA,(DWORD)lp);//lp = this
			//nacitaj udaje z nastavenia
			::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_DBNAME, app.options->connection.dbname);
			if (app.options->connection.host)
			{
				::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_HOST, app.options->connection.host);
				//::EnableWindow(GetDlgItem(hwnd, DLG_CONNECT_TO_DB_IP), FALSE);//ip disabluj
			}
			if (app.options->connection.host_ip)
			{
				::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_IP, app.options->connection.host_ip);
				//::EnableWindow(GetDlgItem(hwnd, DLG_CONNECT_TO_DB_HOST), FALSE);//adresu disabluj
			}
			::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_PASS, app.options->connection.password);
			itoa(app.options->connection.port, str, 10);::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_PORT, str);
			itoa(app.options->connection.timeout, str, 10);::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_TIMEOUT, str);
			::SetDlgItemText(hwnd, DLG_CONNECT_TO_DB_USER, app.options->connection.user);

			::CheckDlgButton(hwnd, DLG_CONNECT_TO_DB_SSL, app.options->connection.ssl ? BST_CHECKED : BST_UNCHECKED);
			_DELETE(str);
			//nastav cele property sheet okno do stredu hlavneho okna
			RECT rcP,rc, rcPc;
			::GetWindowRect(app.mainWindow->GetHandle(), &rcP);
			::GetClientRect(app.mainWindow->GetHandle(), &rcPc);
			::GetClientRect(hwnd, &rc);
			::SetWindowPos(GetParent(hwnd), 0, rcP.left + (rcPc.right - rc.right)/2, rcP.top + (rcPc.bottom -  rc.bottom)/2,
				0, 0, SWP_NOSIZE|SWP_NOREPOSITION);
			return 1;
		}
	case WM_NOTIFY:
		switch (((NMHDR FAR *) lp)->code) 
		{
		case PSN_KILLACTIVE:
			return DlgProcDB_ClosingOk(hwnd);
		}
		break;
	}
	return 0;
}

void COptions::SetDefault()
{
	//rozdelenie okien v hlavnom okne
	win.sepHor = 200;
	win.sepVer = 200;

	win.mainX = 300; win.mainY = 300;
	win.mainXX = 700; win.mainYY = 400;
	win.viewX = 100; win.viewY = 100;
	win.viewXX = 200; win.viewYY = 500;

	win.viewOne = true;


	//pripojenie
//	#ifdef _DEBUG
		connection.host = NULL;//_NEW(char[1]); strcpy(connection.host,"");
		connection.dbname = _NEW(char[3]); strcpy(connection.dbname,"db");
		connection.host_ip = _NEW(char[10]); strcpy(connection.host_ip,"127.0.0.1");
		connection.password = _NEW(char[9]); strcpy(connection.password,"postgres");
		connection.port = 5432;
		connection.user = _NEW(char[9]); strcpy(connection.user,"postgres");
		connection.timeout = 20;
		connection.ssl = false;
/*	#else
		connection.host = _NEW(char[24]); strcpy(connection.host,"cvika.dcs.fmph.uniba.sk");
		connection.dbname = NULL;
		connection.host_ip = NULL;
		connection.password = NULL;
		connection.port = 5432;
		connection.user = NULL;
		connection.timeout = 5;
		connection.ssl = false;
	#endif*/

/*	//editor
	editor.fontName = _NEW(char[12]); strcpy(editor.fontName, "Courier New");
	editor.fontSize = 18;
	for (DWORD i=0; i<OPTIONS_COLOR_CATEGORIES; i++)
		editor.color[i] = editorDefColor[i];
}

char *COptions::colorNames[OPTIONS_COLOR_CATEGORIES] = {"Text","Kľúčové slovo","atóm EDB","atóm IDB",
												"atóm answer","Číslo","Reťazec","Komentár"};
DWORD COptions::editorDefColor[OPTIONS_COLOR_CATEGORIES] =
	{	RGB(0,0,0),RGB(0,0,255),RGB(0,120,0),RGB(255,120,40),RGB(255,0,0),RGB(200,0,0),RGB(0,120,255),
		RGB(80,80,80)
	};*/