Cocoa: Use NSTextInputClient instead of NSTextInput for text input handling. The latter was deprecated in OS X 10.6.
authorAlex Szpakowski <slime73@gmail.com>
Thu, 10 Dec 2015 22:17:22 -0400
changeset 9959182bfe0e7a60
parent 9958 687e118144c7
child 9960 a20484c998da
Cocoa: Use NSTextInputClient instead of NSTextInput for text input handling. The latter was deprecated in OS X 10.6.
src/video/cocoa/SDL_cocoakeyboard.m
     1.1 --- a/src/video/cocoa/SDL_cocoakeyboard.m	Thu Dec 10 20:25:34 2015 -0400
     1.2 +++ b/src/video/cocoa/SDL_cocoakeyboard.m	Thu Dec 10 22:17:22 2015 -0400
     1.3 @@ -33,25 +33,27 @@
     1.4  /*#define DEBUG_IME NSLog */
     1.5  #define DEBUG_IME(...)
     1.6  
     1.7 -@interface SDLTranslatorResponder : NSView <NSTextInput> {
     1.8 +@interface SDLTranslatorResponder : NSView <NSTextInputClient> {
     1.9      NSString *_markedText;
    1.10      NSRange   _markedRange;
    1.11      NSRange   _selectedRange;
    1.12      SDL_Rect  _inputRect;
    1.13  }
    1.14 -- (void) doCommandBySelector:(SEL)myselector;
    1.15 -- (void) setInputRect:(SDL_Rect *) rect;
    1.16 +- (void)doCommandBySelector:(SEL)myselector;
    1.17 +- (void)setInputRect:(SDL_Rect *)rect;
    1.18  @end
    1.19  
    1.20  @implementation SDLTranslatorResponder
    1.21  
    1.22 -- (void) setInputRect:(SDL_Rect *) rect
    1.23 +- (void)setInputRect:(SDL_Rect *)rect
    1.24  {
    1.25      _inputRect = *rect;
    1.26  }
    1.27  
    1.28 -- (void) insertText:(id) aString
    1.29 +- (void)insertText:(id)aString replacementRange:(NSRange)replacementRange
    1.30  {
    1.31 +    /* TODO: Make use of replacementRange? */
    1.32 +
    1.33      const char *str;
    1.34  
    1.35      DEBUG_IME(@"insertText: %@", aString);
    1.36 @@ -67,7 +69,15 @@
    1.37      SDL_SendKeyboardText(str);
    1.38  }
    1.39  
    1.40 -- (void) doCommandBySelector:(SEL) myselector
    1.41 +- (void)insertText:(id)insertString
    1.42 +{
    1.43 +    /* This method is part of NSTextInput and not NSTextInputClient, but
    1.44 +     * apparently it still might be called in OS X 10.5 and can cause beeps if
    1.45 +     * the implementation is missing: http://crbug.com/47890 */
    1.46 +    [self insertText:insertString replacementRange:NSMakeRange(0, 0)];
    1.47 +}
    1.48 +
    1.49 +- (void)doCommandBySelector:(SEL)myselector
    1.50  {
    1.51      /* No need to do anything since we are not using Cocoa
    1.52         selectors to handle special keys, instead we use SDL
    1.53 @@ -75,23 +85,22 @@
    1.54      */
    1.55  }
    1.56  
    1.57 -- (BOOL) hasMarkedText
    1.58 +- (BOOL)hasMarkedText
    1.59  {
    1.60      return _markedText != nil;
    1.61  }
    1.62  
    1.63 -- (NSRange) markedRange
    1.64 +- (NSRange)markedRange
    1.65  {
    1.66      return _markedRange;
    1.67  }
    1.68  
    1.69 -- (NSRange) selectedRange
    1.70 +- (NSRange)selectedRange
    1.71  {
    1.72      return _selectedRange;
    1.73  }
    1.74  
    1.75 -- (void) setMarkedText:(id) aString
    1.76 -         selectedRange:(NSRange) selRange
    1.77 +- (void)setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange;
    1.78  {
    1.79      if ([aString isKindOfClass: [NSAttributedString class]]) {
    1.80          aString = [aString string];
    1.81 @@ -107,17 +116,17 @@
    1.82          _markedText = [aString retain];
    1.83      }
    1.84  
    1.85 -    _selectedRange = selRange;
    1.86 +    _selectedRange = selectedRange;
    1.87      _markedRange = NSMakeRange(0, [aString length]);
    1.88  
    1.89      SDL_SendEditingText([aString UTF8String],
    1.90 -                        selRange.location, selRange.length);
    1.91 +                        selectedRange.location, selectedRange.length);
    1.92  
    1.93      DEBUG_IME(@"setMarkedText: %@, (%d, %d)", _markedText,
    1.94            selRange.location, selRange.length);
    1.95  }
    1.96  
    1.97 -- (void) unmarkText
    1.98 +- (void)unmarkText
    1.99  {
   1.100      [_markedText release];
   1.101      _markedText = nil;
   1.102 @@ -125,29 +134,38 @@
   1.103      SDL_SendEditingText("", 0, 0);
   1.104  }
   1.105  
   1.106 -- (NSRect) firstRectForCharacterRange: (NSRange) theRange
   1.107 +- (NSRect)firstRectForCharacterRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange;
   1.108  {
   1.109      NSWindow *window = [self window];
   1.110 -    NSRect contentRect = [window contentRectForFrameRect: [window frame]];
   1.111 +    NSRect contentRect = [window contentRectForFrameRect:[window frame]];
   1.112      float windowHeight = contentRect.size.height;
   1.113      NSRect rect = NSMakeRect(_inputRect.x, windowHeight - _inputRect.y - _inputRect.h,
   1.114                               _inputRect.w, _inputRect.h);
   1.115  
   1.116 +    if (actualRange) {
   1.117 +        *actualRange = aRange;
   1.118 +    }
   1.119 +
   1.120      DEBUG_IME(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@",
   1.121 -            theRange.location, theRange.length, windowHeight,
   1.122 +            aRange.location, aRange.length, windowHeight,
   1.123              NSStringFromRect(rect));
   1.124 -    rect.origin = [[self window] convertBaseToScreen: rect.origin];
   1.125 +
   1.126 +    if ([[self window] respondsToSelector:@selector(convertRectToScreen:)]) {
   1.127 +        rect = [[self window] convertRectToScreen:rect];
   1.128 +    } else {
   1.129 +        rect.origin = [[self window] convertBaseToScreen:rect.origin];
   1.130 +    }
   1.131  
   1.132      return rect;
   1.133  }
   1.134  
   1.135 -- (NSAttributedString *) attributedSubstringFromRange: (NSRange) theRange
   1.136 +- (NSAttributedString *)attributedSubstringForProposedRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange;
   1.137  {
   1.138 -    DEBUG_IME(@"attributedSubstringFromRange: (%d, %d)", theRange.location, theRange.length);
   1.139 +    DEBUG_IME(@"attributedSubstringFromRange: (%d, %d)", aRange.location, aRange.length);
   1.140      return nil;
   1.141  }
   1.142  
   1.143 -- (NSInteger) conversationIdentifier
   1.144 +- (NSInteger)conversationIdentifier
   1.145  {
   1.146      return (NSInteger) self;
   1.147  }
   1.148 @@ -155,7 +173,7 @@
   1.149  /* This method returns the index for character that is
   1.150   * nearest to thePoint.  thPoint is in screen coordinate system.
   1.151   */
   1.152 -- (NSUInteger) characterIndexForPoint:(NSPoint) thePoint
   1.153 +- (NSUInteger)characterIndexForPoint:(NSPoint)thePoint
   1.154  {
   1.155      DEBUG_IME(@"characterIndexForPoint: (%g, %g)", thePoint.x, thePoint.y);
   1.156      return 0;
   1.157 @@ -166,7 +184,7 @@
   1.158   * NSInputServer examines the return value of this
   1.159   * method & constructs appropriate attributed string.
   1.160   */
   1.161 -- (NSArray *) validAttributesForMarkedText
   1.162 +- (NSArray *)validAttributesForMarkedText
   1.163  {
   1.164      return [NSArray array];
   1.165  }
   1.166 @@ -504,7 +522,7 @@
   1.167              [[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];
   1.168      }
   1.169  
   1.170 -    if (![[data->fieldEdit superview] isEqual: parentView]) {
   1.171 +    if (![[data->fieldEdit superview] isEqual:parentView]) {
   1.172          /* DEBUG_IME(@"add fieldEdit to window contentView"); */
   1.173          [data->fieldEdit removeFromSuperview];
   1.174          [parentView addSubview: data->fieldEdit];
   1.175 @@ -535,7 +553,7 @@
   1.176          return;
   1.177      }
   1.178  
   1.179 -    [data->fieldEdit setInputRect: rect];
   1.180 +    [data->fieldEdit setInputRect:rect];
   1.181  }
   1.182  
   1.183  void