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"
38 /* Do protocol to assert ourself as a selection owner. Under mswindows
39 this is easy, we just set the clipboard. */
41 mswindows_own_selection (Lisp_Object selection_name,
42 Lisp_Object selection_value)
44 Lisp_Object converted_value = get_local_selection (selection_name, QSTRING);
46 if (!NILP (converted_value) &&
47 CONSP (converted_value) &&
48 EQ (XCAR (converted_value), QSTRING) &&
49 /* pure mswindows behaviour only says we can own the selection
50 if it is the clipboard */
51 EQ (selection_name, QCLIPBOARD))
54 unsigned char *src, *dst, *next;
56 struct frame *f = NULL;
57 struct gcpro gcpro1, gcpro2;
58 Lisp_Object string = XCDR (converted_value);
60 GCPRO2 (converted_value, string);
62 CHECK_STRING (string);
64 /* Calculate size with LFs converted to CRLFs because
65 * CF_TEXT format uses CRLF delimited ASCIIZ */
66 src = XSTRING_DATA (string);
67 size = rawsize = XSTRING_LENGTH (string) + 1;
68 for (i=0; i<rawsize; i++)
72 f = selected_frame ();
73 if (!OpenClipboard (FRAME_MSWINDOWS_HANDLE (f)))
79 /* This call to EmptyClipboard may post an event back to us if
80 we already own the clipboard (to tell us we lost it) and this
81 event may execute random lisp code. Hence we must protect
82 the string and get its address again after the call. */
83 if (!EmptyClipboard () ||
84 (h = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, size)) == NULL ||
85 (dst = (unsigned char *) GlobalLock (h)) == NULL)
87 if (h != NULL) GlobalFree (h);
92 src = XSTRING_DATA (string);
94 /* Convert LFs to CRLFs */
97 /* copy next line or remaining bytes including '\0' */
98 next = (char*) memccpy (dst, src, '\n', rawsize);
101 /* copied one line ending with '\n' */
102 int copied = next - dst;
105 /* insert '\r' before '\n' */
115 i = (SetClipboardData (CF_TEXT, h) != NULL);
120 /* #### we are supposed to return a time! */
121 /* return i ? Qt : Qnil; */
129 mswindows_get_foreign_selection (Lisp_Object selection_symbol,
130 Lisp_Object target_type)
132 if (EQ (selection_symbol, QCLIPBOARD))
135 unsigned char *src, *dst, *next;
136 Lisp_Object ret = Qnil;
138 if (!OpenClipboard (NULL))
141 if ((h = GetClipboardData (CF_TEXT)) != NULL &&
142 (src = (unsigned char *) GlobalLock (h)) != NULL)
146 size = rawsize = strlen (src);
148 for (i=0; i<rawsize; i++)
149 if (src[i] == '\r' && src[i+1] == '\n')
152 /* Convert CRLFs to LFs */
153 ret = make_uninit_string (size);
154 dst = XSTRING_DATA (ret);
157 /* copy next line or remaining bytes excluding '\0' */
158 next = (unsigned char *) memccpy (dst, src, '\r', rawsize);
161 /* copied one line ending with '\r' */
162 int copied = next - dst;
166 dst += copied - 1; /* overwrite '\r' */
185 mswindows_disown_selection (Lisp_Object selection, Lisp_Object timeval)
187 if (EQ (selection, QCLIPBOARD))
189 BOOL success = OpenClipboard (NULL);
192 success = EmptyClipboard ();
193 /* Close it regardless of whether empty worked. */
194 if (!CloseClipboard ())
198 /* #### return success ? Qt : Qnil; */
203 mswindows_selection_exists_p (Lisp_Object selection)
205 if (EQ (selection, QCLIPBOARD))
206 return IsClipboardFormatAvailable (CF_TEXT) ? Qt : Qnil;
212 /************************************************************************/
214 /************************************************************************/
217 console_type_create_select_mswindows (void)
219 CONSOLE_HAS_METHOD (mswindows, own_selection);
220 CONSOLE_HAS_METHOD (mswindows, disown_selection);
221 CONSOLE_HAS_METHOD (mswindows, selection_exists_p);
222 CONSOLE_HAS_METHOD (mswindows, get_foreign_selection);
226 syms_of_select_mswindows (void)
231 vars_of_select_mswindows (void)