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