Further polish API, fix crash in test program. gsoc2009_IME
authorJiang Jiang <gzjjgod@gmail.com>
Thu, 06 Aug 2009 08:59:53 +0000
branchgsoc2009_IME
changeset 3136962357f325e1
parent 3135 f4e553ec6a62
child 3137 311c678f3b2e
Further polish API, fix crash in test program.
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_cocoamodes.m
test/testime.c
     1.1 --- a/include/SDL_video.h	Thu Aug 06 08:56:48 2009 +0000
     1.2 +++ b/include/SDL_video.h	Thu Aug 06 08:59:53 2009 +0000
     1.3 @@ -1468,14 +1468,14 @@
     1.4  extern DECLSPEC void SDLCALL SDL_GL_DeleteContext(SDL_GLContext context);
     1.5  
     1.6  /**
     1.7 - * \fn void SDL_StartTextInput(SDL_WindowID windowID)
     1.8 + * \fn void SDL_StartTextInput(void)
     1.9   *
    1.10 - * \brief Start accepting Unicode text input events in given window.
    1.11 + * \brief Start accepting Unicode text input events.
    1.12   *
    1.13   * \sa SDL_StopTextInput()
    1.14   * \sa SDL_SetTextInputRect()
    1.15   */
    1.16 -extern DECLSPEC void SDLCALL SDL_StartTextInput(SDL_WindowID windowID);
    1.17 +extern DECLSPEC void SDLCALL SDL_StartTextInput(void);
    1.18  
    1.19  /**
    1.20   * \fn void SDL_StopTextInput(void)
    1.21 @@ -1495,6 +1495,13 @@
    1.22   */
    1.23  extern DECLSPEC void SDLCALL SDL_SetTextInputRect(SDL_Rect *rect);
    1.24  
    1.25 +/**
    1.26 + * \fn SDL_WindowID SDL_GetFocusWindow(void)
    1.27 + *
    1.28 + * \brief Get focused window.
    1.29 + */
    1.30 +extern DECLSPEC SDL_WindowID SDLCALL SDL_GetFocusWindow(void);
    1.31 +
    1.32  /* Ends C function definitions when using C++ */
    1.33  #ifdef __cplusplus
    1.34  /* *INDENT-OFF* */
     2.1 --- a/src/video/SDL_sysvideo.h	Thu Aug 06 08:56:48 2009 +0000
     2.2 +++ b/src/video/SDL_sysvideo.h	Thu Aug 06 08:59:53 2009 +0000
     2.3 @@ -278,7 +278,7 @@
     2.4      void (*SuspendScreenSaver) (_THIS);
     2.5  
     2.6      /* Text input */
     2.7 -    void (*StartTextInput) (_THIS, SDL_Window *window);
     2.8 +    void (*StartTextInput) (_THIS);
     2.9      void (*StopTextInput) (_THIS);
    2.10      void (*SetTextInputRect) (_THIS, SDL_Rect *rect);
    2.11  
    2.12 @@ -428,7 +428,6 @@
    2.13  extern void SDL_OnWindowResized(SDL_Window * window);
    2.14  extern void SDL_OnWindowFocusGained(SDL_Window * window);
    2.15  extern void SDL_OnWindowFocusLost(SDL_Window * window);
    2.16 -extern SDL_WindowID SDL_GetFocusWindow(void);
    2.17  
    2.18  #endif /* _SDL_sysvideo_h */
    2.19  
     3.1 --- a/src/video/SDL_video.c	Thu Aug 06 08:56:48 2009 +0000
     3.2 +++ b/src/video/SDL_video.c	Thu Aug 06 08:59:53 2009 +0000
     3.3 @@ -3068,12 +3068,10 @@
     3.4  }
     3.5  
     3.6  void
     3.7 -SDL_StartTextInput(SDL_WindowID windowID)
     3.8 +SDL_StartTextInput(void)
     3.9  {
    3.10 -    SDL_Window *window = SDL_GetWindowFromID(windowID);
    3.11 -
    3.12      if (_this->StartTextInput) {
    3.13 -        _this->StartTextInput(_this, window);
    3.14 +        _this->StartTextInput(_this);
    3.15      }
    3.16  }
    3.17  
     4.1 --- a/src/video/cocoa/SDL_cocoakeyboard.h	Thu Aug 06 08:56:48 2009 +0000
     4.2 +++ b/src/video/cocoa/SDL_cocoakeyboard.h	Thu Aug 06 08:59:53 2009 +0000
     4.3 @@ -28,7 +28,7 @@
     4.4  extern void Cocoa_HandleKeyEvent(_THIS, NSEvent * event);
     4.5  extern void Cocoa_QuitKeyboard(_THIS);
     4.6  
     4.7 -extern void Cocoa_StartTextInput(_THIS, SDL_Window *window);
     4.8 +extern void Cocoa_StartTextInput(_THIS);
     4.9  extern void Cocoa_StopTextInput(_THIS);
    4.10  extern void Cocoa_SetTextInputRect(_THIS, SDL_Rect *rect);
    4.11  
     5.1 --- a/src/video/cocoa/SDL_cocoakeyboard.m	Thu Aug 06 08:56:48 2009 +0000
     5.2 +++ b/src/video/cocoa/SDL_cocoakeyboard.m	Thu Aug 06 08:59:53 2009 +0000
     5.3 @@ -54,7 +54,7 @@
     5.4      #define NX_DEVICERCTLKEYMASK    0x00002000
     5.5  #endif
     5.6  
     5.7 -@interface SDLTranslatorResponder : NSTextView
     5.8 +@interface SDLTranslatorResponder : NSView <NSTextInput>
     5.9  {
    5.10      NSString *_markedText;
    5.11      NSRange   _markedRange;
    5.12 @@ -97,7 +97,6 @@
    5.13  
    5.14  - (void) doCommandBySelector:(SEL) myselector
    5.15  {
    5.16 -    NSLog(@"doCommandBySelector, passed down");
    5.17      [super doCommandBySelector: myselector];
    5.18  }
    5.19  
    5.20 @@ -152,7 +151,8 @@
    5.21  - (NSRect) firstRectForCharacterRange: (NSRange) theRange
    5.22  {
    5.23      float windowHeight = [[self window] frame].size.height;
    5.24 -    NSRect rect = NSMakeRect(_inputRect.x, windowHeight - _inputRect.y, _inputRect.w, _inputRect.h);
    5.25 +    NSRect rect = NSMakeRect(_inputRect.x, windowHeight - _inputRect.y - _inputRect.h,
    5.26 +                             _inputRect.w, _inputRect.h);
    5.27  
    5.28      NSLog(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@",
    5.29              theRange.location, theRange.length, windowHeight,
    5.30 @@ -584,15 +584,9 @@
    5.31  {
    5.32      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    5.33      SDL_Keyboard keyboard;
    5.34 -    NSAutoreleasePool *pool;
    5.35  
    5.36 -    pool = [[NSAutoreleasePool alloc] init];
    5.37 -    data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)];
    5.38 -    [pool release];
    5.39 -    
    5.40      SDL_zero(keyboard);
    5.41      data->keyboard = SDL_AddKeyboard(&keyboard, -1);
    5.42 -    [data->fieldEdit setKeyboard: data->keyboard];
    5.43      UpdateKeymap(data);
    5.44      
    5.45      /* Set our own names for the platform-dependent but layout-independent keys */
    5.46 @@ -605,20 +599,25 @@
    5.47  }
    5.48  
    5.49  void
    5.50 -Cocoa_StartTextInput(_THIS, SDL_Window *window)
    5.51 +Cocoa_StartTextInput(_THIS)
    5.52  {
    5.53 -    SDL_VideoData *videoData = (SDL_VideoData *) _this->driverdata;
    5.54 -    SDL_WindowData *windowData = (SDL_WindowData *) window->driverdata;
    5.55 -    NSView *parentView = [windowData->window contentView];
    5.56 +    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    5.57 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    5.58 +    NSView *parentView = [[NSApp keyWindow] contentView];
    5.59  
    5.60 -    if (! [[videoData->fieldEdit superview] isEqual: parentView])
    5.61 +    data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)];
    5.62 +    [data->fieldEdit setKeyboard: data->keyboard];
    5.63 +
    5.64 +    if (! [[data->fieldEdit superview] isEqual: parentView])
    5.65      {
    5.66          NSLog(@"add fieldEdit to window contentView");
    5.67 -        [videoData->fieldEdit removeFromSuperview];
    5.68 -        [parentView addSubview: videoData->fieldEdit];
    5.69 -        [windowData->window makeFirstResponder: videoData->fieldEdit];
    5.70 +        [data->fieldEdit removeFromSuperview];
    5.71 +        [parentView addSubview: data->fieldEdit];
    5.72 +        [[NSApp keyWindow] makeFirstResponder: data->fieldEdit];
    5.73      }
    5.74  
    5.75 +    [pool release];
    5.76 +
    5.77      SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
    5.78      SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
    5.79  }
    5.80 @@ -636,7 +635,12 @@
    5.81  {
    5.82      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    5.83  
    5.84 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    5.85      [data->fieldEdit removeFromSuperview];
    5.86 +    [data->fieldEdit release];
    5.87 +    data->fieldEdit = nil;
    5.88 +    [pool release];
    5.89 +
    5.90      SDL_EventState(SDL_TEXTINPUT, SDL_IGNORE);
    5.91      SDL_EventState(SDL_TEXTEDITING, SDL_IGNORE);
    5.92  }
    5.93 @@ -676,7 +680,6 @@
    5.94          }
    5.95          if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
    5.96              /* 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. */
    5.97 -            NSLog(@"interpretKeyEvents");
    5.98              [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]];
    5.99  #if 0
   5.100              text = [[event characters] UTF8String];
   5.101 @@ -706,10 +709,6 @@
   5.102      NSAutoreleasePool *pool;
   5.103  
   5.104      SDL_DelKeyboard(data->keyboard);
   5.105 -
   5.106 -    pool = [[NSAutoreleasePool alloc] init];
   5.107 -    [data->fieldEdit release];
   5.108 -    [pool release];
   5.109  }
   5.110  
   5.111  /* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/video/cocoa/SDL_cocoamodes.m	Thu Aug 06 08:56:48 2009 +0000
     6.2 +++ b/src/video/cocoa/SDL_cocoamodes.m	Thu Aug 06 08:59:53 2009 +0000
     6.3 @@ -248,6 +248,11 @@
     6.4          CGReleaseDisplayFadeReservation(fade_token);
     6.5      }
     6.6  
     6.7 +    NSRect frame = [[NSScreen mainScreen] frame];
     6.8 +    NSLog(@"mainScreen frame: %gx%g", frame.size.width, frame.size.height);
     6.9 +
    6.10 +    [[NSApp mainWindow] makeKeyAndOrderFront: nil];
    6.11 +
    6.12      /* 
    6.13          There is a bug in Cocoa where NSScreen doesn't synchronize
    6.14          with CGDirectDisplay, so the main screen's frame is wrong.
     7.1 --- a/test/testime.c	Thu Aug 06 08:56:48 2009 +0000
     7.2 +++ b/test/testime.c	Thu Aug 06 08:59:53 2009 +0000
     7.3 @@ -65,6 +65,7 @@
     7.4  
     7.5  void CleanupVideo()
     7.6  {
     7.7 +    SDL_StopTextInput();
     7.8      TTF_CloseFont(font);
     7.9      TTF_Quit();
    7.10  }
    7.11 @@ -82,6 +83,8 @@
    7.12      text[0] = 0;
    7.13      markedRect = textRect;
    7.14      markedText = NULL;
    7.15 +
    7.16 +    SDL_StartTextInput();
    7.17  }
    7.18  
    7.19  static void RenderText(SDL_Surface *sur,
    7.20 @@ -115,6 +118,8 @@
    7.21      if (markedRect.w < 0)
    7.22      {
    7.23          SDL_Flip(screen);
    7.24 +        // Stop text input because we cannot hold any more characters
    7.25 +        SDL_StopTextInput();
    7.26          return;
    7.27      }
    7.28  
    7.29 @@ -139,7 +144,7 @@
    7.30  
    7.31      SDL_Flip(screen);
    7.32  
    7.33 -    SDL_StartTextInput(&markedRect);
    7.34 +    SDL_SetTextInputRect(&markedRect);
    7.35  }
    7.36  
    7.37  void