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