Button: implement setCompoundDrawables()

This commit is contained in:
Julian Winkler
2025-02-18 22:09:02 +01:00
parent 161904c8a8
commit 77fec3c9e6
4 changed files with 48 additions and 5 deletions

View File

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

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

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