m_copypaste.c revision 1.1 1 /*-
2 * Copyright (c) 1996
3 * Rob Zimmermann. All rights reserved.
4 * Copyright (c) 1996
5 * Keith Bostic. All rights reserved.
6 *
7 * See the LICENSE file for redistribution information.
8 */
9
10 #include "config.h"
11
12 #ifndef lint
13 static const char sccsid[] = "Id: m_copypaste.c,v 8.10 2003/11/05 17:09:59 skimo Exp (Berkeley) Date: 2003/11/05 17:09:59 ";
14 #endif /* not lint */
15
16 /* ICCCM Cut and paste Utilities: */
17
18 #include <sys/types.h>
19 #include <sys/queue.h>
20
21 #include <X11/X.h>
22 #include <X11/Intrinsic.h>
23 #include <X11/Xatom.h>
24
25 #include <bitstring.h>
26 #include <stdio.h>
27
28 #undef LOCK_SUCCESS
29 #include "../common/common.h"
30 #include "../ipc/ip.h"
31 #include "m_motif.h"
32
33 typedef int (*PFI)();
34
35 static PFI icccm_paste,
36 icccm_copy,
37 icccm_clear,
38 icccm_error;
39
40 /*
41 * InitCopyPaste --
42 *
43 * PUBLIC: void __vi_InitCopyPaste
44 * PUBLIC: __P((int (*)(), int (*)(), int (*)(), int (*)()));
45 */
46 void
47 __vi_InitCopyPaste(PFI f_copy, PFI f_paste, PFI f_clear, PFI f_error)
48 {
49 icccm_paste = f_paste;
50 icccm_clear = f_clear;
51 icccm_copy = f_copy;
52 icccm_error = f_error;
53 }
54
55
56 #if defined(__STDC__)
57 static void peekProc( Widget widget,
58 void *data,
59 Atom *selection,
60 Atom *type,
61 void *value,
62 unsigned long *length,
63 int *format
64 )
65 #else
66 static void peekProc( widget, data, selection, type, value, length, format )
67 Widget widget;
68 void *data;
69 Atom *selection, *type;
70 void *value;
71 unsigned long *length;
72 int *format;
73 #endif
74 {
75 if ( *type == 0 )
76 (*icccm_error)( stderr, "Nothing in the primary selection buffer");
77 else if ( *type != XA_STRING )
78 (*icccm_error)( stderr, "Unknown type return from selection");
79 else
80 XtFree( value );
81 }
82
83
84 #if 0
85 #if defined(__STDC__)
86 void _vi_AcquireClipboard( Widget wid )
87 #else
88 void _vi_AcquireClipboard( wid )
89 Widget wid;
90 #endif
91 {
92 XtGetSelectionValue( wid,
93 XA_PRIMARY,
94 XA_STRING,
95 (XtSelectionCallbackProc) peekProc,
96 NULL,
97 XtLastTimestampProcessed( XtDisplay(wid) )
98 );
99 }
100 #endif
101
102
103 #if defined(__STDC__)
104 static void loseProc( Widget widget )
105 #else
106 static void loseProc( widget )
107 Widget widget;
108 #endif
109 {
110 /* we have lost ownership of the selection. clear it */
111 (*icccm_clear)( widget );
112
113 /* also participate in the protocols */
114 XtDisownSelection( widget,
115 XA_PRIMARY,
116 XtLastTimestampProcessed( XtDisplay(widget) )
117 );
118 }
119
120
121 #if defined(__STDC__)
122 static int convertProc( Widget widget,
123 Atom *selection,
124 Atom *target,
125 Atom *type,
126 void **value,
127 int *length,
128 int *format
129 )
130 #else
131 static int convertProc( widget, selection, target, type, value, length, format )
132 Widget widget;
133 Atom *selection, *target, *type;
134 void **value;
135 int *length;
136 int *format;
137 #endif
138 {
139 String buffer;
140 int len;
141
142 /* someone wants a copy of the selection. is there one? */
143 (*icccm_copy)( &buffer, &len );
144 if ( len == 0 ) return False;
145
146 /* do they want the string? */
147 if ( *target == XA_STRING ) {
148 *length = len;
149 *value = (void *) XtMalloc( len );
150 *type = XA_STRING;
151 *format = 8;
152 memcpy( (char *) *value, buffer, *length );
153 return True;
154 }
155
156 /* do they want the length? */
157 if ( *target == XInternAtom( XtDisplay(widget), "LENGTH", FALSE) ) {
158 *length = 1;
159 *value = (void *) XtMalloc( sizeof(int) );
160 *type = *target;
161 *format = 32;
162 * ((int *) *value) = len;
163 return True;
164 }
165
166 /* we lose */
167 return False;
168 }
169
170 /*
171 * __vi_AcquirePrimary --
172 *
173 * PUBLIC: void __vi_AcquirePrimary __P((Widget));
174 */
175 void
176 __vi_AcquirePrimary(Widget widget)
177 {
178 /* assert we own the primary selection */
179 XtOwnSelection( widget,
180 XA_PRIMARY,
181 XtLastTimestampProcessed( XtDisplay(widget) ),
182 (XtConvertSelectionProc) convertProc,
183 (XtLoseSelectionProc) loseProc,
184 NULL
185 );
186
187 #if defined(OPENLOOK)
188 /* assert we also own the clipboard */
189 XtOwnSelection( widget,
190 XA_CLIPBOARD( XtDisplay(widget) ),
191 XtLastTimestampProcessed( XtDisplay(widget) ),
192 convertProc,
193 loseProc,
194 NULL
195 );
196 #endif
197 }
198
199
200 #if defined(__STDC__)
201 static void gotProc( Widget widget,
202 void *data,
203 Atom *selection,
204 Atom *type,
205 void *value,
206 unsigned long *length,
207 int *format
208 )
209 #else
210 static void gotProc( widget, data, selection, type, value, length, format )
211 Widget widget;
212 void *data;
213 Atom *selection, *type;
214 void *value;
215 unsigned long *length;
216 int *format;
217 #endif
218 {
219 if ( *type == 0 )
220 (*icccm_error)( stderr, "Nothing in the primary selection buffer");
221 else if ( *type != XA_STRING )
222 (*icccm_error)( stderr, "Unknown type return from selection");
223 else {
224 (*icccm_paste)( widget, value, *length );
225 XtFree( value );
226 }
227 }
228
229 /*
230 * __vi_PasteFromClipboard --
231 *
232 * PUBLIC: void __vi_PasteFromClipboard __P((Widget));
233 */
234 void
235 __vi_PasteFromClipboard(Widget widget)
236 {
237 XtGetSelectionValue( widget,
238 XA_PRIMARY,
239 XA_STRING,
240 (XtSelectionCallbackProc) gotProc,
241 NULL,
242 XtLastTimestampProcessed( XtDisplay(widget) )
243 );
244 }
245