changeset 2500:ac0b7e579229

Android: Implement dw_scrollbox_new() using ScrollView.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 05 May 2021 21:59:23 +0000
parents ff3310fa6d72
children 41984ffb5ca2
files android/DWindows.kt android/dw.cpp
diffstat 2 files changed, 103 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Wed May 05 19:53:33 2021 +0000
+++ b/android/DWindows.kt	Wed May 05 21:59:23 2021 +0000
@@ -203,8 +203,31 @@
         return box
     }
 
+    fun scrollBoxNew(type: Int, pad: Int) : ScrollView {
+        val scrollBox = ScrollView(this)
+        val box = LinearLayout(this)
+        var dataArrayMap = SimpleArrayMap<String, Long>()
+
+        scrollBox.tag = dataArrayMap
+        box.layoutParams =
+            LinearLayout.LayoutParams(
+                LinearLayout.LayoutParams.MATCH_PARENT,
+                LinearLayout.LayoutParams.MATCH_PARENT
+            )
+        if (type > 0) {
+            box.orientation = LinearLayout.VERTICAL
+        } else {
+            box.orientation = LinearLayout.HORIZONTAL
+        }
+        box.setPadding(pad, pad, pad, pad)
+        // Add a pointer back to the ScrollView
+        box.tag = scrollBox
+        scrollBox.addView(box)
+        return scrollBox
+    }
+
     fun boxPack(
-        box: LinearLayout,
+        boxview: View,
         item: View,
         index: Int,
         width: Int,
@@ -215,63 +238,78 @@
     ) {
         var w: Int = LinearLayout.LayoutParams.WRAP_CONTENT
         var h: Int = LinearLayout.LayoutParams.WRAP_CONTENT
+        var box: LinearLayout? = null
 
-        if (item is LinearLayout) {
-            if (box.orientation == LinearLayout.VERTICAL) {
-                if (hsize > 0) {
-                    w = LinearLayout.LayoutParams.MATCH_PARENT
-                }
-            } else {
-                if (vsize > 0) {
-                    h = LinearLayout.LayoutParams.MATCH_PARENT
-                }
-            }
-        }
-        var params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(w, h)
+        // Handle scrollboxes by pulling the LinearLayout
+        // out of the ScrollView to pack into
+        if(boxview is LinearLayout) {
+            box = boxview as LinearLayout
+        } else if(boxview is ScrollView) {
+            var sv: ScrollView = boxview
 
-        if (item !is LinearLayout && (width != -1 || height != -1)) {
-            item.measure(0, 0)
-            if (width > 0) {
-                w = width
-            } else if (width == -1) {
-                w = item.getMeasuredWidth()
-            }
-            if (height > 0) {
-                h = height
-            } else if (height == -1) {
-                h = item.getMeasuredHeight()
+            if(sv.getChildAt(0) is LinearLayout) {
+                box = sv.getChildAt(0) as LinearLayout
             }
         }
-        if (box.orientation == LinearLayout.VERTICAL) {
-            if (vsize > 0) {
-                if (w > 0) {
-                    params.weight = w.toFloat()
+
+        if(box != null) {
+            if ((item is LinearLayout) or (item is ScrollView)) {
+                if (box.orientation == LinearLayout.VERTICAL) {
+                    if (hsize > 0) {
+                        w = LinearLayout.LayoutParams.MATCH_PARENT
+                    }
                 } else {
-                    params.weight = 1F
+                    if (vsize > 0) {
+                        h = LinearLayout.LayoutParams.MATCH_PARENT
+                    }
+                }
+            }
+            var params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(w, h)
+
+            if (item !is LinearLayout && (width != -1 || height != -1)) {
+                item.measure(0, 0)
+                if (width > 0) {
+                    w = width
+                } else if (width == -1) {
+                    w = item.getMeasuredWidth()
+                }
+                if (height > 0) {
+                    h = height
+                } else if (height == -1) {
+                    h = item.getMeasuredHeight()
                 }
             }
-        } else {
-            if (hsize > 0) {
-                if (h > 0) {
-                    params.weight = h.toFloat()
-                } else {
-                    params.weight = 1F
+            if (box.orientation == LinearLayout.VERTICAL) {
+                if (vsize > 0) {
+                    if (w > 0) {
+                        params.weight = w.toFloat()
+                    } else {
+                        params.weight = 1F
+                    }
+                }
+            } else {
+                if (hsize > 0) {
+                    if (h > 0) {
+                        params.weight = h.toFloat()
+                    } else {
+                        params.weight = 1F
+                    }
                 }
             }
-        }
-        if (pad > 0) {
-            params.setMargins(pad, pad, pad, pad)
+            if (pad > 0) {
+                params.setMargins(pad, pad, pad, pad)
+            }
+            var grav: Int = Gravity.CLIP_HORIZONTAL or Gravity.CLIP_VERTICAL
+            if (hsize > 0 && vsize > 0) {
+                params.gravity = Gravity.FILL or grav
+            } else if (hsize > 0) {
+                params.gravity = Gravity.FILL_HORIZONTAL or grav
+            } else if (vsize > 0) {
+                params.gravity = Gravity.FILL_VERTICAL or grav
+            }
+            item.layoutParams = params
+            box.addView(item, index)
         }
-        var grav: Int = Gravity.CLIP_HORIZONTAL or Gravity.CLIP_VERTICAL
-        if (hsize > 0 && vsize > 0) {
-            params.gravity = Gravity.FILL or grav
-        } else if (hsize > 0) {
-            params.gravity = Gravity.FILL_HORIZONTAL or grav
-        } else if (vsize > 0) {
-            params.gravity = Gravity.FILL_VERTICAL or grav
-        }
-        item.layoutParams = params
-        box.addView(item, index)
     }
 
     fun boxUnpack(item: View) {
--- a/android/dw.cpp	Wed May 05 19:53:33 2021 +0000
+++ b/android/dw.cpp	Wed May 05 21:59:23 2021 +0000
@@ -826,7 +826,8 @@
  */
 HWND API dw_groupbox_new(int type, int pad, const char *title)
 {
-    return 0;
+    /* TODO: Just create a normal box for now */
+    return dw_box_new(type, pad);
 }
 
 /*
@@ -839,8 +840,20 @@
  */
 HWND API dw_scrollbox_new(int type, int pad)
 {
-    /* TODO: Just create a normal box for now */
-    return dw_box_new(type, pad);
+    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 scrollBoxNew = env->GetMethodID(clazz, "scrollBoxNew",
+                                            "(II)Landroid/widget/ScrollView;");
+        // Call the method on the object
+        jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, scrollBoxNew, type, pad));
+        return result;
+    }
+    return 0;
 }
 
 /*
@@ -879,7 +892,7 @@
         // 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 boxPack = env->GetMethodID(clazz, "boxPack", "(Landroid/widget/LinearLayout;Landroid/view/View;IIIIII)V");
+        jmethodID boxPack = env->GetMethodID(clazz, "boxPack", "(Landroid/view/View;Landroid/view/View;IIIIII)V");
         // Call the method on the object
         env->CallVoidMethod(_dw_obj, boxPack, box, item, index, width, height, hsize, vsize, pad);
     }