]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
c876c321b6c71df89cd9c9705d89130f098c50c9
[vlc] / 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.6 2003/01/29 11:34:11 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 - (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     i_items = 0;
236     o_enum = [o_array objectEnumerator];
237     while( ( o_value = [o_enum nextObject] ) )
238     {
239         NSURL * o_url;
240
241         int i_mode = PLAYLIST_INSERT;
242         
243         if (i_items == 0 && !b_enqueue)
244             i_mode |= PLAYLIST_GO;
245
246         playlist_Add( p_playlist, [o_value fileSystemRepresentation],
247             i_mode, i_pos == -1 ? PLAYLIST_END : i_pos + i_items );
248
249         o_url = [NSURL fileURLWithPath: o_value];
250         if( o_url != nil )
251         { 
252             [[NSDocumentController sharedDocumentController]
253                 noteNewRecentDocumentURL: o_url]; 
254         }
255
256         i_items++;
257     }
258
259     vlc_object_release( p_playlist );
260
261     [self playlistUpdated];
262 }
263
264 - (void)playlistUpdated
265 {
266     [o_table_view reloadData];
267 }
268
269 @end
270
271 @implementation VLCPlaylist (NSTableDataSource)
272
273 - (int)numberOfRowsInTableView:(NSTableView *)o_tv
274 {
275     int i_count = 0;
276     intf_thread_t * p_intf = [NSApp getIntf];
277     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
278                                                FIND_ANYWHERE );
279
280     if( p_playlist != NULL )
281     {
282         i_count = p_playlist->i_size;
283         vlc_object_release( p_playlist );
284     }
285
286     return( i_count );
287 }
288
289 - (id)tableView:(NSTableView *)o_tv 
290                 objectValueForTableColumn:(NSTableColumn *)o_tc 
291                 row:(int)i_row
292 {
293     id o_value = nil;
294     intf_thread_t * p_intf = [NSApp getIntf];
295     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
296                                                FIND_ANYWHERE );
297
298     if( p_playlist == NULL )
299     {
300         return( nil );
301     }
302
303     vlc_mutex_lock( &p_playlist->object_lock );
304     o_value = [NSString stringWithUTF8String: 
305         p_playlist->pp_items[i_row]->psz_name]; 
306     vlc_mutex_unlock( &p_playlist->object_lock ); 
307
308     vlc_object_release( p_playlist );
309
310     return( o_value );
311 }
312
313 @end
314