changeset 2519:551313c064f2

Android: Implement ListBox with ListView and fill in the missing listbox functions. Multiple selection doesn't seem to be working, so might need some more work.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 08 May 2021 21:34:55 +0000
parents c4e90a623437
children 167af4b0004b
files android/DWindows.kt android/dw.cpp
diffstat 2 files changed, 140 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Sat May 08 08:43:47 2021 +0000
+++ b/android/DWindows.kt	Sat May 08 21:34:55 2021 +0000
@@ -134,6 +134,34 @@
     )
 }
 
+class DWListBox(context: Context) : ListView(context), OnItemClickListener {
+    var list = mutableListOf<String>()
+    var selected: Int = -1
+
+    init {
+        setAdapter(
+            ArrayAdapter(
+                context,
+                android.R.layout.simple_list_item_1, list
+            )
+        )
+        setOnItemClickListener(this)
+    }
+
+    override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
+        selected = position
+        eventHandlerInt(11, position, 0, 0, 0)
+    }
+
+    external fun eventHandlerInt(
+        message: Int,
+        inta: Int,
+        intb: Int,
+        intc: Int,
+        intd: Int
+    )
+}
+
 class DWindows : AppCompatActivity() {
     var firstWindow: Boolean = true
     var windowLayout: LinearLayout? = null
@@ -1047,6 +1075,23 @@
         return combobox
     }
 
+    fun listBoxNew(cid: Int, multi: Int): DWListBox?
+    {
+        var listbox: DWListBox? = null
+
+        waitOnUiThread {
+            var dataArrayMap = SimpleArrayMap<String, Long>()
+
+            listbox = DWListBox(this)
+            listbox!!.tag = dataArrayMap
+            listbox!!.id = cid
+            if(multi != 0) {
+                listbox!!.choiceMode = ListView.CHOICE_MODE_MULTIPLE;
+            }
+        }
+        return listbox
+    }
+
     fun listOrComboBoxAppend(window: View, text: String)
     {
         waitOnUiThread {
@@ -1054,6 +1099,10 @@
                 val combobox = window
 
                 combobox.list.add(text)
+            } else if(window is DWListBox) {
+                val listbox = window
+
+                listbox.list.add(text)
             }
         }
     }
@@ -1065,6 +1114,10 @@
                 val combobox = window
 
                 combobox.list.add(pos, text)
+            } else if(window is DWListBox) {
+                val listbox = window
+
+                listbox.list.add(pos, text)
             }
         }
     }
@@ -1076,6 +1129,10 @@
                 val combobox = window
 
                 combobox.list.clear()
+            } else if(window is DWListBox) {
+                val listbox = window
+
+                listbox.list.clear()
             }
         }
     }
@@ -1089,6 +1146,10 @@
                 val combobox = window
 
                 retval = combobox.list.count()
+            } else if(window is DWListBox) {
+                val listbox = window
+
+                retval = listbox.list.count()
             }
         }
         return retval
@@ -1102,6 +1163,11 @@
 
                 if(index < combobox.list.count())
                     combobox.list[index] = text
+            } else if(window is DWListBox) {
+                val listbox = window
+
+                if(index < listbox.list.count())
+                    listbox.list[index] = text
             }
         }
     }
@@ -1116,6 +1182,11 @@
 
                 if(index < combobox.list.count())
                     retval = combobox.list[index]
+            } else if(window is DWListBox) {
+                val listbox = window
+
+                if(index < listbox.list.count())
+                    retval = listbox.list[index]
             }
         }
         return retval
@@ -1130,6 +1201,10 @@
                 val combobox = window
 
                 retval = combobox.selected
+            } else if(window is DWListBox) {
+                val listbox = window
+
+                retval = listbox.selected
             }
         }
         return retval
@@ -1145,6 +1220,17 @@
                     combobox.selected = index
                     combobox.setText(combobox.list[index])
                 }
+            } else if(window is DWListBox) {
+                val listbox = window
+
+                if(index < listbox.list.count()) {
+                    if(state != 0) {
+                        listbox.selected = index
+                        listbox.setItemChecked(index, true);
+                    } else {
+                        listbox.setItemChecked(index, false);
+                    }
+                }
             }
         }
     }
@@ -1158,6 +1244,25 @@
                 if(index < combobox.list.count()) {
                     combobox.list.removeAt(index)
                 }
+            } else if(window is DWListBox) {
+                val listbox = window
+
+                if(index < listbox.list.count()) {
+                    listbox.list.removeAt(index)
+                }
+            }
+        }
+    }
+
+    fun listSetTop(window: View, top: Int)
+    {
+        waitOnUiThread {
+            if(window is DWListBox) {
+                val listbox = window
+
+                if(top < listbox.list.count()) {
+                    listbox.smoothScrollToPosition(top)
+                }
             }
         }
     }
--- a/android/dw.cpp	Sat May 08 08:43:47 2021 +0000
+++ b/android/dw.cpp	Sat May 08 21:34:55 2021 +0000
@@ -433,6 +433,16 @@
     _dw_event_handler(obj, params, message);
 }
 
+JNIEXPORT void JNICALL
+Java_org_dbsoft_dwindows_DWListBox_eventHandlerInt(JNIEnv* env, jobject obj, jint message,
+                                                    jint inta, jint intb, jint intc, jint intd) {
+    void *params[8] = { NULL, NULL, NULL,
+                        DW_INT_TO_POINTER(inta), DW_INT_TO_POINTER(intb),
+                        DW_INT_TO_POINTER(intc), DW_INT_TO_POINTER(intd), NULL };
+
+    _dw_event_handler(obj, params, message);
+}
+
 /* Handler for Timer events */
 JNIEXPORT jint JNICALL
 Java_org_dbsoft_dwindows_DWindows_eventHandlerTimer(JNIEnv* env, jobject obj, jlong sigfunc, jlong data) {
@@ -1517,6 +1527,19 @@
  */
 HWND API dw_listbox_new(ULONG cid, int multi)
 {
+    JNIEnv *env;
+
+    if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // First get the class that contains the method you need to call
+        jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID listBoxNew = env->GetMethodID(clazz, "listBoxNew",
+                                                 "(II)Lorg/dbsoft/dwindows/DWListBox;");
+        // Call the method on the object
+        jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, listBoxNew, (int)cid, multi));
+        return result;
+    }
     return 0;
 }
 
@@ -1639,6 +1662,18 @@
  */
 void API dw_listbox_set_top(HWND handle, int top)
 {
+    JNIEnv *env;
+
+    if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // First get the class that contains the method you need to call
+        jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID listSetTop = env->GetMethodID(clazz, "listSetTop",
+                                                         "(Landroid/view/View;I)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, listSetTop, handle, top);
+    }
 }
 
 /*