Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkSelection.c
blob3244d236422fcd51d9f451cdec244600f7c91bbb
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)
9 any later version.
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
19 02110-1301 USA.
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.
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. */
39 #include "jcl.h"
40 #include "gtkpeer.h"
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. */
47 static void
48 clipboard_targets_received (GtkClipboard *clipboard
49 __attribute__((unused)),
50 GtkSelectionData *target_data,
51 gpointer selection)
53 GdkAtom *targets = NULL;
54 gint targets_len = 0;
55 gchar **target_strings = NULL;
56 jobjectArray strings = NULL;
57 int strings_len = 0;
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,
70 TRUE);
71 #endif
72 if (gtk_selection_data_get_targets (target_data, &targets, &targets_len))
74 int i;
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;
84 strings_len++;
85 if (! include_uris && targets[i] == uri_list_atom)
86 include_uris = TRUE;
88 else
89 target_strings[i] = NULL;
93 if (target_strings != NULL)
95 int i = 0, j = 0;
96 jclass stringClass;
98 if (include_text)
99 strings_len++;
100 if (include_image)
101 strings_len++;
102 if (include_uris)
103 strings_len++;
105 stringClass = (*env)->FindClass (env, "java/lang/String");
106 strings = (*env)->NewObjectArray (env, strings_len, stringClass,
107 NULL);
108 if (strings != NULL)
110 if (include_text)
111 (*env)->SetObjectArrayElement (env, strings, i++,
112 cp_gtk_stringTarget);
113 if (include_image)
114 (*env)->SetObjectArrayElement (env, strings, i++,
115 cp_gtk_imageTarget);
116 if (include_uris)
117 (*env)->SetObjectArrayElement (env, strings, i++,
118 cp_gtk_filesTarget);
120 while(i < strings_len)
122 if (target_strings[j] == NULL)
123 j++;
124 else
126 jstring string;
127 string = (*env)->NewStringUTF (env,
128 target_strings[j++]);
129 if (string == NULL)
130 break;
131 (*env)->SetObjectArrayElement (env, strings, i++,
132 string);
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,
146 strings);
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)
157 return;
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)
167 return;
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;
186 static void
187 clipboard_text_received (GtkClipboard *clipboard
188 __attribute__((unused)),
189 const gchar *text,
190 gpointer selection)
192 jstring string;
193 jobject selection_obj = (jobject) selection;
195 JNIEnv *env = cp_gtk_gdk_env ();
196 if (text != NULL)
197 string = (*env)->NewStringUTF (env, text);
198 else
199 string = NULL;
201 (*env)->CallVoidMethod (env, selection_obj,
202 textAvailableID,
203 string);
204 (*env)->DeleteGlobalRef (env, selection_obj);
206 if (string != NULL)
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)
218 return;
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,
225 "textAvailable",
226 "(Ljava/lang/String;)V");
227 if (textAvailableID == NULL)
228 return;
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;
240 static void
241 clipboard_image_received (GtkClipboard *clipboard
242 __attribute__((unused)),
243 GdkPixbuf *pixbuf,
244 gpointer selection)
246 jobject pointer = NULL;
247 jobject selection_obj = (jobject) selection;
248 JNIEnv *env = cp_gtk_gdk_env ();
250 if (pixbuf != NULL)
252 g_object_ref (pixbuf);
253 pointer = JCL_NewRawDataObject (env, (void *) pixbuf);
256 (*env)->CallVoidMethod (env, selection_obj,
257 imageAvailableID,
258 pointer);
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)
268 return;
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,
275 "imageAvailable",
276 "(Lgnu/classpath/Pointer;)V");
277 if (imageAvailableID == NULL)
278 return;
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 ();
287 #else
288 clipboard_image_received (cp_gtk_clipboard, NULL, (gpointer) selection_obj);
289 #endif
292 static jmethodID urisAvailableID;
294 static void
295 clipboard_uris_received (GtkClipboard *clipboard
296 __attribute__((unused)),
297 GtkSelectionData *uri_data,
298 gpointer selection)
300 gchar **uris = NULL;
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);
308 #else
309 if (uri_data != NULL)
310 uris = NULL;
311 #endif
313 if (uris != NULL)
315 int len, i;
316 gchar **count = uris;
317 jclass stringClass = (*env)->FindClass (env, "java/lang/String");
319 len = 0;
320 while (count[len])
321 len++;
323 strings = (*env)->NewObjectArray (env, len, stringClass, NULL);
324 if (strings != NULL)
326 for (i = 0; i < len; i++)
328 jstring string = (*env)->NewStringUTF (env, uris[i]);
329 if (string == NULL)
330 break;
331 (*env)->SetObjectArrayElement (env, strings, i, string);
332 (*env)->DeleteLocalRef (env, string);
335 g_strfreev (uris);
338 (*env)->CallVoidMethod (env, selection_obj,
339 urisAvailableID,
340 strings);
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
348 GdkAtom uri_atom;
349 #endif
350 jobject selection_obj;
351 selection_obj = (*env)->NewGlobalRef(env, obj);
352 if (selection_obj == NULL)
353 return;
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,
360 "urisAvailable",
361 "([Ljava/lang/String;)V");
362 if (urisAvailableID == NULL)
363 return;
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,
371 uri_atom,
372 clipboard_uris_received,
373 (gpointer) selection_obj);
374 gdk_threads_leave ();
375 #else
376 clipboard_uris_received (cp_gtk_clipboard, NULL, (gpointer) selection_obj);
377 #endif
380 static jmethodID bytesAvailableID;
382 static void
383 clipboard_bytes_received (GtkClipboard *clipboard
384 __attribute__((unused)),
385 GtkSelectionData *selection_data,
386 gpointer selection)
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);
395 if (bytes != NULL)
396 (*env)->SetByteArrayRegion(env, bytes, 0, selection_data->length,
397 (jbyte *) selection_data->data);
400 (*env)->CallVoidMethod (env, selection_obj,
401 bytesAvailableID,
402 bytes);
403 (*env)->DeleteGlobalRef (env, selection_obj);
406 JNIEXPORT void JNICALL
407 Java_gnu_java_awt_peer_gtk_GtkSelection_requestBytes (JNIEnv *env,
408 jobject obj,
409 jstring target_string)
411 int len;
412 const gchar *target_text;
413 GdkAtom target_atom;
414 jobject selection_obj;
415 selection_obj = (*env)->NewGlobalRef(env, obj);
416 if (selection_obj == NULL)
417 return;
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,
424 "bytesAvailable",
425 "([B)V");
426 if (bytesAvailableID == NULL)
427 return;
430 len = (*env)->GetStringUTFLength (env, target_string);
431 if (len == -1)
432 return;
433 target_text = (*env)->GetStringUTFChars (env, target_string, NULL);
434 if (target_text == NULL)
435 return;
437 gdk_threads_enter ();
438 target_atom = gdk_atom_intern (target_text, FALSE);
439 gtk_clipboard_request_contents (cp_gtk_clipboard,
440 target_atom,
441 clipboard_bytes_received,
442 (gpointer) selection_obj);
443 gdk_threads_leave ();
445 (*env)->ReleaseStringUTFChars (env, target_string, target_text);