changeset 2515:211044d98e86

Android: Initial attempt at our own ComboBox class, EditText with PopupList. Still need to access the internal resource for the down arrow button.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 08 May 2021 01:15:54 +0000
parents 5f711e86a211
children 8f5d064b7054
files android/DWindows.kt android/dw.cpp
diffstat 2 files changed, 102 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Fri May 07 19:54:05 2021 +0000
+++ b/android/DWindows.kt	Sat May 08 01:15:54 2021 +0000
@@ -1,7 +1,9 @@
 package org.dbsoft.dwindows
 
+import android.app.Activity
 import android.content.ClipData
 import android.content.ClipboardManager
+import android.content.Context
 import android.content.DialogInterface
 import android.content.pm.ActivityInfo
 import android.content.res.Configuration
@@ -20,15 +22,19 @@
 import android.util.Base64
 import android.util.Log
 import android.view.Gravity
+import android.view.MotionEvent
 import android.view.View
+import android.view.View.OnTouchListener
 import android.view.ViewGroup
 import android.view.inputmethod.EditorInfo
 import android.webkit.WebView
 import android.webkit.WebViewClient
 import android.widget.*
+import android.widget.AdapterView.OnItemClickListener
 import android.widget.SeekBar.OnSeekBarChangeListener
 import androidx.appcompat.app.AlertDialog
 import androidx.appcompat.app.AppCompatActivity
+import androidx.appcompat.widget.AppCompatEditText
 import androidx.collection.SimpleArrayMap
 import androidx.recyclerview.widget.RecyclerView
 import androidx.viewpager2.widget.ViewPager2
@@ -40,9 +46,9 @@
 
 
 class DWTabViewPagerAdapter : RecyclerView.Adapter<DWTabViewPagerAdapter.DWEventViewHolder>() {
-    public val viewList = mutableListOf<LinearLayout>()
-    public val pageList = mutableListOf<Long>()
-    public var currentPageID = 0L
+    val viewList = mutableListOf<LinearLayout>()
+    val pageList = mutableListOf<Long>()
+    var currentPageID = 0L
 
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
             DWEventViewHolder(viewList.get(viewType))
@@ -78,6 +84,45 @@
     external fun eventHandlerHTMLChanged(obj1: View, message: Int, URI: String, status: Int)
 }
 
+class DWComboBox(context: Context) : AppCompatEditText(context), OnTouchListener, OnItemClickListener {
+    var lpw: ListPopupWindow? = null
+    var list = mutableListOf<String>()
+
+    init {
+        //setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.unfold, 0);
+        setOnTouchListener(this)
+        lpw = ListPopupWindow(context)
+        lpw!!.setAdapter(
+            ArrayAdapter(
+                context,
+                android.R.layout.simple_list_item_1, list
+            )
+        )
+        lpw!!.anchorView = this
+        lpw!!.isModal = true
+        lpw!!.setOnItemClickListener(this)
+    }
+
+    override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
+        val item = list[position]
+        setText(item)
+        lpw!!.dismiss()
+    }
+
+    override fun onTouch(v: View, event: MotionEvent): Boolean {
+        val DRAWABLE_RIGHT = 2
+        if (event.action == MotionEvent.ACTION_UP) {
+            if (event.x >= v.width - (v as EditText)
+                    .compoundDrawables[DRAWABLE_RIGHT].bounds.width()
+            ) {
+                lpw!!.show()
+                return true
+            }
+        }
+        return false
+    }
+}
+
 class DWindows : AppCompatActivity() {
     var firstWindow: Boolean = true
     var windowLayout: LinearLayout? = null
@@ -976,6 +1021,31 @@
         }
     }
 
+    fun comboBoxNew(text: String, cid: Int): DWComboBox?
+    {
+        var combobox: DWComboBox? = null
+
+        waitOnUiThread {
+            var dataArrayMap = SimpleArrayMap<String, Long>()
+
+            combobox = DWComboBox(this)
+            combobox!!.tag = dataArrayMap
+            combobox!!.id = cid
+        }
+        return combobox
+    }
+
+    fun listOrComboBoxAppend(window: View, text: String)
+    {
+        waitOnUiThread {
+            if(window is DWComboBox) {
+                val combobox = window as DWComboBox
+
+                combobox.list.add(text)
+            }
+        }
+    }
+
     fun timerConnect(interval: Long, sigfunc: Long, data: Long): Timer
     {
         // creating timer task, timer
--- a/android/dw.cpp	Fri May 07 19:54:05 2021 +0000
+++ b/android/dw.cpp	Sat May 08 01:15:54 2021 +0000
@@ -1518,6 +1518,20 @@
  */
 void API dw_listbox_append(HWND handle, const char *text)
 {
+    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 listOrComboBoxAppend = env->GetMethodID(clazz, "listOrComboBoxAppend",
+                                                          "(Landroid/view/View;Ljava/lang/String;)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, listOrComboBoxAppend, handle, jstr);
+    }
 }
 
 /*
@@ -1652,6 +1666,21 @@
  */
 HWND API dw_combobox_new(const char *text, ULONG cid)
 {
+    JNIEnv *env;
+
+    if((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 comboBoxNew = env->GetMethodID(clazz, "comboBoxNew",
+                                                 "(Ljava/lang/String;I)Lorg/dbsoft/dwindows/DWComboBox;");
+        // Call the method on the object
+        jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, comboBoxNew, jstr, (int)cid));
+        return result;
+    }
     return 0;
 }