comparison src/video/cocoa/SDL_cocoakeyboard.m @ 3135:f4e553ec6a62 gsoc2009_IME

Update API, first step: make it build
author Jiang Jiang <gzjjgod@gmail.com>
date Thu, 06 Aug 2009 08:56:48 +0000
parents 88861448961f
children 962357f325e1
comparison
equal deleted inserted replaced
3134:f896821736fb 3135:f4e553ec6a62
57 @interface SDLTranslatorResponder : NSTextView 57 @interface SDLTranslatorResponder : NSTextView
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;
63 int _keyboard;
63 } 64 }
64 - (void) doCommandBySelector:(SEL)myselector; 65 - (void) doCommandBySelector:(SEL)myselector;
65 - (void) setInputRect:(SDL_Rect *) rect; 66 - (void) setInputRect:(SDL_Rect *) rect;
67 - (void) setKeyboard:(int) keyboard;
66 @end 68 @end
67 69
68 @implementation SDLTranslatorResponder 70 @implementation SDLTranslatorResponder
69 71
72 - (void) setKeyboard:(int) keyboard
73 {
74 _keyboard = keyboard;
75 }
76
70 - (void) setInputRect:(SDL_Rect *) rect 77 - (void) setInputRect:(SDL_Rect *) rect
71 { 78 {
72 inputRect = *rect; 79 _inputRect = *rect;
73 } 80 }
74 81
75 - (void) insertText:(id) aString 82 - (void) insertText:(id) aString
76 { 83 {
77 const char *str; 84 const char *str;
83 if ([aString isKindOfClass: [NSAttributedString class]]) 90 if ([aString isKindOfClass: [NSAttributedString class]])
84 str = [[aString string] UTF8String]; 91 str = [[aString string] UTF8String];
85 else 92 else
86 str = [aString UTF8String]; 93 str = [aString UTF8String];
87 94
88 SDL_SendKeyboardText(0, str); 95 SDL_SendKeyboardText(_keyboard, str);
89 } 96 }
90 97
91 - (void) doCommandBySelector:(SEL) myselector 98 - (void) doCommandBySelector:(SEL) myselector
92 { 99 {
93 NSLog(@"doCommandBySelector, passed down"); 100 NSLog(@"doCommandBySelector, passed down");
143 } 150 }
144 151
145 - (NSRect) firstRectForCharacterRange: (NSRange) theRange 152 - (NSRect) firstRectForCharacterRange: (NSRange) theRange
146 { 153 {
147 float windowHeight = [[self window] frame].size.height; 154 float windowHeight = [[self window] frame].size.height;
148 NSRect rect = NSMakeRect(inputRect.x, windowHeight - inputRect.y, inputRect.w, inputRect.h); 155 NSRect rect = NSMakeRect(_inputRect.x, windowHeight - _inputRect.y, _inputRect.w, _inputRect.h);
149 156
150 NSLog(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@", 157 NSLog(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@",
151 theRange.location, theRange.length, windowHeight, 158 theRange.location, theRange.length, windowHeight,
152 NSStringFromRect(rect)); 159 NSStringFromRect(rect));
153 rect.origin = [[self window] convertBaseToScreen: rect.origin]; 160 rect.origin = [[self window] convertBaseToScreen: rect.origin];
583 data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)]; 590 data->fieldEdit = [[SDLTranslatorResponder alloc] initWithFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)];
584 [pool release]; 591 [pool release];
585 592
586 SDL_zero(keyboard); 593 SDL_zero(keyboard);
587 data->keyboard = SDL_AddKeyboard(&keyboard, -1); 594 data->keyboard = SDL_AddKeyboard(&keyboard, -1);
595 [data->fieldEdit setKeyboard: data->keyboard];
588 UpdateKeymap(data); 596 UpdateKeymap(data);
589 597
590 /* Set our own names for the platform-dependent but layout-independent keys */ 598 /* Set our own names for the platform-dependent but layout-independent keys */
591 /* This key is NumLock on the MacBook keyboard. :) */ 599 /* This key is NumLock on the MacBook keyboard. :) */
592 /*SDL_SetScancodeName(SDL_SCANCODE_NUMLOCKCLEAR, "Clear");*/ 600 /*SDL_SetScancodeName(SDL_SCANCODE_NUMLOCKCLEAR, "Clear");*/
595 SDL_SetScancodeName(SDL_SCANCODE_RALT, "Right Option"); 603 SDL_SetScancodeName(SDL_SCANCODE_RALT, "Right Option");
596 SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Command"); 604 SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Command");
597 } 605 }
598 606
599 void 607 void
600 Cocoa_StartTextInput(_THIS, SDL_Rect *rect) 608 Cocoa_StartTextInput(_THIS, SDL_Window *window)
609 {
610 SDL_VideoData *videoData = (SDL_VideoData *) _this->driverdata;
611 SDL_WindowData *windowData = (SDL_WindowData *) window->driverdata;
612 NSView *parentView = [windowData->window contentView];
613
614 if (! [[videoData->fieldEdit superview] isEqual: parentView])
615 {
616 NSLog(@"add fieldEdit to window contentView");
617 [videoData->fieldEdit removeFromSuperview];
618 [parentView addSubview: videoData->fieldEdit];
619 [windowData->window makeFirstResponder: videoData->fieldEdit];
620 }
621
622 SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
623 SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
624 }
625
626 void
627 Cocoa_SetTextInputRect(_THIS, SDL_Rect *rect)
601 { 628 {
602 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; 629 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
603 630
604 NSLog(@"StartTextInput: (%d, %d) (w=%d, h=%d)", rect->x, rect->y, rect->w, rect->h);
605 [data->fieldEdit setInputRect: rect]; 631 [data->fieldEdit setInputRect: rect];
632 }
633
634 void
635 Cocoa_StopTextInput(_THIS)
636 {
637 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
638
639 [data->fieldEdit removeFromSuperview];
640 SDL_EventState(SDL_TEXTINPUT, SDL_IGNORE);
641 SDL_EventState(SDL_TEXTEDITING, SDL_IGNORE);
606 } 642 }
607 643
608 void 644 void
609 Cocoa_HandleKeyEvent(_THIS, NSEvent *event) 645 Cocoa_HandleKeyEvent(_THIS, NSEvent *event)
610 { 646 {
639 #endif 675 #endif
640 } 676 }
641 if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) { 677 if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
642 /* 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. */ 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. */
643 NSLog(@"interpretKeyEvents"); 679 NSLog(@"interpretKeyEvents");
644 if (! [[data->fieldEdit superview] isEqual: [[event window] contentView]])
645 {
646 NSLog(@"add fieldEdit to window contentView");
647 [data->fieldEdit removeFromSuperview];
648 [[[event window] contentView] addSubview: data->fieldEdit];
649 [[event window] makeFirstResponder: data->fieldEdit];
650 }
651 [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]]; 680 [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]];
652 #if 0 681 #if 0
653 text = [[event characters] UTF8String]; 682 text = [[event characters] UTF8String];
654 if(text && *text) { 683 if(text && *text) {
655 SDL_SendKeyboardText(data->keyboard, text); 684 SDL_SendKeyboardText(data->keyboard, text);