// DisplayTypeLibDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TypeLibraryBrowser.h"
#include "DisplayTypeLibDlg.h"
#include "DisplayEnumDlg.h"
#include "DisplayStructDlg.h"
#include "DisplayCoclassDlg.h"
#include "DisplayInterfaceDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDisplayTypeLibDlg dialog


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


void CDisplayTypeLibDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDisplayTypeLibDlg)
	DDX_Control(pDX, IDC_BUTTON1, m_btnOpenThisTypLib);
	DDX_Control(pDX, IDC_EDIT2, m_EditTypeInfoHelpString);
	DDX_Control(pDX, IDC_EDIT1, m_EditTypeInfoType);
	DDX_Control(pDX, IDC_LIST1, m_List);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDisplayTypeLibDlg, CDialog)
	//{{AFX_MSG_MAP(CDisplayTypeLibDlg)
	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
	ON_BN_CLICKED(IDC_BUTTON1, OnOpenThisTypeInfo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDisplayTypeLibDlg message handlers

BOOL CDisplayTypeLibDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	USES_CONVERSION ;

/*
Get the name of the type library
Put it in the dialog box's title bar
*/

	BSTR bstrTypeLibName ;
	_bstr_t Title = "Browsing Type Library: " ;

	m_pTypeLib->GetDocumentation (-1, &bstrTypeLibName, NULL, NULL, NULL) ;
	Title += bstrTypeLibName ;
	SetWindowText ((char *) Title) ;
	SysFreeString (bstrTypeLibName) ;

/*
Enumerate all of the type infos in the type library.
For each type info in the library, get its name and add it to the type list
control in the dialog box.  The name comes in a BSTRING which we must free
*/

	int i ;
	int TypeInfoCount = m_pTypeLib->GetTypeInfoCount();

	BSTR bstrName;

	for(i = 0; i < TypeInfoCount; i++)
	{
	  m_pTypeLib->GetDocumentation(i, &bstrName, NULL, NULL, NULL);
	  m_List.AddString (W2A(bstrName)) ;
	  SysFreeString(bstrName);
	}               
	

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


void CDisplayTypeLibDlg::OnSelchangeList1() 
{

	USES_CONVERSION ;

	static char * TypeKind[] = {
	 "Enum",             /* TKIND_ENUM */
	 "Struct",           /* TKIND_RECORD, actually means 'structure' */
	 "Module",           /* TKIND_MODULE */
	 "Interface",        /* TKIND_INTERFACE */
	 "Dispinterface",    /* TKIND_DISPATCH */
	 "Coclass",          /* TKIND_COCLASS */
	 "Typedef",          /* TKIND_ALIAS, actually means 'typedef' */
	 "Union",    };        /* TKIND_UNION */


	m_CurSel = m_List.GetCurSel ( ) ;

	if (m_CurSel != LB_ERR)
	{
		m_btnOpenThisTypLib.EnableWindow (TRUE) ;

		TYPEKIND tk ;
		m_pTypeLib->GetTypeInfoType (m_CurSel, &tk) ;
		m_EditTypeInfoType.SetWindowText (TypeKind [tk]) ;

		
		BSTR bstrDocString ;
		m_pTypeLib->GetDocumentation (m_CurSel, NULL, &bstrDocString, NULL, NULL) ;
		m_EditTypeInfoHelpString.SetWindowText (W2A(bstrDocString)) ;
		SysFreeString (bstrDocString) ;

	}
}

void CDisplayTypeLibDlg::OnOpenThisTypeInfo() 
{

/*
Get open the type info that the user has selected.
*/

	m_pTypeLib->GetTypeInfo (m_CurSel, &m_pTypeInfo) ;

/*
Based on the type of the currently selected type info, pop up
the right type of dialog box.
*/
	TYPEKIND tk ;
	m_pTypeLib->GetTypeInfoType (m_CurSel, &tk) ;


	switch (tk)
	{
		case TKIND_ENUM:
		{
			CDisplayEnumDlg dlg (NULL, m_pTypeInfo) ;
			dlg.DoModal ( ) ;
			break ;
		}


		case TKIND_RECORD:	// that's what they call a structure
		{
			CDisplayStructDlg dlg (NULL, m_pTypeInfo) ;
			dlg.DoModal ( ) ;
			break ;
		}
			
		case TKIND_INTERFACE:
		case TKIND_DISPATCH:
		{
			CDisplayInterfaceDlg dlg (NULL, m_pTypeInfo) ;
			dlg.DoModal ( ) ;
			break ;
		}
			
		case TKIND_COCLASS:
		{
			CDisplayCoclassDlg dlg (NULL, m_pTypeInfo) ;
			dlg.DoModal ( ) ;
			break ;
		}
	}
}
