1 /* gtkselection.c -- Native C functions for GtkSelection class using gtk+.
2 Copyright (C) 2005 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
)
154 jobject selection_obj
;
155 selection_obj
= (*env
)->NewGlobalRef(env
, selection
);
156 if (selection_obj
== NULL
)
159 if (mimeTypesAvailableID
== NULL
)
161 jclass gtk_selection_class
;
162 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
163 mimeTypesAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
164 "mimeTypesAvailable",
165 "([Ljava/lang/String;)V");
166 if (mimeTypesAvailableID
== NULL
)
170 /* We would have liked to call gtk_clipboard_request_targets ()
171 since that is more general. But the result of that, an array of
172 GdkAtoms, cannot be used with the
173 gtk_selection_data_targets_include_<x> functions (despite what
174 the name suggests). */
175 gdk_threads_enter ();
176 gtk_clipboard_request_contents (cp_gtk_clipboard
,
177 gdk_atom_intern ("TARGETS", FALSE
),
178 clipboard_targets_received
,
179 (gpointer
) selection_obj
);
180 gdk_threads_leave ();
184 static jmethodID textAvailableID
;
187 clipboard_text_received (GtkClipboard
*clipboard
188 __attribute__((unused
)),
193 jobject selection_obj
= (jobject
) selection
;
195 JNIEnv
*env
= cp_gtk_gdk_env ();
197 string
= (*env
)->NewStringUTF (env
, text
);
201 (*env
)->CallVoidMethod (env
, selection_obj
,
204 (*env
)->DeleteGlobalRef (env
, selection_obj
);
207 (*env
)->DeleteLocalRef (env
, string
);
211 JNIEXPORT
void JNICALL
212 Java_gnu_java_awt_peer_gtk_GtkSelection_requestText
213 (JNIEnv
*env
, jobject selection
)
215 jobject selection_obj
;
216 selection_obj
= (*env
)->NewGlobalRef(env
, selection
);
217 if (selection_obj
== NULL
)
220 if (textAvailableID
== NULL
)
222 jclass gtk_selection_class
;
223 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
224 textAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
226 "(Ljava/lang/String;)V");
227 if (textAvailableID
== NULL
)
231 gdk_threads_enter ();
232 gtk_clipboard_request_text (cp_gtk_clipboard
,
233 clipboard_text_received
,
234 (gpointer
) selection_obj
);
235 gdk_threads_leave ();
238 static jmethodID imageAvailableID
;
241 clipboard_image_received (GtkClipboard
*clipboard
242 __attribute__((unused
)),
246 jobject pointer
= NULL
;
247 jobject selection_obj
= (jobject
) selection
;
248 JNIEnv
*env
= cp_gtk_gdk_env ();
252 g_object_ref (pixbuf
);
253 pointer
= JCL_NewRawDataObject (env
, (void *) pixbuf
);
256 (*env
)->CallVoidMethod (env
, selection_obj
,
259 (*env
)->DeleteGlobalRef (env
, selection_obj
);
262 JNIEXPORT
void JNICALL
263 Java_gnu_java_awt_peer_gtk_GtkSelection_requestImage (JNIEnv
*env
, jobject obj
)
265 jobject selection_obj
;
266 selection_obj
= (*env
)->NewGlobalRef(env
, obj
);
267 if (selection_obj
== NULL
)
270 if (imageAvailableID
== NULL
)
272 jclass gtk_selection_class
;
273 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
274 imageAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
276 "(Lgnu/classpath/Pointer;)V");
277 if (imageAvailableID
== NULL
)
281 #if GTK_MINOR_VERSION > 4
282 gdk_threads_enter ();
283 gtk_clipboard_request_image (cp_gtk_clipboard
,
284 clipboard_image_received
,
285 (gpointer
) selection_obj
);
286 gdk_threads_leave ();
288 clipboard_image_received (cp_gtk_clipboard
, NULL
, (gpointer
) selection_obj
);
292 static jmethodID urisAvailableID
;
295 clipboard_uris_received (GtkClipboard
*clipboard
296 __attribute__((unused
)),
297 GtkSelectionData
*uri_data
,
301 jobjectArray strings
= NULL
;
302 jobject selection_obj
= (jobject
) selection
;
303 JNIEnv
*env
= cp_gtk_gdk_env ();
305 #if GTK_MINOR_VERSION > 4
306 if (uri_data
!= NULL
)
307 uris
= gtk_selection_data_get_uris (uri_data
);
309 if (uri_data
!= NULL
)
316 gchar
**count
= uris
;
317 jclass stringClass
= (*env
)->FindClass (env
, "java/lang/String");
323 strings
= (*env
)->NewObjectArray (env
, len
, stringClass
, NULL
);
326 for (i
= 0; i
< len
; i
++)
328 jstring string
= (*env
)->NewStringUTF (env
, uris
[i
]);
331 (*env
)->SetObjectArrayElement (env
, strings
, i
, string
);
332 (*env
)->DeleteLocalRef (env
, string
);
338 (*env
)->CallVoidMethod (env
, selection_obj
,
341 (*env
)->DeleteGlobalRef (env
, selection_obj
);
344 JNIEXPORT
void JNICALL
345 Java_gnu_java_awt_peer_gtk_GtkSelection_requestURIs (JNIEnv
*env
, jobject obj
)
347 #if GTK_MINOR_VERSION > 4
350 jobject selection_obj
;
351 selection_obj
= (*env
)->NewGlobalRef(env
, obj
);
352 if (selection_obj
== NULL
)
355 if (urisAvailableID
== NULL
)
357 jclass gtk_selection_class
;
358 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
359 urisAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
361 "([Ljava/lang/String;)V");
362 if (urisAvailableID
== NULL
)
366 #if GTK_MINOR_VERSION > 4
367 /* There is no real request_uris so we have to make one ourselves. */
368 gdk_threads_enter ();
369 uri_atom
= gdk_atom_intern ("text/uri-list", FALSE
);
370 gtk_clipboard_request_contents (cp_gtk_clipboard
,
372 clipboard_uris_received
,
373 (gpointer
) selection_obj
);
374 gdk_threads_leave ();
376 clipboard_uris_received (cp_gtk_clipboard
, NULL
, (gpointer
) selection_obj
);
380 static jmethodID bytesAvailableID
;
383 clipboard_bytes_received (GtkClipboard
*clipboard
384 __attribute__((unused
)),
385 GtkSelectionData
*selection_data
,
388 jbyteArray bytes
= NULL
;
389 jobject selection_obj
= (jobject
) selection
;
390 JNIEnv
*env
= cp_gtk_gdk_env ();
392 if (selection_data
!= NULL
&& selection_data
->length
> 0)
394 bytes
= (*env
)->NewByteArray (env
, selection_data
->length
);
396 (*env
)->SetByteArrayRegion(env
, bytes
, 0, selection_data
->length
,
397 (jbyte
*) selection_data
->data
);
400 (*env
)->CallVoidMethod (env
, selection_obj
,
403 (*env
)->DeleteGlobalRef (env
, selection_obj
);
406 JNIEXPORT
void JNICALL
407 Java_gnu_java_awt_peer_gtk_GtkSelection_requestBytes (JNIEnv
*env
,
409 jstring target_string
)
412 const gchar
*target_text
;
414 jobject selection_obj
;
415 selection_obj
= (*env
)->NewGlobalRef(env
, obj
);
416 if (selection_obj
== NULL
)
419 if (bytesAvailableID
== NULL
)
421 jclass gtk_selection_class
;
422 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
423 bytesAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
426 if (bytesAvailableID
== NULL
)
430 len
= (*env
)->GetStringUTFLength (env
, target_string
);
433 target_text
= (*env
)->GetStringUTFChars (env
, target_string
, NULL
);
434 if (target_text
== NULL
)
437 gdk_threads_enter ();
438 target_atom
= gdk_atom_intern (target_text
, FALSE
);
439 gtk_clipboard_request_contents (cp_gtk_clipboard
,
441 clipboard_bytes_received
,
442 (gpointer
) selection_obj
);
443 gdk_threads_leave ();
445 (*env
)->ReleaseStringUTFChars (env
, target_string
, target_text
);