comparison android/dw.cpp @ 2501:41984ffb5ca2

Android: Initial implement of HTML control with WebView. Getting an error about view calls not on the UI thread, even though the call stack is on the main thread... confusing.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 06 May 2021 00:05:32 +0000
parents ac0b7e579229
children b6319aed3298
comparison
equal deleted inserted replaced
2500:ac0b7e579229 2501:41984ffb5ca2
369 void *params[8] = { NULL }; 369 void *params[8] = { NULL };
370 370
371 _dw_event_handler(obj1, params, message); 371 _dw_event_handler(obj1, params, message);
372 } 372 }
373 373
374 /* A more simple method for quicker calls */ 374 /* Handler for notebook page changes */
375 JNIEXPORT void JNICALL 375 JNIEXPORT void JNICALL
376 Java_org_dbsoft_dwindows_DWindows_eventHandlerNotebook(JNIEnv* env, jobject obj, jobject obj1, jint message, jlong pageID) { 376 Java_org_dbsoft_dwindows_DWindows_eventHandlerNotebook(JNIEnv* env, jobject obj, jobject obj1, jint message, jlong pageID) {
377 void *params[8] = { NULL }; 377 void *params[8] = { NULL };
378 378
379 params[3] = DW_INT_TO_POINTER(pageID); 379 params[3] = DW_INT_TO_POINTER(pageID);
380 _dw_event_handler(obj1, params, message);
381 }
382
383 /* Handler for HTML events */
384 JNIEXPORT void JNICALL
385 Java_org_dbsoft_dwindows_DWindows_eventHandlerHTMLResult(JNIEnv* env, jobject obj, jobject obj1,
386 jint message, jstring htmlResult, jlong data) {
387 const char *result = env->GetStringUTFChars(htmlResult, NULL);
388 void *params[8] = { NULL, DW_POINTER(result), NULL, 0, 0, 0, 0, DW_INT_TO_POINTER(data) };
389
380 _dw_event_handler(obj1, params, message); 390 _dw_event_handler(obj1, params, message);
381 } 391 }
382 392
383 JNIEXPORT void JNICALL 393 JNIEXPORT void JNICALL
384 Java_org_dbsoft_dwindows_DWindows_eventHandlerInt(JNIEnv* env, jobject obj, jobject obj1, jint message, 394 Java_org_dbsoft_dwindows_DWindows_eventHandlerInt(JNIEnv* env, jobject obj, jobject obj1, jint message,
885 /* Internal box packing function called by the other 3 functions */ 895 /* Internal box packing function called by the other 3 functions */
886 void _dw_box_pack(HWND box, HWND item, int index, int width, int height, int hsize, int vsize, int pad, const char *funcname) 896 void _dw_box_pack(HWND box, HWND item, int index, int width, int height, int hsize, int vsize, int pad, const char *funcname)
887 { 897 {
888 JNIEnv *env; 898 JNIEnv *env;
889 899
890 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 900 if(box && item && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
891 { 901 {
892 // First get the class that contains the method you need to call 902 // First get the class that contains the method you need to call
893 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 903 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
894 // Get the method that you want to call 904 // Get the method that you want to call
895 jmethodID boxPack = env->GetMethodID(clazz, "boxPack", "(Landroid/view/View;Landroid/view/View;IIIIII)V"); 905 jmethodID boxPack = env->GetMethodID(clazz, "boxPack", "(Landroid/view/View;Landroid/view/View;IIIIII)V");
907 */ 917 */
908 int API dw_box_unpack(HWND handle) 918 int API dw_box_unpack(HWND handle)
909 { 919 {
910 JNIEnv *env; 920 JNIEnv *env;
911 921
912 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 922 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
913 { 923 {
914 // First get the class that contains the method you need to call 924 // First get the class that contains the method you need to call
915 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 925 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
916 // Get the method that you want to call 926 // Get the method that you want to call
917 jmethodID boxUnpack = env->GetMethodID(clazz, "boxUnpack", "(Landroid/view/View;)V"); 927 jmethodID boxUnpack = env->GetMethodID(clazz, "boxUnpack", "(Landroid/view/View;)V");
933 HWND API dw_box_unpack_at_index(HWND box, int index) 943 HWND API dw_box_unpack_at_index(HWND box, int index)
934 { 944 {
935 JNIEnv *env; 945 JNIEnv *env;
936 HWND retval = 0; 946 HWND retval = 0;
937 947
938 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key))) { 948 if(box && (env = (JNIEnv *)pthread_getspecific(_dw_env_key))) {
939 // First get the class that contains the method you need to call 949 // First get the class that contains the method you need to call
940 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 950 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
941 // Get the method that you want to call 951 // Get the method that you want to call
942 jmethodID boxUnpackAtIndex = env->GetMethodID(clazz, "boxUnpackAtIndex", 952 jmethodID boxUnpackAtIndex = env->GetMethodID(clazz, "boxUnpackAtIndex",
943 "(Landroid/widget/LinearLayout;I)Landroid/view/View;"); 953 "(Landroid/widget/LinearLayout;I)Landroid/view/View;");
1080 */ 1090 */
1081 void API dw_entryfield_set_limit(HWND handle, ULONG limit) 1091 void API dw_entryfield_set_limit(HWND handle, ULONG limit)
1082 { 1092 {
1083 JNIEnv *env; 1093 JNIEnv *env;
1084 1094
1085 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 1095 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
1086 { 1096 {
1087 // First get the class that contains the method you need to call 1097 // First get the class that contains the method you need to call
1088 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1098 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1089 // Get the method that you want to call 1099 // Get the method that you want to call
1090 jmethodID entryfieldSetLimit = env->GetMethodID(clazz, "entryfieldSetLimit", 1100 jmethodID entryfieldSetLimit = env->GetMethodID(clazz, "entryfieldSetLimit",
1248 */ 1258 */
1249 unsigned int API dw_slider_get_pos(HWND handle) 1259 unsigned int API dw_slider_get_pos(HWND handle)
1250 { 1260 {
1251 JNIEnv *env; 1261 JNIEnv *env;
1252 1262
1253 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 1263 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
1254 { 1264 {
1255 // First get the class that contains the method you need to call 1265 // First get the class that contains the method you need to call
1256 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1266 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1257 // Get the method that you want to call 1267 // Get the method that you want to call
1258 jmethodID percentGetPos = env->GetMethodID(clazz, "percentGetPos", 1268 jmethodID percentGetPos = env->GetMethodID(clazz, "percentGetPos",
1319 */ 1329 */
1320 void API dw_scrollbar_set_range(HWND handle, unsigned int range, unsigned int visible) 1330 void API dw_scrollbar_set_range(HWND handle, unsigned int range, unsigned int visible)
1321 { 1331 {
1322 JNIEnv *env; 1332 JNIEnv *env;
1323 1333
1324 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 1334 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
1325 { 1335 {
1326 // First get the class that contains the method you need to call 1336 // First get the class that contains the method you need to call
1327 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1337 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1328 // Get the method that you want to call 1338 // Get the method that you want to call
1329 jmethodID percentSetRange = env->GetMethodID(clazz, "percentSetRange", 1339 jmethodID percentSetRange = env->GetMethodID(clazz, "percentSetRange",
1366 */ 1376 */
1367 void API dw_percent_set_pos(HWND handle, unsigned int position) 1377 void API dw_percent_set_pos(HWND handle, unsigned int position)
1368 { 1378 {
1369 JNIEnv *env; 1379 JNIEnv *env;
1370 1380
1371 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 1381 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
1372 { 1382 {
1373 // First get the class that contains the method you need to call 1383 // First get the class that contains the method you need to call
1374 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1384 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1375 // Get the method that you want to call 1385 // Get the method that you want to call
1376 jmethodID percentSetPos = env->GetMethodID(clazz, "percentSetPos", 1386 jmethodID percentSetPos = env->GetMethodID(clazz, "percentSetPos",
1417 */ 1427 */
1418 int API dw_checkbox_get(HWND handle) 1428 int API dw_checkbox_get(HWND handle)
1419 { 1429 {
1420 JNIEnv *env; 1430 JNIEnv *env;
1421 1431
1422 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 1432 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
1423 { 1433 {
1424 // First get the class that contains the method you need to call 1434 // First get the class that contains the method you need to call
1425 //jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1435 //jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1426 jclass clazz = env->FindClass(DW_CLASS_NAME); 1436 jclass clazz = env->FindClass(DW_CLASS_NAME);
1427 // Get the method that you want to call 1437 // Get the method that you want to call
1441 */ 1451 */
1442 void API dw_checkbox_set(HWND handle, int value) 1452 void API dw_checkbox_set(HWND handle, int value)
1443 { 1453 {
1444 JNIEnv *env; 1454 JNIEnv *env;
1445 1455
1446 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 1456 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
1447 { 1457 {
1448 // First get the class that contains the method you need to call 1458 // First get the class that contains the method you need to call
1449 //jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1459 //jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1450 jclass clazz = env->FindClass(DW_CLASS_NAME); 1460 jclass clazz = env->FindClass(DW_CLASS_NAME);
1451 // Get the method that you want to call 1461 // Get the method that you want to call
2788 * handle: Handle to the window. 2798 * handle: Handle to the window.
2789 * action: One of the DW_HTML_* constants. 2799 * action: One of the DW_HTML_* constants.
2790 */ 2800 */
2791 void API dw_html_action(HWND handle, int action) 2801 void API dw_html_action(HWND handle, int action)
2792 { 2802 {
2803 JNIEnv *env;
2804
2805 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2806 {
2807 // First get the class that contains the method you need to call
2808 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2809 // Get the method that you want to call
2810 jmethodID htmlAction = env->GetMethodID(clazz, "htmlAction",
2811 "(Landroid/webkit/WebView;I)V");
2812 // Call the method on the object
2813 env->CallVoidMethod(_dw_obj, htmlAction, handle, action);
2814 }
2793 } 2815 }
2794 2816
2795 /* 2817 /*
2796 * Render raw HTML code in the embedded HTML widget.. 2818 * Render raw HTML code in the embedded HTML widget..
2797 * Parameters: 2819 * Parameters:
2798 * handle: Handle to the window. 2820 * handle: Handle to the window.
2799 * string: String buffer containt HTML code to 2821 * string: String buffer containing HTML code to
2800 * be rendered. 2822 * be rendered.
2801 * Returns: 2823 * Returns:
2802 * DW_ERROR_NONE (0) on success. 2824 * DW_ERROR_NONE (0) on success.
2803 */ 2825 */
2804 int API dw_html_raw(HWND handle, const char *string) 2826 int API dw_html_raw(HWND handle, const char *string)
2805 { 2827 {
2828 JNIEnv *env;
2829
2830 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2831 {
2832 // Construct a String
2833 jstring jstr = env->NewStringUTF(string);
2834 // First get the class that contains the method you need to call
2835 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2836 // Get the method that you want to call
2837 jmethodID htmlRaw = env->GetMethodID(clazz, "htmlRaw",
2838 "(Landroid/webkit/WebView;Ljava/lang/String;)V");
2839 // Call the method on the object
2840 env->CallVoidMethod(_dw_obj, htmlRaw, handle, jstr);
2841 return DW_ERROR_NONE;
2842 }
2806 return DW_ERROR_GENERAL; 2843 return DW_ERROR_GENERAL;
2807 } 2844 }
2808 2845
2809 /* 2846 /*
2810 * Render file or web page in the embedded HTML widget.. 2847 * Render file or web page in the embedded HTML widget..
2815 * Returns: 2852 * Returns:
2816 * DW_ERROR_NONE (0) on success. 2853 * DW_ERROR_NONE (0) on success.
2817 */ 2854 */
2818 int API dw_html_url(HWND handle, const char *url) 2855 int API dw_html_url(HWND handle, const char *url)
2819 { 2856 {
2857 JNIEnv *env;
2858
2859 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2860 {
2861 // Construct a String
2862 jstring jstr = env->NewStringUTF(url);
2863 // First get the class that contains the method you need to call
2864 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2865 // Get the method that you want to call
2866 jmethodID htmlLoadURL = env->GetMethodID(clazz, "htmlLoadURL",
2867 "(Landroid/webkit/WebView;Ljava/lang/String;)V");
2868 // Call the method on the object
2869 env->CallVoidMethod(_dw_obj, htmlLoadURL, handle, jstr);
2870 return DW_ERROR_NONE;
2871 }
2820 return DW_ERROR_GENERAL; 2872 return DW_ERROR_GENERAL;
2821 } 2873 }
2822 2874
2823 /* 2875 /*
2824 * Executes the javascript contained in "script" in the HTML window. 2876 * Executes the javascript contained in "script" in the HTML window.
2830 * Returns: 2882 * Returns:
2831 * DW_ERROR_NONE (0) on success. 2883 * DW_ERROR_NONE (0) on success.
2832 */ 2884 */
2833 int API dw_html_javascript_run(HWND handle, const char *script, void *scriptdata) 2885 int API dw_html_javascript_run(HWND handle, const char *script, void *scriptdata)
2834 { 2886 {
2887 JNIEnv *env;
2888
2889 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2890 {
2891 // Construct a String
2892 jstring jstr = env->NewStringUTF(script);
2893 // First get the class that contains the method you need to call
2894 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2895 // Get the method that you want to call
2896 jmethodID htmlJavascriptRun = env->GetMethodID(clazz, "htmlJavascriptRun",
2897 "(Landroid/webkit/WebView;Ljava/lang/String;J)V");
2898 // Call the method on the object
2899 env->CallVoidMethod(_dw_obj, htmlJavascriptRun, handle, jstr, (jlong)scriptdata);
2900 return DW_ERROR_NONE;
2901 }
2835 return DW_ERROR_UNKNOWN; 2902 return DW_ERROR_UNKNOWN;
2836 } 2903 }
2837 2904
2838 /* 2905 /*
2839 * Create a new HTML window (widget) to be packed. 2906 * Create a new HTML window (widget) to be packed.
2842 * Returns: 2909 * Returns:
2843 * Handle to the created html widget or NULL on error. 2910 * Handle to the created html widget or NULL on error.
2844 */ 2911 */
2845 HWND API dw_html_new(unsigned long cid) 2912 HWND API dw_html_new(unsigned long cid)
2846 { 2913 {
2914 JNIEnv *env;
2915
2916 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
2917 {
2918 // First get the class that contains the method you need to call
2919 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
2920 // Get the method that you want to call
2921 jmethodID htmlNew = env->GetMethodID(clazz, "htmlNew",
2922 "(I)Landroid/webkit/WebView;");
2923 // Call the method on the object
2924 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, htmlNew, (int)cid));
2925 return result;
2926 }
2847 return 0; 2927 return 0;
2848 } 2928 }
2849 2929
2850 /* 2930 /*
2851 * Returns the current X and Y coordinates of the mouse pointer. 2931 * Returns the current X and Y coordinates of the mouse pointer.