Comment change.
[bpt/emacs.git] / src / w32select.c
CommitLineData
ee78dc32
GV
1/* Win32 Selection processing for emacs
2 Copyright (C) 1993, 1994 Free Software Foundation.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20/* Written by Kevin Gallo */
21
22#include <config.h>
23#include "lisp.h"
24#include "w32term.h" /* for all of the win32 includes */
25#include "dispextern.h" /* frame.h seems to want this */
26#include "frame.h" /* Need this to get the X window of selected_frame */
27#include "blockinput.h"
28
29#if 0
30DEFUN ("win32-open-clipboard", Fwin32_open_clipboard, Swin32_open_clipboard, 0, 1, 0,
31 "This opens the clipboard with the given frame pointer.")
32 (frame)
33 Lisp_Object frame;
34{
35 BOOL ok = FALSE;
36
37 if (!NILP (frame))
38 CHECK_LIVE_FRAME (frame, 0);
39
40 BLOCK_INPUT;
41
42 ok = OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL);
43
44 UNBLOCK_INPUT;
45
46 return (ok ? frame : Qnil);
47}
48
49DEFUN ("win32-empty-clipboard", Fwin32_empty_clipboard, Swin32_empty_clipboard, 0, 0, 0,
50 "This empties the clipboard and assigns ownership to the window which opened the clipboard.")
51 ()
52{
53 BOOL ok = FALSE;
54
55 BLOCK_INPUT;
56
57 ok = EmptyClipboard ();
58
59 UNBLOCK_INPUT;
60
61 return (ok ? Qt : Qnil);
62}
63
64DEFUN ("win32-close-clipboard", Fwin32_close_clipboard, Swin32_close_clipboard, 0, 0, 0,
65 "This closes the clipboard.")
66 ()
67{
68 BOOL ok = FALSE;
69
70 BLOCK_INPUT;
71
72 ok = CloseClipboard ();
73
74 UNBLOCK_INPUT;
75
76 return (ok ? Qt : Qnil);
77}
78
79#endif
80
81DEFUN ("win32-set-clipboard-data", Fwin32_set_clipboard_data, Swin32_set_clipboard_data, 1, 2, 0,
82 "This sets the clipboard data to the given text.")
83 (string, frame)
84 Lisp_Object string, frame;
85{
86 BOOL ok = TRUE;
87 HANDLE htext;
88
89 CHECK_STRING (string, 0);
90
91 if (!NILP (frame))
92 CHECK_LIVE_FRAME (frame, 0);
93
94 BLOCK_INPUT;
95
96 /* Allocate twice the amount so we can convert lf to cr-lf */
97
98 if ((htext = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, (2 * XSTRING (string)->size) + 1)) == NULL)
99 goto error;
100
101 {
102 unsigned char *lptext;
103
104 if ((lptext = (unsigned char *)GlobalLock (htext)) == NULL)
105 goto error;
106
107 {
108 int i = XSTRING (string)->size;
109 int newsize = XSTRING (string)->size;
110 register char *p1 = XSTRING (string)->data;
111 register char *p2 = lptext;
112
113 while (i--)
114 {
115 if (*p1 == '\n')
116 {
117 newsize++;
118 *p2++ = '\r';
119 }
120
121 *p2++ = *p1++;
122 }
123
124 *p2 = 0;
125 }
126
127 GlobalUnlock (htext);
128 }
129
130 if (!OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL))
131 goto error;
132
133 ok = EmptyClipboard () && SetClipboardData (CF_TEXT, htext);
134
135 CloseClipboard ();
136
137 if (ok) goto done;
138
139 error:
140
141 ok = FALSE;
142 if (htext) GlobalFree (htext);
143
144 done:
145 UNBLOCK_INPUT;
146
147 return (ok ? string : Qnil);
148}
149
150DEFUN ("win32-get-clipboard-data", Fwin32_get_clipboard_data, Swin32_get_clipboard_data, 0, 1, 0,
151 "This gets the clipboard data in text format.")
152 (frame)
153 Lisp_Object frame;
154{
155 HANDLE htext;
156 Lisp_Object ret = Qnil;
157
158 if (!NILP (frame))
159 CHECK_LIVE_FRAME (frame, 0);
160
161 BLOCK_INPUT;
162
163 if (!OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL))
164 goto done;
165
166 if ((htext = GetClipboardData (CF_TEXT)) == NULL)
167 goto closeclip;
168
169
170 {
171 unsigned char *lptext;
172 int nbytes;
173
174 if ((lptext = (unsigned char *)GlobalLock (htext)) == NULL)
175 goto closeclip;
176
177 nbytes = strlen (lptext);
178
179 {
180 char *buf = (char *) xmalloc (nbytes);
181 register char *p1 = lptext;
182 register char *p2 = buf;
183 int i = nbytes;
184
185 if (buf == NULL) goto closeclip;
186
187 while (i--)
188 {
189 if (p1[0] == '\r' && i && p1[1] == '\n')
190 {
191 p1++;
192 i--;
193 nbytes--;
194 }
195
196 *p2++ = *p1++;
197 }
198
199 ret = make_string (buf, nbytes);
200
201 xfree (buf);
202 }
203
204 GlobalUnlock (htext);
205 }
206
207 closeclip:
208 CloseClipboard ();
209
210 done:
211 UNBLOCK_INPUT;
212
213 return (ret);
214}
215
216void
217syms_of_win32select ()
218{
219#if 0
220 defsubr (&Swin32_open_clipboard);
221 defsubr (&Swin32_empty_clipboard);
222 defsubr (&Swin32_close_clipboard);
223#endif
224 defsubr (&Swin32_set_clipboard_data);
225 defsubr (&Swin32_get_clipboard_data);
226}