Skip to content

Commit 5327d80

Browse files
committed
[GTK4] Implement Clipboard.getAvailableTypes()
The list of available types is calculated using the GDK4 clipboard and no longer falls back to the GTK3 bindings. Note that UTF8_STRING, COMPOUND_TEXT and STRING_ID are not implemented for GTK4, which is why those types must not be used in the TextTransfer, regardless of whether X11 or Wayland is used.
1 parent 6a2b931 commit 5327d80

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/gtk/org/eclipse/swt/dnd/Clipboard.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2019 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -570,7 +570,6 @@ public TransferData[] getAvailableTypes() {
570570
public TransferData[] getAvailableTypes(int clipboards) {
571571
checkWidget();
572572

573-
//TODO: [GTK4] This currently will not work in GTK4
574573
TransferData[] result = null;
575574
if ((clipboards & DND.CLIPBOARD) != 0) {
576575
int[] types = getAvailableClipboardTypes();
@@ -656,6 +655,10 @@ public String[] getAvailableTypeNames() {
656655
}
657656

658657
private int[] getAvailablePrimaryTypes() {
658+
if (GTK.GTK4) {
659+
return gtk4_getAvailableTypes(GTKPRIMARYCLIPBOARD);
660+
}
661+
659662
int[] types = new int[0];
660663
long selection_data = gtk_clipboard_wait_for_contents(GTKPRIMARYCLIPBOARD, TARGET);
661664
if (selection_data != 0) {
@@ -674,6 +677,9 @@ private int[] getAvailablePrimaryTypes() {
674677
return types;
675678
}
676679
private int[] getAvailableClipboardTypes () {
680+
if (GTK.GTK4) {
681+
return gtk4_getAvailableTypes(GTKCLIPBOARD);
682+
}
677683

678684
int[] types = new int[0];
679685
long selection_data = gtk_clipboard_wait_for_contents(GTKCLIPBOARD, TARGET);
@@ -693,6 +699,21 @@ private int[] getAvailableClipboardTypes () {
693699
return types;
694700
}
695701

702+
int[] gtk4_getAvailableTypes(long clipboard) {
703+
long formats = GTK4.gdk_clipboard_get_formats(clipboard);
704+
long[] n_gtypes = new long[1];
705+
long gtypes = GTK4.gdk_content_formats_get_gtypes(formats, n_gtypes);
706+
707+
int gtypes_length = (int) n_gtypes[0];
708+
int[] types = new int[gtypes_length];
709+
for (int i = 0 ; i < gtypes_length ; ++i) {
710+
long[] ptr = new long[1];
711+
C.memmove(ptr, gtypes + i * C.PTR_SIZEOF, C.PTR_SIZEOF);
712+
types[i] = (int) ptr[0];
713+
}
714+
return types;
715+
}
716+
696717
long gtk_clipboard_wait_for_contents(long clipboard, long target) {
697718
long startTime = System.currentTimeMillis();
698719
String key = "org.eclipse.swt.internal.gtk.dispatchEvent";

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/gtk/org/eclipse/swt/dnd/TextTransfer.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2017 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -141,23 +141,23 @@ public Object nativeToJava(TransferData transferData){
141141

142142
@Override
143143
protected int[] getTypeIds() {
144-
if (OS.isX11()) {
145-
return new int[] {UTF8_STRING_ID, COMPOUND_TEXT_ID, STRING_ID};
146-
}
147144
if(GTK.GTK4) {
148145
return new int[] {(int) OS.G_TYPE_STRING()};
149146
}
147+
if (OS.isX11()) {
148+
return new int[] {UTF8_STRING_ID, COMPOUND_TEXT_ID, STRING_ID};
149+
}
150150
return new int[] {UTF8_STRING_ID, STRING_ID, TEXT_PLAIN_UTF8_ID};
151151
}
152152

153153
@Override
154154
protected String[] getTypeNames() {
155-
if (OS.isX11()) {
156-
return new String[] {UTF8_STRING, COMPOUND_TEXT, STRING};
157-
}
158155
if(GTK.GTK4) {
159156
return new String[] {"text/plain", STRING};
160157
}
158+
if (OS.isX11()) {
159+
return new String[] {UTF8_STRING, COMPOUND_TEXT, STRING};
160+
}
161161

162162
return new String[] {UTF8_STRING, STRING, TEXT_PLAIN_UTF8};
163163
}

bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/gtk4.c

+16
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ JNIEXPORT jlong JNICALL GTK4_NATIVE(gdk_1content_1formats_1builder_1new)
127127
}
128128
#endif
129129

130+
#ifndef NO_gdk_1content_1formats_1get_1gtypes
131+
JNIEXPORT jlong JNICALL GTK4_NATIVE(gdk_1content_1formats_1get_1gtypes)
132+
(JNIEnv *env, jclass that, jlong arg0, jlongArray arg1)
133+
{
134+
jlong *lparg1=NULL;
135+
jlong rc = 0;
136+
GTK4_NATIVE_ENTER(env, that, gdk_1content_1formats_1get_1gtypes_FUNC);
137+
if (arg1) if ((lparg1 = (*env)->GetLongArrayElements(env, arg1, NULL)) == NULL) goto fail;
138+
rc = (jlong)gdk_content_formats_get_gtypes((GdkContentFormats *)arg0, (gsize *)lparg1);
139+
fail:
140+
if (arg1 && lparg1) (*env)->ReleaseLongArrayElements(env, arg1, lparg1, 0);
141+
GTK4_NATIVE_EXIT(env, that, gdk_1content_1formats_1get_1gtypes_FUNC);
142+
return rc;
143+
}
144+
#endif
145+
130146
#ifndef NO_gdk_1content_1formats_1to_1string
131147
JNIEXPORT jlong JNICALL GTK4_NATIVE(gdk_1content_1formats_1to_1string)
132148
(JNIEnv *env, jclass that, jlong arg0)

bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/gtk4_stats.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef enum {
3131
gdk_1content_1formats_1builder_1add_1mime_1type_FUNC,
3232
gdk_1content_1formats_1builder_1free_1to_1formats_FUNC,
3333
gdk_1content_1formats_1builder_1new_FUNC,
34+
gdk_1content_1formats_1get_1gtypes_FUNC,
3435
gdk_1content_1formats_1to_1string_FUNC,
3536
gdk_1content_1provider_1get_1value_FUNC,
3637
gdk_1content_1provider_1new_1for_1value_FUNC,

bundles/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk4/GTK4.java

+5
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,11 @@ public class GTK4 {
818818
public static final native long gdk_content_provider_new_union(long[] providers, int n_providers);
819819
/** @param formats cast=(GdkContentFormats *) */
820820
public static final native long gdk_content_formats_to_string(long formats);
821+
/**
822+
* @param formats cast=(GdkContentFormats *)
823+
* @param n_gtypes cast=(gsize *)
824+
*/
825+
public static final native long gdk_content_formats_get_gtypes(long formats, long[] n_gtypes);
821826

822827
public static final native long gtk_gesture_rotate_new();
823828

0 commit comments

Comments
 (0)