| 1 |
// |
| 2 |
// RemapDevicePanelCotroller.m |
| 3 |
// |
| 4 |
// Created by Toshi Nagata. |
| 5 |
/* |
| 6 |
Copyright (c) 2000-2016 Toshi Nagata. All rights reserved. |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation version 2 of the License. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
*/ |
| 17 |
|
| 18 |
#import "RemapDevicePanelController.h" |
| 19 |
#import "MyMIDISequence.h" |
| 20 |
#import "MyDocument.h" |
| 21 |
#import "MDHeaders.h" |
| 22 |
|
| 23 |
@implementation RemapDevicePanelController |
| 24 |
|
| 25 |
- (id)initWithDocument: (MyDocument *)document trackSelection: (NSArray *)trackSelection |
| 26 |
{ |
| 27 |
self = [super initWithWindowNibName:@"RemapDevicePanel"]; |
| 28 |
myDocument = [document retain]; |
| 29 |
myTrackSelection = [trackSelection retain]; |
| 30 |
return self; |
| 31 |
} |
| 32 |
|
| 33 |
- (void)dealloc |
| 34 |
{ |
| 35 |
[deviceNumbers release]; |
| 36 |
[initialValues release]; |
| 37 |
[currentValues release]; |
| 38 |
[myTrackSelection release]; |
| 39 |
[myDocument release]; |
| 40 |
[super dealloc]; |
| 41 |
} |
| 42 |
|
| 43 |
- (void)updateComboBoxContent |
| 44 |
{ |
| 45 |
id cell; |
| 46 |
int i, j, n1, n2; |
| 47 |
id aname; |
| 48 |
NSArray *array = [myDocument getDestinationNames]; |
| 49 |
NSTableColumn *tableColumn = [myTableView tableColumnWithIdentifier:@"new"]; |
| 50 |
cell = [tableColumn dataCell]; |
| 51 |
if (cell == nil || ![cell isKindOfClass:[NSComboBoxCell class]]) { |
| 52 |
cell = [[[NSComboBoxCell alloc] init] autorelease]; |
| 53 |
[cell setEditable: YES]; |
| 54 |
[cell setCompletes: YES]; |
| 55 |
[tableColumn setDataCell:cell]; |
| 56 |
} |
| 57 |
n1 = MDPlayerGetNumberOfDestinations(); |
| 58 |
n2 = (int)[array count]; |
| 59 |
for (i = 0; i < n2; i++) { |
| 60 |
aname = [array objectAtIndex:i]; |
| 61 |
if (i > n1) { |
| 62 |
aname = [[[NSAttributedString alloc] initWithString:aname attributes: |
| 63 |
[NSDictionary dictionaryWithObjectsAndKeys: |
| 64 |
[NSColor redColor], NSForegroundColorAttributeName, nil]] autorelease]; |
| 65 |
} |
| 66 |
[cell addItemWithObjectValue:aname]; |
| 67 |
} |
| 68 |
n1 = (int)[currentValues count]; |
| 69 |
for (i = 0; i < n1; i++) { |
| 70 |
aname = [currentValues objectAtIndex:i]; |
| 71 |
if ([aname isKindOfClass:[NSAttributedString class]]) |
| 72 |
aname = [aname string]; |
| 73 |
for (j = 0; j < n2; j++) { |
| 74 |
id bname = [cell itemObjectValueAtIndex:j]; |
| 75 |
NSString *cname; |
| 76 |
if ([bname isKindOfClass:[NSAttributedString class]]) |
| 77 |
cname = [bname string]; |
| 78 |
else cname = bname; |
| 79 |
if ([aname isEqualToString:cname]) { |
| 80 |
[currentValues replaceObjectAtIndex:i withObject:bname]; |
| 81 |
break; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
- (void)windowDidLoad |
| 88 |
{ |
| 89 |
int i, j, n; |
| 90 |
NSEnumerator *en; |
| 91 |
id obj; |
| 92 |
|
| 93 |
/* Update the device information before starting the dialog */ |
| 94 |
MDPlayerReloadDeviceInformation(); |
| 95 |
|
| 96 |
if (myTrackSelection == nil) { |
| 97 |
NSMutableArray *array = [[NSMutableArray allocWithZone: [self zone]] init]; |
| 98 |
n = [[myDocument myMIDISequence] trackCount]; |
| 99 |
for (i = 0; i < n; i++) |
| 100 |
[array addObject: [NSNumber numberWithInt: i]]; |
| 101 |
myTrackSelection = array; |
| 102 |
} |
| 103 |
|
| 104 |
initialValues = [[NSMutableArray allocWithZone: [self zone]] init]; |
| 105 |
deviceNumbers = [[NSMutableArray allocWithZone: [self zone]] init]; |
| 106 |
en = [myTrackSelection objectEnumerator]; |
| 107 |
while ((obj = [en nextObject]) != nil) { |
| 108 |
NSString *name; |
| 109 |
i = [obj intValue]; |
| 110 |
name = [[myDocument myMIDISequence] deviceName: i]; |
| 111 |
if (name == nil) { |
| 112 |
[deviceNumbers addObject: [NSNumber numberWithInt: -1]]; |
| 113 |
continue; |
| 114 |
} |
| 115 |
for (j = (int)[initialValues count] - 1; j >= 0; j--) { |
| 116 |
if ([name isEqualToString: [initialValues objectAtIndex: j]]) |
| 117 |
break; |
| 118 |
} |
| 119 |
if (j == -1) { |
| 120 |
/* Not found: register this */ |
| 121 |
[initialValues addObject: name]; |
| 122 |
j = (int)[initialValues count] - 1; |
| 123 |
} |
| 124 |
[deviceNumbers addObject: [NSNumber numberWithInt: j]]; |
| 125 |
} |
| 126 |
currentValues = [[NSMutableArray allocWithZone: [self zone]] initWithArray: initialValues]; |
| 127 |
|
| 128 |
[self updateComboBoxContent]; |
| 129 |
} |
| 130 |
|
| 131 |
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo |
| 132 |
{ |
| 133 |
// NSLog(@"SheetDidEnd invoked with return code %d", returnCode); |
| 134 |
[[self window] close]; |
| 135 |
if (stopModalFlag) |
| 136 |
[[NSApplication sharedApplication] stopModalWithCode: returnCode]; |
| 137 |
} |
| 138 |
|
| 139 |
- (void)beginSheetForWindow: (NSWindow *)parentWindow invokeStopModalWhenDone: (BOOL)flag |
| 140 |
{ |
| 141 |
NSWindow *window = [self window]; |
| 142 |
stopModalFlag = flag; |
| 143 |
[[NSApplication sharedApplication] beginSheet: window |
| 144 |
modalForWindow: parentWindow |
| 145 |
modalDelegate: self |
| 146 |
didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) |
| 147 |
contextInfo: nil]; |
| 148 |
} |
| 149 |
|
| 150 |
- (IBAction)changeAction:(id)sender |
| 151 |
{ |
| 152 |
int i, j; |
| 153 |
NSEnumerator *en; |
| 154 |
id obj; |
| 155 |
en = [myTrackSelection objectEnumerator]; |
| 156 |
i = 0; |
| 157 |
while ((obj = [en nextObject]) != nil) { |
| 158 |
j = [[deviceNumbers objectAtIndex: i] intValue]; |
| 159 |
if (j >= 0 && j < [currentValues count]) { |
| 160 |
id str = [currentValues objectAtIndex: j]; |
| 161 |
if ([str isKindOfClass:[NSAttributedString class]]) |
| 162 |
str = [str string]; |
| 163 |
// int32_t deviceNumber = MDPlayerGetDestinationNumberFromName([str cString]); |
| 164 |
// [myDocument changeDeviceNumber: deviceNumber forTrack: [obj intValue]]; |
| 165 |
[myDocument changeDevice: str forTrack: [obj intValue]]; |
| 166 |
} |
| 167 |
i++; |
| 168 |
} |
| 169 |
[[NSApplication sharedApplication] endSheet: [self window] returnCode: 1]; |
| 170 |
// [self release]; |
| 171 |
} |
| 172 |
|
| 173 |
- (IBAction)dontChangeAction:(id)sender |
| 174 |
{ |
| 175 |
[[NSApplication sharedApplication] endSheet: [self window] returnCode: 0]; |
| 176 |
// [self release]; |
| 177 |
} |
| 178 |
|
| 179 |
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView |
| 180 |
{ |
| 181 |
return [initialValues count]; |
| 182 |
} |
| 183 |
|
| 184 |
- (id)tableView:(NSTableView *)aTableView |
| 185 |
objectValueForTableColumn:(NSTableColumn *)aTableColumn |
| 186 |
row:(NSInteger)rowIndex |
| 187 |
{ |
| 188 |
id identifier = [aTableColumn identifier]; |
| 189 |
if ([@"current" isEqualToString: identifier]) |
| 190 |
return [initialValues objectAtIndex: rowIndex]; |
| 191 |
else if ([@"new" isEqualToString: identifier]) |
| 192 |
return [currentValues objectAtIndex: rowIndex]; |
| 193 |
else return nil; |
| 194 |
} |
| 195 |
|
| 196 |
- (void)tableView:(NSTableView *)aTableView |
| 197 |
setObjectValue:(id)anObject |
| 198 |
forTableColumn:(NSTableColumn *)aTableColumn |
| 199 |
row:(NSInteger)rowIndex |
| 200 |
{ |
| 201 |
if ([@"new" isEqualToString: [aTableColumn identifier]]) { |
| 202 |
NSComboBoxCell *cell = [aTableColumn dataCell]; |
| 203 |
int i, n = (int)[cell numberOfItems]; |
| 204 |
if ([anObject isKindOfClass:[NSAttributedString class]]) |
| 205 |
anObject = [anObject string]; |
| 206 |
for (i = 0; i < n; i++) { |
| 207 |
id obj = [cell itemObjectValueAtIndex:i]; |
| 208 |
NSString *sobj; |
| 209 |
if ([obj isKindOfClass:[NSAttributedString class]]) |
| 210 |
sobj = [obj string]; |
| 211 |
else sobj = obj; |
| 212 |
if ([sobj isEqualToString:anObject]) { |
| 213 |
anObject = obj; |
| 214 |
break; |
| 215 |
} |
| 216 |
} |
| 217 |
if (i >= n) { |
| 218 |
// No match: we need to add a new item |
| 219 |
anObject = [[[NSAttributedString alloc] initWithString:anObject attributes: |
| 220 |
[NSDictionary dictionaryWithObjectsAndKeys: |
| 221 |
[NSColor redColor], NSForegroundColorAttributeName, nil]] autorelease]; |
| 222 |
[cell addItemWithObjectValue:anObject]; |
| 223 |
} |
| 224 |
if (rowIndex >= 0 && rowIndex < [currentValues count]) |
| 225 |
[currentValues replaceObjectAtIndex: rowIndex withObject: anObject]; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
@end |