diff android/dw.cpp @ 2476:20c9e83cba2a

Android: Implement dw_entryfield_new, dw_entryfield_password_new() dw_button_new(), dw_text_new(), dw_status_text_new(), dw_checkbox_new() and dw_radiobutton_new(). Fix the signatures used in GetMethodID(), Android Studio can apparently do it autoamtically. Add a LinearLayout to the DWindows activity, use it as the content view so we don't need XML. dw_dwindow_new() will now return the LinearLayout instead of the Activity object... so the handle can be used with dw_box_pack*().
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 22 Apr 2021 00:04:01 +0000
parents 16d195d46f2a
children 3fbf8783122d
line wrap: on
line diff
--- a/android/dw.cpp	Wed Apr 21 11:15:26 2021 +0000
+++ b/android/dw.cpp	Thu Apr 22 00:04:01 2021 +0000
@@ -22,9 +22,7 @@
 #include <unistd.h>
 #if defined(__ANDROID__) && (__ANDROID_API__+0) < 21
 #include <sys/syscall.h>
-#endif
-
-#if defined(__ANDROID__) && (__ANDROID_API__+0) < 21
+
 /* Until Android API version 21 NDK does not define getsid wrapper in libc, although there is the corresponding syscall */
 inline pid_t getsid(pid_t pid)
 {
@@ -36,6 +34,8 @@
 extern "C" {
 #endif
 
+#define DW_CLASS_NAME "org/dbsoft/dwindows/dwtest/DWindows"
+
 static pthread_key_t _dw_env_key;
 static HEV _dw_main_event;
 static JavaVM *_dw_jvm;
@@ -172,7 +172,7 @@
 }
 
 /*
- * Returns a pointer to a static buffer which containes the
+ * Returns a pointer to a static buffer which contains the
  * private application data directory.
  */
 char * API dw_app_dir(void)
@@ -180,7 +180,7 @@
     static char _dw_exec_dir[MAX_PATH+1] = {0};
     /* Code to determine the execution directory here,
      * some implementations make this variable global
-     * and determin the location in dw_init().
+     * and determine the location in dw_init().
      */
     return _dw_exec_dir;
 }
@@ -367,9 +367,9 @@
     if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
     {
         // First get the class that contains the method you need to call
-        jclass clazz = env->FindClass("org/dbsoft/dwindows/dwtest/DWindows");
+        jclass clazz = env->FindClass(DW_CLASS_NAME);
         // Get the method that you want to call
-        jmethodID boxNew = env->GetMethodID(clazz, "boxNew", "(Ljava/lang/String;)V");
+        jmethodID boxNew = env->GetMethodID(clazz, "boxNew", "(II)Landroid/widget/LinearLayout;");
         // Call the method on the object
         jobject result = env->CallObjectMethod(_dw_obj, boxNew, type, pad);
         return result;
@@ -438,9 +438,9 @@
     if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
     {
         // First get the class that contains the method you need to call
-        jclass clazz = env->FindClass("org/dbsoft/dwindows/dwtest/DWindows");
+        jclass clazz = env->FindClass(DW_CLASS_NAME);
         // Get the method that you want to call
-        jmethodID boxPack = env->GetMethodID(clazz, "boxPack", "(Ljava/lang/String;)V");
+        jmethodID boxPack = env->GetMethodID(clazz, "boxPack", "(Landroid/widget/LinearLayout;Landroid/view/View;IIIIII)V");
         // Call the method on the object
         env->CallVoidMethod(_dw_obj, boxPack, box, item, index, width, height, hsize, vsize, pad);
     }
@@ -533,9 +533,43 @@
  */
 HWND API dw_button_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 = env->FindClass(DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID buttonNew = env->GetMethodID(clazz, "buttonNew",
+                                               "(Ljava/lang/String;I)Landroid/widget/Button;");
+        // Call the method on the object
+        jobject result = env->CallObjectMethod(_dw_obj, buttonNew, jstr, (int)cid);
+        return result;
+    }
     return 0;
 }
 
+HWND _dw_entryfield_new(const char *text, ULONG cid, int password)
+{
+    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 = env->FindClass(DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID entryfieldNew = env->GetMethodID(clazz, "entryfieldNew",
+                                                   "(Ljava/lang/String;II)Landroid/widget/EditText;");
+        // Call the method on the object
+        jobject result = env->CallObjectMethod(_dw_obj, entryfieldNew, jstr, (int)cid, password);
+        return result;
+    }
+    return 0;
+}
 /*
  * Create a new Entryfield window (widget) to be packed.
  * Parameters:
@@ -546,7 +580,7 @@
  */
 HWND API dw_entryfield_new(const char *text, ULONG cid)
 {
-    return 0;
+    return _dw_entryfield_new(text, cid, FALSE);
 }
 
 /*
@@ -559,7 +593,7 @@
  */
 HWND API dw_entryfield_password_new(const char *text, ULONG cid)
 {
-    return 0;
+    return _dw_entryfield_new(text, cid, TRUE);
 }
 
 /*
@@ -673,6 +707,21 @@
  */
 HWND API dw_radiobutton_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 = env->FindClass(DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID radioButtonNew = env->GetMethodID(clazz, "radioButtonNew",
+                                                    "(Ljava/lang/String;I)Landroid/widget/RadioButton;");
+        // Call the method on the object
+        jobject result = env->CallObjectMethod(_dw_obj, radioButtonNew, jstr, (int)cid);
+        return result;
+    }
     return 0;
 }
 
@@ -790,6 +839,21 @@
  */
 HWND API dw_checkbox_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 = env->FindClass(DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID checkboxNew = env->GetMethodID(clazz, "checkboxNew",
+                                                 "(Ljava/lang/String;I)Landroid/widget/CheckBox;");
+        // Call the method on the object
+        jobject result = env->CallObjectMethod(_dw_obj, checkboxNew, jstr, (int)cid);
+        return result;
+    }
     return 0;
 }
 
@@ -1129,6 +1193,26 @@
 {
 }
 
+HWND _dw_text_new(const char *text, ULONG cid, int status)
+{
+    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 = env->FindClass(DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID textNew = env->GetMethodID(clazz, "textNew",
+                                             "(Ljava/lang/String;II)Landroid/widget/TextView;");
+        // Call the method on the object
+        jobject result = env->CallObjectMethod(_dw_obj, textNew, jstr, (int)cid, status);
+        return result;
+    }
+    return 0;
+}
+
 /*
  * Create a new status text window (widget) to be packed.
  * Parameters:
@@ -1139,7 +1223,7 @@
  */
 HWND API dw_status_text_new(const char *text, ULONG cid)
 {
-    return 0;
+    return _dw_text_new(text, cid, TRUE);
 }
 
 /*
@@ -1152,7 +1236,7 @@
  */
 HWND API dw_text_new(const char *text, ULONG cid)
 {
-    return 0;
+    return _dw_text_new(text, cid, FALSE);
 }
 
 /*
@@ -2416,11 +2500,12 @@
         // Construct a String
         jstring jstr = env->NewStringUTF(title);
         // First get the class that contains the method you need to call
-        jclass clazz = env->FindClass("org/dbsoft/dwindows/dwtest/DWindows");
+        jclass clazz = env->FindClass(DW_CLASS_NAME);
         // Get the method that you want to call
-        jmethodID windowNew = env->GetMethodID(clazz, "windowNew", "(Ljava/lang/String;)V");
+        jmethodID windowNew = env->GetMethodID(clazz, "windowNew",
+                                               "(Ljava/lang/String;I)Landroid/widget/LinearLayout;");
         // Call the method on the object
-        jobject result = env->CallObjectMethod(_dw_obj, windowNew, jstr);
+        jobject result = env->CallObjectMethod(_dw_obj, windowNew, jstr, (int)flStyle);
         return result;
     }
     return 0;