Commit | Line | Data |
---|---|---|
ee78dc32 GV |
1 | /* Win32 Selection processing for emacs |
2 | Copyright (C) 1993, 1994 Free Software Foundation. | |
3 | ||
3b7ad313 EN |
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, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
ee78dc32 GV |
20 | |
21 | /* Written by Kevin Gallo */ | |
22 | ||
23 | #include <config.h> | |
24 | #include "lisp.h" | |
25 | #include "w32term.h" /* for all of the win32 includes */ | |
26 | #include "dispextern.h" /* frame.h seems to want this */ | |
27 | #include "frame.h" /* Need this to get the X window of selected_frame */ | |
28 | #include "blockinput.h" | |
29 | ||
30 | #if 0 | |
31 | DEFUN ("win32-open-clipboard", Fwin32_open_clipboard, Swin32_open_clipboard, 0, 1, 0, | |
32 | "This opens the clipboard with the given frame pointer.") | |
33 | (frame) | |
34 | Lisp_Object frame; | |
35 | { | |
36 | BOOL ok = FALSE; | |
37 | ||
38 | if (!NILP (frame)) | |
39 | CHECK_LIVE_FRAME (frame, 0); | |
40 | ||
41 | BLOCK_INPUT; | |
42 | ||
43 | ok = OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL); | |
44 | ||
45 | UNBLOCK_INPUT; | |
46 | ||
47 | return (ok ? frame : Qnil); | |
48 | } | |
49 | ||
50 | DEFUN ("win32-empty-clipboard", Fwin32_empty_clipboard, Swin32_empty_clipboard, 0, 0, 0, | |
51 | "This empties the clipboard and assigns ownership to the window which opened the clipboard.") | |
52 | () | |
53 | { | |
54 | BOOL ok = FALSE; | |
55 | ||
56 | BLOCK_INPUT; | |
57 | ||
58 | ok = EmptyClipboard (); | |
59 | ||
60 | UNBLOCK_INPUT; | |
61 | ||
62 | return (ok ? Qt : Qnil); | |
63 | } | |
64 | ||
65 | DEFUN ("win32-close-clipboard", Fwin32_close_clipboard, Swin32_close_clipboard, 0, 0, 0, | |
66 | "This closes the clipboard.") | |
67 | () | |
68 | { | |
69 | BOOL ok = FALSE; | |
70 | ||
71 | BLOCK_INPUT; | |
72 | ||
73 | ok = CloseClipboard (); | |
74 | ||
75 | UNBLOCK_INPUT; | |
76 | ||
77 | return (ok ? Qt : Qnil); | |
78 | } | |
79 | ||
80 | #endif | |
81 | ||
82 | DEFUN ("win32-set-clipboard-data", Fwin32_set_clipboard_data, Swin32_set_clipboard_data, 1, 2, 0, | |
83 | "This sets the clipboard data to the given text.") | |
84 | (string, frame) | |
85 | Lisp_Object string, frame; | |
86 | { | |
87 | BOOL ok = TRUE; | |
88 | HANDLE htext; | |
89 | ||
90 | CHECK_STRING (string, 0); | |
91 | ||
92 | if (!NILP (frame)) | |
93 | CHECK_LIVE_FRAME (frame, 0); | |
94 | ||
95 | BLOCK_INPUT; | |
96 | ||
97 | /* Allocate twice the amount so we can convert lf to cr-lf */ | |
98 | ||
99 | if ((htext = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, (2 * XSTRING (string)->size) + 1)) == NULL) | |
100 | goto error; | |
101 | ||
102 | { | |
103 | unsigned char *lptext; | |
104 | ||
105 | if ((lptext = (unsigned char *)GlobalLock (htext)) == NULL) | |
106 | goto error; | |
107 | ||
108 | { | |
109 | int i = XSTRING (string)->size; | |
110 | int newsize = XSTRING (string)->size; | |
111 | register char *p1 = XSTRING (string)->data; | |
112 | register char *p2 = lptext; | |
113 | ||
114 | while (i--) | |
115 | { | |
116 | if (*p1 == '\n') | |
117 | { | |
118 | newsize++; | |
119 | *p2++ = '\r'; | |
120 | } | |
121 | ||
122 | *p2++ = *p1++; | |
123 | } | |
124 | ||
125 | *p2 = 0; | |
126 | } | |
127 | ||
128 | GlobalUnlock (htext); | |
129 | } | |
130 | ||
131 | if (!OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL)) | |
132 | goto error; | |
133 | ||
134 | ok = EmptyClipboard () && SetClipboardData (CF_TEXT, htext); | |
135 | ||
136 | CloseClipboard (); | |
137 | ||
138 | if (ok) goto done; | |
139 | ||
140 | error: | |
141 | ||
142 | ok = FALSE; | |
143 | if (htext) GlobalFree (htext); | |
144 | ||
145 | done: | |
146 | UNBLOCK_INPUT; | |
147 | ||
148 | return (ok ? string : Qnil); | |
149 | } | |
150 | ||
151 | DEFUN ("win32-get-clipboard-data", Fwin32_get_clipboard_data, Swin32_get_clipboard_data, 0, 1, 0, | |
152 | "This gets the clipboard data in text format.") | |
153 | (frame) | |
154 | Lisp_Object frame; | |
155 | { | |
156 | HANDLE htext; | |
157 | Lisp_Object ret = Qnil; | |
158 | ||
159 | if (!NILP (frame)) | |
160 | CHECK_LIVE_FRAME (frame, 0); | |
161 | ||
162 | BLOCK_INPUT; | |
163 | ||
164 | if (!OpenClipboard ((!NILP (frame) && FRAME_WIN32_P (XFRAME (frame))) ? FRAME_WIN32_WINDOW (XFRAME (frame)) : NULL)) | |
165 | goto done; | |
166 | ||
167 | if ((htext = GetClipboardData (CF_TEXT)) == NULL) | |
168 | goto closeclip; | |
169 | ||
170 | ||
171 | { | |
172 | unsigned char *lptext; | |
173 | int nbytes; | |
174 | ||
175 | if ((lptext = (unsigned char *)GlobalLock (htext)) == NULL) | |
176 | goto closeclip; | |
177 | ||
178 | nbytes = strlen (lptext); | |
179 | ||
180 | { | |
181 | char *buf = (char *) xmalloc (nbytes); | |
182 | register char *p1 = lptext; | |
183 | register char *p2 = buf; | |
184 | int i = nbytes; | |
185 | ||
186 | if (buf == NULL) goto closeclip; | |
187 | ||
188 | while (i--) | |
189 | { | |
190 | if (p1[0] == '\r' && i && p1[1] == '\n') | |
191 | { | |
192 | p1++; | |
193 | i--; | |
194 | nbytes--; | |
195 | } | |
196 | ||
197 | *p2++ = *p1++; | |
198 | } | |
199 | ||
200 | ret = make_string (buf, nbytes); | |
201 | ||
202 | xfree (buf); | |
203 | } | |
204 | ||
205 | GlobalUnlock (htext); | |
206 | } | |
207 | ||
208 | closeclip: | |
209 | CloseClipboard (); | |
210 | ||
211 | done: | |
212 | UNBLOCK_INPUT; | |
213 | ||
214 | return (ret); | |
215 | } | |
216 | ||
217 | void | |
218 | syms_of_win32select () | |
219 | { | |
220 | #if 0 | |
221 | defsubr (&Swin32_open_clipboard); | |
222 | defsubr (&Swin32_empty_clipboard); | |
223 | defsubr (&Swin32_close_clipboard); | |
224 | #endif | |
225 | defsubr (&Swin32_set_clipboard_data); | |
226 | defsubr (&Swin32_get_clipboard_data); | |
227 | } |