// DataParamView.cpp : implementation file
//

#include "stdafx.h"
#include "pixelwhippro.h"
#include "DataParamView.h"
#include "EffectEditWin.h"
#include "TreeParamView.h"



extern CPixelWhipProApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDataParamView

IMPLEMENT_DYNCREATE(CDataParamView, CView)

CDataParamView::CDataParamView()
{
	VisiblePanelCount=0;
	Initalized=false;
}

CDataParamView::~CDataParamView()
{
}


BEGIN_MESSAGE_MAP(CDataParamView, CView)
//{{AFX_MSG_MAP(CDataParamView)
ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDataParamView drawing

void CDataParamView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CDataParamView diagnostics

#ifdef _DEBUG
void CDataParamView::AssertValid() const
{
	CView::AssertValid();
}

void CDataParamView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDataParamView message handlers

void CDataParamView::Refresh()
{
	
	HideNotVisible();
	SHORT cy=theApp.ParamWin->TreePane->m_PTree.GetItemHeight();
	int ypos=0;
	
	ypos=-theApp.ParamWin->TreePane->m_PTree.GetScrollPos(SB_VERT)*cy;   
	
	CRect crct,tvrct;
	GetClientRect(&crct);
	theApp.ParamWin->TreePane->GetClientRect(&tvrct);
	SetWindowPos(NULL,0,0,crct.Width(),tvrct.Height(),SWP_NOMOVE|SWP_NOZORDER);
	SetActivePanels();
	
	Initalized=true;
	UpdateControls();
	UpdateScrollPos();	
}

void CDataParamView::HideNotVisible()
{
	for(int i=0;i<VisiblePanelCount;i++)
		PanPnt[i]->ShowWindow(SW_HIDE);
	VisiblePanelCount=0;
}

void CDataParamView::UpdateControls()
{
	for(int i=0;i<VisiblePanelCount;i++)
		PanPnt[i]->UpdateControl();
}

void CDataParamView::UpdateScrollPos()
{
	CRect rct;
	GetClientRect(&rct);
	SHORT cy=theApp.ParamWin->TreePane->m_PTree.GetItemHeight();
	int oypos,ypos,lsstart;
	oypos=ypos=-theApp.ParamWin->TreePane->m_PTree.GetScrollPos(SB_VERT)*cy;   
	CWnd *LastShown;
	for(int i=0;i<VisiblePanelCount;i++)
	{
		PanPnt[i]->SetWindowPos(NULL,0,ypos,rct.Width(),cy,SWP_SHOWWINDOW|SWP_NOZORDER);
		if(ypos>0 && ypos<rct.bottom) {LastShown=PanPnt[i]; lsstart=ypos;}
		ypos+=cy;
	}
	
	if( VisiblePanelCount!=0)
		LastShown->SetWindowPos(NULL,0,lsstart,rct.Width(),rct.bottom,SWP_SHOWWINDOW|SWP_NOZORDER);
	
}

void CDataParamView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	if(!Initalized) return;	
	UpdateScrollPos();
}


void CDataParamView::SetActivePanels(HTREEITEM itm)
{
	if(itm==NULL) return;
	
	if(itm==TVI_ROOT)
	{
		HTREEITEM nitm=theApp.ParamWin->TreePane->m_PTree.GetChildItem(itm);
		
		while(nitm!=NULL)
			{
				SetActivePanels(nitm);
				nitm=theApp.ParamWin->TreePane->m_PTree.GetNextSiblingItem(nitm);
			}
			
		return;
	}
	
	CRect crct;
	GetClientRect(&crct);
	SHORT cy=theApp.ParamWin->TreePane->m_PTree.GetItemHeight();
	
	
	CurveSet *cset;
	cset=(CurveSet *)theApp.ParamWin->TreePane->m_PTree.GetItemData(itm);
	if(cset && cset->Panel)
	{
		cset->Panel->SetWindowPos(NULL,0,VisiblePanelCount*cy,crct.Width(),cy,SWP_SHOWWINDOW|SWP_NOZORDER);
		cset->Panel->UpdateControl();
		PanPnt[VisiblePanelCount++]=cset->Panel;	
		
	}
	
	if(theApp.ParamWin->TreePane->m_PTree.GetItemState(itm,0xff)&TVIS_EXPANDED)
	{
		HTREEITEM nitm=theApp.ParamWin->TreePane->m_PTree.GetChildItem(itm);
		
		while(nitm!=NULL)
			{
				SetActivePanels(nitm);
				nitm=theApp.ParamWin->TreePane->m_PTree.GetNextSiblingItem(nitm);
			}
			
	}
	
	
	
}

void CDataParamView::Reset()
{
VisiblePanelCount=0;
}

void CDataParamView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	
	// Do not call CView::OnPaint() for painting messages
}

BOOL CDataParamView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	return TRUE;
//	return CView::OnEraseBkgnd(pDC);
}
