/*
*********************************************************************
*
*	The Essence of OLE
*
*	Source code sample
*
*	Copyright (c) 1996, Rolling Thunder Computing, Ipswich MA
*
*	e-mail: essence@rollthunder.com
*	CompuServe: 73540,3430
*	Web: http://www.rollthunder.com
*
*********************************************************************
*/

// ControlListDlg.cpp : implementation file
//

#include "stdafx.h"
#include "script.h"
#include "ControlListDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CControlListDlg dialog


CControlListDlg::CControlListDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CControlListDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CControlListDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CControlListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CControlListDlg)
	DDX_Control(pDX, IDC_LIST1, m_List);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CControlListDlg, CDialog)
	//{{AFX_MSG_MAP(CControlListDlg)
	ON_LBN_DBLCLK(IDC_LIST1, OnDblclkList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CControlListDlg message handlers

BOOL CControlListDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	
	
	HKEY hKey, hClsidKey, hControlKey ;
	DWORD index = 0;
	char ClsidString [128] ;

	RegOpenKey (HKEY_CLASSES_ROOT, "Clsid", &hKey) ;

/*
For each registered class ID in that section, open the class ID and look
for the presence of the key "Control".
*/
	while (RegEnumKey (hKey, index, ClsidString, sizeof (ClsidString)) == ERROR_SUCCESS)
	{
		RegOpenKey (hKey, ClsidString, &hClsidKey) ;

		if (RegOpenKey (hClsidKey, "Control", &hControlKey) == ERROR_SUCCESS)
		{
/*
Control key found, read name and put name and class ID into list box.
*/
			char name [256] ; long namelength = sizeof (name) ;
			RegQueryValue (hKey, ClsidString, name, &namelength) ;

			int index = m_List.AddString (name) ;
			char *cp = new char [64] ;
			lstrcpy (cp, ClsidString) ;
			m_List.SetItemDataPtr (index, cp) ;
			RegCloseKey (hControlKey) ;
		}

		RegCloseKey (hClsidKey) ;
		index ++ ;
	}

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

void CControlListDlg::OnCancel() 
{
	FreeListBoxData ( ) ;
	CDialog::OnCancel();
}

void CControlListDlg::OnOK() 
{
	m_ClsidString = (char *)m_List.GetItemDataPtr (m_List.GetCurSel( )) ;

	FreeListBoxData ( ) ;
	CDialog::OnOK();
}

void CControlListDlg::FreeListBoxData ( )
{
	int Count = m_List.GetCount( ) ;

	for (int i = 0 ; i < Count ; i++)
	{
		char *cp = (char *)m_List.GetItemDataPtr (i) ;
		delete cp ;
	}
}

void CControlListDlg::OnDblclkList1() 
{
	OnOK( ) ;	
}
