]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* extras/MacOSX/vlc.pbproj/project.pbxproj: Added a slew of file extensions
[vlc] / modules / gui / macosx / playlist.m
1 /*****************************************************************************
2  * playlist.m: MacOS X interface plugin
3  *****************************************************************************
4  * Copyright (C) 2002-2003 VideoLAN
5  * $Id: playlist.m,v 1.31 2003/09/19 23:03:27 hartman Exp $
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Derk-Jan Hartman <thedj@users.sourceforge.net>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <sys/param.h>                                    /* for MAXPATHLEN */
30 #include <string.h>
31 #include <math.h>
32
33 #include "intf.h"
34 #include "playlist.h"
35
36 int MacVersion102 = -1;
37
38 /*****************************************************************************
39  * VLCPlaylistView implementation 
40  *****************************************************************************/
41 @implementation VLCPlaylistView
42
43 - (void)dealloc
44 {
45     if( o_striped_row_color != nil )
46     {
47         [o_striped_row_color release];
48     }
49     [super dealloc];
50 }
51
52 - (NSMenu *)menuForEvent:(NSEvent *)o_event
53 {
54     return( [[self delegate] menuForEvent: o_event] );
55 }
56
57 - (void)keyDown:(NSEvent *)o_event
58 {
59     unichar key = 0;
60     int i, c, i_row;
61     NSMutableArray *o_to_delete;
62     NSNumber *o_number;
63     
64     playlist_t * p_playlist;
65     intf_thread_t * p_intf = [NSApp getIntf];
66
67     if( [[o_event characters] length] )
68     {
69         key = [[o_event characters] characterAtIndex: 0];
70     }
71
72     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
73                                           FIND_ANYWHERE );
74     
75     if ( p_playlist == NULL )
76     {
77         return;
78     }
79     
80     switch( key )
81     {
82         case ' ':
83             vlc_mutex_lock( &p_playlist->object_lock );
84             if( p_playlist->p_input != NULL )
85             {
86                 input_SetStatus( p_playlist->p_input, INPUT_STATUS_PAUSE );
87             }
88             vlc_mutex_unlock( &p_playlist->object_lock );
89             break;
90
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];
97             
98             for( i = 0; i < c; i++ ) {
99                 o_number = [o_to_delete lastObject];
100                 i_row = [o_number intValue];
101                 
102                 if( p_playlist->i_index == i_row && p_playlist->i_status )
103                 {
104                     playlist_Stop( p_playlist );
105                 }
106                 [o_to_delete removeObject: o_number];
107                 [self deselectRow: i_row];
108                 playlist_Delete( p_playlist, i_row );
109             }
110             [self reloadData];
111             break;
112             
113         default:
114             [super keyDown: o_event];
115             break;
116     }
117
118     if( p_playlist != NULL )
119     {
120         vlc_object_release( p_playlist );
121     }
122 }
123
124 - (void)highlightSelectionInClipRect:(NSRect)o_rect {
125     NSRect o_new_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;
129     
130     if ( i_row % 2 == 0 )
131     {
132         i_row++;
133     }
134     
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;
139    
140     if( ( MacVersion102 < 0 ) && ( floor( NSAppKitVersionNumber ) > NSAppKitVersionNumber10_1 ) )
141     {
142         MacVersion102 = 102;
143     }
144  
145     if ( MacVersion102 == 102 && o_striped_row_color == nil )
146     {
147         o_striped_row_color = [[[NSColor alternateSelectedControlColor]
148                                 highlightWithLevel: 0.90] retain];
149         
150     }
151     else if ( o_striped_row_color == nil )
152     {
153         /* OSX 10.1 and before ain't that smart ;) */
154         o_striped_row_color = [[NSColor whiteColor] retain];
155     }
156
157     [o_striped_row_color set];
158     
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;
162     }
163     [super highlightSelectionInClipRect:o_rect];
164 }
165
166 @end
167
168 /*****************************************************************************
169  * VLCPlaylist implementation 
170  *****************************************************************************/
171 @implementation VLCPlaylist
172
173 - (id)init
174 {
175     self = [super init];
176     if ( self !=nil )
177     {
178         i_moveRow = -1;
179     }
180     return self;
181 }
182
183 - (void)awakeFromNib
184 {
185     [o_table_view setTarget: self];
186     [o_table_view setDelegate: self];
187     [o_table_view setDataSource: self];
188
189     [o_table_view setDoubleAction: @selector(playItem:)];
190
191     [o_table_view registerForDraggedTypes: 
192         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
193
194     [o_mi_play setTitle: _NS("Play")];
195     [o_mi_delete setTitle: _NS("Delete")];
196     [o_mi_selectall setTitle: _NS("Select All")];
197     
198     [o_btn_add setToolTip: _NS("Add")];
199     [o_btn_remove setToolTip: _NS("Delete")];
200 }
201
202 - (BOOL)tableView:(NSTableView *)o_tv 
203                   shouldEditTableColumn:(NSTableColumn *)o_tc
204                   row:(int)i_row
205 {
206     return( NO );
207 }
208
209 - (NSMenu *)menuForEvent:(NSEvent *)o_event
210 {
211     NSPoint pt;
212     vlc_bool_t b_rows;
213     vlc_bool_t b_item_sel;
214
215     pt = [o_table_view convertPoint: [o_event locationInWindow] 
216                                                  fromView: nil];
217     b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
218                    [o_table_view selectedRow] != -1 );
219     b_rows = [o_table_view numberOfRows] != 0;
220
221     [o_mi_play setEnabled: b_item_sel];
222     [o_mi_delete setEnabled: b_item_sel];
223     [o_mi_selectall setEnabled: b_rows];
224
225     return( o_ctx_menu );
226 }
227
228 - (IBAction)playItem:(id)sender
229 {
230     intf_thread_t * p_intf = [NSApp getIntf];
231     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
232                                                        FIND_ANYWHERE );
233
234     if( p_playlist == NULL )
235     {
236         return;
237     }
238
239     playlist_Goto( p_playlist, [o_table_view selectedRow] );
240
241     vlc_object_release( p_playlist );
242 }
243
244 - (IBAction)deleteItems:(id)sender
245 {
246     int i, c, i_row;
247     NSMutableArray *o_to_delete;
248     NSNumber *o_number;
249
250     intf_thread_t * p_intf = [NSApp getIntf];
251     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
252                                                        FIND_ANYWHERE );
253
254     if( p_playlist == NULL )
255     {
256         return;
257     }
258     
259     o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
260     c = [o_to_delete count];
261     
262     for( i = 0; i < c; i++ ) {
263         o_number = [o_to_delete lastObject];
264         i_row = [o_number intValue];
265         
266         if( p_playlist->i_index == i_row && p_playlist->i_status )
267         {
268             playlist_Stop( p_playlist );
269         }
270         [o_to_delete removeObject: o_number];
271         [o_table_view deselectRow: i_row];
272         playlist_Delete( p_playlist, i_row );
273     }
274
275     vlc_object_release( p_playlist );
276
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];
282 }
283
284 - (IBAction)selectAll:(id)sender
285 {
286     [o_table_view selectAll: nil];
287 }
288
289 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
290 {
291     int i_item;
292     intf_thread_t * p_intf = [NSApp getIntf];
293     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
294                                                        FIND_ANYWHERE );
295
296     if( p_playlist == NULL )
297     {
298         return;
299     }
300
301     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
302     {
303         /* One item */
304         NSDictionary *o_one_item;
305         NSString *o_url;
306         NSString *o_name;
307         NSArray *o_options;
308         int j, i_total_options = 0;
309         char **ppsz_options = NULL;
310         int i_mode = PLAYLIST_INSERT;
311         
312         /* Get the item */
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"];
317         
318         if( !o_name) o_name = o_url;
319         
320         if (i_item == 0 && !b_enqueue)
321             i_mode |= PLAYLIST_GO;
322
323         if( o_options && [o_options count] > 0 )
324         {
325             /* Count the input options */
326             i_total_options = [o_options count];
327     
328             /* Allocate ppsz_options */
329             for( j = 0; j < i_total_options; j++ )
330             {
331                 if( !ppsz_options )
332                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
333     
334                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
335             }
336         }
337         
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 );
341
342         /* clean up */
343         for( j = 0; j < i_total_options; j++ )
344             free( ppsz_options[j] );
345         if( ppsz_options ) free( ppsz_options );
346
347         NSURL *o_true_url = [NSURL fileURLWithPath: o_url];
348         if( o_true_url != nil )
349         { 
350             [[NSDocumentController sharedDocumentController]
351                 noteNewRecentDocumentURL: o_true_url]; 
352         }
353     }
354
355     vlc_object_release( p_playlist );
356 }
357
358 - (void)playlistUpdated
359 {
360     [o_table_view reloadData];
361 }
362
363 - (void)updateRowSelection
364 {
365     int i_row;
366
367     intf_thread_t * p_intf = [NSApp getIntf];
368     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
369                                                        FIND_ANYWHERE );
370
371     if( p_playlist == NULL )
372     {
373         return;
374     }
375
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 );
380
381     [o_table_view selectRow: i_row byExtendingSelection: NO];
382     [o_table_view scrollRowToVisible: i_row];
383 }
384
385 @end
386
387 @implementation VLCPlaylist (NSTableDataSource)
388
389 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
390 {
391     int i_count = 0;
392     intf_thread_t * p_intf = [NSApp getIntf];
393     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
394                                                        FIND_ANYWHERE );
395
396     if( p_playlist != NULL )
397     {
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 );
402     }
403
404     return( i_count );
405 }
406
407 - (id)tableView:(NSTableView *)o_tv 
408                 objectValueForTableColumn:(NSTableColumn *)o_tc 
409                 row:(int)i_row
410 {
411     id o_value = nil;
412     intf_thread_t * p_intf = [NSApp getIntf];
413     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
414                                                FIND_ANYWHERE );
415
416     if( p_playlist == NULL )
417     {
418         return( nil );
419     }
420
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 ); 
425
426     vlc_object_release( p_playlist );
427
428     return( o_value );
429 }
430
431 - (BOOL)tableView:(NSTableView *)o_tv
432                     writeRows:(NSArray*)o_rows
433                     toPasteboard:(NSPasteboard*)o_pasteboard 
434 {
435     int i_rows = [o_rows count];
436     NSArray *o_filenames = [NSArray array];
437     
438     [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
439     [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
440     if ( i_rows == 1 )
441     {
442         i_moveRow = [[o_rows objectAtIndex:0]intValue];
443         return YES;
444     }
445     return NO;
446 }
447
448 - (NSDragOperation)tableView:(NSTableView*)o_tv
449                     validateDrop:(id <NSDraggingInfo>)o_info
450                     proposedRow:(int)i_row
451                     proposedDropOperation:(NSTableViewDropOperation)o_operation 
452 {
453     if ( o_operation == NSTableViewDropAbove )
454     {
455         if ( i_moveRow >= 0 )
456         {
457             if ( i_row != i_moveRow )
458             {
459                 return NSDragOperationMove;
460             }
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;
464         }
465         return NSDragOperationGeneric;
466     }
467     return NSDragOperationNone;
468 }
469
470 - (BOOL)tableView:(NSTableView*)o_tv
471                     acceptDrop:(id <NSDraggingInfo>)o_info
472                     row:(int)i_proposed_row
473                     dropOperation:(NSTableViewDropOperation)o_operation 
474 {
475     if (  i_moveRow >= 0 )
476     {
477         if (i_moveRow != -1 && i_proposed_row != -1)
478         {
479             intf_thread_t * p_intf = [NSApp getIntf];
480             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
481                                                             FIND_ANYWHERE );
482         
483             if( p_playlist == NULL )
484             {
485                 i_moveRow = -1;
486                 return NO;
487             }
488     
489             playlist_Move( p_playlist, i_moveRow, i_proposed_row ); 
490         
491             vlc_object_release( p_playlist );
492         }
493         [self playlistUpdated];
494         i_moveRow = -1;
495         return YES;
496     }
497     else
498     {
499         NSPasteboard * o_pasteboard;
500         o_pasteboard = [o_info draggingPasteboard];
501         
502         if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
503         {
504             int i;
505             NSArray *o_array = [NSArray array];
506             NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
507                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
508
509             for( i = 0; i < (int)[o_values count]; i++)
510             {
511                 NSDictionary *o_dic;
512                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
513                 o_array = [o_array arrayByAddingObject: o_dic];
514             }
515             [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
516
517             return( YES );
518         }
519         
520         return( NO );
521     }
522     [self updateRowSelection];
523 }
524
525 @end
526