summaryrefslogtreecommitdiff
path: root/native/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkSelection.c
blob: b845aebdc9c89acabff2b932f11e19312facb3fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* gtkselection.c -- Native C functions for GtkSelection class using gtk+.
   Copyright (C) 2005, 2006 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


#include "jcl.h"
#include "gtkpeer.h"
#include "gnu_java_awt_peer_gtk_GtkSelection.h"

static jmethodID mimeTypesAvailableID;

/* Note this is actually just a GtkClipboardReceivedFunc, not a real
   GtkClipboardTargetsReceivedFunc, see requestMimeTypes. */
static void
clipboard_targets_received (GtkClipboard *clipboard
			    __attribute__((unused)),
			    GtkSelectionData *target_data,
			    gpointer selection)
{
  GdkAtom *targets = NULL;
  gint targets_len = 0;
  gchar **target_strings = NULL;
  jobjectArray strings = NULL;
  int strings_len = 0;
  gboolean include_text = FALSE;
  gboolean include_image = FALSE;
  gboolean include_uris = FALSE;
  jobject selection_obj = (jobject) selection;
  JNIEnv *env = cp_gtk_gdk_env ();

  //if (target_data != NULL && target_data->length > 0)
  if (target_data != NULL && gtk_selection_data_get_length(target_data) > 0)
    {
      include_text = gtk_selection_data_targets_include_text (target_data);

#if GTK_MINOR_VERSION > 4
      include_image = gtk_selection_data_targets_include_image (target_data,
								TRUE);
#endif
      if (gtk_selection_data_get_targets (target_data, &targets, &targets_len))
	{
	  int i;
	  GdkAtom uri_list_atom = gdk_atom_intern ("text/uri-list", FALSE);
	  target_strings = g_new (gchar*, targets_len);
	  if (target_strings != NULL)
	    for (i = 0; i < targets_len; i++)
	      {
		gchar *name =  gdk_atom_name (targets[i]);
		if (strchr (name, '/') != NULL)
		  {
		    target_strings[i] = name;
		    strings_len++;
		    if (! include_uris && targets[i] == uri_list_atom)
		      include_uris = TRUE;
		  }
		else
		  target_strings[i] = NULL;
	      }
	}

      if (target_strings != NULL)
	{
	  int i = 0, j = 0;
	  jclass stringClass;
	  
	  if (include_text)
	    strings_len++;
	  if (include_image)
	    strings_len++;
	  if (include_uris)
	    strings_len++;
	  
	  stringClass = (*env)->FindClass (env, "java/lang/String");
	  strings = (*env)->NewObjectArray (env, strings_len, stringClass,
					    NULL);
	  (*env)->DeleteLocalRef(env, stringClass);

	  if (strings != NULL)
	    {
	      if (include_text)
		(*env)->SetObjectArrayElement (env, strings, i++,
					       cp_gtk_stringTarget);
	      if (include_image)
		(*env)->SetObjectArrayElement (env, strings, i++,
					       cp_gtk_imageTarget);
	      if (include_uris)
		(*env)->SetObjectArrayElement (env, strings, i++,
					       cp_gtk_filesTarget);
	      
	      while(i < strings_len)
		{
		  if (target_strings[j] == NULL)
		    j++;
		  else
		    {
		      jstring string;
		      string = (*env)->NewStringUTF (env,
						     target_strings[j++]);
		      if (string == NULL)
			break;
		      (*env)->SetObjectArrayElement (env, strings, i++,
						     string);
		      (*env)->DeleteLocalRef (env, string);
		    }
		}

	      (*env)->DeleteLocalRef(env, strings);
	    }

	  for (i = 0; i < targets_len; i++)
	    g_free (target_strings[i]);
	  g_free (target_strings);
	}
    }

  (*env)->CallVoidMethod (env, selection_obj,
			  mimeTypesAvailableID,
			  strings);
  (*env)->DeleteGlobalRef (env, selection_obj);
}

JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_GtkSelection_requestMimeTypes
(JNIEnv *env, jobject selection, jboolean clipboard)
{
  jobject selection_obj;
  GtkClipboard *gtk_clipboard;
  selection_obj = (*env)->NewGlobalRef(env, selection);
  if (selection_obj == NULL)
    return;

  if (mimeTypesAvailableID == NULL)
    {
      jclass gtk_selection_class;
      gtk_selection_class = (*env)->GetObjectClass (env, selection_obj);
      mimeTypesAvailableID = (*env)->GetMethodID (env, gtk_selection_class,
						"mimeTypesAvailable",
						"([Ljava/lang/String;)V");
      if (mimeTypesAvailableID == NULL)
	return;
    }

  if (clipboard)
    gtk_clipboard = cp_gtk_clipboard;
  else
    gtk_clipboard = cp_gtk_selection;

  /* We would have liked to call gtk_clipboard_request_targets ()
     since that is more general. But the result of that, an array of
     GdkAtoms, cannot be used with the
     gtk_selection_data_targets_include_<x> functions (despite what
     the name suggests). */
  gdk_threads_enter ();
  gtk_clipboard_request_contents (gtk_clipboard,
				  gdk_atom_intern ("TARGETS", FALSE),
				  clipboard_targets_received,
				  (gpointer) selection_obj);
  gdk_threads_leave ();
}


static jmethodID textAvailableID;

static void
clipboard_text_received (GtkClipboard *clipboard
			 __attribute__((unused)),
			 const gchar *text,
			 gpointer selection)
{
  jstring string;
  jobject selection_obj = (jobject) selection;

  JNIEnv *env = cp_gtk_gdk_env ();
  if (text != NULL)
    string = (*env)->NewStringUTF (env, text);
  else
    string = NULL;

  (*env)->CallVoidMethod (env, selection_obj,
                          textAvailableID,
                          string);
  (*env)->DeleteGlobalRef (env, selection_obj);

  if (string != NULL)
    (*env)->DeleteLocalRef (env, string);

}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkSelection_requestText
(JNIEnv *env, jobject selection, jboolean clipboard)
{
  jobject selection_obj;
  GtkClipboard *gtk_clipboard;
  selection_obj = (*env)->NewGlobalRef(env, selection);
  if (selection_obj == NULL)
    return;

  if (textAvailableID == NULL)
    {
      jclass gtk_selection_class;
      gtk_selection_class = (*env)->GetObjectClass (env, selection_obj);
      textAvailableID = (*env)->GetMethodID (env, gtk_selection_class,
					     "textAvailable",
					     "(Ljava/lang/String;)V");
      if (textAvailableID == NULL)
        return;
    }

  if (clipboard)
    gtk_clipboard = cp_gtk_clipboard;
  else
    gtk_clipboard = cp_gtk_selection;

  gdk_threads_enter ();
  gtk_clipboard_request_text (gtk_clipboard,
			      clipboard_text_received,
			      (gpointer) selection_obj);
  gdk_threads_leave ();
}

static jmethodID imageAvailableID;

static void
clipboard_image_received (GtkClipboard *clipboard
			  __attribute__((unused)),
			  GdkPixbuf *pixbuf,
			  gpointer selection)
{
  jobject pointer = NULL;
  jobject selection_obj = (jobject) selection;
  JNIEnv *env = cp_gtk_gdk_env ();

  if (pixbuf != NULL)
    {
      g_object_ref (pixbuf);
      pointer = JCL_NewRawDataObject (env, (void *) pixbuf);
    }

  (*env)->CallVoidMethod (env, selection_obj,
			  imageAvailableID,
                          pointer);
  (*env)->DeleteGlobalRef (env, selection_obj);
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkSelection_requestImage (JNIEnv *env,
						      jobject obj,
						      jboolean clipboard)
{
  jobject selection_obj;
  GtkClipboard *gtk_clipboard;
  selection_obj = (*env)->NewGlobalRef(env, obj);
  if (selection_obj == NULL)
    return;

  if (imageAvailableID == NULL)
    {
      jclass gtk_selection_class;
      gtk_selection_class = (*env)->GetObjectClass (env, selection_obj);
      imageAvailableID = (*env)->GetMethodID (env, gtk_selection_class,
					     "imageAvailable",
					     "(Lgnu/classpath/Pointer;)V");
      if (imageAvailableID == NULL)
        return;
    }

  if (clipboard)
    gtk_clipboard = cp_gtk_clipboard;
  else
    gtk_clipboard = cp_gtk_selection;

#if GTK_MINOR_VERSION > 4
  gdk_threads_enter ();
  gtk_clipboard_request_image (gtk_clipboard,
			       clipboard_image_received,
			       (gpointer) selection_obj);
  gdk_threads_leave ();
#else
  clipboard_image_received (gtk_clipboard, NULL, (gpointer) selection_obj);
#endif
}

static jmethodID urisAvailableID;

static void
clipboard_uris_received (GtkClipboard *clipboard
			 __attribute__((unused)),
			 GtkSelectionData *uri_data,
			 gpointer selection)
{
  gchar **uris = NULL;
  jobjectArray strings = NULL;
  jobject selection_obj = (jobject) selection;
  JNIEnv *env = cp_gtk_gdk_env ();

#if GTK_MINOR_VERSION > 4
  if (uri_data != NULL)
    uris = gtk_selection_data_get_uris (uri_data);
#else
  if (uri_data != NULL)
    uris = NULL;
#endif

  if (uris != NULL)
    {
      int len, i;
      gchar **count = uris;
      jclass stringClass = (*env)->FindClass (env, "java/lang/String");

      len = 0;
      while (count[len])
	len++;

      strings = (*env)->NewObjectArray (env, len, stringClass, NULL);
      (*env)->DeleteLocalRef(env, stringClass);

      if (strings != NULL)
	{
	  for (i = 0; i < len; i++)
	    {
	      jstring string = (*env)->NewStringUTF (env, uris[i]);
	      if (string == NULL)
		break;
	      (*env)->SetObjectArrayElement (env, strings, i, string);
	      (*env)->DeleteLocalRef (env, string);
	    }

	  (*env)->DeleteLocalRef(env, strings);
	}
      g_strfreev (uris);
    }

  (*env)->CallVoidMethod (env, selection_obj,
                          urisAvailableID,
                          strings);
  (*env)->DeleteGlobalRef (env, selection_obj);
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkSelection_requestURIs (JNIEnv *env,
						     jobject obj,
						     jboolean clipboard)
{
#if GTK_MINOR_VERSION > 4
  GdkAtom uri_atom;
#endif
  jobject selection_obj;
  GtkClipboard *gtk_clipboard;
  selection_obj = (*env)->NewGlobalRef(env, obj);
  if (selection_obj == NULL)
    return;

  if (urisAvailableID == NULL)
    {
      jclass gtk_selection_class;
      gtk_selection_class = (*env)->GetObjectClass (env, selection_obj);
      urisAvailableID = (*env)->GetMethodID (env, gtk_selection_class,
					     "urisAvailable",
                                             "([Ljava/lang/String;)V");
      if (urisAvailableID == NULL)
        return;
    }

  if (clipboard)
    gtk_clipboard = cp_gtk_clipboard;
  else
    gtk_clipboard = cp_gtk_selection;

#if GTK_MINOR_VERSION > 4
  /* There is no real request_uris so we have to make one ourselves. */
  gdk_threads_enter ();
  uri_atom = gdk_atom_intern ("text/uri-list", FALSE);
  gtk_clipboard_request_contents (gtk_clipboard,
				  uri_atom,
				  clipboard_uris_received,
				  (gpointer) selection_obj);
  gdk_threads_leave ();
#else
  clipboard_uris_received (gtk_clipboard, NULL, (gpointer) selection_obj);
#endif
}

static jmethodID bytesAvailableID;

static void
clipboard_bytes_received (GtkClipboard *clipboard
			  __attribute__((unused)),
			  GtkSelectionData *selection_data,
			  gpointer selection)
{
  jbyteArray bytes = NULL;
  jobject selection_obj = (jobject) selection;
  JNIEnv *env = cp_gtk_gdk_env ();

   //if (selection_data != NULL && selection_data->length > 0)
     if (selection_data != NULL && gtk_selection_data_get_length(selection_data) > 0)
    {
      //bytes = (*env)->NewByteArray (env, selection_data->length);
      bytes = (*env)->NewByteArray (env, gtk_selection_data_get_length(selection_data));
      if (bytes != NULL)
	/*(*env)->SetByteArrayRegion(env, bytes, 0, selection_data->length,
				   (jbyte *) selection_data->data);*/
         (*env)->SetByteArrayRegion(env, bytes, 0, gtk_selection_data_get_length(selection_data),
				   (jbyte *) gtk_selection_data_get_data(selection_data));
    }

  (*env)->CallVoidMethod (env, selection_obj,
                          bytesAvailableID,
                          bytes);
  (*env)->DeleteGlobalRef (env, selection_obj);
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkSelection_requestBytes (JNIEnv *env,
						      jobject obj,
						      jboolean clipboard,
						      jstring target_string)
{
  int len;
  const gchar *target_text;
  GdkAtom target_atom;
  jobject selection_obj;
  GtkClipboard *gtk_clipboard;
  selection_obj = (*env)->NewGlobalRef(env, obj);
  if (selection_obj == NULL)
    return;

  if (bytesAvailableID == NULL)
    {
      jclass gtk_selection_class;
      gtk_selection_class = (*env)->GetObjectClass (env, selection_obj);
      bytesAvailableID = (*env)->GetMethodID (env, gtk_selection_class,
					      "bytesAvailable",
					      "([B)V");
      if (bytesAvailableID == NULL)
        return;
    }

  len = (*env)->GetStringUTFLength (env, target_string);
  if (len == -1)
    return;
  target_text = (*env)->GetStringUTFChars (env, target_string, NULL);
  if (target_text == NULL)
    return;

  if (clipboard)
    gtk_clipboard = cp_gtk_clipboard;
  else
    gtk_clipboard = cp_gtk_selection;

  gdk_threads_enter ();
  target_atom = gdk_atom_intern (target_text, FALSE);
  gtk_clipboard_request_contents (gtk_clipboard,
                                  target_atom,
                                  clipboard_bytes_received,
                                  (gpointer) selection_obj);
  gdk_threads_leave ();

  (*env)->ReleaseStringUTFChars (env, target_string, target_text);
}