Click here to Skip to main content
15,879,239 members
Articles / Desktop Programming / MFC

Resource ID Organiser Add-In for Visual C++ 5.0/6.0/.NET

Rate me:
Please Sign up or sign in to vote.
4.98/5 (71 votes)
10 Jan 2005CPOL25 min read 529.7K   12.1K   201  
An application/add-in to organise and renumber resource symbol IDs
/************************************************************************
 *
 *                 Resource ID Organiser Core Library
 *
 * (c) Copyright 2000-2004 by Anna-Jayne Metcalfe (resorg@annasplace.me.uk)
 *                         All rights reserved.
 *
 ************************************************************************
 *                                                                       
 *  Filename    : ResOrgSymbolsDocMultiFile.h
 *
 *  Description : CResOrgSymbolsDocMultiFile - document class for multiple
 *                resource symbol files
 *                
 *  Compiler    : Microsoft Visual C++ 6.0, Service Pack 3 or later
 *                Microsoft Visual C++ .NET 2003
 *                                                                       
 *  Target                                                               
 *  Environment : Windows 98/NT/2000/XP
 *
 *  NOTE:
 *
 *    This software is provided "as is" free for personal use. All
 *    title and copyrights in and to the software, including but not
 *    limited to any images, text, etc. incorporated into it, are
 *    owned by Anna-Jayne Metcalfe, except where acknowledged otherwise.
 *
 *    Your may freely to use this code in your own products, PROVIDED
 *    this notice is not removed or modified.
 *
 *
 *    Visit http://www.annasplace.me.uk/resorg for latest updates
 *
 ************************************************************************
 *
 *   MODIFICATION HISTORY:
 *
 *           This is a controlled document. See project configuration
 *           control tool for latest version and full version history.
 *
 *    $Archive: /Projects/AddIns/ResOrg/ResOrgCore/ResOrgSymbolsDocMultiFile.cpp $
 *   $Revision: 11 $
 *       $Date: 29/08/04 22:00 $
 *     $Author: Anna $
 *
 *    $History: ResOrgSymbolsDocMultiFile.cpp $
 * 
 * *****************  Version 11  *****************
 * User: Anna         Date: 29/08/04   Time: 22:00
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 *  CResOrgSymbolsDocMultiFile::Serialize() now checks if the file being
 * saved exists before assuming it is read only
 * 
 * *****************  Version 10  *****************
 * User: Anna         Date: 29/08/04   Time: 21:25
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Removed CResOrgSymbolsDocMultiFile::DoOverwritePrompt(). This is now a
 * global function in ResOrgCore-Globals.cpp
 * 
 * *****************  Version 9  *****************
 * User: Anna         Date: 26/03/03   Time: 14:09
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Added CResOrgSymbolsDocMultiFile::GetDisplayTitle() to choose the title
 * displayed for a multi-file Symbols Display
 * 
 * *****************  Version 8  *****************
 * User: Anna         Date: 6/03/03    Time: 23:58
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Minor mod to CResOrgSymbolsDocMultiFile::AddFile()
 * 
 * *****************  Version 7  *****************
 * User: Anna         Date: 4/03/03    Time: 20:50
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * CResOrgSymbolsDocMultiFile::AddFiles() now displays a progress dialog
 * 
 * *****************  Version 6  *****************
 * User: Anna         Date: 15/02/03   Time: 20:49
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Changed big BOOLs into little bools
 * 
 * *****************  Version 5  *****************
 * User: Anna         Date: 28/01/03   Time: 21:19
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Improved multi-file support by allowing files to be Added/Removed from
 * a multi-file display via the File Properties Dialog
 * 
 * *****************  Version 4  *****************
 * User: Anna         Date: 19/01/03   Time: 17:19
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * 1.  Added CResOrgSymbolsDocMultiFile::GetMultiFileSymbolManager()
 * 2.  Added an override of CResOrgSymbolsDoc()::Set
 * 3.  Implemented multi-file saving
 * 
 * *****************  Version 3  *****************
 * User: Anna         Date: 3/01/03    Time: 15:51
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Added a "Files" tab to the Symbol File Properties dialog. Not fully
 * functional yet
 * 
 * *****************  Version 2  *****************
 * User: Anna         Date: 3/01/03    Time: 0:09
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Speeded up multi-file loading slightly
 * 
 * *****************  Version 1  *****************
 * User: Anna         Date: 2/01/03    Time: 0:03
 * Created in $/Projects/AddIns/ResOrg/ResOrgCore
 * 
 *
 * $Nokeywords: $
 *
 ************************************************************************/


#include "StdAfx.h"
#include "ResOrgCore_Priv.h"
#include "ResOrgCore_Globals.h"

#include "ResourceSymbolManagerMultiFile.h"
#include "ResourceSymbolFilePropertySheet.h"
#include "ResOrgProgressDlg.h"

#include "ResOrgSymbolsDocMultiFile.h"




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


/////////////////////////////////////////////////////////////////////////////
// CResOrgSymbolsDocMultiFile

IMPLEMENT_DYNCREATE(CResOrgSymbolsDocMultiFile, CResOrgSymbolsDocMultiFile_BASE)

BEGIN_MESSAGE_MAP(CResOrgSymbolsDocMultiFile, CResOrgSymbolsDocMultiFile_BASE)

	ON_COMMAND(				ID_FILE_CLOSE,					OnFileClose)
	ON_COMMAND(				ID_FILE_SAVE,					OnFileSave)
	ON_UPDATE_COMMAND_UI(	ID_FILE_SAVE,					OnUpdateFileSave)
	ON_UPDATE_COMMAND_UI(	ID_FILE_SAVE_AS,				OnUpdateFileSaveAs)
	ON_COMMAND(				ID_FILE_CHANGED,				OnCmdFileChanged)
	ON_COMMAND(				ID_FILE_PROPERTIES,				OnCmdFileProperties)
	ON_COMMAND(				ID_TOOLS_RENUMBER,				OnCmdRenumberSymbols)
	ON_UPDATE_COMMAND_UI(	ID_TOOLS_RENUMBER,				OnUpdateRenumberSymbols)

END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CResOrgSymbolsDocMultiFile construction/destruction

CResOrgSymbolsDocMultiFile::CResOrgSymbolsDocMultiFile(void)
{
}


CResOrgSymbolsDocMultiFile::~CResOrgSymbolsDocMultiFile(void)
{
}


/////////////////////////////////////////////////////////////////////////////
// CResOrgSymbolsDocMultiFile overrides

BOOL CResOrgSymbolsDocMultiFile::OnNewDocument(void)
{
	BOOL bResult = CResOrgSymbolsDocMultiFile_BASE::OnNewDocument();

	return bResult;
}


BOOL CResOrgSymbolsDocMultiFile::OnOpenDocument(LPCTSTR lpszPathName)
{
	BOOL bResult = CResOrgSymbolsDocMultiFile_BASE::OnOpenDocument(lpszPathName);
//	if (bResult)
//	{
//		StartFileChangeMonitor(lpszPathName);
//	}
	return bResult;
}


BOOL CResOrgSymbolsDocMultiFile::OnSaveDocument(LPCTSTR lpszPathName)
{
	BOOL bResult = CResOrgSymbolsDocMultiFile_BASE::OnSaveDocument(lpszPathName);

//	if (bResult)
//	{
//		m_pSymbolManager->SetModifiedFlag(FALSE);
//
//		StartFileChangeMonitor(lpszPathName);
//
//		UpdateAllViews(NULL, SYM_REFRESH);
//	}
	return bResult;
}


void CResOrgSymbolsDocMultiFile::OnCloseDocument(void)
{
//	StopFileChangeMonitor();

	CResOrgSymbolsDocMultiFile_BASE::OnCloseDocument();
}


void CResOrgSymbolsDocMultiFile::DeleteContents(void) 
{
//	if (NULL != m_pSymbolManager)
//	{
//		m_pSymbolManager->RemoveAll();
//	}
//	else
//	{
//		VERIFY( CreateSymbolManager() );
//	}
//
//	UpdateAllViews(NULL, SYM_REFRESH);

	CResOrgSymbolsDocMultiFile_BASE::DeleteContents();
}


bool CResOrgSymbolsDocMultiFile::Set(	CResourceSymbol* pSymbol,
										const CString& sName,
										UINT uValue,
										bool bReadOnly /*= false*/)
{
	CResourceSymbolList listSymbols;

	m_pSymbolManager->GetSymbols(	pSymbol->GetValue(),
									listSymbols);

	// Work out which symbols are affected so we can issue the right view hints
	CResourceSymbolList listSymbolsWithNewValue;

	m_pSymbolManager->GetSymbols(	uValue,
									listSymbolsWithNewValue);

	listSymbols.AddTail(&listSymbolsWithNewValue);

	bool bResult = m_pSymbolManager->Set(	pSymbol,
											sName,
											uValue,
											bReadOnly);
	if (bResult)
	{
		CResourceSymbolManager* pMngr = reinterpret_cast<CResourceSymbolManager*>
														( pSymbol->GetUserData() );
		ASSERT(NULL != pMngr);

		if (Options.AutoFixNextSymbolValues() && (NULL != pMngr) )
		{
			pMngr->FixNextSymbolValues();
		}

		if (m_pSymbolManager->IsModified())
		{
			if (NULL != pMngr)
			{
				pMngr->SetModifiedFlag(true);
			}
			SetModifiedFlag(TRUE);
		}

		POSITION pos = listSymbols.GetHeadPosition();
		while (NULL != pos)
		{
			CResourceSymbol* pSymbol = listSymbols.GetNext(pos);
			if (NULL != pSymbol)
			{
				UpdateAllViews(NULL, SYM_CHANGED, (CObject*)pSymbol);
			}
		}
	}
	return bResult;
}


/////////////////////////////////////////////////////////////////////////////
// CResOrgSymbolsDocMultiFile serialization

void CResOrgSymbolsDocMultiFile::Serialize(CArchive& ar)
{
	CResOrgSymbolsDocMultiFile_BASE::Serialize(ar);

//	if (NULL != m_pSymbolManager)
//	{
//		m_pSymbolManager->Serialize(ar);
//	}
}


/////////////////////////////////////////////////////////////////////////////
// CResOrgSymbolsDocMultiFile diagnostics

#ifdef _DEBUG
void CResOrgSymbolsDocMultiFile::AssertValid(void) const
{
	CResOrgSymbolsDocMultiFile_BASE::AssertValid();
}


void CResOrgSymbolsDocMultiFile::Dump(CDumpContext& dc) const
{
	CResOrgSymbolsDocMultiFile_BASE::Dump(dc);
}
#endif //_DEBUG


/////////////////////////////////////////////////////////////////////////////
// CResOrgSymbolsDocMultiFile operations

CResourceSymbolManagerMultiFile* CResOrgSymbolsDocMultiFile::GetMultiFileSymbolManager(void) const
{
	return static_cast<CResourceSymbolManagerMultiFile*>(m_pSymbolManager);
}


CString CResOrgSymbolsDocMultiFile::GetDisplayTitle(const CStringArray& arraySymbolFilePathNames, int nMaxFiles /*= 2*/)
{
	CString sTitle;

	for (int n = 0; n <  arraySymbolFilePathNames.GetSize(); n++)
	{
		 CNGSplitPath split(arraySymbolFilePathNames[n]);
		 
		 sTitle += split.GetFileName() + split.GetExtension() + _T(", ");

		 if (n + 1 >= nMaxFiles)
		 {
			 if (nMaxFiles < arraySymbolFilePathNames.GetSize() )
			 {
				 sTitle += _T("...");			// Indicate there's more...
			 }
			 break;
		 }
	}
	sTitle.TrimRight( _T(", ") );

	return sTitle;
}


bool CResOrgSymbolsDocMultiFile::AddFiles(const CStringArray& arraySymbolFilePathNames)
{
	ASSERT(NULL != m_pSymbolManager);

	bool bResult = false;

	if ( (NULL != m_pSymbolManager) && (arraySymbolFilePathNames.GetSize() > 0) )
	{	
		CResOrgProgressDlg dlg;

		dlg.Initialise( _T("Open Projects"), arraySymbolFilePathNames.GetSize());
		dlg.Create();

		for (int n = 0; n < arraySymbolFilePathNames.GetSize(); n++)
		{
			CString sPathName = arraySymbolFilePathNames[n];


			if (!sPathName.IsEmpty() )
			{
				CString sCompactedPath;

				LPTSTR pszCompactedPath = sCompactedPath.GetBuffer(_MAX_PATH);

				VERIFY(::PathCompactPathEx(	pszCompactedPath,
											sPathName,
											40,
											0) );

				sCompactedPath.ReleaseBuffer();

				CString sMsg;
				sMsg.Format( _T("Opening <b>%s</b> "), sCompactedPath);

				dlg.SetMessage(sMsg);

				if (NULL != static_cast<CResourceSymbolManagerMultiFile*>(m_pSymbolManager)->AddSymbolFile(sPathName) )
				{
					bResult = true;
				}
			}
		}
		if (bResult)
		{
			m_pSymbolManager->SortByValue();

			m_pSymbolManager->CountConflicts();

			UpdateAllViews(NULL, SYM_REFRESH);
		}
		dlg.DestroyWindow();
	}
	return bResult;
}


/////////////////////////////////////////////////////////////////////////////
// CResOrgSymbolsDocMultiFile implementation

bool CResOrgSymbolsDocMultiFile::CreateSymbolManager(void)
{
	if (NULL == m_pSymbolManager)
	{
		m_pSymbolManager = new CResourceSymbolManagerMultiFile;

		return (NULL != m_pSymbolManager);
	}
	return false;
}


bool CResOrgSymbolsDocMultiFile::StartFileChangeMonitor(const CString& sPathName)
{
	UNREFERENCED_PARAMETER(sPathName);

/*	if (!m_FileChangeMonitor.IsMonitoring())
	{
		POSITION pos = GetFirstViewPosition();
		if (pos != NULL)
		{
			CView* pView = GetNextView(pos);

			return m_FileChangeMonitor.Start(	sPathName,
												pView,
												WM_COMMAND,
												ID_FILE_CHANGED,
												THREAD_PRIORITY_LOWEST);
		}
	}
*/
	return false;
}


bool CResOrgSymbolsDocMultiFile::StopFileChangeMonitor(void)
{
//	if (m_FileChangeMonitor.IsMonitoring())
//	{
//		return m_FileChangeMonitor.Stop();
//	}
	return false;
}


// TODO: Move this to the ResOrgCore namespace as a helper function
// (CResOrgNETAddInToolWinFormView has the same code!)
bool CResOrgSymbolsDocMultiFile::Save(CResourceSymbolManager* pSymbols)
{
	bool bResult = false;

	CString sPathName = pSymbols->GetPathName();
	if (!sPathName.IsEmpty() )
	{
		DWORD dwAttrib = ::GetFileAttributes(sPathName);
		if (::FileExists(sPathName) && (dwAttrib & FILE_ATTRIBUTE_READONLY) )
		{
			UINT eResult = ::DoOverwritePrompt(sPathName);
			switch (eResult)
			{
			case IDYES:
				if (!::SetFileAttributes(sPathName, dwAttrib & ~FILE_ATTRIBUTE_READONLY) )
				{
					::AfxMessageBox(IDP_SYM_FILE_ERR_FILE_ATTRIBUTES, MB_OK | MB_ICONSTOP);

					return false;
				}
				break;

			case IDNO:
				{
					CFileDialog dlg(FALSE,									// Save As
									CString( (LPCTSTR)IDS_FILE_FILTER_RESOURCE_SYMBOL_FILES_EXT),
																			// Default extension
									NULL,									// Default filename
									OFN_PATHMUSTEXIST | OFN_ENABLESIZING,	// Flags
									CString( (LPCTSTR)IDS_FILE_FILTER_RESOURCE_SYMBOL_FILES),
																			// File filter
									::AfxGetMainWnd() );					// Parent window

					if (IDOK != dlg.DoModal())
					{
						return false;
					}
					sPathName = dlg.GetPathName();
				}
				break;

			case IDCANCEL:
				return false;

			default:
				ASSERT(FALSE);
				break;
			}
		}

		try
		{
			// Save the file to disk
			CStdioFile f(	sPathName,
							CFile::modeWrite | CFile::modeCreate | CFile::shareDenyWrite);

			// File opened OK
			//
			// Now create an archive, and an empty text buffer
			CArchive ar(&f, CArchive::store);

			pSymbols->Serialize(ar);

			ar.Flush();
			ar.Close();
			f.Flush();
			f.Close();

			bResult = true;
		}
		catch (CException* e)
		{
			TCHAR szCause[_MAX_PATH];
			e->GetErrorMessage(szCause, _MAX_PATH);
			CString sMsg;

			sMsg.Format(IDP_SYM_FILE_ERR_SAVE_FILE, szCause);

			TRACE1("%s\n", sMsg);
			::AfxMessageBox(sMsg, MB_OK | MB_ICONSTOP);

			e->Delete();
		}
	}
	return bResult;
}


/////////////////////////////////////////////////////////////////////////////
// CResOrgSymbolsDocMultiFile message handlers

void CResOrgSymbolsDocMultiFile::OnCmdFileChanged(void)
{
/*
	CString sPathName = GetPathName();

	CNGSplitPath split(sPathName);
	CString sFileName = split.GetFileName() + split.GetExtension();

	BOOL bModified	= IsModified();
	BOOL bPrompt	= bModified || !Options.AutoReloadFiles();
	BOOL bReload	= !bModified && Options.AutoReloadFiles();

	if (bPrompt)
	{
		CNGMessageBox dlg;

		dlg.FormatMsgEx(MAKEINTRESOURCE( bModified ? IDP_SYM_FILE_CHANGED_EX : IDP_SYM_FILE_CHANGED),
						_T("RTF"),
						sFileName);
		dlg.SetRtf();
		dlg.SetStandardIcon(IDI_WARNING);
		dlg.AddButton(IDYES, FALSE, FALSE, _T("&Yes") );
		dlg.AddButton(IDNO, TRUE, TRUE, _T("&No") );

		bReload =  (IDYES == dlg.DoModal() );
	}

	if (bReload)
	{
		OnOpenDocument(sPathName);
		UpdateAllViews(NULL, SYM_REFRESH);
	}
	else
	{
		StartFileChangeMonitor(sPathName);
	}
*/
}


void CResOrgSymbolsDocMultiFile::OnFileSave(void)
{
//	StopFileChangeMonitor();

	if (NULL != m_pSymbolManager)
	{
		CStringArray arrayPathNames;
		GetMultiFileSymbolManager()->GetSymbolFilePathNames(arrayPathNames);

		CStringArray arrayFailedSaves;

		for (int n = 0; n < arrayPathNames.GetSize(); n++)
		{
			CString sPathName = arrayPathNames[n];
			if (!sPathName.IsEmpty() )
			{
				CResourceSymbolManager* pMngr = GetMultiFileSymbolManager()->GetSymbolManager(sPathName);
				if ( (NULL != pMngr) && pMngr->IsModified() )
				{
					if (Save(pMngr) )
					{
						pMngr->SetModifiedFlag(false);
					}
					else
					{
						// We'll probably use this later
						arrayFailedSaves.Add(pMngr->GetPathName() );
					}
				}
			}
		}
		bool bModified = (arrayFailedSaves.GetSize() > 0);

		SetModifiedFlag(bModified);

		m_pSymbolManager->SetModifiedFlag(bModified);		// This really should be linked to the document...
	}
}


void CResOrgSymbolsDocMultiFile::OnUpdateFileSave(CCmdUI* pCmdUI)
{
	BOOL bEnable = FALSE;

	if (NULL != GetMultiFileSymbolManager() )
	{
		bEnable = (GetMultiFileSymbolManager()->GetSymbolFileCount() > 0);
	}
	pCmdUI->Enable(bEnable);
}


void CResOrgSymbolsDocMultiFile::OnUpdateFileSaveAs(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(FALSE);
}


// Renumber all symbols
void CResOrgSymbolsDocMultiFile::OnCmdRenumberSymbols(void) 
{
/*	// To renumber all the symbols in a given file we need to pick starting
	// values for each of the four symbol catgories identified by the _APS_NEXT...
	// symbols in each resource file.
	//
	// The Symbol Renumbering Wizard works this out by looking at the values
	// of existing symbols in the resource symbol file.
	//
	CRenumWizard Wizard;
	Wizard.Initialise(m_pSymbolManager);

	if (ID_WIZFINISH == Wizard.DoModal() )
	{
		if (m_pSymbolManager->IsModified() )
		{
			SetModifiedFlag(TRUE);
			UpdateAllViews(NULL, SYM_REFRESH);
		}
	}
*/
}


void CResOrgSymbolsDocMultiFile::OnUpdateRenumberSymbols(CCmdUI* pCmdUI)
{
//	pCmdUI->Enable(m_pSymbolManager->GetSymbolCount() > 0);
	pCmdUI->Enable(FALSE);		// Temporary
}


void CResOrgSymbolsDocMultiFile::OnCmdFileProperties(void) 
{
	CResourceSymbolFilePropertySheet dlg(IDP_SYM_FILE_PROPERTIES_CAPTION, ::AfxGetMainWnd() );

	dlg.Initialise(m_pSymbolManager, this);

	dlg.DoModal();

	if (m_pSymbolManager->IsModified() )
	{
		CStringArray arrayPathNames;
		GetMultiFileSymbolManager()->GetSymbolFilePathNames(arrayPathNames);
			
		SetTitle( GetDisplayTitle(arrayPathNames) );

		SetModifiedFlag(TRUE);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Riverblade Limited
United Kingdom United Kingdom
I haven't always written software for a living. When I graduated from Surrey University in 1989, it was with an Electronic Engineering degree, but unfortunately that never really gave me the opportunity to do anything particularly interesting (with the possible exception of designing Darth Vader's Codpiece * for the UK Army in 1990).
    * Also known as the Standard Army Bootswitch. But that's another story...
Since the opportunity arose to lead a software team developing C++ software for Avionic Test Systems in 1996, I've not looked back. More recently I've been involved in the development of subsea acoustic navigation systems, digital TV broadcast systems, port security/tracking systems, and most recently software development tools with my own company, Riverblade Ltd.

One of my personal specialities is IDE plug-in development. ResOrg was my first attempt at a plug-in, but my day to day work is with Visual Lint, an interactive code analysis tool environment with works within the Visual Studio and Eclipse IDEs or on build servers.

I love lots of things, but particularly music, photography and anything connected with history or engineering. I despise ignorant, intolerant and obstructive people - and it shows...I can be a bolshy cow if you wind me up the wrong way...Laugh | :laugh:

I'm currently based 15 minutes walk from the beach in Bournemouth on the south coast of England. Since I moved here I've grown to love the place - even if it is full of grockles in Summer!

Comments and Discussions