| 1 |
masaki |
2 |
// |
| 2 |
masaki |
72 |
// XspfQTPlayListWindowController.m |
| 3 |
masaki |
2 |
// XspfQT |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 08/08/31. |
| 6 |
|
|
// Copyright 2008 masakih. All rights reserved. |
| 7 |
|
|
// |
| 8 |
|
|
|
| 9 |
masaki |
72 |
#import "XspfQTPlayListWindowController.h" |
| 10 |
|
|
#import "XspfQTDocument.h" |
| 11 |
|
|
#import "XspfQTComponent.h" |
| 12 |
masaki |
2 |
|
| 13 |
|
|
|
| 14 |
masaki |
72 |
@interface XspfQTPlayListWindowController(Private) |
| 15 |
masaki |
41 |
- (void)setObserveObject:(id)new; |
| 16 |
|
|
@end |
| 17 |
|
|
|
| 18 |
masaki |
72 |
@implementation XspfQTPlayListWindowController |
| 19 |
masaki |
2 |
|
| 20 |
masaki |
53 |
static NSString *const XspfQTPlayListItemType = @"XspfQTPlayListItemType"; |
| 21 |
|
|
|
| 22 |
masaki |
2 |
- (id)init |
| 23 |
|
|
{ |
| 24 |
masaki |
73 |
return [super initWithWindowNibName:@"XspfQTPlayList"]; |
| 25 |
masaki |
2 |
} |
| 26 |
|
|
|
| 27 |
|
|
- (void)awakeFromNib |
| 28 |
|
|
{ |
| 29 |
|
|
[listView setDoubleAction:@selector(changeCurrentTrack:)]; |
| 30 |
masaki |
41 |
[[self window] setReleasedWhenClosed:NO]; |
| 31 |
|
|
|
| 32 |
|
|
[trackListTree addObserver:self |
| 33 |
|
|
forKeyPath:@"selection" |
| 34 |
|
|
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld |
| 35 |
|
|
context:NULL]; |
| 36 |
|
|
[self setObserveObject:[trackListTree valueForKeyPath:@"selection.self"]]; |
| 37 |
masaki |
51 |
|
| 38 |
|
|
[listView expandItem:[listView itemAtRow:0]]; |
| 39 |
masaki |
53 |
|
| 40 |
masaki |
76 |
[listView registerForDraggedTypes:[NSArray arrayWithObject:XspfQTPlayListItemType]]; |
| 41 |
masaki |
2 |
} |
| 42 |
masaki |
41 |
- (void)dealloc |
| 43 |
|
|
{ |
| 44 |
|
|
[trackListTree removeObserver:self forKeyPath:@"selection"]; |
| 45 |
|
|
[self setObserveObject:nil]; |
| 46 |
|
|
|
| 47 |
|
|
[super dealloc]; |
| 48 |
|
|
} |
| 49 |
masaki |
2 |
|
| 50 |
|
|
- (IBAction)changeCurrentTrack:(id)sender |
| 51 |
|
|
{ |
| 52 |
|
|
id selections = [trackListTree selectedObjects]; |
| 53 |
|
|
if([selections count] == 0) return; |
| 54 |
|
|
|
| 55 |
|
|
NSIndexPath *selectionIndexPath = [trackListTree selectionIndexPath]; |
| 56 |
|
|
// NSLog(@"Selection %@", selectionIndexPath); |
| 57 |
|
|
// NSLog(@"Selection index %d", [selectionIndexPath indexAtPosition:1]); |
| 58 |
|
|
|
| 59 |
|
|
if([selectionIndexPath length] > 1) { |
| 60 |
|
|
[[self document] setPlayTrackindex:[selectionIndexPath indexAtPosition:1]]; |
| 61 |
|
|
} |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
masaki |
34 |
- (void)keyDown:(NSEvent *)theEvent |
| 65 |
|
|
{ |
| 66 |
|
|
if([theEvent isARepeat]) return; |
| 67 |
|
|
|
| 68 |
|
|
unsigned short code = [theEvent keyCode]; |
| 69 |
|
|
if(code == 49 /* space bar */) { |
| 70 |
|
|
[[self document] togglePlayAndPause:self]; |
| 71 |
|
|
} |
| 72 |
masaki |
83 |
if(code == 51 /* delete key */) { |
| 73 |
|
|
id selection = [trackListTree valueForKeyPath:@"selection.self"];// representedObject]; |
| 74 |
|
|
// NSLog(@"new item class is %@\n%@", NSStringFromClass([selection class]), selection); |
| 75 |
|
|
[[self document] removeItem:selection]; |
| 76 |
|
|
} |
| 77 |
masaki |
34 |
} |
| 78 |
|
|
|
| 79 |
masaki |
2 |
- (BOOL)windowShouldClose:(id)sender |
| 80 |
|
|
{ |
| 81 |
|
|
[sender orderOut:self]; |
| 82 |
|
|
|
| 83 |
|
|
return NO; |
| 84 |
|
|
} |
| 85 |
masaki |
41 |
- (void)setObserveObject:(id)new |
| 86 |
|
|
{ |
| 87 |
|
|
if(obseveObject == new) return; |
| 88 |
|
|
|
| 89 |
|
|
[obseveObject removeObserver:self forKeyPath:@"title"]; |
| 90 |
|
|
|
| 91 |
|
|
obseveObject = new; |
| 92 |
|
|
[obseveObject addObserver:self |
| 93 |
|
|
forKeyPath:@"title" |
| 94 |
|
|
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld |
| 95 |
|
|
context:NULL]; |
| 96 |
|
|
} |
| 97 |
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context |
| 98 |
|
|
{ |
| 99 |
masaki |
49 |
if([keyPath isEqualToString:@"selection"]) { |
| 100 |
masaki |
41 |
id new = [object valueForKeyPath:@"selection.self"]; |
| 101 |
|
|
[self setObserveObject:new]; |
| 102 |
|
|
} |
| 103 |
|
|
|
| 104 |
masaki |
49 |
if([keyPath isEqualToString:@"title"]) { |
| 105 |
masaki |
41 |
id new = [change objectForKey:NSKeyValueChangeNewKey]; |
| 106 |
|
|
id old = [change objectForKey:NSKeyValueChangeOldKey]; |
| 107 |
|
|
|
| 108 |
|
|
if(new == old) return; |
| 109 |
|
|
if([new isEqualTo:old]) return; |
| 110 |
|
|
|
| 111 |
|
|
id um = [[self document] undoManager]; |
| 112 |
|
|
[um registerUndoWithTarget:obseveObject |
| 113 |
|
|
selector:@selector(setTitle:) |
| 114 |
|
|
object:old]; |
| 115 |
|
|
} |
| 116 |
|
|
} |
| 117 |
masaki |
2 |
|
| 118 |
masaki |
53 |
|
| 119 |
|
|
- (BOOL)outlineView:(NSOutlineView *)outlineView |
| 120 |
|
|
writeItems:(NSArray *)items |
| 121 |
|
|
toPasteboard:(NSPasteboard *)pasteboard |
| 122 |
|
|
{ |
| 123 |
|
|
if([items count] > 1) return NO; |
| 124 |
|
|
|
| 125 |
|
|
id item = [[items objectAtIndex:0] representedObject]; |
| 126 |
|
|
|
| 127 |
masaki |
72 |
if(![item isKindOfClass:[XspfQTComponent class]]) { |
| 128 |
masaki |
53 |
NSLog(@"Ouch! %@", NSStringFromClass([item class])); |
| 129 |
|
|
return NO; |
| 130 |
|
|
} |
| 131 |
|
|
if(![item isLeaf]) return NO; |
| 132 |
|
|
|
| 133 |
|
|
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:item]; |
| 134 |
|
|
if(!data) { |
| 135 |
|
|
NSLog(@"Could not archive."); |
| 136 |
|
|
return NO; |
| 137 |
|
|
} |
| 138 |
|
|
|
| 139 |
|
|
[pasteboard declareTypes:[NSArray arrayWithObject:XspfQTPlayListItemType] |
| 140 |
|
|
owner:self]; |
| 141 |
|
|
[pasteboard setData:data |
| 142 |
|
|
forType:XspfQTPlayListItemType]; |
| 143 |
|
|
return YES; |
| 144 |
|
|
} |
| 145 |
|
|
- (NSDragOperation)outlineView:(NSOutlineView *)outlineView |
| 146 |
|
|
validateDrop:(id <NSDraggingInfo>)info |
| 147 |
|
|
proposedItem:(id)item |
| 148 |
|
|
proposedChildIndex:(NSInteger)index |
| 149 |
|
|
{ |
| 150 |
|
|
if([item isLeaf]) { |
| 151 |
|
|
return NSDragOperationNone; |
| 152 |
|
|
} |
| 153 |
|
|
|
| 154 |
|
|
id pb = [info draggingPasteboard]; |
| 155 |
|
|
|
| 156 |
|
|
if(![[pb types] containsObject:XspfQTPlayListItemType]) { |
| 157 |
|
|
return NSDragOperationNone; |
| 158 |
|
|
} |
| 159 |
|
|
|
| 160 |
masaki |
80 |
if(index == -1) return NSDragOperationNone; |
| 161 |
|
|
|
| 162 |
masaki |
53 |
return NSDragOperationMove; |
| 163 |
|
|
} |
| 164 |
|
|
- (BOOL)outlineView:(NSOutlineView *)outlineView |
| 165 |
|
|
acceptDrop:(id <NSDraggingInfo>)info |
| 166 |
|
|
item:(id)item |
| 167 |
|
|
childIndex:(NSInteger)index |
| 168 |
|
|
{ |
| 169 |
|
|
if([item isLeaf]) { |
| 170 |
|
|
return NO; |
| 171 |
|
|
} |
| 172 |
|
|
|
| 173 |
|
|
id pb = [info draggingPasteboard]; |
| 174 |
|
|
|
| 175 |
|
|
NSData *data = [pb dataForType:XspfQTPlayListItemType]; |
| 176 |
|
|
if(!data) return NO; |
| 177 |
|
|
|
| 178 |
|
|
id newItem = [NSKeyedUnarchiver unarchiveObjectWithData:data]; |
| 179 |
|
|
if(!newItem) return NO; |
| 180 |
|
|
|
| 181 |
masaki |
59 |
// NSLog(@"new item class is %@\n%@", NSStringFromClass([newItem class]), newItem); |
| 182 |
masaki |
80 |
id doc = [self document]; |
| 183 |
|
|
NSInteger oldIndex = [[doc trackList] indexOfChild:newItem]; |
| 184 |
masaki |
53 |
|
| 185 |
masaki |
80 |
if(oldIndex == index) return YES; |
| 186 |
|
|
if(oldIndex < index) { |
| 187 |
|
|
index--; |
| 188 |
|
|
} |
| 189 |
|
|
|
| 190 |
|
|
// change archive to original. |
| 191 |
|
|
newItem = [[doc trackList] childAtIndex:oldIndex]; |
| 192 |
|
|
|
| 193 |
|
|
BOOL mustSelectionChange = NO; |
| 194 |
|
|
if([newItem isSelected]) { |
| 195 |
|
|
mustSelectionChange = YES; |
| 196 |
|
|
} |
| 197 |
|
|
|
| 198 |
masaki |
83 |
id undo = [doc undoManager]; |
| 199 |
|
|
[undo beginUndoGrouping]; |
| 200 |
|
|
{ |
| 201 |
|
|
if(mustSelectionChange) { |
| 202 |
|
|
[[undo prepareWithInvocationTarget:doc] setPlayTrackindex:oldIndex]; |
| 203 |
|
|
} |
| 204 |
|
|
[doc removeItem:newItem]; |
| 205 |
|
|
[doc insertItem:newItem atIndex:index]; |
| 206 |
|
|
} |
| 207 |
|
|
[undo endUndoGrouping]; |
| 208 |
masaki |
80 |
|
| 209 |
|
|
if(mustSelectionChange) { |
| 210 |
|
|
[doc setPlayTrackindex:index]; |
| 211 |
|
|
} |
| 212 |
|
|
|
| 213 |
masaki |
59 |
return YES; |
| 214 |
masaki |
53 |
} |
| 215 |
|
|
|
| 216 |
masaki |
2 |
@end |
| 217 |
masaki |
34 |
|
| 218 |
masaki |
72 |
@implementation XspfQTThrowSpacebarKeyDownOutlineView |
| 219 |
masaki |
34 |
- (void)keyDown:(NSEvent *)theEvent |
| 220 |
|
|
{ |
| 221 |
masaki |
83 |
if(_delegate && [_delegate respondsToSelector:@selector(keyDown:)]) { |
| 222 |
|
|
[_delegate keyDown:theEvent]; |
| 223 |
masaki |
34 |
} |
| 224 |
|
|
|
| 225 |
|
|
[super keyDown:theEvent]; |
| 226 |
|
|
} |
| 227 |
|
|
@end |