From 7b11277287f412d38c40d6ffaa195d8db228034e Mon Sep 17 00:00:00 2001 From: Julian Winkler Date: Wed, 24 Jul 2024 22:52:33 +0200 Subject: [PATCH] AlertDialog: handle buttons --- src/api-impl-jni/app/android_app_AlertDialog.c | 18 ++++++++++++++++-- .../android_app_AlertDialog.h | 4 ++-- src/api-impl/android/app/AlertDialog.java | 4 ++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/api-impl-jni/app/android_app_AlertDialog.c b/src/api-impl-jni/app/android_app_AlertDialog.c index 36087afc..06760a9c 100644 --- a/src/api-impl-jni/app/android_app_AlertDialog.c +++ b/src/api-impl-jni/app/android_app_AlertDialog.c @@ -2,6 +2,7 @@ #include #include "../defines.h" +#include "../util.h" #include "../generated_headers/android_app_AlertDialog.h" JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetMessage(JNIEnv *env, jobject this, jlong ptr, jstring message) @@ -15,12 +16,25 @@ JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetMessage(JNIEnv *env (*env)->ReleaseStringUTFChars(env, message, nativeMessage); } -JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetButton(JNIEnv *env, jobject this, jlong ptr, jint id, jstring text) { +static void button_clicked_cb(GtkWidget *button, gpointer user_data) { + JNIEnv *env = get_jni_env(); + jobject listener = user_data; + jobject this = g_object_get_data(G_OBJECT(button), "this_dialog"); + jint which = _INTPTR(g_object_get_data(G_OBJECT(button), "which")); + jmethodID on_click_method = _METHOD(_CLASS(listener), "onClick", "(Landroid/content/DialogInterface;I)V"); + (*env)->CallVoidMethod(env, listener, on_click_method, this, which); + if ((*env)->ExceptionCheck(env)) + (*env)->ExceptionDescribe(env); +} + +JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetButton(JNIEnv *env, jobject this, jlong ptr, jint id, jstring text, jobject listener) { GtkWindow *dialog = GTK_WINDOW(_PTR(ptr)); GtkWidget *content_area = gtk_window_get_child(dialog); const char* nativeText = (*env)->GetStringUTFChars(env, text, NULL); GtkWidget *button = gtk_button_new_with_label(nativeText); - g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_window_destroy), dialog); + g_object_set_data(G_OBJECT(button), "which", _PTR(id)); + g_object_set_data(G_OBJECT(button), "this_dialog", _REF(this)); + g_signal_connect(button, "clicked", G_CALLBACK(button_clicked_cb), _REF(listener)); gtk_box_append(GTK_BOX(content_area), button); (*env)->ReleaseStringUTFChars(env, text, nativeText); } diff --git a/src/api-impl-jni/generated_headers/android_app_AlertDialog.h b/src/api-impl-jni/generated_headers/android_app_AlertDialog.h index 666afa8b..388e9038 100644 --- a/src/api-impl-jni/generated_headers/android_app_AlertDialog.h +++ b/src/api-impl-jni/generated_headers/android_app_AlertDialog.h @@ -18,10 +18,10 @@ JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetMessage /* * Class: android_app_AlertDialog * Method: nativeSetButton - * Signature: (JILjava/lang/String;)V + * Signature: (JILjava/lang/String;Landroid/content/DialogInterface/OnClickListener;)V */ JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetButton - (JNIEnv *, jobject, jlong, jint, jstring); + (JNIEnv *, jobject, jlong, jint, jstring, jobject); /* * Class: android_app_AlertDialog diff --git a/src/api-impl/android/app/AlertDialog.java b/src/api-impl/android/app/AlertDialog.java index ee20c976..5a7ff3ae 100644 --- a/src/api-impl/android/app/AlertDialog.java +++ b/src/api-impl/android/app/AlertDialog.java @@ -7,7 +7,7 @@ import android.view.View; public class AlertDialog extends Dialog implements DialogInterface { private native void nativeSetMessage(long ptr, String message); - private native void nativeSetButton(long ptr, int whichButton, String text); + private native void nativeSetButton(long ptr, int whichButton, String text, OnClickListener listener); private native void nativeSetItems(long ptr, String[] items, DialogInterface.OnClickListener listener); public AlertDialog(Context context) { @@ -24,7 +24,7 @@ public class AlertDialog extends Dialog implements DialogInterface { } public void setButton(int whichButton, CharSequence text, OnClickListener listener) { - nativeSetButton(nativePtr, whichButton, String.valueOf(text)); + nativeSetButton(nativePtr, whichButton, String.valueOf(text), listener); } public static class Builder {