From 561317beecbe4dd445b0c3cb067326828ea5566a Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Wed, 27 Feb 2013 02:20:33 +0100 Subject: [PATCH] Change KeyInputTextView from a TextView to a TextField. Simplifies the nib contents to remove surrounding NSScrollView. Refactor to simplify drawing and clean up properties. --- English.lproj/MainMenu.xib | 258 +++++++++------------------ KeyInputTextView.h | 23 +-- KeyInputTextView.m | 350 +++++++++++++++++++------------------ TargetController.m | 2 +- 4 files changed, 266 insertions(+), 367 deletions(-) diff --git a/English.lproj/MainMenu.xib b/English.lproj/MainMenu.xib index a4efa46..7ba1177 100644 --- a/English.lproj/MainMenu.xib +++ b/English.lproj/MainMenu.xib @@ -32,7 +32,6 @@ NSTableView NSTextField NSTextFieldCell - NSTextView NSToolbar NSToolbarFlexibleSpaceItem NSToolbarItem @@ -717,7 +716,7 @@ {{20, 16}, {201, 236}} - + NO 7 1 @@ -1077,126 +1076,6 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA - - - 8460 - - - - 2304 - - - - 2322 - {172, 20} - - - - - - - - - - - - - - - 38 - - - - 172 - 1 - - - 2320 - 0 - - - - - 6 - System - selectedTextBackgroundColor - - - - 6 - System - selectedTextColor - - - - - - - 1 - MCAwIDEAA - - - - - - 0 - - 6 - {463, 10000000} - - - - {{2, 2}, {172, 20}} - - - - - - - {1, -1} - 0 - - 4 - - - - -2147483392 - {{-100, -100}, {15, 133}} - - - - NO - - _doScroller: - 0.73888889999999996 - - - - -2147483392 - {{-100, -100}, {87, 18}} - - - - NO - 1 - - _doScroller: - 1 - 0.94565220000000005 - - - {{229, 191}, {176, 24}} - - - - 133123 - - - - 0.25 - 4 - 1 - 268 @@ -1283,6 +1162,15 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 0 NO + + + 265 + {{229, 190}, {176, 24}} + + + _NS:9 + KeyInputTextView + {{211, 0}, {429, 300}} @@ -1503,6 +1391,29 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA TargetController + + + 268 + {82, 32} + + _NS:9 + YES + + 67108864 + 134217728 + Button + + _NS:9 + + -2038284288 + 129 + + + 200 + 25 + + NO + @@ -1786,30 +1697,6 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 696 - - - window - - - - 684 - - - - targetController - - - - 688 - - - - keyInput - - - - 689 - radioKey @@ -1938,6 +1825,30 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 757 + + + keyInput + + + + 781 + + + + targetController + + + + 779 + + + + window + + + + 780 + @@ -2343,8 +2254,8 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA - + @@ -2353,31 +2264,6 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA - - 680 - - - - - - - - - - 681 - - - - - 682 - - - - - 683 - - - 656 @@ -2517,6 +2403,24 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA + + 765 + + + + + + + + 766 + + + + + 778 + + + @@ -2583,11 +2487,6 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - KeyInputTextView - com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -2610,6 +2509,9 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -2618,7 +2520,7 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA - 764 + 781 diff --git a/KeyInputTextView.h b/KeyInputTextView.h index e327c78..9927385 100644 --- a/KeyInputTextView.h +++ b/KeyInputTextView.h @@ -1,5 +1,5 @@ // -// KeyInputTextField.h +// KeyInputTextView.h // Enjoy // // Created by Sam McCall on 5/05/09. @@ -7,22 +7,17 @@ // #import -@class TargetController; -@interface KeyInputTextView: NSTextView { - IBOutlet NSWindow* window; - IBOutlet TargetController* targetController; - BOOL hasKey; - int vk; - NSString* descr; - BOOL enabled; +@interface KeyInputTextView : NSTextField { + IBOutlet NSWindow *window; + IBOutlet TargetController *targetController; } -@property(readonly) BOOL hasKey; -@property(readwrite) int vk; -@property(copy) NSString* descr; -@property(readwrite) BOOL enabled; +@property (copy) NSString* descr; +@property (assign) int vk; +@property (readonly) BOOL hasKey; +@property (assign) BOOL enabled; --(void) clear; +- (void)clear; @end diff --git a/KeyInputTextView.m b/KeyInputTextView.m index 9b6bc37..288d903 100644 --- a/KeyInputTextView.m +++ b/KeyInputTextView.m @@ -1,208 +1,210 @@ // -// KeyInputTextField.m +// KeyInputTextView.m // Enjoy // // Created by Sam McCall on 5/05/09. // -@implementation KeyInputTextView +#import "KeyInputTextView.h" -@synthesize descr, hasKey; +@implementation KeyInputTextView { + int vk; + BOOL enabled; +} + +@synthesize descr; --(id) init { - if(self = [super init]) { - setEnabled: NO; - } - return self; +- (id)initWithFrame:(NSRect)frameRect { + if ((self = [super initWithFrame:frameRect])) { + self.alignment = NSCenterTextAlignment; + [self setEditable:NO]; + [self setSelectable:NO]; + } + return self; } --(void) clear { - [self setString: [NSString string]]; - vk = -1; - hasKey = NO; - descr = NULL; +- (void)clear { + self.vk = -1; } --(NSString*) stringForKeyCode: (int) keycode { - switch(keycode) { - case 0x7a : return @"F1"; - case 0x78 : return @"F2"; - case 0x63 : return @"F3"; - case 0x76 : return @"F4"; - case 0x60 : return @"F5"; - case 0x61 : return @"F6"; - case 0x62 : return @"F7"; - case 0x64 : return @"F8"; - case 0x65 : return @"F9"; - case 0x6d : return @"F10"; - case 0x67 : return @"F11"; - case 0x6f : return @"F12"; - case 0x69 : return @"F13"; - case 0x6b : return @"F14"; - case 0x71 : return @"F15"; - case 0x6a : return @"F16"; - case 0x40 : return @"F17"; - case 0x4f : return @"F18"; - case 0x50 : return @"F19"; - - case 0x35 : return @"Esc"; - case 0x32 : return @"`"; - - case 0x12 : return @"1"; - case 0x13 : return @"2"; - case 0x14 : return @"3"; - case 0x15 : return @"4"; - case 0x17 : return @"5"; - case 0x16 : return @"6"; - case 0x1a : return @"7"; - case 0x1c : return @"8"; - case 0x19 : return @"9"; - case 0x1d : return @"0"; - case 0x1b : return @"-"; - case 0x18 : return @"="; - - case 0x3f : return @"Fn"; - case 0x39 : return @"Caps Lock"; - case 0x38 : return @"Left Shift"; - case 0x3b : return @"Left Control"; - case 0x3a : return @"Left Option"; - case 0x37 : return @"Left Command"; - case 0x36 : return @"Right Command"; - case 0x3d : return @"Right Option"; - case 0x3e : return @"Right Control"; - case 0x3c : return @"Right Shift"; - - case 0x73 : return @"Home"; - case 0x74 : return @"Page Up"; - case 0x75 : return @"Delete"; - case 0x77 : return @"End"; - case 0x79 : return @"Page Down"; - - case 0x30 : return @"Tab"; - case 0x33 : return @"Backspace"; - case 0x24 : return @"Return"; - case 0x31 : return @"Space"; - - case 0x0c : return @"Q"; - case 0x0d : return @"W"; - case 0x0e : return @"E"; - case 0x0f : return @"R"; - case 0x11 : return @"T"; - case 0x10 : return @"Y"; - case 0x20 : return @"U"; - case 0x22 : return @"I"; - case 0x1f : return @"O"; - case 0x23 : return @"P"; - case 0x21 : return @"["; - case 0x1e : return @"]"; - case 0x2a : return @"\\"; - case 0x00 : return @"A"; - case 0x01 : return @"S"; - case 0x02 : return @"D"; - case 0x03 : return @"F"; - case 0x05 : return @"G"; - case 0x04 : return @"H"; - case 0x26 : return @"J"; - case 0x28 : return @"K"; - case 0x25 : return @"L"; - case 0x29 : return @";"; - case 0x27 : return @"'"; - case 0x06 : return @"Z"; - case 0x07 : return @"X"; - case 0x08 : return @"C"; - case 0x09 : return @"V"; - case 0x0b : return @"B"; - case 0x2d : return @"N"; - case 0x2e : return @"M"; - case 0x2b : return @","; - case 0x2f : return @"."; - case 0x2c : return @"/"; - - case 0x47 : return @"Clear"; - case 0x51 : return @"Keypad ="; - case 0x4b : return @"Keypad /"; - case 0x43 : return @"Keypad *"; - case 0x59 : return @"Keypad 7"; - case 0x5b : return @"Keypad 8"; - case 0x5c : return @"Keypad 9"; - case 0x4e : return @"Keypad -"; - case 0x56 : return @"Keypad 4"; - case 0x57 : return @"Keypad 5"; - case 0x58 : return @"Keypad 6"; - case 0x45 : return @"Keypad +"; - case 0x53 : return @"Keypad 1"; - case 0x54 : return @"Keypad 2"; - case 0x55 : return @"Keypad 3"; - case 0x52 : return @"Keypad 0"; - case 0x41 : return @"Keypad ."; - case 0x4c : return @"Enter"; - - case 0x7e : return @"Up"; - case 0x7d : return @"Down"; - case 0x7b : return @"Left"; - case 0x7c : return @"Right"; - } - return [[NSString alloc] initWithFormat: @"Key 0x%x",keycode]; +- (BOOL)hasKey { + return self.vk >= 0; } --(BOOL) acceptsFirstResponder { - return enabled; +- (NSString *)stringForKeyCode:(int)keycode { + switch(keycode) { + case -1: return @""; + case 0x7a: return @"F1"; + case 0x78: return @"F2"; + case 0x63: return @"F3"; + case 0x76: return @"F4"; + case 0x60: return @"F5"; + case 0x61: return @"F6"; + case 0x62: return @"F7"; + case 0x64: return @"F8"; + case 0x65: return @"F9"; + case 0x6d: return @"F10"; + case 0x67: return @"F11"; + case 0x6f: return @"F12"; + case 0x69: return @"F13"; + case 0x6b: return @"F14"; + case 0x71: return @"F15"; + case 0x6a: return @"F16"; + case 0x40: return @"F17"; + case 0x4f: return @"F18"; + case 0x50: return @"F19"; + + case 0x35: return @"Esc"; + case 0x32: return @"`"; + + case 0x12: return @"1"; + case 0x13: return @"2"; + case 0x14: return @"3"; + case 0x15: return @"4"; + case 0x17: return @"5"; + case 0x16: return @"6"; + case 0x1a: return @"7"; + case 0x1c: return @"8"; + case 0x19: return @"9"; + case 0x1d: return @"0"; + case 0x1b: return @"-"; + case 0x18: return @"="; + + case 0x3f: return @"Fn"; + case 0x39: return @"Caps Lock"; + case 0x38: return @"Left Shift"; + case 0x3b: return @"Left Control"; + case 0x3a: return @"Left Option"; + case 0x37: return @"Left Command"; + case 0x36: return @"Right Command"; + case 0x3d: return @"Right Option"; + case 0x3e: return @"Right Control"; + case 0x3c: return @"Right Shift"; + + case 0x73: return @"Home"; + case 0x74: return @"Page Up"; + case 0x75: return @"Delete"; + case 0x77: return @"End"; + case 0x79: return @"Page Down"; + + case 0x30: return @"Tab"; + case 0x33: return @"Backspace"; + case 0x24: return @"Return"; + case 0x31: return @"Space"; + + case 0x0c: return @"Q"; + case 0x0d: return @"W"; + case 0x0e: return @"E"; + case 0x0f: return @"R"; + case 0x11: return @"T"; + case 0x10: return @"Y"; + case 0x20: return @"U"; + case 0x22: return @"I"; + case 0x1f: return @"O"; + case 0x23: return @"P"; + case 0x21: return @"["; + case 0x1e: return @"]"; + case 0x2a: return @"\\"; + case 0x00: return @"A"; + case 0x01: return @"S"; + case 0x02: return @"D"; + case 0x03: return @"F"; + case 0x05: return @"G"; + case 0x04: return @"H"; + case 0x26: return @"J"; + case 0x28: return @"K"; + case 0x25: return @"L"; + case 0x29: return @";"; + case 0x27: return @"'"; + case 0x06: return @"Z"; + case 0x07: return @"X"; + case 0x08: return @"C"; + case 0x09: return @"V"; + case 0x0b: return @"B"; + case 0x2d: return @"N"; + case 0x2e: return @"M"; + case 0x2b: return @","; + case 0x2f: return @"."; + case 0x2c: return @"/"; + + case 0x47: return @"Clear"; + case 0x51: return @"Keypad ="; + case 0x4b: return @"Keypad /"; + case 0x43: return @"Keypad *"; + case 0x59: return @"Keypad 7"; + case 0x5b: return @"Keypad 8"; + case 0x5c: return @"Keypad 9"; + case 0x4e: return @"Keypad -"; + case 0x56: return @"Keypad 4"; + case 0x57: return @"Keypad 5"; + case 0x58: return @"Keypad 6"; + case 0x45: return @"Keypad +"; + case 0x53: return @"Keypad 1"; + case 0x54: return @"Keypad 2"; + case 0x55: return @"Keypad 3"; + case 0x52: return @"Keypad 0"; + case 0x41: return @"Keypad ."; + case 0x4c: return @"Enter"; + + case 0x7e: return @"Up"; + case 0x7d: return @"Down"; + case 0x7b: return @"Left"; + case 0x7c: return @"Right"; + } + return [[NSString alloc] initWithFormat: @"Key 0x%x",keycode]; } --(BOOL) becomeFirstResponder { - [self setBackgroundColor: [NSColor selectedTextBackgroundColor]]; - return YES; +- (BOOL)acceptsFirstResponder { + return enabled; } --(BOOL) resignFirstResponder { - [self setBackgroundColor: [NSColor textBackgroundColor]]; - return YES; +- (BOOL)becomeFirstResponder { + [self setBackgroundColor:[NSColor selectedTextBackgroundColor]]; + return [super becomeFirstResponder]; } --(void) pressed:(int) keycode { - [self setVk: keycode]; - [[self window] makeFirstResponder: nil]; - [targetController keyChanged]; +- (BOOL)resignFirstResponder { + [self setBackgroundColor:[NSColor textBackgroundColor]]; + return [super resignFirstResponder]; } --(void) setVk: (int) key { - vk=key; - hasKey = YES; - descr = [self stringForKeyCode: key]; - [self setString: descr]; +- (void)setBackgroundColor:(NSColor *)color { + [super setBackgroundColor:color]; } --(int) vk { - return vk; + +- (void)setVk:(int)key { + vk = key; + descr = [self stringForKeyCode:key]; + [self setStringValue:descr]; + if (self.hasKey) + [targetController keyChanged]; +} + +- (int)vk { + return vk; } - (void)keyDown:(NSEvent *)evt { - if([evt isARepeat]) - return; - [self pressed: [evt keyCode]]; + if (!evt.isARepeat) { + self.vk = evt.keyCode; + [[self window] makeFirstResponder:nil]; + } } --(void) flagsChanged:(NSEvent*)evt { - // XXX sometimes it's key up - [self pressed: [evt keyCode]]; +- (void)flagsChanged:(NSEvent *)evt { + self.vk = evt.keyCode; + [[self window] makeFirstResponder:nil]; } --(void) setEnabled: (BOOL) newEnabled { - enabled = newEnabled; - if(!newEnabled && [window firstResponder] == self) - [window makeFirstResponder: NULL]; - - - if(enabled) { - if([window firstResponder] == self) - [self setBackgroundColor: [NSColor selectedTextBackgroundColor]]; - else - [self setBackgroundColor: [NSColor textBackgroundColor]]; - } else { - [self setBackgroundColor: [NSColor textBackgroundColor]]; - } +- (void)setEnabled:(BOOL)newEnabled { + enabled = newEnabled; + + if (!enabled && window.firstResponder == self) + [window makeFirstResponder:nil]; } --(BOOL) enabled { - return enabled; + +- (BOOL)enabled { + return enabled; } diff --git a/TargetController.m b/TargetController.m index 9f85fd9..82eec03 100644 --- a/TargetController.m +++ b/TargetController.m @@ -37,7 +37,7 @@ case 0: // none return NULL; case 1: // key - if([keyInput hasKey]) { + if(keyInput.hasKey) { TargetKeyboard* k = [[TargetKeyboard alloc] init]; [k setVk: [keyInput vk]]; [k setDescr: [keyInput descr]]; -- 2.30.2