// HFPreview.cpp : implementation file
//

#include "stdafx.h"
#include "imagegrinder.h"
#include "HFPreview.h"

#include "ImageGrinderView.h"
#include "UserValueDlg.h"



/////////////////////////////////////////////////////////////////////////////
// CHFPreview

IMPLEMENT_DYNCREATE(CHFPreview, CFrameWnd)

CHFPreview::CHFPreview()
{
	Adjust=false;
}

CHFPreview::~CHFPreview()
{
}


BEGIN_MESSAGE_MAP(CHFPreview, CFrameWnd)
//{{AFX_MSG_MAP(CHFPreview)
ON_WM_PAINT()
ON_WM_CLOSE()
ON_WM_SIZE()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHFPreview message handlers

void CHFPreview::UpdateWindow()
{
	CDC  memDC,*hfDC;
	hfDC=GetDC();
	int res;
	
	BITMAP bmp;
	res=BackBuffer.GetBitmap(&bmp);
	Verify(res);
	
	BOOL bres;
   bres=memDC.CreateCompatibleDC(hfDC);
    Verify(bres);
	memDC.SelectObject(&BackBuffer);
	CRect hfrct;
	GetClientRect(&hfrct);
	hfDC->SetStretchBltMode(COLORONCOLOR);
	bres=hfDC->StretchBlt(0,0,hfrct.Width(),hfrct.Height(),&memDC,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
	Verify(bres);
	memDC.DeleteDC();


		hfDC->SelectObject(GetStockObject(NULL_BRUSH));
		CImageGrinderDoc *pDoc;
		pDoc=OwnerView->GetDocument();
		CRect drct;

		hfDC->SelectObject(&OwnerView->GridPen);
		
		CPoint tpnt=pDoc->NewPlacePoint;
		DataToScreen(&tpnt);
		hfDC->Ellipse(tpnt.x-5,tpnt.y-5,tpnt.x+5,tpnt.y+5);
		
		int x,y;
		CPoint tmppnt;
		for(y=0;y<pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize() ;y+=OwnerView->PixelSpace)
		{
			tmppnt.x=0;
			tmppnt.y=y;
			DataToScreen(&tmppnt);
			hfDC->MoveTo(tmppnt);	
			tmppnt.x=pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize();
			tmppnt.y=y;
			DataToScreen(&tmppnt);
			hfDC->LineTo(tmppnt);	
		}
		
		for(x=0;x<pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize() ;x+=OwnerView->PixelSpace)
		{
			tmppnt.x=x;
			tmppnt.y=0;
			DataToScreen(&tmppnt);
			hfDC->MoveTo(tmppnt);	
			tmppnt.x=x;
			tmppnt.y=pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize();
			DataToScreen(&tmppnt);
			hfDC->LineTo(tmppnt);	
		}


		const Stuff::DynamicArrayOf<Compost::FeatureInstance*> *FList;
		int count;
		int CurFet;

		FList=pDoc->FGrid->GetUniqueFeatureInstances(count);
		for(CurFet=0;CurFet<count;CurFet++)
		{
			hfDC->SelectObject(GetStockObject(NULL_BRUSH));
		if(OwnerView->WireMode)
		{
			if(((Compost::Tool_Feature *)(*FList)[CurFet]->feature)->StatFlags&FF_HIGHLIGHTED)
			hfDC->SelectObject(&OwnerView->WireHLPen);
		else
			hfDC->SelectObject(&OwnerView->WirePen);

		drct.top=(*FList)[CurFet]->feature_z_pos;
		drct.left=(*FList)[CurFet]->feature_x_pos;
		drct.bottom=drct.top+(*FList)[CurFet]->feature->GetFeatureHeight()+1;
		drct.right=drct.left+(*FList)[CurFet]->feature->GetFeatureWidth()+1;
		DataToScreen(&drct);
		hfDC->Rectangle(&drct);
		

		}// end of wireframe mode	
		

		if((*FList)[CurFet]->StatFlags&FIF_SELECTED)
			{
			hfDC->SelectObject(&PenSelected);
			hfDC->SelectObject(GetStockObject(NULL_BRUSH));
			CPoint tl,tr,bl,br,ct;
			bl.x=tl.x=(*FList)[CurFet]->feature_x_pos;
			tr.y=tl.y=(*FList)[CurFet]->feature_z_pos;
			tr.x=br.x=(*FList)[CurFet]->feature_x_pos+(*FList)[CurFet]->feature->GetFeatureWidth()+1;
			bl.y=br.y=(*FList)[CurFet]->feature_z_pos+(*FList)[CurFet]->feature->GetFeatureHeight()+1;
			ct.x=tl.x+(*FList)[CurFet]->feature->GetFeatureWidth()/2;
			ct.y=tl.y+(*FList)[CurFet]->feature->GetFeatureHeight()/2;
			// Draw Selected Box
			CRect rct;
			rct.SetRect(tl,br);
			DataToScreen(&rct);
			hfDC->Rectangle(&rct);
			
			hfDC->SelectObject(&OwnerView->SolidSel);
			rct.left=rct.top=-HANDLESIZE/2;
			rct.right=rct.bottom=HANDLESIZE/2;
			rct.OffsetRect(ct);
			DataToScreen(&rct);
			hfDC->Rectangle(&rct);
			
			hfDC->SelectObject(&OwnerView->GreenBrush);
			// Draw Streching handles
			//	rct.left=rct.top=-HANDLESIZE/2; rct.right=rct.bottom=HANDLESIZE/2;
			rct.left=rct.top=0; rct.right=rct.bottom=HANDLESIZE;
			rct.OffsetRect(tl);
			DataToScreen(&rct);
			hfDC->Rectangle(&rct);
			
			rct.left=0;
			rct.top=-HANDLESIZE; 
			rct.right=HANDLESIZE;
			rct.bottom=0;
			
			rct.OffsetRect(bl);
			DataToScreen(&rct);
			hfDC->Rectangle(&rct);
			
			rct.left=rct.top=-HANDLESIZE; rct.right=rct.bottom=0;
			rct.OffsetRect(br);
			DataToScreen(&rct);
			hfDC->Rectangle(&rct);
			
			rct.left=-HANDLESIZE;
			rct.top=0; 
			rct.right=0;
			rct.bottom=HANDLESIZE;
			rct.OffsetRect(tr);
			DataToScreen(&rct);
			hfDC->Rectangle(&rct);
			
			rct.left=-HANDLESIZE/2;
			rct.top=0; 
			rct.right=HANDLESIZE/2;
			rct.bottom=HANDLESIZE;
			rct.OffsetRect(tl.x+(tr.x-tl.x)/2,tl.y);
			DataToScreen(&rct);
			hfDC->Rectangle(&rct);
			
			rct.left=-HANDLESIZE/2;
			rct.top=-HANDLESIZE; 
			rct.right=HANDLESIZE/2;
			rct.bottom=0;
			rct.OffsetRect(bl.x+(br.x-bl.x)/2,bl.y);
			DataToScreen(&rct);
			hfDC->Rectangle(&rct);
			
			rct.left=0;
			rct.top=-HANDLESIZE/2; 
			rct.right=HANDLESIZE;
			rct.bottom=HANDLESIZE/2;
			rct.OffsetRect(tl.x,tl.y+(bl.y-tl.y)/2);
			DataToScreen(&rct);
			hfDC->Rectangle(&rct);
			
			rct.left=-HANDLESIZE;
			rct.top=-HANDLESIZE/2; 
			rct.right=0;
			rct.bottom=HANDLESIZE/2;
			rct.OffsetRect(tr.x,tl.y+(br.y-tr.y)/2);
			DataToScreen(&rct);
			hfDC->Rectangle(&rct);
			
		}	// end of idf selected

		
		}// end of feature loop

	

/*	
	CRect rct;
for(int CurFet=0;CurFet<pDoc->SelectionCount;CurFet++)
	{
		hfDC->SelectObject(PenSelected);
		hfDC->SelectObject(GetStockObject(NULL_BRUSH));
		
		rct.left=(pDoc->SelectedFeature[CurFet]->feature_x_pos*hfrct.Width())/(pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize());
		rct.top=(pDoc->SelectedFeature[CurFet]->feature_z_pos*hfrct.Height())/(pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize());
		rct.right=rct.left+(pDoc->SelectedFeature[CurFet]->feature->GetFeatureWidth()*hfrct.Width())/(pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize());
		rct.bottom=rct.top+(pDoc->SelectedFeature[CurFet]->feature->GetFeatureHeight()*hfrct.Height())/(pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize());
		hfDC->Rectangle(&rct);
	}
*/
  ReleaseDC(hfDC);
}

void CHFPreview::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	UpdateWindow();
	// Do not call CFrameWnd::OnPaint() for painting messages
}

BOOL CHFPreview::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);;
}

void CHFPreview::Initalize(CImageGrinderView *view)
{
	OwnerView=view;
	CDC *hfDC,memDC;
	
	LOGBRUSH lb;
	lb.lbColor=0x00ff00;
	lb.lbStyle=BS_SOLID;
	PenSelected.CreatePen(PS_DOT,1,&lb);
	
	hfDC=GetDC();
	BOOL bres;
	bres=BackBuffer.CreateCompatibleBitmap(hfDC,4,4);
    Verify(bres);
	memDC.CreateCompatibleDC(hfDC);
    memDC.SelectObject(&BackBuffer);
	memDC.BitBlt(0,0,4,4,NULL,0,0,BLACKNESS);
	memDC.DeleteDC();
	ReleaseDC(hfDC);
	
}

void CHFPreview::OnClose() 
{
	
	ShowWindow(SW_HIDE);
	//	CFrameWnd::OnClose();
}

void CHFPreview::OnSize(UINT nType, int cx, int cy) 
{
	CFrameWnd::OnSize(nType, cx, cy);
	
	BITMAP bmp;
	BackBuffer.GetBitmap(&bmp);
	
	int ph;
	ph=((cx*bmp.bmHeight)/bmp.bmWidth)+GetSystemMetrics(SM_CYCAPTION);
	
	if(Adjust==false)
	{
		if(cy!=ph)
		{
			Adjust=true;
			SetWindowPos(NULL,0,0,cx,ph,SWP_NOMOVE|SWP_NOZORDER);
		}
	}
	else
	{
		Adjust=false;
	}
}

void CHFPreview::DataToScreen(CPoint *pnt)
{
	CRect hfrct;
	GetClientRect(&hfrct);
	CImageGrinderDoc *pDoc;
	pDoc=OwnerView->GetDocument();

	pnt->x=(pnt->x*hfrct.Width())/(pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize());
	pnt->y=(pnt->y*hfrct.Height())/(pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize());

}

void CHFPreview::DataToScreen(CRect *rct)
{
	CRect hfrct;
	GetClientRect(&hfrct);
	CImageGrinderDoc *pDoc;
	pDoc=OwnerView->GetDocument();

	rct->left=(rct->left*hfrct.Width())/(pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize());
	rct->right=(rct->right*hfrct.Width())/(pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize());
	rct->top=(rct->top*hfrct.Height())/(pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize());
	rct->bottom=(rct->bottom*hfrct.Height())/(pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize());

}

void CHFPreview::ScreenToData(CPoint *pnt)
{
	CRect hfrct;
	GetClientRect(&hfrct);
	CImageGrinderDoc *pDoc;
	pDoc=OwnerView->GetDocument();

	pnt->x=(pnt->x*(pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize()))/hfrct.Width();
	pnt->y=(pnt->y*(pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize()))/hfrct.Height();

}

void CHFPreview::ScreenToData(CRect *rct)
{
	CRect hfrct;
	GetClientRect(&hfrct);
	CImageGrinderDoc *pDoc;
	pDoc=OwnerView->GetDocument();

	rct->left=(rct->left*(pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize()))/hfrct.Width();
	rct->right=(rct->right*(pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize()))/hfrct.Width();
	rct->top=(rct->top*(pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize()))/hfrct.Height();
	rct->bottom=(rct->bottom*(pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize()))/hfrct.Height();

}

void CHFPreview::UpdateHF()
{
	CImageGrinderDoc *pDoc;
	pDoc=OwnerView->GetDocument();

	CDC  memDC,*hfDC;
	hfDC=GetDC();
	
	BackBuffer.DeleteObject();
	
	BOOL bres;
	bres=BackBuffer.CreateCompatibleBitmap(hfDC,pDoc->FinalHF.GetWidth(),pDoc->FinalHF.GetHeight());
	Verify(bres);

    memDC.CreateCompatibleDC(hfDC);
    memDC.SelectObject(&BackBuffer);
	
	BITMAPINFOHEADER bhd;
	BITMAPINFO       binf;
	bhd.biSize=sizeof(BITMAPINFOHEADER);
	bhd.biWidth=pDoc->FinalHF.GetWidth(); bhd.biHeight=-pDoc->FinalHF.GetHeight();
	bhd.biPlanes=1;
	bhd.biBitCount=pDoc->FinalHF.GetBpp();
	bhd.biCompression=BI_RGB;
	bhd.biSizeImage=0;
	bhd.biClrImportant=0;
	binf.bmiHeader=bhd;
	
	BYTE *imgdat=(BYTE *)(pDoc->FinalHF.Lock());
	
	SetDIBitsToDevice(memDC,0,0,pDoc->FinalHF.GetWidth(),
		pDoc->FinalHF.GetHeight(),0,0,0,pDoc->FinalHF.GetWidth(),imgdat,
		&binf,DIB_RGB_COLORS);  
	
	pDoc->FinalHF.UnLock();
	
	memDC.DeleteDC();
	ReleaseDC(hfDC);
	
	UpdateWindow();
}

void CHFPreview::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CFrameWnd::OnRButtonDown(nFlags, point);
}

void CHFPreview::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CUserValueDlg dlg;
	int tmp=128;
	CImageGrinderDoc *pDoc;
	pDoc=OwnerView->GetDocument();
	CPoint tpnt=point;
	ScreenToData(&tpnt);
	Compost::FeatureInstance *fetint=pDoc->GetTopFeature(tpnt);
	if(fetint)
	{
	
	Compost::Tool_Feature *fet=(Compost::Tool_Feature *)fetint->feature;
		dlg.DoModal(this,&(fet->BlendVal));
	}
	CFrameWnd::OnRButtonUp(nFlags, point);
}
