Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved gvimext by Tianmiao Hu |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * gvimext is a DLL which is used for the "Edit with Vim" context menu |
| 11 | * extension. It implements a MS defined interface with the Shell. |
| 12 | * |
| 13 | * If you have any questions or any suggestions concerning gvimext, please |
| 14 | * contact Tianmiao Hu: tianmiao@acm.org. |
| 15 | */ |
| 16 | |
| 17 | #include "gvimext.h" |
| 18 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 19 | static char *searchpath(char *name); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 20 | |
| 21 | // Always get an error while putting the following stuff to the |
| 22 | // gvimext.h file as class protected variables, give up and |
| 23 | // declare them as global stuff |
| 24 | FORMATETC fmte = {CF_HDROP, |
| 25 | (DVTARGETDEVICE FAR *)NULL, |
| 26 | DVASPECT_CONTENT, |
| 27 | -1, |
| 28 | TYMED_HGLOBAL |
| 29 | }; |
| 30 | STGMEDIUM medium; |
| 31 | HRESULT hres = 0; |
| 32 | UINT cbFiles = 0; |
| 33 | |
Bram Moolenaar | 6199d43 | 2017-10-14 19:05:44 +0200 | [diff] [blame] | 34 | /* The buffers size used to be MAX_PATH (260 bytes), but that's not always |
Bram Moolenaar | e759a7a | 2005-07-12 22:50:18 +0000 | [diff] [blame] | 35 | * enough */ |
| 36 | #define BUFSIZE 1100 |
| 37 | |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 38 | // The "Edit with Vim" shell extension provides these choices when |
| 39 | // a new instance of Gvim is selected: |
| 40 | // - use tabpages |
| 41 | // - enable diff mode |
| 42 | // - none of the above |
| 43 | #define EDIT_WITH_VIM_USE_TABPAGES (2) |
| 44 | #define EDIT_WITH_VIM_IN_DIFF_MODE (1) |
| 45 | #define EDIT_WITH_VIM_NO_OPTIONS (0) |
| 46 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 47 | // |
| 48 | // Get the name of the Gvim executable to use, with the path. |
| 49 | // When "runtime" is non-zero, we were called to find the runtime directory. |
Bram Moolenaar | e759a7a | 2005-07-12 22:50:18 +0000 | [diff] [blame] | 50 | // Returns the path in name[BUFSIZE]. It's empty when it fails. |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 51 | // |
| 52 | static void |
| 53 | getGvimName(char *name, int runtime) |
| 54 | { |
| 55 | HKEY keyhandle; |
| 56 | DWORD hlen; |
| 57 | |
| 58 | // Get the location of gvim from the registry. |
| 59 | name[0] = 0; |
| 60 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim", 0, |
| 61 | KEY_READ, &keyhandle) == ERROR_SUCCESS) |
| 62 | { |
Bram Moolenaar | e759a7a | 2005-07-12 22:50:18 +0000 | [diff] [blame] | 63 | hlen = BUFSIZE; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 64 | if (RegQueryValueEx(keyhandle, "path", 0, NULL, (BYTE *)name, &hlen) |
| 65 | != ERROR_SUCCESS) |
| 66 | name[0] = 0; |
| 67 | else |
| 68 | name[hlen] = 0; |
| 69 | RegCloseKey(keyhandle); |
| 70 | } |
| 71 | |
| 72 | // Registry didn't work, use the search path. |
| 73 | if (name[0] == 0) |
Bram Moolenaar | 7baa45d | 2007-08-18 15:00:42 +0000 | [diff] [blame] | 74 | strcpy(name, searchpath((char *)"gvim.exe")); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 75 | |
| 76 | if (!runtime) |
| 77 | { |
| 78 | // Only when looking for the executable, not the runtime dir, we can |
| 79 | // search for the batch file or a name without a path. |
| 80 | if (name[0] == 0) |
Bram Moolenaar | 7baa45d | 2007-08-18 15:00:42 +0000 | [diff] [blame] | 81 | strcpy(name, searchpath((char *)"gvim.bat")); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 82 | if (name[0] == 0) |
| 83 | strcpy(name, "gvim"); // finds gvim.bat or gvim.exe |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 87 | static void |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 88 | getGvimInvocation(char *name, int runtime) |
| 89 | { |
| 90 | getGvimName(name, runtime); |
| 91 | // avoid that Vim tries to expand wildcards in the file names |
| 92 | strcat(name, " --literal"); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | getGvimInvocationW(wchar_t *nameW) |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 97 | { |
| 98 | char *name; |
| 99 | |
| 100 | name = (char *)malloc(BUFSIZE); |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 101 | getGvimInvocation(name, 0); |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 102 | mbstowcs(nameW, name, BUFSIZE); |
| 103 | free(name); |
| 104 | } |
| 105 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 106 | // |
Bram Moolenaar | e759a7a | 2005-07-12 22:50:18 +0000 | [diff] [blame] | 107 | // Get the Vim runtime directory into buf[BUFSIZE]. |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 108 | // The result is empty when it failed. |
| 109 | // When it works, the path ends in a slash or backslash. |
| 110 | // |
| 111 | static void |
| 112 | getRuntimeDir(char *buf) |
| 113 | { |
| 114 | int idx; |
| 115 | |
| 116 | getGvimName(buf, 1); |
| 117 | if (buf[0] != 0) |
| 118 | { |
| 119 | // When no path found, use the search path to expand it. |
| 120 | if (strchr(buf, '/') == NULL && strchr(buf, '\\') == NULL) |
| 121 | strcpy(buf, searchpath(buf)); |
| 122 | |
| 123 | // remove "gvim.exe" from the end |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 124 | for (idx = (int)strlen(buf) - 1; idx >= 0; idx--) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 125 | if (buf[idx] == '\\' || buf[idx] == '/') |
| 126 | { |
| 127 | buf[idx + 1] = 0; |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 133 | HBITMAP IconToBitmap(HICON hIcon, HBRUSH hBackground, int width, int height) |
| 134 | { |
| 135 | HDC hDC = GetDC(NULL); |
| 136 | HDC hMemDC = CreateCompatibleDC(hDC); |
| 137 | HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, width, height); |
| 138 | HBITMAP hResultBmp = NULL; |
| 139 | HGDIOBJ hOrgBMP = SelectObject(hMemDC, hMemBmp); |
| 140 | |
| 141 | DrawIconEx(hMemDC, 0, 0, hIcon, width, height, 0, hBackground, DI_NORMAL); |
| 142 | |
| 143 | hResultBmp = hMemBmp; |
| 144 | hMemBmp = NULL; |
| 145 | |
| 146 | SelectObject(hMemDC, hOrgBMP); |
| 147 | DeleteDC(hMemDC); |
| 148 | ReleaseDC(NULL, hDC); |
| 149 | DestroyIcon(hIcon); |
| 150 | return hResultBmp; |
| 151 | } |
| 152 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 153 | // |
| 154 | // GETTEXT: translated messages and menu entries |
| 155 | // |
| 156 | #ifndef FEAT_GETTEXT |
| 157 | # define _(x) x |
| 158 | #else |
| 159 | # define _(x) (*dyn_libintl_gettext)(x) |
| 160 | # define VIMPACKAGE "vim" |
| 161 | # ifndef GETTEXT_DLL |
| 162 | # define GETTEXT_DLL "libintl.dll" |
Bram Moolenaar | 271273c | 2016-02-21 20:30:22 +0100 | [diff] [blame] | 163 | # define GETTEXT_DLL_ALT "libintl-8.dll" |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 164 | # endif |
| 165 | |
| 166 | // Dummy functions |
| 167 | static char *null_libintl_gettext(const char *); |
| 168 | static char *null_libintl_textdomain(const char *); |
| 169 | static char *null_libintl_bindtextdomain(const char *, const char *); |
| 170 | static int dyn_libintl_init(char *dir); |
| 171 | static void dyn_libintl_end(void); |
| 172 | |
| 173 | static HINSTANCE hLibintlDLL = 0; |
| 174 | static char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext; |
| 175 | static char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain; |
| 176 | static char *(*dyn_libintl_bindtextdomain)(const char *, const char *) |
| 177 | = null_libintl_bindtextdomain; |
| 178 | |
| 179 | // |
| 180 | // Attempt to load libintl.dll. If it doesn't work, use dummy functions. |
| 181 | // "dir" is the directory where the libintl.dll might be. |
| 182 | // Return 1 for success, 0 for failure. |
| 183 | // |
| 184 | static int |
| 185 | dyn_libintl_init(char *dir) |
| 186 | { |
| 187 | int i; |
| 188 | static struct |
| 189 | { |
| 190 | char *name; |
| 191 | FARPROC *ptr; |
| 192 | } libintl_entry[] = |
| 193 | { |
Bram Moolenaar | 7baa45d | 2007-08-18 15:00:42 +0000 | [diff] [blame] | 194 | {(char *)"gettext", (FARPROC*)&dyn_libintl_gettext}, |
| 195 | {(char *)"textdomain", (FARPROC*)&dyn_libintl_textdomain}, |
| 196 | {(char *)"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain}, |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 197 | {NULL, NULL} |
| 198 | }; |
Bram Moolenaar | 271273c | 2016-02-21 20:30:22 +0100 | [diff] [blame] | 199 | DWORD len, len2; |
| 200 | LPWSTR buf = NULL; |
| 201 | LPWSTR buf2 = NULL; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 202 | |
| 203 | // No need to initialize twice. |
| 204 | if (hLibintlDLL) |
| 205 | return 1; |
| 206 | |
Bram Moolenaar | 6199d43 | 2017-10-14 19:05:44 +0200 | [diff] [blame] | 207 | // Load gettext library from $VIMRUNTIME\GvimExt{64,32} directory. |
Bram Moolenaar | 271273c | 2016-02-21 20:30:22 +0100 | [diff] [blame] | 208 | // Add the directory to $PATH temporarily. |
| 209 | len = GetEnvironmentVariableW(L"PATH", NULL, 0); |
| 210 | len2 = MAX_PATH + 1 + len; |
| 211 | buf = (LPWSTR)malloc(len * sizeof(WCHAR)); |
| 212 | buf2 = (LPWSTR)malloc(len2 * sizeof(WCHAR)); |
| 213 | if (buf != NULL && buf2 != NULL) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 214 | { |
Bram Moolenaar | 271273c | 2016-02-21 20:30:22 +0100 | [diff] [blame] | 215 | GetEnvironmentVariableW(L"PATH", buf, len); |
Bram Moolenaar | 3823192 | 2020-11-18 15:30:09 +0100 | [diff] [blame] | 216 | # ifdef _WIN64 |
Bram Moolenaar | 6199d43 | 2017-10-14 19:05:44 +0200 | [diff] [blame] | 217 | _snwprintf(buf2, len2, L"%S\\GvimExt64;%s", dir, buf); |
Bram Moolenaar | 3823192 | 2020-11-18 15:30:09 +0100 | [diff] [blame] | 218 | # else |
Bram Moolenaar | 6199d43 | 2017-10-14 19:05:44 +0200 | [diff] [blame] | 219 | _snwprintf(buf2, len2, L"%S\\GvimExt32;%s", dir, buf); |
Bram Moolenaar | 3823192 | 2020-11-18 15:30:09 +0100 | [diff] [blame] | 220 | # endif |
Bram Moolenaar | 271273c | 2016-02-21 20:30:22 +0100 | [diff] [blame] | 221 | SetEnvironmentVariableW(L"PATH", buf2); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 222 | hLibintlDLL = LoadLibrary(GETTEXT_DLL); |
Bram Moolenaar | 3823192 | 2020-11-18 15:30:09 +0100 | [diff] [blame] | 223 | # ifdef GETTEXT_DLL_ALT |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 224 | if (!hLibintlDLL) |
Bram Moolenaar | 271273c | 2016-02-21 20:30:22 +0100 | [diff] [blame] | 225 | hLibintlDLL = LoadLibrary(GETTEXT_DLL_ALT); |
Bram Moolenaar | 3823192 | 2020-11-18 15:30:09 +0100 | [diff] [blame] | 226 | # endif |
Bram Moolenaar | 271273c | 2016-02-21 20:30:22 +0100 | [diff] [blame] | 227 | SetEnvironmentVariableW(L"PATH", buf); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 228 | } |
Bram Moolenaar | 271273c | 2016-02-21 20:30:22 +0100 | [diff] [blame] | 229 | free(buf); |
| 230 | free(buf2); |
| 231 | if (!hLibintlDLL) |
| 232 | return 0; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 233 | |
| 234 | // Get the addresses of the functions we need. |
| 235 | for (i = 0; libintl_entry[i].name != NULL |
| 236 | && libintl_entry[i].ptr != NULL; ++i) |
| 237 | { |
| 238 | if ((*libintl_entry[i].ptr = GetProcAddress(hLibintlDLL, |
| 239 | libintl_entry[i].name)) == NULL) |
| 240 | { |
| 241 | dyn_libintl_end(); |
| 242 | return 0; |
| 243 | } |
| 244 | } |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | static void |
| 249 | dyn_libintl_end(void) |
| 250 | { |
| 251 | if (hLibintlDLL) |
| 252 | FreeLibrary(hLibintlDLL); |
| 253 | hLibintlDLL = NULL; |
| 254 | dyn_libintl_gettext = null_libintl_gettext; |
| 255 | dyn_libintl_textdomain = null_libintl_textdomain; |
| 256 | dyn_libintl_bindtextdomain = null_libintl_bindtextdomain; |
| 257 | } |
| 258 | |
| 259 | static char * |
| 260 | null_libintl_gettext(const char *msgid) |
| 261 | { |
| 262 | return (char *)msgid; |
| 263 | } |
| 264 | |
| 265 | static char * |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 266 | null_libintl_bindtextdomain(const char * /* domainname */, const char * /* dirname */) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 267 | { |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | static char * |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 272 | null_libintl_textdomain(const char* /* domainname */) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 273 | { |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | // |
| 278 | // Setup for translating strings. |
| 279 | // |
| 280 | static void |
| 281 | dyn_gettext_load(void) |
| 282 | { |
Bram Moolenaar | e759a7a | 2005-07-12 22:50:18 +0000 | [diff] [blame] | 283 | char szBuff[BUFSIZE]; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 284 | DWORD len; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 285 | |
| 286 | // Try to locate the runtime files. The path is used to find libintl.dll |
| 287 | // and the vim.mo files. |
| 288 | getRuntimeDir(szBuff); |
| 289 | if (szBuff[0] != 0) |
| 290 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 291 | len = (DWORD)strlen(szBuff); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 292 | if (dyn_libintl_init(szBuff)) |
| 293 | { |
| 294 | strcpy(szBuff + len, "lang"); |
| 295 | |
| 296 | (*dyn_libintl_bindtextdomain)(VIMPACKAGE, szBuff); |
| 297 | (*dyn_libintl_textdomain)(VIMPACKAGE); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static void |
| 303 | dyn_gettext_free(void) |
| 304 | { |
| 305 | dyn_libintl_end(); |
| 306 | } |
| 307 | #endif // FEAT_GETTEXT |
| 308 | |
| 309 | // |
| 310 | // Global variables |
| 311 | // |
| 312 | UINT g_cRefThisDll = 0; // Reference count of this DLL. |
| 313 | HINSTANCE g_hmodThisDll = NULL; // Handle to this DLL itself. |
| 314 | |
| 315 | |
| 316 | //--------------------------------------------------------------------------- |
| 317 | // DllMain |
| 318 | //--------------------------------------------------------------------------- |
| 319 | extern "C" int APIENTRY |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 320 | DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /* lpReserved */) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 321 | { |
| 322 | switch (dwReason) |
| 323 | { |
| 324 | case DLL_PROCESS_ATTACH: |
| 325 | // Extension DLL one-time initialization |
| 326 | g_hmodThisDll = hInstance; |
| 327 | break; |
| 328 | |
| 329 | case DLL_PROCESS_DETACH: |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | return 1; // ok |
| 334 | } |
| 335 | |
| 336 | static void |
| 337 | inc_cRefThisDLL() |
| 338 | { |
| 339 | #ifdef FEAT_GETTEXT |
Bram Moolenaar | 3823192 | 2020-11-18 15:30:09 +0100 | [diff] [blame] | 340 | if (g_cRefThisDll == 0) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 341 | dyn_gettext_load(); |
| 342 | #endif |
| 343 | InterlockedIncrement((LPLONG)&g_cRefThisDll); |
| 344 | } |
| 345 | |
| 346 | static void |
| 347 | dec_cRefThisDLL() |
| 348 | { |
| 349 | #ifdef FEAT_GETTEXT |
Bram Moolenaar | 3823192 | 2020-11-18 15:30:09 +0100 | [diff] [blame] | 350 | if (InterlockedDecrement((LPLONG)&g_cRefThisDll) == 0) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 351 | dyn_gettext_free(); |
| 352 | #else |
| 353 | InterlockedDecrement((LPLONG)&g_cRefThisDll); |
| 354 | #endif |
| 355 | } |
| 356 | |
| 357 | //--------------------------------------------------------------------------- |
| 358 | // DllCanUnloadNow |
| 359 | //--------------------------------------------------------------------------- |
| 360 | |
| 361 | STDAPI DllCanUnloadNow(void) |
| 362 | { |
| 363 | return (g_cRefThisDll == 0 ? S_OK : S_FALSE); |
| 364 | } |
| 365 | |
| 366 | STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut) |
| 367 | { |
| 368 | *ppvOut = NULL; |
| 369 | |
| 370 | if (IsEqualIID(rclsid, CLSID_ShellExtension)) |
| 371 | { |
| 372 | CShellExtClassFactory *pcf = new CShellExtClassFactory; |
| 373 | |
| 374 | return pcf->QueryInterface(riid, ppvOut); |
| 375 | } |
| 376 | |
| 377 | return CLASS_E_CLASSNOTAVAILABLE; |
| 378 | } |
| 379 | |
| 380 | CShellExtClassFactory::CShellExtClassFactory() |
| 381 | { |
| 382 | m_cRef = 0L; |
| 383 | |
| 384 | inc_cRefThisDLL(); |
| 385 | } |
| 386 | |
| 387 | CShellExtClassFactory::~CShellExtClassFactory() |
| 388 | { |
| 389 | dec_cRefThisDLL(); |
| 390 | } |
| 391 | |
| 392 | STDMETHODIMP CShellExtClassFactory::QueryInterface(REFIID riid, |
| 393 | LPVOID FAR *ppv) |
| 394 | { |
| 395 | *ppv = NULL; |
| 396 | |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 397 | // any interface on this object is the object pointer |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 398 | |
| 399 | if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory)) |
| 400 | { |
| 401 | *ppv = (LPCLASSFACTORY)this; |
| 402 | |
| 403 | AddRef(); |
| 404 | |
| 405 | return NOERROR; |
| 406 | } |
| 407 | |
| 408 | return E_NOINTERFACE; |
| 409 | } |
| 410 | |
| 411 | STDMETHODIMP_(ULONG) CShellExtClassFactory::AddRef() |
| 412 | { |
| 413 | return InterlockedIncrement((LPLONG)&m_cRef); |
| 414 | } |
| 415 | |
| 416 | STDMETHODIMP_(ULONG) CShellExtClassFactory::Release() |
| 417 | { |
| 418 | if (InterlockedDecrement((LPLONG)&m_cRef)) |
| 419 | return m_cRef; |
| 420 | |
| 421 | delete this; |
| 422 | |
| 423 | return 0L; |
| 424 | } |
| 425 | |
| 426 | STDMETHODIMP CShellExtClassFactory::CreateInstance(LPUNKNOWN pUnkOuter, |
| 427 | REFIID riid, |
| 428 | LPVOID *ppvObj) |
| 429 | { |
| 430 | *ppvObj = NULL; |
| 431 | |
| 432 | // Shell extensions typically don't support aggregation (inheritance) |
| 433 | |
| 434 | if (pUnkOuter) |
| 435 | return CLASS_E_NOAGGREGATION; |
| 436 | |
| 437 | // Create the main shell extension object. The shell will then call |
| 438 | // QueryInterface with IID_IShellExtInit--this is how shell extensions are |
| 439 | // initialized. |
| 440 | |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 441 | LPCSHELLEXT pShellExt = new CShellExt(); // create the CShellExt object |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 442 | |
| 443 | if (NULL == pShellExt) |
| 444 | return E_OUTOFMEMORY; |
| 445 | |
| 446 | return pShellExt->QueryInterface(riid, ppvObj); |
| 447 | } |
| 448 | |
| 449 | |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 450 | STDMETHODIMP CShellExtClassFactory::LockServer(BOOL /* fLock */) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 451 | { |
| 452 | return NOERROR; |
| 453 | } |
| 454 | |
| 455 | // *********************** CShellExt ************************* |
| 456 | CShellExt::CShellExt() |
| 457 | { |
| 458 | m_cRef = 0L; |
| 459 | m_pDataObj = NULL; |
| 460 | |
| 461 | inc_cRefThisDLL(); |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 462 | |
| 463 | LoadMenuIcon(); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | CShellExt::~CShellExt() |
| 467 | { |
| 468 | if (m_pDataObj) |
| 469 | m_pDataObj->Release(); |
| 470 | |
| 471 | dec_cRefThisDLL(); |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 472 | |
| 473 | if (m_hVimIconBitmap) |
| 474 | DeleteObject(m_hVimIconBitmap); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | STDMETHODIMP CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv) |
| 478 | { |
| 479 | *ppv = NULL; |
| 480 | |
| 481 | if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown)) |
| 482 | { |
| 483 | *ppv = (LPSHELLEXTINIT)this; |
| 484 | } |
| 485 | else if (IsEqualIID(riid, IID_IContextMenu)) |
| 486 | { |
| 487 | *ppv = (LPCONTEXTMENU)this; |
| 488 | } |
| 489 | |
| 490 | if (*ppv) |
| 491 | { |
| 492 | AddRef(); |
| 493 | |
| 494 | return NOERROR; |
| 495 | } |
| 496 | |
| 497 | return E_NOINTERFACE; |
| 498 | } |
| 499 | |
| 500 | STDMETHODIMP_(ULONG) CShellExt::AddRef() |
| 501 | { |
| 502 | return InterlockedIncrement((LPLONG)&m_cRef); |
| 503 | } |
| 504 | |
| 505 | STDMETHODIMP_(ULONG) CShellExt::Release() |
| 506 | { |
| 507 | |
| 508 | if (InterlockedDecrement((LPLONG)&m_cRef)) |
| 509 | return m_cRef; |
| 510 | |
| 511 | delete this; |
| 512 | |
| 513 | return 0L; |
| 514 | } |
| 515 | |
| 516 | |
| 517 | // |
| 518 | // FUNCTION: CShellExt::Initialize(LPCITEMIDLIST, LPDATAOBJECT, HKEY) |
| 519 | // |
| 520 | // PURPOSE: Called by the shell when initializing a context menu or property |
| 521 | // sheet extension. |
| 522 | // |
| 523 | // PARAMETERS: |
| 524 | // pIDFolder - Specifies the parent folder |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 525 | // pDataObj - Specifies the set of items selected in that folder. |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 526 | // hRegKey - Specifies the type of the focused item in the selection. |
| 527 | // |
| 528 | // RETURN VALUE: |
| 529 | // |
| 530 | // NOERROR in all cases. |
| 531 | // |
| 532 | // COMMENTS: Note that at the time this function is called, we don't know |
| 533 | // (or care) what type of shell extension is being initialized. |
| 534 | // It could be a context menu or a property sheet. |
| 535 | // |
| 536 | |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 537 | STDMETHODIMP CShellExt::Initialize(LPCITEMIDLIST /* pIDFolder */, |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 538 | LPDATAOBJECT pDataObj, |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 539 | HKEY /* hRegKey */) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 540 | { |
| 541 | // Initialize can be called more than once |
| 542 | if (m_pDataObj) |
| 543 | m_pDataObj->Release(); |
| 544 | |
| 545 | // duplicate the object pointer and registry handle |
| 546 | |
| 547 | if (pDataObj) |
| 548 | { |
| 549 | m_pDataObj = pDataObj; |
| 550 | pDataObj->AddRef(); |
| 551 | } |
| 552 | |
| 553 | return NOERROR; |
| 554 | } |
| 555 | |
| 556 | |
| 557 | // |
| 558 | // FUNCTION: CShellExt::QueryContextMenu(HMENU, UINT, UINT, UINT, UINT) |
| 559 | // |
| 560 | // PURPOSE: Called by the shell just before the context menu is displayed. |
| 561 | // This is where you add your specific menu items. |
| 562 | // |
| 563 | // PARAMETERS: |
| 564 | // hMenu - Handle to the context menu |
| 565 | // indexMenu - Index of where to begin inserting menu items |
| 566 | // idCmdFirst - Lowest value for new menu ID's |
| 567 | // idCmtLast - Highest value for new menu ID's |
| 568 | // uFlags - Specifies the context of the menu event |
| 569 | // |
| 570 | // RETURN VALUE: |
| 571 | // |
| 572 | // |
| 573 | // COMMENTS: |
| 574 | // |
| 575 | |
| 576 | STDMETHODIMP CShellExt::QueryContextMenu(HMENU hMenu, |
| 577 | UINT indexMenu, |
| 578 | UINT idCmdFirst, |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 579 | UINT /* idCmdLast */, |
| 580 | UINT /* uFlags */) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 581 | { |
| 582 | UINT idCmd = idCmdFirst; |
| 583 | |
| 584 | hres = m_pDataObj->GetData(&fmte, &medium); |
| 585 | if (medium.hGlobal) |
| 586 | cbFiles = DragQueryFile((HDROP)medium.hGlobal, (UINT)-1, 0, 0); |
| 587 | |
| 588 | // InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); |
| 589 | |
| 590 | // Initialize m_cntOfHWnd to 0 |
| 591 | m_cntOfHWnd = 0; |
Bram Moolenaar | ce35c88 | 2011-07-20 17:27:25 +0200 | [diff] [blame] | 592 | |
| 593 | HKEY keyhandle; |
| 594 | bool showExisting = true; |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 595 | bool showIcons = true; |
Bram Moolenaar | ce35c88 | 2011-07-20 17:27:25 +0200 | [diff] [blame] | 596 | |
| 597 | // Check whether "Edit with existing Vim" entries are disabled. |
| 598 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim", 0, |
| 599 | KEY_READ, &keyhandle) == ERROR_SUCCESS) |
| 600 | { |
| 601 | if (RegQueryValueEx(keyhandle, "DisableEditWithExisting", 0, NULL, |
| 602 | NULL, NULL) == ERROR_SUCCESS) |
| 603 | showExisting = false; |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 604 | if (RegQueryValueEx(keyhandle, "DisableContextMenuIcons", 0, NULL, |
| 605 | NULL, NULL) == ERROR_SUCCESS) |
| 606 | showIcons = false; |
Bram Moolenaar | ce35c88 | 2011-07-20 17:27:25 +0200 | [diff] [blame] | 607 | RegCloseKey(keyhandle); |
| 608 | } |
| 609 | |
| 610 | // Retrieve all the vim instances, unless disabled. |
| 611 | if (showExisting) |
| 612 | EnumWindows(EnumWindowsProc, (LPARAM)this); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 613 | |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 614 | MENUITEMINFO mii = { sizeof(MENUITEMINFO) }; |
| 615 | mii.fMask = MIIM_STRING | MIIM_ID; |
| 616 | if (showIcons) |
| 617 | { |
| 618 | mii.fMask |= MIIM_BITMAP; |
| 619 | mii.hbmpItem = m_hVimIconBitmap; |
| 620 | } |
| 621 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 622 | if (cbFiles > 1) |
| 623 | { |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 624 | mii.wID = idCmd++; |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 625 | mii.dwTypeData = _("Edit with Vim using &tabpages"); |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 626 | mii.cch = lstrlen(mii.dwTypeData); |
| 627 | InsertMenuItem(hMenu, indexMenu++, TRUE, &mii); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 628 | |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 629 | mii.wID = idCmd++; |
| 630 | mii.dwTypeData = _("Edit with single &Vim"); |
| 631 | mii.cch = lstrlen(mii.dwTypeData); |
| 632 | InsertMenuItem(hMenu, indexMenu++, TRUE, &mii); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 633 | |
| 634 | if (cbFiles <= 4) |
| 635 | { |
| 636 | // Can edit up to 4 files in diff mode |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 637 | mii.wID = idCmd++; |
| 638 | mii.dwTypeData = _("Diff with Vim"); |
| 639 | mii.cch = lstrlen(mii.dwTypeData); |
| 640 | InsertMenuItem(hMenu, indexMenu++, TRUE, &mii); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 641 | m_edit_existing_off = 3; |
| 642 | } |
| 643 | else |
| 644 | m_edit_existing_off = 2; |
| 645 | |
| 646 | } |
| 647 | else |
| 648 | { |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 649 | mii.wID = idCmd++; |
| 650 | mii.dwTypeData = _("Edit with &Vim"); |
| 651 | mii.cch = lstrlen(mii.dwTypeData); |
| 652 | InsertMenuItem(hMenu, indexMenu++, TRUE, &mii); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 653 | m_edit_existing_off = 1; |
| 654 | } |
| 655 | |
Bram Moolenaar | bf9679a | 2018-10-25 11:25:53 +0200 | [diff] [blame] | 656 | HMENU hSubMenu = NULL; |
| 657 | if (m_cntOfHWnd > 1) |
| 658 | { |
| 659 | hSubMenu = CreatePopupMenu(); |
| 660 | mii.fMask |= MIIM_SUBMENU; |
| 661 | mii.wID = idCmd; |
| 662 | mii.dwTypeData = _("Edit with existing Vim"); |
| 663 | mii.cch = lstrlen(mii.dwTypeData); |
| 664 | mii.hSubMenu = hSubMenu; |
| 665 | InsertMenuItem(hMenu, indexMenu++, TRUE, &mii); |
| 666 | mii.fMask = mii.fMask & ~MIIM_SUBMENU; |
| 667 | mii.hSubMenu = NULL; |
| 668 | } |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 669 | // Now display all the vim instances |
| 670 | for (int i = 0; i < m_cntOfHWnd; i++) |
| 671 | { |
Bram Moolenaar | e759a7a | 2005-07-12 22:50:18 +0000 | [diff] [blame] | 672 | char title[BUFSIZE]; |
| 673 | char temp[BUFSIZE]; |
Bram Moolenaar | bf9679a | 2018-10-25 11:25:53 +0200 | [diff] [blame] | 674 | int index; |
| 675 | HMENU hmenu; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 676 | |
| 677 | // Obtain window title, continue if can not |
Bram Moolenaar | e759a7a | 2005-07-12 22:50:18 +0000 | [diff] [blame] | 678 | if (GetWindowText(m_hWnd[i], title, BUFSIZE - 1) == 0) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 679 | continue; |
| 680 | // Truncate the title before the path, keep the file name |
| 681 | char *pos = strchr(title, '('); |
| 682 | if (pos != NULL) |
| 683 | { |
| 684 | if (pos > title && pos[-1] == ' ') |
| 685 | --pos; |
| 686 | *pos = 0; |
| 687 | } |
| 688 | // Now concatenate |
Bram Moolenaar | bf9679a | 2018-10-25 11:25:53 +0200 | [diff] [blame] | 689 | if (m_cntOfHWnd > 1) |
| 690 | temp[0] = '\0'; |
| 691 | else |
| 692 | { |
| 693 | strncpy(temp, _("Edit with existing Vim - "), BUFSIZE - 1); |
| 694 | temp[BUFSIZE - 1] = '\0'; |
| 695 | } |
Bram Moolenaar | dc7e00e | 2009-09-11 11:26:56 +0000 | [diff] [blame] | 696 | strncat(temp, title, BUFSIZE - 1 - strlen(temp)); |
| 697 | temp[BUFSIZE - 1] = '\0'; |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 698 | |
| 699 | mii.wID = idCmd++; |
| 700 | mii.dwTypeData = temp; |
| 701 | mii.cch = lstrlen(mii.dwTypeData); |
Bram Moolenaar | bf9679a | 2018-10-25 11:25:53 +0200 | [diff] [blame] | 702 | if (m_cntOfHWnd > 1) |
| 703 | { |
| 704 | hmenu = hSubMenu; |
| 705 | index = i; |
| 706 | } |
| 707 | else |
| 708 | { |
| 709 | hmenu = hMenu; |
| 710 | index = indexMenu++; |
| 711 | } |
| 712 | InsertMenuItem(hmenu, index, TRUE, &mii); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 713 | } |
| 714 | // InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); |
| 715 | |
| 716 | // Must return number of menu items we added. |
| 717 | return ResultFromShort(idCmd-idCmdFirst); |
| 718 | } |
| 719 | |
| 720 | // |
| 721 | // FUNCTION: CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO) |
| 722 | // |
| 723 | // PURPOSE: Called by the shell after the user has selected on of the |
| 724 | // menu items that was added in QueryContextMenu(). |
| 725 | // |
| 726 | // PARAMETERS: |
| 727 | // lpcmi - Pointer to an CMINVOKECOMMANDINFO structure |
| 728 | // |
| 729 | // RETURN VALUE: |
| 730 | // |
| 731 | // |
| 732 | // COMMENTS: |
| 733 | // |
| 734 | |
| 735 | STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi) |
| 736 | { |
| 737 | HRESULT hr = E_INVALIDARG; |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 738 | int gvimExtraOptions; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 739 | |
| 740 | // If HIWORD(lpcmi->lpVerb) then we have been called programmatically |
| 741 | // and lpVerb is a command that should be invoked. Otherwise, the shell |
| 742 | // has called us, and LOWORD(lpcmi->lpVerb) is the menu ID the user has |
| 743 | // selected. Actually, it's (menu ID - idCmdFirst) from QueryContextMenu(). |
| 744 | if (!HIWORD(lpcmi->lpVerb)) |
| 745 | { |
| 746 | UINT idCmd = LOWORD(lpcmi->lpVerb); |
| 747 | |
| 748 | if (idCmd >= m_edit_existing_off) |
| 749 | { |
| 750 | // Existing with vim instance |
| 751 | hr = PushToWindow(lpcmi->hwnd, |
| 752 | lpcmi->lpDirectory, |
| 753 | lpcmi->lpVerb, |
| 754 | lpcmi->lpParameters, |
| 755 | lpcmi->nShow, |
| 756 | idCmd - m_edit_existing_off); |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | switch (idCmd) |
| 761 | { |
| 762 | case 0: |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 763 | gvimExtraOptions = EDIT_WITH_VIM_USE_TABPAGES; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 764 | break; |
| 765 | case 1: |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 766 | gvimExtraOptions = EDIT_WITH_VIM_NO_OPTIONS; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 767 | break; |
| 768 | case 2: |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 769 | gvimExtraOptions = EDIT_WITH_VIM_IN_DIFF_MODE; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 770 | break; |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 771 | default: |
| 772 | // If execution reaches this point we likely have an |
| 773 | // inconsistency between the code that setup the menus |
| 774 | // and this code that determines what the user |
| 775 | // selected. This should be detected and fixed during |
| 776 | // development. |
| 777 | return E_FAIL; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 778 | } |
Nir Lichtman | 1aeccdb | 2021-12-22 15:21:15 +0000 | [diff] [blame] | 779 | |
| 780 | LPCMINVOKECOMMANDINFOEX lpcmiex = (LPCMINVOKECOMMANDINFOEX)lpcmi; |
| 781 | LPCWSTR currentDirectory = lpcmi->cbSize == sizeof(CMINVOKECOMMANDINFOEX) ? lpcmiex->lpDirectoryW : NULL; |
| 782 | |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 783 | hr = InvokeSingleGvim(lpcmi->hwnd, |
Nir Lichtman | 1aeccdb | 2021-12-22 15:21:15 +0000 | [diff] [blame] | 784 | currentDirectory, |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 785 | lpcmi->lpVerb, |
| 786 | lpcmi->lpParameters, |
| 787 | lpcmi->nShow, |
| 788 | gvimExtraOptions); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | return hr; |
| 792 | } |
| 793 | |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 794 | STDMETHODIMP CShellExt::PushToWindow(HWND /* hParent */, |
| 795 | LPCSTR /* pszWorkingDir */, |
| 796 | LPCSTR /* pszCmd */, |
| 797 | LPCSTR /* pszParam */, |
| 798 | int /* iShowCmd */, |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 799 | int idHWnd) |
| 800 | { |
| 801 | HWND hWnd = m_hWnd[idHWnd]; |
| 802 | |
| 803 | // Show and bring vim instance to foreground |
| 804 | if (IsIconic(hWnd) != 0) |
| 805 | ShowWindow(hWnd, SW_RESTORE); |
| 806 | else |
| 807 | ShowWindow(hWnd, SW_SHOW); |
| 808 | SetForegroundWindow(hWnd); |
| 809 | |
| 810 | // Post the selected files to the vim instance |
| 811 | PostMessage(hWnd, WM_DROPFILES, (WPARAM)medium.hGlobal, 0); |
| 812 | |
| 813 | return NOERROR; |
| 814 | } |
| 815 | |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 816 | STDMETHODIMP CShellExt::GetCommandString(UINT_PTR /* idCmd */, |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 817 | UINT uFlags, |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 818 | UINT FAR * /* reserved */, |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 819 | LPSTR pszName, |
| 820 | UINT cchMax) |
| 821 | { |
| 822 | if (uFlags == GCS_HELPTEXT && cchMax > 35) |
| 823 | lstrcpy(pszName, _("Edits the selected file(s) with Vim")); |
| 824 | |
| 825 | return NOERROR; |
| 826 | } |
| 827 | |
| 828 | BOOL CALLBACK CShellExt::EnumWindowsProc(HWND hWnd, LPARAM lParam) |
| 829 | { |
Bram Moolenaar | e759a7a | 2005-07-12 22:50:18 +0000 | [diff] [blame] | 830 | char temp[BUFSIZE]; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 831 | |
| 832 | // First do a bunch of check |
| 833 | // No invisible window |
| 834 | if (!IsWindowVisible(hWnd)) return TRUE; |
| 835 | // No child window ??? |
| 836 | // if (GetParent(hWnd)) return TRUE; |
| 837 | // Class name should be Vim, if failed to get class name, return |
| 838 | if (GetClassName(hWnd, temp, sizeof(temp)) == 0) |
| 839 | return TRUE; |
| 840 | // Compare class name to that of vim, if not, return |
| 841 | if (_strnicmp(temp, "vim", sizeof("vim")) != 0) |
| 842 | return TRUE; |
| 843 | // First check if the number of vim instance exceeds MAX_HWND |
| 844 | CShellExt *cs = (CShellExt*) lParam; |
| 845 | if (cs->m_cntOfHWnd >= MAX_HWND) return TRUE; |
| 846 | // Now we get the vim window, put it into some kind of array |
| 847 | cs->m_hWnd[cs->m_cntOfHWnd] = hWnd; |
| 848 | cs->m_cntOfHWnd ++; |
| 849 | |
| 850 | return TRUE; // continue enumeration (otherwise this would be false) |
| 851 | } |
| 852 | |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 853 | BOOL CShellExt::LoadMenuIcon() |
| 854 | { |
| 855 | char vimExeFile[BUFSIZE]; |
| 856 | getGvimName(vimExeFile, 1); |
| 857 | if (vimExeFile[0] == '\0') |
| 858 | return FALSE; |
| 859 | HICON hVimIcon; |
| 860 | if (ExtractIconEx(vimExeFile, 0, NULL, &hVimIcon, 1) == 0) |
| 861 | return FALSE; |
| 862 | m_hVimIconBitmap = IconToBitmap(hVimIcon, |
| 863 | GetSysColorBrush(COLOR_MENU), |
| 864 | GetSystemMetrics(SM_CXSMICON), |
| 865 | GetSystemMetrics(SM_CYSMICON)); |
| 866 | return TRUE; |
| 867 | } |
| 868 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 869 | static char * |
| 870 | searchpath(char *name) |
| 871 | { |
Bram Moolenaar | e759a7a | 2005-07-12 22:50:18 +0000 | [diff] [blame] | 872 | static char widename[2 * BUFSIZE]; |
| 873 | static char location[2 * BUFSIZE + 2]; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 874 | |
| 875 | // There appears to be a bug in FindExecutableA() on Windows NT. |
| 876 | // Use FindExecutableW() instead... |
Bram Moolenaar | 6199d43 | 2017-10-14 19:05:44 +0200 | [diff] [blame] | 877 | MultiByteToWideChar(CP_ACP, 0, (LPCSTR)name, -1, |
| 878 | (LPWSTR)widename, BUFSIZE); |
| 879 | if (FindExecutableW((LPCWSTR)widename, (LPCWSTR)"", |
| 880 | (LPWSTR)location) > (HINSTANCE)32) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 881 | { |
Bram Moolenaar | 6199d43 | 2017-10-14 19:05:44 +0200 | [diff] [blame] | 882 | WideCharToMultiByte(CP_ACP, 0, (LPWSTR)location, -1, |
| 883 | (LPSTR)widename, 2 * BUFSIZE, NULL, NULL); |
| 884 | return widename; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 885 | } |
Bram Moolenaar | 7baa45d | 2007-08-18 15:00:42 +0000 | [diff] [blame] | 886 | return (char *)""; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 887 | } |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 888 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 889 | |
| 890 | STDMETHODIMP CShellExt::InvokeSingleGvim(HWND hParent, |
Nir Lichtman | 1aeccdb | 2021-12-22 15:21:15 +0000 | [diff] [blame] | 891 | LPCWSTR workingDir, |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 892 | LPCSTR /* pszCmd */, |
| 893 | LPCSTR /* pszParam */, |
| 894 | int /* iShowCmd */, |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 895 | int gvimExtraOptions) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 896 | { |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 897 | wchar_t m_szFileUserClickedOn[BUFSIZE]; |
| 898 | wchar_t *cmdStrW; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 899 | size_t cmdlen; |
| 900 | size_t len; |
| 901 | UINT i; |
| 902 | |
Bram Moolenaar | e759a7a | 2005-07-12 22:50:18 +0000 | [diff] [blame] | 903 | cmdlen = BUFSIZE; |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 904 | cmdStrW = (wchar_t *) malloc(cmdlen * sizeof(wchar_t)); |
Bram Moolenaar | 06d4c4c | 2018-12-14 19:20:02 +0100 | [diff] [blame] | 905 | if (cmdStrW == NULL) |
Bram Moolenaar | 142a975 | 2018-12-14 19:54:39 +0100 | [diff] [blame] | 906 | return E_FAIL; |
Bram Moolenaar | 7bc25ae | 2015-05-04 18:27:36 +0200 | [diff] [blame] | 907 | getGvimInvocationW(cmdStrW); |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 908 | |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 909 | if (gvimExtraOptions == EDIT_WITH_VIM_IN_DIFF_MODE) |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 910 | wcscat(cmdStrW, L" -d"); |
msoyka-of-wharton | 83cd015 | 2021-07-29 19:18:33 +0200 | [diff] [blame] | 911 | else if (gvimExtraOptions == EDIT_WITH_VIM_USE_TABPAGES) |
| 912 | wcscat(cmdStrW, L" -p"); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 913 | for (i = 0; i < cbFiles; i++) |
| 914 | { |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 915 | DragQueryFileW((HDROP)medium.hGlobal, |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 916 | i, |
| 917 | m_szFileUserClickedOn, |
| 918 | sizeof(m_szFileUserClickedOn)); |
| 919 | |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 920 | len = wcslen(cmdStrW) + wcslen(m_szFileUserClickedOn) + 4; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 921 | if (len > cmdlen) |
| 922 | { |
Bram Moolenaar | e759a7a | 2005-07-12 22:50:18 +0000 | [diff] [blame] | 923 | cmdlen = len + BUFSIZE; |
Bram Moolenaar | 06d4c4c | 2018-12-14 19:20:02 +0100 | [diff] [blame] | 924 | wchar_t *cmdStrW_new = (wchar_t *)realloc(cmdStrW, cmdlen * sizeof(wchar_t)); |
| 925 | if (cmdStrW_new == NULL) |
Bram Moolenaar | 142a975 | 2018-12-14 19:54:39 +0100 | [diff] [blame] | 926 | { |
| 927 | free(cmdStrW); |
| 928 | return E_FAIL; |
| 929 | } |
Bram Moolenaar | 06d4c4c | 2018-12-14 19:20:02 +0100 | [diff] [blame] | 930 | cmdStrW = cmdStrW_new; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 931 | } |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 932 | wcscat(cmdStrW, L" \""); |
| 933 | wcscat(cmdStrW, m_szFileUserClickedOn); |
| 934 | wcscat(cmdStrW, L"\""); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 937 | STARTUPINFOW si; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 938 | PROCESS_INFORMATION pi; |
| 939 | |
| 940 | ZeroMemory(&si, sizeof(si)); |
| 941 | si.cb = sizeof(si); |
| 942 | |
| 943 | // Start the child process. |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 944 | if (!CreateProcessW(NULL, // No module name (use command line). |
| 945 | cmdStrW, // Command line. |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 946 | NULL, // Process handle not inheritable. |
| 947 | NULL, // Thread handle not inheritable. |
| 948 | FALSE, // Set handle inheritance to FALSE. |
Bram Moolenaar | 3823192 | 2020-11-18 15:30:09 +0100 | [diff] [blame] | 949 | 0, // No creation flags. |
| 950 | NULL, // Use parent's environment block. |
Nir Lichtman | 1aeccdb | 2021-12-22 15:21:15 +0000 | [diff] [blame] | 951 | workingDir, // Use parent's starting directory. |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 952 | &si, // Pointer to STARTUPINFO structure. |
| 953 | &pi) // Pointer to PROCESS_INFORMATION structure. |
| 954 | ) |
| 955 | { |
| 956 | MessageBox( |
| 957 | hParent, |
| 958 | _("Error creating process: Check if gvim is in your path!"), |
| 959 | _("gvimext.dll error"), |
| 960 | MB_OK); |
| 961 | } |
| 962 | else |
| 963 | { |
| 964 | CloseHandle(pi.hProcess); |
| 965 | CloseHandle(pi.hThread); |
| 966 | } |
Bram Moolenaar | 7e6d3bd | 2010-07-10 19:22:44 +0200 | [diff] [blame] | 967 | free(cmdStrW); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 968 | |
| 969 | return NOERROR; |
| 970 | } |