comparison android/dw.cpp @ 2521:5f92284e2b08

Android: Implement bitmap buttons, implement dw_listbox_selected_multi(). Added a number of safety checks to prevent java exceptions.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 09 May 2021 09:31:14 +0000
parents 167af4b0004b
children 66c490aa719d
comparison
equal deleted inserted replaced
2520:167af4b0004b 2521:5f92284e2b08
1175 * Returns: 1175 * Returns:
1176 * A handle to a bitmap button window or NULL on failure. 1176 * A handle to a bitmap button window or NULL on failure.
1177 */ 1177 */
1178 HWND API dw_bitmapbutton_new(const char *text, ULONG resid) 1178 HWND API dw_bitmapbutton_new(const char *text, ULONG resid)
1179 { 1179 {
1180 JNIEnv *env;
1181
1182 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
1183 {
1184 // Construct a String
1185 jstring jstr = env->NewStringUTF(text);
1186 // First get the class that contains the method you need to call
1187 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1188 // Get the method that you want to call
1189 jmethodID bitmapButtonNew = env->GetMethodID(clazz, "bitmapButtonNew",
1190 "(Ljava/lang/String;I)Landroid/widget/ImageButton;");
1191 // Call the method on the object
1192 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, bitmapButtonNew, jstr, (int)resid));
1193 return result;
1194 }
1180 return 0; 1195 return 0;
1181 } 1196 }
1182 1197
1183 /* 1198 /*
1184 * Create a new bitmap button window (widget) to be packed from a file. 1199 * Create a new bitmap button window (widget) to be packed from a file.
1191 * Returns: 1206 * Returns:
1192 * A handle to a bitmap button window or NULL on failure. 1207 * A handle to a bitmap button window or NULL on failure.
1193 */ 1208 */
1194 HWND API dw_bitmapbutton_new_from_file(const char *text, unsigned long cid, const char *filename) 1209 HWND API dw_bitmapbutton_new_from_file(const char *text, unsigned long cid, const char *filename)
1195 { 1210 {
1211 JNIEnv *env;
1212
1213 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
1214 {
1215 // Construct a String
1216 jstring jstr = env->NewStringUTF(text);
1217 jstring path = env->NewStringUTF(text);
1218 // First get the class that contains the method you need to call
1219 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1220 // Get the method that you want to call
1221 jmethodID bitmapButtonNewFromFile = env->GetMethodID(clazz, "bitmapButtonNewFromFile",
1222 "(Ljava/lang/String;ILjava/lang/String;)Landroid/widget/ImageButton;");
1223 // Call the method on the object
1224 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, bitmapButtonNewFromFile, jstr, (int)cid, path));
1225 return result;
1226 }
1196 return 0; 1227 return 0;
1197 } 1228 }
1198 1229
1199 /* 1230 /*
1200 * Create a new bitmap button window (widget) to be packed from data. 1231 * Create a new bitmap button window (widget) to be packed from data.
1207 * Returns: 1238 * Returns:
1208 * A handle to a bitmap button window or NULL on failure. 1239 * A handle to a bitmap button window or NULL on failure.
1209 */ 1240 */
1210 HWND API dw_bitmapbutton_new_from_data(const char *text, unsigned long cid, const char *data, int len) 1241 HWND API dw_bitmapbutton_new_from_data(const char *text, unsigned long cid, const char *data, int len)
1211 { 1242 {
1243 JNIEnv *env;
1244
1245 if(data && len > 0 && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
1246 {
1247 // Construct a String
1248 jstring jstr = env->NewStringUTF(text);
1249 // Construct a byte array
1250 jbyteArray bytearray = env->NewByteArray(len);
1251 env->SetByteArrayRegion(bytearray, 0, len, reinterpret_cast<const jbyte *>(data));
1252 // First get the class that contains the method you need to call
1253 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1254 // Get the method that you want to call
1255 jmethodID bitmapButtonNewFromData = env->GetMethodID(clazz, "bitmapButtonNewFromData",
1256 "(Ljava/lang/String;I[BI)Landroid/widget/ImageButton;");
1257 // Call the method on the object
1258 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, bitmapButtonNewFromData, jstr, (int)cid, bytearray, len));
1259 // Clean up after the array now that we are finished
1260 //env->ReleaseByteArrayElements(bytearray, (jbyte *) data, 0);
1261 return result;
1262 }
1212 return 0; 1263 return 0;
1213 } 1264 }
1214 1265
1215 /* 1266 /*
1216 * Create a new spinbutton window (widget) to be packed. 1267 * Create a new spinbutton window (widget) to be packed.
1729 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 1780 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
1730 { 1781 {
1731 // First get the class that contains the method you need to call 1782 // First get the class that contains the method you need to call
1732 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1783 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1733 // Get the method that you want to call 1784 // Get the method that you want to call
1734 jmethodID listSetTop = env->GetMethodID(clazz, "listSetTop", 1785 jmethodID listBoxSetTop = env->GetMethodID(clazz, "listBoxSetTop",
1735 "(Landroid/view/View;I)V"); 1786 "(Landroid/view/View;I)V");
1736 // Call the method on the object 1787 // Call the method on the object
1737 env->CallVoidMethod(_dw_obj, listSetTop, handle, top); 1788 env->CallVoidMethod(_dw_obj, listBoxSetTop, handle, top);
1738 } 1789 }
1739 } 1790 }
1740 1791
1741 /* 1792 /*
1742 * Copies the given index item's text into buffer. 1793 * Copies the given index item's text into buffer.
1754 { 1805 {
1755 // First get the class that contains the method you need to call 1806 // First get the class that contains the method you need to call
1756 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1807 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1757 // Get the method that you want to call 1808 // Get the method that you want to call
1758 jmethodID listOrComboBoxGetText = env->GetMethodID(clazz, "listOrComboBoxGetText", 1809 jmethodID listOrComboBoxGetText = env->GetMethodID(clazz, "listOrComboBoxGetText",
1759 "(Landroid/view/View;)ILjava/lang/String;"); 1810 "(Landroid/view/View;I)Ljava/lang/String;");
1760 // Call the method on the object 1811 // Call the method on the object
1761 jstring result = (jstring)env->CallObjectMethod(_dw_obj, listOrComboBoxGetText, handle, index); 1812 jstring result = (jstring)env->CallObjectMethod(_dw_obj, listOrComboBoxGetText, handle, index);
1762 // Get the UTF8 string result 1813 // Get the UTF8 string result
1763 if(result) 1814 if(result)
1764 { 1815 {
1827 * Returns: 1878 * Returns:
1828 * The next selected item or DW_ERROR_UNKNOWN (-1) on error. 1879 * The next selected item or DW_ERROR_UNKNOWN (-1) on error.
1829 */ 1880 */
1830 int API dw_listbox_selected_multi(HWND handle, int where) 1881 int API dw_listbox_selected_multi(HWND handle, int where)
1831 { 1882 {
1832 return DW_ERROR_UNKNOWN; 1883 JNIEnv *env;
1884 int retval = DW_ERROR_UNKNOWN;
1885
1886 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
1887 {
1888 // First get the class that contains the method you need to call
1889 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1890 // Get the method that you want to call
1891 jmethodID listBoxSelectedMulti = env->GetMethodID(clazz, "listBoxSelectedMulti",
1892 "(Landroid/view/View;I)I");
1893 // Call the method on the object
1894 retval = env->CallIntMethod(_dw_obj, listBoxSelectedMulti, handle, where);
1895 }
1896 return retval;
1833 } 1897 }
1834 1898
1835 /* 1899 /*
1836 * Sets the selection state of a given index. 1900 * Sets the selection state of a given index.
1837 * Parameters: 1901 * Parameters: