390766725659c1b56df5ea4a5d99b04dd16f8dfa
[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.4 2003/01/20 03:45:06 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 @end
50
51 /*****************************************************************************
52  * VLCPlaylist implementation 
53  *****************************************************************************/
54 @implementation VLCPlaylist
55
56 - (void)awakeFromNib
57 {
58     [o_table_view setTarget: self];
59     [o_table_view setDelegate: self];
60     [o_table_view setDataSource: self];
61
62     [o_table_view setDoubleAction: @selector(playItem:)];
63
64     [o_table_view registerForDraggedTypes: 
65         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
66
67     [o_mi_play setTitle: _NS("Play")];
68     [o_mi_delete setTitle: _NS("Delete")];
69     [o_mi_selectall setTitle: _NS("Select All")];
70 }
71
72 - (BOOL)tableView:(NSTableView *)o_tv 
73                   shouldEditTableColumn:(NSTableColumn *)o_tc
74                   row:(int)i_row
75 {
76     return( NO );
77 }
78
79 - (NSDragOperation)tableView:(NSTableView*)o_tv 
80                    validateDrop:(id <NSDraggingInfo>)info 
81                    proposedRow:(int)i_row 
82                    proposedDropOperation:(NSTableViewDropOperation)operation
83 {
84     return( NSDragOperationPrivate );
85 }
86
87 - (BOOL)tableView:(NSTableView*)o_tv 
88                   acceptDrop:(id <NSDraggingInfo>)info 
89                   row:(int)i_row 
90                   dropOperation:(NSTableViewDropOperation)operation
91 {
92     NSArray * o_values;
93     NSPasteboard * o_pasteboard;
94
95     o_pasteboard = [info draggingPasteboard];
96
97     if( [[o_pasteboard types] containsObject: NSFilenamesPboardType] )
98     {
99         o_values = [o_pasteboard propertyListForType: NSFilenamesPboardType];
100
101         [self appendArray: o_values atPos: i_row enqueue:YES];
102
103         return( YES );
104     }
105
106     return( NO ); 
107 }
108
109 - (void)tableView:(NSTableView *)o_tv willDisplayCell:(id)o_cell
110                   forTableColumn:(NSTableColumn *)o_tc row:(int)i_row
111 {
112     NSColor * o_color;
113
114     [o_cell setDrawsBackground: YES];
115
116     if( i_row % 2 )
117     {
118         o_color = [[NSColor alternateSelectedControlColor]
119                             highlightWithLevel: 0.90];
120     }
121     else
122     {
123         o_color = [o_tv backgroundColor]; 
124     }
125
126     [o_cell setBackgroundColor: o_color];
127 }
128
129 - (NSMenu *)menuForEvent:(NSEvent *)o_event
130 {
131     NSPoint pt;
132     vlc_bool_t b_rows;
133     vlc_bool_t b_item_sel;
134
135     pt = [o_table_view convertPoint: [o_event locationInWindow] 
136                                                  fromView: nil];
137     b_item_sel = ( [o_table_view rowAtPoint: pt] != -1 &&
138                    [o_table_view selectedRow] != -1 );
139     b_rows = [o_table_view numberOfRows] != 0;
140
141     [o_mi_play setEnabled: b_item_sel];
142     [o_mi_delete setEnabled: b_item_sel];
143     [o_mi_selectall setEnabled: b_rows];
144
145     return( o_ctx_menu );
146 }
147
148 - (IBAction)playItem:(id)sender
149 {
150     intf_thread_t * p_intf = [NSApp getIntf];
151     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
152                                                        FIND_ANYWHERE );
153
154     if( p_playlist == NULL )
155     {
156         return;
157     }
158
159     playlist_Goto( p_playlist, [o_table_view selectedRow] );
160
161     vlc_object_release( p_playlist );
162 }
163
164 - (IBAction)deleteItems:(id)sender
165 {
166     int i_row;
167
168     intf_thread_t * p_intf = [NSApp getIntf];
169     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
170                                                        FIND_ANYWHERE );
171
172     if( p_playlist == NULL )
173     {
174         return;
175     }
176
177     while( ( i_row = [o_table_view selectedRow] ) != -1 )
178     {
179         if( p_playlist->i_index == i_row && p_playlist->i_status )
180         {
181             playlist_Stop( p_playlist );
182         }
183
184         playlist_Delete( p_playlist, i_row ); 
185
186         [o_table_view deselectRow: i_row];
187     }
188
189     vlc_object_release( p_playlist );
190
191     [self playlistUpdated];
192 }
193
194 - (IBAction)selectAll:(id)sender
195 {
196     [o_table_view selectAll: nil];
197 }
198
199 - (void)appendArray:(NSArray*)o_array atPos:(int)i_pos enqueue:(BOOL)b_enqueue
200 {
201     int i_items;
202     NSString * o_value;
203     NSEnumerator * o_enum;
204     intf_thread_t * p_intf = [NSApp getIntf];
205     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
206                                                        FIND_ANYWHERE );
207
208     if( p_playlist == NULL )
209     {
210         return;
211     }
212
213     if( p_intf->p_sys->b_loop )
214     {
215         playlist_Delete( p_playlist, p_playlist->i_size - 1 );
216     }
217
218     i_items = 0;
219     o_enum = [o_array objectEnumerator];
220     while( ( o_value = [o_enum nextObject] ) )
221     {
222         NSURL * o_url;
223
224         int i_mode = PLAYLIST_INSERT;
225         
226         if (i_items == 0 && !b_enqueue)
227             i_mode |= PLAYLIST_GO;
228
229         playlist_Add( p_playlist, [o_value fileSystemRepresentation],
230             i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );
231
232         o_url = [NSURL fileURLWithPath: o_value];
233         if( o_url != nil )
234         { 
235             [[NSDocumentController sharedDocumentController]
236                 noteNewRecentDocumentURL: o_url]; 
237         }
238
239         i_items++;
240     }
241
242     if( p_intf->p_sys->b_loop )
243     {
244         playlist_Add( p_playlist, "vlc:loop",
245                       PLAYLIST_APPEND, PLAYLIST_END );
246     }
247
248     vlc_object_release( p_playlist );
249
250     [self playlistUpdated];
251 }
252
253 - (void)playlistUpdated
254 {
255     [o_table_view reloadData];
256 }
257
258 @end
259
260 @implementation VLCPlaylist (NSTableDataSource)
261
262 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
263 {
264     int i_count = 0;
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         i_count = p_playlist->i_size;
272         vlc_object_release( p_playlist );
273     }
274
275     return( i_count );
276 }
277
278 - (id)tableView:(NSTableView *)o_tv 
279                 objectValueForTableColumn:(NSTableColumn *)o_tc 
280                 row:(int)i_row
281 {
282     id o_value = nil;
283     intf_thread_t * p_intf = [NSApp getIntf];
284     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
285                                                FIND_ANYWHERE );
286
287     if( p_playlist == NULL )
288     {
289         return( nil );
290     }
291
292     vlc_mutex_lock( &p_playlist->object_lock );
293     o_value = [NSString stringWithUTF8String: 
294         p_playlist->pp_items[i_row]->psz_name]; 
295     vlc_mutex_unlock( &p_playlist->object_lock ); 
296
297     vlc_object_release( p_playlist );
298
299     return( o_value );
300 }
301
302 @end
303