-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRidmikInputController.m
197 lines (140 loc) · 5.64 KB
/
RidmikInputController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#import "RidmikInputController.h"
#import "ConversionEngine.h"
#import "ApplicationDelegate.h"
@implementation RidmikInputController
-(BOOL) inputText:(NSString *)string key:(NSInteger)keyCode modifiers:(NSUInteger)flags client:(id)sender
{
// Return YES to indicate the the key input was received and dealt with. Key processing will not continue in that case.
// In other words the system will not deliver a key down event to the application.
// Returning NO means the original key down will be passed on to the client.
if (flags & NSCommandKeyMask){
return NO;
}
BOOL inputHandled = NO;
// NSLog(@"Input:%@ key:%ld modifiers:%lx", string, keyCode, flags);
NSRange range = [string rangeOfString:@"^[a-zA-Z]+$" options: NSRegularExpressionSearch];
if(range.location != NSNotFound){
[self originalBufferAppend:string client:sender];
inputHandled = YES;
return YES;
}
NSRange r2 = [string rangeOfString:@"^[0-9|:]$" options: NSRegularExpressionSearch];
if (r2.location != NSNotFound){
[self commitComposition: sender];
[self setComposedBuffer: [[[NSApp delegate] conversionEngine] convertOther:string]];
[self commitComposition: sender];
return YES;
}
// If the input isn't part of a decimal number see if we need to convert the previously input text.
inputHandled = [self convert:string key:keyCode client:sender];
//NSLog(@"Before inputtext finish");
return inputHandled;
}
/*!
@method
@abstract Called when a user action was taken that ends an input session. Typically triggered by the user selecting a new input method or keyboard layout.
@discussion When this method is called your controller should send the current input buffer to the client via a call to insertText:replacementRange:. Additionally, this is the time to clean up if that is necessary.
*/
-(void)commitComposition:(id)sender
{
NSString* text = [self composedBuffer];
if ( text == nil || [text length] == 0 ) {
text = [self originalBuffer];
// NSLog(@"composed buffer empty");
}
[sender insertText:text replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
[self setComposedBuffer:@""];
[self setOriginalBuffer:@""];
_insertionIndex = 0;
_didConvert = NO;
}
// Return the composed buffer. If it is NIL create it.
-(NSMutableString*)composedBuffer;
{
if ( _composedBuffer == nil ) {
_composedBuffer = [[NSMutableString alloc] init];
}
return _composedBuffer;
}
// Change the composed buffer.
-(void)setComposedBuffer:(NSString*)string
{
NSMutableString* buffer = [self composedBuffer];
[buffer setString:string];
}
// Get the original buffer.
-(NSMutableString*)originalBuffer
{
if ( _originalBuffer == nil ) {
_originalBuffer = [[NSMutableString alloc] init];
}
return _originalBuffer;
}
// Add newly input text to the original buffer.
-(void)originalBufferAppend:(NSString*)string client:(id)sender
{
NSMutableString* buffer = [self originalBuffer];
[buffer appendString: string];
//NSLog(@"Final Buffer: %@", buffer);
NSString* news = [[[NSApp delegate] conversionEngine] convert:buffer];
[self setComposedBuffer:news];
_insertionIndex++;
NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0], NSMarkedClauseSegmentAttributeName,
[NSNumber numberWithInt:NSUnderlineStyleNone], NSUnderlineStyleAttributeName,
nil];
NSMutableAttributedString *attrString = [[[NSMutableAttributedString alloc] initWithString:news attributes:attrDict] autorelease];
[sender setMarkedText:attrString selectionRange:NSMakeRange(0, [news length]) replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
}
// Change the original buffer.
-(void)setOriginalBuffer:(NSString*)string
{
NSMutableString* buffer = [self originalBuffer];
[buffer setString:string];
}
- (void)deleteBackward:(id)sender
{
NSMutableString* originalText = [self originalBuffer];
NSString* convertedString;
if ( _insertionIndex > 0 && _insertionIndex <= [originalText length] ) {
--_insertionIndex;
[originalText deleteCharactersInRange:NSMakeRange(_insertionIndex,1)];
convertedString = [[[NSApp delegate] conversionEngine] convert:originalText];
[self setComposedBuffer:convertedString];
[sender setMarkedText:convertedString selectionRange:NSMakeRange(_insertionIndex, 0) replacementRange:NSMakeRange(NSNotFound,NSNotFound)];
}
}
- (BOOL)convert:(NSString*)trigger key:(NSInteger)keyCode client:(id)sender
{
NSString* originalText = [self originalBuffer];
NSString* convertedString = [self composedBuffer];
BOOL handled = NO;
if(keyCode == 51 && [originalText length] > 0){
[self deleteBackward:sender];
return YES;
}
convertedString = [[[NSApp delegate] conversionEngine] convert:originalText];
[self setComposedBuffer:convertedString];
[self commitComposition:sender];
handled = NO;
return handled;
}
-(void)setValue:(id)value forTag:(long)tag client:(id)sender
{
//NSLog(@"setvalll llllllll");
}
- (NSUInteger)recognizedEvents:(id)sender
{
return NSKeyDownMask | NSFlagsChangedMask | NSLeftMouseDownMask | NSRightMouseDownMask | NSLeftMouseDraggedMask | NSRightMouseDraggedMask;
}
- (BOOL)mouseDownOnCharacterIndex:(NSUInteger)index coordinate:(NSPoint)point withModifier:(NSUInteger)flags continueTracking:(BOOL *)keepTracking client:(id)sender
{
return NO;
}
-(void)dealloc
{
[_composedBuffer release];
[_originalBuffer release];
[super dealloc];
}
@end