9e8b360f73227f33270399823b64a8c9773f9b30
[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.3 2003/01/05 03:21:50 jlj 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];
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
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 = i_items == 0 ? PLAYLIST_INSERT | PLAYLIST_GO :
225                                                    PLAYLIST_INSERT;
226
227         playlist_Add( p_playlist, [o_value fileSystemRepresentation],
228             i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );
229
230         o_url = [NSURL fileURLWithPath: o_value];
231         if( o_url != nil )
232         { 
233             [[NSDocumentController sharedDocumentController]
234                 noteNewRecentDocumentURL: o_url]; 
235         }
236
237         i_items++;
238     }
239
240     if( p_intf->p_sys->b_loop )
241     {
242         playlist_Add( p_playlist, "vlc:loop",
243                       PLAYLIST_APPEND, PLAYLIST_END );
244     }
245
246     vlc_object_release( p_playlist );
247
248     [self playlistUpdated];
249 }
250
251 - (void)playlistUpdated
252 {
253     [o_table_view reloadData];
254 }
255
256 @end
257
258 @implementation VLCPlaylist (NSTableDataSource)
259
260 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
261 {
262     int i_count = 0;
263     intf_thread_t * p_intf = [NSApp getIntf];
264     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
265                                                FIND_ANYWHERE );
266
267     if( p_playlist != NULL )
268     {
269         i_count = p_playlist->i_size;
270         vlc_object_release( p_playlist );
271     }
272
273     return( i_count );
274 }
275
276 - (id)tableView:(NSTableView *)o_tv 
277                 objectValueForTableColumn:(NSTableColumn *)o_tc 
278                 row:(int)i_row
279 {
280     id o_value = nil;
281     intf_thread_t * p_intf = [NSApp getIntf];
282     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
283                                                FIND_ANYWHERE );
284
285     if( p_playlist == NULL )
286     {
287         return( nil );
288     }
289
290     vlc_mutex_lock( &p_playlist->object_lock );
291     o_value = [NSString stringWithUTF8String: 
292         p_playlist->pp_items[i_row]->psz_name]; 
293     vlc_mutex_unlock( &p_playlist->object_lock ); 
294
295     vlc_object_release( p_playlist );
296
297     return( o_value );
298 }
299
300 @end
301