]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* gtk2_theme.cpp: window are not automatically visibled
[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.20 2003/04/15 14:05:13 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 }
254
255 - (IBAction)selectAll:(id)sender
256 {
257     [o_table_view selectAll: nil];
258 }
259
260 - (void)appendArray:(NSArray*)o_array atPos:(int)i_pos enqueue:(BOOL)b_enqueue
261 {
262     int i_items;
263     NSString * o_value;
264     NSEnumerator * o_enum;
265     intf_thread_t * p_intf = [NSApp getIntf];
266     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
267                                                        FIND_ANYWHERE );
268
269     if( p_playlist == NULL )
270     {
271         return;
272     }
273
274     i_items = 0;
275     o_enum = [o_array objectEnumerator];
276     while( ( o_value = [o_enum nextObject] ) )
277     {
278         NSURL * o_url;
279
280         int i_mode = PLAYLIST_INSERT;
281         
282         if (i_items == 0 && !b_enqueue)
283             i_mode |= PLAYLIST_GO;
284
285         playlist_Add( p_playlist, [o_value fileSystemRepresentation],
286             i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );
287
288         o_url = [NSURL fileURLWithPath: o_value];
289         if( o_url != nil )
290         { 
291             [[NSDocumentController sharedDocumentController]
292                 noteNewRecentDocumentURL: o_url]; 
293         }
294
295         i_items++;
296     }
297
298     vlc_object_release( p_playlist );
299 }
300
301 - (void)playlistUpdated
302 {
303     [o_table_view reloadData];
304 }
305
306 - (void)updateRowSelection
307 {
308     int i_row;
309
310     intf_thread_t * p_intf = [NSApp getIntf];
311     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
312                                                        FIND_ANYWHERE );
313
314     if( p_playlist == NULL )
315     {
316         return;
317     }
318
319     vlc_mutex_lock( &p_playlist->object_lock );    
320     i_row = p_playlist->i_index;
321     vlc_mutex_unlock( &p_playlist->object_lock );
322     vlc_object_release( p_playlist );
323
324     [o_table_view selectRow: i_row byExtendingSelection: NO];
325     [o_table_view scrollRowToVisible: i_row];
326 }
327
328 @end
329
330 @implementation VLCPlaylist (NSTableDataSource)
331
332 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
333 {
334     int i_count = 0;
335     intf_thread_t * p_intf = [NSApp getIntf];
336     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
337                                                        FIND_ANYWHERE );
338
339     if( p_playlist != NULL )
340     {
341         vlc_mutex_lock( &p_playlist->object_lock );
342         i_count = p_playlist->i_size;
343         vlc_mutex_unlock( &p_playlist->object_lock );
344         vlc_object_release( p_playlist );
345     }
346
347     return( i_count );
348 }
349
350 - (id)tableView:(NSTableView *)o_tv 
351                 objectValueForTableColumn:(NSTableColumn *)o_tc 
352                 row:(int)i_row
353 {
354     id o_value = nil;
355     intf_thread_t * p_intf = [NSApp getIntf];
356     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
357                                                FIND_ANYWHERE );
358
359     if( p_playlist == NULL )
360     {
361         return( nil );
362     }
363
364     vlc_mutex_lock( &p_playlist->object_lock );
365     o_value = [[NSString stringWithUTF8String: 
366         p_playlist->pp_items[i_row]->psz_name] lastPathComponent]; 
367     vlc_mutex_unlock( &p_playlist->object_lock ); 
368
369     vlc_object_release( p_playlist );
370
371     return( o_value );
372 }
373
374 - (BOOL)tableView:(NSTableView *)o_tv
375                     writeRows:(NSArray*)o_rows
376                     toPasteboard:(NSPasteboard*)o_pasteboard 
377 {
378     int i_rows = [o_rows count];
379     NSArray *o_filenames = [NSArray array];
380     
381     [o_pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
382     [o_pasteboard setPropertyList:o_filenames forType:NSFilenamesPboardType];
383     if ( i_rows == 1 )
384     {
385         i_moveRow = [[o_rows objectAtIndex:0]intValue];
386         return YES;
387     }
388     return NO;
389 }
390
391 - (NSDragOperation)tableView:(NSTableView*)o_tv
392                     validateDrop:(id <NSDraggingInfo>)o_info
393                     proposedRow:(int)i_row
394                     proposedDropOperation:(NSTableViewDropOperation)o_operation 
395 {
396     if ( o_operation == NSTableViewDropAbove )
397     {
398         if ( i_moveRow >= 0 )
399         {
400             if ( i_row != i_moveRow )
401             {
402                 return NSDragOperationMove;
403             }
404             /* what if in the previous run, the row wasn't actually moved? 
405                then we can't drop new files on this location */
406             return NSDragOperationNone;
407         }
408         return NSDragOperationGeneric;
409     }
410     return NSDragOperationNone;
411 }
412
413 - (BOOL)tableView:(NSTableView*)o_tv
414                     acceptDrop:(id <NSDraggingInfo>)o_info
415                     row:(int)i_proposed_row
416                     dropOperation:(NSTableViewDropOperation)o_operation 
417 {
418     if (  i_moveRow >= 0 )
419     {
420         if (i_moveRow != -1 && i_proposed_row != -1)
421         {
422             intf_thread_t * p_intf = [NSApp getIntf];
423             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
424                                                             FIND_ANYWHERE );
425         
426             if( p_playlist == NULL )
427             {
428                 i_moveRow = -1;
429                 return NO;
430             }
431     
432             playlist_Move( p_playlist, i_moveRow, i_proposed_row ); 
433         
434             vlc_object_release( p_playlist );
435         }
436         [self playlistUpdated];
437         i_moveRow = -1;
438         return YES;
439     }
440     else
441     {
442         NSArray * o_values;
443         NSPasteboard * o_pasteboard;
444         
445         intf_thread_t * p_intf = [NSApp getIntf];
446         o_pasteboard = [o_info draggingPasteboard];
447         
448         if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
449         {
450             o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType]
451                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
452             [self appendArray: o_values atPos: i_proposed_row enqueue:YES];
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             return( YES );
460         }
461         
462         return( NO );
463     }
464     [self updateRowSelection];
465 }
466
467 @end
468