text: // IUIDocManager.cpp: implementation of the CIUIDocManager class.
  //
  //////////////////////////////////////////////////////////////////////
  
  #include "stdafx.h"
  #include <afxpriv.h>
  
  #ifdef _DEBUG
  #undef THIS_FILE
  static char THIS_FILE[] = __FILE__;
  #define new DEBUG_NEW
  #endif
  
  
  AFX_STATIC_DATA const TCHAR _afxShellOpenFmt[] = _T("%s\\shell\\open\\%s");
  AFX_STATIC_DATA const TCHAR _afxShellPrintFmt[] = _T("%s\\shell\\print\\%s");
  AFX_STATIC_DATA const TCHAR _afxShellPrintToFmt[] = _T("%s\\shell\\printto\\%s");
  AFX_STATIC_DATA const TCHAR _afxDefaultIconFmt[] = _T("%s\\DefaultIcon");
  AFX_STATIC_DATA const TCHAR _afxShellNewFmt[] = _T("%s\\ShellNew");
  
  #define DEFAULT_ICON_INDEX 0
  
  AFX_STATIC_DATA const TCHAR _afxIconIndexFmt[] = _T(",%d");
  AFX_STATIC_DATA const TCHAR _afxCommand[] = _T("command");
  AFX_STATIC_DATA const TCHAR _afxOpenArg[] = _T(" \"%1\"");
  AFX_STATIC_DATA const TCHAR _afxPrintArg[] = _T(" /p \"%1\"");
  AFX_STATIC_DATA const TCHAR _afxPrintToArg[] = _T(" /pt \"%1\" \"%2\" \"%3\" \"%4\"");
  AFX_STATIC_DATA const TCHAR _afxDDEArg[] = _T(" /dde");
  
  AFX_STATIC_DATA const TCHAR _afxDDEExec[] = _T("ddeexec");
  AFX_STATIC_DATA const TCHAR _afxDDEOpen[] = _T("[open(\"%1\")]");
  AFX_STATIC_DATA const TCHAR _afxDDEPrint[] = _T("[print(\"%1\")]");
  AFX_STATIC_DATA const TCHAR _afxDDEPrintTo[] = _T("[printto(\"%1\",\"%2\",\"%3\",\"%4\")]");
  
  AFX_STATIC_DATA const TCHAR _afxShellNewValueName[] = _T("NullFile");
  AFX_STATIC_DATA const TCHAR _afxShellNewValue[] = _T("");
  
  
  IMPLEMENT_DYNAMIC(CIUIDocManager, CDocManager)
  
  //////////////////////////////////////////////////////////////////////
  // Construction/Destruction
  //////////////////////////////////////////////////////////////////////
  
  CIUIDocManager::CIUIDocManager()
  {
  
  }
  
  CIUIDocManager::~CIUIDocManager()
  {
  
  }
  
  //////////////////////////////////////////////////////////////////////////
  // virtual
  
  // bCompat for print or print to
  void CIUIDocManager::RegisterShellFileTypes(BOOL bCompat)
  {
  	bCompat = FALSE;
  	ASSERT(!m_templateList.IsEmpty());  // must have some doc templates
  
  	CString strPathName, strTemp;
  
  	AfxGetModuleShortFileName(AfxGetInstanceHandle(), strPathName);
  
  	POSITION pos = m_templateList.GetHeadPosition();
  	for (int nTemplateIndex = 1; pos != NULL; nTemplateIndex++)
  	{
  		CDocTemplate *pTemplate = (CDocTemplate *)m_templateList.GetNext(pos);
  
  		CString strOpenCommandLine = strPathName;
  		CString strPrintCommandLine = strPathName;
  		CString strPrintToCommandLine = strPathName;
  		CString strDefaultIconCommandLine = strPathName;
  
  		if (bCompat)
  		{
  			CString strIconIndex;
  			HICON hIcon = ::ExtractIcon(AfxGetInstanceHandle(), strPathName, nTemplateIndex);
  			if (hIcon != NULL)
  			{
  				strIconIndex.Format(_afxIconIndexFmt, nTemplateIndex);
  				DestroyIcon(hIcon);
  			}
  			else
  			{
  				strIconIndex.Format(_afxIconIndexFmt, DEFAULT_ICON_INDEX);
  			}
  			strDefaultIconCommandLine += strIconIndex;
  		}
  
  		CString strFilterExt, strFileTypeId, strFileTypeName;
  		if (pTemplate->GetDocString(strFileTypeId,
  				CDocTemplate::regFileTypeId) && !strFileTypeId.IsEmpty())
  		{
  			// enough info to register it
  			if (!pTemplate->GetDocString(strFileTypeName,
  					CDocTemplate::regFileTypeName))
  			{
  				strFileTypeName = strFileTypeId;    // use id name
  			}
  
  			ASSERT(strFileTypeId.Find(' ') == -1);  // no spaces allowed
  
  			// first register the type ID of our server
  			if (!IUISetRegKey(strFileTypeId, strFileTypeName, NULL))
  			{
  				continue;    // just skip it
  			}
  
  			if (bCompat)
  			{
  				// path\DefaultIcon = path,1
  				strTemp.Format(_afxDefaultIconFmt, (LPCTSTR)strFileTypeId);
  				if (!IUISetRegKey(strTemp, strDefaultIconCommandLine, NULL))
  				{
  					continue;    // just skip it
  				}
  			}
  
  			// If MDI Application
  			if (!pTemplate->GetDocString(strTemp, CDocTemplate::windowTitle) ||
  				strTemp.IsEmpty())
  			{
  				// 				// path\shell\open\ddeexec = [open("%1")]
  				// 				strTemp.Format(_afxShellOpenFmt, (LPCTSTR)strFileTypeId,
  				// 					(LPCTSTR)_afxDDEExec);
  				// 				if (!IUISetRegKey(strTemp, _afxDDEOpen))
  				// 					continue;       // just skip it
  
  				if (bCompat)
  				{
  					// path\shell\print\ddeexec = [print("%1")]
  					strTemp.Format(_afxShellPrintFmt, (LPCTSTR)strFileTypeId,
  						(LPCTSTR)_afxDDEExec);
  					if (!IUISetRegKey(strTemp, _afxDDEPrint, NULL))
  					{
  						continue;    // just skip it
  					}
  
  					// path\shell\printto\ddeexec = [printto("%1","%2","%3","%4")]
  					strTemp.Format(_afxShellPrintToFmt, (LPCTSTR)strFileTypeId,
  						(LPCTSTR)_afxDDEExec);
  					if (!IUISetRegKey(strTemp, _afxDDEPrintTo, NULL))
  					{
  						continue;    // just skip it
  					}
  
  					// path\shell\open\command = path /dde
  					// path\shell\print\command = path /dde
  					// path\shell\printto\command = path /dde
  					strOpenCommandLine += _afxDDEArg;
  					strPrintCommandLine += _afxDDEArg;
  					strPrintToCommandLine += _afxDDEArg;
  				}
  				else
  				{
  					strOpenCommandLine += _afxOpenArg;
  				}
  			}
  			else
  			{
  				// path\shell\open\command = path filename
  				// path\shell\print\command = path /p filename
  				// path\shell\printto\command = path /pt filename printer driver port
  				strOpenCommandLine += _afxOpenArg;
  				if (bCompat)
  				{
  					strPrintCommandLine += _afxPrintArg;
  					strPrintToCommandLine += _afxPrintToArg;
  				}
  			}
  
  			// path\shell\open\command = path filename
  			strTemp.Format(_afxShellOpenFmt, (LPCTSTR)strFileTypeId,
  				(LPCTSTR)_afxCommand);
  			if (!IUISetRegKey(strTemp, strOpenCommandLine, NULL))
  			{
  				continue;    // just skip it
  			}
  
  			if (bCompat)
  			{
  				// path\shell\print\command = path /p filename
  				strTemp.Format(_afxShellPrintFmt, (LPCTSTR)strFileTypeId,
  					(LPCTSTR)_afxCommand);
  				if (!IUISetRegKey(strTemp, strPrintCommandLine, NULL))
  				{
  					continue;    // just skip it
  				}
  
  				// path\shell\printto\command = path /pt filename printer driver port
  				strTemp.Format(_afxShellPrintToFmt, (LPCTSTR)strFileTypeId,
  					(LPCTSTR)_afxCommand);
  				if (!IUISetRegKey(strTemp, strPrintToCommandLine, NULL))
  				{
  					continue;    // just skip it
  				}
  			}
  
  			pTemplate->GetDocString(strFilterExt, CDocTemplate::filterExt);
  			if (!strFilterExt.IsEmpty())
  			{
  				ASSERT(strFilterExt[0] == '.');
  
  				LONG lSize = _MAX_PATH * 2;
  				LONG lResult = ::RegQueryValue(HKEY_CLASSES_ROOT, strFilterExt,
  						strTemp.GetBuffer(lSize), &lSize);
  				strTemp.ReleaseBuffer();
  
  				if (lResult != ERROR_SUCCESS || strTemp.IsEmpty() ||
  					strTemp == strFileTypeId)
  				{
  					// no association for that suffix
  					if (!IUISetRegKey(strFilterExt, strFileTypeId, NULL))
  					{
  						continue;
  					}
  
  					if (bCompat)
  					{
  						strTemp.Format(_afxShellNewFmt, (LPCTSTR)strFilterExt);
  						(void)IUISetRegKey(strTemp, _afxShellNewValue, _afxShellNewValueName);
  					}
  				}
  			}
  		}
  	}
  }
  