]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* modules/codec/libmpeg2.c: fixed a crasher with invalid pictures
[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.25 2003/06/02 12:42:15 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         if ( o_striped_row_color == nil )
136         {
137             /* OSX 10.1 and before ain't that smart ;) */
138             o_striped_row_color = [[NSColor whiteColor] retain];
139         }
140     }
141     [o_striped_row_color set];
142     
143     while ( o_new_rect.origin.y < f_origin_y ) {
144         NSRectFill( o_new_rect );
145         o_new_rect.origin.y += f_height * 2.0;
146     }
147     [super highlightSelectionInClipRect:o_rect];
148 }
149
150 @end
151
152 /*****************************************************************************
153  * VLCPlaylist implementation 
154  *****************************************************************************/
155 @implementation VLCPlaylist
156
157 - (id)init
158 {
159     self = [super init];
160     if ( self !=nil )
161     {
162         i_moveRow = -1;
163     }
164     return self;
165 }
166
167 - (void)awakeFromNib
168 {
169     [o_table_view setTarget: self];
170     [o_table_view setDelegate: self];
171     [o_table_view setDataSource: self];
172
173     [o_table_view setDoubleAction: @selector(playItem:)];
174
175     [o_table_view registerForDraggedTypes: 
176         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
177
178     [o_mi_play setTitle: _NS("Play")];
179     [o_mi_delete setTitle: _NS("Delete")];
180     [o_mi_selectall setTitle: _NS("Select All")];
181     
182     [o_btn_add setToolTip: _NS("Add")];
183     [o_btn_remove setToolTip: _NS("Delete")];
184 }
185
186 - (BOOL)tableView:(NSTableView *)o_tv 
187                   shouldEditTableColumn:(NSTableColumn *)o_tc
188                   row:(int)i_row
189 {
190     return( NO );
191 }
192
193 - (NSMenu *)menuForEvent:(NSEvent *)o_event
194 {
195     NSPoint pt;
196     vlc_bool_t b_rows;
197     vlc_bool_t b_item_sel;
198
199     pt = [o_table_view convertPoint: [o_event locationInWindow] 
200                                                  fromView: nil];
201     b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
202                    [o_table_view selectedRow] != -1 );
203     b_rows = [o_table_view numberOfRows] != 0;
204
205     [o_mi_play setEnabled: b_item_sel];
206     [o_mi_delete setEnabled: b_item_sel];
207     [o_mi_selectall setEnabled: b_rows];
208
209     return( o_ctx_menu );
210 }
211
212 - (IBAction)playItem:(id)sender
213 {
214     intf_thread_t * p_intf = [NSApp getIntf];
215     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
216                                                        FIND_ANYWHERE );
217
218     if( p_playlist == NULL )
219     {
220         return;
221     }
222
223     playlist_Goto( p_playlist, [o_table_view selectedRow] );
224
225     vlc_object_release( p_playlist );
226 }
227
228 - (IBAction)deleteItems:(id)sender
229 {
230     int i_row;
231
232     intf_thread_t * p_intf = [NSApp getIntf];
233     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
234                                                        FIND_ANYWHERE );
235
236     if( p_playlist == NULL )
237     {
238         return;
239     }
240
241     while( ( i_row = [o_table_view selectedRow] ) != -1 )
242     {
243         if( p_playlist->i_index == i_row && p_playlist->i_status )
244         {
245             playlist_Stop( p_playlist );
246         }
247
248         playlist_Delete( p_playlist, i_row ); 
249
250         [o_table_view deselectRow: i_row];
251     }
252
253     vlc_object_release( p_playlist );
254
255     /* this is actually duplicity, because the intf.m manage also updates the view
256      * when the playlist changes. we do this on purpose, because else there is a 
257      * delay of .5 sec or so when we delete an item */
258     [self playlistUpdated];
259     [self updateRowSelection];
260 }
261
262 - (IBAction)selectAll:(id)sender
263 {
264     [o_table_view selectAll: nil];
265 }
266
267 - (void)appendArray:(NSArray*)o_array atPos:(int)i_pos enqueue:(BOOL)b_enqueue
268 {
269     int i_items;
270     NSString * o_value;
271     NSEnumerator * o_enum;
272     intf_thread_t * p_intf = [NSApp getIntf];
273     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
274                                                        FIND_ANYWHERE );
275
276     if( p_playlist == NULL )
277     {
278         return;
279     }
280
281     i_items = 0;
282     o_enum = [o_array objectEnumerator];
283     while( ( o_value = [o_enum nextObject] ) )
284     {
285         NSURL * o_url;
286
287         int i_mode = PLAYLIST_INSERT;
288         
289         if (i_items == 0 && !b_enqueue)
290             i_mode |= PLAYLIST_GO;
291
292         playlist_Add( p_playlist, [o_value fileSystemRepresentation],
293             i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );
294
295         o_url = [NSURL fileURLWithPath: o_value];
296         if( o_url != nil )
297         { 
298             [[NSDocumentController sharedDocumentController]
299                 noteNewRecentDocumentURL: o_url]; 
300         }
301
302         i_items++;
303     }
304
305     vlc_object_release( p_playlist );
306 }
307
308 - (void)playlistUpdated
309 {
310     [o_table_view reloadData];
311 }
312
313 - (void)updateRowSelection
314 {
315     int i_row;
316
317     intf_thread_t * p_intf = [NSApp getIntf];
318     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
319                                                        FIND_ANYWHERE );
320
321     if( p_playlist == NULL )
322     {
323         return;
324     }
325
326     vlc_mutex_lock( &p_playlist->object_lock );    
327     i_row = p_playlist->i_index;
328     vlc_mutex_unlock( &p_playlist->object_lock );
329     vlc_object_release( p_playlist );
330
331     [o_table_view selectRow: i_row byExtendingSelection: NO];
332     [o_table_view scrollRowToVisible: i_row];
333 }
334
335 @end
336
337 @implementation VLCPlaylist (NSTableDataSource)
338
339 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
340 {
341     int i_count = 0;
342     intf_thread_t * p_intf = [NSApp getIntf];
343     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
344                                                        FIND_ANYWHERE );
345
346     if( p_playlist != NULL )
347     {
348         vlc_mutex_lock( &p_playlist->object_lock );
349         i_count = p_playlist->i_size;
350         vlc_mutex_unlock( &p_playlist->object_lock );
351         vlc_object_release( p_playlist );
352     }
353
354     return( i_count );
355 }
356
357 - (id)tableView:(NSTableView *)o_tv 
358                 objectValueForTableColumn:(NSTableColumn *)o_tc 
359                 row:(int)i_row
360 {
361     id o_value = nil;
362     intf_thread_t * p_intf = [NSApp getIntf];
363     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
364                                                FIND_ANYWHERE );
365
366     if( p_playlist == NULL )
367     {
368         return( nil );
369     }
370
371     vlc_mutex_lock( &p_playlist->object_lock );
372     o_value = [[NSString stringWithUTF8String: 
373         p_playlist->pp_items[i_row]->psz_name] lastPathComponent]; 
374     vlc_mutex_unlock( &p_playlist->object_lock ); 
375
376     vlc_object_release( p_playlist );
377
378     return( o_value );
379 }
380
381 - (BOOL)tableView:(NSTableView *)o_tv
382                     writeRows:(NSArray*)o_rows
383                     toPasteboard:(NSPasteboard*)o_pasteboard 
384 {
385     int i_rows = [o_rows count];
386     NSArray *o_filenames = [NSArray array];
387     
388     [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
389     [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
390     if ( i_rows == 1 )
391     {
392         i_moveRow = [[o_rows objectAtIndex:0]intValue];
393         return YES;
394     }
395     return NO;
396 }
397
398 - (NSDragOperation)tableView:(NSTableView*)o_tv
399                     validateDrop:(id <NSDraggingInfo>)o_info
400                     proposedRow:(int)i_row
401                     proposedDropOperation:(NSTableViewDropOperation)o_operation 
402 {
403     if ( o_operation == NSTableViewDropAbove )
404     {
405         if ( i_moveRow >= 0 )
406         {
407             if ( i_row != i_moveRow )
408             {
409                 return NSDragOperationMove;
410             }
411             /* what if in the previous run, the row wasn't actually moved? 
412                then we can't drop new files on this location */
413             return NSDragOperationNone;
414         }
415         return NSDragOperationGeneric;
416     }
417     return NSDragOperationNone;
418 }
419
420 - (BOOL)tableView:(NSTableView*)o_tv
421                     acceptDrop:(id <NSDraggingInfo>)o_info
422                     row:(int)i_proposed_row
423                     dropOperation:(NSTableViewDropOperation)o_operation 
424 {
425     if (  i_moveRow >= 0 )
426     {
427         if (i_moveRow != -1 && i_proposed_row != -1)
428         {
429             intf_thread_t * p_intf = [NSApp getIntf];
430             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
431                                                             FIND_ANYWHERE );
432         
433             if( p_playlist == NULL )
434             {
435                 i_moveRow = -1;
436                 return NO;
437             }
438     
439             playlist_Move( p_playlist, i_moveRow, i_proposed_row ); 
440         
441             vlc_object_release( p_playlist );
442         }
443         [self playlistUpdated];
444         i_moveRow = -1;
445         return YES;
446     }
447     else
448     {
449         NSArray * o_values;
450         NSPasteboard * o_pasteboard;
451         
452         intf_thread_t * p_intf = [NSApp getIntf];
453         o_pasteboard = [o_info draggingPasteboard];
454         
455         if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
456         {
457             o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
458                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
459
460             config_PutPsz( p_intf, "sub-file", "" );
461             config_PutInt( p_intf, "sub-delay", 0 );
462             config_PutFloat( p_intf, "sub-fps", 0.0 );
463             config_PutPsz( p_intf, "sout", "" );
464
465             [self appendArray: o_values atPos: i_proposed_row enqueue:YES];
466
467             return( YES );
468         }
469         
470         return( NO );
471     }
472     [self updateRowSelection];
473 }
474
475 @end
476