* when the playlist is selected and you push the spacebar we pause/play
[vlc.git] / modules / gui / macosx / playlist.m
1 /*****************************************************************************
2  * playlist.m: MacOS X interface plugin
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: playlist.m,v 1.5 2003/01/24 02:31:53 hartman Exp $
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <sys/param.h>                                    /* for MAXPATHLEN */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/intf.h>
33
34 #include <Cocoa/Cocoa.h> 
35
36 #include "intf.h"
37 #include "playlist.h"
38
39 /*****************************************************************************
40  * VLCPlaylistView implementation 
41  *****************************************************************************/
42 @implementation VLCPlaylistView
43
44 - (NSMenu *)menuForEvent:(NSEvent *)o_event
45 {
46     return( [[self delegate] menuForEvent: o_event] );
47 }
48
49 - (void)keyDown:(NSEvent *)o_event
50 {
51     intf_thread_t * p_intf = [NSApp getIntf];
52     unichar key = 0;
53
54     if( [[o_event characters] length] )
55     {
56         key = [[o_event characters] characterAtIndex: 0];
57     }
58
59     switch( key )
60     {
61         case ' ':
62             input_SetStatus(  p_intf->p_sys->p_input , INPUT_STATUS_PAUSE );
63             break;
64
65         default:
66             [super keyDown: o_event];
67             break;
68     }
69 }
70
71 @end
72
73 /*****************************************************************************
74  * VLCPlaylist implementation 
75  *****************************************************************************/
76 @implementation VLCPlaylist
77
78 - (void)awakeFromNib
79 {
80     [o_table_view setTarget: self];
81     [o_table_view setDelegate: self];
82     [o_table_view setDataSource: self];
83
84     [o_table_view setDoubleAction: @selector(playItem:)];
85
86     [o_table_view registerForDraggedTypes: 
87         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
88
89     [o_mi_play setTitle: _NS("Play")];
90     [o_mi_delete setTitle: _NS("Delete")];
91     [o_mi_selectall setTitle: _NS("Select All")];
92 }
93
94 - (BOOL)tableView:(NSTableView *)o_tv 
95                   shouldEditTableColumn:(NSTableColumn *)o_tc
96                   row:(int)i_row
97 {
98     return( NO );
99 }
100
101 - (NSDragOperation)tableView:(NSTableView*)o_tv 
102                    validateDrop:(id <NSDraggingInfo>)info 
103                    proposedRow:(int)i_row 
104                    proposedDropOperation:(NSTableViewDropOperation)operation
105 {
106     return( NSDragOperationPrivate );
107 }
108
109 - (BOOL)tableView:(NSTableView*)o_tv 
110                   acceptDrop:(id <NSDraggingInfo>)info 
111                   row:(int)i_row 
112                   dropOperation:(NSTableViewDropOperation)operation
113 {
114     NSArray * o_values;
115     NSPasteboard * o_pasteboard;
116
117     o_pasteboard = [info draggingPasteboard];
118
119     if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
120     {
121         o_values = [o_pasteboard propertyListForType: NSFilenamesPboardType];
122
123         [self appendArray: o_values atPos: i_row enqueue:YES];
124
125         return( YES );
126     }
127
128     return( NO ); 
129 }
130
131 - (void)tableView:(NSTableView *)o_tv willDisplayCell:(id)o_cell
132                   forTableColumn:(NSTableColumn *)o_tc row:(int)i_row
133 {
134     NSColor * o_color;
135
136     [o_cell setDrawsBackground: YES];
137
138     if( i_row % 2 )
139     {
140         o_color = [[NSColor alternateSelectedControlColor]
141                             highlightWithLevel: 0.90];
142     }
143     else
144     {
145         o_color = [o_tv backgroundColor]; 
146     }
147
148     [o_cell setBackgroundColor: o_color];
149 }
150
151 - (NSMenu *)menuForEvent:(NSEvent *)o_event
152 {
153     NSPoint pt;
154     vlc_bool_t b_rows;
155     vlc_bool_t b_item_sel;
156
157     pt = [o_table_view convertPoint: [o_event locationInWindow] 
158                                                  fromView: nil];
159     b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
160                    [o_table_view selectedRow] != -1 );
161     b_rows = [o_table_view numberOfRows] != 0;
162
163     [o_mi_play setEnabled: b_item_sel];
164     [o_mi_delete setEnabled: b_item_sel];
165     [o_mi_selectall setEnabled: b_rows];
166
167     return( o_ctx_menu );
168 }
169
170 - (IBAction)playItem:(id)sender
171 {
172     intf_thread_t * p_intf = [NSApp getIntf];
173     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
174                                                        FIND_ANYWHERE );
175
176     if( p_playlist == NULL )
177     {
178         return;
179     }
180
181     playlist_Goto( p_playlist, [o_table_view selectedRow] );
182
183     vlc_object_release( p_playlist );
184 }
185
186 - (IBAction)deleteItems:(id)sender
187 {
188     int i_row;
189
190     intf_thread_t * p_intf = [NSApp getIntf];
191     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
192                                                        FIND_ANYWHERE );
193
194     if( p_playlist == NULL )
195     {
196         return;
197     }
198
199     while( ( i_row = [o_table_view selectedRow] ) != -1 )
200     {
201         if( p_playlist->i_index == i_row && p_playlist->i_status )
202         {
203             playlist_Stop( p_playlist );
204         }
205
206         playlist_Delete( p_playlist, i_row ); 
207
208         [o_table_view deselectRow: i_row];
209     }
210
211     vlc_object_release( p_playlist );
212
213     [self playlistUpdated];
214 }
215
216 - (IBAction)selectAll:(id)sender
217 {
218     [o_table_view selectAll: nil];
219 }
220
221 - (void)appendArray:(NSArray*)o_array atPos:(int)i_pos enqueue:(BOOL)b_enqueue
222 {
223     int i_items;
224     NSString * o_value;
225     NSEnumerator * o_enum;
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     if( p_intf->p_sys->b_loop )
236     {
237         playlist_Delete( p_playlist, p_playlist->i_size - 1 );
238     }
239
240     i_items = 0;
241     o_enum = [o_array objectEnumerator];
242     while( ( o_value = [o_enum nextObject] ) )
243     {
244         NSURL * o_url;
245
246         int i_mode = PLAYLIST_INSERT;
247         
248         if (i_items == 0 && !b_enqueue)
249             i_mode |= PLAYLIST_GO;
250
251         playlist_Add( p_playlist, [o_value fileSystemRepresentation],
252             i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );
253
254         o_url = [NSURL fileURLWithPath: o_value];
255         if( o_url != nil )
256         { 
257             [[NSDocumentController sharedDocumentController]
258                 noteNewRecentDocumentURL: o_url]; 
259         }
260
261         i_items++;
262     }
263
264     if( p_intf->p_sys->b_loop )
265     {
266         playlist_Add( p_playlist, "vlc:loop",
267                       PLAYLIST_APPEND, PLAYLIST_END );
268     }
269
270     vlc_object_release( p_playlist );
271
272     [self playlistUpdated];
273 }
274
275 - (void)playlistUpdated
276 {
277     [o_table_view reloadData];
278 }
279
280 @end
281
282 @implementation VLCPlaylist (NSTableDataSource)
283
284 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
285 {
286     int i_count = 0;
287     intf_thread_t * p_intf = [NSApp getIntf];
288     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
289                                                FIND_ANYWHERE );
290
291     if( p_playlist != NULL )
292     {
293         i_count = p_playlist->i_size;
294         vlc_object_release( p_playlist );
295     }
296
297     return( i_count );
298 }
299
300 - (id)tableView:(NSTableView *)o_tv 
301                 objectValueForTableColumn:(NSTableColumn *)o_tc 
302                 row:(int)i_row
303 {
304     id o_value = nil;
305     intf_thread_t * p_intf = [NSApp getIntf];
306     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
307                                                FIND_ANYWHERE );
308
309     if( p_playlist == NULL )
310     {
311         return( nil );
312     }
313
314     vlc_mutex_lock( &p_playlist->object_lock );
315     o_value = [NSString stringWithUTF8String: 
316         p_playlist->pp_items[i_row]->psz_name]; 
317     vlc_mutex_unlock( &p_playlist->object_lock ); 
318
319     vlc_object_release( p_playlist );
320
321     return( o_value );
322 }
323
324 @end
325