Path: blob/master/src/java.desktop/macosx/native/libosxui/JRSUIController.m
41152 views
/*1* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#import "JNIUtilities.h"26#import <JavaRuntimeSupport/JavaRuntimeSupport.h>2728#import "apple_laf_JRSUIControl.h"29#import "apple_laf_JRSUIConstants_DoubleValue.h"30#import "apple_laf_JRSUIConstants_Hit.h"3132#import "JRSUIConstantSync.h"333435static JRSUIRendererRef gRenderer;3637/*38* Class: apple_laf_JRSUIControl39* Method: initNativeJRSUI40* Signature: ()I41*/42JNIEXPORT jint JNICALL Java_apple_laf_JRSUIControl_initNativeJRSUI43(JNIEnv *env, jclass clazz)44{45BOOL coherent = _InitializeJRSProperties();46if (!coherent) return apple_laf_JRSUIControl_INCOHERENT;4748gRenderer = JRSUIRendererCreate();49if (gRenderer == NULL) return apple_laf_JRSUIControl_NULL_PTR;5051return apple_laf_JRSUIControl_SUCCESS;52}5354/*55* Class: apple_laf_JRSUIControl56* Method: getPtrOfBuffer57* Signature: (Ljava/nio/ByteBuffer;)J58*/59JNIEXPORT jlong JNICALL Java_apple_laf_JRSUIControl_getPtrOfBuffer60(JNIEnv *env, jclass clazz, jobject byteBuffer)61{62char *byteBufferPtr = (*env)->GetDirectBufferAddress(env, byteBuffer);63if (byteBufferPtr == NULL) return 0L;64return ptr_to_jlong(byteBufferPtr); // GC65}6667/*68* Class: apple_laf_JRSUIControl69* Method: getCFDictionary70* Signature: (Z)J71*/72JNIEXPORT jlong JNICALL Java_apple_laf_JRSUIControl_getCFDictionary73(JNIEnv *env, jclass clazz, jboolean isFlipped)74{75return ptr_to_jlong(JRSUIControlCreate(isFlipped));76}7778/*79* Class: apple_laf_JRSUIControl80* Method: disposeCFDictionary81* Signature: (J)V82*/83JNIEXPORT void JNICALL Java_apple_laf_JRSUIControl_disposeCFDictionary84(JNIEnv *env, jclass clazz, jlong controlPtr)85{86void *ptr = jlong_to_ptr(controlPtr);87if (!ptr) return;88JRSUIControlRelease((JRSUIControlRef)ptr);89}909192static inline void *getValueFor93(jbyte code, UInt8 *changeBuffer, size_t *dataSizePtr)94{95switch (code)96{97case apple_laf_JRSUIConstants_DoubleValue_TYPE_CODE:98*dataSizePtr = sizeof(jdouble);99jdouble doubleValue = (*(jdouble *)changeBuffer);100return (void *)CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &doubleValue);101}102103return NULL;104}105106static inline jint syncChangesToControl107(JRSUIControlRef control, UInt8 *changeBuffer)108{109UInt8 *endOfBuffer = changeBuffer + apple_laf_JRSUIControl_NIO_BUFFER_SIZE;110111while (changeBuffer < endOfBuffer)112{113// dereference the pointer to the constant that was stored as a jlong in the byte buffer114CFStringRef key = (CFStringRef)jlong_to_ptr(*((jlong *)changeBuffer));115if (key == NULL) return apple_laf_JRSUIControl_SUCCESS;116changeBuffer += sizeof(jlong);117118jbyte code = *((jbyte *)changeBuffer);119changeBuffer += sizeof(jbyte);120121size_t dataSize;122void *value = (void *)getValueFor(code, changeBuffer, &dataSize);123if (value == NULL) {124NSLog(@"null pointer for %@ for value %d", key, (int)code);125126return apple_laf_JRSUIControl_NULL_PTR;127}128129changeBuffer += dataSize;130JRSUIControlSetValueByKey(control, key, value);131CFRelease(value);132}133134return apple_laf_JRSUIControl_SUCCESS;135}136137static inline jint doSyncChanges138(JNIEnv *env, jlong controlPtr, jlong byteBufferPtr)139{140JRSUIControlRef control = (JRSUIControlRef)jlong_to_ptr(controlPtr);141UInt8 *changeBuffer = (UInt8 *)jlong_to_ptr(byteBufferPtr);142143return syncChangesToControl(control, changeBuffer);144}145146/*147* Class: apple_laf_JRSUIControl148* Method: syncChanges149* Signature: (JJ)I150*/151JNIEXPORT jint JNICALL Java_apple_laf_JRSUIControl_syncChanges152(JNIEnv *env, jclass clazz, jlong controlPtr, jlong byteBufferPtr)153{154return doSyncChanges(env, controlPtr, byteBufferPtr);155}156157static inline jint doPaintCGContext(CGContextRef cgRef, jlong controlPtr, jlong oldProperties, jlong newProperties, jdouble x, jdouble y, jdouble w, jdouble h)158{159JRSUIControlRef control = (JRSUIControlRef)jlong_to_ptr(controlPtr);160_SyncEncodedProperties(control, oldProperties, newProperties);161CGRect bounds = CGRectMake(x, y, w, h);162JRSUIControlDraw(gRenderer, control, cgRef, bounds);163return 0;164}165166/*167* Class: apple_laf_JRSUIControl168* Method: paintToCGContext169* Signature: (JJJJDDDD)I170*/171JNIEXPORT jint JNICALL Java_apple_laf_JRSUIControl_paintToCGContext172(JNIEnv *env, jclass clazz, jlong cgContextPtr, jlong controlPtr, jlong oldProperties, jlong newProperties, jdouble x, jdouble y, jdouble w, jdouble h)173{174return doPaintCGContext((CGContextRef)jlong_to_ptr(cgContextPtr), controlPtr, oldProperties, newProperties, x, y, w, h);175}176177/*178* Class: apple_laf_JRSUIControl179* Method: paintChangesToCGContext180* Signature: (JJJJDDDDJ)I181*/182JNIEXPORT jint JNICALL Java_apple_laf_JRSUIControl_paintChangesToCGContext183(JNIEnv *env, jclass clazz, jlong cgContextPtr, jlong controlPtr, jlong oldProperties, jlong newProperties, jdouble x, jdouble y, jdouble w, jdouble h, jlong changes)184{185int syncStatus = doSyncChanges(env, controlPtr, changes);186if (syncStatus != apple_laf_JRSUIControl_SUCCESS) return syncStatus;187188return doPaintCGContext((CGContextRef)jlong_to_ptr(cgContextPtr), controlPtr, oldProperties, newProperties, x, y, w, h);189}190191static inline jint doPaintImage192(JNIEnv *env, jlong controlPtr, jlong oldProperties, jlong newProperties, jintArray data, jint imgW, jint imgH, jdouble x, jdouble y, jdouble w, jdouble h)193{194jboolean isCopy = JNI_FALSE;195void *rawPixelData = (*env)->GetPrimitiveArrayCritical(env, data, &isCopy);196if (!rawPixelData) return apple_laf_JRSUIControl_NULL_PTR;197198CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();199CGContextRef cgRef = CGBitmapContextCreate(rawPixelData, imgW, imgH, 8, imgW * 4, colorspace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);200CGColorSpaceRelease(colorspace);201CGContextScaleCTM(cgRef, imgW/(w + x + x) , imgH/(h + y + y));202203jint status = doPaintCGContext(cgRef, controlPtr, oldProperties, newProperties, x, y, w, h);204CGContextRelease(cgRef);205206(*env)->ReleasePrimitiveArrayCritical(env, data, rawPixelData, 0);207208return status == noErr ? apple_laf_JRSUIControl_SUCCESS : status;209}210211/*212* Class: apple_laf_JRSUIControl213* Method: paintImage214* Signature: ([IIIJJJDDDD)I215*/216JNIEXPORT jint JNICALL Java_apple_laf_JRSUIControl_paintImage217(JNIEnv *env, jclass clazz, jintArray data, jint imgW, jint imgH, jlong controlPtr, jlong oldProperties, jlong newProperties, jdouble x, jdouble y, jdouble w, jdouble h)218{219return doPaintImage(env, controlPtr, oldProperties, newProperties, data, imgW, imgH, x, y, w, h);220}221222/*223* Class: apple_laf_JRSUIControl224* Method: paintChangesImage225* Signature: ([IIIJJJDDDDJ)I226*/227JNIEXPORT jint JNICALL Java_apple_laf_JRSUIControl_paintChangesImage228(JNIEnv *env, jclass clazz, jintArray data, jint imgW, jint imgH, jlong controlPtr, jlong oldProperties, jlong newProperties, jdouble x, jdouble y, jdouble w, jdouble h, jlong changes)229{230int syncStatus = doSyncChanges(env, controlPtr, changes);231if (syncStatus != apple_laf_JRSUIControl_SUCCESS) return syncStatus;232233return doPaintImage(env, controlPtr, oldProperties, newProperties, data, imgW, imgH, x, y, w, h);234}235236/*237* Class: apple_laf_JRSUIControl238* Method: getNativeHitPart239* Signature: (JJJDDDDDD)I240*/241JNIEXPORT jint JNICALL Java_apple_laf_JRSUIControl_getNativeHitPart242(JNIEnv *env, jclass clazz, jlong controlPtr, jlong oldProperties, jlong newProperties, jdouble x, jdouble y, jdouble w, jdouble h, jdouble pointX, jdouble pointY)243{244JRSUIControlRef control = (JRSUIControlRef)jlong_to_ptr(controlPtr);245_SyncEncodedProperties(control, oldProperties, newProperties);246247CGRect bounds = CGRectMake(x, y, w, h);248CGPoint point = CGPointMake(pointX, pointY);249250return JRSUIControlGetHitPart(gRenderer, control, bounds, point);251}252253/*254* Class: apple_laf_JRSUIUtils_ScrollBar255* Method: shouldUseScrollToClick256* Signature: ()Z257*/258JNIEXPORT jboolean JNICALL Java_apple_laf_JRSUIUtils_00024ScrollBar_shouldUseScrollToClick259(JNIEnv *env, jclass clazz)260{261return JRSUIControlShouldScrollToClick();262}263264/*265* Class: apple_laf_JRSUIControl266* Method: getNativePartBounds267* Signature: ([DJJJDDDDI)V268*/269JNIEXPORT void JNICALL Java_apple_laf_JRSUIControl_getNativePartBounds270(JNIEnv *env, jclass clazz, jdoubleArray rectArray, jlong controlPtr, jlong oldProperties, jlong newProperties, jdouble x, jdouble y, jdouble w, jdouble h, jint part)271{272JRSUIControlRef control = (JRSUIControlRef)jlong_to_ptr(controlPtr);273_SyncEncodedProperties(control, oldProperties, newProperties);274275CGRect frame = CGRectMake(x, y, w, h);276CGRect partBounds = JRSUIControlGetScrollBarPartBounds(control, frame, part);277278jdouble *rect = (*env)->GetPrimitiveArrayCritical(env, rectArray, NULL);279rect[0] = partBounds.origin.x;280rect[1] = partBounds.origin.y;281rect[2] = partBounds.size.width;282rect[3] = partBounds.size.height;283(*env)->ReleasePrimitiveArrayCritical(env, rectArray, rect, 0);284}285286/*287* Class: apple_laf_JRSUIControl288* Method: getNativeScrollBarOffsetChange289* Signature: (JJJDDDDIII)D290*/291JNIEXPORT jdouble JNICALL Java_apple_laf_JRSUIControl_getNativeScrollBarOffsetChange292(JNIEnv *env, jclass clazz, jlong controlPtr, jlong oldProperties, jlong newProperties, jdouble x, jdouble y, jdouble w, jdouble h, jint offset, jint visibleAmount, jint extent)293{294JRSUIControlRef control = (JRSUIControlRef)jlong_to_ptr(controlPtr);295_SyncEncodedProperties(control, oldProperties, newProperties);296297CGRect frame = CGRectMake(x, y, w, h);298return (jdouble)JRSUIControlGetScrollBarOffsetFor(control, frame, offset, visibleAmount, extent);299}300301302