/***
* Typelib.cpp
*
*
*****************************************************************************/

#include "typbrows.h"


TCHAR * TypeKind[] = {
	TEXT("Enum"),             /* TKIND_ENUM */
	TEXT("Struct"),           /* TKIND_RECORD */
	TEXT("Module"),           /* TKIND_MODULE */
	TEXT("Interface"),        /* TKIND_INTERFACE */
	TEXT("Dispinterface"),    /* TKIND_DISPATCH */
	TEXT("Coclass"),          /* TKIND_COCLASS */
	TEXT("Typedef"),          /* TKIND_ALIAS */
	TEXT("Union"),            /* TKIND_UNION */
};



BOOL InitTypeLib (ITypeLib *pTypeLib, HWND hParent)
{
	unsigned int utypeinfoCount, i;
	BSTR bstrDoc, bstrHelpFile ;
	DWORD dwHelpContext ;
	TCHAR out [256] ;	
	TCHAR FAR* psz;


	utypeinfoCount = pTypeLib->GetTypeInfoCount();

/*
Get help string for type library itself and place into text control
*/

  pTypeLib->GetDocumentation((unsigned int)-1, 
  	NULL, &bstrDoc, &dwHelpContext, &bstrHelpFile);

	wsprintf(out, TEXT("%ld"), dwHelpContext);
	SetDlgItemText (hParent,IDC_HELPCONTEXT, out);

	psz = (bstrDoc != NULL) ? bstrDoc : TEXT("<none>");
	SetDlgItemText (hParent,IDC_HELPSTRING, psz);
	if (bstrDoc) SysFreeString (bstrDoc) ;
		
	psz = (bstrHelpFile != NULL) ? bstrHelpFile : TEXT("<none>") ;
	SetDlgItemText (hParent, IDC_HELPFILE, psz) ;
	if (bstrHelpFile) SysFreeString (bstrHelpFile) ;


/*
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
*/
	utypeinfoCount = pTypeLib->GetTypeInfoCount();

	BSTR bstrName;
		
	for(i = 0; i < utypeinfoCount; i++)
	{
	  pTypeLib->GetDocumentation(i, &bstrName, NULL, NULL, NULL);
	  SendDlgItemMessage (hParent,IDC_TYPELIST,  LB_ADDSTRING, 0, (LPARAM)bstrName);
	  SysFreeString(bstrName);
	}               
	
	return TRUE;
}

/***
*void SetSelectedType
*Purpose:
*  When the user changes the selection of a type, this function updates the
*  dialog by changing the member list and the help for the type. It also sets
*  pTypeInfoCur to refer to the typeinfo.
*
*Entry:
*  dwIndex =
*
*Exit:
*  return value = None
*
***********************************************************************/
void SetSelectedType(LPTYPELIB pTypeLib, DWORD dwIndex, HWND hParent)
{
	BSTR bstrDoc, bstrHelpFile ;
	TCHAR FAR* psz;
	TCHAR szBuf[40];
	unsigned long dwHelpContext;
	TYPEKIND tkind;
    

	// Note: the index in the list box is conveniently the same as the
	// one to pass to GetTypeInfo/GetTypeInfoType

/*
Find the type of typeinfo this one is -- interface, class, etc.  Set
into dialog item edit control
*/

	pTypeLib->GetTypeInfoType((unsigned int)dwIndex, &tkind);
	SetDlgItemText(hParent, IDC_TYPEKIND, TypeKind[tkind]);


/*
Get the documentation string and help context from the type lib.
Place into their dialog items
*/

  	pTypeLib->GetDocumentation(
		(unsigned int)dwIndex, NULL, &bstrDoc, &dwHelpContext, &bstrHelpFile);

	wsprintf(szBuf, TEXT("%ld"), dwHelpContext);
	SetDlgItemText (hParent,IDC_HELPCONTEXT, szBuf);

	psz = (bstrDoc != NULL) ? bstrDoc : TEXT("<none>");
	SetDlgItemText (hParent,IDC_HELPSTRING, psz);
	
	psz = (bstrHelpFile != NULL) ? bstrHelpFile : TEXT("<none>") ;
	SetDlgItemText (hParent, IDC_HELPFILE, psz) ;
	
	SysFreeString(bstrDoc);
	SysFreeString(bstrHelpFile) ;
}
