blob: ed7aa834d4e471743ba42cce3da5b22d83e366e8 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * os_mswin.c
12 *
13 * Routines common to both Win16 and Win32.
14 */
15
16#ifdef WIN16
17# ifdef __BORLANDC__
18# pragma warn -par
19# pragma warn -ucp
20# pragma warn -use
21# pragma warn -aus
22# endif
23#endif
24
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#include "vim.h"
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef WIN16
28# define SHORT_FNAME /* always 8.3 file name */
Bram Moolenaar82881492012-11-20 16:53:39 +010029/* cproto fails on missing include files */
30# ifndef PROTO
31# include <dos.h>
32# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000033# include <string.h>
34#endif
35#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000036#include <signal.h>
37#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010038#ifndef PROTO
39# include <process.h>
40#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#undef chdir
43#ifdef __GNUC__
44# ifndef __MINGW32__
45# include <dirent.h>
46# endif
47#else
48# include <direct.h>
49#endif
50
Bram Moolenaar82881492012-11-20 16:53:39 +010051#ifndef PROTO
52# if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32)
53# include <shellapi.h>
54# endif
55
56# if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
57# include <dlgs.h>
58# ifdef WIN3264
59# include <winspool.h>
60# else
61# include <print.h>
62# endif
63# include <commdlg.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
65
Bram Moolenaar82881492012-11-20 16:53:39 +010066#endif /* PROTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68#ifdef __MINGW32__
69# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
70# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
71# endif
72# ifndef RIGHTMOST_BUTTON_PRESSED
73# define RIGHTMOST_BUTTON_PRESSED 0x0002
74# endif
75# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
76# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
77# endif
78# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
79# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
80# endif
81# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
82# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
83# endif
84
85/*
86 * EventFlags
87 */
88# ifndef MOUSE_MOVED
89# define MOUSE_MOVED 0x0001
90# endif
91# ifndef DOUBLE_CLICK
92# define DOUBLE_CLICK 0x0002
93# endif
94#endif
95
96/*
97 * When generating prototypes for Win32 on Unix, these lines make the syntax
98 * errors disappear. They do not need to be correct.
99 */
100#ifdef PROTO
101#define WINAPI
102#define WINBASEAPI
103typedef int BOOL;
104typedef int CALLBACK;
105typedef int COLORREF;
106typedef int CONSOLE_CURSOR_INFO;
107typedef int COORD;
108typedef int DWORD;
109typedef int ENUMLOGFONT;
110typedef int HANDLE;
111typedef int HDC;
112typedef int HFONT;
113typedef int HICON;
114typedef int HWND;
115typedef int INPUT_RECORD;
116typedef int KEY_EVENT_RECORD;
117typedef int LOGFONT;
118typedef int LPARAM;
119typedef int LPBOOL;
120typedef int LPCSTR;
121typedef int LPCWSTR;
122typedef int LPSTR;
123typedef int LPTSTR;
124typedef int LPWSTR;
125typedef int LRESULT;
126typedef int MOUSE_EVENT_RECORD;
127typedef int NEWTEXTMETRIC;
128typedef int PACL;
129typedef int PRINTDLG;
130typedef int PSECURITY_DESCRIPTOR;
131typedef int PSID;
132typedef int SECURITY_INFORMATION;
133typedef int SHORT;
134typedef int SMALL_RECT;
135typedef int TEXTMETRIC;
136typedef int UINT;
137typedef int WCHAR;
138typedef int WORD;
139typedef int WPARAM;
140typedef void VOID;
141#endif
142
143/* Record all output and all keyboard & mouse input */
144/* #define MCH_WRITE_DUMP */
145
146#ifdef MCH_WRITE_DUMP
147FILE* fdDump = NULL;
148#endif
149
150#ifdef WIN3264
151extern DWORD g_PlatformId;
152#endif
153
154#ifndef FEAT_GUI_MSWIN
155extern char g_szOrigTitle[];
156#endif
157
158#ifdef FEAT_GUI
159extern HWND s_hwnd;
160#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161static HWND s_hwnd = 0; /* console window handle, set by GetConsoleHwnd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
163
164extern int WSInitialized;
165
166/* Don't generate prototypes here, because some systems do have these
167 * functions. */
168#if defined(__GNUC__) && !defined(PROTO)
169# ifndef __MINGW32__
170int _stricoll(char *a, char *b)
171{
172 // the ANSI-ish correct way is to use strxfrm():
173 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
174 strxfrm(a_buff, a, 512);
175 strxfrm(b_buff, b, 512);
176 return strcoll(a_buff, b_buff);
177}
178
179char * _fullpath(char *buf, char *fname, int len)
180{
181 LPTSTR toss;
182
183 return (char *)GetFullPathName(fname, len, buf, &toss);
184}
185# endif
186
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100187# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188int _chdrive(int drive)
189{
190 char temp [3] = "-:";
191 temp[0] = drive + 'A' - 1;
192 return !SetCurrentDirectory(temp);
193}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#else
196# ifdef __BORLANDC__
197/* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll:
198 * but it does in BC 5.02! */
199# if __BORLANDC__ < 0x502
200int _stricoll(char *a, char *b)
201{
202# if 1
203 // this is fast but not correct:
204 return stricmp(a, b);
205# else
206 // the ANSI-ish correct way is to use strxfrm():
207 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
208 strxfrm(a_buff, a, 512);
209 strxfrm(b_buff, b, 512);
210 return strcoll(a_buff, b_buff);
211# endif
212}
213# endif
214# endif
215#endif
216
217
218#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
219/*
220 * GUI version of mch_exit().
221 * Shut down and exit with status `r'
222 * Careful: mch_exit() may be called before mch_init()!
223 */
224 void
225mch_exit(int r)
226{
227 display_errors();
228
229 ml_close_all(TRUE); /* remove all memfiles */
230
231# ifdef FEAT_OLE
232 UninitOLE();
233# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100234# ifdef FEAT_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 if (WSInitialized)
236 {
237 WSInitialized = FALSE;
238 WSACleanup();
239 }
240# endif
241#ifdef DYNAMIC_GETTEXT
242 dyn_libintl_end();
243#endif
244
245 if (gui.in_use)
246 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000247
248#ifdef EXITFREE
249 free_all_mem();
250#endif
251
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 exit(r);
253}
254
255#endif /* FEAT_GUI_MSWIN */
256
257
258/*
259 * Init the tables for toupper() and tolower().
260 */
261 void
262mch_early_init(void)
263{
264 int i;
265
266#ifdef WIN3264
267 PlatformId();
268#endif
269
270 /* Init the tables for toupper() and tolower() */
271 for (i = 0; i < 256; ++i)
272 toupper_tab[i] = tolower_tab[i] = i;
273#ifdef WIN3264
274 CharUpperBuff(toupper_tab, 256);
275 CharLowerBuff(tolower_tab, 256);
276#else
277 AnsiUpperBuff(toupper_tab, 256);
278 AnsiLowerBuff(tolower_tab, 256);
279#endif
280}
281
282
283/*
284 * Return TRUE if the input comes from a terminal, FALSE otherwise.
285 */
286 int
287mch_input_isatty()
288{
289#ifdef FEAT_GUI_MSWIN
290 return OK; /* GUI always has a tty */
291#else
292 if (isatty(read_cmd_fd))
293 return TRUE;
294 return FALSE;
295#endif
296}
297
298#ifdef FEAT_TITLE
299/*
300 * mch_settitle(): set titlebar of our window
301 */
302 void
303mch_settitle(
304 char_u *title,
305 char_u *icon)
306{
307# ifdef FEAT_GUI_MSWIN
308 gui_mch_settitle(title, icon);
309# else
310 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000311 {
312# ifdef FEAT_MBYTE
313 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
314 {
315 /* Convert the title from 'encoding' to the active codepage. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000316 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000317 int n;
318
319 if (wp != NULL)
320 {
321 n = SetConsoleTitleW(wp);
322 vim_free(wp);
323 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
324 return;
325 }
326 }
327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 SetConsoleTitle(title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330# endif
331}
332
333
334/*
335 * Restore the window/icon title.
336 * which is one of:
337 * 1: Just restore title
338 * 2: Just restore icon (which we don't have)
339 * 3: Restore title and icon (which we don't have)
340 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000341/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 void
343mch_restore_title(
344 int which)
345{
346#ifndef FEAT_GUI_MSWIN
Bram Moolenaar1df52d72014-10-15 22:50:10 +0200347 SetConsoleTitle(g_szOrigTitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#endif
349}
350
351
352/*
353 * Return TRUE if we can restore the title (we can)
354 */
355 int
356mch_can_restore_title()
357{
358 return TRUE;
359}
360
361
362/*
363 * Return TRUE if we can restore the icon title (we can't)
364 */
365 int
366mch_can_restore_icon()
367{
368 return FALSE;
369}
370#endif /* FEAT_TITLE */
371
372
373/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000374 * Get absolute file name into buffer "buf" of length "len" bytes,
375 * turning all '/'s into '\\'s and getting the correct case of each component
376 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 * When 'shellslash' set do it the other way around.
378 * Return OK or FAIL.
379 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000380/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 int
382mch_FullName(
383 char_u *fname,
384 char_u *buf,
385 int len,
386 int force)
387{
388 int nResult = FAIL;
389
390#ifdef __BORLANDC__
391 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
392 nResult = mch_dirname(buf, len);
393 else
394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000396#ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000397 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
398# ifdef __BORLANDC__
399 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
400 && g_PlatformId == VER_PLATFORM_WIN32_NT
401# endif
402 )
403 {
404 WCHAR *wname;
405 WCHAR wbuf[MAX_PATH];
406 char_u *cname = NULL;
407
408 /* Use the wide function:
409 * - convert the fname from 'encoding' to UCS2.
410 * - invoke _wfullpath()
411 * - convert the result from UCS2 to 'encoding'.
412 */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000413 wname = enc_to_utf16(fname, NULL);
Bram Moolenaar374bf022014-11-05 19:33:24 +0100414 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000415 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000416 cname = utf16_to_enc((short_u *)wbuf, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000417 if (cname != NULL)
418 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000419 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000420 nResult = OK;
421 }
422 }
423 vim_free(wname);
424 vim_free(cname);
425 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000426 if (nResult == FAIL) /* fall back to non-wide function */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000428 {
429 if (_fullpath(buf, fname, len - 1) == NULL)
430 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000431 /* failed, use relative path name */
432 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000433 }
434 else
435 nResult = OK;
436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438
439#ifdef USE_FNAME_CASE
440 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000441#else
442 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#endif
444
445 return nResult;
446}
447
448
449/*
450 * Return TRUE if "fname" does not depend on the current directory.
451 */
452 int
453mch_isFullName(char_u *fname)
454{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200455#ifdef FEAT_MBYTE
456 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
457 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
458 * UTF-8. */
459 char szName[_MAX_PATH * 3 + 1];
460#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 char szName[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
464 /* A name like "d:/foo" and "//server/share" is absolute */
465 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
466 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
467 return TRUE;
468
469 /* A name that can't be made absolute probably isn't absolute. */
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200470 if (mch_FullName(fname, szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 return FALSE;
472
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000473 return pathcmp(fname, szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474}
475
476/*
477 * Replace all slashes by backslashes.
478 * This used to be the other way around, but MS-DOS sometimes has problems
479 * with slashes (e.g. in a command name). We can't have mixed slashes and
480 * backslashes, because comparing file names will not work correctly. The
481 * commands that use a file name should try to avoid the need to type a
482 * backslash twice.
483 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200484 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 */
486 void
487slash_adjust(p)
488 char_u *p;
489{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200490 if (path_with_url(p))
491 return;
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000492 while (*p)
493 {
494 if (*p == psepcN)
495 *p = psepc;
496 mb_ptr_adv(p);
497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498}
499
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100500#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200501# define OPEN_OH_ARGTYPE intptr_t
502#else
503# define OPEN_OH_ARGTYPE long
504#endif
505
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200506 static int
507stat_symlink_aware(const char *name, struct stat *stp)
508{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100509#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
510 /* Work around for VC12 or earlier (and MinGW). stat() can't handle
511 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200512 * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
513 * status of a symlink itself.
514 * VC10: stat() supports a symlink to a normal file, but it doesn't support
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100515 * a symlink to a directory (always returns an error).
516 * VC11 and VC12: stat() doesn't return an error for a symlink to a
517 * directory, but it doesn't set S_IFDIR flag.
518 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200519 WIN32_FIND_DATA findData;
520 HANDLE hFind, h;
521 DWORD attr = 0;
522 BOOL is_symlink = FALSE;
523
524 hFind = FindFirstFile(name, &findData);
525 if (hFind != INVALID_HANDLE_VALUE)
526 {
527 attr = findData.dwFileAttributes;
528 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
529 && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
530 is_symlink = TRUE;
531 FindClose(hFind);
532 }
533 if (is_symlink)
534 {
535 h = CreateFile(name, FILE_READ_ATTRIBUTES,
536 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
537 OPEN_EXISTING,
538 (attr & FILE_ATTRIBUTE_DIRECTORY)
539 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
540 NULL);
541 if (h != INVALID_HANDLE_VALUE)
542 {
543 int fd, n;
544
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200545 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200546 n = _fstat(fd, (struct _stat*)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100547 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
548 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200549 _close(fd);
550 return n;
551 }
552 }
553#endif
554 return stat(name, stp);
555}
556
557#ifdef FEAT_MBYTE
558 static int
559wstat_symlink_aware(const WCHAR *name, struct _stat *stp)
560{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100561# if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
562 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
563 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200564 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
565 * status of a symlink itself.
566 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100567 * support a symlink to a directory (always returns an error).
568 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
569 * directory, but it doesn't set S_IFDIR flag.
570 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200571 int n;
572 BOOL is_symlink = FALSE;
573 HANDLE hFind, h;
574 DWORD attr = 0;
575 WIN32_FIND_DATAW findDataW;
576
577 hFind = FindFirstFileW(name, &findDataW);
578 if (hFind != INVALID_HANDLE_VALUE)
579 {
580 attr = findDataW.dwFileAttributes;
581 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
582 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
583 is_symlink = TRUE;
584 FindClose(hFind);
585 }
586 if (is_symlink)
587 {
588 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
589 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
590 OPEN_EXISTING,
591 (attr & FILE_ATTRIBUTE_DIRECTORY)
592 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
593 NULL);
594 if (h != INVALID_HANDLE_VALUE)
595 {
596 int fd;
597
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200598 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200599 n = _fstat(fd, stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100600 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
601 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200602 _close(fd);
603 return n;
604 }
605 }
606# endif
607 return _wstat(name, stp);
608}
609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611/*
612 * stat() can't handle a trailing '/' or '\', remove it first.
613 */
614 int
615vim_stat(const char *name, struct stat *stp)
616{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200617#ifdef FEAT_MBYTE
618 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
619 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
620 * UTF-8. */
621 char buf[_MAX_PATH * 3 + 1];
622#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 char buf[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 char *p;
626
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200627 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 p = buf + strlen(buf);
629 if (p > buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000630 mb_ptr_back(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100631
632 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
634 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100635
636 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
637 {
638 /* UNC root path must be followed by '\\'. */
639 p = vim_strpbrk(buf + 2, "\\/");
640 if (p != NULL)
641 {
642 p = vim_strpbrk(p + 1, "\\/");
643 if (p == NULL)
644 STRCAT(buf, "\\");
645 }
646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#ifdef FEAT_MBYTE
648 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
649# ifdef __BORLANDC__
650 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
651 && g_PlatformId == VER_PLATFORM_WIN32_NT
652# endif
653 )
654 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000655 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 int n;
657
658 if (wp != NULL)
659 {
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200660 n = wstat_symlink_aware(wp, (struct _stat *)stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 vim_free(wp);
Bram Moolenaarcd981f22014-02-11 17:06:00 +0100662 if (n >= 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 return n;
664 /* Retry with non-wide function (for Windows 98). Can't use
665 * GetLastError() here and it's unclear what errno gets set to if
666 * the _wstat() fails for missing wide functions. */
667 }
668 }
669#endif
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200670 return stat_symlink_aware(buf, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671}
672
673#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000674/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 void
676mch_settmode(int tmode)
677{
678 /* nothing to do */
679}
680
681 int
682mch_get_shellsize(void)
683{
684 /* never used */
685 return OK;
686}
687
688 void
689mch_set_shellsize(void)
690{
691 /* never used */
692}
693
694/*
695 * Rows and/or Columns has changed.
696 */
697 void
698mch_new_shellsize(void)
699{
700 /* never used */
701}
702
703#endif
704
705/*
706 * We have no job control, so fake it by starting a new shell.
707 */
708 void
709mch_suspend()
710{
711 suspend_shell();
712}
713
714#if defined(USE_MCH_ERRMSG) || defined(PROTO)
715
716#ifdef display_errors
717# undef display_errors
718#endif
719
720/*
721 * Display the saved error message(s).
722 */
723 void
724display_errors()
725{
726 char *p;
727
728 if (error_ga.ga_data != NULL)
729 {
730 /* avoid putting up a message box with blanks only */
731 for (p = (char *)error_ga.ga_data; *p; ++p)
732 if (!isspace(*p))
733 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000734 (void)gui_mch_dialog(
735#ifdef FEAT_GUI
736 gui.starting ? VIM_INFO :
737#endif
738 VIM_ERROR,
739#ifdef FEAT_GUI
740 gui.starting ? (char_u *)_("Message") :
741#endif
742 (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +0100743 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 break;
745 }
746 ga_clear(&error_ga);
747 }
748}
749#endif
750
751
752/*
753 * Return TRUE if "p" contain a wildcard that can be expanded by
754 * dos_expandpath().
755 */
756 int
757mch_has_exp_wildcard(char_u *p)
758{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000759 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {
761 if (vim_strchr((char_u *)"?*[", *p) != NULL
762 || (*p == '~' && p[1] != NUL))
763 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 }
765 return FALSE;
766}
767
768/*
769 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
770 * shortened file name).
771 */
772 int
773mch_has_wildcard(char_u *p)
774{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000775 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 {
777 if (vim_strchr((char_u *)
778# ifdef VIM_BACKTICK
779 "?*$[`"
780# else
781 "?*$["
782# endif
783 , *p) != NULL
784 || (*p == '~' && p[1] != NUL))
785 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787 return FALSE;
788}
789
790
791/*
792 * The normal _chdir() does not change the default drive. This one does.
793 * Returning 0 implies success; -1 implies failure.
794 */
795 int
796mch_chdir(char *path)
797{
798 if (path[0] == NUL) /* just checking... */
799 return -1;
800
Bram Moolenaara2974d72009-07-14 16:38:36 +0000801 if (p_verbose >= 5)
802 {
803 verbose_enter();
804 smsg((char_u *)"chdir(%s)", path);
805 verbose_leave();
806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
808 {
809 /* If we can change to the drive, skip that part of the path. If we
810 * can't then the current directory may be invalid, try using chdir()
811 * with the whole path. */
812 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
813 path += 2;
814 }
815
816 if (*path == NUL) /* drive name only */
817 return 0;
818
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000819#ifdef FEAT_MBYTE
820 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
821 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000822 WCHAR *p = enc_to_utf16(path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000823 int n;
824
825 if (p != NULL)
826 {
827 n = _wchdir(p);
828 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +0100829 if (n == 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
830 return n;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000831 /* Retry with non-wide function (for Windows 98). */
832 }
833 }
834#endif
835
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 return chdir(path); /* let the normal chdir() do the rest */
837}
838
839
840/*
841 * Switching off termcap mode is only allowed when Columns is 80, otherwise a
842 * crash may result. It's always allowed on NT or when running the GUI.
843 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000844/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 int
846can_end_termcap_mode(
847 int give_msg)
848{
849#ifdef FEAT_GUI_MSWIN
850 return TRUE; /* GUI starts a new console anyway */
851#else
852 if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80)
853 return TRUE;
854 if (give_msg)
855 msg(_("'columns' is not 80, cannot execute external commands"));
856 return FALSE;
857#endif
858}
859
860#ifdef FEAT_GUI_MSWIN
861/*
862 * return non-zero if a character is available
863 */
864 int
865mch_char_avail()
866{
867 /* never used */
868 return TRUE;
869}
870#endif
871
872
873/*
874 * set screen mode, always fails.
875 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000876/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 int
878mch_screenmode(
879 char_u *arg)
880{
881 EMSG(_(e_screenmode));
882 return FAIL;
883}
884
885
886#if defined(FEAT_LIBCALL) || defined(PROTO)
887/*
888 * Call a DLL routine which takes either a string or int param
889 * and returns an allocated string.
890 * Return OK if it worked, FAIL if not.
891 */
892# ifdef WIN3264
893typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
894typedef LPTSTR (*MYINTPROCSTR)(int);
895typedef int (*MYSTRPROCINT)(LPTSTR);
896typedef int (*MYINTPROCINT)(int);
897# else
898typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
899typedef LPSTR (*MYINTPROCSTR)(int);
900typedef int (*MYSTRPROCINT)(LPSTR);
901typedef int (*MYINTPROCINT)(int);
902# endif
903
904# ifndef WIN16
905/*
906 * Check if a pointer points to a valid NUL terminated string.
907 * Return the length of the string, including terminating NUL.
908 * Returns 0 for an invalid pointer, 1 for an empty string.
909 */
910 static size_t
911check_str_len(char_u *str)
912{
913 SYSTEM_INFO si;
914 MEMORY_BASIC_INFORMATION mbi;
915 size_t length = 0;
916 size_t i;
917 const char *p;
918
919 /* get page size */
920 GetSystemInfo(&si);
921
922 /* get memory information */
923 if (VirtualQuery(str, &mbi, sizeof(mbi)))
924 {
925 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000926 long_u dwStr = (long_u)str;
927 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
929 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000930 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
932 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000933 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaar442b4222010-05-24 21:34:22 +0200935 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 p += pageLength, pageLength = si.dwPageSize)
937 for (i = 0; i < pageLength; ++i, ++length)
938 if (p[i] == NUL)
939 return length + 1;
940 }
941
942 return 0;
943}
944# endif
945
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200946/*
947 * Passed to do_in_runtimepath() to load a vim.ico file.
948 */
949 static void
950mch_icon_load_cb(char_u *fname, void *cookie)
951{
952 HANDLE *h = (HANDLE *)cookie;
953
954 *h = LoadImage(NULL,
955 fname,
956 IMAGE_ICON,
957 64,
958 64,
959 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
960}
961
962/*
963 * Try loading an icon file from 'runtimepath'.
964 */
965 int
966mch_icon_load(iconp)
967 HANDLE *iconp;
968{
969 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
970 FALSE, mch_icon_load_cb, iconp);
971}
972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 int
974mch_libcall(
975 char_u *libname,
976 char_u *funcname,
977 char_u *argstring, /* NULL when using a argint */
978 int argint,
979 char_u **string_result,/* NULL when using number_result */
980 int *number_result)
981{
982 HINSTANCE hinstLib;
983 MYSTRPROCSTR ProcAdd;
984 MYINTPROCSTR ProcAddI;
985 char_u *retval_str = NULL;
986 int retval_int = 0;
987 size_t len;
988
989 BOOL fRunTimeLinkSuccess = FALSE;
990
991 // Get a handle to the DLL module.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200992# ifdef WIN16
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 hinstLib = LoadLibrary(libname);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200994# else
995 hinstLib = vimLoadLib(libname);
996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
998 // If the handle is valid, try to get the function address.
999 if (hinstLib != NULL)
1000 {
1001#ifdef HAVE_TRY_EXCEPT
1002 __try
1003 {
1004#endif
1005 if (argstring != NULL)
1006 {
1007 /* Call with string argument */
1008 ProcAdd = (MYSTRPROCSTR) GetProcAddress(hinstLib, funcname);
1009 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
1010 {
1011 if (string_result == NULL)
1012 retval_int = ((MYSTRPROCINT)ProcAdd)(argstring);
1013 else
1014 retval_str = (ProcAdd)(argstring);
1015 }
1016 }
1017 else
1018 {
1019 /* Call with number argument */
1020 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, funcname);
1021 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
1022 {
1023 if (string_result == NULL)
1024 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
1025 else
1026 retval_str = (ProcAddI)(argint);
1027 }
1028 }
1029
1030 // Save the string before we free the library.
1031 // Assume that a "1" result is an illegal pointer.
1032 if (string_result == NULL)
1033 *number_result = retval_int;
1034 else if (retval_str != NULL
1035# ifdef WIN16
1036 && retval_str != (char_u *)1
1037 && retval_str != (char_u *)-1
1038 && !IsBadStringPtr(retval_str, INT_MAX)
1039 && (len = strlen(retval_str) + 1) > 0
1040# else
1041 && (len = check_str_len(retval_str)) > 0
1042# endif
1043 )
1044 {
1045 *string_result = lalloc((long_u)len, TRUE);
1046 if (*string_result != NULL)
1047 mch_memmove(*string_result, retval_str, len);
1048 }
1049
1050#ifdef HAVE_TRY_EXCEPT
1051 }
1052 __except(EXCEPTION_EXECUTE_HANDLER)
1053 {
1054 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
1055 RESETSTKOFLW();
1056 fRunTimeLinkSuccess = 0;
1057 }
1058#endif
1059
1060 // Free the DLL module.
1061 (void)FreeLibrary(hinstLib);
1062 }
1063
1064 if (!fRunTimeLinkSuccess)
1065 {
1066 EMSG2(_(e_libcall), funcname);
1067 return FAIL;
1068 }
1069
1070 return OK;
1071}
1072#endif
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074/*
1075 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1076 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001077/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 void
1079DumpPutS(
1080 const char *psz)
1081{
1082# ifdef MCH_WRITE_DUMP
1083 if (fdDump)
1084 {
1085 fputs(psz, fdDump);
1086 if (psz[strlen(psz) - 1] != '\n')
1087 fputc('\n', fdDump);
1088 fflush(fdDump);
1089 }
1090# endif
1091}
1092
1093#ifdef _DEBUG
1094
1095void __cdecl
1096Trace(
1097 char *pszFormat,
1098 ...)
1099{
1100 CHAR szBuff[2048];
1101 va_list args;
1102
1103 va_start(args, pszFormat);
1104 vsprintf(szBuff, pszFormat, args);
1105 va_end(args);
1106
1107 OutputDebugString(szBuff);
1108}
1109
1110#endif //_DEBUG
1111
Bram Moolenaar843ee412004-06-30 16:16:41 +00001112#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113# if defined(FEAT_TITLE) && defined(WIN3264)
1114extern HWND g_hWnd; /* This is in os_win32.c. */
1115# endif
1116
1117/*
1118 * Showing the printer dialog is tricky since we have no GUI
1119 * window to parent it. The following routines are needed to
1120 * get the window parenting and Z-order to work properly.
1121 */
1122 static void
1123GetConsoleHwnd(void)
1124{
1125# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1126
1127 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1128 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1129
1130 /* Skip if it's already set. */
1131 if (s_hwnd != 0)
1132 return;
1133
1134# if defined(FEAT_TITLE) && defined(WIN3264)
1135 /* Window handle may have been found by init code (Windows NT only) */
1136 if (g_hWnd != 0)
1137 {
1138 s_hwnd = g_hWnd;
1139 return;
1140 }
1141# endif
1142
1143 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1144
1145 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1146 pszOldWindowTitle,
1147 GetTickCount(),
1148 GetCurrentProcessId());
1149 SetConsoleTitle(pszNewWindowTitle);
1150 Sleep(40);
1151 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1152
1153 SetConsoleTitle(pszOldWindowTitle);
1154}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001155
1156/*
1157 * Console implementation of ":winpos".
1158 */
1159 int
1160mch_get_winpos(int *x, int *y)
1161{
1162 RECT rect;
1163
1164 GetConsoleHwnd();
1165 GetWindowRect(s_hwnd, &rect);
1166 *x = rect.left;
1167 *y = rect.top;
1168 return OK;
1169}
1170
1171/*
1172 * Console implementation of ":winpos x y".
1173 */
1174 void
1175mch_set_winpos(int x, int y)
1176{
1177 GetConsoleHwnd();
1178 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1179 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1180}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#endif
1182
1183#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1184
1185# ifdef WIN16
1186# define TEXT(a) a
1187# endif
1188/*=================================================================
1189 * Win32 printer stuff
1190 */
1191
1192static HFONT prt_font_handles[2][2][2];
1193static PRINTDLG prt_dlg;
1194static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1195static TEXTMETRIC prt_tm;
1196static int prt_line_height;
1197static int prt_number_width;
1198static int prt_left_margin;
1199static int prt_right_margin;
1200static int prt_top_margin;
1201static char_u szAppName[] = TEXT("VIM");
1202static HWND hDlgPrint;
1203static int *bUserAbort = NULL;
1204static char_u *prt_name = NULL;
1205
1206/* Defines which are also in vim.rc. */
1207#define IDC_BOX1 400
1208#define IDC_PRINTTEXT1 401
1209#define IDC_PRINTTEXT2 402
1210#define IDC_PROGRESS 403
1211
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001212#if !defined(FEAT_MBYTE) || defined(WIN16)
1213# define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s)
1214#else
1215 static BOOL
1216vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1217{
1218 WCHAR *wp = NULL;
1219 BOOL ret;
1220
1221 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1222 {
1223 wp = enc_to_utf16(s, NULL);
1224 }
1225 if (wp != NULL)
1226 {
1227 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1228 vim_free(wp);
1229 return ret;
1230 }
1231 return SetDlgItemText(hDlg, nIDDlgItem, s);
1232}
1233#endif
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235/*
1236 * Convert BGR to RGB for Windows GDI calls
1237 */
1238 static COLORREF
1239swap_me(COLORREF colorref)
1240{
1241 int temp;
1242 char *ptr = (char *)&colorref;
1243
1244 temp = *(ptr);
1245 *(ptr ) = *(ptr + 2);
1246 *(ptr + 2) = temp;
1247 return colorref;
1248}
1249
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001250/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001251#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001252# define PDP_RETVAL BOOL
1253#else
1254# define PDP_RETVAL INT_PTR
1255#endif
1256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001257/*ARGSUSED*/
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001258 static PDP_RETVAL CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1260{
1261#ifdef FEAT_GETTEXT
1262 NONCLIENTMETRICS nm;
1263 static HFONT hfont;
1264#endif
1265
1266 switch (message)
1267 {
1268 case WM_INITDIALOG:
1269#ifdef FEAT_GETTEXT
1270 nm.cbSize = sizeof(NONCLIENTMETRICS);
1271 if (SystemParametersInfo(
1272 SPI_GETNONCLIENTMETRICS,
1273 sizeof(NONCLIENTMETRICS),
1274 &nm,
1275 0))
1276 {
1277 char buff[MAX_PATH];
1278 int i;
1279
1280 /* Translate the dialog texts */
1281 hfont = CreateFontIndirect(&nm.lfMessageFont);
1282 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1283 {
1284 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1285 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001286 vimSetDlgItemText(hDlg,i, _(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 SendDlgItemMessage(hDlg, IDCANCEL,
1289 WM_SETFONT, (WPARAM)hfont, 1);
1290 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001291 vimSetDlgItemText(hDlg,IDCANCEL, _(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293#endif
1294 SetWindowText(hDlg, szAppName);
1295 if (prt_name != NULL)
1296 {
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001297 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (LPSTR)prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 vim_free(prt_name);
1299 prt_name = NULL;
1300 }
1301 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1302#ifndef FEAT_GUI
1303 BringWindowToTop(s_hwnd);
1304#endif
1305 return TRUE;
1306
1307 case WM_COMMAND:
1308 *bUserAbort = TRUE;
1309 EnableWindow(GetParent(hDlg), TRUE);
1310 DestroyWindow(hDlg);
1311 hDlgPrint = NULL;
1312#ifdef FEAT_GETTEXT
1313 DeleteObject(hfont);
1314#endif
1315 return TRUE;
1316 }
1317 return FALSE;
1318}
1319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001320/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 static BOOL CALLBACK
1322AbortProc(HDC hdcPrn, int iCode)
1323{
1324 MSG msg;
1325
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001326 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001328 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
1330 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001331 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 }
1333 }
1334 return !*bUserAbort;
1335}
1336
1337#ifndef FEAT_GUI
1338
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001339 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340PrintHookProc(
1341 HWND hDlg, // handle to dialog box
1342 UINT uiMsg, // message identifier
1343 WPARAM wParam, // message parameter
1344 LPARAM lParam // message parameter
1345 )
1346{
1347 HWND hwndOwner;
1348 RECT rc, rcDlg, rcOwner;
1349 PRINTDLG *pPD;
1350
1351 if (uiMsg == WM_INITDIALOG)
1352 {
1353 // Get the owner window and dialog box rectangles.
1354 if ((hwndOwner = GetParent(hDlg)) == NULL)
1355 hwndOwner = GetDesktopWindow();
1356
1357 GetWindowRect(hwndOwner, &rcOwner);
1358 GetWindowRect(hDlg, &rcDlg);
1359 CopyRect(&rc, &rcOwner);
1360
1361 // Offset the owner and dialog box rectangles so that
1362 // right and bottom values represent the width and
1363 // height, and then offset the owner again to discard
1364 // space taken up by the dialog box.
1365
1366 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1367 OffsetRect(&rc, -rc.left, -rc.top);
1368 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1369
1370 // The new position is the sum of half the remaining
1371 // space and the owner's original position.
1372
1373 SetWindowPos(hDlg,
1374 HWND_TOP,
1375 rcOwner.left + (rc.right / 2),
1376 rcOwner.top + (rc.bottom / 2),
1377 0, 0, // ignores size arguments
1378 SWP_NOSIZE);
1379
1380 /* tackle the printdlg copiesctrl problem */
1381 pPD = (PRINTDLG *)lParam;
1382 pPD->nCopies = (WORD)pPD->lCustData;
1383 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1384 /* Bring the window to top */
1385 BringWindowToTop(GetParent(hDlg));
1386 SetForegroundWindow(hDlg);
1387 }
1388
1389 return FALSE;
1390}
1391#endif
1392
1393 void
1394mch_print_cleanup(void)
1395{
1396 int pifItalic;
1397 int pifBold;
1398 int pifUnderline;
1399
1400 for (pifBold = 0; pifBold <= 1; pifBold++)
1401 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1402 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1403 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1404
1405 if (prt_dlg.hDC != NULL)
1406 DeleteDC(prt_dlg.hDC);
1407 if (!*bUserAbort)
1408 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1409}
1410
1411 static int
1412to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1413{
1414 int ret = 0;
1415 int u;
1416 int nr;
1417
1418 u = prt_get_unit(idx);
1419 if (u == PRT_UNIT_NONE)
1420 {
1421 u = PRT_UNIT_PERC;
1422 nr = def_number;
1423 }
1424 else
1425 nr = printer_opts[idx].number;
1426
1427 switch (u)
1428 {
1429 case PRT_UNIT_PERC:
1430 ret = (physsize * nr) / 100;
1431 break;
1432 case PRT_UNIT_INCH:
1433 ret = (nr * dpi);
1434 break;
1435 case PRT_UNIT_MM:
1436 ret = (nr * 10 * dpi) / 254;
1437 break;
1438 case PRT_UNIT_POINT:
1439 ret = (nr * 10 * dpi) / 720;
1440 break;
1441 }
1442
1443 if (ret < offset)
1444 return 0;
1445 else
1446 return ret - offset;
1447}
1448
1449 static int
1450prt_get_cpl(void)
1451{
1452 int hr;
1453 int phyw;
1454 int dvoff;
1455 int rev_offset;
1456 int dpi;
1457#ifdef WIN16
1458 POINT pagesize;
1459#endif
1460
1461 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1462 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1463
1464 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
1465#ifdef WIN16
1466 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1467 phyw = pagesize.x;
1468 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1469 dvoff = pagesize.x;
1470#else
1471 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1472 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
1473#endif
1474 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1475
1476 rev_offset = phyw - (dvoff + hr);
1477
1478 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1479 if (prt_use_number())
1480 {
1481 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1482 prt_left_margin += prt_number_width;
1483 }
1484 else
1485 prt_number_width = 0;
1486
1487 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1488 rev_offset, 5);
1489
1490 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1491}
1492
1493 static int
1494prt_get_lpp(void)
1495{
1496 int vr;
1497 int phyw;
1498 int dvoff;
1499 int rev_offset;
1500 int bottom_margin;
1501 int dpi;
1502#ifdef WIN16
1503 POINT pagesize;
1504#endif
1505
1506 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
1507#ifdef WIN16
1508 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1509 phyw = pagesize.y;
1510 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1511 dvoff = pagesize.y;
1512#else
1513 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1514 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
1515#endif
1516 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1517
1518 rev_offset = phyw - (dvoff + vr);
1519
1520 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1521
1522 /* adjust top margin if there is a header */
1523 prt_top_margin += prt_line_height * prt_header_height();
1524
1525 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1526 rev_offset, 5);
1527
1528 return (bottom_margin - prt_top_margin) / prt_line_height;
1529}
1530
1531 int
1532mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1533{
1534 static HGLOBAL stored_dm = NULL;
1535 static HGLOBAL stored_devn = NULL;
1536 static int stored_nCopies = 1;
1537 static int stored_nFlags = 0;
1538
1539 LOGFONT fLogFont;
1540 int pifItalic;
1541 int pifBold;
1542 int pifUnderline;
1543
1544 DEVMODE *mem;
1545 DEVNAMES *devname;
1546 int i;
1547
1548 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001549 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 prt_dlg.lStructSize = sizeof(PRINTDLG);
1551#ifndef FEAT_GUI
1552 GetConsoleHwnd(); /* get value of s_hwnd */
1553#endif
1554 prt_dlg.hwndOwner = s_hwnd;
1555 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1556 if (!forceit)
1557 {
1558 prt_dlg.hDevMode = stored_dm;
1559 prt_dlg.hDevNames = stored_devn;
1560 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1561#ifndef FEAT_GUI
1562 /*
1563 * Use hook to prevent console window being sent to back
1564 */
1565 prt_dlg.lpfnPrintHook = PrintHookProc;
1566 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1567#endif
1568 prt_dlg.Flags |= stored_nFlags;
1569 }
1570
1571 /*
1572 * If bang present, return default printer setup with no dialog
1573 * never show dialog if we are running over telnet
1574 */
1575 if (forceit
1576#ifndef FEAT_GUI
1577 || !term_console
1578#endif
1579 )
1580 {
1581 prt_dlg.Flags |= PD_RETURNDEFAULT;
1582#ifdef WIN3264
1583 /*
1584 * MSDN suggests setting the first parameter to WINSPOOL for
1585 * NT, but NULL appears to work just as well.
1586 */
1587 if (*p_pdev != NUL)
1588 prt_dlg.hDC = CreateDC(NULL, p_pdev, NULL, NULL);
1589 else
1590#endif
1591 {
1592 prt_dlg.Flags |= PD_RETURNDEFAULT;
1593 if (PrintDlg(&prt_dlg) == 0)
1594 goto init_fail_dlg;
1595 }
1596 }
1597 else if (PrintDlg(&prt_dlg) == 0)
1598 goto init_fail_dlg;
1599 else
1600 {
1601 /*
1602 * keep the previous driver context
1603 */
1604 stored_dm = prt_dlg.hDevMode;
1605 stored_devn = prt_dlg.hDevNames;
1606 stored_nFlags = prt_dlg.Flags;
1607 stored_nCopies = prt_dlg.nCopies;
1608 }
1609
1610 if (prt_dlg.hDC == NULL)
1611 {
1612 EMSG(_("E237: Printer selection failed"));
1613 mch_print_cleanup();
1614 return FALSE;
1615 }
1616
1617 /* Not all printer drivers report the support of color (or grey) in the
1618 * same way. Let's set has_color if there appears to be some way to print
1619 * more than B&W. */
1620 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1621 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1622 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1623 || i > 2 || i == -1);
1624
1625 /* Ensure all font styles are baseline aligned */
1626 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1627
1628 /*
1629 * On some windows systems the nCopies parameter is not
1630 * passed back correctly. It must be retrieved from the
1631 * hDevMode struct.
1632 */
1633 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1634 if (mem != NULL)
1635 {
1636#ifdef WIN3264
1637 if (mem->dmCopies != 1)
1638 stored_nCopies = mem->dmCopies;
1639#endif
1640 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1641 psettings->duplex = TRUE;
1642 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1643 psettings->has_color = TRUE;
1644 }
1645 GlobalUnlock(prt_dlg.hDevMode);
1646
1647 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1648 if (devname != 0)
1649 {
1650 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1651 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
1652 char_u *text = _("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001653#ifdef FEAT_MBYTE
1654 char_u *printer_name_orig = printer_name;
1655 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaarf191d552014-10-09 17:05:56 +02001657 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1658 {
1659 char_u *to_free = NULL;
1660 int maxlen;
1661
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001662 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1663 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001664 if (to_free != NULL)
1665 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001666 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001667 if (to_free != NULL)
1668 port_name = to_free;
1669 }
1670#endif
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001671 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1672 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (prt_name != NULL)
1674 wsprintf(prt_name, text, printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001675#ifdef FEAT_MBYTE
1676 if (printer_name != printer_name_orig)
1677 vim_free(printer_name);
1678 if (port_name != port_name_orig)
1679 vim_free(port_name);
1680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
1682 GlobalUnlock(prt_dlg.hDevNames);
1683
1684 /*
1685 * Initialise the font according to 'printfont'
1686 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001687 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001688 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
1690 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1691 mch_print_cleanup();
1692 return FALSE;
1693 }
1694
1695 for (pifBold = 0; pifBold <= 1; pifBold++)
1696 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1697 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1698 {
1699 fLogFont.lfWeight = boldface[pifBold];
1700 fLogFont.lfItalic = pifItalic;
1701 fLogFont.lfUnderline = pifUnderline;
1702 prt_font_handles[pifBold][pifItalic][pifUnderline]
1703 = CreateFontIndirect(&fLogFont);
1704 }
1705
1706 SetBkMode(prt_dlg.hDC, OPAQUE);
1707 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1708
1709 /*
1710 * Fill in the settings struct
1711 */
1712 psettings->chars_per_line = prt_get_cpl();
1713 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001714 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1715 {
1716 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1717 ? prt_dlg.nCopies : 1;
1718 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1719 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001721 if (psettings->n_collated_copies == 0)
1722 psettings->n_collated_copies = 1;
1723
1724 if (psettings->n_uncollated_copies == 0)
1725 psettings->n_uncollated_copies = 1;
1726 } else {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731 psettings->jobname = jobname;
1732
1733 return TRUE;
1734
1735init_fail_dlg:
1736 {
1737 DWORD err = CommDlgExtendedError();
1738
1739 if (err)
1740 {
1741#ifdef WIN16
1742 char buf[20];
1743
1744 sprintf(buf, "%ld", err);
1745 EMSG2(_("E238: Print error: %s"), buf);
1746#else
1747 char_u *buf;
1748
1749 /* I suspect FormatMessage() doesn't work for values returned by
1750 * CommDlgExtendedError(). What does? */
1751 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1752 FORMAT_MESSAGE_FROM_SYSTEM |
1753 FORMAT_MESSAGE_IGNORE_INSERTS,
1754 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1755 EMSG2(_("E238: Print error: %s"),
1756 buf == NULL ? (char_u *)_("Unknown") : buf);
1757 LocalFree((LPVOID)(buf));
1758#endif
1759 }
1760 else
1761 msg_clr_eos(); /* Maybe canceled */
1762
1763 mch_print_cleanup();
1764 return FALSE;
1765 }
1766}
1767
1768
1769 int
1770mch_print_begin(prt_settings_T *psettings)
1771{
1772 int ret;
1773 static DOCINFO di;
1774 char szBuffer[300];
1775
1776 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1777 prt_dlg.hwndOwner, PrintDlgProc);
1778#ifdef WIN16
1779 Escape(prt_dlg.hDC, SETABORTPROC, 0, (LPSTR)AbortProc, NULL);
1780#else
1781 SetAbortProc(prt_dlg.hDC, AbortProc);
1782#endif
1783 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001784 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (LPSTR)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001786 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 di.cbSize = sizeof(DOCINFO);
1788 di.lpszDocName = psettings->jobname;
1789 ret = StartDoc(prt_dlg.hDC, &di);
1790
1791#ifdef FEAT_GUI
1792 /* Give focus back to main window (when using MDI). */
1793 SetFocus(s_hwnd);
1794#endif
1795
1796 return (ret > 0);
1797}
1798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001799/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 void
1801mch_print_end(prt_settings_T *psettings)
1802{
1803 EndDoc(prt_dlg.hDC);
1804 if (!*bUserAbort)
1805 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1806}
1807
1808 int
1809mch_print_end_page(void)
1810{
1811 return (EndPage(prt_dlg.hDC) > 0);
1812}
1813
1814 int
1815mch_print_begin_page(char_u *msg)
1816{
1817 if (msg != NULL)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001818 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, (LPSTR)msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 return (StartPage(prt_dlg.hDC) > 0);
1820}
1821
1822 int
1823mch_print_blank_page(void)
1824{
1825 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1826}
1827
1828static int prt_pos_x = 0;
1829static int prt_pos_y = 0;
1830
1831 void
1832mch_print_start_line(margin, page_line)
1833 int margin;
1834 int page_line;
1835{
1836 if (margin)
1837 prt_pos_x = -prt_number_width;
1838 else
1839 prt_pos_x = 0;
1840 prt_pos_y = page_line * prt_line_height
1841 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1842}
1843
1844 int
1845mch_print_text_out(char_u *p, int len)
1846{
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001847#if defined(FEAT_PROPORTIONAL_FONTS) || (defined(FEAT_MBYTE) && !defined(WIN16))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 SIZE sz;
1849#endif
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001850#if defined(FEAT_MBYTE) && !defined(WIN16)
1851 WCHAR *wp = NULL;
1852 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001854 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1855 {
1856 wp = enc_to_utf16(p, &wlen);
1857 }
1858 if (wp != NULL)
1859 {
1860 int ret = FALSE;
1861
1862 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1863 prt_pos_y + prt_top_margin, wp, wlen);
1864 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1865 vim_free(wp);
1866 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1867 /* This is wrong when printing spaces for a TAB. */
1868 if (p[len] != NUL)
1869 {
1870 wlen = MB_PTR2LEN(p + len);
1871 wp = enc_to_utf16(p + len, &wlen);
1872 if (wp != NULL)
1873 {
1874 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1875 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1876 vim_free(wp);
1877 }
1878 }
1879 return ret;
1880 }
1881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1883 prt_pos_y + prt_top_margin, p, len);
1884#ifndef FEAT_PROPORTIONAL_FONTS
1885 prt_pos_x += len * prt_tm.tmAveCharWidth;
1886 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1887 + prt_tm.tmOverhang > prt_right_margin);
1888#else
1889# ifdef WIN16
1890 GetTextExtentPoint(prt_dlg.hDC, p, len, &sz);
1891# else
1892 GetTextExtentPoint32(prt_dlg.hDC, p, len, &sz);
1893# endif
1894 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1895 /* This is wrong when printing spaces for a TAB. */
1896 if (p[len] == NUL)
1897 return FALSE;
1898# ifdef WIN16
1899 GetTextExtentPoint(prt_dlg.hDC, p + len, 1, &sz);
1900# else
1901 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
1902# endif
1903 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1904#endif
1905}
1906
1907 void
1908mch_print_set_font(int iBold, int iItalic, int iUnderline)
1909{
1910 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1911}
1912
1913 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001914mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001916 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1917 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 /*
1919 * With a white background we can draw characters transparent, which is
1920 * good for italic characters that overlap to the next char cell.
1921 */
1922 if (bgcol == 0xffffffUL)
1923 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1924 else
1925 SetBkMode(prt_dlg.hDC, OPAQUE);
1926}
1927
1928 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001929mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001931 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1932 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933}
1934
1935#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1936
Bram Moolenaar58d98232005-07-23 22:25:46 +00001937
1938
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001940# ifndef PROTO
1941# include <shlobj.h>
1942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
1944/*
1945 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1946 * to and return that name in allocated memory.
1947 * Otherwise NULL is returned.
1948 */
1949 char_u *
1950mch_resolve_shortcut(char_u *fname)
1951{
1952 HRESULT hr;
1953 IShellLink *psl = NULL;
1954 IPersistFile *ppf = NULL;
1955 OLECHAR wsz[MAX_PATH];
1956 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001957 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 char_u *rfname = NULL;
1959 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001960# ifdef FEAT_MBYTE
1961 IShellLinkW *pslw = NULL;
1962 WIN32_FIND_DATAW ffdw; // we get those free of charge
1963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
1965 /* Check if the file name ends in ".lnk". Avoid calling
1966 * CoCreateInstance(), it's quite slow. */
1967 if (fname == NULL)
1968 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001969 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1971 return rfname;
1972
1973 CoInitialize(NULL);
1974
Bram Moolenaar604729e2013-08-30 16:44:19 +02001975# ifdef FEAT_MBYTE
1976 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1977 {
1978 // create a link manager object and request its interface
1979 hr = CoCreateInstance(
1980 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1981 &IID_IShellLinkW, (void**)&pslw);
1982 if (hr == S_OK)
1983 {
1984 WCHAR *p = enc_to_utf16(fname, NULL);
1985
1986 if (p != NULL)
1987 {
1988 // Get a pointer to the IPersistFile interface.
1989 hr = pslw->lpVtbl->QueryInterface(
1990 pslw, &IID_IPersistFile, (void**)&ppf);
1991 if (hr != S_OK)
1992 goto shortcut_errorw;
1993
1994 // "load" the name and resolve the link
1995 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1996 if (hr != S_OK)
1997 goto shortcut_errorw;
1998# if 0 // This makes Vim wait a long time if the target does not exist.
1999 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
2000 if (hr != S_OK)
2001 goto shortcut_errorw;
2002# endif
2003
2004 // Get the path to the link target.
2005 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
2006 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
2007 if (hr == S_OK && wsz[0] != NUL)
2008 rfname = utf16_to_enc(wsz, NULL);
2009
2010shortcut_errorw:
2011 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01002012 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02002013 }
2014 }
2015 /* Retry with non-wide function (for Windows 98). */
2016 }
2017# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 // create a link manager object and request its interface
2019 hr = CoCreateInstance(
2020 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2021 &IID_IShellLink, (void**)&psl);
2022 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002023 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
2025 // Get a pointer to the IPersistFile interface.
2026 hr = psl->lpVtbl->QueryInterface(
2027 psl, &IID_IPersistFile, (void**)&ppf);
2028 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002029 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030
2031 // full path string must be in Unicode.
2032 MultiByteToWideChar(CP_ACP, 0, fname, -1, wsz, MAX_PATH);
2033
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002034 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
2036 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002037 goto shortcut_end;
2038# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
2040 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002041 goto shortcut_end;
2042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
2044 // Get the path to the link target.
2045 ZeroMemory(buf, MAX_PATH);
2046 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
2047 if (hr == S_OK && buf[0] != NUL)
2048 rfname = vim_strsave(buf);
2049
Bram Moolenaar604729e2013-08-30 16:44:19 +02002050shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 // Release all interface pointers (both belong to the same object)
2052 if (ppf != NULL)
2053 ppf->lpVtbl->Release(ppf);
2054 if (psl != NULL)
2055 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02002056# ifdef FEAT_MBYTE
2057 if (pslw != NULL)
2058 pslw->lpVtbl->Release(pslw);
2059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
2061 CoUninitialize();
2062 return rfname;
2063}
2064#endif
2065
2066#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
2067/*
2068 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
2069 */
2070 void
2071win32_set_foreground()
2072{
2073# ifndef FEAT_GUI
2074 GetConsoleHwnd(); /* get value of s_hwnd */
2075# endif
2076 if (s_hwnd != 0)
2077 SetForegroundWindow(s_hwnd);
2078}
2079#endif
2080
2081#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2082/*
2083 * Client-server code for Vim
2084 *
2085 * Originally written by Paul Moore
2086 */
2087
2088/* In order to handle inter-process messages, we need to have a window. But
2089 * the functions in this module can be called before the main GUI window is
2090 * created (and may also be called in the console version, where there is no
2091 * GUI window at all).
2092 *
2093 * So we create a hidden window, and arrange to destroy it on exit.
2094 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002095HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096
2097#define VIM_CLASSNAME "VIM_MESSAGES"
2098#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2099
2100/* Communication is via WM_COPYDATA messages. The message type is send in
2101 * the dwData parameter. Types are defined here. */
2102#define COPYDATA_KEYS 0
2103#define COPYDATA_REPLY 1
2104#define COPYDATA_EXPR 10
2105#define COPYDATA_RESULT 11
2106#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002107#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
2109/* This is a structure containing a server HWND and its name. */
2110struct server_id
2111{
2112 HWND hwnd;
2113 char_u *name;
2114};
2115
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002116/* Last received 'encoding' that the client uses. */
2117static char_u *client_enc = NULL;
2118
2119/*
2120 * Tell the other side what encoding we are using.
2121 * Errors are ignored.
2122 */
2123 static void
2124serverSendEnc(HWND target)
2125{
2126 COPYDATASTRUCT data;
2127
2128 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002129#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002130 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002131 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002132#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002133 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002134 data.lpData = "latin1";
2135#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002136 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2137 (LPARAM)(&data));
2138}
2139
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140/*
2141 * Clean up on exit. This destroys the hidden message window.
2142 */
2143 static void
2144#ifdef __BORLANDC__
2145 _RTLENTRYF
2146#endif
2147CleanUpMessaging(void)
2148{
2149 if (message_window != 0)
2150 {
2151 DestroyWindow(message_window);
2152 message_window = 0;
2153 }
2154}
2155
2156static int save_reply(HWND server, char_u *reply, int expr);
2157
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 * The window procedure for the hidden message window.
2160 * It handles callback messages and notifications from servers.
2161 * In order to process these messages, it is necessary to run a
2162 * message loop. Code which may run before the main message loop
2163 * is started (in the GUI) is careful to pump messages when it needs
2164 * to. Features which require message delivery during normal use will
2165 * not work in the console version - this basically means those
2166 * features which allow Vim to act as a server, rather than a client.
2167 */
2168 static LRESULT CALLBACK
2169Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2170{
2171 if (msg == WM_COPYDATA)
2172 {
2173 /* This is a message from another Vim. The dwData member of the
2174 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002175 * COPYDATA_ENCODING:
2176 * The encoding that the client uses. Following messages will
2177 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 * COPYDATA_KEYS:
2179 * A key sequence. We are a server, and a client wants these keys
2180 * adding to the input queue.
2181 * COPYDATA_REPLY:
2182 * A reply. We are a client, and a server has sent this message
2183 * in response to a request. (server2client())
2184 * COPYDATA_EXPR:
2185 * An expression. We are a server, and a client wants us to
2186 * evaluate this expression.
2187 * COPYDATA_RESULT:
2188 * A reply. We are a client, and a server has sent this message
2189 * in response to a COPYDATA_EXPR.
2190 * COPYDATA_ERROR_RESULT:
2191 * A reply. We are a client, and a server has sent this message
2192 * in response to a COPYDATA_EXPR that failed to evaluate.
2193 */
2194 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2195 HWND sender = (HWND)wParam;
2196 COPYDATASTRUCT reply;
2197 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002199 char_u *str;
2200 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
2202 switch (data->dwData)
2203 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002204 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002205# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002206 /* Remember the encoding that the client uses. */
2207 vim_free(client_enc);
2208 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002209# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002210 return 1;
2211
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 case COPYDATA_KEYS:
2213 /* Remember who sent this, for <client> */
2214 clientWindow = sender;
2215
2216 /* Add the received keys to the input buffer. The loop waiting
2217 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002218 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2219 server_to_input_buf(str);
2220 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
2222# ifdef FEAT_GUI
2223 /* Wake up the main GUI loop. */
2224 if (s_hwnd != 0)
2225 PostMessage(s_hwnd, WM_NULL, 0, 0);
2226# endif
2227 return 1;
2228
2229 case COPYDATA_EXPR:
2230 /* Remember who sent this, for <client> */
2231 clientWindow = sender;
2232
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002233 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2234 res = eval_client_expr_to_string(str);
2235 vim_free(tofree);
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (res == NULL)
2238 {
2239 res = vim_strsave(_(e_invexprmsg));
2240 reply.dwData = COPYDATA_ERROR_RESULT;
2241 }
2242 else
2243 reply.dwData = COPYDATA_RESULT;
2244 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002245 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002247 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002248 retval = (int)SendMessage(sender, WM_COPYDATA,
2249 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 vim_free(res);
2251 return retval;
2252
2253 case COPYDATA_REPLY:
2254 case COPYDATA_RESULT:
2255 case COPYDATA_ERROR_RESULT:
2256 if (data->lpData != NULL)
2257 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002258 str = serverConvert(client_enc, (char_u *)data->lpData,
2259 &tofree);
2260 if (tofree == NULL)
2261 str = vim_strsave(str);
2262 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 (data->dwData == COPYDATA_REPLY ? 0 :
2264 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002265 2))) == FAIL)
2266 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002268 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002270 char_u winstr[30];
2271
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002272 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002273 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 TRUE, curbuf);
2275 }
2276#endif
2277 }
2278 return 1;
2279 }
2280
2281 return 0;
2282 }
2283
2284 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2285 {
2286 /* When the message window is activated (brought to the foreground),
2287 * this actually applies to the text window. */
2288#ifndef FEAT_GUI
2289 GetConsoleHwnd(); /* get value of s_hwnd */
2290#endif
2291 if (s_hwnd != 0)
2292 {
2293 SetForegroundWindow(s_hwnd);
2294 return 0;
2295 }
2296 }
2297
2298 return DefWindowProc(hwnd, msg, wParam, lParam);
2299}
2300
2301/*
2302 * Initialise the message handling process. This involves creating a window
2303 * to handle messages - the window will not be visible.
2304 */
2305 void
2306serverInitMessaging(void)
2307{
2308 WNDCLASS wndclass;
2309 HINSTANCE s_hinst;
2310
2311 /* Clean up on exit */
2312 atexit(CleanUpMessaging);
2313
2314 /* Register a window class - we only really care
2315 * about the window procedure
2316 */
2317 s_hinst = (HINSTANCE)GetModuleHandle(0);
2318 wndclass.style = 0;
2319 wndclass.lpfnWndProc = Messaging_WndProc;
2320 wndclass.cbClsExtra = 0;
2321 wndclass.cbWndExtra = 0;
2322 wndclass.hInstance = s_hinst;
2323 wndclass.hIcon = NULL;
2324 wndclass.hCursor = NULL;
2325 wndclass.hbrBackground = NULL;
2326 wndclass.lpszMenuName = NULL;
2327 wndclass.lpszClassName = VIM_CLASSNAME;
2328 RegisterClass(&wndclass);
2329
2330 /* Create the message window. It will be hidden, so the details don't
2331 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2332 * focus from gvim. */
2333 message_window = CreateWindow(VIM_CLASSNAME, "",
2334 WS_POPUPWINDOW | WS_CAPTION,
2335 CW_USEDEFAULT, CW_USEDEFAULT,
2336 100, 100, NULL, NULL,
2337 s_hinst, NULL);
2338}
2339
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002340/* Used by serverSendToVim() to find an alternate server name. */
2341static char_u *altname_buf_ptr = NULL;
2342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343/*
2344 * Get the title of the window "hwnd", which is the Vim server name, in
2345 * "name[namelen]" and return the length.
2346 * Returns zero if window "hwnd" is not a Vim server.
2347 */
2348 static int
2349getVimServerName(HWND hwnd, char *name, int namelen)
2350{
2351 int len;
2352 char buffer[VIM_CLASSNAME_LEN + 1];
2353
2354 /* Ignore windows which aren't Vim message windows */
2355 len = GetClassName(hwnd, buffer, sizeof(buffer));
2356 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2357 return 0;
2358
2359 /* Get the title of the window */
2360 return GetWindowText(hwnd, name, namelen);
2361}
2362
2363 static BOOL CALLBACK
2364enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2365{
2366 struct server_id *id = (struct server_id *)lparam;
2367 char server[MAX_PATH];
2368
2369 /* Get the title of the window */
2370 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2371 return TRUE;
2372
2373 /* If this is the server we're looking for, return its HWND */
2374 if (STRICMP(server, id->name) == 0)
2375 {
2376 id->hwnd = hwnd;
2377 return FALSE;
2378 }
2379
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002380 /* If we are looking for an alternate server, remember this name. */
2381 if (altname_buf_ptr != NULL
2382 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2383 && vim_isdigit(server[STRLEN(id->name)]))
2384 {
2385 STRCPY(altname_buf_ptr, server);
2386 altname_buf_ptr = NULL; /* don't use another name */
2387 }
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 /* Otherwise, keep looking */
2390 return TRUE;
2391}
2392
2393 static BOOL CALLBACK
2394enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2395{
2396 garray_T *ga = (garray_T *)lparam;
2397 char server[MAX_PATH];
2398
2399 /* Get the title of the window */
2400 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2401 return TRUE;
2402
2403 /* Add the name to the list */
2404 ga_concat(ga, server);
2405 ga_concat(ga, "\n");
2406 return TRUE;
2407}
2408
2409 static HWND
2410findServer(char_u *name)
2411{
2412 struct server_id id;
2413
2414 id.name = name;
2415 id.hwnd = 0;
2416
2417 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2418
2419 return id.hwnd;
2420}
2421
2422 void
2423serverSetName(char_u *name)
2424{
2425 char_u *ok_name;
2426 HWND hwnd = 0;
2427 int i = 0;
2428 char_u *p;
2429
2430 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002431 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
2433 STRCPY(ok_name, name);
2434 p = ok_name + STRLEN(name);
2435
2436 for (;;)
2437 {
2438 /* This is inefficient - we're doing an EnumWindows loop for each
2439 * possible name. It would be better to grab all names in one go,
2440 * and scan the list each time...
2441 */
2442 hwnd = findServer(ok_name);
2443 if (hwnd == 0)
2444 break;
2445
2446 ++i;
2447 if (i >= 1000)
2448 break;
2449
2450 sprintf((char *)p, "%d", i);
2451 }
2452
2453 if (hwnd != 0)
2454 vim_free(ok_name);
2455 else
2456 {
2457 /* Remember the name */
2458 serverName = ok_name;
2459#ifdef FEAT_TITLE
2460 need_maketitle = TRUE; /* update Vim window title later */
2461#endif
2462
2463 /* Update the message window title */
2464 SetWindowText(message_window, ok_name);
2465
2466#ifdef FEAT_EVAL
2467 /* Set the servername variable */
2468 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2469#endif
2470 }
2471}
2472
2473 char_u *
2474serverGetVimNames(void)
2475{
2476 garray_T ga;
2477
2478 ga_init2(&ga, 1, 100);
2479
2480 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002481 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483 return ga.ga_data;
2484}
2485
2486 int
2487serverSendReply(name, reply)
2488 char_u *name; /* Where to send. */
2489 char_u *reply; /* What to send. */
2490{
2491 HWND target;
2492 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002493 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
2495 /* The "name" argument is a magic cookie obtained from expand("<client>").
2496 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2497 * value of the client's message window HWND.
2498 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002499 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (n == 0)
2501 return -1;
2502
2503 target = (HWND)n;
2504 if (!IsWindow(target))
2505 return -1;
2506
2507 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002508 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 data.lpData = reply;
2510
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002511 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2513 (LPARAM)(&data)))
2514 return 0;
2515
2516 return -1;
2517}
2518
2519 int
2520serverSendToVim(name, cmd, result, ptarget, asExpr, silent)
2521 char_u *name; /* Where to send. */
2522 char_u *cmd; /* What to send. */
2523 char_u **result; /* Result of eval'ed expression */
2524 void *ptarget; /* HWND of server */
2525 int asExpr; /* Expression or keys? */
2526 int silent; /* don't complain about no server */
2527{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002528 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 COPYDATASTRUCT data;
2530 char_u *retval = NULL;
2531 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002532 char_u altname_buf[MAX_PATH];
2533
2534 /* If the server name does not end in a digit then we look for an
2535 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2536 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2537 altname_buf_ptr = altname_buf;
2538 altname_buf[0] = NUL;
2539 target = findServer(name);
2540 altname_buf_ptr = NULL;
2541 if (target == 0 && altname_buf[0] != NUL)
2542 /* Use another server name we found. */
2543 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
2545 if (target == 0)
2546 {
2547 if (!silent)
2548 EMSG2(_(e_noserver), name);
2549 return -1;
2550 }
2551
2552 if (ptarget)
2553 *(HWND *)ptarget = target;
2554
2555 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002556 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 data.lpData = cmd;
2558
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002559 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2561 (LPARAM)(&data)) == 0)
2562 return -1;
2563
2564 if (asExpr)
2565 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2566
2567 if (result == NULL)
2568 vim_free(retval);
2569 else
2570 *result = retval; /* Caller assumes responsibility for freeing */
2571
2572 return retcode;
2573}
2574
2575/*
2576 * Bring the server to the foreground.
2577 */
2578 void
2579serverForeground(name)
2580 char_u *name;
2581{
2582 HWND target = findServer(name);
2583
2584 if (target != 0)
2585 SetForegroundWindow(target);
2586}
2587
2588/* Replies from server need to be stored until the client picks them up via
2589 * remote_read(). So we maintain a list of server-id/reply pairs.
2590 * Note that there could be multiple replies from one server pending if the
2591 * client is slow picking them up.
2592 * We just store the replies in a simple list. When we remove an entry, we
2593 * move list entries down to fill the gap.
2594 * The server ID is simply the HWND.
2595 */
2596typedef struct
2597{
2598 HWND server; /* server window */
2599 char_u *reply; /* reply string */
2600 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002601} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
2603static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2604
2605#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2606#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607
2608/* Flag which is used to wait for a reply */
2609static int reply_received = 0;
2610
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002611/*
2612 * Store a reply. "reply" must be allocated memory (or NULL).
2613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 static int
2615save_reply(HWND server, char_u *reply, int expr)
2616{
2617 reply_T *rep;
2618
2619 if (ga_grow(&reply_list, 1) == FAIL)
2620 return FAIL;
2621
2622 rep = REPLY_ITEM(REPLY_COUNT);
2623 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002624 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 rep->expr_result = expr;
2626 if (rep->reply == NULL)
2627 return FAIL;
2628
2629 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 reply_received = 1;
2631 return OK;
2632}
2633
2634/*
2635 * Get a reply from server "server".
2636 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2637 * server2client() message.
2638 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2639 * If "remove" is TRUE, consume the message, the caller must free it then.
2640 * if "wait" is TRUE block until a message arrives (or the server exits).
2641 */
2642 char_u *
2643serverGetReply(HWND server, int *expr_res, int remove, int wait)
2644{
2645 int i;
2646 char_u *reply;
2647 reply_T *rep;
2648
2649 /* When waiting, loop until the message waiting for is received. */
2650 for (;;)
2651 {
2652 /* Reset this here, in case a message arrives while we are going
2653 * through the already received messages. */
2654 reply_received = 0;
2655
2656 for (i = 0; i < REPLY_COUNT; ++i)
2657 {
2658 rep = REPLY_ITEM(i);
2659 if (rep->server == server
2660 && ((rep->expr_result != 0) == (expr_res != NULL)))
2661 {
2662 /* Save the values we've found for later */
2663 reply = rep->reply;
2664 if (expr_res != NULL)
2665 *expr_res = rep->expr_result == 1 ? 0 : -1;
2666
2667 if (remove)
2668 {
2669 /* Move the rest of the list down to fill the gap */
2670 mch_memmove(rep, rep + 1,
2671 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2672 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 }
2674
2675 /* Return the reply to the caller, who takes on responsibility
2676 * for freeing it if "remove" is TRUE. */
2677 return reply;
2678 }
2679 }
2680
2681 /* If we got here, we didn't find a reply. Return immediately if the
2682 * "wait" parameter isn't set. */
2683 if (!wait)
2684 break;
2685
2686 /* We need to wait for a reply. Enter a message loop until the
2687 * "reply_received" flag gets set. */
2688
2689 /* Loop until we receive a reply */
2690 while (reply_received == 0)
2691 {
2692 /* Wait for a SendMessage() call to us. This could be the reply
2693 * we are waiting for. Use a timeout of a second, to catch the
2694 * situation that the server died unexpectedly. */
2695 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2696
2697 /* If the server has died, give up */
2698 if (!IsWindow(server))
2699 return NULL;
2700
2701 serverProcessPendingMessages();
2702 }
2703 }
2704
2705 return NULL;
2706}
2707
2708/*
2709 * Process any messages in the Windows message queue.
2710 */
2711 void
2712serverProcessPendingMessages(void)
2713{
2714 MSG msg;
2715
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002716 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
2718 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002719 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721}
2722
2723#endif /* FEAT_CLIENTSERVER */
2724
2725#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2726 || defined(PROTO)
2727
2728struct charset_pair
2729{
2730 char *name;
2731 BYTE charset;
2732};
2733
2734static struct charset_pair
2735charset_pairs[] =
2736{
2737 {"ANSI", ANSI_CHARSET},
2738 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2739 {"DEFAULT", DEFAULT_CHARSET},
2740 {"HANGEUL", HANGEUL_CHARSET},
2741 {"OEM", OEM_CHARSET},
2742 {"SHIFTJIS", SHIFTJIS_CHARSET},
2743 {"SYMBOL", SYMBOL_CHARSET},
2744#ifdef WIN3264
2745 {"ARABIC", ARABIC_CHARSET},
2746 {"BALTIC", BALTIC_CHARSET},
2747 {"EASTEUROPE", EASTEUROPE_CHARSET},
2748 {"GB2312", GB2312_CHARSET},
2749 {"GREEK", GREEK_CHARSET},
2750 {"HEBREW", HEBREW_CHARSET},
2751 {"JOHAB", JOHAB_CHARSET},
2752 {"MAC", MAC_CHARSET},
2753 {"RUSSIAN", RUSSIAN_CHARSET},
2754 {"THAI", THAI_CHARSET},
2755 {"TURKISH", TURKISH_CHARSET},
2756# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
2757 && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
2758 {"VIETNAMESE", VIETNAMESE_CHARSET},
2759# endif
2760#endif
2761 {NULL, 0}
2762};
2763
2764/*
2765 * Convert a charset ID to a name.
2766 * Return NULL when not recognized.
2767 */
2768 char *
2769charset_id2name(int id)
2770{
2771 struct charset_pair *cp;
2772
2773 for (cp = charset_pairs; cp->name != NULL; ++cp)
2774 if ((BYTE)id == cp->charset)
2775 break;
2776 return cp->name;
2777}
2778
2779static const LOGFONT s_lfDefault =
2780{
2781 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2782 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2783 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2784 "Fixedsys" /* see _ReadVimIni */
2785};
2786
2787/* Initialise the "current height" to -12 (same as s_lfDefault) just
2788 * in case the user specifies a font in "guifont" with no size before a font
2789 * with an explicit size has been set. This defaults the size to this value
2790 * (-12 equates to roughly 9pt).
2791 */
2792int current_font_height = -12; /* also used in gui_w48.c */
2793
2794/* Convert a string representing a point size into pixels. The string should
2795 * be a positive decimal number, with an optional decimal point (eg, "12", or
2796 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2797 * character is stored in *end. The flag "vertical" says whether this
2798 * calculation is for a vertical (height) size or a horizontal (width) one.
2799 */
2800 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002801points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802{
2803 int pixels;
2804 int points = 0;
2805 int divisor = 0;
2806 HWND hwnd = (HWND)0;
2807 HDC hdc;
2808 HDC printer_dc = (HDC)pprinter_dc;
2809
2810 while (*str != NUL)
2811 {
2812 if (*str == '.' && divisor == 0)
2813 {
2814 /* Start keeping a divisor, for later */
2815 divisor = 1;
2816 }
2817 else
2818 {
2819 if (!VIM_ISDIGIT(*str))
2820 break;
2821
2822 points *= 10;
2823 points += *str - '0';
2824 divisor *= 10;
2825 }
2826 ++str;
2827 }
2828
2829 if (divisor == 0)
2830 divisor = 1;
2831
2832 if (printer_dc == NULL)
2833 {
2834 hwnd = GetDesktopWindow();
2835 hdc = GetWindowDC(hwnd);
2836 }
2837 else
2838 hdc = printer_dc;
2839
2840 pixels = MulDiv(points,
2841 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2842 72 * divisor);
2843
2844 if (printer_dc == NULL)
2845 ReleaseDC(hwnd, hdc);
2846
2847 *end = str;
2848 return pixels;
2849}
2850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 static int CALLBACK
2853font_enumproc(
2854 ENUMLOGFONT *elf,
2855 NEWTEXTMETRIC *ntm,
2856 int type,
2857 LPARAM lparam)
2858{
2859 /* Return value:
2860 * 0 = terminate now (monospace & ANSI)
2861 * 1 = continue, still no luck...
2862 * 2 = continue, but we have an acceptable LOGFONT
2863 * (monospace, not ANSI)
2864 * We use these values, as EnumFontFamilies returns 1 if the
2865 * callback function is never called. So, we check the return as
2866 * 0 = perfect, 2 = OK, 1 = no good...
2867 * It's not pretty, but it works!
2868 */
2869
2870 LOGFONT *lf = (LOGFONT *)(lparam);
2871
2872#ifndef FEAT_PROPORTIONAL_FONTS
2873 /* Ignore non-monospace fonts without further ado */
2874 if ((ntm->tmPitchAndFamily & 1) != 0)
2875 return 1;
2876#endif
2877
2878 /* Remember this LOGFONT as a "possible" */
2879 *lf = elf->elfLogFont;
2880
2881 /* Terminate the scan as soon as we find an ANSI font */
2882 if (lf->lfCharSet == ANSI_CHARSET
2883 || lf->lfCharSet == OEM_CHARSET
2884 || lf->lfCharSet == DEFAULT_CHARSET)
2885 return 0;
2886
2887 /* Continue the scan - we have a non-ANSI font */
2888 return 2;
2889}
2890
2891 static int
2892init_logfont(LOGFONT *lf)
2893{
2894 int n;
2895 HWND hwnd = GetDesktopWindow();
2896 HDC hdc = GetWindowDC(hwnd);
2897
2898 n = EnumFontFamilies(hdc,
2899 (LPCSTR)lf->lfFaceName,
2900 (FONTENUMPROC)font_enumproc,
2901 (LPARAM)lf);
2902
2903 ReleaseDC(hwnd, hdc);
2904
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002905 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 if (n == 1)
2907 return FAIL;
2908
2909 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2910 * font - get_logfont() sets bold, italic, etc based on the user's
2911 * input.
2912 */
2913 lf->lfHeight = current_font_height;
2914 lf->lfWidth = 0;
2915 lf->lfItalic = FALSE;
2916 lf->lfUnderline = FALSE;
2917 lf->lfStrikeOut = FALSE;
2918 lf->lfWeight = FW_NORMAL;
2919
2920 /* Return success */
2921 return OK;
2922}
2923
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002924/*
2925 * Get font info from "name" into logfont "lf".
2926 * Return OK for a valid name, FAIL otherwise.
2927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 int
2929get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002930 LOGFONT *lf,
2931 char_u *name,
2932 HDC printer_dc,
2933 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
2935 char_u *p;
2936 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002937 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002939#ifdef FEAT_MBYTE
2940 char_u *acpname = NULL;
2941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
2943 *lf = s_lfDefault;
2944 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002945 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002947#ifdef FEAT_MBYTE
2948 /* Convert 'name' from 'encoding' to the current codepage, because
2949 * lf->lfFaceName uses the current codepage.
2950 * TODO: Use Wide APIs instead of ANSI APIs. */
2951 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2952 {
2953 int len;
Bram Moolenaara0844a12014-03-19 12:37:22 +01002954 enc_to_acp(name, (int)strlen(name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002955 name = acpname;
2956 }
2957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 if (STRCMP(name, "*") == 0)
2959 {
2960#if defined(FEAT_GUI_W32)
2961 CHOOSEFONT cf;
2962 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002963 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 cf.lStructSize = sizeof(cf);
2965 cf.hwndOwner = s_hwnd;
2966 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2967 if (lastlf != NULL)
2968 *lf = *lastlf;
2969 cf.lpLogFont = lf;
2970 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2971 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002972 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002974 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
2976
2977 /*
2978 * Split name up, it could be <name>:h<height>:w<width> etc.
2979 */
2980 for (p = name; *p && *p != ':'; p++)
2981 {
2982 if (p - name + 1 > LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002983 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 lf->lfFaceName[p - name] = *p;
2985 }
2986 if (p != name)
2987 lf->lfFaceName[p - name] = NUL;
2988
2989 /* First set defaults */
2990 lf->lfHeight = -12;
2991 lf->lfWidth = 0;
2992 lf->lfWeight = FW_NORMAL;
2993 lf->lfItalic = FALSE;
2994 lf->lfUnderline = FALSE;
2995 lf->lfStrikeOut = FALSE;
2996
2997 /*
2998 * If the font can't be found, try replacing '_' by ' '.
2999 */
3000 if (init_logfont(lf) == FAIL)
3001 {
3002 int did_replace = FALSE;
3003
3004 for (i = 0; lf->lfFaceName[i]; ++i)
3005 if (lf->lfFaceName[i] == '_')
3006 {
3007 lf->lfFaceName[i] = ' ';
3008 did_replace = TRUE;
3009 }
3010 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003011 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
3013
3014 while (*p == ':')
3015 p++;
3016
3017 /* Set the values found after ':' */
3018 while (*p)
3019 {
3020 switch (*p++)
3021 {
3022 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003023 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 break;
3025 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003026 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 break;
3028 case 'b':
3029#ifndef MSWIN16_FASTTEXT
3030 lf->lfWeight = FW_BOLD;
3031#endif
3032 break;
3033 case 'i':
3034#ifndef MSWIN16_FASTTEXT
3035 lf->lfItalic = TRUE;
3036#endif
3037 break;
3038 case 'u':
3039 lf->lfUnderline = TRUE;
3040 break;
3041 case 's':
3042 lf->lfStrikeOut = TRUE;
3043 break;
3044 case 'c':
3045 {
3046 struct charset_pair *cp;
3047
3048 for (cp = charset_pairs; cp->name != NULL; ++cp)
3049 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
3050 {
3051 lf->lfCharSet = cp->charset;
3052 p += strlen(cp->name);
3053 break;
3054 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003055 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003057 vim_snprintf((char *)IObuff, IOSIZE,
3058 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 EMSG(IObuff);
3060 break;
3061 }
3062 break;
3063 }
3064 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003065 if (verbose)
3066 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003067 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003068 _("E245: Illegal char '%c' in font name \"%s\""),
3069 p[-1], name);
3070 EMSG(IObuff);
3071 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003072 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 }
3074 while (*p == ':')
3075 p++;
3076 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003077 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003081 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 {
3083 vim_free(lastlf);
3084 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3085 if (lastlf != NULL)
3086 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3087 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003088#ifdef FEAT_MBYTE
3089 vim_free(acpname);
3090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003092 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093}
3094
3095#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */