#include "global.h"
#include <commctrl.h>
#include "catomswin.h"
#include <commctrl.h>

CAtomsWindow::~CAtomsWindow()
{
	DestroyWindow(hwnd);
}

CAtomsWindow::CAtomsWindow(HWND parent)
{
	RECT rc;
	::GetClientRect(parent,&rc);

	hwnd = ::CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, NULL, WS_CHILD|
		TVS_HASBUTTONS|TVS_LINESATROOT|TVS_HASLINES,
		0, 0, 200, rc.bottom,
		parent, (HMENU)NULL, app.hInstance, 0);


/*	::SetWindowLong(hwnd, GWL_USERDATA, (DWORD)this);

	oldWindowProc = (WNDPROC)::GetWindowLong(hwnd, GWL_WNDPROC);
	::SetWindowLong(hwnd, GWL_WNDPROC, (LPARAM)AtomsWindow::StaticWindowProc);
*/	
}

/*LRESULT CALLBACK AtomsWindow::StaticWindowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
	DWORD p = ::GetWindowLong(hwnd,GWL_USERDATA);
	return ((AtomsWindow*)p)->WindowProc(hwnd,msg,wp,lp);
}

LRESULT CALLBACK AtomsWindow::WindowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
	return CallWindowProc(oldWindowProc, hwnd, msg, wp, lp);
}*/

void CAtomsWindow::DoResize()
{
	RECT rc,rcParent;
	::GetWindowRect(hwnd, &rc);
	::GetClientRect(::GetParent(hwnd), &rcParent);
	::SetWindowPos(hwnd, 0, 0, 0, rc.right-rc.left, rcParent.bottom-1, SWP_NOMOVE | SWP_NOZORDER);
}

HWND CAtomsWindow::GetHandle()
{
	return hwnd;
}

void CAtomsWindow::ShowWindow(bool show)
{
	::ShowWindow(hwnd, show ? SW_SHOW : SW_HIDE);
}

void CAtomsWindow::Refresh()
{
	//naplni svoj listbox relacnymi atomami (nazvami tabuliek) s atributami
	app.GetDatabase()->FillTreeViewWithAtoms(hwnd);
}

void CAtomsWindow::WmNotify(LPARAM lp)
{
	/*switch (((NMHDR*)lp)->code)
	{
	case TVN_SELCHANGED:
		SelChanged((NMTREEVIEW*)lp);
		break;
	case NM_DBLCLK:
		DblClick();
		break;
	}*/
}

/*void CAtomsWindow::DblClick()
{
	if (!selectedItem)
		return;

	char* s = _NEW(char[200]);
	TVITEM i;
	i.mask = TVIF_TEXT;
	i.hItem = selectedItem;
	i.pszText = s;
	i.cchTextMax = 200;
	SendMessage(hwnd, TVM_GETITEM, 0, (LPARAM)&i);

	SendMessage(app.mainWindow->GetDatalogWindow()->GetHandle(), EM_REPLACESEL, FALSE, (LPARAM)s);
	_DELETE(s);
}*/

void CAtomsWindow::SelChanged(NMTREEVIEW *tv)
{
	if (tv->itemNew.lParam == 1)
	{
		selectedItem = tv->itemNew.hItem;
		SendMessage(hwnd, TVM_ENSUREVISIBLE, 0, (LPARAM)selectedItem);
	}
	else
		selectedItem = NULL;
}