playlist.m 14.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * playlist.m: MacOS X interface plugin
 *****************************************************************************
4
 * Copyright (C) 2002-2003 VideoLAN
Gildas Bazin's avatar
 
Gildas Bazin committed
5
 * $Id: playlist.m,v 1.28 2003/07/23 01:13:47 gbazin Exp $
6 7
 *
 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8
 *          Derk-Jan Hartman <thedj@users.sourceforge.net>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <sys/param.h>                                    /* for MAXPATHLEN */
#include <string.h>
31
#include <math.h>
32 33 34 35

#include "intf.h"
#include "playlist.h"

36 37
int MacVersion102 = -1;

38 39 40 41 42
/*****************************************************************************
 * VLCPlaylistView implementation 
 *****************************************************************************/
@implementation VLCPlaylistView

43 44 45 46 47 48 49 50 51
- (void)dealloc
{
    if( o_striped_row_color != nil )
    {
        [o_striped_row_color release];
    }
    [super dealloc];
}

52 53
- (NSMenu *)menuForEvent:(NSEvent *)o_event
{
Jon Lech Johansen's avatar
Jon Lech Johansen committed
54
    return( [[self delegate] menuForEvent: o_event] );
55 56
}

57 58 59
- (void)keyDown:(NSEvent *)o_event
{
    unichar key = 0;
60 61 62 63
    int i, c, i_row;
    NSMutableArray *o_to_delete;
    NSNumber *o_number;
    
64 65
    playlist_t * p_playlist;
    intf_thread_t * p_intf = [NSApp getIntf];
66 67 68 69 70 71

    if( [[o_event characters] length] )
    {
        key = [[o_event characters] characterAtIndex: 0];
    }

72 73
    p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                          FIND_ANYWHERE );
74 75
    
    if ( p_playlist == NULL )
76
    {
77
        return;
78
    }
79
    
80 81 82
    switch( key )
    {
        case ' ':
83
            vlc_mutex_lock( &p_playlist->object_lock );
84
            if( p_playlist->p_input != NULL )
85 86 87
            {
                input_SetStatus( p_playlist->p_input, INPUT_STATUS_PAUSE );
            }
88
            vlc_mutex_unlock( &p_playlist->object_lock );
89 90
            break;

91 92 93 94
        case NSDeleteCharacter:
        case NSDeleteFunctionKey:
        case NSDeleteCharFunctionKey:
        case NSBackspaceCharacter:
95 96 97 98 99 100 101
            o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
            c = [o_to_delete count];
            
            for( i = 0; i < c; i++ ) {
                o_number = [o_to_delete lastObject];
                i_row = [o_number intValue];
                
102 103 104 105
                if( p_playlist->i_index == i_row && p_playlist->i_status )
                {
                    playlist_Stop( p_playlist );
                }
106
                [o_to_delete removeObject: o_number];
107
                [self deselectRow: i_row];
108
                playlist_Delete( p_playlist, i_row );
109 110 111 112
            }
            [self reloadData];
            break;
            
113 114 115 116
        default:
            [super keyDown: o_event];
            break;
    }
117 118 119 120 121

    if( p_playlist != NULL )
    {
        vlc_object_release( p_playlist );
    }
122 123
}

124
- (void)highlightSelectionInClipRect:(NSRect)o_rect {
125 126 127 128 129 130 131 132
    NSRect o_new_rect;
    float f_height = [self rowHeight] + [self intercellSpacing].height;
    float f_origin_y = NSMaxY( o_rect );
    int i_row = o_rect.origin.y / f_height;
    
    if ( i_row % 2 == 0 )
    {
        i_row++;
133
    }
134 135 136 137 138
    
    o_new_rect.size.width = o_rect.size.width;
    o_new_rect.size.height = f_height;
    o_new_rect.origin.x = o_rect.origin.x;
    o_new_rect.origin.y = i_row * f_height;
139 140 141 142 143 144 145
   
    if( ( MacVersion102 < 0 ) && ( floor( NSAppKitVersionNumber ) > NSAppKitVersionNumber10_1 ) )
    {
        MacVersion102 = 102;
    }
 
    if ( MacVersion102 == 102 && o_striped_row_color == nil )
146 147 148
    {
        o_striped_row_color = [[[NSColor alternateSelectedControlColor]
                                highlightWithLevel: 0.90] retain];
149
        
150
    }
151 152 153 154 155 156
    else if ( o_striped_row_color == nil )
    {
        /* OSX 10.1 and before ain't that smart ;) */
        o_striped_row_color = [[NSColor whiteColor] retain];
    }

157 158 159 160 161 162 163
    [o_striped_row_color set];
    
    while ( o_new_rect.origin.y < f_origin_y ) {
        NSRectFill( o_new_rect );
        o_new_rect.origin.y += f_height * 2.0;
    }
    [super highlightSelectionInClipRect:o_rect];
164 165
}

166 167 168 169 170 171 172
@end

/*****************************************************************************
 * VLCPlaylist implementation 
 *****************************************************************************/
@implementation VLCPlaylist

173 174 175 176 177 178 179 180 181 182
- (id)init
{
    self = [super init];
    if ( self !=nil )
    {
        i_moveRow = -1;
    }
    return self;
}

183 184 185 186 187 188
- (void)awakeFromNib
{
    [o_table_view setTarget: self];
    [o_table_view setDelegate: self];
    [o_table_view setDataSource: self];

Jon Lech Johansen's avatar
Jon Lech Johansen committed
189
    [o_table_view setDoubleAction: @selector(playItem:)];
190 191 192 193

    [o_table_view registerForDraggedTypes: 
        [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];

Jon Lech Johansen's avatar
Jon Lech Johansen committed
194 195 196
    [o_mi_play setTitle: _NS("Play")];
    [o_mi_delete setTitle: _NS("Delete")];
    [o_mi_selectall setTitle: _NS("Select All")];
197 198 199
    
    [o_btn_add setToolTip: _NS("Add")];
    [o_btn_remove setToolTip: _NS("Delete")];
200 201 202 203 204 205 206 207 208
}

- (BOOL)tableView:(NSTableView *)o_tv 
                  shouldEditTableColumn:(NSTableColumn *)o_tc
                  row:(int)i_row
{
    return( NO );
}

Jon Lech Johansen's avatar
Jon Lech Johansen committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
- (NSMenu *)menuForEvent:(NSEvent *)o_event
{
    NSPoint pt;
    vlc_bool_t b_rows;
    vlc_bool_t b_item_sel;

    pt = [o_table_view convertPoint: [o_event locationInWindow] 
                                                 fromView: nil];
    b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
                   [o_table_view selectedRow] != -1 );
    b_rows = [o_table_view numberOfRows] != 0;

    [o_mi_play setEnabled: b_item_sel];
    [o_mi_delete setEnabled: b_item_sel];
    [o_mi_selectall setEnabled: b_rows];

    return( o_ctx_menu );
}

- (IBAction)playItem:(id)sender
229 230 231 232 233 234 235 236 237 238
{
    intf_thread_t * p_intf = [NSApp getIntf];
    playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                                       FIND_ANYWHERE );

    if( p_playlist == NULL )
    {
        return;
    }

Jon Lech Johansen's avatar
Jon Lech Johansen committed
239
    playlist_Goto( p_playlist, [o_table_view selectedRow] );
240 241 242 243

    vlc_object_release( p_playlist );
}

Jon Lech Johansen's avatar
Jon Lech Johansen committed
244 245
- (IBAction)deleteItems:(id)sender
{
246 247 248
    int i, c, i_row;
    NSMutableArray *o_to_delete;
    NSNumber *o_number;
Jon Lech Johansen's avatar
Jon Lech Johansen committed
249 250 251 252 253 254 255 256 257

    intf_thread_t * p_intf = [NSApp getIntf];
    playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                                       FIND_ANYWHERE );

    if( p_playlist == NULL )
    {
        return;
    }
258 259 260 261 262 263 264 265
    
    o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
    c = [o_to_delete count];
    
    for( i = 0; i < c; i++ ) {
        o_number = [o_to_delete lastObject];
        i_row = [o_number intValue];
        
Jon Lech Johansen's avatar
Jon Lech Johansen committed
266 267 268 269
        if( p_playlist->i_index == i_row && p_playlist->i_status )
        {
            playlist_Stop( p_playlist );
        }
270
        [o_to_delete removeObject: o_number];
Jon Lech Johansen's avatar
Jon Lech Johansen committed
271
        [o_table_view deselectRow: i_row];
272
        playlist_Delete( p_playlist, i_row );
Jon Lech Johansen's avatar
Jon Lech Johansen committed
273 274 275 276
    }

    vlc_object_release( p_playlist );

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
277 278 279
    /* this is actually duplicity, because the intf.m manage also updates the view
     * when the playlist changes. we do this on purpose, because else there is a 
     * delay of .5 sec or so when we delete an item */
Jon Lech Johansen's avatar
Jon Lech Johansen committed
280
    [self playlistUpdated];
281
    [self updateRowSelection];
Jon Lech Johansen's avatar
Jon Lech Johansen committed
282 283 284 285 286 287 288
}

- (IBAction)selectAll:(id)sender
{
    [o_table_view selectAll: nil];
}

289
- (void)appendArray:(NSArray*)o_array atPos:(int)i_pos enqueue:(BOOL)b_enqueue
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
{
    int i_items;
    NSString * o_value;
    NSEnumerator * o_enum;
    intf_thread_t * p_intf = [NSApp getIntf];
    playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                                       FIND_ANYWHERE );

    if( p_playlist == NULL )
    {
        return;
    }

    i_items = 0;
    o_enum = [o_array objectEnumerator];
    while( ( o_value = [o_enum nextObject] ) )
    {
        NSURL * o_url;

309 310 311 312
        int i_mode = PLAYLIST_INSERT;
        
        if (i_items == 0 && !b_enqueue)
            i_mode |= PLAYLIST_GO;
313 314

        playlist_Add( p_playlist, [o_value fileSystemRepresentation],
Gildas Bazin's avatar
 
Gildas Bazin committed
315
            0, 0, i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );
316 317 318 319 320 321 322 323 324 325 326 327

        o_url = [NSURL fileURLWithPath: o_value];
        if( o_url != nil )
        { 
            [[NSDocumentController sharedDocumentController]
                noteNewRecentDocumentURL: o_url]; 
        }

        i_items++;
    }

    vlc_object_release( p_playlist );
Jon Lech Johansen's avatar
Jon Lech Johansen committed
328 329 330 331 332
}

- (void)playlistUpdated
{
    [o_table_view reloadData];
333 334
}

335
- (void)updateRowSelection
Derk-Jan Hartman's avatar
ALL:  
Derk-Jan Hartman committed
336 337 338 339 340 341 342 343 344 345 346
{
    int i_row;

    intf_thread_t * p_intf = [NSApp getIntf];
    playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                                       FIND_ANYWHERE );

    if( p_playlist == NULL )
    {
        return;
    }
347 348

    vlc_mutex_lock( &p_playlist->object_lock );    
Derk-Jan Hartman's avatar
ALL:  
Derk-Jan Hartman committed
349
    i_row = p_playlist->i_index;
350
    vlc_mutex_unlock( &p_playlist->object_lock );
Derk-Jan Hartman's avatar
ALL:  
Derk-Jan Hartman committed
351
    vlc_object_release( p_playlist );
352

Derk-Jan Hartman's avatar
ALL:  
Derk-Jan Hartman committed
353 354 355 356
    [o_table_view selectRow: i_row byExtendingSelection: NO];
    [o_table_view scrollRowToVisible: i_row];
}

357 358 359 360 361 362 363 364 365
@end

@implementation VLCPlaylist (NSTableDataSource)

- (int)numberOfRowsInTableView:(NSTableView *)o_tv
{
    int i_count = 0;
    intf_thread_t * p_intf = [NSApp getIntf];
    playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
366
                                                       FIND_ANYWHERE );
367 368 369

    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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
        vlc_object_release( p_playlist );
    }

    return( i_count );
}

- (id)tableView:(NSTableView *)o_tv 
                objectValueForTableColumn:(NSTableColumn *)o_tc 
                row:(int)i_row
{
    id o_value = nil;
    intf_thread_t * p_intf = [NSApp getIntf];
    playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                               FIND_ANYWHERE );

    if( p_playlist == NULL )
    {
        return( nil );
    }

    vlc_mutex_lock( &p_playlist->object_lock );
394 395
    o_value = [[NSString stringWithUTF8String: 
        p_playlist->pp_items[i_row]->psz_name] lastPathComponent]; 
396 397 398 399 400 401 402
    vlc_mutex_unlock( &p_playlist->object_lock ); 

    vlc_object_release( p_playlist );

    return( o_value );
}

403 404 405
- (BOOL)tableView:(NSTableView *)o_tv
                    writeRows:(NSArray*)o_rows
                    toPasteboard:(NSPasteboard*)o_pasteboard 
406
{
407
    int i_rows = [o_rows count];
408 409
    NSArray *o_filenames = [NSArray array];
    
410 411 412
    [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
    [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
    if ( i_rows == 1 )
413
    {
414
        i_moveRow = [[o_rows objectAtIndex:0]intValue];
415 416 417 418 419
        return YES;
    }
    return NO;
}

420 421 422 423
- (NSDragOperation)tableView:(NSTableView*)o_tv
                    validateDrop:(id <NSDraggingInfo>)o_info
                    proposedRow:(int)i_row
                    proposedDropOperation:(NSTableViewDropOperation)o_operation 
424
{
425
    if ( o_operation == NSTableViewDropAbove )
426
    {
427
        if ( i_moveRow >= 0 )
428
        {
429 430 431 432 433 434 435
            if ( i_row != i_moveRow )
            {
                return NSDragOperationMove;
            }
            /* what if in the previous run, the row wasn't actually moved? 
               then we can't drop new files on this location */
            return NSDragOperationNone;
436
        }
437
        return NSDragOperationGeneric;
438 439 440 441
    }
    return NSDragOperationNone;
}

442 443 444 445
- (BOOL)tableView:(NSTableView*)o_tv
                    acceptDrop:(id <NSDraggingInfo>)o_info
                    row:(int)i_proposed_row
                    dropOperation:(NSTableViewDropOperation)o_operation 
446
{
447
    if (  i_moveRow >= 0 )
448
    {
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
        if (i_moveRow != -1 && i_proposed_row != -1)
        {
            intf_thread_t * p_intf = [NSApp getIntf];
            playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                                            FIND_ANYWHERE );
        
            if( p_playlist == NULL )
            {
                i_moveRow = -1;
                return NO;
            }
    
            playlist_Move( p_playlist, i_moveRow, i_proposed_row ); 
        
            vlc_object_release( p_playlist );
        }
465
        [self playlistUpdated];
466 467
        i_moveRow = -1;
        return YES;
468 469 470 471 472 473
    }
    else
    {
        NSArray * o_values;
        NSPasteboard * o_pasteboard;
        
474
        intf_thread_t * p_intf = [NSApp getIntf];
475
        o_pasteboard = [o_info draggingPasteboard];
476 477 478
        
        if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
        {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
479 480
            o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
                        sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
481

482 483 484 485
            config_PutPsz( p_intf, "sub-file", "" );
            config_PutInt( p_intf, "sub-delay", 0 );
            config_PutFloat( p_intf, "sub-fps", 0.0 );
            config_PutPsz( p_intf, "sout", "" );
486 487 488

            [self appendArray: o_values atPos: i_proposed_row enqueue:YES];

489 490 491 492 493
            return( YES );
        }
        
        return( NO );
    }
494
    [self updateRowSelection];
495 496
}

497 498
@end