From 77fec3c9e6283207f1af07fe374d708da916fff9 Mon Sep 17 00:00:00 2001 From: Julian Winkler Date: Tue, 18 Feb 2025 22:09:02 +0100 Subject: [PATCH] Button: implement setCompoundDrawables() --- .../generated_headers/android_widget_Button.h | 8 +++++ .../widgets/android_widget_Button.c | 34 ++++++++++++++++--- src/api-impl/android/widget/Button.java | 8 ++++- .../android/widget/CompoundButton.java | 3 ++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/api-impl-jni/generated_headers/android_widget_Button.h b/src/api-impl-jni/generated_headers/android_widget_Button.h index d0e629f4..2ed25898 100644 --- a/src/api-impl-jni/generated_headers/android_widget_Button.h +++ b/src/api-impl-jni/generated_headers/android_widget_Button.h @@ -223,6 +223,14 @@ JNIEXPORT void JNICALL Java_android_widget_Button_native_1setText JNIEXPORT void JNICALL Java_android_widget_Button_nativeSetOnClickListener (JNIEnv *, jobject, jlong); +/* + * Class: android_widget_Button + * Method: native_setCompoundDrawables + * Signature: (JJ)V + */ +JNIEXPORT void JNICALL Java_android_widget_Button_native_1setCompoundDrawables + (JNIEnv *, jobject, jlong, jlong); + /* * Class: android_widget_Button * Method: getText diff --git a/src/api-impl-jni/widgets/android_widget_Button.c b/src/api-impl-jni/widgets/android_widget_Button.c index f8dd71b4..a778943b 100644 --- a/src/api-impl-jni/widgets/android_widget_Button.c +++ b/src/api-impl-jni/widgets/android_widget_Button.c @@ -7,17 +7,28 @@ #include "../generated_headers/android_widget_Button.h" +static GtkLabel *box_get_label(JNIEnv *env, GtkWidget *box) +{ + GtkWidget *label = gtk_widget_get_last_child(GTK_WIDGET(box)); + if (!GTK_IS_LABEL(label)) + label = gtk_widget_get_prev_sibling(label); + return GTK_LABEL(label); +} + JNIEXPORT jlong JNICALL Java_android_widget_Button_native_1constructor(JNIEnv *env, jobject this, jobject context, jobject attrs) { const char *text = attribute_set_get_string(env, attrs, "text", NULL); GtkWidget *wrapper = g_object_ref(wrapper_widget_new()); - GtkWidget *label = gtk_button_new_with_label(text); - wrapper_widget_set_child(WRAPPER_WIDGET(wrapper), label); + GtkWidget *button = gtk_button_new(); + GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); + gtk_button_set_child(GTK_BUTTON(button), box); + gtk_box_append(GTK_BOX(box), gtk_label_new(text)); + wrapper_widget_set_child(WRAPPER_WIDGET(wrapper), button); wrapper_widget_consume_touch_events(WRAPPER_WIDGET(wrapper)); // Android button consumes touch events wrapper_widget_set_jobject(WRAPPER_WIDGET(wrapper), env, this); - return _INTPTR(label); + return _INTPTR(button); } JNIEXPORT void JNICALL Java_android_widget_Button_native_1setText(JNIEnv *env, jobject this, jlong widget_ptr, jobject text) @@ -25,6 +36,7 @@ JNIEXPORT void JNICALL Java_android_widget_Button_native_1setText(JNIEnv *env, j GtkButton *button = GTK_BUTTON(_PTR(widget_ptr)); const char *nativeText = ((*env)->GetStringUTFChars(env, text, NULL)); + gtk_label_set_text(box_get_label(env, gtk_button_get_child(button)), nativeText); gtk_button_set_label(button, nativeText); ((*env)->ReleaseStringUTFChars(env, text, nativeText)); } @@ -50,5 +62,19 @@ JNIEXPORT void JNICALL Java_android_widget_Button_nativeSetOnClickListener(JNIEn JNIEXPORT jobject JNICALL Java_android_widget_Button_getText(JNIEnv *env, jobject this) { GtkButton *button = GTK_BUTTON(_PTR(_GET_LONG_FIELD(this, "widget"))); - return (*env)->NewStringUTF(env, gtk_button_get_label(button)); + return (*env)->NewStringUTF(env, gtk_label_get_text(box_get_label(env, gtk_button_get_child(button)))); +} + +JNIEXPORT void JNICALL Java_android_widget_Button_native_1setCompoundDrawables(JNIEnv *env, jobject this, jlong widget_ptr, jlong paintable_ptr) +{ + GtkButton *button = GTK_BUTTON(_PTR(widget_ptr)); + GdkPaintable *paintable = GDK_PAINTABLE(_PTR(paintable_ptr)); + GtkWidget *box = gtk_button_get_child(button); + GtkWidget *picture = gtk_widget_get_first_child(box); + if (GTK_IS_PICTURE(picture)) { + gtk_picture_set_paintable(GTK_PICTURE(picture), paintable); + } else if (paintable) { + picture = gtk_picture_new_for_paintable(paintable); + gtk_widget_insert_after(picture, box, NULL); + } } diff --git a/src/api-impl/android/widget/Button.java b/src/api-impl/android/widget/Button.java index 9cf8ce4b..9c276b67 100644 --- a/src/api-impl/android/widget/Button.java +++ b/src/api-impl/android/widget/Button.java @@ -7,6 +7,8 @@ import android.util.AttributeSet; public class Button extends TextView { + private Drawable compoundDrawableLeft; + public Button(Context context) { this(context, null); } @@ -38,6 +40,7 @@ public class Button extends TextView { public native final void native_setText(long widget, String text); @Override protected native void nativeSetOnClickListener(long widget); + protected native void native_setCompoundDrawables(long widget, long paintable); @Override public void setText(CharSequence text) { @@ -51,6 +54,9 @@ public class Button extends TextView { public void setTextSize(float size) {} @Override - public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {} + public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) { + compoundDrawableLeft = left; + native_setCompoundDrawables(widget, left != null ? left.paintable : 0); + } } diff --git a/src/api-impl/android/widget/CompoundButton.java b/src/api-impl/android/widget/CompoundButton.java index ec1f3362..2aeb88de 100644 --- a/src/api-impl/android/widget/CompoundButton.java +++ b/src/api-impl/android/widget/CompoundButton.java @@ -72,4 +72,7 @@ public abstract class CompoundButton extends Button implements Checkable { public ColorStateList getButtonTintList() { return null; } + + @Override + public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {} }