]> git.sesse.net Git - vlc/blob - modules/gui/macosx/playlist.m
* Fix title display above the scrollbar
[vlc] / modules / gui / macosx / playlist.m
1 /*****************************************************************************
2  * playlist.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *          Benjamin Pracht <bigben at videolab dot org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /* TODO
27  * add 'icons' for different types of nodes? (http://www.cocoadev.com/index.pl?IconAndTextInTableCell)
28  * create a new 'playlist toggle' that hides the playlist and in effect give you the old controller
29  * create a new search field build with pictures from the 'regular' search field, so it can be emulated on 10.2
30  * create toggle buttons for the shuffle, repeat one, repeat all functions.
31  * implement drag and drop and item reordering.
32  * reimplement enable/disable item
33  * create a new 'tool' button (see the gear button in the Finder window) for 'actions'
34    (adding service discovery, other views, new node/playlist, save node/playlist) stuff like that
35  */
36
37
38
39 /*****************************************************************************
40  * Preamble
41  *****************************************************************************/
42 #include <stdlib.h>                                      /* malloc(), free() */
43 #include <sys/param.h>                                    /* for MAXPATHLEN */
44 #include <string.h>
45 #include <math.h>
46 #include <sys/mount.h>
47 #include <vlc_keys.h>
48
49 #include "intf.h"
50 #include "playlist.h"
51 #include "controls.h"
52 #include "osd.h"
53 #include "misc.h"
54
55 #define REF_HEIGHT 500
56 #define REF_WIDTH 500
57
58 /*****************************************************************************
59  * VLCPlaylistView implementation 
60  *****************************************************************************/
61 @implementation VLCPlaylistView
62
63 - (NSMenu *)menuForEvent:(NSEvent *)o_event
64 {
65     return( [[self delegate] menuForEvent: o_event] );
66 }
67
68 - (void)keyDown:(NSEvent *)o_event
69 {
70     unichar key = 0;
71
72     if( [[o_event characters] length] )
73     {
74         key = [[o_event characters] characterAtIndex: 0];
75     }
76
77     switch( key )
78     {
79         case NSDeleteCharacter:
80         case NSDeleteFunctionKey:
81         case NSDeleteCharFunctionKey:
82         case NSBackspaceCharacter:
83             [[self delegate] deleteItem:self];
84             break;
85
86         default:
87             [super keyDown: o_event];
88             break;
89     }
90 }
91
92 @end
93
94 /*****************************************************************************
95  * VLCPlaylist implementation 
96  *****************************************************************************/
97 @implementation VLCPlaylist
98
99 - (id)init
100 {
101     self = [super init];
102     if ( self !=nil )
103     {
104         //i_moveRow = -1;
105     }
106     return self;
107 }
108
109 - (void)awakeFromNib
110 {
111     [o_outline_view setTarget: self];
112     [o_outline_view setDelegate: self];
113     [o_outline_view setDataSource: self];
114
115     [o_outline_view setDoubleAction: @selector(playItem:)];
116
117     [o_outline_view registerForDraggedTypes: 
118         [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
119     [o_outline_view setIntercellSpacing: NSMakeSize (0.0, 1.0)];
120
121 /* We need to check whether _defaultTableHeaderSortImage exists, since it 
122 belongs to an Apple hidden private API, and then can "disapear" at any time*/
123
124     if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderSortImage)] )
125     {
126         o_ascendingSortingImage = [[NSOutlineView class] _defaultTableHeaderSortImage];
127     }
128     else
129     {
130         o_ascendingSortingImage = nil;
131     }
132
133     if( [[NSOutlineView class] respondsToSelector:@selector(_defaultTableHeaderReverseSortImage)] )
134     {
135         o_descendingSortingImage = [[NSOutlineView class] _defaultTableHeaderReverseSortImage];
136     }
137     else
138     {
139         o_descendingSortingImage = nil;
140     }
141
142     o_outline_dict = [[NSMutableDictionary alloc] init];
143     o_tc_sortColumn = nil;
144
145     [self initStrings];
146     //[self playlistUpdated];
147 }
148
149 - (void)initStrings
150 {
151     [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
152     [o_mi_play setTitle: _NS("Play")];
153     [o_mi_delete setTitle: _NS("Delete")];
154     [o_mi_selectall setTitle: _NS("Select All")];
155     [o_mi_info setTitle: _NS("Properties")];
156     [o_mi_sort_name setTitle: _NS("Sort Node by Name")];
157     [o_mi_sort_author setTitle: _NS("Sort Node by Author")];
158     [[o_tc_name headerCell] setStringValue:_NS("Name")];
159     [[o_tc_author headerCell] setStringValue:_NS("Author")];
160     [[o_tc_duration headerCell] setStringValue:_NS("Duration")];
161     [o_status_field setStringValue: [NSString stringWithFormat:
162                         _NS("0 items in playlist")]];
163
164     [o_random_ckb setTitle: _NS("Random")];
165 #if 0
166     [o_search_button setTitle: _NS("Search")];
167 #endif
168     [o_btn_playlist setToolTip: _NS("Playlist")];
169     [[o_loop_popup itemAtIndex:0] setTitle: _NS("Standard Play")];
170     [[o_loop_popup itemAtIndex:1] setTitle: _NS("Repeat One")];
171     [[o_loop_popup itemAtIndex:2] setTitle: _NS("Repeat All")];
172 }
173
174 - (IBAction)toggleWindow:(id)sender
175 {
176     NSRect o_rect;
177     /*First, check if the playlist is visible*/
178     if ( [o_controller frame].size.height == [o_controller minSize].height )
179     {
180         /*Check if the stored heigth of the controller is usable (!= minSize)*/
181         if ([o_controller getSizeWithPlaylist].height !=
182                                         [o_controller minSize].height)
183         {
184             o_rect.size.height = [o_controller getSizeWithPlaylist].height;
185         }
186         else
187         {
188             /*If the stored height is not usable, use a reference one*/
189             o_rect.size.height = REF_HEIGHT;
190         }
191
192         /*Check if the controller width is the minimum one*/
193         if ( [o_controller frame].size.width == [o_controller minSize].width)
194         {
195             /*If the controller width is minimum, check if the stored height
196               of the playlist makes it visible*/
197             if ([o_controller getSizeWithPlaylist].height !=
198                                                [o_controller minSize].height)
199             {
200                 o_rect.size.width = [o_controller getSizeWithPlaylist].width;
201             }
202             else
203             {
204                 /*If not, use a reference width*/
205                 o_rect.size.width = REF_WIDTH;
206             }
207         }
208         else
209         {
210             o_rect.size.width = [o_controller frame].size.width;
211         }
212         o_rect.origin.x = [o_controller frame].origin.x;
213         o_rect.origin.y = [o_controller frame].origin.y - o_rect.size.height +
214                                                 [o_controller minSize].height;
215
216         [o_btn_playlist setState: YES];
217     }
218     else
219     {
220         o_rect.size = [o_controller minSize];
221         o_rect.origin.x = [o_controller frame].origin.x;
222         /*Calculate the position of the lower right corner after resize*/
223         o_rect.origin.y = [o_controller frame].origin.y +
224             [o_controller frame].size.height - [o_controller minSize].height;
225
226         [o_btn_playlist setState: NO];
227     }
228
229     [o_controller setFrame: o_rect display:YES animate: YES];
230 }
231
232 - (void)playlistUpdated
233 {
234     unsigned int i;
235
236     /* Clear indications of any existing column sorting*/
237     for( i = 0 ; i < [[o_outline_view tableColumns] count] ; i++ )
238     {
239         [o_outline_view setIndicatorImage:nil inTableColumn:
240                             [[o_outline_view tableColumns] objectAtIndex:i]];
241     }
242
243     [o_outline_view setHighlightedTableColumn:nil];
244     o_tc_sortColumn = nil;
245     [o_outline_dict removeAllObjects];
246     [o_outline_view reloadData];
247 }
248
249 - (void)updateTogglePlaylistState
250 {
251     if ([o_controller getSizeWithPlaylist].height ==
252                                     [o_controller minSize].height)
253     {
254         [o_btn_playlist setState: NO];
255     }
256     else
257     {
258         [o_btn_playlist setState: YES];
259     }
260 }
261
262 - (void)updateRowSelection
263 {
264     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
265                                           FIND_ANYWHERE );
266     playlist_item_t * p_item, * p_temp_item;
267     NSMutableArray * o_array = [NSMutableArray array];
268     int i,i_row;
269     unsigned int j;
270
271     if (p_playlist == NULL)
272         return;
273
274     p_item = p_playlist->status.p_item;
275     p_temp_item = p_item;
276
277     while (p_temp_item->i_parents > 0)
278     {
279         [o_array insertObject: [NSValue valueWithPointer: p_temp_item] atIndex: 0];
280         for (i = 0 ; i < p_temp_item->i_parents ; i++)
281         {
282             if (p_temp_item->pp_parents[i]->i_view == VIEW_SIMPLE)
283             {
284                 p_temp_item = p_temp_item->pp_parents[i]->p_parent;
285                 break;
286             }
287         }
288     }
289
290     for (j = 0 ; j < [o_array count] - 1 ; j++)
291     {
292         [o_outline_view expandItem: [o_outline_dict objectForKey:
293                             [NSString stringWithFormat: @"%p",
294                             [[o_array objectAtIndex:j] pointerValue]]]];
295
296     }
297
298     i_row = [o_outline_view rowForItem:[o_outline_dict
299             objectForKey:[NSString stringWithFormat: @"%p", p_item]]];
300
301     [o_outline_view selectRow: i_row byExtendingSelection: NO];
302     [o_outline_view scrollRowToVisible: i_row];
303
304     vlc_object_release(p_playlist);
305 }
306
307 - (bool)isItem:(playlist_item_t *)p_item inNode:(playlist_item_t *)p_node
308 {
309     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
310                                           FIND_ANYWHERE );
311     playlist_item_t * p_temp_item = p_item;
312
313     if ( p_playlist == NULL )
314     {
315         return NO;
316     }
317
318     while ( p_temp_item->i_parents > 0 )
319     {
320         int i;
321         for (i = 0; i < p_temp_item->i_parents ; i++)
322         {
323             if (p_temp_item->pp_parents[i]->i_view == VIEW_SIMPLE)
324             {
325                 if (p_temp_item->pp_parents[i]->p_parent == p_node)
326                 {
327                     vlc_object_release(p_playlist);
328                     return YES;
329                 }
330                 else
331                 {
332                     p_temp_item = p_temp_item->pp_parents[i]->p_parent;
333                     break;
334                 }
335             }
336         }
337     }
338
339     vlc_object_release(p_playlist);
340     return NO;
341 }
342
343
344 - (IBAction)playItem:(id)sender
345 {
346     intf_thread_t * p_intf = VLCIntf;
347     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
348                                                        FIND_ANYWHERE );
349
350     if( p_playlist != NULL )
351     {
352         playlist_item_t *p_item;
353         playlist_item_t *p_node = NULL;
354         int i;
355
356         p_item = [[o_outline_view itemAtRow:[o_outline_view selectedRow]] pointerValue];
357
358         if( p_item )
359         {
360             if (p_item->i_children == -1)
361             {
362                 for (i = 0 ; i < p_item->i_parents ; i++)
363                 {
364                     if (p_item->pp_parents[i]->i_view == VIEW_SIMPLE)
365                     {
366                         p_node = p_item->pp_parents[i]->p_parent;
367                     }
368                 }
369             }
370             else
371             {
372                 p_node = p_item;
373                 if (p_node->pp_children[0]->i_children == -1 &&
374                     p_node->i_children > 0)
375                 {
376                     p_item = p_node->pp_children[0];
377                 }
378                 else
379                 {
380                     p_item = NULL;
381                 }
382             }
383
384 //        p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
385
386
387             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_node, p_item );
388 //            playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_SIMPLE, p_view ? p_view->p_root : NULL, p_item );
389         }
390         vlc_object_release( p_playlist );
391     }
392 }
393
394 - (IBAction)selectAll:(id)sender
395 {
396     [o_outline_view selectAll: nil];
397 }
398
399 - (IBAction)deleteItem:(id)sender
400 {
401     int i, c, i_row;
402     NSMutableArray *o_to_delete;
403     NSNumber *o_number;
404
405     playlist_t * p_playlist;
406     intf_thread_t * p_intf = VLCIntf;
407
408     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
409                                           FIND_ANYWHERE );
410
411     if ( p_playlist == NULL )
412     {
413         return;
414     }
415     o_to_delete = [NSMutableArray arrayWithArray:[[o_outline_view selectedRowEnumerator] allObjects]];
416     c = [o_to_delete count];
417
418     for( i = 0; i < c; i++ ) {
419         playlist_item_t * p_item;
420         o_number = [o_to_delete lastObject];
421         i_row = [o_number intValue];
422
423         [o_to_delete removeObject: o_number];
424         [o_outline_view deselectRow: i_row];
425         p_item = (playlist_item_t *)[[o_outline_view itemAtRow: i_row]pointerValue];
426         if (p_item->i_children > -1)
427         {
428             if (p_playlist->status.i_status)
429             {
430                 if ([self isItem:p_playlist->status.p_item inNode: p_item]
431                                     == YES && p_playlist->status.i_status)
432                 {
433                     playlist_Stop( p_playlist );
434                 }
435             }
436             playlist_NodeDelete( p_playlist, p_item, VLC_TRUE);
437         }
438         else
439         {
440             if( p_playlist->status.p_item == [[o_outline_view itemAtRow: i_row]
441                         pointerValue] && p_playlist->status.i_status )
442             {
443                 playlist_Stop( p_playlist );
444             }
445             playlist_LockDelete( p_playlist, p_item->input.i_id );
446         }
447         [self playlistUpdated];
448     }
449 }
450
451 - (IBAction)sortNodeByName:(id)sender
452 {
453     [self sortNode: SORT_TITLE];
454 }
455
456 - (IBAction)sortNodeByAuthor:(id)sender
457 {
458     [self sortNode: SORT_AUTHOR];
459 }
460
461 - (void)sortNode:(int)i_mode
462 {
463     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
464                                           FIND_ANYWHERE );
465     playlist_item_t * p_item;
466
467     if (p_playlist == NULL)
468     {
469         return;
470     }
471
472     if ([o_outline_view selectedRow] > -1)
473     {
474         p_item = [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
475                                                                 pointerValue];
476     }
477     else
478     /*If no item is selected, sort the whole playlist*/
479     {
480         playlist_view_t * p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
481         p_item = p_view->p_root;
482     }
483
484     if (p_item->i_children > -1)
485     {
486         vlc_mutex_lock(&p_playlist->object_lock );
487         playlist_RecursiveNodeSort( p_playlist, p_item, i_mode, ORDER_NORMAL );
488         vlc_mutex_unlock(&p_playlist->object_lock );
489     }
490     else
491     {
492         int i;
493
494         for (i = 0 ; i < p_item->i_parents ; i++)
495         {
496             if (p_item->pp_parents[i]->i_view == VIEW_SIMPLE)
497             {
498                 vlc_mutex_lock(&p_playlist->object_lock );
499                 playlist_RecursiveNodeSort( p_playlist,
500                         p_item->pp_parents[i]->p_parent, i_mode, ORDER_NORMAL );
501                 vlc_mutex_unlock(&p_playlist->object_lock );
502                 break;
503             }
504         }
505     }
506     vlc_object_release(p_playlist);
507     [self playlistUpdated];
508 }
509
510 - (void)appendArray:(NSArray*)o_array atPos:(int)i_position enqueue:(BOOL)b_enqueue
511 {
512     int i_item;
513     intf_thread_t * p_intf = VLCIntf;
514     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
515                                                        FIND_ANYWHERE );
516
517     if( p_playlist == NULL )
518     {
519         return;
520     }
521
522     for ( i_item = 0; i_item < (int)[o_array count]; i_item++ )
523     {
524         /* One item */
525         NSDictionary *o_one_item;
526         int j, i_total_options = 0, i_new_id = -1;
527         int i_mode = PLAYLIST_INSERT;
528         BOOL b_rem = FALSE, b_dir = FALSE;
529         NSString *o_uri, *o_name;
530         NSArray *o_options;
531         NSURL *o_true_file;
532         char **ppsz_options = NULL;
533
534         /* Get the item */
535         o_one_item = [o_array objectAtIndex: i_item];
536         o_uri = (NSString *)[o_one_item objectForKey: @"ITEM_URL"];
537         o_name = (NSString *)[o_one_item objectForKey: @"ITEM_NAME"];
538         o_options = (NSArray *)[o_one_item objectForKey: @"ITEM_OPTIONS"];
539
540         /* If no name, then make a guess */
541         if( !o_name) o_name = [[NSFileManager defaultManager] displayNameAtPath: o_uri];
542
543         if( [[NSFileManager defaultManager] fileExistsAtPath:o_uri isDirectory:&b_dir] && b_dir &&
544             [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath: o_uri isRemovable: &b_rem
545                     isWritable:NULL isUnmountable:NULL description:NULL type:NULL] && b_rem   )
546         {
547             /* All of this is to make sure CD's play when you D&D them on VLC */
548             /* Converts mountpoint to a /dev file */
549             struct statfs *buf;
550             char *psz_dev;
551             buf = (struct statfs *) malloc (sizeof(struct statfs));
552             statfs( [o_uri fileSystemRepresentation], buf );
553             psz_dev = strdup(buf->f_mntfromname);
554             o_uri = [NSString stringWithCString: psz_dev ];
555         }
556
557         if( o_options && [o_options count] > 0 )
558         {
559             /* Count the input options */
560             i_total_options = [o_options count];
561
562             /* Allocate ppsz_options */
563             for( j = 0; j < i_total_options; j++ )
564             {
565                 if( !ppsz_options )
566                     ppsz_options = (char **)malloc( sizeof(char *) * i_total_options );
567
568                 ppsz_options[j] = strdup([[o_options objectAtIndex:j] UTF8String]);
569             }
570         }
571
572         /* Add the item */
573         i_new_id = playlist_AddExt( p_playlist, [o_uri fileSystemRepresentation],
574                       [o_name UTF8String], i_mode,
575                       i_position == -1 ? PLAYLIST_END : i_position + i_item,
576                       0, (ppsz_options != NULL ) ? (const char **)ppsz_options : 0, i_total_options );
577
578         /* clean up
579         for( j = 0; j < i_total_options; j++ )
580             free( ppsz_options[j] );
581         if( ppsz_options ) free( ppsz_options ); */
582
583         /* Recent documents menu */
584         o_true_file = [NSURL fileURLWithPath: o_uri];
585         if( o_true_file != nil )
586         {
587             [[NSDocumentController sharedDocumentController]
588                 noteNewRecentDocumentURL: o_true_file];
589         }
590
591         if( i_item == 0 && !b_enqueue )
592         {
593             playlist_Goto( p_playlist, playlist_GetPositionById( p_playlist, i_new_id ) );
594             playlist_Play( p_playlist );
595         }
596     }
597
598     vlc_object_release( p_playlist );
599 }
600
601 - (IBAction)handlePopUp:(id)sender
602
603 {
604              intf_thread_t * p_intf = VLCIntf;
605              vlc_value_t val1,val2;
606              playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
607                                                         FIND_ANYWHERE );
608              if( p_playlist == NULL )
609              {
610                  return;
611              }
612
613     switch ([o_loop_popup indexOfSelectedItem])
614     {
615         case 1:
616
617              val1.b_bool = 0;
618              var_Set( p_playlist, "loop", val1 );
619              val1.b_bool = 1;
620              var_Set( p_playlist, "repeat", val1 );
621              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
622         break;
623
624         case 2:
625              val1.b_bool = 0;
626              var_Set( p_playlist, "repeat", val1 );
627              val1.b_bool = 1;
628              var_Set( p_playlist, "loop", val1 );
629              vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
630         break;
631
632         default:
633              var_Get( p_playlist, "repeat", &val1 );
634              var_Get( p_playlist, "loop", &val2 );
635              if (val1.b_bool || val2.b_bool)
636              {
637                   val1.b_bool = 0;
638                   var_Set( p_playlist, "repeat", val1 );
639                   var_Set( p_playlist, "loop", val1 );
640                   vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
641              }
642          break;
643      }
644      vlc_object_release( p_playlist );
645      [self playlistUpdated];
646 }
647
648 - (NSMutableArray *)subSearchItem:(playlist_item_t *)p_item
649 {
650     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
651                                                        FIND_ANYWHERE );
652     playlist_item_t * p_selected_item;
653     int i_current, i_selected_row;
654
655     if (!p_playlist)
656         return NULL;
657
658     i_selected_row = [o_outline_view selectedRow];
659     if (i_selected_row < 0)
660         i_selected_row = 0;
661
662     p_selected_item = (playlist_item_t *)[[o_outline_view itemAtRow:
663                                             i_selected_row] pointerValue];
664
665     for (i_current = 0; i_current < p_item->i_children ; i_current++)
666     {
667         char * psz_temp;
668         NSString * o_current_name, * o_current_author;
669
670         vlc_mutex_lock( &p_playlist->object_lock );
671         o_current_name = [NSString stringWithUTF8String:
672             p_item->pp_children[i_current]->input.psz_name];
673         psz_temp = playlist_ItemGetInfo(p_item ,_("Meta-information"),_("Author") );
674         o_current_author = [NSString stringWithUTF8String: psz_temp];
675         free( psz_temp);
676         vlc_mutex_unlock( &p_playlist->object_lock );
677
678         if (p_selected_item == p_item->pp_children[i_current] &&
679                     b_selected_item_met == NO)
680         {
681             b_selected_item_met = YES;
682         }
683         else if (p_selected_item == p_item->pp_children[i_current] &&
684                     b_selected_item_met == YES)
685         {
686             vlc_object_release(p_playlist);
687             return NULL;
688         }
689         else if (b_selected_item_met == YES &&
690                     ([o_current_name rangeOfString:[o_search_field
691                         stringValue] options:NSCaseInsensitiveSearch ].length ||
692                     [o_current_author rangeOfString:[o_search_field
693                         stringValue] options:NSCaseInsensitiveSearch ].length))
694         {
695             vlc_object_release(p_playlist);
696             /*Adds the parent items in the result array as well, so that we can
697             expand the tree*/
698             return [NSMutableArray arrayWithObject: [NSValue
699                             valueWithPointer: p_item->pp_children[i_current]]];
700         }
701         if (p_item->pp_children[i_current]->i_children > 0)
702         {
703             id o_result = [self subSearchItem:
704                                             p_item->pp_children[i_current]];
705             if (o_result != NULL)
706             {
707                 vlc_object_release(p_playlist);
708                 [o_result insertObject: [NSValue valueWithPointer:
709                                 p_item->pp_children[i_current]] atIndex:0];
710                 return o_result;
711             }
712         }
713     }
714     vlc_object_release(p_playlist);
715     return NULL;
716 }
717
718 - (IBAction)searchItem:(id)sender
719 {
720     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
721                                                        FIND_ANYWHERE );
722     playlist_view_t * p_view;
723     id o_result;
724
725     unsigned int i;
726     int i_row = -1;
727
728     b_selected_item_met = NO;
729
730     if( p_playlist == NULL )
731         return;
732
733     p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
734
735     if (p_view)
736     {
737         /*First, only search after the selected item:*
738          *(b_selected_item_met = NO)                 */
739         o_result = [self subSearchItem:p_view->p_root];
740         if (o_result == NULL)
741         {
742             /* If the first search failed, search again from the beginning */
743             o_result = [self subSearchItem:p_view->p_root];
744         }
745         if (o_result != NULL)
746         {
747             for (i = 0 ; i < [o_result count] - 1 ; i++)
748             {
749                 [o_outline_view expandItem: [o_outline_dict objectForKey:
750                             [NSString stringWithFormat: @"%p",
751                             [[o_result objectAtIndex: i] pointerValue]]]];
752             }
753             i_row = [o_outline_view rowForItem: [o_outline_dict objectForKey:
754                             [NSString stringWithFormat: @"%p",
755                             [[o_result objectAtIndex: [o_result count] - 1 ]
756                             pointerValue]]]];
757         }
758         if (i_row > -1)
759         {
760             [o_outline_view selectRow:i_row byExtendingSelection: NO];
761             [o_outline_view scrollRowToVisible: i_row];
762         }
763     }
764     vlc_object_release(p_playlist);
765
766 }
767
768 - (NSMenu *)menuForEvent:(NSEvent *)o_event
769 {
770     NSPoint pt;
771     vlc_bool_t b_rows;
772     vlc_bool_t b_item_sel;
773
774     pt = [o_outline_view convertPoint: [o_event locationInWindow]
775                                                  fromView: nil];
776     b_item_sel = ( [o_outline_view rowAtPoint: pt] != -1 &&
777                    [o_outline_view selectedRow] != -1 );
778     b_rows = [o_outline_view numberOfRows] != 0;
779
780     [o_mi_play setEnabled: b_item_sel];
781     [o_mi_delete setEnabled: b_item_sel];
782     [o_mi_selectall setEnabled: b_rows];
783     [o_mi_info setEnabled: b_item_sel];
784
785     return( o_ctx_menu );
786 }
787
788 - (playlist_item_t *)selectedPlaylistItem
789 {
790     return [[o_outline_view itemAtRow: [o_outline_view selectedRow]]
791                                                                 pointerValue];
792 }
793
794 - (void) outlineView:(NSTableView*)o_tv
795                   didClickTableColumn:(NSTableColumn *)o_tc
796 {
797     intf_thread_t * p_intf = VLCIntf;
798     playlist_t *p_playlist =
799         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
800                                        FIND_ANYWHERE );
801     playlist_view_t * p_view  = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
802     int i_mode = 0, i_type;
803
804     if( p_playlist == NULL )
805     {
806         return;
807     }
808
809     /* Check whether the selected table column header corresponds to a
810        sortable table column*/
811     if ( !(o_tc == o_tc_name || o_tc == o_tc_author))
812     {
813         return;
814     }
815
816     if( o_tc_sortColumn == o_tc )
817     {
818         b_isSortDescending = !b_isSortDescending;
819     }
820     else
821     {
822         b_isSortDescending = VLC_FALSE;
823     }
824
825     if (o_tc == o_tc_name)
826     {
827         i_mode = SORT_TITLE;
828     }
829     else if (o_tc == o_tc_author)
830     {
831         i_mode = SORT_AUTHOR;
832     }
833
834     if (b_isSortDescending)
835     {
836         i_type = ORDER_REVERSE;
837     }
838     else
839     {
840         i_type = ORDER_NORMAL;
841     }
842
843     vlc_mutex_lock(&p_playlist->object_lock );
844     playlist_RecursiveNodeSort(p_playlist, p_view->p_root, i_mode, i_type);
845     vlc_mutex_unlock(&p_playlist->object_lock );
846
847     vlc_object_release( p_playlist );
848     [self playlistUpdated];
849
850     o_tc_sortColumn = o_tc;
851     [o_outline_view setHighlightedTableColumn:o_tc];
852
853     if (b_isSortDescending)
854     {
855         [o_outline_view setIndicatorImage:o_descendingSortingImage
856                                                         inTableColumn:o_tc];
857     }
858     else
859     {
860         [o_outline_view setIndicatorImage:o_ascendingSortingImage
861                                                         inTableColumn:o_tc];
862     }
863 }
864
865 @end
866
867 @implementation VLCPlaylist (NSOutlineViewDataSource)
868
869 /* return the number of children for Obj-C pointer item */ /* DONE */
870 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
871 {
872     int i_return = 0;
873     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
874                                                        FIND_ANYWHERE );
875     if( p_playlist == NULL )
876         return 0;
877
878     if( item == nil )
879     {
880         /* root object */
881         playlist_view_t *p_view;
882         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
883         if( p_view && p_view->p_root )
884             i_return = p_view->p_root->i_children;
885     }
886     else
887     {
888         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
889         if( p_item )
890             i_return = p_item->i_children;
891     }
892     vlc_object_release( p_playlist );
893     if( i_return == -1 ) i_return = 0;
894     msg_Dbg( p_playlist, "I have %d children", i_return );
895     return i_return;
896 }
897
898 /* return the child at index for the Obj-C pointer item */ /* DONE */
899 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
900 {
901     playlist_item_t *p_return = NULL;
902     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
903                                                        FIND_ANYWHERE );
904     NSValue * o_value;
905
906     if( p_playlist == NULL )
907         return nil;
908
909     if( item == nil )
910     {
911         /* root object */
912         playlist_view_t *p_view;
913         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
914         if( p_view && index < p_view->p_root->i_children )
915             p_return = p_view->p_root->pp_children[index];
916     }
917     else
918     {
919         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
920         if( p_item && index < p_item->i_children )
921         {
922             p_return = p_item->pp_children[index];
923         }
924     }
925
926     [o_status_field setStringValue: [NSString stringWithFormat:
927                         _NS("%i items in playlist"), p_playlist->i_size]];
928
929     vlc_object_release( p_playlist );
930     msg_Dbg( p_playlist, "childitem with index %d", index );
931
932     o_value = [NSValue valueWithPointer: p_return];
933
934     [o_outline_dict setObject:o_value forKey:[NSString stringWithFormat:@"%p",                                                                      p_return]];
935     return o_value;
936 }
937
938 /* is the item expandable */
939 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
940 {
941     int i_return = 0;
942     playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
943                                                        FIND_ANYWHERE );
944     if( p_playlist == NULL )
945         return NO;
946
947     if( item == nil )
948     {
949         /* root object */
950         playlist_view_t *p_view;
951         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
952         if( p_view && p_view->p_root )
953             i_return = p_view->p_root->i_children;
954     }
955     else
956     {
957         playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
958         if( p_item )
959             i_return = p_item->i_children;
960     }
961     vlc_object_release( p_playlist );
962
963     if( i_return == -1 || i_return == 0 )
964         return NO;
965     else
966         return YES;
967 }
968
969 /* retrieve the string values for the cells */
970 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
971 {
972     id o_value = nil;
973     intf_thread_t * p_intf = VLCIntf;
974     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
975                                                FIND_ANYWHERE );
976     playlist_item_t *p_item = (playlist_item_t *)[item pointerValue];
977
978     if( p_playlist == NULL || p_item == NULL )
979     {
980         return( @"error" );
981     }
982
983     if( [[o_tc identifier] isEqualToString:@"1"] )
984     {
985         o_value = [NSString stringWithUTF8String:
986             p_item->input.psz_name];
987         if( o_value == NULL )
988             o_value = [NSString stringWithCString:
989                 p_item->input.psz_name];
990     }
991     else if( [[o_tc identifier] isEqualToString:@"2"] )
992     {
993         char *psz_temp;
994         psz_temp = playlist_ItemGetInfo( p_item ,_("Meta-information"),_("Artist") );
995
996         if( psz_temp == NULL )
997             o_value = @"";
998         else
999         {
1000             o_value = [NSString stringWithUTF8String: psz_temp];
1001             if( o_value == NULL )
1002             {
1003                 o_value = [NSString stringWithCString: psz_temp];
1004             }
1005             free( psz_temp );
1006         }
1007     }
1008     else if( [[o_tc identifier] isEqualToString:@"3"] )
1009     {
1010         char psz_duration[MSTRTIME_MAX_SIZE];
1011         mtime_t dur = p_item->input.i_duration;
1012         if( dur != -1 )
1013         {
1014             secstotimestr( psz_duration, dur/1000000 );
1015             o_value = [NSString stringWithUTF8String: psz_duration];
1016         }
1017         else
1018         {
1019             o_value = @"-:--:--";
1020         }
1021     }
1022
1023     vlc_object_release( p_playlist );
1024
1025     return( o_value );
1026 }
1027
1028 /* Required for drag & drop and reordering */
1029 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
1030 {
1031     return NO;
1032 }
1033
1034 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index
1035 {
1036     return NSDragOperationNone;
1037 }
1038
1039 /* Delegate method of NSWindow */
1040 /*- (void)windowWillClose:(NSNotification *)aNotification
1041 {
1042     [o_btn_playlist setState: NSOffState];
1043 }
1044 */
1045 @end
1046
1047