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