1 /* mswindows selection processing for XEmacs
2 Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of XEmacs.
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Synched up with: Not synched with FSF. */
25 Written by Kevin Gallo for FSF Emacs.
26 Rewritten for mswindows by Jonathan Harris, December 1997 for 21.0.
34 #include "console-msw.h"
36 DEFUN ("mswindows-set-clipboard", Fmswindows_set_clipboard, 1, 1, 0, /*
37 Copy STRING to the mswindows clipboard.
42 unsigned char *src, *dst, *next;
45 CHECK_STRING (string);
47 /* Calculate size with LFs converted to CRLFs because
48 * CF_TEXT format uses CRLF delimited ASCIIZ */
49 src = XSTRING_DATA (string);
50 size = rawsize = XSTRING_LENGTH (string) + 1;
51 for (i=0; i<rawsize; i++)
55 if (!OpenClipboard (NULL))
58 if (!EmptyClipboard () ||
59 (h = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, size)) == NULL ||
60 (dst = (unsigned char *) GlobalLock (h)) == NULL)
62 if (h != NULL) GlobalFree (h);
67 /* Convert LFs to CRLFs */
70 /* copy next line or remaining bytes including '\0' */
71 next = memccpy (dst, src, '\n', rawsize);
74 /* copied one line ending with '\n' */
75 int copied = next - dst;
78 /* insert '\r' before '\n' */
88 i = (SetClipboardData (CF_TEXT, h) != NULL);
96 /* Do protocol to assert ourself as a selection owner. Under mswindows
97 this is easy, we just set the clipboard. */
99 mswindows_own_selection (Lisp_Object selection_name, Lisp_Object selection_value)
101 Lisp_Object converted_value = get_local_selection (selection_name, QSTRING);
102 if (!NILP (converted_value) &&
103 CONSP (converted_value) &&
104 EQ (XCAR (converted_value), QSTRING))
105 Fmswindows_set_clipboard (XCDR (converted_value));
110 DEFUN ("mswindows-get-clipboard", Fmswindows_get_clipboard, 0, 0, 0, /*
111 Return the contents of the mswindows clipboard.
116 unsigned char *src, *dst, *next;
117 Lisp_Object ret = Qnil;
119 if (!OpenClipboard (NULL))
122 if ((h = GetClipboardData (CF_TEXT)) != NULL &&
123 (src = (unsigned char *) GlobalLock (h)) != NULL)
127 size = rawsize = strlen (src);
129 for (i=0; i<rawsize; i++)
130 if (src[i] == '\r' && src[i+1] == '\n')
133 /* Convert CRLFs to LFs */
134 ret = make_uninit_string (size);
135 dst = XSTRING_DATA (ret);
138 /* copy next line or remaining bytes excluding '\0' */
139 next = memccpy (dst, src, '\r', rawsize);
142 /* copied one line ending with '\r' */
143 int copied = next - dst;
147 dst += copied - 1; /* overwrite '\r' */
163 mswindows_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type)
165 return Fmswindows_get_clipboard ();
168 DEFUN ("mswindows-selection-exists-p", Fmswindows_selection_exists_p, 0, 0, 0, /*
169 Whether there is an MS-Windows selection.
173 return IsClipboardFormatAvailable (CF_TEXT) ? Qt : Qnil;
176 DEFUN ("mswindows-delete-selection", Fmswindows_delete_selection, 0, 0, 0, /*
177 Remove the current MS-Windows selection from the clipboard.
181 return EmptyClipboard () ? Qt : Qnil;
185 mswindows_disown_selection (Lisp_Object selection, Lisp_Object timeval)
187 Fmswindows_delete_selection ();
191 /************************************************************************/
193 /************************************************************************/
196 console_type_create_select_mswindows (void)
198 CONSOLE_HAS_METHOD (mswindows, own_selection);
199 CONSOLE_HAS_METHOD (mswindows, disown_selection);
200 CONSOLE_HAS_METHOD (mswindows, get_foreign_selection);
204 syms_of_select_mswindows (void)
206 DEFSUBR (Fmswindows_set_clipboard);
207 DEFSUBR (Fmswindows_get_clipboard);
208 DEFSUBR (Fmswindows_selection_exists_p);
209 DEFSUBR (Fmswindows_delete_selection);
213 vars_of_select_mswindows (void)