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