]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/dialogs/playlist.cpp
Beginning of a WX implementation
[vlc] / modules / gui / wxwidgets / dialogs / 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 "dialogs/playlist.hpp"
29 #include "dialogs/iteminfo.hpp"
30 #include "interface.hpp" // Needed for D&D - TODO: Split
31
32 #include "bitmaps/shuffle.xpm"
33 #include "bitmaps/repeat.xpm"
34 #include "bitmaps/loop.xpm"
35
36 #include "bitmaps/type_unknown.xpm"
37 #include "bitmaps/type_afile.xpm"
38 #include "bitmaps/type_vfile.xpm"
39 #include "bitmaps/type_net.xpm"
40 #include "bitmaps/type_card.xpm"
41 #include "bitmaps/type_disc.xpm"
42 #include "bitmaps/type_cdda.xpm"
43 #include "bitmaps/type_directory.xpm"
44 #include "bitmaps/type_playlist.xpm"
45 #include "bitmaps/type_node.xpm"
46
47 #include <wx/dynarray.h>
48 #include <wx/imaglist.h>
49
50 #define HELP_SHUFFLE N_( "Shuffle" )
51 #define HELP_LOOP N_( "Repeat All" )
52 #define HELP_REPEAT N_( "Repeat One" )
53
54 namespace wxvlc {
55 /* Callback prototype */
56 static int PlaylistChanged( vlc_object_t *, const char *,
57                             vlc_value_t, vlc_value_t, void * );
58 static int PlaylistNext( vlc_object_t *, const char *,
59                          vlc_value_t, vlc_value_t, void * );
60 static int ItemChanged( vlc_object_t *, const char *,
61                         vlc_value_t, vlc_value_t, void * );
62 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
63                       vlc_value_t oval, vlc_value_t nval, void *param );
64 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
65                       vlc_value_t oval, vlc_value_t nval, void *param );
66
67 /*****************************************************************************
68  * Event Table.
69  *****************************************************************************/
70
71 /* IDs for the controls and the menu commands */
72 enum
73 {
74     /* menu items */
75     AddFile_Event = 1,
76     AddDir_Event,
77     AddMRL_Event,
78     Close_Event,
79     Open_Event,
80     Save_Event,
81
82     SortTitle_Event,
83     RSortTitle_Event,
84     Randomize_Event,
85
86     DeleteSelection_Event,
87     Random_Event,
88     Loop_Event,
89     Repeat_Event,
90
91     PopupPlay_Event,
92     PopupPlayThis_Event,
93     PopupPreparse_Event,
94     PopupSort_Event,
95     PopupDel_Event,
96     PopupInfo_Event,
97
98     SearchText_Event,
99     Search_Event,
100
101     /* controls */
102     TreeCtrl_Event,
103
104     Browse_Event,  /* For export playlist */
105
106     /* custom events */
107     UpdateItem_Event,
108     AppendItem_Event,
109     RemoveItem_Event,
110
111     MenuDummy_Event = wxID_HIGHEST + 999,
112
113     FirstView_Event = wxID_HIGHEST + 1000,
114     LastView_Event = wxID_HIGHEST + 1100,
115
116     FirstSD_Event = wxID_HIGHEST + 2000,
117     LastSD_Event = wxID_HIGHEST + 2100,
118 };
119
120 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
121
122 BEGIN_EVENT_TABLE(Playlist, wxFrame)
123     EVT_SIZE(Playlist::OnSize)
124
125     /* Menu events */
126     EVT_MENU(AddFile_Event, Playlist::OnAddFile)
127     EVT_MENU(AddDir_Event, Playlist::OnAddDir)
128     EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
129     EVT_MENU(Close_Event, Playlist::OnMenuClose)
130     EVT_MENU(Open_Event, Playlist::OnOpen)
131     EVT_MENU(Save_Event, Playlist::OnSave)
132
133     EVT_MENU(SortTitle_Event, Playlist::OnSort)
134     EVT_MENU(RSortTitle_Event, Playlist::OnSort)
135
136     EVT_MENU(Randomize_Event, Playlist::OnSort)
137
138     EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
139
140     EVT_MENU_OPEN( Playlist::OnMenuOpen )
141     EVT_MENU( -1, Playlist::OnMenuEvent )
142
143     EVT_TOOL(Random_Event, Playlist::OnRandom)
144     EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
145     EVT_TOOL(Loop_Event, Playlist::OnLoop)
146
147     /* Popup events */
148     EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
149     EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
150     EVT_MENU( PopupPreparse_Event, Playlist::OnPopupPreparse)
151     EVT_MENU( PopupSort_Event, Playlist::OnPopupSort)
152     EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
153     EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
154
155     /* Tree control events */
156     EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
157     EVT_TREE_KEY_DOWN( -1, Playlist::OnKeyDown )
158
159     EVT_CONTEXT_MENU( Playlist::OnPopup )
160
161     /* Button events */
162     EVT_BUTTON( Search_Event, Playlist::OnSearch)
163     EVT_BUTTON( Save_Event, Playlist::OnSave)
164
165     /*EVT_TEXT( SearchText_Event, Playlist::OnSearchTextChange )*/
166     EVT_TEXT_ENTER( SearchText_Event, Playlist::OnSearch )
167
168     /* Custom events */
169     EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
170
171     /* Special events : we don't want to destroy the window when the user
172      * clicks on (X) */
173     EVT_CLOSE(Playlist::OnClose)
174 END_EVENT_TABLE()
175
176 /*****************************************************************************
177  * PlaylistItem class
178  ****************************************************************************/
179 class PlaylistItem : public wxTreeItemData
180 {
181 public:
182     PlaylistItem( playlist_item_t *p_item ) : wxTreeItemData()
183     {
184         i_id = p_item->input.i_id;
185     }
186 protected:
187     int i_id;
188 friend class Playlist;
189 };
190
191 /*****************************************************************************
192  * Constructor.
193  *****************************************************************************/
194 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
195     wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
196              wxSize(500,300), wxDEFAULT_FRAME_STYLE )
197 {
198     vlc_value_t val;
199
200     /* Initializations */
201     p_intf = _p_intf;
202     pp_sds = NULL;
203     i_update_counter = 0;
204     i_sort_mode = MODE_NONE;
205     b_need_update = VLC_FALSE;
206     i_items_to_append = 0;
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 )
487     {
488         UnlockPlaylist( p_intf->p_sys, p_playlist );
489         return;
490     }
491
492     wxString msg;
493     wxString duration = wxU( "" );
494     char *psz_author = vlc_input_item_GetInfo( &p_item->input,
495                                                      _("Meta-information"),
496                                                      _("Artist"));
497     if( !psz_author )
498     {
499         UnlockPlaylist( p_intf->p_sys, p_playlist );
500         return;
501     }
502
503     char psz_duration[MSTRTIME_MAX_SIZE];
504     mtime_t dur = p_item->input.i_duration;
505
506     if( dur != -1 )
507     {
508         secstotimestr( psz_duration, dur/1000000 );
509         duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
510                          wxU( " )" ) );
511     }
512
513     if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
514     {
515         msg = wxString( wxU( p_item->input.psz_name ) ) + duration;
516     }
517     else
518     {
519         msg = wxString(wxU( psz_author )) + wxT(" - ") +
520                     wxString(wxU(p_item->input.psz_name)) + duration;
521     }
522     free( psz_author );
523     treectrl->SetItemText( item , msg );
524     treectrl->SetItemImage( item, p_item->input.i_type );
525
526     if( p_playlist->status.p_item == p_item )
527     {
528         treectrl->SetItemBold( item, true );
529         while( treectrl->GetItemParent( item ).IsOk() )
530         {
531             item = treectrl->GetItemParent( item );
532             treectrl->Expand( item );
533         }
534     }
535     else
536     {
537         treectrl->SetItemBold( item, false );
538     }
539     UnlockPlaylist( p_intf->p_sys, p_playlist );
540 }
541
542 /* Process a AppendIt em request */
543 void Playlist::AppendItem( wxCommandEvent& event )
544 {
545     playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
546     playlist_item_t *p_item = NULL;
547     wxTreeItemId item, node;
548
549     i_items_to_append--;
550
551     /* No need to do anything if the playlist is going to be rebuilt */
552     if( b_need_update ) return;
553
554     if( p_add->i_view != i_current_view ) goto update;
555
556     node = FindItem( treectrl->GetRootItem(), p_add->i_node );
557     if( !node.IsOk() ) goto update;
558
559     p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
560     if( !p_item ) goto update;
561
562     item = FindItem( treectrl->GetRootItem(), p_add->i_item );
563     if( item.IsOk() ) goto update;
564
565     item = treectrl->AppendItem( node,
566                                  wxL2U( p_item->input.psz_name ), -1,-1,
567                                  new PlaylistItem( p_item ) );
568     treectrl->SetItemImage( item, p_item->input.i_type );
569
570     if( item.IsOk() && p_item->i_children == -1 )
571     {
572         UpdateTreeItem( item );
573     }
574
575 update:
576     int i_count = CountItems( treectrl->GetRootItem());
577     if( i_count != p_playlist->i_size )
578     {
579         statusbar->SetStatusText( wxString::Format( wxU(_(
580                                   "%i items in playlist (%i not shown)")),
581                                   p_playlist->i_size,
582                                   p_playlist->i_size - i_count ) );
583         if( !b_changed_view )
584         {
585             i_current_view = VIEW_CATEGORY;
586             b_changed_view = VLC_TRUE;
587             b_need_update = VLC_TRUE;
588         }
589     }
590     else
591     {
592         statusbar->SetStatusText( wxString::Format( wxU(_(
593                                   "%i items in playlist")),
594                                   p_playlist->i_size ), 0 );
595     }
596
597     return;
598 }
599
600 /* Process a updateitem request */
601 void Playlist::UpdateItem( int i )
602 {
603     if( i < 0 ) return; /* Sanity check */
604
605     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
606
607     if( item.IsOk() )
608     {
609         UpdateTreeItem( item );
610     }
611 }
612
613 void Playlist::RemoveItem( int i )
614 {
615     if( i <= 0 ) return; /* Sanity check */
616     if( i == i_saved_id ) i_saved_id = -1;
617
618     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
619
620     if( item.IsOk() )
621     {
622         treectrl->Delete( item );
623     }
624 }
625
626
627 /**********************************************************************
628  * Search functions (internal)
629  **********************************************************************/
630
631 /* Find a wxItem from a playlist id */
632 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
633 {
634     wxTreeItemIdValue cookie;
635     PlaylistItem *p_wxcurrent;
636     wxTreeItemId search;
637     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
638     wxTreeItemId child;
639
640     p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
641
642     if( i_id < 0 )
643     {
644         wxTreeItemId dummy;
645         return dummy;
646     }
647     if( i_saved_id == i_id )
648     {
649         return saved_tree_item;
650     }
651
652     if( !p_wxcurrent )
653     {
654         wxTreeItemId dummy;
655         return dummy;
656     }
657
658     if( p_wxcurrent->i_id == i_id )
659     {
660         i_saved_id = i_id;
661         saved_tree_item = root;
662         return root;
663     }
664
665     while( item.IsOk() )
666     {
667         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
668         if( p_wxcurrent->i_id == i_id )
669         {
670             i_saved_id = i_id;
671             saved_tree_item = item;
672             return item;
673         }
674         if( treectrl->ItemHasChildren( item ) )
675         {
676             wxTreeItemId search = FindItem( item, i_id );
677             if( search.IsOk() )
678             {
679                 i_saved_id = i_id;
680                 saved_tree_item = search;
681                 return search;
682             }
683         }
684         item = treectrl->GetNextChild( root, cookie );
685     }
686     /* Not found */
687     wxTreeItemId dummy;
688     return dummy;
689 }
690
691 int Playlist::CountItems( wxTreeItemId root )
692 {
693     wxTreeItemIdValue cookie;
694     int count = 0;
695     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
696
697     while( item.IsOk() )
698     {
699         if( treectrl->ItemHasChildren( item ) )
700         {
701             count += CountItems( item );
702         }
703         else
704         {
705             playlist_item_t *p_item;
706             LockPlaylist( p_intf->p_sys, p_playlist );
707             p_item = playlist_ItemGetById( p_playlist, ((PlaylistItem *)treectrl->GetItemData( item ))->i_id );
708             if( p_item && p_item->i_children == -1 )
709                 count++;
710             UnlockPlaylist( p_intf->p_sys, p_playlist );
711         }
712         item = treectrl->GetNextChild( root, cookie );
713     }
714     return count;
715 }
716
717 /* Find a wxItem from a name (from current) */
718 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
719 {
720     wxTreeItemIdValue cookie;
721     wxTreeItemId search;
722     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
723     wxTreeItemId child;
724
725     while( item.IsOk() )
726     {
727         if( treectrl->GetItemText( item).Lower().Contains(
728                                                  search_string.Lower() ) )
729         {
730             if( !current.IsOk() || *pb_current_found == VLC_TRUE )
731             {
732                 return item;
733             }
734             else if( current.IsOk() && item == current )
735             {
736                 *pb_current_found = VLC_TRUE;
737             }
738         }
739         if( treectrl->ItemHasChildren( item ) )
740         {
741             wxTreeItemId search = FindItemByName( item, search_string, current,
742                                                   pb_current_found );
743             if( search.IsOk() )
744             {
745                 return search;
746             }
747         }
748         item = treectrl->GetNextChild( root, cookie);
749     }
750     /* Not found */
751     wxTreeItemId dummy;
752     return dummy;
753 }
754
755 /**********************************************************************
756  * Rebuild the playlist
757  **********************************************************************/
758 void Playlist::Rebuild( vlc_bool_t b_root )
759 {
760     playlist_view_t *p_view;
761
762     i_items_to_append = 0;
763
764     /* We can remove the callbacks before locking, anyway, we won't
765      * miss anything */
766     if( b_root )
767     {
768         var_DelCallback( p_playlist, "item-change", ItemChanged, this );
769         var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
770         var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
771         var_DelCallback( p_playlist, "item-append", ItemAppended, this );
772         var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
773
774         /* ...and rebuild it */
775         LockPlaylist( p_intf->p_sys, p_playlist );
776     }
777     i_saved_id = -1;
778
779     p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
780
781     /* HACK we should really get new*/
782     treectrl->DeleteAllItems();
783     treectrl->AddRoot( wxU(_("root" )), -1, -1,
784                          new PlaylistItem( p_view->p_root) );
785
786     wxTreeItemId root = treectrl->GetRootItem();
787     UpdateNode( p_view->p_root, root );
788
789     int i_count = CountItems( treectrl->GetRootItem() );
790
791     if( i_count < p_playlist->i_size && !b_changed_view )
792     {
793         i_current_view = VIEW_CATEGORY;
794         b_changed_view = VLC_TRUE;
795         Rebuild( VLC_FALSE );
796     }
797     else if( i_count != p_playlist->i_size )
798     {
799         statusbar->SetStatusText( wxString::Format( wxU(_(
800                                   "%i items in playlist (%i not shown)")),
801                                   p_playlist->i_size,
802                                   p_playlist->i_size - i_count ) );
803     }
804     else
805     {
806         statusbar->SetStatusText( wxString::Format( wxU(_(
807                                   "%i items in playlist")),
808                                   p_playlist->i_size ), 0 );
809     }
810
811     if( b_root )
812     {
813         /* Put callbacks back online */
814         var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
815         var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
816         var_AddCallback( p_playlist, "item-change", ItemChanged, this );
817         var_AddCallback( p_playlist, "item-append", ItemAppended, this );
818         var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
819
820         UnlockPlaylist( p_intf->p_sys, p_playlist );
821     }
822 }
823
824
825
826 void Playlist::ShowPlaylist( bool show )
827 {
828     if( show ) Rebuild( VLC_TRUE );
829     Show( show );
830 }
831
832 /* This function is called on a regular basis */
833 void Playlist::UpdatePlaylist()
834 {
835     i_update_counter++;
836
837     /* If the playlist isn't show there's no need to update it */
838     if( !IsShown() ) return;
839
840     if( this->b_need_update )
841     {
842         this->b_need_update = VLC_FALSE;
843         Rebuild( VLC_TRUE );
844     }
845
846     /* Updating the playing status every 0.5s is enough */
847     if( i_update_counter % 5 ) return;
848 }
849
850 /*****************************************************************************
851  * Private methods.
852  *****************************************************************************/
853 void Playlist::DeleteTreeItem( wxTreeItemId item )
854 {
855    PlaylistItem *p_wxitem;
856    playlist_item_t *p_item;
857    p_wxitem = (PlaylistItem *)treectrl->GetItemData( item );
858
859    LockPlaylist( p_intf->p_sys, p_playlist );
860    p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
861
862    if( !p_item )
863    {
864        UnlockPlaylist( p_intf->p_sys, p_playlist );
865        return;
866    }
867
868    if( p_item->i_children == -1 ) DeleteItem( p_item->input.i_id );
869    else DeleteNode( p_item );
870
871    RemoveItem( item );
872    UnlockPlaylist( p_intf->p_sys, p_playlist );
873 }
874
875 void Playlist::DeleteItem( int item_id )
876 {
877     playlist_Delete( p_playlist, item_id );
878 }
879
880 void Playlist::DeleteNode( playlist_item_t *p_item )
881 {
882     playlist_NodeDelete( p_playlist, p_item, VLC_TRUE , VLC_FALSE );
883 }
884
885 void Playlist::OnMenuClose( wxCommandEvent& event )
886 {
887     wxCloseEvent cevent;
888     OnClose(cevent);
889 }
890
891 void Playlist::OnClose( wxCloseEvent& WXUNUSED(event) )
892 {
893     Hide();
894 }
895
896 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
897 {
898     struct {
899         char *psz_desc;
900         char *psz_filter;
901         char *psz_module;
902     } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" }};
903
904     wxString filter = wxT("");
905
906     if( p_playlist->i_size == 0 )
907     {
908         wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
909                       wxICON_WARNING | wxOK, this );
910         return;
911     }
912
913     for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
914     {
915         filter.Append( wxU(formats[i].psz_desc) );
916         filter.Append( wxT("|") );
917         filter.Append( wxU(formats[i].psz_filter) );
918         filter.Append( wxT("|") );
919     }
920     wxFileDialog dialog( this, wxU(_("Save playlist")),
921                          wxT(""), wxT(""), filter, wxSAVE );
922
923     if( dialog.ShowModal() == wxID_OK )
924     {
925         if( dialog.GetPath().mb_str() )
926         {
927             playlist_Export( p_playlist, dialog.GetPath().mb_str(),
928                              formats[dialog.GetFilterIndex()].psz_module );
929         }
930     }
931
932 }
933
934 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
935 {
936     wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
937         wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
938
939     if( dialog.ShowModal() == wxID_OK )
940     {
941         playlist_Import( p_playlist, dialog.GetPath().mb_str() );
942     }
943 }
944
945 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
946 {
947     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
948
949 }
950
951 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
952 {
953     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
954
955 }
956
957 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
958 {
959     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
960
961 }
962
963 /********************************************************************
964  * Sorting functions
965  ********************************************************************/
966 void Playlist::OnSort( wxCommandEvent& event )
967 {
968     PlaylistItem *p_wxitem;
969     p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
970
971     LockPlaylist( p_intf->p_sys, p_playlist );
972     switch( event.GetId() )
973     {
974         case SortTitle_Event:
975             playlist_RecursiveNodeSort( p_playlist,
976                             playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
977                             SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
978             break;
979         case RSortTitle_Event:
980             playlist_RecursiveNodeSort( p_playlist,
981                             playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
982                             SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
983     }
984     UnlockPlaylist( p_intf->p_sys, p_playlist );
985
986     Rebuild( VLC_TRUE );
987 }
988
989 /**********************************************************************
990  * Search functions (user)
991  **********************************************************************/
992 /*void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
993 {
994    search_button->SetDefault();
995 }*/
996
997 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
998 {
999     wxString search_string = search_text->GetValue();
1000
1001     vlc_bool_t pb_found = VLC_FALSE;
1002
1003     wxTreeItemId found =
1004      FindItemByName( treectrl->GetRootItem(), search_string,
1005                      search_current, &pb_found );
1006
1007     if( !found.IsOk() )
1008     {
1009         wxTreeItemId dummy;
1010         search_current = dummy;
1011         found =  FindItemByName( treectrl->GetRootItem(), search_string,
1012                                  search_current, &pb_found );
1013     }
1014
1015     if( found.IsOk() )
1016     {
1017         search_current = found;
1018         treectrl->EnsureVisible( found );
1019         treectrl->UnselectAll();
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         p_item = NULL;
1105 /*        if( p_node && p_node->i_children > 0 &&
1106             p_node->pp_children[0]->i_children == -1)
1107         {
1108             p_item = p_node->pp_children[0];
1109         }
1110         else
1111         {
1112             p_item = NULL;
1113         }*/
1114     }
1115
1116     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1117                       p_node, p_item );
1118     UnlockPlaylist( p_intf->p_sys, p_playlist );
1119 }
1120
1121 void Playlist::OnKeyDown( wxTreeEvent& event )
1122 {
1123     long keycode = event.GetKeyCode();
1124     /* Delete selected items */
1125     if( keycode == WXK_BACK || keycode == WXK_DELETE )
1126     {
1127         /* We send a dummy event */
1128         OnDeleteSelection( event );
1129     }
1130     else
1131     {
1132         event.Skip();
1133     }
1134 }
1135
1136 void Playlist::OnEnDis( wxCommandEvent& event )
1137 {
1138     msg_Warn( p_intf, "not implemented" );
1139 }
1140
1141 /**********************************************************************
1142  * Menu
1143  **********************************************************************/
1144
1145 void Playlist::OnMenuOpen( wxMenuEvent& event)
1146 {
1147 #if defined( __WXMSW__ )
1148 #   define GetEventObject GetMenu
1149 #endif
1150
1151     if( event.GetEventObject() == p_view_menu )
1152     {
1153         p_view_menu = ViewMenu();
1154     }
1155 #if defined( __WXMSW__ )
1156 #   undef GetEventObject
1157 #endif
1158 }
1159
1160 void Playlist::OnMenuEvent( wxCommandEvent& event )
1161 {
1162     if( event.GetId() < FirstView_Event )
1163     {
1164         event.Skip();
1165         return;
1166     }
1167     else if( event.GetId() < LastView_Event )
1168     {
1169
1170         int i_new_view = event.GetId() - FirstView_Event;
1171
1172         playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1173
1174         if( p_view != NULL )
1175         {
1176             b_changed_view = VLC_TRUE;
1177             i_current_view = i_new_view;
1178             playlist_ViewUpdate( p_playlist, i_new_view );
1179             Rebuild( VLC_TRUE );
1180             return;
1181         }
1182         else if( i_new_view >= VIEW_FIRST_SORTED &&
1183                  i_new_view <= VIEW_LAST_SORTED )
1184         {
1185             b_changed_view = VLC_TRUE;
1186             playlist_ViewInsert( p_playlist, i_new_view, "View" );
1187             playlist_ViewUpdate( p_playlist, i_new_view );
1188
1189             i_current_view = i_new_view;
1190
1191             Rebuild( VLC_TRUE );
1192         }
1193     }
1194     else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1195     {
1196         if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1197                                 pp_sds[event.GetId() - FirstSD_Event] ) )
1198         {
1199             playlist_ServicesDiscoveryAdd( p_playlist,
1200                             pp_sds[event.GetId() - FirstSD_Event] );
1201         }
1202         else
1203         {
1204             //wxMutexGuiLeave();
1205             playlist_ServicesDiscoveryRemove( p_playlist,
1206                             pp_sds[event.GetId() - FirstSD_Event] );
1207             //wxMutexGuiEnter();
1208         }
1209     }
1210 }
1211
1212 wxMenu * Playlist::ViewMenu()
1213 {
1214     if( !p_view_menu )
1215     {
1216         p_view_menu = new wxMenu;
1217     }
1218     else
1219     {
1220         wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1221         for( ; node; )
1222         {
1223             wxMenuItem *item = node->GetData();
1224             node = node->GetNext();
1225             p_view_menu->Delete( item );
1226         }
1227     }
1228
1229     /* FIXME : have a list of "should have" views */
1230     p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1231                            wxU(_("Normal") ) );
1232     p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1233                            wxU(_("Sorted by artist") ) );
1234     p_view_menu->Append( FirstView_Event + VIEW_S_ALBUM,
1235                            wxU(_("Sorted by Album") ) );
1236
1237     return p_view_menu;
1238 }
1239
1240 wxMenu *Playlist::SDMenu()
1241 {
1242     p_sd_menu = new wxMenu;
1243
1244     vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1245                                         FIND_ANYWHERE );
1246
1247     int i_number = 0;
1248     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1249     {
1250         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1251
1252         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1253             i_number++;
1254     }
1255     if( i_number ) pp_sds = (char **)calloc( i_number, sizeof(void *) );
1256
1257     i_number = 0;
1258     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1259     {
1260         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1261
1262         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1263         {
1264             p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1265                 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1266                      (p_parser->psz_shortname ?
1267                       p_parser->psz_shortname : p_parser->psz_object_name) ) );
1268
1269             if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1270                                     p_parser->psz_object_name ) )
1271             {
1272                 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1273             }
1274
1275             pp_sds[i_number++] = p_parser->psz_object_name;
1276         }
1277     }
1278     vlc_list_release( p_list );
1279     return p_sd_menu;
1280 }
1281
1282
1283 /*****************************************************************************
1284  * Popup management functions
1285  *****************************************************************************/
1286 void Playlist::OnPopup( wxContextMenuEvent& event )
1287 {
1288     wxPoint pt = event.GetPosition();
1289     playlist_item_t *p_item;
1290
1291     i_wx_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1292     if( i_wx_popup_item.IsOk() )
1293     {
1294         PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1295                                                             i_wx_popup_item );
1296         PlaylistItem *p_wxparent= (PlaylistItem *)treectrl->GetItemData(
1297                                   treectrl->GetItemParent( i_wx_popup_item ) );
1298         i_popup_item = p_wxitem->i_id;
1299         i_popup_parent = p_wxparent->i_id;
1300         treectrl->SelectItem( i_wx_popup_item );
1301
1302         LockPlaylist( p_intf->p_sys, p_playlist );
1303         p_item = playlist_ItemGetById( p_playlist, i_popup_item );
1304
1305         if( !p_item )
1306         {
1307             UnlockPlaylist( p_intf->p_sys, p_playlist );
1308             return;
1309         }
1310         if( p_item->i_children == -1 )
1311         {
1312             UnlockPlaylist( p_intf->p_sys, p_playlist );
1313             Playlist::PopupMenu( item_popup,
1314                                  ScreenToClient( wxGetMousePosition() ) );
1315         }
1316         else
1317         {
1318             UnlockPlaylist( p_intf->p_sys, p_playlist );
1319             Playlist::PopupMenu( node_popup,
1320                                  ScreenToClient( wxGetMousePosition() ) );
1321         }
1322     }
1323 }
1324
1325 void Playlist::OnPopupPlay( wxCommandEvent& event )
1326 {
1327     playlist_item_t *p_popup_item, *p_popup_parent;
1328     LockPlaylist( p_intf->p_sys, p_playlist );
1329     p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1330     p_popup_parent = playlist_ItemGetById( p_playlist, i_popup_parent );
1331     if( p_popup_item != NULL )
1332     {
1333         if( p_popup_item->i_children > -1 )
1334         {
1335             if( event.GetId() == PopupPlay_Event &&
1336                 p_popup_item->i_children > 0 )
1337             {
1338                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1339                                   i_current_view, p_popup_item,
1340                                   p_popup_item->pp_children[0] );
1341             }
1342             else
1343             {
1344                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1345                                   i_current_view, p_popup_item, NULL );
1346             }
1347         }
1348         else
1349         {
1350             if( event.GetId() == PopupPlay_Event )
1351             {
1352                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1353                                   i_current_view, p_popup_parent,
1354                                   p_popup_item );
1355             }
1356         }
1357     }
1358     UnlockPlaylist( p_intf->p_sys, p_playlist );
1359 }
1360
1361 void Playlist::OnPopupPreparse( wxCommandEvent& event )
1362 {
1363     Preparse();
1364 }
1365
1366 void Playlist::Preparse()
1367 {
1368     playlist_item_t *p_popup_item;
1369     LockPlaylist( p_intf->p_sys, p_playlist );
1370     p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1371
1372     if( p_popup_item != NULL )
1373     {
1374         if( p_popup_item->i_children == -1 )
1375         {
1376             playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1377         }
1378         else
1379         {
1380             int i = 0;
1381             playlist_item_t *p_parent = p_popup_item;
1382             for( i = 0; i< p_parent->i_children ; i++ )
1383             {
1384                 wxMenuEvent dummy;
1385                 i_wx_popup_item = FindItem( treectrl->GetRootItem(),
1386                                          p_parent->pp_children[i]->input.i_id );
1387                 i_popup_item = p_parent->pp_children[i]->input.i_id;
1388                 Preparse();
1389             }
1390         }
1391     }
1392     UnlockPlaylist( p_intf->p_sys, p_playlist );
1393 }
1394
1395 void Playlist::OnPopupDel( wxCommandEvent& event )
1396 {
1397     DeleteTreeItem( i_wx_popup_item );
1398 }
1399
1400 void Playlist::OnPopupSort( wxCommandEvent& event )
1401 {
1402     PlaylistItem *p_wxitem;
1403     playlist_item_t *p_item;
1404
1405     p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_wx_popup_item );
1406     LockPlaylist( p_intf->p_sys, p_playlist );
1407
1408     p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
1409     if( p_item->i_children >= 0 )
1410     {
1411         playlist_RecursiveNodeSort( p_playlist, p_item,
1412                                     SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1413
1414         treectrl->DeleteChildren( i_wx_popup_item );
1415         i_saved_id = -1;
1416         UpdateNodeChildren( p_item, i_wx_popup_item );
1417
1418     }
1419     UnlockPlaylist( p_intf->p_sys, p_playlist );
1420 }
1421
1422 void Playlist::OnPopupInfo( wxCommandEvent& event )
1423 {
1424     LockPlaylist( p_intf->p_sys, p_playlist );
1425     playlist_item_t *p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1426     if( p_popup_item )
1427     {
1428         iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1429         if( iteminfo_dialog->ShowModal() == wxID_OK )
1430         {
1431             UpdateItem( i_wx_popup_item );
1432         }
1433         delete iteminfo_dialog;
1434     }
1435     UnlockPlaylist( p_intf->p_sys, p_playlist );
1436 }
1437
1438
1439 /*****************************************************************************
1440  * Custom events management
1441  *****************************************************************************/
1442 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1443 {
1444     switch( event.GetId() )
1445     {
1446         case UpdateItem_Event:
1447             UpdateItem( event.GetInt() );
1448             break;
1449         case AppendItem_Event:
1450             AppendItem( event );
1451             break;
1452         case RemoveItem_Event:
1453             RemoveItem( event.GetInt() );
1454             break;
1455     }
1456 }
1457
1458 /*****************************************************************************
1459  * PlaylistChanged: callback triggered by the intf-change playlist variable
1460  *  We don't rebuild the playlist directly here because we don't want the
1461  *  caller to block for a too long time.
1462  *****************************************************************************/
1463 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1464                             vlc_value_t oval, vlc_value_t nval, void *param )
1465 {
1466     Playlist *p_playlist_dialog = (Playlist *)param;
1467     p_playlist_dialog->b_need_update = VLC_TRUE;
1468     return VLC_SUCCESS;
1469 }
1470
1471 /*****************************************************************************
1472  * Next: callback triggered by the playlist-current playlist variable
1473  *****************************************************************************/
1474 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1475                          vlc_value_t oval, vlc_value_t nval, void *param )
1476 {
1477     Playlist *p_playlist_dialog = (Playlist *)param;
1478
1479     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1480     event.SetInt( oval.i_int );
1481     p_playlist_dialog->AddPendingEvent( event );
1482     event.SetInt( nval.i_int );
1483     p_playlist_dialog->AddPendingEvent( event );
1484
1485     return 0;
1486 }
1487
1488 /*****************************************************************************
1489  * ItemChanged: callback triggered by the item-change playlist variable
1490  *****************************************************************************/
1491 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1492                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1493 {
1494     Playlist *p_playlist_dialog = (Playlist *)param;
1495
1496     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1497     event.SetInt( new_val.i_int );
1498     p_playlist_dialog->AddPendingEvent( event );
1499
1500     return 0;
1501 }
1502 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1503                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1504 {
1505     Playlist *p_playlist_dialog = (Playlist *)param;
1506
1507     wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1508     event.SetInt( new_val.i_int );
1509     p_playlist_dialog->AddPendingEvent( event );
1510
1511     return 0;
1512 }
1513
1514 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1515                          vlc_value_t oval, vlc_value_t nval, void *param )
1516 {
1517     Playlist *p_playlist_dialog = (Playlist *)param;
1518
1519     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1520     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1521
1522     if( p_playlist_dialog->i_items_to_append++ > 50 )
1523     {
1524         /* Too many items waiting to be added, it will be quicker to rebuild
1525          * the whole playlist */
1526         p_playlist_dialog->b_need_update = VLC_TRUE;
1527         return VLC_SUCCESS;
1528     }
1529
1530     wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1531     event.SetClientData( (void *)p_add );
1532     p_playlist_dialog->AddPendingEvent( event );
1533
1534     return VLC_SUCCESS;
1535 }
1536 }