1 /*****************************************************************************
2 * playlist.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: playlist.m,v 1.30 2003/08/14 13:02:55 sigmunau Exp $
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Derk-Jan Hartman <thedj@users.sourceforge.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
29 #include <sys/param.h> /* for MAXPATHLEN */
36 int MacVersion102 = -1;
38 /*****************************************************************************
39 * VLCPlaylistView implementation
40 *****************************************************************************/
41 @implementation VLCPlaylistView
45 if( o_striped_row_color != nil )
47 [o_striped_row_color release];
52 - (NSMenu *)menuForEvent:(NSEvent *)o_event
54 return( [[self delegate] menuForEvent: o_event] );
57 - (void)keyDown:(NSEvent *)o_event
61 NSMutableArray *o_to_delete;
64 playlist_t * p_playlist;
65 intf_thread_t * p_intf = [NSApp getIntf];
67 if( [[o_event characters] length] )
69 key = [[o_event characters] characterAtIndex: 0];
72 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
75 if ( p_playlist == NULL )
83 vlc_mutex_lock( &p_playlist->object_lock );
84 if( p_playlist->p_input != NULL )
86 input_SetStatus( p_playlist->p_input, INPUT_STATUS_PAUSE );
88 vlc_mutex_unlock( &p_playlist->object_lock );
91 case NSDeleteCharacter:
92 case NSDeleteFunctionKey:
93 case NSDeleteCharFunctionKey:
94 case NSBackspaceCharacter:
95 o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
96 c = [o_to_delete count];
98 for( i = 0; i < c; i++ ) {
99 o_number = [o_to_delete lastObject];
100 i_row = [o_number intValue];
102 if( p_playlist->i_index == i_row && p_playlist->i_status )
104 playlist_Stop( p_playlist );
106 [o_to_delete removeObject: o_number];
107 [self deselectRow: i_row];
108 playlist_Delete( p_playlist, i_row );
114 [super keyDown: o_event];
118 if( p_playlist != NULL )
120 vlc_object_release( p_playlist );
124 - (void)highlightSelectionInClipRect:(NSRect)o_rect {
126 float f_height = [self rowHeight] + [self intercellSpacing].height;
127 float f_origin_y = NSMaxY( o_rect );
128 int i_row = o_rect.origin.y / f_height;
130 if ( i_row % 2 == 0 )
135 o_new_rect.size.width = o_rect.size.width;
136 o_new_rect.size.height = f_height;
137 o_new_rect.origin.x = o_rect.origin.x;
138 o_new_rect.origin.y = i_row * f_height;
140 if( ( MacVersion102 < 0 ) && ( floor( NSAppKitVersionNumber ) > NSAppKitVersionNumber10_1 ) )
145 if ( MacVersion102 == 102 && o_striped_row_color == nil )
147 o_striped_row_color = [[[NSColor alternateSelectedControlColor]
148 highlightWithLevel: 0.90] retain];
151 else if ( o_striped_row_color == nil )
153 /* OSX 10.1 and before ain't that smart ;) */
154 o_striped_row_color = [[NSColor whiteColor] retain];
157 [o_striped_row_color set];
159 while ( o_new_rect.origin.y < f_origin_y ) {
160 NSRectFill( o_new_rect );
161 o_new_rect.origin.y += f_height * 2.0;
163 [super highlightSelectionInClipRect:o_rect];
168 /*****************************************************************************
169 * VLCPlaylist implementation
170 *****************************************************************************/
171 @implementation VLCPlaylist
185 [o_table_view setTarget: self];
186 [o_table_view setDelegate: self];
187 [o_table_view setDataSource: self];
189 [o_table_view setDoubleAction: @selector(playItem:)];
191 [o_table_view registerForDraggedTypes:
192 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
194 [o_mi_play setTitle: _NS("Play")];
195 [o_mi_delete setTitle: _NS("Delete")];
196 [o_mi_selectall setTitle: _NS("Select All")];
198 [o_btn_add setToolTip: _NS("Add")];
199 [o_btn_remove setToolTip: _NS("Delete")];
202 - (BOOL)tableView:(NSTableView *)o_tv
203 shouldEditTableColumn:(NSTableColumn *)o_tc
209 - (NSMenu *)menuForEvent:(NSEvent *)o_event
213 vlc_bool_t b_item_sel;
215 pt = [o_table_view convertPoint: [o_event locationInWindow]
217 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
218 [o_table_view selectedRow] != -1 );
219 b_rows = [o_table_view numberOfRows] != 0;
221 [o_mi_play setEnabled: b_item_sel];
222 [o_mi_delete setEnabled: b_item_sel];
223 [o_mi_selectall setEnabled: b_rows];
225 return( o_ctx_menu );
228 - (IBAction)playItem:(id)sender
230 intf_thread_t * p_intf = [NSApp getIntf];
231 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
234 if( p_playlist == NULL )
239 playlist_Goto( p_playlist, [o_table_view selectedRow] );
241 vlc_object_release( p_playlist );
244 - (IBAction)deleteItems:(id)sender
247 NSMutableArray *o_to_delete;
250 intf_thread_t * p_intf = [NSApp getIntf];
251 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
254 if( p_playlist == NULL )
259 o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
260 c = [o_to_delete count];
262 for( i = 0; i < c; i++ ) {
263 o_number = [o_to_delete lastObject];
264 i_row = [o_number intValue];
266 if( p_playlist->i_index == i_row && p_playlist->i_status )
268 playlist_Stop( p_playlist );
270 [o_to_delete removeObject: o_number];
271 [o_table_view deselectRow: i_row];
272 playlist_Delete( p_playlist, i_row );
275 vlc_object_release( p_playlist );
277 /* this is actually duplicity, because the intf.m manage also updates the view
278 * when the playlist changes. we do this on purpose, because else there is a
279 * delay of .5 sec or so when we delete an item */
280 [self playlistUpdated];
281 [self updateRowSelection];
284 - (IBAction)selectAll:(id)sender
286 [o_table_view selectAll: nil];
289 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
292 intf_thread_t * p_intf = [NSApp getIntf];
293 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
296 if( p_playlist == NULL )
301 for ( i_item = 0; i_item < [o_array count]; i_item++ )
304 NSDictionary *o_one_item;
308 int j, i_total_options = 0;
309 char **ppsz_options = NULL;
310 int i_mode = PLAYLIST_INSERT;
313 o_one_item = [o_array objectAtIndex: i_item];
314 o_url = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
315 o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
316 o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
318 if( !o_name) o_name = o_url;
320 if (i_item == 0 && !b_enqueue)
321 i_mode |= PLAYLIST_GO;
323 if( o_options && [o_options count] > 0 )
325 /* Count the input options */
326 i_total_options = [o_options count];
328 /* Allocate ppsz_options */
329 for( j = 0; j < i_total_options; j++ )
332 ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
334 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
338 playlist_AddExt( p_playlist, [o_name UTF8String], [o_url fileSystemRepresentation], -1,
339 (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options,
340 i_mode, i_position == -1 ? PLAYLIST_END : i_position + i_item );
343 for( j = 0; j < i_total_options; j++ )
344 free( ppsz_options[j] );
345 if( ppsz_options ) free( ppsz_options );
347 NSURL *o_true_url = [NSURL fileURLWithPath: o_url];
348 if( o_true_url != nil )
350 [[NSDocumentController sharedDocumentController]
351 noteNewRecentDocumentURL: o_true_url];
355 vlc_object_release( p_playlist );
358 - (void)playlistUpdated
360 [o_table_view reloadData];
363 - (void)updateRowSelection
367 intf_thread_t * p_intf = [NSApp getIntf];
368 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
371 if( p_playlist == NULL )
376 vlc_mutex_lock( &p_playlist->object_lock );
377 i_row = p_playlist->i_index;
378 vlc_mutex_unlock( &p_playlist->object_lock );
379 vlc_object_release( p_playlist );
381 [o_table_view selectRow: i_row byExtendingSelection: NO];
382 [o_table_view scrollRowToVisible: i_row];
387 @implementation VLCPlaylist (NSTableDataSource)
389 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
392 intf_thread_t * p_intf = [NSApp getIntf];
393 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
396 if( p_playlist != NULL )
398 vlc_mutex_lock( &p_playlist->object_lock );
399 i_count = p_playlist->i_size;
400 vlc_mutex_unlock( &p_playlist->object_lock );
401 vlc_object_release( p_playlist );
407 - (id)tableView:(NSTableView *)o_tv
408 objectValueForTableColumn:(NSTableColumn *)o_tc
412 intf_thread_t * p_intf = [NSApp getIntf];
413 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
416 if( p_playlist == NULL )
421 vlc_mutex_lock( &p_playlist->object_lock );
422 o_value = [[NSString stringWithUTF8String:
423 p_playlist->pp_items[i_row]->psz_name] lastPathComponent];
424 vlc_mutex_unlock( &p_playlist->object_lock );
426 vlc_object_release( p_playlist );
431 - (BOOL)tableView:(NSTableView *)o_tv
432 writeRows:(NSArray*)o_rows
433 toPasteboard:(NSPasteboard*)o_pasteboard
435 int i_rows = [o_rows count];
436 NSArray *o_filenames = [NSArray array];
438 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
439 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
442 i_moveRow = [[o_rows objectAtIndex:0]intValue];
448 - (NSDragOperation)tableView:(NSTableView*)o_tv
449 validateDrop:(id <NSDraggingInfo>)o_info
450 proposedRow:(int)i_row
451 proposedDropOperation:(NSTableViewDropOperation)o_operation
453 if ( o_operation == NSTableViewDropAbove )
455 if ( i_moveRow >= 0 )
457 if ( i_row != i_moveRow )
459 return NSDragOperationMove;
461 /* what if in the previous run, the row wasn't actually moved?
462 then we can't drop new files on this location */
463 return NSDragOperationNone;
465 return NSDragOperationGeneric;
467 return NSDragOperationNone;
470 - (BOOL)tableView:(NSTableView*)o_tv
471 acceptDrop:(id <NSDraggingInfo>)o_info
472 row:(int)i_proposed_row
473 dropOperation:(NSTableViewDropOperation)o_operation
475 if ( i_moveRow >= 0 )
477 if (i_moveRow != -1 && i_proposed_row != -1)
479 intf_thread_t * p_intf = [NSApp getIntf];
480 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
483 if( p_playlist == NULL )
489 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
491 vlc_object_release( p_playlist );
493 [self playlistUpdated];
499 NSPasteboard * o_pasteboard;
501 intf_thread_t * p_intf = [NSApp getIntf];
502 o_pasteboard = [o_info draggingPasteboard];
504 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
507 NSArray *o_array = [NSArray array];
508 NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
509 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
511 for( i = 0; i < [o_values count]; i++)
514 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
515 o_array = [o_array arrayByAddingObject: o_dic];
517 [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
524 [self updateRowSelection];