blob: e8000aa6b6fa549f4bfa6ac2ea74fa3f1dcbb18b [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 */
1797HWND message_window = 0; /* window that's handling messsages */
1798
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
1860/*s
1861 * 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;
1900 char_u winstr[30];
1901 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001902 char_u *str;
1903 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
1905 switch (data->dwData)
1906 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001907 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001908# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001909 /* Remember the encoding that the client uses. */
1910 vim_free(client_enc);
1911 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001912# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001913 return 1;
1914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 case COPYDATA_KEYS:
1916 /* Remember who sent this, for <client> */
1917 clientWindow = sender;
1918
1919 /* Add the received keys to the input buffer. The loop waiting
1920 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001921 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
1922 server_to_input_buf(str);
1923 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
1925# ifdef FEAT_GUI
1926 /* Wake up the main GUI loop. */
1927 if (s_hwnd != 0)
1928 PostMessage(s_hwnd, WM_NULL, 0, 0);
1929# endif
1930 return 1;
1931
1932 case COPYDATA_EXPR:
1933 /* Remember who sent this, for <client> */
1934 clientWindow = sender;
1935
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001936 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
1937 res = eval_client_expr_to_string(str);
1938 vim_free(tofree);
1939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 if (res == NULL)
1941 {
1942 res = vim_strsave(_(e_invexprmsg));
1943 reply.dwData = COPYDATA_ERROR_RESULT;
1944 }
1945 else
1946 reply.dwData = COPYDATA_RESULT;
1947 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001948 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001950 serverSendEnc(sender);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001951 retval = (int)SendMessage(sender, WM_COPYDATA, (WPARAM)message_window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 (LPARAM)(&reply));
1953 vim_free(res);
1954 return retval;
1955
1956 case COPYDATA_REPLY:
1957 case COPYDATA_RESULT:
1958 case COPYDATA_ERROR_RESULT:
1959 if (data->lpData != NULL)
1960 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001961 str = serverConvert(client_enc, (char_u *)data->lpData,
1962 &tofree);
1963 if (tofree == NULL)
1964 str = vim_strsave(str);
1965 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 (data->dwData == COPYDATA_REPLY ? 0 :
1967 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001968 2))) == FAIL)
1969 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001971 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001973 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001974 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 TRUE, curbuf);
1976 }
1977#endif
1978 }
1979 return 1;
1980 }
1981
1982 return 0;
1983 }
1984
1985 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
1986 {
1987 /* When the message window is activated (brought to the foreground),
1988 * this actually applies to the text window. */
1989#ifndef FEAT_GUI
1990 GetConsoleHwnd(); /* get value of s_hwnd */
1991#endif
1992 if (s_hwnd != 0)
1993 {
1994 SetForegroundWindow(s_hwnd);
1995 return 0;
1996 }
1997 }
1998
1999 return DefWindowProc(hwnd, msg, wParam, lParam);
2000}
2001
2002/*
2003 * Initialise the message handling process. This involves creating a window
2004 * to handle messages - the window will not be visible.
2005 */
2006 void
2007serverInitMessaging(void)
2008{
2009 WNDCLASS wndclass;
2010 HINSTANCE s_hinst;
2011
2012 /* Clean up on exit */
2013 atexit(CleanUpMessaging);
2014
2015 /* Register a window class - we only really care
2016 * about the window procedure
2017 */
2018 s_hinst = (HINSTANCE)GetModuleHandle(0);
2019 wndclass.style = 0;
2020 wndclass.lpfnWndProc = Messaging_WndProc;
2021 wndclass.cbClsExtra = 0;
2022 wndclass.cbWndExtra = 0;
2023 wndclass.hInstance = s_hinst;
2024 wndclass.hIcon = NULL;
2025 wndclass.hCursor = NULL;
2026 wndclass.hbrBackground = NULL;
2027 wndclass.lpszMenuName = NULL;
2028 wndclass.lpszClassName = VIM_CLASSNAME;
2029 RegisterClass(&wndclass);
2030
2031 /* Create the message window. It will be hidden, so the details don't
2032 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2033 * focus from gvim. */
2034 message_window = CreateWindow(VIM_CLASSNAME, "",
2035 WS_POPUPWINDOW | WS_CAPTION,
2036 CW_USEDEFAULT, CW_USEDEFAULT,
2037 100, 100, NULL, NULL,
2038 s_hinst, NULL);
2039}
2040
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002041/* Used by serverSendToVim() to find an alternate server name. */
2042static char_u *altname_buf_ptr = NULL;
2043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044/*
2045 * Get the title of the window "hwnd", which is the Vim server name, in
2046 * "name[namelen]" and return the length.
2047 * Returns zero if window "hwnd" is not a Vim server.
2048 */
2049 static int
2050getVimServerName(HWND hwnd, char *name, int namelen)
2051{
2052 int len;
2053 char buffer[VIM_CLASSNAME_LEN + 1];
2054
2055 /* Ignore windows which aren't Vim message windows */
2056 len = GetClassName(hwnd, buffer, sizeof(buffer));
2057 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2058 return 0;
2059
2060 /* Get the title of the window */
2061 return GetWindowText(hwnd, name, namelen);
2062}
2063
2064 static BOOL CALLBACK
2065enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2066{
2067 struct server_id *id = (struct server_id *)lparam;
2068 char server[MAX_PATH];
2069
2070 /* Get the title of the window */
2071 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2072 return TRUE;
2073
2074 /* If this is the server we're looking for, return its HWND */
2075 if (STRICMP(server, id->name) == 0)
2076 {
2077 id->hwnd = hwnd;
2078 return FALSE;
2079 }
2080
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002081 /* If we are looking for an alternate server, remember this name. */
2082 if (altname_buf_ptr != NULL
2083 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2084 && vim_isdigit(server[STRLEN(id->name)]))
2085 {
2086 STRCPY(altname_buf_ptr, server);
2087 altname_buf_ptr = NULL; /* don't use another name */
2088 }
2089
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 /* Otherwise, keep looking */
2091 return TRUE;
2092}
2093
2094 static BOOL CALLBACK
2095enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2096{
2097 garray_T *ga = (garray_T *)lparam;
2098 char server[MAX_PATH];
2099
2100 /* Get the title of the window */
2101 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2102 return TRUE;
2103
2104 /* Add the name to the list */
2105 ga_concat(ga, server);
2106 ga_concat(ga, "\n");
2107 return TRUE;
2108}
2109
2110 static HWND
2111findServer(char_u *name)
2112{
2113 struct server_id id;
2114
2115 id.name = name;
2116 id.hwnd = 0;
2117
2118 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2119
2120 return id.hwnd;
2121}
2122
2123 void
2124serverSetName(char_u *name)
2125{
2126 char_u *ok_name;
2127 HWND hwnd = 0;
2128 int i = 0;
2129 char_u *p;
2130
2131 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002132 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
2134 STRCPY(ok_name, name);
2135 p = ok_name + STRLEN(name);
2136
2137 for (;;)
2138 {
2139 /* This is inefficient - we're doing an EnumWindows loop for each
2140 * possible name. It would be better to grab all names in one go,
2141 * and scan the list each time...
2142 */
2143 hwnd = findServer(ok_name);
2144 if (hwnd == 0)
2145 break;
2146
2147 ++i;
2148 if (i >= 1000)
2149 break;
2150
2151 sprintf((char *)p, "%d", i);
2152 }
2153
2154 if (hwnd != 0)
2155 vim_free(ok_name);
2156 else
2157 {
2158 /* Remember the name */
2159 serverName = ok_name;
2160#ifdef FEAT_TITLE
2161 need_maketitle = TRUE; /* update Vim window title later */
2162#endif
2163
2164 /* Update the message window title */
2165 SetWindowText(message_window, ok_name);
2166
2167#ifdef FEAT_EVAL
2168 /* Set the servername variable */
2169 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2170#endif
2171 }
2172}
2173
2174 char_u *
2175serverGetVimNames(void)
2176{
2177 garray_T ga;
2178
2179 ga_init2(&ga, 1, 100);
2180
2181 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002182 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
2184 return ga.ga_data;
2185}
2186
2187 int
2188serverSendReply(name, reply)
2189 char_u *name; /* Where to send. */
2190 char_u *reply; /* What to send. */
2191{
2192 HWND target;
2193 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002194 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196 /* The "name" argument is a magic cookie obtained from expand("<client>").
2197 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2198 * value of the client's message window HWND.
2199 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002200 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (n == 0)
2202 return -1;
2203
2204 target = (HWND)n;
2205 if (!IsWindow(target))
2206 return -1;
2207
2208 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002209 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 data.lpData = reply;
2211
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002212 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2214 (LPARAM)(&data)))
2215 return 0;
2216
2217 return -1;
2218}
2219
2220 int
2221serverSendToVim(name, cmd, result, ptarget, asExpr, silent)
2222 char_u *name; /* Where to send. */
2223 char_u *cmd; /* What to send. */
2224 char_u **result; /* Result of eval'ed expression */
2225 void *ptarget; /* HWND of server */
2226 int asExpr; /* Expression or keys? */
2227 int silent; /* don't complain about no server */
2228{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002229 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 COPYDATASTRUCT data;
2231 char_u *retval = NULL;
2232 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002233 char_u altname_buf[MAX_PATH];
2234
2235 /* If the server name does not end in a digit then we look for an
2236 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2237 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2238 altname_buf_ptr = altname_buf;
2239 altname_buf[0] = NUL;
2240 target = findServer(name);
2241 altname_buf_ptr = NULL;
2242 if (target == 0 && altname_buf[0] != NUL)
2243 /* Use another server name we found. */
2244 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 if (target == 0)
2247 {
2248 if (!silent)
2249 EMSG2(_(e_noserver), name);
2250 return -1;
2251 }
2252
2253 if (ptarget)
2254 *(HWND *)ptarget = target;
2255
2256 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002257 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 data.lpData = cmd;
2259
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002260 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2262 (LPARAM)(&data)) == 0)
2263 return -1;
2264
2265 if (asExpr)
2266 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2267
2268 if (result == NULL)
2269 vim_free(retval);
2270 else
2271 *result = retval; /* Caller assumes responsibility for freeing */
2272
2273 return retcode;
2274}
2275
2276/*
2277 * Bring the server to the foreground.
2278 */
2279 void
2280serverForeground(name)
2281 char_u *name;
2282{
2283 HWND target = findServer(name);
2284
2285 if (target != 0)
2286 SetForegroundWindow(target);
2287}
2288
2289/* Replies from server need to be stored until the client picks them up via
2290 * remote_read(). So we maintain a list of server-id/reply pairs.
2291 * Note that there could be multiple replies from one server pending if the
2292 * client is slow picking them up.
2293 * We just store the replies in a simple list. When we remove an entry, we
2294 * move list entries down to fill the gap.
2295 * The server ID is simply the HWND.
2296 */
2297typedef struct
2298{
2299 HWND server; /* server window */
2300 char_u *reply; /* reply string */
2301 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002302} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2305
2306#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2307#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
2309/* Flag which is used to wait for a reply */
2310static int reply_received = 0;
2311
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002312/*
2313 * Store a reply. "reply" must be allocated memory (or NULL).
2314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 static int
2316save_reply(HWND server, char_u *reply, int expr)
2317{
2318 reply_T *rep;
2319
2320 if (ga_grow(&reply_list, 1) == FAIL)
2321 return FAIL;
2322
2323 rep = REPLY_ITEM(REPLY_COUNT);
2324 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002325 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 rep->expr_result = expr;
2327 if (rep->reply == NULL)
2328 return FAIL;
2329
2330 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 reply_received = 1;
2332 return OK;
2333}
2334
2335/*
2336 * Get a reply from server "server".
2337 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2338 * server2client() message.
2339 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2340 * If "remove" is TRUE, consume the message, the caller must free it then.
2341 * if "wait" is TRUE block until a message arrives (or the server exits).
2342 */
2343 char_u *
2344serverGetReply(HWND server, int *expr_res, int remove, int wait)
2345{
2346 int i;
2347 char_u *reply;
2348 reply_T *rep;
2349
2350 /* When waiting, loop until the message waiting for is received. */
2351 for (;;)
2352 {
2353 /* Reset this here, in case a message arrives while we are going
2354 * through the already received messages. */
2355 reply_received = 0;
2356
2357 for (i = 0; i < REPLY_COUNT; ++i)
2358 {
2359 rep = REPLY_ITEM(i);
2360 if (rep->server == server
2361 && ((rep->expr_result != 0) == (expr_res != NULL)))
2362 {
2363 /* Save the values we've found for later */
2364 reply = rep->reply;
2365 if (expr_res != NULL)
2366 *expr_res = rep->expr_result == 1 ? 0 : -1;
2367
2368 if (remove)
2369 {
2370 /* Move the rest of the list down to fill the gap */
2371 mch_memmove(rep, rep + 1,
2372 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2373 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
2375
2376 /* Return the reply to the caller, who takes on responsibility
2377 * for freeing it if "remove" is TRUE. */
2378 return reply;
2379 }
2380 }
2381
2382 /* If we got here, we didn't find a reply. Return immediately if the
2383 * "wait" parameter isn't set. */
2384 if (!wait)
2385 break;
2386
2387 /* We need to wait for a reply. Enter a message loop until the
2388 * "reply_received" flag gets set. */
2389
2390 /* Loop until we receive a reply */
2391 while (reply_received == 0)
2392 {
2393 /* Wait for a SendMessage() call to us. This could be the reply
2394 * we are waiting for. Use a timeout of a second, to catch the
2395 * situation that the server died unexpectedly. */
2396 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2397
2398 /* If the server has died, give up */
2399 if (!IsWindow(server))
2400 return NULL;
2401
2402 serverProcessPendingMessages();
2403 }
2404 }
2405
2406 return NULL;
2407}
2408
2409/*
2410 * Process any messages in the Windows message queue.
2411 */
2412 void
2413serverProcessPendingMessages(void)
2414{
2415 MSG msg;
2416
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002417 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
2419 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002420 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422}
2423
2424#endif /* FEAT_CLIENTSERVER */
2425
2426#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2427 || defined(PROTO)
2428
2429struct charset_pair
2430{
2431 char *name;
2432 BYTE charset;
2433};
2434
2435static struct charset_pair
2436charset_pairs[] =
2437{
2438 {"ANSI", ANSI_CHARSET},
2439 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2440 {"DEFAULT", DEFAULT_CHARSET},
2441 {"HANGEUL", HANGEUL_CHARSET},
2442 {"OEM", OEM_CHARSET},
2443 {"SHIFTJIS", SHIFTJIS_CHARSET},
2444 {"SYMBOL", SYMBOL_CHARSET},
2445#ifdef WIN3264
2446 {"ARABIC", ARABIC_CHARSET},
2447 {"BALTIC", BALTIC_CHARSET},
2448 {"EASTEUROPE", EASTEUROPE_CHARSET},
2449 {"GB2312", GB2312_CHARSET},
2450 {"GREEK", GREEK_CHARSET},
2451 {"HEBREW", HEBREW_CHARSET},
2452 {"JOHAB", JOHAB_CHARSET},
2453 {"MAC", MAC_CHARSET},
2454 {"RUSSIAN", RUSSIAN_CHARSET},
2455 {"THAI", THAI_CHARSET},
2456 {"TURKISH", TURKISH_CHARSET},
2457# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
2458 && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
2459 {"VIETNAMESE", VIETNAMESE_CHARSET},
2460# endif
2461#endif
2462 {NULL, 0}
2463};
2464
2465/*
2466 * Convert a charset ID to a name.
2467 * Return NULL when not recognized.
2468 */
2469 char *
2470charset_id2name(int id)
2471{
2472 struct charset_pair *cp;
2473
2474 for (cp = charset_pairs; cp->name != NULL; ++cp)
2475 if ((BYTE)id == cp->charset)
2476 break;
2477 return cp->name;
2478}
2479
2480static const LOGFONT s_lfDefault =
2481{
2482 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2483 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2484 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2485 "Fixedsys" /* see _ReadVimIni */
2486};
2487
2488/* Initialise the "current height" to -12 (same as s_lfDefault) just
2489 * in case the user specifies a font in "guifont" with no size before a font
2490 * with an explicit size has been set. This defaults the size to this value
2491 * (-12 equates to roughly 9pt).
2492 */
2493int current_font_height = -12; /* also used in gui_w48.c */
2494
2495/* Convert a string representing a point size into pixels. The string should
2496 * be a positive decimal number, with an optional decimal point (eg, "12", or
2497 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2498 * character is stored in *end. The flag "vertical" says whether this
2499 * calculation is for a vertical (height) size or a horizontal (width) one.
2500 */
2501 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002502points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503{
2504 int pixels;
2505 int points = 0;
2506 int divisor = 0;
2507 HWND hwnd = (HWND)0;
2508 HDC hdc;
2509 HDC printer_dc = (HDC)pprinter_dc;
2510
2511 while (*str != NUL)
2512 {
2513 if (*str == '.' && divisor == 0)
2514 {
2515 /* Start keeping a divisor, for later */
2516 divisor = 1;
2517 }
2518 else
2519 {
2520 if (!VIM_ISDIGIT(*str))
2521 break;
2522
2523 points *= 10;
2524 points += *str - '0';
2525 divisor *= 10;
2526 }
2527 ++str;
2528 }
2529
2530 if (divisor == 0)
2531 divisor = 1;
2532
2533 if (printer_dc == NULL)
2534 {
2535 hwnd = GetDesktopWindow();
2536 hdc = GetWindowDC(hwnd);
2537 }
2538 else
2539 hdc = printer_dc;
2540
2541 pixels = MulDiv(points,
2542 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2543 72 * divisor);
2544
2545 if (printer_dc == NULL)
2546 ReleaseDC(hwnd, hdc);
2547
2548 *end = str;
2549 return pixels;
2550}
2551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002552/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 static int CALLBACK
2554font_enumproc(
2555 ENUMLOGFONT *elf,
2556 NEWTEXTMETRIC *ntm,
2557 int type,
2558 LPARAM lparam)
2559{
2560 /* Return value:
2561 * 0 = terminate now (monospace & ANSI)
2562 * 1 = continue, still no luck...
2563 * 2 = continue, but we have an acceptable LOGFONT
2564 * (monospace, not ANSI)
2565 * We use these values, as EnumFontFamilies returns 1 if the
2566 * callback function is never called. So, we check the return as
2567 * 0 = perfect, 2 = OK, 1 = no good...
2568 * It's not pretty, but it works!
2569 */
2570
2571 LOGFONT *lf = (LOGFONT *)(lparam);
2572
2573#ifndef FEAT_PROPORTIONAL_FONTS
2574 /* Ignore non-monospace fonts without further ado */
2575 if ((ntm->tmPitchAndFamily & 1) != 0)
2576 return 1;
2577#endif
2578
2579 /* Remember this LOGFONT as a "possible" */
2580 *lf = elf->elfLogFont;
2581
2582 /* Terminate the scan as soon as we find an ANSI font */
2583 if (lf->lfCharSet == ANSI_CHARSET
2584 || lf->lfCharSet == OEM_CHARSET
2585 || lf->lfCharSet == DEFAULT_CHARSET)
2586 return 0;
2587
2588 /* Continue the scan - we have a non-ANSI font */
2589 return 2;
2590}
2591
2592 static int
2593init_logfont(LOGFONT *lf)
2594{
2595 int n;
2596 HWND hwnd = GetDesktopWindow();
2597 HDC hdc = GetWindowDC(hwnd);
2598
2599 n = EnumFontFamilies(hdc,
2600 (LPCSTR)lf->lfFaceName,
2601 (FONTENUMPROC)font_enumproc,
2602 (LPARAM)lf);
2603
2604 ReleaseDC(hwnd, hdc);
2605
2606 /* If we couldn't find a useable font, return failure */
2607 if (n == 1)
2608 return FAIL;
2609
2610 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2611 * font - get_logfont() sets bold, italic, etc based on the user's
2612 * input.
2613 */
2614 lf->lfHeight = current_font_height;
2615 lf->lfWidth = 0;
2616 lf->lfItalic = FALSE;
2617 lf->lfUnderline = FALSE;
2618 lf->lfStrikeOut = FALSE;
2619 lf->lfWeight = FW_NORMAL;
2620
2621 /* Return success */
2622 return OK;
2623}
2624
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002625/*
2626 * Get font info from "name" into logfont "lf".
2627 * Return OK for a valid name, FAIL otherwise.
2628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 int
2630get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002631 LOGFONT *lf,
2632 char_u *name,
2633 HDC printer_dc,
2634 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
2636 char_u *p;
2637 int i;
2638 static LOGFONT *lastlf = NULL;
2639
2640 *lf = s_lfDefault;
2641 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002642 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
2644 if (STRCMP(name, "*") == 0)
2645 {
2646#if defined(FEAT_GUI_W32)
2647 CHOOSEFONT cf;
2648 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002649 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 cf.lStructSize = sizeof(cf);
2651 cf.hwndOwner = s_hwnd;
2652 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2653 if (lastlf != NULL)
2654 *lf = *lastlf;
2655 cf.lpLogFont = lf;
2656 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2657 if (ChooseFont(&cf))
2658 goto theend;
2659#else
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002660 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661#endif
2662 }
2663
2664 /*
2665 * Split name up, it could be <name>:h<height>:w<width> etc.
2666 */
2667 for (p = name; *p && *p != ':'; p++)
2668 {
2669 if (p - name + 1 > LF_FACESIZE)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002670 return FAIL; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 lf->lfFaceName[p - name] = *p;
2672 }
2673 if (p != name)
2674 lf->lfFaceName[p - name] = NUL;
2675
2676 /* First set defaults */
2677 lf->lfHeight = -12;
2678 lf->lfWidth = 0;
2679 lf->lfWeight = FW_NORMAL;
2680 lf->lfItalic = FALSE;
2681 lf->lfUnderline = FALSE;
2682 lf->lfStrikeOut = FALSE;
2683
2684 /*
2685 * If the font can't be found, try replacing '_' by ' '.
2686 */
2687 if (init_logfont(lf) == FAIL)
2688 {
2689 int did_replace = FALSE;
2690
2691 for (i = 0; lf->lfFaceName[i]; ++i)
2692 if (lf->lfFaceName[i] == '_')
2693 {
2694 lf->lfFaceName[i] = ' ';
2695 did_replace = TRUE;
2696 }
2697 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002698 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
2700
2701 while (*p == ':')
2702 p++;
2703
2704 /* Set the values found after ':' */
2705 while (*p)
2706 {
2707 switch (*p++)
2708 {
2709 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002710 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 break;
2712 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002713 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 break;
2715 case 'b':
2716#ifndef MSWIN16_FASTTEXT
2717 lf->lfWeight = FW_BOLD;
2718#endif
2719 break;
2720 case 'i':
2721#ifndef MSWIN16_FASTTEXT
2722 lf->lfItalic = TRUE;
2723#endif
2724 break;
2725 case 'u':
2726 lf->lfUnderline = TRUE;
2727 break;
2728 case 's':
2729 lf->lfStrikeOut = TRUE;
2730 break;
2731 case 'c':
2732 {
2733 struct charset_pair *cp;
2734
2735 for (cp = charset_pairs; cp->name != NULL; ++cp)
2736 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
2737 {
2738 lf->lfCharSet = cp->charset;
2739 p += strlen(cp->name);
2740 break;
2741 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002742 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002744 vim_snprintf((char *)IObuff, IOSIZE,
2745 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 EMSG(IObuff);
2747 break;
2748 }
2749 break;
2750 }
2751 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002752 if (verbose)
2753 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002754 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002755 _("E245: Illegal char '%c' in font name \"%s\""),
2756 p[-1], name);
2757 EMSG(IObuff);
2758 }
2759 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761 while (*p == ':')
2762 p++;
2763 }
2764
2765#if defined(FEAT_GUI_W32)
2766theend:
2767#endif
2768 /* ron: init lastlf */
2769 if (printer_dc == NULL)
2770 {
2771 vim_free(lastlf);
2772 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
2773 if (lastlf != NULL)
2774 mch_memmove(lastlf, lf, sizeof(LOGFONT));
2775 }
2776
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002777 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778}
2779
2780#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */