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