playlist.m 10.7 KB
Newer Older
1
2
3
/*****************************************************************************
 * playlist.m: MacOS X interface plugin
 *****************************************************************************
4
 * Copyright (C) 2002-2003 VideoLAN
hartman's avatar
hartman committed
5
 * $Id: playlist.m,v 1.13 2003/03/06 11:43:07 hartman Exp $
6
7
 *
 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
hartman's avatar
hartman committed
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
31
32
33
34
35
36
37
38
39
40
41
 *
 * 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>

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

/*****************************************************************************
 * VLCPlaylistView implementation 
 *****************************************************************************/
@implementation VLCPlaylistView

- (NSMenu *)menuForEvent:(NSEvent *)o_event
{
Jon Lech Johansen's avatar
Jon Lech Johansen committed
42
    return( [[self delegate] menuForEvent: o_event] );
43
44
}

45
46
47
- (void)keyDown:(NSEvent *)o_event
{
    unichar key = 0;
48
    int i_row;
49
50
    playlist_t * p_playlist;
    intf_thread_t * p_intf = [NSApp getIntf];
51
52
53
54
55
56

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

57
58
    p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                          FIND_ANYWHERE );
59
60
    
    if ( p_playlist == NULL )
61
    {
62
        return;
63
    }
64
    
65
66
67
    switch( key )
    {
        case ' ':
68
            vlc_mutex_lock( &p_playlist->object_lock );
69
            if( p_playlist->p_input != NULL )
70
71
72
            {
                input_SetStatus( p_playlist->p_input, INPUT_STATUS_PAUSE );
            }
73
            vlc_mutex_unlock( &p_playlist->object_lock );
74
75
            break;

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        case NSDeleteCharacter:
        case NSDeleteFunctionKey:
        case NSDeleteCharFunctionKey:
        case NSBackspaceCharacter:
            while( ( i_row = [self selectedRow] ) != -1 )
            {
                if( p_playlist->i_index == i_row && p_playlist->i_status )
                {
                    playlist_Stop( p_playlist );
                }
        
                playlist_Delete( p_playlist, i_row ); 
        
                [self deselectRow: i_row];
            }
            [self reloadData];
            break;
            
94
95
96
97
        default:
            [super keyDown: o_event];
            break;
    }
98
99
100
101
102

    if( p_playlist != NULL )
    {
        vlc_object_release( p_playlist );
    }
103
104
}

105
106
107
108
109
110
111
112
113
114
115
116
117
@end

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

- (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
118
    [o_table_view setDoubleAction: @selector(playItem:)];
119
120
121
122

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

Jon Lech Johansen's avatar
Jon Lech Johansen committed
123
124
125
    [o_mi_play setTitle: _NS("Play")];
    [o_mi_delete setTitle: _NS("Delete")];
    [o_mi_selectall setTitle: _NS("Select All")];
hartman's avatar
hartman committed
126
127
128
    
    [o_btn_add setToolTip: _NS("Add")];
    [o_btn_remove setToolTip: _NS("Delete")];
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
}

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

- (NSDragOperation)tableView:(NSTableView*)o_tv 
                   validateDrop:(id <NSDraggingInfo>)info 
                   proposedRow:(int)i_row 
                   proposedDropOperation:(NSTableViewDropOperation)operation
{
    return( NSDragOperationPrivate );
}

- (BOOL)tableView:(NSTableView*)o_tv 
                  acceptDrop:(id <NSDraggingInfo>)info 
                  row:(int)i_row 
                  dropOperation:(NSTableViewDropOperation)operation
{
    NSArray * o_values;
    NSPasteboard * o_pasteboard;

    o_pasteboard = [info draggingPasteboard];

    if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
    {
        o_values = [o_pasteboard propertyListForType: NSFilenamesPboardType];

160
        [self appendArray: o_values atPos: i_row enqueue:YES];
161
162
163
164
165
166
167
168
169
170

        return( YES );
    }

    return( NO ); 
}

- (void)tableView:(NSTableView *)o_tv willDisplayCell:(id)o_cell
                  forTableColumn:(NSTableColumn *)o_tc row:(int)i_row
{
Jon Lech Johansen's avatar
Jon Lech Johansen committed
171
172
    NSColor * o_color;

173
174
175
176
    [o_cell setDrawsBackground: YES];

    if( i_row % 2 )
    {
Jon Lech Johansen's avatar
Jon Lech Johansen committed
177
178
        o_color = [[NSColor alternateSelectedControlColor]
                            highlightWithLevel: 0.90];
179
180
181
    }
    else
    {
Jon Lech Johansen's avatar
Jon Lech Johansen committed
182
        o_color = [o_tv backgroundColor]; 
183
    }
Jon Lech Johansen's avatar
Jon Lech Johansen committed
184
185

    [o_cell setBackgroundColor: o_color];
186
187
}

Jon Lech Johansen's avatar
Jon Lech Johansen committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
- (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
208
209
210
211
212
213
214
215
216
217
{
    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
218
    playlist_Goto( p_playlist, [o_table_view selectedRow] );
219
220
221
222

    vlc_object_release( p_playlist );
}

Jon Lech Johansen's avatar
Jon Lech Johansen committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
- (IBAction)deleteItems:(id)sender
{
    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;
    }

    while( ( i_row = [o_table_view selectedRow] ) != -1 )
    {
        if( p_playlist->i_index == i_row && p_playlist->i_status )
        {
            playlist_Stop( p_playlist );
        }

        playlist_Delete( p_playlist, i_row ); 

        [o_table_view deselectRow: i_row];
    }

    vlc_object_release( p_playlist );

hartman's avatar
hartman committed
250
251
252
    /* 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
253
254
255
256
257
258
259
260
    [self playlistUpdated];
}

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

261
- (void)appendArray:(NSArray*)o_array atPos:(int)i_pos enqueue:(BOOL)b_enqueue
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
{
    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;

281
282
283
284
        int i_mode = PLAYLIST_INSERT;
        
        if (i_items == 0 && !b_enqueue)
            i_mode |= PLAYLIST_GO;
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

        playlist_Add( p_playlist, [o_value fileSystemRepresentation],
            i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );

        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
300
301
302
303
304
}

- (void)playlistUpdated
{
    [o_table_view reloadData];
305
306
}

307
- (void)updateRowSelection
hartman's avatar
ALL:    
hartman committed
308
309
310
311
312
313
314
315
316
317
318
{
    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;
    }
319
320

    vlc_mutex_lock( &p_playlist->object_lock );    
hartman's avatar
ALL:    
hartman committed
321
    i_row = p_playlist->i_index;
322
    vlc_mutex_unlock( &p_playlist->object_lock );
hartman's avatar
ALL:    
hartman committed
323
    vlc_object_release( p_playlist );
324

hartman's avatar
ALL:    
hartman committed
325
326
    [o_table_view selectRow: i_row byExtendingSelection: NO];
    [o_table_view scrollRowToVisible: i_row];
327

hartman's avatar
ALL:    
hartman committed
328
    vout_thread_t * p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
329
                                                      FIND_ANYWHERE );
hartman's avatar
ALL:    
hartman committed
330

331
    if( p_vout == NULL || !p_vout->b_fullscreen )
hartman's avatar
ALL:    
hartman committed
332
    {
333
        [[NSApp keyWindow] makeFirstResponder: o_table_view];
hartman's avatar
ALL:    
hartman committed
334
    }
335
336

    if( p_vout != NULL )
hartman's avatar
ALL:    
hartman committed
337
    {
338
        vlc_object_release( (vlc_object_t *)p_vout );
hartman's avatar
ALL:    
hartman committed
339
340
341
    }
}

342
343
344
345
346
347
348
349
350
@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,
351
                                                       FIND_ANYWHERE );
352
353
354

    if( p_playlist != NULL )
    {
355
        vlc_mutex_lock( &p_playlist->object_lock );
356
        i_count = p_playlist->i_size;
357
        vlc_mutex_unlock( &p_playlist->object_lock );
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
        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 );
379
380
    o_value = [[NSString stringWithUTF8String: 
        p_playlist->pp_items[i_row]->psz_name] lastPathComponent]; 
381
382
383
384
385
386
387
388
389
    vlc_mutex_unlock( &p_playlist->object_lock ); 

    vlc_object_release( p_playlist );

    return( o_value );
}

@end