# HG changeset patch # User Jiang Jiang # Date 1246012677 0 # Node ID fef1a835af4389678658fd8fddaeeea81e752d8f # Parent e42873b9c6f155b0fef6c24c3fce2aec05f18338 Basic text input API diff -r e42873b9c6f1 -r fef1a835af43 include/SDL_video.h --- a/include/SDL_video.h Sun May 31 16:57:29 2009 +0000 +++ b/include/SDL_video.h Fri Jun 26 10:37:57 2009 +0000 @@ -1467,6 +1467,12 @@ */ extern DECLSPEC void SDLCALL SDL_GL_DeleteContext(SDL_GLContext context); +/** + * \fn void SDL_StartTextInput(SDL_rect *rect) + * + * \brief Start Unicode text input within the given rectangle. + */ +extern DECLSPEC void SDLCALL SDL_StartTextInput(SDL_Rect *rect); /* Ends C function definitions when using C++ */ #ifdef __cplusplus diff -r e42873b9c6f1 -r fef1a835af43 src/video/SDL_sysvideo.h --- a/src/video/SDL_sysvideo.h Sun May 31 16:57:29 2009 +0000 +++ b/src/video/SDL_sysvideo.h Fri Jun 26 10:37:57 2009 +0000 @@ -277,6 +277,9 @@ /* Suspend the screensaver */ void (*SuspendScreenSaver) (_THIS); + /* Text input */ + void (*StartTextInput) (_THIS, SDL_Rect *rect); + /* * * */ /* Data common to all drivers */ SDL_bool suspend_screensaver; diff -r e42873b9c6f1 -r fef1a835af43 src/video/SDL_video.c --- a/src/video/SDL_video.c Sun May 31 16:57:29 2009 +0000 +++ b/src/video/SDL_video.c Fri Jun 26 10:37:57 2009 +0000 @@ -3067,4 +3067,12 @@ return (_this->GetWindowWMInfo(_this, window, info)); } +void +SDL_StartTextInput(SDL_Rect *rect) +{ + if (_this->StartTextInput) { + _this->StartTextInput(_this, rect); + } +} + /* vi: set ts=4 sw=4 expandtab: */ diff -r e42873b9c6f1 -r fef1a835af43 src/video/cocoa/SDL_cocoakeyboard.h --- a/src/video/cocoa/SDL_cocoakeyboard.h Sun May 31 16:57:29 2009 +0000 +++ b/src/video/cocoa/SDL_cocoakeyboard.h Fri Jun 26 10:37:57 2009 +0000 @@ -28,6 +28,8 @@ extern void Cocoa_HandleKeyEvent(_THIS, NSEvent * event); extern void Cocoa_QuitKeyboard(_THIS); +extern void Cocoa_StartTextInput(_THIS, SDL_Rect *rect); + #endif /* _SDL_cocoakeyboard_h */ /* vi: set ts=4 sw=4 expandtab: */ diff -r e42873b9c6f1 -r fef1a835af43 src/video/cocoa/SDL_cocoakeyboard.m --- a/src/video/cocoa/SDL_cocoakeyboard.m Sun May 31 16:57:29 2009 +0000 +++ b/src/video/cocoa/SDL_cocoakeyboard.m Fri Jun 26 10:37:57 2009 +0000 @@ -59,12 +59,19 @@ NSString *_markedText; NSRange _markedRange; NSRange _selectedRange; + SDL_Rect inputRect; } - (void) doCommandBySelector:(SEL)myselector; +- (void) setInputRect:(SDL_Rect *) rect; @end @implementation SDLTranslatorResponder +- (void) setInputRect:(SDL_Rect *) rect +{ + inputRect = *rect; +} + - (void) insertText:(id) aString { const char *str; @@ -135,11 +142,20 @@ - (NSRect) firstRectForCharacterRange: (NSRange) theRange { - return NSMakeRect(0, 0, 0, 0); + float windowHeight = [[self window] frame].size.height; + NSRect rect = NSMakeRect(inputRect.x, windowHeight - inputRect.y, inputRect.w, inputRect.h); + + NSLog(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@", + theRange.location, theRange.length, windowHeight, + NSStringFromRect(rect)); + rect.origin = [[self window] convertBaseToScreen: rect.origin]; + + return rect; } - (NSAttributedString *) attributedSubstringFromRange: (NSRange) theRange { + NSLog(@"attributedSubstringFromRange: (%d, %d)", theRange.location, theRange.length); return nil; } @@ -152,6 +168,7 @@ // nearest to thePoint. thPoint is in screen coordinate system. - (NSUInteger) characterIndexForPoint:(NSPoint) thePoint { + NSLog(@"characterIndexForPoint: (%g, %g)", thePoint.x, thePoint.y); return 0; } @@ -578,6 +595,15 @@ } void +Cocoa_StartTextInput(_THIS, SDL_Rect *rect) +{ + SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; + + NSLog(@"StartTextInput: (%d, %d) (w=%d, h=%d)", rect->x, rect->y, rect->w, rect->h); + [data->fieldEdit setInputRect: rect]; +} + +void Cocoa_HandleKeyEvent(_THIS, NSEvent *event) { SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; diff -r e42873b9c6f1 -r fef1a835af43 src/video/cocoa/SDL_cocoavideo.h --- a/src/video/cocoa/SDL_cocoavideo.h Sun May 31 16:57:29 2009 +0000 +++ b/src/video/cocoa/SDL_cocoavideo.h Fri Jun 26 10:37:57 2009 +0000 @@ -41,6 +41,8 @@ /* Private display data */ +@class SDLTranslatorResponder; + typedef struct SDL_VideoData { SInt32 osversion; @@ -48,7 +50,7 @@ int mouse; int keyboard; void *key_layout; - NSText *fieldEdit; + SDLTranslatorResponder *fieldEdit; Uint32 screensaver_activity; } SDL_VideoData; diff -r e42873b9c6f1 -r fef1a835af43 src/video/cocoa/SDL_cocoavideo.m --- a/src/video/cocoa/SDL_cocoavideo.m Sun May 31 16:57:29 2009 +0000 +++ b/src/video/cocoa/SDL_cocoavideo.m Fri Jun 26 10:37:57 2009 +0000 @@ -102,6 +102,8 @@ device->GL_DeleteContext = Cocoa_GL_DeleteContext; #endif + device->StartTextInput = Cocoa_StartTextInput; + device->free = Cocoa_DeleteDevice; return device;