Mercurial > sdl-ios-xcode
comparison src/video/cocoa/SDL_cocoakeyboard.m @ 3136:962357f325e1 gsoc2009_IME
Further polish API, fix crash in test program.
author | Jiang Jiang <gzjjgod@gmail.com> |
---|---|
date | Thu, 06 Aug 2009 08:59:53 +0000 |
parents | f4e553ec6a62 |
children | 7100a1a20143 |
comparison
equal
deleted
inserted
replaced
3135:f4e553ec6a62 | 3136:962357f325e1 |
---|---|
52 #endif | 52 #endif |
53 #ifndef NX_DEVICERCTLKEYMASK | 53 #ifndef NX_DEVICERCTLKEYMASK |
54 #define NX_DEVICERCTLKEYMASK 0x00002000 | 54 #define NX_DEVICERCTLKEYMASK 0x00002000 |
55 #endif | 55 #endif |
56 | 56 |
57 @interface SDLTranslatorResponder : NSTextView | 57 @interface SDLTranslatorResponder : NSView <NSTextInput> |
58 { | 58 { |
59 NSString *_markedText; | 59 NSString *_markedText; |
60 NSRange _markedRange; | 60 NSRange _markedRange; |
61 NSRange _selectedRange; | 61 NSRange _selectedRange; |
62 SDL_Rect _inputRect; | 62 SDL_Rect _inputRect; |
95 SDL_SendKeyboardText(_keyboard, str); | 95 SDL_SendKeyboardText(_keyboard, str); |
96 } | 96 } |
97 | 97 |
98 - (void) doCommandBySelector:(SEL) myselector | 98 - (void) doCommandBySelector:(SEL) myselector |
99 { | 99 { |
100 NSLog(@"doCommandBySelector, passed down"); | |
101 [super doCommandBySelector: myselector]; | 100 [super doCommandBySelector: myselector]; |
102 } | 101 } |
103 | 102 |
104 - (BOOL) hasMarkedText | 103 - (BOOL) hasMarkedText |
105 { | 104 { |
150 } | 149 } |
151 | 150 |
152 - (NSRect) firstRectForCharacterRange: (NSRange) theRange | 151 - (NSRect) firstRectForCharacterRange: (NSRange) theRange |
153 { | 152 { |
154 float windowHeight = [[self window] frame].size.height; | 153 float windowHeight = [[self window] frame].size.height; |
155 NSRect rect = NSMakeRect(_inputRect.x, windowHeight - _inputRect.y, _inputRect.w, _inputRect.h); | 154 NSRect rect = NSMakeRect(_inputRect.x, windowHeight - _inputRect.y - _inputRect.h, |
155 _inputRect.w, _inputRect.h); | |
156 | 156 |
157 NSLog(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@", | 157 NSLog(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@", |
158 theRange.location, theRange.length, windowHeight, | 158 theRange.location, theRange.length, windowHeight, |
159 NSStringFromRect(rect)); | 159 NSStringFromRect(rect)); |
160 rect.origin = [[self window] convertBaseToScreen: rect.origin]; | 160 rect.origin = [[self window] convertBaseToScreen: rect.origin]; |
582 void | 582 void |
583 Cocoa_InitKeyboard(_THIS) | 583 Cocoa_InitKeyboard(_THIS) |
584 { | 584 { |
585 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; | 585 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; |
586 SDL_Keyboard keyboard; | 586 SDL_Keyboard keyboard; |
587 NSAutoreleasePool *pool; | 587 |
588 | |
589 pool = [[NSAutoreleasePool alloc] init]; | |
590 data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)]; | |
591 [pool release]; | |
592 | |
593 SDL_zero(keyboard); | 588 SDL_zero(keyboard); |
594 data->keyboard = SDL_AddKeyboard(&keyboard, -1); | 589 data->keyboard = SDL_AddKeyboard(&keyboard, -1); |
595 [data->fieldEdit setKeyboard: data->keyboard]; | |
596 UpdateKeymap(data); | 590 UpdateKeymap(data); |
597 | 591 |
598 /* Set our own names for the platform-dependent but layout-independent keys */ | 592 /* Set our own names for the platform-dependent but layout-independent keys */ |
599 /* This key is NumLock on the MacBook keyboard. :) */ | 593 /* This key is NumLock on the MacBook keyboard. :) */ |
600 /*SDL_SetScancodeName(SDL_SCANCODE_NUMLOCKCLEAR, "Clear");*/ | 594 /*SDL_SetScancodeName(SDL_SCANCODE_NUMLOCKCLEAR, "Clear");*/ |
603 SDL_SetScancodeName(SDL_SCANCODE_RALT, "Right Option"); | 597 SDL_SetScancodeName(SDL_SCANCODE_RALT, "Right Option"); |
604 SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Command"); | 598 SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Command"); |
605 } | 599 } |
606 | 600 |
607 void | 601 void |
608 Cocoa_StartTextInput(_THIS, SDL_Window *window) | 602 Cocoa_StartTextInput(_THIS) |
609 { | 603 { |
610 SDL_VideoData *videoData = (SDL_VideoData *) _this->driverdata; | 604 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; |
611 SDL_WindowData *windowData = (SDL_WindowData *) window->driverdata; | 605 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
612 NSView *parentView = [windowData->window contentView]; | 606 NSView *parentView = [[NSApp keyWindow] contentView]; |
613 | 607 |
614 if (! [[videoData->fieldEdit superview] isEqual: parentView]) | 608 data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)]; |
609 [data->fieldEdit setKeyboard: data->keyboard]; | |
610 | |
611 if (! [[data->fieldEdit superview] isEqual: parentView]) | |
615 { | 612 { |
616 NSLog(@"add fieldEdit to window contentView"); | 613 NSLog(@"add fieldEdit to window contentView"); |
617 [videoData->fieldEdit removeFromSuperview]; | 614 [data->fieldEdit removeFromSuperview]; |
618 [parentView addSubview: videoData->fieldEdit]; | 615 [parentView addSubview: data->fieldEdit]; |
619 [windowData->window makeFirstResponder: videoData->fieldEdit]; | 616 [[NSApp keyWindow] makeFirstResponder: data->fieldEdit]; |
620 } | 617 } |
618 | |
619 [pool release]; | |
621 | 620 |
622 SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE); | 621 SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE); |
623 SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE); | 622 SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE); |
624 } | 623 } |
625 | 624 |
634 void | 633 void |
635 Cocoa_StopTextInput(_THIS) | 634 Cocoa_StopTextInput(_THIS) |
636 { | 635 { |
637 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; | 636 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; |
638 | 637 |
638 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
639 [data->fieldEdit removeFromSuperview]; | 639 [data->fieldEdit removeFromSuperview]; |
640 [data->fieldEdit release]; | |
641 data->fieldEdit = nil; | |
642 [pool release]; | |
643 | |
640 SDL_EventState(SDL_TEXTINPUT, SDL_IGNORE); | 644 SDL_EventState(SDL_TEXTINPUT, SDL_IGNORE); |
641 SDL_EventState(SDL_TEXTEDITING, SDL_IGNORE); | 645 SDL_EventState(SDL_TEXTEDITING, SDL_IGNORE); |
642 } | 646 } |
643 | 647 |
644 void | 648 void |
674 } | 678 } |
675 #endif | 679 #endif |
676 } | 680 } |
677 if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) { | 681 if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) { |
678 /* 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. */ | 682 /* 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. */ |
679 NSLog(@"interpretKeyEvents"); | |
680 [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]]; | 683 [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]]; |
681 #if 0 | 684 #if 0 |
682 text = [[event characters] UTF8String]; | 685 text = [[event characters] UTF8String]; |
683 if(text && *text) { | 686 if(text && *text) { |
684 SDL_SendKeyboardText(data->keyboard, text); | 687 SDL_SendKeyboardText(data->keyboard, text); |
704 { | 707 { |
705 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; | 708 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; |
706 NSAutoreleasePool *pool; | 709 NSAutoreleasePool *pool; |
707 | 710 |
708 SDL_DelKeyboard(data->keyboard); | 711 SDL_DelKeyboard(data->keyboard); |
709 | |
710 pool = [[NSAutoreleasePool alloc] init]; | |
711 [data->fieldEdit release]; | |
712 [pool release]; | |
713 } | 712 } |
714 | 713 |
715 /* vi: set ts=4 sw=4 expandtab: */ | 714 /* vi: set ts=4 sw=4 expandtab: */ |