changeset 2520:167af4b0004b

Android: Implement spinbuttons and callbacks.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 08 May 2021 23:22:52 +0000
parents 551313c064f2
children 5f92284e2b08
files android/DWindows.kt android/dw.cpp
diffstat 2 files changed, 171 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Sat May 08 21:34:55 2021 +0000
+++ b/android/DWindows.kt	Sat May 08 23:22:52 2021 +0000
@@ -84,6 +84,55 @@
     external fun eventHandlerHTMLChanged(obj1: View, message: Int, URI: String, status: Int)
 }
 
+class DWSpinButton(context: Context) : AppCompatEditText(context), OnTouchListener {
+    var value: Long = 0
+    var minimum: Long = 0
+    var maximum: Long = 65535
+
+    init {
+        setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_media_previous, 0, android.R.drawable.ic_media_next, 0);
+        setOnTouchListener(this)
+    }
+
+    override fun onTouch(v: View, event: MotionEvent): Boolean {
+        val DRAWABLE_RIGHT = 2
+        val DRAWABLE_LEFT = 0
+
+        if (event.action == MotionEvent.ACTION_UP) {
+            if (event.x >= v.width - (v as EditText)
+                    .compoundDrawables[DRAWABLE_RIGHT].bounds.width()
+            ) {
+                value += 1
+                if(value > maximum) {
+                    value = maximum
+                }
+                setText(value.toString())
+                eventHandlerInt(14, value.toInt(), 0, 0, 0)
+                return true
+            } else if (event.x <= (v as EditText)
+                    .compoundDrawables[DRAWABLE_LEFT].bounds.width()
+            ) {
+                value -= 1
+                if(value < minimum) {
+                    value = minimum
+                }
+                setText(value.toString())
+                eventHandlerInt(14, value.toInt(), 0, 0, 0)
+                return true
+            }
+        }
+        return false
+    }
+
+    external fun eventHandlerInt(
+        message: Int,
+        inta: Int,
+        intb: Int,
+        intc: Int,
+        intd: Int
+    )
+}
+
 class DWComboBox(context: Context) : AppCompatEditText(context), OnTouchListener, OnItemClickListener {
     var lpw: ListPopupWindow? = null
     var list = mutableListOf<String>()
@@ -114,6 +163,7 @@
 
     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()
@@ -1060,6 +1110,64 @@
         }
     }
 
+    fun spinButtonNew(text: String, cid: Int): DWSpinButton?
+    {
+        var spinbutton: DWSpinButton? = null
+
+        waitOnUiThread {
+            var dataArrayMap = SimpleArrayMap<String, Long>()
+            val newval = text.toLongOrNull()
+
+            spinbutton = DWSpinButton(this)
+            spinbutton!!.tag = dataArrayMap
+            spinbutton!!.id = cid
+            spinbutton!!.setText(text)
+            if(newval != null) {
+                spinbutton!!.value = newval
+            }
+        }
+        return spinbutton
+    }
+
+    fun spinButtonSetPos(spinbutton: DWSpinButton, position: Long)
+    {
+        waitOnUiThread {
+            spinbutton.value = position
+            spinbutton.setText(position.toString())
+        }
+    }
+
+    fun spinButtonSetLimits(spinbutton: DWSpinButton, upper: Long, lower: Long)
+    {
+        waitOnUiThread {
+            spinbutton.maximum = upper
+            spinbutton.minimum = lower
+            if(spinbutton.value > upper) {
+                spinbutton.value = upper
+            }
+            if(spinbutton.value < lower) {
+                spinbutton.value = lower
+            }
+            spinbutton.setText(spinbutton.value.toString())
+        }
+    }
+
+    fun spinButtonGetPos(spinbutton: DWSpinButton): Long
+    {
+        var retval: Long = 0
+
+        waitOnUiThread {
+            val newvalue = spinbutton.text.toString().toLongOrNull()
+
+            if(newvalue == null) {
+                retval = spinbutton.value
+            } else {
+                retval = newvalue
+            }
+        }
+        return retval
+    }
+
     fun comboBoxNew(text: String, cid: Int): DWComboBox?
     {
         var combobox: DWComboBox? = null
--- a/android/dw.cpp	Sat May 08 21:34:55 2021 +0000
+++ b/android/dw.cpp	Sat May 08 23:22:52 2021 +0000
@@ -434,6 +434,16 @@
 }
 
 JNIEXPORT void JNICALL
+Java_org_dbsoft_dwindows_DWSpinButton_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);
+}
+
+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,
@@ -1212,6 +1222,21 @@
  */
 HWND API dw_spinbutton_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 spinButtonNew = env->GetMethodID(clazz, "spinButtonNew",
+                                                   "(Ljava/lang/String;I)Lorg/dbsoft/dwindows/DWSpinButton;");
+        // Call the method on the object
+        jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, spinButtonNew, jstr, (int)cid));
+        return result;
+    }
     return 0;
 }
 
@@ -1223,6 +1248,18 @@
  */
 void API dw_spinbutton_set_pos(HWND handle, long position)
 {
+    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 spinButtonSetPos = env->GetMethodID(clazz, "spinButtonSetPos",
+                                                      "(Lorg/dbsoft/dwindows/DWSpinButton;J)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, spinButtonSetPos, handle, (jlong)position);
+    }
 }
 
 /*
@@ -1234,6 +1271,18 @@
  */
 void API dw_spinbutton_set_limits(HWND handle, long upper, long lower)
 {
+    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 spinButtonSetLimits = env->GetMethodID(clazz, "spinButtonSetLimits",
+                                                         "(Lorg/dbsoft/dwindows/DWSpinButton;JJ)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, spinButtonSetLimits, handle, (jlong)upper, (jlong)lower);
+    }
 }
 
 /*
@@ -1245,7 +1294,20 @@
  */
 long API dw_spinbutton_get_pos(HWND handle)
 {
-    return 0;
+    JNIEnv *env;
+    long retval = 0;
+
+    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 spinButtonGetPos = env->GetMethodID(clazz, "spinButtonGetPos",
+                                                      "(Lorg/dbsoft/dwindows/DWSpinButton;)J");
+        // Call the method on the object
+        retval = env->CallLongMethod(_dw_obj, spinButtonGetPos, handle);
+    }
+    return retval;
 }
 
 /*