Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / C++

Project Line Counter Add-In v2.10 for VS.NET and VC6

Rate me:
Please Sign up or sign in to vote.
4.92/5 (38 votes)
29 Jun 2003 447.9K   5.3K   142  
Get statistics about your source code with a click of a button
/***************************************************************************/
/* NOTE:                                                                   */
/* This document is copyright (c) by Oz Solomonovich, and is bound by the  */
/* MIT open source license (www.opensource.org/licenses/mit-license.html). */
/* See License.txt for more information.                                   */
/***************************************************************************/

// Connect.cpp : Implementation of CConnect
#include "stdafx.h"

#ifdef TARGET_VC7

#include "LineCount.h"
#include "ConnectVC7.h"
#include "WorkspaceInfo.h"
#include "ResultsDlg.h"
#include "Config.h"

const wchar_t * const TOOLBAR_NAME      = L"PLC (Project Line Counter)";

const wchar_t * const COMMAND_NAME_FULL = L"LineCount.CountLines";
const wchar_t * const COMMAND_NAME      = L"CountLines";
const wchar_t * const COMMAND_TEXT      = L"Count Lines";
const wchar_t * const COMMAND_TIP       = L"Count lines of code in your projects";

// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
//   1) You moved this project to a computer other than which is was originally created on.
//   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
//   3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup project 
// by right clicking the project in the Solution Explorer, then choosing install.


// CConnect
STDMETHODIMP CConnect::OnConnection(IDispatch *pApplication, AddInDesignerObjects::ext_ConnectMode ConnectMode, IDispatch *pAddInInst, SAFEARRAY ** /*custom*/ )
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    HRESULT hr = S_OK;
    pApplication->QueryInterface(__uuidof(EnvDTE::_DTE), (LPVOID*)&m_pDTE);
    pAddInInst->QueryInterface(__uuidof(EnvDTE::AddIn), (LPVOID*)&m_pAddInInstance);

    OnAddinConnect();

    if(ConnectMode == 5 /* AddInDesignerObjects::ext_cm_UISetup */)
    {
        CComPtr<EnvDTE::Commands> pCommands;
        CComPtr<EnvDTE::Command> pCreatedCommand;

        hr = m_pDTE->get_Commands(&pCommands);

        if (SUCCEEDED(hr))
        {
            hr = pCommands->Item(CComVariant(COMMAND_NAME_FULL), 0, 
                &pCreatedCommand);

            // remove command if it's already there
            if (SUCCEEDED(hr)  &&  pCreatedCommand)
            {
                pCreatedCommand->Delete();
                pCreatedCommand = NULL;
            }

            // add our command
            hr = pCommands->AddNamedCommand(
                m_pAddInInstance, 
                CComBSTR(COMMAND_NAME), 
                CComBSTR(COMMAND_TEXT), 
                CComBSTR(COMMAND_TIP), 
                VARIANT_FALSE, 
                IDB_LCGO,  // MSO: 127
                NULL, 
                EnvDTE::vsCommandStatusSupported+EnvDTE::vsCommandStatusEnabled, 
                &pCreatedCommand);

            if (SUCCEEDED(hr)  &&  pCreatedCommand)
            {
                CComPtr<Office::_CommandBars>      pCommandBars;
                CComPtr<Office::CommandBarControl> pCommandBarControl;
                CComQIPtr<Office::CommandBar>      pToolbar;

                hr = m_pDTE->get_CommandBars(&pCommandBars);
                if (SUCCEEDED(hr))
                {
                    // Get our toolbar
                    hr = pCommandBars->get_Item(CComVariant(TOOLBAR_NAME), 
                        &pToolbar);
                    if (!SUCCEEDED(hr))
                    {
                        // haven't created the toolbar yet
                        CComPtr<IDispatch> pDisp;
                        hr = pCommands->AddCommandBar((LPWSTR)TOOLBAR_NAME,
                            EnvDTE::vsCommandBarTypeToolbar, NULL, 0, 
                            &pDisp);        
                        if (SUCCEEDED(hr))
                        {
                            pToolbar = pDisp;
                        }
                        if (pToolbar == NULL)
                        {
                            hr = E_FAIL;
                        }
                    }
                    else
                    {
                        // make sure visible
                        pToolbar->put_Visible(VARIANT_TRUE);
                    }
                }
                if (SUCCEEDED(hr))
                {
                    // this causes a memory leak -- don't know why
                    hr = pCreatedCommand->AddControl(pToolbar, 1, 
                        &pCommandBarControl);
                    if (SUCCEEDED(hr))
                    {
                        // make sure it's image only
                        CComQIPtr<Office::_CommandBarButton> 
                            pButton(pCommandBarControl);
                        if (pButton) pButton->put_Style(Office::msoButtonIcon);
                    }
                }
            }
        }
        
        return hr;
    }

    if (g_pWorkspaceInfo == NULL)
    {
        InitializeWorkspaceInfo_VC7(m_pDTE);
    }

    return hr;
}

STDMETHODIMP CConnect::OnDisconnection(AddInDesignerObjects::ext_DisconnectMode /*RemoveMode*/, SAFEARRAY ** /*custom*/ )
{
    OnAddinDisconnect();
    
    m_pDTE = NULL;
    return S_OK;
}

STDMETHODIMP CConnect::OnAddInsUpdate (SAFEARRAY ** /*custom*/ )
{
    return S_OK;
}

STDMETHODIMP CConnect::OnStartupComplete (SAFEARRAY ** /*custom*/ )
{
    return S_OK;
}

STDMETHODIMP CConnect::OnBeginShutdown (SAFEARRAY ** /*custom*/ )
{
    return S_OK;
}

STDMETHODIMP CConnect::QueryStatus(BSTR bstrCmdName, EnvDTE::vsCommandStatusTextWanted NeededText, EnvDTE::vsCommandStatus *pStatusOption, VARIANT *pvarCommandText)
{
  if(NeededText == EnvDTE::vsCommandStatusTextWantedNone)
    {
      if(!_wcsicmp(bstrCmdName, COMMAND_NAME_FULL))
      {
          *pStatusOption = (EnvDTE::vsCommandStatus)
              (EnvDTE::vsCommandStatusEnabled |
               EnvDTE::vsCommandStatusSupported);
      }
  }
    return S_OK;
}

STDMETHODIMP CConnect::Exec(BSTR bstrCmdName, EnvDTE::vsCommandExecOption ExecuteOption, VARIANT * /*pvarVariantIn*/, VARIANT * /*pvarVariantOut*/, VARIANT_BOOL *pvbHandled)
{
    *pvbHandled = VARIANT_FALSE;
    if(ExecuteOption == EnvDTE::vsCommandExecOptionDoDefault)
    {
        if(!_wcsicmp(bstrCmdName, COMMAND_NAME_FULL))
        {
            AFX_MANAGE_STATE(AfxGetStaticModuleState());
            try
            {
                CResultsDlg dlg(g_pWorkspaceInfo);
                dlg.DoModal();
            }
            catch (...)
            {
                AfxMessageBox("Exception caught in LineCounter", 
                    MB_OK | MB_ICONEXCLAMATION);
            }
            *pvbHandled = VARIANT_TRUE;
            return S_OK;
        }
    }
    return S_OK;
}

#endif // TARGET_VC7

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Experion
Canada Canada
You may know Oz from his WndTabs days. Oz has long since left client side development to work on web technologies and to consult in the R&D management field.

Comments and Discussions