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