// ColorProps.cpp : implementation file
//

#include "stdafx.h"
#include "imagegrinder.h"
#include "ColorProps.h"
#include "..\..\Libraries\ImageLib\Image.h"
 


extern CImageGrinderApp theApp;
#define COLSIZE 16
/////////////////////////////////////////////////////////////////////////////
// CColorProps dialog


CColorProps::CColorProps(CWnd* pParent /*=NULL*/)
	: CDialog(CColorProps::IDD, pParent)
{
	//{{AFX_DATA_INIT(CColorProps)
	//}}AFX_DATA_INIT
}


void CColorProps::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CColorProps)
	DDX_Control(pDX, IDC_WFIMG, m_wfimg);
	DDX_Control(pDX, IDC_HLIMG, m_hlimg);
	DDX_Control(pDX, IDC_GCIMG, m_gcimg);
	DDX_Control(pDX, IDC_SELIMG, m_selimg);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CColorProps, CDialog)
	//{{AFX_MSG_MAP(CColorProps)
	ON_BN_CLICKED(IDC_GCMOD, OnGcmod)
	ON_BN_CLICKED(IDC_HLMOD, OnHlmod)
	ON_BN_CLICKED(IDC_SELMOD, OnSelmod)
	ON_BN_CLICKED(IDC_WFMOD, OnWfmod)
	ON_WM_HSCROLL()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorProps message handlers

void CColorProps::OnGcmod() 
{
CColorDialog dlg;

if(dlg.DoModal()==IDOK)
{
Grid=dlg.GetColor();
UpdateBitmaps();	

}

	
}

void CColorProps::OnHlmod() 
{
CColorDialog dlg;

if(dlg.DoModal()==IDOK)
{
WireHL=dlg.GetColor();
UpdateBitmaps();	
}

}

void CColorProps::OnSelmod() 
{
CColorDialog dlg;

if(dlg.DoModal()==IDOK)
{
Selection=dlg.GetColor();
UpdateBitmaps();	
}

}

void CColorProps::OnWfmod() 
{
CColorDialog dlg;


if(dlg.DoModal()==IDOK)
{
Wire=dlg.GetColor();
UpdateBitmaps();	
}

}

int CColorProps::DoModal() 
{
	return CDialog::DoModal();
}

void CColorProps::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

BOOL CColorProps::OnInitDialog() 
{
	CDialog::OnInitDialog();
CString AppName;
		AppName.LoadString(AFX_IDS_APP_TITLE);
Selection=theApp.GetProfileInt(AppName,"SelectionColor",0x00ff00);
Grid=theApp.GetProfileInt(AppName,"GridColor",0xffff00);
Wire=theApp.GetProfileInt(AppName,"WireColor",0x7f6f7f);
WireHL=theApp.GetProfileInt(AppName,"WireHLColor",0xff7fff);
Box=theApp.GetProfileInt(AppName,"BoxColor",0xffffff);

CDC *dc=GetDC();
SelBmp.CreateCompatibleBitmap(dc,COLSIZE,COLSIZE);
GridBmp.CreateCompatibleBitmap(dc,COLSIZE,COLSIZE);
WireBmp.CreateCompatibleBitmap(dc,COLSIZE,COLSIZE);
WireHLBmp.CreateCompatibleBitmap(dc,COLSIZE,COLSIZE);
BoxBmp.CreateCompatibleBitmap(dc,COLSIZE,COLSIZE);
ReleaseDC(dc);

UpdateBitmaps();	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CColorProps::OnOK() 
{
CString AppName;
		AppName.LoadString(AFX_IDS_APP_TITLE);
theApp.WriteProfileInt(AppName,"SelectionColor",Selection);
theApp.WriteProfileInt(AppName,"GridColor",Grid);
theApp.WriteProfileInt(AppName,"WireColor",Wire);
theApp.WriteProfileInt(AppName,"WireHLColor",WireHL);
theApp.WriteProfileInt(AppName,"BoxColor",Box);
	
	CDialog::OnOK();
}

void CColorProps::UpdateBitmaps()
{
CPen pen;
CPoint pt;
pt.x=100;
pt.y=20;
Image img;
img.CreateBlank(COLSIZE,COLSIZE,ITYPE_RGB,24);

	BITMAPINFO       binf;
	binf.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
	binf.bmiHeader.biWidth=img.GetWidth(); 
	binf.bmiHeader.biHeight=-img.GetHeight();
	binf.bmiHeader.biPlanes=1;
	binf.bmiHeader.biBitCount=img.GetBpp();
	binf.bmiHeader.biCompression=BI_RGB;
	binf.bmiHeader.biSizeImage=0;
	binf.bmiHeader.biClrImportant=0;

CDC memdc,*dc=GetDC();
int res;

memdc.CreateCompatibleDC(dc);
memdc.SelectObject(SelBmp);
img.FillColor(ImgRGBColor(Selection));
res=SetDIBitsToDevice(memdc,0,0,
				img.GetWidth(),img.GetHeight(),
				0,0,0,img.GetHeight(),
				img.Lock(),&binf,DIB_RGB_COLORS);
ASSERT(res!=0);
img.UnLock();
memdc.DeleteDC();
m_selimg.SetBitmap(SelBmp);

memdc.CreateCompatibleDC(dc);
memdc.SelectObject(GridBmp);
img.FillColor(ImgRGBColor(Grid));
res=SetDIBitsToDevice(memdc,0,0,
				img.GetWidth(),img.GetHeight(),
				0,0,0,img.GetHeight(),
				img.Lock(),&binf,DIB_RGB_COLORS);
ASSERT(res!=0);
img.UnLock();
memdc.DeleteDC();
m_gcimg.SetBitmap(GridBmp);

memdc.CreateCompatibleDC(dc);
memdc.SelectObject(WireBmp);
img.FillColor(ImgRGBColor(Wire));
res=SetDIBitsToDevice(memdc,0,0,
				img.GetWidth(),img.GetHeight(),
				0,0,0,img.GetHeight(),
				img.Lock(),&binf,DIB_RGB_COLORS);
ASSERT(res!=0);
img.UnLock();
memdc.DeleteDC();
m_wfimg.SetBitmap(WireBmp);


memdc.CreateCompatibleDC(dc);
memdc.SelectObject(WireHLBmp);
img.FillColor(ImgRGBColor(WireHL));
res=SetDIBitsToDevice(memdc,0,0,
				img.GetWidth(),img.GetHeight(),
				0,0,0,img.GetHeight(),
				img.Lock(),&binf,DIB_RGB_COLORS);
ASSERT(res!=0);
img.UnLock();
memdc.DeleteDC();
m_hlimg.SetBitmap(WireHLBmp);
/*
pen.CreatePen(PS_SOLID,1,Grid);
dc->MoveTo(pt);
dc->LineTo(pt.x+50,pt.y);
pen.DeleteObject();
pt.y+=32;

pen.CreatePen(PS_SOLID,1,Selection);
dc->MoveTo(pt);
dc->LineTo(pt.x+50,pt.y);
pen.DeleteObject();
pt.y+=32;

pen.CreatePen(PS_SOLID,1,Wire);
dc->MoveTo(pt);
dc->LineTo(pt.x+50,pt.y);
pen.DeleteObject();
pt.y+=32;

pen.CreatePen(PS_SOLID,1,WireHL);
dc->MoveTo(pt);
dc->LineTo(pt.x+50,pt.y);
pen.DeleteObject();
pt.y+=32;
*/
ReleaseDC(dc);
}

void CColorProps::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
//UpdateBitmaps();	
	// Do not call CDialog::OnPaint() for painting messages
}
