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.
35 #include "console-msw.h"
37 DEFUN ("mswindows-set-clipboard", Fmswindows_set_clipboard, 1, 1, 0, /*
38 Copy STRING to the mswindows clipboard.
43 unsigned char *src, *dst, *next;
45 struct frame *f = NULL;
47 CHECK_STRING (string);
49 /* Calculate size with LFs converted to CRLFs because
50 * CF_TEXT format uses CRLF delimited ASCIIZ */
51 src = XSTRING_DATA (string);
52 size = rawsize = XSTRING_LENGTH (string) + 1;
53 for (i=0; i<rawsize; i++)
57 f = selected_frame ();
58 if (!OpenClipboard (FRAME_MSWINDOWS_HANDLE (f)))
61 if (!EmptyClipboard () ||
62 (h = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, size)) == NULL ||
63 (dst = (unsigned char *) GlobalLock (h)) == NULL)
65 if (h != NULL) GlobalFree (h);
70 /* Convert LFs to CRLFs */
73 /* copy next line or remaining bytes including '\0' */
74 next = memccpy (dst, src, '\n', rawsize);
77 /* copied one line ending with '\n' */
78 int copied = next - dst;
81 /* insert '\r' before '\n' */
91 i = (SetClipboardData (CF_TEXT, h) != NULL);
98 /* Do protocol to assert ourself as a selection owner. Under mswindows
99 this is easy, we just set the clipboard. */
101 mswindows_own_selection (Lisp_Object selection_name, Lisp_Object selection_value)
103 Lisp_Object converted_value = get_local_selection (selection_name, QSTRING);
104 if (!NILP (converted_value) &&
105 CONSP (converted_value) &&
106 EQ (XCAR (converted_value), QSTRING) &&
107 /* pure mswindows behaviour only says we can own the selection
108 if it is the clipboard */
109 EQ (selection_name, QCLIPBOARD))
110 Fmswindows_set_clipboard (XCDR (converted_value));
115 DEFUN ("mswindows-get-clipboard", Fmswindows_get_clipboard, 0, 0, 0, /*
116 Return the contents of the mswindows clipboard.
121 unsigned char *src, *dst, *next;
122 Lisp_Object ret = Qnil;
124 if (!OpenClipboard (NULL))
127 if ((h = GetClipboardData (CF_TEXT)) != NULL &&
128 (src = (unsigned char *) GlobalLock (h)) != NULL)
132 size = rawsize = strlen (src);
134 for (i=0; i<rawsize; i++)
135 if (src[i] == '\r' && src[i+1] == '\n')
138 /* Convert CRLFs to LFs */
139 ret = make_uninit_string (size);
140 dst = XSTRING_DATA (ret);
143 /* copy next line or remaining bytes excluding '\0' */
144 next = memccpy (dst, src, '\r', rawsize);
147 /* copied one line ending with '\r' */
148 int copied = next - dst;
152 dst += copied - 1; /* overwrite '\r' */
168 mswindows_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type)
170 if (EQ (selection_symbol, QCLIPBOARD))
171 return Fmswindows_get_clipboard ();
176 DEFUN ("mswindows-selection-exists-p", Fmswindows_selection_exists_p, 0, 0, 0, /*
177 Whether there is an MS-Windows selection.
181 return IsClipboardFormatAvailable (CF_TEXT) ? Qt : Qnil;
184 DEFUN ("mswindows-delete-selection", Fmswindows_delete_selection, 0, 0, 0, /*
185 Remove the current MS-Windows selection from the clipboard.
189 BOOL success = OpenClipboard (NULL);
192 success = EmptyClipboard ();
193 /* Close it regardless of whether empty worked. */
194 if (!CloseClipboard ())
198 return success ? Qt : Qnil;
202 mswindows_disown_selection (Lisp_Object selection, Lisp_Object timeval)
204 if (EQ (selection, QCLIPBOARD))
205 Fmswindows_delete_selection ();
209 /************************************************************************/
211 /************************************************************************/
214 console_type_create_select_mswindows (void)
216 CONSOLE_HAS_METHOD (mswindows, own_selection);
217 CONSOLE_HAS_METHOD (mswindows, disown_selection);
218 CONSOLE_HAS_METHOD (mswindows, get_foreign_selection);
222 syms_of_select_mswindows (void)
224 DEFSUBR (Fmswindows_set_clipboard);
225 DEFSUBR (Fmswindows_get_clipboard);
226 DEFSUBR (Fmswindows_selection_exists_p);
227 DEFSUBR (Fmswindows_delete_selection);
231 vars_of_select_mswindows (void)