implement TextView.setCompoundDrawables()

This adds an additional GtkBox for every TextView
This commit is contained in:
Julian Winkler
2024-03-30 11:04:21 +01:00
parent 849701c5c4
commit ae58d2b319
7 changed files with 84 additions and 21 deletions

View File

@@ -247,6 +247,14 @@ JNIEXPORT void JNICALL Java_android_widget_TextView_native_1setTextColor
JNIEXPORT jobject JNICALL Java_android_widget_TextView_getText
(JNIEnv *, jobject);
/*
* Class: android_widget_TextView
* Method: native_setCompoundDrawables
* Signature: (JJJJJ)V
*/
JNIEXPORT void JNICALL Java_android_widget_TextView_native_1setCompoundDrawables
(JNIEnv *, jobject, jlong, jlong, jlong, jlong, jlong);
#ifdef __cplusplus
}
#endif

View File

@@ -159,3 +159,8 @@ JNIEXPORT void JNICALL Java_android_graphics_Path_native_1rQuadTo(JNIEnv *env, j
sk_path_rquad_to(path, dx1, dy1, dx2, dy2);
}
JNIEXPORT void JNICALL Java_android_graphics_Path_native_1rMoveTo(JNIEnv *env, jclass class, jlong path_ptr, jfloat dx, jfloat dy)
{
sk_path_t *path = (sk_path_t *)_PTR(path_ptr);
sk_path_rmove_to(path, dx, dy);
}

View File

@@ -9,7 +9,7 @@
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) ?: "FIXME";
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);

View File

@@ -7,43 +7,55 @@
#include "../generated_headers/android_widget_TextView.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_TextView_native_1constructor(JNIEnv *env, jobject this, jobject context, jobject attrs)
{
const char *text = attribute_set_get_string(env, attrs, "text", NULL) ?: "FIXME";
const char *text = attribute_set_get_string(env, attrs, "text", NULL);
// _SET_OBJ_FIELD(this, "text", "Ljava/lang/String;", _JSTRING(text)); //TODO: sadly this might be needed, but it's not atm
GtkWidget *wrapper = g_object_ref(wrapper_widget_new());
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
GtkWidget *label = gtk_label_new(text);
gtk_label_set_wrap(GTK_LABEL(label), TRUE);
gtk_label_set_xalign(GTK_LABEL(label), 0.f);
gtk_label_set_yalign(GTK_LABEL(label), 0.f);
wrapper_widget_set_child(WRAPPER_WIDGET(wrapper), label);
gtk_widget_set_hexpand(label, TRUE);
gtk_box_append(GTK_BOX(box), label);
wrapper_widget_set_child(WRAPPER_WIDGET(wrapper), box);
PangoAttrList* pango_attrs = pango_attr_list_new();
pango_attr_list_insert(pango_attrs, pango_attr_font_features_new("tnum"));
gtk_label_set_attributes(GTK_LABEL(label), pango_attrs);
pango_attr_list_unref(pango_attrs);
return _INTPTR(label);
return _INTPTR(box);
}
JNIEXPORT void JNICALL Java_android_widget_TextView_native_1setText(JNIEnv *env, jobject this, jobject charseq)
{
// _SET_OBJ_FIELD(this, "text", "Ljava/lang/String;", charseq); //TODO: sadly this might be needed, but it's not atm
gtk_label_set_text(GTK_LABEL(_PTR(_GET_LONG_FIELD(this, "widget"))), _CSTRING(charseq));
const char *text = charseq ? (*env)->GetStringUTFChars(env, charseq, NULL) : NULL;
gtk_label_set_text(box_get_label(env, _PTR(_GET_LONG_FIELD(this, "widget"))), text ?: "");
if(text)
(*env)->ReleaseStringUTFChars(env, charseq, text);
}
JNIEXPORT void JNICALL Java_android_widget_TextView_native_1setTextColor(JNIEnv *env, jobject this, jint color)
{
GtkWidget *widget = GTK_WIDGET(_PTR(_GET_LONG_FIELD(this, "widget")));
GtkWidget *widget = GTK_WIDGET(box_get_label(env, _PTR(_GET_LONG_FIELD(this, "widget"))));
GtkStyleContext *style_context = gtk_widget_get_style_context(widget);
GtkCssProvider *old_provider = g_object_get_data(G_OBJECT(widget), "color_style_provider");
if(old_provider)
gtk_style_context_remove_provider(style_context, old_provider);
gtk_style_context_remove_provider(style_context, GTK_STYLE_PROVIDER(old_provider));
GtkCssProvider *css_provider = gtk_css_provider_new();
@@ -57,7 +69,7 @@ JNIEXPORT void JNICALL Java_android_widget_TextView_native_1setTextColor(JNIEnv
JNIEXPORT void JNICALL Java_android_widget_TextView_setTextSize(JNIEnv *env, jobject this, jfloat size)
{
GtkLabel *label = GTK_LABEL(_PTR(_GET_LONG_FIELD(this, "widget")));
GtkLabel *label = box_get_label(env, _PTR(_GET_LONG_FIELD(this, "widget")));
PangoAttrList *attrs;
PangoAttrList *old_attrs = gtk_label_get_attributes(label);
@@ -75,12 +87,36 @@ JNIEXPORT void JNICALL Java_android_widget_TextView_setTextSize(JNIEnv *env, job
JNIEXPORT void JNICALL Java_android_widget_TextView_native_1set_1markup(JNIEnv *env, jobject this, jint value)
{
GtkLabel *label = GTK_LABEL(_PTR(_GET_LONG_FIELD(this, "widget")));
GtkLabel *label = box_get_label(env, _PTR(_GET_LONG_FIELD(this, "widget")));
gtk_label_set_use_markup(label, value);
}
JNIEXPORT jobject JNICALL Java_android_widget_TextView_getText(JNIEnv *env, jobject this)
{
return _JSTRING(gtk_label_get_text(GTK_LABEL(_PTR(_GET_LONG_FIELD(this, "widget")))));
return _JSTRING(gtk_label_get_text(box_get_label(env, _PTR(_GET_LONG_FIELD(this, "widget")))));
}
JNIEXPORT void JNICALL Java_android_widget_TextView_native_1setCompoundDrawables(JNIEnv *env, jobject this, jlong widget_ptr, jlong left, jlong top, jlong right, jlong bottom)
{
GtkWidget *box = GTK_WIDGET(_PTR(widget_ptr));
gtk_orientable_set_orientation(GTK_ORIENTABLE(box), (left || right) ? GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL);
GdkPaintable *paintable = _PTR(left ?: top); // paintable before text
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);
}
paintable = _PTR(right ?: bottom); // paintable after text
picture = gtk_widget_get_last_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_before(picture, box, NULL);
}
}

View File

@@ -2,6 +2,7 @@ package android.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
public class Button extends TextView {
@@ -41,4 +42,7 @@ public class Button extends TextView {
native_setOnClickListener(widget, l);
}
@Override
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {}
}

View File

@@ -1,6 +1,7 @@
package android.widget;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
@@ -46,4 +47,7 @@ public class EditText extends TextView {
public void setOnEditorActionListener(OnEditorActionListener l) {
native_setOnEditorActionListener(widget, l);
}
@Override
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {}
}

View File

@@ -67,16 +67,12 @@ public class TextView extends View {
protected native long native_constructor(Context context, AttributeSet attrs);
public void setText(CharSequence text) {
if (text == null) {
native_setText("NULL");
return;
}
native_setText(text.toString());
native_setText(text != null ? text.toString() : null);
if (text instanceof android.text.Spanned)
native_set_markup(1);
requestLayout();
if (!isLayoutRequested())
requestLayout();
}
public void setText(int resId) {
@@ -158,7 +154,15 @@ public class TextView extends View {
public void setCompoundDrawablePadding(int pad) {}
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {}
protected native void native_setCompoundDrawables(long widget, long left, long top, long right, long bottom);
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
native_setCompoundDrawables(widget,
left != null ? left.paintable : 0,
top != null ? top.paintable : 0,
right != null ? right.paintable : 0,
bottom != null ? bottom.paintable : 0);
}
public void setAllCaps(boolean allCaps) {}
@@ -178,7 +182,9 @@ public class TextView extends View {
public int getMaxLines() {return -1;}
public void setCompoundDrawablesRelative(Drawable start, Drawable top, Drawable end, Drawable bottom) {}
public void setCompoundDrawablesRelative(Drawable start, Drawable top, Drawable end, Drawable bottom) {
setCompoundDrawables(start, top, end, bottom);
}
public int getLineCount() {return 1;}