// Custom Items.cpp : implementation file
//
// This file is a part of the Xtreme Toolkit for MFC.
// ©1998-2002 Codejock Software, All Rights Reserved.
//
// This source code can only be used under the terms and conditions
// outlined in the accompanying license agreement.
//
// support@codejock.com
// http://www.codejock.com
//
//////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "CustomItems.h"

////////////////////////////////////////////////////////////////////////////////////////////////

CCustomItemIcon::CCustomItemIcon(CString strCaption, HICON hIcon)
	: CXTPropertyGridItem(strCaption)
{
	m_hIcon = hIcon? CopyIcon(hIcon): 0;
	m_nFlags = pgiHasExpandButton;
}

CCustomItemIcon::~CCustomItemIcon(void)
{
	if (m_hIcon)
		DestroyIcon(m_hIcon);
}

BOOL CCustomItemIcon::OnDrawItemValue(CDC& dc, CRect rcValue)
{
	if (m_hIcon)
	{
		COLORREF clr = dc.GetTextColor();
		CRect rcSample(rcValue.left - 2, rcValue.top + 1, rcValue.left + 18, rcValue.bottom - 1);
		DrawIconEx(dc, rcSample.left, rcSample.top, m_hIcon, rcSample.Width(), rcSample.Height(), 0, 0, DI_NORMAL);
		dc.Draw3dRect(rcSample, clr, clr);
	}

	CRect rcText(rcValue);
	rcText.left += 25;

	dc.DrawText( _T("(Icon)"), rcText,  DT_SINGLELINE|DT_VCENTER);

	return TRUE;
}

void CCustomItemIcon::OnInplaceButtonDown()
{
   const TCHAR szFilters[]=
      _T("Icon files (*.ico)|*.ico|All Files (*.*)|*.*||");

	CFileDialog dlg(TRUE, _T("ico"), _T("*.ico"), OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters);
	
	if (dlg.DoModal() == IDOK)
	{
		if (m_hIcon)
			DestroyIcon(m_hIcon);
		m_hIcon = (HICON)LoadImage(NULL, dlg.GetPathName(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE|LR_DEFAULTSIZE ); 


		OnValueChanged(_T(""));
		((CWnd*)m_pGrid)->Invalidate(FALSE);
	}
}



////////////////////////////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CCustomItemSpinInplaceButton, CSpinButtonCtrl)
	ON_NOTIFY_REFLECT(UDN_DELTAPOS, OnDeltapos)
END_MESSAGE_MAP()


void CCustomItemSpinInplaceButton::OnDeltapos(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
	
	m_pItem->OnValidateEdit();
	long nValue = m_pItem->GetNumber() + pNMUpDown->iDelta;
	
	CString str;
	str.Format(_T("%i"), nValue);		
	m_pItem->OnValueChanged(str);
	
	*pResult = 1;
}

CCustomItemSpin::CCustomItemSpin(CString strCaption)
	: CXTPropertyGridItemNumber(strCaption)
{
	m_wndSpin.m_pItem = this;
}
void CCustomItemSpin::OnDeselect()
{
	if (m_wndSpin.m_hWnd) m_wndSpin.ShowWindow(SW_HIDE);
}

void CCustomItemSpin::OnSelect()
{
	CXTPropertyGridItem::OnSelect();

	CRect rc = GetItemRect();
	rc.left = rc.right - 15;
	if (!m_wndSpin.m_hWnd)
	{
		m_wndSpin.Create(UDS_ARROWKEYS|WS_CHILD, rc, (CWnd*)m_pGrid, 0);
		m_wndSpin.SetRange(0, 100);
	}
	m_wndSpin.MoveWindow(rc);	
	m_wndSpin.ShowWindow(SW_SHOW);
}
CRect CCustomItemSpin::GetValueRect()
{
	CRect rcValue(CXTPropertyGridItem::GetValueRect());
	rcValue.right -= 17;
	return rcValue;
}


////////////////////////////////////////////////////////////////////////////////////////////////

class CCustomItemChilds::CCustomItemChildsAll : public CXTPropertyGridItemNumber
{
public:
	CCustomItemChildsAll(CString strCaption) : CXTPropertyGridItemNumber(strCaption) {}
	virtual void OnValueChanged(CString strValue)
	{
		SetValue(strValue);

		CCustomItemChilds* pParent = ((CCustomItemChilds*)m_pParent);
		CRect& rc = pParent->m_rcValue;
		rc.left = rc.right = rc.top = rc.bottom = GetNumber();
		pParent->OnValueChanged(pParent->RectToString(rc));      
	}
};

class CCustomItemChilds::CCustomItemChildsPad : public CXTPropertyGridItemNumber
{
public:
	CCustomItemChildsPad(CString strCaption, LONG& nPad) : CXTPropertyGridItemNumber(strCaption), m_nPad(nPad) {}
	virtual void OnValueChanged(CString strValue)
	{
		SetValue(strValue);

		CCustomItemChilds* pParent = ((CCustomItemChilds*)m_pParent);
		m_nPad = GetNumber();
		pParent->m_itemAll->SetNumber(0);
		pParent->OnValueChanged(pParent->RectToString(pParent->m_rcValue));

	}
	LONG& m_nPad;
};

CCustomItemChilds::CCustomItemChilds(CString strCaption, CRect rcValue)
	: CXTPropertyGridItem(strCaption)
{
	m_rcValue = rcValue;
	m_strValue = RectToString(rcValue);
	m_nFlags = 0;
}

void CCustomItemChilds::OnAddChildItem()
{
	m_itemAll = (CCustomItemChildsAll*)AddChildItem(new CCustomItemChildsAll(_T("All")));
	m_itemLeft = (CCustomItemChildsPad*)AddChildItem(new CCustomItemChildsPad(_T("Left"), m_rcValue.left));
	m_itemTop = (CCustomItemChildsPad*)AddChildItem(new CCustomItemChildsPad(_T("Top"), m_rcValue.top));
	m_itemRight = (CCustomItemChildsPad*)AddChildItem(new CCustomItemChildsPad(_T("Right"), m_rcValue.right));
	m_itemBottom = (CCustomItemChildsPad*)AddChildItem(new CCustomItemChildsPad(_T("Bottom"), m_rcValue.bottom));

	UpdateChilds();
}

void CCustomItemChilds::UpdateChilds()
{
	m_itemLeft->SetNumber(m_rcValue.left);
	m_itemRight->SetNumber(m_rcValue.right);
	m_itemTop->SetNumber(m_rcValue.top);
	m_itemBottom->SetNumber(m_rcValue.bottom);
}

void CCustomItemChilds::SetValue(CString strValue)
{
	CXTPropertyGridItem::SetValue(strValue);
	UpdateChilds();
}

CString CCustomItemChilds::RectToString(CRect rc)
{
	CString str;
	str.Format(_T("%i; %i; %i; %i"), rc.left, rc.top, rc.right, rc.bottom);
	return str;
}


///////////////////////////////////////////////////////////////////////////////


class CCustomItemColorPopup: public CXTColorPopup 
{
	friend class CCustomItemColor;
public:
	CCustomItemColorPopup() : CXTColorPopup(TRUE) {}
private:
	DECLARE_MESSAGE_MAP()
	afx_msg LRESULT OnSelEndOK(WPARAM wParam, LPARAM lParam);
	
	CCustomItemColor* m_pItem;
};

BEGIN_MESSAGE_MAP(CCustomItemColorPopup, CXTColorPopup)
	 ON_MESSAGE(CPN_XT_SELENDOK, OnSelEndOK)
END_MESSAGE_MAP()

	
LRESULT CCustomItemColorPopup::OnSelEndOK(WPARAM wParam, LPARAM lParam)
{
	m_pItem->OnValueChanged(m_pItem->RGBToString((COLORREF)wParam));
	return 0;
}


CCustomItemColor::CCustomItemColor(CString strCaption, COLORREF clr)
	: CXTPropertyGridItemColor(strCaption, clr)
{
	m_nFlags = pgiHasComboButton|pgiHasEdit;
}


void CCustomItemColor::OnInplaceButtonDown()
{
	CCustomItemColorPopup *pColorPopup = new CCustomItemColorPopup();
	
	CRect rcItem= GetItemRect();
	m_pGrid->ClientToScreen(&rcItem);
	rcItem.left = rcItem.right - 158; // small hack. need to add CPS_XT_LEFTALLIGN

	pColorPopup->Create(rcItem, m_pGrid, CPS_XT_USERCOLORS|CPS_XT_EXTENDED|CPS_XT_MORECOLORS|CPS_XT_SHOW3DSELECTION|CPS_XT_SHOWHEXVALUE, GetColor(), GetColor());
	pColorPopup->SetOwner(m_pGrid);
	pColorPopup->SetFocus();	
	pColorPopup->AddListener(pColorPopup->GetSafeHwnd());
	pColorPopup->m_pItem = this;
}



