1 /*****************************************************************************
2 * playlist.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: playlist.m,v 1.25 2003/06/02 12:42:15 hartman 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 */
35 /*****************************************************************************
36 * VLCPlaylistView implementation
37 *****************************************************************************/
38 @implementation VLCPlaylistView
42 if( o_striped_row_color != nil )
44 [o_striped_row_color release];
49 - (NSMenu *)menuForEvent:(NSEvent *)o_event
51 return( [[self delegate] menuForEvent: o_event] );
54 - (void)keyDown:(NSEvent *)o_event
58 playlist_t * p_playlist;
59 intf_thread_t * p_intf = [NSApp getIntf];
61 if( [[o_event characters] length] )
63 key = [[o_event characters] characterAtIndex: 0];
66 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
69 if ( p_playlist == NULL )
77 vlc_mutex_lock( &p_playlist->object_lock );
78 if( p_playlist->p_input != NULL )
80 input_SetStatus( p_playlist->p_input, INPUT_STATUS_PAUSE );
82 vlc_mutex_unlock( &p_playlist->object_lock );
85 case NSDeleteCharacter:
86 case NSDeleteFunctionKey:
87 case NSDeleteCharFunctionKey:
88 case NSBackspaceCharacter:
89 while( ( i_row = [self selectedRow] ) != -1 )
91 if( p_playlist->i_index == i_row && p_playlist->i_status )
93 playlist_Stop( p_playlist );
96 playlist_Delete( p_playlist, i_row );
98 [self deselectRow: i_row];
104 [super keyDown: o_event];
108 if( p_playlist != NULL )
110 vlc_object_release( p_playlist );
114 - (void)highlightSelectionInClipRect:(NSRect)o_rect {
116 float f_height = [self rowHeight] + [self intercellSpacing].height;
117 float f_origin_y = NSMaxY( o_rect );
118 int i_row = o_rect.origin.y / f_height;
120 if ( i_row % 2 == 0 )
125 o_new_rect.size.width = o_rect.size.width;
126 o_new_rect.size.height = f_height;
127 o_new_rect.origin.x = o_rect.origin.x;
128 o_new_rect.origin.y = i_row * f_height;
130 if ( o_striped_row_color == nil )
132 o_striped_row_color = [[[NSColor alternateSelectedControlColor]
133 highlightWithLevel: 0.90] retain];
135 if ( o_striped_row_color == nil )
137 /* OSX 10.1 and before ain't that smart ;) */
138 o_striped_row_color = [[NSColor whiteColor] retain];
141 [o_striped_row_color set];
143 while ( o_new_rect.origin.y < f_origin_y ) {
144 NSRectFill( o_new_rect );
145 o_new_rect.origin.y += f_height * 2.0;
147 [super highlightSelectionInClipRect:o_rect];
152 /*****************************************************************************
153 * VLCPlaylist implementation
154 *****************************************************************************/
155 @implementation VLCPlaylist
169 [o_table_view setTarget: self];
170 [o_table_view setDelegate: self];
171 [o_table_view setDataSource: self];
173 [o_table_view setDoubleAction: @selector(playItem:)];
175 [o_table_view registerForDraggedTypes:
176 [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
178 [o_mi_play setTitle: _NS("Play")];
179 [o_mi_delete setTitle: _NS("Delete")];
180 [o_mi_selectall setTitle: _NS("Select All")];
182 [o_btn_add setToolTip: _NS("Add")];
183 [o_btn_remove setToolTip: _NS("Delete")];
186 - (BOOL)tableView:(NSTableView *)o_tv
187 shouldEditTableColumn:(NSTableColumn *)o_tc
193 - (NSMenu *)menuForEvent:(NSEvent *)o_event
197 vlc_bool_t b_item_sel;
199 pt = [o_table_view convertPoint: [o_event locationInWindow]
201 b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
202 [o_table_view selectedRow] != -1 );
203 b_rows = [o_table_view numberOfRows] != 0;
205 [o_mi_play setEnabled: b_item_sel];
206 [o_mi_delete setEnabled: b_item_sel];
207 [o_mi_selectall setEnabled: b_rows];
209 return( o_ctx_menu );
212 - (IBAction)playItem:(id)sender
214 intf_thread_t * p_intf = [NSApp getIntf];
215 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
218 if( p_playlist == NULL )
223 playlist_Goto( p_playlist, [o_table_view selectedRow] );
225 vlc_object_release( p_playlist );
228 - (IBAction)deleteItems:(id)sender
232 intf_thread_t * p_intf = [NSApp getIntf];
233 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
236 if( p_playlist == NULL )
241 while( ( i_row = [o_table_view selectedRow] ) != -1 )
243 if( p_playlist->i_index == i_row && p_playlist->i_status )
245 playlist_Stop( p_playlist );
248 playlist_Delete( p_playlist, i_row );
250 [o_table_view deselectRow: i_row];
253 vlc_object_release( p_playlist );
255 /* this is actually duplicity, because the intf.m manage also updates the view
256 * when the playlist changes. we do this on purpose, because else there is a
257 * delay of .5 sec or so when we delete an item */
258 [self playlistUpdated];
259 [self updateRowSelection];
262 - (IBAction)selectAll:(id)sender
264 [o_table_view selectAll: nil];
267 - (void)appendArray:(NSArray*)o_array atPos:(int)i_pos enqueue:(BOOL)b_enqueue
271 NSEnumerator * o_enum;
272 intf_thread_t * p_intf = [NSApp getIntf];
273 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
276 if( p_playlist == NULL )
282 o_enum = [o_array objectEnumerator];
283 while( ( o_value = [o_enum nextObject] ) )
287 int i_mode = PLAYLIST_INSERT;
289 if (i_items == 0 && !b_enqueue)
290 i_mode |= PLAYLIST_GO;
292 playlist_Add( p_playlist, [o_value fileSystemRepresentation],
293 i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );
295 o_url = [NSURL fileURLWithPath: o_value];
298 [[NSDocumentController sharedDocumentController]
299 noteNewRecentDocumentURL: o_url];
305 vlc_object_release( p_playlist );
308 - (void)playlistUpdated
310 [o_table_view reloadData];
313 - (void)updateRowSelection
317 intf_thread_t * p_intf = [NSApp getIntf];
318 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
321 if( p_playlist == NULL )
326 vlc_mutex_lock( &p_playlist->object_lock );
327 i_row = p_playlist->i_index;
328 vlc_mutex_unlock( &p_playlist->object_lock );
329 vlc_object_release( p_playlist );
331 [o_table_view selectRow: i_row byExtendingSelection: NO];
332 [o_table_view scrollRowToVisible: i_row];
337 @implementation VLCPlaylist (NSTableDataSource)
339 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
342 intf_thread_t * p_intf = [NSApp getIntf];
343 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
346 if( p_playlist != NULL )
348 vlc_mutex_lock( &p_playlist->object_lock );
349 i_count = p_playlist->i_size;
350 vlc_mutex_unlock( &p_playlist->object_lock );
351 vlc_object_release( p_playlist );
357 - (id)tableView:(NSTableView *)o_tv
358 objectValueForTableColumn:(NSTableColumn *)o_tc
362 intf_thread_t * p_intf = [NSApp getIntf];
363 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
366 if( p_playlist == NULL )
371 vlc_mutex_lock( &p_playlist->object_lock );
372 o_value = [[NSString stringWithUTF8String:
373 p_playlist->pp_items[i_row]->psz_name] lastPathComponent];
374 vlc_mutex_unlock( &p_playlist->object_lock );
376 vlc_object_release( p_playlist );
381 - (BOOL)tableView:(NSTableView *)o_tv
382 writeRows:(NSArray*)o_rows
383 toPasteboard:(NSPasteboard*)o_pasteboard
385 int i_rows = [o_rows count];
386 NSArray *o_filenames = [NSArray array];
388 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
389 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
392 i_moveRow = [[o_rows objectAtIndex:0]intValue];
398 - (NSDragOperation)tableView:(NSTableView*)o_tv
399 validateDrop:(id <NSDraggingInfo>)o_info
400 proposedRow:(int)i_row
401 proposedDropOperation:(NSTableViewDropOperation)o_operation
403 if ( o_operation == NSTableViewDropAbove )
405 if ( i_moveRow >= 0 )
407 if ( i_row != i_moveRow )
409 return NSDragOperationMove;
411 /* what if in the previous run, the row wasn't actually moved?
412 then we can't drop new files on this location */
413 return NSDragOperationNone;
415 return NSDragOperationGeneric;
417 return NSDragOperationNone;
420 - (BOOL)tableView:(NSTableView*)o_tv
421 acceptDrop:(id <NSDraggingInfo>)o_info
422 row:(int)i_proposed_row
423 dropOperation:(NSTableViewDropOperation)o_operation
425 if ( i_moveRow >= 0 )
427 if (i_moveRow != -1 && i_proposed_row != -1)
429 intf_thread_t * p_intf = [NSApp getIntf];
430 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
433 if( p_playlist == NULL )
439 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
441 vlc_object_release( p_playlist );
443 [self playlistUpdated];
450 NSPasteboard * o_pasteboard;
452 intf_thread_t * p_intf = [NSApp getIntf];
453 o_pasteboard = [o_info draggingPasteboard];
455 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
457 o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
458 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
460 config_PutPsz( p_intf, "sub-file", "" );
461 config_PutInt( p_intf, "sub-delay", 0 );
462 config_PutFloat( p_intf, "sub-fps", 0.0 );
463 config_PutPsz( p_intf, "sout", "" );
465 [self appendArray: o_values atPos: i_proposed_row enqueue:YES];
472 [self updateRowSelection];