Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GTK4] Implement Clipboard.getAvailableTypes() #1990

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -570,7 +570,6 @@ public TransferData[] getAvailableTypes() {
public TransferData[] getAvailableTypes(int clipboards) {
checkWidget();

//TODO: [GTK4] This currently will not work in GTK4
TransferData[] result = null;
if ((clipboards & DND.CLIPBOARD) != 0) {
int[] types = getAvailableClipboardTypes();
Expand Down Expand Up @@ -656,27 +655,36 @@ public String[] getAvailableTypeNames() {
}

private int[] getAvailablePrimaryTypes() {
int[] types = new int[0];
long selection_data = gtk_clipboard_wait_for_contents(GTKPRIMARYCLIPBOARD, TARGET);
if (selection_data != 0) {
try {
int length = GTK3.gtk_selection_data_get_length(selection_data);
int format = GTK3.gtk_selection_data_get_format(selection_data);
long data = GTK3.gtk_selection_data_get_data(selection_data);
if (length != 0) {
types = new int[length * 8 / format];
C.memmove(types, data, length);
}
} finally {
GTK3.gtk_selection_data_free(selection_data);
}
if (GTK.GTK4) {
return gtk4_getAvailableTypes(GTKPRIMARYCLIPBOARD);
}
return types;
return gtk3_getAvailableTypes(GTKPRIMARYCLIPBOARD);
}
private int[] getAvailableClipboardTypes () {
if (GTK.GTK4) {
return gtk4_getAvailableTypes(GTKCLIPBOARD);
}
return gtk3_getAvailableTypes(GTKCLIPBOARD);
}

private int[] gtk4_getAvailableTypes(long clipboard) {
long formats = GTK4.gdk_clipboard_get_formats(clipboard);
long[] n_gtypes = new long[1];
long gtypes = GTK4.gdk_content_formats_get_gtypes(formats, n_gtypes);

int gtypes_length = (int) n_gtypes[0];
int[] types = new int[gtypes_length];
for (int i = 0 ; i < gtypes_length ; ++i) {
long[] ptr = new long[1];
C.memmove(ptr, gtypes + i * C.PTR_SIZEOF, C.PTR_SIZEOF);
types[i] = (int) ptr[0];
}
return types;
}

private int[] gtk3_getAvailableTypes(long clipboard) {
int[] types = new int[0];
long selection_data = gtk_clipboard_wait_for_contents(GTKCLIPBOARD, TARGET);
long selection_data = gtk_clipboard_wait_for_contents(clipboard, TARGET);
if (selection_data != 0) {
try {
int length = GTK3.gtk_selection_data_get_length(selection_data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2017 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -141,23 +141,23 @@ public Object nativeToJava(TransferData transferData){

@Override
protected int[] getTypeIds() {
if (OS.isX11()) {
return new int[] {UTF8_STRING_ID, COMPOUND_TEXT_ID, STRING_ID};
}
if(GTK.GTK4) {
return new int[] {(int) OS.G_TYPE_STRING()};
}
if (OS.isX11()) {
return new int[] {UTF8_STRING_ID, COMPOUND_TEXT_ID, STRING_ID};
}
return new int[] {UTF8_STRING_ID, STRING_ID, TEXT_PLAIN_UTF8_ID};
}

@Override
protected String[] getTypeNames() {
if (OS.isX11()) {
return new String[] {UTF8_STRING, COMPOUND_TEXT, STRING};
}
if(GTK.GTK4) {
return new String[] {"text/plain", STRING};
}
if (OS.isX11()) {
return new String[] {UTF8_STRING, COMPOUND_TEXT, STRING};
}

return new String[] {UTF8_STRING, STRING, TEXT_PLAIN_UTF8};
}
Expand Down
16 changes: 16 additions & 0 deletions bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/gtk4.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ JNIEXPORT jlong JNICALL GTK4_NATIVE(gdk_1content_1formats_1builder_1new)
}
#endif

#ifndef NO_gdk_1content_1formats_1get_1gtypes
JNIEXPORT jlong JNICALL GTK4_NATIVE(gdk_1content_1formats_1get_1gtypes)
(JNIEnv *env, jclass that, jlong arg0, jlongArray arg1)
{
jlong *lparg1=NULL;
jlong rc = 0;
GTK4_NATIVE_ENTER(env, that, gdk_1content_1formats_1get_1gtypes_FUNC);
if (arg1) if ((lparg1 = (*env)->GetLongArrayElements(env, arg1, NULL)) == NULL) goto fail;
rc = (jlong)gdk_content_formats_get_gtypes((GdkContentFormats *)arg0, (gsize *)lparg1);
fail:
if (arg1 && lparg1) (*env)->ReleaseLongArrayElements(env, arg1, lparg1, 0);
GTK4_NATIVE_EXIT(env, that, gdk_1content_1formats_1get_1gtypes_FUNC);
return rc;
}
#endif

#ifndef NO_gdk_1content_1formats_1to_1string
JNIEXPORT jlong JNICALL GTK4_NATIVE(gdk_1content_1formats_1to_1string)
(JNIEnv *env, jclass that, jlong arg0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef enum {
gdk_1content_1formats_1builder_1add_1mime_1type_FUNC,
gdk_1content_1formats_1builder_1free_1to_1formats_FUNC,
gdk_1content_1formats_1builder_1new_FUNC,
gdk_1content_1formats_1get_1gtypes_FUNC,
gdk_1content_1formats_1to_1string_FUNC,
gdk_1content_1provider_1get_1value_FUNC,
gdk_1content_1provider_1new_1for_1value_FUNC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,11 @@ public class GTK4 {
public static final native long gdk_content_provider_new_union(long[] providers, int n_providers);
/** @param formats cast=(GdkContentFormats *) */
public static final native long gdk_content_formats_to_string(long formats);
/**
* @param formats cast=(GdkContentFormats *)
* @param n_gtypes cast=(gsize *)
*/
public static final native long gdk_content_formats_get_gtypes(long formats, long[] n_gtypes);

public static final native long gtk_gesture_rotate_new();

Expand Down
Loading