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