blob: b7e3b5e5ec97e67fd4a42381832f1110265ba6ce [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
276 CharUpperBuff(toupper_tab, 256);
277 CharLowerBuff(tolower_tab, 256);
278#else
279 AnsiUpperBuff(toupper_tab, 256);
280 AnsiLowerBuff(tolower_tab, 256);
281#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 Moolenaar071d4272004-06-13 20:20:40 +0000330 SetConsoleTitle(title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332# endif
333}
334
335
336/*
337 * Restore the window/icon title.
338 * which is one of:
339 * 1: Just restore title
340 * 2: Just restore icon (which we don't have)
341 * 3: Restore title and icon (which we don't have)
342 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000343/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 void
345mch_restore_title(
346 int which)
347{
348#ifndef FEAT_GUI_MSWIN
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 {
431 if (_fullpath(buf, fname, len - 1) == NULL)
432 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000433 /* failed, use relative path name */
434 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000435 }
436 else
437 nResult = OK;
438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440
441#ifdef USE_FNAME_CASE
442 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000443#else
444 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#endif
446
447 return nResult;
448}
449
450
451/*
452 * Return TRUE if "fname" does not depend on the current directory.
453 */
454 int
455mch_isFullName(char_u *fname)
456{
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 Moolenaard2a203b2013-08-30 16:51:18 +0200472 if (mch_FullName(fname, szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 return FALSE;
474
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000475 return pathcmp(fname, 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. */
622 char buf[_MAX_PATH * 3 + 1];
623#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 char buf[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 char *p;
627
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200628 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 p = buf + strlen(buf);
630 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 '\\'. */
640 p = vim_strpbrk(buf + 2, "\\/");
641 if (p != NULL)
642 {
643 p = vim_strpbrk(p + 1, "\\/");
644 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 Moolenaar2ee95f72013-09-25 19:13:38 +0200671 return stat_symlink_aware(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 Moolenaar36f692d2008-11-20 16:10:17 +0000823 WCHAR *p = enc_to_utf16(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)
856 msg(_("'columns' is not 80, cannot execute external commands"));
857 return FALSE;
858#endif
859}
860
861#ifdef FEAT_GUI_MSWIN
862/*
863 * return non-zero if a character is available
864 */
865 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100866mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867{
868 /* never used */
869 return TRUE;
870}
871#endif
872
873
874/*
875 * set screen mode, always fails.
876 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000877/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 int
879mch_screenmode(
880 char_u *arg)
881{
882 EMSG(_(e_screenmode));
883 return FAIL;
884}
885
886
887#if defined(FEAT_LIBCALL) || defined(PROTO)
888/*
889 * Call a DLL routine which takes either a string or int param
890 * and returns an allocated string.
891 * Return OK if it worked, FAIL if not.
892 */
893# ifdef WIN3264
894typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
895typedef LPTSTR (*MYINTPROCSTR)(int);
896typedef int (*MYSTRPROCINT)(LPTSTR);
897typedef int (*MYINTPROCINT)(int);
898# else
899typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
900typedef LPSTR (*MYINTPROCSTR)(int);
901typedef int (*MYSTRPROCINT)(LPSTR);
902typedef int (*MYINTPROCINT)(int);
903# endif
904
905# ifndef WIN16
906/*
907 * Check if a pointer points to a valid NUL terminated string.
908 * Return the length of the string, including terminating NUL.
909 * Returns 0 for an invalid pointer, 1 for an empty string.
910 */
911 static size_t
912check_str_len(char_u *str)
913{
914 SYSTEM_INFO si;
915 MEMORY_BASIC_INFORMATION mbi;
916 size_t length = 0;
917 size_t i;
918 const char *p;
919
920 /* get page size */
921 GetSystemInfo(&si);
922
923 /* get memory information */
924 if (VirtualQuery(str, &mbi, sizeof(mbi)))
925 {
926 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000927 long_u dwStr = (long_u)str;
928 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
930 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000931 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000934 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Bram Moolenaar442b4222010-05-24 21:34:22 +0200936 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 p += pageLength, pageLength = si.dwPageSize)
938 for (i = 0; i < pageLength; ++i, ++length)
939 if (p[i] == NUL)
940 return length + 1;
941 }
942
943 return 0;
944}
945# endif
946
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200947/*
948 * Passed to do_in_runtimepath() to load a vim.ico file.
949 */
950 static void
951mch_icon_load_cb(char_u *fname, void *cookie)
952{
953 HANDLE *h = (HANDLE *)cookie;
954
955 *h = LoadImage(NULL,
956 fname,
957 IMAGE_ICON,
958 64,
959 64,
960 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
961}
962
963/*
964 * Try loading an icon file from 'runtimepath'.
965 */
966 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100967mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200968{
969 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
970 FALSE, mch_icon_load_cb, iconp);
971}
972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 int
974mch_libcall(
975 char_u *libname,
976 char_u *funcname,
977 char_u *argstring, /* NULL when using a argint */
978 int argint,
979 char_u **string_result,/* NULL when using number_result */
980 int *number_result)
981{
982 HINSTANCE hinstLib;
983 MYSTRPROCSTR ProcAdd;
984 MYINTPROCSTR ProcAddI;
985 char_u *retval_str = NULL;
986 int retval_int = 0;
987 size_t len;
988
989 BOOL fRunTimeLinkSuccess = FALSE;
990
991 // Get a handle to the DLL module.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200992# ifdef WIN16
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 hinstLib = LoadLibrary(libname);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200994# else
995 hinstLib = vimLoadLib(libname);
996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
998 // If the handle is valid, try to get the function address.
999 if (hinstLib != NULL)
1000 {
1001#ifdef HAVE_TRY_EXCEPT
1002 __try
1003 {
1004#endif
1005 if (argstring != NULL)
1006 {
1007 /* Call with string argument */
1008 ProcAdd = (MYSTRPROCSTR) GetProcAddress(hinstLib, funcname);
1009 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
1010 {
1011 if (string_result == NULL)
1012 retval_int = ((MYSTRPROCINT)ProcAdd)(argstring);
1013 else
1014 retval_str = (ProcAdd)(argstring);
1015 }
1016 }
1017 else
1018 {
1019 /* Call with number argument */
1020 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, funcname);
1021 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
1022 {
1023 if (string_result == NULL)
1024 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
1025 else
1026 retval_str = (ProcAddI)(argint);
1027 }
1028 }
1029
1030 // Save the string before we free the library.
1031 // Assume that a "1" result is an illegal pointer.
1032 if (string_result == NULL)
1033 *number_result = retval_int;
1034 else if (retval_str != NULL
1035# ifdef WIN16
1036 && retval_str != (char_u *)1
1037 && retval_str != (char_u *)-1
1038 && !IsBadStringPtr(retval_str, INT_MAX)
1039 && (len = strlen(retval_str) + 1) > 0
1040# else
1041 && (len = check_str_len(retval_str)) > 0
1042# endif
1043 )
1044 {
1045 *string_result = lalloc((long_u)len, TRUE);
1046 if (*string_result != NULL)
1047 mch_memmove(*string_result, retval_str, len);
1048 }
1049
1050#ifdef HAVE_TRY_EXCEPT
1051 }
1052 __except(EXCEPTION_EXECUTE_HANDLER)
1053 {
1054 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
1055 RESETSTKOFLW();
1056 fRunTimeLinkSuccess = 0;
1057 }
1058#endif
1059
1060 // Free the DLL module.
1061 (void)FreeLibrary(hinstLib);
1062 }
1063
1064 if (!fRunTimeLinkSuccess)
1065 {
1066 EMSG2(_(e_libcall), funcname);
1067 return FAIL;
1068 }
1069
1070 return OK;
1071}
1072#endif
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074/*
1075 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1076 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001077/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 void
1079DumpPutS(
1080 const char *psz)
1081{
1082# ifdef MCH_WRITE_DUMP
1083 if (fdDump)
1084 {
1085 fputs(psz, fdDump);
1086 if (psz[strlen(psz) - 1] != '\n')
1087 fputc('\n', fdDump);
1088 fflush(fdDump);
1089 }
1090# endif
1091}
1092
1093#ifdef _DEBUG
1094
1095void __cdecl
1096Trace(
1097 char *pszFormat,
1098 ...)
1099{
1100 CHAR szBuff[2048];
1101 va_list args;
1102
1103 va_start(args, pszFormat);
1104 vsprintf(szBuff, pszFormat, args);
1105 va_end(args);
1106
1107 OutputDebugString(szBuff);
1108}
1109
1110#endif //_DEBUG
1111
Bram Moolenaar843ee412004-06-30 16:16:41 +00001112#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113# if defined(FEAT_TITLE) && defined(WIN3264)
1114extern HWND g_hWnd; /* This is in os_win32.c. */
1115# endif
1116
1117/*
1118 * Showing the printer dialog is tricky since we have no GUI
1119 * window to parent it. The following routines are needed to
1120 * get the window parenting and Z-order to work properly.
1121 */
1122 static void
1123GetConsoleHwnd(void)
1124{
1125# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1126
1127 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1128 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1129
1130 /* Skip if it's already set. */
1131 if (s_hwnd != 0)
1132 return;
1133
1134# if defined(FEAT_TITLE) && defined(WIN3264)
1135 /* Window handle may have been found by init code (Windows NT only) */
1136 if (g_hWnd != 0)
1137 {
1138 s_hwnd = g_hWnd;
1139 return;
1140 }
1141# endif
1142
1143 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1144
1145 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1146 pszOldWindowTitle,
1147 GetTickCount(),
1148 GetCurrentProcessId());
1149 SetConsoleTitle(pszNewWindowTitle);
1150 Sleep(40);
1151 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1152
1153 SetConsoleTitle(pszOldWindowTitle);
1154}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001155
1156/*
1157 * Console implementation of ":winpos".
1158 */
1159 int
1160mch_get_winpos(int *x, int *y)
1161{
1162 RECT rect;
1163
1164 GetConsoleHwnd();
1165 GetWindowRect(s_hwnd, &rect);
1166 *x = rect.left;
1167 *y = rect.top;
1168 return OK;
1169}
1170
1171/*
1172 * Console implementation of ":winpos x y".
1173 */
1174 void
1175mch_set_winpos(int x, int y)
1176{
1177 GetConsoleHwnd();
1178 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1179 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1180}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#endif
1182
1183#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1184
1185# ifdef WIN16
1186# define TEXT(a) a
1187# endif
1188/*=================================================================
1189 * Win32 printer stuff
1190 */
1191
1192static HFONT prt_font_handles[2][2][2];
1193static PRINTDLG prt_dlg;
1194static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1195static TEXTMETRIC prt_tm;
1196static int prt_line_height;
1197static int prt_number_width;
1198static int prt_left_margin;
1199static int prt_right_margin;
1200static int prt_top_margin;
1201static char_u szAppName[] = TEXT("VIM");
1202static HWND hDlgPrint;
1203static int *bUserAbort = NULL;
1204static char_u *prt_name = NULL;
1205
1206/* Defines which are also in vim.rc. */
1207#define IDC_BOX1 400
1208#define IDC_PRINTTEXT1 401
1209#define IDC_PRINTTEXT2 402
1210#define IDC_PROGRESS 403
1211
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001212#if !defined(FEAT_MBYTE) || defined(WIN16)
1213# define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s)
1214#else
1215 static BOOL
1216vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1217{
1218 WCHAR *wp = NULL;
1219 BOOL ret;
1220
1221 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1222 {
1223 wp = enc_to_utf16(s, NULL);
1224 }
1225 if (wp != NULL)
1226 {
1227 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1228 vim_free(wp);
1229 return ret;
1230 }
1231 return SetDlgItemText(hDlg, nIDDlgItem, s);
1232}
1233#endif
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235/*
1236 * Convert BGR to RGB for Windows GDI calls
1237 */
1238 static COLORREF
1239swap_me(COLORREF colorref)
1240{
1241 int temp;
1242 char *ptr = (char *)&colorref;
1243
1244 temp = *(ptr);
1245 *(ptr ) = *(ptr + 2);
1246 *(ptr + 2) = temp;
1247 return colorref;
1248}
1249
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001250/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001251#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001252# define PDP_RETVAL BOOL
1253#else
1254# define PDP_RETVAL INT_PTR
1255#endif
1256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001257/*ARGSUSED*/
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001258 static PDP_RETVAL CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1260{
1261#ifdef FEAT_GETTEXT
1262 NONCLIENTMETRICS nm;
1263 static HFONT hfont;
1264#endif
1265
1266 switch (message)
1267 {
1268 case WM_INITDIALOG:
1269#ifdef FEAT_GETTEXT
1270 nm.cbSize = sizeof(NONCLIENTMETRICS);
1271 if (SystemParametersInfo(
1272 SPI_GETNONCLIENTMETRICS,
1273 sizeof(NONCLIENTMETRICS),
1274 &nm,
1275 0))
1276 {
1277 char buff[MAX_PATH];
1278 int i;
1279
1280 /* Translate the dialog texts */
1281 hfont = CreateFontIndirect(&nm.lfMessageFont);
1282 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1283 {
1284 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1285 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001286 vimSetDlgItemText(hDlg,i, _(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 SendDlgItemMessage(hDlg, IDCANCEL,
1289 WM_SETFONT, (WPARAM)hfont, 1);
1290 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001291 vimSetDlgItemText(hDlg,IDCANCEL, _(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293#endif
1294 SetWindowText(hDlg, szAppName);
1295 if (prt_name != NULL)
1296 {
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001297 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (LPSTR)prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 vim_free(prt_name);
1299 prt_name = NULL;
1300 }
1301 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1302#ifndef FEAT_GUI
1303 BringWindowToTop(s_hwnd);
1304#endif
1305 return TRUE;
1306
1307 case WM_COMMAND:
1308 *bUserAbort = TRUE;
1309 EnableWindow(GetParent(hDlg), TRUE);
1310 DestroyWindow(hDlg);
1311 hDlgPrint = NULL;
1312#ifdef FEAT_GETTEXT
1313 DeleteObject(hfont);
1314#endif
1315 return TRUE;
1316 }
1317 return FALSE;
1318}
1319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001320/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 static BOOL CALLBACK
1322AbortProc(HDC hdcPrn, int iCode)
1323{
1324 MSG msg;
1325
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001326 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001328 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
1330 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001331 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 }
1333 }
1334 return !*bUserAbort;
1335}
1336
1337#ifndef FEAT_GUI
1338
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001339 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340PrintHookProc(
1341 HWND hDlg, // handle to dialog box
1342 UINT uiMsg, // message identifier
1343 WPARAM wParam, // message parameter
1344 LPARAM lParam // message parameter
1345 )
1346{
1347 HWND hwndOwner;
1348 RECT rc, rcDlg, rcOwner;
1349 PRINTDLG *pPD;
1350
1351 if (uiMsg == WM_INITDIALOG)
1352 {
1353 // Get the owner window and dialog box rectangles.
1354 if ((hwndOwner = GetParent(hDlg)) == NULL)
1355 hwndOwner = GetDesktopWindow();
1356
1357 GetWindowRect(hwndOwner, &rcOwner);
1358 GetWindowRect(hDlg, &rcDlg);
1359 CopyRect(&rc, &rcOwner);
1360
1361 // Offset the owner and dialog box rectangles so that
1362 // right and bottom values represent the width and
1363 // height, and then offset the owner again to discard
1364 // space taken up by the dialog box.
1365
1366 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1367 OffsetRect(&rc, -rc.left, -rc.top);
1368 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1369
1370 // The new position is the sum of half the remaining
1371 // space and the owner's original position.
1372
1373 SetWindowPos(hDlg,
1374 HWND_TOP,
1375 rcOwner.left + (rc.right / 2),
1376 rcOwner.top + (rc.bottom / 2),
1377 0, 0, // ignores size arguments
1378 SWP_NOSIZE);
1379
1380 /* tackle the printdlg copiesctrl problem */
1381 pPD = (PRINTDLG *)lParam;
1382 pPD->nCopies = (WORD)pPD->lCustData;
1383 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1384 /* Bring the window to top */
1385 BringWindowToTop(GetParent(hDlg));
1386 SetForegroundWindow(hDlg);
1387 }
1388
1389 return FALSE;
1390}
1391#endif
1392
1393 void
1394mch_print_cleanup(void)
1395{
1396 int pifItalic;
1397 int pifBold;
1398 int pifUnderline;
1399
1400 for (pifBold = 0; pifBold <= 1; pifBold++)
1401 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1402 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1403 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1404
1405 if (prt_dlg.hDC != NULL)
1406 DeleteDC(prt_dlg.hDC);
1407 if (!*bUserAbort)
1408 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1409}
1410
1411 static int
1412to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1413{
1414 int ret = 0;
1415 int u;
1416 int nr;
1417
1418 u = prt_get_unit(idx);
1419 if (u == PRT_UNIT_NONE)
1420 {
1421 u = PRT_UNIT_PERC;
1422 nr = def_number;
1423 }
1424 else
1425 nr = printer_opts[idx].number;
1426
1427 switch (u)
1428 {
1429 case PRT_UNIT_PERC:
1430 ret = (physsize * nr) / 100;
1431 break;
1432 case PRT_UNIT_INCH:
1433 ret = (nr * dpi);
1434 break;
1435 case PRT_UNIT_MM:
1436 ret = (nr * 10 * dpi) / 254;
1437 break;
1438 case PRT_UNIT_POINT:
1439 ret = (nr * 10 * dpi) / 720;
1440 break;
1441 }
1442
1443 if (ret < offset)
1444 return 0;
1445 else
1446 return ret - offset;
1447}
1448
1449 static int
1450prt_get_cpl(void)
1451{
1452 int hr;
1453 int phyw;
1454 int dvoff;
1455 int rev_offset;
1456 int dpi;
1457#ifdef WIN16
1458 POINT pagesize;
1459#endif
1460
1461 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1462 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1463
1464 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
1465#ifdef WIN16
1466 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1467 phyw = pagesize.x;
1468 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1469 dvoff = pagesize.x;
1470#else
1471 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1472 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
1473#endif
1474 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1475
1476 rev_offset = phyw - (dvoff + hr);
1477
1478 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1479 if (prt_use_number())
1480 {
1481 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1482 prt_left_margin += prt_number_width;
1483 }
1484 else
1485 prt_number_width = 0;
1486
1487 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1488 rev_offset, 5);
1489
1490 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1491}
1492
1493 static int
1494prt_get_lpp(void)
1495{
1496 int vr;
1497 int phyw;
1498 int dvoff;
1499 int rev_offset;
1500 int bottom_margin;
1501 int dpi;
1502#ifdef WIN16
1503 POINT pagesize;
1504#endif
1505
1506 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
1507#ifdef WIN16
1508 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1509 phyw = pagesize.y;
1510 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1511 dvoff = pagesize.y;
1512#else
1513 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1514 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
1515#endif
1516 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1517
1518 rev_offset = phyw - (dvoff + vr);
1519
1520 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1521
1522 /* adjust top margin if there is a header */
1523 prt_top_margin += prt_line_height * prt_header_height();
1524
1525 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1526 rev_offset, 5);
1527
1528 return (bottom_margin - prt_top_margin) / prt_line_height;
1529}
1530
1531 int
1532mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1533{
1534 static HGLOBAL stored_dm = NULL;
1535 static HGLOBAL stored_devn = NULL;
1536 static int stored_nCopies = 1;
1537 static int stored_nFlags = 0;
1538
1539 LOGFONT fLogFont;
1540 int pifItalic;
1541 int pifBold;
1542 int pifUnderline;
1543
1544 DEVMODE *mem;
1545 DEVNAMES *devname;
1546 int i;
1547
1548 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001549 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 prt_dlg.lStructSize = sizeof(PRINTDLG);
1551#ifndef FEAT_GUI
1552 GetConsoleHwnd(); /* get value of s_hwnd */
1553#endif
1554 prt_dlg.hwndOwner = s_hwnd;
1555 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1556 if (!forceit)
1557 {
1558 prt_dlg.hDevMode = stored_dm;
1559 prt_dlg.hDevNames = stored_devn;
1560 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1561#ifndef FEAT_GUI
1562 /*
1563 * Use hook to prevent console window being sent to back
1564 */
1565 prt_dlg.lpfnPrintHook = PrintHookProc;
1566 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1567#endif
1568 prt_dlg.Flags |= stored_nFlags;
1569 }
1570
1571 /*
1572 * If bang present, return default printer setup with no dialog
1573 * never show dialog if we are running over telnet
1574 */
1575 if (forceit
1576#ifndef FEAT_GUI
1577 || !term_console
1578#endif
1579 )
1580 {
1581 prt_dlg.Flags |= PD_RETURNDEFAULT;
1582#ifdef WIN3264
1583 /*
1584 * MSDN suggests setting the first parameter to WINSPOOL for
1585 * NT, but NULL appears to work just as well.
1586 */
1587 if (*p_pdev != NUL)
1588 prt_dlg.hDC = CreateDC(NULL, p_pdev, NULL, NULL);
1589 else
1590#endif
1591 {
1592 prt_dlg.Flags |= PD_RETURNDEFAULT;
1593 if (PrintDlg(&prt_dlg) == 0)
1594 goto init_fail_dlg;
1595 }
1596 }
1597 else if (PrintDlg(&prt_dlg) == 0)
1598 goto init_fail_dlg;
1599 else
1600 {
1601 /*
1602 * keep the previous driver context
1603 */
1604 stored_dm = prt_dlg.hDevMode;
1605 stored_devn = prt_dlg.hDevNames;
1606 stored_nFlags = prt_dlg.Flags;
1607 stored_nCopies = prt_dlg.nCopies;
1608 }
1609
1610 if (prt_dlg.hDC == NULL)
1611 {
1612 EMSG(_("E237: Printer selection failed"));
1613 mch_print_cleanup();
1614 return FALSE;
1615 }
1616
1617 /* Not all printer drivers report the support of color (or grey) in the
1618 * same way. Let's set has_color if there appears to be some way to print
1619 * more than B&W. */
1620 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1621 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1622 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1623 || i > 2 || i == -1);
1624
1625 /* Ensure all font styles are baseline aligned */
1626 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1627
1628 /*
1629 * On some windows systems the nCopies parameter is not
1630 * passed back correctly. It must be retrieved from the
1631 * hDevMode struct.
1632 */
1633 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1634 if (mem != NULL)
1635 {
1636#ifdef WIN3264
1637 if (mem->dmCopies != 1)
1638 stored_nCopies = mem->dmCopies;
1639#endif
1640 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1641 psettings->duplex = TRUE;
1642 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1643 psettings->has_color = TRUE;
1644 }
1645 GlobalUnlock(prt_dlg.hDevMode);
1646
1647 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1648 if (devname != 0)
1649 {
1650 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1651 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
1652 char_u *text = _("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001653#ifdef FEAT_MBYTE
1654 char_u *printer_name_orig = printer_name;
1655 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaarf191d552014-10-09 17:05:56 +02001657 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1658 {
1659 char_u *to_free = NULL;
1660 int maxlen;
1661
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001662 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1663 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001664 if (to_free != NULL)
1665 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001666 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001667 if (to_free != NULL)
1668 port_name = to_free;
1669 }
1670#endif
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001671 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1672 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (prt_name != NULL)
1674 wsprintf(prt_name, text, printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001675#ifdef FEAT_MBYTE
1676 if (printer_name != printer_name_orig)
1677 vim_free(printer_name);
1678 if (port_name != port_name_orig)
1679 vim_free(port_name);
1680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
1682 GlobalUnlock(prt_dlg.hDevNames);
1683
1684 /*
1685 * Initialise the font according to 'printfont'
1686 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001687 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001688 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
1690 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1691 mch_print_cleanup();
1692 return FALSE;
1693 }
1694
1695 for (pifBold = 0; pifBold <= 1; pifBold++)
1696 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1697 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1698 {
1699 fLogFont.lfWeight = boldface[pifBold];
1700 fLogFont.lfItalic = pifItalic;
1701 fLogFont.lfUnderline = pifUnderline;
1702 prt_font_handles[pifBold][pifItalic][pifUnderline]
1703 = CreateFontIndirect(&fLogFont);
1704 }
1705
1706 SetBkMode(prt_dlg.hDC, OPAQUE);
1707 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1708
1709 /*
1710 * Fill in the settings struct
1711 */
1712 psettings->chars_per_line = prt_get_cpl();
1713 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001714 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1715 {
1716 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1717 ? prt_dlg.nCopies : 1;
1718 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1719 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001721 if (psettings->n_collated_copies == 0)
1722 psettings->n_collated_copies = 1;
1723
1724 if (psettings->n_uncollated_copies == 0)
1725 psettings->n_uncollated_copies = 1;
1726 } else {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731 psettings->jobname = jobname;
1732
1733 return TRUE;
1734
1735init_fail_dlg:
1736 {
1737 DWORD err = CommDlgExtendedError();
1738
1739 if (err)
1740 {
1741#ifdef WIN16
1742 char buf[20];
1743
1744 sprintf(buf, "%ld", err);
1745 EMSG2(_("E238: Print error: %s"), buf);
1746#else
1747 char_u *buf;
1748
1749 /* I suspect FormatMessage() doesn't work for values returned by
1750 * CommDlgExtendedError(). What does? */
1751 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1752 FORMAT_MESSAGE_FROM_SYSTEM |
1753 FORMAT_MESSAGE_IGNORE_INSERTS,
1754 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1755 EMSG2(_("E238: Print error: %s"),
1756 buf == NULL ? (char_u *)_("Unknown") : buf);
1757 LocalFree((LPVOID)(buf));
1758#endif
1759 }
1760 else
1761 msg_clr_eos(); /* Maybe canceled */
1762
1763 mch_print_cleanup();
1764 return FALSE;
1765 }
1766}
1767
1768
1769 int
1770mch_print_begin(prt_settings_T *psettings)
1771{
1772 int ret;
1773 static DOCINFO di;
1774 char szBuffer[300];
1775
1776 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1777 prt_dlg.hwndOwner, PrintDlgProc);
1778#ifdef WIN16
1779 Escape(prt_dlg.hDC, SETABORTPROC, 0, (LPSTR)AbortProc, NULL);
1780#else
1781 SetAbortProc(prt_dlg.hDC, AbortProc);
1782#endif
1783 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001784 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (LPSTR)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001786 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 di.cbSize = sizeof(DOCINFO);
1788 di.lpszDocName = psettings->jobname;
1789 ret = StartDoc(prt_dlg.hDC, &di);
1790
1791#ifdef FEAT_GUI
1792 /* Give focus back to main window (when using MDI). */
1793 SetFocus(s_hwnd);
1794#endif
1795
1796 return (ret > 0);
1797}
1798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001799/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 void
1801mch_print_end(prt_settings_T *psettings)
1802{
1803 EndDoc(prt_dlg.hDC);
1804 if (!*bUserAbort)
1805 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1806}
1807
1808 int
1809mch_print_end_page(void)
1810{
1811 return (EndPage(prt_dlg.hDC) > 0);
1812}
1813
1814 int
1815mch_print_begin_page(char_u *msg)
1816{
1817 if (msg != NULL)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001818 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, (LPSTR)msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 return (StartPage(prt_dlg.hDC) > 0);
1820}
1821
1822 int
1823mch_print_blank_page(void)
1824{
1825 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1826}
1827
1828static int prt_pos_x = 0;
1829static int prt_pos_y = 0;
1830
1831 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001832mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833{
1834 if (margin)
1835 prt_pos_x = -prt_number_width;
1836 else
1837 prt_pos_x = 0;
1838 prt_pos_y = page_line * prt_line_height
1839 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1840}
1841
1842 int
1843mch_print_text_out(char_u *p, int len)
1844{
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001845#if defined(FEAT_PROPORTIONAL_FONTS) || (defined(FEAT_MBYTE) && !defined(WIN16))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 SIZE sz;
1847#endif
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001848#if defined(FEAT_MBYTE) && !defined(WIN16)
1849 WCHAR *wp = NULL;
1850 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001852 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1853 {
1854 wp = enc_to_utf16(p, &wlen);
1855 }
1856 if (wp != NULL)
1857 {
1858 int ret = FALSE;
1859
1860 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1861 prt_pos_y + prt_top_margin, wp, wlen);
1862 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1863 vim_free(wp);
1864 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1865 /* This is wrong when printing spaces for a TAB. */
1866 if (p[len] != NUL)
1867 {
1868 wlen = MB_PTR2LEN(p + len);
1869 wp = enc_to_utf16(p + len, &wlen);
1870 if (wp != NULL)
1871 {
1872 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1873 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1874 vim_free(wp);
1875 }
1876 }
1877 return ret;
1878 }
1879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1881 prt_pos_y + prt_top_margin, p, len);
1882#ifndef FEAT_PROPORTIONAL_FONTS
1883 prt_pos_x += len * prt_tm.tmAveCharWidth;
1884 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1885 + prt_tm.tmOverhang > prt_right_margin);
1886#else
1887# ifdef WIN16
1888 GetTextExtentPoint(prt_dlg.hDC, p, len, &sz);
1889# else
1890 GetTextExtentPoint32(prt_dlg.hDC, p, len, &sz);
1891# endif
1892 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1893 /* This is wrong when printing spaces for a TAB. */
1894 if (p[len] == NUL)
1895 return FALSE;
1896# ifdef WIN16
1897 GetTextExtentPoint(prt_dlg.hDC, p + len, 1, &sz);
1898# else
1899 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
1900# endif
1901 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1902#endif
1903}
1904
1905 void
1906mch_print_set_font(int iBold, int iItalic, int iUnderline)
1907{
1908 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1909}
1910
1911 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001912mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001914 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1915 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 /*
1917 * With a white background we can draw characters transparent, which is
1918 * good for italic characters that overlap to the next char cell.
1919 */
1920 if (bgcol == 0xffffffUL)
1921 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1922 else
1923 SetBkMode(prt_dlg.hDC, OPAQUE);
1924}
1925
1926 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001927mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001929 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1930 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931}
1932
1933#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1934
Bram Moolenaar58d98232005-07-23 22:25:46 +00001935
1936
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001938# ifndef PROTO
1939# include <shlobj.h>
1940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941
1942/*
1943 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1944 * to and return that name in allocated memory.
1945 * Otherwise NULL is returned.
1946 */
1947 char_u *
1948mch_resolve_shortcut(char_u *fname)
1949{
1950 HRESULT hr;
1951 IShellLink *psl = NULL;
1952 IPersistFile *ppf = NULL;
1953 OLECHAR wsz[MAX_PATH];
1954 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001955 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 char_u *rfname = NULL;
1957 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001958# ifdef FEAT_MBYTE
1959 IShellLinkW *pslw = NULL;
1960 WIN32_FIND_DATAW ffdw; // we get those free of charge
1961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963 /* Check if the file name ends in ".lnk". Avoid calling
1964 * CoCreateInstance(), it's quite slow. */
1965 if (fname == NULL)
1966 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001967 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1969 return rfname;
1970
1971 CoInitialize(NULL);
1972
Bram Moolenaar604729e2013-08-30 16:44:19 +02001973# ifdef FEAT_MBYTE
1974 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1975 {
1976 // create a link manager object and request its interface
1977 hr = CoCreateInstance(
1978 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1979 &IID_IShellLinkW, (void**)&pslw);
1980 if (hr == S_OK)
1981 {
1982 WCHAR *p = enc_to_utf16(fname, NULL);
1983
1984 if (p != NULL)
1985 {
1986 // Get a pointer to the IPersistFile interface.
1987 hr = pslw->lpVtbl->QueryInterface(
1988 pslw, &IID_IPersistFile, (void**)&ppf);
1989 if (hr != S_OK)
1990 goto shortcut_errorw;
1991
1992 // "load" the name and resolve the link
1993 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1994 if (hr != S_OK)
1995 goto shortcut_errorw;
1996# if 0 // This makes Vim wait a long time if the target does not exist.
1997 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1998 if (hr != S_OK)
1999 goto shortcut_errorw;
2000# endif
2001
2002 // Get the path to the link target.
2003 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
2004 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
2005 if (hr == S_OK && wsz[0] != NUL)
2006 rfname = utf16_to_enc(wsz, NULL);
2007
2008shortcut_errorw:
2009 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01002010 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02002011 }
2012 }
2013 /* Retry with non-wide function (for Windows 98). */
2014 }
2015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 // create a link manager object and request its interface
2017 hr = CoCreateInstance(
2018 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2019 &IID_IShellLink, (void**)&psl);
2020 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002021 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022
2023 // Get a pointer to the IPersistFile interface.
2024 hr = psl->lpVtbl->QueryInterface(
2025 psl, &IID_IPersistFile, (void**)&ppf);
2026 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002027 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 // full path string must be in Unicode.
2030 MultiByteToWideChar(CP_ACP, 0, fname, -1, wsz, MAX_PATH);
2031
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002032 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
2034 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002035 goto shortcut_end;
2036# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
2038 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002039 goto shortcut_end;
2040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 // Get the path to the link target.
2043 ZeroMemory(buf, MAX_PATH);
2044 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
2045 if (hr == S_OK && buf[0] != NUL)
2046 rfname = vim_strsave(buf);
2047
Bram Moolenaar604729e2013-08-30 16:44:19 +02002048shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 // Release all interface pointers (both belong to the same object)
2050 if (ppf != NULL)
2051 ppf->lpVtbl->Release(ppf);
2052 if (psl != NULL)
2053 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02002054# ifdef FEAT_MBYTE
2055 if (pslw != NULL)
2056 pslw->lpVtbl->Release(pslw);
2057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 CoUninitialize();
2060 return rfname;
2061}
2062#endif
2063
2064#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
2065/*
2066 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
2067 */
2068 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002069win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070{
2071# ifndef FEAT_GUI
2072 GetConsoleHwnd(); /* get value of s_hwnd */
2073# endif
2074 if (s_hwnd != 0)
2075 SetForegroundWindow(s_hwnd);
2076}
2077#endif
2078
2079#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2080/*
2081 * Client-server code for Vim
2082 *
2083 * Originally written by Paul Moore
2084 */
2085
2086/* In order to handle inter-process messages, we need to have a window. But
2087 * the functions in this module can be called before the main GUI window is
2088 * created (and may also be called in the console version, where there is no
2089 * GUI window at all).
2090 *
2091 * So we create a hidden window, and arrange to destroy it on exit.
2092 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002093HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
2095#define VIM_CLASSNAME "VIM_MESSAGES"
2096#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2097
2098/* Communication is via WM_COPYDATA messages. The message type is send in
2099 * the dwData parameter. Types are defined here. */
2100#define COPYDATA_KEYS 0
2101#define COPYDATA_REPLY 1
2102#define COPYDATA_EXPR 10
2103#define COPYDATA_RESULT 11
2104#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002105#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
2107/* This is a structure containing a server HWND and its name. */
2108struct server_id
2109{
2110 HWND hwnd;
2111 char_u *name;
2112};
2113
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002114/* Last received 'encoding' that the client uses. */
2115static char_u *client_enc = NULL;
2116
2117/*
2118 * Tell the other side what encoding we are using.
2119 * Errors are ignored.
2120 */
2121 static void
2122serverSendEnc(HWND target)
2123{
2124 COPYDATASTRUCT data;
2125
2126 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002127#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002128 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002129 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002130#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002131 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002132 data.lpData = "latin1";
2133#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002134 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2135 (LPARAM)(&data));
2136}
2137
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138/*
2139 * Clean up on exit. This destroys the hidden message window.
2140 */
2141 static void
2142#ifdef __BORLANDC__
2143 _RTLENTRYF
2144#endif
2145CleanUpMessaging(void)
2146{
2147 if (message_window != 0)
2148 {
2149 DestroyWindow(message_window);
2150 message_window = 0;
2151 }
2152}
2153
2154static int save_reply(HWND server, char_u *reply, int expr);
2155
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 * The window procedure for the hidden message window.
2158 * It handles callback messages and notifications from servers.
2159 * In order to process these messages, it is necessary to run a
2160 * message loop. Code which may run before the main message loop
2161 * is started (in the GUI) is careful to pump messages when it needs
2162 * to. Features which require message delivery during normal use will
2163 * not work in the console version - this basically means those
2164 * features which allow Vim to act as a server, rather than a client.
2165 */
2166 static LRESULT CALLBACK
2167Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2168{
2169 if (msg == WM_COPYDATA)
2170 {
2171 /* This is a message from another Vim. The dwData member of the
2172 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002173 * COPYDATA_ENCODING:
2174 * The encoding that the client uses. Following messages will
2175 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 * COPYDATA_KEYS:
2177 * A key sequence. We are a server, and a client wants these keys
2178 * adding to the input queue.
2179 * COPYDATA_REPLY:
2180 * A reply. We are a client, and a server has sent this message
2181 * in response to a request. (server2client())
2182 * COPYDATA_EXPR:
2183 * An expression. We are a server, and a client wants us to
2184 * evaluate this expression.
2185 * COPYDATA_RESULT:
2186 * A reply. We are a client, and a server has sent this message
2187 * in response to a COPYDATA_EXPR.
2188 * COPYDATA_ERROR_RESULT:
2189 * A reply. We are a client, and a server has sent this message
2190 * in response to a COPYDATA_EXPR that failed to evaluate.
2191 */
2192 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2193 HWND sender = (HWND)wParam;
2194 COPYDATASTRUCT reply;
2195 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002197 char_u *str;
2198 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
2200 switch (data->dwData)
2201 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002202 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002203# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002204 /* Remember the encoding that the client uses. */
2205 vim_free(client_enc);
2206 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002207# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002208 return 1;
2209
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 case COPYDATA_KEYS:
2211 /* Remember who sent this, for <client> */
2212 clientWindow = sender;
2213
2214 /* Add the received keys to the input buffer. The loop waiting
2215 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002216 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2217 server_to_input_buf(str);
2218 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220# ifdef FEAT_GUI
2221 /* Wake up the main GUI loop. */
2222 if (s_hwnd != 0)
2223 PostMessage(s_hwnd, WM_NULL, 0, 0);
2224# endif
2225 return 1;
2226
2227 case COPYDATA_EXPR:
2228 /* Remember who sent this, for <client> */
2229 clientWindow = sender;
2230
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002231 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2232 res = eval_client_expr_to_string(str);
2233 vim_free(tofree);
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 if (res == NULL)
2236 {
2237 res = vim_strsave(_(e_invexprmsg));
2238 reply.dwData = COPYDATA_ERROR_RESULT;
2239 }
2240 else
2241 reply.dwData = COPYDATA_RESULT;
2242 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002243 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002245 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002246 retval = (int)SendMessage(sender, WM_COPYDATA,
2247 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 vim_free(res);
2249 return retval;
2250
2251 case COPYDATA_REPLY:
2252 case COPYDATA_RESULT:
2253 case COPYDATA_ERROR_RESULT:
2254 if (data->lpData != NULL)
2255 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002256 str = serverConvert(client_enc, (char_u *)data->lpData,
2257 &tofree);
2258 if (tofree == NULL)
2259 str = vim_strsave(str);
2260 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 (data->dwData == COPYDATA_REPLY ? 0 :
2262 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002263 2))) == FAIL)
2264 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002266 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002268 char_u winstr[30];
2269
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002270 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002271 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 TRUE, curbuf);
2273 }
2274#endif
2275 }
2276 return 1;
2277 }
2278
2279 return 0;
2280 }
2281
2282 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2283 {
2284 /* When the message window is activated (brought to the foreground),
2285 * this actually applies to the text window. */
2286#ifndef FEAT_GUI
2287 GetConsoleHwnd(); /* get value of s_hwnd */
2288#endif
2289 if (s_hwnd != 0)
2290 {
2291 SetForegroundWindow(s_hwnd);
2292 return 0;
2293 }
2294 }
2295
2296 return DefWindowProc(hwnd, msg, wParam, lParam);
2297}
2298
2299/*
2300 * Initialise the message handling process. This involves creating a window
2301 * to handle messages - the window will not be visible.
2302 */
2303 void
2304serverInitMessaging(void)
2305{
2306 WNDCLASS wndclass;
2307 HINSTANCE s_hinst;
2308
2309 /* Clean up on exit */
2310 atexit(CleanUpMessaging);
2311
2312 /* Register a window class - we only really care
2313 * about the window procedure
2314 */
2315 s_hinst = (HINSTANCE)GetModuleHandle(0);
2316 wndclass.style = 0;
2317 wndclass.lpfnWndProc = Messaging_WndProc;
2318 wndclass.cbClsExtra = 0;
2319 wndclass.cbWndExtra = 0;
2320 wndclass.hInstance = s_hinst;
2321 wndclass.hIcon = NULL;
2322 wndclass.hCursor = NULL;
2323 wndclass.hbrBackground = NULL;
2324 wndclass.lpszMenuName = NULL;
2325 wndclass.lpszClassName = VIM_CLASSNAME;
2326 RegisterClass(&wndclass);
2327
2328 /* Create the message window. It will be hidden, so the details don't
2329 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2330 * focus from gvim. */
2331 message_window = CreateWindow(VIM_CLASSNAME, "",
2332 WS_POPUPWINDOW | WS_CAPTION,
2333 CW_USEDEFAULT, CW_USEDEFAULT,
2334 100, 100, NULL, NULL,
2335 s_hinst, NULL);
2336}
2337
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002338/* Used by serverSendToVim() to find an alternate server name. */
2339static char_u *altname_buf_ptr = NULL;
2340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341/*
2342 * Get the title of the window "hwnd", which is the Vim server name, in
2343 * "name[namelen]" and return the length.
2344 * Returns zero if window "hwnd" is not a Vim server.
2345 */
2346 static int
2347getVimServerName(HWND hwnd, char *name, int namelen)
2348{
2349 int len;
2350 char buffer[VIM_CLASSNAME_LEN + 1];
2351
2352 /* Ignore windows which aren't Vim message windows */
2353 len = GetClassName(hwnd, buffer, sizeof(buffer));
2354 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2355 return 0;
2356
2357 /* Get the title of the window */
2358 return GetWindowText(hwnd, name, namelen);
2359}
2360
2361 static BOOL CALLBACK
2362enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2363{
2364 struct server_id *id = (struct server_id *)lparam;
2365 char server[MAX_PATH];
2366
2367 /* Get the title of the window */
2368 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2369 return TRUE;
2370
2371 /* If this is the server we're looking for, return its HWND */
2372 if (STRICMP(server, id->name) == 0)
2373 {
2374 id->hwnd = hwnd;
2375 return FALSE;
2376 }
2377
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002378 /* If we are looking for an alternate server, remember this name. */
2379 if (altname_buf_ptr != NULL
2380 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2381 && vim_isdigit(server[STRLEN(id->name)]))
2382 {
2383 STRCPY(altname_buf_ptr, server);
2384 altname_buf_ptr = NULL; /* don't use another name */
2385 }
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 /* Otherwise, keep looking */
2388 return TRUE;
2389}
2390
2391 static BOOL CALLBACK
2392enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2393{
2394 garray_T *ga = (garray_T *)lparam;
2395 char server[MAX_PATH];
2396
2397 /* Get the title of the window */
2398 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2399 return TRUE;
2400
2401 /* Add the name to the list */
2402 ga_concat(ga, server);
2403 ga_concat(ga, "\n");
2404 return TRUE;
2405}
2406
2407 static HWND
2408findServer(char_u *name)
2409{
2410 struct server_id id;
2411
2412 id.name = name;
2413 id.hwnd = 0;
2414
2415 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2416
2417 return id.hwnd;
2418}
2419
2420 void
2421serverSetName(char_u *name)
2422{
2423 char_u *ok_name;
2424 HWND hwnd = 0;
2425 int i = 0;
2426 char_u *p;
2427
2428 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002429 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431 STRCPY(ok_name, name);
2432 p = ok_name + STRLEN(name);
2433
2434 for (;;)
2435 {
2436 /* This is inefficient - we're doing an EnumWindows loop for each
2437 * possible name. It would be better to grab all names in one go,
2438 * and scan the list each time...
2439 */
2440 hwnd = findServer(ok_name);
2441 if (hwnd == 0)
2442 break;
2443
2444 ++i;
2445 if (i >= 1000)
2446 break;
2447
2448 sprintf((char *)p, "%d", i);
2449 }
2450
2451 if (hwnd != 0)
2452 vim_free(ok_name);
2453 else
2454 {
2455 /* Remember the name */
2456 serverName = ok_name;
2457#ifdef FEAT_TITLE
2458 need_maketitle = TRUE; /* update Vim window title later */
2459#endif
2460
2461 /* Update the message window title */
2462 SetWindowText(message_window, ok_name);
2463
2464#ifdef FEAT_EVAL
2465 /* Set the servername variable */
2466 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2467#endif
2468 }
2469}
2470
2471 char_u *
2472serverGetVimNames(void)
2473{
2474 garray_T ga;
2475
2476 ga_init2(&ga, 1, 100);
2477
2478 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002479 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 return ga.ga_data;
2482}
2483
2484 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002485serverSendReply(
2486 char_u *name, /* Where to send. */
2487 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
2489 HWND target;
2490 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002491 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
2493 /* The "name" argument is a magic cookie obtained from expand("<client>").
2494 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2495 * value of the client's message window HWND.
2496 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002497 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (n == 0)
2499 return -1;
2500
2501 target = (HWND)n;
2502 if (!IsWindow(target))
2503 return -1;
2504
2505 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002506 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 data.lpData = reply;
2508
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002509 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2511 (LPARAM)(&data)))
2512 return 0;
2513
2514 return -1;
2515}
2516
2517 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002518serverSendToVim(
2519 char_u *name, /* Where to send. */
2520 char_u *cmd, /* What to send. */
2521 char_u **result, /* Result of eval'ed expression */
2522 void *ptarget, /* HWND of server */
2523 int asExpr, /* Expression or keys? */
2524 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002526 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 COPYDATASTRUCT data;
2528 char_u *retval = NULL;
2529 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002530 char_u altname_buf[MAX_PATH];
2531
2532 /* If the server name does not end in a digit then we look for an
2533 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2534 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2535 altname_buf_ptr = altname_buf;
2536 altname_buf[0] = NUL;
2537 target = findServer(name);
2538 altname_buf_ptr = NULL;
2539 if (target == 0 && altname_buf[0] != NUL)
2540 /* Use another server name we found. */
2541 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543 if (target == 0)
2544 {
2545 if (!silent)
2546 EMSG2(_(e_noserver), name);
2547 return -1;
2548 }
2549
2550 if (ptarget)
2551 *(HWND *)ptarget = target;
2552
2553 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002554 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 data.lpData = cmd;
2556
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002557 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2559 (LPARAM)(&data)) == 0)
2560 return -1;
2561
2562 if (asExpr)
2563 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2564
2565 if (result == NULL)
2566 vim_free(retval);
2567 else
2568 *result = retval; /* Caller assumes responsibility for freeing */
2569
2570 return retcode;
2571}
2572
2573/*
2574 * Bring the server to the foreground.
2575 */
2576 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002577serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
2579 HWND target = findServer(name);
2580
2581 if (target != 0)
2582 SetForegroundWindow(target);
2583}
2584
2585/* Replies from server need to be stored until the client picks them up via
2586 * remote_read(). So we maintain a list of server-id/reply pairs.
2587 * Note that there could be multiple replies from one server pending if the
2588 * client is slow picking them up.
2589 * We just store the replies in a simple list. When we remove an entry, we
2590 * move list entries down to fill the gap.
2591 * The server ID is simply the HWND.
2592 */
2593typedef struct
2594{
2595 HWND server; /* server window */
2596 char_u *reply; /* reply string */
2597 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002598} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2601
2602#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2603#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
2605/* Flag which is used to wait for a reply */
2606static int reply_received = 0;
2607
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002608/*
2609 * Store a reply. "reply" must be allocated memory (or NULL).
2610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 static int
2612save_reply(HWND server, char_u *reply, int expr)
2613{
2614 reply_T *rep;
2615
2616 if (ga_grow(&reply_list, 1) == FAIL)
2617 return FAIL;
2618
2619 rep = REPLY_ITEM(REPLY_COUNT);
2620 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002621 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 rep->expr_result = expr;
2623 if (rep->reply == NULL)
2624 return FAIL;
2625
2626 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 reply_received = 1;
2628 return OK;
2629}
2630
2631/*
2632 * Get a reply from server "server".
2633 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2634 * server2client() message.
2635 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2636 * If "remove" is TRUE, consume the message, the caller must free it then.
2637 * if "wait" is TRUE block until a message arrives (or the server exits).
2638 */
2639 char_u *
2640serverGetReply(HWND server, int *expr_res, int remove, int wait)
2641{
2642 int i;
2643 char_u *reply;
2644 reply_T *rep;
2645
2646 /* When waiting, loop until the message waiting for is received. */
2647 for (;;)
2648 {
2649 /* Reset this here, in case a message arrives while we are going
2650 * through the already received messages. */
2651 reply_received = 0;
2652
2653 for (i = 0; i < REPLY_COUNT; ++i)
2654 {
2655 rep = REPLY_ITEM(i);
2656 if (rep->server == server
2657 && ((rep->expr_result != 0) == (expr_res != NULL)))
2658 {
2659 /* Save the values we've found for later */
2660 reply = rep->reply;
2661 if (expr_res != NULL)
2662 *expr_res = rep->expr_result == 1 ? 0 : -1;
2663
2664 if (remove)
2665 {
2666 /* Move the rest of the list down to fill the gap */
2667 mch_memmove(rep, rep + 1,
2668 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2669 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671
2672 /* Return the reply to the caller, who takes on responsibility
2673 * for freeing it if "remove" is TRUE. */
2674 return reply;
2675 }
2676 }
2677
2678 /* If we got here, we didn't find a reply. Return immediately if the
2679 * "wait" parameter isn't set. */
2680 if (!wait)
2681 break;
2682
2683 /* We need to wait for a reply. Enter a message loop until the
2684 * "reply_received" flag gets set. */
2685
2686 /* Loop until we receive a reply */
2687 while (reply_received == 0)
2688 {
2689 /* Wait for a SendMessage() call to us. This could be the reply
2690 * we are waiting for. Use a timeout of a second, to catch the
2691 * situation that the server died unexpectedly. */
2692 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2693
2694 /* If the server has died, give up */
2695 if (!IsWindow(server))
2696 return NULL;
2697
2698 serverProcessPendingMessages();
2699 }
2700 }
2701
2702 return NULL;
2703}
2704
2705/*
2706 * Process any messages in the Windows message queue.
2707 */
2708 void
2709serverProcessPendingMessages(void)
2710{
2711 MSG msg;
2712
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002713 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
2715 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002716 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718}
2719
2720#endif /* FEAT_CLIENTSERVER */
2721
2722#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2723 || defined(PROTO)
2724
2725struct charset_pair
2726{
2727 char *name;
2728 BYTE charset;
2729};
2730
2731static struct charset_pair
2732charset_pairs[] =
2733{
2734 {"ANSI", ANSI_CHARSET},
2735 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2736 {"DEFAULT", DEFAULT_CHARSET},
2737 {"HANGEUL", HANGEUL_CHARSET},
2738 {"OEM", OEM_CHARSET},
2739 {"SHIFTJIS", SHIFTJIS_CHARSET},
2740 {"SYMBOL", SYMBOL_CHARSET},
2741#ifdef WIN3264
2742 {"ARABIC", ARABIC_CHARSET},
2743 {"BALTIC", BALTIC_CHARSET},
2744 {"EASTEUROPE", EASTEUROPE_CHARSET},
2745 {"GB2312", GB2312_CHARSET},
2746 {"GREEK", GREEK_CHARSET},
2747 {"HEBREW", HEBREW_CHARSET},
2748 {"JOHAB", JOHAB_CHARSET},
2749 {"MAC", MAC_CHARSET},
2750 {"RUSSIAN", RUSSIAN_CHARSET},
2751 {"THAI", THAI_CHARSET},
2752 {"TURKISH", TURKISH_CHARSET},
2753# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
2754 && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
2755 {"VIETNAMESE", VIETNAMESE_CHARSET},
2756# endif
2757#endif
2758 {NULL, 0}
2759};
2760
2761/*
2762 * Convert a charset ID to a name.
2763 * Return NULL when not recognized.
2764 */
2765 char *
2766charset_id2name(int id)
2767{
2768 struct charset_pair *cp;
2769
2770 for (cp = charset_pairs; cp->name != NULL; ++cp)
2771 if ((BYTE)id == cp->charset)
2772 break;
2773 return cp->name;
2774}
2775
2776static const LOGFONT s_lfDefault =
2777{
2778 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2779 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2780 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2781 "Fixedsys" /* see _ReadVimIni */
2782};
2783
2784/* Initialise the "current height" to -12 (same as s_lfDefault) just
2785 * in case the user specifies a font in "guifont" with no size before a font
2786 * with an explicit size has been set. This defaults the size to this value
2787 * (-12 equates to roughly 9pt).
2788 */
2789int current_font_height = -12; /* also used in gui_w48.c */
2790
2791/* Convert a string representing a point size into pixels. The string should
2792 * be a positive decimal number, with an optional decimal point (eg, "12", or
2793 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2794 * character is stored in *end. The flag "vertical" says whether this
2795 * calculation is for a vertical (height) size or a horizontal (width) one.
2796 */
2797 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002798points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 int pixels;
2801 int points = 0;
2802 int divisor = 0;
2803 HWND hwnd = (HWND)0;
2804 HDC hdc;
2805 HDC printer_dc = (HDC)pprinter_dc;
2806
2807 while (*str != NUL)
2808 {
2809 if (*str == '.' && divisor == 0)
2810 {
2811 /* Start keeping a divisor, for later */
2812 divisor = 1;
2813 }
2814 else
2815 {
2816 if (!VIM_ISDIGIT(*str))
2817 break;
2818
2819 points *= 10;
2820 points += *str - '0';
2821 divisor *= 10;
2822 }
2823 ++str;
2824 }
2825
2826 if (divisor == 0)
2827 divisor = 1;
2828
2829 if (printer_dc == NULL)
2830 {
2831 hwnd = GetDesktopWindow();
2832 hdc = GetWindowDC(hwnd);
2833 }
2834 else
2835 hdc = printer_dc;
2836
2837 pixels = MulDiv(points,
2838 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2839 72 * divisor);
2840
2841 if (printer_dc == NULL)
2842 ReleaseDC(hwnd, hdc);
2843
2844 *end = str;
2845 return pixels;
2846}
2847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002848/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 static int CALLBACK
2850font_enumproc(
2851 ENUMLOGFONT *elf,
2852 NEWTEXTMETRIC *ntm,
2853 int type,
2854 LPARAM lparam)
2855{
2856 /* Return value:
2857 * 0 = terminate now (monospace & ANSI)
2858 * 1 = continue, still no luck...
2859 * 2 = continue, but we have an acceptable LOGFONT
2860 * (monospace, not ANSI)
2861 * We use these values, as EnumFontFamilies returns 1 if the
2862 * callback function is never called. So, we check the return as
2863 * 0 = perfect, 2 = OK, 1 = no good...
2864 * It's not pretty, but it works!
2865 */
2866
2867 LOGFONT *lf = (LOGFONT *)(lparam);
2868
2869#ifndef FEAT_PROPORTIONAL_FONTS
2870 /* Ignore non-monospace fonts without further ado */
2871 if ((ntm->tmPitchAndFamily & 1) != 0)
2872 return 1;
2873#endif
2874
2875 /* Remember this LOGFONT as a "possible" */
2876 *lf = elf->elfLogFont;
2877
2878 /* Terminate the scan as soon as we find an ANSI font */
2879 if (lf->lfCharSet == ANSI_CHARSET
2880 || lf->lfCharSet == OEM_CHARSET
2881 || lf->lfCharSet == DEFAULT_CHARSET)
2882 return 0;
2883
2884 /* Continue the scan - we have a non-ANSI font */
2885 return 2;
2886}
2887
2888 static int
2889init_logfont(LOGFONT *lf)
2890{
2891 int n;
2892 HWND hwnd = GetDesktopWindow();
2893 HDC hdc = GetWindowDC(hwnd);
2894
2895 n = EnumFontFamilies(hdc,
2896 (LPCSTR)lf->lfFaceName,
2897 (FONTENUMPROC)font_enumproc,
2898 (LPARAM)lf);
2899
2900 ReleaseDC(hwnd, hdc);
2901
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002902 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 if (n == 1)
2904 return FAIL;
2905
2906 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2907 * font - get_logfont() sets bold, italic, etc based on the user's
2908 * input.
2909 */
2910 lf->lfHeight = current_font_height;
2911 lf->lfWidth = 0;
2912 lf->lfItalic = FALSE;
2913 lf->lfUnderline = FALSE;
2914 lf->lfStrikeOut = FALSE;
2915 lf->lfWeight = FW_NORMAL;
2916
2917 /* Return success */
2918 return OK;
2919}
2920
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002921/*
2922 * Get font info from "name" into logfont "lf".
2923 * Return OK for a valid name, FAIL otherwise.
2924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 int
2926get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002927 LOGFONT *lf,
2928 char_u *name,
2929 HDC printer_dc,
2930 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 char_u *p;
2933 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002934 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002936#ifdef FEAT_MBYTE
2937 char_u *acpname = NULL;
2938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939
2940 *lf = s_lfDefault;
2941 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002942 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002944#ifdef FEAT_MBYTE
2945 /* Convert 'name' from 'encoding' to the current codepage, because
2946 * lf->lfFaceName uses the current codepage.
2947 * TODO: Use Wide APIs instead of ANSI APIs. */
2948 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2949 {
2950 int len;
Bram Moolenaara0844a12014-03-19 12:37:22 +01002951 enc_to_acp(name, (int)strlen(name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002952 name = acpname;
2953 }
2954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if (STRCMP(name, "*") == 0)
2956 {
2957#if defined(FEAT_GUI_W32)
2958 CHOOSEFONT cf;
2959 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002960 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 cf.lStructSize = sizeof(cf);
2962 cf.hwndOwner = s_hwnd;
2963 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2964 if (lastlf != NULL)
2965 *lf = *lastlf;
2966 cf.lpLogFont = lf;
2967 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2968 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002969 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002971 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
2973
2974 /*
2975 * Split name up, it could be <name>:h<height>:w<width> etc.
2976 */
2977 for (p = name; *p && *p != ':'; p++)
2978 {
2979 if (p - name + 1 > LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002980 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 lf->lfFaceName[p - name] = *p;
2982 }
2983 if (p != name)
2984 lf->lfFaceName[p - name] = NUL;
2985
2986 /* First set defaults */
2987 lf->lfHeight = -12;
2988 lf->lfWidth = 0;
2989 lf->lfWeight = FW_NORMAL;
2990 lf->lfItalic = FALSE;
2991 lf->lfUnderline = FALSE;
2992 lf->lfStrikeOut = FALSE;
2993
2994 /*
2995 * If the font can't be found, try replacing '_' by ' '.
2996 */
2997 if (init_logfont(lf) == FAIL)
2998 {
2999 int did_replace = FALSE;
3000
3001 for (i = 0; lf->lfFaceName[i]; ++i)
3002 if (lf->lfFaceName[i] == '_')
3003 {
3004 lf->lfFaceName[i] = ' ';
3005 did_replace = TRUE;
3006 }
3007 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003008 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 }
3010
3011 while (*p == ':')
3012 p++;
3013
3014 /* Set the values found after ':' */
3015 while (*p)
3016 {
3017 switch (*p++)
3018 {
3019 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003020 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 break;
3022 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003023 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 break;
3025 case 'b':
3026#ifndef MSWIN16_FASTTEXT
3027 lf->lfWeight = FW_BOLD;
3028#endif
3029 break;
3030 case 'i':
3031#ifndef MSWIN16_FASTTEXT
3032 lf->lfItalic = TRUE;
3033#endif
3034 break;
3035 case 'u':
3036 lf->lfUnderline = TRUE;
3037 break;
3038 case 's':
3039 lf->lfStrikeOut = TRUE;
3040 break;
3041 case 'c':
3042 {
3043 struct charset_pair *cp;
3044
3045 for (cp = charset_pairs; cp->name != NULL; ++cp)
3046 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
3047 {
3048 lf->lfCharSet = cp->charset;
3049 p += strlen(cp->name);
3050 break;
3051 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003052 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003054 vim_snprintf((char *)IObuff, IOSIZE,
3055 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 EMSG(IObuff);
3057 break;
3058 }
3059 break;
3060 }
3061 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003062 if (verbose)
3063 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003064 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003065 _("E245: Illegal char '%c' in font name \"%s\""),
3066 p[-1], name);
3067 EMSG(IObuff);
3068 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003069 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
3071 while (*p == ':')
3072 p++;
3073 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003074 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003078 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
3080 vim_free(lastlf);
3081 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3082 if (lastlf != NULL)
3083 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3084 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003085#ifdef FEAT_MBYTE
3086 vim_free(acpname);
3087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003089 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090}
3091
3092#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003093
3094#if defined(FEAT_CHANNEL) || defined(PROTO)
3095/*
3096 * Initialize the Winsock dll.
3097 */
3098 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003099channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003100{
3101 WSADATA wsaData;
3102 int wsaerr;
3103
3104 if (WSInitialized)
3105 return;
3106
3107 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3108 if (wsaerr == 0)
3109 WSInitialized = TRUE;
3110}
3111#endif