Click here to Skip to main content
15,860,943 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 527.5K   12.1K   201  
An application/add-in to organise and renumber resource symbol IDs
/************************************************************************
 *
 *                   Resource ID Organiser Add-In
 *
 * (c) Copyright 2000-2002 by Anna-Jayne Metcalfe (resorg@annasplace.me.uk)
 *                         All rights reserved.
 *
 ************************************************************************
 *                                                                       
 *  Filename    : Commands.cpp
 *
 *  Description : CCommands - Add-In implementation class
 *
 *  Compiler    : Microsoft Visual C++ 6.0, Service Pack 3 or later
 *                                                                       
 *  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/ResOrgAddIn/Commands.cpp $
 *   $Revision: 13 $
 *       $Date: 25/11/02 16:52 $
 *     $Author: Anna $
 *
 *    $History: Commands.cpp $
 * 
 * *****************  Version 13  *****************
 * User: Anna         Date: 25/11/02   Time: 16:52
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * Changed website address in banner
 * 
 * *****************  Version 12  *****************
 * User: Anna         Date: 22/10/02   Time: 13:34
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * Changed name/mail address (at last!)
 * 
 * *****************  Version 11  *****************
 * User: Andy         Date: 2/08/02    Time: 17:03
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * Renamed CWorkspaceHelper to CVc6AutomationHelper, and moved it to the
 * ResOrgCore module
 * 
 * *****************  Version 10  *****************
 * User: Andy         Date: 12/06/02   Time: 14:56
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * Set the module state for the About Box to that of the Add-In's DLL
 * before invoking it. This ensures that the Add-In will be listed in the
 * "Modules" control
 * 
 * *****************  Version 9  *****************
 * User: Andy         Date: 1/25/02    Time: 2:53p
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * Added some (deactivated) code to identify the document being saved in
 * CCommands::XApplicationEvents::DocumentSave()
 * 
 * *****************  Version 8  *****************
 * User: Andy         Date: 12/29/01   Time: 11:42p
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * Got rid of compiler warnings
 * 
 * *****************  Version 7  *****************
 * User: Andy         Date: 8/07/01    Time: 17:06
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * The "Options" dialog and "About" Box are now parented by DevStudio's
 * main window rather than ResOrg's (this fixes a pretty nasty bug)
 * 
 * *****************  Version 6  *****************
 * User: Andy         Date: 26/05/01   Time: 21:00
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * Make Visual Studio the Active Window after executing ResOrg commands
 * (prevents odd behaviour when the ResOrg toolbar in DevStudio is
 * floated)
 * 
 * *****************  Version 5  *****************
 * User: Andy         Date: 24/04/01   Time: 0:19
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * Modified startup to prevent the wrong window being identified as the
 * DevStudio window if another window was active.
 * 
 * The active window is now checked when a command on the toolbar is
 * activated instead
 * 
 * *****************  Version 4  *****************
 * User: Andy         Date: 21/04/01   Time: 7:36
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * Added version expiry check
 * 
 * *****************  Version 3  *****************
 * User: Andy         Date: 29/11/00   Time: 18:37
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * 1.  Moved Office2KDlg code to its own DLL
 * 2.  Added file banners
 *
 * $Nokeywords: $
 *
 ************************************************************************/

// Commands.cpp : implementation file
//

#include "StdAfx.h"

#include "ResOrgAddIn.h"
#include "MainFrame.h"
#include "ResOrgProjectsDoc.h"
#include "ResOrgAddInApp.h"

#include "Commands.h"

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



//	CVc6AutomationHelper hides away the grot of talking to the COM interfaces
//	exposed by Visual Studio...
CVc6AutomationHelper Workspace;


/////////////////////////////////////////////////////////////////////////////
// CCommands

CCommands::CCommands(void)
{
	m_pApplication			= NULL;
	m_pApplicationEventsObj = NULL;
	m_pDebuggerEventsObj	= NULL;
}


CCommands::~CCommands(void)
{
	Workspace.SetApplication(NULL);

	ASSERT (m_pApplication != NULL);
	m_pApplication->Release();
}


void CCommands::SetApplicationObject(IApplication* pApplication)
{
	// This function assumes pApplication has already been AddRef'd
	// for us, which CDSAddIn did in its QueryInterface call
	// just before it called us.
	m_pApplication = pApplication;

	Workspace.SetApplication(m_pApplication);

	// Create Application event handlers
	XApplicationEventsObj::CreateInstance(&m_pApplicationEventsObj);
	m_pApplicationEventsObj->AddRef();
	m_pApplicationEventsObj->Connect(m_pApplication);
	m_pApplicationEventsObj->m_pCommands = this;

	// Create Debugger event handler
	CComPtr<IDispatch> pDebugger;
	if (SUCCEEDED(m_pApplication->get_Debugger(&pDebugger)) 
		&& pDebugger != NULL)
	{
		XDebuggerEventsObj::CreateInstance(&m_pDebuggerEventsObj);
		m_pDebuggerEventsObj->AddRef();
		m_pDebuggerEventsObj->Connect(pDebugger);
		m_pDebuggerEventsObj->m_pCommands = this;
	}
}


void CCommands::UnadviseFromEvents(void)
{
	ASSERT (m_pApplicationEventsObj != NULL);
	m_pApplicationEventsObj->Disconnect(m_pApplication);
	m_pApplicationEventsObj->Release();
	m_pApplicationEventsObj = NULL;

	if (m_pDebuggerEventsObj != NULL)
	{
		// Since we were able to connect to the Debugger events, we
		//  should be able to access the Debugger object again to
		//  unadvise from its events (thus the VERIFY_OK below--see stdafx.h).
		CComPtr<IDispatch> pDebugger;
		VERIFY_OK(m_pApplication->get_Debugger(&pDebugger));
		ASSERT (pDebugger != NULL);
		m_pDebuggerEventsObj->Disconnect(pDebugger);
		m_pDebuggerEventsObj->Release();
		m_pDebuggerEventsObj = NULL;
	}
}


HWND CCommands::GetDevStudioWindow(void) const
{
	HWND hWndDevStudio	= NULL;

	HWND hWndDesktop	= ::GetDesktopWindow();
	HWND hWnd = ::GetActiveWindow();

    while ( (NULL != hWnd)  &&  (hWnd != hWndDesktop) )
    {
        hWndDevStudio = hWnd;
        hWnd = ::GetParent(hWnd);
    }
	return hWndDevStudio;
}


/////////////////////////////////////////////////////////////////////////////
// Event handlers

// TODO: Fill out the implementation for those events you wish handle
//  Use m_pCommands->GetApplicationObject() to access the Developer
//  Studio Application object

// Application events

HRESULT CCommands::XApplicationEvents::BeforeBuildStart(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}


HRESULT CCommands::XApplicationEvents::BuildFinish(long nNumErrors, long nNumWarnings)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	UNREFERENCED_PARAMETER(nNumErrors);
	UNREFERENCED_PARAMETER(nNumWarnings);

	return S_OK;
}


HRESULT CCommands::XApplicationEvents::BeforeApplicationShutDown(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TO DO: Shut down main window

	return S_OK;
}


HRESULT CCommands::XApplicationEvents::DocumentOpen(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	UNREFERENCED_PARAMETER(theDocument);
	
	return S_OK;
}


HRESULT CCommands::XApplicationEvents::BeforeDocumentClose(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	UNREFERENCED_PARAMETER(theDocument);

	return S_OK;
}


HRESULT CCommands::XApplicationEvents::DocumentSave(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	UNREFERENCED_PARAMETER(theDocument);
	
/*
	// How to find out which file is being saved...
	IGenericDocument* pDocument = NULL;

	// Yum. Dontcha just *love* COM code?
	m_pCommands->GetApplicationObject()->get_ActiveDocument( (IDispatch **)&pDocument);
	if (NULL != pDocument)
	{
		CString sFullName = Workspace.GetFullName(pDocument);

		pDocument->Release();

		TRACE(" File %s saved\n", sFullName);
	}
*/
	return S_OK;
}


HRESULT CCommands::XApplicationEvents::NewDocument(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	UNREFERENCED_PARAMETER(theDocument);
	
	return S_OK;
}


HRESULT CCommands::XApplicationEvents::WindowActivate(IDispatch* theWindow)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	UNREFERENCED_PARAMETER(theWindow);
	
	return S_OK;
}


HRESULT CCommands::XApplicationEvents::WindowDeactivate(IDispatch* theWindow)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	UNREFERENCED_PARAMETER(theWindow);
	
	return S_OK;
}


HRESULT CCommands::XApplicationEvents::WorkspaceOpen(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TO DO: Get the pathname of the workspace and read it

	return S_OK;
}


HRESULT CCommands::XApplicationEvents::WorkspaceClose(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	return S_OK;
}


HRESULT CCommands::XApplicationEvents::NewWorkspace(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	return S_OK;
}


// Debugger event

HRESULT CCommands::XDebuggerEvents::BreakpointHit(IDispatch* pBreakpoint)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	UNREFERENCED_PARAMETER(pBreakpoint);
	
	return S_OK;
}


/////////////////////////////////////////////////////////////////////////////
// CCommands methods

STDMETHODIMP CCommands::ResOrgShowMethod(void) 
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE) );

	CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
	if (pMainFrame != NULL)
	{
		ASSERT_VALID(pMainFrame);

		#ifdef _RESORG_EXPIRY_DATE
			::DoVersionExpiryCheck();
		#endif

		pMainFrame->DoModal( GetDevStudioWindow() );
	}
	else
	{
		AfxMessageBox(	IDP_WND_CREATION_FAILURE,
						MB_OK | MB_ICONSTOP);
	}

#pragma warning(push, 3)
	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE) );
#pragma warning(pop)

	return S_OK;
}


STDMETHODIMP CCommands::ResOrgOptionsMethod(void) 
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE) );

	// Because this add-in is implemented as a Regular DLL with a
	// mainframe window the usual trick of calling EnableModeless()
	// isn't enough to ensure that modal dialogs behave correctly -
	// we also have to ensure that any dialogs are parented by
	// DevStudio's main window rather than our own...
	HWND hWndDevStudio = GetDevStudioWindow();
	CWnd* pWnd = CWnd::FromHandle(hWndDevStudio);

	Options.Configure(pWnd);

#pragma warning(push, 3)
	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE) );
#pragma warning(pop)

	return S_OK;
}


STDMETHODIMP CCommands::ResOrgAboutMethod(void) 
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE) );

	// Because this add-in is implemented as a Regular DLL with a
	// mainframe window the usual trick of calling EnableModeless()
	// isn't enough to ensure that modal dialogs behave correctly -
	// we also have to ensure that any dialogs are parented by
	// DevStudio's main window rather than our own...
	HWND hWndDevStudio = GetDevStudioWindow();
	CWnd* pWnd = CWnd::FromHandle(hWndDevStudio);
	
	CResOrgAboutBox aboutDlg(pWnd);

	aboutDlg.SetModuleState( ::AfxGetStaticModuleState() );
	aboutDlg.DoModal();

#pragma warning(push, 3)
	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE) );
#pragma warning(pop)

	return S_OK;
}

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