Contents in 1999-06-04-13 of release-21-2.
[chise/xemacs-chise.git.1] / src / select-msw.c
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     Fmswindows_set_clipboard (XCDR (converted_value));
106
107   return Qnil;
108 }
109
110 DEFUN ("mswindows-get-clipboard", Fmswindows_get_clipboard, 0, 0, 0, /*
111 Return the contents of the mswindows clipboard.
112 */
113        ())
114 {
115   HANDLE h;
116   unsigned char *src, *dst, *next;
117   Lisp_Object ret = Qnil;
118
119   if (!OpenClipboard (NULL))
120     return Qnil;
121
122   if ((h = GetClipboardData (CF_TEXT)) != NULL &&
123       (src = (unsigned char *) GlobalLock (h)) != NULL)
124     {
125       int i;
126       int size, rawsize;
127       size = rawsize = strlen (src);
128
129       for (i=0; i<rawsize; i++)
130         if (src[i] == '\r' && src[i+1] == '\n')
131           size--;
132
133       /* Convert CRLFs to LFs */
134       ret = make_uninit_string (size);
135       dst = XSTRING_DATA (ret);
136       do
137         {
138           /* copy next line or remaining bytes excluding '\0' */
139           next = memccpy (dst, src, '\r', rawsize);
140           if (next)
141             {
142               /* copied one line ending with '\r' */
143               int copied = next - dst;
144               rawsize -= copied;
145               src += copied;
146               if (*src == '\n')
147                 dst += copied - 1;              /* overwrite '\r' */
148               else
149                 dst += copied;
150             }       
151         }
152       while (next);
153
154       GlobalUnlock (h);
155     }
156
157   CloseClipboard ();
158
159   return ret;
160 }
161
162 static Lisp_Object
163 mswindows_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type)
164 {
165   return Fmswindows_get_clipboard ();
166 }
167
168 DEFUN ("mswindows-selection-exists-p", Fmswindows_selection_exists_p, 0, 0, 0, /*
169 Whether there is an MS-Windows selection.
170 */
171        ())
172 {
173   return IsClipboardFormatAvailable (CF_TEXT) ? Qt : Qnil;
174 }
175
176 DEFUN ("mswindows-delete-selection", Fmswindows_delete_selection, 0, 0, 0, /*
177 Remove the current MS-Windows selection from the clipboard.
178 */
179        ())
180 {
181   return EmptyClipboard () ? Qt : Qnil;
182 }
183
184 static void
185 mswindows_disown_selection (Lisp_Object selection, Lisp_Object timeval)
186 {
187   Fmswindows_delete_selection ();
188 }
189
190 \f
191 /************************************************************************/
192 /*                            initialization                            */
193 /************************************************************************/
194
195 void
196 console_type_create_select_mswindows (void)
197 {
198   CONSOLE_HAS_METHOD (mswindows, own_selection);
199   CONSOLE_HAS_METHOD (mswindows, disown_selection);
200   CONSOLE_HAS_METHOD (mswindows, get_foreign_selection);
201 }
202
203 void
204 syms_of_select_mswindows (void)
205 {
206   DEFSUBR (Fmswindows_set_clipboard);
207   DEFSUBR (Fmswindows_get_clipboard);
208   DEFSUBR (Fmswindows_selection_exists_p);
209   DEFSUBR (Fmswindows_delete_selection);
210 }
211
212 void
213 vars_of_select_mswindows (void)
214 {
215 }