From a6091bd43b90f341e30b6f44ed3418407b03de09 Mon Sep 17 00:00:00 2001 From: Jiang Jiang Date: Wed, 1 Jul 2009 07:33:58 +0000 Subject: [PATCH] Add SDL_TEXTEDTING event to inform application about marked text. --- include/SDL_events.h | 21 +++++++++++++++++++-- src/events/SDL_keyboard.c | 18 ++++++++++++++++++ src/events/SDL_keyboard_c.h | 3 +++ src/video/cocoa/SDL_cocoakeyboard.m | 2 ++ test/Makefile.in | 3 ++- test/testime.c | 9 +++++++++ 6 files changed, 53 insertions(+), 3 deletions(-) diff --git a/include/SDL_events.h b/include/SDL_events.h index 34c2a1242..2d981245e 100644 --- a/include/SDL_events.h +++ b/include/SDL_events.h @@ -74,9 +74,10 @@ typedef enum SDL_SYSWMEVENT, /**< System specific event */ SDL_PROXIMITYIN, /**< Proximity In event */ SDL_PROXIMITYOUT, /**< Proximity Out event */ - SDL_EVENT_RESERVED1, /**< Reserved for future use... */ + SDL_EVENT_RESERVED1, SDL_EVENT_RESERVED2, SDL_EVENT_RESERVED3, + SDL_TEXTEDITING, /**< Reserved for future use... */ /* Events SDL_USEREVENT through SDL_MAXEVENTS-1 are for your use */ SDL_USEREVENT = 24, /* This last event is only for bounding internal arrays @@ -116,7 +117,8 @@ typedef enum SDL_QUITMASK = SDL_EVENTMASK(SDL_QUIT), SDL_SYSWMEVENTMASK = SDL_EVENTMASK(SDL_SYSWMEVENT), SDL_PROXIMITYINMASK = SDL_EVENTMASK(SDL_PROXIMITYIN), - SDL_PROXIMITYOUTMASK = SDL_EVENTMASK(SDL_PROXIMITYOUT) + SDL_PROXIMITYOUTMASK = SDL_EVENTMASK(SDL_PROXIMITYOUT), + SDL_TEXTEDITINGMASK = SDL_EVENTMASK(SDL_TEXTEDITING) } SDL_EventMask; #define SDL_ALLEVENTS 0xFFFFFFFF @@ -162,6 +164,20 @@ typedef struct SDL_TextInputEvent SDL_WindowID windowID; /**< The window with keyboard focus, if any */ } SDL_TextInputEvent; +/** + * \struct SDL_TextEditingEvent + * + * \brief Keyboard text editing event structure (event.edit.*) + */ +#define SDL_TEXTEDITINGEVENT_TEXT_SIZE (32) +typedef struct SDL_TextEditingEvent +{ + Uint8 type; /**< SDL_TEXTEDITING */ + char text[SDL_TEXTEDITINGEVENT_TEXT_SIZE]; /**< The editing text */ + int start; /**< The start cursor of selected editing text */ + int length; /**< The length of selected editing text */ +} SDL_TextEditingEvent; + /** * \struct SDL_MouseMotionEvent * @@ -358,6 +374,7 @@ typedef union SDL_Event SDL_UserEvent user; /**< Custom event data */ SDL_SysWMEvent syswm; /**< System dependent window event data */ SDL_ProximityEvent proximity; /**< Proximity In or Out event */ + SDL_TextEditingEvent edit; /**< Text editing event data */ /* Temporarily here for backwards compatibility */ SDL_ActiveEvent active; diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c index ffac838db..0173c4513 100644 --- a/src/events/SDL_keyboard.c +++ b/src/events/SDL_keyboard.c @@ -852,6 +852,24 @@ SDL_SendKeyboardText(int index, const char *text) return (posted); } +int +SDL_SendEditingText(const char *text, int start, int length) +{ + int posted; + + /* Post the event, if desired */ + posted = 0; + if (SDL_ProcessEvents[SDL_TEXTEDITING] == SDL_ENABLE) { + SDL_Event event; + event.edit.type = SDL_TEXTEDITING; + event.edit.start = start; + event.edit.length = length; + SDL_strlcpy(event.edit.text, text, SDL_arraysize(event.text.text)); + posted = (SDL_PushEvent(&event) > 0); + } + return (posted); +} + void SDL_KeyboardQuit(void) { diff --git a/src/events/SDL_keyboard_c.h b/src/events/SDL_keyboard_c.h index a6f44c1aa..a067494ef 100644 --- a/src/events/SDL_keyboard_c.h +++ b/src/events/SDL_keyboard_c.h @@ -81,6 +81,9 @@ extern int SDL_SendKeyboardKey(int index, Uint8 state, SDL_scancode scancode); /* Send keyboard text input for a keyboard at an index */ extern int SDL_SendKeyboardText(int index, const char *text); +/* Send editing text for selected range from start to end */ +extern int SDL_SendEditingText(const char *text, int start, int end); + /* Shutdown the keyboard subsystem */ extern void SDL_KeyboardQuit(void); diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m index 2c052e808..e50ec2a21 100644 --- a/src/video/cocoa/SDL_cocoakeyboard.m +++ b/src/video/cocoa/SDL_cocoakeyboard.m @@ -130,6 +130,8 @@ - (void) setMarkedText:(id) aString _selectedRange = selRange; _markedRange = NSMakeRange(0, [aString length]); + SDL_SendEditingText([aString UTF8String], selRange.location, selRange.length); + NSLog(@"setMarkedText: %@, (%d, %d)", _markedText, selRange.location, selRange.length); } diff --git a/test/Makefile.in b/test/Makefile.in index 4e7096259..04babd390 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -147,7 +147,8 @@ testmmousetablet$(EXE): $(srcdir)/testmmousetablet.c $(CC) -o $@ $? $(CFLAGS) $(LIBS) testime$(EXE): $(srcdir)/testime.c - $(CC) -o $@ $? $(CFLAGS) -L../build/.libs $(LIBS) -lSDL_ttf + $(CC) -o $@ $? -I../include $(CFLAGS) -L../build/.libs $(LIBS) -lSDL_ttf + install_name_tool -change /usr/local/lib/libSDL-1.3.0.dylib ../build/.libs/libSDL-1.3.0.dylib $@ clean: rm -f $(TARGETS) diff --git a/test/testime.c b/test/testime.c index 3a80d911a..e6c773f3b 100644 --- a/test/testime.c +++ b/test/testime.c @@ -111,6 +111,15 @@ int main(int argc, char *argv[]) SDL_StartTextInput(&markedRect); break; + case SDL_TEXTEDITING: + fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n", + event.edit.text, event.edit.start, event.edit.length); + + SDL_FillRect(screen, &markedRect, backColor); + render_text(screen, font, event.edit.text, markedRect.x, markedRect.y, textColor); + SDL_Flip(screen); + break; + case SDL_QUIT: done = 1; break;