textinput_darwin.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2023 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build !ios
  15. // TODO: Remove Cgo with PureGo (#1162).
  16. #import <Cocoa/Cocoa.h>
  17. void ebitengine_textinput_update(const char* text, int start, int end, BOOL committed);
  18. void ebitengine_textinput_end();
  19. @interface TextInputClient : NSView<NSTextInputClient>
  20. {
  21. NSString* markedText_;
  22. NSRange markedRange_;
  23. NSRange selectedRange_;
  24. }
  25. @end
  26. @implementation TextInputClient
  27. - (BOOL)hasMarkedText {
  28. return markedText_ != nil;
  29. }
  30. - (NSRange)markedRange {
  31. return markedRange_;
  32. }
  33. - (NSRange)selectedRange {
  34. return selectedRange_;
  35. }
  36. - (void)setMarkedText:(id)string
  37. selectedRange:(NSRange)selectedRange
  38. replacementRange:(NSRange)replacementRange {
  39. if ([string isKindOfClass:[NSAttributedString class]]) {
  40. string = [string string];
  41. }
  42. markedText_ = string;
  43. selectedRange_ = selectedRange;
  44. markedRange_ = NSMakeRange(0, [string length]);
  45. ebitengine_textinput_update([string UTF8String], selectedRange.location, selectedRange.location + selectedRange.length, NO);
  46. }
  47. - (void)unmarkText {
  48. markedText_ = nil;
  49. }
  50. - (NSArray<NSAttributedStringKey> *)validAttributesForMarkedText {
  51. return @[];
  52. }
  53. - (NSAttributedString *)attributedSubstringForProposedRange:(NSRange)range
  54. actualRange:(NSRangePointer)actualRange {
  55. return nil;
  56. }
  57. - (void)insertText:(id)string
  58. replacementRange:(NSRange)replacementRange {
  59. if ([string isKindOfClass:[NSAttributedString class]]) {
  60. string = [string string];
  61. }
  62. if ([string length] == 1 && [string characterAtIndex:0] < 0x20) {
  63. return;
  64. }
  65. ebitengine_textinput_update([string UTF8String], 0, [string length], YES);
  66. }
  67. - (NSUInteger)characterIndexForPoint:(NSPoint)point {
  68. return 0;
  69. }
  70. - (NSRect)firstRectForCharacterRange:(NSRange)range
  71. actualRange:(NSRangePointer)actualRange {
  72. NSWindow* window = [self window];
  73. return [window convertRectToScreen:[self frame]];
  74. }
  75. - (void)doCommandBySelector:(SEL)selector {
  76. // Do nothing.
  77. }
  78. - (BOOL)resignFirstResponder {
  79. ebitengine_textinput_end();
  80. return [super resignFirstResponder];
  81. }
  82. @end