implement android.widget.Spinner using GtkDropDown

Helper classes from ListView implementation are reused. The declarations
have been moved to a new header file AdaperView.h
This commit is contained in:
Julian Winkler
2024-03-20 22:14:34 +01:00
parent dda3063e79
commit 7ee4effb86
7 changed files with 382 additions and 17 deletions

View File

@@ -114,6 +114,7 @@ libtranslationlayer_so = shared_library('translation_layer_main', [
'src/api-impl-jni/widgets/WrapperWidget.c', 'src/api-impl-jni/widgets/WrapperWidget.c',
'src/api-impl-jni/widgets/android_view_SurfaceView.c', 'src/api-impl-jni/widgets/android_view_SurfaceView.c',
'src/api-impl-jni/widgets/android_widget_AbsListView.c', 'src/api-impl-jni/widgets/android_widget_AbsListView.c',
'src/api-impl-jni/widgets/android_widget_AbsSpinner.c',
'src/api-impl-jni/widgets/android_widget_Button.c', 'src/api-impl-jni/widgets/android_widget_Button.c',
'src/api-impl-jni/widgets/android_widget_CheckBox.c', 'src/api-impl-jni/widgets/android_widget_CheckBox.c',
'src/api-impl-jni/widgets/android_widget_CompoundButton.c', 'src/api-impl-jni/widgets/android_widget_CompoundButton.c',

View File

@@ -0,0 +1,229 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class android_widget_AbsSpinner */
#ifndef _Included_android_widget_AbsSpinner
#define _Included_android_widget_AbsSpinner
#ifdef __cplusplus
extern "C" {
#endif
#undef android_widget_AbsSpinner_NO_ID
#define android_widget_AbsSpinner_NO_ID -1L
#undef android_widget_AbsSpinner_NOT_FOCUSABLE
#define android_widget_AbsSpinner_NOT_FOCUSABLE 0L
#undef android_widget_AbsSpinner_FOCUSABLE
#define android_widget_AbsSpinner_FOCUSABLE 1L
#undef android_widget_AbsSpinner_FOCUSABLE_MASK
#define android_widget_AbsSpinner_FOCUSABLE_MASK 1L
#undef android_widget_AbsSpinner_FITS_SYSTEM_WINDOWS
#define android_widget_AbsSpinner_FITS_SYSTEM_WINDOWS 2L
#undef android_widget_AbsSpinner_VISIBLE
#define android_widget_AbsSpinner_VISIBLE 0L
#undef android_widget_AbsSpinner_INVISIBLE
#define android_widget_AbsSpinner_INVISIBLE 4L
#undef android_widget_AbsSpinner_GONE
#define android_widget_AbsSpinner_GONE 8L
#undef android_widget_AbsSpinner_VISIBILITY_MASK
#define android_widget_AbsSpinner_VISIBILITY_MASK 12L
#undef android_widget_AbsSpinner_ENABLED
#define android_widget_AbsSpinner_ENABLED 0L
#undef android_widget_AbsSpinner_DISABLED
#define android_widget_AbsSpinner_DISABLED 32L
#undef android_widget_AbsSpinner_ENABLED_MASK
#define android_widget_AbsSpinner_ENABLED_MASK 32L
#undef android_widget_AbsSpinner_WILL_NOT_DRAW
#define android_widget_AbsSpinner_WILL_NOT_DRAW 128L
#undef android_widget_AbsSpinner_DRAW_MASK
#define android_widget_AbsSpinner_DRAW_MASK 128L
#undef android_widget_AbsSpinner_SCROLLBARS_NONE
#define android_widget_AbsSpinner_SCROLLBARS_NONE 0L
#undef android_widget_AbsSpinner_SCROLLBARS_HORIZONTAL
#define android_widget_AbsSpinner_SCROLLBARS_HORIZONTAL 256L
#undef android_widget_AbsSpinner_SCROLLBARS_VERTICAL
#define android_widget_AbsSpinner_SCROLLBARS_VERTICAL 512L
#undef android_widget_AbsSpinner_SCROLLBARS_MASK
#define android_widget_AbsSpinner_SCROLLBARS_MASK 768L
#undef android_widget_AbsSpinner_FILTER_TOUCHES_WHEN_OBSCURED
#define android_widget_AbsSpinner_FILTER_TOUCHES_WHEN_OBSCURED 1024L
#undef android_widget_AbsSpinner_OPTIONAL_FITS_SYSTEM_WINDOWS
#define android_widget_AbsSpinner_OPTIONAL_FITS_SYSTEM_WINDOWS 2048L
#undef android_widget_AbsSpinner_FADING_EDGE_NONE
#define android_widget_AbsSpinner_FADING_EDGE_NONE 0L
#undef android_widget_AbsSpinner_FADING_EDGE_HORIZONTAL
#define android_widget_AbsSpinner_FADING_EDGE_HORIZONTAL 4096L
#undef android_widget_AbsSpinner_FADING_EDGE_VERTICAL
#define android_widget_AbsSpinner_FADING_EDGE_VERTICAL 8192L
#undef android_widget_AbsSpinner_FADING_EDGE_MASK
#define android_widget_AbsSpinner_FADING_EDGE_MASK 12288L
#undef android_widget_AbsSpinner_CLICKABLE
#define android_widget_AbsSpinner_CLICKABLE 16384L
#undef android_widget_AbsSpinner_DRAWING_CACHE_ENABLED
#define android_widget_AbsSpinner_DRAWING_CACHE_ENABLED 32768L
#undef android_widget_AbsSpinner_SAVE_DISABLED
#define android_widget_AbsSpinner_SAVE_DISABLED 65536L
#undef android_widget_AbsSpinner_SAVE_DISABLED_MASK
#define android_widget_AbsSpinner_SAVE_DISABLED_MASK 65536L
#undef android_widget_AbsSpinner_WILL_NOT_CACHE_DRAWING
#define android_widget_AbsSpinner_WILL_NOT_CACHE_DRAWING 131072L
#undef android_widget_AbsSpinner_FOCUSABLE_IN_TOUCH_MODE
#define android_widget_AbsSpinner_FOCUSABLE_IN_TOUCH_MODE 262144L
#undef android_widget_AbsSpinner_DRAWING_CACHE_QUALITY_LOW
#define android_widget_AbsSpinner_DRAWING_CACHE_QUALITY_LOW 524288L
#undef android_widget_AbsSpinner_DRAWING_CACHE_QUALITY_HIGH
#define android_widget_AbsSpinner_DRAWING_CACHE_QUALITY_HIGH 1048576L
#undef android_widget_AbsSpinner_DRAWING_CACHE_QUALITY_AUTO
#define android_widget_AbsSpinner_DRAWING_CACHE_QUALITY_AUTO 0L
#undef android_widget_AbsSpinner_DRAWING_CACHE_QUALITY_MASK
#define android_widget_AbsSpinner_DRAWING_CACHE_QUALITY_MASK 1572864L
#undef android_widget_AbsSpinner_LONG_CLICKABLE
#define android_widget_AbsSpinner_LONG_CLICKABLE 2097152L
#undef android_widget_AbsSpinner_DUPLICATE_PARENT_STATE
#define android_widget_AbsSpinner_DUPLICATE_PARENT_STATE 4194304L
#undef android_widget_AbsSpinner_SCROLLBARS_INSIDE_OVERLAY
#define android_widget_AbsSpinner_SCROLLBARS_INSIDE_OVERLAY 0L
#undef android_widget_AbsSpinner_SCROLLBARS_INSIDE_INSET
#define android_widget_AbsSpinner_SCROLLBARS_INSIDE_INSET 16777216L
#undef android_widget_AbsSpinner_SCROLLBARS_OUTSIDE_OVERLAY
#define android_widget_AbsSpinner_SCROLLBARS_OUTSIDE_OVERLAY 33554432L
#undef android_widget_AbsSpinner_SCROLLBARS_OUTSIDE_INSET
#define android_widget_AbsSpinner_SCROLLBARS_OUTSIDE_INSET 50331648L
#undef android_widget_AbsSpinner_SCROLLBARS_INSET_MASK
#define android_widget_AbsSpinner_SCROLLBARS_INSET_MASK 16777216L
#undef android_widget_AbsSpinner_SCROLLBARS_OUTSIDE_MASK
#define android_widget_AbsSpinner_SCROLLBARS_OUTSIDE_MASK 33554432L
#undef android_widget_AbsSpinner_SCROLLBARS_STYLE_MASK
#define android_widget_AbsSpinner_SCROLLBARS_STYLE_MASK 50331648L
#undef android_widget_AbsSpinner_KEEP_SCREEN_ON
#define android_widget_AbsSpinner_KEEP_SCREEN_ON 67108864L
#undef android_widget_AbsSpinner_SOUND_EFFECTS_ENABLED
#define android_widget_AbsSpinner_SOUND_EFFECTS_ENABLED 134217728L
#undef android_widget_AbsSpinner_HAPTIC_FEEDBACK_ENABLED
#define android_widget_AbsSpinner_HAPTIC_FEEDBACK_ENABLED 268435456L
#undef android_widget_AbsSpinner_PARENT_SAVE_DISABLED
#define android_widget_AbsSpinner_PARENT_SAVE_DISABLED 536870912L
#undef android_widget_AbsSpinner_PARENT_SAVE_DISABLED_MASK
#define android_widget_AbsSpinner_PARENT_SAVE_DISABLED_MASK 536870912L
#undef android_widget_AbsSpinner_FOCUSABLES_ALL
#define android_widget_AbsSpinner_FOCUSABLES_ALL 0L
#undef android_widget_AbsSpinner_FOCUSABLES_TOUCH_MODE
#define android_widget_AbsSpinner_FOCUSABLES_TOUCH_MODE 1L
#undef android_widget_AbsSpinner_FOCUS_BACKWARD
#define android_widget_AbsSpinner_FOCUS_BACKWARD 1L
#undef android_widget_AbsSpinner_FOCUS_FORWARD
#define android_widget_AbsSpinner_FOCUS_FORWARD 2L
#undef android_widget_AbsSpinner_FOCUS_LEFT
#define android_widget_AbsSpinner_FOCUS_LEFT 17L
#undef android_widget_AbsSpinner_FOCUS_UP
#define android_widget_AbsSpinner_FOCUS_UP 33L
#undef android_widget_AbsSpinner_FOCUS_RIGHT
#define android_widget_AbsSpinner_FOCUS_RIGHT 66L
#undef android_widget_AbsSpinner_FOCUS_DOWN
#define android_widget_AbsSpinner_FOCUS_DOWN 130L
#undef android_widget_AbsSpinner_MEASURED_SIZE_MASK
#define android_widget_AbsSpinner_MEASURED_SIZE_MASK 16777215L
#undef android_widget_AbsSpinner_MEASURED_STATE_MASK
#define android_widget_AbsSpinner_MEASURED_STATE_MASK -16777216L
#undef android_widget_AbsSpinner_MEASURED_HEIGHT_STATE_SHIFT
#define android_widget_AbsSpinner_MEASURED_HEIGHT_STATE_SHIFT 16L
#undef android_widget_AbsSpinner_MEASURED_STATE_TOO_SMALL
#define android_widget_AbsSpinner_MEASURED_STATE_TOO_SMALL 16777216L
#undef android_widget_AbsSpinner_PFLAG2_DRAG_CAN_ACCEPT
#define android_widget_AbsSpinner_PFLAG2_DRAG_CAN_ACCEPT 1L
#undef android_widget_AbsSpinner_PFLAG2_DRAG_HOVERED
#define android_widget_AbsSpinner_PFLAG2_DRAG_HOVERED 2L
#undef android_widget_AbsSpinner_LAYOUT_DIRECTION_LTR
#define android_widget_AbsSpinner_LAYOUT_DIRECTION_LTR 0L
#undef android_widget_AbsSpinner_LAYOUT_DIRECTION_RTL
#define android_widget_AbsSpinner_LAYOUT_DIRECTION_RTL 1L
#undef android_widget_AbsSpinner_LAYOUT_DIRECTION_INHERIT
#define android_widget_AbsSpinner_LAYOUT_DIRECTION_INHERIT 2L
#undef android_widget_AbsSpinner_LAYOUT_DIRECTION_LOCALE
#define android_widget_AbsSpinner_LAYOUT_DIRECTION_LOCALE 3L
#undef android_widget_AbsSpinner_PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT
#define android_widget_AbsSpinner_PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT 2L
#undef android_widget_AbsSpinner_PFLAG2_LAYOUT_DIRECTION_MASK
#define android_widget_AbsSpinner_PFLAG2_LAYOUT_DIRECTION_MASK 12L
#undef android_widget_AbsSpinner_PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL
#define android_widget_AbsSpinner_PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL 16L
#undef android_widget_AbsSpinner_PFLAG2_LAYOUT_DIRECTION_RESOLVED
#define android_widget_AbsSpinner_PFLAG2_LAYOUT_DIRECTION_RESOLVED 32L
#undef android_widget_AbsSpinner_PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK
#define android_widget_AbsSpinner_PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK 48L
#undef android_widget_AbsSpinner_STATUS_BAR_HIDDEN
#define android_widget_AbsSpinner_STATUS_BAR_HIDDEN 1L
#undef android_widget_AbsSpinner_STATUS_BAR_VISIBLE
#define android_widget_AbsSpinner_STATUS_BAR_VISIBLE 0L
#undef android_widget_AbsSpinner_SYSTEM_UI_FLAG_FULLSCREEN
#define android_widget_AbsSpinner_SYSTEM_UI_FLAG_FULLSCREEN 4L
#undef android_widget_AbsSpinner_SYSTEM_UI_FLAG_HIDE_NAVIGATION
#define android_widget_AbsSpinner_SYSTEM_UI_FLAG_HIDE_NAVIGATION 2L
#undef android_widget_AbsSpinner_SYSTEM_UI_FLAG_IMMERSIVE
#define android_widget_AbsSpinner_SYSTEM_UI_FLAG_IMMERSIVE 2048L
#undef android_widget_AbsSpinner_SYSTEM_UI_FLAG_IMMERSIVE_STICKY
#define android_widget_AbsSpinner_SYSTEM_UI_FLAG_IMMERSIVE_STICKY 4096L
#undef android_widget_AbsSpinner_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
#define android_widget_AbsSpinner_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 1024L
#undef android_widget_AbsSpinner_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
#define android_widget_AbsSpinner_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 512L
#undef android_widget_AbsSpinner_SYSTEM_UI_FLAG_LAYOUT_STABLE
#define android_widget_AbsSpinner_SYSTEM_UI_FLAG_LAYOUT_STABLE 256L
#undef android_widget_AbsSpinner_SYSTEM_UI_FLAG_LOW_PROFILE
#define android_widget_AbsSpinner_SYSTEM_UI_FLAG_LOW_PROFILE 1L
#undef android_widget_AbsSpinner_SYSTEM_UI_FLAG_VISIBLE
#define android_widget_AbsSpinner_SYSTEM_UI_FLAG_VISIBLE 0L
#undef android_widget_AbsSpinner_SYSTEM_UI_LAYOUT_FLAGS
#define android_widget_AbsSpinner_SYSTEM_UI_LAYOUT_FLAGS 1536L
#undef android_widget_AbsSpinner_TEXT_ALIGNMENT_CENTER
#define android_widget_AbsSpinner_TEXT_ALIGNMENT_CENTER 4L
#undef android_widget_AbsSpinner_TEXT_ALIGNMENT_GRAVITY
#define android_widget_AbsSpinner_TEXT_ALIGNMENT_GRAVITY 1L
#undef android_widget_AbsSpinner_TEXT_ALIGNMENT_INHERIT
#define android_widget_AbsSpinner_TEXT_ALIGNMENT_INHERIT 0L
#undef android_widget_AbsSpinner_TEXT_ALIGNMENT_TEXT_END
#define android_widget_AbsSpinner_TEXT_ALIGNMENT_TEXT_END 3L
#undef android_widget_AbsSpinner_TEXT_ALIGNMENT_TEXT_START
#define android_widget_AbsSpinner_TEXT_ALIGNMENT_TEXT_START 2L
#undef android_widget_AbsSpinner_TEXT_ALIGNMENT_VIEW_END
#define android_widget_AbsSpinner_TEXT_ALIGNMENT_VIEW_END 6L
#undef android_widget_AbsSpinner_TEXT_ALIGNMENT_VIEW_START
#define android_widget_AbsSpinner_TEXT_ALIGNMENT_VIEW_START 5L
#undef android_widget_AbsSpinner_TEXT_DIRECTION_ANY_RTL
#define android_widget_AbsSpinner_TEXT_DIRECTION_ANY_RTL 2L
#undef android_widget_AbsSpinner_TEXT_DIRECTION_FIRST_STRONG
#define android_widget_AbsSpinner_TEXT_DIRECTION_FIRST_STRONG 1L
#undef android_widget_AbsSpinner_TEXT_DIRECTION_INHERIT
#define android_widget_AbsSpinner_TEXT_DIRECTION_INHERIT 0L
#undef android_widget_AbsSpinner_TEXT_DIRECTION_LOCALE
#define android_widget_AbsSpinner_TEXT_DIRECTION_LOCALE 5L
#undef android_widget_AbsSpinner_TEXT_DIRECTION_LTR
#define android_widget_AbsSpinner_TEXT_DIRECTION_LTR 3L
#undef android_widget_AbsSpinner_TEXT_DIRECTION_RTL
#define android_widget_AbsSpinner_TEXT_DIRECTION_RTL 4L
/*
* Class: android_widget_AbsSpinner
* Method: native_constructor
* Signature: (Landroid/content/Context;Landroid/util/AttributeSet;)J
*/
JNIEXPORT jlong JNICALL Java_android_widget_AbsSpinner_native_1constructor
(JNIEnv *, jobject, jobject, jobject);
/*
* Class: android_widget_AbsSpinner
* Method: native_setAdapter
* Signature: (JLandroid/widget/SpinnerAdapter;)V
*/
JNIEXPORT void JNICALL Java_android_widget_AbsSpinner_native_1setAdapter
(JNIEnv *, jobject, jlong, jobject);
/*
* Class: android_widget_AbsSpinner
* Method: setOnItemSelectedListener
* Signature: (Landroid/widget/AdapterView/OnItemSelectedListener;)V
*/
JNIEXPORT void JNICALL Java_android_widget_AbsSpinner_setOnItemSelectedListener
(JNIEnv *, jobject, jobject);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,31 @@
#ifndef _ADAPTER_VIEW_H_
#define _ADAPTER_VIEW_H_
#include <gtk/gtk.h>
#include <jni.h>
/*
* RangeListModel:
* Implementation of GListModel for use in AdapterView subclasses.
* Provides RangeListItems, which are created on demand.
*/
struct _RangeListModel {
GObject parent_instance;
GtkWidget *list_view;
jobject jobject;
jobject adapter;
guint n_items;
};
G_DECLARE_FINAL_TYPE(RangeListModel, range_list_model, RANGE, LIST_MODEL, GObject)
/*
* RangeListItem:
* Dummy type to be returned by RangeListModel. Contains nothing, but a reference to the RangeListModel.
*/
struct _RangeListItem {
GObject parent_instance;
RangeListModel *model;
};
G_DECLARE_FINAL_TYPE(RangeListItem, range_list_item, RANGE, LIST_ITEM, GObject)
#endif // _ADAPTER_VIEW_H_

View File

@@ -6,17 +6,10 @@
#include "../util.h" #include "../util.h"
#include "WrapperWidget.h" #include "WrapperWidget.h"
#include "AdapterView.h"
#include "../generated_headers/android_widget_AbsListView.h" #include "../generated_headers/android_widget_AbsListView.h"
struct _RangeListModel {
GObject parent_instance;
GtkListView *list_view;
jobject jobject;
jobject adapter;
guint n_items;
};
G_DECLARE_FINAL_TYPE(RangeListModel, range_list_model, RANGE, LIST_MODEL, GObject)
static void range_list_model_init(RangeListModel *list_model) {} static void range_list_model_init(RangeListModel *list_model) {}
static void range_list_model_class_init(RangeListModelClass *class) {} static void range_list_model_class_init(RangeListModelClass *class) {}
@@ -27,24 +20,32 @@ static guint range_list_model_get_n_items(GListModel *list_model)
static gpointer range_list_model_get_item(GListModel *list_model, guint index) static gpointer range_list_model_get_item(GListModel *list_model, guint index)
{ {
return g_object_ref(list_model); if (index >= RANGE_LIST_MODEL(list_model)->n_items)
return NULL;
RangeListItem *item = g_object_new(range_list_item_get_type(), NULL);
item->model = RANGE_LIST_MODEL(list_model);
return item;
} }
static void range_list_model_model_init(GListModelInterface *iface) static void range_list_model_model_init(GListModelInterface *iface)
{ {
iface->get_n_items = range_list_model_get_n_items; iface->get_n_items = range_list_model_get_n_items;
iface->get_item_type = (GType (*)(GListModel *))range_list_model_get_type; iface->get_item_type = (GType (*)(GListModel *))range_list_item_get_type;
iface->get_item = range_list_model_get_item; iface->get_item = range_list_model_get_item;
} }
G_DEFINE_TYPE_WITH_CODE(RangeListModel, range_list_model, G_TYPE_OBJECT, G_DEFINE_TYPE_WITH_CODE(RangeListModel, range_list_model, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL, range_list_model_model_init)) G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL, range_list_model_model_init))
static void range_list_item_class_init(RangeListItemClass *cls){}
static void range_list_item_init(RangeListItem *self){}
G_DEFINE_TYPE(RangeListItem, range_list_item, G_TYPE_OBJECT)
static void on_click(GtkGestureClick *gesture, int n_press, double x, double y, GtkListItem *list_item) static void on_click(GtkGestureClick *gesture, int n_press, double x, double y, GtkListItem *list_item)
{ {
JNIEnv *env = get_jni_env(); JNIEnv *env = get_jni_env();
guint position = gtk_list_item_get_position(list_item); guint position = gtk_list_item_get_position(list_item);
RangeListModel *model = RANGE_LIST_MODEL(gtk_list_item_get_item(list_item)); RangeListModel *model = RANGE_LIST_ITEM(gtk_list_item_get_item(list_item))->model;
jobject listener = g_object_get_data(G_OBJECT(model->list_view), "on_click_listener"); jobject listener = g_object_get_data(G_OBJECT(model->list_view), "on_click_listener");
if (!listener) if (!listener)
return; return;
@@ -63,7 +64,7 @@ static void bind_listitem_cb(GtkListItemFactory *factory, GtkListItem *list_item
guint index = gtk_list_item_get_position(list_item); guint index = gtk_list_item_get_position(list_item);
WrapperWidget *wrapper = WRAPPER_WIDGET(gtk_list_item_get_child(list_item)); WrapperWidget *wrapper = WRAPPER_WIDGET(gtk_list_item_get_child(list_item));
RangeListModel *model = RANGE_LIST_MODEL(gtk_list_item_get_item(list_item)); RangeListModel *model = RANGE_LIST_ITEM(gtk_list_item_get_item(list_item))->model;
int n_items = g_list_model_get_n_items(G_LIST_MODEL(model)); int n_items = g_list_model_get_n_items(G_LIST_MODEL(model));
if (index >= n_items) { if (index >= n_items) {
printf("invalid index: %d >= %d\n", index, n_items); printf("invalid index: %d >= %d\n", index, n_items);
@@ -98,7 +99,7 @@ JNIEXPORT jlong JNICALL Java_android_widget_AbsListView_native_1constructor(JNIE
g_signal_connect(factory, "unbind", G_CALLBACK(unbind_listitem_cb), NULL); g_signal_connect(factory, "unbind", G_CALLBACK(unbind_listitem_cb), NULL);
RangeListModel *model = g_object_new(range_list_model_get_type(), NULL); RangeListModel *model = g_object_new(range_list_model_get_type(), NULL);
GtkWidget *list_view = gtk_list_view_new(GTK_SELECTION_MODEL(gtk_single_selection_new(G_LIST_MODEL(model))), factory); GtkWidget *list_view = gtk_list_view_new(GTK_SELECTION_MODEL(gtk_single_selection_new(G_LIST_MODEL(model))), factory);
model->list_view = GTK_LIST_VIEW(list_view); model->list_view = list_view;
model->jobject = _REF(this); model->jobject = _REF(this);
GtkWidget *scrolled_window = gtk_scrolled_window_new(); GtkWidget *scrolled_window = gtk_scrolled_window_new();
gtk_scrolled_window_set_propagate_natural_height(GTK_SCROLLED_WINDOW(scrolled_window), TRUE); gtk_scrolled_window_set_propagate_natural_height(GTK_SCROLLED_WINDOW(scrolled_window), TRUE);

View File

@@ -0,0 +1,71 @@
#include <gtk/gtk.h>
#include "../defines.h"
#include "../util.h"
#include "WrapperWidget.h"
#include "AdapterView.h"
#include "../generated_headers/android_widget_AbsSpinner.h"
static void bind_listitem_cb(GtkListItemFactory *factory, GtkListItem *list_item)
{
JNIEnv *env = get_jni_env();
guint index = gtk_list_item_get_position(list_item);
WrapperWidget *wrapper = WRAPPER_WIDGET(gtk_list_item_get_child(list_item));
RangeListModel *model = RANGE_LIST_ITEM(gtk_list_item_get_item(list_item))->model;
int n_items = g_list_model_get_n_items(G_LIST_MODEL(model));
if (index >= n_items) {
printf("invalid index: %d >= %d\n", index, n_items);
exit(0);
}
jmethodID getView = _METHOD(_CLASS(model->adapter), "getDropDownView", "(ILandroid/view/View;Landroid/view/ViewGroup;)Landroid/view/View;");
jobject view = (*env)->CallObjectMethod(env, model->adapter, getView, index, wrapper ? wrapper->jobj : NULL, model->jobject);
view = _REF(view);
GtkWidget *child = gtk_widget_get_parent(GTK_WIDGET(_PTR(_GET_LONG_FIELD(view, "widget"))));
gtk_list_item_set_child(list_item, child);
}
JNIEXPORT jlong JNICALL Java_android_widget_AbsSpinner_native_1constructor(JNIEnv *env, jobject this, jobject context, jobject attrs)
{
GtkWidget *wrapper = g_object_ref(wrapper_widget_new());
GtkListItemFactory *factory = gtk_signal_list_item_factory_new();
g_signal_connect(factory, "bind", G_CALLBACK(bind_listitem_cb), NULL);
RangeListModel *model = g_object_new(range_list_model_get_type(), NULL);
GtkWidget *dropdown = gtk_drop_down_new(G_LIST_MODEL(model), NULL);
gtk_drop_down_set_factory(GTK_DROP_DOWN(dropdown), factory);
model->list_view = dropdown;
model->jobject = _REF(this);
wrapper_widget_set_child(WRAPPER_WIDGET(wrapper), dropdown);
gtk_widget_set_name(dropdown, "Spinner");
return _INTPTR(dropdown);
}
JNIEXPORT void JNICALL Java_android_widget_AbsSpinner_native_1setAdapter(JNIEnv *env, jobject this, jlong widget_ptr, jobject adapter)
{
GtkDropDown *dropdown = GTK_DROP_DOWN(_PTR(widget_ptr));
RangeListModel *model = RANGE_LIST_MODEL(gtk_drop_down_get_model(dropdown));
if (model->adapter)
_UNREF(model->adapter);
model->adapter = adapter ? _REF(adapter) : NULL;
guint old_n_items = model->n_items;
model->n_items = adapter ? (*env)->CallIntMethod(env, adapter, _METHOD(_CLASS(adapter), "getCount", "()I")) : 0;
g_list_model_items_changed(G_LIST_MODEL(model), 0, old_n_items, model->n_items);
}
static void on_selected_changed(GtkDropDown *dropdown, GParamSpec *pspec, jobject listener)
{
JNIEnv *env = get_jni_env();
int index = gtk_drop_down_get_selected(dropdown);
RangeListModel *model = RANGE_LIST_ITEM(gtk_drop_down_get_selected_item(dropdown))->model;
jmethodID onItemSelected = _METHOD(_CLASS(listener), "onItemSelected", "(Landroid/widget/AdapterView;Landroid/view/View;IJ)V");
(*env)->CallVoidMethod(env, listener, onItemSelected, model->jobject, NULL, index, (long)0);
}
JNIEXPORT void JNICALL Java_android_widget_AbsSpinner_setOnItemSelectedListener(JNIEnv *env, jobject this, jobject listener)
{
GtkDropDown *dropdown = GTK_DROP_DOWN(_PTR(_GET_LONG_FIELD(this, "widget")));
g_signal_connect(dropdown, "notify::selected", G_CALLBACK(on_selected_changed), _REF(listener));
}

View File

@@ -1,16 +1,52 @@
package android.widget; package android.widget;
import android.content.Context; import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet; import android.util.AttributeSet;
public abstract class AbsSpinner extends AdapterView { public abstract class AbsSpinner extends AdapterView {
private Observer observer = new Observer();
public AbsSpinner(Context context) { public AbsSpinner(Context context) {
super(context); super(context);
haveCustomMeasure = false;
} }
public AbsSpinner(Context context, AttributeSet attributeSet) { public AbsSpinner(Context context, AttributeSet attributeSet) {
super(context, attributeSet); super(context, attributeSet);
haveCustomMeasure = false;
} }
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
protected native void native_setAdapter(long widget, SpinnerAdapter adapter);
public void setAdapter(SpinnerAdapter adapter) {
SpinnerAdapter oldAdapter = getAdapter();
if (oldAdapter != null)
oldAdapter.unregisterDataSetObserver(observer);
super.setAdapter(adapter);
if (adapter != null)
adapter.registerDataSetObserver(observer);
native_setAdapter(this.widget, adapter);
}
public SpinnerAdapter getAdapter() {
return (SpinnerAdapter) super.getAdapter();
}
@Override
public native void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener);
private class Observer extends DataSetObserver {
@Override
public void onChanged() {
AbsSpinner.this.native_setAdapter(widget, getAdapter());
}
@Override
public void onInvalidated() {
AbsSpinner.this.native_setAdapter(widget, getAdapter());
}
}
} }

View File

@@ -13,8 +13,4 @@ public class Spinner extends AbsSpinner {
super(context, attributeSet); super(context, attributeSet);
} }
public void setAdapter(SpinnerAdapter adapter) {
super.setAdapter(adapter);
}
} }