1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import "CGLGraphicsConfig.h"
  27 
  28 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  29 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
  30 
  31 #import "ThreadUtilities.h"
  32 #import "AWTView.h"
  33 #import "AWTEvent.h"
  34 #import "AWTWindow.h"
  35 #import "LWCToolkit.h"
  36 #import "JavaComponentAccessibility.h"
  37 #import "JavaTextAccessibility.h"
  38 #import "GeomUtilities.h"
  39 #import "OSVersion.h"
  40 #import "CGLLayer.h"
  41 
  42 @interface AWTView()
  43 @property (retain) CDropTarget *_dropTarget;
  44 @property (retain) CDragSource *_dragSource;
  45 @end
  46 
  47 // Uncomment this line to see fprintfs of each InputMethod API being called on this View
  48 //#define IM_DEBUG TRUE
  49 //#define EXTRA_DEBUG
  50 
  51 
  52 static BOOL shouldUsePressAndHold() {
  53     static int shouldUsePressAndHold = -1;
  54     if (shouldUsePressAndHold != -1) return shouldUsePressAndHold;
  55     shouldUsePressAndHold = !isSnowLeopardOrLower();
  56     return shouldUsePressAndHold;
  57 }
  58 
  59 @implementation AWTView
  60 
  61 @synthesize _dropTarget;
  62 @synthesize _dragSource;
  63 @synthesize cglLayer;
  64 @synthesize mouseIsOver;
  65 
  66 // Note: Must be called on main (AppKit) thread only
  67 - (id) initWithRect: (NSRect) rect
  68        platformView: (jobject) cPlatformView
  69        windowLayer: (CALayer*) windowLayer
  70 {
  71 AWT_ASSERT_APPKIT_THREAD;
  72     // Initialize ourselves
  73     self = [super initWithFrame: rect];
  74     if (self == nil) return self;
  75 
  76     m_cPlatformView = cPlatformView;
  77     fInputMethodLOCKABLE = NULL;
  78     fKeyEventsNeeded = NO;
  79     fProcessingKeystroke = NO;
  80 
  81     fEnablePressAndHold = shouldUsePressAndHold();
  82     fInPressAndHold = NO;
  83     fPAHNeedsToSelect = NO;
  84     
  85     mouseIsOver = NO;
  86 
  87     if (windowLayer != nil) {
  88         self.cglLayer = windowLayer;
  89         [self setWantsLayer: YES];
  90         [self.layer addSublayer: (CALayer *)cglLayer];
  91         [self setLayerContentsRedrawPolicy: NSViewLayerContentsRedrawDuringViewResize];
  92         [self setLayerContentsPlacement: NSViewLayerContentsPlacementTopLeft];
  93         [self setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
  94 
  95 #ifdef REMOTELAYER
  96         CGLLayer *parentLayer = (CGLLayer*)self.cglLayer;
  97         parentLayer.parentLayer = NULL;
  98         parentLayer.remoteLayer = NULL;
  99         if (JRSRemotePort != 0 && remoteSocketFD > 0) {
 100             CGLLayer *remoteLayer = [[CGLLayer alloc] initWithJavaLayer: parentLayer.javaLayer];
 101             remoteLayer.target = GL_TEXTURE_2D;
 102             NSLog(@"Creating Parent=%p, Remote=%p", parentLayer, remoteLayer);
 103             parentLayer.remoteLayer = remoteLayer;
 104             remoteLayer.parentLayer = parentLayer;
 105             remoteLayer.remoteLayer = NULL;
 106             remoteLayer.jrsRemoteLayer = [remoteLayer createRemoteLayerBoundTo:JRSRemotePort];
 107             CFRetain(remoteLayer);  // REMIND
 108             remoteLayer.frame = CGRectMake(0, 0, 720, 500); // REMIND
 109             CFRetain(remoteLayer.jrsRemoteLayer); // REMIND
 110             int layerID = [remoteLayer.jrsRemoteLayer layerID];
 111             NSLog(@"layer id to send = %d", layerID);
 112             sendLayerID(layerID);
 113         }
 114 #endif /* REMOTELAYER */
 115     }
 116 
 117     return self;
 118 }
 119 
 120 - (void) dealloc {
 121 AWT_ASSERT_APPKIT_THREAD;
 122 
 123     self.cglLayer = nil;
 124 
 125     JNIEnv *env = [ThreadUtilities getJNIEnv];
 126     (*env)->DeleteGlobalRef(env, m_cPlatformView);
 127     m_cPlatformView = NULL;
 128 
 129     if (fInputMethodLOCKABLE != NULL)
 130     {
 131         JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 132 
 133         JNFDeleteGlobalRef(env, fInputMethodLOCKABLE);
 134         fInputMethodLOCKABLE = NULL;
 135     }
 136 
 137 
 138     [super dealloc];
 139 }
 140 
 141 - (void) viewDidMoveToWindow {
 142 AWT_ASSERT_APPKIT_THREAD;
 143 
 144     [AWTToolkit eventCountPlusPlus];
 145 
 146     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^() {
 147         [[self window] makeFirstResponder: self];
 148     }];
 149     if ([self window] != NULL) {
 150         [self resetTrackingRect];
 151     }
 152 }
 153 
 154 - (BOOL) acceptsFirstMouse: (NSEvent *)event {
 155     return YES;
 156 }
 157 
 158 - (BOOL) acceptsFirstResponder {
 159     return YES;
 160 }
 161 
 162 - (BOOL) becomeFirstResponder {
 163     return YES;
 164 }
 165 
 166 - (BOOL) preservesContentDuringLiveResize {
 167     return YES;
 168 }
 169 
 170 /*
 171  * Automatically triggered functions.
 172  */
 173 
 174 /*
 175  * MouseEvents support
 176  */
 177 
 178 - (void) mouseDown: (NSEvent *)event {
 179     NSInputManager *inputManager = [NSInputManager currentInputManager];
 180     if ([inputManager wantsToHandleMouseEvents]) {
 181 #if IM_DEBUG
 182         NSLog(@"-> IM wants to handle event");
 183 #endif
 184         if (![inputManager handleMouseEvent:event]) {
 185             [self deliverJavaMouseEvent: event];
 186         } else {
 187 #if IM_DEBUG
 188             NSLog(@"-> Event was handled.");
 189 #endif
 190         }
 191     } else {
 192         NSLog(@"-> IM does not want to handle event");
 193         [self deliverJavaMouseEvent: event];
 194     }
 195 }
 196 
 197 - (void) mouseUp: (NSEvent *)event {
 198     [self deliverJavaMouseEvent: event];
 199 }
 200 
 201 - (void) rightMouseDown: (NSEvent *)event {
 202     [self deliverJavaMouseEvent: event];
 203 }
 204 
 205 - (void) rightMouseUp: (NSEvent *)event {
 206     [self deliverJavaMouseEvent: event];
 207 }
 208 
 209 - (void) otherMouseDown: (NSEvent *)event {
 210     [self deliverJavaMouseEvent: event];
 211 }
 212 
 213 - (void) otherMouseUp: (NSEvent *)event {
 214     [self deliverJavaMouseEvent: event];
 215 }
 216 
 217 - (void) mouseMoved: (NSEvent *)event {
 218     // TODO: better way to redirect move events to the "under" view
 219 
 220     NSPoint eventLocation = [event locationInWindow];
 221     NSPoint localPoint = [self convertPoint: eventLocation fromView: nil];
 222 
 223     if  ([self mouse: localPoint inRect: [self bounds]]) {
 224         [self deliverJavaMouseEvent: event];
 225     } else {
 226         [[self nextResponder] mouseDown:event];
 227     }
 228 }
 229 
 230 - (void) mouseDragged: (NSEvent *)event {
 231     [self deliverJavaMouseEvent: event];
 232 }
 233 
 234 - (void) rightMouseDragged: (NSEvent *)event {
 235     [self deliverJavaMouseEvent: event];
 236 }
 237 
 238 - (void) otherMouseDragged: (NSEvent *)event {
 239     [self deliverJavaMouseEvent: event];
 240 }
 241 
 242 - (void) mouseEntered: (NSEvent *)event {
 243     [[self window] setAcceptsMouseMovedEvents:YES];
 244     //[[self window] makeFirstResponder:self];
 245     [self deliverJavaMouseEvent: event];
 246 }
 247 
 248 - (void) mouseExited: (NSEvent *)event {
 249     [[self window] setAcceptsMouseMovedEvents:NO];
 250     [self deliverJavaMouseEvent: event];
 251     //Restore the cursor back.
 252     //[CCursorManager _setCursor: [NSCursor arrowCursor]];
 253 }
 254 
 255 - (void) scrollWheel: (NSEvent*) event {
 256     [self deliverJavaMouseEvent: event];
 257 }
 258 
 259 /*
 260  * KeyEvents support
 261  */
 262 
 263 - (void) keyDown: (NSEvent *)event {
 264 
 265     fProcessingKeystroke = YES;
 266     fKeyEventsNeeded = YES;
 267 
 268     // Allow TSM to look at the event and potentially send back NSTextInputClient messages.
 269     [self interpretKeyEvents:[NSArray arrayWithObject:event]];
 270 
 271     if (fEnablePressAndHold && [event willBeHandledByComplexInputMethod]) {
 272         fProcessingKeystroke = NO;
 273         if (!fInPressAndHold) {
 274             fInPressAndHold = YES;
 275             fPAHNeedsToSelect = YES;
 276         }
 277         return;
 278     }
 279 
 280     if (![self hasMarkedText] && fKeyEventsNeeded) {
 281         [self deliverJavaKeyEventHelper: event];
 282     }
 283 
 284     fProcessingKeystroke = NO;
 285 }
 286 
 287 - (void) keyUp: (NSEvent *)event {
 288     [self deliverJavaKeyEventHelper: event];
 289 }
 290 
 291 - (void) flagsChanged: (NSEvent *)event {
 292     [self deliverJavaKeyEventHelper: event];
 293 }
 294 
 295 - (BOOL) performKeyEquivalent: (NSEvent *) event {
 296     [self deliverJavaKeyEventHelper: event];
 297     return NO;
 298 }
 299 
 300 /**
 301  * Utility methods and accessors
 302  */
 303 
 304 -(void) deliverJavaMouseEvent: (NSEvent *) event {
 305     
 306     NSEventType type = [event type];    
 307     
 308     // check synthesized mouse entered/exited events
 309     if ((type == NSMouseEntered && mouseIsOver) || (type == NSMouseExited && !mouseIsOver)) {
 310         return;
 311     }else if ((type == NSMouseEntered && !mouseIsOver) || (type == NSMouseExited && mouseIsOver)) {
 312         mouseIsOver = !mouseIsOver;
 313     }
 314     
 315     [AWTToolkit eventCountPlusPlus];
 316 
 317     JNIEnv *env = [ThreadUtilities getJNIEnv];
 318 
 319     NSPoint eventLocation = [event locationInWindow];
 320     NSPoint localPoint = [self convertPoint: eventLocation fromView: nil];
 321     NSPoint absP = [NSEvent mouseLocation];
 322 
 323     // Convert global numbers between Cocoa's coordinate system and Java.
 324     // TODO: need consitent way for doing that both with global as well as with local coordinates.
 325     // The reason to do it here is one more native method for getting screen dimension otherwise.
 326 
 327     NSRect screenRect = [[NSScreen mainScreen] frame];
 328     absP.y = screenRect.size.height - absP.y;
 329     jint clickCount;
 330 
 331     if (type == NSMouseEntered ||
 332         type == NSMouseExited ||
 333         type == NSScrollWheel ||
 334         type == NSMouseMoved) {
 335         clickCount = 0;
 336     } else {
 337         clickCount = [event clickCount];
 338     }
 339 
 340     static JNF_CLASS_CACHE(jc_NSEvent, "sun/lwawt/macosx/event/NSEvent");
 341     static JNF_CTOR_CACHE(jctor_NSEvent, jc_NSEvent, "(IIIIIIIIDD)V");
 342     jobject jEvent = JNFNewObject(env, jctor_NSEvent,
 343                                   [event type],
 344                                   [event modifierFlags],
 345                                   clickCount,
 346                                   [event buttonNumber],
 347                                   (jint)localPoint.x, (jint)localPoint.y,
 348                                   (jint)absP.x, (jint)absP.y,
 349                                   [event deltaY],
 350                                   [event deltaX]);
 351     if (jEvent == nil) {
 352         // Unable to create event by some reason.
 353         return;
 354     }
 355 
 356     static JNF_CLASS_CACHE(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");
 357     static JNF_MEMBER_CACHE(jm_deliverMouseEvent, jc_PlatformView, "deliverMouseEvent", "(Lsun/lwawt/macosx/event/NSEvent;)V");
 358     JNFCallVoidMethod(env, m_cPlatformView, jm_deliverMouseEvent, jEvent);
 359 }
 360 
 361 
 362 - (void) clearTrackingRect {
 363     if (rolloverTrackingRectTag > 0) {
 364         [self removeTrackingRect:rolloverTrackingRectTag];
 365         rolloverTrackingRectTag = 0;
 366     }
 367 }
 368 
 369 - (void) resetTrackingRect {
 370     [self clearTrackingRect];
 371     rolloverTrackingRectTag = [self addTrackingRect:[self visibleRect]
 372                                               owner:self
 373                                            userData:NULL
 374                                        assumeInside:NO];
 375 }
 376 
 377 - (void)updateTrackingAreas {
 378     [super updateTrackingAreas];
 379     [self resetTrackingRect];
 380 }
 381 
 382 - (void) resetCursorRects {
 383     [super resetCursorRects];
 384     [self resetTrackingRect];
 385 }
 386 
 387 -(void) deliverJavaKeyEventHelper: (NSEvent *) event {
 388     [AWTToolkit eventCountPlusPlus];
 389     JNIEnv *env = [ThreadUtilities getJNIEnv];
 390 
 391     jstring characters = NULL;
 392     if ([event type] != NSFlagsChanged) {
 393         characters = JNFNSToJavaString(env, [event characters]);
 394     }
 395 
 396     static JNF_CLASS_CACHE(jc_NSEvent, "sun/lwawt/macosx/event/NSEvent");
 397     static JNF_CTOR_CACHE(jctor_NSEvent, jc_NSEvent, "(IISLjava/lang/String;)V");
 398     jobject jevent = JNFNewObject(env, jctor_NSEvent,
 399                                   [event type],
 400                                   [event modifierFlags],
 401                                   [event keyCode],
 402                                   characters);
 403 
 404     static JNF_CLASS_CACHE(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");
 405     static JNF_MEMBER_CACHE(jm_deliverKeyEvent, jc_PlatformView,
 406                             "deliverKeyEvent", "(Lsun/lwawt/macosx/event/NSEvent;)V");
 407     JNFCallVoidMethod(env, m_cPlatformView, jm_deliverKeyEvent, jevent);
 408 
 409     if (characters != NULL) {
 410         (*env)->DeleteLocalRef(env, characters);
 411     }
 412 }
 413 
 414 - (void) drawRect:(NSRect)dirtyRect {
 415 AWT_ASSERT_APPKIT_THREAD;
 416 
 417     [super drawRect:dirtyRect];
 418     JNIEnv *env = [ThreadUtilities getJNIEnv];
 419     if (env != NULL) {
 420 /*
 421         if ([self inLiveResize]) {
 422         NSRect rs[4];
 423         NSInteger count;
 424         [self getRectsExposedDuringLiveResize:rs count:&count];
 425         for (int i = 0; i < count; i++) {
 426             JNU_CallMethodByName(env, NULL, [m_awtWindow cPlatformView],
 427                  "deliverWindowDidExposeEvent", "(FFFF)V",
 428                  (jfloat)rs[i].origin.x, (jfloat)rs[i].origin.y,
 429                  (jfloat)rs[i].size.width, (jfloat)rs[i].size.height);
 430         if ((*env)->ExceptionOccurred(env)) {
 431             (*env)->ExceptionDescribe(env);
 432             (*env)->ExceptionClear(env);
 433         }
 434         }
 435         } else {
 436 */
 437         static JNF_CLASS_CACHE(jc_CPlatformView, "sun/lwawt/macosx/CPlatformView");
 438         static JNF_MEMBER_CACHE(jm_deliverWindowDidExposeEvent, jc_CPlatformView, "deliverWindowDidExposeEvent", "()V");
 439         JNFCallVoidMethod(env, m_cPlatformView, jm_deliverWindowDidExposeEvent);
 440 /*
 441         }
 442 */
 443     }
 444 }
 445 
 446 // NSAccessibility support
 447 - (jobject)awtComponent:(JNIEnv*)env
 448 {
 449     static JNF_CLASS_CACHE(jc_CPlatformView, "sun/lwawt/macosx/CPlatformView");
 450     static JNF_MEMBER_CACHE(jf_Peer, jc_CPlatformView, "peer", "Lsun/lwawt/LWWindowPeer;");
 451     if ((env == NULL) || (m_cPlatformView == NULL)) {
 452         NSLog(@"Apple AWT : Error AWTView:awtComponent given bad parameters.");
 453         if (env != NULL)
 454         {
 455             JNFDumpJavaStack(env);
 456         }
 457         return NULL;
 458     }
 459     jobject peer = JNFGetObjectField(env, m_cPlatformView, jf_Peer);
 460     static JNF_CLASS_CACHE(jc_LWWindowPeer, "sun/lwawt/LWWindowPeer");
 461     static JNF_MEMBER_CACHE(jf_Target, jc_LWWindowPeer, "target", "Ljava/awt/Component;");
 462     if (peer == NULL) {
 463         NSLog(@"Apple AWT : Error AWTView:awtComponent got null peer from CPlatformView");
 464         JNFDumpJavaStack(env);
 465         return NULL;
 466     }
 467     return JNFGetObjectField(env, peer, jf_Target);
 468 }
 469 
 470 - (id)getAxData:(JNIEnv*)env
 471 {
 472     return [[[JavaComponentAccessibility alloc] initWithParent:self withEnv:env withAccessible:[self awtComponent:env] withIndex:-1 withView:self withJavaRole:nil] autorelease];
 473 }
 474 
 475 - (NSArray *)accessibilityAttributeNames
 476 {
 477     return [[super accessibilityAttributeNames] arrayByAddingObject:NSAccessibilityChildrenAttribute];
 478 }
 479 
 480 // NSAccessibility messages
 481 // attribute methods
 482 - (id)accessibilityAttributeValue:(NSString *)attribute
 483 {
 484     AWT_ASSERT_APPKIT_THREAD;
 485 
 486     if ([attribute isEqualToString:NSAccessibilityChildrenAttribute])
 487     {
 488         JNIEnv *env = [ThreadUtilities getJNIEnv];
 489 
 490         (*env)->PushLocalFrame(env, 4);
 491 
 492         id result = NSAccessibilityUnignoredChildrenForOnlyChild([self getAxData:env]);
 493 
 494         (*env)->PopLocalFrame(env, NULL);
 495 
 496         return result;
 497     }
 498     else
 499     {
 500         return [super accessibilityAttributeValue:attribute];
 501     }
 502 }
 503 - (BOOL)accessibilityIsIgnored
 504 {
 505     return YES;
 506 }
 507 
 508 - (id)accessibilityHitTest:(NSPoint)point
 509 {
 510     AWT_ASSERT_APPKIT_THREAD;
 511     JNIEnv *env = [ThreadUtilities getJNIEnv];
 512 
 513     (*env)->PushLocalFrame(env, 4);
 514 
 515     id result = [[self getAxData:env] accessibilityHitTest:point withEnv:env];
 516 
 517     (*env)->PopLocalFrame(env, NULL);
 518 
 519     return result;
 520 }
 521 
 522 - (id)accessibilityFocusedUIElement
 523 {
 524     AWT_ASSERT_APPKIT_THREAD;
 525 
 526     JNIEnv *env = [ThreadUtilities getJNIEnv];
 527 
 528     (*env)->PushLocalFrame(env, 4);
 529 
 530     id result = [[self getAxData:env] accessibilityFocusedUIElement];
 531 
 532     (*env)->PopLocalFrame(env, NULL);
 533 
 534     return result;
 535 }
 536 
 537 // --- Services menu support for lightweights ---
 538 
 539 // finds the focused accessable element, and if it's a text element, obtains the text from it
 540 - (NSString *)accessibleSelectedText
 541 {
 542     id focused = [self accessibilityFocusedUIElement];
 543     if (![focused isKindOfClass:[JavaTextAccessibility class]]) return nil;
 544     return [(JavaTextAccessibility *)focused accessibilitySelectedTextAttribute];
 545 }
 546 
 547 // same as above, but converts to RTFD
 548 - (NSData *)accessibleSelectedTextAsRTFD
 549 {
 550     NSString *selectedText = [self accessibleSelectedText];
 551     NSAttributedString *styledText = [[NSAttributedString alloc] initWithString:selectedText];
 552     NSData *rtfdData = [styledText RTFDFromRange:NSMakeRange(0, [styledText length]) documentAttributes:nil];
 553     [styledText release];
 554     return rtfdData;
 555 }
 556 
 557 // finds the focused accessable element, and if it's a text element, sets the text in it
 558 - (BOOL)replaceAccessibleTextSelection:(NSString *)text
 559 {
 560     id focused = [self accessibilityFocusedUIElement];
 561     if (![focused isKindOfClass:[JavaTextAccessibility class]]) return NO;
 562     [(JavaTextAccessibility *)focused accessibilitySetSelectedTextAttribute:text];
 563     return YES;
 564 }
 565 
 566 // called for each service in the Services menu - only handle text for now
 567 - (id)validRequestorForSendType:(NSString *)sendType returnType:(NSString *)returnType
 568 {
 569     if ([[self window] firstResponder] != self) return nil; // let AWT components handle themselves
 570 
 571     if ([sendType isEqual:NSStringPboardType] || [returnType isEqual:NSStringPboardType]) {
 572         NSString *selectedText = [self accessibleSelectedText];
 573         if (selectedText) return self;
 574     }
 575 
 576     return nil;
 577 }
 578 
 579 // fetch text from Java and hand off to the service
 580 - (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard types:(NSArray *)types
 581 {
 582     if ([types containsObject:NSStringPboardType])
 583     {
 584         [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
 585         return [pboard setString:[self accessibleSelectedText] forType:NSStringPboardType];
 586     }
 587 
 588     if ([types containsObject:NSRTFDPboardType])
 589     {
 590         [pboard declareTypes:[NSArray arrayWithObject:NSRTFDPboardType] owner:nil];
 591         return [pboard setData:[self accessibleSelectedTextAsRTFD] forType:NSRTFDPboardType];
 592     }
 593 
 594     return NO;
 595 }
 596 
 597 // write text back to Java from the service
 598 - (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard
 599 {
 600     if ([[pboard types] containsObject:NSStringPboardType])
 601     {
 602         NSString *text = [pboard stringForType:NSStringPboardType];
 603         return [self replaceAccessibleTextSelection:text];
 604     }
 605 
 606     if ([[pboard types] containsObject:NSRTFDPboardType])
 607     {
 608         NSData *rtfdData = [pboard dataForType:NSRTFDPboardType];
 609         NSAttributedString *styledText = [[NSAttributedString alloc] initWithRTFD:rtfdData documentAttributes:nil];
 610         NSString *text = [styledText string];
 611         [styledText release];
 612 
 613         return [self replaceAccessibleTextSelection:text];
 614     }
 615 
 616     return NO;
 617 }
 618 
 619 
 620 -(void) setDragSource:(CDragSource *)source {
 621     self._dragSource = source;
 622 }
 623 
 624 
 625 - (void) setDropTarget:(CDropTarget *)target {
 626     self._dropTarget = target;
 627     [ThreadUtilities performOnMainThread:@selector(controlModelControlValid) onObject:self._dropTarget withObject:nil waitUntilDone:YES awtMode:YES];
 628 }
 629 
 630 /********************************  BEGIN NSDraggingSource Interface  ********************************/
 631 
 632 - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)flag
 633 {
 634     // If draggingSource is nil route the message to the superclass (if responding to the selector):
 635     CDragSource *dragSource = self._dragSource;
 636     NSDragOperation dragOp = NSDragOperationNone;
 637 
 638     if (dragSource != nil)
 639         dragOp = [dragSource draggingSourceOperationMaskForLocal:flag];
 640     else if ([super respondsToSelector:@selector(draggingSourceOperationMaskForLocal:)])
 641         dragOp = [super draggingSourceOperationMaskForLocal:flag];
 642 
 643     return dragOp;
 644 }
 645 
 646 - (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination
 647 {
 648     // If draggingSource is nil route the message to the superclass (if responding to the selector):
 649     CDragSource *dragSource = self._dragSource;
 650     NSArray* array = nil;
 651 
 652     if (dragSource != nil)
 653         array = [dragSource namesOfPromisedFilesDroppedAtDestination:dropDestination];
 654     else if ([super respondsToSelector:@selector(namesOfPromisedFilesDroppedAtDestination:)])
 655         array = [super namesOfPromisedFilesDroppedAtDestination:dropDestination];
 656 
 657     return array;
 658 }
 659 
 660 - (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint
 661 {
 662     // If draggingSource is nil route the message to the superclass (if responding to the selector):
 663     CDragSource *dragSource = self._dragSource;
 664 
 665     if (dragSource != nil)
 666         [dragSource draggedImage:image beganAt:screenPoint];
 667     else if ([super respondsToSelector:@selector(draggedImage::)])
 668         [super draggedImage:image beganAt:screenPoint];
 669 }
 670 
 671 - (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint operation:(NSDragOperation)operation
 672 {
 673     // If draggingSource is nil route the message to the superclass (if responding to the selector):
 674     CDragSource *dragSource = self._dragSource;
 675 
 676     if (dragSource != nil)
 677         [dragSource draggedImage:image endedAt:screenPoint operation:operation];
 678     else if ([super respondsToSelector:@selector(draggedImage:::)])
 679         [super draggedImage:image endedAt:screenPoint operation:operation];
 680 }
 681 
 682 - (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint
 683 {
 684     // If draggingSource is nil route the message to the superclass (if responding to the selector):
 685     CDragSource *dragSource = self._dragSource;
 686 
 687     if (dragSource != nil)
 688         [dragSource draggedImage:image movedTo:screenPoint];
 689     else if ([super respondsToSelector:@selector(draggedImage::)])
 690         [super draggedImage:image movedTo:screenPoint];
 691 }
 692 
 693 - (BOOL)ignoreModifierKeysWhileDragging
 694 {
 695     // If draggingSource is nil route the message to the superclass (if responding to the selector):
 696     CDragSource *dragSource = self._dragSource;
 697     BOOL result = FALSE;
 698 
 699     if (dragSource != nil)
 700         result = [dragSource ignoreModifierKeysWhileDragging];
 701     else if ([super respondsToSelector:@selector(ignoreModifierKeysWhileDragging)])
 702         result = [super ignoreModifierKeysWhileDragging];
 703 
 704     return result;
 705 }
 706 
 707 /********************************  END NSDraggingSource Interface  ********************************/
 708 
 709 /********************************  BEGIN NSDraggingDestination Interface  ********************************/
 710 
 711 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
 712 {
 713     // If draggingDestination is nil route the message to the superclass:
 714     CDropTarget *dropTarget = self._dropTarget;
 715     NSDragOperation dragOp = NSDragOperationNone;
 716 
 717     if (dropTarget != nil)
 718         dragOp = [dropTarget draggingEntered:sender];
 719     else if ([super respondsToSelector:@selector(draggingEntered:)])
 720         dragOp = [super draggingEntered:sender];
 721 
 722     return dragOp;
 723 }
 724 
 725 - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
 726 {
 727     // If draggingDestination is nil route the message to the superclass:
 728     CDropTarget *dropTarget = self._dropTarget;
 729     NSDragOperation dragOp = NSDragOperationNone;
 730 
 731     if (dropTarget != nil)
 732         dragOp = [dropTarget draggingUpdated:sender];
 733     else if ([super respondsToSelector:@selector(draggingUpdated:)])
 734         dragOp = [super draggingUpdated:sender];
 735 
 736     return dragOp;
 737 }
 738 
 739 - (void)draggingExited:(id <NSDraggingInfo>)sender
 740 {
 741     // If draggingDestination is nil route the message to the superclass:
 742     CDropTarget *dropTarget = self._dropTarget;
 743 
 744     if (dropTarget != nil)
 745         [dropTarget draggingExited:sender];
 746     else if ([super respondsToSelector:@selector(draggingExited:)])
 747         [super draggingExited:sender];
 748 }
 749 
 750 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
 751 {
 752     // If draggingDestination is nil route the message to the superclass:
 753     CDropTarget *dropTarget = self._dropTarget;
 754     BOOL result = FALSE;
 755 
 756     if (dropTarget != nil)
 757         result = [dropTarget prepareForDragOperation:sender];
 758     else if ([super respondsToSelector:@selector(prepareForDragOperation:)])
 759         result = [super prepareForDragOperation:sender];
 760 
 761     return result;
 762 }
 763 
 764 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
 765 {
 766     // If draggingDestination is nil route the message to the superclass:
 767     CDropTarget *dropTarget = self._dropTarget;
 768     BOOL result = FALSE;
 769 
 770     if (dropTarget != nil)
 771         result = [dropTarget performDragOperation:sender];
 772     else if ([super respondsToSelector:@selector(performDragOperation:)])
 773         result = [super performDragOperation:sender];
 774 
 775     return result;
 776 }
 777 
 778 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
 779 {
 780     // If draggingDestination is nil route the message to the superclass:
 781     CDropTarget *dropTarget = self._dropTarget;
 782 
 783     if (dropTarget != nil)
 784         [dropTarget concludeDragOperation:sender];
 785     else if ([super respondsToSelector:@selector(concludeDragOperation:)])
 786         [super concludeDragOperation:sender];
 787 }
 788 
 789 - (void)draggingEnded:(id <NSDraggingInfo>)sender
 790 {
 791     // If draggingDestination is nil route the message to the superclass:
 792     CDropTarget *dropTarget = self._dropTarget;
 793 
 794     if (dropTarget != nil)
 795         [dropTarget draggingEnded:sender];
 796     else if ([super respondsToSelector:@selector(draggingEnded:)])
 797         [super draggingEnded:sender];
 798 }
 799 
 800 /********************************  END NSDraggingDestination Interface  ********************************/
 801 
 802 /********************************  BEGIN NSTextInputClient Protocol  ********************************/
 803 
 804 
 805 JNF_CLASS_CACHE(jc_CInputMethod, "sun/lwawt/macosx/CInputMethod");
 806 
 807 - (void) insertText:(id)aString replacementRange:(NSRange)replacementRange
 808 {
 809 #ifdef IM_DEBUG
 810     fprintf(stderr, "AWTView InputMethod Selector Called : [insertText]: %s\n", [aString UTF8String]);
 811 #endif // IM_DEBUG
 812 
 813     if (fInputMethodLOCKABLE == NULL) {
 814         return;
 815     }
 816 
 817     // Insert happens at the end of PAH
 818     fInPressAndHold = NO;
 819 
 820     // insertText gets called when the user commits text generated from an input method.  It also gets
 821     // called during ordinary input as well.  We only need to send an input method event when we have marked
 822     // text, or 'text in progress'.  We also need to send the event if we get an insert text out of the blue!
 823     // (i.e., when the user uses the Character palette or Inkwell), or when the string to insert is a complex
 824     // Unicode value.
 825     NSUInteger utf8Length = [aString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
 826 
 827     if ([self hasMarkedText] || !fProcessingKeystroke || (utf8Length > 1)) {
 828         JNIEnv *env = [ThreadUtilities getJNIEnv];
 829 
 830         static JNF_MEMBER_CACHE(jm_selectPreviousGlyph, jc_CInputMethod, "selectPreviousGlyph", "()V");
 831         // We need to select the previous glyph so that it is overwritten.
 832         if (fPAHNeedsToSelect) {
 833             JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_selectPreviousGlyph);
 834             fPAHNeedsToSelect = NO;
 835         }
 836 
 837         static JNF_MEMBER_CACHE(jm_insertText, jc_CInputMethod, "insertText", "(Ljava/lang/String;)V");
 838         jstring insertedText =  JNFNSToJavaString(env, aString);
 839         JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_insertText, insertedText); // AWT_THREADING Safe (AWTRunLoopMode)
 840         (*env)->DeleteLocalRef(env, insertedText);
 841 
 842         // The input method event will create psuedo-key events for each character in the committed string.
 843         // We also don't want to send the character that triggered the insertText, usually a return. [3337563]
 844         fKeyEventsNeeded = NO;
 845     }
 846 
 847     fPAHNeedsToSelect = NO;
 848 
 849 }
 850 
 851 - (void) doCommandBySelector:(SEL)aSelector
 852 {
 853 #ifdef IM_DEBUG
 854     fprintf(stderr, "AWTView InputMethod Selector Called : [doCommandBySelector]\n");
 855     NSLog(@"%@", NSStringFromSelector(aSelector));
 856 #endif // IM_DEBUG
 857     if (@selector(insertNewline:) == aSelector || @selector(insertTab:) == aSelector || @selector(deleteBackward:) == aSelector)
 858     {
 859         fKeyEventsNeeded = YES;
 860     }
 861 }
 862 
 863 // setMarkedText: cannot take a nil first argument. aString can be NSString or NSAttributedString
 864 - (void) setMarkedText:(id)aString selectedRange:(NSRange)selectionRange replacementRange:(NSRange)replacementRange
 865 {
 866     if (!fInputMethodLOCKABLE)
 867         return;
 868 
 869     BOOL isAttributedString = [aString isKindOfClass:[NSAttributedString class]];
 870     NSAttributedString *attrString = (isAttributedString ? (NSAttributedString *)aString : nil);
 871     NSString *incomingString = (isAttributedString ? [aString string] : aString);
 872 #ifdef IM_DEBUG
 873     fprintf(stderr, "AWTView InputMethod Selector Called : [setMarkedText] \"%s\", loc=%lu, length=%lu\n", [incomingString UTF8String], (unsigned long)selectionRange.location, (unsigned long)selectionRange.length);
 874 #endif // IM_DEBUG
 875     static JNF_MEMBER_CACHE(jm_startIMUpdate, jc_CInputMethod, "startIMUpdate", "(Ljava/lang/String;)V");
 876     static JNF_MEMBER_CACHE(jm_addAttribute, jc_CInputMethod, "addAttribute", "(ZZII)V");
 877     static JNF_MEMBER_CACHE(jm_dispatchText, jc_CInputMethod, "dispatchText", "(IIZ)V");
 878     JNIEnv *env = [ThreadUtilities getJNIEnv];
 879 
 880     // NSInputContext already did the analysis of the TSM event and created attributes indicating
 881     // the underlining and color that should be done to the string.  We need to look at the underline
 882     // style and color to determine what kind of Java hilighting needs to be done.
 883     jstring inProcessText = JNFNSToJavaString(env, incomingString);
 884     JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_startIMUpdate, inProcessText); // AWT_THREADING Safe (AWTRunLoopMode)
 885     (*env)->DeleteLocalRef(env, inProcessText);
 886 
 887     if (isAttributedString) {
 888         NSUInteger length;
 889         NSRange effectiveRange;
 890         NSDictionary *attributes;
 891         length = [attrString length];
 892         effectiveRange = NSMakeRange(0, 0);
 893         while (NSMaxRange(effectiveRange) < length) {
 894             attributes = [attrString attributesAtIndex:NSMaxRange(effectiveRange)
 895                                         effectiveRange:&effectiveRange];
 896             if (attributes) {
 897                 BOOL isThickUnderline, isGray;
 898                 NSNumber *underlineSizeObj =
 899                 (NSNumber *)[attributes objectForKey:NSUnderlineStyleAttributeName];
 900                 NSInteger underlineSize = [underlineSizeObj integerValue];
 901                 isThickUnderline = (underlineSize > 1);
 902 
 903                 NSColor *underlineColorObj =
 904                 (NSColor *)[attributes objectForKey:NSUnderlineColorAttributeName];
 905                 isGray = !([underlineColorObj isEqual:[NSColor blackColor]]);
 906 
 907                 JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_addAttribute, isThickUnderline, isGray, effectiveRange.location, effectiveRange.length); // AWT_THREADING Safe (AWTRunLoopMode)
 908             }
 909         }
 910     }
 911 
 912     static JNF_MEMBER_CACHE(jm_selectPreviousGlyph, jc_CInputMethod, "selectPreviousGlyph", "()V");
 913     // We need to select the previous glyph so that it is overwritten.
 914     if (fPAHNeedsToSelect) {
 915         JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_selectPreviousGlyph);
 916         fPAHNeedsToSelect = NO;
 917     }
 918 
 919     JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_dispatchText, selectionRange.location, selectionRange.length, JNI_FALSE); // AWT_THREADING Safe (AWTRunLoopMode)
 920 
 921     // If the marked text is being cleared (zero-length string) don't handle the key event.
 922     if ([incomingString length] == 0) {
 923         fKeyEventsNeeded = NO;
 924     }
 925 }
 926 
 927 - (void) unmarkText
 928 {
 929 #ifdef IM_DEBUG
 930     fprintf(stderr, "AWTView InputMethod Selector Called : [unmarkText]\n");
 931 #endif // IM_DEBUG
 932 
 933     if (!fInputMethodLOCKABLE) {
 934         return;
 935     }
 936 
 937     // unmarkText cancels any input in progress and commits it to the text field.
 938     static JNF_MEMBER_CACHE(jm_unmarkText, jc_CInputMethod, "unmarkText", "()V");
 939     JNIEnv *env = [ThreadUtilities getJNIEnv];
 940     JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_unmarkText); // AWT_THREADING Safe (AWTRunLoopMode)
 941 
 942 }
 943 
 944 - (BOOL) hasMarkedText
 945 {
 946 #ifdef IM_DEBUG
 947     fprintf(stderr, "AWTView InputMethod Selector Called : [hasMarkedText]\n");
 948 #endif // IM_DEBUG
 949 
 950     if (!fInputMethodLOCKABLE) {
 951         return NO;
 952     }
 953 
 954     static JNF_MEMBER_CACHE(jf_fCurrentText, jc_CInputMethod, "fCurrentText", "Ljava/text/AttributedString;");
 955     static JNF_MEMBER_CACHE(jf_fCurrentTextLength, jc_CInputMethod, "fCurrentTextLength", "I");
 956     JNIEnv *env = [ThreadUtilities getJNIEnv];
 957     jobject currentText = JNFGetObjectField(env, fInputMethodLOCKABLE, jf_fCurrentText);
 958 
 959     jint currentTextLength = JNFGetIntField(env, fInputMethodLOCKABLE, jf_fCurrentTextLength);
 960 
 961     BOOL hasMarkedText = (currentText != NULL && currentTextLength > 0);
 962 
 963     if (currentText != NULL) {
 964         (*env)->DeleteLocalRef(env, currentText);
 965     }
 966 
 967     return hasMarkedText;
 968 }
 969 
 970 - (NSInteger) conversationIdentifier
 971 {
 972 #ifdef IM_DEBUG
 973     fprintf(stderr, "AWTView InputMethod Selector Called : [conversationIdentifier]\n");
 974 #endif // IM_DEBUG
 975 
 976     return (NSInteger) self;
 977 }
 978 
 979 /* Returns attributed string at the range.  This allows input mangers to
 980  query any range in backing-store (Andy's request)
 981  */
 982 - (NSAttributedString *) attributedSubstringForProposedRange:(NSRange)theRange actualRange:(NSRangePointer)actualRange
 983 {
 984 #ifdef IM_DEBUG
 985     fprintf(stderr, "AWTView InputMethod Selector Called : [attributedSubstringFromRange] location=%lu, length=%lu\n", (unsigned long)theRange.location, (unsigned long)theRange.length);
 986 #endif // IM_DEBUG
 987 
 988     static JNF_MEMBER_CACHE(jm_substringFromRange, jc_CInputMethod, "attributedSubstringFromRange", "(II)Ljava/lang/String;");
 989     JNIEnv *env = [ThreadUtilities getJNIEnv];
 990     jobject theString = JNFCallObjectMethod(env, fInputMethodLOCKABLE, jm_substringFromRange, theRange.location, theRange.length); // AWT_THREADING Safe (AWTRunLoopMode)
 991 
 992     id result = [[[NSAttributedString alloc] initWithString:JNFJavaToNSString(env, theString)] autorelease];
 993 #ifdef IM_DEBUG
 994     NSLog(@"attributedSubstringFromRange returning \"%@\"", result);
 995 #endif // IM_DEBUG
 996 
 997     (*env)->DeleteLocalRef(env, theString);
 998     return result;
 999 }
1000 
1001 /* This method returns the range for marked region.  If hasMarkedText == false,
1002  it'll return NSNotFound location & 0 length range.
1003  */
1004 - (NSRange) markedRange
1005 {
1006 
1007 #ifdef IM_DEBUG
1008     fprintf(stderr, "AWTView InputMethod Selector Called : [markedRange]\n");
1009 #endif // IM_DEBUG
1010 
1011     if (!fInputMethodLOCKABLE) {
1012         return NSMakeRange(NSNotFound, 0);
1013     }
1014 
1015     static JNF_MEMBER_CACHE(jm_markedRange, jc_CInputMethod, "markedRange", "()[I");
1016     JNIEnv *env = [ThreadUtilities getJNIEnv];
1017     jarray array;
1018     jboolean isCopy;
1019     jint *_array;
1020     NSRange range;
1021 
1022     array = JNFCallObjectMethod(env, fInputMethodLOCKABLE, jm_markedRange); // AWT_THREADING Safe (AWTRunLoopMode)
1023 
1024     if (array) {
1025         _array = (*env)->GetIntArrayElements(env, array, &isCopy);
1026         range = NSMakeRange(_array[0], _array[1]);
1027 
1028 #ifdef IM_DEBUG
1029         fprintf(stderr, "markedRange returning (%lu, %lu)\n", (unsigned long)range.location, (unsigned long)range.length);
1030 #endif // IM_DEBUG
1031         (*env)->ReleaseIntArrayElements(env, array, _array, 0);
1032         (*env)->DeleteLocalRef(env, array);
1033     } else {
1034         range = NSMakeRange(NSNotFound, 0);
1035     }
1036 
1037     return range;
1038 }
1039 
1040 /* This method returns the range for selected region.  Just like markedRange method,
1041  its location field contains char index from the text beginning.
1042  */
1043 - (NSRange) selectedRange
1044 {
1045     if (!fInputMethodLOCKABLE) {
1046         return NSMakeRange(NSNotFound, 0);
1047     }
1048 
1049     static JNF_MEMBER_CACHE(jm_selectedRange, jc_CInputMethod, "selectedRange", "()[I");
1050     JNIEnv *env = [ThreadUtilities getJNIEnv];
1051     jarray array;
1052     jboolean isCopy;
1053     jint *_array;
1054     NSRange range;
1055 
1056 #ifdef IM_DEBUG
1057     fprintf(stderr, "AWTView InputMethod Selector Called : [selectedRange]\n");
1058 #endif // IM_DEBUG
1059 
1060     array = JNFCallObjectMethod(env, fInputMethodLOCKABLE, jm_selectedRange); // AWT_THREADING Safe (AWTRunLoopMode)
1061     if (array) {
1062         _array = (*env)->GetIntArrayElements(env, array, &isCopy);
1063         range = NSMakeRange(_array[0], _array[1]);
1064         (*env)->ReleaseIntArrayElements(env, array, _array, 0);
1065         (*env)->DeleteLocalRef(env, array);
1066     } else {
1067         range = NSMakeRange(NSNotFound, 0);
1068     }
1069 
1070     return range;
1071 
1072 }
1073 
1074 /* This method returns the first frame of rects for theRange in screen coordindate system.
1075  */
1076 - (NSRect) firstRectForCharacterRange:(NSRange)theRange actualRange:(NSRangePointer)actualRange
1077 {
1078     if (!fInputMethodLOCKABLE) {
1079         return NSMakeRect(0, 0, 0, 0);
1080     }
1081 
1082     static JNF_MEMBER_CACHE(jm_firstRectForCharacterRange, jc_CInputMethod,
1083                             "firstRectForCharacterRange", "(I)[I");
1084     JNIEnv *env = [ThreadUtilities getJNIEnv];
1085     jarray array;
1086     jboolean isCopy;
1087     jint *_array;
1088     NSRect rect;
1089 
1090 #ifdef IM_DEBUG
1091     fprintf(stderr, "AWTView InputMethod Selector Called : [firstRectForCharacterRange:] location=%lu, length=%lu\n", (unsigned long)theRange.location, (unsigned long)theRange.length);
1092 #endif // IM_DEBUG
1093 
1094     array = JNFCallObjectMethod(env, fInputMethodLOCKABLE, jm_firstRectForCharacterRange, theRange.location); // AWT_THREADING Safe (AWTRunLoopMode)
1095 
1096     _array = (*env)->GetIntArrayElements(env, array, &isCopy);
1097     rect = ConvertNSScreenRect(env, NSMakeRect(_array[0], _array[1], _array[2], _array[3]));
1098     (*env)->ReleaseIntArrayElements(env, array, _array, 0);
1099     (*env)->DeleteLocalRef(env, array);
1100 
1101 #ifdef IM_DEBUG
1102     fprintf(stderr, "firstRectForCharacterRange returning x=%f, y=%f, width=%f, height=%f\n", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
1103 #endif // IM_DEBUG
1104     return rect;
1105 }
1106 
1107 /* This method returns the index for character that is nearest to thePoint.  thPoint is in
1108  screen coordinate system.
1109  */
1110 - (NSUInteger)characterIndexForPoint:(NSPoint)thePoint
1111 {
1112     if (!fInputMethodLOCKABLE) {
1113         return NSNotFound;
1114     }
1115 
1116     static JNF_MEMBER_CACHE(jm_characterIndexForPoint, jc_CInputMethod,
1117                             "characterIndexForPoint", "(II)I");
1118     JNIEnv *env = [ThreadUtilities getJNIEnv];
1119 
1120     NSPoint flippedLocation = ConvertNSScreenPoint(env, thePoint);
1121 
1122 #ifdef IM_DEBUG
1123     fprintf(stderr, "AWTView InputMethod Selector Called : [characterIndexForPoint:(NSPoint)thePoint] x=%f, y=%f\n", flippedLocation.x, flippedLocation.y);
1124 #endif // IM_DEBUG
1125 
1126     jint index = JNFCallIntMethod(env, fInputMethodLOCKABLE, jm_characterIndexForPoint, (jint)flippedLocation.x, (jint)flippedLocation.y); // AWT_THREADING Safe (AWTRunLoopMode)
1127 
1128 #ifdef IM_DEBUG
1129     fprintf(stderr, "characterIndexForPoint returning %ld\n", index);
1130 #endif // IM_DEBUG
1131 
1132     if (index == -1) {
1133         return NSNotFound;
1134     } else {
1135         return (NSUInteger)index;
1136     }
1137 }
1138 
1139 - (NSArray*) validAttributesForMarkedText
1140 {
1141 #ifdef IM_DEBUG
1142     fprintf(stderr, "AWTView InputMethod Selector Called : [validAttributesForMarkedText]\n");
1143 #endif // IM_DEBUG
1144 
1145     return [NSArray array];
1146 }
1147 
1148 - (void)setInputMethod:(jobject)inputMethod
1149 {
1150 #ifdef IM_DEBUG
1151     fprintf(stderr, "AWTView InputMethod Selector Called : [setInputMethod]\n");
1152 #endif // IM_DEBUG
1153 
1154     JNIEnv *env = [ThreadUtilities getJNIEnv];
1155 
1156     // Get rid of the old one
1157     if (fInputMethodLOCKABLE) {
1158         JNFDeleteGlobalRef(env, fInputMethodLOCKABLE);
1159     }
1160 
1161     // Save a global ref to the new input method.
1162     if (inputMethod != NULL)
1163         fInputMethodLOCKABLE = JNFNewGlobalRef(env, inputMethod);
1164     else
1165         fInputMethodLOCKABLE = NULL;
1166 }
1167 
1168 - (void)abandonInput
1169 {
1170 #ifdef IM_DEBUG
1171     fprintf(stderr, "AWTView InputMethod Selector Called : [abandonInput]\n");
1172 #endif // IM_DEBUG
1173 
1174     [ThreadUtilities performOnMainThread:@selector(markedTextAbandoned:) onObject:[NSInputManager currentInputManager] withObject:self waitUntilDone:YES awtMode:YES];
1175     [self unmarkText];
1176 }
1177 
1178 /********************************   END NSTextInputClient Protocol   ********************************/
1179 
1180 
1181 
1182 
1183 @end // AWTView
1184 
1185 /*
1186  * Class:     sun_lwawt_macosx_CPlatformView
1187  * Method:    nativeCreateView
1188  * Signature: (IIII)J
1189  */
1190 JNIEXPORT jlong JNICALL
1191 Java_sun_lwawt_macosx_CPlatformView_nativeCreateView
1192 (JNIEnv *env, jobject obj, jint originX, jint originY, jint width, jint height, jlong windowLayerPtr)
1193 {
1194     __block AWTView *newView = nil;
1195 
1196 JNF_COCOA_ENTER(env);
1197 AWT_ASSERT_NOT_APPKIT_THREAD;
1198 
1199     NSRect rect = NSMakeRect(originX, originY, width, height);
1200     jobject cPlatformView = (*env)->NewGlobalRef(env, obj);
1201 
1202     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
1203         AWT_ASSERT_APPKIT_THREAD;
1204 
1205         CALayer *windowLayer = jlong_to_ptr(windowLayerPtr);
1206         AWTView *view = [[AWTView alloc] initWithRect:rect
1207                                          platformView:cPlatformView
1208                                          windowLayer:windowLayer];
1209         CFRetain(view);
1210         [view release]; // GC
1211 
1212         newView = view;
1213     }];
1214 
1215 JNF_COCOA_EXIT(env);
1216 
1217     return ptr_to_jlong(newView);
1218 }