changeset 2517:d746323f2841

Android: Implement most of the dw_listbox_*() functions for ComboBoxes. Eliminate some kotlin code warnings.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 08 May 2021 08:11:51 +0000
parents 8f5d064b7054
children c4e90a623437
files android/DWindows.kt android/dw.cpp
diffstat 2 files changed, 246 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Sat May 08 01:54:52 2021 +0000
+++ b/android/DWindows.kt	Sat May 08 08:11:51 2021 +0000
@@ -87,6 +87,7 @@
 class DWComboBox(context: Context) : AppCompatEditText(context), OnTouchListener, OnItemClickListener {
     var lpw: ListPopupWindow? = null
     var list = mutableListOf<String>()
+    var selected: Int = -1
 
     init {
         setCompoundDrawablesWithIntrinsicBounds(0, 0, android.R.drawable.arrow_down_float, 0);
@@ -105,8 +106,10 @@
 
     override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
         val item = list[position]
+        selected = position
         setText(item)
         lpw!!.dismiss()
+        eventHandlerInt(11, position, 0, 0, 0)
     }
 
     override fun onTouch(v: View, event: MotionEvent): Boolean {
@@ -121,6 +124,14 @@
         }
         return false
     }
+
+    external fun eventHandlerInt(
+        message: Int,
+        inta: Int,
+        intb: Int,
+        intc: Int,
+        intd: Int
+    )
 }
 
 class DWindows : AppCompatActivity() {
@@ -366,7 +377,7 @@
             // Handle scrollboxes by pulling the LinearLayout
             // out of the ScrollView to pack into
             if (boxview is LinearLayout) {
-                box = boxview as LinearLayout
+                box = boxview
             } else if (boxview is ScrollView) {
                 var sv: ScrollView = boxview
 
@@ -615,9 +626,9 @@
     {
         waitOnUiThread {
             if (state != 0) {
-                mle!!.inputType = (InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE)
+                mle.inputType = (InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE)
             } else {
-                mle!!.inputType = InputType.TYPE_NULL
+                mle.inputType = InputType.TYPE_NULL
             }
         }
     }
@@ -1031,6 +1042,7 @@
             combobox = DWComboBox(this)
             combobox!!.tag = dataArrayMap
             combobox!!.id = cid
+            combobox!!.setText(text)
         }
         return combobox
     }
@@ -1039,13 +1051,117 @@
     {
         waitOnUiThread {
             if(window is DWComboBox) {
-                val combobox = window as DWComboBox
+                val combobox = window
 
                 combobox.list.add(text)
             }
         }
     }
 
+    fun listOrComboBoxInsert(window: View, text: String, pos: Int)
+    {
+        waitOnUiThread {
+            if(window is DWComboBox) {
+                val combobox = window
+
+                combobox.list.add(pos, text)
+            }
+        }
+    }
+
+    fun listOrComboBoxClear(window: View)
+    {
+        waitOnUiThread {
+            if(window is DWComboBox) {
+                val combobox = window
+
+                combobox.list.clear()
+            }
+        }
+    }
+
+    fun listOrComboBoxCount(window: View): Int
+    {
+        var retval: Int = 0
+
+        waitOnUiThread {
+            if(window is DWComboBox) {
+                val combobox = window
+
+                retval = combobox.list.count()
+            }
+        }
+        return retval
+    }
+
+    fun listOrComboBoxSetText(window: View, index: Int, text: String)
+    {
+        waitOnUiThread {
+            if(window is DWComboBox) {
+                val combobox = window
+
+                if(index < combobox.list.count())
+                    combobox.list[index] = text
+            }
+        }
+    }
+
+    fun listOrComboBoxGetText(window: View, index: Int): String?
+    {
+        var retval: String? = null
+
+        waitOnUiThread {
+            if(window is DWComboBox) {
+                val combobox = window
+
+                if(index < combobox.list.count())
+                    retval = combobox.list[index]
+            }
+        }
+        return retval
+    }
+
+    fun listOrComboBoxGetSelected(window: View): Int
+    {
+        var retval: Int = -1
+
+        waitOnUiThread {
+            if(window is DWComboBox) {
+                val combobox = window
+
+                retval = combobox.selected
+            }
+        }
+        return retval
+    }
+
+    fun listOrComboBoxSelect(window: View, index: Int, state: Int)
+    {
+        waitOnUiThread {
+            if(window is DWComboBox) {
+                val combobox = window
+
+                if(index < combobox.list.count() && state != 0) {
+                    combobox.selected = index
+                    combobox.setText(combobox.list[index])
+                }
+            }
+        }
+    }
+
+    fun listOrComboBoxDelete(window: View, index: Int)
+    {
+        waitOnUiThread {
+            if(window is DWComboBox) {
+                val combobox = window
+
+                if(index < combobox.list.count()) {
+                    combobox.list.removeAt(index)
+                }
+            }
+        }
+    }
+
     fun timerConnect(interval: Long, sigfunc: Long, data: Long): Timer
     {
         // creating timer task, timer
--- a/android/dw.cpp	Sat May 08 01:54:52 2021 +0000
+++ b/android/dw.cpp	Sat May 08 08:11:51 2021 +0000
@@ -423,6 +423,16 @@
     _dw_event_handler(obj1, params, message);
 }
 
+JNIEXPORT void JNICALL
+Java_org_dbsoft_dwindows_DWComboBox_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) {
@@ -1543,6 +1553,20 @@
  */
 void API dw_listbox_insert(HWND handle, const char *text, int pos)
 {
+    JNIEnv *env;
+
+    if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // Construct a String
+        jstring jstr = env->NewStringUTF(text);
+        // 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 listOrComboBoxInsert = env->GetMethodID(clazz, "listOrComboBoxInsert",
+                                                          "(Landroid/view/View;Ljava/lang/String;I)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, listOrComboBoxInsert, handle, jstr, pos);
+    }
 }
 
 /*
@@ -1554,6 +1578,11 @@
  */
 void API dw_listbox_list_append(HWND handle, char **text, int count)
 {
+    int x;
+
+    /* TODO: this would be more efficient passing in an array */
+    for(x=0;x<count;x++)
+        dw_listbox_append(handle, text[x]);
 }
 
 /*
@@ -1563,6 +1592,18 @@
  */
 void API dw_listbox_clear(HWND handle)
 {
+    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 listOrComboBoxClear = env->GetMethodID(clazz, "listOrComboBoxClear",
+                                                          "(Landroid/view/View;)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, listOrComboBoxClear, handle);
+    }
 }
 
 /*
@@ -1574,7 +1615,20 @@
  */
 int API dw_listbox_count(HWND handle)
 {
-    return 0;
+    JNIEnv *env;
+    int retval = 0;
+
+    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 listOrComboBoxCount = env->GetMethodID(clazz, "listOrComboBoxCount",
+                                                         "(Landroid/view/View;)I");
+        // Call the method on the object
+        retval = env->CallIntMethod(_dw_obj, listOrComboBoxCount, handle);
+    }
+    return retval;
 }
 
 /*
@@ -1597,6 +1651,25 @@
  */
 void API dw_listbox_get_text(HWND handle, unsigned int index, char *buffer, unsigned int length)
 {
+    JNIEnv *env;
+
+    if(buffer && length > 0 && 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 listOrComboBoxGetText = env->GetMethodID(clazz, "listOrComboBoxGetText",
+                                                           "(Landroid/view/View;)ILjava/lang/String;");
+        // Call the method on the object
+        jstring result = (jstring)env->CallObjectMethod(_dw_obj, listOrComboBoxGetText, handle, index);
+        // Get the UTF8 string result
+        if(result)
+        {
+            const char *utf8 = env->GetStringUTFChars(result, 0);
+
+            strncpy(buffer, utf8, length);
+        }
+    }
 }
 
 /*
@@ -1608,6 +1681,20 @@
  */
 void API dw_listbox_set_text(HWND handle, unsigned int index, const char *buffer)
 {
+    JNIEnv *env;
+
+    if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // Construct a String
+        jstring jstr = env->NewStringUTF(buffer);
+        // 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 listOrComboBoxSetText = env->GetMethodID(clazz, "listOrComboBoxSetText",
+                                                          "(Landroid/view/View;ILjava/lang/String;)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, listOrComboBoxSetText, handle, index, jstr);
+    }
 }
 
 /*
@@ -1619,7 +1706,20 @@
  */
 int API dw_listbox_selected(HWND handle)
 {
-    return DW_ERROR_UNKNOWN;
+    JNIEnv *env;
+    int retval = DW_ERROR_UNKNOWN;
+
+    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 listOrComboBoxGetSelected = env->GetMethodID(clazz, "listOrComboBoxGetSelected",
+                                                           "(Landroid/view/View;)I");
+        // Call the method on the object
+        retval = env->CallIntMethod(_dw_obj, listOrComboBoxGetSelected, handle);
+    }
+    return retval;
 }
 
 /*
@@ -1644,6 +1744,18 @@
  */
 void API dw_listbox_select(HWND handle, int index, int state)
 {
+    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 listOrComboBoxSelect = env->GetMethodID(clazz, "listOrComboBoxSelect",
+                                                           "(Landroid/view/View;II)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, listOrComboBoxSelect, handle, index, state);
+    }
 }
 
 /*
@@ -1654,6 +1766,18 @@
  */
 void API dw_listbox_delete(HWND handle, int index)
 {
+    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 listOrComboBoxDelete = env->GetMethodID(clazz, "listOrComboBoxDelete",
+                                                          "(Landroid/view/View;I)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, listOrComboBoxDelete, handle, index);
+    }
 }
 
 /*