AlertDialog: handle buttons

This commit is contained in:
Julian Winkler
2024-07-24 22:52:33 +02:00
committed by Mis012
parent fb82eabd2c
commit 7b11277287
3 changed files with 20 additions and 6 deletions

View File

@@ -2,6 +2,7 @@
#include <jni.h>
#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);
}

View File

@@ -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

View File

@@ -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 {