]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* First set config variable, then add the files (and therefore potentially
[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.22 2003/05/12 01:17:10 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
32 #include "intf.h"
33 #include "playlist.h"
34
35 /*****************************************************************************
36  * VLCPlaylistView implementation 
37  *****************************************************************************/
38 @implementation VLCPlaylistView
39
40 - (void)dealloc
41 {
42     if( o_striped_row_color != nil )
43     {
44         [o_striped_row_color release];
45     }
46     [super dealloc];
47 }
48
49 - (NSMenu *)menuForEvent:(NSEvent *)o_event
50 {
51     return( [[self delegate] menuForEvent: o_event] );
52 }
53
54 - (void)keyDown:(NSEvent *)o_event
55 {
56     unichar key = 0;
57     int i_row;
58     playlist_t * p_playlist;
59     intf_thread_t * p_intf = [NSApp getIntf];
60
61     if( [[o_event characters] length] )
62     {
63         key = [[o_event characters] characterAtIndex: 0];
64     }
65
66     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
67                                           FIND_ANYWHERE );
68     
69     if ( p_playlist == NULL )
70     {
71         return;
72     }
73     
74     switch( key )
75     {
76         case ' ':
77             vlc_mutex_lock( &p_playlist->object_lock );
78             if( p_playlist->p_input != NULL )
79             {
80                 input_SetStatus( p_playlist->p_input, INPUT_STATUS_PAUSE );
81             }
82             vlc_mutex_unlock( &p_playlist->object_lock );
83             break;
84
85         case NSDeleteCharacter:
86         case NSDeleteFunctionKey:
87         case NSDeleteCharFunctionKey:
88         case NSBackspaceCharacter:
89             while( ( i_row = [self selectedRow] ) != -1 )
90             {
91                 if( p_playlist->i_index == i_row && p_playlist->i_status )
92                 {
93                     playlist_Stop( p_playlist );
94                 }
95         
96                 playlist_Delete( p_playlist, i_row ); 
97         
98                 [self deselectRow: i_row];
99             }
100             [self reloadData];
101             break;
102             
103         default:
104             [super keyDown: o_event];
105             break;
106     }
107
108     if( p_playlist != NULL )
109     {
110         vlc_object_release( p_playlist );
111     }
112 }
113
114 - (void) highlightSelectionInClipRect:(NSRect)o_rect {
115     NSRect o_new_rect;
116     float f_height = [self rowHeight] + [self intercellSpacing].height;
117     float f_origin_y = NSMaxY( o_rect );
118     int i_row = o_rect.origin.y / f_height;
119     
120     if ( i_row % 2 == 0 )
121     {
122         i_row++;
123     }
124     
125     o_new_rect.size.width = o_rect.size.width;
126     o_new_rect.size.height = f_height;
127     o_new_rect.origin.x = o_rect.origin.x;
128     o_new_rect.origin.y = i_row * f_height;
129     
130     if ( o_striped_row_color == nil )
131     {
132         o_striped_row_color = [[[NSColor alternateSelectedControlColor]
133                                 highlightWithLevel: 0.90] retain];
134     }
135     [o_striped_row_color set];
136     
137     while ( o_new_rect.origin.y < f_origin_y ) {
138         NSRectFill( o_new_rect );
139         o_new_rect.origin.y += f_height * 2.0;
140     }
141     [super highlightSelectionInClipRect:o_rect];
142 }
143
144 @end
145
146 /*****************************************************************************
147  * VLCPlaylist implementation 
148  *****************************************************************************/
149 @implementation VLCPlaylist
150
151 - (id)init
152 {
153     self = [super init];
154     if ( self !=nil )
155     {
156         i_moveRow = -1;
157     }
158     return self;
159 }
160
161 - (void)awakeFromNib
162 {
163     [o_table_view setTarget: self];
164     [o_table_view setDelegate: self];
165     [o_table_view setDataSource: self];
166
167     [o_table_view setDoubleAction: @selector(playItem:)];
168
169     [o_table_view registerForDraggedTypes: 
170         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
171
172     [o_mi_play setTitle: _NS("Play")];
173     [o_mi_delete setTitle: _NS("Delete")];
174     [o_mi_selectall setTitle: _NS("Select All")];
175     
176     [o_btn_add setToolTip: _NS("Add")];
177     [o_btn_remove setToolTip: _NS("Delete")];
178 }
179
180 - (BOOL)tableView:(NSTableView *)o_tv 
181                   shouldEditTableColumn:(NSTableColumn *)o_tc
182                   row:(int)i_row
183 {
184     return( NO );
185 }
186
187 - (NSMenu *)menuForEvent:(NSEvent *)o_event
188 {
189     NSPoint pt;
190     vlc_bool_t b_rows;
191     vlc_bool_t b_item_sel;
192
193     pt = [o_table_view convertPoint: [o_event locationInWindow] 
194                                                  fromView: nil];
195     b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
196                    [o_table_view selectedRow] != -1 );
197     b_rows = [o_table_view numberOfRows] != 0;
198
199     [o_mi_play setEnabled: b_item_sel];
200     [o_mi_delete setEnabled: b_item_sel];
201     [o_mi_selectall setEnabled: b_rows];
202
203     return( o_ctx_menu );
204 }
205
206 - (IBAction)playItem:(id)sender
207 {
208     intf_thread_t * p_intf = [NSApp getIntf];
209     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
210                                                        FIND_ANYWHERE );
211
212     if( p_playlist == NULL )
213     {
214         return;
215     }
216
217     playlist_Goto( p_playlist, [o_table_view selectedRow] );
218
219     vlc_object_release( p_playlist );
220 }
221
222 - (IBAction)deleteItems:(id)sender
223 {
224     int i_row;
225
226     intf_thread_t * p_intf = [NSApp getIntf];
227     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
228                                                        FIND_ANYWHERE );
229
230     if( p_playlist == NULL )
231     {
232         return;
233     }
234
235     while( ( i_row = [o_table_view selectedRow] ) != -1 )
236     {
237         if( p_playlist->i_index == i_row && p_playlist->i_status )
238         {
239             playlist_Stop( p_playlist );
240         }
241
242         playlist_Delete( p_playlist, i_row ); 
243
244         [o_table_view deselectRow: i_row];
245     }
246
247     vlc_object_release( p_playlist );
248
249     /* this is actually duplicity, because the intf.m manage also updates the view
250      * when the playlist changes. we do this on purpose, because else there is a 
251      * delay of .5 sec or so when we delete an item */
252     [self playlistUpdated];
253     [self updateRowSelection];
254 }
255
256 - (IBAction)selectAll:(id)sender
257 {
258     [o_table_view selectAll: nil];
259 }
260
261 - (void)appendArray:(NSArray*)o_array atPos:(int)i_pos enqueue:(BOOL)b_enqueue
262 {
263     int i_items;
264     NSString * o_value;
265     NSEnumerator * o_enum;
266     intf_thread_t * p_intf = [NSApp getIntf];
267     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
268                                                        FIND_ANYWHERE );
269
270     if( p_playlist == NULL )
271     {
272         return;
273     }
274
275     i_items = 0;
276     o_enum = [o_array objectEnumerator];
277     while( ( o_value = [o_enum nextObject] ) )
278     {
279         NSURL * o_url;
280
281         int i_mode = PLAYLIST_INSERT;
282         
283         if (i_items == 0 && !b_enqueue)
284             i_mode |= PLAYLIST_GO;
285
286         playlist_Add( p_playlist, [o_value fileSystemRepresentation],
287             i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );
288
289         o_url = [NSURL fileURLWithPath: o_value];
290         if( o_url != nil )
291         { 
292             [[NSDocumentController sharedDocumentController]
293                 noteNewRecentDocumentURL: o_url]; 
294         }
295
296         i_items++;
297     }
298
299     vlc_object_release( p_playlist );
300 }
301
302 - (void)playlistUpdated
303 {
304     [o_table_view reloadData];
305 }
306
307 - (void)updateRowSelection
308 {
309     int i_row;
310
311     intf_thread_t * p_intf = [NSApp getIntf];
312     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
313                                                        FIND_ANYWHERE );
314
315     if( p_playlist == NULL )
316     {
317         return;
318     }
319
320     vlc_mutex_lock( &p_playlist->object_lock );    
321     i_row = p_playlist->i_index;
322     vlc_mutex_unlock( &p_playlist->object_lock );
323     vlc_object_release( p_playlist );
324
325     [o_table_view selectRow: i_row byExtendingSelection: NO];
326     [o_table_view scrollRowToVisible: i_row];
327 }
328
329 @end
330
331 @implementation VLCPlaylist (NSTableDataSource)
332
333 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
334 {
335     int i_count = 0;
336     intf_thread_t * p_intf = [NSApp getIntf];
337     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
338                                                        FIND_ANYWHERE );
339
340     if( p_playlist != NULL )
341     {
342         vlc_mutex_lock( &p_playlist->object_lock );
343         i_count = p_playlist->i_size;
344         vlc_mutex_unlock( &p_playlist->object_lock );
345         vlc_object_release( p_playlist );
346     }
347
348     return( i_count );
349 }
350
351 - (id)tableView:(NSTableView *)o_tv 
352                 objectValueForTableColumn:(NSTableColumn *)o_tc 
353                 row:(int)i_row
354 {
355     id o_value = nil;
356     intf_thread_t * p_intf = [NSApp getIntf];
357     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
358                                                FIND_ANYWHERE );
359
360     if( p_playlist == NULL )
361     {
362         return( nil );
363     }
364
365     vlc_mutex_lock( &p_playlist->object_lock );
366     o_value = [[NSString stringWithUTF8String: 
367         p_playlist->pp_items[i_row]->psz_name] lastPathComponent]; 
368     vlc_mutex_unlock( &p_playlist->object_lock ); 
369
370     vlc_object_release( p_playlist );
371
372     return( o_value );
373 }
374
375 - (BOOL)tableView:(NSTableView *)o_tv
376                     writeRows:(NSArray*)o_rows
377                     toPasteboard:(NSPasteboard*)o_pasteboard 
378 {
379     int i_rows = [o_rows count];
380     NSArray *o_filenames = [NSArray array];
381     
382     [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
383     [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
384     if ( i_rows == 1 )
385     {
386         i_moveRow = [[o_rows objectAtIndex:0]intValue];
387         return YES;
388     }
389     return NO;
390 }
391
392 - (NSDragOperation)tableView:(NSTableView*)o_tv
393                     validateDrop:(id <NSDraggingInfo>)o_info
394                     proposedRow:(int)i_row
395                     proposedDropOperation:(NSTableViewDropOperation)o_operation 
396 {
397     if ( o_operation == NSTableViewDropAbove )
398     {
399         if ( i_moveRow >= 0 )
400         {
401             if ( i_row != i_moveRow )
402             {
403                 return NSDragOperationMove;
404             }
405             /* what if in the previous run, the row wasn't actually moved? 
406                then we can't drop new files on this location */
407             return NSDragOperationNone;
408         }
409         return NSDragOperationGeneric;
410     }
411     return NSDragOperationNone;
412 }
413
414 - (BOOL)tableView:(NSTableView*)o_tv
415                     acceptDrop:(id <NSDraggingInfo>)o_info
416                     row:(int)i_proposed_row
417                     dropOperation:(NSTableViewDropOperation)o_operation 
418 {
419     if (  i_moveRow >= 0 )
420     {
421         if (i_moveRow != -1 && i_proposed_row != -1)
422         {
423             intf_thread_t * p_intf = [NSApp getIntf];
424             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
425                                                             FIND_ANYWHERE );
426         
427             if( p_playlist == NULL )
428             {
429                 i_moveRow = -1;
430                 return NO;
431             }
432     
433             playlist_Move( p_playlist, i_moveRow, i_proposed_row ); 
434         
435             vlc_object_release( p_playlist );
436         }
437         [self playlistUpdated];
438         i_moveRow = -1;
439         return YES;
440     }
441     else
442     {
443         NSArray * o_values;
444         NSPasteboard * o_pasteboard;
445         
446         intf_thread_t * p_intf = [NSApp getIntf];
447         o_pasteboard = [o_info draggingPasteboard];
448         
449         if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
450         {
451             o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
452                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
453
454             config_PutPsz( p_intf, "sub-file", "" );
455             config_PutInt( p_intf, "sub-delay", 0 );
456             config_PutFloat( p_intf, "sub-fps", 0.0 );
457             config_PutPsz( p_intf, "sout", "" );
458
459             [self appendArray: o_values atPos: i_proposed_row enqueue:YES];
460
461             return( YES );
462         }
463         
464         return( NO );
465     }
466     [self updateRowSelection];
467 }
468
469 @end
470