Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 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 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * os_mswin.c |
| 12 | * |
Bram Moolenaar | cf7164a | 2016-02-20 13:55:06 +0100 | [diff] [blame] | 13 | * Routines for Win32. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 16 | #include "vim.h" |
| 17 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 18 | #include <sys/types.h> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 19 | #include <signal.h> |
| 20 | #include <limits.h> |
Bram Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 21 | #ifndef PROTO |
| 22 | # include <process.h> |
| 23 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 24 | |
| 25 | #undef chdir |
| 26 | #ifdef __GNUC__ |
| 27 | # ifndef __MINGW32__ |
| 28 | # include <dirent.h> |
| 29 | # endif |
| 30 | #else |
| 31 | # include <direct.h> |
| 32 | #endif |
| 33 | |
Bram Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 34 | #ifndef PROTO |
| 35 | # if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32) |
| 36 | # include <shellapi.h> |
| 37 | # endif |
| 38 | |
| 39 | # if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT) |
| 40 | # include <dlgs.h> |
| 41 | # ifdef WIN3264 |
| 42 | # include <winspool.h> |
| 43 | # else |
| 44 | # include <print.h> |
| 45 | # endif |
| 46 | # include <commdlg.h> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Bram Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 49 | #endif /* PROTO */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 50 | |
| 51 | #ifdef __MINGW32__ |
| 52 | # ifndef FROM_LEFT_1ST_BUTTON_PRESSED |
| 53 | # define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001 |
| 54 | # endif |
| 55 | # ifndef RIGHTMOST_BUTTON_PRESSED |
| 56 | # define RIGHTMOST_BUTTON_PRESSED 0x0002 |
| 57 | # endif |
| 58 | # ifndef FROM_LEFT_2ND_BUTTON_PRESSED |
| 59 | # define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004 |
| 60 | # endif |
| 61 | # ifndef FROM_LEFT_3RD_BUTTON_PRESSED |
| 62 | # define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008 |
| 63 | # endif |
| 64 | # ifndef FROM_LEFT_4TH_BUTTON_PRESSED |
| 65 | # define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010 |
| 66 | # endif |
| 67 | |
| 68 | /* |
| 69 | * EventFlags |
| 70 | */ |
| 71 | # ifndef MOUSE_MOVED |
| 72 | # define MOUSE_MOVED 0x0001 |
| 73 | # endif |
| 74 | # ifndef DOUBLE_CLICK |
| 75 | # define DOUBLE_CLICK 0x0002 |
| 76 | # endif |
| 77 | #endif |
| 78 | |
| 79 | /* |
| 80 | * When generating prototypes for Win32 on Unix, these lines make the syntax |
| 81 | * errors disappear. They do not need to be correct. |
| 82 | */ |
| 83 | #ifdef PROTO |
| 84 | #define WINAPI |
| 85 | #define WINBASEAPI |
| 86 | typedef int BOOL; |
| 87 | typedef int CALLBACK; |
| 88 | typedef int COLORREF; |
| 89 | typedef int CONSOLE_CURSOR_INFO; |
| 90 | typedef int COORD; |
| 91 | typedef int DWORD; |
| 92 | typedef int ENUMLOGFONT; |
| 93 | typedef int HANDLE; |
| 94 | typedef int HDC; |
| 95 | typedef int HFONT; |
| 96 | typedef int HICON; |
| 97 | typedef int HWND; |
| 98 | typedef int INPUT_RECORD; |
| 99 | typedef int KEY_EVENT_RECORD; |
| 100 | typedef int LOGFONT; |
| 101 | typedef int LPARAM; |
| 102 | typedef int LPBOOL; |
| 103 | typedef int LPCSTR; |
| 104 | typedef int LPCWSTR; |
| 105 | typedef int LPSTR; |
| 106 | typedef int LPTSTR; |
| 107 | typedef int LPWSTR; |
| 108 | typedef int LRESULT; |
| 109 | typedef int MOUSE_EVENT_RECORD; |
| 110 | typedef int NEWTEXTMETRIC; |
| 111 | typedef int PACL; |
| 112 | typedef int PRINTDLG; |
| 113 | typedef int PSECURITY_DESCRIPTOR; |
| 114 | typedef int PSID; |
| 115 | typedef int SECURITY_INFORMATION; |
| 116 | typedef int SHORT; |
| 117 | typedef int SMALL_RECT; |
| 118 | typedef int TEXTMETRIC; |
| 119 | typedef int UINT; |
| 120 | typedef int WCHAR; |
| 121 | typedef int WORD; |
| 122 | typedef int WPARAM; |
| 123 | typedef void VOID; |
| 124 | #endif |
| 125 | |
| 126 | /* Record all output and all keyboard & mouse input */ |
| 127 | /* #define MCH_WRITE_DUMP */ |
| 128 | |
| 129 | #ifdef MCH_WRITE_DUMP |
| 130 | FILE* fdDump = NULL; |
| 131 | #endif |
| 132 | |
| 133 | #ifdef WIN3264 |
| 134 | extern DWORD g_PlatformId; |
| 135 | #endif |
| 136 | |
| 137 | #ifndef FEAT_GUI_MSWIN |
| 138 | extern char g_szOrigTitle[]; |
| 139 | #endif |
| 140 | |
| 141 | #ifdef FEAT_GUI |
| 142 | extern HWND s_hwnd; |
| 143 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 144 | static HWND s_hwnd = 0; /* console window handle, set by GetConsoleHwnd() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 145 | #endif |
| 146 | |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 147 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 148 | int WSInitialized = FALSE; /* WinSock is initialized */ |
| 149 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 150 | |
| 151 | /* Don't generate prototypes here, because some systems do have these |
| 152 | * functions. */ |
| 153 | #if defined(__GNUC__) && !defined(PROTO) |
| 154 | # ifndef __MINGW32__ |
| 155 | int _stricoll(char *a, char *b) |
| 156 | { |
| 157 | // the ANSI-ish correct way is to use strxfrm(): |
| 158 | char a_buff[512], b_buff[512]; // file names, so this is enough on Win32 |
| 159 | strxfrm(a_buff, a, 512); |
| 160 | strxfrm(b_buff, b, 512); |
| 161 | return strcoll(a_buff, b_buff); |
| 162 | } |
| 163 | |
| 164 | char * _fullpath(char *buf, char *fname, int len) |
| 165 | { |
| 166 | LPTSTR toss; |
| 167 | |
| 168 | return (char *)GetFullPathName(fname, len, buf, &toss); |
| 169 | } |
| 170 | # endif |
| 171 | |
Bram Moolenaar | af62ff3 | 2013-03-19 14:48:29 +0100 | [diff] [blame] | 172 | # if !defined(__MINGW32__) || (__GNUC__ < 4) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 173 | int _chdrive(int drive) |
| 174 | { |
| 175 | char temp [3] = "-:"; |
| 176 | temp[0] = drive + 'A' - 1; |
| 177 | return !SetCurrentDirectory(temp); |
| 178 | } |
Bram Moolenaar | af62ff3 | 2013-03-19 14:48:29 +0100 | [diff] [blame] | 179 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 180 | #else |
| 181 | # ifdef __BORLANDC__ |
| 182 | /* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll: |
| 183 | * but it does in BC 5.02! */ |
| 184 | # if __BORLANDC__ < 0x502 |
| 185 | int _stricoll(char *a, char *b) |
| 186 | { |
| 187 | # if 1 |
| 188 | // this is fast but not correct: |
| 189 | return stricmp(a, b); |
| 190 | # else |
| 191 | // the ANSI-ish correct way is to use strxfrm(): |
| 192 | char a_buff[512], b_buff[512]; // file names, so this is enough on Win32 |
| 193 | strxfrm(a_buff, a, 512); |
| 194 | strxfrm(b_buff, b, 512); |
| 195 | return strcoll(a_buff, b_buff); |
| 196 | # endif |
| 197 | } |
| 198 | # endif |
| 199 | # endif |
| 200 | #endif |
| 201 | |
| 202 | |
| 203 | #if defined(FEAT_GUI_MSWIN) || defined(PROTO) |
| 204 | /* |
| 205 | * GUI version of mch_exit(). |
| 206 | * Shut down and exit with status `r' |
| 207 | * Careful: mch_exit() may be called before mch_init()! |
| 208 | */ |
| 209 | void |
| 210 | mch_exit(int r) |
| 211 | { |
| 212 | display_errors(); |
| 213 | |
| 214 | ml_close_all(TRUE); /* remove all memfiles */ |
| 215 | |
| 216 | # ifdef FEAT_OLE |
| 217 | UninitOLE(); |
| 218 | # endif |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 219 | # ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 220 | if (WSInitialized) |
| 221 | { |
| 222 | WSInitialized = FALSE; |
| 223 | WSACleanup(); |
| 224 | } |
| 225 | # endif |
| 226 | #ifdef DYNAMIC_GETTEXT |
| 227 | dyn_libintl_end(); |
| 228 | #endif |
| 229 | |
| 230 | if (gui.in_use) |
| 231 | gui_exit(r); |
Bram Moolenaar | 85c79d3 | 2007-02-20 01:59:20 +0000 | [diff] [blame] | 232 | |
| 233 | #ifdef EXITFREE |
| 234 | free_all_mem(); |
| 235 | #endif |
| 236 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 237 | exit(r); |
| 238 | } |
| 239 | |
| 240 | #endif /* FEAT_GUI_MSWIN */ |
| 241 | |
| 242 | |
| 243 | /* |
| 244 | * Init the tables for toupper() and tolower(). |
| 245 | */ |
| 246 | void |
| 247 | mch_early_init(void) |
| 248 | { |
| 249 | int i; |
| 250 | |
| 251 | #ifdef WIN3264 |
| 252 | PlatformId(); |
| 253 | #endif |
| 254 | |
| 255 | /* Init the tables for toupper() and tolower() */ |
| 256 | for (i = 0; i < 256; ++i) |
| 257 | toupper_tab[i] = tolower_tab[i] = i; |
| 258 | #ifdef WIN3264 |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 259 | CharUpperBuff((LPSTR)toupper_tab, 256); |
| 260 | CharLowerBuff((LPSTR)tolower_tab, 256); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 261 | #else |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 262 | AnsiUpperBuff((LPSTR)toupper_tab, 256); |
| 263 | AnsiLowerBuff((LPSTR)tolower_tab, 256); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 264 | #endif |
| 265 | } |
| 266 | |
| 267 | |
| 268 | /* |
| 269 | * Return TRUE if the input comes from a terminal, FALSE otherwise. |
| 270 | */ |
| 271 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 272 | mch_input_isatty(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 273 | { |
| 274 | #ifdef FEAT_GUI_MSWIN |
| 275 | return OK; /* GUI always has a tty */ |
| 276 | #else |
| 277 | if (isatty(read_cmd_fd)) |
| 278 | return TRUE; |
| 279 | return FALSE; |
| 280 | #endif |
| 281 | } |
| 282 | |
| 283 | #ifdef FEAT_TITLE |
| 284 | /* |
| 285 | * mch_settitle(): set titlebar of our window |
| 286 | */ |
| 287 | void |
| 288 | mch_settitle( |
| 289 | char_u *title, |
| 290 | char_u *icon) |
| 291 | { |
| 292 | # ifdef FEAT_GUI_MSWIN |
| 293 | gui_mch_settitle(title, icon); |
| 294 | # else |
| 295 | if (title != NULL) |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 296 | { |
| 297 | # ifdef FEAT_MBYTE |
| 298 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 299 | { |
| 300 | /* Convert the title from 'encoding' to the active codepage. */ |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 301 | WCHAR *wp = enc_to_utf16(title, NULL); |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 302 | int n; |
| 303 | |
| 304 | if (wp != NULL) |
| 305 | { |
| 306 | n = SetConsoleTitleW(wp); |
| 307 | vim_free(wp); |
| 308 | if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 309 | return; |
| 310 | } |
| 311 | } |
| 312 | # endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 313 | SetConsoleTitle((LPCSTR)title); |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 314 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 315 | # endif |
| 316 | } |
| 317 | |
| 318 | |
| 319 | /* |
| 320 | * Restore the window/icon title. |
| 321 | * which is one of: |
| 322 | * 1: Just restore title |
| 323 | * 2: Just restore icon (which we don't have) |
| 324 | * 3: Restore title and icon (which we don't have) |
| 325 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 326 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 327 | void |
| 328 | mch_restore_title( |
| 329 | int which) |
| 330 | { |
| 331 | #ifndef FEAT_GUI_MSWIN |
Bram Moolenaar | 1df52d7 | 2014-10-15 22:50:10 +0200 | [diff] [blame] | 332 | SetConsoleTitle(g_szOrigTitle); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 333 | #endif |
| 334 | } |
| 335 | |
| 336 | |
| 337 | /* |
| 338 | * Return TRUE if we can restore the title (we can) |
| 339 | */ |
| 340 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 341 | mch_can_restore_title(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 342 | { |
| 343 | return TRUE; |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /* |
| 348 | * Return TRUE if we can restore the icon title (we can't) |
| 349 | */ |
| 350 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 351 | mch_can_restore_icon(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 352 | { |
| 353 | return FALSE; |
| 354 | } |
| 355 | #endif /* FEAT_TITLE */ |
| 356 | |
| 357 | |
| 358 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 359 | * Get absolute file name into buffer "buf" of length "len" bytes, |
| 360 | * turning all '/'s into '\\'s and getting the correct case of each component |
| 361 | * of the file name. Append a (back)slash to a directory name. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 362 | * When 'shellslash' set do it the other way around. |
| 363 | * Return OK or FAIL. |
| 364 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 365 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 366 | int |
| 367 | mch_FullName( |
| 368 | char_u *fname, |
| 369 | char_u *buf, |
| 370 | int len, |
| 371 | int force) |
| 372 | { |
| 373 | int nResult = FAIL; |
| 374 | |
| 375 | #ifdef __BORLANDC__ |
| 376 | if (*fname == NUL) /* Borland behaves badly here - make it consistent */ |
| 377 | nResult = mch_dirname(buf, len); |
| 378 | else |
| 379 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 380 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 381 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 382 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage |
| 383 | # ifdef __BORLANDC__ |
| 384 | /* Wide functions of Borland C 5.5 do not work on Windows 98. */ |
| 385 | && g_PlatformId == VER_PLATFORM_WIN32_NT |
| 386 | # endif |
| 387 | ) |
| 388 | { |
| 389 | WCHAR *wname; |
| 390 | WCHAR wbuf[MAX_PATH]; |
| 391 | char_u *cname = NULL; |
| 392 | |
| 393 | /* Use the wide function: |
| 394 | * - convert the fname from 'encoding' to UCS2. |
| 395 | * - invoke _wfullpath() |
| 396 | * - convert the result from UCS2 to 'encoding'. |
| 397 | */ |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 398 | wname = enc_to_utf16(fname, NULL); |
Bram Moolenaar | 374bf02 | 2014-11-05 19:33:24 +0100 | [diff] [blame] | 399 | if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 400 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 401 | cname = utf16_to_enc((short_u *)wbuf, NULL); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 402 | if (cname != NULL) |
| 403 | { |
Bram Moolenaar | fe3ca8d | 2005-07-18 21:43:02 +0000 | [diff] [blame] | 404 | vim_strncpy(buf, cname, len - 1); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 405 | nResult = OK; |
| 406 | } |
| 407 | } |
| 408 | vim_free(wname); |
| 409 | vim_free(cname); |
| 410 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 411 | if (nResult == FAIL) /* fall back to non-wide function */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 412 | #endif |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 413 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 414 | if (_fullpath((char *)buf, (const char *)fname, len - 1) == NULL) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 415 | { |
Bram Moolenaar | fe3ca8d | 2005-07-18 21:43:02 +0000 | [diff] [blame] | 416 | /* failed, use relative path name */ |
| 417 | vim_strncpy(buf, fname, len - 1); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 418 | } |
| 419 | else |
| 420 | nResult = OK; |
| 421 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 422 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 423 | |
| 424 | #ifdef USE_FNAME_CASE |
| 425 | fname_case(buf, len); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 426 | #else |
| 427 | slash_adjust(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 428 | #endif |
| 429 | |
| 430 | return nResult; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /* |
| 435 | * Return TRUE if "fname" does not depend on the current directory. |
| 436 | */ |
| 437 | int |
| 438 | mch_isFullName(char_u *fname) |
| 439 | { |
Bram Moolenaar | d2a203b | 2013-08-30 16:51:18 +0200 | [diff] [blame] | 440 | #ifdef FEAT_MBYTE |
| 441 | /* WinNT and later can use _MAX_PATH wide characters for a pathname, which |
| 442 | * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is |
| 443 | * UTF-8. */ |
| 444 | char szName[_MAX_PATH * 3 + 1]; |
| 445 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 446 | char szName[_MAX_PATH + 1]; |
Bram Moolenaar | d2a203b | 2013-08-30 16:51:18 +0200 | [diff] [blame] | 447 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 448 | |
| 449 | /* A name like "d:/foo" and "//server/share" is absolute */ |
| 450 | if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\')) |
| 451 | || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\'))) |
| 452 | return TRUE; |
| 453 | |
| 454 | /* A name that can't be made absolute probably isn't absolute. */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 455 | if (mch_FullName(fname, (char_u *)szName, sizeof(szName) - 1, FALSE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 456 | return FALSE; |
| 457 | |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 458 | return pathcmp((const char *)fname, (const char *)szName, -1) == 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Replace all slashes by backslashes. |
| 463 | * This used to be the other way around, but MS-DOS sometimes has problems |
| 464 | * with slashes (e.g. in a command name). We can't have mixed slashes and |
| 465 | * backslashes, because comparing file names will not work correctly. The |
| 466 | * commands that use a file name should try to avoid the need to type a |
| 467 | * backslash twice. |
| 468 | * When 'shellslash' set do it the other way around. |
Bram Moolenaar | b4f6a46 | 2015-10-13 19:43:17 +0200 | [diff] [blame] | 469 | * When the path looks like a URL leave it unmodified. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 470 | */ |
| 471 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 472 | slash_adjust(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 473 | { |
Bram Moolenaar | b4f6a46 | 2015-10-13 19:43:17 +0200 | [diff] [blame] | 474 | if (path_with_url(p)) |
| 475 | return; |
Bram Moolenaar | a3ffd9c | 2005-07-21 21:03:15 +0000 | [diff] [blame] | 476 | while (*p) |
| 477 | { |
| 478 | if (*p == psepcN) |
| 479 | *p = psepc; |
| 480 | mb_ptr_adv(p); |
| 481 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 484 | /* Use 64-bit stat functions if available. */ |
| 485 | #ifdef HAVE_STAT64 |
| 486 | # undef stat |
| 487 | # undef _stat |
| 488 | # undef _wstat |
| 489 | # undef _fstat |
| 490 | # define stat _stat64 |
| 491 | # define _stat _stat64 |
| 492 | # define _wstat _wstat64 |
| 493 | # define _fstat _fstat64 |
| 494 | #endif |
| 495 | |
Bram Moolenaar | 9d1685d | 2014-01-14 12:18:45 +0100 | [diff] [blame] | 496 | #if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__) |
Bram Moolenaar | 8962fda | 2013-09-29 19:05:21 +0200 | [diff] [blame] | 497 | # define OPEN_OH_ARGTYPE intptr_t |
| 498 | #else |
| 499 | # define OPEN_OH_ARGTYPE long |
| 500 | #endif |
| 501 | |
Bram Moolenaar | 2ee95f7 | 2013-09-25 19:13:38 +0200 | [diff] [blame] | 502 | static int |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 503 | stat_symlink_aware(const char *name, stat_T *stp) |
Bram Moolenaar | 2ee95f7 | 2013-09-25 19:13:38 +0200 | [diff] [blame] | 504 | { |
Bram Moolenaar | fce7b3d | 2016-01-19 19:00:32 +0100 | [diff] [blame] | 505 | #if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__) |
| 506 | /* Work around for VC12 or earlier (and MinGW). stat() can't handle |
| 507 | * symlinks properly. |
Bram Moolenaar | 2ee95f7 | 2013-09-25 19:13:38 +0200 | [diff] [blame] | 508 | * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves |
| 509 | * status of a symlink itself. |
| 510 | * VC10: stat() supports a symlink to a normal file, but it doesn't support |
Bram Moolenaar | fce7b3d | 2016-01-19 19:00:32 +0100 | [diff] [blame] | 511 | * a symlink to a directory (always returns an error). |
| 512 | * VC11 and VC12: stat() doesn't return an error for a symlink to a |
| 513 | * directory, but it doesn't set S_IFDIR flag. |
| 514 | * MinGW: Same as VC9. */ |
Bram Moolenaar | 2ee95f7 | 2013-09-25 19:13:38 +0200 | [diff] [blame] | 515 | WIN32_FIND_DATA findData; |
| 516 | HANDLE hFind, h; |
| 517 | DWORD attr = 0; |
| 518 | BOOL is_symlink = FALSE; |
| 519 | |
| 520 | hFind = FindFirstFile(name, &findData); |
| 521 | if (hFind != INVALID_HANDLE_VALUE) |
| 522 | { |
| 523 | attr = findData.dwFileAttributes; |
| 524 | if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) |
| 525 | && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) |
| 526 | is_symlink = TRUE; |
| 527 | FindClose(hFind); |
| 528 | } |
| 529 | if (is_symlink) |
| 530 | { |
| 531 | h = CreateFile(name, FILE_READ_ATTRIBUTES, |
| 532 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 533 | OPEN_EXISTING, |
| 534 | (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 535 | ? FILE_FLAG_BACKUP_SEMANTICS : 0, |
| 536 | NULL); |
| 537 | if (h != INVALID_HANDLE_VALUE) |
| 538 | { |
| 539 | int fd, n; |
| 540 | |
Bram Moolenaar | 8962fda | 2013-09-29 19:05:21 +0200 | [diff] [blame] | 541 | fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY); |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 542 | n = _fstat(fd, (struct _stat *)stp); |
Bram Moolenaar | fce7b3d | 2016-01-19 19:00:32 +0100 | [diff] [blame] | 543 | if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY)) |
| 544 | stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR; |
Bram Moolenaar | 2ee95f7 | 2013-09-25 19:13:38 +0200 | [diff] [blame] | 545 | _close(fd); |
| 546 | return n; |
| 547 | } |
| 548 | } |
| 549 | #endif |
| 550 | return stat(name, stp); |
| 551 | } |
| 552 | |
| 553 | #ifdef FEAT_MBYTE |
| 554 | static int |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 555 | wstat_symlink_aware(const WCHAR *name, stat_T *stp) |
Bram Moolenaar | 2ee95f7 | 2013-09-25 19:13:38 +0200 | [diff] [blame] | 556 | { |
Bram Moolenaar | fce7b3d | 2016-01-19 19:00:32 +0100 | [diff] [blame] | 557 | # if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__) |
| 558 | /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle |
| 559 | * symlinks properly. |
Bram Moolenaar | 2ee95f7 | 2013-09-25 19:13:38 +0200 | [diff] [blame] | 560 | * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves |
| 561 | * status of a symlink itself. |
| 562 | * VC10: _wstat() supports a symlink to a normal file, but it doesn't |
Bram Moolenaar | fce7b3d | 2016-01-19 19:00:32 +0100 | [diff] [blame] | 563 | * support a symlink to a directory (always returns an error). |
| 564 | * VC11 and VC12: _wstat() doesn't return an error for a symlink to a |
| 565 | * directory, but it doesn't set S_IFDIR flag. |
| 566 | * MinGW: Same as VC9. */ |
Bram Moolenaar | 2ee95f7 | 2013-09-25 19:13:38 +0200 | [diff] [blame] | 567 | int n; |
| 568 | BOOL is_symlink = FALSE; |
| 569 | HANDLE hFind, h; |
| 570 | DWORD attr = 0; |
| 571 | WIN32_FIND_DATAW findDataW; |
| 572 | |
| 573 | hFind = FindFirstFileW(name, &findDataW); |
| 574 | if (hFind != INVALID_HANDLE_VALUE) |
| 575 | { |
| 576 | attr = findDataW.dwFileAttributes; |
| 577 | if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) |
| 578 | && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) |
| 579 | is_symlink = TRUE; |
| 580 | FindClose(hFind); |
| 581 | } |
| 582 | if (is_symlink) |
| 583 | { |
| 584 | h = CreateFileW(name, FILE_READ_ATTRIBUTES, |
| 585 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 586 | OPEN_EXISTING, |
| 587 | (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 588 | ? FILE_FLAG_BACKUP_SEMANTICS : 0, |
| 589 | NULL); |
| 590 | if (h != INVALID_HANDLE_VALUE) |
| 591 | { |
| 592 | int fd; |
| 593 | |
Bram Moolenaar | 8962fda | 2013-09-29 19:05:21 +0200 | [diff] [blame] | 594 | fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY); |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 595 | n = _fstat(fd, (struct _stat *)stp); |
Bram Moolenaar | fce7b3d | 2016-01-19 19:00:32 +0100 | [diff] [blame] | 596 | if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY)) |
| 597 | stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR; |
Bram Moolenaar | 2ee95f7 | 2013-09-25 19:13:38 +0200 | [diff] [blame] | 598 | _close(fd); |
| 599 | return n; |
| 600 | } |
| 601 | } |
| 602 | # endif |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 603 | return _wstat(name, (struct _stat *)stp); |
Bram Moolenaar | 2ee95f7 | 2013-09-25 19:13:38 +0200 | [diff] [blame] | 604 | } |
| 605 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 606 | |
| 607 | /* |
| 608 | * stat() can't handle a trailing '/' or '\', remove it first. |
| 609 | */ |
| 610 | int |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 611 | vim_stat(const char *name, stat_T *stp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 612 | { |
Bram Moolenaar | d2a203b | 2013-08-30 16:51:18 +0200 | [diff] [blame] | 613 | #ifdef FEAT_MBYTE |
| 614 | /* WinNT and later can use _MAX_PATH wide characters for a pathname, which |
| 615 | * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is |
| 616 | * UTF-8. */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 617 | char_u buf[_MAX_PATH * 3 + 1]; |
Bram Moolenaar | d2a203b | 2013-08-30 16:51:18 +0200 | [diff] [blame] | 618 | #else |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 619 | char_u buf[_MAX_PATH + 1]; |
Bram Moolenaar | d2a203b | 2013-08-30 16:51:18 +0200 | [diff] [blame] | 620 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 621 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 622 | |
Bram Moolenaar | d2a203b | 2013-08-30 16:51:18 +0200 | [diff] [blame] | 623 | vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1); |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 624 | p = buf + STRLEN(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 625 | if (p > buf) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 626 | mb_ptr_back(buf, p); |
Bram Moolenaar | b1cb35f | 2014-01-10 13:05:20 +0100 | [diff] [blame] | 627 | |
| 628 | /* Remove trailing '\\' except root path. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 629 | if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':') |
| 630 | *p = NUL; |
Bram Moolenaar | b1cb35f | 2014-01-10 13:05:20 +0100 | [diff] [blame] | 631 | |
| 632 | if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/')) |
| 633 | { |
| 634 | /* UNC root path must be followed by '\\'. */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 635 | p = vim_strpbrk(buf + 2, (char_u *)"\\/"); |
Bram Moolenaar | b1cb35f | 2014-01-10 13:05:20 +0100 | [diff] [blame] | 636 | if (p != NULL) |
| 637 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 638 | p = vim_strpbrk(p + 1, (char_u *)"\\/"); |
Bram Moolenaar | b1cb35f | 2014-01-10 13:05:20 +0100 | [diff] [blame] | 639 | if (p == NULL) |
| 640 | STRCAT(buf, "\\"); |
| 641 | } |
| 642 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 643 | #ifdef FEAT_MBYTE |
| 644 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage |
| 645 | # ifdef __BORLANDC__ |
| 646 | /* Wide functions of Borland C 5.5 do not work on Windows 98. */ |
| 647 | && g_PlatformId == VER_PLATFORM_WIN32_NT |
| 648 | # endif |
| 649 | ) |
| 650 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 651 | WCHAR *wp = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 652 | int n; |
| 653 | |
| 654 | if (wp != NULL) |
| 655 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 656 | n = wstat_symlink_aware(wp, stp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 657 | vim_free(wp); |
Bram Moolenaar | cd981f2 | 2014-02-11 17:06:00 +0100 | [diff] [blame] | 658 | if (n >= 0 || g_PlatformId == VER_PLATFORM_WIN32_NT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 659 | return n; |
| 660 | /* Retry with non-wide function (for Windows 98). Can't use |
| 661 | * GetLastError() here and it's unclear what errno gets set to if |
| 662 | * the _wstat() fails for missing wide functions. */ |
| 663 | } |
| 664 | } |
| 665 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 666 | return stat_symlink_aware((char *)buf, stp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | #if defined(FEAT_GUI_MSWIN) || defined(PROTO) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 670 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 671 | void |
| 672 | mch_settmode(int tmode) |
| 673 | { |
| 674 | /* nothing to do */ |
| 675 | } |
| 676 | |
| 677 | int |
| 678 | mch_get_shellsize(void) |
| 679 | { |
| 680 | /* never used */ |
| 681 | return OK; |
| 682 | } |
| 683 | |
| 684 | void |
| 685 | mch_set_shellsize(void) |
| 686 | { |
| 687 | /* never used */ |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Rows and/or Columns has changed. |
| 692 | */ |
| 693 | void |
| 694 | mch_new_shellsize(void) |
| 695 | { |
| 696 | /* never used */ |
| 697 | } |
| 698 | |
| 699 | #endif |
| 700 | |
| 701 | /* |
| 702 | * We have no job control, so fake it by starting a new shell. |
| 703 | */ |
| 704 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 705 | mch_suspend(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 706 | { |
| 707 | suspend_shell(); |
| 708 | } |
| 709 | |
| 710 | #if defined(USE_MCH_ERRMSG) || defined(PROTO) |
| 711 | |
| 712 | #ifdef display_errors |
| 713 | # undef display_errors |
| 714 | #endif |
| 715 | |
| 716 | /* |
| 717 | * Display the saved error message(s). |
| 718 | */ |
| 719 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 720 | display_errors(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 721 | { |
| 722 | char *p; |
| 723 | |
| 724 | if (error_ga.ga_data != NULL) |
| 725 | { |
| 726 | /* avoid putting up a message box with blanks only */ |
| 727 | for (p = (char *)error_ga.ga_data; *p; ++p) |
| 728 | if (!isspace(*p)) |
| 729 | { |
Bram Moolenaar | c17ef8e | 2006-03-25 21:48:58 +0000 | [diff] [blame] | 730 | (void)gui_mch_dialog( |
| 731 | #ifdef FEAT_GUI |
| 732 | gui.starting ? VIM_INFO : |
| 733 | #endif |
| 734 | VIM_ERROR, |
| 735 | #ifdef FEAT_GUI |
| 736 | gui.starting ? (char_u *)_("Message") : |
| 737 | #endif |
| 738 | (char_u *)_("Error"), |
Bram Moolenaar | 418f81b | 2016-02-16 20:12:02 +0100 | [diff] [blame] | 739 | (char_u *)p, (char_u *)_("&Ok"), |
| 740 | 1, NULL, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 741 | break; |
| 742 | } |
| 743 | ga_clear(&error_ga); |
| 744 | } |
| 745 | } |
| 746 | #endif |
| 747 | |
| 748 | |
| 749 | /* |
| 750 | * Return TRUE if "p" contain a wildcard that can be expanded by |
| 751 | * dos_expandpath(). |
| 752 | */ |
| 753 | int |
| 754 | mch_has_exp_wildcard(char_u *p) |
| 755 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 756 | for ( ; *p; mb_ptr_adv(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 757 | { |
| 758 | if (vim_strchr((char_u *)"?*[", *p) != NULL |
| 759 | || (*p == '~' && p[1] != NUL)) |
| 760 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 761 | } |
| 762 | return FALSE; |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a |
| 767 | * shortened file name). |
| 768 | */ |
| 769 | int |
| 770 | mch_has_wildcard(char_u *p) |
| 771 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 772 | for ( ; *p; mb_ptr_adv(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 773 | { |
| 774 | if (vim_strchr((char_u *) |
| 775 | # ifdef VIM_BACKTICK |
| 776 | "?*$[`" |
| 777 | # else |
| 778 | "?*$[" |
| 779 | # endif |
| 780 | , *p) != NULL |
| 781 | || (*p == '~' && p[1] != NUL)) |
| 782 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 783 | } |
| 784 | return FALSE; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | /* |
| 789 | * The normal _chdir() does not change the default drive. This one does. |
| 790 | * Returning 0 implies success; -1 implies failure. |
| 791 | */ |
| 792 | int |
| 793 | mch_chdir(char *path) |
| 794 | { |
| 795 | if (path[0] == NUL) /* just checking... */ |
| 796 | return -1; |
| 797 | |
Bram Moolenaar | a2974d7 | 2009-07-14 16:38:36 +0000 | [diff] [blame] | 798 | if (p_verbose >= 5) |
| 799 | { |
| 800 | verbose_enter(); |
| 801 | smsg((char_u *)"chdir(%s)", path); |
| 802 | verbose_leave(); |
| 803 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 804 | if (isalpha(path[0]) && path[1] == ':') /* has a drive name */ |
| 805 | { |
| 806 | /* If we can change to the drive, skip that part of the path. If we |
| 807 | * can't then the current directory may be invalid, try using chdir() |
| 808 | * with the whole path. */ |
| 809 | if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0) |
| 810 | path += 2; |
| 811 | } |
| 812 | |
| 813 | if (*path == NUL) /* drive name only */ |
| 814 | return 0; |
| 815 | |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 816 | #ifdef FEAT_MBYTE |
| 817 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 818 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 819 | WCHAR *p = enc_to_utf16((char_u *)path, NULL); |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 820 | int n; |
| 821 | |
| 822 | if (p != NULL) |
| 823 | { |
| 824 | n = _wchdir(p); |
| 825 | vim_free(p); |
Bram Moolenaar | cd981f2 | 2014-02-11 17:06:00 +0100 | [diff] [blame] | 826 | if (n == 0 || g_PlatformId == VER_PLATFORM_WIN32_NT) |
| 827 | return n; |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 828 | /* Retry with non-wide function (for Windows 98). */ |
| 829 | } |
| 830 | } |
| 831 | #endif |
| 832 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 833 | return chdir(path); /* let the normal chdir() do the rest */ |
| 834 | } |
| 835 | |
| 836 | |
| 837 | /* |
| 838 | * Switching off termcap mode is only allowed when Columns is 80, otherwise a |
| 839 | * crash may result. It's always allowed on NT or when running the GUI. |
| 840 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 841 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 842 | int |
| 843 | can_end_termcap_mode( |
| 844 | int give_msg) |
| 845 | { |
| 846 | #ifdef FEAT_GUI_MSWIN |
| 847 | return TRUE; /* GUI starts a new console anyway */ |
| 848 | #else |
| 849 | if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80) |
| 850 | return TRUE; |
| 851 | if (give_msg) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 852 | msg((char_u *) |
| 853 | _("'columns' is not 80, cannot execute external commands")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 854 | return FALSE; |
| 855 | #endif |
| 856 | } |
| 857 | |
| 858 | #ifdef FEAT_GUI_MSWIN |
| 859 | /* |
| 860 | * return non-zero if a character is available |
| 861 | */ |
| 862 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 863 | mch_char_avail(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 864 | { |
| 865 | /* never used */ |
| 866 | return TRUE; |
| 867 | } |
| 868 | #endif |
| 869 | |
| 870 | |
| 871 | /* |
| 872 | * set screen mode, always fails. |
| 873 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 874 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 875 | int |
| 876 | mch_screenmode( |
| 877 | char_u *arg) |
| 878 | { |
| 879 | EMSG(_(e_screenmode)); |
| 880 | return FAIL; |
| 881 | } |
| 882 | |
| 883 | |
| 884 | #if defined(FEAT_LIBCALL) || defined(PROTO) |
| 885 | /* |
| 886 | * Call a DLL routine which takes either a string or int param |
| 887 | * and returns an allocated string. |
| 888 | * Return OK if it worked, FAIL if not. |
| 889 | */ |
| 890 | # ifdef WIN3264 |
| 891 | typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR); |
| 892 | typedef LPTSTR (*MYINTPROCSTR)(int); |
| 893 | typedef int (*MYSTRPROCINT)(LPTSTR); |
| 894 | typedef int (*MYINTPROCINT)(int); |
| 895 | # else |
| 896 | typedef LPSTR (*MYSTRPROCSTR)(LPSTR); |
| 897 | typedef LPSTR (*MYINTPROCSTR)(int); |
| 898 | typedef int (*MYSTRPROCINT)(LPSTR); |
| 899 | typedef int (*MYINTPROCINT)(int); |
| 900 | # endif |
| 901 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 902 | /* |
| 903 | * Check if a pointer points to a valid NUL terminated string. |
| 904 | * Return the length of the string, including terminating NUL. |
| 905 | * Returns 0 for an invalid pointer, 1 for an empty string. |
| 906 | */ |
| 907 | static size_t |
| 908 | check_str_len(char_u *str) |
| 909 | { |
| 910 | SYSTEM_INFO si; |
| 911 | MEMORY_BASIC_INFORMATION mbi; |
| 912 | size_t length = 0; |
| 913 | size_t i; |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 914 | const char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 915 | |
| 916 | /* get page size */ |
| 917 | GetSystemInfo(&si); |
| 918 | |
| 919 | /* get memory information */ |
| 920 | if (VirtualQuery(str, &mbi, sizeof(mbi))) |
| 921 | { |
| 922 | /* pre cast these (typing savers) */ |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 923 | long_u dwStr = (long_u)str; |
| 924 | long_u dwBaseAddress = (long_u)mbi.BaseAddress; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 925 | |
| 926 | /* get start address of page that str is on */ |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 927 | long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 928 | |
| 929 | /* get length from str to end of page */ |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 930 | long_u pageLength = si.dwPageSize - (dwStr - strPage); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 931 | |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 932 | for (p = str; !IsBadReadPtr(p, (UINT)pageLength); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 933 | p += pageLength, pageLength = si.dwPageSize) |
| 934 | for (i = 0; i < pageLength; ++i, ++length) |
| 935 | if (p[i] == NUL) |
| 936 | return length + 1; |
| 937 | } |
| 938 | |
| 939 | return 0; |
| 940 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 941 | |
Bram Moolenaar | cddc91c | 2014-09-23 21:53:41 +0200 | [diff] [blame] | 942 | /* |
| 943 | * Passed to do_in_runtimepath() to load a vim.ico file. |
| 944 | */ |
| 945 | static void |
| 946 | mch_icon_load_cb(char_u *fname, void *cookie) |
| 947 | { |
| 948 | HANDLE *h = (HANDLE *)cookie; |
| 949 | |
| 950 | *h = LoadImage(NULL, |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 951 | (LPSTR)fname, |
Bram Moolenaar | cddc91c | 2014-09-23 21:53:41 +0200 | [diff] [blame] | 952 | IMAGE_ICON, |
| 953 | 64, |
| 954 | 64, |
| 955 | LR_LOADFROMFILE | LR_LOADMAP3DCOLORS); |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Try loading an icon file from 'runtimepath'. |
| 960 | */ |
| 961 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 962 | mch_icon_load(HANDLE *iconp) |
Bram Moolenaar | cddc91c | 2014-09-23 21:53:41 +0200 | [diff] [blame] | 963 | { |
| 964 | return do_in_runtimepath((char_u *)"bitmaps/vim.ico", |
Bram Moolenaar | 7f8989d | 2016-03-12 22:11:39 +0100 | [diff] [blame] | 965 | 0, mch_icon_load_cb, iconp); |
Bram Moolenaar | cddc91c | 2014-09-23 21:53:41 +0200 | [diff] [blame] | 966 | } |
| 967 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 968 | int |
| 969 | mch_libcall( |
| 970 | char_u *libname, |
| 971 | char_u *funcname, |
| 972 | char_u *argstring, /* NULL when using a argint */ |
| 973 | int argint, |
| 974 | char_u **string_result,/* NULL when using number_result */ |
| 975 | int *number_result) |
| 976 | { |
| 977 | HINSTANCE hinstLib; |
| 978 | MYSTRPROCSTR ProcAdd; |
| 979 | MYINTPROCSTR ProcAddI; |
| 980 | char_u *retval_str = NULL; |
| 981 | int retval_int = 0; |
| 982 | size_t len; |
| 983 | |
| 984 | BOOL fRunTimeLinkSuccess = FALSE; |
| 985 | |
| 986 | // Get a handle to the DLL module. |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 987 | hinstLib = vimLoadLib((char *)libname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 988 | |
| 989 | // If the handle is valid, try to get the function address. |
| 990 | if (hinstLib != NULL) |
| 991 | { |
| 992 | #ifdef HAVE_TRY_EXCEPT |
| 993 | __try |
| 994 | { |
| 995 | #endif |
| 996 | if (argstring != NULL) |
| 997 | { |
| 998 | /* Call with string argument */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 999 | ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1000 | if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0) |
| 1001 | { |
| 1002 | if (string_result == NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1003 | retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1004 | else |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1005 | retval_str = (char_u *)(ProcAdd)((LPSTR)argstring); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | else |
| 1009 | { |
| 1010 | /* Call with number argument */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1011 | ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1012 | if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0) |
| 1013 | { |
| 1014 | if (string_result == NULL) |
| 1015 | retval_int = ((MYINTPROCINT)ProcAddI)(argint); |
| 1016 | else |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1017 | retval_str = (char_u *)(ProcAddI)(argint); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | // Save the string before we free the library. |
| 1022 | // Assume that a "1" result is an illegal pointer. |
| 1023 | if (string_result == NULL) |
| 1024 | *number_result = retval_int; |
| 1025 | else if (retval_str != NULL |
Bram Moolenaar | cf7164a | 2016-02-20 13:55:06 +0100 | [diff] [blame] | 1026 | && (len = check_str_len(retval_str)) > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1027 | { |
| 1028 | *string_result = lalloc((long_u)len, TRUE); |
| 1029 | if (*string_result != NULL) |
| 1030 | mch_memmove(*string_result, retval_str, len); |
| 1031 | } |
| 1032 | |
| 1033 | #ifdef HAVE_TRY_EXCEPT |
| 1034 | } |
| 1035 | __except(EXCEPTION_EXECUTE_HANDLER) |
| 1036 | { |
| 1037 | if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW) |
| 1038 | RESETSTKOFLW(); |
| 1039 | fRunTimeLinkSuccess = 0; |
| 1040 | } |
| 1041 | #endif |
| 1042 | |
| 1043 | // Free the DLL module. |
| 1044 | (void)FreeLibrary(hinstLib); |
| 1045 | } |
| 1046 | |
| 1047 | if (!fRunTimeLinkSuccess) |
| 1048 | { |
| 1049 | EMSG2(_(e_libcall), funcname); |
| 1050 | return FAIL; |
| 1051 | } |
| 1052 | |
| 1053 | return OK; |
| 1054 | } |
| 1055 | #endif |
| 1056 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1057 | /* |
| 1058 | * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules |
| 1059 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1060 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1061 | void |
| 1062 | DumpPutS( |
| 1063 | const char *psz) |
| 1064 | { |
| 1065 | # ifdef MCH_WRITE_DUMP |
| 1066 | if (fdDump) |
| 1067 | { |
| 1068 | fputs(psz, fdDump); |
| 1069 | if (psz[strlen(psz) - 1] != '\n') |
| 1070 | fputc('\n', fdDump); |
| 1071 | fflush(fdDump); |
| 1072 | } |
| 1073 | # endif |
| 1074 | } |
| 1075 | |
| 1076 | #ifdef _DEBUG |
| 1077 | |
| 1078 | void __cdecl |
| 1079 | Trace( |
| 1080 | char *pszFormat, |
| 1081 | ...) |
| 1082 | { |
| 1083 | CHAR szBuff[2048]; |
| 1084 | va_list args; |
| 1085 | |
| 1086 | va_start(args, pszFormat); |
| 1087 | vsprintf(szBuff, pszFormat, args); |
| 1088 | va_end(args); |
| 1089 | |
| 1090 | OutputDebugString(szBuff); |
| 1091 | } |
| 1092 | |
| 1093 | #endif //_DEBUG |
| 1094 | |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 1095 | #if !defined(FEAT_GUI) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1096 | # if defined(FEAT_TITLE) && defined(WIN3264) |
| 1097 | extern HWND g_hWnd; /* This is in os_win32.c. */ |
| 1098 | # endif |
| 1099 | |
| 1100 | /* |
| 1101 | * Showing the printer dialog is tricky since we have no GUI |
| 1102 | * window to parent it. The following routines are needed to |
| 1103 | * get the window parenting and Z-order to work properly. |
| 1104 | */ |
| 1105 | static void |
| 1106 | GetConsoleHwnd(void) |
| 1107 | { |
| 1108 | # define MY_BUFSIZE 1024 // Buffer size for console window titles. |
| 1109 | |
| 1110 | char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle. |
| 1111 | char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle. |
| 1112 | |
| 1113 | /* Skip if it's already set. */ |
| 1114 | if (s_hwnd != 0) |
| 1115 | return; |
| 1116 | |
| 1117 | # if defined(FEAT_TITLE) && defined(WIN3264) |
| 1118 | /* Window handle may have been found by init code (Windows NT only) */ |
| 1119 | if (g_hWnd != 0) |
| 1120 | { |
| 1121 | s_hwnd = g_hWnd; |
| 1122 | return; |
| 1123 | } |
| 1124 | # endif |
| 1125 | |
| 1126 | GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE); |
| 1127 | |
| 1128 | wsprintf(pszNewWindowTitle, "%s/%d/%d", |
| 1129 | pszOldWindowTitle, |
| 1130 | GetTickCount(), |
| 1131 | GetCurrentProcessId()); |
| 1132 | SetConsoleTitle(pszNewWindowTitle); |
| 1133 | Sleep(40); |
| 1134 | s_hwnd = FindWindow(NULL, pszNewWindowTitle); |
| 1135 | |
| 1136 | SetConsoleTitle(pszOldWindowTitle); |
| 1137 | } |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 1138 | |
| 1139 | /* |
| 1140 | * Console implementation of ":winpos". |
| 1141 | */ |
| 1142 | int |
| 1143 | mch_get_winpos(int *x, int *y) |
| 1144 | { |
| 1145 | RECT rect; |
| 1146 | |
| 1147 | GetConsoleHwnd(); |
| 1148 | GetWindowRect(s_hwnd, &rect); |
| 1149 | *x = rect.left; |
| 1150 | *y = rect.top; |
| 1151 | return OK; |
| 1152 | } |
| 1153 | |
| 1154 | /* |
| 1155 | * Console implementation of ":winpos x y". |
| 1156 | */ |
| 1157 | void |
| 1158 | mch_set_winpos(int x, int y) |
| 1159 | { |
| 1160 | GetConsoleHwnd(); |
| 1161 | SetWindowPos(s_hwnd, NULL, x, y, 0, 0, |
| 1162 | SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE); |
| 1163 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1164 | #endif |
| 1165 | |
| 1166 | #if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO) |
| 1167 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1168 | /*================================================================= |
| 1169 | * Win32 printer stuff |
| 1170 | */ |
| 1171 | |
| 1172 | static HFONT prt_font_handles[2][2][2]; |
| 1173 | static PRINTDLG prt_dlg; |
| 1174 | static const int boldface[2] = {FW_REGULAR, FW_BOLD}; |
| 1175 | static TEXTMETRIC prt_tm; |
| 1176 | static int prt_line_height; |
| 1177 | static int prt_number_width; |
| 1178 | static int prt_left_margin; |
| 1179 | static int prt_right_margin; |
| 1180 | static int prt_top_margin; |
| 1181 | static char_u szAppName[] = TEXT("VIM"); |
| 1182 | static HWND hDlgPrint; |
| 1183 | static int *bUserAbort = NULL; |
| 1184 | static char_u *prt_name = NULL; |
| 1185 | |
| 1186 | /* Defines which are also in vim.rc. */ |
| 1187 | #define IDC_BOX1 400 |
| 1188 | #define IDC_PRINTTEXT1 401 |
| 1189 | #define IDC_PRINTTEXT2 402 |
| 1190 | #define IDC_PROGRESS 403 |
| 1191 | |
Bram Moolenaar | cf7164a | 2016-02-20 13:55:06 +0100 | [diff] [blame] | 1192 | #if !defined(FEAT_MBYTE) |
Bram Moolenaar | 5246cd7 | 2013-06-16 16:41:47 +0200 | [diff] [blame] | 1193 | # define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s) |
| 1194 | #else |
| 1195 | static BOOL |
| 1196 | vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s) |
| 1197 | { |
| 1198 | WCHAR *wp = NULL; |
| 1199 | BOOL ret; |
| 1200 | |
| 1201 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 1202 | { |
| 1203 | wp = enc_to_utf16(s, NULL); |
| 1204 | } |
| 1205 | if (wp != NULL) |
| 1206 | { |
| 1207 | ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp); |
| 1208 | vim_free(wp); |
| 1209 | return ret; |
| 1210 | } |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1211 | return SetDlgItemText(hDlg, nIDDlgItem, (LPCSTR)s); |
Bram Moolenaar | 5246cd7 | 2013-06-16 16:41:47 +0200 | [diff] [blame] | 1212 | } |
| 1213 | #endif |
| 1214 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1215 | /* |
| 1216 | * Convert BGR to RGB for Windows GDI calls |
| 1217 | */ |
| 1218 | static COLORREF |
| 1219 | swap_me(COLORREF colorref) |
| 1220 | { |
| 1221 | int temp; |
| 1222 | char *ptr = (char *)&colorref; |
| 1223 | |
| 1224 | temp = *(ptr); |
| 1225 | *(ptr ) = *(ptr + 2); |
| 1226 | *(ptr + 2) = temp; |
| 1227 | return colorref; |
| 1228 | } |
| 1229 | |
Bram Moolenaar | ed39e1d | 2008-08-09 17:55:22 +0000 | [diff] [blame] | 1230 | /* Attempt to make this work for old and new compilers */ |
Bram Moolenaar | 9f733d1 | 2011-09-21 20:09:42 +0200 | [diff] [blame] | 1231 | #if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300) |
Bram Moolenaar | ed39e1d | 2008-08-09 17:55:22 +0000 | [diff] [blame] | 1232 | # define PDP_RETVAL BOOL |
| 1233 | #else |
| 1234 | # define PDP_RETVAL INT_PTR |
| 1235 | #endif |
| 1236 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1237 | /*ARGSUSED*/ |
Bram Moolenaar | ed39e1d | 2008-08-09 17:55:22 +0000 | [diff] [blame] | 1238 | static PDP_RETVAL CALLBACK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1239 | PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) |
| 1240 | { |
| 1241 | #ifdef FEAT_GETTEXT |
| 1242 | NONCLIENTMETRICS nm; |
| 1243 | static HFONT hfont; |
| 1244 | #endif |
| 1245 | |
| 1246 | switch (message) |
| 1247 | { |
| 1248 | case WM_INITDIALOG: |
| 1249 | #ifdef FEAT_GETTEXT |
| 1250 | nm.cbSize = sizeof(NONCLIENTMETRICS); |
| 1251 | if (SystemParametersInfo( |
| 1252 | SPI_GETNONCLIENTMETRICS, |
| 1253 | sizeof(NONCLIENTMETRICS), |
| 1254 | &nm, |
| 1255 | 0)) |
| 1256 | { |
| 1257 | char buff[MAX_PATH]; |
| 1258 | int i; |
| 1259 | |
| 1260 | /* Translate the dialog texts */ |
| 1261 | hfont = CreateFontIndirect(&nm.lfMessageFont); |
| 1262 | for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++) |
| 1263 | { |
| 1264 | SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1); |
| 1265 | if (GetDlgItemText(hDlg,i, buff, sizeof(buff))) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1266 | vimSetDlgItemText(hDlg,i, (char_u *)_(buff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1267 | } |
| 1268 | SendDlgItemMessage(hDlg, IDCANCEL, |
| 1269 | WM_SETFONT, (WPARAM)hfont, 1); |
| 1270 | if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff))) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1271 | vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1272 | } |
| 1273 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1274 | SetWindowText(hDlg, (LPCSTR)szAppName); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1275 | if (prt_name != NULL) |
| 1276 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1277 | vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1278 | vim_free(prt_name); |
| 1279 | prt_name = NULL; |
| 1280 | } |
| 1281 | EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED); |
| 1282 | #ifndef FEAT_GUI |
| 1283 | BringWindowToTop(s_hwnd); |
| 1284 | #endif |
| 1285 | return TRUE; |
| 1286 | |
| 1287 | case WM_COMMAND: |
| 1288 | *bUserAbort = TRUE; |
| 1289 | EnableWindow(GetParent(hDlg), TRUE); |
| 1290 | DestroyWindow(hDlg); |
| 1291 | hDlgPrint = NULL; |
| 1292 | #ifdef FEAT_GETTEXT |
| 1293 | DeleteObject(hfont); |
| 1294 | #endif |
| 1295 | return TRUE; |
| 1296 | } |
| 1297 | return FALSE; |
| 1298 | } |
| 1299 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1300 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1301 | static BOOL CALLBACK |
| 1302 | AbortProc(HDC hdcPrn, int iCode) |
| 1303 | { |
| 1304 | MSG msg; |
| 1305 | |
Bram Moolenaar | 8c85fa3 | 2011-08-10 17:08:03 +0200 | [diff] [blame] | 1306 | while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1307 | { |
Bram Moolenaar | 8c85fa3 | 2011-08-10 17:08:03 +0200 | [diff] [blame] | 1308 | if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1309 | { |
| 1310 | TranslateMessage(&msg); |
Bram Moolenaar | 8c85fa3 | 2011-08-10 17:08:03 +0200 | [diff] [blame] | 1311 | pDispatchMessage(&msg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1312 | } |
| 1313 | } |
| 1314 | return !*bUserAbort; |
| 1315 | } |
| 1316 | |
| 1317 | #ifndef FEAT_GUI |
| 1318 | |
Bram Moolenaar | 26fdd7d | 2011-11-30 13:42:44 +0100 | [diff] [blame] | 1319 | static UINT_PTR CALLBACK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1320 | PrintHookProc( |
| 1321 | HWND hDlg, // handle to dialog box |
| 1322 | UINT uiMsg, // message identifier |
| 1323 | WPARAM wParam, // message parameter |
| 1324 | LPARAM lParam // message parameter |
| 1325 | ) |
| 1326 | { |
| 1327 | HWND hwndOwner; |
| 1328 | RECT rc, rcDlg, rcOwner; |
| 1329 | PRINTDLG *pPD; |
| 1330 | |
| 1331 | if (uiMsg == WM_INITDIALOG) |
| 1332 | { |
| 1333 | // Get the owner window and dialog box rectangles. |
| 1334 | if ((hwndOwner = GetParent(hDlg)) == NULL) |
| 1335 | hwndOwner = GetDesktopWindow(); |
| 1336 | |
| 1337 | GetWindowRect(hwndOwner, &rcOwner); |
| 1338 | GetWindowRect(hDlg, &rcDlg); |
| 1339 | CopyRect(&rc, &rcOwner); |
| 1340 | |
| 1341 | // Offset the owner and dialog box rectangles so that |
| 1342 | // right and bottom values represent the width and |
| 1343 | // height, and then offset the owner again to discard |
| 1344 | // space taken up by the dialog box. |
| 1345 | |
| 1346 | OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top); |
| 1347 | OffsetRect(&rc, -rc.left, -rc.top); |
| 1348 | OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom); |
| 1349 | |
| 1350 | // The new position is the sum of half the remaining |
| 1351 | // space and the owner's original position. |
| 1352 | |
| 1353 | SetWindowPos(hDlg, |
| 1354 | HWND_TOP, |
| 1355 | rcOwner.left + (rc.right / 2), |
| 1356 | rcOwner.top + (rc.bottom / 2), |
| 1357 | 0, 0, // ignores size arguments |
| 1358 | SWP_NOSIZE); |
| 1359 | |
| 1360 | /* tackle the printdlg copiesctrl problem */ |
| 1361 | pPD = (PRINTDLG *)lParam; |
| 1362 | pPD->nCopies = (WORD)pPD->lCustData; |
| 1363 | SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE ); |
| 1364 | /* Bring the window to top */ |
| 1365 | BringWindowToTop(GetParent(hDlg)); |
| 1366 | SetForegroundWindow(hDlg); |
| 1367 | } |
| 1368 | |
| 1369 | return FALSE; |
| 1370 | } |
| 1371 | #endif |
| 1372 | |
| 1373 | void |
| 1374 | mch_print_cleanup(void) |
| 1375 | { |
| 1376 | int pifItalic; |
| 1377 | int pifBold; |
| 1378 | int pifUnderline; |
| 1379 | |
| 1380 | for (pifBold = 0; pifBold <= 1; pifBold++) |
| 1381 | for (pifItalic = 0; pifItalic <= 1; pifItalic++) |
| 1382 | for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++) |
| 1383 | DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]); |
| 1384 | |
| 1385 | if (prt_dlg.hDC != NULL) |
| 1386 | DeleteDC(prt_dlg.hDC); |
| 1387 | if (!*bUserAbort) |
| 1388 | SendMessage(hDlgPrint, WM_COMMAND, 0, 0); |
| 1389 | } |
| 1390 | |
| 1391 | static int |
| 1392 | to_device_units(int idx, int dpi, int physsize, int offset, int def_number) |
| 1393 | { |
| 1394 | int ret = 0; |
| 1395 | int u; |
| 1396 | int nr; |
| 1397 | |
| 1398 | u = prt_get_unit(idx); |
| 1399 | if (u == PRT_UNIT_NONE) |
| 1400 | { |
| 1401 | u = PRT_UNIT_PERC; |
| 1402 | nr = def_number; |
| 1403 | } |
| 1404 | else |
| 1405 | nr = printer_opts[idx].number; |
| 1406 | |
| 1407 | switch (u) |
| 1408 | { |
| 1409 | case PRT_UNIT_PERC: |
| 1410 | ret = (physsize * nr) / 100; |
| 1411 | break; |
| 1412 | case PRT_UNIT_INCH: |
| 1413 | ret = (nr * dpi); |
| 1414 | break; |
| 1415 | case PRT_UNIT_MM: |
| 1416 | ret = (nr * 10 * dpi) / 254; |
| 1417 | break; |
| 1418 | case PRT_UNIT_POINT: |
| 1419 | ret = (nr * 10 * dpi) / 720; |
| 1420 | break; |
| 1421 | } |
| 1422 | |
| 1423 | if (ret < offset) |
| 1424 | return 0; |
| 1425 | else |
| 1426 | return ret - offset; |
| 1427 | } |
| 1428 | |
| 1429 | static int |
| 1430 | prt_get_cpl(void) |
| 1431 | { |
| 1432 | int hr; |
| 1433 | int phyw; |
| 1434 | int dvoff; |
| 1435 | int rev_offset; |
| 1436 | int dpi; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1437 | |
| 1438 | GetTextMetrics(prt_dlg.hDC, &prt_tm); |
| 1439 | prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading; |
| 1440 | |
| 1441 | hr = GetDeviceCaps(prt_dlg.hDC, HORZRES); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1442 | phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH); |
| 1443 | dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1444 | dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX); |
| 1445 | |
| 1446 | rev_offset = phyw - (dvoff + hr); |
| 1447 | |
| 1448 | prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10); |
| 1449 | if (prt_use_number()) |
| 1450 | { |
| 1451 | prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth; |
| 1452 | prt_left_margin += prt_number_width; |
| 1453 | } |
| 1454 | else |
| 1455 | prt_number_width = 0; |
| 1456 | |
| 1457 | prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw, |
| 1458 | rev_offset, 5); |
| 1459 | |
| 1460 | return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth; |
| 1461 | } |
| 1462 | |
| 1463 | static int |
| 1464 | prt_get_lpp(void) |
| 1465 | { |
| 1466 | int vr; |
| 1467 | int phyw; |
| 1468 | int dvoff; |
| 1469 | int rev_offset; |
| 1470 | int bottom_margin; |
| 1471 | int dpi; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1472 | |
| 1473 | vr = GetDeviceCaps(prt_dlg.hDC, VERTRES); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1474 | phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT); |
| 1475 | dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1476 | dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY); |
| 1477 | |
| 1478 | rev_offset = phyw - (dvoff + vr); |
| 1479 | |
| 1480 | prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5); |
| 1481 | |
| 1482 | /* adjust top margin if there is a header */ |
| 1483 | prt_top_margin += prt_line_height * prt_header_height(); |
| 1484 | |
| 1485 | bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw, |
| 1486 | rev_offset, 5); |
| 1487 | |
| 1488 | return (bottom_margin - prt_top_margin) / prt_line_height; |
| 1489 | } |
| 1490 | |
| 1491 | int |
| 1492 | mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit) |
| 1493 | { |
| 1494 | static HGLOBAL stored_dm = NULL; |
| 1495 | static HGLOBAL stored_devn = NULL; |
| 1496 | static int stored_nCopies = 1; |
| 1497 | static int stored_nFlags = 0; |
| 1498 | |
| 1499 | LOGFONT fLogFont; |
| 1500 | int pifItalic; |
| 1501 | int pifBold; |
| 1502 | int pifUnderline; |
| 1503 | |
| 1504 | DEVMODE *mem; |
| 1505 | DEVNAMES *devname; |
| 1506 | int i; |
| 1507 | |
| 1508 | bUserAbort = &(psettings->user_abort); |
Bram Moolenaar | 7db5fc8 | 2010-05-24 11:59:29 +0200 | [diff] [blame] | 1509 | vim_memset(&prt_dlg, 0, sizeof(PRINTDLG)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1510 | prt_dlg.lStructSize = sizeof(PRINTDLG); |
| 1511 | #ifndef FEAT_GUI |
| 1512 | GetConsoleHwnd(); /* get value of s_hwnd */ |
| 1513 | #endif |
| 1514 | prt_dlg.hwndOwner = s_hwnd; |
| 1515 | prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC; |
| 1516 | if (!forceit) |
| 1517 | { |
| 1518 | prt_dlg.hDevMode = stored_dm; |
| 1519 | prt_dlg.hDevNames = stored_devn; |
| 1520 | prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog |
| 1521 | #ifndef FEAT_GUI |
| 1522 | /* |
| 1523 | * Use hook to prevent console window being sent to back |
| 1524 | */ |
| 1525 | prt_dlg.lpfnPrintHook = PrintHookProc; |
| 1526 | prt_dlg.Flags |= PD_ENABLEPRINTHOOK; |
| 1527 | #endif |
| 1528 | prt_dlg.Flags |= stored_nFlags; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * If bang present, return default printer setup with no dialog |
| 1533 | * never show dialog if we are running over telnet |
| 1534 | */ |
| 1535 | if (forceit |
| 1536 | #ifndef FEAT_GUI |
| 1537 | || !term_console |
| 1538 | #endif |
| 1539 | ) |
| 1540 | { |
| 1541 | prt_dlg.Flags |= PD_RETURNDEFAULT; |
| 1542 | #ifdef WIN3264 |
| 1543 | /* |
| 1544 | * MSDN suggests setting the first parameter to WINSPOOL for |
| 1545 | * NT, but NULL appears to work just as well. |
| 1546 | */ |
| 1547 | if (*p_pdev != NUL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1548 | prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1549 | else |
| 1550 | #endif |
| 1551 | { |
| 1552 | prt_dlg.Flags |= PD_RETURNDEFAULT; |
| 1553 | if (PrintDlg(&prt_dlg) == 0) |
| 1554 | goto init_fail_dlg; |
| 1555 | } |
| 1556 | } |
| 1557 | else if (PrintDlg(&prt_dlg) == 0) |
| 1558 | goto init_fail_dlg; |
| 1559 | else |
| 1560 | { |
| 1561 | /* |
| 1562 | * keep the previous driver context |
| 1563 | */ |
| 1564 | stored_dm = prt_dlg.hDevMode; |
| 1565 | stored_devn = prt_dlg.hDevNames; |
| 1566 | stored_nFlags = prt_dlg.Flags; |
| 1567 | stored_nCopies = prt_dlg.nCopies; |
| 1568 | } |
| 1569 | |
| 1570 | if (prt_dlg.hDC == NULL) |
| 1571 | { |
| 1572 | EMSG(_("E237: Printer selection failed")); |
| 1573 | mch_print_cleanup(); |
| 1574 | return FALSE; |
| 1575 | } |
| 1576 | |
| 1577 | /* Not all printer drivers report the support of color (or grey) in the |
| 1578 | * same way. Let's set has_color if there appears to be some way to print |
| 1579 | * more than B&W. */ |
| 1580 | i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS); |
| 1581 | psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1 |
| 1582 | || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1 |
| 1583 | || i > 2 || i == -1); |
| 1584 | |
| 1585 | /* Ensure all font styles are baseline aligned */ |
| 1586 | SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT); |
| 1587 | |
| 1588 | /* |
| 1589 | * On some windows systems the nCopies parameter is not |
| 1590 | * passed back correctly. It must be retrieved from the |
| 1591 | * hDevMode struct. |
| 1592 | */ |
| 1593 | mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode); |
| 1594 | if (mem != NULL) |
| 1595 | { |
| 1596 | #ifdef WIN3264 |
| 1597 | if (mem->dmCopies != 1) |
| 1598 | stored_nCopies = mem->dmCopies; |
| 1599 | #endif |
| 1600 | if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX)) |
| 1601 | psettings->duplex = TRUE; |
| 1602 | if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR)) |
| 1603 | psettings->has_color = TRUE; |
| 1604 | } |
| 1605 | GlobalUnlock(prt_dlg.hDevMode); |
| 1606 | |
| 1607 | devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames); |
| 1608 | if (devname != 0) |
| 1609 | { |
| 1610 | char_u *printer_name = (char_u *)devname + devname->wDeviceOffset; |
| 1611 | char_u *port_name = (char_u *)devname +devname->wOutputOffset; |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1612 | char_u *text = (char_u *)_("to %s on %s"); |
Bram Moolenaar | f191d55 | 2014-10-09 17:05:56 +0200 | [diff] [blame] | 1613 | #ifdef FEAT_MBYTE |
| 1614 | char_u *printer_name_orig = printer_name; |
| 1615 | char_u *port_name_orig = port_name; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1616 | |
Bram Moolenaar | f191d55 | 2014-10-09 17:05:56 +0200 | [diff] [blame] | 1617 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 1618 | { |
| 1619 | char_u *to_free = NULL; |
| 1620 | int maxlen; |
| 1621 | |
Bram Moolenaar | 861d80a | 2014-10-16 20:35:36 +0200 | [diff] [blame] | 1622 | acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free, |
| 1623 | &maxlen); |
Bram Moolenaar | f191d55 | 2014-10-09 17:05:56 +0200 | [diff] [blame] | 1624 | if (to_free != NULL) |
| 1625 | printer_name = to_free; |
Bram Moolenaar | 861d80a | 2014-10-16 20:35:36 +0200 | [diff] [blame] | 1626 | acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen); |
Bram Moolenaar | f191d55 | 2014-10-09 17:05:56 +0200 | [diff] [blame] | 1627 | if (to_free != NULL) |
| 1628 | port_name = to_free; |
| 1629 | } |
| 1630 | #endif |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 1631 | prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name) |
| 1632 | + STRLEN(text))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1633 | if (prt_name != NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1634 | wsprintf((char *)prt_name, (const char *)text, |
| 1635 | printer_name, port_name); |
Bram Moolenaar | f191d55 | 2014-10-09 17:05:56 +0200 | [diff] [blame] | 1636 | #ifdef FEAT_MBYTE |
| 1637 | if (printer_name != printer_name_orig) |
| 1638 | vim_free(printer_name); |
| 1639 | if (port_name != port_name_orig) |
| 1640 | vim_free(port_name); |
| 1641 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1642 | } |
| 1643 | GlobalUnlock(prt_dlg.hDevNames); |
| 1644 | |
| 1645 | /* |
| 1646 | * Initialise the font according to 'printfont' |
| 1647 | */ |
Bram Moolenaar | 7db5fc8 | 2010-05-24 11:59:29 +0200 | [diff] [blame] | 1648 | vim_memset(&fLogFont, 0, sizeof(fLogFont)); |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1649 | if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1650 | { |
| 1651 | EMSG2(_("E613: Unknown printer font: %s"), p_pfn); |
| 1652 | mch_print_cleanup(); |
| 1653 | return FALSE; |
| 1654 | } |
| 1655 | |
| 1656 | for (pifBold = 0; pifBold <= 1; pifBold++) |
| 1657 | for (pifItalic = 0; pifItalic <= 1; pifItalic++) |
| 1658 | for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++) |
| 1659 | { |
| 1660 | fLogFont.lfWeight = boldface[pifBold]; |
| 1661 | fLogFont.lfItalic = pifItalic; |
| 1662 | fLogFont.lfUnderline = pifUnderline; |
| 1663 | prt_font_handles[pifBold][pifItalic][pifUnderline] |
| 1664 | = CreateFontIndirect(&fLogFont); |
| 1665 | } |
| 1666 | |
| 1667 | SetBkMode(prt_dlg.hDC, OPAQUE); |
| 1668 | SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]); |
| 1669 | |
| 1670 | /* |
| 1671 | * Fill in the settings struct |
| 1672 | */ |
| 1673 | psettings->chars_per_line = prt_get_cpl(); |
| 1674 | psettings->lines_per_page = prt_get_lpp(); |
Bram Moolenaar | 7ddc642 | 2014-09-27 11:18:19 +0200 | [diff] [blame] | 1675 | if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE) |
| 1676 | { |
| 1677 | psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE) |
| 1678 | ? prt_dlg.nCopies : 1; |
| 1679 | psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE) |
| 1680 | ? 1 : prt_dlg.nCopies; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1681 | |
Bram Moolenaar | 7ddc642 | 2014-09-27 11:18:19 +0200 | [diff] [blame] | 1682 | if (psettings->n_collated_copies == 0) |
| 1683 | psettings->n_collated_copies = 1; |
| 1684 | |
| 1685 | if (psettings->n_uncollated_copies == 0) |
| 1686 | psettings->n_uncollated_copies = 1; |
| 1687 | } else { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1688 | psettings->n_collated_copies = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1689 | psettings->n_uncollated_copies = 1; |
Bram Moolenaar | 7ddc642 | 2014-09-27 11:18:19 +0200 | [diff] [blame] | 1690 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1691 | |
| 1692 | psettings->jobname = jobname; |
| 1693 | |
| 1694 | return TRUE; |
| 1695 | |
| 1696 | init_fail_dlg: |
| 1697 | { |
| 1698 | DWORD err = CommDlgExtendedError(); |
| 1699 | |
| 1700 | if (err) |
| 1701 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1702 | char_u *buf; |
| 1703 | |
| 1704 | /* I suspect FormatMessage() doesn't work for values returned by |
| 1705 | * CommDlgExtendedError(). What does? */ |
| 1706 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 1707 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 1708 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 1709 | NULL, err, 0, (LPTSTR)(&buf), 0, NULL); |
| 1710 | EMSG2(_("E238: Print error: %s"), |
| 1711 | buf == NULL ? (char_u *)_("Unknown") : buf); |
| 1712 | LocalFree((LPVOID)(buf)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1713 | } |
| 1714 | else |
| 1715 | msg_clr_eos(); /* Maybe canceled */ |
| 1716 | |
| 1717 | mch_print_cleanup(); |
| 1718 | return FALSE; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | |
| 1723 | int |
| 1724 | mch_print_begin(prt_settings_T *psettings) |
| 1725 | { |
| 1726 | int ret; |
| 1727 | static DOCINFO di; |
| 1728 | char szBuffer[300]; |
| 1729 | |
| 1730 | hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"), |
| 1731 | prt_dlg.hwndOwner, PrintDlgProc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1732 | SetAbortProc(prt_dlg.hDC, AbortProc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1733 | wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname)); |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1734 | vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1735 | |
Bram Moolenaar | 7db5fc8 | 2010-05-24 11:59:29 +0200 | [diff] [blame] | 1736 | vim_memset(&di, 0, sizeof(DOCINFO)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1737 | di.cbSize = sizeof(DOCINFO); |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1738 | di.lpszDocName = (LPCSTR)psettings->jobname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1739 | ret = StartDoc(prt_dlg.hDC, &di); |
| 1740 | |
| 1741 | #ifdef FEAT_GUI |
| 1742 | /* Give focus back to main window (when using MDI). */ |
| 1743 | SetFocus(s_hwnd); |
| 1744 | #endif |
| 1745 | |
| 1746 | return (ret > 0); |
| 1747 | } |
| 1748 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1749 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1750 | void |
| 1751 | mch_print_end(prt_settings_T *psettings) |
| 1752 | { |
| 1753 | EndDoc(prt_dlg.hDC); |
| 1754 | if (!*bUserAbort) |
| 1755 | SendMessage(hDlgPrint, WM_COMMAND, 0, 0); |
| 1756 | } |
| 1757 | |
| 1758 | int |
| 1759 | mch_print_end_page(void) |
| 1760 | { |
| 1761 | return (EndPage(prt_dlg.hDC) > 0); |
| 1762 | } |
| 1763 | |
| 1764 | int |
| 1765 | mch_print_begin_page(char_u *msg) |
| 1766 | { |
| 1767 | if (msg != NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1768 | vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1769 | return (StartPage(prt_dlg.hDC) > 0); |
| 1770 | } |
| 1771 | |
| 1772 | int |
| 1773 | mch_print_blank_page(void) |
| 1774 | { |
| 1775 | return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE); |
| 1776 | } |
| 1777 | |
| 1778 | static int prt_pos_x = 0; |
| 1779 | static int prt_pos_y = 0; |
| 1780 | |
| 1781 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1782 | mch_print_start_line(int margin, int page_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1783 | { |
| 1784 | if (margin) |
| 1785 | prt_pos_x = -prt_number_width; |
| 1786 | else |
| 1787 | prt_pos_x = 0; |
| 1788 | prt_pos_y = page_line * prt_line_height |
| 1789 | + prt_tm.tmAscent + prt_tm.tmExternalLeading; |
| 1790 | } |
| 1791 | |
| 1792 | int |
| 1793 | mch_print_text_out(char_u *p, int len) |
| 1794 | { |
Bram Moolenaar | cf7164a | 2016-02-20 13:55:06 +0100 | [diff] [blame] | 1795 | #if defined(FEAT_PROPORTIONAL_FONTS) || defined(FEAT_MBYTE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1796 | SIZE sz; |
| 1797 | #endif |
Bram Moolenaar | cf7164a | 2016-02-20 13:55:06 +0100 | [diff] [blame] | 1798 | #if defined(FEAT_MBYTE) |
Bram Moolenaar | 5246cd7 | 2013-06-16 16:41:47 +0200 | [diff] [blame] | 1799 | WCHAR *wp = NULL; |
| 1800 | int wlen = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1801 | |
Bram Moolenaar | 5246cd7 | 2013-06-16 16:41:47 +0200 | [diff] [blame] | 1802 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 1803 | { |
| 1804 | wp = enc_to_utf16(p, &wlen); |
| 1805 | } |
| 1806 | if (wp != NULL) |
| 1807 | { |
| 1808 | int ret = FALSE; |
| 1809 | |
| 1810 | TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin, |
| 1811 | prt_pos_y + prt_top_margin, wp, wlen); |
| 1812 | GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz); |
| 1813 | vim_free(wp); |
| 1814 | prt_pos_x += (sz.cx - prt_tm.tmOverhang); |
| 1815 | /* This is wrong when printing spaces for a TAB. */ |
| 1816 | if (p[len] != NUL) |
| 1817 | { |
| 1818 | wlen = MB_PTR2LEN(p + len); |
| 1819 | wp = enc_to_utf16(p + len, &wlen); |
| 1820 | if (wp != NULL) |
| 1821 | { |
| 1822 | GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz); |
| 1823 | ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin); |
| 1824 | vim_free(wp); |
| 1825 | } |
| 1826 | } |
| 1827 | return ret; |
| 1828 | } |
| 1829 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1830 | TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin, |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1831 | prt_pos_y + prt_top_margin, |
| 1832 | (LPCSTR)p, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1833 | #ifndef FEAT_PROPORTIONAL_FONTS |
| 1834 | prt_pos_x += len * prt_tm.tmAveCharWidth; |
| 1835 | return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth |
| 1836 | + prt_tm.tmOverhang > prt_right_margin); |
| 1837 | #else |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1838 | GetTextExtentPoint32(prt_dlg.hDC, (LPCSTR)p, len, &sz); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1839 | prt_pos_x += (sz.cx - prt_tm.tmOverhang); |
| 1840 | /* This is wrong when printing spaces for a TAB. */ |
| 1841 | if (p[len] == NUL) |
| 1842 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1843 | GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1844 | return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin); |
| 1845 | #endif |
| 1846 | } |
| 1847 | |
| 1848 | void |
| 1849 | mch_print_set_font(int iBold, int iItalic, int iUnderline) |
| 1850 | { |
| 1851 | SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]); |
| 1852 | } |
| 1853 | |
| 1854 | void |
Bram Moolenaar | 551dbcc | 2006-04-25 22:13:59 +0000 | [diff] [blame] | 1855 | mch_print_set_bg(long_u bgcol) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1856 | { |
Bram Moolenaar | 551dbcc | 2006-04-25 22:13:59 +0000 | [diff] [blame] | 1857 | SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC, |
| 1858 | swap_me((COLORREF)bgcol))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1859 | /* |
| 1860 | * With a white background we can draw characters transparent, which is |
| 1861 | * good for italic characters that overlap to the next char cell. |
| 1862 | */ |
| 1863 | if (bgcol == 0xffffffUL) |
| 1864 | SetBkMode(prt_dlg.hDC, TRANSPARENT); |
| 1865 | else |
| 1866 | SetBkMode(prt_dlg.hDC, OPAQUE); |
| 1867 | } |
| 1868 | |
| 1869 | void |
Bram Moolenaar | 551dbcc | 2006-04-25 22:13:59 +0000 | [diff] [blame] | 1870 | mch_print_set_fg(long_u fgcol) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1871 | { |
Bram Moolenaar | 551dbcc | 2006-04-25 22:13:59 +0000 | [diff] [blame] | 1872 | SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC, |
| 1873 | swap_me((COLORREF)fgcol))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | #endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/ |
| 1877 | |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1878 | |
| 1879 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1880 | #if defined(FEAT_SHORTCUT) || defined(PROTO) |
Bram Moolenaar | 8288149 | 2012-11-20 16:53:39 +0100 | [diff] [blame] | 1881 | # ifndef PROTO |
| 1882 | # include <shlobj.h> |
| 1883 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1884 | |
| 1885 | /* |
| 1886 | * When "fname" is the name of a shortcut (*.lnk) resolve the file it points |
| 1887 | * to and return that name in allocated memory. |
| 1888 | * Otherwise NULL is returned. |
| 1889 | */ |
| 1890 | char_u * |
| 1891 | mch_resolve_shortcut(char_u *fname) |
| 1892 | { |
| 1893 | HRESULT hr; |
| 1894 | IShellLink *psl = NULL; |
| 1895 | IPersistFile *ppf = NULL; |
| 1896 | OLECHAR wsz[MAX_PATH]; |
| 1897 | WIN32_FIND_DATA ffd; // we get those free of charge |
Bram Moolenaar | 604729e | 2013-08-30 16:44:19 +0200 | [diff] [blame] | 1898 | CHAR buf[MAX_PATH]; // could have simply reused 'wsz'... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1899 | char_u *rfname = NULL; |
| 1900 | int len; |
Bram Moolenaar | 604729e | 2013-08-30 16:44:19 +0200 | [diff] [blame] | 1901 | # ifdef FEAT_MBYTE |
| 1902 | IShellLinkW *pslw = NULL; |
| 1903 | WIN32_FIND_DATAW ffdw; // we get those free of charge |
| 1904 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1905 | |
| 1906 | /* Check if the file name ends in ".lnk". Avoid calling |
| 1907 | * CoCreateInstance(), it's quite slow. */ |
| 1908 | if (fname == NULL) |
| 1909 | return rfname; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1910 | len = (int)STRLEN(fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1911 | if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0) |
| 1912 | return rfname; |
| 1913 | |
| 1914 | CoInitialize(NULL); |
| 1915 | |
Bram Moolenaar | 604729e | 2013-08-30 16:44:19 +0200 | [diff] [blame] | 1916 | # ifdef FEAT_MBYTE |
| 1917 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 1918 | { |
| 1919 | // create a link manager object and request its interface |
| 1920 | hr = CoCreateInstance( |
| 1921 | &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, |
| 1922 | &IID_IShellLinkW, (void**)&pslw); |
| 1923 | if (hr == S_OK) |
| 1924 | { |
| 1925 | WCHAR *p = enc_to_utf16(fname, NULL); |
| 1926 | |
| 1927 | if (p != NULL) |
| 1928 | { |
| 1929 | // Get a pointer to the IPersistFile interface. |
| 1930 | hr = pslw->lpVtbl->QueryInterface( |
| 1931 | pslw, &IID_IPersistFile, (void**)&ppf); |
| 1932 | if (hr != S_OK) |
| 1933 | goto shortcut_errorw; |
| 1934 | |
| 1935 | // "load" the name and resolve the link |
| 1936 | hr = ppf->lpVtbl->Load(ppf, p, STGM_READ); |
| 1937 | if (hr != S_OK) |
| 1938 | goto shortcut_errorw; |
| 1939 | # if 0 // This makes Vim wait a long time if the target does not exist. |
| 1940 | hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI); |
| 1941 | if (hr != S_OK) |
| 1942 | goto shortcut_errorw; |
| 1943 | # endif |
| 1944 | |
| 1945 | // Get the path to the link target. |
| 1946 | ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR)); |
| 1947 | hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0); |
| 1948 | if (hr == S_OK && wsz[0] != NUL) |
| 1949 | rfname = utf16_to_enc(wsz, NULL); |
| 1950 | |
| 1951 | shortcut_errorw: |
| 1952 | vim_free(p); |
Bram Moolenaar | cd981f2 | 2014-02-11 17:06:00 +0100 | [diff] [blame] | 1953 | goto shortcut_end; |
Bram Moolenaar | 604729e | 2013-08-30 16:44:19 +0200 | [diff] [blame] | 1954 | } |
| 1955 | } |
| 1956 | /* Retry with non-wide function (for Windows 98). */ |
| 1957 | } |
| 1958 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1959 | // create a link manager object and request its interface |
| 1960 | hr = CoCreateInstance( |
| 1961 | &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, |
| 1962 | &IID_IShellLink, (void**)&psl); |
| 1963 | if (hr != S_OK) |
Bram Moolenaar | 604729e | 2013-08-30 16:44:19 +0200 | [diff] [blame] | 1964 | goto shortcut_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1965 | |
| 1966 | // Get a pointer to the IPersistFile interface. |
| 1967 | hr = psl->lpVtbl->QueryInterface( |
| 1968 | psl, &IID_IPersistFile, (void**)&ppf); |
| 1969 | if (hr != S_OK) |
Bram Moolenaar | 604729e | 2013-08-30 16:44:19 +0200 | [diff] [blame] | 1970 | goto shortcut_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1971 | |
| 1972 | // full path string must be in Unicode. |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1973 | MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fname, -1, wsz, MAX_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1974 | |
Bram Moolenaar | ff1d0d4 | 2007-05-10 17:24:16 +0000 | [diff] [blame] | 1975 | // "load" the name and resolve the link |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1976 | hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ); |
| 1977 | if (hr != S_OK) |
Bram Moolenaar | 604729e | 2013-08-30 16:44:19 +0200 | [diff] [blame] | 1978 | goto shortcut_end; |
| 1979 | # if 0 // This makes Vim wait a long time if the target doesn't exist. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1980 | hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI); |
| 1981 | if (hr != S_OK) |
Bram Moolenaar | 604729e | 2013-08-30 16:44:19 +0200 | [diff] [blame] | 1982 | goto shortcut_end; |
| 1983 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1984 | |
| 1985 | // Get the path to the link target. |
| 1986 | ZeroMemory(buf, MAX_PATH); |
| 1987 | hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0); |
| 1988 | if (hr == S_OK && buf[0] != NUL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1989 | rfname = vim_strsave((char_u *)buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1990 | |
Bram Moolenaar | 604729e | 2013-08-30 16:44:19 +0200 | [diff] [blame] | 1991 | shortcut_end: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1992 | // Release all interface pointers (both belong to the same object) |
| 1993 | if (ppf != NULL) |
| 1994 | ppf->lpVtbl->Release(ppf); |
| 1995 | if (psl != NULL) |
| 1996 | psl->lpVtbl->Release(psl); |
Bram Moolenaar | 604729e | 2013-08-30 16:44:19 +0200 | [diff] [blame] | 1997 | # ifdef FEAT_MBYTE |
| 1998 | if (pslw != NULL) |
| 1999 | pslw->lpVtbl->Release(pslw); |
| 2000 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2001 | |
| 2002 | CoUninitialize(); |
| 2003 | return rfname; |
| 2004 | } |
| 2005 | #endif |
| 2006 | |
| 2007 | #if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO) |
| 2008 | /* |
| 2009 | * Bring ourselves to the foreground. Does work if the OS doesn't allow it. |
| 2010 | */ |
| 2011 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2012 | win32_set_foreground(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2013 | { |
| 2014 | # ifndef FEAT_GUI |
| 2015 | GetConsoleHwnd(); /* get value of s_hwnd */ |
| 2016 | # endif |
| 2017 | if (s_hwnd != 0) |
| 2018 | SetForegroundWindow(s_hwnd); |
| 2019 | } |
| 2020 | #endif |
| 2021 | |
| 2022 | #if defined(FEAT_CLIENTSERVER) || defined(PROTO) |
| 2023 | /* |
| 2024 | * Client-server code for Vim |
| 2025 | * |
| 2026 | * Originally written by Paul Moore |
| 2027 | */ |
| 2028 | |
| 2029 | /* In order to handle inter-process messages, we need to have a window. But |
| 2030 | * the functions in this module can be called before the main GUI window is |
| 2031 | * created (and may also be called in the console version, where there is no |
| 2032 | * GUI window at all). |
| 2033 | * |
| 2034 | * So we create a hidden window, and arrange to destroy it on exit. |
| 2035 | */ |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2036 | HWND message_window = 0; /* window that's handling messages */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2037 | |
| 2038 | #define VIM_CLASSNAME "VIM_MESSAGES" |
| 2039 | #define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1) |
| 2040 | |
| 2041 | /* Communication is via WM_COPYDATA messages. The message type is send in |
| 2042 | * the dwData parameter. Types are defined here. */ |
| 2043 | #define COPYDATA_KEYS 0 |
| 2044 | #define COPYDATA_REPLY 1 |
| 2045 | #define COPYDATA_EXPR 10 |
| 2046 | #define COPYDATA_RESULT 11 |
| 2047 | #define COPYDATA_ERROR_RESULT 12 |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2048 | #define COPYDATA_ENCODING 20 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2049 | |
| 2050 | /* This is a structure containing a server HWND and its name. */ |
| 2051 | struct server_id |
| 2052 | { |
| 2053 | HWND hwnd; |
| 2054 | char_u *name; |
| 2055 | }; |
| 2056 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2057 | /* Last received 'encoding' that the client uses. */ |
| 2058 | static char_u *client_enc = NULL; |
| 2059 | |
| 2060 | /* |
| 2061 | * Tell the other side what encoding we are using. |
| 2062 | * Errors are ignored. |
| 2063 | */ |
| 2064 | static void |
| 2065 | serverSendEnc(HWND target) |
| 2066 | { |
| 2067 | COPYDATASTRUCT data; |
| 2068 | |
| 2069 | data.dwData = COPYDATA_ENCODING; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2070 | #ifdef FEAT_MBYTE |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2071 | data.cbData = (DWORD)STRLEN(p_enc) + 1; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2072 | data.lpData = p_enc; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2073 | #else |
Bram Moolenaar | 8765a4a | 2010-07-27 22:41:43 +0200 | [diff] [blame] | 2074 | data.cbData = (DWORD)STRLEN("latin1") + 1; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2075 | data.lpData = "latin1"; |
| 2076 | #endif |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2077 | (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window, |
| 2078 | (LPARAM)(&data)); |
| 2079 | } |
| 2080 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2081 | /* |
| 2082 | * Clean up on exit. This destroys the hidden message window. |
| 2083 | */ |
| 2084 | static void |
| 2085 | #ifdef __BORLANDC__ |
| 2086 | _RTLENTRYF |
| 2087 | #endif |
| 2088 | CleanUpMessaging(void) |
| 2089 | { |
| 2090 | if (message_window != 0) |
| 2091 | { |
| 2092 | DestroyWindow(message_window); |
| 2093 | message_window = 0; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | static int save_reply(HWND server, char_u *reply, int expr); |
| 2098 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2099 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2100 | * The window procedure for the hidden message window. |
| 2101 | * It handles callback messages and notifications from servers. |
| 2102 | * In order to process these messages, it is necessary to run a |
| 2103 | * message loop. Code which may run before the main message loop |
| 2104 | * is started (in the GUI) is careful to pump messages when it needs |
| 2105 | * to. Features which require message delivery during normal use will |
| 2106 | * not work in the console version - this basically means those |
| 2107 | * features which allow Vim to act as a server, rather than a client. |
| 2108 | */ |
| 2109 | static LRESULT CALLBACK |
| 2110 | Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 2111 | { |
| 2112 | if (msg == WM_COPYDATA) |
| 2113 | { |
| 2114 | /* This is a message from another Vim. The dwData member of the |
| 2115 | * COPYDATASTRUCT determines the type of message: |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2116 | * COPYDATA_ENCODING: |
| 2117 | * The encoding that the client uses. Following messages will |
| 2118 | * use this encoding, convert if needed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2119 | * COPYDATA_KEYS: |
| 2120 | * A key sequence. We are a server, and a client wants these keys |
| 2121 | * adding to the input queue. |
| 2122 | * COPYDATA_REPLY: |
| 2123 | * A reply. We are a client, and a server has sent this message |
| 2124 | * in response to a request. (server2client()) |
| 2125 | * COPYDATA_EXPR: |
| 2126 | * An expression. We are a server, and a client wants us to |
| 2127 | * evaluate this expression. |
| 2128 | * COPYDATA_RESULT: |
| 2129 | * A reply. We are a client, and a server has sent this message |
| 2130 | * in response to a COPYDATA_EXPR. |
| 2131 | * COPYDATA_ERROR_RESULT: |
| 2132 | * A reply. We are a client, and a server has sent this message |
| 2133 | * in response to a COPYDATA_EXPR that failed to evaluate. |
| 2134 | */ |
| 2135 | COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam; |
| 2136 | HWND sender = (HWND)wParam; |
| 2137 | COPYDATASTRUCT reply; |
| 2138 | char_u *res; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2139 | int retval; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2140 | char_u *str; |
| 2141 | char_u *tofree; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2142 | |
| 2143 | switch (data->dwData) |
| 2144 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2145 | case COPYDATA_ENCODING: |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2146 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2147 | /* Remember the encoding that the client uses. */ |
| 2148 | vim_free(client_enc); |
| 2149 | client_enc = enc_canonize((char_u *)data->lpData); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2150 | # endif |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2151 | return 1; |
| 2152 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2153 | case COPYDATA_KEYS: |
| 2154 | /* Remember who sent this, for <client> */ |
| 2155 | clientWindow = sender; |
| 2156 | |
| 2157 | /* Add the received keys to the input buffer. The loop waiting |
| 2158 | * for the user to do something should check the input buffer. */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2159 | str = serverConvert(client_enc, (char_u *)data->lpData, &tofree); |
| 2160 | server_to_input_buf(str); |
| 2161 | vim_free(tofree); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2162 | |
| 2163 | # ifdef FEAT_GUI |
| 2164 | /* Wake up the main GUI loop. */ |
| 2165 | if (s_hwnd != 0) |
| 2166 | PostMessage(s_hwnd, WM_NULL, 0, 0); |
| 2167 | # endif |
| 2168 | return 1; |
| 2169 | |
| 2170 | case COPYDATA_EXPR: |
| 2171 | /* Remember who sent this, for <client> */ |
| 2172 | clientWindow = sender; |
| 2173 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2174 | str = serverConvert(client_enc, (char_u *)data->lpData, &tofree); |
| 2175 | res = eval_client_expr_to_string(str); |
| 2176 | vim_free(tofree); |
| 2177 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2178 | if (res == NULL) |
| 2179 | { |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2180 | res = vim_strsave((char_u *)_(e_invexprmsg)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2181 | reply.dwData = COPYDATA_ERROR_RESULT; |
| 2182 | } |
| 2183 | else |
| 2184 | reply.dwData = COPYDATA_RESULT; |
| 2185 | reply.lpData = res; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2186 | reply.cbData = (DWORD)STRLEN(res) + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2187 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2188 | serverSendEnc(sender); |
Bram Moolenaar | 5246cd7 | 2013-06-16 16:41:47 +0200 | [diff] [blame] | 2189 | retval = (int)SendMessage(sender, WM_COPYDATA, |
| 2190 | (WPARAM)message_window, (LPARAM)(&reply)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2191 | vim_free(res); |
| 2192 | return retval; |
| 2193 | |
| 2194 | case COPYDATA_REPLY: |
| 2195 | case COPYDATA_RESULT: |
| 2196 | case COPYDATA_ERROR_RESULT: |
| 2197 | if (data->lpData != NULL) |
| 2198 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2199 | str = serverConvert(client_enc, (char_u *)data->lpData, |
| 2200 | &tofree); |
| 2201 | if (tofree == NULL) |
| 2202 | str = vim_strsave(str); |
| 2203 | if (save_reply(sender, str, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2204 | (data->dwData == COPYDATA_REPLY ? 0 : |
| 2205 | (data->dwData == COPYDATA_RESULT ? 1 : |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2206 | 2))) == FAIL) |
| 2207 | vim_free(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2208 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2209 | else if (data->dwData == COPYDATA_REPLY) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2210 | { |
Bram Moolenaar | 427d51c | 2013-06-16 16:01:25 +0200 | [diff] [blame] | 2211 | char_u winstr[30]; |
| 2212 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 2213 | sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2214 | apply_autocmds(EVENT_REMOTEREPLY, winstr, str, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2215 | TRUE, curbuf); |
| 2216 | } |
| 2217 | #endif |
| 2218 | } |
| 2219 | return 1; |
| 2220 | } |
| 2221 | |
| 2222 | return 0; |
| 2223 | } |
| 2224 | |
| 2225 | else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE) |
| 2226 | { |
| 2227 | /* When the message window is activated (brought to the foreground), |
| 2228 | * this actually applies to the text window. */ |
| 2229 | #ifndef FEAT_GUI |
| 2230 | GetConsoleHwnd(); /* get value of s_hwnd */ |
| 2231 | #endif |
| 2232 | if (s_hwnd != 0) |
| 2233 | { |
| 2234 | SetForegroundWindow(s_hwnd); |
| 2235 | return 0; |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | return DefWindowProc(hwnd, msg, wParam, lParam); |
| 2240 | } |
| 2241 | |
| 2242 | /* |
| 2243 | * Initialise the message handling process. This involves creating a window |
| 2244 | * to handle messages - the window will not be visible. |
| 2245 | */ |
| 2246 | void |
| 2247 | serverInitMessaging(void) |
| 2248 | { |
| 2249 | WNDCLASS wndclass; |
| 2250 | HINSTANCE s_hinst; |
| 2251 | |
| 2252 | /* Clean up on exit */ |
| 2253 | atexit(CleanUpMessaging); |
| 2254 | |
| 2255 | /* Register a window class - we only really care |
| 2256 | * about the window procedure |
| 2257 | */ |
| 2258 | s_hinst = (HINSTANCE)GetModuleHandle(0); |
| 2259 | wndclass.style = 0; |
| 2260 | wndclass.lpfnWndProc = Messaging_WndProc; |
| 2261 | wndclass.cbClsExtra = 0; |
| 2262 | wndclass.cbWndExtra = 0; |
| 2263 | wndclass.hInstance = s_hinst; |
| 2264 | wndclass.hIcon = NULL; |
| 2265 | wndclass.hCursor = NULL; |
| 2266 | wndclass.hbrBackground = NULL; |
| 2267 | wndclass.lpszMenuName = NULL; |
| 2268 | wndclass.lpszClassName = VIM_CLASSNAME; |
| 2269 | RegisterClass(&wndclass); |
| 2270 | |
| 2271 | /* Create the message window. It will be hidden, so the details don't |
| 2272 | * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove |
| 2273 | * focus from gvim. */ |
| 2274 | message_window = CreateWindow(VIM_CLASSNAME, "", |
| 2275 | WS_POPUPWINDOW | WS_CAPTION, |
| 2276 | CW_USEDEFAULT, CW_USEDEFAULT, |
| 2277 | 100, 100, NULL, NULL, |
| 2278 | s_hinst, NULL); |
| 2279 | } |
| 2280 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 2281 | /* Used by serverSendToVim() to find an alternate server name. */ |
| 2282 | static char_u *altname_buf_ptr = NULL; |
| 2283 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2284 | /* |
| 2285 | * Get the title of the window "hwnd", which is the Vim server name, in |
| 2286 | * "name[namelen]" and return the length. |
| 2287 | * Returns zero if window "hwnd" is not a Vim server. |
| 2288 | */ |
| 2289 | static int |
| 2290 | getVimServerName(HWND hwnd, char *name, int namelen) |
| 2291 | { |
| 2292 | int len; |
| 2293 | char buffer[VIM_CLASSNAME_LEN + 1]; |
| 2294 | |
| 2295 | /* Ignore windows which aren't Vim message windows */ |
| 2296 | len = GetClassName(hwnd, buffer, sizeof(buffer)); |
| 2297 | if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0) |
| 2298 | return 0; |
| 2299 | |
| 2300 | /* Get the title of the window */ |
| 2301 | return GetWindowText(hwnd, name, namelen); |
| 2302 | } |
| 2303 | |
| 2304 | static BOOL CALLBACK |
| 2305 | enumWindowsGetServer(HWND hwnd, LPARAM lparam) |
| 2306 | { |
| 2307 | struct server_id *id = (struct server_id *)lparam; |
| 2308 | char server[MAX_PATH]; |
| 2309 | |
| 2310 | /* Get the title of the window */ |
| 2311 | if (getVimServerName(hwnd, server, sizeof(server)) == 0) |
| 2312 | return TRUE; |
| 2313 | |
| 2314 | /* If this is the server we're looking for, return its HWND */ |
| 2315 | if (STRICMP(server, id->name) == 0) |
| 2316 | { |
| 2317 | id->hwnd = hwnd; |
| 2318 | return FALSE; |
| 2319 | } |
| 2320 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 2321 | /* If we are looking for an alternate server, remember this name. */ |
| 2322 | if (altname_buf_ptr != NULL |
| 2323 | && STRNICMP(server, id->name, STRLEN(id->name)) == 0 |
| 2324 | && vim_isdigit(server[STRLEN(id->name)])) |
| 2325 | { |
| 2326 | STRCPY(altname_buf_ptr, server); |
| 2327 | altname_buf_ptr = NULL; /* don't use another name */ |
| 2328 | } |
| 2329 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2330 | /* Otherwise, keep looking */ |
| 2331 | return TRUE; |
| 2332 | } |
| 2333 | |
| 2334 | static BOOL CALLBACK |
| 2335 | enumWindowsGetNames(HWND hwnd, LPARAM lparam) |
| 2336 | { |
| 2337 | garray_T *ga = (garray_T *)lparam; |
| 2338 | char server[MAX_PATH]; |
| 2339 | |
| 2340 | /* Get the title of the window */ |
| 2341 | if (getVimServerName(hwnd, server, sizeof(server)) == 0) |
| 2342 | return TRUE; |
| 2343 | |
| 2344 | /* Add the name to the list */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2345 | ga_concat(ga, (char_u *)server); |
| 2346 | ga_concat(ga, (char_u *)"\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2347 | return TRUE; |
| 2348 | } |
| 2349 | |
| 2350 | static HWND |
| 2351 | findServer(char_u *name) |
| 2352 | { |
| 2353 | struct server_id id; |
| 2354 | |
| 2355 | id.name = name; |
| 2356 | id.hwnd = 0; |
| 2357 | |
| 2358 | EnumWindows(enumWindowsGetServer, (LPARAM)(&id)); |
| 2359 | |
| 2360 | return id.hwnd; |
| 2361 | } |
| 2362 | |
| 2363 | void |
| 2364 | serverSetName(char_u *name) |
| 2365 | { |
| 2366 | char_u *ok_name; |
| 2367 | HWND hwnd = 0; |
| 2368 | int i = 0; |
| 2369 | char_u *p; |
| 2370 | |
| 2371 | /* Leave enough space for a 9-digit suffix to ensure uniqueness! */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2372 | ok_name = alloc((unsigned)STRLEN(name) + 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2373 | |
| 2374 | STRCPY(ok_name, name); |
| 2375 | p = ok_name + STRLEN(name); |
| 2376 | |
| 2377 | for (;;) |
| 2378 | { |
| 2379 | /* This is inefficient - we're doing an EnumWindows loop for each |
| 2380 | * possible name. It would be better to grab all names in one go, |
| 2381 | * and scan the list each time... |
| 2382 | */ |
| 2383 | hwnd = findServer(ok_name); |
| 2384 | if (hwnd == 0) |
| 2385 | break; |
| 2386 | |
| 2387 | ++i; |
| 2388 | if (i >= 1000) |
| 2389 | break; |
| 2390 | |
| 2391 | sprintf((char *)p, "%d", i); |
| 2392 | } |
| 2393 | |
| 2394 | if (hwnd != 0) |
| 2395 | vim_free(ok_name); |
| 2396 | else |
| 2397 | { |
| 2398 | /* Remember the name */ |
| 2399 | serverName = ok_name; |
| 2400 | #ifdef FEAT_TITLE |
| 2401 | need_maketitle = TRUE; /* update Vim window title later */ |
| 2402 | #endif |
| 2403 | |
| 2404 | /* Update the message window title */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2405 | SetWindowText(message_window, (LPCSTR)ok_name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2406 | |
| 2407 | #ifdef FEAT_EVAL |
| 2408 | /* Set the servername variable */ |
| 2409 | set_vim_var_string(VV_SEND_SERVER, serverName, -1); |
| 2410 | #endif |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | char_u * |
| 2415 | serverGetVimNames(void) |
| 2416 | { |
| 2417 | garray_T ga; |
| 2418 | |
| 2419 | ga_init2(&ga, 1, 100); |
| 2420 | |
| 2421 | EnumWindows(enumWindowsGetNames, (LPARAM)(&ga)); |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 2422 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2423 | |
| 2424 | return ga.ga_data; |
| 2425 | } |
| 2426 | |
| 2427 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2428 | serverSendReply( |
| 2429 | char_u *name, /* Where to send. */ |
| 2430 | char_u *reply) /* What to send. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2431 | { |
| 2432 | HWND target; |
| 2433 | COPYDATASTRUCT data; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 2434 | long_u n = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2435 | |
| 2436 | /* The "name" argument is a magic cookie obtained from expand("<client>"). |
| 2437 | * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the |
| 2438 | * value of the client's message window HWND. |
| 2439 | */ |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 2440 | sscanf((char *)name, SCANF_HEX_LONG_U, &n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2441 | if (n == 0) |
| 2442 | return -1; |
| 2443 | |
| 2444 | target = (HWND)n; |
| 2445 | if (!IsWindow(target)) |
| 2446 | return -1; |
| 2447 | |
| 2448 | data.dwData = COPYDATA_REPLY; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2449 | data.cbData = (DWORD)STRLEN(reply) + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2450 | data.lpData = reply; |
| 2451 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2452 | serverSendEnc(target); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2453 | if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window, |
| 2454 | (LPARAM)(&data))) |
| 2455 | return 0; |
| 2456 | |
| 2457 | return -1; |
| 2458 | } |
| 2459 | |
| 2460 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2461 | serverSendToVim( |
| 2462 | char_u *name, /* Where to send. */ |
| 2463 | char_u *cmd, /* What to send. */ |
| 2464 | char_u **result, /* Result of eval'ed expression */ |
| 2465 | void *ptarget, /* HWND of server */ |
| 2466 | int asExpr, /* Expression or keys? */ |
| 2467 | int silent) /* don't complain about no server */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2468 | { |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 2469 | HWND target; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2470 | COPYDATASTRUCT data; |
| 2471 | char_u *retval = NULL; |
| 2472 | int retcode = 0; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 2473 | char_u altname_buf[MAX_PATH]; |
| 2474 | |
| 2475 | /* If the server name does not end in a digit then we look for an |
| 2476 | * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */ |
| 2477 | if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1])) |
| 2478 | altname_buf_ptr = altname_buf; |
| 2479 | altname_buf[0] = NUL; |
| 2480 | target = findServer(name); |
| 2481 | altname_buf_ptr = NULL; |
| 2482 | if (target == 0 && altname_buf[0] != NUL) |
| 2483 | /* Use another server name we found. */ |
| 2484 | target = findServer(altname_buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2485 | |
| 2486 | if (target == 0) |
| 2487 | { |
| 2488 | if (!silent) |
| 2489 | EMSG2(_(e_noserver), name); |
| 2490 | return -1; |
| 2491 | } |
| 2492 | |
| 2493 | if (ptarget) |
| 2494 | *(HWND *)ptarget = target; |
| 2495 | |
| 2496 | data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2497 | data.cbData = (DWORD)STRLEN(cmd) + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2498 | data.lpData = cmd; |
| 2499 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2500 | serverSendEnc(target); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2501 | if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window, |
| 2502 | (LPARAM)(&data)) == 0) |
| 2503 | return -1; |
| 2504 | |
| 2505 | if (asExpr) |
| 2506 | retval = serverGetReply(target, &retcode, TRUE, TRUE); |
| 2507 | |
| 2508 | if (result == NULL) |
| 2509 | vim_free(retval); |
| 2510 | else |
| 2511 | *result = retval; /* Caller assumes responsibility for freeing */ |
| 2512 | |
| 2513 | return retcode; |
| 2514 | } |
| 2515 | |
| 2516 | /* |
| 2517 | * Bring the server to the foreground. |
| 2518 | */ |
| 2519 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2520 | serverForeground(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2521 | { |
| 2522 | HWND target = findServer(name); |
| 2523 | |
| 2524 | if (target != 0) |
| 2525 | SetForegroundWindow(target); |
| 2526 | } |
| 2527 | |
| 2528 | /* Replies from server need to be stored until the client picks them up via |
| 2529 | * remote_read(). So we maintain a list of server-id/reply pairs. |
| 2530 | * Note that there could be multiple replies from one server pending if the |
| 2531 | * client is slow picking them up. |
| 2532 | * We just store the replies in a simple list. When we remove an entry, we |
| 2533 | * move list entries down to fill the gap. |
| 2534 | * The server ID is simply the HWND. |
| 2535 | */ |
| 2536 | typedef struct |
| 2537 | { |
| 2538 | HWND server; /* server window */ |
| 2539 | char_u *reply; /* reply string */ |
| 2540 | int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2541 | } reply_T; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2542 | |
| 2543 | static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0}; |
| 2544 | |
| 2545 | #define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i)) |
| 2546 | #define REPLY_COUNT (reply_list.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2547 | |
| 2548 | /* Flag which is used to wait for a reply */ |
| 2549 | static int reply_received = 0; |
| 2550 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2551 | /* |
| 2552 | * Store a reply. "reply" must be allocated memory (or NULL). |
| 2553 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2554 | static int |
| 2555 | save_reply(HWND server, char_u *reply, int expr) |
| 2556 | { |
| 2557 | reply_T *rep; |
| 2558 | |
| 2559 | if (ga_grow(&reply_list, 1) == FAIL) |
| 2560 | return FAIL; |
| 2561 | |
| 2562 | rep = REPLY_ITEM(REPLY_COUNT); |
| 2563 | rep->server = server; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2564 | rep->reply = reply; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2565 | rep->expr_result = expr; |
| 2566 | if (rep->reply == NULL) |
| 2567 | return FAIL; |
| 2568 | |
| 2569 | ++REPLY_COUNT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2570 | reply_received = 1; |
| 2571 | return OK; |
| 2572 | } |
| 2573 | |
| 2574 | /* |
| 2575 | * Get a reply from server "server". |
| 2576 | * When "expr_res" is non NULL, get the result of an expression, otherwise a |
| 2577 | * server2client() message. |
| 2578 | * When non NULL, point to return code. 0 => OK, -1 => ERROR |
| 2579 | * If "remove" is TRUE, consume the message, the caller must free it then. |
| 2580 | * if "wait" is TRUE block until a message arrives (or the server exits). |
| 2581 | */ |
| 2582 | char_u * |
| 2583 | serverGetReply(HWND server, int *expr_res, int remove, int wait) |
| 2584 | { |
| 2585 | int i; |
| 2586 | char_u *reply; |
| 2587 | reply_T *rep; |
| 2588 | |
| 2589 | /* When waiting, loop until the message waiting for is received. */ |
| 2590 | for (;;) |
| 2591 | { |
| 2592 | /* Reset this here, in case a message arrives while we are going |
| 2593 | * through the already received messages. */ |
| 2594 | reply_received = 0; |
| 2595 | |
| 2596 | for (i = 0; i < REPLY_COUNT; ++i) |
| 2597 | { |
| 2598 | rep = REPLY_ITEM(i); |
| 2599 | if (rep->server == server |
| 2600 | && ((rep->expr_result != 0) == (expr_res != NULL))) |
| 2601 | { |
| 2602 | /* Save the values we've found for later */ |
| 2603 | reply = rep->reply; |
| 2604 | if (expr_res != NULL) |
| 2605 | *expr_res = rep->expr_result == 1 ? 0 : -1; |
| 2606 | |
| 2607 | if (remove) |
| 2608 | { |
| 2609 | /* Move the rest of the list down to fill the gap */ |
| 2610 | mch_memmove(rep, rep + 1, |
| 2611 | (REPLY_COUNT - i - 1) * sizeof(reply_T)); |
| 2612 | --REPLY_COUNT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | /* Return the reply to the caller, who takes on responsibility |
| 2616 | * for freeing it if "remove" is TRUE. */ |
| 2617 | return reply; |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | /* If we got here, we didn't find a reply. Return immediately if the |
| 2622 | * "wait" parameter isn't set. */ |
| 2623 | if (!wait) |
| 2624 | break; |
| 2625 | |
| 2626 | /* We need to wait for a reply. Enter a message loop until the |
| 2627 | * "reply_received" flag gets set. */ |
| 2628 | |
| 2629 | /* Loop until we receive a reply */ |
| 2630 | while (reply_received == 0) |
| 2631 | { |
| 2632 | /* Wait for a SendMessage() call to us. This could be the reply |
| 2633 | * we are waiting for. Use a timeout of a second, to catch the |
| 2634 | * situation that the server died unexpectedly. */ |
| 2635 | MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT); |
| 2636 | |
| 2637 | /* If the server has died, give up */ |
| 2638 | if (!IsWindow(server)) |
| 2639 | return NULL; |
| 2640 | |
| 2641 | serverProcessPendingMessages(); |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | return NULL; |
| 2646 | } |
| 2647 | |
| 2648 | /* |
| 2649 | * Process any messages in the Windows message queue. |
| 2650 | */ |
| 2651 | void |
| 2652 | serverProcessPendingMessages(void) |
| 2653 | { |
| 2654 | MSG msg; |
| 2655 | |
Bram Moolenaar | 8c85fa3 | 2011-08-10 17:08:03 +0200 | [diff] [blame] | 2656 | while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2657 | { |
| 2658 | TranslateMessage(&msg); |
Bram Moolenaar | 8c85fa3 | 2011-08-10 17:08:03 +0200 | [diff] [blame] | 2659 | pDispatchMessage(&msg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | #endif /* FEAT_CLIENTSERVER */ |
| 2664 | |
| 2665 | #if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \ |
| 2666 | || defined(PROTO) |
| 2667 | |
| 2668 | struct charset_pair |
| 2669 | { |
| 2670 | char *name; |
| 2671 | BYTE charset; |
| 2672 | }; |
| 2673 | |
| 2674 | static struct charset_pair |
| 2675 | charset_pairs[] = |
| 2676 | { |
| 2677 | {"ANSI", ANSI_CHARSET}, |
| 2678 | {"CHINESEBIG5", CHINESEBIG5_CHARSET}, |
| 2679 | {"DEFAULT", DEFAULT_CHARSET}, |
| 2680 | {"HANGEUL", HANGEUL_CHARSET}, |
| 2681 | {"OEM", OEM_CHARSET}, |
| 2682 | {"SHIFTJIS", SHIFTJIS_CHARSET}, |
| 2683 | {"SYMBOL", SYMBOL_CHARSET}, |
| 2684 | #ifdef WIN3264 |
| 2685 | {"ARABIC", ARABIC_CHARSET}, |
| 2686 | {"BALTIC", BALTIC_CHARSET}, |
| 2687 | {"EASTEUROPE", EASTEUROPE_CHARSET}, |
| 2688 | {"GB2312", GB2312_CHARSET}, |
| 2689 | {"GREEK", GREEK_CHARSET}, |
| 2690 | {"HEBREW", HEBREW_CHARSET}, |
| 2691 | {"JOHAB", JOHAB_CHARSET}, |
| 2692 | {"MAC", MAC_CHARSET}, |
| 2693 | {"RUSSIAN", RUSSIAN_CHARSET}, |
| 2694 | {"THAI", THAI_CHARSET}, |
| 2695 | {"TURKISH", TURKISH_CHARSET}, |
| 2696 | # if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \ |
| 2697 | && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500)) |
| 2698 | {"VIETNAMESE", VIETNAMESE_CHARSET}, |
| 2699 | # endif |
| 2700 | #endif |
| 2701 | {NULL, 0} |
| 2702 | }; |
| 2703 | |
Bram Moolenaar | 7c1c6db | 2016-04-03 22:08:05 +0200 | [diff] [blame] | 2704 | struct quality_pair |
| 2705 | { |
| 2706 | char *name; |
| 2707 | DWORD quality; |
| 2708 | }; |
| 2709 | |
| 2710 | static struct quality_pair |
| 2711 | quality_pairs[] = { |
| 2712 | #ifdef CLEARTYPE_QUALITY |
| 2713 | {"CLEARTYPE", CLEARTYPE_QUALITY}, |
| 2714 | #endif |
| 2715 | #ifdef ANTIALIASED_QUALITY |
| 2716 | {"ANTIALIASED", ANTIALIASED_QUALITY}, |
| 2717 | #endif |
Bram Moolenaar | 73a733e | 2016-05-11 21:05:05 +0200 | [diff] [blame] | 2718 | #ifdef NONANTIALIASED_QUALITY |
| 2719 | {"NONANTIALIASED", NONANTIALIASED_QUALITY}, |
Bram Moolenaar | 7c1c6db | 2016-04-03 22:08:05 +0200 | [diff] [blame] | 2720 | #endif |
| 2721 | #ifdef PROOF_QUALITY |
| 2722 | {"PROOF", PROOF_QUALITY}, |
| 2723 | #endif |
Bram Moolenaar | 4c9ce05 | 2016-04-04 21:06:19 +0200 | [diff] [blame] | 2724 | #ifdef DRAFT_QUALITY |
Bram Moolenaar | 7c1c6db | 2016-04-03 22:08:05 +0200 | [diff] [blame] | 2725 | {"DRAFT", DRAFT_QUALITY}, |
| 2726 | #endif |
| 2727 | {"DEFAULT", DEFAULT_QUALITY}, |
| 2728 | {NULL, 0} |
| 2729 | }; |
| 2730 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2731 | /* |
| 2732 | * Convert a charset ID to a name. |
| 2733 | * Return NULL when not recognized. |
| 2734 | */ |
| 2735 | char * |
| 2736 | charset_id2name(int id) |
| 2737 | { |
| 2738 | struct charset_pair *cp; |
| 2739 | |
| 2740 | for (cp = charset_pairs; cp->name != NULL; ++cp) |
| 2741 | if ((BYTE)id == cp->charset) |
| 2742 | break; |
| 2743 | return cp->name; |
| 2744 | } |
| 2745 | |
Bram Moolenaar | 7c1c6db | 2016-04-03 22:08:05 +0200 | [diff] [blame] | 2746 | /* |
| 2747 | * Convert a quality ID to a name. |
| 2748 | * Return NULL when not recognized. |
| 2749 | */ |
| 2750 | char * |
| 2751 | quality_id2name(DWORD id) |
| 2752 | { |
| 2753 | struct quality_pair *qp; |
| 2754 | |
| 2755 | for (qp = quality_pairs; qp->name != NULL; ++qp) |
| 2756 | if (id == qp->quality) |
| 2757 | break; |
| 2758 | return qp->name; |
| 2759 | } |
| 2760 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2761 | static const LOGFONT s_lfDefault = |
| 2762 | { |
| 2763 | -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, |
| 2764 | OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, |
| 2765 | PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE, |
| 2766 | "Fixedsys" /* see _ReadVimIni */ |
| 2767 | }; |
| 2768 | |
| 2769 | /* Initialise the "current height" to -12 (same as s_lfDefault) just |
| 2770 | * in case the user specifies a font in "guifont" with no size before a font |
| 2771 | * with an explicit size has been set. This defaults the size to this value |
| 2772 | * (-12 equates to roughly 9pt). |
| 2773 | */ |
| 2774 | int current_font_height = -12; /* also used in gui_w48.c */ |
| 2775 | |
| 2776 | /* Convert a string representing a point size into pixels. The string should |
| 2777 | * be a positive decimal number, with an optional decimal point (eg, "12", or |
| 2778 | * "10.5"). The pixel value is returned, and a pointer to the next unconverted |
| 2779 | * character is stored in *end. The flag "vertical" says whether this |
| 2780 | * calculation is for a vertical (height) size or a horizontal (width) one. |
| 2781 | */ |
| 2782 | static int |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 2783 | points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2784 | { |
| 2785 | int pixels; |
| 2786 | int points = 0; |
| 2787 | int divisor = 0; |
| 2788 | HWND hwnd = (HWND)0; |
| 2789 | HDC hdc; |
| 2790 | HDC printer_dc = (HDC)pprinter_dc; |
| 2791 | |
| 2792 | while (*str != NUL) |
| 2793 | { |
| 2794 | if (*str == '.' && divisor == 0) |
| 2795 | { |
| 2796 | /* Start keeping a divisor, for later */ |
| 2797 | divisor = 1; |
| 2798 | } |
| 2799 | else |
| 2800 | { |
| 2801 | if (!VIM_ISDIGIT(*str)) |
| 2802 | break; |
| 2803 | |
| 2804 | points *= 10; |
| 2805 | points += *str - '0'; |
| 2806 | divisor *= 10; |
| 2807 | } |
| 2808 | ++str; |
| 2809 | } |
| 2810 | |
| 2811 | if (divisor == 0) |
| 2812 | divisor = 1; |
| 2813 | |
| 2814 | if (printer_dc == NULL) |
| 2815 | { |
| 2816 | hwnd = GetDesktopWindow(); |
| 2817 | hdc = GetWindowDC(hwnd); |
| 2818 | } |
| 2819 | else |
| 2820 | hdc = printer_dc; |
| 2821 | |
| 2822 | pixels = MulDiv(points, |
| 2823 | GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX), |
| 2824 | 72 * divisor); |
| 2825 | |
| 2826 | if (printer_dc == NULL) |
| 2827 | ReleaseDC(hwnd, hdc); |
| 2828 | |
| 2829 | *end = str; |
| 2830 | return pixels; |
| 2831 | } |
| 2832 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2833 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2834 | static int CALLBACK |
| 2835 | font_enumproc( |
| 2836 | ENUMLOGFONT *elf, |
| 2837 | NEWTEXTMETRIC *ntm, |
| 2838 | int type, |
| 2839 | LPARAM lparam) |
| 2840 | { |
| 2841 | /* Return value: |
| 2842 | * 0 = terminate now (monospace & ANSI) |
| 2843 | * 1 = continue, still no luck... |
| 2844 | * 2 = continue, but we have an acceptable LOGFONT |
| 2845 | * (monospace, not ANSI) |
| 2846 | * We use these values, as EnumFontFamilies returns 1 if the |
| 2847 | * callback function is never called. So, we check the return as |
| 2848 | * 0 = perfect, 2 = OK, 1 = no good... |
| 2849 | * It's not pretty, but it works! |
| 2850 | */ |
| 2851 | |
| 2852 | LOGFONT *lf = (LOGFONT *)(lparam); |
| 2853 | |
| 2854 | #ifndef FEAT_PROPORTIONAL_FONTS |
| 2855 | /* Ignore non-monospace fonts without further ado */ |
| 2856 | if ((ntm->tmPitchAndFamily & 1) != 0) |
| 2857 | return 1; |
| 2858 | #endif |
| 2859 | |
| 2860 | /* Remember this LOGFONT as a "possible" */ |
| 2861 | *lf = elf->elfLogFont; |
| 2862 | |
| 2863 | /* Terminate the scan as soon as we find an ANSI font */ |
| 2864 | if (lf->lfCharSet == ANSI_CHARSET |
| 2865 | || lf->lfCharSet == OEM_CHARSET |
| 2866 | || lf->lfCharSet == DEFAULT_CHARSET) |
| 2867 | return 0; |
| 2868 | |
| 2869 | /* Continue the scan - we have a non-ANSI font */ |
| 2870 | return 2; |
| 2871 | } |
| 2872 | |
| 2873 | static int |
| 2874 | init_logfont(LOGFONT *lf) |
| 2875 | { |
| 2876 | int n; |
| 2877 | HWND hwnd = GetDesktopWindow(); |
| 2878 | HDC hdc = GetWindowDC(hwnd); |
| 2879 | |
| 2880 | n = EnumFontFamilies(hdc, |
| 2881 | (LPCSTR)lf->lfFaceName, |
| 2882 | (FONTENUMPROC)font_enumproc, |
| 2883 | (LPARAM)lf); |
| 2884 | |
| 2885 | ReleaseDC(hwnd, hdc); |
| 2886 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2887 | /* If we couldn't find a usable font, return failure */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2888 | if (n == 1) |
| 2889 | return FAIL; |
| 2890 | |
| 2891 | /* Tidy up the rest of the LOGFONT structure. We set to a basic |
| 2892 | * font - get_logfont() sets bold, italic, etc based on the user's |
| 2893 | * input. |
| 2894 | */ |
| 2895 | lf->lfHeight = current_font_height; |
| 2896 | lf->lfWidth = 0; |
| 2897 | lf->lfItalic = FALSE; |
| 2898 | lf->lfUnderline = FALSE; |
| 2899 | lf->lfStrikeOut = FALSE; |
| 2900 | lf->lfWeight = FW_NORMAL; |
| 2901 | |
| 2902 | /* Return success */ |
| 2903 | return OK; |
| 2904 | } |
| 2905 | |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2906 | /* |
| 2907 | * Get font info from "name" into logfont "lf". |
| 2908 | * Return OK for a valid name, FAIL otherwise. |
| 2909 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2910 | int |
| 2911 | get_logfont( |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2912 | LOGFONT *lf, |
| 2913 | char_u *name, |
| 2914 | HDC printer_dc, |
| 2915 | int verbose) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2916 | { |
| 2917 | char_u *p; |
| 2918 | int i; |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 2919 | int ret = FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2920 | static LOGFONT *lastlf = NULL; |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 2921 | #ifdef FEAT_MBYTE |
| 2922 | char_u *acpname = NULL; |
| 2923 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2924 | |
| 2925 | *lf = s_lfDefault; |
| 2926 | if (name == NULL) |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2927 | return OK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2928 | |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 2929 | #ifdef FEAT_MBYTE |
| 2930 | /* Convert 'name' from 'encoding' to the current codepage, because |
| 2931 | * lf->lfFaceName uses the current codepage. |
| 2932 | * TODO: Use Wide APIs instead of ANSI APIs. */ |
| 2933 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 2934 | { |
| 2935 | int len; |
Bram Moolenaar | 418f81b | 2016-02-16 20:12:02 +0100 | [diff] [blame] | 2936 | enc_to_acp(name, (int)STRLEN(name), &acpname, &len); |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 2937 | name = acpname; |
| 2938 | } |
| 2939 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2940 | if (STRCMP(name, "*") == 0) |
| 2941 | { |
| 2942 | #if defined(FEAT_GUI_W32) |
| 2943 | CHOOSEFONT cf; |
| 2944 | /* if name is "*", bring up std font dialog: */ |
Bram Moolenaar | 7db5fc8 | 2010-05-24 11:59:29 +0200 | [diff] [blame] | 2945 | vim_memset(&cf, 0, sizeof(cf)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2946 | cf.lStructSize = sizeof(cf); |
| 2947 | cf.hwndOwner = s_hwnd; |
| 2948 | cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT; |
| 2949 | if (lastlf != NULL) |
| 2950 | *lf = *lastlf; |
| 2951 | cf.lpLogFont = lf; |
| 2952 | cf.nFontType = 0 ; //REGULAR_FONTTYPE; |
| 2953 | if (ChooseFont(&cf)) |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 2954 | ret = OK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2955 | #endif |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 2956 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | /* |
| 2960 | * Split name up, it could be <name>:h<height>:w<width> etc. |
| 2961 | */ |
| 2962 | for (p = name; *p && *p != ':'; p++) |
| 2963 | { |
| 2964 | if (p - name + 1 > LF_FACESIZE) |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 2965 | goto theend; /* Name too long */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2966 | lf->lfFaceName[p - name] = *p; |
| 2967 | } |
| 2968 | if (p != name) |
| 2969 | lf->lfFaceName[p - name] = NUL; |
| 2970 | |
| 2971 | /* First set defaults */ |
| 2972 | lf->lfHeight = -12; |
| 2973 | lf->lfWidth = 0; |
| 2974 | lf->lfWeight = FW_NORMAL; |
| 2975 | lf->lfItalic = FALSE; |
| 2976 | lf->lfUnderline = FALSE; |
| 2977 | lf->lfStrikeOut = FALSE; |
| 2978 | |
| 2979 | /* |
| 2980 | * If the font can't be found, try replacing '_' by ' '. |
| 2981 | */ |
| 2982 | if (init_logfont(lf) == FAIL) |
| 2983 | { |
| 2984 | int did_replace = FALSE; |
| 2985 | |
| 2986 | for (i = 0; lf->lfFaceName[i]; ++i) |
| 2987 | if (lf->lfFaceName[i] == '_') |
| 2988 | { |
| 2989 | lf->lfFaceName[i] = ' '; |
| 2990 | did_replace = TRUE; |
| 2991 | } |
| 2992 | if (!did_replace || init_logfont(lf) == FAIL) |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 2993 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | while (*p == ':') |
| 2997 | p++; |
| 2998 | |
| 2999 | /* Set the values found after ':' */ |
| 3000 | while (*p) |
| 3001 | { |
| 3002 | switch (*p++) |
| 3003 | { |
| 3004 | case 'h': |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 3005 | lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3006 | break; |
| 3007 | case 'w': |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 3008 | lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3009 | break; |
| 3010 | case 'b': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3011 | lf->lfWeight = FW_BOLD; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3012 | break; |
| 3013 | case 'i': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3014 | lf->lfItalic = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3015 | break; |
| 3016 | case 'u': |
| 3017 | lf->lfUnderline = TRUE; |
| 3018 | break; |
| 3019 | case 's': |
| 3020 | lf->lfStrikeOut = TRUE; |
| 3021 | break; |
| 3022 | case 'c': |
| 3023 | { |
| 3024 | struct charset_pair *cp; |
| 3025 | |
| 3026 | for (cp = charset_pairs; cp->name != NULL; ++cp) |
| 3027 | if (STRNCMP(p, cp->name, strlen(cp->name)) == 0) |
| 3028 | { |
| 3029 | lf->lfCharSet = cp->charset; |
| 3030 | p += strlen(cp->name); |
| 3031 | break; |
| 3032 | } |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 3033 | if (cp->name == NULL && verbose) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3034 | { |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 3035 | vim_snprintf((char *)IObuff, IOSIZE, |
| 3036 | _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3037 | EMSG(IObuff); |
| 3038 | break; |
| 3039 | } |
| 3040 | break; |
| 3041 | } |
Bram Moolenaar | 7c1c6db | 2016-04-03 22:08:05 +0200 | [diff] [blame] | 3042 | case 'q': |
| 3043 | { |
| 3044 | struct quality_pair *qp; |
| 3045 | |
| 3046 | for (qp = quality_pairs; qp->name != NULL; ++qp) |
| 3047 | if (STRNCMP(p, qp->name, strlen(qp->name)) == 0) |
| 3048 | { |
| 3049 | lf->lfQuality = qp->quality; |
| 3050 | p += strlen(qp->name); |
| 3051 | break; |
| 3052 | } |
| 3053 | if (qp->name == NULL && verbose) |
| 3054 | { |
| 3055 | vim_snprintf((char *)IObuff, IOSIZE, |
| 3056 | _("E244: Illegal quality name \"%s\" in font name \"%s\""), p, name); |
| 3057 | EMSG(IObuff); |
| 3058 | break; |
| 3059 | } |
| 3060 | break; |
| 3061 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3062 | default: |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 3063 | if (verbose) |
| 3064 | { |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 3065 | vim_snprintf((char *)IObuff, IOSIZE, |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 3066 | _("E245: Illegal char '%c' in font name \"%s\""), |
| 3067 | p[-1], name); |
| 3068 | EMSG(IObuff); |
| 3069 | } |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 3070 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3071 | } |
| 3072 | while (*p == ':') |
| 3073 | p++; |
| 3074 | } |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 3075 | ret = OK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3076 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3077 | theend: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3078 | /* ron: init lastlf */ |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 3079 | if (ret == OK && printer_dc == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3080 | { |
| 3081 | vim_free(lastlf); |
| 3082 | lastlf = (LOGFONT *)alloc(sizeof(LOGFONT)); |
| 3083 | if (lastlf != NULL) |
| 3084 | mch_memmove(lastlf, lf, sizeof(LOGFONT)); |
| 3085 | } |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 3086 | #ifdef FEAT_MBYTE |
| 3087 | vim_free(acpname); |
| 3088 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3089 | |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 3090 | return ret; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | #endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */ |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 3094 | |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 3095 | #if defined(FEAT_JOB_CHANNEL) || defined(PROTO) |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 3096 | /* |
| 3097 | * Initialize the Winsock dll. |
| 3098 | */ |
| 3099 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3100 | channel_init_winsock(void) |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 3101 | { |
| 3102 | WSADATA wsaData; |
| 3103 | int wsaerr; |
| 3104 | |
| 3105 | if (WSInitialized) |
| 3106 | return; |
| 3107 | |
| 3108 | wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData); |
| 3109 | if (wsaerr == 0) |
| 3110 | WSInitialized = TRUE; |
| 3111 | } |
| 3112 | #endif |