changeset 2852:5018df4f952e

Win/Android/Template: Add return values to dw_window_set_bitmap(_from_data). Also update the readme regarding this change.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 15 Nov 2022 00:34:20 +0000
parents fdd21139c07f
children c250764b2f32
files android/DWindows.kt android/dw.cpp readme.txt template/dw.c win/dw.c
diffstat 5 files changed, 104 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Mon Nov 14 22:00:47 2022 +0000
+++ b/android/DWindows.kt	Tue Nov 15 00:34:20 2022 +0000
@@ -5782,8 +5782,10 @@
         return imageview
     }
 
-    fun windowSetBitmap(window: View, resID: Int, file: String?)
-    {
+    fun windowSetBitmap(window: View, resID: Int, file: String?): Int
+    {
+        var retval: Int = -1
+
         waitOnUiThread {
             var filename: String? = file
 
@@ -5794,10 +5796,12 @@
                     val button = window
 
                     button.setImageResource(resID)
+                    retval = 0
                 } else if (window is ImageView) {
                     val imageview = window
 
                     imageview.setImageResource(resID)
+                    retval = 0
                 }
             }
             if(filename != null) {
@@ -5812,48 +5816,64 @@
                                 val button = window
 
                                 button.setImageBitmap(b)
+                                retval = 0
                             } else if (window is ImageView) {
                                 val imageview = window
 
                                 imageview.setImageBitmap(b)
+                                retval = 0
                             }
                             break
+                        } else {
+                            retval = 1
                         }
                     } catch (e: IOException) {
                     }
                 }
             }
         }
-    }
-
-    fun windowSetBitmapFromData(window: View, resID: Int, data: ByteArray?, length: Int)
-    {
+        return retval
+    }
+
+    fun windowSetBitmapFromData(window: View, resID: Int, data: ByteArray?, length: Int): Int
+    {
+        var retval: Int = -1
+
         waitOnUiThread {
             if(resID != 0) {
                 if (window is ImageButton) {
                     val button = window
 
                     button.setImageResource(resID)
+                    retval = 0
                 } else if (window is ImageView) {
                     val imageview = window
 
                     imageview.setImageResource(resID)
+                    retval = 0
                 }
             }
             if(data != null) {
                 val b = BitmapFactory.decodeByteArray(data, 0, length)
 
-                if (window is ImageButton) {
-                    val button = window
-
-                    button.setImageBitmap(b)
-                } else if (window is ImageView) {
-                    val imageview = window
-
-                    imageview.setImageBitmap(b)
+                if(b != null) {
+                    if (window is ImageButton) {
+                        val button = window
+
+                        button.setImageBitmap(b)
+                        retval = 0
+                    } else if (window is ImageView) {
+                        val imageview = window
+
+                        imageview.setImageBitmap(b)
+                        retval = 0
+                    }
+                } else {
+                    retval = 1
                 }
             }
         }
+        return retval
     }
 
     fun iconNew(file: String?, data: ByteArray?, length: Int, resID: Int): Drawable?
--- a/android/dw.cpp	Mon Nov 14 22:00:47 2022 +0000
+++ b/android/dw.cpp	Tue Nov 15 00:34:20 2022 +0000
@@ -6197,10 +6197,15 @@
  *                 Windows and a pixmap on Unix, pass
  *                 nullptr if you use the id param)
  *       len: Length of data passed
- */
-void API dw_window_set_bitmap_from_data(HWND handle, unsigned long cid, const char *data, int len)
-{
-    JNIEnv *env;
+ * Returns:
+ *        DW_ERROR_NONE on success.
+ *        DW_ERROR_UNKNOWN if the parameters were invalid.
+ *        DW_ERROR_GENERAL if the bitmap was unable to be loaded.
+ */
+int API dw_window_set_bitmap_from_data(HWND handle, unsigned long cid, const char *data, int len)
+{
+    JNIEnv *env;
+    int retval = DW_ERROR_UNKNOWN;
 
     if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
     {
@@ -6215,14 +6220,15 @@
         jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
         // Get the method that you want to call
         jmethodID windowSetBitmapFromData = env->GetMethodID(clazz, "windowSetBitmapFromData",
-                                                             "(Landroid/view/View;I[BI)V");
-        // Call the method on the object
-        env->CallVoidMethod(_dw_obj, windowSetBitmapFromData, handle, (int)cid, bytearray, len);
+                                                             "(Landroid/view/View;I[BI)I");
+        // Call the method on the object
+        retval = env->CallIntMethod(_dw_obj, windowSetBitmapFromData, handle, (int)cid, bytearray, len);
         _dw_jni_check_exception(env);
         // Clean up after the array now that we are finished
         //if(bytearray)
             //env->ReleaseByteArrayElements(bytearray, (jbyte *) data, 0);
     }
+    return retval;
 }
 
 /*
@@ -6234,10 +6240,15 @@
  *       filename: a path to a file (Bitmap on OS/2 or
  *                 Windows and a pixmap on Unix, pass
  *                 nullptr if you use the id param)
- */
-void API dw_window_set_bitmap(HWND handle, unsigned long resid, const char *filename)
-{
-    JNIEnv *env;
+ * Returns:
+ *        DW_ERROR_NONE on success.
+ *        DW_ERROR_UNKNOWN if the parameters were invalid.
+ *        DW_ERROR_GENERAL if the bitmap was unable to be loaded.
+ */
+int API dw_window_set_bitmap(HWND handle, unsigned long resid, const char *filename)
+{
+    JNIEnv *env;
+    int retval = DW_ERROR_UNKNOWN;
 
     if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
     {
@@ -6250,13 +6261,14 @@
         jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
         // Get the method that you want to call
         jmethodID windowSetBitmapFromData = env->GetMethodID(clazz, "windowSetBitmap",
-                                                             "(Landroid/view/View;ILjava/lang/String;)V");
-        // Call the method on the object
-        env->CallVoidMethod(_dw_obj, windowSetBitmapFromData, handle, (int)resid, jstr);
+                                                             "(Landroid/view/View;ILjava/lang/String;)I");
+        // Call the method on the object
+        retval = env->CallIntMethod(_dw_obj, windowSetBitmapFromData, handle, (int)resid, jstr);
         _dw_jni_check_exception(env);
         if(jstr)
             env->DeleteLocalRef(jstr);
     }
+    return retval;
 }
 
 /*
--- a/readme.txt	Mon Nov 14 22:00:47 2022 +0000
+++ b/readme.txt	Tue Nov 15 00:34:20 2022 +0000
@@ -61,6 +61,9 @@
     DW_CONTAINER_MODE_DEFAULT: Minimal container; icon and text only.
     DW_CONTAINER_MODE_EXTRA: Extra columns displayed on a second line.
     DW_CONTAINER_MODE_MULTI: A separate clickable line for each column.
+Added return values to several functions previously returning void.
+    Previous code should just be able to ignore the new return values.
+    Currently affected: dw_window_set_bitmap(_from_data)
 
 
 Dynamic Windows Documentation is available at:
--- a/template/dw.c	Mon Nov 14 22:00:47 2022 +0000
+++ b/template/dw.c	Tue Nov 15 00:34:20 2022 +0000
@@ -2862,9 +2862,14 @@
  *                 Windows and a pixmap on Unix, pass
  *                 NULL if you use the id param)
  *       len: Length of data passed
- */
-void API dw_window_set_bitmap_from_data(HWND handle, unsigned long cid, const char *data, int len)
-{
+ * Returns:
+ *        DW_ERROR_NONE on success.
+ *        DW_ERROR_UNKNOWN if the parameters were invalid.
+ *        DW_ERROR_GENERAL if the bitmap was unable to be loaded.
+ */
+int API dw_window_set_bitmap_from_data(HWND handle, unsigned long cid, const char *data, int len)
+{
+   return DW_ERROR_UNKNOWN;
 }
 
 /*
@@ -2876,9 +2881,14 @@
  *       filename: a path to a file (Bitmap on OS/2 or
  *                 Windows and a pixmap on Unix, pass
  *                 NULL if you use the id param)
- */
-void API dw_window_set_bitmap(HWND handle, unsigned long resid, const char *filename)
-{
+ * Returns:
+ *        DW_ERROR_NONE on success.
+ *        DW_ERROR_UNKNOWN if the parameters were invalid.
+ *        DW_ERROR_GENERAL if the bitmap was unable to be loaded.
+ */
+int API dw_window_set_bitmap(HWND handle, unsigned long resid, const char *filename)
+{
+   return DW_ERROR_UNKNOWN;
 }
 
 /*
--- a/win/dw.c	Mon Nov 14 22:00:47 2022 +0000
+++ b/win/dw.c	Tue Nov 15 00:34:20 2022 +0000
@@ -7693,14 +7693,14 @@
 }
 
 /* Internal function to set bitmap for the next two functions */
-void _dw_window_set_bitmap(HWND handle, HICON icon, HBITMAP hbitmap)
+int _dw_window_set_bitmap(HWND handle, HICON icon, HBITMAP hbitmap)
 {
    HBITMAP oldbitmap = 0;
    HANDLE oldicon = 0;
    TCHAR tmpbuf[100] = {0};
 
    if (!icon && !hbitmap)
-      return;
+      return DW_ERROR_GENERAL;
 
    GetClassName(handle, tmpbuf, 99);
 
@@ -7770,6 +7770,7 @@
          _dw_redraw(_dw_toplevel_window(handle), TRUE);
       }
    }
+   return DW_ERROR_NONE;
 }
 
 /*
@@ -7781,8 +7782,12 @@
  *       filename: a path to a file (Bitmap on OS/2 or
  *                 Windows and a pixmap on Unix, pass
  *                 NULL if you use the id param)
- */
-void API dw_window_set_bitmap(HWND handle, unsigned long id, const char *filename)
+ * Returns:
+ *        DW_ERROR_NONE on success.
+ *        DW_ERROR_UNKNOWN if the parameters were invalid.
+ *        DW_ERROR_GENERAL if the bitmap was unable to be loaded.
+ */
+int API dw_window_set_bitmap(HWND handle, unsigned long id, const char *filename)
 {
    HBITMAP hbitmap = 0;
    HANDLE icon = 0;
@@ -7800,8 +7805,10 @@
       _dw_get_image_handle(filename, &icon, &hbitmap);
 #endif
    }
-
-   _dw_window_set_bitmap(handle, icon, hbitmap);
+   else
+      return DW_ERROR_UNKNOWN;
+
+   return _dw_window_set_bitmap(handle, icon, hbitmap);
 }
 
 /*
@@ -7814,8 +7821,12 @@
  *                 Bitmap on Windows and a pixmap on Unix, pass
  *                 NULL if you use the id param)
  *       len: length of data
- */
-void API dw_window_set_bitmap_from_data(HWND handle, unsigned long id, const char *data, int len)
+ * Returns:
+ *        DW_ERROR_NONE on success.
+ *        DW_ERROR_UNKNOWN if the parameters were invalid.
+ *        DW_ERROR_GENERAL if the bitmap was unable to be loaded.
+ */
+int API dw_window_set_bitmap_from_data(HWND handle, unsigned long id, const char *data, int len)
 {
    HBITMAP hbitmap=0;
    HICON icon=0;
@@ -7845,21 +7856,23 @@
          {
             _unlink( file );
             free( file );
-            return;
+            return DW_ERROR_GENERAL;
          }
          _unlink( file );
          free( file );
       }
       if (icon == 0 && hbitmap == 0)
-         return;
+         return DW_ERROR_GENERAL;
    }
    else if ( id )
    {
       hbitmap = LoadBitmap(_DWInstance, MAKEINTRESOURCE(id));
       icon = LoadImage(_DWInstance, MAKEINTRESOURCE(id), IMAGE_ICON, 0, 0, LR_SHARED);
    }
-
-   _dw_window_set_bitmap(handle, icon, hbitmap);
+   else
+      return DW_ERROR_UNKNOWN;
+
+   return _dw_window_set_bitmap(handle, icon, hbitmap);
 }