1 |
774 |
jeremybenn |
/* gtkclipboard.c
|
2 |
|
|
Copyright (C) 1998, 1999, 2005, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath 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 Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
#include "jcl.h"
|
40 |
|
|
#include "gtkpeer.h"
|
41 |
|
|
#include "gnu_java_awt_peer_gtk_GtkClipboard.h"
|
42 |
|
|
|
43 |
|
|
#define OBJECT_TARGET 1
|
44 |
|
|
#define TEXT_TARGET 2
|
45 |
|
|
#define IMAGE_TARGET 3
|
46 |
|
|
#define URI_TARGET 4
|
47 |
|
|
|
48 |
|
|
/* The clipboard and selection plus corresponding GtkClipboard objects. */
|
49 |
|
|
GtkClipboard *cp_gtk_clipboard;
|
50 |
|
|
GtkClipboard *cp_gtk_selection;
|
51 |
|
|
|
52 |
|
|
jobject cp_gtk_clipboard_instance;
|
53 |
|
|
jobject cp_gtk_selection_instance;
|
54 |
|
|
|
55 |
|
|
/* Standard (string targets) shared with GtkSelection. */
|
56 |
|
|
jstring cp_gtk_stringTarget;
|
57 |
|
|
jstring cp_gtk_imageTarget;
|
58 |
|
|
jstring cp_gtk_filesTarget;
|
59 |
|
|
|
60 |
|
|
static jclass gtk_clipboard_class;
|
61 |
|
|
|
62 |
|
|
static jmethodID setSystemContentsID;
|
63 |
|
|
static jmethodID provideContentID;
|
64 |
|
|
static jmethodID provideTextID;
|
65 |
|
|
static jmethodID provideImageID;
|
66 |
|
|
static jmethodID provideURIsID;
|
67 |
|
|
|
68 |
|
|
/* Called when clipboard owner changes. Used to update available targets. */
|
69 |
|
|
#if GTK_MINOR_VERSION > 4
|
70 |
|
|
static void
|
71 |
|
|
clipboard_owner_change_cb (GtkClipboard *clipboard,
|
72 |
|
|
GdkEvent *event __attribute__((unused)),
|
73 |
|
|
gpointer user_data __attribute__((unused)))
|
74 |
|
|
{
|
75 |
|
|
JNIEnv *env = cp_gtk_gdk_env ();
|
76 |
|
|
if (clipboard == cp_gtk_clipboard)
|
77 |
|
|
(*env)->CallVoidMethod (env, cp_gtk_clipboard_instance,
|
78 |
|
|
setSystemContentsID, JNI_FALSE);
|
79 |
|
|
else
|
80 |
|
|
(*env)->CallVoidMethod (env, cp_gtk_selection_instance,
|
81 |
|
|
setSystemContentsID, JNI_FALSE);
|
82 |
|
|
|
83 |
|
|
}
|
84 |
|
|
#endif
|
85 |
|
|
|
86 |
|
|
JNIEXPORT jboolean JNICALL
|
87 |
|
|
Java_gnu_java_awt_peer_gtk_GtkClipboard_initNativeState (JNIEnv *env,
|
88 |
|
|
jclass clz,
|
89 |
|
|
jobject gtkclipboard,
|
90 |
|
|
jobject gtkselection,
|
91 |
|
|
jstring string,
|
92 |
|
|
jstring image,
|
93 |
|
|
jstring files)
|
94 |
|
|
{
|
95 |
|
|
GdkDisplay* display;
|
96 |
|
|
jboolean can_cache;
|
97 |
|
|
|
98 |
|
|
gtk_clipboard_class = clz;
|
99 |
|
|
setSystemContentsID = (*env)->GetMethodID (env, gtk_clipboard_class,
|
100 |
|
|
"setSystemContents",
|
101 |
|
|
"(Z)V");
|
102 |
|
|
if (setSystemContentsID == NULL)
|
103 |
|
|
return JNI_FALSE;
|
104 |
|
|
|
105 |
|
|
provideContentID = (*env)->GetMethodID (env, gtk_clipboard_class,
|
106 |
|
|
"provideContent",
|
107 |
|
|
"(Ljava/lang/String;)[B");
|
108 |
|
|
if (provideContentID == NULL)
|
109 |
|
|
return JNI_FALSE;
|
110 |
|
|
|
111 |
|
|
provideTextID = (*env)->GetMethodID (env, gtk_clipboard_class,
|
112 |
|
|
"provideText",
|
113 |
|
|
"()Ljava/lang/String;");
|
114 |
|
|
if (provideTextID == NULL)
|
115 |
|
|
return JNI_FALSE;
|
116 |
|
|
|
117 |
|
|
provideImageID = (*env)->GetMethodID (env, gtk_clipboard_class,
|
118 |
|
|
"provideImage",
|
119 |
|
|
"()Lgnu/java/awt/peer/gtk/GtkImage;");
|
120 |
|
|
if (provideImageID == NULL)
|
121 |
|
|
return JNI_FALSE;
|
122 |
|
|
|
123 |
|
|
provideURIsID = (*env)->GetMethodID (env, gtk_clipboard_class,
|
124 |
|
|
"provideURIs",
|
125 |
|
|
"()[Ljava/lang/String;");
|
126 |
|
|
if (provideURIsID == NULL)
|
127 |
|
|
return JNI_FALSE;
|
128 |
|
|
|
129 |
|
|
cp_gtk_clipboard_instance = (*env)->NewGlobalRef(env, gtkclipboard);
|
130 |
|
|
cp_gtk_selection_instance = (*env)->NewGlobalRef(env, gtkselection);
|
131 |
|
|
|
132 |
|
|
cp_gtk_stringTarget = (*env)->NewGlobalRef(env, string);
|
133 |
|
|
cp_gtk_imageTarget = (*env)->NewGlobalRef(env, image);
|
134 |
|
|
cp_gtk_filesTarget = (*env)->NewGlobalRef(env, files);
|
135 |
|
|
|
136 |
|
|
gdk_threads_enter ();
|
137 |
|
|
cp_gtk_clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
|
138 |
|
|
cp_gtk_selection = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
|
139 |
|
|
|
140 |
|
|
display = gtk_clipboard_get_display (cp_gtk_clipboard);
|
141 |
|
|
/* Check for support for clipboard owner changes. */
|
142 |
|
|
#if GTK_MINOR_VERSION > 4
|
143 |
|
|
if (gdk_display_supports_selection_notification (display))
|
144 |
|
|
{
|
145 |
|
|
g_signal_connect (cp_gtk_clipboard, "owner-change",
|
146 |
|
|
G_CALLBACK (clipboard_owner_change_cb), NULL);
|
147 |
|
|
g_signal_connect (cp_gtk_selection, "owner-change",
|
148 |
|
|
G_CALLBACK (clipboard_owner_change_cb), NULL);
|
149 |
|
|
gdk_display_request_selection_notification (display,
|
150 |
|
|
GDK_SELECTION_CLIPBOARD);
|
151 |
|
|
gdk_display_request_selection_notification (display,
|
152 |
|
|
GDK_SELECTION_PRIMARY);
|
153 |
|
|
can_cache = JNI_TRUE;
|
154 |
|
|
}
|
155 |
|
|
else
|
156 |
|
|
#endif
|
157 |
|
|
can_cache = JNI_FALSE;
|
158 |
|
|
|
159 |
|
|
gdk_threads_leave ();
|
160 |
|
|
|
161 |
|
|
return can_cache;
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
static void
|
165 |
|
|
clipboard_get_func (GtkClipboard *clipboard,
|
166 |
|
|
GtkSelectionData *selection,
|
167 |
|
|
guint info,
|
168 |
|
|
gpointer user_data __attribute__((unused)))
|
169 |
|
|
{
|
170 |
|
|
jobject gtk_clipboard_instance;
|
171 |
|
|
JNIEnv *env = cp_gtk_gdk_env ();
|
172 |
|
|
|
173 |
|
|
if (clipboard == cp_gtk_clipboard)
|
174 |
|
|
gtk_clipboard_instance = cp_gtk_clipboard_instance;
|
175 |
|
|
else
|
176 |
|
|
gtk_clipboard_instance = cp_gtk_selection_instance;
|
177 |
|
|
|
178 |
|
|
if (info == OBJECT_TARGET)
|
179 |
|
|
{
|
180 |
|
|
const gchar *target_name;
|
181 |
|
|
jstring target_string;
|
182 |
|
|
jbyteArray bytes;
|
183 |
|
|
jint len;
|
184 |
|
|
jbyte *barray;
|
185 |
|
|
|
186 |
|
|
target_name = gdk_atom_name (selection->target);
|
187 |
|
|
if (target_name == NULL)
|
188 |
|
|
return;
|
189 |
|
|
target_string = (*env)->NewStringUTF (env, target_name);
|
190 |
|
|
if (target_string == NULL)
|
191 |
|
|
return;
|
192 |
|
|
bytes = (*env)->CallObjectMethod(env,
|
193 |
|
|
gtk_clipboard_instance,
|
194 |
|
|
provideContentID,
|
195 |
|
|
target_string);
|
196 |
|
|
(*env)->DeleteLocalRef(env, target_string);
|
197 |
|
|
if (bytes == NULL)
|
198 |
|
|
return;
|
199 |
|
|
len = (*env)->GetArrayLength(env, bytes);
|
200 |
|
|
if (len <= 0)
|
201 |
|
|
return;
|
202 |
|
|
barray = (*env)->GetByteArrayElements(env, bytes, NULL);
|
203 |
|
|
if (barray == NULL)
|
204 |
|
|
return;
|
205 |
|
|
gtk_selection_data_set (selection, selection->target, 8,
|
206 |
|
|
(guchar *) barray, len);
|
207 |
|
|
|
208 |
|
|
(*env)->ReleaseByteArrayElements(env, bytes, barray, 0);
|
209 |
|
|
|
210 |
|
|
}
|
211 |
|
|
else if (info == TEXT_TARGET)
|
212 |
|
|
{
|
213 |
|
|
jstring string;
|
214 |
|
|
const gchar *text;
|
215 |
|
|
int len;
|
216 |
|
|
string = (*env)->CallObjectMethod(env,
|
217 |
|
|
gtk_clipboard_instance,
|
218 |
|
|
provideTextID);
|
219 |
|
|
if (string == NULL)
|
220 |
|
|
return;
|
221 |
|
|
len = (*env)->GetStringUTFLength (env, string);
|
222 |
|
|
if (len == -1)
|
223 |
|
|
return;
|
224 |
|
|
text = (*env)->GetStringUTFChars (env, string, NULL);
|
225 |
|
|
if (text == NULL)
|
226 |
|
|
return;
|
227 |
|
|
|
228 |
|
|
gtk_selection_data_set_text (selection, text, len);
|
229 |
|
|
(*env)->ReleaseStringUTFChars (env, string, text);
|
230 |
|
|
}
|
231 |
|
|
/* Images and URIs/Files support only available with gtk+2.6 or higher. */
|
232 |
|
|
#if GTK_MINOR_VERSION > 4
|
233 |
|
|
else if (info == IMAGE_TARGET)
|
234 |
|
|
{
|
235 |
|
|
jobject gtkimage;
|
236 |
|
|
GdkPixbuf *pixbuf = NULL;
|
237 |
|
|
|
238 |
|
|
gtkimage = (*env)->CallObjectMethod(env,
|
239 |
|
|
gtk_clipboard_instance,
|
240 |
|
|
provideImageID);
|
241 |
|
|
if (gtkimage == NULL)
|
242 |
|
|
return;
|
243 |
|
|
|
244 |
|
|
pixbuf = cp_gtk_image_get_pixbuf (env, gtkimage);
|
245 |
|
|
if (pixbuf != NULL)
|
246 |
|
|
gtk_selection_data_set_pixbuf (selection, pixbuf);
|
247 |
|
|
}
|
248 |
|
|
else if (info == URI_TARGET)
|
249 |
|
|
{
|
250 |
|
|
jobjectArray uris;
|
251 |
|
|
jint count;
|
252 |
|
|
int i;
|
253 |
|
|
gchar **list;
|
254 |
|
|
|
255 |
|
|
uris = (*env)->CallObjectMethod(env,
|
256 |
|
|
gtk_clipboard_instance,
|
257 |
|
|
provideURIsID);
|
258 |
|
|
if (uris == NULL)
|
259 |
|
|
return;
|
260 |
|
|
count = (*env)->GetArrayLength (env, uris);
|
261 |
|
|
if (count <= 0)
|
262 |
|
|
return;
|
263 |
|
|
|
264 |
|
|
list = (gchar **) JCL_malloc (env, (count + 1) * sizeof (gchar *));
|
265 |
|
|
for (i = 0; i < count; i++)
|
266 |
|
|
{
|
267 |
|
|
const char *text;
|
268 |
|
|
jstring uri;
|
269 |
|
|
|
270 |
|
|
/* Mark NULL in so case of some error we can find the end. */
|
271 |
|
|
list[i] = NULL;
|
272 |
|
|
uri = (*env)->GetObjectArrayElement (env, uris, i);
|
273 |
|
|
if (uri == NULL)
|
274 |
|
|
break;
|
275 |
|
|
text = (*env)->GetStringUTFChars (env, uri, NULL);
|
276 |
|
|
if (text == NULL)
|
277 |
|
|
{
|
278 |
|
|
(*env)->DeleteLocalRef(env, uri);
|
279 |
|
|
break;
|
280 |
|
|
}
|
281 |
|
|
list[i] = strdup (text);
|
282 |
|
|
(*env)->ReleaseStringUTFChars (env, uri, text);
|
283 |
|
|
(*env)->DeleteLocalRef(env, uri);
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
if (i == count)
|
287 |
|
|
{
|
288 |
|
|
list[count] = NULL;
|
289 |
|
|
gtk_selection_data_set_uris (selection, list);
|
290 |
|
|
}
|
291 |
|
|
|
292 |
|
|
for (i = 0; list[i] != NULL; i++)
|
293 |
|
|
free (list[i]);
|
294 |
|
|
JCL_free (env, list);
|
295 |
|
|
}
|
296 |
|
|
#endif
|
297 |
|
|
}
|
298 |
|
|
|
299 |
|
|
static void
|
300 |
|
|
clipboard_clear_func (GtkClipboard *clipboard,
|
301 |
|
|
gpointer user_data __attribute__((unused)))
|
302 |
|
|
{
|
303 |
|
|
JNIEnv *env = cp_gtk_gdk_env();
|
304 |
|
|
if (clipboard == cp_gtk_clipboard)
|
305 |
|
|
(*env)->CallVoidMethod (env, cp_gtk_clipboard_instance,
|
306 |
|
|
setSystemContentsID, JNI_TRUE);
|
307 |
|
|
else
|
308 |
|
|
(*env)->CallVoidMethod (env, cp_gtk_selection_instance,
|
309 |
|
|
setSystemContentsID, JNI_TRUE);
|
310 |
|
|
|
311 |
|
|
}
|
312 |
|
|
|
313 |
|
|
JNIEXPORT void JNICALL
|
314 |
|
|
Java_gnu_java_awt_peer_gtk_GtkClipboard_advertiseContent
|
315 |
|
|
(JNIEnv *env,
|
316 |
|
|
jobject instance,
|
317 |
|
|
jobjectArray mime_array,
|
318 |
|
|
#if GTK_MINOR_VERSION > 4
|
319 |
|
|
jboolean add_text, jboolean add_images, jboolean add_uris)
|
320 |
|
|
#else
|
321 |
|
|
jboolean add_text __attribute__((unused)),
|
322 |
|
|
jboolean add_images __attribute__((unused)),
|
323 |
|
|
jboolean add_uris __attribute__((unused)))
|
324 |
|
|
#endif
|
325 |
|
|
{
|
326 |
|
|
GtkTargetList *target_list;
|
327 |
|
|
GList *list;
|
328 |
|
|
GtkTargetEntry *targets;
|
329 |
|
|
gint n, i;
|
330 |
|
|
|
331 |
|
|
gdk_threads_enter ();
|
332 |
|
|
target_list = gtk_target_list_new (NULL, 0);
|
333 |
|
|
|
334 |
|
|
if (mime_array != NULL)
|
335 |
|
|
{
|
336 |
|
|
n = (*env)->GetArrayLength (env, mime_array);
|
337 |
|
|
for (i = 0; i < n; i++)
|
338 |
|
|
{
|
339 |
|
|
const char *text;
|
340 |
|
|
jstring target;
|
341 |
|
|
GdkAtom atom;
|
342 |
|
|
|
343 |
|
|
target = (*env)->GetObjectArrayElement (env, mime_array, i);
|
344 |
|
|
if (target == NULL)
|
345 |
|
|
break;
|
346 |
|
|
text = (*env)->GetStringUTFChars (env, target, NULL);
|
347 |
|
|
if (text == NULL)
|
348 |
|
|
break;
|
349 |
|
|
|
350 |
|
|
atom = gdk_atom_intern (text, FALSE);
|
351 |
|
|
gtk_target_list_add (target_list, atom, 0, OBJECT_TARGET);
|
352 |
|
|
|
353 |
|
|
(*env)->ReleaseStringUTFChars (env, target, text);
|
354 |
|
|
}
|
355 |
|
|
}
|
356 |
|
|
|
357 |
|
|
/* Add extra targets that gtk+ can provide/translate for us. */
|
358 |
|
|
#if GTK_MINOR_VERSION > 4
|
359 |
|
|
if (add_text)
|
360 |
|
|
gtk_target_list_add_text_targets (target_list, TEXT_TARGET);
|
361 |
|
|
if (add_images)
|
362 |
|
|
gtk_target_list_add_image_targets (target_list, IMAGE_TARGET, TRUE);
|
363 |
|
|
if (add_uris)
|
364 |
|
|
gtk_target_list_add_uri_targets (target_list, URI_TARGET);
|
365 |
|
|
#else
|
366 |
|
|
if (add_text)
|
367 |
|
|
gtk_target_list_add (target_list,
|
368 |
|
|
gdk_atom_intern ("STRING", FALSE),
|
369 |
|
|
0, TEXT_TARGET);
|
370 |
|
|
#endif
|
371 |
|
|
|
372 |
|
|
|
373 |
|
|
/* Turn list into a target table. */
|
374 |
|
|
n = g_list_length (target_list->list);
|
375 |
|
|
if (n > 0)
|
376 |
|
|
{
|
377 |
|
|
targets = g_new (GtkTargetEntry, n);
|
378 |
|
|
for (list = target_list->list, i = 0;
|
379 |
|
|
list != NULL;
|
380 |
|
|
list = list->next, i++)
|
381 |
|
|
{
|
382 |
|
|
GtkTargetPair *pair = (GtkTargetPair *) list->data;
|
383 |
|
|
targets[i].target = gdk_atom_name (pair->target);
|
384 |
|
|
targets[i].flags = pair->flags;
|
385 |
|
|
targets[i].info = pair->info;
|
386 |
|
|
}
|
387 |
|
|
|
388 |
|
|
/* Set the targets plus callback functions and ask for the clipboard
|
389 |
|
|
to be stored when the application exists if supported. */
|
390 |
|
|
if ((*env)->IsSameObject(env, instance, cp_gtk_clipboard_instance))
|
391 |
|
|
{
|
392 |
|
|
if (gtk_clipboard_set_with_data (cp_gtk_clipboard, targets, n,
|
393 |
|
|
clipboard_get_func,
|
394 |
|
|
clipboard_clear_func,
|
395 |
|
|
NULL))
|
396 |
|
|
{
|
397 |
|
|
#if GTK_MINOR_VERSION > 4
|
398 |
|
|
gtk_clipboard_set_can_store (cp_gtk_clipboard, NULL, 0);
|
399 |
|
|
#endif
|
400 |
|
|
}
|
401 |
|
|
}
|
402 |
|
|
else
|
403 |
|
|
{
|
404 |
|
|
if (gtk_clipboard_set_with_data (cp_gtk_selection, targets, n,
|
405 |
|
|
clipboard_get_func,
|
406 |
|
|
clipboard_clear_func,
|
407 |
|
|
NULL))
|
408 |
|
|
{
|
409 |
|
|
#if GTK_MINOR_VERSION > 4
|
410 |
|
|
gtk_clipboard_set_can_store (cp_gtk_selection, NULL, 0);
|
411 |
|
|
#endif
|
412 |
|
|
}
|
413 |
|
|
}
|
414 |
|
|
|
415 |
|
|
for (i = 0; i < n; i++)
|
416 |
|
|
g_free (targets[i].target);
|
417 |
|
|
g_free (targets);
|
418 |
|
|
}
|
419 |
|
|
|
420 |
|
|
gtk_target_list_unref (target_list);
|
421 |
|
|
gdk_threads_leave ();
|
422 |
|
|
}
|