blob: 16268920e358362d52c1fcbd9e629283471c5f62 [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
289mch_input_isatty()
290{
291#ifdef FEAT_GUI_MSWIN
292 return OK; /* GUI always has a tty */
293#else
294 if (isatty(read_cmd_fd))
295 return TRUE;
296 return FALSE;
297#endif
298}
299
300#ifdef FEAT_TITLE
301/*
302 * mch_settitle(): set titlebar of our window
303 */
304 void
305mch_settitle(
306 char_u *title,
307 char_u *icon)
308{
309# ifdef FEAT_GUI_MSWIN
310 gui_mch_settitle(title, icon);
311# else
312 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000313 {
314# ifdef FEAT_MBYTE
315 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
316 {
317 /* Convert the title from 'encoding' to the active codepage. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000318 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000319 int n;
320
321 if (wp != NULL)
322 {
323 n = SetConsoleTitleW(wp);
324 vim_free(wp);
325 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
326 return;
327 }
328 }
329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 SetConsoleTitle(title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332# endif
333}
334
335
336/*
337 * Restore the window/icon title.
338 * which is one of:
339 * 1: Just restore title
340 * 2: Just restore icon (which we don't have)
341 * 3: Restore title and icon (which we don't have)
342 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000343/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 void
345mch_restore_title(
346 int which)
347{
348#ifndef FEAT_GUI_MSWIN
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
358mch_can_restore_title()
359{
360 return TRUE;
361}
362
363
364/*
365 * Return TRUE if we can restore the icon title (we can't)
366 */
367 int
368mch_can_restore_icon()
369{
370 return FALSE;
371}
372#endif /* FEAT_TITLE */
373
374
375/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000376 * Get absolute file name into buffer "buf" of length "len" bytes,
377 * turning all '/'s into '\\'s and getting the correct case of each component
378 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 * When 'shellslash' set do it the other way around.
380 * Return OK or FAIL.
381 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000382/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 int
384mch_FullName(
385 char_u *fname,
386 char_u *buf,
387 int len,
388 int force)
389{
390 int nResult = FAIL;
391
392#ifdef __BORLANDC__
393 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
394 nResult = mch_dirname(buf, len);
395 else
396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000398#ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000399 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
400# ifdef __BORLANDC__
401 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
402 && g_PlatformId == VER_PLATFORM_WIN32_NT
403# endif
404 )
405 {
406 WCHAR *wname;
407 WCHAR wbuf[MAX_PATH];
408 char_u *cname = NULL;
409
410 /* Use the wide function:
411 * - convert the fname from 'encoding' to UCS2.
412 * - invoke _wfullpath()
413 * - convert the result from UCS2 to 'encoding'.
414 */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000415 wname = enc_to_utf16(fname, NULL);
Bram 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
489slash_adjust(p)
490 char_u *p;
491{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200492 if (path_with_url(p))
493 return;
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000494 while (*p)
495 {
496 if (*p == psepcN)
497 *p = psepc;
498 mb_ptr_adv(p);
499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500}
501
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100502#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200503# define OPEN_OH_ARGTYPE intptr_t
504#else
505# define OPEN_OH_ARGTYPE long
506#endif
507
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200508 static int
509stat_symlink_aware(const char *name, struct stat *stp)
510{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100511#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
512 /* Work around for VC12 or earlier (and MinGW). stat() can't handle
513 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200514 * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
515 * status of a symlink itself.
516 * VC10: stat() supports a symlink to a normal file, but it doesn't support
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100517 * a symlink to a directory (always returns an error).
518 * VC11 and VC12: stat() doesn't return an error for a symlink to a
519 * directory, but it doesn't set S_IFDIR flag.
520 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200521 WIN32_FIND_DATA findData;
522 HANDLE hFind, h;
523 DWORD attr = 0;
524 BOOL is_symlink = FALSE;
525
526 hFind = FindFirstFile(name, &findData);
527 if (hFind != INVALID_HANDLE_VALUE)
528 {
529 attr = findData.dwFileAttributes;
530 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
531 && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
532 is_symlink = TRUE;
533 FindClose(hFind);
534 }
535 if (is_symlink)
536 {
537 h = CreateFile(name, FILE_READ_ATTRIBUTES,
538 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
539 OPEN_EXISTING,
540 (attr & FILE_ATTRIBUTE_DIRECTORY)
541 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
542 NULL);
543 if (h != INVALID_HANDLE_VALUE)
544 {
545 int fd, n;
546
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200547 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200548 n = _fstat(fd, (struct _stat*)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100549 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
550 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200551 _close(fd);
552 return n;
553 }
554 }
555#endif
556 return stat(name, stp);
557}
558
559#ifdef FEAT_MBYTE
560 static int
561wstat_symlink_aware(const WCHAR *name, struct _stat *stp)
562{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100563# if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
564 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
565 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200566 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
567 * status of a symlink itself.
568 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100569 * support a symlink to a directory (always returns an error).
570 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
571 * directory, but it doesn't set S_IFDIR flag.
572 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200573 int n;
574 BOOL is_symlink = FALSE;
575 HANDLE hFind, h;
576 DWORD attr = 0;
577 WIN32_FIND_DATAW findDataW;
578
579 hFind = FindFirstFileW(name, &findDataW);
580 if (hFind != INVALID_HANDLE_VALUE)
581 {
582 attr = findDataW.dwFileAttributes;
583 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
584 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
585 is_symlink = TRUE;
586 FindClose(hFind);
587 }
588 if (is_symlink)
589 {
590 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
591 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
592 OPEN_EXISTING,
593 (attr & FILE_ATTRIBUTE_DIRECTORY)
594 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
595 NULL);
596 if (h != INVALID_HANDLE_VALUE)
597 {
598 int fd;
599
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200600 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200601 n = _fstat(fd, stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100602 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
603 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200604 _close(fd);
605 return n;
606 }
607 }
608# endif
609 return _wstat(name, stp);
610}
611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613/*
614 * stat() can't handle a trailing '/' or '\', remove it first.
615 */
616 int
617vim_stat(const char *name, struct stat *stp)
618{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200619#ifdef FEAT_MBYTE
620 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
621 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
622 * UTF-8. */
623 char buf[_MAX_PATH * 3 + 1];
624#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 char buf[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 char *p;
628
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200629 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 p = buf + strlen(buf);
631 if (p > buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000632 mb_ptr_back(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100633
634 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
636 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100637
638 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
639 {
640 /* UNC root path must be followed by '\\'. */
641 p = vim_strpbrk(buf + 2, "\\/");
642 if (p != NULL)
643 {
644 p = vim_strpbrk(p + 1, "\\/");
645 if (p == NULL)
646 STRCAT(buf, "\\");
647 }
648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#ifdef FEAT_MBYTE
650 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
651# ifdef __BORLANDC__
652 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
653 && g_PlatformId == VER_PLATFORM_WIN32_NT
654# endif
655 )
656 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000657 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 int n;
659
660 if (wp != NULL)
661 {
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200662 n = wstat_symlink_aware(wp, (struct _stat *)stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 vim_free(wp);
Bram Moolenaarcd981f22014-02-11 17:06:00 +0100664 if (n >= 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 return n;
666 /* Retry with non-wide function (for Windows 98). Can't use
667 * GetLastError() here and it's unclear what errno gets set to if
668 * the _wstat() fails for missing wide functions. */
669 }
670 }
671#endif
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200672 return stat_symlink_aware(buf, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673}
674
675#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000676/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 void
678mch_settmode(int tmode)
679{
680 /* nothing to do */
681}
682
683 int
684mch_get_shellsize(void)
685{
686 /* never used */
687 return OK;
688}
689
690 void
691mch_set_shellsize(void)
692{
693 /* never used */
694}
695
696/*
697 * Rows and/or Columns has changed.
698 */
699 void
700mch_new_shellsize(void)
701{
702 /* never used */
703}
704
705#endif
706
707/*
708 * We have no job control, so fake it by starting a new shell.
709 */
710 void
711mch_suspend()
712{
713 suspend_shell();
714}
715
716#if defined(USE_MCH_ERRMSG) || defined(PROTO)
717
718#ifdef display_errors
719# undef display_errors
720#endif
721
722/*
723 * Display the saved error message(s).
724 */
725 void
726display_errors()
727{
728 char *p;
729
730 if (error_ga.ga_data != NULL)
731 {
732 /* avoid putting up a message box with blanks only */
733 for (p = (char *)error_ga.ga_data; *p; ++p)
734 if (!isspace(*p))
735 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000736 (void)gui_mch_dialog(
737#ifdef FEAT_GUI
738 gui.starting ? VIM_INFO :
739#endif
740 VIM_ERROR,
741#ifdef FEAT_GUI
742 gui.starting ? (char_u *)_("Message") :
743#endif
744 (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +0100745 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 break;
747 }
748 ga_clear(&error_ga);
749 }
750}
751#endif
752
753
754/*
755 * Return TRUE if "p" contain a wildcard that can be expanded by
756 * dos_expandpath().
757 */
758 int
759mch_has_exp_wildcard(char_u *p)
760{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000761 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 {
763 if (vim_strchr((char_u *)"?*[", *p) != NULL
764 || (*p == '~' && p[1] != NUL))
765 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 }
767 return FALSE;
768}
769
770/*
771 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
772 * shortened file name).
773 */
774 int
775mch_has_wildcard(char_u *p)
776{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000777 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {
779 if (vim_strchr((char_u *)
780# ifdef VIM_BACKTICK
781 "?*$[`"
782# else
783 "?*$["
784# endif
785 , *p) != NULL
786 || (*p == '~' && p[1] != NUL))
787 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 }
789 return FALSE;
790}
791
792
793/*
794 * The normal _chdir() does not change the default drive. This one does.
795 * Returning 0 implies success; -1 implies failure.
796 */
797 int
798mch_chdir(char *path)
799{
800 if (path[0] == NUL) /* just checking... */
801 return -1;
802
Bram Moolenaara2974d72009-07-14 16:38:36 +0000803 if (p_verbose >= 5)
804 {
805 verbose_enter();
806 smsg((char_u *)"chdir(%s)", path);
807 verbose_leave();
808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
810 {
811 /* If we can change to the drive, skip that part of the path. If we
812 * can't then the current directory may be invalid, try using chdir()
813 * with the whole path. */
814 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
815 path += 2;
816 }
817
818 if (*path == NUL) /* drive name only */
819 return 0;
820
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000821#ifdef FEAT_MBYTE
822 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
823 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000824 WCHAR *p = enc_to_utf16(path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000825 int n;
826
827 if (p != NULL)
828 {
829 n = _wchdir(p);
830 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +0100831 if (n == 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
832 return n;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000833 /* Retry with non-wide function (for Windows 98). */
834 }
835 }
836#endif
837
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 return chdir(path); /* let the normal chdir() do the rest */
839}
840
841
842/*
843 * Switching off termcap mode is only allowed when Columns is 80, otherwise a
844 * crash may result. It's always allowed on NT or when running the GUI.
845 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000846/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 int
848can_end_termcap_mode(
849 int give_msg)
850{
851#ifdef FEAT_GUI_MSWIN
852 return TRUE; /* GUI starts a new console anyway */
853#else
854 if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80)
855 return TRUE;
856 if (give_msg)
857 msg(_("'columns' is not 80, cannot execute external commands"));
858 return FALSE;
859#endif
860}
861
862#ifdef FEAT_GUI_MSWIN
863/*
864 * return non-zero if a character is available
865 */
866 int
867mch_char_avail()
868{
869 /* never used */
870 return TRUE;
871}
872#endif
873
874
875/*
876 * set screen mode, always fails.
877 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000878/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 int
880mch_screenmode(
881 char_u *arg)
882{
883 EMSG(_(e_screenmode));
884 return FAIL;
885}
886
887
888#if defined(FEAT_LIBCALL) || defined(PROTO)
889/*
890 * Call a DLL routine which takes either a string or int param
891 * and returns an allocated string.
892 * Return OK if it worked, FAIL if not.
893 */
894# ifdef WIN3264
895typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
896typedef LPTSTR (*MYINTPROCSTR)(int);
897typedef int (*MYSTRPROCINT)(LPTSTR);
898typedef int (*MYINTPROCINT)(int);
899# else
900typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
901typedef LPSTR (*MYINTPROCSTR)(int);
902typedef int (*MYSTRPROCINT)(LPSTR);
903typedef int (*MYINTPROCINT)(int);
904# endif
905
906# ifndef WIN16
907/*
908 * Check if a pointer points to a valid NUL terminated string.
909 * Return the length of the string, including terminating NUL.
910 * Returns 0 for an invalid pointer, 1 for an empty string.
911 */
912 static size_t
913check_str_len(char_u *str)
914{
915 SYSTEM_INFO si;
916 MEMORY_BASIC_INFORMATION mbi;
917 size_t length = 0;
918 size_t i;
919 const char *p;
920
921 /* get page size */
922 GetSystemInfo(&si);
923
924 /* get memory information */
925 if (VirtualQuery(str, &mbi, sizeof(mbi)))
926 {
927 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000928 long_u dwStr = (long_u)str;
929 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
931 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000932 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
934 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000935 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
Bram Moolenaar442b4222010-05-24 21:34:22 +0200937 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 p += pageLength, pageLength = si.dwPageSize)
939 for (i = 0; i < pageLength; ++i, ++length)
940 if (p[i] == NUL)
941 return length + 1;
942 }
943
944 return 0;
945}
946# endif
947
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200948/*
949 * Passed to do_in_runtimepath() to load a vim.ico file.
950 */
951 static void
952mch_icon_load_cb(char_u *fname, void *cookie)
953{
954 HANDLE *h = (HANDLE *)cookie;
955
956 *h = LoadImage(NULL,
957 fname,
958 IMAGE_ICON,
959 64,
960 64,
961 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
962}
963
964/*
965 * Try loading an icon file from 'runtimepath'.
966 */
967 int
968mch_icon_load(iconp)
969 HANDLE *iconp;
970{
971 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
972 FALSE, mch_icon_load_cb, iconp);
973}
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 int
976mch_libcall(
977 char_u *libname,
978 char_u *funcname,
979 char_u *argstring, /* NULL when using a argint */
980 int argint,
981 char_u **string_result,/* NULL when using number_result */
982 int *number_result)
983{
984 HINSTANCE hinstLib;
985 MYSTRPROCSTR ProcAdd;
986 MYINTPROCSTR ProcAddI;
987 char_u *retval_str = NULL;
988 int retval_int = 0;
989 size_t len;
990
991 BOOL fRunTimeLinkSuccess = FALSE;
992
993 // Get a handle to the DLL module.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200994# ifdef WIN16
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 hinstLib = LoadLibrary(libname);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200996# else
997 hinstLib = vimLoadLib(libname);
998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
1000 // If the handle is valid, try to get the function address.
1001 if (hinstLib != NULL)
1002 {
1003#ifdef HAVE_TRY_EXCEPT
1004 __try
1005 {
1006#endif
1007 if (argstring != NULL)
1008 {
1009 /* Call with string argument */
1010 ProcAdd = (MYSTRPROCSTR) GetProcAddress(hinstLib, funcname);
1011 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
1012 {
1013 if (string_result == NULL)
1014 retval_int = ((MYSTRPROCINT)ProcAdd)(argstring);
1015 else
1016 retval_str = (ProcAdd)(argstring);
1017 }
1018 }
1019 else
1020 {
1021 /* Call with number argument */
1022 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, funcname);
1023 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
1024 {
1025 if (string_result == NULL)
1026 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
1027 else
1028 retval_str = (ProcAddI)(argint);
1029 }
1030 }
1031
1032 // Save the string before we free the library.
1033 // Assume that a "1" result is an illegal pointer.
1034 if (string_result == NULL)
1035 *number_result = retval_int;
1036 else if (retval_str != NULL
1037# ifdef WIN16
1038 && retval_str != (char_u *)1
1039 && retval_str != (char_u *)-1
1040 && !IsBadStringPtr(retval_str, INT_MAX)
1041 && (len = strlen(retval_str) + 1) > 0
1042# else
1043 && (len = check_str_len(retval_str)) > 0
1044# endif
1045 )
1046 {
1047 *string_result = lalloc((long_u)len, TRUE);
1048 if (*string_result != NULL)
1049 mch_memmove(*string_result, retval_str, len);
1050 }
1051
1052#ifdef HAVE_TRY_EXCEPT
1053 }
1054 __except(EXCEPTION_EXECUTE_HANDLER)
1055 {
1056 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
1057 RESETSTKOFLW();
1058 fRunTimeLinkSuccess = 0;
1059 }
1060#endif
1061
1062 // Free the DLL module.
1063 (void)FreeLibrary(hinstLib);
1064 }
1065
1066 if (!fRunTimeLinkSuccess)
1067 {
1068 EMSG2(_(e_libcall), funcname);
1069 return FAIL;
1070 }
1071
1072 return OK;
1073}
1074#endif
1075
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076/*
1077 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1078 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001079/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 void
1081DumpPutS(
1082 const char *psz)
1083{
1084# ifdef MCH_WRITE_DUMP
1085 if (fdDump)
1086 {
1087 fputs(psz, fdDump);
1088 if (psz[strlen(psz) - 1] != '\n')
1089 fputc('\n', fdDump);
1090 fflush(fdDump);
1091 }
1092# endif
1093}
1094
1095#ifdef _DEBUG
1096
1097void __cdecl
1098Trace(
1099 char *pszFormat,
1100 ...)
1101{
1102 CHAR szBuff[2048];
1103 va_list args;
1104
1105 va_start(args, pszFormat);
1106 vsprintf(szBuff, pszFormat, args);
1107 va_end(args);
1108
1109 OutputDebugString(szBuff);
1110}
1111
1112#endif //_DEBUG
1113
Bram Moolenaar843ee412004-06-30 16:16:41 +00001114#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115# if defined(FEAT_TITLE) && defined(WIN3264)
1116extern HWND g_hWnd; /* This is in os_win32.c. */
1117# endif
1118
1119/*
1120 * Showing the printer dialog is tricky since we have no GUI
1121 * window to parent it. The following routines are needed to
1122 * get the window parenting and Z-order to work properly.
1123 */
1124 static void
1125GetConsoleHwnd(void)
1126{
1127# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1128
1129 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1130 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1131
1132 /* Skip if it's already set. */
1133 if (s_hwnd != 0)
1134 return;
1135
1136# if defined(FEAT_TITLE) && defined(WIN3264)
1137 /* Window handle may have been found by init code (Windows NT only) */
1138 if (g_hWnd != 0)
1139 {
1140 s_hwnd = g_hWnd;
1141 return;
1142 }
1143# endif
1144
1145 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1146
1147 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1148 pszOldWindowTitle,
1149 GetTickCount(),
1150 GetCurrentProcessId());
1151 SetConsoleTitle(pszNewWindowTitle);
1152 Sleep(40);
1153 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1154
1155 SetConsoleTitle(pszOldWindowTitle);
1156}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001157
1158/*
1159 * Console implementation of ":winpos".
1160 */
1161 int
1162mch_get_winpos(int *x, int *y)
1163{
1164 RECT rect;
1165
1166 GetConsoleHwnd();
1167 GetWindowRect(s_hwnd, &rect);
1168 *x = rect.left;
1169 *y = rect.top;
1170 return OK;
1171}
1172
1173/*
1174 * Console implementation of ":winpos x y".
1175 */
1176 void
1177mch_set_winpos(int x, int y)
1178{
1179 GetConsoleHwnd();
1180 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1181 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1182}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183#endif
1184
1185#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1186
1187# ifdef WIN16
1188# define TEXT(a) a
1189# endif
1190/*=================================================================
1191 * Win32 printer stuff
1192 */
1193
1194static HFONT prt_font_handles[2][2][2];
1195static PRINTDLG prt_dlg;
1196static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1197static TEXTMETRIC prt_tm;
1198static int prt_line_height;
1199static int prt_number_width;
1200static int prt_left_margin;
1201static int prt_right_margin;
1202static int prt_top_margin;
1203static char_u szAppName[] = TEXT("VIM");
1204static HWND hDlgPrint;
1205static int *bUserAbort = NULL;
1206static char_u *prt_name = NULL;
1207
1208/* Defines which are also in vim.rc. */
1209#define IDC_BOX1 400
1210#define IDC_PRINTTEXT1 401
1211#define IDC_PRINTTEXT2 402
1212#define IDC_PROGRESS 403
1213
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001214#if !defined(FEAT_MBYTE) || defined(WIN16)
1215# define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s)
1216#else
1217 static BOOL
1218vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1219{
1220 WCHAR *wp = NULL;
1221 BOOL ret;
1222
1223 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1224 {
1225 wp = enc_to_utf16(s, NULL);
1226 }
1227 if (wp != NULL)
1228 {
1229 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1230 vim_free(wp);
1231 return ret;
1232 }
1233 return SetDlgItemText(hDlg, nIDDlgItem, s);
1234}
1235#endif
1236
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237/*
1238 * Convert BGR to RGB for Windows GDI calls
1239 */
1240 static COLORREF
1241swap_me(COLORREF colorref)
1242{
1243 int temp;
1244 char *ptr = (char *)&colorref;
1245
1246 temp = *(ptr);
1247 *(ptr ) = *(ptr + 2);
1248 *(ptr + 2) = temp;
1249 return colorref;
1250}
1251
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001252/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001253#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001254# define PDP_RETVAL BOOL
1255#else
1256# define PDP_RETVAL INT_PTR
1257#endif
1258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001259/*ARGSUSED*/
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001260 static PDP_RETVAL CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1262{
1263#ifdef FEAT_GETTEXT
1264 NONCLIENTMETRICS nm;
1265 static HFONT hfont;
1266#endif
1267
1268 switch (message)
1269 {
1270 case WM_INITDIALOG:
1271#ifdef FEAT_GETTEXT
1272 nm.cbSize = sizeof(NONCLIENTMETRICS);
1273 if (SystemParametersInfo(
1274 SPI_GETNONCLIENTMETRICS,
1275 sizeof(NONCLIENTMETRICS),
1276 &nm,
1277 0))
1278 {
1279 char buff[MAX_PATH];
1280 int i;
1281
1282 /* Translate the dialog texts */
1283 hfont = CreateFontIndirect(&nm.lfMessageFont);
1284 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1285 {
1286 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1287 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001288 vimSetDlgItemText(hDlg,i, _(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 SendDlgItemMessage(hDlg, IDCANCEL,
1291 WM_SETFONT, (WPARAM)hfont, 1);
1292 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001293 vimSetDlgItemText(hDlg,IDCANCEL, _(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295#endif
1296 SetWindowText(hDlg, szAppName);
1297 if (prt_name != NULL)
1298 {
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001299 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (LPSTR)prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 vim_free(prt_name);
1301 prt_name = NULL;
1302 }
1303 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1304#ifndef FEAT_GUI
1305 BringWindowToTop(s_hwnd);
1306#endif
1307 return TRUE;
1308
1309 case WM_COMMAND:
1310 *bUserAbort = TRUE;
1311 EnableWindow(GetParent(hDlg), TRUE);
1312 DestroyWindow(hDlg);
1313 hDlgPrint = NULL;
1314#ifdef FEAT_GETTEXT
1315 DeleteObject(hfont);
1316#endif
1317 return TRUE;
1318 }
1319 return FALSE;
1320}
1321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001322/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 static BOOL CALLBACK
1324AbortProc(HDC hdcPrn, int iCode)
1325{
1326 MSG msg;
1327
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001328 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001330 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
1332 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001333 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 }
1335 }
1336 return !*bUserAbort;
1337}
1338
1339#ifndef FEAT_GUI
1340
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001341 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342PrintHookProc(
1343 HWND hDlg, // handle to dialog box
1344 UINT uiMsg, // message identifier
1345 WPARAM wParam, // message parameter
1346 LPARAM lParam // message parameter
1347 )
1348{
1349 HWND hwndOwner;
1350 RECT rc, rcDlg, rcOwner;
1351 PRINTDLG *pPD;
1352
1353 if (uiMsg == WM_INITDIALOG)
1354 {
1355 // Get the owner window and dialog box rectangles.
1356 if ((hwndOwner = GetParent(hDlg)) == NULL)
1357 hwndOwner = GetDesktopWindow();
1358
1359 GetWindowRect(hwndOwner, &rcOwner);
1360 GetWindowRect(hDlg, &rcDlg);
1361 CopyRect(&rc, &rcOwner);
1362
1363 // Offset the owner and dialog box rectangles so that
1364 // right and bottom values represent the width and
1365 // height, and then offset the owner again to discard
1366 // space taken up by the dialog box.
1367
1368 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1369 OffsetRect(&rc, -rc.left, -rc.top);
1370 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1371
1372 // The new position is the sum of half the remaining
1373 // space and the owner's original position.
1374
1375 SetWindowPos(hDlg,
1376 HWND_TOP,
1377 rcOwner.left + (rc.right / 2),
1378 rcOwner.top + (rc.bottom / 2),
1379 0, 0, // ignores size arguments
1380 SWP_NOSIZE);
1381
1382 /* tackle the printdlg copiesctrl problem */
1383 pPD = (PRINTDLG *)lParam;
1384 pPD->nCopies = (WORD)pPD->lCustData;
1385 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1386 /* Bring the window to top */
1387 BringWindowToTop(GetParent(hDlg));
1388 SetForegroundWindow(hDlg);
1389 }
1390
1391 return FALSE;
1392}
1393#endif
1394
1395 void
1396mch_print_cleanup(void)
1397{
1398 int pifItalic;
1399 int pifBold;
1400 int pifUnderline;
1401
1402 for (pifBold = 0; pifBold <= 1; pifBold++)
1403 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1404 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1405 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1406
1407 if (prt_dlg.hDC != NULL)
1408 DeleteDC(prt_dlg.hDC);
1409 if (!*bUserAbort)
1410 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1411}
1412
1413 static int
1414to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1415{
1416 int ret = 0;
1417 int u;
1418 int nr;
1419
1420 u = prt_get_unit(idx);
1421 if (u == PRT_UNIT_NONE)
1422 {
1423 u = PRT_UNIT_PERC;
1424 nr = def_number;
1425 }
1426 else
1427 nr = printer_opts[idx].number;
1428
1429 switch (u)
1430 {
1431 case PRT_UNIT_PERC:
1432 ret = (physsize * nr) / 100;
1433 break;
1434 case PRT_UNIT_INCH:
1435 ret = (nr * dpi);
1436 break;
1437 case PRT_UNIT_MM:
1438 ret = (nr * 10 * dpi) / 254;
1439 break;
1440 case PRT_UNIT_POINT:
1441 ret = (nr * 10 * dpi) / 720;
1442 break;
1443 }
1444
1445 if (ret < offset)
1446 return 0;
1447 else
1448 return ret - offset;
1449}
1450
1451 static int
1452prt_get_cpl(void)
1453{
1454 int hr;
1455 int phyw;
1456 int dvoff;
1457 int rev_offset;
1458 int dpi;
1459#ifdef WIN16
1460 POINT pagesize;
1461#endif
1462
1463 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1464 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1465
1466 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
1467#ifdef WIN16
1468 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1469 phyw = pagesize.x;
1470 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1471 dvoff = pagesize.x;
1472#else
1473 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1474 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
1475#endif
1476 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1477
1478 rev_offset = phyw - (dvoff + hr);
1479
1480 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1481 if (prt_use_number())
1482 {
1483 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1484 prt_left_margin += prt_number_width;
1485 }
1486 else
1487 prt_number_width = 0;
1488
1489 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1490 rev_offset, 5);
1491
1492 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1493}
1494
1495 static int
1496prt_get_lpp(void)
1497{
1498 int vr;
1499 int phyw;
1500 int dvoff;
1501 int rev_offset;
1502 int bottom_margin;
1503 int dpi;
1504#ifdef WIN16
1505 POINT pagesize;
1506#endif
1507
1508 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
1509#ifdef WIN16
1510 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1511 phyw = pagesize.y;
1512 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1513 dvoff = pagesize.y;
1514#else
1515 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1516 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
1517#endif
1518 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1519
1520 rev_offset = phyw - (dvoff + vr);
1521
1522 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1523
1524 /* adjust top margin if there is a header */
1525 prt_top_margin += prt_line_height * prt_header_height();
1526
1527 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1528 rev_offset, 5);
1529
1530 return (bottom_margin - prt_top_margin) / prt_line_height;
1531}
1532
1533 int
1534mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1535{
1536 static HGLOBAL stored_dm = NULL;
1537 static HGLOBAL stored_devn = NULL;
1538 static int stored_nCopies = 1;
1539 static int stored_nFlags = 0;
1540
1541 LOGFONT fLogFont;
1542 int pifItalic;
1543 int pifBold;
1544 int pifUnderline;
1545
1546 DEVMODE *mem;
1547 DEVNAMES *devname;
1548 int i;
1549
1550 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001551 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 prt_dlg.lStructSize = sizeof(PRINTDLG);
1553#ifndef FEAT_GUI
1554 GetConsoleHwnd(); /* get value of s_hwnd */
1555#endif
1556 prt_dlg.hwndOwner = s_hwnd;
1557 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1558 if (!forceit)
1559 {
1560 prt_dlg.hDevMode = stored_dm;
1561 prt_dlg.hDevNames = stored_devn;
1562 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1563#ifndef FEAT_GUI
1564 /*
1565 * Use hook to prevent console window being sent to back
1566 */
1567 prt_dlg.lpfnPrintHook = PrintHookProc;
1568 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1569#endif
1570 prt_dlg.Flags |= stored_nFlags;
1571 }
1572
1573 /*
1574 * If bang present, return default printer setup with no dialog
1575 * never show dialog if we are running over telnet
1576 */
1577 if (forceit
1578#ifndef FEAT_GUI
1579 || !term_console
1580#endif
1581 )
1582 {
1583 prt_dlg.Flags |= PD_RETURNDEFAULT;
1584#ifdef WIN3264
1585 /*
1586 * MSDN suggests setting the first parameter to WINSPOOL for
1587 * NT, but NULL appears to work just as well.
1588 */
1589 if (*p_pdev != NUL)
1590 prt_dlg.hDC = CreateDC(NULL, p_pdev, NULL, NULL);
1591 else
1592#endif
1593 {
1594 prt_dlg.Flags |= PD_RETURNDEFAULT;
1595 if (PrintDlg(&prt_dlg) == 0)
1596 goto init_fail_dlg;
1597 }
1598 }
1599 else if (PrintDlg(&prt_dlg) == 0)
1600 goto init_fail_dlg;
1601 else
1602 {
1603 /*
1604 * keep the previous driver context
1605 */
1606 stored_dm = prt_dlg.hDevMode;
1607 stored_devn = prt_dlg.hDevNames;
1608 stored_nFlags = prt_dlg.Flags;
1609 stored_nCopies = prt_dlg.nCopies;
1610 }
1611
1612 if (prt_dlg.hDC == NULL)
1613 {
1614 EMSG(_("E237: Printer selection failed"));
1615 mch_print_cleanup();
1616 return FALSE;
1617 }
1618
1619 /* Not all printer drivers report the support of color (or grey) in the
1620 * same way. Let's set has_color if there appears to be some way to print
1621 * more than B&W. */
1622 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1623 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1624 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1625 || i > 2 || i == -1);
1626
1627 /* Ensure all font styles are baseline aligned */
1628 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1629
1630 /*
1631 * On some windows systems the nCopies parameter is not
1632 * passed back correctly. It must be retrieved from the
1633 * hDevMode struct.
1634 */
1635 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1636 if (mem != NULL)
1637 {
1638#ifdef WIN3264
1639 if (mem->dmCopies != 1)
1640 stored_nCopies = mem->dmCopies;
1641#endif
1642 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1643 psettings->duplex = TRUE;
1644 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1645 psettings->has_color = TRUE;
1646 }
1647 GlobalUnlock(prt_dlg.hDevMode);
1648
1649 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1650 if (devname != 0)
1651 {
1652 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1653 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
1654 char_u *text = _("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001655#ifdef FEAT_MBYTE
1656 char_u *printer_name_orig = printer_name;
1657 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaarf191d552014-10-09 17:05:56 +02001659 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1660 {
1661 char_u *to_free = NULL;
1662 int maxlen;
1663
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001664 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1665 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001666 if (to_free != NULL)
1667 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001668 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001669 if (to_free != NULL)
1670 port_name = to_free;
1671 }
1672#endif
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001673 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1674 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (prt_name != NULL)
1676 wsprintf(prt_name, text, printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001677#ifdef FEAT_MBYTE
1678 if (printer_name != printer_name_orig)
1679 vim_free(printer_name);
1680 if (port_name != port_name_orig)
1681 vim_free(port_name);
1682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684 GlobalUnlock(prt_dlg.hDevNames);
1685
1686 /*
1687 * Initialise the font according to 'printfont'
1688 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001689 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001690 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
1692 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1693 mch_print_cleanup();
1694 return FALSE;
1695 }
1696
1697 for (pifBold = 0; pifBold <= 1; pifBold++)
1698 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1699 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1700 {
1701 fLogFont.lfWeight = boldface[pifBold];
1702 fLogFont.lfItalic = pifItalic;
1703 fLogFont.lfUnderline = pifUnderline;
1704 prt_font_handles[pifBold][pifItalic][pifUnderline]
1705 = CreateFontIndirect(&fLogFont);
1706 }
1707
1708 SetBkMode(prt_dlg.hDC, OPAQUE);
1709 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1710
1711 /*
1712 * Fill in the settings struct
1713 */
1714 psettings->chars_per_line = prt_get_cpl();
1715 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001716 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1717 {
1718 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1719 ? prt_dlg.nCopies : 1;
1720 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1721 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001723 if (psettings->n_collated_copies == 0)
1724 psettings->n_collated_copies = 1;
1725
1726 if (psettings->n_uncollated_copies == 0)
1727 psettings->n_uncollated_copies = 1;
1728 } else {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733 psettings->jobname = jobname;
1734
1735 return TRUE;
1736
1737init_fail_dlg:
1738 {
1739 DWORD err = CommDlgExtendedError();
1740
1741 if (err)
1742 {
1743#ifdef WIN16
1744 char buf[20];
1745
1746 sprintf(buf, "%ld", err);
1747 EMSG2(_("E238: Print error: %s"), buf);
1748#else
1749 char_u *buf;
1750
1751 /* I suspect FormatMessage() doesn't work for values returned by
1752 * CommDlgExtendedError(). What does? */
1753 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1754 FORMAT_MESSAGE_FROM_SYSTEM |
1755 FORMAT_MESSAGE_IGNORE_INSERTS,
1756 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1757 EMSG2(_("E238: Print error: %s"),
1758 buf == NULL ? (char_u *)_("Unknown") : buf);
1759 LocalFree((LPVOID)(buf));
1760#endif
1761 }
1762 else
1763 msg_clr_eos(); /* Maybe canceled */
1764
1765 mch_print_cleanup();
1766 return FALSE;
1767 }
1768}
1769
1770
1771 int
1772mch_print_begin(prt_settings_T *psettings)
1773{
1774 int ret;
1775 static DOCINFO di;
1776 char szBuffer[300];
1777
1778 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1779 prt_dlg.hwndOwner, PrintDlgProc);
1780#ifdef WIN16
1781 Escape(prt_dlg.hDC, SETABORTPROC, 0, (LPSTR)AbortProc, NULL);
1782#else
1783 SetAbortProc(prt_dlg.hDC, AbortProc);
1784#endif
1785 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001786 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (LPSTR)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001788 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 di.cbSize = sizeof(DOCINFO);
1790 di.lpszDocName = psettings->jobname;
1791 ret = StartDoc(prt_dlg.hDC, &di);
1792
1793#ifdef FEAT_GUI
1794 /* Give focus back to main window (when using MDI). */
1795 SetFocus(s_hwnd);
1796#endif
1797
1798 return (ret > 0);
1799}
1800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001801/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 void
1803mch_print_end(prt_settings_T *psettings)
1804{
1805 EndDoc(prt_dlg.hDC);
1806 if (!*bUserAbort)
1807 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1808}
1809
1810 int
1811mch_print_end_page(void)
1812{
1813 return (EndPage(prt_dlg.hDC) > 0);
1814}
1815
1816 int
1817mch_print_begin_page(char_u *msg)
1818{
1819 if (msg != NULL)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001820 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, (LPSTR)msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 return (StartPage(prt_dlg.hDC) > 0);
1822}
1823
1824 int
1825mch_print_blank_page(void)
1826{
1827 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1828}
1829
1830static int prt_pos_x = 0;
1831static int prt_pos_y = 0;
1832
1833 void
1834mch_print_start_line(margin, page_line)
1835 int margin;
1836 int page_line;
1837{
1838 if (margin)
1839 prt_pos_x = -prt_number_width;
1840 else
1841 prt_pos_x = 0;
1842 prt_pos_y = page_line * prt_line_height
1843 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1844}
1845
1846 int
1847mch_print_text_out(char_u *p, int len)
1848{
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001849#if defined(FEAT_PROPORTIONAL_FONTS) || (defined(FEAT_MBYTE) && !defined(WIN16))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 SIZE sz;
1851#endif
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001852#if defined(FEAT_MBYTE) && !defined(WIN16)
1853 WCHAR *wp = NULL;
1854 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001856 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1857 {
1858 wp = enc_to_utf16(p, &wlen);
1859 }
1860 if (wp != NULL)
1861 {
1862 int ret = FALSE;
1863
1864 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1865 prt_pos_y + prt_top_margin, wp, wlen);
1866 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1867 vim_free(wp);
1868 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1869 /* This is wrong when printing spaces for a TAB. */
1870 if (p[len] != NUL)
1871 {
1872 wlen = MB_PTR2LEN(p + len);
1873 wp = enc_to_utf16(p + len, &wlen);
1874 if (wp != NULL)
1875 {
1876 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1877 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1878 vim_free(wp);
1879 }
1880 }
1881 return ret;
1882 }
1883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1885 prt_pos_y + prt_top_margin, p, len);
1886#ifndef FEAT_PROPORTIONAL_FONTS
1887 prt_pos_x += len * prt_tm.tmAveCharWidth;
1888 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1889 + prt_tm.tmOverhang > prt_right_margin);
1890#else
1891# ifdef WIN16
1892 GetTextExtentPoint(prt_dlg.hDC, p, len, &sz);
1893# else
1894 GetTextExtentPoint32(prt_dlg.hDC, p, len, &sz);
1895# endif
1896 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1897 /* This is wrong when printing spaces for a TAB. */
1898 if (p[len] == NUL)
1899 return FALSE;
1900# ifdef WIN16
1901 GetTextExtentPoint(prt_dlg.hDC, p + len, 1, &sz);
1902# else
1903 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
1904# endif
1905 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1906#endif
1907}
1908
1909 void
1910mch_print_set_font(int iBold, int iItalic, int iUnderline)
1911{
1912 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1913}
1914
1915 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001916mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001918 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1919 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 /*
1921 * With a white background we can draw characters transparent, which is
1922 * good for italic characters that overlap to the next char cell.
1923 */
1924 if (bgcol == 0xffffffUL)
1925 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1926 else
1927 SetBkMode(prt_dlg.hDC, OPAQUE);
1928}
1929
1930 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001931mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001933 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1934 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935}
1936
1937#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1938
Bram Moolenaar58d98232005-07-23 22:25:46 +00001939
1940
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001942# ifndef PROTO
1943# include <shlobj.h>
1944# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
1946/*
1947 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1948 * to and return that name in allocated memory.
1949 * Otherwise NULL is returned.
1950 */
1951 char_u *
1952mch_resolve_shortcut(char_u *fname)
1953{
1954 HRESULT hr;
1955 IShellLink *psl = NULL;
1956 IPersistFile *ppf = NULL;
1957 OLECHAR wsz[MAX_PATH];
1958 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001959 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 char_u *rfname = NULL;
1961 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001962# ifdef FEAT_MBYTE
1963 IShellLinkW *pslw = NULL;
1964 WIN32_FIND_DATAW ffdw; // we get those free of charge
1965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
1967 /* Check if the file name ends in ".lnk". Avoid calling
1968 * CoCreateInstance(), it's quite slow. */
1969 if (fname == NULL)
1970 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001971 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1973 return rfname;
1974
1975 CoInitialize(NULL);
1976
Bram Moolenaar604729e2013-08-30 16:44:19 +02001977# ifdef FEAT_MBYTE
1978 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1979 {
1980 // create a link manager object and request its interface
1981 hr = CoCreateInstance(
1982 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1983 &IID_IShellLinkW, (void**)&pslw);
1984 if (hr == S_OK)
1985 {
1986 WCHAR *p = enc_to_utf16(fname, NULL);
1987
1988 if (p != NULL)
1989 {
1990 // Get a pointer to the IPersistFile interface.
1991 hr = pslw->lpVtbl->QueryInterface(
1992 pslw, &IID_IPersistFile, (void**)&ppf);
1993 if (hr != S_OK)
1994 goto shortcut_errorw;
1995
1996 // "load" the name and resolve the link
1997 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1998 if (hr != S_OK)
1999 goto shortcut_errorw;
2000# if 0 // This makes Vim wait a long time if the target does not exist.
2001 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
2002 if (hr != S_OK)
2003 goto shortcut_errorw;
2004# endif
2005
2006 // Get the path to the link target.
2007 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
2008 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
2009 if (hr == S_OK && wsz[0] != NUL)
2010 rfname = utf16_to_enc(wsz, NULL);
2011
2012shortcut_errorw:
2013 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01002014 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02002015 }
2016 }
2017 /* Retry with non-wide function (for Windows 98). */
2018 }
2019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 // create a link manager object and request its interface
2021 hr = CoCreateInstance(
2022 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2023 &IID_IShellLink, (void**)&psl);
2024 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002025 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 // Get a pointer to the IPersistFile interface.
2028 hr = psl->lpVtbl->QueryInterface(
2029 psl, &IID_IPersistFile, (void**)&ppf);
2030 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002031 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033 // full path string must be in Unicode.
2034 MultiByteToWideChar(CP_ACP, 0, fname, -1, wsz, MAX_PATH);
2035
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002036 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
2038 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002039 goto shortcut_end;
2040# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
2042 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002043 goto shortcut_end;
2044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
2046 // Get the path to the link target.
2047 ZeroMemory(buf, MAX_PATH);
2048 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
2049 if (hr == S_OK && buf[0] != NUL)
2050 rfname = vim_strsave(buf);
2051
Bram Moolenaar604729e2013-08-30 16:44:19 +02002052shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 // Release all interface pointers (both belong to the same object)
2054 if (ppf != NULL)
2055 ppf->lpVtbl->Release(ppf);
2056 if (psl != NULL)
2057 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02002058# ifdef FEAT_MBYTE
2059 if (pslw != NULL)
2060 pslw->lpVtbl->Release(pslw);
2061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
2063 CoUninitialize();
2064 return rfname;
2065}
2066#endif
2067
2068#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
2069/*
2070 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
2071 */
2072 void
2073win32_set_foreground()
2074{
2075# ifndef FEAT_GUI
2076 GetConsoleHwnd(); /* get value of s_hwnd */
2077# endif
2078 if (s_hwnd != 0)
2079 SetForegroundWindow(s_hwnd);
2080}
2081#endif
2082
2083#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2084/*
2085 * Client-server code for Vim
2086 *
2087 * Originally written by Paul Moore
2088 */
2089
2090/* In order to handle inter-process messages, we need to have a window. But
2091 * the functions in this module can be called before the main GUI window is
2092 * created (and may also be called in the console version, where there is no
2093 * GUI window at all).
2094 *
2095 * So we create a hidden window, and arrange to destroy it on exit.
2096 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002097HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099#define VIM_CLASSNAME "VIM_MESSAGES"
2100#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2101
2102/* Communication is via WM_COPYDATA messages. The message type is send in
2103 * the dwData parameter. Types are defined here. */
2104#define COPYDATA_KEYS 0
2105#define COPYDATA_REPLY 1
2106#define COPYDATA_EXPR 10
2107#define COPYDATA_RESULT 11
2108#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002109#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
2111/* This is a structure containing a server HWND and its name. */
2112struct server_id
2113{
2114 HWND hwnd;
2115 char_u *name;
2116};
2117
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002118/* Last received 'encoding' that the client uses. */
2119static char_u *client_enc = NULL;
2120
2121/*
2122 * Tell the other side what encoding we are using.
2123 * Errors are ignored.
2124 */
2125 static void
2126serverSendEnc(HWND target)
2127{
2128 COPYDATASTRUCT data;
2129
2130 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002131#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002132 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002133 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002134#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002135 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002136 data.lpData = "latin1";
2137#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002138 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2139 (LPARAM)(&data));
2140}
2141
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142/*
2143 * Clean up on exit. This destroys the hidden message window.
2144 */
2145 static void
2146#ifdef __BORLANDC__
2147 _RTLENTRYF
2148#endif
2149CleanUpMessaging(void)
2150{
2151 if (message_window != 0)
2152 {
2153 DestroyWindow(message_window);
2154 message_window = 0;
2155 }
2156}
2157
2158static int save_reply(HWND server, char_u *reply, int expr);
2159
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 * The window procedure for the hidden message window.
2162 * It handles callback messages and notifications from servers.
2163 * In order to process these messages, it is necessary to run a
2164 * message loop. Code which may run before the main message loop
2165 * is started (in the GUI) is careful to pump messages when it needs
2166 * to. Features which require message delivery during normal use will
2167 * not work in the console version - this basically means those
2168 * features which allow Vim to act as a server, rather than a client.
2169 */
2170 static LRESULT CALLBACK
2171Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2172{
2173 if (msg == WM_COPYDATA)
2174 {
2175 /* This is a message from another Vim. The dwData member of the
2176 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002177 * COPYDATA_ENCODING:
2178 * The encoding that the client uses. Following messages will
2179 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 * COPYDATA_KEYS:
2181 * A key sequence. We are a server, and a client wants these keys
2182 * adding to the input queue.
2183 * COPYDATA_REPLY:
2184 * A reply. We are a client, and a server has sent this message
2185 * in response to a request. (server2client())
2186 * COPYDATA_EXPR:
2187 * An expression. We are a server, and a client wants us to
2188 * evaluate this expression.
2189 * COPYDATA_RESULT:
2190 * A reply. We are a client, and a server has sent this message
2191 * in response to a COPYDATA_EXPR.
2192 * COPYDATA_ERROR_RESULT:
2193 * A reply. We are a client, and a server has sent this message
2194 * in response to a COPYDATA_EXPR that failed to evaluate.
2195 */
2196 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2197 HWND sender = (HWND)wParam;
2198 COPYDATASTRUCT reply;
2199 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002201 char_u *str;
2202 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
2204 switch (data->dwData)
2205 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002206 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002207# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002208 /* Remember the encoding that the client uses. */
2209 vim_free(client_enc);
2210 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002211# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002212 return 1;
2213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 case COPYDATA_KEYS:
2215 /* Remember who sent this, for <client> */
2216 clientWindow = sender;
2217
2218 /* Add the received keys to the input buffer. The loop waiting
2219 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002220 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2221 server_to_input_buf(str);
2222 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
2224# ifdef FEAT_GUI
2225 /* Wake up the main GUI loop. */
2226 if (s_hwnd != 0)
2227 PostMessage(s_hwnd, WM_NULL, 0, 0);
2228# endif
2229 return 1;
2230
2231 case COPYDATA_EXPR:
2232 /* Remember who sent this, for <client> */
2233 clientWindow = sender;
2234
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002235 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2236 res = eval_client_expr_to_string(str);
2237 vim_free(tofree);
2238
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 if (res == NULL)
2240 {
2241 res = vim_strsave(_(e_invexprmsg));
2242 reply.dwData = COPYDATA_ERROR_RESULT;
2243 }
2244 else
2245 reply.dwData = COPYDATA_RESULT;
2246 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002247 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002249 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002250 retval = (int)SendMessage(sender, WM_COPYDATA,
2251 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 vim_free(res);
2253 return retval;
2254
2255 case COPYDATA_REPLY:
2256 case COPYDATA_RESULT:
2257 case COPYDATA_ERROR_RESULT:
2258 if (data->lpData != NULL)
2259 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002260 str = serverConvert(client_enc, (char_u *)data->lpData,
2261 &tofree);
2262 if (tofree == NULL)
2263 str = vim_strsave(str);
2264 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 (data->dwData == COPYDATA_REPLY ? 0 :
2266 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002267 2))) == FAIL)
2268 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002270 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002272 char_u winstr[30];
2273
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002274 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002275 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 TRUE, curbuf);
2277 }
2278#endif
2279 }
2280 return 1;
2281 }
2282
2283 return 0;
2284 }
2285
2286 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2287 {
2288 /* When the message window is activated (brought to the foreground),
2289 * this actually applies to the text window. */
2290#ifndef FEAT_GUI
2291 GetConsoleHwnd(); /* get value of s_hwnd */
2292#endif
2293 if (s_hwnd != 0)
2294 {
2295 SetForegroundWindow(s_hwnd);
2296 return 0;
2297 }
2298 }
2299
2300 return DefWindowProc(hwnd, msg, wParam, lParam);
2301}
2302
2303/*
2304 * Initialise the message handling process. This involves creating a window
2305 * to handle messages - the window will not be visible.
2306 */
2307 void
2308serverInitMessaging(void)
2309{
2310 WNDCLASS wndclass;
2311 HINSTANCE s_hinst;
2312
2313 /* Clean up on exit */
2314 atexit(CleanUpMessaging);
2315
2316 /* Register a window class - we only really care
2317 * about the window procedure
2318 */
2319 s_hinst = (HINSTANCE)GetModuleHandle(0);
2320 wndclass.style = 0;
2321 wndclass.lpfnWndProc = Messaging_WndProc;
2322 wndclass.cbClsExtra = 0;
2323 wndclass.cbWndExtra = 0;
2324 wndclass.hInstance = s_hinst;
2325 wndclass.hIcon = NULL;
2326 wndclass.hCursor = NULL;
2327 wndclass.hbrBackground = NULL;
2328 wndclass.lpszMenuName = NULL;
2329 wndclass.lpszClassName = VIM_CLASSNAME;
2330 RegisterClass(&wndclass);
2331
2332 /* Create the message window. It will be hidden, so the details don't
2333 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2334 * focus from gvim. */
2335 message_window = CreateWindow(VIM_CLASSNAME, "",
2336 WS_POPUPWINDOW | WS_CAPTION,
2337 CW_USEDEFAULT, CW_USEDEFAULT,
2338 100, 100, NULL, NULL,
2339 s_hinst, NULL);
2340}
2341
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002342/* Used by serverSendToVim() to find an alternate server name. */
2343static char_u *altname_buf_ptr = NULL;
2344
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345/*
2346 * Get the title of the window "hwnd", which is the Vim server name, in
2347 * "name[namelen]" and return the length.
2348 * Returns zero if window "hwnd" is not a Vim server.
2349 */
2350 static int
2351getVimServerName(HWND hwnd, char *name, int namelen)
2352{
2353 int len;
2354 char buffer[VIM_CLASSNAME_LEN + 1];
2355
2356 /* Ignore windows which aren't Vim message windows */
2357 len = GetClassName(hwnd, buffer, sizeof(buffer));
2358 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2359 return 0;
2360
2361 /* Get the title of the window */
2362 return GetWindowText(hwnd, name, namelen);
2363}
2364
2365 static BOOL CALLBACK
2366enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2367{
2368 struct server_id *id = (struct server_id *)lparam;
2369 char server[MAX_PATH];
2370
2371 /* Get the title of the window */
2372 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2373 return TRUE;
2374
2375 /* If this is the server we're looking for, return its HWND */
2376 if (STRICMP(server, id->name) == 0)
2377 {
2378 id->hwnd = hwnd;
2379 return FALSE;
2380 }
2381
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002382 /* If we are looking for an alternate server, remember this name. */
2383 if (altname_buf_ptr != NULL
2384 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2385 && vim_isdigit(server[STRLEN(id->name)]))
2386 {
2387 STRCPY(altname_buf_ptr, server);
2388 altname_buf_ptr = NULL; /* don't use another name */
2389 }
2390
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 /* Otherwise, keep looking */
2392 return TRUE;
2393}
2394
2395 static BOOL CALLBACK
2396enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2397{
2398 garray_T *ga = (garray_T *)lparam;
2399 char server[MAX_PATH];
2400
2401 /* Get the title of the window */
2402 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2403 return TRUE;
2404
2405 /* Add the name to the list */
2406 ga_concat(ga, server);
2407 ga_concat(ga, "\n");
2408 return TRUE;
2409}
2410
2411 static HWND
2412findServer(char_u *name)
2413{
2414 struct server_id id;
2415
2416 id.name = name;
2417 id.hwnd = 0;
2418
2419 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2420
2421 return id.hwnd;
2422}
2423
2424 void
2425serverSetName(char_u *name)
2426{
2427 char_u *ok_name;
2428 HWND hwnd = 0;
2429 int i = 0;
2430 char_u *p;
2431
2432 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002433 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 STRCPY(ok_name, name);
2436 p = ok_name + STRLEN(name);
2437
2438 for (;;)
2439 {
2440 /* This is inefficient - we're doing an EnumWindows loop for each
2441 * possible name. It would be better to grab all names in one go,
2442 * and scan the list each time...
2443 */
2444 hwnd = findServer(ok_name);
2445 if (hwnd == 0)
2446 break;
2447
2448 ++i;
2449 if (i >= 1000)
2450 break;
2451
2452 sprintf((char *)p, "%d", i);
2453 }
2454
2455 if (hwnd != 0)
2456 vim_free(ok_name);
2457 else
2458 {
2459 /* Remember the name */
2460 serverName = ok_name;
2461#ifdef FEAT_TITLE
2462 need_maketitle = TRUE; /* update Vim window title later */
2463#endif
2464
2465 /* Update the message window title */
2466 SetWindowText(message_window, ok_name);
2467
2468#ifdef FEAT_EVAL
2469 /* Set the servername variable */
2470 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2471#endif
2472 }
2473}
2474
2475 char_u *
2476serverGetVimNames(void)
2477{
2478 garray_T ga;
2479
2480 ga_init2(&ga, 1, 100);
2481
2482 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002483 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 return ga.ga_data;
2486}
2487
2488 int
2489serverSendReply(name, reply)
2490 char_u *name; /* Where to send. */
2491 char_u *reply; /* What to send. */
2492{
2493 HWND target;
2494 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002495 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497 /* The "name" argument is a magic cookie obtained from expand("<client>").
2498 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2499 * value of the client's message window HWND.
2500 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002501 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 if (n == 0)
2503 return -1;
2504
2505 target = (HWND)n;
2506 if (!IsWindow(target))
2507 return -1;
2508
2509 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002510 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 data.lpData = reply;
2512
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002513 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2515 (LPARAM)(&data)))
2516 return 0;
2517
2518 return -1;
2519}
2520
2521 int
2522serverSendToVim(name, cmd, result, ptarget, asExpr, silent)
2523 char_u *name; /* Where to send. */
2524 char_u *cmd; /* What to send. */
2525 char_u **result; /* Result of eval'ed expression */
2526 void *ptarget; /* HWND of server */
2527 int asExpr; /* Expression or keys? */
2528 int silent; /* don't complain about no server */
2529{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002530 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 COPYDATASTRUCT data;
2532 char_u *retval = NULL;
2533 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002534 char_u altname_buf[MAX_PATH];
2535
2536 /* If the server name does not end in a digit then we look for an
2537 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2538 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2539 altname_buf_ptr = altname_buf;
2540 altname_buf[0] = NUL;
2541 target = findServer(name);
2542 altname_buf_ptr = NULL;
2543 if (target == 0 && altname_buf[0] != NUL)
2544 /* Use another server name we found. */
2545 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
2547 if (target == 0)
2548 {
2549 if (!silent)
2550 EMSG2(_(e_noserver), name);
2551 return -1;
2552 }
2553
2554 if (ptarget)
2555 *(HWND *)ptarget = target;
2556
2557 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002558 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 data.lpData = cmd;
2560
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002561 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2563 (LPARAM)(&data)) == 0)
2564 return -1;
2565
2566 if (asExpr)
2567 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2568
2569 if (result == NULL)
2570 vim_free(retval);
2571 else
2572 *result = retval; /* Caller assumes responsibility for freeing */
2573
2574 return retcode;
2575}
2576
2577/*
2578 * Bring the server to the foreground.
2579 */
2580 void
2581serverForeground(name)
2582 char_u *name;
2583{
2584 HWND target = findServer(name);
2585
2586 if (target != 0)
2587 SetForegroundWindow(target);
2588}
2589
2590/* Replies from server need to be stored until the client picks them up via
2591 * remote_read(). So we maintain a list of server-id/reply pairs.
2592 * Note that there could be multiple replies from one server pending if the
2593 * client is slow picking them up.
2594 * We just store the replies in a simple list. When we remove an entry, we
2595 * move list entries down to fill the gap.
2596 * The server ID is simply the HWND.
2597 */
2598typedef struct
2599{
2600 HWND server; /* server window */
2601 char_u *reply; /* reply string */
2602 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002603} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
2605static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2606
2607#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2608#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
2610/* Flag which is used to wait for a reply */
2611static int reply_received = 0;
2612
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002613/*
2614 * Store a reply. "reply" must be allocated memory (or NULL).
2615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 static int
2617save_reply(HWND server, char_u *reply, int expr)
2618{
2619 reply_T *rep;
2620
2621 if (ga_grow(&reply_list, 1) == FAIL)
2622 return FAIL;
2623
2624 rep = REPLY_ITEM(REPLY_COUNT);
2625 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002626 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 rep->expr_result = expr;
2628 if (rep->reply == NULL)
2629 return FAIL;
2630
2631 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 reply_received = 1;
2633 return OK;
2634}
2635
2636/*
2637 * Get a reply from server "server".
2638 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2639 * server2client() message.
2640 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2641 * If "remove" is TRUE, consume the message, the caller must free it then.
2642 * if "wait" is TRUE block until a message arrives (or the server exits).
2643 */
2644 char_u *
2645serverGetReply(HWND server, int *expr_res, int remove, int wait)
2646{
2647 int i;
2648 char_u *reply;
2649 reply_T *rep;
2650
2651 /* When waiting, loop until the message waiting for is received. */
2652 for (;;)
2653 {
2654 /* Reset this here, in case a message arrives while we are going
2655 * through the already received messages. */
2656 reply_received = 0;
2657
2658 for (i = 0; i < REPLY_COUNT; ++i)
2659 {
2660 rep = REPLY_ITEM(i);
2661 if (rep->server == server
2662 && ((rep->expr_result != 0) == (expr_res != NULL)))
2663 {
2664 /* Save the values we've found for later */
2665 reply = rep->reply;
2666 if (expr_res != NULL)
2667 *expr_res = rep->expr_result == 1 ? 0 : -1;
2668
2669 if (remove)
2670 {
2671 /* Move the rest of the list down to fill the gap */
2672 mch_memmove(rep, rep + 1,
2673 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2674 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
2676
2677 /* Return the reply to the caller, who takes on responsibility
2678 * for freeing it if "remove" is TRUE. */
2679 return reply;
2680 }
2681 }
2682
2683 /* If we got here, we didn't find a reply. Return immediately if the
2684 * "wait" parameter isn't set. */
2685 if (!wait)
2686 break;
2687
2688 /* We need to wait for a reply. Enter a message loop until the
2689 * "reply_received" flag gets set. */
2690
2691 /* Loop until we receive a reply */
2692 while (reply_received == 0)
2693 {
2694 /* Wait for a SendMessage() call to us. This could be the reply
2695 * we are waiting for. Use a timeout of a second, to catch the
2696 * situation that the server died unexpectedly. */
2697 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2698
2699 /* If the server has died, give up */
2700 if (!IsWindow(server))
2701 return NULL;
2702
2703 serverProcessPendingMessages();
2704 }
2705 }
2706
2707 return NULL;
2708}
2709
2710/*
2711 * Process any messages in the Windows message queue.
2712 */
2713 void
2714serverProcessPendingMessages(void)
2715{
2716 MSG msg;
2717
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002718 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002721 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723}
2724
2725#endif /* FEAT_CLIENTSERVER */
2726
2727#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2728 || defined(PROTO)
2729
2730struct charset_pair
2731{
2732 char *name;
2733 BYTE charset;
2734};
2735
2736static struct charset_pair
2737charset_pairs[] =
2738{
2739 {"ANSI", ANSI_CHARSET},
2740 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2741 {"DEFAULT", DEFAULT_CHARSET},
2742 {"HANGEUL", HANGEUL_CHARSET},
2743 {"OEM", OEM_CHARSET},
2744 {"SHIFTJIS", SHIFTJIS_CHARSET},
2745 {"SYMBOL", SYMBOL_CHARSET},
2746#ifdef WIN3264
2747 {"ARABIC", ARABIC_CHARSET},
2748 {"BALTIC", BALTIC_CHARSET},
2749 {"EASTEUROPE", EASTEUROPE_CHARSET},
2750 {"GB2312", GB2312_CHARSET},
2751 {"GREEK", GREEK_CHARSET},
2752 {"HEBREW", HEBREW_CHARSET},
2753 {"JOHAB", JOHAB_CHARSET},
2754 {"MAC", MAC_CHARSET},
2755 {"RUSSIAN", RUSSIAN_CHARSET},
2756 {"THAI", THAI_CHARSET},
2757 {"TURKISH", TURKISH_CHARSET},
2758# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
2759 && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
2760 {"VIETNAMESE", VIETNAMESE_CHARSET},
2761# endif
2762#endif
2763 {NULL, 0}
2764};
2765
2766/*
2767 * Convert a charset ID to a name.
2768 * Return NULL when not recognized.
2769 */
2770 char *
2771charset_id2name(int id)
2772{
2773 struct charset_pair *cp;
2774
2775 for (cp = charset_pairs; cp->name != NULL; ++cp)
2776 if ((BYTE)id == cp->charset)
2777 break;
2778 return cp->name;
2779}
2780
2781static const LOGFONT s_lfDefault =
2782{
2783 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2784 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2785 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2786 "Fixedsys" /* see _ReadVimIni */
2787};
2788
2789/* Initialise the "current height" to -12 (same as s_lfDefault) just
2790 * in case the user specifies a font in "guifont" with no size before a font
2791 * with an explicit size has been set. This defaults the size to this value
2792 * (-12 equates to roughly 9pt).
2793 */
2794int current_font_height = -12; /* also used in gui_w48.c */
2795
2796/* Convert a string representing a point size into pixels. The string should
2797 * be a positive decimal number, with an optional decimal point (eg, "12", or
2798 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2799 * character is stored in *end. The flag "vertical" says whether this
2800 * calculation is for a vertical (height) size or a horizontal (width) one.
2801 */
2802 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002803points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
2805 int pixels;
2806 int points = 0;
2807 int divisor = 0;
2808 HWND hwnd = (HWND)0;
2809 HDC hdc;
2810 HDC printer_dc = (HDC)pprinter_dc;
2811
2812 while (*str != NUL)
2813 {
2814 if (*str == '.' && divisor == 0)
2815 {
2816 /* Start keeping a divisor, for later */
2817 divisor = 1;
2818 }
2819 else
2820 {
2821 if (!VIM_ISDIGIT(*str))
2822 break;
2823
2824 points *= 10;
2825 points += *str - '0';
2826 divisor *= 10;
2827 }
2828 ++str;
2829 }
2830
2831 if (divisor == 0)
2832 divisor = 1;
2833
2834 if (printer_dc == NULL)
2835 {
2836 hwnd = GetDesktopWindow();
2837 hdc = GetWindowDC(hwnd);
2838 }
2839 else
2840 hdc = printer_dc;
2841
2842 pixels = MulDiv(points,
2843 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2844 72 * divisor);
2845
2846 if (printer_dc == NULL)
2847 ReleaseDC(hwnd, hdc);
2848
2849 *end = str;
2850 return pixels;
2851}
2852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002853/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 static int CALLBACK
2855font_enumproc(
2856 ENUMLOGFONT *elf,
2857 NEWTEXTMETRIC *ntm,
2858 int type,
2859 LPARAM lparam)
2860{
2861 /* Return value:
2862 * 0 = terminate now (monospace & ANSI)
2863 * 1 = continue, still no luck...
2864 * 2 = continue, but we have an acceptable LOGFONT
2865 * (monospace, not ANSI)
2866 * We use these values, as EnumFontFamilies returns 1 if the
2867 * callback function is never called. So, we check the return as
2868 * 0 = perfect, 2 = OK, 1 = no good...
2869 * It's not pretty, but it works!
2870 */
2871
2872 LOGFONT *lf = (LOGFONT *)(lparam);
2873
2874#ifndef FEAT_PROPORTIONAL_FONTS
2875 /* Ignore non-monospace fonts without further ado */
2876 if ((ntm->tmPitchAndFamily & 1) != 0)
2877 return 1;
2878#endif
2879
2880 /* Remember this LOGFONT as a "possible" */
2881 *lf = elf->elfLogFont;
2882
2883 /* Terminate the scan as soon as we find an ANSI font */
2884 if (lf->lfCharSet == ANSI_CHARSET
2885 || lf->lfCharSet == OEM_CHARSET
2886 || lf->lfCharSet == DEFAULT_CHARSET)
2887 return 0;
2888
2889 /* Continue the scan - we have a non-ANSI font */
2890 return 2;
2891}
2892
2893 static int
2894init_logfont(LOGFONT *lf)
2895{
2896 int n;
2897 HWND hwnd = GetDesktopWindow();
2898 HDC hdc = GetWindowDC(hwnd);
2899
2900 n = EnumFontFamilies(hdc,
2901 (LPCSTR)lf->lfFaceName,
2902 (FONTENUMPROC)font_enumproc,
2903 (LPARAM)lf);
2904
2905 ReleaseDC(hwnd, hdc);
2906
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002907 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 if (n == 1)
2909 return FAIL;
2910
2911 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2912 * font - get_logfont() sets bold, italic, etc based on the user's
2913 * input.
2914 */
2915 lf->lfHeight = current_font_height;
2916 lf->lfWidth = 0;
2917 lf->lfItalic = FALSE;
2918 lf->lfUnderline = FALSE;
2919 lf->lfStrikeOut = FALSE;
2920 lf->lfWeight = FW_NORMAL;
2921
2922 /* Return success */
2923 return OK;
2924}
2925
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002926/*
2927 * Get font info from "name" into logfont "lf".
2928 * Return OK for a valid name, FAIL otherwise.
2929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 int
2931get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002932 LOGFONT *lf,
2933 char_u *name,
2934 HDC printer_dc,
2935 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
2937 char_u *p;
2938 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002939 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002941#ifdef FEAT_MBYTE
2942 char_u *acpname = NULL;
2943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
2945 *lf = s_lfDefault;
2946 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002947 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002949#ifdef FEAT_MBYTE
2950 /* Convert 'name' from 'encoding' to the current codepage, because
2951 * lf->lfFaceName uses the current codepage.
2952 * TODO: Use Wide APIs instead of ANSI APIs. */
2953 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2954 {
2955 int len;
Bram Moolenaara0844a12014-03-19 12:37:22 +01002956 enc_to_acp(name, (int)strlen(name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002957 name = acpname;
2958 }
2959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 if (STRCMP(name, "*") == 0)
2961 {
2962#if defined(FEAT_GUI_W32)
2963 CHOOSEFONT cf;
2964 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002965 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 cf.lStructSize = sizeof(cf);
2967 cf.hwndOwner = s_hwnd;
2968 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2969 if (lastlf != NULL)
2970 *lf = *lastlf;
2971 cf.lpLogFont = lf;
2972 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2973 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002974 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002976 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 }
2978
2979 /*
2980 * Split name up, it could be <name>:h<height>:w<width> etc.
2981 */
2982 for (p = name; *p && *p != ':'; p++)
2983 {
2984 if (p - name + 1 > LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002985 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 lf->lfFaceName[p - name] = *p;
2987 }
2988 if (p != name)
2989 lf->lfFaceName[p - name] = NUL;
2990
2991 /* First set defaults */
2992 lf->lfHeight = -12;
2993 lf->lfWidth = 0;
2994 lf->lfWeight = FW_NORMAL;
2995 lf->lfItalic = FALSE;
2996 lf->lfUnderline = FALSE;
2997 lf->lfStrikeOut = FALSE;
2998
2999 /*
3000 * If the font can't be found, try replacing '_' by ' '.
3001 */
3002 if (init_logfont(lf) == FAIL)
3003 {
3004 int did_replace = FALSE;
3005
3006 for (i = 0; lf->lfFaceName[i]; ++i)
3007 if (lf->lfFaceName[i] == '_')
3008 {
3009 lf->lfFaceName[i] = ' ';
3010 did_replace = TRUE;
3011 }
3012 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003013 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015
3016 while (*p == ':')
3017 p++;
3018
3019 /* Set the values found after ':' */
3020 while (*p)
3021 {
3022 switch (*p++)
3023 {
3024 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003025 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 break;
3027 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003028 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 break;
3030 case 'b':
3031#ifndef MSWIN16_FASTTEXT
3032 lf->lfWeight = FW_BOLD;
3033#endif
3034 break;
3035 case 'i':
3036#ifndef MSWIN16_FASTTEXT
3037 lf->lfItalic = TRUE;
3038#endif
3039 break;
3040 case 'u':
3041 lf->lfUnderline = TRUE;
3042 break;
3043 case 's':
3044 lf->lfStrikeOut = TRUE;
3045 break;
3046 case 'c':
3047 {
3048 struct charset_pair *cp;
3049
3050 for (cp = charset_pairs; cp->name != NULL; ++cp)
3051 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
3052 {
3053 lf->lfCharSet = cp->charset;
3054 p += strlen(cp->name);
3055 break;
3056 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003057 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003059 vim_snprintf((char *)IObuff, IOSIZE,
3060 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 EMSG(IObuff);
3062 break;
3063 }
3064 break;
3065 }
3066 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003067 if (verbose)
3068 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003069 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003070 _("E245: Illegal char '%c' in font name \"%s\""),
3071 p[-1], name);
3072 EMSG(IObuff);
3073 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003074 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
3076 while (*p == ':')
3077 p++;
3078 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003079 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003083 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 {
3085 vim_free(lastlf);
3086 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3087 if (lastlf != NULL)
3088 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3089 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003090#ifdef FEAT_MBYTE
3091 vim_free(acpname);
3092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003094 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095}
3096
3097#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003098
3099#if defined(FEAT_CHANNEL) || defined(PROTO)
3100/*
3101 * Initialize the Winsock dll.
3102 */
3103 void
3104channel_init_winsock()
3105{
3106 WSADATA wsaData;
3107 int wsaerr;
3108
3109 if (WSInitialized)
3110 return;
3111
3112 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3113 if (wsaerr == 0)
3114 WSInitialized = TRUE;
3115}
3116#endif