comparison android/dw.cpp @ 2523:82cdb3ad7c25

Android: Implement the calendar widget using CalendarView. For some reason it is not actually displaying the correct date.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 09 May 2021 21:17:32 +0000
parents 66c490aa719d
children 9fd26efff9da
comparison
equal deleted inserted replaced
2522:66c490aa719d 2523:82cdb3ad7c25
3236 * Returns: 3236 * Returns:
3237 * Handle to the created calendar or NULL on error. 3237 * Handle to the created calendar or NULL on error.
3238 */ 3238 */
3239 HWND API dw_calendar_new(ULONG cid) 3239 HWND API dw_calendar_new(ULONG cid)
3240 { 3240 {
3241 JNIEnv *env;
3242
3243 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
3244 {
3245 // First get the class that contains the method you need to call
3246 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
3247 // Get the method that you want to call
3248 jmethodID calendarNew = env->GetMethodID(clazz, "calendarNew",
3249 "(I)Landroid/widget/CalendarView;");
3250 // Call the method on the object
3251 jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, calendarNew, (int)cid));
3252 return result;
3253 }
3241 return 0; 3254 return 0;
3242 } 3255 }
3243 3256
3244 /* 3257 /*
3245 * Sets the current date of a calendar. 3258 * Sets the current date of a calendar.
3247 * handle: The handle to the calendar returned by dw_calendar_new(). 3260 * handle: The handle to the calendar returned by dw_calendar_new().
3248 * year, month, day: To set the calendar to display. 3261 * year, month, day: To set the calendar to display.
3249 */ 3262 */
3250 void API dw_calendar_set_date(HWND handle, unsigned int year, unsigned int month, unsigned int day) 3263 void API dw_calendar_set_date(HWND handle, unsigned int year, unsigned int month, unsigned int day)
3251 { 3264 {
3265 JNIEnv *env;
3266
3267 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
3268 {
3269 time_t date;
3270 struct tm ts = {0};
3271
3272 // Convert to Unix time
3273 ts.tm_year = year - 1900;
3274 ts.tm_mon = month;
3275 ts.tm_mday = day;
3276 date = mktime(&ts);
3277
3278 // First get the class that contains the method you need to call
3279 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
3280 // Get the method that you want to call
3281 jmethodID calendarSetDate = env->GetMethodID(clazz, "calendarSetDate",
3282 "(Landroid/widget/CalendarView;J)V");
3283 // Call the method on the object
3284 env->CallVoidMethod(_dw_obj, calendarSetDate, handle, (jlong)date);
3285 }
3252 } 3286 }
3253 3287
3254 /* 3288 /*
3255 * Gets the year, month and day set in the calendar widget. 3289 * Gets the year, month and day set in the calendar widget.
3256 * Parameters: 3290 * Parameters:
3259 * month: Variable to store the month or NULL. 3293 * month: Variable to store the month or NULL.
3260 * day: Variable to store the day or NULL. 3294 * day: Variable to store the day or NULL.
3261 */ 3295 */
3262 void API dw_calendar_get_date(HWND handle, unsigned int *year, unsigned int *month, unsigned int *day) 3296 void API dw_calendar_get_date(HWND handle, unsigned int *year, unsigned int *month, unsigned int *day)
3263 { 3297 {
3298 JNIEnv *env;
3299
3300 if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
3301 {
3302 time_t date;
3303 struct tm ts;
3304
3305 // First get the class that contains the method you need to call
3306 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
3307 // Get the method that you want to call
3308 jmethodID calendarGetDate = env->GetMethodID(clazz, "calendarGetDate",
3309 "(Landroid/widget/CalendarView;)J");
3310 // Call the method on the object
3311 date = (time_t)env->CallLongMethod(_dw_obj, calendarGetDate, handle);
3312 ts = *localtime(&date);
3313 if(year)
3314 *year = ts.tm_year + 1900;
3315 if(month)
3316 *month = ts.tm_mon;
3317 if(day)
3318 *day = ts.tm_mday;
3319 }
3264 } 3320 }
3265 3321
3266 /* 3322 /*
3267 * Causes the embedded HTML widget to take action. 3323 * Causes the embedded HTML widget to take action.
3268 * Parameters: 3324 * Parameters: