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