Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTView.m
41152 views
/*1* Copyright (c) 2011, 2021, 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 "jni_util.h"26#import "CGLGraphicsConfig.h"27#import "AWTView.h"28#import "AWTWindow.h"29#import "JavaComponentAccessibility.h"30#import "JavaTextAccessibility.h"31#import "JavaAccessibilityUtilities.h"32#import "GeomUtilities.h"33#import "ThreadUtilities.h"34#import "JNIUtilities.h"3536#import <Carbon/Carbon.h>3738// keyboard layout39static NSString *kbdLayout;4041@interface AWTView()42@property (retain) CDropTarget *_dropTarget;43@property (retain) CDragSource *_dragSource;4445-(void) deliverResize: (NSRect) rect;46-(void) resetTrackingArea;47-(void) deliverJavaKeyEventHelper: (NSEvent*) event;48-(BOOL) isCodePointInUnicodeBlockNeedingIMEvent: (unichar) codePoint;49-(NSMutableString *) parseString : (id) complexString;50@end5152// Uncomment this line to see fprintfs of each InputMethod API being called on this View53//#define IM_DEBUG TRUE54//#define EXTRA_DEBUG5556static BOOL shouldUsePressAndHold() {57return YES;58}5960@implementation AWTView6162@synthesize _dropTarget;63@synthesize _dragSource;64@synthesize cglLayer;65@synthesize mouseIsOver;6667// Note: Must be called on main (AppKit) thread only68- (id) initWithRect: (NSRect) rect69platformView: (jobject) cPlatformView70windowLayer: (CALayer*) windowLayer71{72AWT_ASSERT_APPKIT_THREAD;73// Initialize ourselves74self = [super initWithFrame: rect];75if (self == nil) return self;7677m_cPlatformView = cPlatformView;78fInputMethodLOCKABLE = NULL;79fKeyEventsNeeded = NO;80fProcessingKeystroke = NO;8182fEnablePressAndHold = shouldUsePressAndHold();83fInPressAndHold = NO;84fPAHNeedsToSelect = NO;8586mouseIsOver = NO;87[self resetTrackingArea];88[self setAutoresizesSubviews:NO];8990if (windowLayer != nil) {91self.cglLayer = windowLayer;92//Layer hosting view93[self setLayer: cglLayer];94[self setWantsLayer: YES];95//Layer backed view96//[self.layer addSublayer: (CALayer *)cglLayer];97//[self setLayerContentsRedrawPolicy: NSViewLayerContentsRedrawDuringViewResize];98//[self setLayerContentsPlacement: NSViewLayerContentsPlacementTopLeft];99//[self setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];100}101102return self;103}104105- (void) dealloc {106AWT_ASSERT_APPKIT_THREAD;107108self.cglLayer = nil;109110JNIEnv *env = [ThreadUtilities getJNIEnvUncached];111(*env)->DeleteWeakGlobalRef(env, m_cPlatformView);112m_cPlatformView = NULL;113114if (fInputMethodLOCKABLE != NULL)115{116JNIEnv *env = [ThreadUtilities getJNIEnvUncached];117118(*env)->DeleteGlobalRef(env, fInputMethodLOCKABLE);119fInputMethodLOCKABLE = NULL;120}121122if (rolloverTrackingArea != nil) {123[self removeTrackingArea:rolloverTrackingArea];124[rolloverTrackingArea release];125rolloverTrackingArea = nil;126}127128[super dealloc];129}130131- (void) viewDidMoveToWindow {132AWT_ASSERT_APPKIT_THREAD;133134[AWTToolkit eventCountPlusPlus];135136[ThreadUtilities performOnMainThreadWaiting:NO block:^() {137[[self window] makeFirstResponder: self];138}];139if ([self window] != NULL) {140[self resetTrackingArea];141}142}143144- (BOOL) acceptsFirstMouse: (NSEvent *)event {145return YES;146}147148- (BOOL) acceptsFirstResponder {149return YES;150}151152- (BOOL) becomeFirstResponder {153return YES;154}155156- (BOOL) preservesContentDuringLiveResize {157return YES;158}159160/*161* Automatically triggered functions.162*/163164- (void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize {165[super resizeWithOldSuperviewSize: oldBoundsSize];166[self deliverResize: [self frame]];167}168169/*170* MouseEvents support171*/172173- (void) mouseDown: (NSEvent *)event {174NSInputManager *inputManager = [NSInputManager currentInputManager];175if ([inputManager wantsToHandleMouseEvents]) {176#if IM_DEBUG177NSLog(@"-> IM wants to handle event");178#endif179if (![inputManager handleMouseEvent:event]) {180[self deliverJavaMouseEvent: event];181} else {182#if IM_DEBUG183NSLog(@"-> Event was handled.");184#endif185}186} else {187#if IM_DEBUG188NSLog(@"-> IM does not want to handle event");189#endif190[self deliverJavaMouseEvent: event];191}192}193194- (void) mouseUp: (NSEvent *)event {195[self deliverJavaMouseEvent: event];196}197198- (void) rightMouseDown: (NSEvent *)event {199[self deliverJavaMouseEvent: event];200}201202- (void) rightMouseUp: (NSEvent *)event {203[self deliverJavaMouseEvent: event];204}205206- (void) otherMouseDown: (NSEvent *)event {207[self deliverJavaMouseEvent: event];208}209210- (void) otherMouseUp: (NSEvent *)event {211[self deliverJavaMouseEvent: event];212}213214- (void) mouseMoved: (NSEvent *)event {215// TODO: better way to redirect move events to the "under" view216217NSPoint eventLocation = [event locationInWindow];218NSPoint localPoint = [self convertPoint: eventLocation fromView: nil];219220if ([self mouse: localPoint inRect: [self bounds]]) {221[self deliverJavaMouseEvent: event];222} else {223[[self nextResponder] mouseDown:event];224}225}226227- (void) mouseDragged: (NSEvent *)event {228[self deliverJavaMouseEvent: event];229}230231- (void) rightMouseDragged: (NSEvent *)event {232[self deliverJavaMouseEvent: event];233}234235- (void) otherMouseDragged: (NSEvent *)event {236[self deliverJavaMouseEvent: event];237}238239- (void) mouseEntered: (NSEvent *)event {240[[self window] setAcceptsMouseMovedEvents:YES];241//[[self window] makeFirstResponder:self];242[self deliverJavaMouseEvent: event];243}244245- (void) mouseExited: (NSEvent *)event {246[[self window] setAcceptsMouseMovedEvents:NO];247[self deliverJavaMouseEvent: event];248//Restore the cursor back.249//[CCursorManager _setCursor: [NSCursor arrowCursor]];250}251252- (void) scrollWheel: (NSEvent*) event {253[self deliverJavaMouseEvent: event];254}255256/*257* KeyEvents support258*/259260- (void) keyDown: (NSEvent *)event {261fProcessingKeystroke = YES;262fKeyEventsNeeded = YES;263264// Allow TSM to look at the event and potentially send back NSTextInputClient messages.265[self interpretKeyEvents:[NSArray arrayWithObject:event]];266267if (fEnablePressAndHold && [event willBeHandledByComplexInputMethod] &&268fInputMethodLOCKABLE)269{270fProcessingKeystroke = NO;271if (!fInPressAndHold) {272fInPressAndHold = YES;273fPAHNeedsToSelect = YES;274} else {275// Abandon input to reset IM and unblock input after canceling276// input accented symbols277278switch([event keyCode]) {279case kVK_Escape:280case kVK_Delete:281case kVK_Return:282case kVK_ForwardDelete:283case kVK_PageUp:284case kVK_PageDown:285case kVK_DownArrow:286case kVK_UpArrow:287case kVK_Home:288case kVK_End:289[self abandonInput];290break;291}292}293return;294}295296NSString *eventCharacters = [event characters];297BOOL isDeadKey = (eventCharacters != nil && [eventCharacters length] == 0);298299if ((![self hasMarkedText] && fKeyEventsNeeded) || isDeadKey) {300[self deliverJavaKeyEventHelper: event];301}302303fProcessingKeystroke = NO;304}305306- (void) keyUp: (NSEvent *)event {307[self deliverJavaKeyEventHelper: event];308}309310- (void) flagsChanged: (NSEvent *)event {311[self deliverJavaKeyEventHelper: event];312}313314- (BOOL) performKeyEquivalent: (NSEvent *) event {315// if IM is active key events should be ignored316if (![self hasMarkedText] && !fInPressAndHold) {317[self deliverJavaKeyEventHelper: event];318}319320// Workaround for 8020209: special case for "Cmd =" and "Cmd ."321// because Cocoa calls performKeyEquivalent twice for these keystrokes322NSUInteger modFlags = [event modifierFlags] &323(NSCommandKeyMask | NSAlternateKeyMask | NSShiftKeyMask | NSControlKeyMask);324if (modFlags == NSCommandKeyMask) {325NSString *eventChars = [event charactersIgnoringModifiers];326if ([eventChars length] == 1) {327unichar ch = [eventChars characterAtIndex:0];328if (ch == '=' || ch == '.') {329[[NSApp mainMenu] performKeyEquivalent: event];330return YES;331}332}333334}335336return NO;337}338339/**340* Utility methods and accessors341*/342343-(void) deliverJavaMouseEvent: (NSEvent *) event {344BOOL isEnabled = YES;345NSWindow* window = [self window];346if ([window isKindOfClass: [AWTWindow_Panel class]] || [window isKindOfClass: [AWTWindow_Normal class]]) {347isEnabled = [(AWTWindow*)[window delegate] isEnabled];348}349350if (!isEnabled) {351return;352}353354NSEventType type = [event type];355356// check synthesized mouse entered/exited events357if ((type == NSMouseEntered && mouseIsOver) || (type == NSMouseExited && !mouseIsOver)) {358return;359}else if ((type == NSMouseEntered && !mouseIsOver) || (type == NSMouseExited && mouseIsOver)) {360mouseIsOver = !mouseIsOver;361}362363[AWTToolkit eventCountPlusPlus];364365JNIEnv *env = [ThreadUtilities getJNIEnv];366367NSPoint eventLocation = [event locationInWindow];368NSPoint localPoint = [self convertPoint: eventLocation fromView: nil];369NSPoint absP = [NSEvent mouseLocation];370371// Convert global numbers between Cocoa's coordinate system and Java.372// TODO: need consitent way for doing that both with global as well as with local coordinates.373// The reason to do it here is one more native method for getting screen dimension otherwise.374375NSRect screenRect = [[[NSScreen screens] objectAtIndex:0] frame];376absP.y = screenRect.size.height - absP.y;377jint clickCount;378379if (type == NSMouseEntered ||380type == NSMouseExited ||381type == NSScrollWheel ||382type == NSMouseMoved) {383clickCount = 0;384} else {385clickCount = [event clickCount];386}387388jdouble deltaX = [event deltaX];389jdouble deltaY = [event deltaY];390if ([AWTToolkit hasPreciseScrollingDeltas: event]) {391deltaX = [event scrollingDeltaX] * 0.1;392deltaY = [event scrollingDeltaY] * 0.1;393}394395DECLARE_CLASS(jc_NSEvent, "sun/lwawt/macosx/NSEvent");396DECLARE_METHOD(jctor_NSEvent, jc_NSEvent, "<init>", "(IIIIIIIIDDI)V");397jobject jEvent = (*env)->NewObject(env, jc_NSEvent, jctor_NSEvent,398[event type],399[event modifierFlags],400clickCount,401[event buttonNumber],402(jint)localPoint.x, (jint)localPoint.y,403(jint)absP.x, (jint)absP.y,404deltaY,405deltaX,406[AWTToolkit scrollStateWithEvent: event]);407CHECK_NULL(jEvent);408409DECLARE_CLASS(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");410DECLARE_METHOD(jm_deliverMouseEvent, jc_PlatformView, "deliverMouseEvent", "(Lsun/lwawt/macosx/NSEvent;)V");411jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);412if (!(*env)->IsSameObject(env, jlocal, NULL)) {413(*env)->CallVoidMethod(env, jlocal, jm_deliverMouseEvent, jEvent);414CHECK_EXCEPTION();415(*env)->DeleteLocalRef(env, jlocal);416}417(*env)->DeleteLocalRef(env, jEvent);418}419420- (void) resetTrackingArea {421if (rolloverTrackingArea != nil) {422[self removeTrackingArea:rolloverTrackingArea];423[rolloverTrackingArea release];424}425426int options = (NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited |427NSTrackingMouseMoved | NSTrackingEnabledDuringMouseDrag);428429rolloverTrackingArea = [[NSTrackingArea alloc] initWithRect:[self visibleRect]430options: options431owner:self432userInfo:nil433];434[self addTrackingArea:rolloverTrackingArea];435}436437- (void)updateTrackingAreas {438[super updateTrackingAreas];439[self resetTrackingArea];440}441442- (void) resetCursorRects {443[super resetCursorRects];444[self resetTrackingArea];445}446447-(void) deliverJavaKeyEventHelper: (NSEvent *) event {448static NSEvent* sLastKeyEvent = nil;449if (event == sLastKeyEvent) {450// The event is repeatedly delivered by keyDown: after performKeyEquivalent:451return;452}453[sLastKeyEvent release];454sLastKeyEvent = [event retain];455456[AWTToolkit eventCountPlusPlus];457JNIEnv *env = [ThreadUtilities getJNIEnv];458459jstring characters = NULL;460jstring charactersIgnoringModifiers = NULL;461if ([event type] != NSFlagsChanged) {462characters = NSStringToJavaString(env, [event characters]);463charactersIgnoringModifiers = NSStringToJavaString(env, [event charactersIgnoringModifiers]);464}465466DECLARE_CLASS(jc_NSEvent, "sun/lwawt/macosx/NSEvent");467DECLARE_METHOD(jctor_NSEvent, jc_NSEvent, "<init>", "(IISLjava/lang/String;Ljava/lang/String;)V");468jobject jEvent = (*env)->NewObject(env, jc_NSEvent, jctor_NSEvent,469[event type],470[event modifierFlags],471[event keyCode],472characters,473charactersIgnoringModifiers);474CHECK_NULL(jEvent);475476DECLARE_CLASS(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");477DECLARE_METHOD(jm_deliverKeyEvent, jc_PlatformView,478"deliverKeyEvent", "(Lsun/lwawt/macosx/NSEvent;)V");479jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);480if (!(*env)->IsSameObject(env, jlocal, NULL)) {481(*env)->CallVoidMethod(env, jlocal, jm_deliverKeyEvent, jEvent);482CHECK_EXCEPTION();483(*env)->DeleteLocalRef(env, jlocal);484}485if (characters != NULL) {486(*env)->DeleteLocalRef(env, characters);487}488(*env)->DeleteLocalRef(env, jEvent);489}490491-(void) deliverResize: (NSRect) rect {492jint x = (jint) rect.origin.x;493jint y = (jint) rect.origin.y;494jint w = (jint) rect.size.width;495jint h = (jint) rect.size.height;496JNIEnv *env = [ThreadUtilities getJNIEnv];497DECLARE_CLASS(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");498DECLARE_METHOD(jm_deliverResize, jc_PlatformView, "deliverResize", "(IIII)V");499500jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);501if (!(*env)->IsSameObject(env, jlocal, NULL)) {502(*env)->CallVoidMethod(env, jlocal, jm_deliverResize, x,y,w,h);503CHECK_EXCEPTION();504(*env)->DeleteLocalRef(env, jlocal);505}506}507508509- (void) drawRect:(NSRect)dirtyRect {510AWT_ASSERT_APPKIT_THREAD;511512[super drawRect:dirtyRect];513JNIEnv *env = [ThreadUtilities getJNIEnv];514if (env != NULL) {515/*516if ([self inLiveResize]) {517NSRect rs[4];518NSInteger count;519[self getRectsExposedDuringLiveResize:rs count:&count];520for (int i = 0; i < count; i++) {521JNU_CallMethodByName(env, NULL, [m_awtWindow cPlatformView],522"deliverWindowDidExposeEvent", "(FFFF)V",523(jfloat)rs[i].origin.x, (jfloat)rs[i].origin.y,524(jfloat)rs[i].size.width, (jfloat)rs[i].size.height);525if ((*env)->ExceptionOccurred(env)) {526(*env)->ExceptionDescribe(env);527(*env)->ExceptionClear(env);528}529}530} else {531*/532DECLARE_CLASS(jc_CPlatformView, "sun/lwawt/macosx/CPlatformView");533DECLARE_METHOD(jm_deliverWindowDidExposeEvent, jc_CPlatformView, "deliverWindowDidExposeEvent", "()V");534jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);535if (!(*env)->IsSameObject(env, jlocal, NULL)) {536(*env)->CallVoidMethod(env, jlocal, jm_deliverWindowDidExposeEvent);537CHECK_EXCEPTION();538(*env)->DeleteLocalRef(env, jlocal);539}540/*541}542*/543}544}545546-(BOOL) isCodePointInUnicodeBlockNeedingIMEvent: (unichar) codePoint {547if ((codePoint == 0x0024) || (codePoint == 0x00A3) ||548(codePoint == 0x00A5) ||549((codePoint >= 0x20A3) && (codePoint <= 0x20BF)) ||550((codePoint >= 0x3000) && (codePoint <= 0x303F)) ||551((codePoint >= 0xFF00) && (codePoint <= 0xFFEF))) {552// Code point is in 'CJK Symbols and Punctuation' or553// 'Halfwidth and Fullwidth Forms' Unicode block or554// currency symbols unicode555return YES;556}557return NO;558}559560-(NSMutableString *) parseString : (id) complexString {561if ([complexString isKindOfClass:[NSString class]]) {562return [complexString mutableCopy];563}564else {565return [complexString mutableString];566}567}568569// NSAccessibility support570- (jobject)awtComponent:(JNIEnv*)env571{572DECLARE_CLASS_RETURN(jc_CPlatformView, "sun/lwawt/macosx/CPlatformView", NULL);573DECLARE_FIELD_RETURN(jf_Peer, jc_CPlatformView, "peer", "Lsun/lwawt/LWWindowPeer;", NULL);574if ((env == NULL) || (m_cPlatformView == NULL)) {575NSLog(@"Apple AWT : Error AWTView:awtComponent given bad parameters.");576NSLog(@"%@",[NSThread callStackSymbols]);577return NULL;578}579580jobject peer = NULL;581jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);582if (!(*env)->IsSameObject(env, jlocal, NULL)) {583peer = (*env)->GetObjectField(env, jlocal, jf_Peer);584(*env)->DeleteLocalRef(env, jlocal);585}586DECLARE_CLASS_RETURN(jc_LWWindowPeer, "sun/lwawt/LWWindowPeer", NULL);587DECLARE_FIELD_RETURN(jf_Target, jc_LWWindowPeer, "target", "Ljava/awt/Component;", NULL);588if (peer == NULL) {589NSLog(@"Apple AWT : Error AWTView:awtComponent got null peer from CPlatformView");590NSLog(@"%@",[NSThread callStackSymbols]);591return NULL;592}593jobject comp = (*env)->GetObjectField(env, peer, jf_Target);594(*env)->DeleteLocalRef(env, peer);595return comp;596}597598+ (AWTView *) awtView:(JNIEnv*)env ofAccessible:(jobject)jaccessible599{600DECLARE_CLASS_RETURN(sjc_CAccessibility, "sun/lwawt/macosx/CAccessibility", NULL);601DECLARE_STATIC_METHOD_RETURN(jm_getAWTView, sjc_CAccessibility, "getAWTView", "(Ljavax/accessibility/Accessible;)J", NULL);602603jlong jptr = (*env)->CallStaticLongMethod(env, sjc_CAccessibility, jm_getAWTView, jaccessible);604CHECK_EXCEPTION();605if (jptr == 0) return nil;606607return (AWTView *)jlong_to_ptr(jptr);608}609610- (id)getAxData:(JNIEnv*)env611{612jobject jcomponent = [self awtComponent:env];613id ax = [[[JavaComponentAccessibility alloc] initWithParent:self withEnv:env withAccessible:jcomponent withIndex:-1 withView:self withJavaRole:nil] autorelease];614(*env)->DeleteLocalRef(env, jcomponent);615return ax;616}617618- (NSArray *)accessibilityAttributeNames619{620return [[super accessibilityAttributeNames] arrayByAddingObject:NSAccessibilityChildrenAttribute];621}622623// NSAccessibility messages624// attribute methods625- (id)accessibilityAttributeValue:(NSString *)attribute626{627AWT_ASSERT_APPKIT_THREAD;628629if ([attribute isEqualToString:NSAccessibilityChildrenAttribute])630{631JNIEnv *env = [ThreadUtilities getJNIEnv];632633(*env)->PushLocalFrame(env, 4);634635id result = NSAccessibilityUnignoredChildrenForOnlyChild([self getAxData:env]);636637(*env)->PopLocalFrame(env, NULL);638639return result;640}641else642{643return [super accessibilityAttributeValue:attribute];644}645}646- (BOOL)accessibilityIsIgnored647{648return YES;649}650651- (id)accessibilityHitTest:(NSPoint)point652{653AWT_ASSERT_APPKIT_THREAD;654JNIEnv *env = [ThreadUtilities getJNIEnv];655656(*env)->PushLocalFrame(env, 4);657658id result = [[self getAxData:env] accessibilityHitTest:point withEnv:env];659660(*env)->PopLocalFrame(env, NULL);661662return result;663}664665- (id)accessibilityFocusedUIElement666{667AWT_ASSERT_APPKIT_THREAD;668669JNIEnv *env = [ThreadUtilities getJNIEnv];670671(*env)->PushLocalFrame(env, 4);672673id result = [[self getAxData:env] accessibilityFocusedUIElement];674675(*env)->PopLocalFrame(env, NULL);676677return result;678}679680// --- Services menu support for lightweights ---681682// finds the focused accessible element, and if it is a text element, obtains the text from it683- (NSString *)accessibleSelectedText684{685id focused = [self accessibilityFocusedUIElement];686if (![focused isKindOfClass:[JavaTextAccessibility class]]) return nil;687return [(JavaTextAccessibility *)focused accessibilitySelectedTextAttribute];688}689690// same as above, but converts to RTFD691- (NSData *)accessibleSelectedTextAsRTFD692{693NSString *selectedText = [self accessibleSelectedText];694NSAttributedString *styledText = [[NSAttributedString alloc] initWithString:selectedText];695NSData *rtfdData = [styledText RTFDFromRange:NSMakeRange(0, [styledText length])696documentAttributes:697@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType}];698[styledText release];699return rtfdData;700}701702// finds the focused accessible element, and if it is a text element, sets the text in it703- (BOOL)replaceAccessibleTextSelection:(NSString *)text704{705id focused = [self accessibilityFocusedUIElement];706if (![focused isKindOfClass:[JavaTextAccessibility class]]) return NO;707[(JavaTextAccessibility *)focused accessibilitySetSelectedTextAttribute:text];708return YES;709}710711// called for each service in the Services menu - only handle text for now712- (id)validRequestorForSendType:(NSString *)sendType returnType:(NSString *)returnType713{714if ([[self window] firstResponder] != self) return nil; // let AWT components handle themselves715716if ([sendType isEqual:NSStringPboardType] || [returnType isEqual:NSStringPboardType]) {717NSString *selectedText = [self accessibleSelectedText];718if (selectedText) return self;719}720721return nil;722}723724// fetch text from Java and hand off to the service725- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard types:(NSArray *)types726{727if ([types containsObject:NSStringPboardType])728{729[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];730return [pboard setString:[self accessibleSelectedText] forType:NSStringPboardType];731}732733if ([types containsObject:NSRTFDPboardType])734{735[pboard declareTypes:[NSArray arrayWithObject:NSRTFDPboardType] owner:nil];736return [pboard setData:[self accessibleSelectedTextAsRTFD] forType:NSRTFDPboardType];737}738739return NO;740}741742// write text back to Java from the service743- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard744{745if ([[pboard types] containsObject:NSStringPboardType])746{747NSString *text = [pboard stringForType:NSStringPboardType];748return [self replaceAccessibleTextSelection:text];749}750751if ([[pboard types] containsObject:NSRTFDPboardType])752{753NSData *rtfdData = [pboard dataForType:NSRTFDPboardType];754NSAttributedString *styledText = [[NSAttributedString alloc] initWithRTFD:rtfdData documentAttributes:NULL];755NSString *text = [styledText string];756[styledText release];757758return [self replaceAccessibleTextSelection:text];759}760761return NO;762}763764765-(void) setDragSource:(CDragSource *)source {766self._dragSource = source;767}768769770- (void) setDropTarget:(CDropTarget *)target {771self._dropTarget = target;772[ThreadUtilities performOnMainThread:@selector(controlModelControlValid) on:self._dropTarget withObject:nil waitUntilDone:YES];773}774775/******************************** BEGIN NSDraggingSource Interface ********************************/776777- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)flag778{779// If draggingSource is nil route the message to the superclass (if responding to the selector):780CDragSource *dragSource = self._dragSource;781NSDragOperation dragOp = NSDragOperationNone;782783if (dragSource != nil) {784dragOp = [dragSource draggingSourceOperationMaskForLocal:flag];785}786return dragOp;787}788789- (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination790{791// If draggingSource is nil route the message to the superclass (if responding to the selector):792CDragSource *dragSource = self._dragSource;793NSArray* array = nil;794795if (dragSource != nil) {796array = [dragSource namesOfPromisedFilesDroppedAtDestination:dropDestination];797}798return array;799}800801- (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint802{803// If draggingSource is nil route the message to the superclass (if responding to the selector):804CDragSource *dragSource = self._dragSource;805806if (dragSource != nil) {807[dragSource draggedImage:image beganAt:screenPoint];808}809}810811- (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint operation:(NSDragOperation)operation812{813// If draggingSource is nil route the message to the superclass (if responding to the selector):814CDragSource *dragSource = self._dragSource;815816if (dragSource != nil) {817[dragSource draggedImage:image endedAt:screenPoint operation:operation];818}819}820821- (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint822{823// If draggingSource is nil route the message to the superclass (if responding to the selector):824CDragSource *dragSource = self._dragSource;825826if (dragSource != nil) {827[dragSource draggedImage:image movedTo:screenPoint];828}829}830831- (BOOL)ignoreModifierKeysWhileDragging832{833// If draggingSource is nil route the message to the superclass (if responding to the selector):834CDragSource *dragSource = self._dragSource;835BOOL result = FALSE;836837if (dragSource != nil) {838result = [dragSource ignoreModifierKeysWhileDragging];839}840return result;841}842843/******************************** END NSDraggingSource Interface ********************************/844845/******************************** BEGIN NSDraggingDestination Interface ********************************/846847- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender848{849// If draggingDestination is nil route the message to the superclass:850CDropTarget *dropTarget = self._dropTarget;851NSDragOperation dragOp = NSDragOperationNone;852853if (dropTarget != nil) {854dragOp = [dropTarget draggingEntered:sender];855}856return dragOp;857}858859- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender860{861// If draggingDestination is nil route the message to the superclass:862CDropTarget *dropTarget = self._dropTarget;863NSDragOperation dragOp = NSDragOperationNone;864865if (dropTarget != nil) {866dragOp = [dropTarget draggingUpdated:sender];867}868return dragOp;869}870871- (void)draggingExited:(id <NSDraggingInfo>)sender872{873// If draggingDestination is nil route the message to the superclass:874CDropTarget *dropTarget = self._dropTarget;875876if (dropTarget != nil) {877[dropTarget draggingExited:sender];878}879}880881- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender882{883// If draggingDestination is nil route the message to the superclass:884CDropTarget *dropTarget = self._dropTarget;885BOOL result = FALSE;886887if (dropTarget != nil) {888result = [dropTarget prepareForDragOperation:sender];889}890return result;891}892893- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender894{895// If draggingDestination is nil route the message to the superclass:896CDropTarget *dropTarget = self._dropTarget;897BOOL result = FALSE;898899if (dropTarget != nil) {900result = [dropTarget performDragOperation:sender];901}902return result;903}904905- (void)concludeDragOperation:(id <NSDraggingInfo>)sender906{907// If draggingDestination is nil route the message to the superclass:908CDropTarget *dropTarget = self._dropTarget;909910if (dropTarget != nil) {911[dropTarget concludeDragOperation:sender];912}913}914915- (void)draggingEnded:(id <NSDraggingInfo>)sender916{917// If draggingDestination is nil route the message to the superclass:918CDropTarget *dropTarget = self._dropTarget;919920if (dropTarget != nil) {921[dropTarget draggingEnded:sender];922}923}924925/******************************** END NSDraggingDestination Interface ********************************/926927/******************************** BEGIN NSTextInputClient Protocol ********************************/928929930static jclass jc_CInputMethod = NULL;931932#define GET_CIM_CLASS() \933GET_CLASS(jc_CInputMethod, "sun/lwawt/macosx/CInputMethod");934935#define GET_CIM_CLASS_RETURN(ret) \936GET_CLASS_RETURN(jc_CInputMethod, "sun/lwawt/macosx/CInputMethod", ret);937938- (void) insertText:(id)aString replacementRange:(NSRange)replacementRange939{940#ifdef IM_DEBUG941fprintf(stderr, "AWTView InputMethod Selector Called : [insertText]: %s\n", [aString UTF8String]);942#endif // IM_DEBUG943944if (fInputMethodLOCKABLE == NULL) {945return;946}947948// Insert happens at the end of PAH949fInPressAndHold = NO;950951// insertText gets called when the user commits text generated from an input method. It also gets952// called during ordinary input as well. We only need to send an input method event when we have marked953// text, or 'text in progress'. We also need to send the event if we get an insert text out of the blue!954// (i.e., when the user uses the Character palette or Inkwell), or when the string to insert is a complex955// Unicode value.956957NSMutableString * useString = [self parseString:aString];958NSUInteger utf16Length = [useString lengthOfBytesUsingEncoding:NSUTF16StringEncoding];959NSUInteger utf8Length = [useString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];960BOOL aStringIsComplex = NO;961962unichar codePoint = [useString characterAtIndex:0];963964#ifdef IM_DEBUG965NSLog(@"insertText kbdlayout %@ ",(NSString *)kbdLayout);966#endif // IM_DEBUG967968if ((utf16Length > 2) ||969((utf8Length > 1) && [self isCodePointInUnicodeBlockNeedingIMEvent:codePoint]) ||970((codePoint == 0x5c) && ([(NSString *)kbdLayout containsString:@"Kotoeri"]))) {971aStringIsComplex = YES;972}973974if ([self hasMarkedText] || !fProcessingKeystroke || aStringIsComplex) {975JNIEnv *env = [ThreadUtilities getJNIEnv];976977GET_CIM_CLASS();978DECLARE_METHOD(jm_selectPreviousGlyph, jc_CInputMethod, "selectPreviousGlyph", "()V");979// We need to select the previous glyph so that it is overwritten.980if (fPAHNeedsToSelect) {981(*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_selectPreviousGlyph);982CHECK_EXCEPTION();983fPAHNeedsToSelect = NO;984}985986DECLARE_METHOD(jm_insertText, jc_CInputMethod, "insertText", "(Ljava/lang/String;)V");987jstring insertedText = NSStringToJavaString(env, useString);988(*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_insertText, insertedText);989CHECK_EXCEPTION();990(*env)->DeleteLocalRef(env, insertedText);991992// The input method event will create psuedo-key events for each character in the committed string.993// We also don't want to send the character that triggered the insertText, usually a return. [3337563]994fKeyEventsNeeded = NO;995}996else {997// Need to set back the fKeyEventsNeeded flag so that the string following the998// marked text is not ignored by keyDown999if ([useString length] > 0) {1000fKeyEventsNeeded = YES;1001}1002}1003fPAHNeedsToSelect = NO;10041005// Abandon input to reset IM and unblock input after entering accented1006// symbols10071008[self abandonInput];1009}10101011+ (void)keyboardInputSourceChanged:(NSNotification *)notification1012{1013#ifdef IM_DEBUG1014NSLog(@"keyboardInputSourceChangeNotification received");1015#endif1016NSTextInputContext *curContxt = [NSTextInputContext currentInputContext];1017kbdLayout = curContxt.selectedKeyboardInputSource;1018}10191020- (void) doCommandBySelector:(SEL)aSelector1021{1022#ifdef IM_DEBUG1023fprintf(stderr, "AWTView InputMethod Selector Called : [doCommandBySelector]\n");1024NSLog(@"%@", NSStringFromSelector(aSelector));1025#endif // IM_DEBUG1026if (@selector(insertNewline:) == aSelector || @selector(insertTab:) == aSelector || @selector(deleteBackward:) == aSelector)1027{1028fKeyEventsNeeded = YES;1029}1030}10311032// setMarkedText: cannot take a nil first argument. aString can be NSString or NSAttributedString1033- (void) setMarkedText:(id)aString selectedRange:(NSRange)selectionRange replacementRange:(NSRange)replacementRange1034{1035if (!fInputMethodLOCKABLE)1036return;10371038BOOL isAttributedString = [aString isKindOfClass:[NSAttributedString class]];1039NSAttributedString *attrString = (isAttributedString ? (NSAttributedString *)aString : nil);1040NSString *incomingString = (isAttributedString ? [aString string] : aString);1041#ifdef IM_DEBUG1042fprintf(stderr, "AWTView InputMethod Selector Called : [setMarkedText] \"%s\", loc=%lu, length=%lu\n", [incomingString UTF8String], (unsigned long)selectionRange.location, (unsigned long)selectionRange.length);1043#endif // IM_DEBUG1044JNIEnv *env = [ThreadUtilities getJNIEnv];1045GET_CIM_CLASS();1046DECLARE_METHOD(jm_startIMUpdate, jc_CInputMethod, "startIMUpdate", "(Ljava/lang/String;)V");1047DECLARE_METHOD(jm_addAttribute, jc_CInputMethod, "addAttribute", "(ZZII)V");1048DECLARE_METHOD(jm_dispatchText, jc_CInputMethod, "dispatchText", "(IIZ)V");10491050// NSInputContext already did the analysis of the TSM event and created attributes indicating1051// the underlining and color that should be done to the string. We need to look at the underline1052// style and color to determine what kind of Java hilighting needs to be done.1053jstring inProcessText = NSStringToJavaString(env, incomingString);1054(*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_startIMUpdate, inProcessText);1055CHECK_EXCEPTION();1056(*env)->DeleteLocalRef(env, inProcessText);10571058if (isAttributedString) {1059NSUInteger length;1060NSRange effectiveRange;1061NSDictionary *attributes;1062length = [attrString length];1063effectiveRange = NSMakeRange(0, 0);1064while (NSMaxRange(effectiveRange) < length) {1065attributes = [attrString attributesAtIndex:NSMaxRange(effectiveRange)1066effectiveRange:&effectiveRange];1067if (attributes) {1068BOOL isThickUnderline, isGray;1069NSNumber *underlineSizeObj =1070(NSNumber *)[attributes objectForKey:NSUnderlineStyleAttributeName];1071NSInteger underlineSize = [underlineSizeObj integerValue];1072isThickUnderline = (underlineSize > 1);10731074NSColor *underlineColorObj =1075(NSColor *)[attributes objectForKey:NSUnderlineColorAttributeName];1076isGray = !([underlineColorObj isEqual:[NSColor blackColor]]);10771078(*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_addAttribute, isThickUnderline,1079isGray, effectiveRange.location, effectiveRange.length);1080CHECK_EXCEPTION();1081}1082}1083}10841085DECLARE_METHOD(jm_selectPreviousGlyph, jc_CInputMethod, "selectPreviousGlyph", "()V");1086// We need to select the previous glyph so that it is overwritten.1087if (fPAHNeedsToSelect) {1088(*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_selectPreviousGlyph);1089CHECK_EXCEPTION();1090fPAHNeedsToSelect = NO;1091}10921093(*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_dispatchText,1094selectionRange.location, selectionRange.length, JNI_FALSE);1095CHECK_EXCEPTION();1096// If the marked text is being cleared (zero-length string) don't handle the key event.1097if ([incomingString length] == 0) {1098fKeyEventsNeeded = NO;1099}1100}11011102- (void) unmarkText1103{1104#ifdef IM_DEBUG1105fprintf(stderr, "AWTView InputMethod Selector Called : [unmarkText]\n");1106#endif // IM_DEBUG11071108if (!fInputMethodLOCKABLE) {1109return;1110}11111112// unmarkText cancels any input in progress and commits it to the text field.1113JNIEnv *env = [ThreadUtilities getJNIEnv];1114GET_CIM_CLASS();1115DECLARE_METHOD(jm_unmarkText, jc_CInputMethod, "unmarkText", "()V");1116(*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_unmarkText);1117CHECK_EXCEPTION();1118}11191120- (BOOL) hasMarkedText1121{1122#ifdef IM_DEBUG1123fprintf(stderr, "AWTView InputMethod Selector Called : [hasMarkedText]\n");1124#endif // IM_DEBUG11251126if (!fInputMethodLOCKABLE) {1127return NO;1128}11291130JNIEnv *env = [ThreadUtilities getJNIEnv];1131GET_CIM_CLASS_RETURN(NO);1132DECLARE_FIELD_RETURN(jf_fCurrentText, jc_CInputMethod, "fCurrentText", "Ljava/text/AttributedString;", NO);1133DECLARE_FIELD_RETURN(jf_fCurrentTextLength, jc_CInputMethod, "fCurrentTextLength", "I", NO);1134jobject currentText = (*env)->GetObjectField(env, fInputMethodLOCKABLE, jf_fCurrentText);1135CHECK_EXCEPTION();11361137jint currentTextLength = (*env)->GetIntField(env, fInputMethodLOCKABLE, jf_fCurrentTextLength);1138CHECK_EXCEPTION();11391140BOOL hasMarkedText = (currentText != NULL && currentTextLength > 0);11411142if (currentText != NULL) {1143(*env)->DeleteLocalRef(env, currentText);1144}11451146return hasMarkedText;1147}11481149- (NSInteger) conversationIdentifier1150{1151#ifdef IM_DEBUG1152fprintf(stderr, "AWTView InputMethod Selector Called : [conversationIdentifier]\n");1153#endif // IM_DEBUG11541155return (NSInteger) self;1156}11571158/* Returns attributed string at the range. This allows input mangers to1159query any range in backing-store (Andy's request)1160*/1161- (NSAttributedString *) attributedSubstringForProposedRange:(NSRange)theRange actualRange:(NSRangePointer)actualRange1162{1163#ifdef IM_DEBUG1164fprintf(stderr, "AWTView InputMethod Selector Called : [attributedSubstringFromRange] location=%lu, length=%lu\n", (unsigned long)theRange.location, (unsigned long)theRange.length);1165#endif // IM_DEBUG1166if (!fInputMethodLOCKABLE) {1167return nil;1168}11691170JNIEnv *env = [ThreadUtilities getJNIEnv];1171GET_CIM_CLASS_RETURN(nil);1172DECLARE_METHOD_RETURN(jm_substringFromRange, jc_CInputMethod, "attributedSubstringFromRange", "(II)Ljava/lang/String;", nil);1173jobject theString = (*env)->CallObjectMethod(env, fInputMethodLOCKABLE, jm_substringFromRange, theRange.location, theRange.length);1174CHECK_EXCEPTION_NULL_RETURN(theString, nil);11751176id result = [[[NSAttributedString alloc] initWithString:JavaStringToNSString(env, theString)] autorelease];1177#ifdef IM_DEBUG1178NSLog(@"attributedSubstringFromRange returning \"%@\"", result);1179#endif // IM_DEBUG11801181(*env)->DeleteLocalRef(env, theString);1182return result;1183}11841185/* This method returns the range for marked region. If hasMarkedText == false,1186it'll return NSNotFound location & 0 length range.1187*/1188- (NSRange) markedRange1189{11901191#ifdef IM_DEBUG1192fprintf(stderr, "AWTView InputMethod Selector Called : [markedRange]\n");1193#endif // IM_DEBUG11941195if (!fInputMethodLOCKABLE) {1196return NSMakeRange(NSNotFound, 0);1197}11981199JNIEnv *env = [ThreadUtilities getJNIEnv];1200jarray array;1201jboolean isCopy;1202jint *_array;1203NSRange range = NSMakeRange(NSNotFound, 0);1204GET_CIM_CLASS_RETURN(range);1205DECLARE_METHOD_RETURN(jm_markedRange, jc_CInputMethod, "markedRange", "()[I", range);12061207array = (*env)->CallObjectMethod(env, fInputMethodLOCKABLE, jm_markedRange);1208CHECK_EXCEPTION();12091210if (array) {1211_array = (*env)->GetIntArrayElements(env, array, &isCopy);1212if (_array != NULL) {1213range.location = _array[0];1214range.length = _array[1];1215#ifdef IM_DEBUG1216fprintf(stderr, "markedRange returning (%lu, %lu)\n",1217(unsigned long)range.location, (unsigned long)range.length);1218#endif // IM_DEBUG1219(*env)->ReleaseIntArrayElements(env, array, _array, 0);1220}1221(*env)->DeleteLocalRef(env, array);1222}12231224return range;1225}12261227/* This method returns the range for selected region. Just like markedRange method,1228its location field contains char index from the text beginning.1229*/1230- (NSRange) selectedRange1231{1232if (!fInputMethodLOCKABLE) {1233return NSMakeRange(NSNotFound, 0);1234}12351236JNIEnv *env = [ThreadUtilities getJNIEnv];1237jarray array;1238jboolean isCopy;1239jint *_array;1240NSRange range = NSMakeRange(NSNotFound, 0);1241GET_CIM_CLASS_RETURN(range);1242DECLARE_METHOD_RETURN(jm_selectedRange, jc_CInputMethod, "selectedRange", "()[I", range);12431244#ifdef IM_DEBUG1245fprintf(stderr, "AWTView InputMethod Selector Called : [selectedRange]\n");1246#endif // IM_DEBUG12471248array = (*env)->CallObjectMethod(env, fInputMethodLOCKABLE, jm_selectedRange);1249CHECK_EXCEPTION();1250if (array) {1251_array = (*env)->GetIntArrayElements(env, array, &isCopy);1252if (_array != NULL) {1253range.location = _array[0];1254range.length = _array[1];1255(*env)->ReleaseIntArrayElements(env, array, _array, 0);1256}1257(*env)->DeleteLocalRef(env, array);1258}12591260return range;1261}12621263/* This method returns the first frame of rects for theRange in screen coordindate system.1264*/1265- (NSRect) firstRectForCharacterRange:(NSRange)theRange actualRange:(NSRangePointer)actualRange1266{1267if (!fInputMethodLOCKABLE) {1268return NSZeroRect;1269}12701271JNIEnv *env = [ThreadUtilities getJNIEnv];1272GET_CIM_CLASS_RETURN(NSZeroRect);1273DECLARE_METHOD_RETURN(jm_firstRectForCharacterRange, jc_CInputMethod,1274"firstRectForCharacterRange", "(I)[I", NSZeroRect);1275jarray array;1276jboolean isCopy;1277jint *_array;1278NSRect rect;12791280#ifdef IM_DEBUG1281fprintf(stderr,1282"AWTView InputMethod Selector Called : [firstRectForCharacterRange:] location=%lu, length=%lu\n",1283(unsigned long)theRange.location, (unsigned long)theRange.length);1284#endif // IM_DEBUG12851286array = (*env)->CallObjectMethod(env, fInputMethodLOCKABLE, jm_firstRectForCharacterRange,1287theRange.location);1288CHECK_EXCEPTION();12891290_array = (*env)->GetIntArrayElements(env, array, &isCopy);1291if (_array) {1292rect = ConvertNSScreenRect(env, NSMakeRect(_array[0], _array[1], _array[2], _array[3]));1293(*env)->ReleaseIntArrayElements(env, array, _array, 0);1294} else {1295rect = NSZeroRect;1296}1297(*env)->DeleteLocalRef(env, array);12981299#ifdef IM_DEBUG1300fprintf(stderr,1301"firstRectForCharacterRange returning x=%f, y=%f, width=%f, height=%f\n",1302rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);1303#endif // IM_DEBUG1304return rect;1305}13061307/* This method returns the index for character that is nearest to thePoint. thPoint is in1308screen coordinate system.1309*/1310- (NSUInteger)characterIndexForPoint:(NSPoint)thePoint1311{1312if (!fInputMethodLOCKABLE) {1313return NSNotFound;1314}13151316JNIEnv *env = [ThreadUtilities getJNIEnv];1317GET_CIM_CLASS_RETURN(NSNotFound);1318DECLARE_METHOD_RETURN(jm_characterIndexForPoint, jc_CInputMethod,1319"characterIndexForPoint", "(II)I", NSNotFound);13201321NSPoint flippedLocation = ConvertNSScreenPoint(env, thePoint);13221323#ifdef IM_DEBUG1324fprintf(stderr, "AWTView InputMethod Selector Called : [characterIndexForPoint:(NSPoint)thePoint] x=%f, y=%f\n", flippedLocation.x, flippedLocation.y);1325#endif // IM_DEBUG13261327jint index = (*env)->CallIntMethod(env, fInputMethodLOCKABLE, jm_characterIndexForPoint,1328(jint)flippedLocation.x, (jint)flippedLocation.y);1329CHECK_EXCEPTION();13301331#ifdef IM_DEBUG1332fprintf(stderr, "characterIndexForPoint returning %d\n", index);1333#endif // IM_DEBUG13341335if (index == -1) {1336return NSNotFound;1337} else {1338return (NSUInteger)index;1339}1340}13411342- (NSArray*) validAttributesForMarkedText1343{1344#ifdef IM_DEBUG1345fprintf(stderr, "AWTView InputMethod Selector Called : [validAttributesForMarkedText]\n");1346#endif // IM_DEBUG13471348return [NSArray array];1349}13501351- (void)setInputMethod:(jobject)inputMethod1352{1353#ifdef IM_DEBUG1354fprintf(stderr, "AWTView InputMethod Selector Called : [setInputMethod]\n");1355#endif // IM_DEBUG13561357JNIEnv *env = [ThreadUtilities getJNIEnv];13581359// Get rid of the old one1360if (fInputMethodLOCKABLE) {1361(*env)->DeleteGlobalRef(env, fInputMethodLOCKABLE);1362}13631364fInputMethodLOCKABLE = inputMethod; // input method arg must be a GlobalRef13651366NSTextInputContext *curContxt = [NSTextInputContext currentInputContext];1367kbdLayout = curContxt.selectedKeyboardInputSource;1368[[NSNotificationCenter defaultCenter] addObserver:[AWTView class]1369selector:@selector(keyboardInputSourceChanged:)1370name:NSTextInputContextKeyboardSelectionDidChangeNotification1371object:nil];1372}13731374- (void)abandonInput1375{1376#ifdef IM_DEBUG1377fprintf(stderr, "AWTView InputMethod Selector Called : [abandonInput]\n");1378#endif // IM_DEBUG13791380[ThreadUtilities performOnMainThread:@selector(markedTextAbandoned:) on:[NSInputManager currentInputManager] withObject:self waitUntilDone:YES];1381[self unmarkText];1382}13831384/******************************** END NSTextInputClient Protocol ********************************/13851386138713881389@end // AWTView13901391/*1392* Class: sun_lwawt_macosx_CPlatformView1393* Method: nativeCreateView1394* Signature: (IIII)J1395*/1396JNIEXPORT jlong JNICALL1397Java_sun_lwawt_macosx_CPlatformView_nativeCreateView1398(JNIEnv *env, jobject obj, jint originX, jint originY, jint width, jint height, jlong windowLayerPtr)1399{1400__block AWTView *newView = nil;14011402JNI_COCOA_ENTER(env);14031404NSRect rect = NSMakeRect(originX, originY, width, height);1405jobject cPlatformView = (*env)->NewWeakGlobalRef(env, obj);1406CHECK_EXCEPTION();14071408[ThreadUtilities performOnMainThreadWaiting:YES block:^(){14091410CALayer *windowLayer = jlong_to_ptr(windowLayerPtr);1411newView = [[AWTView alloc] initWithRect:rect1412platformView:cPlatformView1413windowLayer:windowLayer];1414}];14151416JNI_COCOA_EXIT(env);14171418return ptr_to_jlong(newView);1419}14201421/*1422* Class: sun_lwawt_macosx_CPlatformView1423* Method: nativeSetAutoResizable1424* Signature: (JZ)V;1425*/14261427JNIEXPORT void JNICALL1428Java_sun_lwawt_macosx_CPlatformView_nativeSetAutoResizable1429(JNIEnv *env, jclass cls, jlong viewPtr, jboolean toResize)1430{1431JNI_COCOA_ENTER(env);14321433NSView *view = (NSView *)jlong_to_ptr(viewPtr);14341435[ThreadUtilities performOnMainThreadWaiting:NO block:^(){14361437if (toResize) {1438[view setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];1439} else {1440[view setAutoresizingMask: NSViewMinYMargin | NSViewMaxXMargin];1441}14421443if ([view superview] != nil) {1444[[view superview] setAutoresizesSubviews:(BOOL)toResize];1445}14461447}];1448JNI_COCOA_EXIT(env);1449}14501451/*1452* Class: sun_lwawt_macosx_CPlatformView1453* Method: nativeGetNSViewDisplayID1454* Signature: (J)I;1455*/14561457JNIEXPORT jint JNICALL1458Java_sun_lwawt_macosx_CPlatformView_nativeGetNSViewDisplayID1459(JNIEnv *env, jclass cls, jlong viewPtr)1460{1461__block jint ret; //CGDirectDisplayID14621463JNI_COCOA_ENTER(env);14641465NSView *view = (NSView *)jlong_to_ptr(viewPtr);1466[ThreadUtilities performOnMainThreadWaiting:YES block:^(){1467NSWindow *window = [view window];1468ret = (jint)[[AWTWindow getNSWindowDisplayID_AppKitThread: window] intValue];1469}];14701471JNI_COCOA_EXIT(env);14721473return ret;1474}14751476/*1477* Class: sun_lwawt_macosx_CPlatformView1478* Method: nativeGetLocationOnScreen1479* Signature: (J)Ljava/awt/Rectangle;1480*/14811482JNIEXPORT jobject JNICALL1483Java_sun_lwawt_macosx_CPlatformView_nativeGetLocationOnScreen1484(JNIEnv *env, jclass cls, jlong viewPtr)1485{1486jobject jRect = NULL;14871488JNI_COCOA_ENTER(env);14891490__block NSRect rect = NSZeroRect;14911492NSView *view = (NSView *)jlong_to_ptr(viewPtr);1493[ThreadUtilities performOnMainThreadWaiting:YES block:^(){14941495NSRect viewBounds = [view bounds];1496NSRect frameInWindow = [view convertRect:viewBounds toView:nil];1497rect = [[view window] convertRectToScreen:frameInWindow];1498//Convert coordinates to top-left corner origin1499rect = ConvertNSScreenRect(NULL, rect);15001501}];1502jRect = NSToJavaRect(env, rect);15031504JNI_COCOA_EXIT(env);15051506return jRect;1507}15081509/*1510* Class: sun_lwawt_macosx_CPlatformView1511* Method: nativeIsViewUnderMouse1512* Signature: (J)Z;1513*/15141515JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_CPlatformView_nativeIsViewUnderMouse1516(JNIEnv *env, jclass clazz, jlong viewPtr)1517{1518__block jboolean underMouse = JNI_FALSE;15191520JNI_COCOA_ENTER(env);15211522NSView *nsView = OBJC(viewPtr);1523[ThreadUtilities performOnMainThreadWaiting:YES block:^(){1524NSPoint ptWindowCoords = [[nsView window] mouseLocationOutsideOfEventStream];1525NSPoint ptViewCoords = [nsView convertPoint:ptWindowCoords fromView:nil];1526underMouse = [nsView hitTest:ptViewCoords] != nil;1527}];15281529JNI_COCOA_EXIT(env);15301531return underMouse;1532}153315341535