forked from flit/MidiKeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyMapManager.m
110 lines (95 loc) · 2.27 KB
/
KeyMapManager.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
//
// KeyMapManager.mm
// MidiKeys
//
// Created by Chris Reed on Sat Oct 26 2002.
// Copyright (c) 2002 Chris Reed. All rights reserved.
//
#import "KeyMapManager.h"
#import "MidiKeyMap.h"
@implementation KeyMapManager
static KeyMapManager *sharedInstance = nil;
+ sharedInstance
{
return sharedInstance ? sharedInstance : [[self alloc] init];
}
// always return the shared instance
- init
{
if (sharedInstance)
{
[self release];
}
else if (self = [super init])
{
sharedInstance = self;
}
return sharedInstance;
}
// disallow disposing of the shared instance
- (void)dealloc
{
if (self == sharedInstance)
return;
[mPlist release];
[super dealloc];
}
// read the plist file and find all the keymaps
- (void)awakeFromNib
{
// read the map from the the definition file
NSString *plistPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"KeyMaps" ofType:@"plist"];
mPlist = [[NSDictionary dictionaryWithContentsOfFile:plistPath] retain];
if (!mPlist)
{
mPlist = [[NSDictionary dictionary] retain];
}
}
// return the names sorted alphabetically
- (NSArray *)allKeyMapNames
{
return [[mPlist allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
}
- (NSArray *)allKeyMapLocalisedNames
{
NSArray *names = [self allKeyMapNames];
id iterator;
NSMutableArray *localisedNames = [NSMutableArray array];
for (iterator in names)
{
[localisedNames addObject:[self localisedNameForKeyMapWithName:iterator]];
}
return [localisedNames sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
}
- (id)keyMapDefinitionWithName:(NSString *)name
{
if (!name)
return nil;
return [mPlist objectForKey:name];
}
- (MidiKeyMap *)keyMapWithName:(NSString *)name
{
if (!name)
return nil;
NSArray *def = [self keyMapDefinitionWithName:name];
if (!def)
return nil;
return [[[MidiKeyMap alloc] initWithDefinition:def] autorelease];
}
- (NSString *)localisedNameForKeyMapWithName:(NSString *)name
{
return NSLocalizedStringFromTable(name, @"KeyMapNames", nil);
}
- (NSString *)nameForKeyMapWithLocalisedName:(NSString *)localName
{
NSArray *names = [self allKeyMapNames];
id iterator;
for (iterator in names)
{
if ([localName isEqualToString:[self localisedNameForKeyMapWithName:iterator]])
return iterator;
}
// not found
return nil;
}
@end