blob: fc06ab687c09635c1293a946b3573880798c612e [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
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100164#ifdef FEAT_CHANNEL
165int WSInitialized = FALSE; /* WinSock is initialized */
166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
168/* Don't generate prototypes here, because some systems do have these
169 * functions. */
170#if defined(__GNUC__) && !defined(PROTO)
171# ifndef __MINGW32__
172int _stricoll(char *a, char *b)
173{
174 // the ANSI-ish correct way is to use strxfrm():
175 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
176 strxfrm(a_buff, a, 512);
177 strxfrm(b_buff, b, 512);
178 return strcoll(a_buff, b_buff);
179}
180
181char * _fullpath(char *buf, char *fname, int len)
182{
183 LPTSTR toss;
184
185 return (char *)GetFullPathName(fname, len, buf, &toss);
186}
187# endif
188
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100189# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190int _chdrive(int drive)
191{
192 char temp [3] = "-:";
193 temp[0] = drive + 'A' - 1;
194 return !SetCurrentDirectory(temp);
195}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#else
198# ifdef __BORLANDC__
199/* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll:
200 * but it does in BC 5.02! */
201# if __BORLANDC__ < 0x502
202int _stricoll(char *a, char *b)
203{
204# if 1
205 // this is fast but not correct:
206 return stricmp(a, b);
207# else
208 // the ANSI-ish correct way is to use strxfrm():
209 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
210 strxfrm(a_buff, a, 512);
211 strxfrm(b_buff, b, 512);
212 return strcoll(a_buff, b_buff);
213# endif
214}
215# endif
216# endif
217#endif
218
219
220#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
221/*
222 * GUI version of mch_exit().
223 * Shut down and exit with status `r'
224 * Careful: mch_exit() may be called before mch_init()!
225 */
226 void
227mch_exit(int r)
228{
229 display_errors();
230
231 ml_close_all(TRUE); /* remove all memfiles */
232
233# ifdef FEAT_OLE
234 UninitOLE();
235# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100236# ifdef FEAT_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 if (WSInitialized)
238 {
239 WSInitialized = FALSE;
240 WSACleanup();
241 }
242# endif
243#ifdef DYNAMIC_GETTEXT
244 dyn_libintl_end();
245#endif
246
247 if (gui.in_use)
248 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000249
250#ifdef EXITFREE
251 free_all_mem();
252#endif
253
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 exit(r);
255}
256
257#endif /* FEAT_GUI_MSWIN */
258
259
260/*
261 * Init the tables for toupper() and tolower().
262 */
263 void
264mch_early_init(void)
265{
266 int i;
267
268#ifdef WIN3264
269 PlatformId();
270#endif
271
272 /* Init the tables for toupper() and tolower() */
273 for (i = 0; i < 256; ++i)
274 toupper_tab[i] = tolower_tab[i] = i;
275#ifdef WIN3264
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100276 CharUpperBuff((LPSTR)toupper_tab, 256);
277 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100279 AnsiUpperBuff((LPSTR)toupper_tab, 256);
280 AnsiLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281#endif
282}
283
284
285/*
286 * Return TRUE if the input comes from a terminal, FALSE otherwise.
287 */
288 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100289mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290{
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 Moolenaar6aa2cd42016-02-16 15:06:59 +0100330 SetConsoleTitle((LPCSTR)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
Bram Moolenaar1df52d72014-10-15 22:50:10 +0200349 SetConsoleTitle(g_szOrigTitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#endif
351}
352
353
354/*
355 * Return TRUE if we can restore the title (we can)
356 */
357 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100358mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
360 return TRUE;
361}
362
363
364/*
365 * Return TRUE if we can restore the icon title (we can't)
366 */
367 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100368mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369{
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 Moolenaar374bf022014-11-05 19:33:24 +0100416 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000417 {
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 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100431 if (_fullpath((char *)buf, (const char *)fname, len - 1) == NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000432 {
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{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200457#ifdef FEAT_MBYTE
458 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
459 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
460 * UTF-8. */
461 char szName[_MAX_PATH * 3 + 1];
462#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 char szName[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
466 /* A name like "d:/foo" and "//server/share" is absolute */
467 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
468 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
469 return TRUE;
470
471 /* A name that can't be made absolute probably isn't absolute. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100472 if (mch_FullName(fname, (char_u *)szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 return FALSE;
474
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100475 return pathcmp((const char *)fname, (const char *)szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476}
477
478/*
479 * Replace all slashes by backslashes.
480 * This used to be the other way around, but MS-DOS sometimes has problems
481 * with slashes (e.g. in a command name). We can't have mixed slashes and
482 * backslashes, because comparing file names will not work correctly. The
483 * commands that use a file name should try to avoid the need to type a
484 * backslash twice.
485 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200486 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 */
488 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100489slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200491 if (path_with_url(p))
492 return;
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000493 while (*p)
494 {
495 if (*p == psepcN)
496 *p = psepc;
497 mb_ptr_adv(p);
498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499}
500
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100501#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200502# define OPEN_OH_ARGTYPE intptr_t
503#else
504# define OPEN_OH_ARGTYPE long
505#endif
506
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200507 static int
508stat_symlink_aware(const char *name, struct stat *stp)
509{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100510#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
511 /* Work around for VC12 or earlier (and MinGW). stat() can't handle
512 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200513 * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
514 * status of a symlink itself.
515 * VC10: stat() supports a symlink to a normal file, but it doesn't support
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100516 * a symlink to a directory (always returns an error).
517 * VC11 and VC12: stat() doesn't return an error for a symlink to a
518 * directory, but it doesn't set S_IFDIR flag.
519 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200520 WIN32_FIND_DATA findData;
521 HANDLE hFind, h;
522 DWORD attr = 0;
523 BOOL is_symlink = FALSE;
524
525 hFind = FindFirstFile(name, &findData);
526 if (hFind != INVALID_HANDLE_VALUE)
527 {
528 attr = findData.dwFileAttributes;
529 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
530 && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
531 is_symlink = TRUE;
532 FindClose(hFind);
533 }
534 if (is_symlink)
535 {
536 h = CreateFile(name, FILE_READ_ATTRIBUTES,
537 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
538 OPEN_EXISTING,
539 (attr & FILE_ATTRIBUTE_DIRECTORY)
540 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
541 NULL);
542 if (h != INVALID_HANDLE_VALUE)
543 {
544 int fd, n;
545
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200546 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200547 n = _fstat(fd, (struct _stat*)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100548 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
549 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200550 _close(fd);
551 return n;
552 }
553 }
554#endif
555 return stat(name, stp);
556}
557
558#ifdef FEAT_MBYTE
559 static int
560wstat_symlink_aware(const WCHAR *name, struct _stat *stp)
561{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100562# if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
563 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
564 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200565 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
566 * status of a symlink itself.
567 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100568 * support a symlink to a directory (always returns an error).
569 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
570 * directory, but it doesn't set S_IFDIR flag.
571 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200572 int n;
573 BOOL is_symlink = FALSE;
574 HANDLE hFind, h;
575 DWORD attr = 0;
576 WIN32_FIND_DATAW findDataW;
577
578 hFind = FindFirstFileW(name, &findDataW);
579 if (hFind != INVALID_HANDLE_VALUE)
580 {
581 attr = findDataW.dwFileAttributes;
582 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
583 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
584 is_symlink = TRUE;
585 FindClose(hFind);
586 }
587 if (is_symlink)
588 {
589 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
590 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
591 OPEN_EXISTING,
592 (attr & FILE_ATTRIBUTE_DIRECTORY)
593 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
594 NULL);
595 if (h != INVALID_HANDLE_VALUE)
596 {
597 int fd;
598
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200599 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200600 n = _fstat(fd, stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100601 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
602 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200603 _close(fd);
604 return n;
605 }
606 }
607# endif
608 return _wstat(name, stp);
609}
610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611
612/*
613 * stat() can't handle a trailing '/' or '\', remove it first.
614 */
615 int
616vim_stat(const char *name, struct stat *stp)
617{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200618#ifdef FEAT_MBYTE
619 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
620 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
621 * UTF-8. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100622 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200623#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100624 char_u buf[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200625#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100626 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200628 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100629 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 if (p > buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000631 mb_ptr_back(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100632
633 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
635 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100636
637 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
638 {
639 /* UNC root path must be followed by '\\'. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100640 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100641 if (p != NULL)
642 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100643 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100644 if (p == NULL)
645 STRCAT(buf, "\\");
646 }
647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#ifdef FEAT_MBYTE
649 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
650# ifdef __BORLANDC__
651 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
652 && g_PlatformId == VER_PLATFORM_WIN32_NT
653# endif
654 )
655 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000656 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 int n;
658
659 if (wp != NULL)
660 {
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200661 n = wstat_symlink_aware(wp, (struct _stat *)stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 vim_free(wp);
Bram Moolenaarcd981f22014-02-11 17:06:00 +0100663 if (n >= 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 return n;
665 /* Retry with non-wide function (for Windows 98). Can't use
666 * GetLastError() here and it's unclear what errno gets set to if
667 * the _wstat() fails for missing wide functions. */
668 }
669 }
670#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100671 return stat_symlink_aware((char *)buf, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672}
673
674#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000675/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 void
677mch_settmode(int tmode)
678{
679 /* nothing to do */
680}
681
682 int
683mch_get_shellsize(void)
684{
685 /* never used */
686 return OK;
687}
688
689 void
690mch_set_shellsize(void)
691{
692 /* never used */
693}
694
695/*
696 * Rows and/or Columns has changed.
697 */
698 void
699mch_new_shellsize(void)
700{
701 /* never used */
702}
703
704#endif
705
706/*
707 * We have no job control, so fake it by starting a new shell.
708 */
709 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100710mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712 suspend_shell();
713}
714
715#if defined(USE_MCH_ERRMSG) || defined(PROTO)
716
717#ifdef display_errors
718# undef display_errors
719#endif
720
721/*
722 * Display the saved error message(s).
723 */
724 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100725display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726{
727 char *p;
728
729 if (error_ga.ga_data != NULL)
730 {
731 /* avoid putting up a message box with blanks only */
732 for (p = (char *)error_ga.ga_data; *p; ++p)
733 if (!isspace(*p))
734 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000735 (void)gui_mch_dialog(
736#ifdef FEAT_GUI
737 gui.starting ? VIM_INFO :
738#endif
739 VIM_ERROR,
740#ifdef FEAT_GUI
741 gui.starting ? (char_u *)_("Message") :
742#endif
743 (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +0100744 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 break;
746 }
747 ga_clear(&error_ga);
748 }
749}
750#endif
751
752
753/*
754 * Return TRUE if "p" contain a wildcard that can be expanded by
755 * dos_expandpath().
756 */
757 int
758mch_has_exp_wildcard(char_u *p)
759{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000760 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 {
762 if (vim_strchr((char_u *)"?*[", *p) != NULL
763 || (*p == '~' && p[1] != NUL))
764 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 }
766 return FALSE;
767}
768
769/*
770 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
771 * shortened file name).
772 */
773 int
774mch_has_wildcard(char_u *p)
775{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000776 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {
778 if (vim_strchr((char_u *)
779# ifdef VIM_BACKTICK
780 "?*$[`"
781# else
782 "?*$["
783# endif
784 , *p) != NULL
785 || (*p == '~' && p[1] != NUL))
786 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788 return FALSE;
789}
790
791
792/*
793 * The normal _chdir() does not change the default drive. This one does.
794 * Returning 0 implies success; -1 implies failure.
795 */
796 int
797mch_chdir(char *path)
798{
799 if (path[0] == NUL) /* just checking... */
800 return -1;
801
Bram Moolenaara2974d72009-07-14 16:38:36 +0000802 if (p_verbose >= 5)
803 {
804 verbose_enter();
805 smsg((char_u *)"chdir(%s)", path);
806 verbose_leave();
807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
809 {
810 /* If we can change to the drive, skip that part of the path. If we
811 * can't then the current directory may be invalid, try using chdir()
812 * with the whole path. */
813 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
814 path += 2;
815 }
816
817 if (*path == NUL) /* drive name only */
818 return 0;
819
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000820#ifdef FEAT_MBYTE
821 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
822 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100823 WCHAR *p = enc_to_utf16((char_u *)path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000824 int n;
825
826 if (p != NULL)
827 {
828 n = _wchdir(p);
829 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +0100830 if (n == 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
831 return n;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000832 /* Retry with non-wide function (for Windows 98). */
833 }
834 }
835#endif
836
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 return chdir(path); /* let the normal chdir() do the rest */
838}
839
840
841/*
842 * Switching off termcap mode is only allowed when Columns is 80, otherwise a
843 * crash may result. It's always allowed on NT or when running the GUI.
844 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000845/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 int
847can_end_termcap_mode(
848 int give_msg)
849{
850#ifdef FEAT_GUI_MSWIN
851 return TRUE; /* GUI starts a new console anyway */
852#else
853 if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80)
854 return TRUE;
855 if (give_msg)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100856 msg((char_u *)
857 _("'columns' is not 80, cannot execute external commands"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 return FALSE;
859#endif
860}
861
862#ifdef FEAT_GUI_MSWIN
863/*
864 * return non-zero if a character is available
865 */
866 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100867mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868{
869 /* never used */
870 return TRUE;
871}
872#endif
873
874
875/*
876 * set screen mode, always fails.
877 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000878/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 int
880mch_screenmode(
881 char_u *arg)
882{
883 EMSG(_(e_screenmode));
884 return FAIL;
885}
886
887
888#if defined(FEAT_LIBCALL) || defined(PROTO)
889/*
890 * Call a DLL routine which takes either a string or int param
891 * and returns an allocated string.
892 * Return OK if it worked, FAIL if not.
893 */
894# ifdef WIN3264
895typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
896typedef LPTSTR (*MYINTPROCSTR)(int);
897typedef int (*MYSTRPROCINT)(LPTSTR);
898typedef int (*MYINTPROCINT)(int);
899# else
900typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
901typedef LPSTR (*MYINTPROCSTR)(int);
902typedef int (*MYSTRPROCINT)(LPSTR);
903typedef int (*MYINTPROCINT)(int);
904# endif
905
906# ifndef WIN16
907/*
908 * Check if a pointer points to a valid NUL terminated string.
909 * Return the length of the string, including terminating NUL.
910 * Returns 0 for an invalid pointer, 1 for an empty string.
911 */
912 static size_t
913check_str_len(char_u *str)
914{
915 SYSTEM_INFO si;
916 MEMORY_BASIC_INFORMATION mbi;
917 size_t length = 0;
918 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100919 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
921 /* get page size */
922 GetSystemInfo(&si);
923
924 /* get memory information */
925 if (VirtualQuery(str, &mbi, sizeof(mbi)))
926 {
927 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000928 long_u dwStr = (long_u)str;
929 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
931 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000932 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
934 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000935 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
Bram Moolenaar442b4222010-05-24 21:34:22 +0200937 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 p += pageLength, pageLength = si.dwPageSize)
939 for (i = 0; i < pageLength; ++i, ++length)
940 if (p[i] == NUL)
941 return length + 1;
942 }
943
944 return 0;
945}
946# endif
947
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200948/*
949 * Passed to do_in_runtimepath() to load a vim.ico file.
950 */
951 static void
952mch_icon_load_cb(char_u *fname, void *cookie)
953{
954 HANDLE *h = (HANDLE *)cookie;
955
956 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100957 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200958 IMAGE_ICON,
959 64,
960 64,
961 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
962}
963
964/*
965 * Try loading an icon file from 'runtimepath'.
966 */
967 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100968mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200969{
970 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
971 FALSE, mch_icon_load_cb, iconp);
972}
973
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 int
975mch_libcall(
976 char_u *libname,
977 char_u *funcname,
978 char_u *argstring, /* NULL when using a argint */
979 int argint,
980 char_u **string_result,/* NULL when using number_result */
981 int *number_result)
982{
983 HINSTANCE hinstLib;
984 MYSTRPROCSTR ProcAdd;
985 MYINTPROCSTR ProcAddI;
986 char_u *retval_str = NULL;
987 int retval_int = 0;
988 size_t len;
989
990 BOOL fRunTimeLinkSuccess = FALSE;
991
992 // Get a handle to the DLL module.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200993# ifdef WIN16
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 hinstLib = LoadLibrary(libname);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200995# else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100996 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
999 // If the handle is valid, try to get the function address.
1000 if (hinstLib != NULL)
1001 {
1002#ifdef HAVE_TRY_EXCEPT
1003 __try
1004 {
1005#endif
1006 if (argstring != NULL)
1007 {
1008 /* Call with string argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001009 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
1011 {
1012 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001013 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001015 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 }
1018 else
1019 {
1020 /* Call with number argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001021 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
1023 {
1024 if (string_result == NULL)
1025 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
1026 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001027 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 }
1030
1031 // Save the string before we free the library.
1032 // Assume that a "1" result is an illegal pointer.
1033 if (string_result == NULL)
1034 *number_result = retval_int;
1035 else if (retval_str != NULL
1036# ifdef WIN16
1037 && retval_str != (char_u *)1
1038 && retval_str != (char_u *)-1
1039 && !IsBadStringPtr(retval_str, INT_MAX)
1040 && (len = strlen(retval_str) + 1) > 0
1041# else
1042 && (len = check_str_len(retval_str)) > 0
1043# endif
1044 )
1045 {
1046 *string_result = lalloc((long_u)len, TRUE);
1047 if (*string_result != NULL)
1048 mch_memmove(*string_result, retval_str, len);
1049 }
1050
1051#ifdef HAVE_TRY_EXCEPT
1052 }
1053 __except(EXCEPTION_EXECUTE_HANDLER)
1054 {
1055 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
1056 RESETSTKOFLW();
1057 fRunTimeLinkSuccess = 0;
1058 }
1059#endif
1060
1061 // Free the DLL module.
1062 (void)FreeLibrary(hinstLib);
1063 }
1064
1065 if (!fRunTimeLinkSuccess)
1066 {
1067 EMSG2(_(e_libcall), funcname);
1068 return FAIL;
1069 }
1070
1071 return OK;
1072}
1073#endif
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075/*
1076 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1077 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001078/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 void
1080DumpPutS(
1081 const char *psz)
1082{
1083# ifdef MCH_WRITE_DUMP
1084 if (fdDump)
1085 {
1086 fputs(psz, fdDump);
1087 if (psz[strlen(psz) - 1] != '\n')
1088 fputc('\n', fdDump);
1089 fflush(fdDump);
1090 }
1091# endif
1092}
1093
1094#ifdef _DEBUG
1095
1096void __cdecl
1097Trace(
1098 char *pszFormat,
1099 ...)
1100{
1101 CHAR szBuff[2048];
1102 va_list args;
1103
1104 va_start(args, pszFormat);
1105 vsprintf(szBuff, pszFormat, args);
1106 va_end(args);
1107
1108 OutputDebugString(szBuff);
1109}
1110
1111#endif //_DEBUG
1112
Bram Moolenaar843ee412004-06-30 16:16:41 +00001113#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114# if defined(FEAT_TITLE) && defined(WIN3264)
1115extern HWND g_hWnd; /* This is in os_win32.c. */
1116# endif
1117
1118/*
1119 * Showing the printer dialog is tricky since we have no GUI
1120 * window to parent it. The following routines are needed to
1121 * get the window parenting and Z-order to work properly.
1122 */
1123 static void
1124GetConsoleHwnd(void)
1125{
1126# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1127
1128 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1129 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1130
1131 /* Skip if it's already set. */
1132 if (s_hwnd != 0)
1133 return;
1134
1135# if defined(FEAT_TITLE) && defined(WIN3264)
1136 /* Window handle may have been found by init code (Windows NT only) */
1137 if (g_hWnd != 0)
1138 {
1139 s_hwnd = g_hWnd;
1140 return;
1141 }
1142# endif
1143
1144 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1145
1146 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1147 pszOldWindowTitle,
1148 GetTickCount(),
1149 GetCurrentProcessId());
1150 SetConsoleTitle(pszNewWindowTitle);
1151 Sleep(40);
1152 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1153
1154 SetConsoleTitle(pszOldWindowTitle);
1155}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001156
1157/*
1158 * Console implementation of ":winpos".
1159 */
1160 int
1161mch_get_winpos(int *x, int *y)
1162{
1163 RECT rect;
1164
1165 GetConsoleHwnd();
1166 GetWindowRect(s_hwnd, &rect);
1167 *x = rect.left;
1168 *y = rect.top;
1169 return OK;
1170}
1171
1172/*
1173 * Console implementation of ":winpos x y".
1174 */
1175 void
1176mch_set_winpos(int x, int y)
1177{
1178 GetConsoleHwnd();
1179 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1180 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#endif
1183
1184#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1185
1186# ifdef WIN16
1187# define TEXT(a) a
1188# endif
1189/*=================================================================
1190 * Win32 printer stuff
1191 */
1192
1193static HFONT prt_font_handles[2][2][2];
1194static PRINTDLG prt_dlg;
1195static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1196static TEXTMETRIC prt_tm;
1197static int prt_line_height;
1198static int prt_number_width;
1199static int prt_left_margin;
1200static int prt_right_margin;
1201static int prt_top_margin;
1202static char_u szAppName[] = TEXT("VIM");
1203static HWND hDlgPrint;
1204static int *bUserAbort = NULL;
1205static char_u *prt_name = NULL;
1206
1207/* Defines which are also in vim.rc. */
1208#define IDC_BOX1 400
1209#define IDC_PRINTTEXT1 401
1210#define IDC_PRINTTEXT2 402
1211#define IDC_PROGRESS 403
1212
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001213#if !defined(FEAT_MBYTE) || defined(WIN16)
1214# define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s)
1215#else
1216 static BOOL
1217vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1218{
1219 WCHAR *wp = NULL;
1220 BOOL ret;
1221
1222 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1223 {
1224 wp = enc_to_utf16(s, NULL);
1225 }
1226 if (wp != NULL)
1227 {
1228 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1229 vim_free(wp);
1230 return ret;
1231 }
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001232 return SetDlgItemText(hDlg, nIDDlgItem, (LPCSTR)s);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001233}
1234#endif
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236/*
1237 * Convert BGR to RGB for Windows GDI calls
1238 */
1239 static COLORREF
1240swap_me(COLORREF colorref)
1241{
1242 int temp;
1243 char *ptr = (char *)&colorref;
1244
1245 temp = *(ptr);
1246 *(ptr ) = *(ptr + 2);
1247 *(ptr + 2) = temp;
1248 return colorref;
1249}
1250
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001251/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001252#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001253# define PDP_RETVAL BOOL
1254#else
1255# define PDP_RETVAL INT_PTR
1256#endif
1257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001258/*ARGSUSED*/
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001259 static PDP_RETVAL CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1261{
1262#ifdef FEAT_GETTEXT
1263 NONCLIENTMETRICS nm;
1264 static HFONT hfont;
1265#endif
1266
1267 switch (message)
1268 {
1269 case WM_INITDIALOG:
1270#ifdef FEAT_GETTEXT
1271 nm.cbSize = sizeof(NONCLIENTMETRICS);
1272 if (SystemParametersInfo(
1273 SPI_GETNONCLIENTMETRICS,
1274 sizeof(NONCLIENTMETRICS),
1275 &nm,
1276 0))
1277 {
1278 char buff[MAX_PATH];
1279 int i;
1280
1281 /* Translate the dialog texts */
1282 hfont = CreateFontIndirect(&nm.lfMessageFont);
1283 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1284 {
1285 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1286 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001287 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289 SendDlgItemMessage(hDlg, IDCANCEL,
1290 WM_SETFONT, (WPARAM)hfont, 1);
1291 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001292 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001295 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 if (prt_name != NULL)
1297 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001298 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 vim_free(prt_name);
1300 prt_name = NULL;
1301 }
1302 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1303#ifndef FEAT_GUI
1304 BringWindowToTop(s_hwnd);
1305#endif
1306 return TRUE;
1307
1308 case WM_COMMAND:
1309 *bUserAbort = TRUE;
1310 EnableWindow(GetParent(hDlg), TRUE);
1311 DestroyWindow(hDlg);
1312 hDlgPrint = NULL;
1313#ifdef FEAT_GETTEXT
1314 DeleteObject(hfont);
1315#endif
1316 return TRUE;
1317 }
1318 return FALSE;
1319}
1320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001321/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 static BOOL CALLBACK
1323AbortProc(HDC hdcPrn, int iCode)
1324{
1325 MSG msg;
1326
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001327 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001329 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 {
1331 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001332 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 }
1334 }
1335 return !*bUserAbort;
1336}
1337
1338#ifndef FEAT_GUI
1339
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001340 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341PrintHookProc(
1342 HWND hDlg, // handle to dialog box
1343 UINT uiMsg, // message identifier
1344 WPARAM wParam, // message parameter
1345 LPARAM lParam // message parameter
1346 )
1347{
1348 HWND hwndOwner;
1349 RECT rc, rcDlg, rcOwner;
1350 PRINTDLG *pPD;
1351
1352 if (uiMsg == WM_INITDIALOG)
1353 {
1354 // Get the owner window and dialog box rectangles.
1355 if ((hwndOwner = GetParent(hDlg)) == NULL)
1356 hwndOwner = GetDesktopWindow();
1357
1358 GetWindowRect(hwndOwner, &rcOwner);
1359 GetWindowRect(hDlg, &rcDlg);
1360 CopyRect(&rc, &rcOwner);
1361
1362 // Offset the owner and dialog box rectangles so that
1363 // right and bottom values represent the width and
1364 // height, and then offset the owner again to discard
1365 // space taken up by the dialog box.
1366
1367 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1368 OffsetRect(&rc, -rc.left, -rc.top);
1369 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1370
1371 // The new position is the sum of half the remaining
1372 // space and the owner's original position.
1373
1374 SetWindowPos(hDlg,
1375 HWND_TOP,
1376 rcOwner.left + (rc.right / 2),
1377 rcOwner.top + (rc.bottom / 2),
1378 0, 0, // ignores size arguments
1379 SWP_NOSIZE);
1380
1381 /* tackle the printdlg copiesctrl problem */
1382 pPD = (PRINTDLG *)lParam;
1383 pPD->nCopies = (WORD)pPD->lCustData;
1384 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1385 /* Bring the window to top */
1386 BringWindowToTop(GetParent(hDlg));
1387 SetForegroundWindow(hDlg);
1388 }
1389
1390 return FALSE;
1391}
1392#endif
1393
1394 void
1395mch_print_cleanup(void)
1396{
1397 int pifItalic;
1398 int pifBold;
1399 int pifUnderline;
1400
1401 for (pifBold = 0; pifBold <= 1; pifBold++)
1402 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1403 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1404 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1405
1406 if (prt_dlg.hDC != NULL)
1407 DeleteDC(prt_dlg.hDC);
1408 if (!*bUserAbort)
1409 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1410}
1411
1412 static int
1413to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1414{
1415 int ret = 0;
1416 int u;
1417 int nr;
1418
1419 u = prt_get_unit(idx);
1420 if (u == PRT_UNIT_NONE)
1421 {
1422 u = PRT_UNIT_PERC;
1423 nr = def_number;
1424 }
1425 else
1426 nr = printer_opts[idx].number;
1427
1428 switch (u)
1429 {
1430 case PRT_UNIT_PERC:
1431 ret = (physsize * nr) / 100;
1432 break;
1433 case PRT_UNIT_INCH:
1434 ret = (nr * dpi);
1435 break;
1436 case PRT_UNIT_MM:
1437 ret = (nr * 10 * dpi) / 254;
1438 break;
1439 case PRT_UNIT_POINT:
1440 ret = (nr * 10 * dpi) / 720;
1441 break;
1442 }
1443
1444 if (ret < offset)
1445 return 0;
1446 else
1447 return ret - offset;
1448}
1449
1450 static int
1451prt_get_cpl(void)
1452{
1453 int hr;
1454 int phyw;
1455 int dvoff;
1456 int rev_offset;
1457 int dpi;
1458#ifdef WIN16
1459 POINT pagesize;
1460#endif
1461
1462 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1463 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1464
1465 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
1466#ifdef WIN16
1467 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1468 phyw = pagesize.x;
1469 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1470 dvoff = pagesize.x;
1471#else
1472 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1473 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
1474#endif
1475 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1476
1477 rev_offset = phyw - (dvoff + hr);
1478
1479 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1480 if (prt_use_number())
1481 {
1482 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1483 prt_left_margin += prt_number_width;
1484 }
1485 else
1486 prt_number_width = 0;
1487
1488 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1489 rev_offset, 5);
1490
1491 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1492}
1493
1494 static int
1495prt_get_lpp(void)
1496{
1497 int vr;
1498 int phyw;
1499 int dvoff;
1500 int rev_offset;
1501 int bottom_margin;
1502 int dpi;
1503#ifdef WIN16
1504 POINT pagesize;
1505#endif
1506
1507 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
1508#ifdef WIN16
1509 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1510 phyw = pagesize.y;
1511 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1512 dvoff = pagesize.y;
1513#else
1514 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1515 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
1516#endif
1517 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1518
1519 rev_offset = phyw - (dvoff + vr);
1520
1521 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1522
1523 /* adjust top margin if there is a header */
1524 prt_top_margin += prt_line_height * prt_header_height();
1525
1526 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1527 rev_offset, 5);
1528
1529 return (bottom_margin - prt_top_margin) / prt_line_height;
1530}
1531
1532 int
1533mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1534{
1535 static HGLOBAL stored_dm = NULL;
1536 static HGLOBAL stored_devn = NULL;
1537 static int stored_nCopies = 1;
1538 static int stored_nFlags = 0;
1539
1540 LOGFONT fLogFont;
1541 int pifItalic;
1542 int pifBold;
1543 int pifUnderline;
1544
1545 DEVMODE *mem;
1546 DEVNAMES *devname;
1547 int i;
1548
1549 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001550 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 prt_dlg.lStructSize = sizeof(PRINTDLG);
1552#ifndef FEAT_GUI
1553 GetConsoleHwnd(); /* get value of s_hwnd */
1554#endif
1555 prt_dlg.hwndOwner = s_hwnd;
1556 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1557 if (!forceit)
1558 {
1559 prt_dlg.hDevMode = stored_dm;
1560 prt_dlg.hDevNames = stored_devn;
1561 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1562#ifndef FEAT_GUI
1563 /*
1564 * Use hook to prevent console window being sent to back
1565 */
1566 prt_dlg.lpfnPrintHook = PrintHookProc;
1567 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1568#endif
1569 prt_dlg.Flags |= stored_nFlags;
1570 }
1571
1572 /*
1573 * If bang present, return default printer setup with no dialog
1574 * never show dialog if we are running over telnet
1575 */
1576 if (forceit
1577#ifndef FEAT_GUI
1578 || !term_console
1579#endif
1580 )
1581 {
1582 prt_dlg.Flags |= PD_RETURNDEFAULT;
1583#ifdef WIN3264
1584 /*
1585 * MSDN suggests setting the first parameter to WINSPOOL for
1586 * NT, but NULL appears to work just as well.
1587 */
1588 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001589 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 else
1591#endif
1592 {
1593 prt_dlg.Flags |= PD_RETURNDEFAULT;
1594 if (PrintDlg(&prt_dlg) == 0)
1595 goto init_fail_dlg;
1596 }
1597 }
1598 else if (PrintDlg(&prt_dlg) == 0)
1599 goto init_fail_dlg;
1600 else
1601 {
1602 /*
1603 * keep the previous driver context
1604 */
1605 stored_dm = prt_dlg.hDevMode;
1606 stored_devn = prt_dlg.hDevNames;
1607 stored_nFlags = prt_dlg.Flags;
1608 stored_nCopies = prt_dlg.nCopies;
1609 }
1610
1611 if (prt_dlg.hDC == NULL)
1612 {
1613 EMSG(_("E237: Printer selection failed"));
1614 mch_print_cleanup();
1615 return FALSE;
1616 }
1617
1618 /* Not all printer drivers report the support of color (or grey) in the
1619 * same way. Let's set has_color if there appears to be some way to print
1620 * more than B&W. */
1621 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1622 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1623 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1624 || i > 2 || i == -1);
1625
1626 /* Ensure all font styles are baseline aligned */
1627 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1628
1629 /*
1630 * On some windows systems the nCopies parameter is not
1631 * passed back correctly. It must be retrieved from the
1632 * hDevMode struct.
1633 */
1634 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1635 if (mem != NULL)
1636 {
1637#ifdef WIN3264
1638 if (mem->dmCopies != 1)
1639 stored_nCopies = mem->dmCopies;
1640#endif
1641 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1642 psettings->duplex = TRUE;
1643 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1644 psettings->has_color = TRUE;
1645 }
1646 GlobalUnlock(prt_dlg.hDevMode);
1647
1648 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1649 if (devname != 0)
1650 {
1651 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1652 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001653 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001654#ifdef FEAT_MBYTE
1655 char_u *printer_name_orig = printer_name;
1656 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
Bram Moolenaarf191d552014-10-09 17:05:56 +02001658 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1659 {
1660 char_u *to_free = NULL;
1661 int maxlen;
1662
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001663 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1664 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001665 if (to_free != NULL)
1666 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001667 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001668 if (to_free != NULL)
1669 port_name = to_free;
1670 }
1671#endif
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001672 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1673 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001675 wsprintf((char *)prt_name, (const char *)text,
1676 printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001677#ifdef FEAT_MBYTE
1678 if (printer_name != printer_name_orig)
1679 vim_free(printer_name);
1680 if (port_name != port_name_orig)
1681 vim_free(port_name);
1682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684 GlobalUnlock(prt_dlg.hDevNames);
1685
1686 /*
1687 * Initialise the font according to 'printfont'
1688 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001689 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001690 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
1692 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1693 mch_print_cleanup();
1694 return FALSE;
1695 }
1696
1697 for (pifBold = 0; pifBold <= 1; pifBold++)
1698 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1699 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1700 {
1701 fLogFont.lfWeight = boldface[pifBold];
1702 fLogFont.lfItalic = pifItalic;
1703 fLogFont.lfUnderline = pifUnderline;
1704 prt_font_handles[pifBold][pifItalic][pifUnderline]
1705 = CreateFontIndirect(&fLogFont);
1706 }
1707
1708 SetBkMode(prt_dlg.hDC, OPAQUE);
1709 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1710
1711 /*
1712 * Fill in the settings struct
1713 */
1714 psettings->chars_per_line = prt_get_cpl();
1715 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001716 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1717 {
1718 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1719 ? prt_dlg.nCopies : 1;
1720 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1721 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001723 if (psettings->n_collated_copies == 0)
1724 psettings->n_collated_copies = 1;
1725
1726 if (psettings->n_uncollated_copies == 0)
1727 psettings->n_uncollated_copies = 1;
1728 } else {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733 psettings->jobname = jobname;
1734
1735 return TRUE;
1736
1737init_fail_dlg:
1738 {
1739 DWORD err = CommDlgExtendedError();
1740
1741 if (err)
1742 {
1743#ifdef WIN16
1744 char buf[20];
1745
1746 sprintf(buf, "%ld", err);
1747 EMSG2(_("E238: Print error: %s"), buf);
1748#else
1749 char_u *buf;
1750
1751 /* I suspect FormatMessage() doesn't work for values returned by
1752 * CommDlgExtendedError(). What does? */
1753 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1754 FORMAT_MESSAGE_FROM_SYSTEM |
1755 FORMAT_MESSAGE_IGNORE_INSERTS,
1756 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1757 EMSG2(_("E238: Print error: %s"),
1758 buf == NULL ? (char_u *)_("Unknown") : buf);
1759 LocalFree((LPVOID)(buf));
1760#endif
1761 }
1762 else
1763 msg_clr_eos(); /* Maybe canceled */
1764
1765 mch_print_cleanup();
1766 return FALSE;
1767 }
1768}
1769
1770
1771 int
1772mch_print_begin(prt_settings_T *psettings)
1773{
1774 int ret;
1775 static DOCINFO di;
1776 char szBuffer[300];
1777
1778 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1779 prt_dlg.hwndOwner, PrintDlgProc);
1780#ifdef WIN16
1781 Escape(prt_dlg.hDC, SETABORTPROC, 0, (LPSTR)AbortProc, NULL);
1782#else
1783 SetAbortProc(prt_dlg.hDC, AbortProc);
1784#endif
1785 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001786 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001788 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 di.cbSize = sizeof(DOCINFO);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001790 di.lpszDocName = (LPCSTR)psettings->jobname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 ret = StartDoc(prt_dlg.hDC, &di);
1792
1793#ifdef FEAT_GUI
1794 /* Give focus back to main window (when using MDI). */
1795 SetFocus(s_hwnd);
1796#endif
1797
1798 return (ret > 0);
1799}
1800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001801/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 void
1803mch_print_end(prt_settings_T *psettings)
1804{
1805 EndDoc(prt_dlg.hDC);
1806 if (!*bUserAbort)
1807 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1808}
1809
1810 int
1811mch_print_end_page(void)
1812{
1813 return (EndPage(prt_dlg.hDC) > 0);
1814}
1815
1816 int
1817mch_print_begin_page(char_u *msg)
1818{
1819 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001820 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 return (StartPage(prt_dlg.hDC) > 0);
1822}
1823
1824 int
1825mch_print_blank_page(void)
1826{
1827 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1828}
1829
1830static int prt_pos_x = 0;
1831static int prt_pos_y = 0;
1832
1833 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001834mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
1836 if (margin)
1837 prt_pos_x = -prt_number_width;
1838 else
1839 prt_pos_x = 0;
1840 prt_pos_y = page_line * prt_line_height
1841 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1842}
1843
1844 int
1845mch_print_text_out(char_u *p, int len)
1846{
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001847#if defined(FEAT_PROPORTIONAL_FONTS) || (defined(FEAT_MBYTE) && !defined(WIN16))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 SIZE sz;
1849#endif
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001850#if defined(FEAT_MBYTE) && !defined(WIN16)
1851 WCHAR *wp = NULL;
1852 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001854 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1855 {
1856 wp = enc_to_utf16(p, &wlen);
1857 }
1858 if (wp != NULL)
1859 {
1860 int ret = FALSE;
1861
1862 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1863 prt_pos_y + prt_top_margin, wp, wlen);
1864 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1865 vim_free(wp);
1866 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1867 /* This is wrong when printing spaces for a TAB. */
1868 if (p[len] != NUL)
1869 {
1870 wlen = MB_PTR2LEN(p + len);
1871 wp = enc_to_utf16(p + len, &wlen);
1872 if (wp != NULL)
1873 {
1874 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1875 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1876 vim_free(wp);
1877 }
1878 }
1879 return ret;
1880 }
1881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001883 prt_pos_y + prt_top_margin,
1884 (LPCSTR)p, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885#ifndef FEAT_PROPORTIONAL_FONTS
1886 prt_pos_x += len * prt_tm.tmAveCharWidth;
1887 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1888 + prt_tm.tmOverhang > prt_right_margin);
1889#else
1890# ifdef WIN16
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001891 GetTextExtentPoint(prt_dlg.hDC, (LPCSTR)p, len, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892# else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001893 GetTextExtentPoint32(prt_dlg.hDC, (LPCSTR)p, len, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894# endif
1895 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1896 /* This is wrong when printing spaces for a TAB. */
1897 if (p[len] == NUL)
1898 return FALSE;
1899# ifdef WIN16
1900 GetTextExtentPoint(prt_dlg.hDC, p + len, 1, &sz);
1901# else
1902 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
1903# endif
1904 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1905#endif
1906}
1907
1908 void
1909mch_print_set_font(int iBold, int iItalic, int iUnderline)
1910{
1911 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1912}
1913
1914 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001915mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001917 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1918 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 /*
1920 * With a white background we can draw characters transparent, which is
1921 * good for italic characters that overlap to the next char cell.
1922 */
1923 if (bgcol == 0xffffffUL)
1924 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1925 else
1926 SetBkMode(prt_dlg.hDC, OPAQUE);
1927}
1928
1929 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001930mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001932 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1933 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934}
1935
1936#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1937
Bram Moolenaar58d98232005-07-23 22:25:46 +00001938
1939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001941# ifndef PROTO
1942# include <shlobj.h>
1943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
1945/*
1946 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1947 * to and return that name in allocated memory.
1948 * Otherwise NULL is returned.
1949 */
1950 char_u *
1951mch_resolve_shortcut(char_u *fname)
1952{
1953 HRESULT hr;
1954 IShellLink *psl = NULL;
1955 IPersistFile *ppf = NULL;
1956 OLECHAR wsz[MAX_PATH];
1957 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001958 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 char_u *rfname = NULL;
1960 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001961# ifdef FEAT_MBYTE
1962 IShellLinkW *pslw = NULL;
1963 WIN32_FIND_DATAW ffdw; // we get those free of charge
1964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
1966 /* Check if the file name ends in ".lnk". Avoid calling
1967 * CoCreateInstance(), it's quite slow. */
1968 if (fname == NULL)
1969 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001970 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1972 return rfname;
1973
1974 CoInitialize(NULL);
1975
Bram Moolenaar604729e2013-08-30 16:44:19 +02001976# ifdef FEAT_MBYTE
1977 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1978 {
1979 // create a link manager object and request its interface
1980 hr = CoCreateInstance(
1981 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1982 &IID_IShellLinkW, (void**)&pslw);
1983 if (hr == S_OK)
1984 {
1985 WCHAR *p = enc_to_utf16(fname, NULL);
1986
1987 if (p != NULL)
1988 {
1989 // Get a pointer to the IPersistFile interface.
1990 hr = pslw->lpVtbl->QueryInterface(
1991 pslw, &IID_IPersistFile, (void**)&ppf);
1992 if (hr != S_OK)
1993 goto shortcut_errorw;
1994
1995 // "load" the name and resolve the link
1996 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1997 if (hr != S_OK)
1998 goto shortcut_errorw;
1999# if 0 // This makes Vim wait a long time if the target does not exist.
2000 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
2001 if (hr != S_OK)
2002 goto shortcut_errorw;
2003# endif
2004
2005 // Get the path to the link target.
2006 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
2007 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
2008 if (hr == S_OK && wsz[0] != NUL)
2009 rfname = utf16_to_enc(wsz, NULL);
2010
2011shortcut_errorw:
2012 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01002013 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02002014 }
2015 }
2016 /* Retry with non-wide function (for Windows 98). */
2017 }
2018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 // create a link manager object and request its interface
2020 hr = CoCreateInstance(
2021 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2022 &IID_IShellLink, (void**)&psl);
2023 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002024 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
2026 // Get a pointer to the IPersistFile interface.
2027 hr = psl->lpVtbl->QueryInterface(
2028 psl, &IID_IPersistFile, (void**)&ppf);
2029 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002030 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
2032 // full path string must be in Unicode.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002033 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fname, -1, wsz, MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002035 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
2037 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002038 goto shortcut_end;
2039# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
2041 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002042 goto shortcut_end;
2043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
2045 // Get the path to the link target.
2046 ZeroMemory(buf, MAX_PATH);
2047 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
2048 if (hr == S_OK && buf[0] != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002049 rfname = vim_strsave((char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
Bram Moolenaar604729e2013-08-30 16:44:19 +02002051shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 // Release all interface pointers (both belong to the same object)
2053 if (ppf != NULL)
2054 ppf->lpVtbl->Release(ppf);
2055 if (psl != NULL)
2056 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02002057# ifdef FEAT_MBYTE
2058 if (pslw != NULL)
2059 pslw->lpVtbl->Release(pslw);
2060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062 CoUninitialize();
2063 return rfname;
2064}
2065#endif
2066
2067#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
2068/*
2069 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
2070 */
2071 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002072win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073{
2074# ifndef FEAT_GUI
2075 GetConsoleHwnd(); /* get value of s_hwnd */
2076# endif
2077 if (s_hwnd != 0)
2078 SetForegroundWindow(s_hwnd);
2079}
2080#endif
2081
2082#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2083/*
2084 * Client-server code for Vim
2085 *
2086 * Originally written by Paul Moore
2087 */
2088
2089/* In order to handle inter-process messages, we need to have a window. But
2090 * the functions in this module can be called before the main GUI window is
2091 * created (and may also be called in the console version, where there is no
2092 * GUI window at all).
2093 *
2094 * So we create a hidden window, and arrange to destroy it on exit.
2095 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002096HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
2098#define VIM_CLASSNAME "VIM_MESSAGES"
2099#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2100
2101/* Communication is via WM_COPYDATA messages. The message type is send in
2102 * the dwData parameter. Types are defined here. */
2103#define COPYDATA_KEYS 0
2104#define COPYDATA_REPLY 1
2105#define COPYDATA_EXPR 10
2106#define COPYDATA_RESULT 11
2107#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002108#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109
2110/* This is a structure containing a server HWND and its name. */
2111struct server_id
2112{
2113 HWND hwnd;
2114 char_u *name;
2115};
2116
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002117/* Last received 'encoding' that the client uses. */
2118static char_u *client_enc = NULL;
2119
2120/*
2121 * Tell the other side what encoding we are using.
2122 * Errors are ignored.
2123 */
2124 static void
2125serverSendEnc(HWND target)
2126{
2127 COPYDATASTRUCT data;
2128
2129 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002130#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002131 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002132 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002133#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002134 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002135 data.lpData = "latin1";
2136#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002137 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2138 (LPARAM)(&data));
2139}
2140
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141/*
2142 * Clean up on exit. This destroys the hidden message window.
2143 */
2144 static void
2145#ifdef __BORLANDC__
2146 _RTLENTRYF
2147#endif
2148CleanUpMessaging(void)
2149{
2150 if (message_window != 0)
2151 {
2152 DestroyWindow(message_window);
2153 message_window = 0;
2154 }
2155}
2156
2157static int save_reply(HWND server, char_u *reply, int expr);
2158
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 * The window procedure for the hidden message window.
2161 * It handles callback messages and notifications from servers.
2162 * In order to process these messages, it is necessary to run a
2163 * message loop. Code which may run before the main message loop
2164 * is started (in the GUI) is careful to pump messages when it needs
2165 * to. Features which require message delivery during normal use will
2166 * not work in the console version - this basically means those
2167 * features which allow Vim to act as a server, rather than a client.
2168 */
2169 static LRESULT CALLBACK
2170Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2171{
2172 if (msg == WM_COPYDATA)
2173 {
2174 /* This is a message from another Vim. The dwData member of the
2175 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002176 * COPYDATA_ENCODING:
2177 * The encoding that the client uses. Following messages will
2178 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 * COPYDATA_KEYS:
2180 * A key sequence. We are a server, and a client wants these keys
2181 * adding to the input queue.
2182 * COPYDATA_REPLY:
2183 * A reply. We are a client, and a server has sent this message
2184 * in response to a request. (server2client())
2185 * COPYDATA_EXPR:
2186 * An expression. We are a server, and a client wants us to
2187 * evaluate this expression.
2188 * COPYDATA_RESULT:
2189 * A reply. We are a client, and a server has sent this message
2190 * in response to a COPYDATA_EXPR.
2191 * COPYDATA_ERROR_RESULT:
2192 * A reply. We are a client, and a server has sent this message
2193 * in response to a COPYDATA_EXPR that failed to evaluate.
2194 */
2195 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2196 HWND sender = (HWND)wParam;
2197 COPYDATASTRUCT reply;
2198 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002200 char_u *str;
2201 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203 switch (data->dwData)
2204 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002205 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002206# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002207 /* Remember the encoding that the client uses. */
2208 vim_free(client_enc);
2209 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002210# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002211 return 1;
2212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 case COPYDATA_KEYS:
2214 /* Remember who sent this, for <client> */
2215 clientWindow = sender;
2216
2217 /* Add the received keys to the input buffer. The loop waiting
2218 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002219 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2220 server_to_input_buf(str);
2221 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223# ifdef FEAT_GUI
2224 /* Wake up the main GUI loop. */
2225 if (s_hwnd != 0)
2226 PostMessage(s_hwnd, WM_NULL, 0, 0);
2227# endif
2228 return 1;
2229
2230 case COPYDATA_EXPR:
2231 /* Remember who sent this, for <client> */
2232 clientWindow = sender;
2233
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002234 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2235 res = eval_client_expr_to_string(str);
2236 vim_free(tofree);
2237
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 if (res == NULL)
2239 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002240 res = vim_strsave((char_u *)_(e_invexprmsg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 reply.dwData = COPYDATA_ERROR_RESULT;
2242 }
2243 else
2244 reply.dwData = COPYDATA_RESULT;
2245 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002246 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002248 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002249 retval = (int)SendMessage(sender, WM_COPYDATA,
2250 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 vim_free(res);
2252 return retval;
2253
2254 case COPYDATA_REPLY:
2255 case COPYDATA_RESULT:
2256 case COPYDATA_ERROR_RESULT:
2257 if (data->lpData != NULL)
2258 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002259 str = serverConvert(client_enc, (char_u *)data->lpData,
2260 &tofree);
2261 if (tofree == NULL)
2262 str = vim_strsave(str);
2263 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 (data->dwData == COPYDATA_REPLY ? 0 :
2265 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002266 2))) == FAIL)
2267 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002269 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002271 char_u winstr[30];
2272
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002273 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002274 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 TRUE, curbuf);
2276 }
2277#endif
2278 }
2279 return 1;
2280 }
2281
2282 return 0;
2283 }
2284
2285 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2286 {
2287 /* When the message window is activated (brought to the foreground),
2288 * this actually applies to the text window. */
2289#ifndef FEAT_GUI
2290 GetConsoleHwnd(); /* get value of s_hwnd */
2291#endif
2292 if (s_hwnd != 0)
2293 {
2294 SetForegroundWindow(s_hwnd);
2295 return 0;
2296 }
2297 }
2298
2299 return DefWindowProc(hwnd, msg, wParam, lParam);
2300}
2301
2302/*
2303 * Initialise the message handling process. This involves creating a window
2304 * to handle messages - the window will not be visible.
2305 */
2306 void
2307serverInitMessaging(void)
2308{
2309 WNDCLASS wndclass;
2310 HINSTANCE s_hinst;
2311
2312 /* Clean up on exit */
2313 atexit(CleanUpMessaging);
2314
2315 /* Register a window class - we only really care
2316 * about the window procedure
2317 */
2318 s_hinst = (HINSTANCE)GetModuleHandle(0);
2319 wndclass.style = 0;
2320 wndclass.lpfnWndProc = Messaging_WndProc;
2321 wndclass.cbClsExtra = 0;
2322 wndclass.cbWndExtra = 0;
2323 wndclass.hInstance = s_hinst;
2324 wndclass.hIcon = NULL;
2325 wndclass.hCursor = NULL;
2326 wndclass.hbrBackground = NULL;
2327 wndclass.lpszMenuName = NULL;
2328 wndclass.lpszClassName = VIM_CLASSNAME;
2329 RegisterClass(&wndclass);
2330
2331 /* Create the message window. It will be hidden, so the details don't
2332 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2333 * focus from gvim. */
2334 message_window = CreateWindow(VIM_CLASSNAME, "",
2335 WS_POPUPWINDOW | WS_CAPTION,
2336 CW_USEDEFAULT, CW_USEDEFAULT,
2337 100, 100, NULL, NULL,
2338 s_hinst, NULL);
2339}
2340
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002341/* Used by serverSendToVim() to find an alternate server name. */
2342static char_u *altname_buf_ptr = NULL;
2343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344/*
2345 * Get the title of the window "hwnd", which is the Vim server name, in
2346 * "name[namelen]" and return the length.
2347 * Returns zero if window "hwnd" is not a Vim server.
2348 */
2349 static int
2350getVimServerName(HWND hwnd, char *name, int namelen)
2351{
2352 int len;
2353 char buffer[VIM_CLASSNAME_LEN + 1];
2354
2355 /* Ignore windows which aren't Vim message windows */
2356 len = GetClassName(hwnd, buffer, sizeof(buffer));
2357 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2358 return 0;
2359
2360 /* Get the title of the window */
2361 return GetWindowText(hwnd, name, namelen);
2362}
2363
2364 static BOOL CALLBACK
2365enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2366{
2367 struct server_id *id = (struct server_id *)lparam;
2368 char server[MAX_PATH];
2369
2370 /* Get the title of the window */
2371 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2372 return TRUE;
2373
2374 /* If this is the server we're looking for, return its HWND */
2375 if (STRICMP(server, id->name) == 0)
2376 {
2377 id->hwnd = hwnd;
2378 return FALSE;
2379 }
2380
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002381 /* If we are looking for an alternate server, remember this name. */
2382 if (altname_buf_ptr != NULL
2383 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2384 && vim_isdigit(server[STRLEN(id->name)]))
2385 {
2386 STRCPY(altname_buf_ptr, server);
2387 altname_buf_ptr = NULL; /* don't use another name */
2388 }
2389
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 /* Otherwise, keep looking */
2391 return TRUE;
2392}
2393
2394 static BOOL CALLBACK
2395enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2396{
2397 garray_T *ga = (garray_T *)lparam;
2398 char server[MAX_PATH];
2399
2400 /* Get the title of the window */
2401 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2402 return TRUE;
2403
2404 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002405 ga_concat(ga, (char_u *)server);
2406 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 return TRUE;
2408}
2409
2410 static HWND
2411findServer(char_u *name)
2412{
2413 struct server_id id;
2414
2415 id.name = name;
2416 id.hwnd = 0;
2417
2418 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2419
2420 return id.hwnd;
2421}
2422
2423 void
2424serverSetName(char_u *name)
2425{
2426 char_u *ok_name;
2427 HWND hwnd = 0;
2428 int i = 0;
2429 char_u *p;
2430
2431 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002432 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 STRCPY(ok_name, name);
2435 p = ok_name + STRLEN(name);
2436
2437 for (;;)
2438 {
2439 /* This is inefficient - we're doing an EnumWindows loop for each
2440 * possible name. It would be better to grab all names in one go,
2441 * and scan the list each time...
2442 */
2443 hwnd = findServer(ok_name);
2444 if (hwnd == 0)
2445 break;
2446
2447 ++i;
2448 if (i >= 1000)
2449 break;
2450
2451 sprintf((char *)p, "%d", i);
2452 }
2453
2454 if (hwnd != 0)
2455 vim_free(ok_name);
2456 else
2457 {
2458 /* Remember the name */
2459 serverName = ok_name;
2460#ifdef FEAT_TITLE
2461 need_maketitle = TRUE; /* update Vim window title later */
2462#endif
2463
2464 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002465 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467#ifdef FEAT_EVAL
2468 /* Set the servername variable */
2469 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2470#endif
2471 }
2472}
2473
2474 char_u *
2475serverGetVimNames(void)
2476{
2477 garray_T ga;
2478
2479 ga_init2(&ga, 1, 100);
2480
2481 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002482 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
2484 return ga.ga_data;
2485}
2486
2487 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002488serverSendReply(
2489 char_u *name, /* Where to send. */
2490 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
2492 HWND target;
2493 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002494 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
2496 /* The "name" argument is a magic cookie obtained from expand("<client>").
2497 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2498 * value of the client's message window HWND.
2499 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002500 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 if (n == 0)
2502 return -1;
2503
2504 target = (HWND)n;
2505 if (!IsWindow(target))
2506 return -1;
2507
2508 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002509 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 data.lpData = reply;
2511
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002512 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2514 (LPARAM)(&data)))
2515 return 0;
2516
2517 return -1;
2518}
2519
2520 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002521serverSendToVim(
2522 char_u *name, /* Where to send. */
2523 char_u *cmd, /* What to send. */
2524 char_u **result, /* Result of eval'ed expression */
2525 void *ptarget, /* HWND of server */
2526 int asExpr, /* Expression or keys? */
2527 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002529 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 COPYDATASTRUCT data;
2531 char_u *retval = NULL;
2532 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002533 char_u altname_buf[MAX_PATH];
2534
2535 /* If the server name does not end in a digit then we look for an
2536 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2537 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2538 altname_buf_ptr = altname_buf;
2539 altname_buf[0] = NUL;
2540 target = findServer(name);
2541 altname_buf_ptr = NULL;
2542 if (target == 0 && altname_buf[0] != NUL)
2543 /* Use another server name we found. */
2544 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
2546 if (target == 0)
2547 {
2548 if (!silent)
2549 EMSG2(_(e_noserver), name);
2550 return -1;
2551 }
2552
2553 if (ptarget)
2554 *(HWND *)ptarget = target;
2555
2556 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002557 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 data.lpData = cmd;
2559
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002560 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2562 (LPARAM)(&data)) == 0)
2563 return -1;
2564
2565 if (asExpr)
2566 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2567
2568 if (result == NULL)
2569 vim_free(retval);
2570 else
2571 *result = retval; /* Caller assumes responsibility for freeing */
2572
2573 return retcode;
2574}
2575
2576/*
2577 * Bring the server to the foreground.
2578 */
2579 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002580serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582 HWND target = findServer(name);
2583
2584 if (target != 0)
2585 SetForegroundWindow(target);
2586}
2587
2588/* Replies from server need to be stored until the client picks them up via
2589 * remote_read(). So we maintain a list of server-id/reply pairs.
2590 * Note that there could be multiple replies from one server pending if the
2591 * client is slow picking them up.
2592 * We just store the replies in a simple list. When we remove an entry, we
2593 * move list entries down to fill the gap.
2594 * The server ID is simply the HWND.
2595 */
2596typedef struct
2597{
2598 HWND server; /* server window */
2599 char_u *reply; /* reply string */
2600 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002601} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
2603static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2604
2605#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2606#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607
2608/* Flag which is used to wait for a reply */
2609static int reply_received = 0;
2610
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002611/*
2612 * Store a reply. "reply" must be allocated memory (or NULL).
2613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 static int
2615save_reply(HWND server, char_u *reply, int expr)
2616{
2617 reply_T *rep;
2618
2619 if (ga_grow(&reply_list, 1) == FAIL)
2620 return FAIL;
2621
2622 rep = REPLY_ITEM(REPLY_COUNT);
2623 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002624 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 rep->expr_result = expr;
2626 if (rep->reply == NULL)
2627 return FAIL;
2628
2629 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 reply_received = 1;
2631 return OK;
2632}
2633
2634/*
2635 * Get a reply from server "server".
2636 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2637 * server2client() message.
2638 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2639 * If "remove" is TRUE, consume the message, the caller must free it then.
2640 * if "wait" is TRUE block until a message arrives (or the server exits).
2641 */
2642 char_u *
2643serverGetReply(HWND server, int *expr_res, int remove, int wait)
2644{
2645 int i;
2646 char_u *reply;
2647 reply_T *rep;
2648
2649 /* When waiting, loop until the message waiting for is received. */
2650 for (;;)
2651 {
2652 /* Reset this here, in case a message arrives while we are going
2653 * through the already received messages. */
2654 reply_received = 0;
2655
2656 for (i = 0; i < REPLY_COUNT; ++i)
2657 {
2658 rep = REPLY_ITEM(i);
2659 if (rep->server == server
2660 && ((rep->expr_result != 0) == (expr_res != NULL)))
2661 {
2662 /* Save the values we've found for later */
2663 reply = rep->reply;
2664 if (expr_res != NULL)
2665 *expr_res = rep->expr_result == 1 ? 0 : -1;
2666
2667 if (remove)
2668 {
2669 /* Move the rest of the list down to fill the gap */
2670 mch_memmove(rep, rep + 1,
2671 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2672 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 }
2674
2675 /* Return the reply to the caller, who takes on responsibility
2676 * for freeing it if "remove" is TRUE. */
2677 return reply;
2678 }
2679 }
2680
2681 /* If we got here, we didn't find a reply. Return immediately if the
2682 * "wait" parameter isn't set. */
2683 if (!wait)
2684 break;
2685
2686 /* We need to wait for a reply. Enter a message loop until the
2687 * "reply_received" flag gets set. */
2688
2689 /* Loop until we receive a reply */
2690 while (reply_received == 0)
2691 {
2692 /* Wait for a SendMessage() call to us. This could be the reply
2693 * we are waiting for. Use a timeout of a second, to catch the
2694 * situation that the server died unexpectedly. */
2695 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2696
2697 /* If the server has died, give up */
2698 if (!IsWindow(server))
2699 return NULL;
2700
2701 serverProcessPendingMessages();
2702 }
2703 }
2704
2705 return NULL;
2706}
2707
2708/*
2709 * Process any messages in the Windows message queue.
2710 */
2711 void
2712serverProcessPendingMessages(void)
2713{
2714 MSG msg;
2715
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002716 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
2718 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002719 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721}
2722
2723#endif /* FEAT_CLIENTSERVER */
2724
2725#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2726 || defined(PROTO)
2727
2728struct charset_pair
2729{
2730 char *name;
2731 BYTE charset;
2732};
2733
2734static struct charset_pair
2735charset_pairs[] =
2736{
2737 {"ANSI", ANSI_CHARSET},
2738 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2739 {"DEFAULT", DEFAULT_CHARSET},
2740 {"HANGEUL", HANGEUL_CHARSET},
2741 {"OEM", OEM_CHARSET},
2742 {"SHIFTJIS", SHIFTJIS_CHARSET},
2743 {"SYMBOL", SYMBOL_CHARSET},
2744#ifdef WIN3264
2745 {"ARABIC", ARABIC_CHARSET},
2746 {"BALTIC", BALTIC_CHARSET},
2747 {"EASTEUROPE", EASTEUROPE_CHARSET},
2748 {"GB2312", GB2312_CHARSET},
2749 {"GREEK", GREEK_CHARSET},
2750 {"HEBREW", HEBREW_CHARSET},
2751 {"JOHAB", JOHAB_CHARSET},
2752 {"MAC", MAC_CHARSET},
2753 {"RUSSIAN", RUSSIAN_CHARSET},
2754 {"THAI", THAI_CHARSET},
2755 {"TURKISH", TURKISH_CHARSET},
2756# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
2757 && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
2758 {"VIETNAMESE", VIETNAMESE_CHARSET},
2759# endif
2760#endif
2761 {NULL, 0}
2762};
2763
2764/*
2765 * Convert a charset ID to a name.
2766 * Return NULL when not recognized.
2767 */
2768 char *
2769charset_id2name(int id)
2770{
2771 struct charset_pair *cp;
2772
2773 for (cp = charset_pairs; cp->name != NULL; ++cp)
2774 if ((BYTE)id == cp->charset)
2775 break;
2776 return cp->name;
2777}
2778
2779static const LOGFONT s_lfDefault =
2780{
2781 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2782 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2783 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2784 "Fixedsys" /* see _ReadVimIni */
2785};
2786
2787/* Initialise the "current height" to -12 (same as s_lfDefault) just
2788 * in case the user specifies a font in "guifont" with no size before a font
2789 * with an explicit size has been set. This defaults the size to this value
2790 * (-12 equates to roughly 9pt).
2791 */
2792int current_font_height = -12; /* also used in gui_w48.c */
2793
2794/* Convert a string representing a point size into pixels. The string should
2795 * be a positive decimal number, with an optional decimal point (eg, "12", or
2796 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2797 * character is stored in *end. The flag "vertical" says whether this
2798 * calculation is for a vertical (height) size or a horizontal (width) one.
2799 */
2800 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002801points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802{
2803 int pixels;
2804 int points = 0;
2805 int divisor = 0;
2806 HWND hwnd = (HWND)0;
2807 HDC hdc;
2808 HDC printer_dc = (HDC)pprinter_dc;
2809
2810 while (*str != NUL)
2811 {
2812 if (*str == '.' && divisor == 0)
2813 {
2814 /* Start keeping a divisor, for later */
2815 divisor = 1;
2816 }
2817 else
2818 {
2819 if (!VIM_ISDIGIT(*str))
2820 break;
2821
2822 points *= 10;
2823 points += *str - '0';
2824 divisor *= 10;
2825 }
2826 ++str;
2827 }
2828
2829 if (divisor == 0)
2830 divisor = 1;
2831
2832 if (printer_dc == NULL)
2833 {
2834 hwnd = GetDesktopWindow();
2835 hdc = GetWindowDC(hwnd);
2836 }
2837 else
2838 hdc = printer_dc;
2839
2840 pixels = MulDiv(points,
2841 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2842 72 * divisor);
2843
2844 if (printer_dc == NULL)
2845 ReleaseDC(hwnd, hdc);
2846
2847 *end = str;
2848 return pixels;
2849}
2850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 static int CALLBACK
2853font_enumproc(
2854 ENUMLOGFONT *elf,
2855 NEWTEXTMETRIC *ntm,
2856 int type,
2857 LPARAM lparam)
2858{
2859 /* Return value:
2860 * 0 = terminate now (monospace & ANSI)
2861 * 1 = continue, still no luck...
2862 * 2 = continue, but we have an acceptable LOGFONT
2863 * (monospace, not ANSI)
2864 * We use these values, as EnumFontFamilies returns 1 if the
2865 * callback function is never called. So, we check the return as
2866 * 0 = perfect, 2 = OK, 1 = no good...
2867 * It's not pretty, but it works!
2868 */
2869
2870 LOGFONT *lf = (LOGFONT *)(lparam);
2871
2872#ifndef FEAT_PROPORTIONAL_FONTS
2873 /* Ignore non-monospace fonts without further ado */
2874 if ((ntm->tmPitchAndFamily & 1) != 0)
2875 return 1;
2876#endif
2877
2878 /* Remember this LOGFONT as a "possible" */
2879 *lf = elf->elfLogFont;
2880
2881 /* Terminate the scan as soon as we find an ANSI font */
2882 if (lf->lfCharSet == ANSI_CHARSET
2883 || lf->lfCharSet == OEM_CHARSET
2884 || lf->lfCharSet == DEFAULT_CHARSET)
2885 return 0;
2886
2887 /* Continue the scan - we have a non-ANSI font */
2888 return 2;
2889}
2890
2891 static int
2892init_logfont(LOGFONT *lf)
2893{
2894 int n;
2895 HWND hwnd = GetDesktopWindow();
2896 HDC hdc = GetWindowDC(hwnd);
2897
2898 n = EnumFontFamilies(hdc,
2899 (LPCSTR)lf->lfFaceName,
2900 (FONTENUMPROC)font_enumproc,
2901 (LPARAM)lf);
2902
2903 ReleaseDC(hwnd, hdc);
2904
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002905 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 if (n == 1)
2907 return FAIL;
2908
2909 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2910 * font - get_logfont() sets bold, italic, etc based on the user's
2911 * input.
2912 */
2913 lf->lfHeight = current_font_height;
2914 lf->lfWidth = 0;
2915 lf->lfItalic = FALSE;
2916 lf->lfUnderline = FALSE;
2917 lf->lfStrikeOut = FALSE;
2918 lf->lfWeight = FW_NORMAL;
2919
2920 /* Return success */
2921 return OK;
2922}
2923
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002924/*
2925 * Get font info from "name" into logfont "lf".
2926 * Return OK for a valid name, FAIL otherwise.
2927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 int
2929get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002930 LOGFONT *lf,
2931 char_u *name,
2932 HDC printer_dc,
2933 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
2935 char_u *p;
2936 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002937 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002939#ifdef FEAT_MBYTE
2940 char_u *acpname = NULL;
2941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
2943 *lf = s_lfDefault;
2944 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002945 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002947#ifdef FEAT_MBYTE
2948 /* Convert 'name' from 'encoding' to the current codepage, because
2949 * lf->lfFaceName uses the current codepage.
2950 * TODO: Use Wide APIs instead of ANSI APIs. */
2951 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2952 {
2953 int len;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002954 enc_to_acp(name, (int)strlen((char *)name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002955 name = acpname;
2956 }
2957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 if (STRCMP(name, "*") == 0)
2959 {
2960#if defined(FEAT_GUI_W32)
2961 CHOOSEFONT cf;
2962 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002963 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 cf.lStructSize = sizeof(cf);
2965 cf.hwndOwner = s_hwnd;
2966 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2967 if (lastlf != NULL)
2968 *lf = *lastlf;
2969 cf.lpLogFont = lf;
2970 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2971 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002972 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002974 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
2976
2977 /*
2978 * Split name up, it could be <name>:h<height>:w<width> etc.
2979 */
2980 for (p = name; *p && *p != ':'; p++)
2981 {
2982 if (p - name + 1 > LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002983 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 lf->lfFaceName[p - name] = *p;
2985 }
2986 if (p != name)
2987 lf->lfFaceName[p - name] = NUL;
2988
2989 /* First set defaults */
2990 lf->lfHeight = -12;
2991 lf->lfWidth = 0;
2992 lf->lfWeight = FW_NORMAL;
2993 lf->lfItalic = FALSE;
2994 lf->lfUnderline = FALSE;
2995 lf->lfStrikeOut = FALSE;
2996
2997 /*
2998 * If the font can't be found, try replacing '_' by ' '.
2999 */
3000 if (init_logfont(lf) == FAIL)
3001 {
3002 int did_replace = FALSE;
3003
3004 for (i = 0; lf->lfFaceName[i]; ++i)
3005 if (lf->lfFaceName[i] == '_')
3006 {
3007 lf->lfFaceName[i] = ' ';
3008 did_replace = TRUE;
3009 }
3010 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003011 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
3013
3014 while (*p == ':')
3015 p++;
3016
3017 /* Set the values found after ':' */
3018 while (*p)
3019 {
3020 switch (*p++)
3021 {
3022 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003023 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 break;
3025 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003026 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 break;
3028 case 'b':
3029#ifndef MSWIN16_FASTTEXT
3030 lf->lfWeight = FW_BOLD;
3031#endif
3032 break;
3033 case 'i':
3034#ifndef MSWIN16_FASTTEXT
3035 lf->lfItalic = TRUE;
3036#endif
3037 break;
3038 case 'u':
3039 lf->lfUnderline = TRUE;
3040 break;
3041 case 's':
3042 lf->lfStrikeOut = TRUE;
3043 break;
3044 case 'c':
3045 {
3046 struct charset_pair *cp;
3047
3048 for (cp = charset_pairs; cp->name != NULL; ++cp)
3049 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
3050 {
3051 lf->lfCharSet = cp->charset;
3052 p += strlen(cp->name);
3053 break;
3054 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003055 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003057 vim_snprintf((char *)IObuff, IOSIZE,
3058 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 EMSG(IObuff);
3060 break;
3061 }
3062 break;
3063 }
3064 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003065 if (verbose)
3066 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003067 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003068 _("E245: Illegal char '%c' in font name \"%s\""),
3069 p[-1], name);
3070 EMSG(IObuff);
3071 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003072 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 }
3074 while (*p == ':')
3075 p++;
3076 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003077 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003081 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 {
3083 vim_free(lastlf);
3084 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3085 if (lastlf != NULL)
3086 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3087 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003088#ifdef FEAT_MBYTE
3089 vim_free(acpname);
3090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003092 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093}
3094
3095#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003096
3097#if defined(FEAT_CHANNEL) || defined(PROTO)
3098/*
3099 * Initialize the Winsock dll.
3100 */
3101 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003102channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003103{
3104 WSADATA wsaData;
3105 int wsaerr;
3106
3107 if (WSInitialized)
3108 return;
3109
3110 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3111 if (wsaerr == 0)
3112 WSInitialized = TRUE;
3113}
3114#endif