blob: 5af1dee51861cc72209607d3a12a3cd7b379b87e [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
234# ifdef FEAT_NETBEANS_INTG
235 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
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000280
281#if defined(FEAT_MBYTE) && !defined(FEAT_GUI)
282 (void)get_cmd_argsW(NULL);
283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284}
285
286
287/*
288 * Return TRUE if the input comes from a terminal, FALSE otherwise.
289 */
290 int
291mch_input_isatty()
292{
293#ifdef FEAT_GUI_MSWIN
294 return OK; /* GUI always has a tty */
295#else
296 if (isatty(read_cmd_fd))
297 return TRUE;
298 return FALSE;
299#endif
300}
301
302#ifdef FEAT_TITLE
303/*
304 * mch_settitle(): set titlebar of our window
305 */
306 void
307mch_settitle(
308 char_u *title,
309 char_u *icon)
310{
311# ifdef FEAT_GUI_MSWIN
312 gui_mch_settitle(title, icon);
313# else
314 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000315 {
316# ifdef FEAT_MBYTE
317 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
318 {
319 /* Convert the title from 'encoding' to the active codepage. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000320 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000321 int n;
322
323 if (wp != NULL)
324 {
325 n = SetConsoleTitleW(wp);
326 vim_free(wp);
327 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
328 return;
329 }
330 }
331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 SetConsoleTitle(title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334# endif
335}
336
337
338/*
339 * Restore the window/icon title.
340 * which is one of:
341 * 1: Just restore title
342 * 2: Just restore icon (which we don't have)
343 * 3: Restore title and icon (which we don't have)
344 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000345/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 void
347mch_restore_title(
348 int which)
349{
350#ifndef FEAT_GUI_MSWIN
351 mch_settitle((which & 1) ? g_szOrigTitle : NULL, NULL);
352#endif
353}
354
355
356/*
357 * Return TRUE if we can restore the title (we can)
358 */
359 int
360mch_can_restore_title()
361{
362 return TRUE;
363}
364
365
366/*
367 * Return TRUE if we can restore the icon title (we can't)
368 */
369 int
370mch_can_restore_icon()
371{
372 return FALSE;
373}
374#endif /* FEAT_TITLE */
375
376
377/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000378 * Get absolute file name into buffer "buf" of length "len" bytes,
379 * turning all '/'s into '\\'s and getting the correct case of each component
380 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 * When 'shellslash' set do it the other way around.
382 * Return OK or FAIL.
383 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000384/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 int
386mch_FullName(
387 char_u *fname,
388 char_u *buf,
389 int len,
390 int force)
391{
392 int nResult = FAIL;
393
394#ifdef __BORLANDC__
395 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
396 nResult = mch_dirname(buf, len);
397 else
398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000400#ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000401 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
402# ifdef __BORLANDC__
403 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
404 && g_PlatformId == VER_PLATFORM_WIN32_NT
405# endif
406 )
407 {
408 WCHAR *wname;
409 WCHAR wbuf[MAX_PATH];
410 char_u *cname = NULL;
411
412 /* Use the wide function:
413 * - convert the fname from 'encoding' to UCS2.
414 * - invoke _wfullpath()
415 * - convert the result from UCS2 to 'encoding'.
416 */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000417 wname = enc_to_utf16(fname, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000418 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH - 1) != NULL)
419 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000420 cname = utf16_to_enc((short_u *)wbuf, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000421 if (cname != NULL)
422 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000423 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000424 nResult = OK;
425 }
426 }
427 vim_free(wname);
428 vim_free(cname);
429 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000430 if (nResult == FAIL) /* fall back to non-wide function */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000432 {
433 if (_fullpath(buf, fname, len - 1) == NULL)
434 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000435 /* failed, use relative path name */
436 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000437 }
438 else
439 nResult = OK;
440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
443#ifdef USE_FNAME_CASE
444 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000445#else
446 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447#endif
448
449 return nResult;
450}
451
452
453/*
454 * Return TRUE if "fname" does not depend on the current directory.
455 */
456 int
457mch_isFullName(char_u *fname)
458{
459 char szName[_MAX_PATH + 1];
460
461 /* A name like "d:/foo" and "//server/share" is absolute */
462 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
463 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
464 return TRUE;
465
466 /* A name that can't be made absolute probably isn't absolute. */
467 if (mch_FullName(fname, szName, _MAX_PATH, FALSE) == FAIL)
468 return FALSE;
469
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000470 return pathcmp(fname, szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471}
472
473/*
474 * Replace all slashes by backslashes.
475 * This used to be the other way around, but MS-DOS sometimes has problems
476 * with slashes (e.g. in a command name). We can't have mixed slashes and
477 * backslashes, because comparing file names will not work correctly. The
478 * commands that use a file name should try to avoid the need to type a
479 * backslash twice.
480 * When 'shellslash' set do it the other way around.
481 */
482 void
483slash_adjust(p)
484 char_u *p;
485{
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000486 while (*p)
487 {
488 if (*p == psepcN)
489 *p = psepc;
490 mb_ptr_adv(p);
491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492}
493
494
495/*
496 * stat() can't handle a trailing '/' or '\', remove it first.
497 */
498 int
499vim_stat(const char *name, struct stat *stp)
500{
501 char buf[_MAX_PATH + 1];
502 char *p;
503
Bram Moolenaar84110ac2005-07-20 21:56:21 +0000504 vim_strncpy((char_u *)buf, (char_u *)name, _MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 p = buf + strlen(buf);
506 if (p > buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000507 mb_ptr_back(buf, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
509 *p = NUL;
510#ifdef FEAT_MBYTE
511 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
512# ifdef __BORLANDC__
513 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
514 && g_PlatformId == VER_PLATFORM_WIN32_NT
515# endif
516 )
517 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000518 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 int n;
520
521 if (wp != NULL)
522 {
523 n = _wstat(wp, (struct _stat *)stp);
524 vim_free(wp);
525 if (n >= 0)
526 return n;
527 /* Retry with non-wide function (for Windows 98). Can't use
528 * GetLastError() here and it's unclear what errno gets set to if
529 * the _wstat() fails for missing wide functions. */
530 }
531 }
532#endif
533 return stat(buf, stp);
534}
535
536#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000537/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 void
539mch_settmode(int tmode)
540{
541 /* nothing to do */
542}
543
544 int
545mch_get_shellsize(void)
546{
547 /* never used */
548 return OK;
549}
550
551 void
552mch_set_shellsize(void)
553{
554 /* never used */
555}
556
557/*
558 * Rows and/or Columns has changed.
559 */
560 void
561mch_new_shellsize(void)
562{
563 /* never used */
564}
565
566#endif
567
568/*
569 * We have no job control, so fake it by starting a new shell.
570 */
571 void
572mch_suspend()
573{
574 suspend_shell();
575}
576
577#if defined(USE_MCH_ERRMSG) || defined(PROTO)
578
579#ifdef display_errors
580# undef display_errors
581#endif
582
583/*
584 * Display the saved error message(s).
585 */
586 void
587display_errors()
588{
589 char *p;
590
591 if (error_ga.ga_data != NULL)
592 {
593 /* avoid putting up a message box with blanks only */
594 for (p = (char *)error_ga.ga_data; *p; ++p)
595 if (!isspace(*p))
596 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000597 (void)gui_mch_dialog(
598#ifdef FEAT_GUI
599 gui.starting ? VIM_INFO :
600#endif
601 VIM_ERROR,
602#ifdef FEAT_GUI
603 gui.starting ? (char_u *)_("Message") :
604#endif
605 (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +0100606 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 break;
608 }
609 ga_clear(&error_ga);
610 }
611}
612#endif
613
614
615/*
616 * Return TRUE if "p" contain a wildcard that can be expanded by
617 * dos_expandpath().
618 */
619 int
620mch_has_exp_wildcard(char_u *p)
621{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000622 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {
624 if (vim_strchr((char_u *)"?*[", *p) != NULL
625 || (*p == '~' && p[1] != NUL))
626 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 }
628 return FALSE;
629}
630
631/*
632 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
633 * shortened file name).
634 */
635 int
636mch_has_wildcard(char_u *p)
637{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000638 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 {
640 if (vim_strchr((char_u *)
641# ifdef VIM_BACKTICK
642 "?*$[`"
643# else
644 "?*$["
645# endif
646 , *p) != NULL
647 || (*p == '~' && p[1] != NUL))
648 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 }
650 return FALSE;
651}
652
653
654/*
655 * The normal _chdir() does not change the default drive. This one does.
656 * Returning 0 implies success; -1 implies failure.
657 */
658 int
659mch_chdir(char *path)
660{
661 if (path[0] == NUL) /* just checking... */
662 return -1;
663
Bram Moolenaara2974d72009-07-14 16:38:36 +0000664 if (p_verbose >= 5)
665 {
666 verbose_enter();
667 smsg((char_u *)"chdir(%s)", path);
668 verbose_leave();
669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
671 {
672 /* If we can change to the drive, skip that part of the path. If we
673 * can't then the current directory may be invalid, try using chdir()
674 * with the whole path. */
675 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
676 path += 2;
677 }
678
679 if (*path == NUL) /* drive name only */
680 return 0;
681
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000682#ifdef FEAT_MBYTE
683 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
684 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000685 WCHAR *p = enc_to_utf16(path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000686 int n;
687
688 if (p != NULL)
689 {
690 n = _wchdir(p);
691 vim_free(p);
692 if (n == 0)
693 return 0;
694 /* Retry with non-wide function (for Windows 98). */
695 }
696 }
697#endif
698
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 return chdir(path); /* let the normal chdir() do the rest */
700}
701
702
703/*
704 * Switching off termcap mode is only allowed when Columns is 80, otherwise a
705 * crash may result. It's always allowed on NT or when running the GUI.
706 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000707/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 int
709can_end_termcap_mode(
710 int give_msg)
711{
712#ifdef FEAT_GUI_MSWIN
713 return TRUE; /* GUI starts a new console anyway */
714#else
715 if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80)
716 return TRUE;
717 if (give_msg)
718 msg(_("'columns' is not 80, cannot execute external commands"));
719 return FALSE;
720#endif
721}
722
723#ifdef FEAT_GUI_MSWIN
724/*
725 * return non-zero if a character is available
726 */
727 int
728mch_char_avail()
729{
730 /* never used */
731 return TRUE;
732}
733#endif
734
735
736/*
737 * set screen mode, always fails.
738 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000739/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 int
741mch_screenmode(
742 char_u *arg)
743{
744 EMSG(_(e_screenmode));
745 return FAIL;
746}
747
748
749#if defined(FEAT_LIBCALL) || defined(PROTO)
750/*
751 * Call a DLL routine which takes either a string or int param
752 * and returns an allocated string.
753 * Return OK if it worked, FAIL if not.
754 */
755# ifdef WIN3264
756typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
757typedef LPTSTR (*MYINTPROCSTR)(int);
758typedef int (*MYSTRPROCINT)(LPTSTR);
759typedef int (*MYINTPROCINT)(int);
760# else
761typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
762typedef LPSTR (*MYINTPROCSTR)(int);
763typedef int (*MYSTRPROCINT)(LPSTR);
764typedef int (*MYINTPROCINT)(int);
765# endif
766
767# ifndef WIN16
768/*
769 * Check if a pointer points to a valid NUL terminated string.
770 * Return the length of the string, including terminating NUL.
771 * Returns 0 for an invalid pointer, 1 for an empty string.
772 */
773 static size_t
774check_str_len(char_u *str)
775{
776 SYSTEM_INFO si;
777 MEMORY_BASIC_INFORMATION mbi;
778 size_t length = 0;
779 size_t i;
780 const char *p;
781
782 /* get page size */
783 GetSystemInfo(&si);
784
785 /* get memory information */
786 if (VirtualQuery(str, &mbi, sizeof(mbi)))
787 {
788 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000789 long_u dwStr = (long_u)str;
790 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
792 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000793 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
795 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000796 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
Bram Moolenaar442b4222010-05-24 21:34:22 +0200798 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 p += pageLength, pageLength = si.dwPageSize)
800 for (i = 0; i < pageLength; ++i, ++length)
801 if (p[i] == NUL)
802 return length + 1;
803 }
804
805 return 0;
806}
807# endif
808
809 int
810mch_libcall(
811 char_u *libname,
812 char_u *funcname,
813 char_u *argstring, /* NULL when using a argint */
814 int argint,
815 char_u **string_result,/* NULL when using number_result */
816 int *number_result)
817{
818 HINSTANCE hinstLib;
819 MYSTRPROCSTR ProcAdd;
820 MYINTPROCSTR ProcAddI;
821 char_u *retval_str = NULL;
822 int retval_int = 0;
823 size_t len;
824
825 BOOL fRunTimeLinkSuccess = FALSE;
826
827 // Get a handle to the DLL module.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200828# ifdef WIN16
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 hinstLib = LoadLibrary(libname);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200830# else
831 hinstLib = vimLoadLib(libname);
832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
834 // If the handle is valid, try to get the function address.
835 if (hinstLib != NULL)
836 {
837#ifdef HAVE_TRY_EXCEPT
838 __try
839 {
840#endif
841 if (argstring != NULL)
842 {
843 /* Call with string argument */
844 ProcAdd = (MYSTRPROCSTR) GetProcAddress(hinstLib, funcname);
845 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
846 {
847 if (string_result == NULL)
848 retval_int = ((MYSTRPROCINT)ProcAdd)(argstring);
849 else
850 retval_str = (ProcAdd)(argstring);
851 }
852 }
853 else
854 {
855 /* Call with number argument */
856 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, funcname);
857 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
858 {
859 if (string_result == NULL)
860 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
861 else
862 retval_str = (ProcAddI)(argint);
863 }
864 }
865
866 // Save the string before we free the library.
867 // Assume that a "1" result is an illegal pointer.
868 if (string_result == NULL)
869 *number_result = retval_int;
870 else if (retval_str != NULL
871# ifdef WIN16
872 && retval_str != (char_u *)1
873 && retval_str != (char_u *)-1
874 && !IsBadStringPtr(retval_str, INT_MAX)
875 && (len = strlen(retval_str) + 1) > 0
876# else
877 && (len = check_str_len(retval_str)) > 0
878# endif
879 )
880 {
881 *string_result = lalloc((long_u)len, TRUE);
882 if (*string_result != NULL)
883 mch_memmove(*string_result, retval_str, len);
884 }
885
886#ifdef HAVE_TRY_EXCEPT
887 }
888 __except(EXCEPTION_EXECUTE_HANDLER)
889 {
890 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
891 RESETSTKOFLW();
892 fRunTimeLinkSuccess = 0;
893 }
894#endif
895
896 // Free the DLL module.
897 (void)FreeLibrary(hinstLib);
898 }
899
900 if (!fRunTimeLinkSuccess)
901 {
902 EMSG2(_(e_libcall), funcname);
903 return FAIL;
904 }
905
906 return OK;
907}
908#endif
909
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910/*
911 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
912 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000913/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 void
915DumpPutS(
916 const char *psz)
917{
918# ifdef MCH_WRITE_DUMP
919 if (fdDump)
920 {
921 fputs(psz, fdDump);
922 if (psz[strlen(psz) - 1] != '\n')
923 fputc('\n', fdDump);
924 fflush(fdDump);
925 }
926# endif
927}
928
929#ifdef _DEBUG
930
931void __cdecl
932Trace(
933 char *pszFormat,
934 ...)
935{
936 CHAR szBuff[2048];
937 va_list args;
938
939 va_start(args, pszFormat);
940 vsprintf(szBuff, pszFormat, args);
941 va_end(args);
942
943 OutputDebugString(szBuff);
944}
945
946#endif //_DEBUG
947
Bram Moolenaar843ee412004-06-30 16:16:41 +0000948#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949# if defined(FEAT_TITLE) && defined(WIN3264)
950extern HWND g_hWnd; /* This is in os_win32.c. */
951# endif
952
953/*
954 * Showing the printer dialog is tricky since we have no GUI
955 * window to parent it. The following routines are needed to
956 * get the window parenting and Z-order to work properly.
957 */
958 static void
959GetConsoleHwnd(void)
960{
961# define MY_BUFSIZE 1024 // Buffer size for console window titles.
962
963 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
964 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
965
966 /* Skip if it's already set. */
967 if (s_hwnd != 0)
968 return;
969
970# if defined(FEAT_TITLE) && defined(WIN3264)
971 /* Window handle may have been found by init code (Windows NT only) */
972 if (g_hWnd != 0)
973 {
974 s_hwnd = g_hWnd;
975 return;
976 }
977# endif
978
979 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
980
981 wsprintf(pszNewWindowTitle, "%s/%d/%d",
982 pszOldWindowTitle,
983 GetTickCount(),
984 GetCurrentProcessId());
985 SetConsoleTitle(pszNewWindowTitle);
986 Sleep(40);
987 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
988
989 SetConsoleTitle(pszOldWindowTitle);
990}
Bram Moolenaar843ee412004-06-30 16:16:41 +0000991
992/*
993 * Console implementation of ":winpos".
994 */
995 int
996mch_get_winpos(int *x, int *y)
997{
998 RECT rect;
999
1000 GetConsoleHwnd();
1001 GetWindowRect(s_hwnd, &rect);
1002 *x = rect.left;
1003 *y = rect.top;
1004 return OK;
1005}
1006
1007/*
1008 * Console implementation of ":winpos x y".
1009 */
1010 void
1011mch_set_winpos(int x, int y)
1012{
1013 GetConsoleHwnd();
1014 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1015 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1016}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#endif
1018
1019#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1020
1021# ifdef WIN16
1022# define TEXT(a) a
1023# endif
1024/*=================================================================
1025 * Win32 printer stuff
1026 */
1027
1028static HFONT prt_font_handles[2][2][2];
1029static PRINTDLG prt_dlg;
1030static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1031static TEXTMETRIC prt_tm;
1032static int prt_line_height;
1033static int prt_number_width;
1034static int prt_left_margin;
1035static int prt_right_margin;
1036static int prt_top_margin;
1037static char_u szAppName[] = TEXT("VIM");
1038static HWND hDlgPrint;
1039static int *bUserAbort = NULL;
1040static char_u *prt_name = NULL;
1041
1042/* Defines which are also in vim.rc. */
1043#define IDC_BOX1 400
1044#define IDC_PRINTTEXT1 401
1045#define IDC_PRINTTEXT2 402
1046#define IDC_PROGRESS 403
1047
1048/*
1049 * Convert BGR to RGB for Windows GDI calls
1050 */
1051 static COLORREF
1052swap_me(COLORREF colorref)
1053{
1054 int temp;
1055 char *ptr = (char *)&colorref;
1056
1057 temp = *(ptr);
1058 *(ptr ) = *(ptr + 2);
1059 *(ptr + 2) = temp;
1060 return colorref;
1061}
1062
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001063/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001064#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001065# define PDP_RETVAL BOOL
1066#else
1067# define PDP_RETVAL INT_PTR
1068#endif
1069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001070/*ARGSUSED*/
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001071 static PDP_RETVAL CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1073{
1074#ifdef FEAT_GETTEXT
1075 NONCLIENTMETRICS nm;
1076 static HFONT hfont;
1077#endif
1078
1079 switch (message)
1080 {
1081 case WM_INITDIALOG:
1082#ifdef FEAT_GETTEXT
1083 nm.cbSize = sizeof(NONCLIENTMETRICS);
1084 if (SystemParametersInfo(
1085 SPI_GETNONCLIENTMETRICS,
1086 sizeof(NONCLIENTMETRICS),
1087 &nm,
1088 0))
1089 {
1090 char buff[MAX_PATH];
1091 int i;
1092
1093 /* Translate the dialog texts */
1094 hfont = CreateFontIndirect(&nm.lfMessageFont);
1095 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1096 {
1097 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1098 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
1099 SetDlgItemText(hDlg,i, _(buff));
1100 }
1101 SendDlgItemMessage(hDlg, IDCANCEL,
1102 WM_SETFONT, (WPARAM)hfont, 1);
1103 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
1104 SetDlgItemText(hDlg,IDCANCEL, _(buff));
1105 }
1106#endif
1107 SetWindowText(hDlg, szAppName);
1108 if (prt_name != NULL)
1109 {
1110 SetDlgItemText(hDlg, IDC_PRINTTEXT2, (LPSTR)prt_name);
1111 vim_free(prt_name);
1112 prt_name = NULL;
1113 }
1114 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1115#ifndef FEAT_GUI
1116 BringWindowToTop(s_hwnd);
1117#endif
1118 return TRUE;
1119
1120 case WM_COMMAND:
1121 *bUserAbort = TRUE;
1122 EnableWindow(GetParent(hDlg), TRUE);
1123 DestroyWindow(hDlg);
1124 hDlgPrint = NULL;
1125#ifdef FEAT_GETTEXT
1126 DeleteObject(hfont);
1127#endif
1128 return TRUE;
1129 }
1130 return FALSE;
1131}
1132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001133/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 static BOOL CALLBACK
1135AbortProc(HDC hdcPrn, int iCode)
1136{
1137 MSG msg;
1138
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001139 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001141 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
1143 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001144 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
1146 }
1147 return !*bUserAbort;
1148}
1149
1150#ifndef FEAT_GUI
1151
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001152 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153PrintHookProc(
1154 HWND hDlg, // handle to dialog box
1155 UINT uiMsg, // message identifier
1156 WPARAM wParam, // message parameter
1157 LPARAM lParam // message parameter
1158 )
1159{
1160 HWND hwndOwner;
1161 RECT rc, rcDlg, rcOwner;
1162 PRINTDLG *pPD;
1163
1164 if (uiMsg == WM_INITDIALOG)
1165 {
1166 // Get the owner window and dialog box rectangles.
1167 if ((hwndOwner = GetParent(hDlg)) == NULL)
1168 hwndOwner = GetDesktopWindow();
1169
1170 GetWindowRect(hwndOwner, &rcOwner);
1171 GetWindowRect(hDlg, &rcDlg);
1172 CopyRect(&rc, &rcOwner);
1173
1174 // Offset the owner and dialog box rectangles so that
1175 // right and bottom values represent the width and
1176 // height, and then offset the owner again to discard
1177 // space taken up by the dialog box.
1178
1179 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1180 OffsetRect(&rc, -rc.left, -rc.top);
1181 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1182
1183 // The new position is the sum of half the remaining
1184 // space and the owner's original position.
1185
1186 SetWindowPos(hDlg,
1187 HWND_TOP,
1188 rcOwner.left + (rc.right / 2),
1189 rcOwner.top + (rc.bottom / 2),
1190 0, 0, // ignores size arguments
1191 SWP_NOSIZE);
1192
1193 /* tackle the printdlg copiesctrl problem */
1194 pPD = (PRINTDLG *)lParam;
1195 pPD->nCopies = (WORD)pPD->lCustData;
1196 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1197 /* Bring the window to top */
1198 BringWindowToTop(GetParent(hDlg));
1199 SetForegroundWindow(hDlg);
1200 }
1201
1202 return FALSE;
1203}
1204#endif
1205
1206 void
1207mch_print_cleanup(void)
1208{
1209 int pifItalic;
1210 int pifBold;
1211 int pifUnderline;
1212
1213 for (pifBold = 0; pifBold <= 1; pifBold++)
1214 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1215 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1216 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1217
1218 if (prt_dlg.hDC != NULL)
1219 DeleteDC(prt_dlg.hDC);
1220 if (!*bUserAbort)
1221 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1222}
1223
1224 static int
1225to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1226{
1227 int ret = 0;
1228 int u;
1229 int nr;
1230
1231 u = prt_get_unit(idx);
1232 if (u == PRT_UNIT_NONE)
1233 {
1234 u = PRT_UNIT_PERC;
1235 nr = def_number;
1236 }
1237 else
1238 nr = printer_opts[idx].number;
1239
1240 switch (u)
1241 {
1242 case PRT_UNIT_PERC:
1243 ret = (physsize * nr) / 100;
1244 break;
1245 case PRT_UNIT_INCH:
1246 ret = (nr * dpi);
1247 break;
1248 case PRT_UNIT_MM:
1249 ret = (nr * 10 * dpi) / 254;
1250 break;
1251 case PRT_UNIT_POINT:
1252 ret = (nr * 10 * dpi) / 720;
1253 break;
1254 }
1255
1256 if (ret < offset)
1257 return 0;
1258 else
1259 return ret - offset;
1260}
1261
1262 static int
1263prt_get_cpl(void)
1264{
1265 int hr;
1266 int phyw;
1267 int dvoff;
1268 int rev_offset;
1269 int dpi;
1270#ifdef WIN16
1271 POINT pagesize;
1272#endif
1273
1274 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1275 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1276
1277 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
1278#ifdef WIN16
1279 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1280 phyw = pagesize.x;
1281 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1282 dvoff = pagesize.x;
1283#else
1284 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1285 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
1286#endif
1287 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1288
1289 rev_offset = phyw - (dvoff + hr);
1290
1291 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1292 if (prt_use_number())
1293 {
1294 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1295 prt_left_margin += prt_number_width;
1296 }
1297 else
1298 prt_number_width = 0;
1299
1300 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1301 rev_offset, 5);
1302
1303 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1304}
1305
1306 static int
1307prt_get_lpp(void)
1308{
1309 int vr;
1310 int phyw;
1311 int dvoff;
1312 int rev_offset;
1313 int bottom_margin;
1314 int dpi;
1315#ifdef WIN16
1316 POINT pagesize;
1317#endif
1318
1319 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
1320#ifdef WIN16
1321 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1322 phyw = pagesize.y;
1323 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1324 dvoff = pagesize.y;
1325#else
1326 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1327 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
1328#endif
1329 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1330
1331 rev_offset = phyw - (dvoff + vr);
1332
1333 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1334
1335 /* adjust top margin if there is a header */
1336 prt_top_margin += prt_line_height * prt_header_height();
1337
1338 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1339 rev_offset, 5);
1340
1341 return (bottom_margin - prt_top_margin) / prt_line_height;
1342}
1343
1344 int
1345mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1346{
1347 static HGLOBAL stored_dm = NULL;
1348 static HGLOBAL stored_devn = NULL;
1349 static int stored_nCopies = 1;
1350 static int stored_nFlags = 0;
1351
1352 LOGFONT fLogFont;
1353 int pifItalic;
1354 int pifBold;
1355 int pifUnderline;
1356
1357 DEVMODE *mem;
1358 DEVNAMES *devname;
1359 int i;
1360
1361 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001362 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 prt_dlg.lStructSize = sizeof(PRINTDLG);
1364#ifndef FEAT_GUI
1365 GetConsoleHwnd(); /* get value of s_hwnd */
1366#endif
1367 prt_dlg.hwndOwner = s_hwnd;
1368 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1369 if (!forceit)
1370 {
1371 prt_dlg.hDevMode = stored_dm;
1372 prt_dlg.hDevNames = stored_devn;
1373 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1374#ifndef FEAT_GUI
1375 /*
1376 * Use hook to prevent console window being sent to back
1377 */
1378 prt_dlg.lpfnPrintHook = PrintHookProc;
1379 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1380#endif
1381 prt_dlg.Flags |= stored_nFlags;
1382 }
1383
1384 /*
1385 * If bang present, return default printer setup with no dialog
1386 * never show dialog if we are running over telnet
1387 */
1388 if (forceit
1389#ifndef FEAT_GUI
1390 || !term_console
1391#endif
1392 )
1393 {
1394 prt_dlg.Flags |= PD_RETURNDEFAULT;
1395#ifdef WIN3264
1396 /*
1397 * MSDN suggests setting the first parameter to WINSPOOL for
1398 * NT, but NULL appears to work just as well.
1399 */
1400 if (*p_pdev != NUL)
1401 prt_dlg.hDC = CreateDC(NULL, p_pdev, NULL, NULL);
1402 else
1403#endif
1404 {
1405 prt_dlg.Flags |= PD_RETURNDEFAULT;
1406 if (PrintDlg(&prt_dlg) == 0)
1407 goto init_fail_dlg;
1408 }
1409 }
1410 else if (PrintDlg(&prt_dlg) == 0)
1411 goto init_fail_dlg;
1412 else
1413 {
1414 /*
1415 * keep the previous driver context
1416 */
1417 stored_dm = prt_dlg.hDevMode;
1418 stored_devn = prt_dlg.hDevNames;
1419 stored_nFlags = prt_dlg.Flags;
1420 stored_nCopies = prt_dlg.nCopies;
1421 }
1422
1423 if (prt_dlg.hDC == NULL)
1424 {
1425 EMSG(_("E237: Printer selection failed"));
1426 mch_print_cleanup();
1427 return FALSE;
1428 }
1429
1430 /* Not all printer drivers report the support of color (or grey) in the
1431 * same way. Let's set has_color if there appears to be some way to print
1432 * more than B&W. */
1433 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1434 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1435 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1436 || i > 2 || i == -1);
1437
1438 /* Ensure all font styles are baseline aligned */
1439 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1440
1441 /*
1442 * On some windows systems the nCopies parameter is not
1443 * passed back correctly. It must be retrieved from the
1444 * hDevMode struct.
1445 */
1446 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1447 if (mem != NULL)
1448 {
1449#ifdef WIN3264
1450 if (mem->dmCopies != 1)
1451 stored_nCopies = mem->dmCopies;
1452#endif
1453 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1454 psettings->duplex = TRUE;
1455 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1456 psettings->has_color = TRUE;
1457 }
1458 GlobalUnlock(prt_dlg.hDevMode);
1459
1460 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1461 if (devname != 0)
1462 {
1463 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1464 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
1465 char_u *text = _("to %s on %s");
1466
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001467 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1468 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (prt_name != NULL)
1470 wsprintf(prt_name, text, printer_name, port_name);
1471 }
1472 GlobalUnlock(prt_dlg.hDevNames);
1473
1474 /*
1475 * Initialise the font according to 'printfont'
1476 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001477 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001478 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 {
1480 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1481 mch_print_cleanup();
1482 return FALSE;
1483 }
1484
1485 for (pifBold = 0; pifBold <= 1; pifBold++)
1486 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1487 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1488 {
1489 fLogFont.lfWeight = boldface[pifBold];
1490 fLogFont.lfItalic = pifItalic;
1491 fLogFont.lfUnderline = pifUnderline;
1492 prt_font_handles[pifBold][pifItalic][pifUnderline]
1493 = CreateFontIndirect(&fLogFont);
1494 }
1495
1496 SetBkMode(prt_dlg.hDC, OPAQUE);
1497 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1498
1499 /*
1500 * Fill in the settings struct
1501 */
1502 psettings->chars_per_line = prt_get_cpl();
1503 psettings->lines_per_page = prt_get_lpp();
1504 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1505 ? prt_dlg.nCopies : 1;
1506 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1507 ? 1 : prt_dlg.nCopies;
1508
1509 if (psettings->n_collated_copies == 0)
1510 psettings->n_collated_copies = 1;
1511
1512 if (psettings->n_uncollated_copies == 0)
1513 psettings->n_uncollated_copies = 1;
1514
1515 psettings->jobname = jobname;
1516
1517 return TRUE;
1518
1519init_fail_dlg:
1520 {
1521 DWORD err = CommDlgExtendedError();
1522
1523 if (err)
1524 {
1525#ifdef WIN16
1526 char buf[20];
1527
1528 sprintf(buf, "%ld", err);
1529 EMSG2(_("E238: Print error: %s"), buf);
1530#else
1531 char_u *buf;
1532
1533 /* I suspect FormatMessage() doesn't work for values returned by
1534 * CommDlgExtendedError(). What does? */
1535 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1536 FORMAT_MESSAGE_FROM_SYSTEM |
1537 FORMAT_MESSAGE_IGNORE_INSERTS,
1538 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1539 EMSG2(_("E238: Print error: %s"),
1540 buf == NULL ? (char_u *)_("Unknown") : buf);
1541 LocalFree((LPVOID)(buf));
1542#endif
1543 }
1544 else
1545 msg_clr_eos(); /* Maybe canceled */
1546
1547 mch_print_cleanup();
1548 return FALSE;
1549 }
1550}
1551
1552
1553 int
1554mch_print_begin(prt_settings_T *psettings)
1555{
1556 int ret;
1557 static DOCINFO di;
1558 char szBuffer[300];
1559
1560 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1561 prt_dlg.hwndOwner, PrintDlgProc);
1562#ifdef WIN16
1563 Escape(prt_dlg.hDC, SETABORTPROC, 0, (LPSTR)AbortProc, NULL);
1564#else
1565 SetAbortProc(prt_dlg.hDC, AbortProc);
1566#endif
1567 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
1568 SetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (LPSTR)szBuffer);
1569
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001570 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 di.cbSize = sizeof(DOCINFO);
1572 di.lpszDocName = psettings->jobname;
1573 ret = StartDoc(prt_dlg.hDC, &di);
1574
1575#ifdef FEAT_GUI
1576 /* Give focus back to main window (when using MDI). */
1577 SetFocus(s_hwnd);
1578#endif
1579
1580 return (ret > 0);
1581}
1582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001583/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 void
1585mch_print_end(prt_settings_T *psettings)
1586{
1587 EndDoc(prt_dlg.hDC);
1588 if (!*bUserAbort)
1589 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1590}
1591
1592 int
1593mch_print_end_page(void)
1594{
1595 return (EndPage(prt_dlg.hDC) > 0);
1596}
1597
1598 int
1599mch_print_begin_page(char_u *msg)
1600{
1601 if (msg != NULL)
1602 SetDlgItemText(hDlgPrint, IDC_PROGRESS, (LPSTR)msg);
1603 return (StartPage(prt_dlg.hDC) > 0);
1604}
1605
1606 int
1607mch_print_blank_page(void)
1608{
1609 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1610}
1611
1612static int prt_pos_x = 0;
1613static int prt_pos_y = 0;
1614
1615 void
1616mch_print_start_line(margin, page_line)
1617 int margin;
1618 int page_line;
1619{
1620 if (margin)
1621 prt_pos_x = -prt_number_width;
1622 else
1623 prt_pos_x = 0;
1624 prt_pos_y = page_line * prt_line_height
1625 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1626}
1627
1628 int
1629mch_print_text_out(char_u *p, int len)
1630{
1631#ifdef FEAT_PROPORTIONAL_FONTS
1632 SIZE sz;
1633#endif
1634
1635 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1636 prt_pos_y + prt_top_margin, p, len);
1637#ifndef FEAT_PROPORTIONAL_FONTS
1638 prt_pos_x += len * prt_tm.tmAveCharWidth;
1639 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1640 + prt_tm.tmOverhang > prt_right_margin);
1641#else
1642# ifdef WIN16
1643 GetTextExtentPoint(prt_dlg.hDC, p, len, &sz);
1644# else
1645 GetTextExtentPoint32(prt_dlg.hDC, p, len, &sz);
1646# endif
1647 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1648 /* This is wrong when printing spaces for a TAB. */
1649 if (p[len] == NUL)
1650 return FALSE;
1651# ifdef WIN16
1652 GetTextExtentPoint(prt_dlg.hDC, p + len, 1, &sz);
1653# else
1654 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
1655# endif
1656 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1657#endif
1658}
1659
1660 void
1661mch_print_set_font(int iBold, int iItalic, int iUnderline)
1662{
1663 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1664}
1665
1666 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001667mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001669 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1670 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 /*
1672 * With a white background we can draw characters transparent, which is
1673 * good for italic characters that overlap to the next char cell.
1674 */
1675 if (bgcol == 0xffffffUL)
1676 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1677 else
1678 SetBkMode(prt_dlg.hDC, OPAQUE);
1679}
1680
1681 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001682mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001684 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1685 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686}
1687
1688#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1689
Bram Moolenaar58d98232005-07-23 22:25:46 +00001690
1691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001693# ifndef PROTO
1694# include <shlobj.h>
1695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
1697/*
1698 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1699 * to and return that name in allocated memory.
1700 * Otherwise NULL is returned.
1701 */
1702 char_u *
1703mch_resolve_shortcut(char_u *fname)
1704{
1705 HRESULT hr;
1706 IShellLink *psl = NULL;
1707 IPersistFile *ppf = NULL;
1708 OLECHAR wsz[MAX_PATH];
1709 WIN32_FIND_DATA ffd; // we get those free of charge
1710 TCHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
1711 char_u *rfname = NULL;
1712 int len;
1713
1714 /* Check if the file name ends in ".lnk". Avoid calling
1715 * CoCreateInstance(), it's quite slow. */
1716 if (fname == NULL)
1717 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001718 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1720 return rfname;
1721
1722 CoInitialize(NULL);
1723
1724 // create a link manager object and request its interface
1725 hr = CoCreateInstance(
1726 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1727 &IID_IShellLink, (void**)&psl);
1728 if (hr != S_OK)
1729 goto shortcut_error;
1730
1731 // Get a pointer to the IPersistFile interface.
1732 hr = psl->lpVtbl->QueryInterface(
1733 psl, &IID_IPersistFile, (void**)&ppf);
1734 if (hr != S_OK)
1735 goto shortcut_error;
1736
1737 // full path string must be in Unicode.
1738 MultiByteToWideChar(CP_ACP, 0, fname, -1, wsz, MAX_PATH);
1739
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001740 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
1742 if (hr != S_OK)
1743 goto shortcut_error;
1744#if 0 // This makes Vim wait a long time if the target doesn't exist.
1745 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
1746 if (hr != S_OK)
1747 goto shortcut_error;
1748#endif
1749
1750 // Get the path to the link target.
1751 ZeroMemory(buf, MAX_PATH);
1752 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
1753 if (hr == S_OK && buf[0] != NUL)
1754 rfname = vim_strsave(buf);
1755
1756shortcut_error:
1757 // Release all interface pointers (both belong to the same object)
1758 if (ppf != NULL)
1759 ppf->lpVtbl->Release(ppf);
1760 if (psl != NULL)
1761 psl->lpVtbl->Release(psl);
1762
1763 CoUninitialize();
1764 return rfname;
1765}
1766#endif
1767
1768#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
1769/*
1770 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1771 */
1772 void
1773win32_set_foreground()
1774{
1775# ifndef FEAT_GUI
1776 GetConsoleHwnd(); /* get value of s_hwnd */
1777# endif
1778 if (s_hwnd != 0)
1779 SetForegroundWindow(s_hwnd);
1780}
1781#endif
1782
1783#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1784/*
1785 * Client-server code for Vim
1786 *
1787 * Originally written by Paul Moore
1788 */
1789
1790/* In order to handle inter-process messages, we need to have a window. But
1791 * the functions in this module can be called before the main GUI window is
1792 * created (and may also be called in the console version, where there is no
1793 * GUI window at all).
1794 *
1795 * So we create a hidden window, and arrange to destroy it on exit.
1796 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001797HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799#define VIM_CLASSNAME "VIM_MESSAGES"
1800#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
1801
1802/* Communication is via WM_COPYDATA messages. The message type is send in
1803 * the dwData parameter. Types are defined here. */
1804#define COPYDATA_KEYS 0
1805#define COPYDATA_REPLY 1
1806#define COPYDATA_EXPR 10
1807#define COPYDATA_RESULT 11
1808#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001809#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811/* This is a structure containing a server HWND and its name. */
1812struct server_id
1813{
1814 HWND hwnd;
1815 char_u *name;
1816};
1817
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001818/* Last received 'encoding' that the client uses. */
1819static char_u *client_enc = NULL;
1820
1821/*
1822 * Tell the other side what encoding we are using.
1823 * Errors are ignored.
1824 */
1825 static void
1826serverSendEnc(HWND target)
1827{
1828 COPYDATASTRUCT data;
1829
1830 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001831#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001832 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001833 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001834#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02001835 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001836 data.lpData = "latin1";
1837#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001838 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
1839 (LPARAM)(&data));
1840}
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842/*
1843 * Clean up on exit. This destroys the hidden message window.
1844 */
1845 static void
1846#ifdef __BORLANDC__
1847 _RTLENTRYF
1848#endif
1849CleanUpMessaging(void)
1850{
1851 if (message_window != 0)
1852 {
1853 DestroyWindow(message_window);
1854 message_window = 0;
1855 }
1856}
1857
1858static int save_reply(HWND server, char_u *reply, int expr);
1859
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 * The window procedure for the hidden message window.
1862 * It handles callback messages and notifications from servers.
1863 * In order to process these messages, it is necessary to run a
1864 * message loop. Code which may run before the main message loop
1865 * is started (in the GUI) is careful to pump messages when it needs
1866 * to. Features which require message delivery during normal use will
1867 * not work in the console version - this basically means those
1868 * features which allow Vim to act as a server, rather than a client.
1869 */
1870 static LRESULT CALLBACK
1871Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1872{
1873 if (msg == WM_COPYDATA)
1874 {
1875 /* This is a message from another Vim. The dwData member of the
1876 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001877 * COPYDATA_ENCODING:
1878 * The encoding that the client uses. Following messages will
1879 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 * COPYDATA_KEYS:
1881 * A key sequence. We are a server, and a client wants these keys
1882 * adding to the input queue.
1883 * COPYDATA_REPLY:
1884 * A reply. We are a client, and a server has sent this message
1885 * in response to a request. (server2client())
1886 * COPYDATA_EXPR:
1887 * An expression. We are a server, and a client wants us to
1888 * evaluate this expression.
1889 * COPYDATA_RESULT:
1890 * A reply. We are a client, and a server has sent this message
1891 * in response to a COPYDATA_EXPR.
1892 * COPYDATA_ERROR_RESULT:
1893 * A reply. We are a client, and a server has sent this message
1894 * in response to a COPYDATA_EXPR that failed to evaluate.
1895 */
1896 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
1897 HWND sender = (HWND)wParam;
1898 COPYDATASTRUCT reply;
1899 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001901 char_u *str;
1902 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
1904 switch (data->dwData)
1905 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001906 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001907# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001908 /* Remember the encoding that the client uses. */
1909 vim_free(client_enc);
1910 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001911# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001912 return 1;
1913
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 case COPYDATA_KEYS:
1915 /* Remember who sent this, for <client> */
1916 clientWindow = sender;
1917
1918 /* Add the received keys to the input buffer. The loop waiting
1919 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001920 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
1921 server_to_input_buf(str);
1922 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
1924# ifdef FEAT_GUI
1925 /* Wake up the main GUI loop. */
1926 if (s_hwnd != 0)
1927 PostMessage(s_hwnd, WM_NULL, 0, 0);
1928# endif
1929 return 1;
1930
1931 case COPYDATA_EXPR:
1932 /* Remember who sent this, for <client> */
1933 clientWindow = sender;
1934
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001935 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
1936 res = eval_client_expr_to_string(str);
1937 vim_free(tofree);
1938
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (res == NULL)
1940 {
1941 res = vim_strsave(_(e_invexprmsg));
1942 reply.dwData = COPYDATA_ERROR_RESULT;
1943 }
1944 else
1945 reply.dwData = COPYDATA_RESULT;
1946 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001947 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001949 serverSendEnc(sender);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001950 retval = (int)SendMessage(sender, WM_COPYDATA, (WPARAM)message_window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 (LPARAM)(&reply));
1952 vim_free(res);
1953 return retval;
1954
1955 case COPYDATA_REPLY:
1956 case COPYDATA_RESULT:
1957 case COPYDATA_ERROR_RESULT:
1958 if (data->lpData != NULL)
1959 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001960 str = serverConvert(client_enc, (char_u *)data->lpData,
1961 &tofree);
1962 if (tofree == NULL)
1963 str = vim_strsave(str);
1964 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 (data->dwData == COPYDATA_REPLY ? 0 :
1966 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001967 2))) == FAIL)
1968 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001970 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02001972 char_u winstr[30];
1973
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001974 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001975 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 TRUE, curbuf);
1977 }
1978#endif
1979 }
1980 return 1;
1981 }
1982
1983 return 0;
1984 }
1985
1986 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
1987 {
1988 /* When the message window is activated (brought to the foreground),
1989 * this actually applies to the text window. */
1990#ifndef FEAT_GUI
1991 GetConsoleHwnd(); /* get value of s_hwnd */
1992#endif
1993 if (s_hwnd != 0)
1994 {
1995 SetForegroundWindow(s_hwnd);
1996 return 0;
1997 }
1998 }
1999
2000 return DefWindowProc(hwnd, msg, wParam, lParam);
2001}
2002
2003/*
2004 * Initialise the message handling process. This involves creating a window
2005 * to handle messages - the window will not be visible.
2006 */
2007 void
2008serverInitMessaging(void)
2009{
2010 WNDCLASS wndclass;
2011 HINSTANCE s_hinst;
2012
2013 /* Clean up on exit */
2014 atexit(CleanUpMessaging);
2015
2016 /* Register a window class - we only really care
2017 * about the window procedure
2018 */
2019 s_hinst = (HINSTANCE)GetModuleHandle(0);
2020 wndclass.style = 0;
2021 wndclass.lpfnWndProc = Messaging_WndProc;
2022 wndclass.cbClsExtra = 0;
2023 wndclass.cbWndExtra = 0;
2024 wndclass.hInstance = s_hinst;
2025 wndclass.hIcon = NULL;
2026 wndclass.hCursor = NULL;
2027 wndclass.hbrBackground = NULL;
2028 wndclass.lpszMenuName = NULL;
2029 wndclass.lpszClassName = VIM_CLASSNAME;
2030 RegisterClass(&wndclass);
2031
2032 /* Create the message window. It will be hidden, so the details don't
2033 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2034 * focus from gvim. */
2035 message_window = CreateWindow(VIM_CLASSNAME, "",
2036 WS_POPUPWINDOW | WS_CAPTION,
2037 CW_USEDEFAULT, CW_USEDEFAULT,
2038 100, 100, NULL, NULL,
2039 s_hinst, NULL);
2040}
2041
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002042/* Used by serverSendToVim() to find an alternate server name. */
2043static char_u *altname_buf_ptr = NULL;
2044
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045/*
2046 * Get the title of the window "hwnd", which is the Vim server name, in
2047 * "name[namelen]" and return the length.
2048 * Returns zero if window "hwnd" is not a Vim server.
2049 */
2050 static int
2051getVimServerName(HWND hwnd, char *name, int namelen)
2052{
2053 int len;
2054 char buffer[VIM_CLASSNAME_LEN + 1];
2055
2056 /* Ignore windows which aren't Vim message windows */
2057 len = GetClassName(hwnd, buffer, sizeof(buffer));
2058 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2059 return 0;
2060
2061 /* Get the title of the window */
2062 return GetWindowText(hwnd, name, namelen);
2063}
2064
2065 static BOOL CALLBACK
2066enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2067{
2068 struct server_id *id = (struct server_id *)lparam;
2069 char server[MAX_PATH];
2070
2071 /* Get the title of the window */
2072 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2073 return TRUE;
2074
2075 /* If this is the server we're looking for, return its HWND */
2076 if (STRICMP(server, id->name) == 0)
2077 {
2078 id->hwnd = hwnd;
2079 return FALSE;
2080 }
2081
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002082 /* If we are looking for an alternate server, remember this name. */
2083 if (altname_buf_ptr != NULL
2084 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2085 && vim_isdigit(server[STRLEN(id->name)]))
2086 {
2087 STRCPY(altname_buf_ptr, server);
2088 altname_buf_ptr = NULL; /* don't use another name */
2089 }
2090
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 /* Otherwise, keep looking */
2092 return TRUE;
2093}
2094
2095 static BOOL CALLBACK
2096enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2097{
2098 garray_T *ga = (garray_T *)lparam;
2099 char server[MAX_PATH];
2100
2101 /* Get the title of the window */
2102 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2103 return TRUE;
2104
2105 /* Add the name to the list */
2106 ga_concat(ga, server);
2107 ga_concat(ga, "\n");
2108 return TRUE;
2109}
2110
2111 static HWND
2112findServer(char_u *name)
2113{
2114 struct server_id id;
2115
2116 id.name = name;
2117 id.hwnd = 0;
2118
2119 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2120
2121 return id.hwnd;
2122}
2123
2124 void
2125serverSetName(char_u *name)
2126{
2127 char_u *ok_name;
2128 HWND hwnd = 0;
2129 int i = 0;
2130 char_u *p;
2131
2132 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002133 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
2135 STRCPY(ok_name, name);
2136 p = ok_name + STRLEN(name);
2137
2138 for (;;)
2139 {
2140 /* This is inefficient - we're doing an EnumWindows loop for each
2141 * possible name. It would be better to grab all names in one go,
2142 * and scan the list each time...
2143 */
2144 hwnd = findServer(ok_name);
2145 if (hwnd == 0)
2146 break;
2147
2148 ++i;
2149 if (i >= 1000)
2150 break;
2151
2152 sprintf((char *)p, "%d", i);
2153 }
2154
2155 if (hwnd != 0)
2156 vim_free(ok_name);
2157 else
2158 {
2159 /* Remember the name */
2160 serverName = ok_name;
2161#ifdef FEAT_TITLE
2162 need_maketitle = TRUE; /* update Vim window title later */
2163#endif
2164
2165 /* Update the message window title */
2166 SetWindowText(message_window, ok_name);
2167
2168#ifdef FEAT_EVAL
2169 /* Set the servername variable */
2170 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2171#endif
2172 }
2173}
2174
2175 char_u *
2176serverGetVimNames(void)
2177{
2178 garray_T ga;
2179
2180 ga_init2(&ga, 1, 100);
2181
2182 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002183 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
2185 return ga.ga_data;
2186}
2187
2188 int
2189serverSendReply(name, reply)
2190 char_u *name; /* Where to send. */
2191 char_u *reply; /* What to send. */
2192{
2193 HWND target;
2194 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002195 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
2197 /* The "name" argument is a magic cookie obtained from expand("<client>").
2198 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2199 * value of the client's message window HWND.
2200 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002201 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (n == 0)
2203 return -1;
2204
2205 target = (HWND)n;
2206 if (!IsWindow(target))
2207 return -1;
2208
2209 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002210 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 data.lpData = reply;
2212
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002213 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2215 (LPARAM)(&data)))
2216 return 0;
2217
2218 return -1;
2219}
2220
2221 int
2222serverSendToVim(name, cmd, result, ptarget, asExpr, silent)
2223 char_u *name; /* Where to send. */
2224 char_u *cmd; /* What to send. */
2225 char_u **result; /* Result of eval'ed expression */
2226 void *ptarget; /* HWND of server */
2227 int asExpr; /* Expression or keys? */
2228 int silent; /* don't complain about no server */
2229{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002230 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 COPYDATASTRUCT data;
2232 char_u *retval = NULL;
2233 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002234 char_u altname_buf[MAX_PATH];
2235
2236 /* If the server name does not end in a digit then we look for an
2237 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2238 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2239 altname_buf_ptr = altname_buf;
2240 altname_buf[0] = NUL;
2241 target = findServer(name);
2242 altname_buf_ptr = NULL;
2243 if (target == 0 && altname_buf[0] != NUL)
2244 /* Use another server name we found. */
2245 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
2247 if (target == 0)
2248 {
2249 if (!silent)
2250 EMSG2(_(e_noserver), name);
2251 return -1;
2252 }
2253
2254 if (ptarget)
2255 *(HWND *)ptarget = target;
2256
2257 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002258 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 data.lpData = cmd;
2260
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002261 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2263 (LPARAM)(&data)) == 0)
2264 return -1;
2265
2266 if (asExpr)
2267 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2268
2269 if (result == NULL)
2270 vim_free(retval);
2271 else
2272 *result = retval; /* Caller assumes responsibility for freeing */
2273
2274 return retcode;
2275}
2276
2277/*
2278 * Bring the server to the foreground.
2279 */
2280 void
2281serverForeground(name)
2282 char_u *name;
2283{
2284 HWND target = findServer(name);
2285
2286 if (target != 0)
2287 SetForegroundWindow(target);
2288}
2289
2290/* Replies from server need to be stored until the client picks them up via
2291 * remote_read(). So we maintain a list of server-id/reply pairs.
2292 * Note that there could be multiple replies from one server pending if the
2293 * client is slow picking them up.
2294 * We just store the replies in a simple list. When we remove an entry, we
2295 * move list entries down to fill the gap.
2296 * The server ID is simply the HWND.
2297 */
2298typedef struct
2299{
2300 HWND server; /* server window */
2301 char_u *reply; /* reply string */
2302 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002303} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2306
2307#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2308#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
2310/* Flag which is used to wait for a reply */
2311static int reply_received = 0;
2312
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002313/*
2314 * Store a reply. "reply" must be allocated memory (or NULL).
2315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 static int
2317save_reply(HWND server, char_u *reply, int expr)
2318{
2319 reply_T *rep;
2320
2321 if (ga_grow(&reply_list, 1) == FAIL)
2322 return FAIL;
2323
2324 rep = REPLY_ITEM(REPLY_COUNT);
2325 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002326 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 rep->expr_result = expr;
2328 if (rep->reply == NULL)
2329 return FAIL;
2330
2331 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 reply_received = 1;
2333 return OK;
2334}
2335
2336/*
2337 * Get a reply from server "server".
2338 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2339 * server2client() message.
2340 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2341 * If "remove" is TRUE, consume the message, the caller must free it then.
2342 * if "wait" is TRUE block until a message arrives (or the server exits).
2343 */
2344 char_u *
2345serverGetReply(HWND server, int *expr_res, int remove, int wait)
2346{
2347 int i;
2348 char_u *reply;
2349 reply_T *rep;
2350
2351 /* When waiting, loop until the message waiting for is received. */
2352 for (;;)
2353 {
2354 /* Reset this here, in case a message arrives while we are going
2355 * through the already received messages. */
2356 reply_received = 0;
2357
2358 for (i = 0; i < REPLY_COUNT; ++i)
2359 {
2360 rep = REPLY_ITEM(i);
2361 if (rep->server == server
2362 && ((rep->expr_result != 0) == (expr_res != NULL)))
2363 {
2364 /* Save the values we've found for later */
2365 reply = rep->reply;
2366 if (expr_res != NULL)
2367 *expr_res = rep->expr_result == 1 ? 0 : -1;
2368
2369 if (remove)
2370 {
2371 /* Move the rest of the list down to fill the gap */
2372 mch_memmove(rep, rep + 1,
2373 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2374 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
2376
2377 /* Return the reply to the caller, who takes on responsibility
2378 * for freeing it if "remove" is TRUE. */
2379 return reply;
2380 }
2381 }
2382
2383 /* If we got here, we didn't find a reply. Return immediately if the
2384 * "wait" parameter isn't set. */
2385 if (!wait)
2386 break;
2387
2388 /* We need to wait for a reply. Enter a message loop until the
2389 * "reply_received" flag gets set. */
2390
2391 /* Loop until we receive a reply */
2392 while (reply_received == 0)
2393 {
2394 /* Wait for a SendMessage() call to us. This could be the reply
2395 * we are waiting for. Use a timeout of a second, to catch the
2396 * situation that the server died unexpectedly. */
2397 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2398
2399 /* If the server has died, give up */
2400 if (!IsWindow(server))
2401 return NULL;
2402
2403 serverProcessPendingMessages();
2404 }
2405 }
2406
2407 return NULL;
2408}
2409
2410/*
2411 * Process any messages in the Windows message queue.
2412 */
2413 void
2414serverProcessPendingMessages(void)
2415{
2416 MSG msg;
2417
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002418 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
2420 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002421 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
2423}
2424
2425#endif /* FEAT_CLIENTSERVER */
2426
2427#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2428 || defined(PROTO)
2429
2430struct charset_pair
2431{
2432 char *name;
2433 BYTE charset;
2434};
2435
2436static struct charset_pair
2437charset_pairs[] =
2438{
2439 {"ANSI", ANSI_CHARSET},
2440 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2441 {"DEFAULT", DEFAULT_CHARSET},
2442 {"HANGEUL", HANGEUL_CHARSET},
2443 {"OEM", OEM_CHARSET},
2444 {"SHIFTJIS", SHIFTJIS_CHARSET},
2445 {"SYMBOL", SYMBOL_CHARSET},
2446#ifdef WIN3264
2447 {"ARABIC", ARABIC_CHARSET},
2448 {"BALTIC", BALTIC_CHARSET},
2449 {"EASTEUROPE", EASTEUROPE_CHARSET},
2450 {"GB2312", GB2312_CHARSET},
2451 {"GREEK", GREEK_CHARSET},
2452 {"HEBREW", HEBREW_CHARSET},
2453 {"JOHAB", JOHAB_CHARSET},
2454 {"MAC", MAC_CHARSET},
2455 {"RUSSIAN", RUSSIAN_CHARSET},
2456 {"THAI", THAI_CHARSET},
2457 {"TURKISH", TURKISH_CHARSET},
2458# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
2459 && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
2460 {"VIETNAMESE", VIETNAMESE_CHARSET},
2461# endif
2462#endif
2463 {NULL, 0}
2464};
2465
2466/*
2467 * Convert a charset ID to a name.
2468 * Return NULL when not recognized.
2469 */
2470 char *
2471charset_id2name(int id)
2472{
2473 struct charset_pair *cp;
2474
2475 for (cp = charset_pairs; cp->name != NULL; ++cp)
2476 if ((BYTE)id == cp->charset)
2477 break;
2478 return cp->name;
2479}
2480
2481static const LOGFONT s_lfDefault =
2482{
2483 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2484 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2485 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2486 "Fixedsys" /* see _ReadVimIni */
2487};
2488
2489/* Initialise the "current height" to -12 (same as s_lfDefault) just
2490 * in case the user specifies a font in "guifont" with no size before a font
2491 * with an explicit size has been set. This defaults the size to this value
2492 * (-12 equates to roughly 9pt).
2493 */
2494int current_font_height = -12; /* also used in gui_w48.c */
2495
2496/* Convert a string representing a point size into pixels. The string should
2497 * be a positive decimal number, with an optional decimal point (eg, "12", or
2498 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2499 * character is stored in *end. The flag "vertical" says whether this
2500 * calculation is for a vertical (height) size or a horizontal (width) one.
2501 */
2502 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002503points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
2505 int pixels;
2506 int points = 0;
2507 int divisor = 0;
2508 HWND hwnd = (HWND)0;
2509 HDC hdc;
2510 HDC printer_dc = (HDC)pprinter_dc;
2511
2512 while (*str != NUL)
2513 {
2514 if (*str == '.' && divisor == 0)
2515 {
2516 /* Start keeping a divisor, for later */
2517 divisor = 1;
2518 }
2519 else
2520 {
2521 if (!VIM_ISDIGIT(*str))
2522 break;
2523
2524 points *= 10;
2525 points += *str - '0';
2526 divisor *= 10;
2527 }
2528 ++str;
2529 }
2530
2531 if (divisor == 0)
2532 divisor = 1;
2533
2534 if (printer_dc == NULL)
2535 {
2536 hwnd = GetDesktopWindow();
2537 hdc = GetWindowDC(hwnd);
2538 }
2539 else
2540 hdc = printer_dc;
2541
2542 pixels = MulDiv(points,
2543 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2544 72 * divisor);
2545
2546 if (printer_dc == NULL)
2547 ReleaseDC(hwnd, hdc);
2548
2549 *end = str;
2550 return pixels;
2551}
2552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002553/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 static int CALLBACK
2555font_enumproc(
2556 ENUMLOGFONT *elf,
2557 NEWTEXTMETRIC *ntm,
2558 int type,
2559 LPARAM lparam)
2560{
2561 /* Return value:
2562 * 0 = terminate now (monospace & ANSI)
2563 * 1 = continue, still no luck...
2564 * 2 = continue, but we have an acceptable LOGFONT
2565 * (monospace, not ANSI)
2566 * We use these values, as EnumFontFamilies returns 1 if the
2567 * callback function is never called. So, we check the return as
2568 * 0 = perfect, 2 = OK, 1 = no good...
2569 * It's not pretty, but it works!
2570 */
2571
2572 LOGFONT *lf = (LOGFONT *)(lparam);
2573
2574#ifndef FEAT_PROPORTIONAL_FONTS
2575 /* Ignore non-monospace fonts without further ado */
2576 if ((ntm->tmPitchAndFamily & 1) != 0)
2577 return 1;
2578#endif
2579
2580 /* Remember this LOGFONT as a "possible" */
2581 *lf = elf->elfLogFont;
2582
2583 /* Terminate the scan as soon as we find an ANSI font */
2584 if (lf->lfCharSet == ANSI_CHARSET
2585 || lf->lfCharSet == OEM_CHARSET
2586 || lf->lfCharSet == DEFAULT_CHARSET)
2587 return 0;
2588
2589 /* Continue the scan - we have a non-ANSI font */
2590 return 2;
2591}
2592
2593 static int
2594init_logfont(LOGFONT *lf)
2595{
2596 int n;
2597 HWND hwnd = GetDesktopWindow();
2598 HDC hdc = GetWindowDC(hwnd);
2599
2600 n = EnumFontFamilies(hdc,
2601 (LPCSTR)lf->lfFaceName,
2602 (FONTENUMPROC)font_enumproc,
2603 (LPARAM)lf);
2604
2605 ReleaseDC(hwnd, hdc);
2606
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002607 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 if (n == 1)
2609 return FAIL;
2610
2611 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2612 * font - get_logfont() sets bold, italic, etc based on the user's
2613 * input.
2614 */
2615 lf->lfHeight = current_font_height;
2616 lf->lfWidth = 0;
2617 lf->lfItalic = FALSE;
2618 lf->lfUnderline = FALSE;
2619 lf->lfStrikeOut = FALSE;
2620 lf->lfWeight = FW_NORMAL;
2621
2622 /* Return success */
2623 return OK;
2624}
2625
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002626/*
2627 * Get font info from "name" into logfont "lf".
2628 * Return OK for a valid name, FAIL otherwise.
2629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 int
2631get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002632 LOGFONT *lf,
2633 char_u *name,
2634 HDC printer_dc,
2635 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
2637 char_u *p;
2638 int i;
2639 static LOGFONT *lastlf = NULL;
2640
2641 *lf = s_lfDefault;
2642 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002643 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
2645 if (STRCMP(name, "*") == 0)
2646 {
2647#if defined(FEAT_GUI_W32)
2648 CHOOSEFONT cf;
2649 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002650 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 cf.lStructSize = sizeof(cf);
2652 cf.hwndOwner = s_hwnd;
2653 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2654 if (lastlf != NULL)
2655 *lf = *lastlf;
2656 cf.lpLogFont = lf;
2657 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2658 if (ChooseFont(&cf))
2659 goto theend;
2660#else
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002661 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662#endif
2663 }
2664
2665 /*
2666 * Split name up, it could be <name>:h<height>:w<width> etc.
2667 */
2668 for (p = name; *p && *p != ':'; p++)
2669 {
2670 if (p - name + 1 > LF_FACESIZE)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002671 return FAIL; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 lf->lfFaceName[p - name] = *p;
2673 }
2674 if (p != name)
2675 lf->lfFaceName[p - name] = NUL;
2676
2677 /* First set defaults */
2678 lf->lfHeight = -12;
2679 lf->lfWidth = 0;
2680 lf->lfWeight = FW_NORMAL;
2681 lf->lfItalic = FALSE;
2682 lf->lfUnderline = FALSE;
2683 lf->lfStrikeOut = FALSE;
2684
2685 /*
2686 * If the font can't be found, try replacing '_' by ' '.
2687 */
2688 if (init_logfont(lf) == FAIL)
2689 {
2690 int did_replace = FALSE;
2691
2692 for (i = 0; lf->lfFaceName[i]; ++i)
2693 if (lf->lfFaceName[i] == '_')
2694 {
2695 lf->lfFaceName[i] = ' ';
2696 did_replace = TRUE;
2697 }
2698 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002699 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701
2702 while (*p == ':')
2703 p++;
2704
2705 /* Set the values found after ':' */
2706 while (*p)
2707 {
2708 switch (*p++)
2709 {
2710 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002711 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 break;
2713 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002714 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 break;
2716 case 'b':
2717#ifndef MSWIN16_FASTTEXT
2718 lf->lfWeight = FW_BOLD;
2719#endif
2720 break;
2721 case 'i':
2722#ifndef MSWIN16_FASTTEXT
2723 lf->lfItalic = TRUE;
2724#endif
2725 break;
2726 case 'u':
2727 lf->lfUnderline = TRUE;
2728 break;
2729 case 's':
2730 lf->lfStrikeOut = TRUE;
2731 break;
2732 case 'c':
2733 {
2734 struct charset_pair *cp;
2735
2736 for (cp = charset_pairs; cp->name != NULL; ++cp)
2737 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
2738 {
2739 lf->lfCharSet = cp->charset;
2740 p += strlen(cp->name);
2741 break;
2742 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002743 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002745 vim_snprintf((char *)IObuff, IOSIZE,
2746 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 EMSG(IObuff);
2748 break;
2749 }
2750 break;
2751 }
2752 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002753 if (verbose)
2754 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002755 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002756 _("E245: Illegal char '%c' in font name \"%s\""),
2757 p[-1], name);
2758 EMSG(IObuff);
2759 }
2760 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
2762 while (*p == ':')
2763 p++;
2764 }
2765
2766#if defined(FEAT_GUI_W32)
2767theend:
2768#endif
2769 /* ron: init lastlf */
2770 if (printer_dc == NULL)
2771 {
2772 vim_free(lastlf);
2773 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
2774 if (lastlf != NULL)
2775 mch_memmove(lastlf, lf, sizeof(LOGFONT));
2776 }
2777
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002778 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779}
2780
2781#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */