changeset: 4433:25667ea797fa
authorSam Lantinga <slouken@libsdl.org>
Thu, 15 Apr 2010 22:14:26 -0700
changeset 44345c64052fb476
parent 4433 9fa97c6b0014
child 4435 e953700da4ca
changeset: 4433:25667ea797fa
tag: tip
user: Jiang Jiang <gzjjgod@gmail.com>
date: Thu Apr 15 12:01:46 2010 +0800
summary: Add windowID to text editing event
include/SDL_events.h
src/events/SDL_keyboard.c
src/events/SDL_keyboard_c.h
src/video/cocoa/SDL_cocoakeyboard.m
     1.1 --- a/include/SDL_events.h	Thu Apr 15 21:27:32 2010 -0700
     1.2 +++ b/include/SDL_events.h	Thu Apr 15 22:14:26 2010 -0700
     1.3 @@ -138,6 +138,7 @@
     1.4  typedef struct SDL_TextEditingEvent
     1.5  {
     1.6      Uint32 type;                                /**< ::SDL_TEXTEDITING */
     1.7 +    Uint32 windowID;                            /**< The window with keyboard focus, if any */
     1.8      char text[SDL_TEXTEDITINGEVENT_TEXT_SIZE];  /**< The editing text */
     1.9      int start;                                  /**< The start cursor of selected editing text */
    1.10      int length;                                 /**< The length of selected editing text */
     2.1 --- a/src/events/SDL_keyboard.c	Thu Apr 15 21:27:32 2010 -0700
     2.2 +++ b/src/events/SDL_keyboard.c	Thu Apr 15 22:14:26 2010 -0700
     2.3 @@ -679,6 +679,8 @@
     2.4      if (keyboard->focus) {
     2.5          SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_GAINED,
     2.6                              0, 0);
     2.7 +        if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY))
     2.8 +            SDL_StartTextInput();
     2.9      }
    2.10  }
    2.11  
    2.12 @@ -839,10 +841,14 @@
    2.13  }
    2.14  
    2.15  int
    2.16 -SDL_SendEditingText(const char *text, int start, int length)
    2.17 +SDL_SendEditingText(int index, const char *text, int start, int length)
    2.18  {
    2.19 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    2.20      int posted;
    2.21  
    2.22 +    if (!keyboard)
    2.23 +        return 0;
    2.24 +
    2.25      /* Post the event, if desired */
    2.26      posted = 0;
    2.27      if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) {
    2.28 @@ -851,6 +857,7 @@
    2.29          event.edit.start = start;
    2.30          event.edit.length = length;
    2.31          SDL_strlcpy(event.edit.text, text, SDL_arraysize(event.text.text));
    2.32 +        event.edit.windowID = keyboard->focus->id;
    2.33          posted = (SDL_PushEvent(&event) > 0);
    2.34      }
    2.35      return (posted);
     3.1 --- a/src/events/SDL_keyboard_c.h	Thu Apr 15 21:27:32 2010 -0700
     3.2 +++ b/src/events/SDL_keyboard_c.h	Thu Apr 15 22:14:26 2010 -0700
     3.3 @@ -82,7 +82,7 @@
     3.4  extern int SDL_SendKeyboardText(int index, const char *text);
     3.5  
     3.6  /* Send editing text for selected range from start to end */
     3.7 -extern int SDL_SendEditingText(const char *text, int start, int end);
     3.8 +extern int SDL_SendEditingText(int index, const char *text, int start, int end);
     3.9  
    3.10  /* Shutdown the keyboard subsystem */
    3.11  extern void SDL_KeyboardQuit(void);
     4.1 --- a/src/video/cocoa/SDL_cocoakeyboard.m	Thu Apr 15 21:27:32 2010 -0700
     4.2 +++ b/src/video/cocoa/SDL_cocoakeyboard.m	Thu Apr 15 22:14:26 2010 -0700
     4.3 @@ -140,7 +140,8 @@
     4.4      _selectedRange = selRange;
     4.5      _markedRange = NSMakeRange(0, [aString length]);
     4.6  
     4.7 -    SDL_SendEditingText([aString UTF8String], selRange.location, selRange.length);
     4.8 +    SDL_SendEditingText(_keyboard, [aString UTF8String],
     4.9 +                        selRange.location, selRange.length);
    4.10  
    4.11      DEBUG_IME(@"setMarkedText: %@, (%d, %d)", _markedText,
    4.12            selRange.location, selRange.length);
    4.13 @@ -632,7 +633,15 @@
    4.14      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    4.15      NSView *parentView = [[NSApp keyWindow] contentView];
    4.16  
    4.17 -    data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)];
    4.18 +    /* We only keep one field editor per process, since only the front most
    4.19 +     * window can receive text input events, so it make no sense to keep more
    4.20 +     * than one copy. When we switched to another window and requesting for
    4.21 +     * text input, simply remove the field editor from its superview then add
    4.22 +     * it to the front most window's content view */
    4.23 +    if (! data->fieldEdit)
    4.24 +        data->fieldEdit =
    4.25 +            [[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];
    4.26 +
    4.27      [data->fieldEdit setKeyboard: data->keyboard];
    4.28  
    4.29      if (! [[data->fieldEdit superview] isEqual: parentView])