Add help on how to remap Caps Lock to Esc
[MacVim.git] / src / VisVim / DSAddIn.cpp
blobb16361b9039ead4c77252b92d8ed7684f2e9203e
1 #include "stdafx.h"
2 #include "VisVim.h"
3 #include "DSAddIn.h"
4 #include "Commands.h"
6 #ifdef _DEBUG
7 #define new DEBUG_NEW
8 #undef THIS_FILE
9 static char THIS_FILE[] = __FILE__;
11 #endif
13 // This is called when the user first loads the add-in, and on start-up
14 // of each subsequent Developer Studio session
15 STDMETHODIMP CDSAddIn::OnConnection (IApplication * pApp, VARIANT_BOOL bFirstTime,
16 long dwCookie, VARIANT_BOOL * OnConnection)
18 AFX_MANAGE_STATE (AfxGetStaticModuleState ());
19 *OnConnection = VARIANT_FALSE;
21 // Store info passed to us
22 IApplication *pApplication = NULL;
23 HRESULT hr;
25 hr = pApp->QueryInterface (IID_IApplication, (void **) &pApplication);
26 if (FAILED (hr))
28 ReportLastError (hr);
29 return E_UNEXPECTED;
31 if (pApplication == NULL)
33 ReportInternalError ("IApplication::QueryInterface");
34 return E_UNEXPECTED;
37 m_dwCookie = dwCookie;
39 // Create command dispatch, send info back to DevStudio
40 CCommandsObj::CreateInstance (&m_pCommands);
41 if (! m_pCommands)
43 ReportInternalError ("CCommandsObj::CreateInstance");
44 return E_UNEXPECTED;
46 m_pCommands->AddRef ();
48 // The QueryInterface above AddRef'd the Application object. It will
49 // be Release'd in CCommand's destructor.
50 m_pCommands->SetApplicationObject (pApplication);
52 hr = pApplication->SetAddInInfo ((long) AfxGetInstanceHandle (),
53 (LPDISPATCH) m_pCommands, IDR_TOOLBAR_MEDIUM, IDR_TOOLBAR_LARGE,
54 m_dwCookie);
55 if (FAILED (hr))
57 ReportLastError (hr);
58 return E_UNEXPECTED;
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;
78 *OnConnection = VARIANT_TRUE;
79 return S_OK;
82 // This is called on shut-down, and also when the user unloads the add-in
83 STDMETHODIMP CDSAddIn::OnDisconnection (VARIANT_BOOL bLastTime)
85 AFX_MANAGE_STATE (AfxGetStaticModuleState ());
87 m_pCommands->UnadviseFromEvents ();
88 m_pCommands->Release ();
89 m_pCommands = NULL;
91 return S_OK;
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
101 bool CDSAddIn::AddCommand (IApplication* pApp, char* MethodName, char* CmdName,
102 UINT StrResId, UINT GlyphIndex, VARIANT_BOOL bFirstTime)
104 CString CmdString;
105 CString CmdText;
107 CmdText.LoadString (StrResId);
108 CmdString = CmdName;
109 CmdString += CmdText;
111 CComBSTR bszCmdString (CmdString);
112 CComBSTR bszMethod (MethodName);
113 CComBSTR bszCmdName (CmdName);
115 // (see stdafx.h for the definition of VERIFY_OK)
117 VARIANT_BOOL bRet;
118 VERIFY_OK (pApp->AddCommand (bszCmdString, bszMethod, GlyphIndex,
119 m_dwCookie, &bRet));
120 if (bRet == VARIANT_FALSE)
122 // AddCommand failed because a command with this name already exists.
123 ReportInternalError ("IApplication::AddCommand");
124 return FALSE;
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));
134 return TRUE;
137 void ReportLastError (HRESULT Err)
139 char *Buf = NULL;
140 char Msg[512];
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);
148 ::MessageBox (NULL, Msg, "VisVim", MB_OK | MB_ICONSTOP);
149 if (Buf)
150 LocalFree (Buf);
153 void ReportInternalError (char* Fct)
155 char Msg[512];
157 sprintf (Msg, "Unexpected error\n%s failed", Fct);
158 ::MessageBox (NULL, Msg, "VisVim", MB_OK | MB_ICONSTOP);