Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Further polish API, fix crash in test program.
  • Loading branch information
jjgod committed Aug 6, 2009
1 parent 4478964 commit 3ba1c8a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 33 deletions.
13 changes: 10 additions & 3 deletions include/SDL_video.h
Expand Up @@ -1468,14 +1468,14 @@ 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_WindowID windowID)
* \fn void SDL_StartTextInput(void)
*
* \brief Start accepting Unicode text input events in given window.
* \brief Start accepting Unicode text input events.
*
* \sa SDL_StopTextInput()
* \sa SDL_SetTextInputRect()
*/
extern DECLSPEC void SDLCALL SDL_StartTextInput(SDL_WindowID windowID);
extern DECLSPEC void SDLCALL SDL_StartTextInput(void);

/**
* \fn void SDL_StopTextInput(void)
Expand All @@ -1495,6 +1495,13 @@ extern DECLSPEC void SDLCALL SDL_StopTextInput(void);
*/
extern DECLSPEC void SDLCALL SDL_SetTextInputRect(SDL_Rect *rect);

/**
* \fn SDL_WindowID SDL_GetFocusWindow(void)
*
* \brief Get focused window.
*/
extern DECLSPEC SDL_WindowID SDLCALL SDL_GetFocusWindow(void);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
/* *INDENT-OFF* */
Expand Down
3 changes: 1 addition & 2 deletions src/video/SDL_sysvideo.h
Expand Up @@ -278,7 +278,7 @@ struct SDL_VideoDevice
void (*SuspendScreenSaver) (_THIS);

/* Text input */
void (*StartTextInput) (_THIS, SDL_Window *window);
void (*StartTextInput) (_THIS);
void (*StopTextInput) (_THIS);
void (*SetTextInputRect) (_THIS, SDL_Rect *rect);

Expand Down Expand Up @@ -428,7 +428,6 @@ extern void SDL_OnWindowHidden(SDL_Window * window);
extern void SDL_OnWindowResized(SDL_Window * window);
extern void SDL_OnWindowFocusGained(SDL_Window * window);
extern void SDL_OnWindowFocusLost(SDL_Window * window);
extern SDL_WindowID SDL_GetFocusWindow(void);

#endif /* _SDL_sysvideo_h */

Expand Down
6 changes: 2 additions & 4 deletions src/video/SDL_video.c
Expand Up @@ -3068,12 +3068,10 @@ SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
}

void
SDL_StartTextInput(SDL_WindowID windowID)
SDL_StartTextInput(void)
{
SDL_Window *window = SDL_GetWindowFromID(windowID);

if (_this->StartTextInput) {
_this->StartTextInput(_this, window);
_this->StartTextInput(_this);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/video/cocoa/SDL_cocoakeyboard.h
Expand Up @@ -28,7 +28,7 @@ extern void Cocoa_InitKeyboard(_THIS);
extern void Cocoa_HandleKeyEvent(_THIS, NSEvent * event);
extern void Cocoa_QuitKeyboard(_THIS);

extern void Cocoa_StartTextInput(_THIS, SDL_Window *window);
extern void Cocoa_StartTextInput(_THIS);
extern void Cocoa_StopTextInput(_THIS);
extern void Cocoa_SetTextInputRect(_THIS, SDL_Rect *rect);

Expand Down
43 changes: 21 additions & 22 deletions src/video/cocoa/SDL_cocoakeyboard.m
Expand Up @@ -54,7 +54,7 @@
#define NX_DEVICERCTLKEYMASK 0x00002000
#endif

@interface SDLTranslatorResponder : NSTextView
@interface SDLTranslatorResponder : NSView <NSTextInput>
{
NSString *_markedText;
NSRange _markedRange;
Expand Down Expand Up @@ -97,7 +97,6 @@ - (void) insertText:(id) aString

- (void) doCommandBySelector:(SEL) myselector
{
NSLog(@"doCommandBySelector, passed down");
[super doCommandBySelector: myselector];
}

Expand Down Expand Up @@ -152,7 +151,8 @@ - (void) unmarkText
- (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.h,
_inputRect.w, _inputRect.h);

NSLog(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@",
theRange.location, theRange.length, windowHeight,
Expand Down Expand Up @@ -584,15 +584,9 @@ - (NSArray *) validAttributesForMarkedText
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
SDL_Keyboard keyboard;
NSAutoreleasePool *pool;

pool = [[NSAutoreleasePool alloc] init];
data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)];
[pool release];

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 */
Expand All @@ -605,20 +599,25 @@ - (NSArray *) validAttributesForMarkedText
}

void
Cocoa_StartTextInput(_THIS, SDL_Window *window)
Cocoa_StartTextInput(_THIS)
{
SDL_VideoData *videoData = (SDL_VideoData *) _this->driverdata;
SDL_WindowData *windowData = (SDL_WindowData *) window->driverdata;
NSView *parentView = [windowData->window contentView];
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSView *parentView = [[NSApp keyWindow] contentView];

data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)];
[data->fieldEdit setKeyboard: data->keyboard];

if (! [[videoData->fieldEdit superview] isEqual: parentView])
if (! [[data->fieldEdit superview] isEqual: parentView])
{
NSLog(@"add fieldEdit to window contentView");
[videoData->fieldEdit removeFromSuperview];
[parentView addSubview: videoData->fieldEdit];
[windowData->window makeFirstResponder: videoData->fieldEdit];
[data->fieldEdit removeFromSuperview];
[parentView addSubview: data->fieldEdit];
[[NSApp keyWindow] makeFirstResponder: data->fieldEdit];
}

[pool release];

SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
}
Expand All @@ -636,7 +635,12 @@ - (NSArray *) validAttributesForMarkedText
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[data->fieldEdit removeFromSuperview];
[data->fieldEdit release];
data->fieldEdit = nil;
[pool release];

SDL_EventState(SDL_TEXTINPUT, SDL_IGNORE);
SDL_EventState(SDL_TEXTEDITING, SDL_IGNORE);
}
Expand Down Expand Up @@ -676,7 +680,6 @@ - (NSArray *) validAttributesForMarkedText
}
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");
[data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]];
#if 0
text = [[event characters] UTF8String];
Expand Down Expand Up @@ -706,10 +709,6 @@ - (NSArray *) validAttributesForMarkedText
NSAutoreleasePool *pool;

SDL_DelKeyboard(data->keyboard);

pool = [[NSAutoreleasePool alloc] init];
[data->fieldEdit release];
[pool release];
}

/* vi: set ts=4 sw=4 expandtab: */
5 changes: 5 additions & 0 deletions src/video/cocoa/SDL_cocoamodes.m
Expand Up @@ -248,6 +248,11 @@ - (void) setFrame:(NSRect)frame;
CGReleaseDisplayFadeReservation(fade_token);
}

NSRect frame = [[NSScreen mainScreen] frame];
NSLog(@"mainScreen frame: %gx%g", frame.size.width, frame.size.height);

[[NSApp mainWindow] makeKeyAndOrderFront: nil];

/*
There is a bug in Cocoa where NSScreen doesn't synchronize
with CGDirectDisplay, so the main screen's frame is wrong.
Expand Down
7 changes: 6 additions & 1 deletion test/testime.c
Expand Up @@ -65,6 +65,7 @@ void InitVideo(int argc, char *argv[])

void CleanupVideo()
{
SDL_StopTextInput();
TTF_CloseFont(font);
TTF_Quit();
}
Expand All @@ -82,6 +83,8 @@ void InitInput()
text[0] = 0;
markedRect = textRect;
markedText = NULL;

SDL_StartTextInput();
}

static void RenderText(SDL_Surface *sur,
Expand Down Expand Up @@ -115,6 +118,8 @@ void Redraw()
if (markedRect.w < 0)
{
SDL_Flip(screen);
// Stop text input because we cannot hold any more characters
SDL_StopTextInput();
return;
}

Expand All @@ -139,7 +144,7 @@ void Redraw()

SDL_Flip(screen);

SDL_StartTextInput(&markedRect);
SDL_SetTextInputRect(&markedRect);
}

void
Expand Down

0 comments on commit 3ba1c8a

Please sign in to comment.