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