1 /*****************************************************************************
2 * playlist.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: playlist.m,v 1.28 2003/07/23 01:13:47 gbazin 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_pos enqueue:(BOOL)b_enqueue
293 NSEnumerator * o_enum;
294 intf_thread_t * p_intf = [NSApp getIntf];
295 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
298 if( p_playlist == NULL )
304 o_enum = [o_array objectEnumerator];
305 while( ( o_value = [o_enum nextObject] ) )
309 int i_mode = PLAYLIST_INSERT;
311 if (i_items == 0 && !b_enqueue)
312 i_mode |= PLAYLIST_GO;
314 playlist_Add( p_playlist, [o_value fileSystemRepresentation],
315 0, 0, i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );
317 o_url = [NSURL fileURLWithPath: o_value];
320 [[NSDocumentController sharedDocumentController]
321 noteNewRecentDocumentURL: o_url];
327 vlc_object_release( p_playlist );
330 - (void)playlistUpdated
332 [o_table_view reloadData];
335 - (void)updateRowSelection
339 intf_thread_t * p_intf = [NSApp getIntf];
340 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
343 if( p_playlist == NULL )
348 vlc_mutex_lock( &p_playlist->object_lock );
349 i_row = p_playlist->i_index;
350 vlc_mutex_unlock( &p_playlist->object_lock );
351 vlc_object_release( p_playlist );
353 [o_table_view selectRow: i_row byExtendingSelection: NO];
354 [o_table_view scrollRowToVisible: i_row];
359 @implementation VLCPlaylist (NSTableDataSource)
361 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
364 intf_thread_t * p_intf = [NSApp getIntf];
365 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
368 if( p_playlist != NULL )
370 vlc_mutex_lock( &p_playlist->object_lock );
371 i_count = p_playlist->i_size;
372 vlc_mutex_unlock( &p_playlist->object_lock );
373 vlc_object_release( p_playlist );
379 - (id)tableView:(NSTableView *)o_tv
380 objectValueForTableColumn:(NSTableColumn *)o_tc
384 intf_thread_t * p_intf = [NSApp getIntf];
385 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
388 if( p_playlist == NULL )
393 vlc_mutex_lock( &p_playlist->object_lock );
394 o_value = [[NSString stringWithUTF8String:
395 p_playlist->pp_items[i_row]->psz_name] lastPathComponent];
396 vlc_mutex_unlock( &p_playlist->object_lock );
398 vlc_object_release( p_playlist );
403 - (BOOL)tableView:(NSTableView *)o_tv
404 writeRows:(NSArray*)o_rows
405 toPasteboard:(NSPasteboard*)o_pasteboard
407 int i_rows = [o_rows count];
408 NSArray *o_filenames = [NSArray array];
410 [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
411 [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
414 i_moveRow = [[o_rows objectAtIndex:0]intValue];
420 - (NSDragOperation)tableView:(NSTableView*)o_tv
421 validateDrop:(id <NSDraggingInfo>)o_info
422 proposedRow:(int)i_row
423 proposedDropOperation:(NSTableViewDropOperation)o_operation
425 if ( o_operation == NSTableViewDropAbove )
427 if ( i_moveRow >= 0 )
429 if ( i_row != i_moveRow )
431 return NSDragOperationMove;
433 /* what if in the previous run, the row wasn't actually moved?
434 then we can't drop new files on this location */
435 return NSDragOperationNone;
437 return NSDragOperationGeneric;
439 return NSDragOperationNone;
442 - (BOOL)tableView:(NSTableView*)o_tv
443 acceptDrop:(id <NSDraggingInfo>)o_info
444 row:(int)i_proposed_row
445 dropOperation:(NSTableViewDropOperation)o_operation
447 if ( i_moveRow >= 0 )
449 if (i_moveRow != -1 && i_proposed_row != -1)
451 intf_thread_t * p_intf = [NSApp getIntf];
452 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
455 if( p_playlist == NULL )
461 playlist_Move( p_playlist, i_moveRow, i_proposed_row );
463 vlc_object_release( p_playlist );
465 [self playlistUpdated];
472 NSPasteboard * o_pasteboard;
474 intf_thread_t * p_intf = [NSApp getIntf];
475 o_pasteboard = [o_info draggingPasteboard];
477 if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
479 o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
480 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
482 config_PutPsz( p_intf, "sub-file", "" );
483 config_PutInt( p_intf, "sub-delay", 0 );
484 config_PutFloat( p_intf, "sub-fps", 0.0 );
485 config_PutPsz( p_intf, "sout", "" );
487 [self appendArray: o_values atPos: i_proposed_row enqueue:YES];
494 [self updateRowSelection];