Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkSelection.c
blobf744e90adffd33eee00ff3e474805e76caff023a
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);
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,
145 strings);
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)
156 return;
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)
166 return;
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;
185 static void
186 clipboard_text_received (GtkClipboard *clipboard
187 __attribute__((unused)),
188 const gchar *text,
189 gpointer selection)
191 jstring string;
192 jobject selection_obj = (jobject) selection;
194 JNIEnv *env = cp_gtk_gdk_env ();
195 if (text != NULL)
196 string = (*env)->NewStringUTF (env, text);
197 else
198 string = NULL;
200 (*env)->CallVoidMethod (env, selection_obj,
201 textAvailableID,
202 string);
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)
213 return;
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,
220 "textAvailable",
221 "(Ljava/lang/String;)V");
222 if (textAvailableID == NULL)
223 return;
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;
235 static void
236 clipboard_image_received (GtkClipboard *clipboard
237 __attribute__((unused)),
238 GdkPixbuf *pixbuf,
239 gpointer selection)
241 jobject pointer = NULL;
242 jobject selection_obj = (jobject) selection;
243 JNIEnv *env = cp_gtk_gdk_env ();
245 if (pixbuf != NULL)
247 g_object_ref (pixbuf);
248 pointer = JCL_NewRawDataObject (env, (void *) pixbuf);
251 (*env)->CallVoidMethod (env, selection_obj,
252 imageAvailableID,
253 pointer);
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)
263 return;
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,
270 "imageAvailable",
271 "(Lgnu/classpath/Pointer;)V");
272 if (imageAvailableID == NULL)
273 return;
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 ();
282 #else
283 clipboard_image_received (cp_gtk_clipboard, NULL, (gpointer) selection_obj);
284 #endif
287 static jmethodID urisAvailableID;
289 static void
290 clipboard_uris_received (GtkClipboard *clipboard
291 __attribute__((unused)),
292 GtkSelectionData *uri_data,
293 gpointer selection)
295 gchar **uris = NULL;
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);
303 #else
304 if (uri_data != NULL)
305 uris = NULL;
306 #endif
308 if (uris != NULL)
310 int len, i;
311 gchar **count = uris;
312 jclass stringClass = (*env)->FindClass (env, "java/lang/String");
314 len = 0;
315 while (count[len])
316 len++;
318 strings = (*env)->NewObjectArray (env, len, stringClass, NULL);
319 if (strings != NULL)
321 for (i = 0; i < len; i++)
323 jstring string = (*env)->NewStringUTF (env, uris[i]);
324 if (string == NULL)
325 break;
326 (*env)->SetObjectArrayElement (env, strings, i, string);
329 g_strfreev (uris);
332 (*env)->CallVoidMethod (env, selection_obj,
333 urisAvailableID,
334 strings);
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
342 GdkAtom uri_atom;
343 #endif
344 jobject selection_obj;
345 selection_obj = (*env)->NewGlobalRef(env, obj);
346 if (selection_obj == NULL)
347 return;
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,
354 "urisAvailable",
355 "([Ljava/lang/String;)V");
356 if (urisAvailableID == NULL)
357 return;
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,
365 uri_atom,
366 clipboard_uris_received,
367 (gpointer) selection_obj);
368 gdk_threads_leave ();
369 #else
370 clipboard_uris_received (cp_gtk_clipboard, NULL, (gpointer) selection_obj);
371 #endif
374 static jmethodID bytesAvailableID;
376 static void
377 clipboard_bytes_received (GtkClipboard *clipboard
378 __attribute__((unused)),
379 GtkSelectionData *selection_data,
380 gpointer selection)
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);
389 if (bytes != NULL)
390 (*env)->SetByteArrayRegion(env, bytes, 0, selection_data->length,
391 (jbyte *) selection_data->data);
394 (*env)->CallVoidMethod (env, selection_obj,
395 bytesAvailableID,
396 bytes);
397 (*env)->DeleteGlobalRef (env, selection_obj);
400 JNIEXPORT void JNICALL
401 Java_gnu_java_awt_peer_gtk_GtkSelection_requestBytes (JNIEnv *env,
402 jobject obj,
403 jstring target_string)
405 int len;
406 const gchar *target_text;
407 GdkAtom target_atom;
408 jobject selection_obj;
409 selection_obj = (*env)->NewGlobalRef(env, obj);
410 if (selection_obj == NULL)
411 return;
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,
418 "bytesAvailable",
419 "([B)V");
420 if (bytesAvailableID == NULL)
421 return;
424 len = (*env)->GetStringUTFLength (env, target_string);
425 if (len == -1)
426 return;
427 target_text = (*env)->GetStringUTFChars (env, target_string, NULL);
428 if (target_text == NULL)
429 return;
431 gdk_threads_enter ();
432 target_atom = gdk_atom_intern (target_text, FALSE);
433 gtk_clipboard_request_contents (cp_gtk_clipboard,
434 target_atom,
435 clipboard_bytes_received,
436 (gpointer) selection_obj);
437 gdk_threads_leave ();
439 (*env)->ReleaseStringUTFChars (env, target_string, target_text);