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 /* pure mswindows behaviour only says we can own the selection
106 if it is the clipboard */
107 EQ (selection_name, QCLIPBOARD))
108 Fmswindows_set_clipboard (XCDR (converted_value));
113 DEFUN ("mswindows-get-clipboard", Fmswindows_get_clipboard, 0, 0, 0, /*
114 Return the contents of the mswindows clipboard.
119 unsigned char *src, *dst, *next;
120 Lisp_Object ret = Qnil;
122 if (!OpenClipboard (NULL))
125 if ((h = GetClipboardData (CF_TEXT)) != NULL &&
126 (src = (unsigned char *) GlobalLock (h)) != NULL)
130 size = rawsize = strlen (src);
132 for (i=0; i<rawsize; i++)
133 if (src[i] == '\r' && src[i+1] == '\n')
136 /* Convert CRLFs to LFs */
137 ret = make_uninit_string (size);
138 dst = XSTRING_DATA (ret);
141 /* copy next line or remaining bytes excluding '\0' */
142 next = memccpy (dst, src, '\r', rawsize);
145 /* copied one line ending with '\r' */
146 int copied = next - dst;
150 dst += copied - 1; /* overwrite '\r' */
166 mswindows_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type)
168 if (EQ (selection_symbol, QCLIPBOARD))
169 return Fmswindows_get_clipboard ();
174 DEFUN ("mswindows-selection-exists-p", Fmswindows_selection_exists_p, 0, 0, 0, /*
175 Whether there is an MS-Windows selection.
179 return IsClipboardFormatAvailable (CF_TEXT) ? Qt : Qnil;
182 DEFUN ("mswindows-delete-selection", Fmswindows_delete_selection, 0, 0, 0, /*
183 Remove the current MS-Windows selection from the clipboard.
187 return EmptyClipboard () ? Qt : Qnil;
191 mswindows_disown_selection (Lisp_Object selection, Lisp_Object timeval)
193 if (EQ (selection, QCLIPBOARD))
194 Fmswindows_delete_selection ();
198 /************************************************************************/
200 /************************************************************************/
203 console_type_create_select_mswindows (void)
205 CONSOLE_HAS_METHOD (mswindows, own_selection);
206 CONSOLE_HAS_METHOD (mswindows, disown_selection);
207 CONSOLE_HAS_METHOD (mswindows, get_foreign_selection);
211 syms_of_select_mswindows (void)
213 DEFSUBR (Fmswindows_set_clipboard);
214 DEFSUBR (Fmswindows_get_clipboard);
215 DEFSUBR (Fmswindows_selection_exists_p);
216 DEFSUBR (Fmswindows_delete_selection);
220 vars_of_select_mswindows (void)