]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* changed how cursor hiding works. Not perfect, but it's less complicated.
[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.35 2003/11/06 16:28:28 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 #include <sys/mount.h>
33
34 #include "intf.h"
35 #include "playlist.h"
36 #include "controls.h"
37
38 int MacVersion102 = -1;
39
40 /*****************************************************************************
41  * VLCPlaylistView implementation 
42  *****************************************************************************/
43 @implementation VLCPlaylistView
44
45 - (void)dealloc
46 {
47     if( o_striped_row_color != nil )
48     {
49         [o_striped_row_color release];
50     }
51     [super dealloc];
52 }
53
54 - (NSMenu *)menuForEvent:(NSEvent *)o_event
55 {
56     return( [[self delegate] menuForEvent: o_event] );
57 }
58
59 - (void)keyDown:(NSEvent *)o_event
60 {
61     unichar key = 0;
62     int i, c, i_row;
63     NSMutableArray *o_to_delete;
64     NSNumber *o_number;
65     
66     playlist_t * p_playlist;
67     intf_thread_t * p_intf = [NSApp getIntf];
68
69     if( [[o_event characters] length] )
70     {
71         key = [[o_event characters] characterAtIndex: 0];
72     }
73
74     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
75                                           FIND_ANYWHERE );
76     
77     if ( p_playlist == NULL )
78     {
79         return;
80     }
81     
82     switch( key )
83     {
84         case NSDeleteCharacter:
85         case NSDeleteFunctionKey:
86         case NSDeleteCharFunctionKey:
87         case NSBackspaceCharacter:
88             o_to_delete = [NSMutableArray arrayWithArray:[[self selectedRowEnumerator] allObjects]];
89             c = [o_to_delete count];
90             
91             for( i = 0; i < c; i++ ) {
92                 o_number = [o_to_delete lastObject];
93                 i_row = [o_number intValue];
94                 
95                 if( p_playlist->i_index == i_row && p_playlist->i_status )
96                 {
97                     playlist_Stop( p_playlist );
98                 }
99                 [o_to_delete removeObject: o_number];
100                 [self deselectRow: i_row];
101                 playlist_Delete( p_playlist, i_row );
102             }
103             [self reloadData];
104             break;
105             
106         default:
107             [super keyDown: o_event];
108             break;
109     }
110
111     if( p_playlist != NULL )
112     {
113         vlc_object_release( p_playlist );
114     }
115 }
116
117 - (void)highlightSelectionInClipRect:(NSRect)o_rect {
118     NSRect o_new_rect;
119     float f_height = [self rowHeight] + [self intercellSpacing].height;
120     float f_origin_y = NSMaxY( o_rect );
121     int i_row = o_rect.origin.y / f_height;
122     
123     if ( i_row % 2 == 0 )
124     {
125         i_row++;
126     }
127     
128     o_new_rect.size.width = o_rect.size.width;
129     o_new_rect.size.height = f_height;
130     o_new_rect.origin.x = o_rect.origin.x;
131     o_new_rect.origin.y = i_row * f_height;
132    
133     if( ( MacVersion102 < 0 ) && ( floor( NSAppKitVersionNumber ) > NSAppKitVersionNumber10_1 ) )
134     {
135         MacVersion102 = 102;
136     }
137  
138     if ( MacVersion102 == 102 && o_striped_row_color == nil )
139     {
140         o_striped_row_color = [[[NSColor alternateSelectedControlColor]
141                                 highlightWithLevel: 0.90] retain];
142         
143     }
144     else if ( o_striped_row_color == nil )
145     {
146         /* OSX 10.1 and before ain't that smart ;) */
147         o_striped_row_color = [[NSColor whiteColor] retain];
148     }
149
150     [o_striped_row_color set];
151     
152     while ( o_new_rect.origin.y < f_origin_y ) {
153         NSRectFill( o_new_rect );
154         o_new_rect.origin.y += f_height * 2.0;
155     }
156     [super highlightSelectionInClipRect:o_rect];
157 }
158
159 @end
160
161 /*****************************************************************************
162  * VLCPlaylist implementation 
163  *****************************************************************************/
164 @implementation VLCPlaylist
165
166 - (id)init
167 {
168     self = [super init];
169     if ( self !=nil )
170     {
171         i_moveRow = -1;
172     }
173     return self;
174 }
175
176 - (void)awakeFromNib
177 {
178     [o_table_view setTarget: self];
179     [o_table_view setDelegate: self];
180     [o_table_view setDataSource: self];
181
182     [o_table_view setDoubleAction: @selector(playItem:)];
183
184     [o_table_view registerForDraggedTypes: 
185         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
186
187     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
188     [o_mi_play setTitle: _NS("Play")];
189     [o_mi_delete setTitle: _NS("Delete")];
190     [o_mi_selectall setTitle: _NS("Select All")];
191     
192     [o_btn_add setToolTip: _NS("Add")];
193     [o_btn_remove setToolTip: _NS("Delete")];
194 }
195
196 - (BOOL)tableView:(NSTableView *)o_tv 
197                   shouldEditTableColumn:(NSTableColumn *)o_tc
198                   row:(int)i_row
199 {
200     return( NO );
201 }
202
203 - (NSMenu *)menuForEvent:(NSEvent *)o_event
204 {
205     NSPoint pt;
206     vlc_bool_t b_rows;
207     vlc_bool_t b_item_sel;
208
209     pt = [o_table_view convertPoint: [o_event locationInWindow] 
210                                                  fromView: nil];
211     b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
212                    [o_table_view selectedRow] != -1 );
213     b_rows = [o_table_view numberOfRows] != 0;
214
215     [o_mi_play setEnabled: b_item_sel];
216     [o_mi_delete setEnabled: b_item_sel];
217     [o_mi_selectall setEnabled: b_rows];
218
219     return( o_ctx_menu );
220 }
221
222 - (IBAction)savePlaylist:(id)sender
223 {
224     intf_thread_t * p_intf = [NSApp getIntf];
225     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
226                                                        FIND_ANYWHERE );
227     
228     NSSavePanel *o_save_panel = [NSSavePanel savePanel];
229     NSString * o_name = [NSString stringWithFormat: @"%@.m3u", _NS("Untitled")];
230     [o_save_panel setTitle: _NS("Save Playlist")];
231     [o_save_panel setPrompt: _NS("Save")];
232
233     if( [o_save_panel runModalForDirectory: nil
234             file: o_name] == NSOKButton )
235     {
236         playlist_SaveFile( p_playlist, [[o_save_panel filename] fileSystemRepresentation] );
237     }
238
239 }
240
241 - (IBAction)playItem:(id)sender
242 {
243     intf_thread_t * p_intf = [NSApp getIntf];
244     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
245                                                        FIND_ANYWHERE );
246
247     if( p_playlist != NULL )
248     {
249         playlist_Goto( p_playlist, [o_table_view selectedRow] );
250         vlc_object_release( p_playlist );
251     }
252 }
253
254 - (IBAction)deleteItems:(id)sender
255 {
256     int i, c, i_row;
257     NSMutableArray *o_to_delete;
258     NSNumber *o_number;
259
260     intf_thread_t * p_intf = [NSApp getIntf];
261     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
262                                                        FIND_ANYWHERE );
263
264     if( p_playlist == NULL )
265     {
266         return;
267     }
268     
269     o_to_delete = [NSMutableArray arrayWithArray:[[o_table_view selectedRowEnumerator] allObjects]];
270     c = (int)[o_to_delete count];
271     
272     for( i = 0; i < c; i++ ) {
273         o_number = [o_to_delete lastObject];
274         i_row = [o_number intValue];
275         
276         if( p_playlist->i_index == i_row && p_playlist->i_status )
277         {
278             playlist_Stop( p_playlist );
279         }
280         [o_to_delete removeObject: o_number];
281         [o_table_view deselectRow: i_row];
282         playlist_Delete( p_playlist, i_row );
283     }
284
285     vlc_object_release( p_playlist );
286
287     /* this is actually duplicity, because the intf.m manage also updates the view
288      * when the playlist changes. we do this on purpose, because else there is a 
289      * delay of .5 sec or so when we delete an item */
290     [self playlistUpdated];
291     [self updateRowSelection];
292 }
293
294 - (IBAction)selectAll:(id)sender
295 {
296     [o_table_view selectAll: nil];
297 }
298
299 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
300 {
301     int i_item;
302     intf_thread_t * p_intf = [NSApp getIntf];
303     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
304                                                        FIND_ANYWHERE );
305
306     if( p_playlist == NULL )
307     {
308         return;
309     }
310
311     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
312     {
313         /* One item */
314         NSDictionary *o_one_item;
315         int j;
316         int i_total_options = 0;
317         int i_mode = PLAYLIST_INSERT;
318         BOOL b_rem = FALSE, b_dir = FALSE;
319         NSString *o_url, *o_name;
320         NSArray *o_options;
321         char **ppsz_options = NULL;
322     
323         /* Get the item */
324         o_one_item = [o_array objectAtIndex: i_item];
325         o_url = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
326         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
327         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
328         
329         /* If no name, then make a guess */
330         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_url];
331     
332         if( [[NSFileManager defaultManager] fileExistsAtPath:o_url isDirectory:&b_dir] && b_dir &&
333             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_url isRemovable: &b_rem
334                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
335         {
336             /* All of this is to make sure CD's play when you D&D them on VLC */
337             /* Converts mountpoint to a /dev file */
338             struct statfs *buf;
339             char *psz_dev, *temp;
340             buf = (struct statfs *) malloc (sizeof(struct statfs));
341             statfs( [o_url fileSystemRepresentation], buf );
342             psz_dev = strdup(buf->f_mntfromname);
343             free( buf );
344             temp = strrchr( psz_dev , 's' );
345             psz_dev[temp - psz_dev] = '\0';
346             o_url = [NSString stringWithCString: psz_dev ];
347             NSLog(@"%@", o_url);
348         }
349     
350         if (i_item == 0 && !b_enqueue)
351             i_mode |= PLAYLIST_GO;
352     
353         if( o_options && [o_options count] > 0 )
354         {
355             /* Count the input options */
356             i_total_options = [o_options count];
357     
358             /* Allocate ppsz_options */
359             for( j = 0; j < i_total_options; j++ )
360             {
361                 if( !ppsz_options )
362                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
363     
364                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
365             }
366         }
367     
368         playlist_AddExt( p_playlist, [o_url fileSystemRepresentation], [o_name UTF8String], -1, 
369             (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options,
370             i_mode, i_position == -1 ? PLAYLIST_END : i_position + i_item);
371     
372         /* clean up */
373         for( j = 0; j < i_total_options; j++ )
374             free( ppsz_options[j] );
375         if( ppsz_options ) free( ppsz_options );
376     
377         /* Recent documents menu */
378         NSURL *o_true_url = [NSURL fileURLWithPath: o_url];
379         if( o_true_url != nil )
380         { 
381             [[NSDocumentController sharedDocumentController]
382                 noteNewRecentDocumentURL: o_true_url]; 
383         }
384     }
385
386     vlc_object_release( p_playlist );
387 }
388
389 - (void)playlistUpdated
390 {
391     [o_table_view reloadData];
392 }
393
394 - (void)updateRowSelection
395 {
396     int i_row;
397
398     intf_thread_t * p_intf = [NSApp getIntf];
399     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
400                                                        FIND_ANYWHERE );
401
402     if( p_playlist == NULL )
403     {
404         return;
405     }
406
407     i_row = p_playlist->i_index;
408     vlc_object_release( p_playlist );
409
410     [o_table_view selectRow: i_row byExtendingSelection: NO];
411     [o_table_view scrollRowToVisible: i_row];
412 }
413
414 @end
415
416 @implementation VLCPlaylist (NSTableDataSource)
417
418 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
419 {
420     int i_count = 0;
421     intf_thread_t * p_intf = [NSApp getIntf];
422     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
423                                                        FIND_ANYWHERE );
424
425     if( p_playlist != NULL )
426     {
427         vlc_mutex_lock( &p_playlist->object_lock );
428         i_count = p_playlist->i_size;
429         vlc_mutex_unlock( &p_playlist->object_lock );
430         vlc_object_release( p_playlist );
431     }
432
433     return( i_count );
434 }
435
436 - (id)tableView:(NSTableView *)o_tv 
437                 objectValueForTableColumn:(NSTableColumn *)o_tc 
438                 row:(int)i_row
439 {
440     id o_value = nil;
441     intf_thread_t * p_intf = [NSApp getIntf];
442     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
443                                                FIND_ANYWHERE );
444
445     if( p_playlist == NULL )
446     {
447         return( nil );
448     }
449
450     vlc_mutex_lock( &p_playlist->object_lock );
451     o_value = [[NSString stringWithUTF8String: 
452         p_playlist->pp_items[i_row]->psz_name] lastPathComponent]; 
453     vlc_mutex_unlock( &p_playlist->object_lock ); 
454
455     vlc_object_release( p_playlist );
456
457     return( o_value );
458 }
459
460 - (BOOL)tableView:(NSTableView *)o_tv
461                     writeRows:(NSArray*)o_rows
462                     toPasteboard:(NSPasteboard*)o_pasteboard 
463 {
464     int i_rows = [o_rows count];
465     NSArray *o_filenames = [NSArray array];
466     
467     [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
468     [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
469     if ( i_rows == 1 )
470     {
471         i_moveRow = [[o_rows objectAtIndex:0]intValue];
472         return YES;
473     }
474     return NO;
475 }
476
477 - (NSDragOperation)tableView:(NSTableView*)o_tv
478                     validateDrop:(id <NSDraggingInfo>)o_info
479                     proposedRow:(int)i_row
480                     proposedDropOperation:(NSTableViewDropOperation)o_operation 
481 {
482     if ( o_operation == NSTableViewDropAbove )
483     {
484         if ( i_moveRow >= 0 )
485         {
486             if ( i_row != i_moveRow )
487             {
488                 return NSDragOperationMove;
489             }
490             /* what if in the previous run, the row wasn't actually moved? 
491                then we can't drop new files on this location */
492             return NSDragOperationNone;
493         }
494         return NSDragOperationGeneric;
495     }
496     return NSDragOperationNone;
497 }
498
499 - (BOOL)tableView:(NSTableView*)o_tv
500                     acceptDrop:(id <NSDraggingInfo>)o_info
501                     row:(int)i_proposed_row
502                     dropOperation:(NSTableViewDropOperation)o_operation 
503 {
504     if (  i_moveRow >= 0 )
505     {
506         if (i_moveRow != -1 && i_proposed_row != -1)
507         {
508             intf_thread_t * p_intf = [NSApp getIntf];
509             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
510                                                             FIND_ANYWHERE );
511         
512             if( p_playlist == NULL )
513             {
514                 i_moveRow = -1;
515                 return NO;
516             }
517     
518             playlist_Move( p_playlist, i_moveRow, i_proposed_row ); 
519         
520             vlc_object_release( p_playlist );
521         }
522         [self playlistUpdated];
523         i_moveRow = -1;
524         return YES;
525     }
526     else
527     {
528         NSPasteboard * o_pasteboard;
529         o_pasteboard = [o_info draggingPasteboard];
530         
531         if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
532         {
533             int i;
534             NSArray *o_array = [NSArray array];
535             NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
536                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
537
538             for( i = 0; i < (int)[o_values count]; i++)
539             {
540                 NSDictionary *o_dic;
541                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
542                 o_array = [o_array arrayByAddingObject: o_dic];
543             }
544             [self appendArray: o_array atPos: i_proposed_row enqueue:YES];
545             return YES;
546         }
547         return NO;
548     }
549     [self updateRowSelection];
550 }
551
552 @end
553