]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/playlist.cpp
Add directory in wxWidgets
[vlc] / modules / gui / wxwindows / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Clément Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/OR MODIFy
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/intf.h>
30
31 #include "wxwindows.h"
32
33 #include "bitmaps/shuffle.xpm"
34 #include "bitmaps/repeat.xpm"
35 #include "bitmaps/loop.xpm"
36
37 #include "bitmaps/type_unknown.xpm"
38 #include "bitmaps/type_net.xpm"
39 #include "bitmaps/type_card.xpm"
40 #include "bitmaps/type_disc.xpm"
41 #include "bitmaps/type_directory.xpm"
42 #include "bitmaps/type_playlist.xpm"
43
44 #include <wx/dynarray.h>
45 #include <wx/imaglist.h>
46
47 #define HELP_SHUFFLE N_( "Shuffle" )
48 #define HELP_LOOP N_( "Loop" )
49 #define HELP_REPEAT N_( "Repeat" )
50
51 /* Callback prototype */
52 static int PlaylistChanged( vlc_object_t *, const char *,
53                             vlc_value_t, vlc_value_t, void * );
54 static int PlaylistNext( vlc_object_t *, const char *,
55                          vlc_value_t, vlc_value_t, void * );
56 static int ItemChanged( vlc_object_t *, const char *,
57                         vlc_value_t, vlc_value_t, void * );
58
59 /*****************************************************************************
60  * Event Table.
61  *****************************************************************************/
62
63 /* IDs for the controls and the menu commands */
64 enum
65 {
66     /* menu items */
67     AddFile_Event = 1,
68     AddDir_Event,
69     AddMRL_Event,
70     Close_Event,
71     Open_Event,
72     Save_Event,
73
74     SortTitle_Event,
75     RSortTitle_Event,
76     SortAuthor_Event,
77     RSortAuthor_Event,
78     Randomize_Event,
79
80     EnableSelection_Event,
81     DisableSelection_Event,
82
83     InvertSelection_Event,
84     DeleteSelection_Event,
85     Random_Event,
86     Loop_Event,
87     Repeat_Event,
88     SelectAll_Event,
89
90     Up_Event,
91     Down_Event,
92     Infos_Event,
93
94     PopupPlay_Event,
95     PopupPlayThis_Event,
96     PopupDel_Event,
97     PopupEna_Event,
98     PopupInfo_Event,
99
100     SearchText_Event,
101     Search_Event,
102
103     /* controls */
104     TreeCtrl_Event,
105
106     Browse_Event,  /* For export playlist */
107
108     /* custom events */
109     UpdateItem_Event,
110
111     MenuDummy_Event = wxID_HIGHEST + 999,
112
113     FirstView_Event = wxID_HIGHEST + 1000,
114     LastView_Event = wxID_HIGHEST + 1100,
115
116     FirstSD_Event = wxID_HIGHEST + 2000,
117     LastSD_Event = wxID_HIGHEST + 2100,
118 };
119
120 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
121
122 BEGIN_EVENT_TABLE(Playlist, wxFrame)
123     EVT_SIZE(Playlist::OnSize)
124
125     /* Menu events */
126     EVT_MENU(AddFile_Event, Playlist::OnAddFile)
127     EVT_MENU(AddDir_Event, Playlist::OnAddDir)
128     EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
129     EVT_MENU(Close_Event, Playlist::OnClose)
130     EVT_MENU(Open_Event, Playlist::OnOpen)
131     EVT_MENU(Save_Event, Playlist::OnSave)
132
133     EVT_MENU(SortTitle_Event, Playlist::OnSort)
134     EVT_MENU(RSortTitle_Event, Playlist::OnSort)
135     EVT_MENU(SortAuthor_Event, Playlist::OnSort)
136     EVT_MENU(RSortAuthor_Event, Playlist::OnSort)
137
138     EVT_MENU(Randomize_Event, Playlist::OnSort)
139
140     EVT_MENU(EnableSelection_Event, Playlist::OnEnableSelection)
141     EVT_MENU(DisableSelection_Event, Playlist::OnDisableSelection)
142     EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
143     EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
144     EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
145     EVT_MENU(Infos_Event, Playlist::OnInfos)
146
147     EVT_MENU_OPEN( Playlist::OnMenuOpen )
148     EVT_MENU( -1, Playlist::OnMenuEvent )
149
150     EVT_TOOL(Random_Event, Playlist::OnRandom)
151     EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
152     EVT_TOOL(Loop_Event, Playlist::OnLoop)
153
154     /* Popup events */
155     EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
156     EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
157     EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
158     EVT_MENU( PopupEna_Event, Playlist::OnPopupEna)
159     EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
160
161     /* Tree control events */
162     EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
163
164     EVT_CONTEXT_MENU( Playlist::OnPopup )
165
166     /* Button events */
167     EVT_BUTTON( Search_Event, Playlist::OnSearch)
168     EVT_BUTTON( Save_Event, Playlist::OnSave)
169     EVT_BUTTON( Infos_Event, Playlist::OnInfos)
170
171     EVT_BUTTON( Up_Event, Playlist::OnUp)
172     EVT_BUTTON( Down_Event, Playlist::OnDown)
173
174     EVT_TEXT(SearchText_Event, Playlist::OnSearchTextChange)
175
176     /* Custom events */
177     EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
178
179     /* Special events : we don't want to destroy the window when the user
180      * clicks on (X) */
181     EVT_CLOSE(Playlist::OnClose)
182 END_EVENT_TABLE()
183
184 /*****************************************************************************
185  * PlaylistItem class
186  ****************************************************************************/
187 class PlaylistItem : public wxTreeItemData
188 {
189 public:
190     PlaylistItem( playlist_item_t *_p_item ) : wxTreeItemData()
191     {
192         p_item = _p_item;
193     }
194 protected:
195     playlist_item_t *p_item;
196 friend class Playlist;
197 };
198
199 /*****************************************************************************
200  * Constructor.
201  *****************************************************************************/
202 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
203     wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
204              wxSize(345,400), wxDEFAULT_FRAME_STYLE )
205 {
206     vlc_value_t val;
207
208     /* Initializations */
209     p_intf = _p_intf;
210     i_update_counter = 0;
211     i_sort_mode = MODE_NONE;
212     b_need_update = VLC_FALSE;
213     SetIcon( *p_intf->p_sys->p_icon );
214
215     p_view_menu = NULL;
216     p_sd_menu = SDMenu();
217
218     i_current_view = VIEW_SIMPLE;
219
220     i_title_sorted = 0;
221     i_author_sorted = 0;
222     i_group_sorted = 0;
223     i_duration_sorted = 0;
224
225     var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
226     var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
227     var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
228
229     /* Create our "Manage" menu */
230     wxMenu *manage_menu = new wxMenu;
231     manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
232     manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
233     manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
234     manage_menu->AppendSeparator();
235     manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
236                          p_sd_menu );
237     manage_menu->AppendSeparator();
238     manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
239     manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
240     manage_menu->AppendSeparator();
241     manage_menu->Append( Close_Event, wxU(_("&Close")) );
242
243     /* Create our "Sort" menu */
244     wxMenu *sort_menu = new wxMenu;
245     sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
246     sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
247     sort_menu->AppendSeparator();
248     sort_menu->Append( SortAuthor_Event, wxU(_("Sort by &author")) );
249     sort_menu->Append( RSortAuthor_Event, wxU(_("Reverse sort by author")) );
250     sort_menu->AppendSeparator();
251     sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
252
253     /* Create our "Selection" menu */
254     wxMenu *selection_menu = new wxMenu;
255     selection_menu->Append( EnableSelection_Event, wxU(_("&Enable")) );
256     selection_menu->Append( DisableSelection_Event, wxU(_("&Disable")) );
257     selection_menu->AppendSeparator();
258     selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
259     selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
260     selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
261
262     /* Create our "View" menu */
263     ViewMenu();
264
265     /* Append the freshly created menus to the menu bar */
266     wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
267     menubar->Append( manage_menu, wxU(_("&Manage")) );
268     menubar->Append( sort_menu, wxU(_("S&ort")) );
269     menubar->Append( selection_menu, wxU(_("&Selection")) );
270     menubar->Append( p_view_menu, wxU(_("&View items") ) );
271
272     /* Attach the menu bar to the frame */
273     SetMenuBar( menubar );
274
275     /* Create the popup menu */
276     popup_menu = new wxMenu;
277     popup_menu->Append( PopupPlay_Event, wxU(_("Play")) );
278     popup_menu->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
279     popup_menu->Append( PopupDel_Event, wxU(_("Delete")) );
280     popup_menu->Append( PopupEna_Event, wxU(_("Enable/Disable")) );
281     popup_menu->Append( PopupInfo_Event, wxU(_("Info")) );
282
283     /* Create a panel to put everything in */
284     wxPanel *playlist_panel = new wxPanel( this, -1 );
285     playlist_panel->SetAutoLayout( TRUE );
286
287     /* Create the toolbar */
288     wxToolBar *toolbar =
289         CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT | wxTB_DOCKABLE );
290
291     /* Create the random tool */
292     toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
293                        wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
294                        wxU(_(HELP_SHUFFLE) ) );
295     var_Get( p_intf, "random", &val );
296     toolbar->ToggleTool( Random_Event, val.b_bool );
297
298     /* Create the Loop tool */
299     toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
300                       wxBitmap( loop_xpm), wxITEM_CHECK,
301                       wxU(_(HELP_LOOP )  ) );
302     var_Get( p_intf, "loop", &val );
303     toolbar->ToggleTool( Loop_Event, val.b_bool );
304
305     /* Create the Repeat one checkbox */
306     toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
307                       wxBitmap( repeat_xpm), wxITEM_CHECK,
308                       wxU(_(HELP_REPEAT )  ) );
309     var_Get( p_intf, "repeat", &val );
310     toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
311
312     /* Create the Search Textbox */
313     search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
314                                   wxDefaultPosition, wxSize(100, -1),
315                                   wxTE_PROCESS_ENTER);
316
317     /* Create the search button */
318     search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
319
320     toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
321                          wxSize(16, 16), wxBORDER_NONE ) );
322     toolbar->AddControl( search_text );
323     toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
324                          wxSize(5, 5), wxBORDER_NONE ) );
325     toolbar->AddControl( search_button );
326     search_button->SetDefault();
327     toolbar->Realize();
328
329     /* Create the tree */
330     treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
331                                wxDefaultPosition, wxDefaultSize,
332                                wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
333                                wxTR_NO_LINES |
334                                wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
335                                wxTR_MULTIPLE | wxTR_EXTENDED );
336
337     /* Create image list */
338
339     wxImageList *p_images = new wxImageList( 16 , 16, TRUE);
340
341     wxIcon icons[10];
342     icons[ ITEM_TYPE_UNKNOWN ] = wxIcon( type_unknown_xpm );
343     icons[ ITEM_TYPE_DISC ] = wxIcon( type_disc_xpm );
344     icons[ ITEM_TYPE_DIRECTORY ] = wxIcon( type_directory_xpm );
345     icons[ ITEM_TYPE_PLAYLIST ] = wxIcon( type_playlist_xpm );
346     icons[ ITEM_TYPE_NET ] = wxIcon( type_net_xpm );
347     icons[ ITEM_TYPE_CARD ] = wxIcon( type_card_xpm );
348
349     for( unsigned int i = 0; i< WXSIZEOF( icons ) ; i++ )
350     {
351          p_images->Add( wxIcon( icons[i] ));
352     }
353
354     treectrl->AssignImageList( p_images );
355
356     treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
357
358     /* Reduce font size */
359     wxFont font= treectrl->GetFont();
360     font.SetPointSize(8);
361     treectrl->SetFont( font );
362
363     /* Create the Up-Down buttons */
364 #if 0
365     wxButton *up_button =
366         new wxButton( playlist_panel, Up_Event, wxU(_("Up") ) );
367     wxButton *down_button =
368         new wxButton( playlist_panel, Down_Event, wxU(_("Down") ) );
369
370     wxBoxSizer *updown_sizer = new wxBoxSizer( wxHORIZONTAL );
371     updown_sizer->Layout();
372     /* The top and bottom sizers */
373     wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
374     bottom_sizer->Add( up_button, 0, wxALIGN_LEFT | wxRIGHT, 3);
375     bottom_sizer->Add( down_button, 0, wxALIGN_LEFT | wxLEFT, 3);
376     bottom_sizer->Layout();
377 #endif
378     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
379     panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
380 #if 0
381     panel_sizer->Add( bottom_sizer, 0, wxALL, 5);
382 #endif
383     panel_sizer->Layout();
384
385     playlist_panel->SetSizerAndFit( panel_sizer );
386
387 #if wxUSE_DRAG_AND_DROP
388     /* Associate drop targets with the playlist */
389     SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
390 #endif
391
392     playlist_t *p_playlist =
393         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
394                                        FIND_ANYWHERE );
395     if( p_playlist == NULL )
396     {
397         return;
398     }
399
400     /* We want to be noticed of playlist changes */
401
402     /* Some global changes happened -> Rebuild all */
403     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
404
405     /* We went to the next item */
406     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
407
408     /* One item has been updated */
409     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
410
411     vlc_object_release( p_playlist );
412
413     /* Update the playlist */
414     Rebuild();
415 }
416
417 void Playlist::OnSize( wxSizeEvent& event)
418 {
419 #if 0
420     wxSize size = GetClientSize();
421     if( listview )
422         listview->SetColumnWidth( 0, size.x - listview->GetColumnWidth(1)
423                         - 15 /* margins */ );
424 #endif
425     event.Skip();
426 }
427
428 Playlist::~Playlist()
429 {
430     playlist_t *p_playlist =
431         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
432                                        FIND_ANYWHERE );
433     if( p_playlist == NULL )
434     {
435         return;
436     }
437
438     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
439     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
440     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
441     vlc_object_release( p_playlist );
442 }
443
444 /**********************************************************************
445  * Update one playlist item
446  **********************************************************************/
447 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
448                            wxTreeItemId node )
449 {
450     long cookie;
451     wxTreeItemId child;
452     for( int i = 0; i< p_node->i_children ; i++ )
453     {
454         if( i == 0 )
455         {
456             child = treectrl->GetFirstChild( node, cookie);
457         }
458         else
459         {
460             child = treectrl->GetNextChild( node, cookie );
461         }
462
463         if( !child.IsOk() )
464         {
465             /* Not enough children */
466             CreateNode( p_playlist, p_node->pp_children[i], node );
467             /* Keep the tree pointer up to date */
468             child = treectrl->GetNextChild( node, cookie );
469         }
470         else
471         {
472         }
473     }
474
475 }
476 /* Creates the node p_node as last child of parent */
477 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
478                            wxTreeItemId parent )
479 {
480     long cookie;
481     wxTreeItemId node =
482         treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
483                               -1,-1, new PlaylistItem( p_node ) );
484     treectrl->SetItemImage( node, p_node->input.i_type );
485
486     for( int i = 0; i< p_node->i_children ; i++ )
487     {
488         /* Append the item */
489         if( p_node->pp_children[i]->i_children == -1 )
490         {
491             wxTreeItemId item =
492                 treectrl->AppendItem( node,
493                     wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
494                            new PlaylistItem( p_node->pp_children[i]) );
495
496             treectrl->SetItemImage( item,
497                                     p_node->pp_children[i]->input.i_type );
498         }
499         else
500         {
501             CreateNode( p_playlist, p_node->pp_children[i],
502                         node );
503         }
504     }
505 }
506
507 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
508 {
509     long cookie;
510     PlaylistItem *p_wxcurrent;
511     wxTreeItemId search;
512     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
513     wxTreeItemId child;
514
515     while( item.IsOk() )
516     {
517         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
518         if( p_wxcurrent->p_item == p_item )
519         {
520             return item;
521         }
522         if( treectrl->ItemHasChildren( item ) )
523         {
524             wxTreeItemId search = FindItem( item, p_item );
525             if( search.IsOk() )
526             {
527                 return search;
528             }
529         }
530         item = treectrl->GetNextChild( root, cookie);
531     }
532     /* Not found */
533     wxTreeItemId dummy;
534     return dummy;
535 }
536
537 /*wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current )
538 {
539     long cookie;
540     PlaylistItem *p_wxcurrent;
541     wxTreeItemId search;
542     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
543     wxTreeItemId child;
544
545     while( item.IsOk() )
546     {
547         if( treectrl->GetItemText( item).Lower().Contains(
548                                                  search_string.Lower() ) )
549         {
550             return item;
551         if( treectrl->ItemHasChildren( item ) )
552         {
553             wxTreeItemId search = FindItem( item, p_item );
554             if( search.IsOk() )
555             {
556                 return search;
557             }
558         }
559         item = treectrl->GetNextChild( root, cookie);
560     }
561   */  /* Not found */
562     /*wxTreeItemId dummy;
563     return dummy;
564 }
565 */
566
567
568
569 void Playlist::SetCurrentItem( wxTreeItemId item )
570 {
571     if( item.IsOk() )
572     {
573         treectrl->SetItemBold( item, true );
574         treectrl->EnsureVisible( item );
575     }
576 }
577
578 void Playlist::UpdateItem( int i )
579 {
580     if( i < 0 ) return; /* Sanity check */
581     playlist_item_t *p_item;
582
583     playlist_t *p_playlist =
584         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
585                                        FIND_ANYWHERE );
586
587     if( p_playlist == NULL )
588     {
589         return;
590     }
591
592     p_item = playlist_ItemGetById( p_playlist, i );
593
594     wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
595
596     UpdateTreeItem( p_playlist, item );
597
598     vlc_object_release(p_playlist);
599
600 }
601
602 void Playlist::UpdateTreeItem( playlist_t *p_playlist ,wxTreeItemId item )
603 {
604     playlist_item_t *p_item;
605
606    if( !item.IsOk() )
607    {
608         return;
609    }
610
611     p_item  =  ((PlaylistItem *)treectrl->GetItemData( item ))->p_item;
612
613     if( !p_item )
614     {
615         return;
616     }
617
618     wxString msg;
619     char *psz_author = playlist_ItemGetInfo( p_item, _("Meta-information"),
620                                                          _("Artist"));
621     char psz_duration[MSTRTIME_MAX_SIZE];
622     mtime_t dur = p_item->input.i_duration;
623     if( dur != -1 )
624         secstotimestr( psz_duration, dur/1000000 );
625     else
626         memcpy( psz_duration, "-:--:--", sizeof("-:--:--") );
627
628     if( !strcmp( psz_author, "" ) )
629     {
630         msg.Printf( wxString( wxL2U( p_item->input.psz_name ) ) + wxU( " ( ") +
631                     wxString(wxL2U(psz_duration ) ) + wxU( ")") );
632     }
633     else
634     {
635         msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
636                     wxString(wxL2U(p_item->input.psz_name)) + wxU( " ( ") +
637                     wxString(wxL2U(psz_duration ) ) + wxU( ")") );
638     }
639     treectrl->SetItemText( item , msg );
640
641     if( p_playlist->status.p_item == p_item )
642     {
643         SetCurrentItem( item );
644     }
645     else
646     {
647         treectrl->SetItemBold( item, false );
648     }
649 #if 0
650     if( p_item->b_enabled == VLC_FALSE )
651     {
652         wxListItem listitem;
653         listitem.m_itemId = i;
654         listitem.SetTextColour( *wxLIGHT_GREY);
655         listview->SetItem(listitem);
656     }
657 #endif
658 }
659
660 /**********************************************************************
661  * Rebuild the playlist
662  **********************************************************************/
663 void Playlist::Rebuild()
664 {
665     playlist_view_t *p_view;
666     playlist_t *p_playlist =
667         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
668                                        FIND_ANYWHERE );
669     if( p_playlist == NULL )
670     {
671         return;
672     }
673
674     /* ...and rebuild it */
675     vlc_mutex_lock( &p_playlist->object_lock );
676
677     p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
678
679     /* HACK we should really get new*/
680     msg_Dbg( p_intf, "rebuilding tree" );
681     treectrl->DeleteAllItems();
682     treectrl->AddRoot( wxU(_("root" )), -1, -1,
683                          new PlaylistItem( p_view->p_root) );
684
685     wxTreeItemId root = treectrl->GetRootItem();
686     UpdateNode( p_playlist, p_view->p_root, root );
687
688     wxTreeItemId item;
689     if( p_playlist->status.p_item != NULL )
690     {
691         item = FindItem( root, p_playlist->status.p_item );
692     }
693     else if( p_playlist->status.p_node != NULL )
694     {
695         item = FindItem( root, p_playlist->status.p_node );
696     }
697     else
698     {
699         item = root;
700     }
701
702     SetCurrentItem( item );
703
704     vlc_mutex_unlock( &p_playlist->object_lock );
705
706     vlc_object_release( p_playlist );
707 }
708
709 void Playlist::ShowPlaylist( bool show )
710 {
711     if( show ) Rebuild();
712     Show( show );
713 }
714
715 void Playlist::UpdatePlaylist()
716 {
717     i_update_counter++;
718
719     /* If the playlist isn't show there's no need to update it */
720     if( !IsShown() ) return;
721
722     if( this->b_need_update )
723     {
724         this->b_need_update = VLC_FALSE;
725         Rebuild();
726     }
727
728     /* Updating the playing status every 0.5s is enough */
729     if( i_update_counter % 5 ) return;
730
731     playlist_t *p_playlist =
732         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
733                                        FIND_ANYWHERE );
734     if( p_playlist == NULL )
735     {
736         return;
737     }
738 #if 0
739     /* Update the colour of items */
740     int i_playlist_index = p_playlist->i_index;
741     if( p_intf->p_sys->i_playing != i_playlist_index )
742     {
743         wxListItem listitem;
744         listitem.m_itemId = i_playlist_index;
745         listitem.SetTextColour( *wxRED );
746         listview->SetItem( listitem );
747
748         if( p_intf->p_sys->i_playing != -1 )
749         {
750             listitem.m_itemId = p_intf->p_sys->i_playing;
751             listitem.SetTextColour( *wxBLACK );
752             listview->SetItem( listitem );
753         }
754         p_intf->p_sys->i_playing = i_playlist_index;
755     }
756 #endif
757     vlc_object_release( p_playlist );
758 }
759
760 /*****************************************************************************
761  * Private methods.
762  *****************************************************************************/
763 void Playlist::DeleteItem( int item_id )
764 {
765     playlist_t *p_playlist =
766         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
767                                        FIND_ANYWHERE );
768     if( p_playlist == NULL )
769     {
770         return;
771     }
772
773     playlist_Delete( p_playlist, item_id );
774
775     vlc_object_release( p_playlist );
776 }
777
778 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
779 {
780     Hide();
781 }
782
783 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
784 {
785     struct {
786         char *psz_desc;
787         char *psz_filter;
788         char *psz_module;
789     } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
790                    { _("PLS file"), "*.pls", "export-pls" }};
791
792     wxString filter = wxT("");
793
794     playlist_t * p_playlist =
795                 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
796                                                FIND_ANYWHERE );
797
798     if( ! p_playlist )
799     {
800         return;
801     }
802     if( p_playlist->i_size == 0 )
803     {
804         wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
805                       wxICON_WARNING | wxOK, this );
806         vlc_object_release( p_playlist );
807         return;
808     }
809
810     for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
811     {
812         filter.Append( wxU(formats[i].psz_desc) );
813         filter.Append( wxT("|") );
814         filter.Append( wxU(formats[i].psz_filter) );
815         filter.Append( wxT("|") );
816     }
817     wxFileDialog dialog( this, wxU(_("Save playlist")),
818                          wxT(""), wxT(""), filter, wxSAVE );
819
820     if( dialog.ShowModal() == wxID_OK )
821     {
822         if( dialog.GetPath().mb_str() )
823         {
824             playlist_Export( p_playlist, dialog.GetPath().mb_str(),
825                              formats[dialog.GetFilterIndex()].psz_module );
826         }
827     }
828
829     vlc_object_release( p_playlist );
830
831 }
832
833 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
834 {
835     playlist_t *p_playlist =
836         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
837                                        FIND_ANYWHERE );
838     if( p_playlist == NULL )
839     {
840         return;
841     }
842
843     wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
844         wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
845
846     if( dialog.ShowModal() == wxID_OK )
847     {
848         playlist_Import( p_playlist, dialog.GetPath().mb_str() );
849     }
850
851     vlc_object_release( p_playlist );
852 }
853
854 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
855 {
856     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
857
858 }
859
860 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
861 {
862     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
863
864 }
865
866 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
867 {
868     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
869
870 }
871
872 /********************************************************************
873  * Move functions
874  ********************************************************************/
875 void Playlist::OnUp( wxCommandEvent& event )
876 {
877     playlist_t *p_playlist =
878         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
879                                        FIND_ANYWHERE );
880     if( p_playlist == NULL )
881     {
882         return;
883     }
884 #if 0
885     /* We use the first selected item, so find it */
886     long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
887                                          wxLIST_STATE_SELECTED);
888     if( i_item > 0 && i_item < p_playlist->i_size )
889     {
890         playlist_Move( p_playlist, i_item, i_item - 1 );
891         listview->Focus( i_item - 1 );
892     }
893 #endif
894     vlc_object_release( p_playlist );
895 }
896
897 void Playlist::OnDown( wxCommandEvent& event )
898 {
899     playlist_t *p_playlist =
900         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
901                                        FIND_ANYWHERE );
902     if( p_playlist == NULL )
903     {
904         return;
905     }
906 #if 0
907     /* We use the first selected item, so find it */
908     long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
909                                          wxLIST_STATE_SELECTED );
910     if( i_item >= 0 && i_item < p_playlist->i_size - 1 )
911     {
912         playlist_Move( p_playlist, i_item, i_item + 2 );
913         listview->Focus( i_item + 1 );
914     }
915 #endif
916     vlc_object_release( p_playlist );
917 }
918
919 /********************************************************************
920  * Sorting functions
921  ********************************************************************/
922 void Playlist::OnSort( wxCommandEvent& event )
923 {
924     playlist_t *p_playlist =
925         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
926                                        FIND_ANYWHERE );
927     if( p_playlist == NULL )
928     {
929         return;
930     }
931     switch( event.GetId() )
932     {
933         case SortTitle_Event:
934            playlist_SortTitle( p_playlist, ORDER_NORMAL );
935            break;
936         case RSortTitle_Event:
937            playlist_SortTitle( p_playlist, ORDER_REVERSE );
938            break;
939         case SortAuthor_Event:
940            playlist_SortAuthor(p_playlist, ORDER_NORMAL );
941            break;
942         case RSortAuthor_Event:
943            playlist_SortAuthor( p_playlist, ORDER_REVERSE );
944            break;
945         case Randomize_Event:
946            playlist_Sort( p_playlist, SORT_RANDOM, ORDER_NORMAL );
947            break;
948     }
949     vlc_object_release( p_playlist );
950
951     Rebuild();
952 }
953
954 /**********************************************************************
955  * Search functions
956  **********************************************************************/
957 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
958 {
959    search_button->SetDefault();
960 }
961
962 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
963 {
964     wxString search_string = search_text->GetValue();
965
966     bool b_ok = false;
967     int i_current;
968     int i_first = 0 ;
969     int i_item = -1;
970 }
971
972 #if 0
973     for( i_current = 0; i_current < listview->GetItemCount(); i_current++ )
974     {
975         if( listview->GetItemState( i_current, wxLIST_STATE_SELECTED ) ==
976               wxLIST_STATE_SELECTED )
977         {
978             i_first = i_current;
979             break;
980         }
981     }
982
983     if( i_first == listview->GetItemCount() )
984     {
985         i_first = -1;
986     }
987
988     for( i_current = i_first + 1; i_current < listview->GetItemCount();
989          i_current++ )
990     {
991         wxListItem listitem;
992         listitem.SetId( i_current );
993         listview->GetItem( listitem );
994         if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
995         {
996             i_item = i_current;
997             b_ok = true;
998             break;
999         }
1000         listitem.SetColumn( 1 );
1001         listview->GetItem( listitem );
1002         if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1003         {
1004             i_item = i_current;
1005             b_ok = true;
1006             break;
1007         }
1008     }
1009     if( !b_ok )
1010     {
1011         for( i_current = -1 ; i_current < i_first - 1;
1012              i_current++ )
1013         {
1014             wxListItem listitem;
1015             listitem.SetId( i_current );
1016             listview->GetItem( listitem );
1017             if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1018             {
1019                 i_item = i_current;
1020                 b_ok = true;
1021                 break;
1022             }
1023             listitem.SetColumn( 1 );
1024             listview->GetItem( listitem );
1025             if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1026             {
1027                 i_item = i_current;
1028                 b_ok = true;
1029                 break;
1030             }
1031         }
1032     }
1033
1034     if( i_item < 0 || i_item >= listview->GetItemCount() )
1035     {
1036         return;
1037     }
1038
1039     for( long item = 0; item < listview->GetItemCount(); item++ )
1040     {
1041         listview->Select( item, FALSE );
1042     }
1043
1044     wxListItem listitem;
1045     listitem.SetId(i_item);
1046     listitem.m_state = wxLIST_STATE_SELECTED;
1047     listview->Select( i_item, TRUE );
1048     listview->Focus( i_item );
1049 }
1050 #endif
1051
1052 /**********************************************************************
1053  * Selection functions
1054  **********************************************************************/
1055 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1056 {
1057 }
1058
1059 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1060 {
1061     Rebuild();
1062 }
1063
1064 void Playlist::OnEnableSelection( wxCommandEvent& WXUNUSED(event) )
1065 {
1066     playlist_t *p_playlist =
1067         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1068                                        FIND_ANYWHERE );
1069     if( p_playlist == NULL )
1070     {
1071         return;
1072     }
1073 #if 0
1074     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
1075     {
1076         if( listview->IsSelected( item ) )
1077         {
1078             /*XXX*/
1079             playlist_Enable( p_playlist, item );
1080             UpdateItem( item );
1081         }
1082     }
1083 #endif
1084     vlc_object_release( p_playlist);
1085 }
1086
1087 void Playlist::OnDisableSelection( wxCommandEvent& WXUNUSED(event) )
1088 {
1089     playlist_t *p_playlist =
1090         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1091                                        FIND_ANYWHERE );
1092     if( p_playlist == NULL )
1093     {
1094         return;
1095     }
1096 #if 0
1097     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
1098     {
1099         if( listview->IsSelected( item ) )
1100         {
1101             /*XXX*/
1102             playlist_Disable( p_playlist, item );
1103             UpdateItem( item );
1104         }
1105     }
1106 #endif
1107     vlc_object_release( p_playlist);
1108 }
1109
1110 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1111 {
1112 }
1113
1114 /**********************************************************************
1115  * Playlist mode functions
1116  **********************************************************************/
1117 void Playlist::OnRandom( wxCommandEvent& event )
1118 {
1119     vlc_value_t val;
1120     val.b_bool = event.IsChecked();
1121     playlist_t *p_playlist =
1122         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1123                                        FIND_ANYWHERE );
1124     if( p_playlist == NULL )
1125     {
1126         return;
1127     }
1128     var_Set( p_playlist, "random", val);
1129     vlc_object_release( p_playlist );
1130 }
1131
1132 void Playlist::OnLoop( wxCommandEvent& event )
1133 {
1134     vlc_value_t val;
1135     val.b_bool = event.IsChecked();
1136     playlist_t *p_playlist =
1137         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1138                                        FIND_ANYWHERE );
1139     if( p_playlist == NULL )
1140     {
1141         return;
1142     }
1143     var_Set( p_playlist, "loop", val);
1144     vlc_object_release( p_playlist );
1145 }
1146
1147 void Playlist::OnRepeat( wxCommandEvent& event )
1148 {
1149     vlc_value_t val;
1150     val.b_bool = event.IsChecked();
1151     playlist_t *p_playlist =
1152         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1153                                        FIND_ANYWHERE );
1154     if( p_playlist == NULL )
1155     {
1156         return;
1157     }
1158     var_Set( p_playlist, "repeat", val);
1159     vlc_object_release( p_playlist );
1160 }
1161
1162 void Playlist::OnActivateItem( wxTreeEvent& event )
1163 {
1164     playlist_item_t *p_item,*p_node;
1165     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1166                                     VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1167     PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1168                                                         event.GetItem() );
1169     wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1170
1171     PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1172
1173     if( p_playlist == NULL )
1174     {
1175         return;
1176     }
1177
1178     if( p_wxitem->p_item->i_children == -1 )
1179     {
1180         p_node = p_wxparent->p_item;
1181         p_item = p_wxitem->p_item;
1182     }
1183     else
1184     {
1185         p_node = p_wxitem->p_item;
1186         if( p_wxitem->p_item->i_children > 0 )
1187         {
1188             p_item = p_wxitem->p_item->pp_children[0];
1189         }
1190         else
1191         {
1192             p_item = NULL;
1193         }
1194     }
1195
1196     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1197                       p_node, p_item );
1198
1199     vlc_object_release( p_playlist );
1200 }
1201
1202 void Playlist::OnKeyDown( wxTreeEvent& event )
1203 {
1204     long keycode = event.GetKeyCode();
1205     /* Delete selected items */
1206     if( keycode == WXK_BACK || keycode == WXK_DELETE )
1207     {
1208         /* We send a dummy event */
1209         OnDeleteSelection( event );
1210     }
1211 }
1212
1213 void Playlist::ShowInfos( int i_item )
1214 {
1215 }
1216
1217 void Playlist::OnInfos( wxCommandEvent& WXUNUSED(event) )
1218 {
1219 }
1220
1221 void Playlist::OnEnDis( wxCommandEvent& event )
1222 {
1223     playlist_t *p_playlist =
1224         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1225                                        FIND_ANYWHERE );
1226     if( p_playlist == NULL )
1227     {
1228         return;
1229     }
1230     vlc_object_release( p_playlist );
1231 }
1232
1233 /**********************************************************************
1234  * Menu
1235  **********************************************************************/
1236
1237 void Playlist::OnMenuOpen( wxMenuEvent& event)
1238 {
1239 #if defined( __WXMSW__ )
1240 #   define GetEventObject GetMenu
1241 #endif
1242
1243     if( event.GetEventObject() == p_view_menu )
1244     {
1245         p_view_menu = ViewMenu();
1246     }
1247 #if defined( __WXMSW__ )
1248 #   undef GetEventObject
1249 #endif
1250 }
1251
1252 void Playlist::OnMenuEvent( wxCommandEvent& event )
1253 {
1254     playlist_t *p_playlist =
1255         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1256                                        FIND_ANYWHERE );
1257     if( p_playlist == NULL )
1258     {
1259         return;
1260     }
1261
1262     if( event.GetId() < FirstView_Event )
1263     {
1264         event.Skip();
1265         vlc_object_release( p_playlist );
1266         return;
1267     }
1268     else if( event.GetId() < LastView_Event )
1269     {
1270
1271         int i_new_view = event.GetId() - FirstView_Event;
1272
1273         playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1274
1275         if( p_view != NULL )
1276         {
1277             i_current_view = i_new_view;
1278             playlist_ViewUpdate( p_playlist, i_new_view );
1279             Rebuild();
1280             vlc_object_release( p_playlist );
1281             return;
1282         }
1283         else if( i_new_view >= VIEW_FIRST_SORTED &&
1284                  i_new_view <= VIEW_LAST_SORTED )
1285         {
1286             playlist_ViewInsert( p_playlist, i_new_view, "View" );
1287             playlist_ViewUpdate( p_playlist, i_new_view );
1288
1289             i_current_view = i_new_view;
1290
1291             Rebuild();
1292         }
1293     }
1294     else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1295     {
1296         if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1297                                 pp_sds[event.GetId() - FirstSD_Event] ) )
1298         {
1299             playlist_ServicesDiscoveryAdd( p_playlist,
1300                             pp_sds[event.GetId() - FirstSD_Event] );
1301         }
1302         else
1303         {
1304             playlist_ServicesDiscoveryRemove( p_playlist,
1305                             pp_sds[event.GetId() - FirstSD_Event] );
1306         }
1307     }
1308     vlc_object_release( p_playlist );
1309 }
1310
1311 wxMenu * Playlist::ViewMenu()
1312 {
1313     playlist_t *p_playlist =
1314         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1315                                        FIND_ANYWHERE );
1316     if( p_playlist == NULL )
1317     {
1318         return NULL;
1319     }
1320
1321     if( !p_view_menu )
1322     {
1323         p_view_menu = new wxMenu;
1324     }
1325     else
1326     {
1327         wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1328         for( ; node; )
1329         {
1330             wxMenuItem *item = node->GetData();
1331             node = node->GetNext();
1332             p_view_menu->Delete( item );
1333         }
1334     }
1335
1336     /* FIXME : have a list of "should have" views */
1337     p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1338                            wxU(_("By category") ) );
1339     p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1340                            wxU(_("Manually added") ) );
1341     p_view_menu->Append( FirstView_Event + VIEW_ALL,
1342                            wxU(_("All items, unsorted") ) );
1343     p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1344                            wxU(_("Sorted by author") ) );
1345
1346     vlc_object_release( p_playlist);
1347
1348     return p_view_menu;
1349 }
1350
1351 wxMenu *Playlist::SDMenu()
1352 {
1353
1354     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1355                               VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1356     if( !p_playlist )
1357     {
1358         return NULL;
1359     }
1360     vlc_value_t val, val_list, text_list;
1361     p_sd_menu = new wxMenu;
1362
1363     vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1364                                         FIND_ANYWHERE );
1365
1366     int i_number = 0;
1367     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1368     {
1369         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1370
1371         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1372         {
1373             p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1374                        wxU( p_parser->psz_longname ? p_parser->psz_longname :
1375                             p_parser->psz_shortname ) );
1376
1377             if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1378                                     p_parser->psz_shortname ) )
1379             {
1380                 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1381             }
1382
1383             INSERT_ELEM( (void**)pp_sds, i_number, i_number,
1384                          (void*)p_parser->psz_shortname );
1385         }
1386     }
1387     vlc_list_release( p_list );
1388     vlc_object_release( p_playlist );
1389     return p_sd_menu;
1390 }
1391
1392
1393 /*****************************************************************************
1394  * Popup management functions
1395  *****************************************************************************/
1396 void Playlist::OnPopup( wxContextMenuEvent& event )
1397 {
1398     wxPoint pt = event.GetPosition();
1399
1400     i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1401     if( i_popup_item.IsOk() )
1402     {
1403         PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1404                                                             i_popup_item );
1405         PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1406                                    treectrl->GetItemParent( i_popup_item ) );
1407         p_popup_item = p_wxitem->p_item;
1408         p_popup_parent = p_wxparent->p_item;
1409         treectrl->SelectItem( i_popup_item );
1410         Playlist::PopupMenu( popup_menu,
1411                              ScreenToClient( wxGetMousePosition() ) );
1412     }
1413     else
1414     {
1415     }
1416 }
1417
1418 void Playlist::OnPopupPlay( wxMenuEvent& event )
1419 {
1420     playlist_t *p_playlist =
1421         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1422                                        FIND_ANYWHERE );
1423     if( p_playlist == NULL )
1424     {
1425         return;
1426     }
1427     if( p_popup_item != NULL )
1428     {
1429         if( p_popup_item->i_children > -1 )
1430         {
1431             if( event.GetId() == PopupPlay_Event &&
1432                 p_popup_item->i_children > 0 )
1433             {
1434                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1435                                   i_current_view, p_popup_item,
1436                                   p_popup_item->pp_children[0] );
1437             }
1438             else
1439             {
1440                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1441                                   i_current_view, p_popup_item, NULL );
1442             }
1443         }
1444         else
1445         {
1446             if( event.GetId() == PopupPlay_Event )
1447             {
1448                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1449                                   i_current_view, p_popup_parent,
1450                                   p_popup_item );
1451             }
1452         }
1453     }
1454     vlc_object_release( p_playlist );
1455 }
1456
1457 void Playlist::OnPopupDel( wxMenuEvent& event )
1458 {
1459     PlaylistItem *p_wxitem;
1460
1461     p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1462
1463     if( p_wxitem->p_item->i_children == -1 )
1464     {
1465         DeleteItem( p_wxitem->p_item->input.i_id );
1466     }
1467     else
1468     {
1469         //DeleteNode( p_wxitem->p_item );
1470     }
1471 }
1472
1473 void Playlist::OnPopupEna( wxMenuEvent& event )
1474 {
1475     playlist_t *p_playlist =
1476         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1477                                        FIND_ANYWHERE );
1478     if( p_playlist == NULL )
1479     {
1480         return;
1481     }
1482
1483     p_popup_item->b_enabled = VLC_TRUE - p_popup_item->b_enabled;
1484
1485     vlc_object_release( p_playlist);
1486     UpdateItem( i_popup_item );
1487 }
1488
1489 void Playlist::OnPopupInfo( wxMenuEvent& event )
1490 {
1491     if( p_popup_item )
1492     {
1493         iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1494         if( iteminfo_dialog->ShowModal() == wxID_OK )
1495         {
1496             UpdateItem( i_popup_item );
1497         }
1498         delete iteminfo_dialog;
1499     }
1500 }
1501
1502
1503 /*****************************************************************************
1504  * Custom events management
1505  *****************************************************************************/
1506 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1507 {
1508     switch( event.GetId() )
1509     {
1510     case UpdateItem_Event:
1511         UpdateItem( event.GetInt() );
1512         break;
1513     }
1514 }
1515
1516 /*****************************************************************************
1517  * PlaylistChanged: callback triggered by the intf-change playlist variable
1518  *  We don't rebuild the playlist directly here because we don't want the
1519  *  caller to block for a too long time.
1520  *****************************************************************************/
1521 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1522                             vlc_value_t oval, vlc_value_t nval, void *param )
1523 {
1524     Playlist *p_playlist_dialog = (Playlist *)param;
1525     p_playlist_dialog->b_need_update = VLC_TRUE;
1526     return VLC_SUCCESS;
1527 }
1528
1529 /*****************************************************************************
1530  * Next: callback triggered by the playlist-current playlist variable
1531  *****************************************************************************/
1532 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1533                          vlc_value_t oval, vlc_value_t nval, void *param )
1534 {
1535     Playlist *p_playlist_dialog = (Playlist *)param;
1536
1537     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1538     event.SetInt( oval.i_int );
1539     p_playlist_dialog->AddPendingEvent( event );
1540     event.SetInt( nval.i_int );
1541     p_playlist_dialog->AddPendingEvent( event );
1542
1543     return 0;
1544 }
1545
1546 /*****************************************************************************
1547  * ItemChanged: callback triggered by the item-change playlist variable
1548  *****************************************************************************/
1549 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1550                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1551 {
1552     Playlist *p_playlist_dialog = (Playlist *)param;
1553
1554     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1555     event.SetInt( new_val.i_int );
1556     p_playlist_dialog->AddPendingEvent( event );
1557
1558     return 0;
1559 }