From 651236255e6542b61531f49c20bbe6622c32cbbf Mon Sep 17 00:00:00 2001 From: Jiang Jiang Date: Fri, 26 Jun 2009 10:37:57 +0000 Subject: [PATCH] Basic text input API --- include/SDL_video.h | 6 ++++++ src/video/SDL_sysvideo.h | 3 +++ src/video/SDL_video.c | 8 ++++++++ src/video/cocoa/SDL_cocoakeyboard.h | 2 ++ src/video/cocoa/SDL_cocoakeyboard.m | 28 +++++++++++++++++++++++++++- src/video/cocoa/SDL_cocoavideo.h | 4 +++- src/video/cocoa/SDL_cocoavideo.m | 2 ++ 7 files changed, 51 insertions(+), 2 deletions(-) diff --git a/include/SDL_video.h b/include/SDL_video.h index 4e83fa32f..9d95aeb74 100644 --- a/include/SDL_video.h +++ b/include/SDL_video.h @@ -1467,6 +1467,12 @@ extern DECLSPEC void SDLCALL SDL_GL_SwapWindow(SDL_WindowID windowID); */ 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 --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h index 1b54ed347..d4d8d5acf 100644 --- a/src/video/SDL_sysvideo.h +++ b/src/video/SDL_sysvideo.h @@ -277,6 +277,9 @@ struct SDL_VideoDevice /* Suspend the screensaver */ void (*SuspendScreenSaver) (_THIS); + /* Text input */ + void (*StartTextInput) (_THIS, SDL_Rect *rect); + /* * * */ /* Data common to all drivers */ SDL_bool suspend_screensaver; diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index e23ae2e59..864aeb9ff 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -3067,4 +3067,12 @@ SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info) 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 --git a/src/video/cocoa/SDL_cocoakeyboard.h b/src/video/cocoa/SDL_cocoakeyboard.h index 665db500c..dd5589c4d 100644 --- a/src/video/cocoa/SDL_cocoakeyboard.h +++ b/src/video/cocoa/SDL_cocoakeyboard.h @@ -28,6 +28,8 @@ extern void Cocoa_InitKeyboard(_THIS); 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 --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m index 50394070b..2c052e808 100644 --- a/src/video/cocoa/SDL_cocoakeyboard.m +++ b/src/video/cocoa/SDL_cocoakeyboard.m @@ -59,12 +59,19 @@ @interface SDLTranslatorResponder : NSTextView 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 @@ - (void) unmarkText - (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 @@ - (NSInteger) conversationIdentifier // nearest to thePoint. thPoint is in screen coordinate system. - (NSUInteger) characterIndexForPoint:(NSPoint) thePoint { + NSLog(@"characterIndexForPoint: (%g, %g)", thePoint.x, thePoint.y); return 0; } @@ -577,6 +594,15 @@ - (NSArray *) validAttributesForMarkedText SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Command"); } +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) { diff --git a/src/video/cocoa/SDL_cocoavideo.h b/src/video/cocoa/SDL_cocoavideo.h index 14d886562..13fa7d214 100644 --- a/src/video/cocoa/SDL_cocoavideo.h +++ b/src/video/cocoa/SDL_cocoavideo.h @@ -41,6 +41,8 @@ /* Private display data */ +@class SDLTranslatorResponder; + typedef struct SDL_VideoData { SInt32 osversion; @@ -48,7 +50,7 @@ typedef struct SDL_VideoData int mouse; int keyboard; void *key_layout; - NSText *fieldEdit; + SDLTranslatorResponder *fieldEdit; Uint32 screensaver_activity; } SDL_VideoData; diff --git a/src/video/cocoa/SDL_cocoavideo.m b/src/video/cocoa/SDL_cocoavideo.m index 331e6abad..1b436bfad 100644 --- a/src/video/cocoa/SDL_cocoavideo.m +++ b/src/video/cocoa/SDL_cocoavideo.m @@ -102,6 +102,8 @@ device->GL_DeleteContext = Cocoa_GL_DeleteContext; #endif + device->StartTextInput = Cocoa_StartTextInput; + device->free = Cocoa_DeleteDevice; return device;