f72a46cf4610a1f2db118099a61b8331a2d783f6
[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 "frame.h"
33 #include "select.h"
34
35 #include "console-msw.h"
36
37 DEFUN ("mswindows-set-clipboard", Fmswindows_set_clipboard, 1, 1, 0, /*
38 Copy STRING to the mswindows clipboard.
39 */
40        (string))
41 {
42   int rawsize, size, i;
43   unsigned char *src, *dst, *next;
44   HGLOBAL h = NULL;
45   struct frame *f = NULL;
46
47   CHECK_STRING (string);
48
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++)
54     if (src[i] == '\n')
55       size++;
56
57   f = selected_frame ();
58   if (!OpenClipboard (FRAME_MSWINDOWS_HANDLE (f)))
59     return Qnil;
60
61   if (!EmptyClipboard () ||
62       (h = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, size)) == NULL ||
63       (dst = (unsigned char *) GlobalLock (h)) == NULL)
64     {
65       if (h != NULL) GlobalFree (h);
66       CloseClipboard ();
67       return Qnil;
68     }
69     
70   /* Convert LFs to CRLFs */
71   do
72     {
73       /* copy next line or remaining bytes including '\0' */
74       next = (char*) memccpy (dst, src, '\n', rawsize);
75       if (next)
76         {
77           /* copied one line ending with '\n' */
78           int copied = next - dst;
79           rawsize -= copied;
80           src += copied;
81           /* insert '\r' before '\n' */
82           next[-1] = '\r';
83           next[0] = '\n';
84           dst = next+1;
85         }           
86     }
87   while (next);
88     
89   GlobalUnlock (h);
90   
91   i = (SetClipboardData (CF_TEXT, h) != NULL);
92   
93   CloseClipboard ();
94   
95   return i ? Qt : Qnil;
96 }
97
98 /* Do protocol to assert ourself as a selection owner. Under mswindows
99 this is easy, we just set the clipboard.  */
100 static Lisp_Object
101 mswindows_own_selection (Lisp_Object selection_name, Lisp_Object selection_value)
102 {
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));
111
112   return Qnil;
113 }
114
115 DEFUN ("mswindows-get-clipboard", Fmswindows_get_clipboard, 0, 0, 0, /*
116 Return the contents of the mswindows clipboard.
117 */
118        ())
119 {
120   HANDLE h;
121   unsigned char *src, *dst, *next;
122   Lisp_Object ret = Qnil;
123
124   if (!OpenClipboard (NULL))
125     return Qnil;
126
127   if ((h = GetClipboardData (CF_TEXT)) != NULL &&
128       (src = (unsigned char *) GlobalLock (h)) != NULL)
129     {
130       int i;
131       int size, rawsize;
132       size = rawsize = strlen (src);
133
134       for (i=0; i<rawsize; i++)
135         if (src[i] == '\r' && src[i+1] == '\n')
136           size--;
137
138       /* Convert CRLFs to LFs */
139       ret = make_uninit_string (size);
140       dst = XSTRING_DATA (ret);
141       do
142         {
143           /* copy next line or remaining bytes excluding '\0' */
144           next = (char*) memccpy (dst, src, '\r', rawsize);
145           if (next)
146             {
147               /* copied one line ending with '\r' */
148               int copied = next - dst;
149               rawsize -= copied;
150               src += copied;
151               if (*src == '\n')
152                 dst += copied - 1;              /* overwrite '\r' */
153               else
154                 dst += copied;
155             }       
156         }
157       while (next);
158
159       GlobalUnlock (h);
160     }
161
162   CloseClipboard ();
163
164   return ret;
165 }
166
167 static Lisp_Object
168 mswindows_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type)
169 {
170   if (EQ (selection_symbol, QCLIPBOARD))
171     return Fmswindows_get_clipboard ();
172   else
173     return Qnil;
174 }
175
176 DEFUN ("mswindows-selection-exists-p", Fmswindows_selection_exists_p, 0, 0, 0, /*
177 Whether there is an MS-Windows selection.
178 */
179        ())
180 {
181   return IsClipboardFormatAvailable (CF_TEXT) ? Qt : Qnil;
182 }
183
184 DEFUN ("mswindows-delete-selection", Fmswindows_delete_selection, 0, 0, 0, /*
185 Remove the current MS-Windows selection from the clipboard.
186 */
187        ())
188 {
189   BOOL success = OpenClipboard (NULL);
190   if (success)
191     {
192       success = EmptyClipboard ();
193       /* Close it regardless of whether empty worked. */
194       if (!CloseClipboard ())
195         success = FALSE;
196     }
197
198   return success ? Qt : Qnil;
199 }
200
201 static void
202 mswindows_disown_selection (Lisp_Object selection, Lisp_Object timeval)
203 {
204   if (EQ (selection, QCLIPBOARD))
205     Fmswindows_delete_selection ();
206 }
207
208 \f
209 /************************************************************************/
210 /*                            initialization                            */
211 /************************************************************************/
212
213 void
214 console_type_create_select_mswindows (void)
215 {
216   CONSOLE_HAS_METHOD (mswindows, own_selection);
217   CONSOLE_HAS_METHOD (mswindows, disown_selection);
218   CONSOLE_HAS_METHOD (mswindows, get_foreign_selection);
219 }
220
221 void
222 syms_of_select_mswindows (void)
223 {
224   DEFSUBR (Fmswindows_set_clipboard);
225   DEFSUBR (Fmswindows_get_clipboard);
226   DEFSUBR (Fmswindows_selection_exists_p);
227   DEFSUBR (Fmswindows_delete_selection);
228 }
229
230 void
231 vars_of_select_mswindows (void)
232 {
233 }