changeset 2489:6c01b0132813

Android: Implement window and clipboard text setter and getters.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 30 Apr 2021 02:33:21 +0000
parents 666af45f33b5
children 62e124eecd82
files android/DWindows.kt android/dw.cpp
diffstat 2 files changed, 130 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Fri Apr 30 00:51:33 2021 +0000
+++ b/android/DWindows.kt	Fri Apr 30 02:33:21 2021 +0000
@@ -1,19 +1,19 @@
 package org.dbsoft.dwindows
 
+import android.content.ClipData
+import android.content.ClipboardManager
 import android.content.DialogInterface
 import android.content.pm.ActivityInfo
 import android.os.Bundle
 import android.os.Looper
 import android.text.method.PasswordTransformationMethod
 import android.util.Log
-import android.view.Gravity
-import android.view.LayoutInflater
-import android.view.View
-import android.view.ViewGroup
+import android.view.*
 import android.widget.*
 import androidx.appcompat.app.AlertDialog
 import androidx.appcompat.app.AppCompatActivity
 import androidx.collection.SimpleArrayMap
+import androidx.core.content.res.TypedArrayUtils.getText
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.FragmentActivity
 import androidx.fragment.app.FragmentManager
@@ -123,6 +123,65 @@
         window.setEnabled(state)
     }
 
+    fun windowSetText(window: View, text: String)
+    {
+        if(window is TextView)
+        {
+            var textview: TextView = window as TextView
+            textview.text = text
+        }
+        else if(window is Button)
+        {
+            var button: Button = window as Button
+            button.text = text
+        }
+        else if(window is LinearLayout)
+        {
+            // TODO: Make sure this is actually the top-level layout, not just a box
+            this.title = text
+        }
+    }
+
+    fun windowGetText(window: View): String?
+    {
+        if(window is TextView)
+        {
+            var textview: TextView = window as TextView
+            return textview.text.toString()
+        }
+        else if(window is Button)
+        {
+            var button: Button = window as Button
+            return button.text.toString()
+        }
+        else if(window is LinearLayout)
+        {
+            // TODO: Make sure this is actually the top-level layout, not just a box
+            return this.title.toString()
+        }
+        return null
+    }
+
+    fun clipboardGetText(): String
+    {
+        var cm: ClipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
+        var clipdata = cm.primaryClip
+
+        if(clipdata != null && clipdata.itemCount > 0)
+        {
+            return clipdata.getItemAt(0).coerceToText(this).toString()
+        }
+        return ""
+    }
+
+    fun clipboardSetText(text: String)
+    {
+        var cm: ClipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
+        var clipdata = ClipData.newPlainText("text", text)
+
+        cm.setPrimaryClip(clipdata)
+    }
+
     fun boxNew(type: Int, pad: Int): LinearLayout
     {
         val box = LinearLayout(this)
--- a/android/dw.cpp	Fri Apr 30 00:51:33 2021 +0000
+++ b/android/dw.cpp	Fri Apr 30 02:33:21 2021 +0000
@@ -414,16 +414,6 @@
     return 0L;
 }
 
-/* Implement these to get and set the Box* pointer on the widget handle */
-void *_dw_window_pointer_get(HWND handle)
-{
-    return NULL;
-}
-
-void _dw_window_pointer_set(HWND handle, Box *box)
-{
-}
-
 /*
  * Runs a message loop for Dynamic Windows.
  */
@@ -655,6 +645,24 @@
  */
 char * API dw_clipboard_get_text()
 {
+    JNIEnv *env;
+
+    if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        const char *utf8 = NULL;
+
+        // 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 clipboardGetText = env->GetMethodID(clazz, "clipboardGetText",
+                                                      "()Ljava/lang/String;");
+        // Call the method on the object
+        jstring result = (jstring)env->CallObjectMethod(_dw_obj, clipboardGetText);
+        // Get the UTF8 string result
+        if(result)
+            utf8 = env->GetStringUTFChars(result, 0);
+        return utf8 ? strdup(utf8) : NULL;
+    }
     return NULL;
 }
 
@@ -666,8 +674,21 @@
  */
 void API dw_clipboard_set_text(const char *str, int len)
 {
-}
-
+    JNIEnv *env;
+
+    if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // Construct a String
+        jstring jstr = env->NewStringUTF(str);
+        // 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 clipboardSetText = env->GetMethodID(clazz, "clipboardSetText",
+                                                      "(Ljava/lang/String;)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, clipboardSetText, jstr);
+    }
+}
 
 /*
  * Allocates and initializes a dialog struct.
@@ -678,8 +699,7 @@
  */
 DWDialog * API dw_dialog_new(void *data)
 {
-#if 0
-    DWDialog *tmp = malloc(sizeof(DWDialog));
+    DWDialog *tmp = (DWDialog *)malloc(sizeof(DWDialog));
 
     if(tmp)
     {
@@ -690,8 +710,6 @@
         tmp->result = NULL;
     }
     return tmp;
-#endif
-    return NULL;
 }
 
 /*
@@ -705,12 +723,10 @@
  */
 int API dw_dialog_dismiss(DWDialog *dialog, void *result)
 {
-#if 0
     dialog->result = result;
     dw_event_post(dialog->eve);
     dialog->done = TRUE;
-#endif
-    return DW_ERROR_GENERAL;
+    return DW_ERROR_NONE;
 }
 
 /*
@@ -725,7 +741,6 @@
 {
     void *tmp = NULL;
 
-#if 0
     while(!dialog->done)
     {
         dw_main_iteration();
@@ -733,7 +748,6 @@
     dw_event_close(&dialog->eve);
     tmp = dialog->result;
     free(dialog);
-#endif
     return tmp;
 }
 
@@ -3117,6 +3131,24 @@
  */
 char * API dw_window_get_text(HWND handle)
 {
+    JNIEnv *env;
+
+    if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        const char *utf8 = NULL;
+
+        // 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 windowGetText = env->GetMethodID(clazz, "windowGetText",
+                                                   "(Landroid/view/View;)Ljava/lang/String;");
+        // Call the method on the object
+        jstring result = (jstring)env->CallObjectMethod(_dw_obj, windowGetText, handle);
+        // Get the UTF8 string result
+        if(result)
+            utf8 = env->GetStringUTFChars(result, 0);
+        return utf8 ? strdup(utf8) : NULL;
+    }
     return NULL;
 }
 
@@ -3128,6 +3160,20 @@
  */
 void API dw_window_set_text(HWND handle, const char *text)
 {
+    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 windowSetText = env->GetMethodID(clazz, "windowSetText",
+                                                   "(Landroid/view/View;Ljava/lang/String;)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, windowSetText, handle, jstr);
+    }
 }
 
 /*