cog/Playlist/PlaylistView.m

313 lines
8.0 KiB
Matlab
Raw Normal View History

2005-06-02 18:16:43 +00:00
//
// PlaylistView.m
// Cog
//
// Created by Vincent Spader on 3/20/05.
2005-07-02 21:02:06 +00:00
// Copyright 2005 Vincent Spader All rights reserved.
2005-06-02 18:16:43 +00:00
//
#import "PlaylistView.h"
2006-01-20 15:22:03 +00:00
#import "PlaybackController.h"
2005-06-02 18:16:43 +00:00
#import "PlaylistController.h"
#import "IndexFormatter.h"
#import "SecondsFormatter.h"
#import "BlankZeroFormatter.h"
#import "PlaylistEntry.h"
#import "CogAudio/Status.h"
2005-06-02 18:16:43 +00:00
@implementation PlaylistView
2006-04-28 23:19:14 +00:00
- (void)awakeFromNib
{
[[self menu] setAutoenablesItems:NO];
// Configure bindings to scale font size and row height
NSControlSize s = NSSmallControlSize;
2006-04-28 23:19:14 +00:00
NSFont *f = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:s]];
// NSFont *bf = [[NSFontManager sharedFontManager] convertFont:f toHaveTrait:NSBoldFontMask];
2008-03-01 15:04:46 +00:00
for (NSTableColumn *col in [self tableColumns]) {
[[col dataCell] setControlSize:s];
[[col dataCell] setFont:f];
2006-04-28 23:19:14 +00:00
}
2007-03-14 03:16:37 +00:00
//Set up formatters
NSFormatter *secondsFormatter = [[SecondsFormatter alloc] init];
[[[self tableColumnWithIdentifier:@"length"] dataCell] setFormatter:secondsFormatter];
[secondsFormatter release];
NSFormatter *indexFormatter = [[IndexFormatter alloc] init];
[[[self tableColumnWithIdentifier:@"index"] dataCell] setFormatter:indexFormatter];
[indexFormatter release];
NSFormatter *blankZeroFormatter = [[BlankZeroFormatter alloc] init];
[[[self tableColumnWithIdentifier:@"track"] dataCell] setFormatter:blankZeroFormatter];
[[[self tableColumnWithIdentifier:@"year"] dataCell] setFormatter:blankZeroFormatter];
[blankZeroFormatter release];
2007-03-14 03:16:37 +00:00
//end setting up formatters
2006-04-29 00:03:28 +00:00
[self setVerticalMotionCanBeginDrag:YES];
2007-03-14 03:16:37 +00:00
//Set up header context menu
headerContextMenu = [[NSMenu alloc] initWithTitle:@"Playlist Header Context Menu"];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"identifier" ascending:YES];
2007-03-14 03:16:37 +00:00
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[sortDescriptor release];
int visibleTableColumns = 0;
2007-03-14 03:16:37 +00:00
int menuIndex = 0;
for (NSTableColumn *col in [[self tableColumns] sortedArrayUsingDescriptors: sortDescriptors])
{
NSString *title;
if ([[col identifier] isEqualToString:@"status"])
{
title = @"Status";
}
else if ([[col identifier] isEqualToString:@"index"])
{
title = @"Index";
}
else
{
title = [[col headerCell] title];
}
NSMenuItem *contextMenuItem = [headerContextMenu insertItemWithTitle:title action:@selector(toggleColumn:) keyEquivalent:@"" atIndex:menuIndex];
2007-03-14 03:16:37 +00:00
[contextMenuItem setTarget:self];
[contextMenuItem setRepresentedObject:col];
2008-01-24 01:44:24 +00:00
[contextMenuItem setState:([col isHidden] ? NSOffState : NSOnState)];
2007-03-14 03:16:37 +00:00
visibleTableColumns += ![col isHidden];
2007-03-14 03:16:37 +00:00
menuIndex++;
}
if (visibleTableColumns == 0) {
for (NSTableColumn *col in [self tableColumns]) {
[col setHidden:NO];
}
}
2007-03-14 03:16:37 +00:00
[[self headerView] setMenu:headerContextMenu];
2006-06-19 00:39:41 +00:00
}
- (IBAction)toggleColumn:(id)sender
2006-06-19 00:39:41 +00:00
{
id tc = [sender representedObject];
if ([sender state] == NSOffState)
{
[sender setState:NSOnState];
2006-06-19 00:39:41 +00:00
2008-01-24 01:44:24 +00:00
[tc setHidden: NO];
}
else
{
[sender setState:NSOffState];
2008-01-24 01:44:24 +00:00
[tc setHidden: YES];
}
2006-06-19 00:39:41 +00:00
}
2005-06-02 18:16:43 +00:00
- (BOOL)acceptsFirstResponder
{
return YES;
}
2005-06-02 18:16:43 +00:00
- (BOOL)resignFirstResponder
{
return YES;
}
2005-06-30 17:46:07 +00:00
- (BOOL)acceptsFirstMouse:(NSEvent *)mouseDownEvent
{
return NO;
}
2005-06-02 18:16:43 +00:00
- (void)mouseDown:(NSEvent *)e
{
[super mouseDown:e];
if ([e type] == NSLeftMouseDown && [e clickCount] == 2 && [[self selectedRowIndexes] count] == 1)
2005-06-02 18:16:43 +00:00
{
2006-01-20 15:22:03 +00:00
[playbackController play:self];
2005-06-02 18:16:43 +00:00
}
}
// enables right-click selection for "Show in Finder" contextual menu
-(NSMenu*)menuForEvent:(NSEvent*)event
{
//Find which row is under the cursor
[[self window] makeFirstResponder:self];
NSPoint menuPoint = [self convertPoint:[event locationInWindow] fromView:nil];
NSInteger iRow = [self rowAtPoint:menuPoint];
NSMenu* tableViewMenu = [[self menu] copy];
/* Update the table selection before showing menu
Preserves the selection if the row under the mouse is selected (to allow for
multiple items to be selected), otherwise selects the row under the mouse */
BOOL currentRowIsSelected = [[self selectedRowIndexes] containsIndex:iRow];
if (!currentRowIsSelected) {
if (iRow == -1)
{
[self deselectAll:self];
}
else
{
[self selectRow:iRow byExtendingSelection:NO];
}
}
2007-07-11 01:20:32 +00:00
if ([self numberOfSelectedRows] <=0)
{
//No rows are selected, so the table should be displayed with all items disabled
int i;
for (i=0;i<[tableViewMenu numberOfItems];i++) {
[[tableViewMenu itemAtIndex:i] setEnabled:NO];
}
}
else
{
// Add Spotlight search items
PlaylistEntry *song = [[playlistController arrangedObjects]objectAtIndex:iRow];
NSString *artist = [song artist];
NSString *album = [song album];
unsigned addedItems = 0; // Count the number of added items, used for separator
if(album)
{
NSMenuItem *albumMenuItem = [NSMenuItem alloc];
NSString *title = [NSString
stringWithFormat:@"Search for Songs from %@...", album];
[albumMenuItem initWithTitle:title
action:@selector(searchByAlbum:)
keyEquivalent:@""];
albumMenuItem.target = playlistController;
[tableViewMenu insertItem:albumMenuItem atIndex:[tableViewMenu numberOfItems]];
[albumMenuItem release];
addedItems++;
}
if(artist)
{
NSMenuItem *artistMenuItem = [NSMenuItem alloc];
NSString *title = [NSString
stringWithFormat:@"Search for Songs by %@...", artist];
[artistMenuItem initWithTitle:title
action:@selector(searchByArtist:)
keyEquivalent:@""];
artistMenuItem.target = playlistController;
[tableViewMenu insertItem:artistMenuItem atIndex:[tableViewMenu numberOfItems]];
[artistMenuItem release];
addedItems++;
}
}
return [tableViewMenu autorelease];
}
2005-06-02 18:16:43 +00:00
- (void)keyDown:(NSEvent *)e
{
unsigned int modifiers = [e modifierFlags] & (NSCommandKeyMask | NSShiftKeyMask | NSControlKeyMask | NSAlternateKeyMask);
NSString *characters = [e characters];
unichar c;
2007-07-11 01:20:32 +00:00
if ([characters length] != 1)
{
[super keyDown:e];
2005-06-02 18:16:43 +00:00
return;
}
c = [characters characterAtIndex:0];
if (modifiers == 0 && (c == NSDeleteCharacter || c == NSBackspaceCharacter || c == NSDeleteFunctionKey))
2005-06-02 18:16:43 +00:00
{
[playlistController remove:self];
}
else if (modifiers == 0 && c == ' ')
2005-06-02 18:16:43 +00:00
{
2006-01-20 15:22:03 +00:00
[playbackController playPauseResume:self];
2005-06-02 18:16:43 +00:00
}
else if (modifiers == 0 && (c == NSEnterCharacter || c == NSCarriageReturnCharacter))
2005-06-02 18:16:43 +00:00
{
2006-01-20 15:22:03 +00:00
[playbackController play:self];
2005-06-02 18:16:43 +00:00
}
// Escape
else if (modifiers == 0 && c == 0x1b)
{
[playlistController clearFilterPredicate:self];
}
2005-06-02 18:16:43 +00:00
else
{
[super keyDown:e];
}
}
- (IBAction)scrollToCurrentEntry:(id)sender
{
[self scrollRowToVisible:[[playlistController currentEntry] index]];
[self selectRow:[[playlistController currentEntry] index] byExtendingSelection:NO];
}
2006-05-29 23:03:58 +00:00
- (IBAction)sortByPath:(id)sender
{
[self setSortDescriptors:nil];
[playlistController sortByPath];
}
- (IBAction)shufflePlaylist:(id)sender
{
[self setSortDescriptors:nil];
[playlistController randomizeList];
}
- (IBAction)undo:(id)sender
{
[[playlistController undoManager] undo];
}
- (IBAction)redo:(id)sender
{
[[playlistController undoManager] redo];
}
-(BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
{
SEL action = [anItem action];
if (action == @selector(undo:))
{
if ([[playlistController undoManager] canUndo])
return YES;
else
return NO;
}
if (action == @selector(redo:))
{
if ([[playlistController undoManager] canRedo])
return YES;
else
return NO;
}
if (action == @selector(scrollToCurrentEntry:) && ([playbackController playbackStatus] == kCogStatusStopped))
return NO;
return [super validateUserInterfaceItem:anItem];
}
2008-02-24 15:47:04 +00:00
- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
{
if (isLocal)
return NSDragOperationNone;
else
return NSDragOperationCopy;
}
2008-03-04 01:14:51 +00:00
2005-06-02 18:16:43 +00:00
@end