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