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