home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vile-src.zip / vile-8.1 / visvile / dsaddin.cpp < prev    next >
C/C++ Source or Header  |  1998-08-25  |  5KB  |  175 lines

  1. // AddInMod.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "VisVile.h"
  6. #include "DSAddIn.h"
  7. #include "Commands.h"
  8. #include "oleauto.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. // This is called when the user first loads the add-in, and on start-up
  17. //  of each subsequent Developer Studio session
  18. STDMETHODIMP CDSAddIn::OnConnection(IApplication* pApp, VARIANT_BOOL bFirstTime,
  19.         long dwCookie, VARIANT_BOOL* OnConnection)
  20. {
  21.     HRESULT hr;
  22.  
  23.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  24.     *OnConnection = VARIANT_FALSE;
  25.     hr            = oleauto_init();
  26.     if (FAILED(hr))
  27.         return (hr);
  28.     
  29.     // Store info passed to us
  30.     IApplication* pApplication = NULL;
  31.     hr = pApp->QueryInterface(IID_IApplication, (void**) &pApplication);
  32.     if (FAILED(hr))
  33.         return (ReportLastError(hr));
  34.  
  35.     m_dwCookie = dwCookie;
  36.  
  37.     // Create command dispatch, send info back to DevStudio
  38.     CCommandsObj::CreateInstance(&m_pCommands);
  39.     if (! m_pCommands)
  40.     {
  41.         ::MessageBox(NULL, 
  42.                "Unexpected OLE error:  CCommandsObj::CreateInstance() failed.",
  43.                      PROGNAM, 
  44.                      MB_OK|MB_ICONSTOP);
  45.         return (E_UNEXPECTED);
  46.     }
  47.     m_pCommands->AddRef();
  48.  
  49.     // The QueryInterface above AddRef'd the Application object.  It will
  50.     //  be Release'd in CCommand's destructor.
  51.     if (! m_pCommands->SetApplicationObject(pApplication))
  52.     {
  53.         ::MessageBox(NULL, 
  54.             "Unexpected OLE error:  CCommands::SetApplicationObject() failed.",
  55.                      PROGNAM, 
  56.                      MB_OK|MB_ICONSTOP);
  57.         return (E_UNEXPECTED);
  58.     }
  59.     hr = pApplication->SetAddInInfo((long) AfxGetInstanceHandle(),
  60.                                     (LPDISPATCH) m_pCommands, 
  61.                                     IDR_TOOLBAR_MEDIUM, 
  62.                                     IDR_TOOLBAR_LARGE, 
  63.                                     m_dwCookie);
  64.     if (FAILED(hr))
  65.         return (ReportLastError(hr));
  66.  
  67.     // Inform DevStudio of the commands we implement
  68.     if (! AddCommand(pApplication, 
  69.                      "VisVileConfig", 
  70.                      "VisVileConfigCmd",
  71.                      IDS_CMD_CONFIG, 
  72.                      0,
  73.                      bFirstTime))
  74.     {
  75.         return (E_UNEXPECTED);
  76.     }
  77.     if (! AddCommand(pApplication, 
  78.                      "VisVileEnable", 
  79.                      "VisVileEnableCmd",
  80.                      IDS_CMD_ENABLE, 
  81.                      1,
  82.                      bFirstTime))
  83.     {
  84.         return (E_UNEXPECTED);
  85.     }
  86.     if (! AddCommand(pApplication, 
  87.                      "VisVileDisable", 
  88.                      "VisVileDisableCmd",
  89.                      IDS_CMD_DISABLE, 
  90.                      2,
  91.                      bFirstTime))
  92.     {
  93.         return (E_UNEXPECTED);
  94.     }
  95.     if (! AddCommand(pApplication,
  96.                      "VisVileOpenDoc", 
  97.                      "VisVileOpenDocCmd",
  98.                      IDS_CMD_LOAD, 
  99.                      3,
  100.                      bFirstTime))
  101.     {
  102.         return (E_UNEXPECTED);
  103.     }
  104.     *OnConnection = VARIANT_TRUE;
  105.     return (S_OK);
  106. }
  107.  
  108. // This is called on shut-down, and also when the user unloads the add-in
  109. STDMETHODIMP CDSAddIn::OnDisconnection(VARIANT_BOOL bLastTime)
  110. {
  111.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  112.  
  113.     m_pCommands->UnadviseFromEvents();
  114.     m_pCommands->Release();
  115.     m_pCommands = NULL;
  116.     oleauto_exit();
  117.     return (S_OK);
  118. }
  119.  
  120. // Add a command to DevStudio
  121. // Creates a toolbar button for the command also.
  122. // 'MethodName' is the name of the methode specified in the .odl file
  123. // 'StrResId' the resource id of the descriptive string
  124. // 'GlyphIndex' the image index into the command buttons bitmap
  125. // Return true on success
  126. //
  127. bool CDSAddIn::AddCommand(IApplication *pApp, 
  128.                           char         *MethodName, 
  129.                           char         *CmdName,
  130.                           UINT         StrResId, 
  131.                           UINT         GlyphIndex,
  132.                           VARIANT_BOOL bFirstTime)
  133. {
  134.     VARIANT_BOOL bRet;
  135.     CString      CmdString, CmdText;
  136.  
  137.     CmdText.LoadString (StrResId);
  138.     CmdString = CmdName;
  139.     CmdString += CmdText;
  140.  
  141.     CComBSTR bszCmdString (CmdString);
  142.     CComBSTR bszMethod (MethodName);
  143.     CComBSTR bszCmdName (CmdName);
  144.  
  145.     pApp->AddCommand(bszCmdString, bszMethod, GlyphIndex, m_dwCookie, &bRet);
  146.     if (bRet == VARIANT_FALSE)
  147.     {
  148.         CString tmp;
  149.  
  150.         tmp  = "Duplicate command installation rejected\r\rDuplicate name: ";
  151.         tmp += MethodName;
  152.         ::MessageBox(NULL, 
  153.                      tmp,
  154.                      PROGNAM, 
  155.                      MB_OK|MB_ICONSTOP);
  156.     }
  157.  
  158.     // Add toolbar buttons only if this is the first time the add-in
  159.     //  is being loaded.  Toolbar buttons are automatically remembered
  160.     //  by Developer Studio from session to session, so we should only
  161.     //  add the toolbar buttons once.
  162.     if (bFirstTime == VARIANT_TRUE)
  163.     {
  164.         HRESULT hr;
  165.  
  166.         hr = pApp->AddCommandBarButton(dsGlyph, bszCmdName, m_dwCookie);
  167.         if (FAILED(hr))
  168.         {
  169.             (void) ReportLastError(hr);
  170.             bRet = VARIANT_FALSE;
  171.         }
  172.     }
  173.     return ((bRet == VARIANT_TRUE) ? TRUE : FALSE);
  174. }
  175.