[34] | 1 | //
|
---|
| 2 | // NJRQTMediaPopUpButton.m
|
---|
| 3 | // Pester
|
---|
| 4 | //
|
---|
| 5 | // Created by Nicholas Riley on Sat Oct 26 2002.
|
---|
| 6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
| 7 | //
|
---|
| 8 |
|
---|
| 9 | #import "NJRQTMediaPopUpButton.h"
|
---|
| 10 | #import "SoundFileManager.h"
|
---|
| 11 | #import "NSMovie-NJRExtensions.h"
|
---|
| 12 | #import "NSImage-NJRExtensions.h"
|
---|
| 13 |
|
---|
[39] | 14 | static const int NJRQTMediaPopUpButtonMaxRecentItems = 10;
|
---|
| 15 |
|
---|
[41] | 16 | NSString * const NJRQTMediaPopUpButtonMovieChangedNotification = @"NJRQTMediaPopUpButtonMovieChangedNotification";
|
---|
| 17 |
|
---|
[34] | 18 | @interface NJRQTMediaPopUpButton (Private)
|
---|
[39] | 19 | - (void)_setPath:(NSString *)path;
|
---|
[40] | 20 | - (NSMenuItem *)_itemForAlias:(BDAlias *)alias;
|
---|
[41] | 21 | - (BOOL)_validateWithPreview:(BOOL)doPreview;
|
---|
[34] | 22 | @end
|
---|
| 23 |
|
---|
| 24 | @implementation NJRQTMediaPopUpButton
|
---|
| 25 |
|
---|
| 26 | // XXX handle refreshing sound list on resume
|
---|
[39] | 27 | // XXX don't add icons on Puma, they look like ass
|
---|
| 28 | // XXX launch preview on a separate thread (if movies take too long to load, they inhibit the interface responsiveness)
|
---|
[34] | 29 |
|
---|
[40] | 30 | // Recent media layout:
|
---|
| 31 | // Most recent media are at TOP of menu (smaller item numbers, starting at [self indexOfItem: otherItem] + 1)
|
---|
| 32 | // Most recent media are at END of array (larger indices)
|
---|
| 33 |
|
---|
[41] | 34 | #pragma mark recently selected media tracking
|
---|
| 35 |
|
---|
[39] | 36 | - (NSString *)_defaultKey;
|
---|
| 37 | {
|
---|
| 38 | NSAssert([self tag] != 0, @"CanÕt track recently selected media for popup with tag 0: please set a tag");
|
---|
| 39 | return [NSString stringWithFormat: @"NJRQTMediaPopUpButtonMaxRecentItems tag %d", [self tag]];
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | - (void)_writeRecentMedia;
|
---|
| 43 | {
|
---|
| 44 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
---|
| 45 | [defaults setObject: recentMediaAliasData forKey: [self _defaultKey]];
|
---|
| 46 | [defaults synchronize];
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | - (NSMenuItem *)_addRecentMediaAtPath:(NSString *)path withAlias:(BDAlias *)alias;
|
---|
| 50 | {
|
---|
| 51 | NSString *title = [[NSFileManager defaultManager] displayNameAtPath: path];
|
---|
| 52 | NSMenu *menu = [self menu];
|
---|
[47] | 53 | NSMenuItem *item;
|
---|
| 54 | if (title == nil || path == nil) return nil;
|
---|
| 55 | item = [menu insertItemWithTitle: title action: @selector(_aliasSelected:) keyEquivalent: @"" atIndex: [menu indexOfItem: otherItem] + 1];
|
---|
[39] | 56 | [item setTarget: self];
|
---|
| 57 | [item setRepresentedObject: alias];
|
---|
| 58 | [item setImage: [[[NSWorkspace sharedWorkspace] iconForFile: path] bestFitImageForSize: NSMakeSize(16, 16)]];
|
---|
| 59 | [recentMediaAliasData addObject: [alias aliasData]];
|
---|
| 60 | if ([recentMediaAliasData count] > NJRQTMediaPopUpButtonMaxRecentItems) {
|
---|
| 61 | [menu removeItemAtIndex: [menu numberOfItems] - 1];
|
---|
| 62 | [recentMediaAliasData removeObjectAtIndex: 0];
|
---|
| 63 | }
|
---|
| 64 | return item;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | - (void)_addRecentMediaFromAliasesData:(NSArray *)aliasesData;
|
---|
| 68 | {
|
---|
| 69 | NSEnumerator *e = [aliasesData objectEnumerator];
|
---|
| 70 | NSData *aliasData;
|
---|
| 71 | BDAlias *alias;
|
---|
| 72 | while ( (aliasData = [e nextObject]) != nil) {
|
---|
| 73 | if ( (alias = [[BDAlias alloc] initWithData: aliasData]) != nil) {
|
---|
| 74 | [self _addRecentMediaAtPath: [alias fullPath] withAlias: alias];
|
---|
| 75 | [alias release];
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | - (void)_validateRecentMedia;
|
---|
| 81 | {
|
---|
[40] | 82 | NSEnumerator *e = [recentMediaAliasData reverseObjectEnumerator];
|
---|
[39] | 83 | NSData *aliasData;
|
---|
| 84 | NSMenuItem *item;
|
---|
| 85 | BDAlias *itemAlias;
|
---|
| 86 | int otherIndex = [self indexOfItem: otherItem];
|
---|
| 87 | int aliasDataCount = [recentMediaAliasData count];
|
---|
| 88 | int lastItemIndex = [self numberOfItems] - 1;
|
---|
| 89 | int recentItemCount = lastItemIndex - otherIndex;
|
---|
| 90 | int recentItemIndex = otherIndex;
|
---|
| 91 | NSAssert2(recentItemCount == aliasDataCount, @"Counted %d recent menu items, %d of alias data", recentItemCount, aliasDataCount);
|
---|
[40] | 92 | while ( (aliasData = [e nextObject]) != nil) { // go BACKWARD through array while going DOWN menu
|
---|
[39] | 93 | recentItemIndex++;
|
---|
| 94 | item = [self itemAtIndex: recentItemIndex];
|
---|
| 95 | itemAlias = [item representedObject];
|
---|
[40] | 96 | if ([itemAlias aliasDataIsEqual: aliasData])
|
---|
[39] | 97 | NSLog(@"item %d %@: %@", recentItemIndex, [item title], [itemAlias fullPath]);
|
---|
| 98 | else
|
---|
| 99 | NSLog(@"ITEM %d %@: %@ != aliasData %@", recentItemIndex, [item title], [itemAlias fullPath], [[BDAlias aliasWithData: aliasData] fullPath]);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[41] | 103 | #pragma mark initialize-release
|
---|
| 104 |
|
---|
[34] | 105 | - (void)awakeFromNib;
|
---|
| 106 | {
|
---|
| 107 | NSMenu *menu;
|
---|
| 108 | NSMenuItem *item;
|
---|
| 109 | SoundFileManager *sfm = [SoundFileManager sharedSoundFileManager];
|
---|
| 110 | int soundCount = [sfm count];
|
---|
[39] | 111 |
|
---|
[34] | 112 | [self removeAllItems];
|
---|
| 113 | menu = [self menu];
|
---|
| 114 | item = [menu addItemWithTitle: @"Alert sound" action: @selector(_beepSelected:) keyEquivalent: @""];
|
---|
| 115 | [item setTarget: self];
|
---|
| 116 | [menu addItem: [NSMenuItem separatorItem]];
|
---|
| 117 | if (soundCount == 0) {
|
---|
| 118 | item = [menu addItemWithTitle: @"CanÕt locate alert sounds" action: nil keyEquivalent: @""];
|
---|
| 119 | [item setEnabled: NO];
|
---|
| 120 | } else {
|
---|
| 121 | SoundFile *sf;
|
---|
| 122 | int i;
|
---|
| 123 | [sfm sortByName];
|
---|
| 124 | for (i = 0 ; i < soundCount ; i++) {
|
---|
| 125 | sf = [sfm soundFileAtIndex: i];
|
---|
| 126 | item = [menu addItemWithTitle: [sf name] action: @selector(_soundFileSelected:) keyEquivalent: @""];
|
---|
| 127 | [item setTarget: self];
|
---|
| 128 | [item setRepresentedObject: sf];
|
---|
| 129 | [item setImage: [[[NSWorkspace sharedWorkspace] iconForFile: [sf path]] bestFitImageForSize: NSMakeSize(16, 16)]];
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | [menu addItem: [NSMenuItem separatorItem]];
|
---|
| 133 | item = [menu addItemWithTitle: @"OtherÉ" action: @selector(select:) keyEquivalent: @""];
|
---|
| 134 | [item setTarget: self];
|
---|
[39] | 135 | otherItem = [item retain];
|
---|
| 136 |
|
---|
[41] | 137 | [self _validateWithPreview: NO];
|
---|
| 138 |
|
---|
[39] | 139 | recentMediaAliasData = [[NSMutableArray alloc] initWithCapacity: NJRQTMediaPopUpButtonMaxRecentItems + 1];
|
---|
| 140 | [self _addRecentMediaFromAliasesData: [[NSUserDefaults standardUserDefaults] arrayForKey: [self _defaultKey]]];
|
---|
[41] | 141 | // [self _validateRecentMedia];
|
---|
[39] | 142 |
|
---|
| 143 | [self registerForDraggedTypes:
|
---|
| 144 | [NSArray arrayWithObjects: NSFilenamesPboardType, NSURLPboardType, nil]];
|
---|
[34] | 145 | }
|
---|
| 146 |
|
---|
[39] | 147 | - (void)dealloc;
|
---|
[34] | 148 | {
|
---|
[39] | 149 | [recentMediaAliasData release]; recentMediaAliasData = nil;
|
---|
| 150 | [otherItem release];
|
---|
| 151 | [selectedAlias release]; [previousAlias release];
|
---|
| 152 | [super dealloc];
|
---|
[34] | 153 | }
|
---|
| 154 |
|
---|
[41] | 155 | #pragma mark accessing
|
---|
| 156 |
|
---|
[39] | 157 | - (BDAlias *)selectedAlias;
|
---|
[34] | 158 | {
|
---|
[39] | 159 | return selectedAlias;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | - (void)_setAlias:(BDAlias *)alias;
|
---|
| 163 | {
|
---|
| 164 | BDAlias *oldAlias = [selectedAlias retain];
|
---|
| 165 | [previousAlias release];
|
---|
| 166 | previousAlias = oldAlias;
|
---|
[34] | 167 | if (selectedAlias != alias) {
|
---|
| 168 | [selectedAlias release];
|
---|
| 169 | selectedAlias = [alias retain];
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[39] | 173 | - (void)_setPath:(NSString *)path;
|
---|
[34] | 174 | {
|
---|
[39] | 175 | [self _setAlias: [BDAlias aliasWithPath: path]];
|
---|
[34] | 176 | }
|
---|
| 177 |
|
---|
[39] | 178 | - (NSMenuItem *)_itemForAlias:(BDAlias *)alias;
|
---|
[34] | 179 | {
|
---|
[39] | 180 | NSString *path;
|
---|
| 181 | SoundFile *sf;
|
---|
| 182 | if (alias == nil) {
|
---|
| 183 | return [self itemAtIndex: 0];
|
---|
| 184 | }
|
---|
[34] | 185 |
|
---|
[41] | 186 | // [self _validateRecentMedia];
|
---|
[39] | 187 | path = [alias fullPath];
|
---|
| 188 | sf = [[SoundFileManager sharedSoundFileManager] soundFileFromPath: path];
|
---|
[41] | 189 | // NSLog(@"_itemForAlias: %@", path);
|
---|
[34] | 190 |
|
---|
[39] | 191 | // selected a system sound?
|
---|
| 192 | if (sf != nil) {
|
---|
[41] | 193 | // NSLog(@"_itemForAlias: selected system sound");
|
---|
[39] | 194 | return [self itemAtIndex: [self indexOfItemWithRepresentedObject: sf]];
|
---|
| 195 | } else {
|
---|
[40] | 196 | NSEnumerator *e = [recentMediaAliasData reverseObjectEnumerator];
|
---|
[39] | 197 | NSData *aliasData;
|
---|
| 198 | NSMenuItem *item;
|
---|
[40] | 199 | int recentIndex = 1;
|
---|
[34] | 200 |
|
---|
[39] | 201 | while ( (aliasData = [e nextObject]) != nil) {
|
---|
| 202 | // selected a recently selected, non-system sound?
|
---|
| 203 | if ([alias aliasDataIsEqual: aliasData]) {
|
---|
| 204 | int otherIndex = [self indexOfItem: otherItem];
|
---|
[40] | 205 | int menuIndex = recentIndex + otherIndex;
|
---|
[39] | 206 | if (menuIndex == otherIndex + 1) return [self itemAtIndex: menuIndex]; // already at top
|
---|
| 207 | // remove item, add (at top) later
|
---|
[41] | 208 | // NSLog(@"_itemForAlias removing item: idx %d + otherItemIdx %d + 1 = %d [%@]", recentIndex, otherIndex, menuIndex, [self itemAtIndex: menuIndex]);
|
---|
[39] | 209 | [self removeItemAtIndex: menuIndex];
|
---|
[40] | 210 | [recentMediaAliasData removeObjectAtIndex: [recentMediaAliasData count] - recentIndex];
|
---|
[39] | 211 | break;
|
---|
[34] | 212 | }
|
---|
[39] | 213 | recentIndex++;
|
---|
[34] | 214 | }
|
---|
[39] | 215 |
|
---|
| 216 | // create the item
|
---|
| 217 | item = [self _addRecentMediaAtPath: path withAlias: alias];
|
---|
| 218 | [self _writeRecentMedia];
|
---|
| 219 | return item;
|
---|
[34] | 220 | }
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[41] | 223 | - (BOOL)canRepeat;
|
---|
| 224 | {
|
---|
| 225 | return movieCanRepeat;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | #pragma mark selected media validation
|
---|
| 229 |
|
---|
[39] | 230 | - (void)_invalidateSelection;
|
---|
[34] | 231 | {
|
---|
[39] | 232 | [self _setAlias: previousAlias];
|
---|
| 233 | [self selectItem: [self _itemForAlias: [self selectedAlias]]];
|
---|
[41] | 234 | [[NSNotificationCenter defaultCenter] postNotificationName: NJRQTMediaPopUpButtonMovieChangedNotification object: self];
|
---|
[34] | 235 | }
|
---|
| 236 |
|
---|
[41] | 237 | - (BOOL)_validateWithPreview:(BOOL)doPreview;
|
---|
[34] | 238 | {
|
---|
| 239 | [preview stop: self];
|
---|
| 240 | if (selectedAlias == nil) {
|
---|
| 241 | [preview setMovie: nil];
|
---|
[41] | 242 | movieCanRepeat = YES;
|
---|
| 243 | if (doPreview) NSBeep();
|
---|
[34] | 244 | } else {
|
---|
| 245 | NSMovie *movie = [[NSMovie alloc] initWithURL: [NSURL fileURLWithPath: [selectedAlias fullPath]] byReference: YES];
|
---|
[41] | 246 | movieCanRepeat = ![movie isStatic];
|
---|
[34] | 247 | if ([movie hasAudio])
|
---|
| 248 | [preview setMovie: movie];
|
---|
| 249 | else {
|
---|
| 250 | [preview setMovie: nil];
|
---|
| 251 | if (movie == nil) {
|
---|
| 252 | NSBeginAlertSheet(@"Format not recognized", @"OK", nil, nil, [self window], nil, nil, nil, nil, @"The item you selected isnÕt a sound or movie recognized by QuickTime. Please select a different item.");
|
---|
[39] | 253 | [self _invalidateSelection];
|
---|
[34] | 254 | return NO;
|
---|
| 255 | }
|
---|
| 256 | if (![movie hasAudio] && ![movie hasVideo]) {
|
---|
| 257 | NSBeginAlertSheet(@"No video or audio", @"OK", nil, nil, [self window], nil, nil, nil, nil, @"Ò%@Ó contains neither audio nor video content playable by QuickTime. Please select a different item.", [[NSFileManager defaultManager] displayNameAtPath: [selectedAlias fullPath]]);
|
---|
[39] | 258 | [self _invalidateSelection];
|
---|
[34] | 259 | [movie release];
|
---|
| 260 | return NO;
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 | [movie release];
|
---|
| 264 | [preview start: self];
|
---|
| 265 | }
|
---|
[41] | 266 | [[NSNotificationCenter defaultCenter] postNotificationName: NJRQTMediaPopUpButtonMovieChangedNotification object: self];
|
---|
[34] | 267 | return YES;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[41] | 270 | #pragma mark actions
|
---|
| 271 |
|
---|
[39] | 272 | - (IBAction)stopSoundPreview:(id)sender;
|
---|
| 273 | {
|
---|
| 274 | [preview stop: self];
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | - (void)_beepSelected:(NSMenuItem *)sender;
|
---|
| 278 | {
|
---|
| 279 | [self _setAlias: nil];
|
---|
[41] | 280 | [self _validateWithPreview: YES];
|
---|
[39] | 281 | }
|
---|
| 282 |
|
---|
| 283 | - (void)_soundFileSelected:(NSMenuItem *)sender;
|
---|
| 284 | {
|
---|
| 285 | [self _setPath: [(SoundFile *)[sender representedObject] path]];
|
---|
[41] | 286 | if (![self _validateWithPreview: YES]) {
|
---|
[39] | 287 | [[self menu] removeItem: sender];
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | - (void)_aliasSelected:(NSMenuItem *)sender;
|
---|
| 292 | {
|
---|
| 293 | BDAlias *alias = [sender representedObject];
|
---|
| 294 | int index = [self indexOfItem: sender], otherIndex = [self indexOfItem: otherItem];
|
---|
| 295 | [self _setAlias: alias];
|
---|
[41] | 296 | if (![self _validateWithPreview: YES]) {
|
---|
[39] | 297 | [[self menu] removeItem: sender];
|
---|
| 298 | } else if (index > otherIndex + 1) { // move "other" item to top of list
|
---|
[40] | 299 | int recentIndex = [recentMediaAliasData count] - index + otherIndex;
|
---|
[39] | 300 | NSMenuItem *item = [[self itemAtIndex: index] retain];
|
---|
| 301 | NSData *data = [[recentMediaAliasData objectAtIndex: recentIndex] retain];
|
---|
[45] | 302 | // [self _validateRecentMedia];
|
---|
[39] | 303 | [self removeItemAtIndex: index];
|
---|
| 304 | [[self menu] insertItem: item atIndex: otherIndex + 1];
|
---|
| 305 | [self selectItem: item];
|
---|
| 306 | [item release];
|
---|
| 307 | NSAssert(recentIndex >= 0, @"Recent media index invalid");
|
---|
[41] | 308 | // NSLog(@"_aliasSelected removing item %d - %d + %d = %d of recentMediaAliasData", [recentMediaAliasData count], index, otherIndex, recentIndex);
|
---|
[39] | 309 | [recentMediaAliasData removeObjectAtIndex: recentIndex];
|
---|
| 310 | [recentMediaAliasData addObject: data];
|
---|
[40] | 311 | [self _validateRecentMedia];
|
---|
[39] | 312 | [data release];
|
---|
[40] | 313 | } // else NSLog(@"_aliasSelected ...already at top");
|
---|
[39] | 314 | }
|
---|
| 315 |
|
---|
| 316 | - (IBAction)select:(id)sender;
|
---|
| 317 | {
|
---|
| 318 | NSOpenPanel *openPanel = [NSOpenPanel openPanel];
|
---|
| 319 | NSString *path = [selectedAlias fullPath];
|
---|
| 320 | [openPanel setAllowsMultipleSelection: NO];
|
---|
| 321 | [openPanel setCanChooseDirectories: NO];
|
---|
| 322 | [openPanel setCanChooseFiles: YES];
|
---|
| 323 | [openPanel beginSheetForDirectory: [path stringByDeletingLastPathComponent]
|
---|
| 324 | file: [path lastPathComponent]
|
---|
| 325 | types: nil // XXX fix for QuickTime!
|
---|
| 326 | modalForWindow: [self window]
|
---|
| 327 | modalDelegate: self
|
---|
| 328 | didEndSelector: @selector(openPanelDidEnd:returnCode:contextInfo:)
|
---|
| 329 | contextInfo: nil];
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | - (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
|
---|
| 333 | {
|
---|
| 334 | [sheet close];
|
---|
| 335 |
|
---|
| 336 | if (returnCode == NSOKButton) {
|
---|
| 337 | NSArray *files = [sheet filenames];
|
---|
| 338 | NSAssert1([files count] == 1, @"%d items returned, only one expected", [files count]);
|
---|
| 339 | [self _setPath: [files objectAtIndex: 0]];
|
---|
[41] | 340 | if ([self _validateWithPreview: YES]) {
|
---|
[39] | 341 | [self selectItem: [self _itemForAlias: selectedAlias]];
|
---|
| 342 | }
|
---|
| 343 | } else {
|
---|
| 344 | // "Other..." item is still selected, revert to previously selected item
|
---|
| 345 | // XXX issue with cancelling, top item in recent menu is sometimes duplicated!?
|
---|
| 346 | [self selectItem: [self _itemForAlias: selectedAlias]];
|
---|
| 347 | }
|
---|
| 348 | // [self _validateRecentMedia];
|
---|
| 349 | }
|
---|
| 350 |
|
---|
[43] | 351 | - (void)setEnabled:(BOOL)flag;
|
---|
| 352 | {
|
---|
| 353 | [super setEnabled: flag];
|
---|
| 354 | if (flag) ; // XXX [self startSoundPreview: self]; // need to prohibit at startup
|
---|
| 355 | else [self stopSoundPreview: self];
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[41] | 358 | #pragma mark drag feedback
|
---|
| 359 |
|
---|
| 360 | - (void)drawRect:(NSRect)rect;
|
---|
| 361 | {
|
---|
| 362 | if (dragAccepted) {
|
---|
| 363 | NSWindow *window = [self window];
|
---|
| 364 | NSRect boundsRect = [self bounds];
|
---|
| 365 | BOOL isFirstResponder = ([window firstResponder] == self);
|
---|
| 366 | // focus ring and drag feedback interfere with one another
|
---|
| 367 | if (isFirstResponder) [window makeFirstResponder: window];
|
---|
| 368 | [super drawRect: rect];
|
---|
| 369 | [[NSColor selectedControlColor] set];
|
---|
| 370 | NSFrameRectWithWidthUsingOperation(NSInsetRect(boundsRect, 2, 2), 3, NSCompositeSourceIn);
|
---|
| 371 | if (isFirstResponder) [window makeFirstResponder: self];
|
---|
| 372 | } else {
|
---|
| 373 | [super drawRect: rect];
|
---|
| 374 | }
|
---|
| 375 | }
|
---|
| 376 |
|
---|
[34] | 377 | @end
|
---|
[39] | 378 |
|
---|
| 379 | @implementation NJRQTMediaPopUpButton (NSDraggingDestination)
|
---|
| 380 |
|
---|
| 381 | - (BOOL)acceptsDragFrom:(id <NSDraggingInfo>)sender;
|
---|
| 382 | {
|
---|
| 383 | NSURL *url = [NSURL URLFromPasteboard: [sender draggingPasteboard]];
|
---|
[40] | 384 | NSFileManager *fm = [NSFileManager defaultManager];
|
---|
| 385 | BOOL isDir;
|
---|
[39] | 386 |
|
---|
| 387 | if (url == nil || ![url isFileURL]) return NO;
|
---|
[40] | 388 |
|
---|
| 389 | if (![fm fileExistsAtPath: [url path] isDirectory: &isDir]) return NO;
|
---|
| 390 |
|
---|
| 391 | if (isDir) return NO;
|
---|
| 392 |
|
---|
[39] | 393 | return YES;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
[40] | 396 | - (NSString *)_descriptionForDraggingInfo:(id <NSDraggingInfo>)sender;
|
---|
| 397 | {
|
---|
| 398 | NSDragOperation mask = [sender draggingSourceOperationMask];
|
---|
| 399 | NSMutableString *s = [NSMutableString stringWithFormat: @"Drag seq %d source: %@",
|
---|
| 400 | [sender draggingSequenceNumber], [sender draggingSource]];
|
---|
| 401 | NSPasteboard *draggingPasteboard = [sender draggingPasteboard];
|
---|
| 402 | NSArray *types = [draggingPasteboard types];
|
---|
| 403 | NSEnumerator *e = [types objectEnumerator];
|
---|
| 404 | NSString *type;
|
---|
| 405 | [s appendString: @"\nDrag operations:"];
|
---|
| 406 | if (mask & NSDragOperationCopy) [s appendString: @" copy"];
|
---|
| 407 | if (mask & NSDragOperationLink) [s appendString: @" link"];
|
---|
| 408 | if (mask & NSDragOperationGeneric) [s appendString: @" generic"];
|
---|
| 409 | if (mask & NSDragOperationPrivate) [s appendString: @" private"];
|
---|
| 410 | if (mask & NSDragOperationMove) [s appendString: @" move"];
|
---|
| 411 | if (mask & NSDragOperationDelete) [s appendString: @" delete"];
|
---|
| 412 | if (mask & NSDragOperationEvery) [s appendString: @" every"];
|
---|
| 413 | if (mask & NSDragOperationNone) [s appendString: @" none"];
|
---|
| 414 | [s appendFormat: @"\nImage: %@ at %@", [sender draggedImage],
|
---|
| 415 | NSStringFromPoint([sender draggedImageLocation])];
|
---|
| 416 | [s appendFormat: @"\nDestination: %@ at %@", [sender draggingDestinationWindow],
|
---|
| 417 | NSStringFromPoint([sender draggingLocation])];
|
---|
| 418 | [s appendFormat: @"\nPasteboard: %@ types:", draggingPasteboard];
|
---|
| 419 | while ( (type = [e nextObject]) != nil) {
|
---|
| 420 | if ([type hasPrefix: @"CorePasteboardFlavorType 0x"]) {
|
---|
| 421 | const char *osTypeHex = [[type substringFromIndex: [type rangeOfString: @"0x" options: NSBackwardsSearch].location] lossyCString];
|
---|
| 422 | OSType osType;
|
---|
| 423 | sscanf(osTypeHex, "%lx", &osType);
|
---|
| 424 | [s appendFormat: @" '%4s'", &osType];
|
---|
| 425 | } else {
|
---|
| 426 | [s appendFormat: @" \"%@\"", type];
|
---|
| 427 | }
|
---|
| 428 | }
|
---|
| 429 | return s;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
[39] | 432 | - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender;
|
---|
| 433 | {
|
---|
| 434 | if ([self acceptsDragFrom: sender] && [sender draggingSourceOperationMask] &
|
---|
[40] | 435 | (NSDragOperationCopy | NSDragOperationLink)) {
|
---|
[39] | 436 | dragAccepted = YES;
|
---|
| 437 | [self setNeedsDisplay: YES];
|
---|
[40] | 438 | // NSLog(@"draggingEntered accept:\n%@", [self _descriptionForDraggingInfo: sender]);
|
---|
| 439 | return NSDragOperationLink;
|
---|
[39] | 440 | }
|
---|
| 441 | return NSDragOperationNone;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | - (void)draggingExited:(id <NSDraggingInfo>)sender;
|
---|
| 445 | {
|
---|
| 446 | dragAccepted = NO;
|
---|
| 447 | [self setNeedsDisplay: YES];
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender;
|
---|
| 451 | {
|
---|
| 452 | dragAccepted = NO;
|
---|
| 453 | [self setNeedsDisplay: YES];
|
---|
| 454 | return [self acceptsDragFrom: sender];
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
|
---|
| 458 | {
|
---|
| 459 | if ([sender draggingSource] != self) {
|
---|
| 460 | NSURL *url = [NSURL URLFromPasteboard: [sender draggingPasteboard]];
|
---|
| 461 | if (url == nil) return NO;
|
---|
| 462 | [self _setPath: [url path]];
|
---|
[41] | 463 | if ([self _validateWithPreview: YES]) {
|
---|
[40] | 464 | [self selectItem: [self _itemForAlias: selectedAlias]];
|
---|
| 465 | }
|
---|
[39] | 466 | }
|
---|
| 467 | return YES;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | @end
|
---|