1 /* gtkselection.c -- Native C functions for GtkSelection class using gtk+.
2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
41 #include "gnu_java_awt_peer_gtk_GtkSelection.h"
43 static jmethodID mimeTypesAvailableID
;
45 /* Note this is actually just a GtkClipboardReceivedFunc, not a real
46 GtkClipboardTargetsReceivedFunc, see requestMimeTypes. */
48 clipboard_targets_received (GtkClipboard
*clipboard
49 __attribute__((unused
)),
50 GtkSelectionData
*target_data
,
53 GdkAtom
*targets
= NULL
;
55 gchar
**target_strings
= NULL
;
56 jobjectArray strings
= NULL
;
58 gboolean include_text
= FALSE
;
59 gboolean include_image
= FALSE
;
60 gboolean include_uris
= FALSE
;
61 jobject selection_obj
= (jobject
) selection
;
62 JNIEnv
*env
= cp_gtk_gdk_env ();
64 if (target_data
!= NULL
&& target_data
->length
> 0)
66 include_text
= gtk_selection_data_targets_include_text (target_data
);
68 #if GTK_MINOR_VERSION > 4
69 include_image
= gtk_selection_data_targets_include_image (target_data
,
72 if (gtk_selection_data_get_targets (target_data
, &targets
, &targets_len
))
75 GdkAtom uri_list_atom
= gdk_atom_intern ("text/uri-list", FALSE
);
76 target_strings
= g_new (gchar
*, targets_len
);
77 if (target_strings
!= NULL
)
78 for (i
= 0; i
< targets_len
; i
++)
80 gchar
*name
= gdk_atom_name (targets
[i
]);
81 if (strchr (name
, '/') != NULL
)
83 target_strings
[i
] = name
;
85 if (! include_uris
&& targets
[i
] == uri_list_atom
)
89 target_strings
[i
] = NULL
;
93 if (target_strings
!= NULL
)
105 stringClass
= (*env
)->FindClass (env
, "java/lang/String");
106 strings
= (*env
)->NewObjectArray (env
, strings_len
, stringClass
,
111 (*env
)->SetObjectArrayElement (env
, strings
, i
++,
112 cp_gtk_stringTarget
);
114 (*env
)->SetObjectArrayElement (env
, strings
, i
++,
117 (*env
)->SetObjectArrayElement (env
, strings
, i
++,
120 while(i
< strings_len
)
122 if (target_strings
[j
] == NULL
)
127 string
= (*env
)->NewStringUTF (env
,
128 target_strings
[j
++]);
131 (*env
)->SetObjectArrayElement (env
, strings
, i
++,
133 (*env
)->DeleteLocalRef (env
, string
);
138 for (i
= 0; i
< targets_len
; i
++)
139 g_free (target_strings
[i
]);
140 g_free (target_strings
);
144 (*env
)->CallVoidMethod (env
, selection_obj
,
145 mimeTypesAvailableID
,
147 (*env
)->DeleteGlobalRef (env
, selection_obj
);
150 JNIEXPORT
void JNICALL
151 Java_gnu_java_awt_peer_gtk_GtkSelection_requestMimeTypes
152 (JNIEnv
*env
, jobject selection
, jboolean clipboard
)
154 jobject selection_obj
;
155 GtkClipboard
*gtk_clipboard
;
156 selection_obj
= (*env
)->NewGlobalRef(env
, selection
);
157 if (selection_obj
== NULL
)
160 if (mimeTypesAvailableID
== NULL
)
162 jclass gtk_selection_class
;
163 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
164 mimeTypesAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
165 "mimeTypesAvailable",
166 "([Ljava/lang/String;)V");
167 if (mimeTypesAvailableID
== NULL
)
172 gtk_clipboard
= cp_gtk_clipboard
;
174 gtk_clipboard
= cp_gtk_selection
;
176 /* We would have liked to call gtk_clipboard_request_targets ()
177 since that is more general. But the result of that, an array of
178 GdkAtoms, cannot be used with the
179 gtk_selection_data_targets_include_<x> functions (despite what
180 the name suggests). */
181 gdk_threads_enter ();
182 gtk_clipboard_request_contents (gtk_clipboard
,
183 gdk_atom_intern ("TARGETS", FALSE
),
184 clipboard_targets_received
,
185 (gpointer
) selection_obj
);
186 gdk_threads_leave ();
190 static jmethodID textAvailableID
;
193 clipboard_text_received (GtkClipboard
*clipboard
194 __attribute__((unused
)),
199 jobject selection_obj
= (jobject
) selection
;
201 JNIEnv
*env
= cp_gtk_gdk_env ();
203 string
= (*env
)->NewStringUTF (env
, text
);
207 (*env
)->CallVoidMethod (env
, selection_obj
,
210 (*env
)->DeleteGlobalRef (env
, selection_obj
);
213 (*env
)->DeleteLocalRef (env
, string
);
217 JNIEXPORT
void JNICALL
218 Java_gnu_java_awt_peer_gtk_GtkSelection_requestText
219 (JNIEnv
*env
, jobject selection
, jboolean clipboard
)
221 jobject selection_obj
;
222 GtkClipboard
*gtk_clipboard
;
223 selection_obj
= (*env
)->NewGlobalRef(env
, selection
);
224 if (selection_obj
== NULL
)
227 if (textAvailableID
== NULL
)
229 jclass gtk_selection_class
;
230 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
231 textAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
233 "(Ljava/lang/String;)V");
234 if (textAvailableID
== NULL
)
239 gtk_clipboard
= cp_gtk_clipboard
;
241 gtk_clipboard
= cp_gtk_selection
;
243 gdk_threads_enter ();
244 gtk_clipboard_request_text (gtk_clipboard
,
245 clipboard_text_received
,
246 (gpointer
) selection_obj
);
247 gdk_threads_leave ();
250 static jmethodID imageAvailableID
;
253 clipboard_image_received (GtkClipboard
*clipboard
254 __attribute__((unused
)),
258 jobject pointer
= NULL
;
259 jobject selection_obj
= (jobject
) selection
;
260 JNIEnv
*env
= cp_gtk_gdk_env ();
264 g_object_ref (pixbuf
);
265 pointer
= JCL_NewRawDataObject (env
, (void *) pixbuf
);
268 (*env
)->CallVoidMethod (env
, selection_obj
,
271 (*env
)->DeleteGlobalRef (env
, selection_obj
);
274 JNIEXPORT
void JNICALL
275 Java_gnu_java_awt_peer_gtk_GtkSelection_requestImage (JNIEnv
*env
,
279 jobject selection_obj
;
280 GtkClipboard
*gtk_clipboard
;
281 selection_obj
= (*env
)->NewGlobalRef(env
, obj
);
282 if (selection_obj
== NULL
)
285 if (imageAvailableID
== NULL
)
287 jclass gtk_selection_class
;
288 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
289 imageAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
291 "(Lgnu/classpath/Pointer;)V");
292 if (imageAvailableID
== NULL
)
297 gtk_clipboard
= cp_gtk_clipboard
;
299 gtk_clipboard
= cp_gtk_selection
;
301 #if GTK_MINOR_VERSION > 4
302 gdk_threads_enter ();
303 gtk_clipboard_request_image (gtk_clipboard
,
304 clipboard_image_received
,
305 (gpointer
) selection_obj
);
306 gdk_threads_leave ();
308 clipboard_image_received (gtk_clipboard
, NULL
, (gpointer
) selection_obj
);
312 static jmethodID urisAvailableID
;
315 clipboard_uris_received (GtkClipboard
*clipboard
316 __attribute__((unused
)),
317 GtkSelectionData
*uri_data
,
321 jobjectArray strings
= NULL
;
322 jobject selection_obj
= (jobject
) selection
;
323 JNIEnv
*env
= cp_gtk_gdk_env ();
325 #if GTK_MINOR_VERSION > 4
326 if (uri_data
!= NULL
)
327 uris
= gtk_selection_data_get_uris (uri_data
);
329 if (uri_data
!= NULL
)
336 gchar
**count
= uris
;
337 jclass stringClass
= (*env
)->FindClass (env
, "java/lang/String");
343 strings
= (*env
)->NewObjectArray (env
, len
, stringClass
, NULL
);
346 for (i
= 0; i
< len
; i
++)
348 jstring string
= (*env
)->NewStringUTF (env
, uris
[i
]);
351 (*env
)->SetObjectArrayElement (env
, strings
, i
, string
);
352 (*env
)->DeleteLocalRef (env
, string
);
358 (*env
)->CallVoidMethod (env
, selection_obj
,
361 (*env
)->DeleteGlobalRef (env
, selection_obj
);
364 JNIEXPORT
void JNICALL
365 Java_gnu_java_awt_peer_gtk_GtkSelection_requestURIs (JNIEnv
*env
,
369 #if GTK_MINOR_VERSION > 4
372 jobject selection_obj
;
373 GtkClipboard
*gtk_clipboard
;
374 selection_obj
= (*env
)->NewGlobalRef(env
, obj
);
375 if (selection_obj
== NULL
)
378 if (urisAvailableID
== NULL
)
380 jclass gtk_selection_class
;
381 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
382 urisAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
384 "([Ljava/lang/String;)V");
385 if (urisAvailableID
== NULL
)
390 gtk_clipboard
= cp_gtk_clipboard
;
392 gtk_clipboard
= cp_gtk_selection
;
394 #if GTK_MINOR_VERSION > 4
395 /* There is no real request_uris so we have to make one ourselves. */
396 gdk_threads_enter ();
397 uri_atom
= gdk_atom_intern ("text/uri-list", FALSE
);
398 gtk_clipboard_request_contents (gtk_clipboard
,
400 clipboard_uris_received
,
401 (gpointer
) selection_obj
);
402 gdk_threads_leave ();
404 clipboard_uris_received (gtk_clipboard
, NULL
, (gpointer
) selection_obj
);
408 static jmethodID bytesAvailableID
;
411 clipboard_bytes_received (GtkClipboard
*clipboard
412 __attribute__((unused
)),
413 GtkSelectionData
*selection_data
,
416 jbyteArray bytes
= NULL
;
417 jobject selection_obj
= (jobject
) selection
;
418 JNIEnv
*env
= cp_gtk_gdk_env ();
420 if (selection_data
!= NULL
&& selection_data
->length
> 0)
422 bytes
= (*env
)->NewByteArray (env
, selection_data
->length
);
424 (*env
)->SetByteArrayRegion(env
, bytes
, 0, selection_data
->length
,
425 (jbyte
*) selection_data
->data
);
428 (*env
)->CallVoidMethod (env
, selection_obj
,
431 (*env
)->DeleteGlobalRef (env
, selection_obj
);
434 JNIEXPORT
void JNICALL
435 Java_gnu_java_awt_peer_gtk_GtkSelection_requestBytes (JNIEnv
*env
,
438 jstring target_string
)
441 const gchar
*target_text
;
443 jobject selection_obj
;
444 GtkClipboard
*gtk_clipboard
;
445 selection_obj
= (*env
)->NewGlobalRef(env
, obj
);
446 if (selection_obj
== NULL
)
449 if (bytesAvailableID
== NULL
)
451 jclass gtk_selection_class
;
452 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
453 bytesAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
456 if (bytesAvailableID
== NULL
)
460 len
= (*env
)->GetStringUTFLength (env
, target_string
);
463 target_text
= (*env
)->GetStringUTFChars (env
, target_string
, NULL
);
464 if (target_text
== NULL
)
468 gtk_clipboard
= cp_gtk_clipboard
;
470 gtk_clipboard
= cp_gtk_selection
;
472 gdk_threads_enter ();
473 target_atom
= gdk_atom_intern (target_text
, FALSE
);
474 gtk_clipboard_request_contents (gtk_clipboard
,
476 clipboard_bytes_received
,
477 (gpointer
) selection_obj
);
478 gdk_threads_leave ();
480 (*env
)->ReleaseStringUTFChars (env
, target_string
, target_text
);