changeset 3135:f4e553ec6a62 gsoc2009_IME

Update API, first step: make it build
author Jiang Jiang <gzjjgod@gmail.com>
date Thu, 06 Aug 2009 08:56:48 +0000
parents f896821736fb
children 962357f325e1
files include/SDL_video.h src/video/SDL_sysvideo.h src/video/SDL_video.c src/video/cocoa/SDL_cocoakeyboard.h src/video/cocoa/SDL_cocoakeyboard.m src/video/cocoa/SDL_cocoavideo.m
diffstat 6 files changed, 94 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/include/SDL_video.h	Thu Aug 06 08:53:00 2009 +0000
+++ b/include/SDL_video.h	Thu Aug 06 08:56:48 2009 +0000
@@ -1468,11 +1468,32 @@
 extern DECLSPEC void SDLCALL SDL_GL_DeleteContext(SDL_GLContext context);
 
 /**
- * \fn void SDL_StartTextInput(SDL_rect *rect)
+ * \fn void SDL_StartTextInput(SDL_WindowID windowID)
+ *
+ * \brief Start accepting Unicode text input events in given window.
+ *
+ * \sa SDL_StopTextInput()
+ * \sa SDL_SetTextInputRect()
+ */
+extern DECLSPEC void SDLCALL SDL_StartTextInput(SDL_WindowID windowID);
+
+/**
+ * \fn void SDL_StopTextInput(void)
  *
- * \brief Start Unicode text input within the given rectangle.
+ * \brief Stop receiving any text input events.
+ *
+ * \sa SDL_StartTextInput()
  */
-extern DECLSPEC void SDLCALL SDL_StartTextInput(SDL_Rect *rect);
+extern DECLSPEC void SDLCALL SDL_StopTextInput(void);
+
+/**
+ * \fn void SDL_SetTextInputRect(SDL_Rect *rect)
+ *
+ * \brief Set the rectangle used to type Unicode text inputs.
+ *
+ * \sa SDL_StartTextInput()
+ */
+extern DECLSPEC void SDLCALL SDL_SetTextInputRect(SDL_Rect *rect);
 
 /* Ends C function definitions when using C++ */
 #ifdef __cplusplus
--- a/src/video/SDL_sysvideo.h	Thu Aug 06 08:53:00 2009 +0000
+++ b/src/video/SDL_sysvideo.h	Thu Aug 06 08:56:48 2009 +0000
@@ -278,7 +278,9 @@
     void (*SuspendScreenSaver) (_THIS);
 
     /* Text input */
-    void (*StartTextInput) (_THIS, SDL_Rect *rect);
+    void (*StartTextInput) (_THIS, SDL_Window *window);
+    void (*StopTextInput) (_THIS);
+    void (*SetTextInputRect) (_THIS, SDL_Rect *rect);
 
     /* * * */
     /* Data common to all drivers */
--- a/src/video/SDL_video.c	Thu Aug 06 08:53:00 2009 +0000
+++ b/src/video/SDL_video.c	Thu Aug 06 08:56:48 2009 +0000
@@ -3068,10 +3068,28 @@
 }
 
 void
-SDL_StartTextInput(SDL_Rect *rect)
+SDL_StartTextInput(SDL_WindowID windowID)
 {
+    SDL_Window *window = SDL_GetWindowFromID(windowID);
+
     if (_this->StartTextInput) {
-        _this->StartTextInput(_this, rect);
+        _this->StartTextInput(_this, window);
+    }
+}
+
+void
+SDL_StopTextInput(void)
+{
+    if (_this->StopTextInput) {
+        _this->StopTextInput(_this);
+    }
+}
+
+void
+SDL_SetTextInputRect(SDL_Rect *rect)
+{
+    if (_this->SetTextInputRect) {
+        _this->SetTextInputRect(_this, rect);
     }
 }
 
--- a/src/video/cocoa/SDL_cocoakeyboard.h	Thu Aug 06 08:53:00 2009 +0000
+++ b/src/video/cocoa/SDL_cocoakeyboard.h	Thu Aug 06 08:56:48 2009 +0000
@@ -28,7 +28,9 @@
 extern void Cocoa_HandleKeyEvent(_THIS, NSEvent * event);
 extern void Cocoa_QuitKeyboard(_THIS);
 
-extern void Cocoa_StartTextInput(_THIS, SDL_Rect *rect);
+extern void Cocoa_StartTextInput(_THIS, SDL_Window *window);
+extern void Cocoa_StopTextInput(_THIS);
+extern void Cocoa_SetTextInputRect(_THIS, SDL_Rect *rect);
 
 #endif /* _SDL_cocoakeyboard_h */
 
--- a/src/video/cocoa/SDL_cocoakeyboard.m	Thu Aug 06 08:53:00 2009 +0000
+++ b/src/video/cocoa/SDL_cocoakeyboard.m	Thu Aug 06 08:56:48 2009 +0000
@@ -59,17 +59,24 @@
     NSString *_markedText;
     NSRange   _markedRange;
     NSRange   _selectedRange;
-    SDL_Rect inputRect;
+    SDL_Rect  _inputRect;
+    int       _keyboard;
 }
 - (void) doCommandBySelector:(SEL)myselector;
 - (void) setInputRect:(SDL_Rect *) rect;
+- (void) setKeyboard:(int) keyboard;
 @end
 
 @implementation SDLTranslatorResponder
 
+- (void) setKeyboard:(int) keyboard
+{
+    _keyboard = keyboard;
+}
+
 - (void) setInputRect:(SDL_Rect *) rect
 {
-    inputRect = *rect;
+    _inputRect = *rect;
 }
 
 - (void) insertText:(id) aString
@@ -85,7 +92,7 @@
     else
         str = [aString UTF8String];
 
-    SDL_SendKeyboardText(0, str);
+    SDL_SendKeyboardText(_keyboard, str);
 }
 
 - (void) doCommandBySelector:(SEL) myselector
@@ -145,7 +152,7 @@
 - (NSRect) firstRectForCharacterRange: (NSRange) theRange
 {
     float windowHeight = [[self window] frame].size.height;
-    NSRect rect = NSMakeRect(inputRect.x, windowHeight - inputRect.y, inputRect.w, inputRect.h);
+    NSRect rect = NSMakeRect(_inputRect.x, windowHeight - _inputRect.y, _inputRect.w, _inputRect.h);
 
     NSLog(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@",
             theRange.location, theRange.length, windowHeight,
@@ -585,6 +592,7 @@
     
     SDL_zero(keyboard);
     data->keyboard = SDL_AddKeyboard(&keyboard, -1);
+    [data->fieldEdit setKeyboard: data->keyboard];
     UpdateKeymap(data);
     
     /* Set our own names for the platform-dependent but layout-independent keys */
@@ -597,15 +605,43 @@
 }
 
 void
-Cocoa_StartTextInput(_THIS, SDL_Rect *rect)
+Cocoa_StartTextInput(_THIS, SDL_Window *window)
+{
+    SDL_VideoData *videoData = (SDL_VideoData *) _this->driverdata;
+    SDL_WindowData *windowData = (SDL_WindowData *) window->driverdata;
+    NSView *parentView = [windowData->window contentView];
+
+    if (! [[videoData->fieldEdit superview] isEqual: parentView])
+    {
+        NSLog(@"add fieldEdit to window contentView");
+        [videoData->fieldEdit removeFromSuperview];
+        [parentView addSubview: videoData->fieldEdit];
+        [windowData->window makeFirstResponder: videoData->fieldEdit];
+    }
+
+    SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
+    SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
+}
+
+void
+Cocoa_SetTextInputRect(_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_StopTextInput(_THIS)
+{
+    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
+
+    [data->fieldEdit removeFromSuperview];
+    SDL_EventState(SDL_TEXTINPUT, SDL_IGNORE);
+    SDL_EventState(SDL_TEXTEDITING, SDL_IGNORE);
+}
+
+void
 Cocoa_HandleKeyEvent(_THIS, NSEvent *event)
 {
     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
@@ -641,13 +677,6 @@
         if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
             /* FIXME CW 2007-08-16: only send those events to the field editor for which we actually want text events, not e.g. esc or function keys. Arrow keys in particular seem to produce crashes sometimes. */
             NSLog(@"interpretKeyEvents");
-            if (! [[data->fieldEdit superview] isEqual: [[event window] contentView]])
-            {
-                NSLog(@"add fieldEdit to window contentView");
-                [data->fieldEdit removeFromSuperview];
-                [[[event window] contentView] addSubview: data->fieldEdit];
-                [[event window] makeFirstResponder: data->fieldEdit];
-            }
             [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]];
 #if 0
             text = [[event characters] UTF8String];
--- a/src/video/cocoa/SDL_cocoavideo.m	Thu Aug 06 08:53:00 2009 +0000
+++ b/src/video/cocoa/SDL_cocoavideo.m	Thu Aug 06 08:56:48 2009 +0000
@@ -103,6 +103,8 @@
 #endif
 
     device->StartTextInput = Cocoa_StartTextInput;
+    device->StopTextInput = Cocoa_StopTextInput;
+    device->SetTextInputRect = Cocoa_SetTextInputRect;
 
     device->free = Cocoa_DeleteDevice;