home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / addins / autobld / dsaddin.cpp < prev    next >
C/C++ Source or Header  |  1998-04-02  |  5KB  |  159 lines

  1. // AddInMod.cpp : implementation file
  2. //
  3.  
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6.  
  7. #include "stdafx.h"
  8. #include "AutoBld.h"
  9. #include "DSAddIn.h"
  10. #include "Commands.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. // This is called when the user first loads the add-in, and on start-up
  19. //  of each subsequent Developer Studio session
  20. STDMETHODIMP CDSAddIn::OnConnection(IApplication* pApp, VARIANT_BOOL bFirstTime,
  21.         long dwCookie, VARIANT_BOOL* OnConnection)
  22. {
  23.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  24.  
  25.     // Store info passed to us
  26.     IApplication* pApplication = NULL;
  27.     if (FAILED(pApp->QueryInterface(IID_IApplication, (void**) &pApplication))
  28.         || pApplication == NULL)
  29.     {
  30.         *OnConnection = VARIANT_FALSE;
  31.         return S_OK;
  32.     }
  33.  
  34.     m_dwCookie = dwCookie;
  35.  
  36.     // Create command dispatch, send info back to DevStudio
  37.     CCommandsObj::CreateInstance(&m_pCommands);
  38.     m_pCommands->AddRef();
  39.  
  40.     // The QueryInterface above AddRef'd the Application object.  It will
  41.     //  be Release'd in CCommand's destructor.
  42.     m_pCommands->SetApplicationObject(pApplication);
  43.  
  44.     // (see stdafx.h for the definition of VERIFY_OK)
  45.  
  46.     VERIFY_OK(pApplication->SetAddInInfo((long) AfxGetInstanceHandle(),
  47.         (LPDISPATCH) m_pCommands, IDR_TOOLBAR_MEDIUM, IDR_TOOLBAR_LARGE, m_dwCookie));
  48.  
  49.     // Inform DevStudio of the commands we implement
  50.     {
  51.         // The command name should not be localized to other languages.  The
  52.         //  tooltip, command description, and other strings related to this
  53.         //  command are stored in the string table (IDS_CMD_STRING) and should
  54.         //  be localized.
  55.         LPCTSTR szCommand = _T("AutoBuildTest");
  56.         VARIANT_BOOL bRet;
  57.         CString strCmdString;
  58.         strCmdString.LoadString(IDS_CMD_STRING);
  59.         strCmdString = szCommand + strCmdString;
  60.         CComBSTR bszCmdString(strCmdString);
  61.         CComBSTR bszMethod(_T("AutoBuildTest"));
  62.         CComBSTR bszCmdName(szCommand);
  63.         VERIFY_OK(pApplication->AddCommand(bszCmdString, bszMethod, 0, m_dwCookie, &bRet));
  64.         if (bRet == VARIANT_FALSE)
  65.         {
  66.             // AddCommand failed because a command with this name already
  67.             //  exists.  You may try adding your command under a different name.
  68.             //  Or, you can fail to load as we will do here.
  69.             *OnConnection = VARIANT_FALSE;
  70.             return S_OK;
  71.         }
  72.  
  73.         // Add toolbar buttons only if this is the first time the add-in
  74.         //  is being loaded.  Toolbar buttons are automatically remembered
  75.         //  by Developer Studio from session to session, so we should only
  76.         //  add the toolbar buttons once.
  77.         if (bFirstTime == VARIANT_TRUE)
  78.         {
  79.             VERIFY_OK(pApplication->
  80.                 AddCommandBarButton(dsGlyph, bszCmdName, m_dwCookie));
  81.         }
  82.     }
  83.  
  84.     {
  85.         // The command name should not be localized to other languages.  The
  86.         //  tooltip, command description, and other strings related to this
  87.         //  command are stored in the string table (IDS_CMD_STRING) and should
  88.         //  be localized.
  89.         LPCTSTR szCommand = _T("AutoBuildConfigure");
  90.         VARIANT_BOOL bRet;
  91.         CString strCmdString;
  92.         strCmdString.LoadString(IDS_CMD_STRING3);
  93.         strCmdString = szCommand + strCmdString;
  94.         CComBSTR bszCmdString(strCmdString);
  95.         CComBSTR bszMethod(_T("AutoBuildConfigure"));
  96.         CComBSTR bszCmdName(szCommand);
  97.         pApplication->AddCommand(bszCmdString, bszMethod, 1, m_dwCookie, &bRet);
  98.         if (bRet == VARIANT_FALSE)
  99.         {
  100.             // AddCommand failed because a command with this name already
  101.             //  exists.  You may try adding your command under a different name.
  102.             //  Or, you can fail to load as we will do here.
  103.             *OnConnection = VARIANT_FALSE;
  104.             return S_OK;
  105.         }
  106.  
  107.         // Add toolbar buttons only if this is the first time the add-in
  108.         //  is being loaded.  Toolbar buttons are automatically remembered
  109.         //  by Developer Studio from session to session, so we should only
  110.         //  add the toolbar buttons once.
  111.         if (bFirstTime == VARIANT_TRUE)
  112.         {
  113.             VERIFY(SUCCEEDED(pApplication->
  114.                 AddCommandBarButton(dsGlyph, bszCmdName, m_dwCookie)));
  115.         }
  116.     }
  117.     {
  118.         // The command name should not be localized to other languages.  The
  119.         //  tooltip, command description, and other strings related to this
  120.         //  command are stored in the string table (IDS_CMD_STRING) and should
  121.         //  be localized.
  122.         LPCTSTR szCommand = _T("AutoBuild");
  123.         VARIANT_BOOL bRet;
  124.         CString strCmdString;
  125.         strCmdString.LoadString(IDS_CMD_STRING2);
  126.         strCmdString = szCommand + strCmdString;
  127.         CComBSTR bszCmdString(strCmdString);
  128.         CComBSTR bszMethod(_T("AutoBuild"));
  129.         CComBSTR bszCmdName(szCommand);
  130.         pApplication->AddCommand(bszCmdString, bszMethod, 0, m_dwCookie, &bRet);
  131.         if (bRet == VARIANT_FALSE)
  132.         {
  133.             // AddCommand failed because a command with this name already
  134.             //  exists.  You may try adding your command under a different name.
  135.             //  Or, you can fail to load as we will do here.
  136.             *OnConnection = VARIANT_FALSE;
  137.             return S_OK;
  138.         }
  139.     }
  140.  
  141.  
  142.     *OnConnection = VARIANT_TRUE;
  143.     return S_OK;
  144. }
  145.  
  146. // This is called on shut-down, and also when the user unloads the add-in
  147. STDMETHODIMP CDSAddIn::OnDisconnection(VARIANT_BOOL bLastTime)
  148. {
  149.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  150.  
  151.     m_pCommands->UnadviseFromEvents();
  152.     m_pCommands->Release();
  153.     m_pCommands = NULL;
  154.  
  155.     // TODO: Perform any cleanup work here
  156.  
  157.     return S_OK;
  158. }
  159.