// CircProps.h : Declaration of the CCircProps
#include "resource.h"       // main symbols
#include <atlwin.h>
#include <atlctl.h>
#include "circprres.h"
#include "circ.h"

#define IID_DEFINED
#include "circ_i.c"


extern const GUID CLSID_CCircProps;

template <class T>
class CCircPropertyPageDlg :
	public IPropertyPageImpl<T>,
	public CDialogImpl<T>
	{
public:
	typedef CCircPropertyPageDlg className;

	CCircPropertyPageDlg() 
	{
		lstrcpy(m_szTitle, _T("OldMotherHubbard"));
	}

	enum {IDD = IDD_CIRCPROPS};

	BEGIN_MSG_MAP(CCircPropertyPageDlg< T >)
		COMMAND_ID_HANDLER(IDC_SETTEXT, OnSetText)
		MESSAGE_HANDLER(WM_CTLCOLOREDIT, OnCtlColorEdit)
		CHAIN_MSG_MAP(IPropertyPageImpl<T>)
	END_MSG_MAP()

	TCHAR m_szTitle[_MAX_PATH];

	LRESULT OnSetText(WORD wNotify, WORD wID, HWND hWnd, BOOL& bHandled)
	{
		SendDlgItemMessage(IDC_EDIT1, EM_GETLINE, 0, (LPARAM)m_szTitle);
		SetDirty(TRUE);
		return 0;
	}
	LRESULT OnCtlColorEdit(UINT, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		::SetTextColor((HDC)wParam, RGB(255,255,0));
		::SetBkMode((HDC)wParam, TRANSPARENT);
		return (LRESULT)::GetStockObject(BLACK_BRUSH);
	}
	LRESULT OnPagePropMsg(UINT, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		ATLTRACE(_T("Recieved  Page Prop Message\n"));
		return 0;
	}
};

/////////////////////////////////////////////////////////////////////////////
// circ

class CCircProps :
	public CComObjectRoot,
	public CComCoClass<CCircProps,&CLSID_CCircProps>,
	public CCircPropertyPageDlg<CCircProps>
{

public:
	CCircProps()
	{
		m_dwTitleID = IDS_TITLE;
		m_dwHelpFileID = IDS_HELPFILE;
		m_dwDocStringID = IDS_DOCSTRING;
	}
	STDMETHOD(Apply)(void)
	{
		ICircCtl* pCirc;

		ATLTRACE(_T("CCircProps::Apply\n"));
		for (UINT i = 0; i < m_nObjects; i++)
		{
			m_ppUnk[i]->QueryInterface(IID_ICircCtl, (void**)&pCirc);
			pCirc->put_Caption(CComBSTR(m_szTitle));
			pCirc->Release();
		}
		m_bDirty = FALSE;
		return S_OK;
	}
	STDMETHOD(GetPageInfo)(PROPPAGEINFO *pPageInfo)
	{
		ATLTRACE(_T("CCircProps::GetPageInfo\n"));
		LONG cDlgBaseUnits = GetDialogBaseUnits();

		m_size.cx = (LOWORD(cDlgBaseUnits) * 188) / 4;
		m_size.cy = (HIWORD(cDlgBaseUnits) * 97) / 8;

 		return CCircPropertyPageDlg<CCircProps>::GetPageInfo(pPageInfo);
	}

	STDMETHOD(SetObjects)(ULONG nObjects, IUnknown **ppUnk)
	{
		HRESULT hr = IPropertyPageImpl<CCircProps>::SetObjects(nObjects, ppUnk);
		if (SUCCEEDED(hr))
		{
			CComPtr<ICircCtl> pCirc;
			m_ppUnk[0]->QueryInterface(IID_ICircCtl, (void**)&pCirc);
			if (pCirc != NULL)
			{
				USES_CONVERSION;
				BSTR bstrTitle;
				pCirc->get_Caption(&bstrTitle);
				lstrcpy(m_szTitle, OLE2T(bstrTitle));
			}
		}
		return hr;
	}

BEGIN_COM_MAP(CCircProps) 
	COM_INTERFACE_ENTRY_IMPL(IPropertyPage)
END_COM_MAP()

DECLARE_GET_CONTROLLING_UNKNOWN()
DECLARE_REGISTRY_RESOURCEID(IDR_CircProps)
};

