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
++,
137 for (i
= 0; i
< targets_len
; i
++)
138 g_free (target_strings
[i
]);
139 g_free (target_strings
);
143 (*env
)->CallVoidMethod (env
, selection_obj
,
144 mimeTypesAvailableID
,
146 (*env
)->DeleteGlobalRef (env
, selection_obj
);
149 JNIEXPORT
void JNICALL
150 Java_gnu_java_awt_peer_gtk_GtkSelection_requestMimeTypes
151 (JNIEnv
*env
, jobject selection
)
153 jobject selection_obj
;
154 selection_obj
= (*env
)->NewGlobalRef(env
, selection
);
155 if (selection_obj
== NULL
)
158 if (mimeTypesAvailableID
== NULL
)
160 jclass gtk_selection_class
;
161 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
162 mimeTypesAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
163 "mimeTypesAvailable",
164 "([Ljava/lang/String;)V");
165 if (mimeTypesAvailableID
== NULL
)
169 /* We would have liked to call gtk_clipboard_request_targets ()
170 since that is more general. But the result of that, an array of
171 GdkAtoms, cannot be used with the
172 gtk_selection_data_targets_include_<x> functions (despite what
173 the name suggests). */
174 gdk_threads_enter ();
175 gtk_clipboard_request_contents (cp_gtk_clipboard
,
176 gdk_atom_intern ("TARGETS", FALSE
),
177 clipboard_targets_received
,
178 (gpointer
) selection_obj
);
179 gdk_threads_leave ();
183 static jmethodID textAvailableID
;
186 clipboard_text_received (GtkClipboard
*clipboard
187 __attribute__((unused
)),
192 jobject selection_obj
= (jobject
) selection
;
194 JNIEnv
*env
= cp_gtk_gdk_env ();
196 string
= (*env
)->NewStringUTF (env
, text
);
200 (*env
)->CallVoidMethod (env
, selection_obj
,
203 (*env
)->DeleteGlobalRef (env
, selection_obj
);
206 JNIEXPORT
void JNICALL
207 Java_gnu_java_awt_peer_gtk_GtkSelection_requestText
208 (JNIEnv
*env
, jobject selection
)
210 jobject selection_obj
;
211 selection_obj
= (*env
)->NewGlobalRef(env
, selection
);
212 if (selection_obj
== NULL
)
215 if (textAvailableID
== NULL
)
217 jclass gtk_selection_class
;
218 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
219 textAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
221 "(Ljava/lang/String;)V");
222 if (textAvailableID
== NULL
)
226 gdk_threads_enter ();
227 gtk_clipboard_request_text (cp_gtk_clipboard
,
228 clipboard_text_received
,
229 (gpointer
) selection_obj
);
230 gdk_threads_leave ();
233 static jmethodID imageAvailableID
;
236 clipboard_image_received (GtkClipboard
*clipboard
237 __attribute__((unused
)),
241 jobject pointer
= NULL
;
242 jobject selection_obj
= (jobject
) selection
;
243 JNIEnv
*env
= cp_gtk_gdk_env ();
247 g_object_ref (pixbuf
);
248 pointer
= JCL_NewRawDataObject (env
, (void *) pixbuf
);
251 (*env
)->CallVoidMethod (env
, selection_obj
,
254 (*env
)->DeleteGlobalRef (env
, selection_obj
);
257 JNIEXPORT
void JNICALL
258 Java_gnu_java_awt_peer_gtk_GtkSelection_requestImage (JNIEnv
*env
, jobject obj
)
260 jobject selection_obj
;
261 selection_obj
= (*env
)->NewGlobalRef(env
, obj
);
262 if (selection_obj
== NULL
)
265 if (imageAvailableID
== NULL
)
267 jclass gtk_selection_class
;
268 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
269 imageAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
271 "(Lgnu/classpath/Pointer;)V");
272 if (imageAvailableID
== NULL
)
276 #if GTK_MINOR_VERSION > 4
277 gdk_threads_enter ();
278 gtk_clipboard_request_image (cp_gtk_clipboard
,
279 clipboard_image_received
,
280 (gpointer
) selection_obj
);
281 gdk_threads_leave ();
283 clipboard_image_received (cp_gtk_clipboard
, NULL
, (gpointer
) selection_obj
);
287 static jmethodID urisAvailableID
;
290 clipboard_uris_received (GtkClipboard
*clipboard
291 __attribute__((unused
)),
292 GtkSelectionData
*uri_data
,
296 jobjectArray strings
= NULL
;
297 jobject selection_obj
= (jobject
) selection
;
298 JNIEnv
*env
= cp_gtk_gdk_env ();
300 #if GTK_MINOR_VERSION > 4
301 if (uri_data
!= NULL
)
302 uris
= gtk_selection_data_get_uris (uri_data
);
304 if (uri_data
!= NULL
)
311 gchar
**count
= uris
;
312 jclass stringClass
= (*env
)->FindClass (env
, "java/lang/String");
318 strings
= (*env
)->NewObjectArray (env
, len
, stringClass
, NULL
);
321 for (i
= 0; i
< len
; i
++)
323 jstring string
= (*env
)->NewStringUTF (env
, uris
[i
]);
326 (*env
)->SetObjectArrayElement (env
, strings
, i
, string
);
332 (*env
)->CallVoidMethod (env
, selection_obj
,
335 (*env
)->DeleteGlobalRef (env
, selection_obj
);
338 JNIEXPORT
void JNICALL
339 Java_gnu_java_awt_peer_gtk_GtkSelection_requestURIs (JNIEnv
*env
, jobject obj
)
341 #if GTK_MINOR_VERSION > 4
344 jobject selection_obj
;
345 selection_obj
= (*env
)->NewGlobalRef(env
, obj
);
346 if (selection_obj
== NULL
)
349 if (urisAvailableID
== NULL
)
351 jclass gtk_selection_class
;
352 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
353 urisAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
355 "([Ljava/lang/String;)V");
356 if (urisAvailableID
== NULL
)
360 #if GTK_MINOR_VERSION > 4
361 /* There is no real request_uris so we have to make one ourselves. */
362 gdk_threads_enter ();
363 uri_atom
= gdk_atom_intern ("text/uri-list", FALSE
);
364 gtk_clipboard_request_contents (cp_gtk_clipboard
,
366 clipboard_uris_received
,
367 (gpointer
) selection_obj
);
368 gdk_threads_leave ();
370 clipboard_uris_received (cp_gtk_clipboard
, NULL
, (gpointer
) selection_obj
);
374 static jmethodID bytesAvailableID
;
377 clipboard_bytes_received (GtkClipboard
*clipboard
378 __attribute__((unused
)),
379 GtkSelectionData
*selection_data
,
382 jbyteArray bytes
= NULL
;
383 jobject selection_obj
= (jobject
) selection
;
384 JNIEnv
*env
= cp_gtk_gdk_env ();
386 if (selection_data
!= NULL
&& selection_data
->length
> 0)
388 bytes
= (*env
)->NewByteArray (env
, selection_data
->length
);
390 (*env
)->SetByteArrayRegion(env
, bytes
, 0, selection_data
->length
,
391 (jbyte
*) selection_data
->data
);
394 (*env
)->CallVoidMethod (env
, selection_obj
,
397 (*env
)->DeleteGlobalRef (env
, selection_obj
);
400 JNIEXPORT
void JNICALL
401 Java_gnu_java_awt_peer_gtk_GtkSelection_requestBytes (JNIEnv
*env
,
403 jstring target_string
)
406 const gchar
*target_text
;
408 jobject selection_obj
;
409 selection_obj
= (*env
)->NewGlobalRef(env
, obj
);
410 if (selection_obj
== NULL
)
413 if (bytesAvailableID
== NULL
)
415 jclass gtk_selection_class
;
416 gtk_selection_class
= (*env
)->GetObjectClass (env
, selection_obj
);
417 bytesAvailableID
= (*env
)->GetMethodID (env
, gtk_selection_class
,
420 if (bytesAvailableID
== NULL
)
424 len
= (*env
)->GetStringUTFLength (env
, target_string
);
427 target_text
= (*env
)->GetStringUTFChars (env
, target_string
, NULL
);
428 if (target_text
== NULL
)
431 gdk_threads_enter ();
432 target_atom
= gdk_atom_intern (target_text
, FALSE
);
433 gtk_clipboard_request_contents (cp_gtk_clipboard
,
435 clipboard_bytes_received
,
436 (gpointer
) selection_obj
);
437 gdk_threads_leave ();
439 (*env
)->ReleaseStringUTFChars (env
, target_string
, target_text
);