< prev index next >

src/java.desktop/macosx/native/libawt_lwawt/awt/AWTView.m

Print this page
rev 54094 : 8257853: Remove dependencies on JNF's JNI utility functions in AWT and 2D code
rev 54095 : 8259343: [macOS] Update JNI error handling in Cocoa code.
rev 54096 : 8259651: [macOS] Replace JNF_COCOA_ENTER/EXIT macros
rev 54097 : 8259869: [macOS] Remove desktop module dependencies on JNF Reference APIs
rev 54098 : 8260616: Removing remaining JNF dependencies in the java.desktop module
8259729: Missed JNFInstanceOf -> IsInstanceOf conversion


  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 "jni_util.h"
  27 #import "CGLGraphicsConfig.h"
  28 #import "AWTView.h"
  29 #import "AWTWindow.h"
  30 #import "JavaComponentAccessibility.h"
  31 #import "JavaTextAccessibility.h"
  32 #import "JavaAccessibilityUtilities.h"
  33 #import "GeomUtilities.h"
  34 #import "OSVersion.h"
  35 #import "ThreadUtilities.h"

  36 
  37 #import <Carbon/Carbon.h>
  38 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  39 
  40 // keyboard layout
  41 static NSString *kbdLayout;
  42 
  43 @interface AWTView()
  44 @property (retain) CDropTarget *_dropTarget;
  45 @property (retain) CDragSource *_dragSource;
  46 
  47 -(void) deliverResize: (NSRect) rect;
  48 -(void) resetTrackingArea;
  49 -(void) deliverJavaKeyEventHelper: (NSEvent*) event;
  50 -(BOOL) isCodePointInUnicodeBlockNeedingIMEvent: (unichar) codePoint;
  51 -(NSMutableString *) parseString : (id) complexString;
  52 @end
  53 
  54 // Uncomment this line to see fprintfs of each InputMethod API being called on this View
  55 //#define IM_DEBUG TRUE
  56 //#define EXTRA_DEBUG
  57 
  58 static BOOL shouldUsePressAndHold() {


 124         }
 125 #endif /* REMOTELAYER */
 126     }
 127 
 128     return self;
 129 }
 130 
 131 - (void) dealloc {
 132     AWT_ASSERT_APPKIT_THREAD;
 133 
 134     self.cglLayer = nil;
 135 
 136     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 137     (*env)->DeleteWeakGlobalRef(env, m_cPlatformView);
 138     m_cPlatformView = NULL;
 139 
 140     if (fInputMethodLOCKABLE != NULL)
 141     {
 142         JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 143 
 144         JNFDeleteGlobalRef(env, fInputMethodLOCKABLE);
 145         fInputMethodLOCKABLE = NULL;
 146     }
 147 
 148     if (rolloverTrackingArea != nil) {
 149         [self removeTrackingArea:rolloverTrackingArea];
 150         [rolloverTrackingArea release];
 151         rolloverTrackingArea = nil;
 152     }
 153 
 154     [super dealloc];
 155 }
 156 
 157 - (void) viewDidMoveToWindow {
 158     AWT_ASSERT_APPKIT_THREAD;
 159 
 160     [AWTToolkit eventCountPlusPlus];
 161 
 162     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^() {
 163         [[self window] makeFirstResponder: self];
 164     }];
 165     if ([self window] != NULL) {
 166         [self resetTrackingArea];
 167     }
 168 }
 169 
 170 - (BOOL) acceptsFirstMouse: (NSEvent *)event {
 171     return YES;
 172 }
 173 
 174 - (BOOL) acceptsFirstResponder {
 175     return YES;
 176 }
 177 
 178 - (BOOL) becomeFirstResponder {
 179     return YES;
 180 }
 181 
 182 - (BOOL) preservesContentDuringLiveResize {


 401     NSRect screenRect = [[[NSScreen screens] objectAtIndex:0] frame];
 402     absP.y = screenRect.size.height - absP.y;
 403     jint clickCount;
 404 
 405     if (type == NSMouseEntered ||
 406         type == NSMouseExited ||
 407         type == NSScrollWheel ||
 408         type == NSMouseMoved) {
 409         clickCount = 0;
 410     } else {
 411         clickCount = [event clickCount];
 412     }
 413 
 414     jdouble deltaX = [event deltaX];
 415     jdouble deltaY = [event deltaY];
 416     if ([AWTToolkit hasPreciseScrollingDeltas: event]) {
 417         deltaX = [event scrollingDeltaX] * 0.1;
 418         deltaY = [event scrollingDeltaY] * 0.1;
 419     }
 420 
 421     static JNF_CLASS_CACHE(jc_NSEvent, "sun/lwawt/macosx/NSEvent");
 422     static JNF_CTOR_CACHE(jctor_NSEvent, jc_NSEvent, "(IIIIIIIIDDI)V");
 423     jobject jEvent = JNFNewObject(env, jctor_NSEvent,
 424                                   [event type],
 425                                   [event modifierFlags],
 426                                   clickCount,
 427                                   [event buttonNumber],
 428                                   (jint)localPoint.x, (jint)localPoint.y,
 429                                   (jint)absP.x, (jint)absP.y,
 430                                   deltaY,
 431                                   deltaX,
 432                                   [AWTToolkit scrollStateWithEvent: event]);
 433     CHECK_NULL(jEvent);
 434 
 435     static JNF_CLASS_CACHE(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");
 436     static JNF_MEMBER_CACHE(jm_deliverMouseEvent, jc_PlatformView, "deliverMouseEvent", "(Lsun/lwawt/macosx/NSEvent;)V");
 437     jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
 438     if (!(*env)->IsSameObject(env, jlocal, NULL)) {
 439         JNFCallVoidMethod(env, jlocal, jm_deliverMouseEvent, jEvent);

 440         (*env)->DeleteLocalRef(env, jlocal);
 441     }
 442     (*env)->DeleteLocalRef(env, jEvent);
 443 }
 444 
 445 - (void) resetTrackingArea {
 446     if (rolloverTrackingArea != nil) {
 447         [self removeTrackingArea:rolloverTrackingArea];
 448         [rolloverTrackingArea release];
 449     }
 450 
 451     int options = (NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited |
 452                    NSTrackingMouseMoved | NSTrackingEnabledDuringMouseDrag);
 453 
 454     rolloverTrackingArea = [[NSTrackingArea alloc] initWithRect:[self visibleRect]
 455                                                         options: options
 456                                                           owner:self
 457                                                        userInfo:nil
 458                             ];
 459     [self addTrackingArea:rolloverTrackingArea];


 467 - (void) resetCursorRects {
 468     [super resetCursorRects];
 469     [self resetTrackingArea];
 470 }
 471 
 472 -(void) deliverJavaKeyEventHelper: (NSEvent *) event {
 473     static NSEvent* sLastKeyEvent = nil;
 474     if (event == sLastKeyEvent) {
 475         // The event is repeatedly delivered by keyDown: after performKeyEquivalent:
 476         return;
 477     }
 478     [sLastKeyEvent release];
 479     sLastKeyEvent = [event retain];
 480 
 481     [AWTToolkit eventCountPlusPlus];
 482     JNIEnv *env = [ThreadUtilities getJNIEnv];
 483 
 484     jstring characters = NULL;
 485     jstring charactersIgnoringModifiers = NULL;
 486     if ([event type] != NSFlagsChanged) {
 487         characters = JNFNSToJavaString(env, [event characters]);
 488         charactersIgnoringModifiers = JNFNSToJavaString(env, [event charactersIgnoringModifiers]);
 489     }
 490 
 491     static JNF_CLASS_CACHE(jc_NSEvent, "sun/lwawt/macosx/NSEvent");
 492     static JNF_CTOR_CACHE(jctor_NSEvent, jc_NSEvent, "(IISLjava/lang/String;Ljava/lang/String;)V");
 493     jobject jEvent = JNFNewObject(env, jctor_NSEvent,
 494                                   [event type],
 495                                   [event modifierFlags],
 496                                   [event keyCode],
 497                                   characters,
 498                                   charactersIgnoringModifiers);
 499     CHECK_NULL(jEvent);
 500 
 501     static JNF_CLASS_CACHE(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");
 502     static JNF_MEMBER_CACHE(jm_deliverKeyEvent, jc_PlatformView,
 503                             "deliverKeyEvent", "(Lsun/lwawt/macosx/NSEvent;)V");
 504     jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
 505     if (!(*env)->IsSameObject(env, jlocal, NULL)) {
 506         JNFCallVoidMethod(env, jlocal, jm_deliverKeyEvent, jEvent);

 507         (*env)->DeleteLocalRef(env, jlocal);
 508     }
 509     if (characters != NULL) {
 510         (*env)->DeleteLocalRef(env, characters);
 511     }
 512     (*env)->DeleteLocalRef(env, jEvent);
 513 }
 514 
 515 -(void) deliverResize: (NSRect) rect {
 516     jint x = (jint) rect.origin.x;
 517     jint y = (jint) rect.origin.y;
 518     jint w = (jint) rect.size.width;
 519     jint h = (jint) rect.size.height;
 520     JNIEnv *env = [ThreadUtilities getJNIEnv];
 521     static JNF_CLASS_CACHE(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");
 522     static JNF_MEMBER_CACHE(jm_deliverResize, jc_PlatformView, "deliverResize", "(IIII)V");
 523 
 524     jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
 525     if (!(*env)->IsSameObject(env, jlocal, NULL)) {
 526         JNFCallVoidMethod(env, jlocal, jm_deliverResize, x,y,w,h);

 527         (*env)->DeleteLocalRef(env, jlocal);
 528     }
 529 }
 530 
 531 
 532 - (void) drawRect:(NSRect)dirtyRect {
 533     AWT_ASSERT_APPKIT_THREAD;
 534 
 535     [super drawRect:dirtyRect];
 536     JNIEnv *env = [ThreadUtilities getJNIEnv];
 537     if (env != NULL) {
 538         /*
 539          if ([self inLiveResize]) {
 540          NSRect rs[4];
 541          NSInteger count;
 542          [self getRectsExposedDuringLiveResize:rs count:&count];
 543          for (int i = 0; i < count; i++) {
 544          JNU_CallMethodByName(env, NULL, [m_awtWindow cPlatformView],
 545          "deliverWindowDidExposeEvent", "(FFFF)V",
 546          (jfloat)rs[i].origin.x, (jfloat)rs[i].origin.y,
 547          (jfloat)rs[i].size.width, (jfloat)rs[i].size.height);
 548          if ((*env)->ExceptionOccurred(env)) {
 549          (*env)->ExceptionDescribe(env);
 550          (*env)->ExceptionClear(env);
 551          }
 552          }
 553          } else {
 554          */
 555         static JNF_CLASS_CACHE(jc_CPlatformView, "sun/lwawt/macosx/CPlatformView");
 556         static JNF_MEMBER_CACHE(jm_deliverWindowDidExposeEvent, jc_CPlatformView, "deliverWindowDidExposeEvent", "()V");
 557         jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
 558         if (!(*env)->IsSameObject(env, jlocal, NULL)) {
 559             JNFCallVoidMethod(env, jlocal, jm_deliverWindowDidExposeEvent);

 560             (*env)->DeleteLocalRef(env, jlocal);
 561         }
 562         /*
 563          }
 564          */
 565     }
 566 }
 567 
 568 -(BOOL) isCodePointInUnicodeBlockNeedingIMEvent: (unichar) codePoint {
 569     if ((codePoint == 0x0024) || (codePoint == 0x00A3) ||
 570         (codePoint == 0x00A5) ||
 571         ((codePoint >= 0x20A3) && (codePoint <= 0x20BF)) ||
 572         ((codePoint >= 0x3000) && (codePoint <= 0x303F)) ||
 573         ((codePoint >= 0xFF00) && (codePoint <= 0xFFEF))) {
 574         // Code point is in 'CJK Symbols and Punctuation' or
 575         // 'Halfwidth and Fullwidth Forms' Unicode block or
 576         // currency symbols unicode
 577         return YES;
 578     }
 579     return NO;
 580 }
 581 
 582 -(NSMutableString *) parseString : (id) complexString {
 583     if ([complexString isKindOfClass:[NSString class]]) {
 584         return [complexString mutableCopy];
 585     }
 586     else {
 587         return [complexString mutableString];
 588     }
 589 }
 590 
 591 // NSAccessibility support
 592 - (jobject)awtComponent:(JNIEnv*)env
 593 {
 594     static JNF_CLASS_CACHE(jc_CPlatformView, "sun/lwawt/macosx/CPlatformView");
 595     static JNF_MEMBER_CACHE(jf_Peer, jc_CPlatformView, "peer", "Lsun/lwawt/LWWindowPeer;");
 596     if ((env == NULL) || (m_cPlatformView == NULL)) {
 597         NSLog(@"Apple AWT : Error AWTView:awtComponent given bad parameters.");
 598         if (env != NULL)
 599         {
 600             JNFDumpJavaStack(env);
 601         }
 602         return NULL;
 603     }
 604 
 605     jobject peer = NULL;
 606     jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
 607     if (!(*env)->IsSameObject(env, jlocal, NULL)) {
 608         peer = JNFGetObjectField(env, jlocal, jf_Peer);
 609         (*env)->DeleteLocalRef(env, jlocal);
 610     }
 611     static JNF_CLASS_CACHE(jc_LWWindowPeer, "sun/lwawt/LWWindowPeer");
 612     static JNF_MEMBER_CACHE(jf_Target, jc_LWWindowPeer, "target", "Ljava/awt/Component;");
 613     if (peer == NULL) {
 614         NSLog(@"Apple AWT : Error AWTView:awtComponent got null peer from CPlatformView");
 615         JNFDumpJavaStack(env);
 616         return NULL;
 617     }
 618     jobject comp = JNFGetObjectField(env, peer, jf_Target);
 619     (*env)->DeleteLocalRef(env, peer);
 620     return comp;
 621 }
 622 
 623 + (AWTView *) awtView:(JNIEnv*)env ofAccessible:(jobject)jaccessible
 624 {
 625     static JNF_STATIC_MEMBER_CACHE(jm_getAWTView, sjc_CAccessibility, "getAWTView", "(Ljavax/accessibility/Accessible;)J");

 626 
 627     jlong jptr = JNFCallStaticLongMethod(env, jm_getAWTView, jaccessible);

 628     if (jptr == 0) return nil;
 629 
 630     return (AWTView *)jlong_to_ptr(jptr);
 631 }
 632 
 633 - (id)getAxData:(JNIEnv*)env
 634 {
 635     jobject jcomponent = [self awtComponent:env];
 636     id ax = [[[JavaComponentAccessibility alloc] initWithParent:self withEnv:env withAccessible:jcomponent withIndex:-1 withView:self withJavaRole:nil] autorelease];
 637     (*env)->DeleteLocalRef(env, jcomponent);
 638     return ax;
 639 }
 640 
 641 - (NSArray *)accessibilityAttributeNames
 642 {
 643     return [[super accessibilityAttributeNames] arrayByAddingObject:NSAccessibilityChildrenAttribute];
 644 }
 645 
 646 // NSAccessibility messages
 647 // attribute methods


 933     if (dropTarget != nil) {
 934         [dropTarget concludeDragOperation:sender];
 935     }
 936 }
 937 
 938 - (void)draggingEnded:(id <NSDraggingInfo>)sender
 939 {
 940     // If draggingDestination is nil route the message to the superclass:
 941     CDropTarget *dropTarget = self._dropTarget;
 942 
 943     if (dropTarget != nil) {
 944         [dropTarget draggingEnded:sender];
 945     }
 946 }
 947 
 948 /********************************  END NSDraggingDestination Interface  ********************************/
 949 
 950 /********************************  BEGIN NSTextInputClient Protocol  ********************************/
 951 
 952 
 953 JNF_CLASS_CACHE(jc_CInputMethod, "sun/lwawt/macosx/CInputMethod");






 954 
 955 - (void) insertText:(id)aString replacementRange:(NSRange)replacementRange
 956 {
 957 #ifdef IM_DEBUG
 958     fprintf(stderr, "AWTView InputMethod Selector Called : [insertText]: %s\n", [aString UTF8String]);
 959 #endif // IM_DEBUG
 960 
 961     if (fInputMethodLOCKABLE == NULL) {
 962         return;
 963     }
 964 
 965     // Insert happens at the end of PAH
 966     fInPressAndHold = NO;
 967 
 968     // insertText gets called when the user commits text generated from an input method.  It also gets
 969     // called during ordinary input as well.  We only need to send an input method event when we have marked
 970     // text, or 'text in progress'.  We also need to send the event if we get an insert text out of the blue!
 971     // (i.e., when the user uses the Character palette or Inkwell), or when the string to insert is a complex
 972     // Unicode value.
 973 
 974     NSMutableString * useString = [self parseString:aString];
 975     NSUInteger utf16Length = [useString lengthOfBytesUsingEncoding:NSUTF16StringEncoding];
 976     NSUInteger utf8Length = [useString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
 977     BOOL aStringIsComplex = NO;
 978 
 979     unichar codePoint = [useString characterAtIndex:0];
 980 
 981 #ifdef IM_DEBUG
 982     NSLog(@"insertText kbdlayout %@ ",(NSString *)kbdLayout);
 983 #endif // IM_DEBUG
 984 
 985     if ((utf16Length > 2) ||
 986         ((utf8Length > 1) && [self isCodePointInUnicodeBlockNeedingIMEvent:codePoint]) ||
 987         ((codePoint == 0x5c) && ([(NSString *)kbdLayout containsString:@"Kotoeri"]))) {
 988         aStringIsComplex = YES;
 989     }
 990 
 991     if ([self hasMarkedText] || !fProcessingKeystroke || aStringIsComplex) {
 992         JNIEnv *env = [ThreadUtilities getJNIEnv];
 993 
 994         static JNF_MEMBER_CACHE(jm_selectPreviousGlyph, jc_CInputMethod, "selectPreviousGlyph", "()V");

 995         // We need to select the previous glyph so that it is overwritten.
 996         if (fPAHNeedsToSelect) {
 997             JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_selectPreviousGlyph);

 998             fPAHNeedsToSelect = NO;
 999         }
1000 
1001         static JNF_MEMBER_CACHE(jm_insertText, jc_CInputMethod, "insertText", "(Ljava/lang/String;)V");
1002         jstring insertedText =  JNFNSToJavaString(env, useString);
1003         JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_insertText, insertedText); // AWT_THREADING Safe (AWTRunLoopMode)

1004         (*env)->DeleteLocalRef(env, insertedText);
1005 
1006         // The input method event will create psuedo-key events for each character in the committed string.
1007         // We also don't want to send the character that triggered the insertText, usually a return. [3337563]
1008         fKeyEventsNeeded = NO;
1009     }
1010     else {
1011         // Need to set back the fKeyEventsNeeded flag so that the string following the
1012         // marked text is not ignored by keyDown
1013         if ([useString length] > 0) {
1014             fKeyEventsNeeded = YES;
1015         }
1016     }
1017     fPAHNeedsToSelect = NO;
1018 
1019     // Abandon input to reset IM and unblock input after entering accented
1020     // symbols
1021 
1022     [self abandonInput];
1023 }


1038     NSLog(@"%@", NSStringFromSelector(aSelector));
1039 #endif // IM_DEBUG
1040     if (@selector(insertNewline:) == aSelector || @selector(insertTab:) == aSelector || @selector(deleteBackward:) == aSelector)
1041     {
1042         fKeyEventsNeeded = YES;
1043     }
1044 }
1045 
1046 // setMarkedText: cannot take a nil first argument. aString can be NSString or NSAttributedString
1047 - (void) setMarkedText:(id)aString selectedRange:(NSRange)selectionRange replacementRange:(NSRange)replacementRange
1048 {
1049     if (!fInputMethodLOCKABLE)
1050         return;
1051 
1052     BOOL isAttributedString = [aString isKindOfClass:[NSAttributedString class]];
1053     NSAttributedString *attrString = (isAttributedString ? (NSAttributedString *)aString : nil);
1054     NSString *incomingString = (isAttributedString ? [aString string] : aString);
1055 #ifdef IM_DEBUG
1056     fprintf(stderr, "AWTView InputMethod Selector Called : [setMarkedText] \"%s\", loc=%lu, length=%lu\n", [incomingString UTF8String], (unsigned long)selectionRange.location, (unsigned long)selectionRange.length);
1057 #endif // IM_DEBUG
1058     static JNF_MEMBER_CACHE(jm_startIMUpdate, jc_CInputMethod, "startIMUpdate", "(Ljava/lang/String;)V");
1059     static JNF_MEMBER_CACHE(jm_addAttribute, jc_CInputMethod, "addAttribute", "(ZZII)V");
1060     static JNF_MEMBER_CACHE(jm_dispatchText, jc_CInputMethod, "dispatchText", "(IIZ)V");
1061     JNIEnv *env = [ThreadUtilities getJNIEnv];




1062 
1063     // NSInputContext already did the analysis of the TSM event and created attributes indicating
1064     // the underlining and color that should be done to the string.  We need to look at the underline
1065     // style and color to determine what kind of Java hilighting needs to be done.
1066     jstring inProcessText = JNFNSToJavaString(env, incomingString);
1067     JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_startIMUpdate, inProcessText); // AWT_THREADING Safe (AWTRunLoopMode)

1068     (*env)->DeleteLocalRef(env, inProcessText);
1069 
1070     if (isAttributedString) {
1071         NSUInteger length;
1072         NSRange effectiveRange;
1073         NSDictionary *attributes;
1074         length = [attrString length];
1075         effectiveRange = NSMakeRange(0, 0);
1076         while (NSMaxRange(effectiveRange) < length) {
1077             attributes = [attrString attributesAtIndex:NSMaxRange(effectiveRange)
1078                                         effectiveRange:&effectiveRange];
1079             if (attributes) {
1080                 BOOL isThickUnderline, isGray;
1081                 NSNumber *underlineSizeObj =
1082                 (NSNumber *)[attributes objectForKey:NSUnderlineStyleAttributeName];
1083                 NSInteger underlineSize = [underlineSizeObj integerValue];
1084                 isThickUnderline = (underlineSize > 1);
1085 
1086                 NSColor *underlineColorObj =
1087                 (NSColor *)[attributes objectForKey:NSUnderlineColorAttributeName];
1088                 isGray = !([underlineColorObj isEqual:[NSColor blackColor]]);
1089 
1090                 JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_addAttribute, isThickUnderline, isGray, effectiveRange.location, effectiveRange.length); // AWT_THREADING Safe (AWTRunLoopMode)


1091             }
1092         }
1093     }
1094 
1095     static JNF_MEMBER_CACHE(jm_selectPreviousGlyph, jc_CInputMethod, "selectPreviousGlyph", "()V");
1096     // We need to select the previous glyph so that it is overwritten.
1097     if (fPAHNeedsToSelect) {
1098         JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_selectPreviousGlyph);

1099         fPAHNeedsToSelect = NO;
1100     }
1101 
1102     JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_dispatchText, selectionRange.location, selectionRange.length, JNI_FALSE); // AWT_THREADING Safe (AWTRunLoopMode)
1103 

1104     // If the marked text is being cleared (zero-length string) don't handle the key event.
1105     if ([incomingString length] == 0) {
1106         fKeyEventsNeeded = NO;
1107     }
1108 }
1109 
1110 - (void) unmarkText
1111 {
1112 #ifdef IM_DEBUG
1113     fprintf(stderr, "AWTView InputMethod Selector Called : [unmarkText]\n");
1114 #endif // IM_DEBUG
1115 
1116     if (!fInputMethodLOCKABLE) {
1117         return;
1118     }
1119 
1120     // unmarkText cancels any input in progress and commits it to the text field.
1121     static JNF_MEMBER_CACHE(jm_unmarkText, jc_CInputMethod, "unmarkText", "()V");
1122     JNIEnv *env = [ThreadUtilities getJNIEnv];
1123     JNFCallVoidMethod(env, fInputMethodLOCKABLE, jm_unmarkText); // AWT_THREADING Safe (AWTRunLoopMode)
1124 


1125 }
1126 
1127 - (BOOL) hasMarkedText
1128 {
1129 #ifdef IM_DEBUG
1130     fprintf(stderr, "AWTView InputMethod Selector Called : [hasMarkedText]\n");
1131 #endif // IM_DEBUG
1132 
1133     if (!fInputMethodLOCKABLE) {
1134         return NO;
1135     }
1136 
1137     static JNF_MEMBER_CACHE(jf_fCurrentText, jc_CInputMethod, "fCurrentText", "Ljava/text/AttributedString;");
1138     static JNF_MEMBER_CACHE(jf_fCurrentTextLength, jc_CInputMethod, "fCurrentTextLength", "I");
1139     JNIEnv *env = [ThreadUtilities getJNIEnv];
1140     jobject currentText = JNFGetObjectField(env, fInputMethodLOCKABLE, jf_fCurrentText);




1141 
1142     jint currentTextLength = JNFGetIntField(env, fInputMethodLOCKABLE, jf_fCurrentTextLength);

1143 
1144     BOOL hasMarkedText = (currentText != NULL && currentTextLength > 0);
1145 
1146     if (currentText != NULL) {
1147         (*env)->DeleteLocalRef(env, currentText);
1148     }
1149 
1150     return hasMarkedText;
1151 }
1152 
1153 - (NSInteger) conversationIdentifier
1154 {
1155 #ifdef IM_DEBUG
1156     fprintf(stderr, "AWTView InputMethod Selector Called : [conversationIdentifier]\n");
1157 #endif // IM_DEBUG
1158 
1159     return (NSInteger) self;
1160 }
1161 
1162 /* Returns attributed string at the range.  This allows input mangers to
1163  query any range in backing-store (Andy's request)
1164  */
1165 - (NSAttributedString *) attributedSubstringForProposedRange:(NSRange)theRange actualRange:(NSRangePointer)actualRange
1166 {
1167 #ifdef IM_DEBUG
1168     fprintf(stderr, "AWTView InputMethod Selector Called : [attributedSubstringFromRange] location=%lu, length=%lu\n", (unsigned long)theRange.location, (unsigned long)theRange.length);
1169 #endif // IM_DEBUG
1170 
1171     static JNF_MEMBER_CACHE(jm_substringFromRange, jc_CInputMethod, "attributedSubstringFromRange", "(II)Ljava/lang/String;");
1172     JNIEnv *env = [ThreadUtilities getJNIEnv];
1173     jobject theString = JNFCallObjectMethod(env, fInputMethodLOCKABLE, jm_substringFromRange, theRange.location, theRange.length); // AWT_THREADING Safe (AWTRunLoopMode)


1174 
1175     id result = [[[NSAttributedString alloc] initWithString:JNFJavaToNSString(env, theString)] autorelease];
1176 #ifdef IM_DEBUG
1177     NSLog(@"attributedSubstringFromRange returning \"%@\"", result);
1178 #endif // IM_DEBUG
1179 
1180     (*env)->DeleteLocalRef(env, theString);
1181     return result;
1182 }
1183 
1184 /* This method returns the range for marked region.  If hasMarkedText == false,
1185  it'll return NSNotFound location & 0 length range.
1186  */
1187 - (NSRange) markedRange
1188 {
1189 
1190 #ifdef IM_DEBUG
1191     fprintf(stderr, "AWTView InputMethod Selector Called : [markedRange]\n");
1192 #endif // IM_DEBUG
1193 
1194     if (!fInputMethodLOCKABLE) {
1195         return NSMakeRange(NSNotFound, 0);
1196     }
1197 
1198     static JNF_MEMBER_CACHE(jm_markedRange, jc_CInputMethod, "markedRange", "()[I");
1199     JNIEnv *env = [ThreadUtilities getJNIEnv];
1200     jarray array;
1201     jboolean isCopy;
1202     jint *_array;
1203     NSRange range = NSMakeRange(NSNotFound, 0);


1204 
1205     array = JNFCallObjectMethod(env, fInputMethodLOCKABLE, jm_markedRange); // AWT_THREADING Safe (AWTRunLoopMode)

1206 
1207     if (array) {
1208         _array = (*env)->GetIntArrayElements(env, array, &isCopy);
1209         if (_array != NULL) {
1210             range.location = _array[0];
1211             range.length = _array[1];
1212 #ifdef IM_DEBUG
1213             fprintf(stderr, "markedRange returning (%lu, %lu)\n",
1214                     (unsigned long)range.location, (unsigned long)range.length);
1215 #endif // IM_DEBUG
1216             (*env)->ReleaseIntArrayElements(env, array, _array, 0);
1217         }
1218         (*env)->DeleteLocalRef(env, array);
1219     }
1220 
1221     return range;
1222 }
1223 
1224 /* This method returns the range for selected region.  Just like markedRange method,
1225  its location field contains char index from the text beginning.
1226  */
1227 - (NSRange) selectedRange
1228 {
1229     if (!fInputMethodLOCKABLE) {
1230         return NSMakeRange(NSNotFound, 0);
1231     }
1232 
1233     static JNF_MEMBER_CACHE(jm_selectedRange, jc_CInputMethod, "selectedRange", "()[I");
1234     JNIEnv *env = [ThreadUtilities getJNIEnv];
1235     jarray array;
1236     jboolean isCopy;
1237     jint *_array;
1238     NSRange range = NSMakeRange(NSNotFound, 0);


1239 
1240 #ifdef IM_DEBUG
1241     fprintf(stderr, "AWTView InputMethod Selector Called : [selectedRange]\n");
1242 #endif // IM_DEBUG
1243 
1244     array = JNFCallObjectMethod(env, fInputMethodLOCKABLE, jm_selectedRange); // AWT_THREADING Safe (AWTRunLoopMode)

1245     if (array) {
1246         _array = (*env)->GetIntArrayElements(env, array, &isCopy);
1247         if (_array != NULL) {
1248             range.location = _array[0];
1249             range.length = _array[1];
1250             (*env)->ReleaseIntArrayElements(env, array, _array, 0);
1251         }
1252         (*env)->DeleteLocalRef(env, array);
1253     }
1254 
1255     return range;
1256 }
1257 
1258 /* This method returns the first frame of rects for theRange in screen coordindate system.
1259  */
1260 - (NSRect) firstRectForCharacterRange:(NSRange)theRange actualRange:(NSRangePointer)actualRange
1261 {
1262     if (!fInputMethodLOCKABLE) {
1263         return NSZeroRect;
1264     }
1265 
1266     static JNF_MEMBER_CACHE(jm_firstRectForCharacterRange, jc_CInputMethod,
1267                             "firstRectForCharacterRange", "(I)[I");
1268     JNIEnv *env = [ThreadUtilities getJNIEnv];



1269     jarray array;
1270     jboolean isCopy;
1271     jint *_array;
1272     NSRect rect;
1273 
1274 #ifdef IM_DEBUG
1275     fprintf(stderr,
1276             "AWTView InputMethod Selector Called : [firstRectForCharacterRange:] location=%lu, length=%lu\n",
1277             (unsigned long)theRange.location, (unsigned long)theRange.length);
1278 #endif // IM_DEBUG
1279 
1280     array = JNFCallObjectMethod(env, fInputMethodLOCKABLE, jm_firstRectForCharacterRange,
1281                                 theRange.location); // AWT_THREADING Safe (AWTRunLoopMode)

1282 
1283     _array = (*env)->GetIntArrayElements(env, array, &isCopy);
1284     if (_array) {
1285         rect = ConvertNSScreenRect(env, NSMakeRect(_array[0], _array[1], _array[2], _array[3]));
1286         (*env)->ReleaseIntArrayElements(env, array, _array, 0);
1287     } else {
1288         rect = NSZeroRect;
1289     }
1290     (*env)->DeleteLocalRef(env, array);
1291 
1292 #ifdef IM_DEBUG
1293     fprintf(stderr,
1294             "firstRectForCharacterRange returning x=%f, y=%f, width=%f, height=%f\n",
1295             rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
1296 #endif // IM_DEBUG
1297     return rect;
1298 }
1299 
1300 /* This method returns the index for character that is nearest to thePoint.  thPoint is in
1301  screen coordinate system.
1302  */
1303 - (NSUInteger)characterIndexForPoint:(NSPoint)thePoint
1304 {
1305     if (!fInputMethodLOCKABLE) {
1306         return NSNotFound;
1307     }
1308 
1309     static JNF_MEMBER_CACHE(jm_characterIndexForPoint, jc_CInputMethod,
1310                             "characterIndexForPoint", "(II)I");
1311     JNIEnv *env = [ThreadUtilities getJNIEnv];



1312 
1313     NSPoint flippedLocation = ConvertNSScreenPoint(env, thePoint);
1314 
1315 #ifdef IM_DEBUG
1316     fprintf(stderr, "AWTView InputMethod Selector Called : [characterIndexForPoint:(NSPoint)thePoint] x=%f, y=%f\n", flippedLocation.x, flippedLocation.y);
1317 #endif // IM_DEBUG
1318 
1319     jint index = JNFCallIntMethod(env, fInputMethodLOCKABLE, jm_characterIndexForPoint, (jint)flippedLocation.x, (jint)flippedLocation.y); // AWT_THREADING Safe (AWTRunLoopMode)


1320 
1321 #ifdef IM_DEBUG
1322     fprintf(stderr, "characterIndexForPoint returning %d\n", index);
1323 #endif // IM_DEBUG
1324 
1325     if (index == -1) {
1326         return NSNotFound;
1327     } else {
1328         return (NSUInteger)index;
1329     }
1330 }
1331 
1332 - (NSArray*) validAttributesForMarkedText
1333 {
1334 #ifdef IM_DEBUG
1335     fprintf(stderr, "AWTView InputMethod Selector Called : [validAttributesForMarkedText]\n");
1336 #endif // IM_DEBUG
1337 
1338     return [NSArray array];
1339 }
1340 
1341 - (void)setInputMethod:(jobject)inputMethod
1342 {
1343 #ifdef IM_DEBUG
1344     fprintf(stderr, "AWTView InputMethod Selector Called : [setInputMethod]\n");
1345 #endif // IM_DEBUG
1346 
1347     JNIEnv *env = [ThreadUtilities getJNIEnv];
1348 
1349     // Get rid of the old one
1350     if (fInputMethodLOCKABLE) {
1351         JNFDeleteGlobalRef(env, fInputMethodLOCKABLE);
1352     }
1353 
1354     // Save a global ref to the new input method.
1355     if (inputMethod != NULL)
1356         fInputMethodLOCKABLE = JNFNewGlobalRef(env, inputMethod);
1357     else
1358         fInputMethodLOCKABLE = NULL;
1359 
1360     NSTextInputContext *curContxt = [NSTextInputContext currentInputContext];
1361     kbdLayout = curContxt.selectedKeyboardInputSource;
1362     [[NSNotificationCenter defaultCenter] addObserver:[AWTView class]
1363                                            selector:@selector(keyboardInputSourceChanged:)
1364                                                name:NSTextInputContextKeyboardSelectionDidChangeNotification
1365                                              object:nil];
1366 }
1367 
1368 - (void)abandonInput
1369 {
1370 #ifdef IM_DEBUG
1371     fprintf(stderr, "AWTView InputMethod Selector Called : [abandonInput]\n");
1372 #endif // IM_DEBUG
1373 
1374     [ThreadUtilities performOnMainThread:@selector(markedTextAbandoned:) on:[NSInputManager currentInputManager] withObject:self waitUntilDone:YES];
1375     [self unmarkText];
1376 }
1377 
1378 /********************************   END NSTextInputClient Protocol   ********************************/
1379 
1380 
1381 
1382 
1383 @end // AWTView
1384 
1385 /*
1386  * Class:     sun_lwawt_macosx_CPlatformView
1387  * Method:    nativeCreateView
1388  * Signature: (IIII)J
1389  */
1390 JNIEXPORT jlong JNICALL
1391 Java_sun_lwawt_macosx_CPlatformView_nativeCreateView
1392 (JNIEnv *env, jobject obj, jint originX, jint originY, jint width, jint height, jlong windowLayerPtr)
1393 {
1394     __block AWTView *newView = nil;
1395 
1396     JNF_COCOA_ENTER(env);
1397 
1398     NSRect rect = NSMakeRect(originX, originY, width, height);
1399     jobject cPlatformView = (*env)->NewWeakGlobalRef(env, obj);

1400 
1401     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
1402 
1403         CALayer *windowLayer = jlong_to_ptr(windowLayerPtr);
1404         newView = [[AWTView alloc] initWithRect:rect
1405                                    platformView:cPlatformView
1406                                     windowLayer:windowLayer];
1407     }];
1408 
1409     JNF_COCOA_EXIT(env);
1410 
1411     return ptr_to_jlong(newView);
1412 }
1413 
1414 /*
1415  * Class:     sun_lwawt_macosx_CPlatformView
1416  * Method:    nativeSetAutoResizable
1417  * Signature: (JZ)V;
1418  */
1419 
1420 JNIEXPORT void JNICALL
1421 Java_sun_lwawt_macosx_CPlatformView_nativeSetAutoResizable
1422 (JNIEnv *env, jclass cls, jlong viewPtr, jboolean toResize)
1423 {
1424     JNF_COCOA_ENTER(env);
1425 
1426     NSView *view = (NSView *)jlong_to_ptr(viewPtr);
1427 
1428     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1429 
1430         if (toResize) {
1431             [view setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
1432         } else {
1433             [view setAutoresizingMask: NSViewMinYMargin | NSViewMaxXMargin];
1434         }
1435 
1436         if ([view superview] != nil) {
1437             [[view superview] setAutoresizesSubviews:(BOOL)toResize];
1438         }
1439 
1440     }];
1441     JNF_COCOA_EXIT(env);
1442 }
1443 
1444 /*
1445  * Class:     sun_lwawt_macosx_CPlatformView
1446  * Method:    nativeGetNSViewDisplayID
1447  * Signature: (J)I;
1448  */
1449 
1450 JNIEXPORT jint JNICALL
1451 Java_sun_lwawt_macosx_CPlatformView_nativeGetNSViewDisplayID
1452 (JNIEnv *env, jclass cls, jlong viewPtr)
1453 {
1454     __block jint ret; //CGDirectDisplayID
1455 
1456     JNF_COCOA_ENTER(env);
1457 
1458     NSView *view = (NSView *)jlong_to_ptr(viewPtr);
1459     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
1460         NSWindow *window = [view window];
1461         ret = (jint)[[AWTWindow getNSWindowDisplayID_AppKitThread: window] intValue];
1462     }];
1463 
1464     JNF_COCOA_EXIT(env);
1465 
1466     return ret;
1467 }
1468 
1469 /*
1470  * Class:     sun_lwawt_macosx_CPlatformView
1471  * Method:    nativeGetLocationOnScreen
1472  * Signature: (J)Ljava/awt/Rectangle;
1473  */
1474 
1475 JNIEXPORT jobject JNICALL
1476 Java_sun_lwawt_macosx_CPlatformView_nativeGetLocationOnScreen
1477 (JNIEnv *env, jclass cls, jlong viewPtr)
1478 {
1479     jobject jRect = NULL;
1480 
1481     JNF_COCOA_ENTER(env);
1482 
1483     __block NSRect rect = NSZeroRect;
1484 
1485     NSView *view = (NSView *)jlong_to_ptr(viewPtr);
1486     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
1487 
1488         NSRect viewBounds = [view bounds];
1489         NSRect frameInWindow = [view convertRect:viewBounds toView:nil];
1490         rect = [[view window] convertRectToScreen:frameInWindow];
1491         //Convert coordinates to top-left corner origin
1492         rect = ConvertNSScreenRect(NULL, rect);
1493 
1494     }];
1495     jRect = NSToJavaRect(env, rect);
1496 
1497     JNF_COCOA_EXIT(env);
1498 
1499     return jRect;
1500 }
1501 
1502 /*
1503  * Class:     sun_lwawt_macosx_CPlatformView
1504  * Method:    nativeIsViewUnderMouse
1505  * Signature: (J)Z;
1506  */
1507 
1508 JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_CPlatformView_nativeIsViewUnderMouse
1509 (JNIEnv *env, jclass clazz, jlong viewPtr)
1510 {
1511     __block jboolean underMouse = JNI_FALSE;
1512 
1513     JNF_COCOA_ENTER(env);
1514 
1515     NSView *nsView = OBJC(viewPtr);
1516     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
1517         NSPoint ptWindowCoords = [[nsView window] mouseLocationOutsideOfEventStream];
1518         NSPoint ptViewCoords = [nsView convertPoint:ptWindowCoords fromView:nil];
1519         underMouse = [nsView hitTest:ptViewCoords] != nil;
1520     }];
1521 
1522     JNF_COCOA_EXIT(env);
1523 
1524     return underMouse;
1525 }


  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 "jni_util.h"
  27 #import "CGLGraphicsConfig.h"
  28 #import "AWTView.h"
  29 #import "AWTWindow.h"
  30 #import "JavaComponentAccessibility.h"
  31 #import "JavaTextAccessibility.h"
  32 #import "JavaAccessibilityUtilities.h"
  33 #import "GeomUtilities.h"
  34 #import "OSVersion.h"
  35 #import "ThreadUtilities.h"
  36 #import "JNIUtilities.h"
  37 
  38 #import <Carbon/Carbon.h>

  39 
  40 // keyboard layout
  41 static NSString *kbdLayout;
  42 
  43 @interface AWTView()
  44 @property (retain) CDropTarget *_dropTarget;
  45 @property (retain) CDragSource *_dragSource;
  46 
  47 -(void) deliverResize: (NSRect) rect;
  48 -(void) resetTrackingArea;
  49 -(void) deliverJavaKeyEventHelper: (NSEvent*) event;
  50 -(BOOL) isCodePointInUnicodeBlockNeedingIMEvent: (unichar) codePoint;
  51 -(NSMutableString *) parseString : (id) complexString;
  52 @end
  53 
  54 // Uncomment this line to see fprintfs of each InputMethod API being called on this View
  55 //#define IM_DEBUG TRUE
  56 //#define EXTRA_DEBUG
  57 
  58 static BOOL shouldUsePressAndHold() {


 124         }
 125 #endif /* REMOTELAYER */
 126     }
 127 
 128     return self;
 129 }
 130 
 131 - (void) dealloc {
 132     AWT_ASSERT_APPKIT_THREAD;
 133 
 134     self.cglLayer = nil;
 135 
 136     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 137     (*env)->DeleteWeakGlobalRef(env, m_cPlatformView);
 138     m_cPlatformView = NULL;
 139 
 140     if (fInputMethodLOCKABLE != NULL)
 141     {
 142         JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 143 
 144         (*env)->DeleteGlobalRef(env, fInputMethodLOCKABLE);
 145         fInputMethodLOCKABLE = NULL;
 146     }
 147 
 148     if (rolloverTrackingArea != nil) {
 149         [self removeTrackingArea:rolloverTrackingArea];
 150         [rolloverTrackingArea release];
 151         rolloverTrackingArea = nil;
 152     }
 153 
 154     [super dealloc];
 155 }
 156 
 157 - (void) viewDidMoveToWindow {
 158     AWT_ASSERT_APPKIT_THREAD;
 159 
 160     [AWTToolkit eventCountPlusPlus];
 161 
 162     [ThreadUtilities performOnMainThreadWaiting:NO block:^() {
 163         [[self window] makeFirstResponder: self];
 164     }];
 165     if ([self window] != NULL) {
 166         [self resetTrackingArea];
 167     }
 168 }
 169 
 170 - (BOOL) acceptsFirstMouse: (NSEvent *)event {
 171     return YES;
 172 }
 173 
 174 - (BOOL) acceptsFirstResponder {
 175     return YES;
 176 }
 177 
 178 - (BOOL) becomeFirstResponder {
 179     return YES;
 180 }
 181 
 182 - (BOOL) preservesContentDuringLiveResize {


 401     NSRect screenRect = [[[NSScreen screens] objectAtIndex:0] frame];
 402     absP.y = screenRect.size.height - absP.y;
 403     jint clickCount;
 404 
 405     if (type == NSMouseEntered ||
 406         type == NSMouseExited ||
 407         type == NSScrollWheel ||
 408         type == NSMouseMoved) {
 409         clickCount = 0;
 410     } else {
 411         clickCount = [event clickCount];
 412     }
 413 
 414     jdouble deltaX = [event deltaX];
 415     jdouble deltaY = [event deltaY];
 416     if ([AWTToolkit hasPreciseScrollingDeltas: event]) {
 417         deltaX = [event scrollingDeltaX] * 0.1;
 418         deltaY = [event scrollingDeltaY] * 0.1;
 419     }
 420 
 421     DECLARE_CLASS(jc_NSEvent, "sun/lwawt/macosx/NSEvent");
 422     DECLARE_METHOD(jctor_NSEvent, jc_NSEvent, "<init>", "(IIIIIIIIDDI)V");
 423     jobject jEvent = (*env)->NewObject(env, jc_NSEvent, jctor_NSEvent,
 424                                   [event type],
 425                                   [event modifierFlags],
 426                                   clickCount,
 427                                   [event buttonNumber],
 428                                   (jint)localPoint.x, (jint)localPoint.y,
 429                                   (jint)absP.x, (jint)absP.y,
 430                                   deltaY,
 431                                   deltaX,
 432                                   [AWTToolkit scrollStateWithEvent: event]);
 433     CHECK_NULL(jEvent);
 434 
 435     DECLARE_CLASS(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");
 436     DECLARE_METHOD(jm_deliverMouseEvent, jc_PlatformView, "deliverMouseEvent", "(Lsun/lwawt/macosx/NSEvent;)V");
 437     jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
 438     if (!(*env)->IsSameObject(env, jlocal, NULL)) {
 439         (*env)->CallVoidMethod(env, jlocal, jm_deliverMouseEvent, jEvent);
 440         CHECK_EXCEPTION();
 441         (*env)->DeleteLocalRef(env, jlocal);
 442     }
 443     (*env)->DeleteLocalRef(env, jEvent);
 444 }
 445 
 446 - (void) resetTrackingArea {
 447     if (rolloverTrackingArea != nil) {
 448         [self removeTrackingArea:rolloverTrackingArea];
 449         [rolloverTrackingArea release];
 450     }
 451 
 452     int options = (NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited |
 453                    NSTrackingMouseMoved | NSTrackingEnabledDuringMouseDrag);
 454 
 455     rolloverTrackingArea = [[NSTrackingArea alloc] initWithRect:[self visibleRect]
 456                                                         options: options
 457                                                           owner:self
 458                                                        userInfo:nil
 459                             ];
 460     [self addTrackingArea:rolloverTrackingArea];


 468 - (void) resetCursorRects {
 469     [super resetCursorRects];
 470     [self resetTrackingArea];
 471 }
 472 
 473 -(void) deliverJavaKeyEventHelper: (NSEvent *) event {
 474     static NSEvent* sLastKeyEvent = nil;
 475     if (event == sLastKeyEvent) {
 476         // The event is repeatedly delivered by keyDown: after performKeyEquivalent:
 477         return;
 478     }
 479     [sLastKeyEvent release];
 480     sLastKeyEvent = [event retain];
 481 
 482     [AWTToolkit eventCountPlusPlus];
 483     JNIEnv *env = [ThreadUtilities getJNIEnv];
 484 
 485     jstring characters = NULL;
 486     jstring charactersIgnoringModifiers = NULL;
 487     if ([event type] != NSFlagsChanged) {
 488         characters = NSStringToJavaString(env, [event characters]);
 489         charactersIgnoringModifiers = NSStringToJavaString(env, [event charactersIgnoringModifiers]);
 490     }
 491 
 492     DECLARE_CLASS(jc_NSEvent, "sun/lwawt/macosx/NSEvent");
 493     DECLARE_METHOD(jctor_NSEvent, jc_NSEvent, "<init>", "(IISLjava/lang/String;Ljava/lang/String;)V");
 494     jobject jEvent = (*env)->NewObject(env, jc_NSEvent, jctor_NSEvent,
 495                                   [event type],
 496                                   [event modifierFlags],
 497                                   [event keyCode],
 498                                   characters,
 499                                   charactersIgnoringModifiers);
 500     CHECK_NULL(jEvent);
 501 
 502     DECLARE_CLASS(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");
 503     DECLARE_METHOD(jm_deliverKeyEvent, jc_PlatformView,
 504                             "deliverKeyEvent", "(Lsun/lwawt/macosx/NSEvent;)V");
 505     jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
 506     if (!(*env)->IsSameObject(env, jlocal, NULL)) {
 507         (*env)->CallVoidMethod(env, jlocal, jm_deliverKeyEvent, jEvent);
 508         CHECK_EXCEPTION();
 509         (*env)->DeleteLocalRef(env, jlocal);
 510     }
 511     if (characters != NULL) {
 512         (*env)->DeleteLocalRef(env, characters);
 513     }
 514     (*env)->DeleteLocalRef(env, jEvent);
 515 }
 516 
 517 -(void) deliverResize: (NSRect) rect {
 518     jint x = (jint) rect.origin.x;
 519     jint y = (jint) rect.origin.y;
 520     jint w = (jint) rect.size.width;
 521     jint h = (jint) rect.size.height;
 522     JNIEnv *env = [ThreadUtilities getJNIEnv];
 523     DECLARE_CLASS(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");
 524     DECLARE_METHOD(jm_deliverResize, jc_PlatformView, "deliverResize", "(IIII)V");
 525 
 526     jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
 527     if (!(*env)->IsSameObject(env, jlocal, NULL)) {
 528         (*env)->CallVoidMethod(env, jlocal, jm_deliverResize, x,y,w,h);
 529         CHECK_EXCEPTION();
 530         (*env)->DeleteLocalRef(env, jlocal);
 531     }
 532 }
 533 
 534 
 535 - (void) drawRect:(NSRect)dirtyRect {
 536     AWT_ASSERT_APPKIT_THREAD;
 537 
 538     [super drawRect:dirtyRect];
 539     JNIEnv *env = [ThreadUtilities getJNIEnv];
 540     if (env != NULL) {
 541         /*
 542          if ([self inLiveResize]) {
 543          NSRect rs[4];
 544          NSInteger count;
 545          [self getRectsExposedDuringLiveResize:rs count:&count];
 546          for (int i = 0; i < count; i++) {
 547          JNU_CallMethodByName(env, NULL, [m_awtWindow cPlatformView],
 548          "deliverWindowDidExposeEvent", "(FFFF)V",
 549          (jfloat)rs[i].origin.x, (jfloat)rs[i].origin.y,
 550          (jfloat)rs[i].size.width, (jfloat)rs[i].size.height);
 551          if ((*env)->ExceptionOccurred(env)) {
 552          (*env)->ExceptionDescribe(env);
 553          (*env)->ExceptionClear(env);
 554          }
 555          }
 556          } else {
 557          */
 558         DECLARE_CLASS(jc_CPlatformView, "sun/lwawt/macosx/CPlatformView");
 559         DECLARE_METHOD(jm_deliverWindowDidExposeEvent, jc_CPlatformView, "deliverWindowDidExposeEvent", "()V");
 560         jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
 561         if (!(*env)->IsSameObject(env, jlocal, NULL)) {
 562             (*env)->CallVoidMethod(env, jlocal, jm_deliverWindowDidExposeEvent);
 563             CHECK_EXCEPTION();
 564             (*env)->DeleteLocalRef(env, jlocal);
 565         }
 566         /*
 567          }
 568          */
 569     }
 570 }
 571 
 572 -(BOOL) isCodePointInUnicodeBlockNeedingIMEvent: (unichar) codePoint {
 573     if ((codePoint == 0x0024) || (codePoint == 0x00A3) ||
 574         (codePoint == 0x00A5) ||
 575         ((codePoint >= 0x20A3) && (codePoint <= 0x20BF)) ||
 576         ((codePoint >= 0x3000) && (codePoint <= 0x303F)) ||
 577         ((codePoint >= 0xFF00) && (codePoint <= 0xFFEF))) {
 578         // Code point is in 'CJK Symbols and Punctuation' or
 579         // 'Halfwidth and Fullwidth Forms' Unicode block or
 580         // currency symbols unicode
 581         return YES;
 582     }
 583     return NO;
 584 }
 585 
 586 -(NSMutableString *) parseString : (id) complexString {
 587     if ([complexString isKindOfClass:[NSString class]]) {
 588         return [complexString mutableCopy];
 589     }
 590     else {
 591         return [complexString mutableString];
 592     }
 593 }
 594 
 595 // NSAccessibility support
 596 - (jobject)awtComponent:(JNIEnv*)env
 597 {
 598     DECLARE_CLASS_RETURN(jc_CPlatformView, "sun/lwawt/macosx/CPlatformView", NULL);
 599     DECLARE_FIELD_RETURN(jf_Peer, jc_CPlatformView, "peer", "Lsun/lwawt/LWWindowPeer;", NULL);
 600     if ((env == NULL) || (m_cPlatformView == NULL)) {
 601         NSLog(@"Apple AWT : Error AWTView:awtComponent given bad parameters.");
 602         NSLog(@"%@",[NSThread callStackSymbols]);



 603         return NULL;
 604     }
 605 
 606     jobject peer = NULL;
 607     jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
 608     if (!(*env)->IsSameObject(env, jlocal, NULL)) {
 609         peer = (*env)->GetObjectField(env, jlocal, jf_Peer);
 610         (*env)->DeleteLocalRef(env, jlocal);
 611     }
 612     DECLARE_CLASS_RETURN(jc_LWWindowPeer, "sun/lwawt/LWWindowPeer", NULL);
 613     DECLARE_FIELD_RETURN(jf_Target, jc_LWWindowPeer, "target", "Ljava/awt/Component;", NULL);
 614     if (peer == NULL) {
 615         NSLog(@"Apple AWT : Error AWTView:awtComponent got null peer from CPlatformView");
 616         NSLog(@"%@",[NSThread callStackSymbols]);
 617         return NULL;
 618     }
 619     jobject comp = (*env)->GetObjectField(env, peer, jf_Target);
 620     (*env)->DeleteLocalRef(env, peer);
 621     return comp;
 622 }
 623 
 624 + (AWTView *) awtView:(JNIEnv*)env ofAccessible:(jobject)jaccessible
 625 {
 626     DECLARE_CLASS_RETURN(sjc_CAccessibility, "sun/lwawt/macosx/CAccessibility", NULL);
 627     DECLARE_STATIC_METHOD_RETURN(jm_getAWTView, sjc_CAccessibility, "getAWTView", "(Ljavax/accessibility/Accessible;)J", NULL);
 628 
 629     jlong jptr = (*env)->CallStaticLongMethod(env, sjc_CAccessibility, jm_getAWTView, jaccessible);
 630     CHECK_EXCEPTION();
 631     if (jptr == 0) return nil;
 632 
 633     return (AWTView *)jlong_to_ptr(jptr);
 634 }
 635 
 636 - (id)getAxData:(JNIEnv*)env
 637 {
 638     jobject jcomponent = [self awtComponent:env];
 639     id ax = [[[JavaComponentAccessibility alloc] initWithParent:self withEnv:env withAccessible:jcomponent withIndex:-1 withView:self withJavaRole:nil] autorelease];
 640     (*env)->DeleteLocalRef(env, jcomponent);
 641     return ax;
 642 }
 643 
 644 - (NSArray *)accessibilityAttributeNames
 645 {
 646     return [[super accessibilityAttributeNames] arrayByAddingObject:NSAccessibilityChildrenAttribute];
 647 }
 648 
 649 // NSAccessibility messages
 650 // attribute methods


 936     if (dropTarget != nil) {
 937         [dropTarget concludeDragOperation:sender];
 938     }
 939 }
 940 
 941 - (void)draggingEnded:(id <NSDraggingInfo>)sender
 942 {
 943     // If draggingDestination is nil route the message to the superclass:
 944     CDropTarget *dropTarget = self._dropTarget;
 945 
 946     if (dropTarget != nil) {
 947         [dropTarget draggingEnded:sender];
 948     }
 949 }
 950 
 951 /********************************  END NSDraggingDestination Interface  ********************************/
 952 
 953 /********************************  BEGIN NSTextInputClient Protocol  ********************************/
 954 
 955 
 956 static jclass jc_CInputMethod = NULL;
 957 
 958 #define GET_CIM_CLASS() \
 959     GET_CLASS(jc_CInputMethod, "sun/lwawt/macosx/CInputMethod");
 960 
 961 #define GET_CIM_CLASS_RETURN(ret) \
 962     GET_CLASS_RETURN(jc_CInputMethod, "sun/lwawt/macosx/CInputMethod", ret);
 963 
 964 - (void) insertText:(id)aString replacementRange:(NSRange)replacementRange
 965 {
 966 #ifdef IM_DEBUG
 967     fprintf(stderr, "AWTView InputMethod Selector Called : [insertText]: %s\n", [aString UTF8String]);
 968 #endif // IM_DEBUG
 969 
 970     if (fInputMethodLOCKABLE == NULL) {
 971         return;
 972     }
 973 
 974     // Insert happens at the end of PAH
 975     fInPressAndHold = NO;
 976 
 977     // insertText gets called when the user commits text generated from an input method.  It also gets
 978     // called during ordinary input as well.  We only need to send an input method event when we have marked
 979     // text, or 'text in progress'.  We also need to send the event if we get an insert text out of the blue!
 980     // (i.e., when the user uses the Character palette or Inkwell), or when the string to insert is a complex
 981     // Unicode value.
 982 
 983     NSMutableString * useString = [self parseString:aString];
 984     NSUInteger utf16Length = [useString lengthOfBytesUsingEncoding:NSUTF16StringEncoding];
 985     NSUInteger utf8Length = [useString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
 986     BOOL aStringIsComplex = NO;
 987 
 988     unichar codePoint = [useString characterAtIndex:0];
 989 
 990 #ifdef IM_DEBUG
 991     NSLog(@"insertText kbdlayout %@ ",(NSString *)kbdLayout);
 992 #endif // IM_DEBUG
 993 
 994     if ((utf16Length > 2) ||
 995         ((utf8Length > 1) && [self isCodePointInUnicodeBlockNeedingIMEvent:codePoint]) ||
 996         ((codePoint == 0x5c) && ([(NSString *)kbdLayout containsString:@"Kotoeri"]))) {
 997         aStringIsComplex = YES;
 998     }
 999 
1000     if ([self hasMarkedText] || !fProcessingKeystroke || aStringIsComplex) {
1001         JNIEnv *env = [ThreadUtilities getJNIEnv];
1002 
1003         GET_CIM_CLASS();
1004         DECLARE_METHOD(jm_selectPreviousGlyph, jc_CInputMethod, "selectPreviousGlyph", "()V");
1005         // We need to select the previous glyph so that it is overwritten.
1006         if (fPAHNeedsToSelect) {
1007             (*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_selectPreviousGlyph);
1008             CHECK_EXCEPTION();
1009             fPAHNeedsToSelect = NO;
1010         }
1011 
1012         DECLARE_METHOD(jm_insertText, jc_CInputMethod, "insertText", "(Ljava/lang/String;)V");
1013         jstring insertedText =  NSStringToJavaString(env, useString);
1014         (*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_insertText, insertedText);
1015         CHECK_EXCEPTION();
1016         (*env)->DeleteLocalRef(env, insertedText);
1017 
1018         // The input method event will create psuedo-key events for each character in the committed string.
1019         // We also don't want to send the character that triggered the insertText, usually a return. [3337563]
1020         fKeyEventsNeeded = NO;
1021     }
1022     else {
1023         // Need to set back the fKeyEventsNeeded flag so that the string following the
1024         // marked text is not ignored by keyDown
1025         if ([useString length] > 0) {
1026             fKeyEventsNeeded = YES;
1027         }
1028     }
1029     fPAHNeedsToSelect = NO;
1030 
1031     // Abandon input to reset IM and unblock input after entering accented
1032     // symbols
1033 
1034     [self abandonInput];
1035 }


1050     NSLog(@"%@", NSStringFromSelector(aSelector));
1051 #endif // IM_DEBUG
1052     if (@selector(insertNewline:) == aSelector || @selector(insertTab:) == aSelector || @selector(deleteBackward:) == aSelector)
1053     {
1054         fKeyEventsNeeded = YES;
1055     }
1056 }
1057 
1058 // setMarkedText: cannot take a nil first argument. aString can be NSString or NSAttributedString
1059 - (void) setMarkedText:(id)aString selectedRange:(NSRange)selectionRange replacementRange:(NSRange)replacementRange
1060 {
1061     if (!fInputMethodLOCKABLE)
1062         return;
1063 
1064     BOOL isAttributedString = [aString isKindOfClass:[NSAttributedString class]];
1065     NSAttributedString *attrString = (isAttributedString ? (NSAttributedString *)aString : nil);
1066     NSString *incomingString = (isAttributedString ? [aString string] : aString);
1067 #ifdef IM_DEBUG
1068     fprintf(stderr, "AWTView InputMethod Selector Called : [setMarkedText] \"%s\", loc=%lu, length=%lu\n", [incomingString UTF8String], (unsigned long)selectionRange.location, (unsigned long)selectionRange.length);
1069 #endif // IM_DEBUG



1070     JNIEnv *env = [ThreadUtilities getJNIEnv];
1071     GET_CIM_CLASS();
1072     DECLARE_METHOD(jm_startIMUpdate, jc_CInputMethod, "startIMUpdate", "(Ljava/lang/String;)V");
1073     DECLARE_METHOD(jm_addAttribute, jc_CInputMethod, "addAttribute", "(ZZII)V");
1074     DECLARE_METHOD(jm_dispatchText, jc_CInputMethod, "dispatchText", "(IIZ)V");
1075 
1076     // NSInputContext already did the analysis of the TSM event and created attributes indicating
1077     // the underlining and color that should be done to the string.  We need to look at the underline
1078     // style and color to determine what kind of Java hilighting needs to be done.
1079     jstring inProcessText = NSStringToJavaString(env, incomingString);
1080     (*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_startIMUpdate, inProcessText);
1081     CHECK_EXCEPTION();
1082     (*env)->DeleteLocalRef(env, inProcessText);
1083 
1084     if (isAttributedString) {
1085         NSUInteger length;
1086         NSRange effectiveRange;
1087         NSDictionary *attributes;
1088         length = [attrString length];
1089         effectiveRange = NSMakeRange(0, 0);
1090         while (NSMaxRange(effectiveRange) < length) {
1091             attributes = [attrString attributesAtIndex:NSMaxRange(effectiveRange)
1092                                         effectiveRange:&effectiveRange];
1093             if (attributes) {
1094                 BOOL isThickUnderline, isGray;
1095                 NSNumber *underlineSizeObj =
1096                 (NSNumber *)[attributes objectForKey:NSUnderlineStyleAttributeName];
1097                 NSInteger underlineSize = [underlineSizeObj integerValue];
1098                 isThickUnderline = (underlineSize > 1);
1099 
1100                 NSColor *underlineColorObj =
1101                 (NSColor *)[attributes objectForKey:NSUnderlineColorAttributeName];
1102                 isGray = !([underlineColorObj isEqual:[NSColor blackColor]]);
1103 
1104                 (*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_addAttribute, isThickUnderline,
1105                        isGray, effectiveRange.location, effectiveRange.length);
1106                 CHECK_EXCEPTION();
1107             }
1108         }
1109     }
1110 
1111     DECLARE_METHOD(jm_selectPreviousGlyph, jc_CInputMethod, "selectPreviousGlyph", "()V");
1112     // We need to select the previous glyph so that it is overwritten.
1113     if (fPAHNeedsToSelect) {
1114         (*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_selectPreviousGlyph);
1115          CHECK_EXCEPTION();
1116         fPAHNeedsToSelect = NO;
1117     }
1118 
1119     (*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_dispatchText,
1120             selectionRange.location, selectionRange.length, JNI_FALSE);
1121          CHECK_EXCEPTION();
1122     // If the marked text is being cleared (zero-length string) don't handle the key event.
1123     if ([incomingString length] == 0) {
1124         fKeyEventsNeeded = NO;
1125     }
1126 }
1127 
1128 - (void) unmarkText
1129 {
1130 #ifdef IM_DEBUG
1131     fprintf(stderr, "AWTView InputMethod Selector Called : [unmarkText]\n");
1132 #endif // IM_DEBUG
1133 
1134     if (!fInputMethodLOCKABLE) {
1135         return;
1136     }
1137 
1138     // unmarkText cancels any input in progress and commits it to the text field.

1139     JNIEnv *env = [ThreadUtilities getJNIEnv];
1140     GET_CIM_CLASS();
1141     DECLARE_METHOD(jm_unmarkText, jc_CInputMethod, "unmarkText", "()V");
1142     (*env)->CallVoidMethod(env, fInputMethodLOCKABLE, jm_unmarkText);
1143     CHECK_EXCEPTION();
1144 }
1145 
1146 - (BOOL) hasMarkedText
1147 {
1148 #ifdef IM_DEBUG
1149     fprintf(stderr, "AWTView InputMethod Selector Called : [hasMarkedText]\n");
1150 #endif // IM_DEBUG
1151 
1152     if (!fInputMethodLOCKABLE) {
1153         return NO;
1154     }
1155 


1156     JNIEnv *env = [ThreadUtilities getJNIEnv];
1157     GET_CIM_CLASS_RETURN(NO);
1158     DECLARE_FIELD_RETURN(jf_fCurrentText, jc_CInputMethod, "fCurrentText", "Ljava/text/AttributedString;", NO);
1159     DECLARE_FIELD_RETURN(jf_fCurrentTextLength, jc_CInputMethod, "fCurrentTextLength", "I", NO);
1160     jobject currentText = (*env)->GetObjectField(env, fInputMethodLOCKABLE, jf_fCurrentText);
1161     CHECK_EXCEPTION();
1162 
1163     jint currentTextLength = (*env)->GetIntField(env, fInputMethodLOCKABLE, jf_fCurrentTextLength);
1164     CHECK_EXCEPTION();
1165 
1166     BOOL hasMarkedText = (currentText != NULL && currentTextLength > 0);
1167 
1168     if (currentText != NULL) {
1169         (*env)->DeleteLocalRef(env, currentText);
1170     }
1171 
1172     return hasMarkedText;
1173 }
1174 
1175 - (NSInteger) conversationIdentifier
1176 {
1177 #ifdef IM_DEBUG
1178     fprintf(stderr, "AWTView InputMethod Selector Called : [conversationIdentifier]\n");
1179 #endif // IM_DEBUG
1180 
1181     return (NSInteger) self;
1182 }
1183 
1184 /* Returns attributed string at the range.  This allows input mangers to
1185  query any range in backing-store (Andy's request)
1186  */
1187 - (NSAttributedString *) attributedSubstringForProposedRange:(NSRange)theRange actualRange:(NSRangePointer)actualRange
1188 {
1189 #ifdef IM_DEBUG
1190     fprintf(stderr, "AWTView InputMethod Selector Called : [attributedSubstringFromRange] location=%lu, length=%lu\n", (unsigned long)theRange.location, (unsigned long)theRange.length);
1191 #endif // IM_DEBUG
1192 

1193     JNIEnv *env = [ThreadUtilities getJNIEnv];
1194     DECLARE_METHOD_RETURN(jm_substringFromRange, jc_CInputMethod, "attributedSubstringFromRange", "(II)Ljava/lang/String;", nil);
1195     jobject theString = (*env)->CallObjectMethod(env, fInputMethodLOCKABLE, jm_substringFromRange, theRange.location, theRange.length);
1196     CHECK_EXCEPTION_NULL_RETURN(theString, nil);
1197 
1198     id result = [[[NSAttributedString alloc] initWithString:JavaStringToNSString(env, theString)] autorelease];
1199 #ifdef IM_DEBUG
1200     NSLog(@"attributedSubstringFromRange returning \"%@\"", result);
1201 #endif // IM_DEBUG
1202 
1203     (*env)->DeleteLocalRef(env, theString);
1204     return result;
1205 }
1206 
1207 /* This method returns the range for marked region.  If hasMarkedText == false,
1208  it'll return NSNotFound location & 0 length range.
1209  */
1210 - (NSRange) markedRange
1211 {
1212 
1213 #ifdef IM_DEBUG
1214     fprintf(stderr, "AWTView InputMethod Selector Called : [markedRange]\n");
1215 #endif // IM_DEBUG
1216 
1217     if (!fInputMethodLOCKABLE) {
1218         return NSMakeRange(NSNotFound, 0);
1219     }
1220 

1221     JNIEnv *env = [ThreadUtilities getJNIEnv];
1222     jarray array;
1223     jboolean isCopy;
1224     jint *_array;
1225     NSRange range = NSMakeRange(NSNotFound, 0);
1226     GET_CIM_CLASS_RETURN(range);
1227     DECLARE_METHOD_RETURN(jm_markedRange, jc_CInputMethod, "markedRange", "()[I", range);
1228 
1229     array = (*env)->CallObjectMethod(env, fInputMethodLOCKABLE, jm_markedRange);
1230     CHECK_EXCEPTION();
1231 
1232     if (array) {
1233         _array = (*env)->GetIntArrayElements(env, array, &isCopy);
1234         if (_array != NULL) {
1235             range.location = _array[0];
1236             range.length = _array[1];
1237 #ifdef IM_DEBUG
1238             fprintf(stderr, "markedRange returning (%lu, %lu)\n",
1239                     (unsigned long)range.location, (unsigned long)range.length);
1240 #endif // IM_DEBUG
1241             (*env)->ReleaseIntArrayElements(env, array, _array, 0);
1242         }
1243         (*env)->DeleteLocalRef(env, array);
1244     }
1245 
1246     return range;
1247 }
1248 
1249 /* This method returns the range for selected region.  Just like markedRange method,
1250  its location field contains char index from the text beginning.
1251  */
1252 - (NSRange) selectedRange
1253 {
1254     if (!fInputMethodLOCKABLE) {
1255         return NSMakeRange(NSNotFound, 0);
1256     }
1257 

1258     JNIEnv *env = [ThreadUtilities getJNIEnv];
1259     jarray array;
1260     jboolean isCopy;
1261     jint *_array;
1262     NSRange range = NSMakeRange(NSNotFound, 0);
1263     GET_CIM_CLASS_RETURN(range);
1264     DECLARE_METHOD_RETURN(jm_selectedRange, jc_CInputMethod, "selectedRange", "()[I", range);
1265 
1266 #ifdef IM_DEBUG
1267     fprintf(stderr, "AWTView InputMethod Selector Called : [selectedRange]\n");
1268 #endif // IM_DEBUG
1269 
1270     array = (*env)->CallObjectMethod(env, fInputMethodLOCKABLE, jm_selectedRange);
1271     CHECK_EXCEPTION();
1272     if (array) {
1273         _array = (*env)->GetIntArrayElements(env, array, &isCopy);
1274         if (_array != NULL) {
1275             range.location = _array[0];
1276             range.length = _array[1];
1277             (*env)->ReleaseIntArrayElements(env, array, _array, 0);
1278         }
1279         (*env)->DeleteLocalRef(env, array);
1280     }
1281 
1282     return range;
1283 }
1284 
1285 /* This method returns the first frame of rects for theRange in screen coordindate system.
1286  */
1287 - (NSRect) firstRectForCharacterRange:(NSRange)theRange actualRange:(NSRangePointer)actualRange
1288 {
1289     if (!fInputMethodLOCKABLE) {
1290         return NSZeroRect;
1291     }
1292 


1293     JNIEnv *env = [ThreadUtilities getJNIEnv];
1294     GET_CIM_CLASS_RETURN(NSZeroRect);
1295     DECLARE_METHOD_RETURN(jm_firstRectForCharacterRange, jc_CInputMethod,
1296                             "firstRectForCharacterRange", "(I)[I", NSZeroRect);
1297     jarray array;
1298     jboolean isCopy;
1299     jint *_array;
1300     NSRect rect;
1301 
1302 #ifdef IM_DEBUG
1303     fprintf(stderr,
1304             "AWTView InputMethod Selector Called : [firstRectForCharacterRange:] location=%lu, length=%lu\n",
1305             (unsigned long)theRange.location, (unsigned long)theRange.length);
1306 #endif // IM_DEBUG
1307 
1308     array = (*env)->CallObjectMethod(env, fInputMethodLOCKABLE, jm_firstRectForCharacterRange,
1309                                 theRange.location);
1310     CHECK_EXCEPTION();
1311 
1312     _array = (*env)->GetIntArrayElements(env, array, &isCopy);
1313     if (_array) {
1314         rect = ConvertNSScreenRect(env, NSMakeRect(_array[0], _array[1], _array[2], _array[3]));
1315         (*env)->ReleaseIntArrayElements(env, array, _array, 0);
1316     } else {
1317         rect = NSZeroRect;
1318     }
1319     (*env)->DeleteLocalRef(env, array);
1320 
1321 #ifdef IM_DEBUG
1322     fprintf(stderr,
1323             "firstRectForCharacterRange returning x=%f, y=%f, width=%f, height=%f\n",
1324             rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
1325 #endif // IM_DEBUG
1326     return rect;
1327 }
1328 
1329 /* This method returns the index for character that is nearest to thePoint.  thPoint is in
1330  screen coordinate system.
1331  */
1332 - (NSUInteger)characterIndexForPoint:(NSPoint)thePoint
1333 {
1334     if (!fInputMethodLOCKABLE) {
1335         return NSNotFound;
1336     }
1337 


1338     JNIEnv *env = [ThreadUtilities getJNIEnv];
1339     GET_CIM_CLASS_RETURN(NSNotFound);
1340     DECLARE_METHOD_RETURN(jm_characterIndexForPoint, jc_CInputMethod,
1341                             "characterIndexForPoint", "(II)I", NSNotFound);
1342 
1343     NSPoint flippedLocation = ConvertNSScreenPoint(env, thePoint);
1344 
1345 #ifdef IM_DEBUG
1346     fprintf(stderr, "AWTView InputMethod Selector Called : [characterIndexForPoint:(NSPoint)thePoint] x=%f, y=%f\n", flippedLocation.x, flippedLocation.y);
1347 #endif // IM_DEBUG
1348 
1349     jint index = (*env)->CallIntMethod(env, fInputMethodLOCKABLE, jm_characterIndexForPoint,
1350                       (jint)flippedLocation.x, (jint)flippedLocation.y);
1351     CHECK_EXCEPTION();
1352 
1353 #ifdef IM_DEBUG
1354     fprintf(stderr, "characterIndexForPoint returning %d\n", index);
1355 #endif // IM_DEBUG
1356 
1357     if (index == -1) {
1358         return NSNotFound;
1359     } else {
1360         return (NSUInteger)index;
1361     }
1362 }
1363 
1364 - (NSArray*) validAttributesForMarkedText
1365 {
1366 #ifdef IM_DEBUG
1367     fprintf(stderr, "AWTView InputMethod Selector Called : [validAttributesForMarkedText]\n");
1368 #endif // IM_DEBUG
1369 
1370     return [NSArray array];
1371 }
1372 
1373 - (void)setInputMethod:(jobject)inputMethod
1374 {
1375 #ifdef IM_DEBUG
1376     fprintf(stderr, "AWTView InputMethod Selector Called : [setInputMethod]\n");
1377 #endif // IM_DEBUG
1378 
1379     JNIEnv *env = [ThreadUtilities getJNIEnv];
1380 
1381     // Get rid of the old one
1382     if (fInputMethodLOCKABLE) {
1383         (*env)->DeleteGlobalRef(env, fInputMethodLOCKABLE);
1384     }
1385 
1386     fInputMethodLOCKABLE = inputMethod; // input method arg must be a GlobalRef




1387 
1388     NSTextInputContext *curContxt = [NSTextInputContext currentInputContext];
1389     kbdLayout = curContxt.selectedKeyboardInputSource;
1390     [[NSNotificationCenter defaultCenter] addObserver:[AWTView class]
1391                                            selector:@selector(keyboardInputSourceChanged:)
1392                                                name:NSTextInputContextKeyboardSelectionDidChangeNotification
1393                                              object:nil];
1394 }
1395 
1396 - (void)abandonInput
1397 {
1398 #ifdef IM_DEBUG
1399     fprintf(stderr, "AWTView InputMethod Selector Called : [abandonInput]\n");
1400 #endif // IM_DEBUG
1401 
1402     [ThreadUtilities performOnMainThread:@selector(markedTextAbandoned:) on:[NSInputManager currentInputManager] withObject:self waitUntilDone:YES];
1403     [self unmarkText];
1404 }
1405 
1406 /********************************   END NSTextInputClient Protocol   ********************************/
1407 
1408 
1409 
1410 
1411 @end // AWTView
1412 
1413 /*
1414  * Class:     sun_lwawt_macosx_CPlatformView
1415  * Method:    nativeCreateView
1416  * Signature: (IIII)J
1417  */
1418 JNIEXPORT jlong JNICALL
1419 Java_sun_lwawt_macosx_CPlatformView_nativeCreateView
1420 (JNIEnv *env, jobject obj, jint originX, jint originY, jint width, jint height, jlong windowLayerPtr)
1421 {
1422     __block AWTView *newView = nil;
1423 
1424     JNI_COCOA_ENTER(env);
1425 
1426     NSRect rect = NSMakeRect(originX, originY, width, height);
1427     jobject cPlatformView = (*env)->NewWeakGlobalRef(env, obj);
1428     CHECK_EXCEPTION();
1429 
1430     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
1431 
1432         CALayer *windowLayer = jlong_to_ptr(windowLayerPtr);
1433         newView = [[AWTView alloc] initWithRect:rect
1434                                    platformView:cPlatformView
1435                                     windowLayer:windowLayer];
1436     }];
1437 
1438     JNI_COCOA_EXIT(env);
1439 
1440     return ptr_to_jlong(newView);
1441 }
1442 
1443 /*
1444  * Class:     sun_lwawt_macosx_CPlatformView
1445  * Method:    nativeSetAutoResizable
1446  * Signature: (JZ)V;
1447  */
1448 
1449 JNIEXPORT void JNICALL
1450 Java_sun_lwawt_macosx_CPlatformView_nativeSetAutoResizable
1451 (JNIEnv *env, jclass cls, jlong viewPtr, jboolean toResize)
1452 {
1453     JNI_COCOA_ENTER(env);
1454 
1455     NSView *view = (NSView *)jlong_to_ptr(viewPtr);
1456 
1457     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1458 
1459         if (toResize) {
1460             [view setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
1461         } else {
1462             [view setAutoresizingMask: NSViewMinYMargin | NSViewMaxXMargin];
1463         }
1464 
1465         if ([view superview] != nil) {
1466             [[view superview] setAutoresizesSubviews:(BOOL)toResize];
1467         }
1468 
1469     }];
1470     JNI_COCOA_EXIT(env);
1471 }
1472 
1473 /*
1474  * Class:     sun_lwawt_macosx_CPlatformView
1475  * Method:    nativeGetNSViewDisplayID
1476  * Signature: (J)I;
1477  */
1478 
1479 JNIEXPORT jint JNICALL
1480 Java_sun_lwawt_macosx_CPlatformView_nativeGetNSViewDisplayID
1481 (JNIEnv *env, jclass cls, jlong viewPtr)
1482 {
1483     __block jint ret; //CGDirectDisplayID
1484 
1485     JNI_COCOA_ENTER(env);
1486 
1487     NSView *view = (NSView *)jlong_to_ptr(viewPtr);
1488     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
1489         NSWindow *window = [view window];
1490         ret = (jint)[[AWTWindow getNSWindowDisplayID_AppKitThread: window] intValue];
1491     }];
1492 
1493     JNI_COCOA_EXIT(env);
1494 
1495     return ret;
1496 }
1497 
1498 /*
1499  * Class:     sun_lwawt_macosx_CPlatformView
1500  * Method:    nativeGetLocationOnScreen
1501  * Signature: (J)Ljava/awt/Rectangle;
1502  */
1503 
1504 JNIEXPORT jobject JNICALL
1505 Java_sun_lwawt_macosx_CPlatformView_nativeGetLocationOnScreen
1506 (JNIEnv *env, jclass cls, jlong viewPtr)
1507 {
1508     jobject jRect = NULL;
1509 
1510     JNI_COCOA_ENTER(env);
1511 
1512     __block NSRect rect = NSZeroRect;
1513 
1514     NSView *view = (NSView *)jlong_to_ptr(viewPtr);
1515     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
1516 
1517         NSRect viewBounds = [view bounds];
1518         NSRect frameInWindow = [view convertRect:viewBounds toView:nil];
1519         rect = [[view window] convertRectToScreen:frameInWindow];
1520         //Convert coordinates to top-left corner origin
1521         rect = ConvertNSScreenRect(NULL, rect);
1522 
1523     }];
1524     jRect = NSToJavaRect(env, rect);
1525 
1526     JNI_COCOA_EXIT(env);
1527 
1528     return jRect;
1529 }
1530 
1531 /*
1532  * Class:     sun_lwawt_macosx_CPlatformView
1533  * Method:    nativeIsViewUnderMouse
1534  * Signature: (J)Z;
1535  */
1536 
1537 JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_CPlatformView_nativeIsViewUnderMouse
1538 (JNIEnv *env, jclass clazz, jlong viewPtr)
1539 {
1540     __block jboolean underMouse = JNI_FALSE;
1541 
1542     JNI_COCOA_ENTER(env);
1543 
1544     NSView *nsView = OBJC(viewPtr);
1545     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
1546         NSPoint ptWindowCoords = [[nsView window] mouseLocationOutsideOfEventStream];
1547         NSPoint ptViewCoords = [nsView convertPoint:ptWindowCoords fromView:nil];
1548         underMouse = [nsView hitTest:ptViewCoords] != nil;
1549     }];
1550 
1551     JNI_COCOA_EXIT(env);
1552 
1553     return underMouse;
1554 }
< prev index next >