]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* ALL: changes to the playlist_Add() and VLC_AddTarget() proto to include a list...
[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.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  * 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_pos enqueue:(BOOL)b_enqueue
290 {
291     int i_items;
292     NSString * o_value;
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,
296                                                        FIND_ANYWHERE );
297
298     if( p_playlist == NULL )
299     {
300         return;
301     }
302
303     i_items = 0;
304     o_enum = [o_array objectEnumerator];
305     while( ( o_value = [o_enum nextObject] ) )
306     {
307         NSURL * o_url;
308
309         int i_mode = PLAYLIST_INSERT;
310         
311         if (i_items == 0 && !b_enqueue)
312             i_mode |= PLAYLIST_GO;
313
314         playlist_Add( p_playlist, [o_value fileSystemRepresentation],
315             0, 0, i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );
316
317         o_url = [NSURL fileURLWithPath: o_value];
318         if( o_url != nil )
319         { 
320             [[NSDocumentController sharedDocumentController]
321                 noteNewRecentDocumentURL: o_url]; 
322         }
323
324         i_items++;
325     }
326
327     vlc_object_release( p_playlist );
328 }
329
330 - (void)playlistUpdated
331 {
332     [o_table_view reloadData];
333 }
334
335 - (void)updateRowSelection
336 {
337     int i_row;
338
339     intf_thread_t * p_intf = [NSApp getIntf];
340     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
341                                                        FIND_ANYWHERE );
342
343     if( p_playlist == NULL )
344     {
345         return;
346     }
347
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 );
352
353     [o_table_view selectRow: i_row byExtendingSelection: NO];
354     [o_table_view scrollRowToVisible: i_row];
355 }
356
357 @end
358
359 @implementation VLCPlaylist (NSTableDataSource)
360
361 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
362 {
363     int i_count = 0;
364     intf_thread_t * p_intf = [NSApp getIntf];
365     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
366                                                        FIND_ANYWHERE );
367
368     if( p_playlist != NULL )
369     {
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 );
374     }
375
376     return( i_count );
377 }
378
379 - (id)tableView:(NSTableView *)o_tv 
380                 objectValueForTableColumn:(NSTableColumn *)o_tc 
381                 row:(int)i_row
382 {
383     id o_value = nil;
384     intf_thread_t * p_intf = [NSApp getIntf];
385     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
386                                                FIND_ANYWHERE );
387
388     if( p_playlist == NULL )
389     {
390         return( nil );
391     }
392
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 ); 
397
398     vlc_object_release( p_playlist );
399
400     return( o_value );
401 }
402
403 - (BOOL)tableView:(NSTableView *)o_tv
404                     writeRows:(NSArray*)o_rows
405                     toPasteboard:(NSPasteboard*)o_pasteboard 
406 {
407     int i_rows = [o_rows count];
408     NSArray *o_filenames = [NSArray array];
409     
410     [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
411     [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
412     if ( i_rows == 1 )
413     {
414         i_moveRow = [[o_rows objectAtIndex:0]intValue];
415         return YES;
416     }
417     return NO;
418 }
419
420 - (NSDragOperation)tableView:(NSTableView*)o_tv
421                     validateDrop:(id <NSDraggingInfo>)o_info
422                     proposedRow:(int)i_row
423                     proposedDropOperation:(NSTableViewDropOperation)o_operation 
424 {
425     if ( o_operation == NSTableViewDropAbove )
426     {
427         if ( i_moveRow >= 0 )
428         {
429             if ( i_row != i_moveRow )
430             {
431                 return NSDragOperationMove;
432             }
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;
436         }
437         return NSDragOperationGeneric;
438     }
439     return NSDragOperationNone;
440 }
441
442 - (BOOL)tableView:(NSTableView*)o_tv
443                     acceptDrop:(id <NSDraggingInfo>)o_info
444                     row:(int)i_proposed_row
445                     dropOperation:(NSTableViewDropOperation)o_operation 
446 {
447     if (  i_moveRow >= 0 )
448     {
449         if (i_moveRow != -1 && i_proposed_row != -1)
450         {
451             intf_thread_t * p_intf = [NSApp getIntf];
452             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
453                                                             FIND_ANYWHERE );
454         
455             if( p_playlist == NULL )
456             {
457                 i_moveRow = -1;
458                 return NO;
459             }
460     
461             playlist_Move( p_playlist, i_moveRow, i_proposed_row ); 
462         
463             vlc_object_release( p_playlist );
464         }
465         [self playlistUpdated];
466         i_moveRow = -1;
467         return YES;
468     }
469     else
470     {
471         NSArray * o_values;
472         NSPasteboard * o_pasteboard;
473         
474         intf_thread_t * p_intf = [NSApp getIntf];
475         o_pasteboard = [o_info draggingPasteboard];
476         
477         if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
478         {
479             o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
480                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
481
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", "" );
486
487             [self appendArray: o_values atPos: i_proposed_row enqueue:YES];
488
489             return( YES );
490         }
491         
492         return( NO );
493     }
494     [self updateRowSelection];
495 }
496
497 @end
498