blob: 77affc0c3944ea9ee96264d9aa97441596973363 [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
25#include <io.h>
26#include "vim.h"
27
28#ifdef HAVE_FCNTL_H
29# include <fcntl.h>
30#endif
31#ifdef WIN16
32# define SHORT_FNAME /* always 8.3 file name */
33# include <dos.h>
34# include <string.h>
35#endif
36#include <sys/types.h>
37#include <errno.h>
38#include <signal.h>
39#include <limits.h>
40#include <process.h>
41
42#undef chdir
43#ifdef __GNUC__
44# ifndef __MINGW32__
45# include <dirent.h>
46# endif
47#else
48# include <direct.h>
49#endif
50
51#if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32)
52# include <shellapi.h>
53#endif
54
55#if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
56# include <dlgs.h>
57# ifdef WIN3264
58# include <winspool.h>
59# else
60# include <print.h>
61# endif
62# include <commdlg.h>
63#endif
64
65#ifdef __MINGW32__
66# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
67# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
68# endif
69# ifndef RIGHTMOST_BUTTON_PRESSED
70# define RIGHTMOST_BUTTON_PRESSED 0x0002
71# endif
72# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
73# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
74# endif
75# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
76# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
77# endif
78# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
79# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
80# endif
81
82/*
83 * EventFlags
84 */
85# ifndef MOUSE_MOVED
86# define MOUSE_MOVED 0x0001
87# endif
88# ifndef DOUBLE_CLICK
89# define DOUBLE_CLICK 0x0002
90# endif
91#endif
92
93/*
94 * When generating prototypes for Win32 on Unix, these lines make the syntax
95 * errors disappear. They do not need to be correct.
96 */
97#ifdef PROTO
98#define WINAPI
99#define WINBASEAPI
100typedef int BOOL;
101typedef int CALLBACK;
102typedef int COLORREF;
103typedef int CONSOLE_CURSOR_INFO;
104typedef int COORD;
105typedef int DWORD;
106typedef int ENUMLOGFONT;
107typedef int HANDLE;
108typedef int HDC;
109typedef int HFONT;
110typedef int HICON;
111typedef int HWND;
112typedef int INPUT_RECORD;
113typedef int KEY_EVENT_RECORD;
114typedef int LOGFONT;
115typedef int LPARAM;
116typedef int LPBOOL;
117typedef int LPCSTR;
118typedef int LPCWSTR;
119typedef int LPSTR;
120typedef int LPTSTR;
121typedef int LPWSTR;
122typedef int LRESULT;
123typedef int MOUSE_EVENT_RECORD;
124typedef int NEWTEXTMETRIC;
125typedef int PACL;
126typedef int PRINTDLG;
127typedef int PSECURITY_DESCRIPTOR;
128typedef int PSID;
129typedef int SECURITY_INFORMATION;
130typedef int SHORT;
131typedef int SMALL_RECT;
132typedef int TEXTMETRIC;
133typedef int UINT;
134typedef int WCHAR;
135typedef int WORD;
136typedef int WPARAM;
137typedef void VOID;
138#endif
139
140/* Record all output and all keyboard & mouse input */
141/* #define MCH_WRITE_DUMP */
142
143#ifdef MCH_WRITE_DUMP
144FILE* fdDump = NULL;
145#endif
146
147#ifdef WIN3264
148extern DWORD g_PlatformId;
149#endif
150
151#ifndef FEAT_GUI_MSWIN
152extern char g_szOrigTitle[];
153#endif
154
155#ifdef FEAT_GUI
156extern HWND s_hwnd;
157#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static HWND s_hwnd = 0; /* console window handle, set by GetConsoleHwnd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
160
161extern int WSInitialized;
162
163/* Don't generate prototypes here, because some systems do have these
164 * functions. */
165#if defined(__GNUC__) && !defined(PROTO)
166# ifndef __MINGW32__
167int _stricoll(char *a, char *b)
168{
169 // the ANSI-ish correct way is to use strxfrm():
170 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
171 strxfrm(a_buff, a, 512);
172 strxfrm(b_buff, b, 512);
173 return strcoll(a_buff, b_buff);
174}
175
176char * _fullpath(char *buf, char *fname, int len)
177{
178 LPTSTR toss;
179
180 return (char *)GetFullPathName(fname, len, buf, &toss);
181}
182# endif
183
184int _chdrive(int drive)
185{
186 char temp [3] = "-:";
187 temp[0] = drive + 'A' - 1;
188 return !SetCurrentDirectory(temp);
189}
190#else
191# ifdef __BORLANDC__
192/* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll:
193 * but it does in BC 5.02! */
194# if __BORLANDC__ < 0x502
195int _stricoll(char *a, char *b)
196{
197# if 1
198 // this is fast but not correct:
199 return stricmp(a, b);
200# else
201 // the ANSI-ish correct way is to use strxfrm():
202 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
203 strxfrm(a_buff, a, 512);
204 strxfrm(b_buff, b, 512);
205 return strcoll(a_buff, b_buff);
206# endif
207}
208# endif
209# endif
210#endif
211
212
213#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
214/*
215 * GUI version of mch_exit().
216 * Shut down and exit with status `r'
217 * Careful: mch_exit() may be called before mch_init()!
218 */
219 void
220mch_exit(int r)
221{
222 display_errors();
223
224 ml_close_all(TRUE); /* remove all memfiles */
225
226# ifdef FEAT_OLE
227 UninitOLE();
228# endif
229# ifdef FEAT_NETBEANS_INTG
230 if (WSInitialized)
231 {
232 WSInitialized = FALSE;
233 WSACleanup();
234 }
235# endif
236#ifdef DYNAMIC_GETTEXT
237 dyn_libintl_end();
238#endif
239
240 if (gui.in_use)
241 gui_exit(r);
242 exit(r);
243}
244
245#endif /* FEAT_GUI_MSWIN */
246
247
248/*
249 * Init the tables for toupper() and tolower().
250 */
251 void
252mch_early_init(void)
253{
254 int i;
255
256#ifdef WIN3264
257 PlatformId();
258#endif
259
260 /* Init the tables for toupper() and tolower() */
261 for (i = 0; i < 256; ++i)
262 toupper_tab[i] = tolower_tab[i] = i;
263#ifdef WIN3264
264 CharUpperBuff(toupper_tab, 256);
265 CharLowerBuff(tolower_tab, 256);
266#else
267 AnsiUpperBuff(toupper_tab, 256);
268 AnsiLowerBuff(tolower_tab, 256);
269#endif
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000270
271#if defined(FEAT_MBYTE) && !defined(FEAT_GUI)
272 (void)get_cmd_argsW(NULL);
273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274}
275
276
277/*
278 * Return TRUE if the input comes from a terminal, FALSE otherwise.
279 */
280 int
281mch_input_isatty()
282{
283#ifdef FEAT_GUI_MSWIN
284 return OK; /* GUI always has a tty */
285#else
286 if (isatty(read_cmd_fd))
287 return TRUE;
288 return FALSE;
289#endif
290}
291
292#ifdef FEAT_TITLE
293/*
294 * mch_settitle(): set titlebar of our window
295 */
296 void
297mch_settitle(
298 char_u *title,
299 char_u *icon)
300{
301# ifdef FEAT_GUI_MSWIN
302 gui_mch_settitle(title, icon);
303# else
304 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000305 {
306# ifdef FEAT_MBYTE
307 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
308 {
309 /* Convert the title from 'encoding' to the active codepage. */
310 WCHAR *wp = enc_to_ucs2(title, NULL);
311 int n;
312
313 if (wp != NULL)
314 {
315 n = SetConsoleTitleW(wp);
316 vim_free(wp);
317 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
318 return;
319 }
320 }
321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 SetConsoleTitle(title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324# endif
325}
326
327
328/*
329 * Restore the window/icon title.
330 * which is one of:
331 * 1: Just restore title
332 * 2: Just restore icon (which we don't have)
333 * 3: Restore title and icon (which we don't have)
334 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000335/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 void
337mch_restore_title(
338 int which)
339{
340#ifndef FEAT_GUI_MSWIN
341 mch_settitle((which & 1) ? g_szOrigTitle : NULL, NULL);
342#endif
343}
344
345
346/*
347 * Return TRUE if we can restore the title (we can)
348 */
349 int
350mch_can_restore_title()
351{
352 return TRUE;
353}
354
355
356/*
357 * Return TRUE if we can restore the icon title (we can't)
358 */
359 int
360mch_can_restore_icon()
361{
362 return FALSE;
363}
364#endif /* FEAT_TITLE */
365
366
367/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000368 * Get absolute file name into buffer "buf" of length "len" bytes,
369 * turning all '/'s into '\\'s and getting the correct case of each component
370 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 * When 'shellslash' set do it the other way around.
372 * Return OK or FAIL.
373 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000374/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 int
376mch_FullName(
377 char_u *fname,
378 char_u *buf,
379 int len,
380 int force)
381{
382 int nResult = FAIL;
383
384#ifdef __BORLANDC__
385 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
386 nResult = mch_dirname(buf, len);
387 else
388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000390#ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000391 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
392# ifdef __BORLANDC__
393 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
394 && g_PlatformId == VER_PLATFORM_WIN32_NT
395# endif
396 )
397 {
398 WCHAR *wname;
399 WCHAR wbuf[MAX_PATH];
400 char_u *cname = NULL;
401
402 /* Use the wide function:
403 * - convert the fname from 'encoding' to UCS2.
404 * - invoke _wfullpath()
405 * - convert the result from UCS2 to 'encoding'.
406 */
407 wname = enc_to_ucs2(fname, NULL);
408 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH - 1) != NULL)
409 {
410 cname = ucs2_to_enc((short_u *)wbuf, NULL);
411 if (cname != NULL)
412 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000413 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000414 nResult = OK;
415 }
416 }
417 vim_free(wname);
418 vim_free(cname);
419 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000420 if (nResult == FAIL) /* fall back to non-wide function */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000422 {
423 if (_fullpath(buf, fname, len - 1) == NULL)
424 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000425 /* failed, use relative path name */
426 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000427 }
428 else
429 nResult = OK;
430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
433#ifdef USE_FNAME_CASE
434 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000435#else
436 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#endif
438
439 return nResult;
440}
441
442
443/*
444 * Return TRUE if "fname" does not depend on the current directory.
445 */
446 int
447mch_isFullName(char_u *fname)
448{
449 char szName[_MAX_PATH + 1];
450
451 /* A name like "d:/foo" and "//server/share" is absolute */
452 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
453 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
454 return TRUE;
455
456 /* A name that can't be made absolute probably isn't absolute. */
457 if (mch_FullName(fname, szName, _MAX_PATH, FALSE) == FAIL)
458 return FALSE;
459
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000460 return pathcmp(fname, szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461}
462
463/*
464 * Replace all slashes by backslashes.
465 * This used to be the other way around, but MS-DOS sometimes has problems
466 * with slashes (e.g. in a command name). We can't have mixed slashes and
467 * backslashes, because comparing file names will not work correctly. The
468 * commands that use a file name should try to avoid the need to type a
469 * backslash twice.
470 * When 'shellslash' set do it the other way around.
471 */
472 void
473slash_adjust(p)
474 char_u *p;
475{
476 if (p != NULL)
477 while (*p)
478 {
479 if (*p == psepcN)
480 *p = psepc;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000481 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 }
483}
484
485
486/*
487 * stat() can't handle a trailing '/' or '\', remove it first.
488 */
489 int
490vim_stat(const char *name, struct stat *stp)
491{
492 char buf[_MAX_PATH + 1];
493 char *p;
494
Bram Moolenaar84110ac2005-07-20 21:56:21 +0000495 vim_strncpy((char_u *)buf, (char_u *)name, _MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 p = buf + strlen(buf);
497 if (p > buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000498 mb_ptr_back(buf, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
500 *p = NUL;
501#ifdef FEAT_MBYTE
502 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
503# ifdef __BORLANDC__
504 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
505 && g_PlatformId == VER_PLATFORM_WIN32_NT
506# endif
507 )
508 {
509 WCHAR *wp = enc_to_ucs2(buf, NULL);
510 int n;
511
512 if (wp != NULL)
513 {
514 n = _wstat(wp, (struct _stat *)stp);
515 vim_free(wp);
516 if (n >= 0)
517 return n;
518 /* Retry with non-wide function (for Windows 98). Can't use
519 * GetLastError() here and it's unclear what errno gets set to if
520 * the _wstat() fails for missing wide functions. */
521 }
522 }
523#endif
524 return stat(buf, stp);
525}
526
527#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000528/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 void
530mch_settmode(int tmode)
531{
532 /* nothing to do */
533}
534
535 int
536mch_get_shellsize(void)
537{
538 /* never used */
539 return OK;
540}
541
542 void
543mch_set_shellsize(void)
544{
545 /* never used */
546}
547
548/*
549 * Rows and/or Columns has changed.
550 */
551 void
552mch_new_shellsize(void)
553{
554 /* never used */
555}
556
557#endif
558
559/*
560 * We have no job control, so fake it by starting a new shell.
561 */
562 void
563mch_suspend()
564{
565 suspend_shell();
566}
567
568#if defined(USE_MCH_ERRMSG) || defined(PROTO)
569
570#ifdef display_errors
571# undef display_errors
572#endif
573
574/*
575 * Display the saved error message(s).
576 */
577 void
578display_errors()
579{
580 char *p;
581
582 if (error_ga.ga_data != NULL)
583 {
584 /* avoid putting up a message box with blanks only */
585 for (p = (char *)error_ga.ga_data; *p; ++p)
586 if (!isspace(*p))
587 {
Bram Moolenaar8089cae2005-02-05 21:35:05 +0000588#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 /* Truncate a very long message, it will go off-screen. */
590 if (STRLEN(p) > 2000)
591 {
592 char_u *s = p + 2000 - 14;
593
594#ifdef FEAT_MBYTE
595 if (has_mbyte)
596 s -= (*mb_head_off)(p, s);
597#endif
598 STRCPY(s, _("...(truncated)"));
599 }
Bram Moolenaar8089cae2005-02-05 21:35:05 +0000600#endif
Bram Moolenaar748bf032005-02-02 23:04:36 +0000601
602 (void)gui_mch_dialog(VIM_ERROR, (char_u *)_("Error"),
603 p, (char_u *)_("&Ok"), 1, NULL);
604#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605#ifdef WIN3264
606 MessageBox(NULL, p, "Vim", MB_TASKMODAL|MB_SETFOREGROUND);
607#else
608 MessageBox(NULL, p, "Vim", MB_TASKMODAL);
609#endif
Bram Moolenaar748bf032005-02-02 23:04:36 +0000610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 break;
612 }
613 ga_clear(&error_ga);
614 }
615}
616#endif
617
618
619/*
620 * Return TRUE if "p" contain a wildcard that can be expanded by
621 * dos_expandpath().
622 */
623 int
624mch_has_exp_wildcard(char_u *p)
625{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000626 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 {
628 if (vim_strchr((char_u *)"?*[", *p) != NULL
629 || (*p == '~' && p[1] != NUL))
630 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 }
632 return FALSE;
633}
634
635/*
636 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
637 * shortened file name).
638 */
639 int
640mch_has_wildcard(char_u *p)
641{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000642 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {
644 if (vim_strchr((char_u *)
645# ifdef VIM_BACKTICK
646 "?*$[`"
647# else
648 "?*$["
649# endif
650 , *p) != NULL
651 || (*p == '~' && p[1] != NUL))
652 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654 return FALSE;
655}
656
657
658/*
659 * The normal _chdir() does not change the default drive. This one does.
660 * Returning 0 implies success; -1 implies failure.
661 */
662 int
663mch_chdir(char *path)
664{
665 if (path[0] == NUL) /* just checking... */
666 return -1;
667
668 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
669 {
670 /* If we can change to the drive, skip that part of the path. If we
671 * can't then the current directory may be invalid, try using chdir()
672 * with the whole path. */
673 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
674 path += 2;
675 }
676
677 if (*path == NUL) /* drive name only */
678 return 0;
679
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000680#ifdef FEAT_MBYTE
681 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
682 {
683 WCHAR *p = enc_to_ucs2(path, NULL);
684 int n;
685
686 if (p != NULL)
687 {
688 n = _wchdir(p);
689 vim_free(p);
690 if (n == 0)
691 return 0;
692 /* Retry with non-wide function (for Windows 98). */
693 }
694 }
695#endif
696
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 return chdir(path); /* let the normal chdir() do the rest */
698}
699
700
701/*
702 * Switching off termcap mode is only allowed when Columns is 80, otherwise a
703 * crash may result. It's always allowed on NT or when running the GUI.
704 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000705/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 int
707can_end_termcap_mode(
708 int give_msg)
709{
710#ifdef FEAT_GUI_MSWIN
711 return TRUE; /* GUI starts a new console anyway */
712#else
713 if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80)
714 return TRUE;
715 if (give_msg)
716 msg(_("'columns' is not 80, cannot execute external commands"));
717 return FALSE;
718#endif
719}
720
721#ifdef FEAT_GUI_MSWIN
722/*
723 * return non-zero if a character is available
724 */
725 int
726mch_char_avail()
727{
728 /* never used */
729 return TRUE;
730}
731#endif
732
733
734/*
735 * set screen mode, always fails.
736 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000737/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 int
739mch_screenmode(
740 char_u *arg)
741{
742 EMSG(_(e_screenmode));
743 return FAIL;
744}
745
746
747#if defined(FEAT_LIBCALL) || defined(PROTO)
748/*
749 * Call a DLL routine which takes either a string or int param
750 * and returns an allocated string.
751 * Return OK if it worked, FAIL if not.
752 */
753# ifdef WIN3264
754typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
755typedef LPTSTR (*MYINTPROCSTR)(int);
756typedef int (*MYSTRPROCINT)(LPTSTR);
757typedef int (*MYINTPROCINT)(int);
758# else
759typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
760typedef LPSTR (*MYINTPROCSTR)(int);
761typedef int (*MYSTRPROCINT)(LPSTR);
762typedef int (*MYINTPROCINT)(int);
763# endif
764
765# ifndef WIN16
766/*
767 * Check if a pointer points to a valid NUL terminated string.
768 * Return the length of the string, including terminating NUL.
769 * Returns 0 for an invalid pointer, 1 for an empty string.
770 */
771 static size_t
772check_str_len(char_u *str)
773{
774 SYSTEM_INFO si;
775 MEMORY_BASIC_INFORMATION mbi;
776 size_t length = 0;
777 size_t i;
778 const char *p;
779
780 /* get page size */
781 GetSystemInfo(&si);
782
783 /* get memory information */
784 if (VirtualQuery(str, &mbi, sizeof(mbi)))
785 {
786 /* pre cast these (typing savers) */
787 DWORD dwStr = (DWORD)str;
788 DWORD dwBaseAddress = (DWORD)mbi.BaseAddress;
789
790 /* get start address of page that str is on */
791 DWORD strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
792
793 /* get length from str to end of page */
794 DWORD pageLength = si.dwPageSize - (dwStr - strPage);
795
796 for (p = str; !IsBadReadPtr(p, pageLength);
797 p += pageLength, pageLength = si.dwPageSize)
798 for (i = 0; i < pageLength; ++i, ++length)
799 if (p[i] == NUL)
800 return length + 1;
801 }
802
803 return 0;
804}
805# endif
806
807 int
808mch_libcall(
809 char_u *libname,
810 char_u *funcname,
811 char_u *argstring, /* NULL when using a argint */
812 int argint,
813 char_u **string_result,/* NULL when using number_result */
814 int *number_result)
815{
816 HINSTANCE hinstLib;
817 MYSTRPROCSTR ProcAdd;
818 MYINTPROCSTR ProcAddI;
819 char_u *retval_str = NULL;
820 int retval_int = 0;
821 size_t len;
822
823 BOOL fRunTimeLinkSuccess = FALSE;
824
825 // Get a handle to the DLL module.
826 hinstLib = LoadLibrary(libname);
827
828 // If the handle is valid, try to get the function address.
829 if (hinstLib != NULL)
830 {
831#ifdef HAVE_TRY_EXCEPT
832 __try
833 {
834#endif
835 if (argstring != NULL)
836 {
837 /* Call with string argument */
838 ProcAdd = (MYSTRPROCSTR) GetProcAddress(hinstLib, funcname);
839 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
840 {
841 if (string_result == NULL)
842 retval_int = ((MYSTRPROCINT)ProcAdd)(argstring);
843 else
844 retval_str = (ProcAdd)(argstring);
845 }
846 }
847 else
848 {
849 /* Call with number argument */
850 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, funcname);
851 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
852 {
853 if (string_result == NULL)
854 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
855 else
856 retval_str = (ProcAddI)(argint);
857 }
858 }
859
860 // Save the string before we free the library.
861 // Assume that a "1" result is an illegal pointer.
862 if (string_result == NULL)
863 *number_result = retval_int;
864 else if (retval_str != NULL
865# ifdef WIN16
866 && retval_str != (char_u *)1
867 && retval_str != (char_u *)-1
868 && !IsBadStringPtr(retval_str, INT_MAX)
869 && (len = strlen(retval_str) + 1) > 0
870# else
871 && (len = check_str_len(retval_str)) > 0
872# endif
873 )
874 {
875 *string_result = lalloc((long_u)len, TRUE);
876 if (*string_result != NULL)
877 mch_memmove(*string_result, retval_str, len);
878 }
879
880#ifdef HAVE_TRY_EXCEPT
881 }
882 __except(EXCEPTION_EXECUTE_HANDLER)
883 {
884 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
885 RESETSTKOFLW();
886 fRunTimeLinkSuccess = 0;
887 }
888#endif
889
890 // Free the DLL module.
891 (void)FreeLibrary(hinstLib);
892 }
893
894 if (!fRunTimeLinkSuccess)
895 {
896 EMSG2(_(e_libcall), funcname);
897 return FAIL;
898 }
899
900 return OK;
901}
902#endif
903
904#if defined(FEAT_MBYTE) || defined(PROTO)
905/*
906 * Convert an UTF-8 string to UCS-2.
907 * "instr[inlen]" is the input. "inlen" is in bytes.
908 * When "outstr" is NULL only return the number of UCS-2 words produced.
909 * Otherwise "outstr" must be a buffer of sufficient size.
910 * Returns the number of UCS-2 words produced.
911 */
912 int
913utf8_to_ucs2(char_u *instr, int inlen, short_u *outstr, int *unconvlenp)
914{
915 int outlen = 0;
916 char_u *p = instr;
917 int todo = inlen;
918 int l;
919
920 while (todo > 0)
921 {
922 /* Only convert if we have a complete sequence. */
923 l = utf_ptr2len_check_len(p, todo);
924 if (l > todo)
925 {
926 /* Return length of incomplete sequence. */
927 if (unconvlenp != NULL)
928 *unconvlenp = todo;
929 break;
930 }
931
932 if (outstr != NULL)
933 *outstr++ = utf_ptr2char(p);
934 ++outlen;
935 p += l;
936 todo -= l;
937 }
938
939 return outlen;
940}
941
942/*
943 * Convert an UCS-2 string to UTF-8.
944 * The input is "instr[inlen]" with "inlen" in number of ucs-2 words.
945 * When "outstr" is NULL only return the required number of bytes.
946 * Otherwise "outstr" must be a buffer of sufficient size.
947 * Return the number of bytes produced.
948 */
949 int
950ucs2_to_utf8(short_u *instr, int inlen, char_u *outstr)
951{
952 int outlen = 0;
953 int todo = inlen;
954 short_u *p = instr;
955 int l;
956
957 while (todo > 0)
958 {
959 if (outstr != NULL)
960 {
961 l = utf_char2bytes(*p, outstr);
962 outstr += l;
963 }
964 else
965 l = utf_char2len(*p);
966 ++p;
967 outlen += l;
968 --todo;
969 }
970
971 return outlen;
972}
973
974/*
975 * Call MultiByteToWideChar() and allocate memory for the result.
976 * Returns the result in "*out[*outlen]" with an extra zero appended.
977 * "outlen" is in words.
978 */
979 void
980MultiByteToWideChar_alloc(UINT cp, DWORD flags,
981 LPCSTR in, int inlen,
982 LPWSTR *out, int *outlen)
983{
984 *outlen = MultiByteToWideChar(cp, flags, in, inlen, 0, 0);
985 /* Add one one word to avoid a zero-length alloc(). */
986 *out = (LPWSTR)alloc(sizeof(WCHAR) * (*outlen + 1));
987 if (*out != NULL)
988 {
989 MultiByteToWideChar(cp, flags, in, inlen, *out, *outlen);
990 (*out)[*outlen] = 0;
991 }
992}
993
994/*
995 * Call WideCharToMultiByte() and allocate memory for the result.
996 * Returns the result in "*out[*outlen]" with an extra NUL appended.
997 */
998 void
999WideCharToMultiByte_alloc(UINT cp, DWORD flags,
1000 LPCWSTR in, int inlen,
1001 LPSTR *out, int *outlen,
1002 LPCSTR def, LPBOOL useddef)
1003{
1004 *outlen = WideCharToMultiByte(cp, flags, in, inlen, NULL, 0, def, useddef);
1005 /* Add one one byte to avoid a zero-length alloc(). */
1006 *out = alloc((unsigned)*outlen + 1);
1007 if (*out != NULL)
1008 {
1009 WideCharToMultiByte(cp, flags, in, inlen, *out, *outlen, def, useddef);
1010 (*out)[*outlen] = 0;
1011 }
1012}
1013
1014#endif /* FEAT_MBYTE */
1015
1016#ifdef FEAT_CLIPBOARD
1017/*
1018 * Clipboard stuff, for cutting and pasting text to other windows.
1019 */
1020
1021/* Type used for the clipboard type of Vim's data. */
1022typedef struct
1023{
1024 int type; /* MCHAR, MBLOCK or MLINE */
1025 int txtlen; /* length of CF_TEXT in bytes */
1026 int ucslen; /* length of CF_UNICODETEXT in words */
1027 int rawlen; /* length of clip_star.format_raw, including encoding,
1028 excluding terminating NUL */
1029} VimClipType_t;
1030
1031/*
1032 * Make vim the owner of the current selection. Return OK upon success.
1033 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001034/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 int
1036clip_mch_own_selection(VimClipboard *cbd)
1037{
1038 /*
1039 * Never actually own the clipboard. If another application sets the
1040 * clipboard, we don't want to think that we still own it.
1041 */
1042 return FAIL;
1043}
1044
1045/*
1046 * Make vim NOT the owner of the current selection.
1047 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001048/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 void
1050clip_mch_lose_selection(VimClipboard *cbd)
1051{
1052 /* Nothing needs to be done here */
1053}
1054
1055/*
1056 * Copy "str[*size]" into allocated memory, changing CR-NL to NL.
1057 * Return the allocated result and the size in "*size".
1058 * Returns NULL when out of memory.
1059 */
1060 static char_u *
1061crnl_to_nl(const char_u *str, int *size)
1062{
1063 int pos = 0;
1064 int str_len = *size;
1065 char_u *ret;
1066 char_u *retp;
1067
1068 /* Avoid allocating zero bytes, it generates an error message. */
1069 ret = lalloc((long_u)(str_len == 0 ? 1 : str_len), TRUE);
1070 if (ret != NULL)
1071 {
1072 retp = ret;
1073 for (pos = 0; pos < str_len; ++pos)
1074 {
1075 if (str[pos] == '\r' && str[pos + 1] == '\n')
1076 {
1077 ++pos;
1078 --(*size);
1079 }
1080 *retp++ = str[pos];
1081 }
1082 }
1083
1084 return ret;
1085}
1086
1087#if defined(FEAT_MBYTE) || defined(PROTO)
1088/*
1089 * Note: the following two functions are only guaranteed to work when using
1090 * valid MS-Windows codepages or when iconv() is available.
1091 */
1092
1093/*
1094 * Convert "str" from 'encoding' to UCS-2.
1095 * Input in "str" with length "*lenp". When "lenp" is NULL, use strlen().
1096 * Output is returned as an allocated string. "*lenp" is set to the length of
1097 * the result. A trailing NUL is always added.
1098 * Returns NULL when out of memory.
1099 */
1100 short_u *
1101enc_to_ucs2(char_u *str, int *lenp)
1102{
1103 vimconv_T conv;
1104 WCHAR *ret;
1105 char_u *allocbuf = NULL;
1106 int len_loc;
1107 int length;
1108
1109 if (lenp == NULL)
1110 {
1111 len_loc = STRLEN(str) + 1;
1112 lenp = &len_loc;
1113 }
1114
1115 if (enc_codepage > 0)
1116 {
1117 /* We can do any CP### -> UCS-2 in one pass, and we can do it
1118 * without iconv() (convert_* may need iconv). */
1119 MultiByteToWideChar_alloc(enc_codepage, 0, str, *lenp, &ret, &length);
1120 }
1121 else
1122 {
1123 /* Use "latin1" by default, we might be called before we have p_enc
1124 * set up. Convert to utf-8 first, works better with iconv(). Does
1125 * nothing if 'encoding' is "utf-8". */
1126 conv.vc_type = CONV_NONE;
1127 if (convert_setup(&conv, p_enc ? p_enc : (char_u *)"latin1",
1128 (char_u *)"utf-8") == FAIL)
1129 return NULL;
1130 if (conv.vc_type != CONV_NONE)
1131 {
1132 str = allocbuf = string_convert(&conv, str, lenp);
1133 if (str == NULL)
1134 return NULL;
1135 }
1136 convert_setup(&conv, NULL, NULL);
1137
1138 length = utf8_to_ucs2(str, *lenp, NULL, NULL);
1139 ret = (WCHAR *)alloc((unsigned)((length + 1) * sizeof(WCHAR)));
1140 if (ret != NULL)
1141 {
1142 utf8_to_ucs2(str, *lenp, (short_u *)ret, NULL);
1143 ret[length] = 0;
1144 }
1145
1146 vim_free(allocbuf);
1147 }
1148
1149 *lenp = length;
1150 return (short_u *)ret;
1151}
1152
1153/*
1154 * Convert an UCS-2 string to 'encoding'.
1155 * Input in "str" with length (counted in wide characters) "*lenp". When
1156 * "lenp" is NULL, use wcslen().
1157 * Output is returned as an allocated string. If "*lenp" is not NULL it is
1158 * set to the length of the result.
1159 * Returns NULL when out of memory.
1160 */
1161 char_u *
1162ucs2_to_enc(short_u *str, int *lenp)
1163{
1164 vimconv_T conv;
1165 char_u *utf8_str = NULL, *enc_str = NULL;
1166 int len_loc;
1167
1168 if (lenp == NULL)
1169 {
1170 len_loc = wcslen(str) + 1;
1171 lenp = &len_loc;
1172 }
1173
1174 if (enc_codepage > 0)
1175 {
1176 /* We can do any UCS-2 -> CP### in one pass. */
1177 int length;
1178
1179 WideCharToMultiByte_alloc(enc_codepage, 0, str, *lenp,
1180 (LPSTR *)&enc_str, &length, 0, 0);
1181 *lenp = length;
1182 return enc_str;
1183 }
1184
1185 /* Avoid allocating zero bytes, it generates an error message. */
1186 utf8_str = alloc(ucs2_to_utf8(str, *lenp == 0 ? 1 : *lenp, NULL));
1187 if (utf8_str != NULL)
1188 {
1189 *lenp = ucs2_to_utf8(str, *lenp, utf8_str);
1190
1191 /* We might be called before we have p_enc set up. */
1192 conv.vc_type = CONV_NONE;
1193 convert_setup(&conv, (char_u *)"utf-8",
1194 p_enc? p_enc: (char_u *)"latin1");
1195 if (conv.vc_type == CONV_NONE)
1196 {
1197 /* p_enc is utf-8, so we're done. */
1198 enc_str = utf8_str;
1199 }
1200 else
1201 {
1202 enc_str = string_convert(&conv, utf8_str, lenp);
1203 vim_free(utf8_str);
1204 }
1205
1206 convert_setup(&conv, NULL, NULL);
1207 }
1208
1209 return enc_str;
1210}
1211#endif /* FEAT_MBYTE */
1212
1213/*
1214 * Get the current selection and put it in the clipboard register.
1215 *
1216 * NOTE: Must use GlobalLock/Unlock here to ensure Win32s compatibility.
1217 * On NT/W95 the clipboard data is a fixed global memory object and
1218 * so its handle = its pointer.
1219 * On Win32s, however, co-operation with the Win16 system means that
1220 * the clipboard data is moveable and its handle is not a pointer at all,
1221 * so we can't just cast the return value of GetClipboardData to (char_u*).
1222 * <VN>
1223 */
1224 void
1225clip_mch_request_selection(VimClipboard *cbd)
1226{
1227 VimClipType_t metadata = { -1, -1, -1, -1 };
1228 HGLOBAL hMem = NULL;
1229 char_u *str = NULL;
1230#if defined(FEAT_MBYTE) && defined(WIN3264)
1231 char_u *to_free = NULL;
1232#endif
1233#ifdef FEAT_MBYTE
1234 HGLOBAL rawh = NULL;
1235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 int str_size = 0;
1237 int maxlen;
1238 size_t n;
1239
1240 /*
1241 * Don't pass GetActiveWindow() as an argument to OpenClipboard() because
1242 * then we can't paste back into the same window for some reason - webb.
1243 */
1244 if (!OpenClipboard(NULL))
1245 return;
1246
1247 /* Check for vim's own clipboard format first. This only gets the type of
1248 * the data, still need to use CF_UNICODETEXT or CF_TEXT for the text. */
1249 if (IsClipboardFormatAvailable(cbd->format))
1250 {
1251 VimClipType_t *meta_p;
1252 HGLOBAL meta_h;
1253
1254 /* We have metadata on the clipboard; try to get it. */
1255 if ((meta_h = GetClipboardData(cbd->format)) != NULL
1256 && (meta_p = (VimClipType_t *)GlobalLock(meta_h)) != NULL)
1257 {
1258 /* The size of "VimClipType_t" changed, "rawlen" was added later.
1259 * Only copy what is available for backwards compatibility. */
1260 n = sizeof(VimClipType_t);
1261 if (GlobalSize(meta_h) < n)
1262 n = GlobalSize(meta_h);
1263 memcpy(&metadata, meta_p, n);
1264 GlobalUnlock(meta_h);
1265 }
1266 }
1267
1268#ifdef FEAT_MBYTE
1269 /* Check for Vim's raw clipboard format first. This is used without
1270 * conversion, but only if 'encoding' matches. */
1271 if (IsClipboardFormatAvailable(cbd->format_raw)
1272 && metadata.rawlen > (int)STRLEN(p_enc))
1273 {
1274 /* We have raw data on the clipboard; try to get it. */
1275 if ((rawh = GetClipboardData(cbd->format_raw)) != NULL)
1276 {
1277 char_u *rawp;
1278
1279 rawp = (char_u *)GlobalLock(rawh);
1280 if (rawp != NULL && STRCMP(p_enc, rawp) == 0)
1281 {
1282 n = STRLEN(p_enc) + 1;
1283 str = rawp + n;
1284 str_size = metadata.rawlen - n;
1285 }
1286 else
1287 {
1288 GlobalUnlock(rawh);
1289 rawh = NULL;
1290 }
1291 }
1292 }
1293 if (str == NULL)
1294 {
1295#endif
1296
1297#if defined(FEAT_MBYTE) && defined(WIN3264)
1298 /* Try to get the clipboard in Unicode if it's not an empty string. */
1299 if (IsClipboardFormatAvailable(CF_UNICODETEXT) && metadata.ucslen != 0)
1300 {
1301 HGLOBAL hMemW;
1302
1303 if ((hMemW = GetClipboardData(CF_UNICODETEXT)) != NULL)
1304 {
1305 WCHAR *hMemWstr = (WCHAR *)GlobalLock(hMemW);
1306
1307 /* Use the length of our metadata if possible, but limit it to the
1308 * GlobalSize() for safety. */
1309 maxlen = GlobalSize(hMemW) / sizeof(WCHAR);
1310 if (metadata.ucslen >= 0)
1311 {
1312 if (metadata.ucslen > maxlen)
1313 str_size = maxlen;
1314 else
1315 str_size = metadata.ucslen;
1316 }
1317 else
1318 {
1319 for (str_size = 0; str_size < maxlen; ++str_size)
1320 if (hMemWstr[str_size] == NUL)
1321 break;
1322 }
1323 to_free = str = ucs2_to_enc((short_u *)hMemWstr, &str_size);
1324 GlobalUnlock(hMemW);
1325 }
1326 }
1327 else
1328#endif
1329 /* Get the clipboard in the Active codepage. */
1330 if (IsClipboardFormatAvailable(CF_TEXT))
1331 {
1332 if ((hMem = GetClipboardData(CF_TEXT)) != NULL)
1333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001334 str = (char_u *)GlobalLock(hMem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336 /* The length is either what our metadata says or the strlen().
1337 * But limit it to the GlobalSize() for safety. */
1338 maxlen = GlobalSize(hMem);
1339 if (metadata.txtlen >= 0)
1340 {
1341 if (metadata.txtlen > maxlen)
1342 str_size = maxlen;
1343 else
1344 str_size = metadata.txtlen;
1345 }
1346 else
1347 {
1348 for (str_size = 0; str_size < maxlen; ++str_size)
1349 if (str[str_size] == NUL)
1350 break;
1351 }
1352
Bram Moolenaar05159a02005-02-26 23:04:13 +00001353# if defined(FEAT_MBYTE) && defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 /* The text is in the active codepage. Convert to 'encoding',
1355 * going through UCS-2. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001356 acp_to_enc(str, str_size, &to_free, &maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 if (to_free != NULL)
1358 {
1359 str_size = maxlen;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001360 str = to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00001362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 }
1365#ifdef FEAT_MBYTE
1366 }
1367#endif
1368
1369 if (str != NULL && *str != NUL)
1370 {
1371 char_u *temp_clipboard;
1372
1373 /* If the type is not known guess it. */
1374 if (metadata.type == -1)
1375 metadata.type = (vim_strchr(str, '\n') == NULL) ? MCHAR : MLINE;
1376
1377 /* Translate <CR><NL> into <NL>. */
1378 temp_clipboard = crnl_to_nl(str, &str_size);
1379 if (temp_clipboard != NULL)
1380 {
1381 clip_yank_selection(metadata.type, temp_clipboard, str_size, cbd);
1382 vim_free(temp_clipboard);
1383 }
1384 }
1385
1386 /* unlock the global object */
1387 if (hMem != NULL)
1388 GlobalUnlock(hMem);
1389#ifdef FEAT_MBYTE
1390 if (rawh != NULL)
1391 GlobalUnlock(rawh);
1392#endif
1393 CloseClipboard();
1394#if defined(FEAT_MBYTE) && defined(WIN3264)
1395 vim_free(to_free);
1396#endif
1397}
1398
Bram Moolenaar05159a02005-02-26 23:04:13 +00001399#if (defined(FEAT_MBYTE) && defined(WIN3264)) || defined(PROTO)
1400/*
1401 * Convert from the active codepage to 'encoding'.
1402 * Input is "str[str_size]".
1403 * The result is in allocated memory: "out[outlen]". With terminating NUL.
1404 */
1405 void
1406acp_to_enc(str, str_size, out, outlen)
1407 char_u *str;
1408 int str_size;
1409 char_u **out;
1410 int *outlen;
1411
1412{
1413 LPWSTR widestr;
1414
1415 MultiByteToWideChar_alloc(GetACP(), 0, str, str_size, &widestr, outlen);
1416 if (widestr != NULL)
1417 {
Bram Moolenaar44ecf652005-03-07 23:09:59 +00001418 ++*outlen; /* Include the 0 after the string */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001419 *out = ucs2_to_enc((short_u *)widestr, outlen);
1420 vim_free(widestr);
1421 }
1422}
1423#endif
1424
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425/*
1426 * Send the current selection to the clipboard.
1427 */
1428 void
1429clip_mch_set_selection(VimClipboard *cbd)
1430{
1431 char_u *str = NULL;
1432 VimClipType_t metadata;
1433 long_u txtlen;
1434 HGLOBAL hMemRaw = NULL;
1435 HGLOBAL hMem = NULL;
1436 HGLOBAL hMemVim = NULL;
1437# if defined(FEAT_MBYTE) && defined(WIN3264)
1438 HGLOBAL hMemW = NULL;
1439# endif
1440
1441 /* If the '*' register isn't already filled in, fill it in now */
1442 cbd->owned = TRUE;
1443 clip_get_selection(cbd);
1444 cbd->owned = FALSE;
1445
1446 /* Get the text to be put on the clipboard, with CR-LF. */
1447 metadata.type = clip_convert_selection(&str, &txtlen, cbd);
1448 if (metadata.type < 0)
1449 return;
1450 metadata.txtlen = (int)txtlen;
1451 metadata.ucslen = 0;
1452 metadata.rawlen = 0;
1453
1454#ifdef FEAT_MBYTE
1455 /* Always set the raw bytes: 'encoding', NUL and the text. This is used
1456 * when copy/paste from/to Vim with the same 'encoding', so that illegal
1457 * bytes can also be copied and no conversion is needed. */
1458 {
1459 LPSTR lpszMemRaw;
1460
1461 metadata.rawlen = txtlen + STRLEN(p_enc) + 1;
1462 hMemRaw = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1463 metadata.rawlen + 1);
1464 lpszMemRaw = (LPSTR)GlobalLock(hMemRaw);
1465 if (lpszMemRaw != NULL)
1466 {
1467 STRCPY(lpszMemRaw, p_enc);
1468 memcpy(lpszMemRaw + STRLEN(p_enc) + 1, str, txtlen + 1);
1469 GlobalUnlock(hMemRaw);
1470 }
1471 else
1472 metadata.rawlen = 0;
1473 }
1474#endif
1475
1476# if defined(FEAT_MBYTE) && defined(WIN3264)
1477 {
1478 WCHAR *out;
1479 int len = metadata.txtlen;
1480
1481 /* Convert the text to UCS-2. This is put on the clipboard as
1482 * CF_UNICODETEXT. */
1483 out = (WCHAR *)enc_to_ucs2(str, &len);
1484 if (out != NULL)
1485 {
1486 WCHAR *lpszMemW;
1487
1488 /* Convert the text for CF_TEXT to Active codepage. Otherwise it's
1489 * p_enc, which has no relation to the Active codepage. */
1490 metadata.txtlen = WideCharToMultiByte(GetACP(), 0, out, len,
1491 NULL, 0, 0, 0);
1492 vim_free(str);
1493 str = (char_u *)alloc((unsigned)(metadata.txtlen == 0 ? 1
1494 : metadata.txtlen));
1495 if (str == NULL)
1496 {
1497 vim_free(out);
1498 return; /* out of memory */
1499 }
1500 WideCharToMultiByte(GetACP(), 0, out, len,
1501 str, metadata.txtlen, 0, 0);
1502
1503 /* Allocate memory for the UCS-2 text, add one NUL word to
1504 * terminate the string. */
1505 hMemW = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1506 (len + 1) * sizeof(WCHAR));
1507 lpszMemW = (WCHAR *)GlobalLock(hMemW);
1508 if (lpszMemW != NULL)
1509 {
1510 memcpy(lpszMemW, out, len * sizeof(WCHAR));
1511 lpszMemW[len] = NUL;
1512 GlobalUnlock(hMemW);
1513 }
1514 vim_free(out);
1515 metadata.ucslen = len;
1516 }
1517 }
1518# endif
1519
1520 /* Allocate memory for the text, add one NUL byte to terminate the string.
1521 */
1522 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, metadata.txtlen + 1);
1523 {
1524 LPSTR lpszMem = (LPSTR)GlobalLock(hMem);
1525
1526 if (lpszMem)
1527 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +00001528 vim_strncpy(lpszMem, str, metadata.txtlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 GlobalUnlock(hMem);
1530 }
1531 }
1532
1533 /* Set up metadata: */
1534 {
1535 VimClipType_t *lpszMemVim = NULL;
1536
1537 hMemVim = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
1538 sizeof(VimClipType_t));
1539 lpszMemVim = (VimClipType_t *)GlobalLock(hMemVim);
1540 memcpy(lpszMemVim, &metadata, sizeof(metadata));
1541 GlobalUnlock(hMemVim);
1542 }
1543
1544 /*
1545 * Open the clipboard, clear it and put our text on it.
1546 * Always set our Vim format. Put Unicode and plain text on it.
1547 *
1548 * Don't pass GetActiveWindow() as an argument to OpenClipboard()
1549 * because then we can't paste back into the same window for some
1550 * reason - webb.
1551 */
1552 if (OpenClipboard(NULL))
1553 {
1554 if (EmptyClipboard())
1555 {
1556 SetClipboardData(cbd->format, hMemVim);
1557 hMemVim = 0;
1558# if defined(FEAT_MBYTE) && defined(WIN3264)
1559 if (hMemW != NULL)
1560 {
1561 if (SetClipboardData(CF_UNICODETEXT, hMemW) != NULL)
1562 hMemW = NULL;
1563 }
1564# endif
1565 /* Always use CF_TEXT. On Win98 Notepad won't obtain the
1566 * CF_UNICODETEXT text, only CF_TEXT. */
1567 SetClipboardData(CF_TEXT, hMem);
1568 hMem = 0;
1569 }
1570 CloseClipboard();
1571 }
1572
1573 vim_free(str);
1574 /* Free any allocations we didn't give to the clipboard: */
1575 if (hMemRaw)
1576 GlobalFree(hMemRaw);
1577 if (hMem)
1578 GlobalFree(hMem);
1579# if defined(FEAT_MBYTE) && defined(WIN3264)
1580 if (hMemW)
1581 GlobalFree(hMemW);
1582# endif
1583 if (hMemVim)
1584 GlobalFree(hMemVim);
1585}
1586
1587#endif /* FEAT_CLIPBOARD */
1588
1589
1590/*
1591 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1592 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001593/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 void
1595DumpPutS(
1596 const char *psz)
1597{
1598# ifdef MCH_WRITE_DUMP
1599 if (fdDump)
1600 {
1601 fputs(psz, fdDump);
1602 if (psz[strlen(psz) - 1] != '\n')
1603 fputc('\n', fdDump);
1604 fflush(fdDump);
1605 }
1606# endif
1607}
1608
1609#ifdef _DEBUG
1610
1611void __cdecl
1612Trace(
1613 char *pszFormat,
1614 ...)
1615{
1616 CHAR szBuff[2048];
1617 va_list args;
1618
1619 va_start(args, pszFormat);
1620 vsprintf(szBuff, pszFormat, args);
1621 va_end(args);
1622
1623 OutputDebugString(szBuff);
1624}
1625
1626#endif //_DEBUG
1627
Bram Moolenaar843ee412004-06-30 16:16:41 +00001628#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629# if defined(FEAT_TITLE) && defined(WIN3264)
1630extern HWND g_hWnd; /* This is in os_win32.c. */
1631# endif
1632
1633/*
1634 * Showing the printer dialog is tricky since we have no GUI
1635 * window to parent it. The following routines are needed to
1636 * get the window parenting and Z-order to work properly.
1637 */
1638 static void
1639GetConsoleHwnd(void)
1640{
1641# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1642
1643 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1644 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1645
1646 /* Skip if it's already set. */
1647 if (s_hwnd != 0)
1648 return;
1649
1650# if defined(FEAT_TITLE) && defined(WIN3264)
1651 /* Window handle may have been found by init code (Windows NT only) */
1652 if (g_hWnd != 0)
1653 {
1654 s_hwnd = g_hWnd;
1655 return;
1656 }
1657# endif
1658
1659 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1660
1661 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1662 pszOldWindowTitle,
1663 GetTickCount(),
1664 GetCurrentProcessId());
1665 SetConsoleTitle(pszNewWindowTitle);
1666 Sleep(40);
1667 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1668
1669 SetConsoleTitle(pszOldWindowTitle);
1670}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001671
1672/*
1673 * Console implementation of ":winpos".
1674 */
1675 int
1676mch_get_winpos(int *x, int *y)
1677{
1678 RECT rect;
1679
1680 GetConsoleHwnd();
1681 GetWindowRect(s_hwnd, &rect);
1682 *x = rect.left;
1683 *y = rect.top;
1684 return OK;
1685}
1686
1687/*
1688 * Console implementation of ":winpos x y".
1689 */
1690 void
1691mch_set_winpos(int x, int y)
1692{
1693 GetConsoleHwnd();
1694 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1695 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
1698
1699#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1700
1701# ifdef WIN16
1702# define TEXT(a) a
1703# endif
1704/*=================================================================
1705 * Win32 printer stuff
1706 */
1707
1708static HFONT prt_font_handles[2][2][2];
1709static PRINTDLG prt_dlg;
1710static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1711static TEXTMETRIC prt_tm;
1712static int prt_line_height;
1713static int prt_number_width;
1714static int prt_left_margin;
1715static int prt_right_margin;
1716static int prt_top_margin;
1717static char_u szAppName[] = TEXT("VIM");
1718static HWND hDlgPrint;
1719static int *bUserAbort = NULL;
1720static char_u *prt_name = NULL;
1721
1722/* Defines which are also in vim.rc. */
1723#define IDC_BOX1 400
1724#define IDC_PRINTTEXT1 401
1725#define IDC_PRINTTEXT2 402
1726#define IDC_PROGRESS 403
1727
1728/*
1729 * Convert BGR to RGB for Windows GDI calls
1730 */
1731 static COLORREF
1732swap_me(COLORREF colorref)
1733{
1734 int temp;
1735 char *ptr = (char *)&colorref;
1736
1737 temp = *(ptr);
1738 *(ptr ) = *(ptr + 2);
1739 *(ptr + 2) = temp;
1740 return colorref;
1741}
1742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001743/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 static BOOL CALLBACK
1745PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1746{
1747#ifdef FEAT_GETTEXT
1748 NONCLIENTMETRICS nm;
1749 static HFONT hfont;
1750#endif
1751
1752 switch (message)
1753 {
1754 case WM_INITDIALOG:
1755#ifdef FEAT_GETTEXT
1756 nm.cbSize = sizeof(NONCLIENTMETRICS);
1757 if (SystemParametersInfo(
1758 SPI_GETNONCLIENTMETRICS,
1759 sizeof(NONCLIENTMETRICS),
1760 &nm,
1761 0))
1762 {
1763 char buff[MAX_PATH];
1764 int i;
1765
1766 /* Translate the dialog texts */
1767 hfont = CreateFontIndirect(&nm.lfMessageFont);
1768 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1769 {
1770 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1771 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
1772 SetDlgItemText(hDlg,i, _(buff));
1773 }
1774 SendDlgItemMessage(hDlg, IDCANCEL,
1775 WM_SETFONT, (WPARAM)hfont, 1);
1776 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
1777 SetDlgItemText(hDlg,IDCANCEL, _(buff));
1778 }
1779#endif
1780 SetWindowText(hDlg, szAppName);
1781 if (prt_name != NULL)
1782 {
1783 SetDlgItemText(hDlg, IDC_PRINTTEXT2, (LPSTR)prt_name);
1784 vim_free(prt_name);
1785 prt_name = NULL;
1786 }
1787 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1788#ifndef FEAT_GUI
1789 BringWindowToTop(s_hwnd);
1790#endif
1791 return TRUE;
1792
1793 case WM_COMMAND:
1794 *bUserAbort = TRUE;
1795 EnableWindow(GetParent(hDlg), TRUE);
1796 DestroyWindow(hDlg);
1797 hDlgPrint = NULL;
1798#ifdef FEAT_GETTEXT
1799 DeleteObject(hfont);
1800#endif
1801 return TRUE;
1802 }
1803 return FALSE;
1804}
1805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001806/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 static BOOL CALLBACK
1808AbortProc(HDC hdcPrn, int iCode)
1809{
1810 MSG msg;
1811
1812 while (!*bUserAbort && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1813 {
1814 if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg))
1815 {
1816 TranslateMessage(&msg);
1817 DispatchMessage(&msg);
1818 }
1819 }
1820 return !*bUserAbort;
1821}
1822
1823#ifndef FEAT_GUI
1824
1825 static UINT CALLBACK
1826PrintHookProc(
1827 HWND hDlg, // handle to dialog box
1828 UINT uiMsg, // message identifier
1829 WPARAM wParam, // message parameter
1830 LPARAM lParam // message parameter
1831 )
1832{
1833 HWND hwndOwner;
1834 RECT rc, rcDlg, rcOwner;
1835 PRINTDLG *pPD;
1836
1837 if (uiMsg == WM_INITDIALOG)
1838 {
1839 // Get the owner window and dialog box rectangles.
1840 if ((hwndOwner = GetParent(hDlg)) == NULL)
1841 hwndOwner = GetDesktopWindow();
1842
1843 GetWindowRect(hwndOwner, &rcOwner);
1844 GetWindowRect(hDlg, &rcDlg);
1845 CopyRect(&rc, &rcOwner);
1846
1847 // Offset the owner and dialog box rectangles so that
1848 // right and bottom values represent the width and
1849 // height, and then offset the owner again to discard
1850 // space taken up by the dialog box.
1851
1852 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1853 OffsetRect(&rc, -rc.left, -rc.top);
1854 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1855
1856 // The new position is the sum of half the remaining
1857 // space and the owner's original position.
1858
1859 SetWindowPos(hDlg,
1860 HWND_TOP,
1861 rcOwner.left + (rc.right / 2),
1862 rcOwner.top + (rc.bottom / 2),
1863 0, 0, // ignores size arguments
1864 SWP_NOSIZE);
1865
1866 /* tackle the printdlg copiesctrl problem */
1867 pPD = (PRINTDLG *)lParam;
1868 pPD->nCopies = (WORD)pPD->lCustData;
1869 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1870 /* Bring the window to top */
1871 BringWindowToTop(GetParent(hDlg));
1872 SetForegroundWindow(hDlg);
1873 }
1874
1875 return FALSE;
1876}
1877#endif
1878
1879 void
1880mch_print_cleanup(void)
1881{
1882 int pifItalic;
1883 int pifBold;
1884 int pifUnderline;
1885
1886 for (pifBold = 0; pifBold <= 1; pifBold++)
1887 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1888 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1889 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1890
1891 if (prt_dlg.hDC != NULL)
1892 DeleteDC(prt_dlg.hDC);
1893 if (!*bUserAbort)
1894 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1895}
1896
1897 static int
1898to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1899{
1900 int ret = 0;
1901 int u;
1902 int nr;
1903
1904 u = prt_get_unit(idx);
1905 if (u == PRT_UNIT_NONE)
1906 {
1907 u = PRT_UNIT_PERC;
1908 nr = def_number;
1909 }
1910 else
1911 nr = printer_opts[idx].number;
1912
1913 switch (u)
1914 {
1915 case PRT_UNIT_PERC:
1916 ret = (physsize * nr) / 100;
1917 break;
1918 case PRT_UNIT_INCH:
1919 ret = (nr * dpi);
1920 break;
1921 case PRT_UNIT_MM:
1922 ret = (nr * 10 * dpi) / 254;
1923 break;
1924 case PRT_UNIT_POINT:
1925 ret = (nr * 10 * dpi) / 720;
1926 break;
1927 }
1928
1929 if (ret < offset)
1930 return 0;
1931 else
1932 return ret - offset;
1933}
1934
1935 static int
1936prt_get_cpl(void)
1937{
1938 int hr;
1939 int phyw;
1940 int dvoff;
1941 int rev_offset;
1942 int dpi;
1943#ifdef WIN16
1944 POINT pagesize;
1945#endif
1946
1947 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1948 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1949
1950 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
1951#ifdef WIN16
1952 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1953 phyw = pagesize.x;
1954 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1955 dvoff = pagesize.x;
1956#else
1957 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1958 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
1959#endif
1960 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1961
1962 rev_offset = phyw - (dvoff + hr);
1963
1964 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1965 if (prt_use_number())
1966 {
1967 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1968 prt_left_margin += prt_number_width;
1969 }
1970 else
1971 prt_number_width = 0;
1972
1973 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1974 rev_offset, 5);
1975
1976 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1977}
1978
1979 static int
1980prt_get_lpp(void)
1981{
1982 int vr;
1983 int phyw;
1984 int dvoff;
1985 int rev_offset;
1986 int bottom_margin;
1987 int dpi;
1988#ifdef WIN16
1989 POINT pagesize;
1990#endif
1991
1992 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
1993#ifdef WIN16
1994 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1995 phyw = pagesize.y;
1996 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1997 dvoff = pagesize.y;
1998#else
1999 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
2000 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
2001#endif
2002 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
2003
2004 rev_offset = phyw - (dvoff + vr);
2005
2006 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
2007
2008 /* adjust top margin if there is a header */
2009 prt_top_margin += prt_line_height * prt_header_height();
2010
2011 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
2012 rev_offset, 5);
2013
2014 return (bottom_margin - prt_top_margin) / prt_line_height;
2015}
2016
2017 int
2018mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
2019{
2020 static HGLOBAL stored_dm = NULL;
2021 static HGLOBAL stored_devn = NULL;
2022 static int stored_nCopies = 1;
2023 static int stored_nFlags = 0;
2024
2025 LOGFONT fLogFont;
2026 int pifItalic;
2027 int pifBold;
2028 int pifUnderline;
2029
2030 DEVMODE *mem;
2031 DEVNAMES *devname;
2032 int i;
2033
2034 bUserAbort = &(psettings->user_abort);
2035 memset(&prt_dlg, 0, sizeof(PRINTDLG));
2036 prt_dlg.lStructSize = sizeof(PRINTDLG);
2037#ifndef FEAT_GUI
2038 GetConsoleHwnd(); /* get value of s_hwnd */
2039#endif
2040 prt_dlg.hwndOwner = s_hwnd;
2041 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
2042 if (!forceit)
2043 {
2044 prt_dlg.hDevMode = stored_dm;
2045 prt_dlg.hDevNames = stored_devn;
2046 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
2047#ifndef FEAT_GUI
2048 /*
2049 * Use hook to prevent console window being sent to back
2050 */
2051 prt_dlg.lpfnPrintHook = PrintHookProc;
2052 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
2053#endif
2054 prt_dlg.Flags |= stored_nFlags;
2055 }
2056
2057 /*
2058 * If bang present, return default printer setup with no dialog
2059 * never show dialog if we are running over telnet
2060 */
2061 if (forceit
2062#ifndef FEAT_GUI
2063 || !term_console
2064#endif
2065 )
2066 {
2067 prt_dlg.Flags |= PD_RETURNDEFAULT;
2068#ifdef WIN3264
2069 /*
2070 * MSDN suggests setting the first parameter to WINSPOOL for
2071 * NT, but NULL appears to work just as well.
2072 */
2073 if (*p_pdev != NUL)
2074 prt_dlg.hDC = CreateDC(NULL, p_pdev, NULL, NULL);
2075 else
2076#endif
2077 {
2078 prt_dlg.Flags |= PD_RETURNDEFAULT;
2079 if (PrintDlg(&prt_dlg) == 0)
2080 goto init_fail_dlg;
2081 }
2082 }
2083 else if (PrintDlg(&prt_dlg) == 0)
2084 goto init_fail_dlg;
2085 else
2086 {
2087 /*
2088 * keep the previous driver context
2089 */
2090 stored_dm = prt_dlg.hDevMode;
2091 stored_devn = prt_dlg.hDevNames;
2092 stored_nFlags = prt_dlg.Flags;
2093 stored_nCopies = prt_dlg.nCopies;
2094 }
2095
2096 if (prt_dlg.hDC == NULL)
2097 {
2098 EMSG(_("E237: Printer selection failed"));
2099 mch_print_cleanup();
2100 return FALSE;
2101 }
2102
2103 /* Not all printer drivers report the support of color (or grey) in the
2104 * same way. Let's set has_color if there appears to be some way to print
2105 * more than B&W. */
2106 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
2107 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
2108 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
2109 || i > 2 || i == -1);
2110
2111 /* Ensure all font styles are baseline aligned */
2112 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
2113
2114 /*
2115 * On some windows systems the nCopies parameter is not
2116 * passed back correctly. It must be retrieved from the
2117 * hDevMode struct.
2118 */
2119 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
2120 if (mem != NULL)
2121 {
2122#ifdef WIN3264
2123 if (mem->dmCopies != 1)
2124 stored_nCopies = mem->dmCopies;
2125#endif
2126 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
2127 psettings->duplex = TRUE;
2128 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
2129 psettings->has_color = TRUE;
2130 }
2131 GlobalUnlock(prt_dlg.hDevMode);
2132
2133 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
2134 if (devname != 0)
2135 {
2136 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
2137 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
2138 char_u *text = _("to %s on %s");
2139
2140 prt_name = alloc(STRLEN(printer_name) + STRLEN(port_name)
2141 + STRLEN(text));
2142 if (prt_name != NULL)
2143 wsprintf(prt_name, text, printer_name, port_name);
2144 }
2145 GlobalUnlock(prt_dlg.hDevNames);
2146
2147 /*
2148 * Initialise the font according to 'printfont'
2149 */
2150 memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002151 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
2153 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
2154 mch_print_cleanup();
2155 return FALSE;
2156 }
2157
2158 for (pifBold = 0; pifBold <= 1; pifBold++)
2159 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
2160 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
2161 {
2162 fLogFont.lfWeight = boldface[pifBold];
2163 fLogFont.lfItalic = pifItalic;
2164 fLogFont.lfUnderline = pifUnderline;
2165 prt_font_handles[pifBold][pifItalic][pifUnderline]
2166 = CreateFontIndirect(&fLogFont);
2167 }
2168
2169 SetBkMode(prt_dlg.hDC, OPAQUE);
2170 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
2171
2172 /*
2173 * Fill in the settings struct
2174 */
2175 psettings->chars_per_line = prt_get_cpl();
2176 psettings->lines_per_page = prt_get_lpp();
2177 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
2178 ? prt_dlg.nCopies : 1;
2179 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
2180 ? 1 : prt_dlg.nCopies;
2181
2182 if (psettings->n_collated_copies == 0)
2183 psettings->n_collated_copies = 1;
2184
2185 if (psettings->n_uncollated_copies == 0)
2186 psettings->n_uncollated_copies = 1;
2187
2188 psettings->jobname = jobname;
2189
2190 return TRUE;
2191
2192init_fail_dlg:
2193 {
2194 DWORD err = CommDlgExtendedError();
2195
2196 if (err)
2197 {
2198#ifdef WIN16
2199 char buf[20];
2200
2201 sprintf(buf, "%ld", err);
2202 EMSG2(_("E238: Print error: %s"), buf);
2203#else
2204 char_u *buf;
2205
2206 /* I suspect FormatMessage() doesn't work for values returned by
2207 * CommDlgExtendedError(). What does? */
2208 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
2209 FORMAT_MESSAGE_FROM_SYSTEM |
2210 FORMAT_MESSAGE_IGNORE_INSERTS,
2211 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
2212 EMSG2(_("E238: Print error: %s"),
2213 buf == NULL ? (char_u *)_("Unknown") : buf);
2214 LocalFree((LPVOID)(buf));
2215#endif
2216 }
2217 else
2218 msg_clr_eos(); /* Maybe canceled */
2219
2220 mch_print_cleanup();
2221 return FALSE;
2222 }
2223}
2224
2225
2226 int
2227mch_print_begin(prt_settings_T *psettings)
2228{
2229 int ret;
2230 static DOCINFO di;
2231 char szBuffer[300];
2232
2233 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
2234 prt_dlg.hwndOwner, PrintDlgProc);
2235#ifdef WIN16
2236 Escape(prt_dlg.hDC, SETABORTPROC, 0, (LPSTR)AbortProc, NULL);
2237#else
2238 SetAbortProc(prt_dlg.hDC, AbortProc);
2239#endif
2240 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
2241 SetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (LPSTR)szBuffer);
2242
2243 memset(&di, 0, sizeof(DOCINFO));
2244 di.cbSize = sizeof(DOCINFO);
2245 di.lpszDocName = psettings->jobname;
2246 ret = StartDoc(prt_dlg.hDC, &di);
2247
2248#ifdef FEAT_GUI
2249 /* Give focus back to main window (when using MDI). */
2250 SetFocus(s_hwnd);
2251#endif
2252
2253 return (ret > 0);
2254}
2255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002256/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 void
2258mch_print_end(prt_settings_T *psettings)
2259{
2260 EndDoc(prt_dlg.hDC);
2261 if (!*bUserAbort)
2262 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
2263}
2264
2265 int
2266mch_print_end_page(void)
2267{
2268 return (EndPage(prt_dlg.hDC) > 0);
2269}
2270
2271 int
2272mch_print_begin_page(char_u *msg)
2273{
2274 if (msg != NULL)
2275 SetDlgItemText(hDlgPrint, IDC_PROGRESS, (LPSTR)msg);
2276 return (StartPage(prt_dlg.hDC) > 0);
2277}
2278
2279 int
2280mch_print_blank_page(void)
2281{
2282 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
2283}
2284
2285static int prt_pos_x = 0;
2286static int prt_pos_y = 0;
2287
2288 void
2289mch_print_start_line(margin, page_line)
2290 int margin;
2291 int page_line;
2292{
2293 if (margin)
2294 prt_pos_x = -prt_number_width;
2295 else
2296 prt_pos_x = 0;
2297 prt_pos_y = page_line * prt_line_height
2298 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
2299}
2300
2301 int
2302mch_print_text_out(char_u *p, int len)
2303{
2304#ifdef FEAT_PROPORTIONAL_FONTS
2305 SIZE sz;
2306#endif
2307
2308 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
2309 prt_pos_y + prt_top_margin, p, len);
2310#ifndef FEAT_PROPORTIONAL_FONTS
2311 prt_pos_x += len * prt_tm.tmAveCharWidth;
2312 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
2313 + prt_tm.tmOverhang > prt_right_margin);
2314#else
2315# ifdef WIN16
2316 GetTextExtentPoint(prt_dlg.hDC, p, len, &sz);
2317# else
2318 GetTextExtentPoint32(prt_dlg.hDC, p, len, &sz);
2319# endif
2320 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
2321 /* This is wrong when printing spaces for a TAB. */
2322 if (p[len] == NUL)
2323 return FALSE;
2324# ifdef WIN16
2325 GetTextExtentPoint(prt_dlg.hDC, p + len, 1, &sz);
2326# else
2327 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
2328# endif
2329 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
2330#endif
2331}
2332
2333 void
2334mch_print_set_font(int iBold, int iItalic, int iUnderline)
2335{
2336 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
2337}
2338
2339 void
2340mch_print_set_bg(unsigned long bgcol)
2341{
2342 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC, swap_me(bgcol)));
2343 /*
2344 * With a white background we can draw characters transparent, which is
2345 * good for italic characters that overlap to the next char cell.
2346 */
2347 if (bgcol == 0xffffffUL)
2348 SetBkMode(prt_dlg.hDC, TRANSPARENT);
2349 else
2350 SetBkMode(prt_dlg.hDC, OPAQUE);
2351}
2352
2353 void
2354mch_print_set_fg(unsigned long fgcol)
2355{
2356 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC, swap_me(fgcol)));
2357}
2358
2359#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
2360
2361#if defined(FEAT_SHORTCUT) || defined(PROTO)
2362# include <shlobj.h>
2363
2364/*
2365 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
2366 * to and return that name in allocated memory.
2367 * Otherwise NULL is returned.
2368 */
2369 char_u *
2370mch_resolve_shortcut(char_u *fname)
2371{
2372 HRESULT hr;
2373 IShellLink *psl = NULL;
2374 IPersistFile *ppf = NULL;
2375 OLECHAR wsz[MAX_PATH];
2376 WIN32_FIND_DATA ffd; // we get those free of charge
2377 TCHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
2378 char_u *rfname = NULL;
2379 int len;
2380
2381 /* Check if the file name ends in ".lnk". Avoid calling
2382 * CoCreateInstance(), it's quite slow. */
2383 if (fname == NULL)
2384 return rfname;
2385 len = STRLEN(fname);
2386 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
2387 return rfname;
2388
2389 CoInitialize(NULL);
2390
2391 // create a link manager object and request its interface
2392 hr = CoCreateInstance(
2393 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2394 &IID_IShellLink, (void**)&psl);
2395 if (hr != S_OK)
2396 goto shortcut_error;
2397
2398 // Get a pointer to the IPersistFile interface.
2399 hr = psl->lpVtbl->QueryInterface(
2400 psl, &IID_IPersistFile, (void**)&ppf);
2401 if (hr != S_OK)
2402 goto shortcut_error;
2403
2404 // full path string must be in Unicode.
2405 MultiByteToWideChar(CP_ACP, 0, fname, -1, wsz, MAX_PATH);
2406
2407 // "load" the name and resove the link
2408 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
2409 if (hr != S_OK)
2410 goto shortcut_error;
2411#if 0 // This makes Vim wait a long time if the target doesn't exist.
2412 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
2413 if (hr != S_OK)
2414 goto shortcut_error;
2415#endif
2416
2417 // Get the path to the link target.
2418 ZeroMemory(buf, MAX_PATH);
2419 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
2420 if (hr == S_OK && buf[0] != NUL)
2421 rfname = vim_strsave(buf);
2422
2423shortcut_error:
2424 // Release all interface pointers (both belong to the same object)
2425 if (ppf != NULL)
2426 ppf->lpVtbl->Release(ppf);
2427 if (psl != NULL)
2428 psl->lpVtbl->Release(psl);
2429
2430 CoUninitialize();
2431 return rfname;
2432}
2433#endif
2434
2435#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
2436/*
2437 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
2438 */
2439 void
2440win32_set_foreground()
2441{
2442# ifndef FEAT_GUI
2443 GetConsoleHwnd(); /* get value of s_hwnd */
2444# endif
2445 if (s_hwnd != 0)
2446 SetForegroundWindow(s_hwnd);
2447}
2448#endif
2449
2450#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2451/*
2452 * Client-server code for Vim
2453 *
2454 * Originally written by Paul Moore
2455 */
2456
2457/* In order to handle inter-process messages, we need to have a window. But
2458 * the functions in this module can be called before the main GUI window is
2459 * created (and may also be called in the console version, where there is no
2460 * GUI window at all).
2461 *
2462 * So we create a hidden window, and arrange to destroy it on exit.
2463 */
2464HWND message_window = 0; /* window that's handling messsages */
2465
2466#define VIM_CLASSNAME "VIM_MESSAGES"
2467#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2468
2469/* Communication is via WM_COPYDATA messages. The message type is send in
2470 * the dwData parameter. Types are defined here. */
2471#define COPYDATA_KEYS 0
2472#define COPYDATA_REPLY 1
2473#define COPYDATA_EXPR 10
2474#define COPYDATA_RESULT 11
2475#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002476#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478/* This is a structure containing a server HWND and its name. */
2479struct server_id
2480{
2481 HWND hwnd;
2482 char_u *name;
2483};
2484
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002485/* Last received 'encoding' that the client uses. */
2486static char_u *client_enc = NULL;
2487
2488/*
2489 * Tell the other side what encoding we are using.
2490 * Errors are ignored.
2491 */
2492 static void
2493serverSendEnc(HWND target)
2494{
2495 COPYDATASTRUCT data;
2496
2497 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002498#ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002499 data.cbData = STRLEN(p_enc) + 1;
2500 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002501#else
2502 data.cbData = STRLEN("latin1") + 1;
2503 data.lpData = "latin1";
2504#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002505 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2506 (LPARAM)(&data));
2507}
2508
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509/*
2510 * Clean up on exit. This destroys the hidden message window.
2511 */
2512 static void
2513#ifdef __BORLANDC__
2514 _RTLENTRYF
2515#endif
2516CleanUpMessaging(void)
2517{
2518 if (message_window != 0)
2519 {
2520 DestroyWindow(message_window);
2521 message_window = 0;
2522 }
2523}
2524
2525static int save_reply(HWND server, char_u *reply, int expr);
2526
2527/*s
2528 * The window procedure for the hidden message window.
2529 * It handles callback messages and notifications from servers.
2530 * In order to process these messages, it is necessary to run a
2531 * message loop. Code which may run before the main message loop
2532 * is started (in the GUI) is careful to pump messages when it needs
2533 * to. Features which require message delivery during normal use will
2534 * not work in the console version - this basically means those
2535 * features which allow Vim to act as a server, rather than a client.
2536 */
2537 static LRESULT CALLBACK
2538Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2539{
2540 if (msg == WM_COPYDATA)
2541 {
2542 /* This is a message from another Vim. The dwData member of the
2543 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002544 * COPYDATA_ENCODING:
2545 * The encoding that the client uses. Following messages will
2546 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 * COPYDATA_KEYS:
2548 * A key sequence. We are a server, and a client wants these keys
2549 * adding to the input queue.
2550 * COPYDATA_REPLY:
2551 * A reply. We are a client, and a server has sent this message
2552 * in response to a request. (server2client())
2553 * COPYDATA_EXPR:
2554 * An expression. We are a server, and a client wants us to
2555 * evaluate this expression.
2556 * COPYDATA_RESULT:
2557 * A reply. We are a client, and a server has sent this message
2558 * in response to a COPYDATA_EXPR.
2559 * COPYDATA_ERROR_RESULT:
2560 * A reply. We are a client, and a server has sent this message
2561 * in response to a COPYDATA_EXPR that failed to evaluate.
2562 */
2563 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2564 HWND sender = (HWND)wParam;
2565 COPYDATASTRUCT reply;
2566 char_u *res;
2567 char_u winstr[30];
2568 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002569 char_u *str;
2570 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
2572 switch (data->dwData)
2573 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002574 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002575# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002576 /* Remember the encoding that the client uses. */
2577 vim_free(client_enc);
2578 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002579# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002580 return 1;
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 case COPYDATA_KEYS:
2583 /* Remember who sent this, for <client> */
2584 clientWindow = sender;
2585
2586 /* Add the received keys to the input buffer. The loop waiting
2587 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002588 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2589 server_to_input_buf(str);
2590 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592# ifdef FEAT_GUI
2593 /* Wake up the main GUI loop. */
2594 if (s_hwnd != 0)
2595 PostMessage(s_hwnd, WM_NULL, 0, 0);
2596# endif
2597 return 1;
2598
2599 case COPYDATA_EXPR:
2600 /* Remember who sent this, for <client> */
2601 clientWindow = sender;
2602
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002603 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2604 res = eval_client_expr_to_string(str);
2605 vim_free(tofree);
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 if (res == NULL)
2608 {
2609 res = vim_strsave(_(e_invexprmsg));
2610 reply.dwData = COPYDATA_ERROR_RESULT;
2611 }
2612 else
2613 reply.dwData = COPYDATA_RESULT;
2614 reply.lpData = res;
2615 reply.cbData = STRLEN(res) + 1;
2616
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002617 serverSendEnc(sender);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 retval = SendMessage(sender, WM_COPYDATA, (WPARAM)message_window,
2619 (LPARAM)(&reply));
2620 vim_free(res);
2621 return retval;
2622
2623 case COPYDATA_REPLY:
2624 case COPYDATA_RESULT:
2625 case COPYDATA_ERROR_RESULT:
2626 if (data->lpData != NULL)
2627 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002628 str = serverConvert(client_enc, (char_u *)data->lpData,
2629 &tofree);
2630 if (tofree == NULL)
2631 str = vim_strsave(str);
2632 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 (data->dwData == COPYDATA_REPLY ? 0 :
2634 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002635 2))) == FAIL)
2636 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002638 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 {
2640 sprintf((char *)winstr, "0x%x", (unsigned)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002641 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 TRUE, curbuf);
2643 }
2644#endif
2645 }
2646 return 1;
2647 }
2648
2649 return 0;
2650 }
2651
2652 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2653 {
2654 /* When the message window is activated (brought to the foreground),
2655 * this actually applies to the text window. */
2656#ifndef FEAT_GUI
2657 GetConsoleHwnd(); /* get value of s_hwnd */
2658#endif
2659 if (s_hwnd != 0)
2660 {
2661 SetForegroundWindow(s_hwnd);
2662 return 0;
2663 }
2664 }
2665
2666 return DefWindowProc(hwnd, msg, wParam, lParam);
2667}
2668
2669/*
2670 * Initialise the message handling process. This involves creating a window
2671 * to handle messages - the window will not be visible.
2672 */
2673 void
2674serverInitMessaging(void)
2675{
2676 WNDCLASS wndclass;
2677 HINSTANCE s_hinst;
2678
2679 /* Clean up on exit */
2680 atexit(CleanUpMessaging);
2681
2682 /* Register a window class - we only really care
2683 * about the window procedure
2684 */
2685 s_hinst = (HINSTANCE)GetModuleHandle(0);
2686 wndclass.style = 0;
2687 wndclass.lpfnWndProc = Messaging_WndProc;
2688 wndclass.cbClsExtra = 0;
2689 wndclass.cbWndExtra = 0;
2690 wndclass.hInstance = s_hinst;
2691 wndclass.hIcon = NULL;
2692 wndclass.hCursor = NULL;
2693 wndclass.hbrBackground = NULL;
2694 wndclass.lpszMenuName = NULL;
2695 wndclass.lpszClassName = VIM_CLASSNAME;
2696 RegisterClass(&wndclass);
2697
2698 /* Create the message window. It will be hidden, so the details don't
2699 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2700 * focus from gvim. */
2701 message_window = CreateWindow(VIM_CLASSNAME, "",
2702 WS_POPUPWINDOW | WS_CAPTION,
2703 CW_USEDEFAULT, CW_USEDEFAULT,
2704 100, 100, NULL, NULL,
2705 s_hinst, NULL);
2706}
2707
2708/*
2709 * Get the title of the window "hwnd", which is the Vim server name, in
2710 * "name[namelen]" and return the length.
2711 * Returns zero if window "hwnd" is not a Vim server.
2712 */
2713 static int
2714getVimServerName(HWND hwnd, char *name, int namelen)
2715{
2716 int len;
2717 char buffer[VIM_CLASSNAME_LEN + 1];
2718
2719 /* Ignore windows which aren't Vim message windows */
2720 len = GetClassName(hwnd, buffer, sizeof(buffer));
2721 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2722 return 0;
2723
2724 /* Get the title of the window */
2725 return GetWindowText(hwnd, name, namelen);
2726}
2727
2728 static BOOL CALLBACK
2729enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2730{
2731 struct server_id *id = (struct server_id *)lparam;
2732 char server[MAX_PATH];
2733
2734 /* Get the title of the window */
2735 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2736 return TRUE;
2737
2738 /* If this is the server we're looking for, return its HWND */
2739 if (STRICMP(server, id->name) == 0)
2740 {
2741 id->hwnd = hwnd;
2742 return FALSE;
2743 }
2744
2745 /* Otherwise, keep looking */
2746 return TRUE;
2747}
2748
2749 static BOOL CALLBACK
2750enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2751{
2752 garray_T *ga = (garray_T *)lparam;
2753 char server[MAX_PATH];
2754
2755 /* Get the title of the window */
2756 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2757 return TRUE;
2758
2759 /* Add the name to the list */
2760 ga_concat(ga, server);
2761 ga_concat(ga, "\n");
2762 return TRUE;
2763}
2764
2765 static HWND
2766findServer(char_u *name)
2767{
2768 struct server_id id;
2769
2770 id.name = name;
2771 id.hwnd = 0;
2772
2773 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2774
2775 return id.hwnd;
2776}
2777
2778 void
2779serverSetName(char_u *name)
2780{
2781 char_u *ok_name;
2782 HWND hwnd = 0;
2783 int i = 0;
2784 char_u *p;
2785
2786 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
2787 ok_name = alloc(STRLEN(name) + 10);
2788
2789 STRCPY(ok_name, name);
2790 p = ok_name + STRLEN(name);
2791
2792 for (;;)
2793 {
2794 /* This is inefficient - we're doing an EnumWindows loop for each
2795 * possible name. It would be better to grab all names in one go,
2796 * and scan the list each time...
2797 */
2798 hwnd = findServer(ok_name);
2799 if (hwnd == 0)
2800 break;
2801
2802 ++i;
2803 if (i >= 1000)
2804 break;
2805
2806 sprintf((char *)p, "%d", i);
2807 }
2808
2809 if (hwnd != 0)
2810 vim_free(ok_name);
2811 else
2812 {
2813 /* Remember the name */
2814 serverName = ok_name;
2815#ifdef FEAT_TITLE
2816 need_maketitle = TRUE; /* update Vim window title later */
2817#endif
2818
2819 /* Update the message window title */
2820 SetWindowText(message_window, ok_name);
2821
2822#ifdef FEAT_EVAL
2823 /* Set the servername variable */
2824 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2825#endif
2826 }
2827}
2828
2829 char_u *
2830serverGetVimNames(void)
2831{
2832 garray_T ga;
2833
2834 ga_init2(&ga, 1, 100);
2835
2836 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002837 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
2839 return ga.ga_data;
2840}
2841
2842 int
2843serverSendReply(name, reply)
2844 char_u *name; /* Where to send. */
2845 char_u *reply; /* What to send. */
2846{
2847 HWND target;
2848 COPYDATASTRUCT data;
2849 int n = 0;
2850
2851 /* The "name" argument is a magic cookie obtained from expand("<client>").
2852 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2853 * value of the client's message window HWND.
2854 */
2855 sscanf((char *)name, "%x", &n);
2856 if (n == 0)
2857 return -1;
2858
2859 target = (HWND)n;
2860 if (!IsWindow(target))
2861 return -1;
2862
2863 data.dwData = COPYDATA_REPLY;
2864 data.cbData = STRLEN(reply) + 1;
2865 data.lpData = reply;
2866
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002867 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2869 (LPARAM)(&data)))
2870 return 0;
2871
2872 return -1;
2873}
2874
2875 int
2876serverSendToVim(name, cmd, result, ptarget, asExpr, silent)
2877 char_u *name; /* Where to send. */
2878 char_u *cmd; /* What to send. */
2879 char_u **result; /* Result of eval'ed expression */
2880 void *ptarget; /* HWND of server */
2881 int asExpr; /* Expression or keys? */
2882 int silent; /* don't complain about no server */
2883{
2884 HWND target = findServer(name);
2885 COPYDATASTRUCT data;
2886 char_u *retval = NULL;
2887 int retcode = 0;
2888
2889 if (target == 0)
2890 {
2891 if (!silent)
2892 EMSG2(_(e_noserver), name);
2893 return -1;
2894 }
2895
2896 if (ptarget)
2897 *(HWND *)ptarget = target;
2898
2899 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
2900 data.cbData = STRLEN(cmd) + 1;
2901 data.lpData = cmd;
2902
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002903 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2905 (LPARAM)(&data)) == 0)
2906 return -1;
2907
2908 if (asExpr)
2909 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2910
2911 if (result == NULL)
2912 vim_free(retval);
2913 else
2914 *result = retval; /* Caller assumes responsibility for freeing */
2915
2916 return retcode;
2917}
2918
2919/*
2920 * Bring the server to the foreground.
2921 */
2922 void
2923serverForeground(name)
2924 char_u *name;
2925{
2926 HWND target = findServer(name);
2927
2928 if (target != 0)
2929 SetForegroundWindow(target);
2930}
2931
2932/* Replies from server need to be stored until the client picks them up via
2933 * remote_read(). So we maintain a list of server-id/reply pairs.
2934 * Note that there could be multiple replies from one server pending if the
2935 * client is slow picking them up.
2936 * We just store the replies in a simple list. When we remove an entry, we
2937 * move list entries down to fill the gap.
2938 * The server ID is simply the HWND.
2939 */
2940typedef struct
2941{
2942 HWND server; /* server window */
2943 char_u *reply; /* reply string */
2944 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002945} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2948
2949#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2950#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951
2952/* Flag which is used to wait for a reply */
2953static int reply_received = 0;
2954
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002955/*
2956 * Store a reply. "reply" must be allocated memory (or NULL).
2957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 static int
2959save_reply(HWND server, char_u *reply, int expr)
2960{
2961 reply_T *rep;
2962
2963 if (ga_grow(&reply_list, 1) == FAIL)
2964 return FAIL;
2965
2966 rep = REPLY_ITEM(REPLY_COUNT);
2967 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002968 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 rep->expr_result = expr;
2970 if (rep->reply == NULL)
2971 return FAIL;
2972
2973 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 reply_received = 1;
2975 return OK;
2976}
2977
2978/*
2979 * Get a reply from server "server".
2980 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2981 * server2client() message.
2982 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2983 * If "remove" is TRUE, consume the message, the caller must free it then.
2984 * if "wait" is TRUE block until a message arrives (or the server exits).
2985 */
2986 char_u *
2987serverGetReply(HWND server, int *expr_res, int remove, int wait)
2988{
2989 int i;
2990 char_u *reply;
2991 reply_T *rep;
2992
2993 /* When waiting, loop until the message waiting for is received. */
2994 for (;;)
2995 {
2996 /* Reset this here, in case a message arrives while we are going
2997 * through the already received messages. */
2998 reply_received = 0;
2999
3000 for (i = 0; i < REPLY_COUNT; ++i)
3001 {
3002 rep = REPLY_ITEM(i);
3003 if (rep->server == server
3004 && ((rep->expr_result != 0) == (expr_res != NULL)))
3005 {
3006 /* Save the values we've found for later */
3007 reply = rep->reply;
3008 if (expr_res != NULL)
3009 *expr_res = rep->expr_result == 1 ? 0 : -1;
3010
3011 if (remove)
3012 {
3013 /* Move the rest of the list down to fill the gap */
3014 mch_memmove(rep, rep + 1,
3015 (REPLY_COUNT - i - 1) * sizeof(reply_T));
3016 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018
3019 /* Return the reply to the caller, who takes on responsibility
3020 * for freeing it if "remove" is TRUE. */
3021 return reply;
3022 }
3023 }
3024
3025 /* If we got here, we didn't find a reply. Return immediately if the
3026 * "wait" parameter isn't set. */
3027 if (!wait)
3028 break;
3029
3030 /* We need to wait for a reply. Enter a message loop until the
3031 * "reply_received" flag gets set. */
3032
3033 /* Loop until we receive a reply */
3034 while (reply_received == 0)
3035 {
3036 /* Wait for a SendMessage() call to us. This could be the reply
3037 * we are waiting for. Use a timeout of a second, to catch the
3038 * situation that the server died unexpectedly. */
3039 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
3040
3041 /* If the server has died, give up */
3042 if (!IsWindow(server))
3043 return NULL;
3044
3045 serverProcessPendingMessages();
3046 }
3047 }
3048
3049 return NULL;
3050}
3051
3052/*
3053 * Process any messages in the Windows message queue.
3054 */
3055 void
3056serverProcessPendingMessages(void)
3057{
3058 MSG msg;
3059
3060 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
3061 {
3062 TranslateMessage(&msg);
3063 DispatchMessage(&msg);
3064 }
3065}
3066
3067#endif /* FEAT_CLIENTSERVER */
3068
3069#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
3070 || defined(PROTO)
3071
3072struct charset_pair
3073{
3074 char *name;
3075 BYTE charset;
3076};
3077
3078static struct charset_pair
3079charset_pairs[] =
3080{
3081 {"ANSI", ANSI_CHARSET},
3082 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
3083 {"DEFAULT", DEFAULT_CHARSET},
3084 {"HANGEUL", HANGEUL_CHARSET},
3085 {"OEM", OEM_CHARSET},
3086 {"SHIFTJIS", SHIFTJIS_CHARSET},
3087 {"SYMBOL", SYMBOL_CHARSET},
3088#ifdef WIN3264
3089 {"ARABIC", ARABIC_CHARSET},
3090 {"BALTIC", BALTIC_CHARSET},
3091 {"EASTEUROPE", EASTEUROPE_CHARSET},
3092 {"GB2312", GB2312_CHARSET},
3093 {"GREEK", GREEK_CHARSET},
3094 {"HEBREW", HEBREW_CHARSET},
3095 {"JOHAB", JOHAB_CHARSET},
3096 {"MAC", MAC_CHARSET},
3097 {"RUSSIAN", RUSSIAN_CHARSET},
3098 {"THAI", THAI_CHARSET},
3099 {"TURKISH", TURKISH_CHARSET},
3100# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
3101 && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
3102 {"VIETNAMESE", VIETNAMESE_CHARSET},
3103# endif
3104#endif
3105 {NULL, 0}
3106};
3107
3108/*
3109 * Convert a charset ID to a name.
3110 * Return NULL when not recognized.
3111 */
3112 char *
3113charset_id2name(int id)
3114{
3115 struct charset_pair *cp;
3116
3117 for (cp = charset_pairs; cp->name != NULL; ++cp)
3118 if ((BYTE)id == cp->charset)
3119 break;
3120 return cp->name;
3121}
3122
3123static const LOGFONT s_lfDefault =
3124{
3125 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
3126 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
3127 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
3128 "Fixedsys" /* see _ReadVimIni */
3129};
3130
3131/* Initialise the "current height" to -12 (same as s_lfDefault) just
3132 * in case the user specifies a font in "guifont" with no size before a font
3133 * with an explicit size has been set. This defaults the size to this value
3134 * (-12 equates to roughly 9pt).
3135 */
3136int current_font_height = -12; /* also used in gui_w48.c */
3137
3138/* Convert a string representing a point size into pixels. The string should
3139 * be a positive decimal number, with an optional decimal point (eg, "12", or
3140 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
3141 * character is stored in *end. The flag "vertical" says whether this
3142 * calculation is for a vertical (height) size or a horizontal (width) one.
3143 */
3144 static int
3145points_to_pixels(char_u *str, char_u **end, int vertical, int pprinter_dc)
3146{
3147 int pixels;
3148 int points = 0;
3149 int divisor = 0;
3150 HWND hwnd = (HWND)0;
3151 HDC hdc;
3152 HDC printer_dc = (HDC)pprinter_dc;
3153
3154 while (*str != NUL)
3155 {
3156 if (*str == '.' && divisor == 0)
3157 {
3158 /* Start keeping a divisor, for later */
3159 divisor = 1;
3160 }
3161 else
3162 {
3163 if (!VIM_ISDIGIT(*str))
3164 break;
3165
3166 points *= 10;
3167 points += *str - '0';
3168 divisor *= 10;
3169 }
3170 ++str;
3171 }
3172
3173 if (divisor == 0)
3174 divisor = 1;
3175
3176 if (printer_dc == NULL)
3177 {
3178 hwnd = GetDesktopWindow();
3179 hdc = GetWindowDC(hwnd);
3180 }
3181 else
3182 hdc = printer_dc;
3183
3184 pixels = MulDiv(points,
3185 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
3186 72 * divisor);
3187
3188 if (printer_dc == NULL)
3189 ReleaseDC(hwnd, hdc);
3190
3191 *end = str;
3192 return pixels;
3193}
3194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003195/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 static int CALLBACK
3197font_enumproc(
3198 ENUMLOGFONT *elf,
3199 NEWTEXTMETRIC *ntm,
3200 int type,
3201 LPARAM lparam)
3202{
3203 /* Return value:
3204 * 0 = terminate now (monospace & ANSI)
3205 * 1 = continue, still no luck...
3206 * 2 = continue, but we have an acceptable LOGFONT
3207 * (monospace, not ANSI)
3208 * We use these values, as EnumFontFamilies returns 1 if the
3209 * callback function is never called. So, we check the return as
3210 * 0 = perfect, 2 = OK, 1 = no good...
3211 * It's not pretty, but it works!
3212 */
3213
3214 LOGFONT *lf = (LOGFONT *)(lparam);
3215
3216#ifndef FEAT_PROPORTIONAL_FONTS
3217 /* Ignore non-monospace fonts without further ado */
3218 if ((ntm->tmPitchAndFamily & 1) != 0)
3219 return 1;
3220#endif
3221
3222 /* Remember this LOGFONT as a "possible" */
3223 *lf = elf->elfLogFont;
3224
3225 /* Terminate the scan as soon as we find an ANSI font */
3226 if (lf->lfCharSet == ANSI_CHARSET
3227 || lf->lfCharSet == OEM_CHARSET
3228 || lf->lfCharSet == DEFAULT_CHARSET)
3229 return 0;
3230
3231 /* Continue the scan - we have a non-ANSI font */
3232 return 2;
3233}
3234
3235 static int
3236init_logfont(LOGFONT *lf)
3237{
3238 int n;
3239 HWND hwnd = GetDesktopWindow();
3240 HDC hdc = GetWindowDC(hwnd);
3241
3242 n = EnumFontFamilies(hdc,
3243 (LPCSTR)lf->lfFaceName,
3244 (FONTENUMPROC)font_enumproc,
3245 (LPARAM)lf);
3246
3247 ReleaseDC(hwnd, hdc);
3248
3249 /* If we couldn't find a useable font, return failure */
3250 if (n == 1)
3251 return FAIL;
3252
3253 /* Tidy up the rest of the LOGFONT structure. We set to a basic
3254 * font - get_logfont() sets bold, italic, etc based on the user's
3255 * input.
3256 */
3257 lf->lfHeight = current_font_height;
3258 lf->lfWidth = 0;
3259 lf->lfItalic = FALSE;
3260 lf->lfUnderline = FALSE;
3261 lf->lfStrikeOut = FALSE;
3262 lf->lfWeight = FW_NORMAL;
3263
3264 /* Return success */
3265 return OK;
3266}
3267
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003268/*
3269 * Get font info from "name" into logfont "lf".
3270 * Return OK for a valid name, FAIL otherwise.
3271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 int
3273get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003274 LOGFONT *lf,
3275 char_u *name,
3276 HDC printer_dc,
3277 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
3279 char_u *p;
3280 int i;
3281 static LOGFONT *lastlf = NULL;
3282
3283 *lf = s_lfDefault;
3284 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003285 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
3287 if (STRCMP(name, "*") == 0)
3288 {
3289#if defined(FEAT_GUI_W32)
3290 CHOOSEFONT cf;
3291 /* if name is "*", bring up std font dialog: */
3292 memset(&cf, 0, sizeof(cf));
3293 cf.lStructSize = sizeof(cf);
3294 cf.hwndOwner = s_hwnd;
3295 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
3296 if (lastlf != NULL)
3297 *lf = *lastlf;
3298 cf.lpLogFont = lf;
3299 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
3300 if (ChooseFont(&cf))
3301 goto theend;
3302#else
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003303 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#endif
3305 }
3306
3307 /*
3308 * Split name up, it could be <name>:h<height>:w<width> etc.
3309 */
3310 for (p = name; *p && *p != ':'; p++)
3311 {
3312 if (p - name + 1 > LF_FACESIZE)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003313 return FAIL; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 lf->lfFaceName[p - name] = *p;
3315 }
3316 if (p != name)
3317 lf->lfFaceName[p - name] = NUL;
3318
3319 /* First set defaults */
3320 lf->lfHeight = -12;
3321 lf->lfWidth = 0;
3322 lf->lfWeight = FW_NORMAL;
3323 lf->lfItalic = FALSE;
3324 lf->lfUnderline = FALSE;
3325 lf->lfStrikeOut = FALSE;
3326
3327 /*
3328 * If the font can't be found, try replacing '_' by ' '.
3329 */
3330 if (init_logfont(lf) == FAIL)
3331 {
3332 int did_replace = FALSE;
3333
3334 for (i = 0; lf->lfFaceName[i]; ++i)
3335 if (lf->lfFaceName[i] == '_')
3336 {
3337 lf->lfFaceName[i] = ' ';
3338 did_replace = TRUE;
3339 }
3340 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343
3344 while (*p == ':')
3345 p++;
3346
3347 /* Set the values found after ':' */
3348 while (*p)
3349 {
3350 switch (*p++)
3351 {
3352 case 'h':
3353 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (int)printer_dc);
3354 break;
3355 case 'w':
3356 lf->lfWidth = points_to_pixels(p, &p, FALSE, (int)printer_dc);
3357 break;
3358 case 'b':
3359#ifndef MSWIN16_FASTTEXT
3360 lf->lfWeight = FW_BOLD;
3361#endif
3362 break;
3363 case 'i':
3364#ifndef MSWIN16_FASTTEXT
3365 lf->lfItalic = TRUE;
3366#endif
3367 break;
3368 case 'u':
3369 lf->lfUnderline = TRUE;
3370 break;
3371 case 's':
3372 lf->lfStrikeOut = TRUE;
3373 break;
3374 case 'c':
3375 {
3376 struct charset_pair *cp;
3377
3378 for (cp = charset_pairs; cp->name != NULL; ++cp)
3379 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
3380 {
3381 lf->lfCharSet = cp->charset;
3382 p += strlen(cp->name);
3383 break;
3384 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003385 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003387 vim_snprintf((char *)IObuff, IOSIZE,
3388 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 EMSG(IObuff);
3390 break;
3391 }
3392 break;
3393 }
3394 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003395 if (verbose)
3396 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003397 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003398 _("E245: Illegal char '%c' in font name \"%s\""),
3399 p[-1], name);
3400 EMSG(IObuff);
3401 }
3402 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
3404 while (*p == ':')
3405 p++;
3406 }
3407
3408#if defined(FEAT_GUI_W32)
3409theend:
3410#endif
3411 /* ron: init lastlf */
3412 if (printer_dc == NULL)
3413 {
3414 vim_free(lastlf);
3415 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3416 if (lastlf != NULL)
3417 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3418 }
3419
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003420 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421}
3422
3423#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */