comparison src/select-msw.c @ 428:3ecd8885ac67 r21-2-22

Import from CVS: tag r21-2-22
author cvs
date Mon, 13 Aug 2007 11:28:15 +0200
parents
children a5df635868b2
comparison
equal deleted inserted replaced
427:0a0253eac470 428:3ecd8885ac67
1 /* mswindows selection processing for XEmacs
2 Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of XEmacs.
5
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
9 later version.
10
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
14 for more details.
15
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. */
20
21 /* Synched up with: Not synched with FSF. */
22
23 /* Authorship:
24
25 Written by Kevin Gallo for FSF Emacs.
26 Rewritten for mswindows by Jonathan Harris, December 1997 for 21.0.
27 */
28
29
30 #include <config.h>
31 #include "lisp.h"
32 #include "select.h"
33
34 #include "console-msw.h"
35
36 DEFUN ("mswindows-set-clipboard", Fmswindows_set_clipboard, 1, 1, 0, /*
37 Copy STRING to the mswindows clipboard.
38 */
39 (string))
40 {
41 int rawsize, size, i;
42 unsigned char *src, *dst, *next;
43 HGLOBAL h = NULL;
44
45 CHECK_STRING (string);
46
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++)
52 if (src[i] == '\n')
53 size++;
54
55 if (!OpenClipboard (NULL))
56 return Qnil;
57
58 if (!EmptyClipboard () ||
59 (h = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, size)) == NULL ||
60 (dst = (unsigned char *) GlobalLock (h)) == NULL)
61 {
62 if (h != NULL) GlobalFree (h);
63 CloseClipboard ();
64 return Qnil;
65 }
66
67 /* Convert LFs to CRLFs */
68 do
69 {
70 /* copy next line or remaining bytes including '\0' */
71 next = memccpy (dst, src, '\n', rawsize);
72 if (next)
73 {
74 /* copied one line ending with '\n' */
75 int copied = next - dst;
76 rawsize -= copied;
77 src += copied;
78 /* insert '\r' before '\n' */
79 next[-1] = '\r';
80 next[0] = '\n';
81 dst = next+1;
82 }
83 }
84 while (next);
85
86 GlobalUnlock (h);
87
88 i = (SetClipboardData (CF_TEXT, h) != NULL);
89
90 CloseClipboard ();
91 GlobalFree (h);
92
93 return i ? Qt : Qnil;
94 }
95
96 /* Do protocol to assert ourself as a selection owner. Under mswindows
97 this is easy, we just set the clipboard. */
98 static Lisp_Object
99 mswindows_own_selection (Lisp_Object selection_name, Lisp_Object selection_value)
100 {
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));
109
110 return Qnil;
111 }
112
113 DEFUN ("mswindows-get-clipboard", Fmswindows_get_clipboard, 0, 0, 0, /*
114 Return the contents of the mswindows clipboard.
115 */
116 ())
117 {
118 HANDLE h;
119 unsigned char *src, *dst, *next;
120 Lisp_Object ret = Qnil;
121
122 if (!OpenClipboard (NULL))
123 return Qnil;
124
125 if ((h = GetClipboardData (CF_TEXT)) != NULL &&
126 (src = (unsigned char *) GlobalLock (h)) != NULL)
127 {
128 int i;
129 int size, rawsize;
130 size = rawsize = strlen (src);
131
132 for (i=0; i<rawsize; i++)
133 if (src[i] == '\r' && src[i+1] == '\n')
134 size--;
135
136 /* Convert CRLFs to LFs */
137 ret = make_uninit_string (size);
138 dst = XSTRING_DATA (ret);
139 do
140 {
141 /* copy next line or remaining bytes excluding '\0' */
142 next = memccpy (dst, src, '\r', rawsize);
143 if (next)
144 {
145 /* copied one line ending with '\r' */
146 int copied = next - dst;
147 rawsize -= copied;
148 src += copied;
149 if (*src == '\n')
150 dst += copied - 1; /* overwrite '\r' */
151 else
152 dst += copied;
153 }
154 }
155 while (next);
156
157 GlobalUnlock (h);
158 }
159
160 CloseClipboard ();
161
162 return ret;
163 }
164
165 static Lisp_Object
166 mswindows_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type)
167 {
168 if (EQ (selection_symbol, QCLIPBOARD))
169 return Fmswindows_get_clipboard ();
170 else
171 return Qnil;
172 }
173
174 DEFUN ("mswindows-selection-exists-p", Fmswindows_selection_exists_p, 0, 0, 0, /*
175 Whether there is an MS-Windows selection.
176 */
177 ())
178 {
179 return IsClipboardFormatAvailable (CF_TEXT) ? Qt : Qnil;
180 }
181
182 DEFUN ("mswindows-delete-selection", Fmswindows_delete_selection, 0, 0, 0, /*
183 Remove the current MS-Windows selection from the clipboard.
184 */
185 ())
186 {
187 return EmptyClipboard () ? Qt : Qnil;
188 }
189
190 static void
191 mswindows_disown_selection (Lisp_Object selection, Lisp_Object timeval)
192 {
193 if (EQ (selection, QCLIPBOARD))
194 Fmswindows_delete_selection ();
195 }
196
197
198 /************************************************************************/
199 /* initialization */
200 /************************************************************************/
201
202 void
203 console_type_create_select_mswindows (void)
204 {
205 CONSOLE_HAS_METHOD (mswindows, own_selection);
206 CONSOLE_HAS_METHOD (mswindows, disown_selection);
207 CONSOLE_HAS_METHOD (mswindows, get_foreign_selection);
208 }
209
210 void
211 syms_of_select_mswindows (void)
212 {
213 DEFSUBR (Fmswindows_set_clipboard);
214 DEFSUBR (Fmswindows_get_clipboard);
215 DEFSUBR (Fmswindows_selection_exists_p);
216 DEFSUBR (Fmswindows_delete_selection);
217 }
218
219 void
220 vars_of_select_mswindows (void)
221 {
222 }