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