]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/playlist.cpp
gcc 4.0 compile fix by Arwed von Merkatz
[vlc] / modules / gui / wxwindows / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Clément Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/OR MODIFy
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/intf.h>
30
31 #include "wxwindows.h"
32
33 #include "bitmaps/shuffle.xpm"
34 #include "bitmaps/repeat.xpm"
35 #include "bitmaps/loop.xpm"
36
37 #include "bitmaps/type_unknown.xpm"
38 #include "bitmaps/type_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
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     i_update_counter = 0;
203     i_sort_mode = MODE_NONE;
204     b_need_update = VLC_FALSE;
205     p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
206                                                 FIND_ANYWHERE );
207     if( p_playlist == NULL )
208     {
209         return;
210     }
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 )
398         free( pp_sds );
399
400     if( p_playlist == NULL )
401     {
402         return;
403     }
404
405     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
406     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
407     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
408     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
409     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
410     vlc_object_release( p_playlist );
411 }
412
413 /**********************************************************************
414  * Update functions
415  **********************************************************************/
416
417 /* Update a node */
418 void Playlist::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
419 {
420     wxTreeItemIdValue cookie;
421     wxTreeItemId child;
422     for( int i = 0; i< p_node->i_children ; i++ )
423     {
424         if( i == 0 )
425         {
426             child = treectrl->GetFirstChild( node, cookie);
427         }
428         else
429         {
430             child = treectrl->GetNextChild( node, cookie );
431         }
432
433         if( !child.IsOk() )
434         {
435             /* Not enough children */
436             CreateNode( p_node->pp_children[i], node );
437             /* Keep the tree pointer up to date */
438             child = treectrl->GetNextChild( node, cookie );
439         }
440     }
441     treectrl->SetItemImage( node, p_node->input.i_type );
442
443 }
444
445 /* Creates the node p_node as last child of parent */
446 void Playlist::CreateNode( playlist_item_t *p_node, wxTreeItemId parent )
447 {
448     wxTreeItemId node =
449         treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
450                               -1,-1, new PlaylistItem( p_node ) );
451     treectrl->SetItemImage( node, p_node->input.i_type );
452
453     UpdateNodeChildren( p_node, node );
454 }
455
456 /* Update all children (recursively) of this node */
457 void Playlist::UpdateNodeChildren( playlist_item_t *p_node,
458                                    wxTreeItemId node )
459 {
460
461     for( int i = 0; i< p_node->i_children ; i++ )
462     {
463         /* Append the item */
464         if( p_node->pp_children[i]->i_children == -1 )
465         {
466             wxTreeItemId item =
467                 treectrl->AppendItem( node,
468                     wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
469                            new PlaylistItem( p_node->pp_children[i]) );
470
471             UpdateTreeItem( item );
472         }
473         else
474         {
475             CreateNode( p_node->pp_children[i], node );
476         }
477     }
478 }
479
480 /* Update an item in the tree */
481 void Playlist::UpdateTreeItem( wxTreeItemId item )
482 {
483     if( ! item.IsOk() ) return;
484
485     wxTreeItemData *p_data = treectrl->GetItemData( item );
486     if( !p_data ) return;
487     
488     LockPlaylist( p_intf->p_sys, p_playlist );
489     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
490                                           ((PlaylistItem *)p_data)->i_id );
491     if( !p_item ) return;
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 == NULL )
499         return;
500     char psz_duration[MSTRTIME_MAX_SIZE];
501     mtime_t dur = p_item->input.i_duration;
502
503     if( dur != -1 )
504     {
505         secstotimestr( psz_duration, dur/1000000 );
506         duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
507                          wxU( " )" ) );
508     }
509
510     if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
511     {
512         msg.Printf( wxString( wxU( p_item->input.psz_name ) ) + duration );
513     }
514     else
515     {
516         msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
517                     wxString(wxU(p_item->input.psz_name)) + duration );
518     }
519     free( psz_author );
520     treectrl->SetItemText( item , msg );
521     treectrl->SetItemImage( item, p_item->input.i_type );
522
523     if( p_playlist->status.p_item == p_item )
524     {
525         treectrl->SetItemBold( item, true );
526         treectrl->EnsureVisible( item );
527     }
528     else
529     {
530         treectrl->SetItemBold( item, false );
531     }
532     UnlockPlaylist( p_intf->p_sys, p_playlist );
533 }
534
535 /* Process a AppendIt em request */
536 void Playlist::AppendItem( wxCommandEvent& event )
537 {
538     playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
539     playlist_item_t *p_item = NULL;
540
541     wxTreeItemId item,node;
542
543     if( p_add->i_view != i_current_view )
544     {
545         goto update;
546     }
547
548     node = FindItem( treectrl->GetRootItem(), p_add->i_node );
549     if( !node.IsOk() )
550     {
551         goto update;
552     }
553
554     p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
555     if( !p_item )
556         goto update;
557
558     item = treectrl->AppendItem( node,
559                                  wxL2U( p_item->input.psz_name ), -1,-1,
560                                  new PlaylistItem( p_item ) );
561     treectrl->SetItemImage( item, p_item->input.i_type );
562
563     if( item.IsOk() && p_item->i_children == -1 )
564     {
565         UpdateTreeItem( item );
566     }
567
568 update:
569     int i_count = CountItems( treectrl->GetRootItem());
570     if( i_count != p_playlist->i_size )
571     {
572         statusbar->SetStatusText( wxString::Format( wxU(_(
573                                   "%i items in playlist (%i not shown)")),
574                                   p_playlist->i_size,
575                                   p_playlist->i_size - i_count ) );
576         if( !b_changed_view )
577         {
578             i_current_view = VIEW_CATEGORY;
579             b_changed_view = VLC_TRUE;
580             b_need_update = VLC_TRUE;
581         }
582     }
583     else
584     {
585         statusbar->SetStatusText( wxString::Format( wxU(_(
586                                   "%i items in playlist")),
587                                   p_playlist->i_size ), 0 );
588     }
589
590     return;
591 }
592
593 /* Process a updateitem request */
594 void Playlist::UpdateItem( int i )
595 {
596     if( i < 0 ) return; /* Sanity check */
597     
598     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
599
600     if( item.IsOk() )
601     {
602         UpdateTreeItem( item );
603     }
604 }
605
606 void Playlist::RemoveItem( int i )
607 {
608     if( i <= 0 ) return; /* Sanity check */
609     if( i == i_saved_id ) i_saved_id = -1;
610
611     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
612
613     if( item.IsOk() )
614     {
615         treectrl->Delete( item );
616     }
617 }
618
619
620 /**********************************************************************
621  * Search functions (internal)
622  **********************************************************************/
623
624 /* Find a wxItem from a playlist id */
625 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
626 {
627     wxTreeItemIdValue cookie;
628     PlaylistItem *p_wxcurrent;
629     wxTreeItemId search;
630     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
631     wxTreeItemId child;
632
633     p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
634
635     if( i_id < 0 )
636     {
637         wxTreeItemId dummy;
638         return dummy;
639     }
640     if( i_saved_id == i_id )
641     {
642         return saved_tree_item;
643     }
644
645     if( !p_wxcurrent )
646     {
647         wxTreeItemId dummy;
648         return dummy;
649     }        
650
651     if( p_wxcurrent->i_id == i_id )
652     {
653         i_saved_id = i_id;
654         saved_tree_item = root;
655         return root;
656     }
657
658     while( item.IsOk() )
659     {
660         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
661         if( p_wxcurrent->i_id == i_id )
662         {
663             i_saved_id = i_id;
664             saved_tree_item = item;
665             return item;
666         }
667         if( treectrl->ItemHasChildren( item ) )
668         {
669             wxTreeItemId search = FindItem( item, i_id );
670             if( search.IsOk() )
671             {
672                 i_saved_id = i_id;
673                 saved_tree_item = search;
674                 return search;
675             }
676         }
677         item = treectrl->GetNextChild( root, cookie );
678     }
679     /* Not found */
680     wxTreeItemId dummy;
681     return dummy;
682 }
683
684 int Playlist::CountItems( wxTreeItemId root )
685 {
686     wxTreeItemIdValue cookie;
687     int count = 0;
688     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
689     
690     while( item.IsOk() )
691     {
692         if( treectrl->ItemHasChildren( item ) )
693         {
694             count += CountItems( item );
695         }
696         else
697         {
698             playlist_item_t *p_item;
699             LockPlaylist( p_intf->p_sys, p_playlist );
700             p_item = playlist_ItemGetById( p_playlist, ((PlaylistItem *)treectrl->GetItemData( item ))->i_id );
701             if( p_item && p_item->i_children == -1 )
702                 count++;
703             UnlockPlaylist( p_intf->p_sys, p_playlist );
704         }
705         item = treectrl->GetNextChild( root, cookie );
706     }
707     return count;
708 }
709
710 /* Find a wxItem from a name (from current) */
711 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
712 {
713     wxTreeItemIdValue cookie;
714     wxTreeItemId search;
715     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
716     wxTreeItemId child;
717
718     while( item.IsOk() )
719     {
720         if( treectrl->GetItemText( item).Lower().Contains(
721                                                  search_string.Lower() ) )
722         {
723             if( !current.IsOk() || *pb_current_found == VLC_TRUE )
724             {
725                 return item;
726             }
727             else if( current.IsOk() && item == current )
728             {
729                 *pb_current_found = VLC_TRUE;
730             }
731         }
732         if( treectrl->ItemHasChildren( item ) )
733         {
734             wxTreeItemId search = FindItemByName( item, search_string, current,
735                                                   pb_current_found );
736             if( search.IsOk() )
737             {
738                 return search;
739             }
740         }
741         item = treectrl->GetNextChild( root, cookie);
742     }
743     /* Not found */
744     wxTreeItemId dummy;
745     return dummy;
746 }
747
748 /**********************************************************************
749  * Rebuild the playlist
750  **********************************************************************/
751 void Playlist::Rebuild( vlc_bool_t b_root )
752 {
753     playlist_view_t *p_view;
754
755     /* We can remove the callbacks before locking, anyway, we won't
756      * miss anything */
757     if( b_root )
758     {
759         var_DelCallback( p_playlist, "item-change", ItemChanged, this );
760         var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
761         var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
762         var_DelCallback( p_playlist, "item-append", ItemAppended, this );
763         var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
764
765         /* ...and rebuild it */
766         LockPlaylist( p_intf->p_sys, p_playlist );
767     }
768     i_saved_id = -1;
769
770     p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
771
772     /* HACK we should really get new*/
773     treectrl->DeleteAllItems();
774     treectrl->AddRoot( wxU(_("root" )), -1, -1,
775                          new PlaylistItem( p_view->p_root) );
776
777     wxTreeItemId root = treectrl->GetRootItem();
778     UpdateNode( p_view->p_root, root );
779
780     int i_count = CountItems( treectrl->GetRootItem() );
781
782     if( i_count < p_playlist->i_size && !b_changed_view )
783     {
784         i_current_view = VIEW_CATEGORY;
785         b_changed_view = VLC_TRUE;
786         Rebuild( VLC_FALSE );
787     }
788     else if( i_count != p_playlist->i_size )
789     {
790         statusbar->SetStatusText( wxString::Format( wxU(_(
791                                   "%i items in playlist (%i not shown)")),
792                                   p_playlist->i_size,
793                                   p_playlist->i_size - i_count ) );
794     }
795     else
796     {
797         statusbar->SetStatusText( wxString::Format( wxU(_(
798                                   "%i items in playlist")),
799                                   p_playlist->i_size ), 0 );
800     }
801
802     if( b_root )
803     {
804         /* Put callbacks back online */
805         var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
806         var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
807         var_AddCallback( p_playlist, "item-change", ItemChanged, this );
808         var_AddCallback( p_playlist, "item-append", ItemAppended, this );
809         var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
810
811         UnlockPlaylist( p_intf->p_sys, p_playlist );
812     }
813 }
814
815
816
817 void Playlist::ShowPlaylist( bool show )
818 {
819     if( show ) Rebuild( VLC_TRUE );
820     Show( show );
821 }
822
823 /* This function is called on a regular basis */
824 void Playlist::UpdatePlaylist()
825 {
826     i_update_counter++;
827
828     /* If the playlist isn't show there's no need to update it */
829     if( !IsShown() ) return;
830
831     if( this->b_need_update )
832     {
833         this->b_need_update = VLC_FALSE;
834         Rebuild( VLC_TRUE );
835     }
836
837     /* Updating the playing status every 0.5s is enough */
838     if( i_update_counter % 5 ) return;
839 }
840
841 /*****************************************************************************
842  * Private methods.
843  *****************************************************************************/
844 void Playlist::DeleteTreeItem( wxTreeItemId item )
845 {
846    PlaylistItem *p_wxitem;
847    playlist_item_t *p_item;
848    p_wxitem = (PlaylistItem *)treectrl->GetItemData( item );
849
850    LockPlaylist( p_intf->p_sys, p_playlist );
851    p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
852
853    if( !p_item )
854    {
855        UnlockPlaylist( p_intf->p_sys, p_playlist );
856        return;
857    }
858
859    if( p_item->i_children == -1 )
860    {
861        UnlockPlaylist( p_intf->p_sys, p_playlist );
862        DeleteItem( p_item->input.i_id );
863    }
864    else
865    {
866        UnlockPlaylist( p_intf->p_sys, p_playlist );
867        DeleteNode( p_item );
868    }
869    RemoveItem( item );
870 }
871
872
873
874 void Playlist::DeleteItem( int item_id )
875 {
876     playlist_LockDelete( p_playlist, item_id );
877 }
878
879 void Playlist::DeleteNode( playlist_item_t *p_item )
880 {
881     playlist_NodeDelete( p_playlist, p_item, VLC_TRUE , VLC_FALSE );
882 }
883
884 void Playlist::OnMenuClose( wxCommandEvent& event )
885 {
886     wxCloseEvent cevent;
887     OnClose(cevent);
888 }
889
890 void Playlist::OnClose( wxCloseEvent& WXUNUSED(event) )
891 {
892     Hide();
893 }
894
895 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
896 {
897     struct {
898         char *psz_desc;
899         char *psz_filter;
900         char *psz_module;
901     } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" }};
902
903     wxString filter = wxT("");
904
905     if( p_playlist->i_size == 0 )
906     {
907         wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
908                       wxICON_WARNING | wxOK, this );
909         return;
910     }
911
912     for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
913     {
914         filter.Append( wxU(formats[i].psz_desc) );
915         filter.Append( wxT("|") );
916         filter.Append( wxU(formats[i].psz_filter) );
917         filter.Append( wxT("|") );
918     }
919     wxFileDialog dialog( this, wxU(_("Save playlist")),
920                          wxT(""), wxT(""), filter, wxSAVE );
921
922     if( dialog.ShowModal() == wxID_OK )
923     {
924         if( dialog.GetPath().mb_str() )
925         {
926             playlist_Export( p_playlist, dialog.GetPath().mb_str(),
927                              formats[dialog.GetFilterIndex()].psz_module );
928         }
929     }
930
931 }
932
933 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
934 {
935     wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
936         wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
937
938     if( dialog.ShowModal() == wxID_OK )
939     {
940         playlist_Import( p_playlist, dialog.GetPath().mb_str() );
941     }
942 }
943
944 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
945 {
946     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
947
948 }
949
950 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
951 {
952     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
953
954 }
955
956 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
957 {
958     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
959
960 }
961
962 /********************************************************************
963  * Sorting functions
964  ********************************************************************/
965 void Playlist::OnSort( wxCommandEvent& event )
966 {
967     PlaylistItem *p_wxitem;
968     p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
969
970     LockPlaylist( p_intf->p_sys, p_playlist );
971     switch( event.GetId() )
972     {
973         case SortTitle_Event:
974             playlist_RecursiveNodeSort( p_playlist, playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
975                                         SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
976             break;
977         case RSortTitle_Event:
978             playlist_RecursiveNodeSort( p_playlist, playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
979                                         SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
980     }
981     UnlockPlaylist( p_intf->p_sys, p_playlist );
982
983     Rebuild( VLC_TRUE );
984 }
985
986 /**********************************************************************
987  * Search functions (user)
988  **********************************************************************/
989 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
990 {
991    search_button->SetDefault();
992 }
993
994 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
995 {
996     wxString search_string = search_text->GetValue();
997
998     vlc_bool_t pb_found = VLC_FALSE;
999
1000     wxTreeItemId found =
1001      FindItemByName( treectrl->GetRootItem(), search_string,
1002                      search_current, &pb_found );
1003
1004     if( found.IsOk() )
1005     {
1006         search_current = found;
1007         treectrl->SelectItem( found, true );
1008     }
1009     else
1010     {
1011         wxTreeItemId dummy;
1012         search_current = dummy;
1013         found =  FindItemByName( treectrl->GetRootItem(), search_string,
1014                                  search_current, &pb_found );
1015         if( found.IsOk() )
1016         {
1017             search_current = found;
1018             treectrl->SelectItem( found, true );
1019         }
1020     }
1021 }
1022
1023 /**********************************************************************
1024  * Selection functions
1025  **********************************************************************/
1026 void Playlist::RecursiveDeleteSelection(  wxTreeItemId root )
1027 {
1028     wxTreeItemIdValue cookie;
1029     wxTreeItemId child = treectrl->GetFirstChild( root, cookie );
1030     while( child.IsOk() )
1031     {
1032         if( treectrl->ItemHasChildren( child ) )
1033         {
1034             RecursiveDeleteSelection( child );
1035             if( treectrl->IsSelected(child ) ) DeleteTreeItem( child );
1036         }
1037         else if( treectrl->IsSelected( child ) )
1038             DeleteTreeItem( child );
1039         child = treectrl->GetNextChild( root, cookie );
1040     }
1041 }
1042
1043 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1044 {
1045     RecursiveDeleteSelection( treectrl->GetRootItem() );
1046 }
1047
1048 /**********************************************************************
1049  * Playlist mode functions
1050  **********************************************************************/
1051 void Playlist::OnRandom( wxCommandEvent& event )
1052 {
1053     vlc_value_t val;
1054     val.b_bool = event.IsChecked();
1055     var_Set( p_playlist, "random", val);
1056 }
1057
1058 void Playlist::OnLoop( wxCommandEvent& event )
1059 {
1060     vlc_value_t val;
1061     val.b_bool = event.IsChecked();
1062     var_Set( p_playlist, "loop", val);
1063 }
1064
1065 void Playlist::OnRepeat( wxCommandEvent& event )
1066 {
1067     vlc_value_t val;
1068     val.b_bool = event.IsChecked();
1069     var_Set( p_playlist, "repeat", val);
1070 }
1071
1072 /********************************************************************
1073  * Event
1074  ********************************************************************/
1075 void Playlist::OnActivateItem( wxTreeEvent& event )
1076 {
1077     playlist_item_t *p_item,*p_node,*p_item2,*p_node2;
1078
1079     PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1080                                                             event.GetItem() );
1081     wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1082
1083     PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1084
1085     LockPlaylist( p_intf->p_sys, p_playlist );
1086
1087     if( !( p_wxitem && p_wxparent ) ) return;
1088
1089     p_item2 = playlist_ItemGetById(p_playlist, p_wxitem->i_id);
1090     p_node2 = playlist_ItemGetById(p_playlist, p_wxparent->i_id);
1091     if( p_item2 && p_item2->i_children == -1 )
1092     {
1093         p_node = p_node2;
1094         p_item = p_item2;
1095     }
1096     else
1097     {
1098         p_node = p_item2;
1099         if( p_node && p_node->i_children > 0 &&
1100             p_node->pp_children[0]->i_children == -1)
1101         {
1102             p_item = p_node->pp_children[0];
1103         }
1104         else
1105         {
1106             p_item = NULL;
1107         }
1108     }
1109
1110     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1111                       p_node, p_item );
1112     UnlockPlaylist( p_intf->p_sys, p_playlist );
1113 }
1114
1115 void Playlist::OnKeyDown( wxTreeEvent& event )
1116 {
1117     long keycode = event.GetKeyCode();
1118     /* Delete selected items */
1119     if( keycode == WXK_BACK || keycode == WXK_DELETE )
1120     {
1121         /* We send a dummy event */
1122         OnDeleteSelection( event );
1123     }
1124     else
1125     {
1126         event.Skip();
1127     }
1128 }
1129
1130 void Playlist::OnEnDis( wxCommandEvent& event )
1131 {
1132     msg_Warn( p_intf, "not implemented" );
1133 }
1134
1135 /**********************************************************************
1136  * Menu
1137  **********************************************************************/
1138
1139 void Playlist::OnMenuOpen( wxMenuEvent& event)
1140 {
1141 #if defined( __WXMSW__ )
1142 #   define GetEventObject GetMenu
1143 #endif
1144
1145     if( event.GetEventObject() == p_view_menu )
1146     {
1147         p_view_menu = ViewMenu();
1148     }
1149 #if defined( __WXMSW__ )
1150 #   undef GetEventObject
1151 #endif
1152 }
1153
1154 void Playlist::OnMenuEvent( wxCommandEvent& event )
1155 {
1156     if( event.GetId() < FirstView_Event )
1157     {
1158         event.Skip();
1159         return;
1160     }
1161     else if( event.GetId() < LastView_Event )
1162     {
1163
1164         int i_new_view = event.GetId() - FirstView_Event;
1165
1166         playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1167
1168         if( p_view != NULL )
1169         {
1170             b_changed_view = VLC_TRUE;
1171             i_current_view = i_new_view;
1172             playlist_ViewUpdate( p_playlist, i_new_view );
1173             Rebuild( VLC_TRUE );
1174             return;
1175         }
1176         else if( i_new_view >= VIEW_FIRST_SORTED &&
1177                  i_new_view <= VIEW_LAST_SORTED )
1178         {
1179             b_changed_view = VLC_TRUE;
1180             playlist_ViewInsert( p_playlist, i_new_view, "View" );
1181             playlist_ViewUpdate( p_playlist, i_new_view );
1182
1183             i_current_view = i_new_view;
1184
1185             Rebuild( VLC_TRUE );
1186         }
1187     }
1188     else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1189     {
1190         if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1191                                 pp_sds[event.GetId() - FirstSD_Event] ) )
1192         {
1193             playlist_ServicesDiscoveryAdd( p_playlist,
1194                             pp_sds[event.GetId() - FirstSD_Event] );
1195         }
1196         else
1197         {
1198             //wxMutexGuiLeave();
1199             playlist_ServicesDiscoveryRemove( p_playlist,
1200                             pp_sds[event.GetId() - FirstSD_Event] );
1201             //wxMutexGuiEnter();
1202         }
1203     }
1204 }
1205
1206 wxMenu * Playlist::ViewMenu()
1207 {
1208     if( !p_view_menu )
1209     {
1210         p_view_menu = new wxMenu;
1211     }
1212     else
1213     {
1214         wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1215         for( ; node; )
1216         {
1217             wxMenuItem *item = node->GetData();
1218             node = node->GetNext();
1219             p_view_menu->Delete( item );
1220         }
1221     }
1222
1223     /* FIXME : have a list of "should have" views */
1224     p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1225                            wxU(_("Normal") ) );
1226     p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1227                            wxU(_("Sorted by artist") ) );
1228
1229     return p_view_menu;
1230 }
1231
1232 wxMenu *Playlist::SDMenu()
1233 {
1234     p_sd_menu = new wxMenu;
1235
1236     vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1237                                         FIND_ANYWHERE );
1238
1239     int i_number = 0;
1240     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1241     {
1242         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1243
1244         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1245             i_number++;
1246     }
1247     if( i_number ) pp_sds = (char **)calloc( i_number, sizeof(void *) );
1248
1249     i_number = 0;
1250     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1251     {
1252         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1253
1254         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1255         {
1256             p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1257                 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1258                      (p_parser->psz_shortname ?
1259                       p_parser->psz_shortname : p_parser->psz_object_name) ) );
1260
1261             if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1262                                     p_parser->psz_object_name ) )
1263             {
1264                 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1265             }
1266
1267             pp_sds[i_number++] = p_parser->psz_object_name;
1268         }
1269     }
1270     vlc_list_release( p_list );
1271     return p_sd_menu;
1272 }
1273
1274
1275 /*****************************************************************************
1276  * Popup management functions
1277  *****************************************************************************/
1278 void Playlist::OnPopup( wxContextMenuEvent& event )
1279 {
1280     wxPoint pt = event.GetPosition();
1281     playlist_item_t *p_item;
1282
1283     i_wx_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1284     if( i_wx_popup_item.IsOk() )
1285     {
1286         PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1287                                                             i_wx_popup_item );
1288         PlaylistItem *p_wxparent= (PlaylistItem *)treectrl->GetItemData(
1289                                   treectrl->GetItemParent( i_wx_popup_item ) );
1290         i_popup_item = p_wxitem->i_id;
1291         i_popup_parent = p_wxparent->i_id;
1292         treectrl->SelectItem( i_wx_popup_item );
1293
1294         LockPlaylist( p_intf->p_sys, p_playlist );
1295         p_item = playlist_ItemGetById( p_playlist, i_popup_item );
1296
1297         if( !p_item )
1298         {
1299             UnlockPlaylist( p_intf->p_sys, p_playlist );
1300             return;
1301         }
1302         if( p_item->i_children == -1 )
1303         {
1304             UnlockPlaylist( p_intf->p_sys, p_playlist );
1305             Playlist::PopupMenu( item_popup,
1306                                  ScreenToClient( wxGetMousePosition() ) );
1307         }
1308         else
1309         {
1310             UnlockPlaylist( p_intf->p_sys, p_playlist );
1311             Playlist::PopupMenu( node_popup,
1312                                  ScreenToClient( wxGetMousePosition() ) );
1313         }
1314     }
1315 }
1316
1317 void Playlist::OnPopupPlay( wxCommandEvent& event )
1318 {
1319     playlist_item_t *p_popup_item, *p_popup_parent;
1320     LockPlaylist( p_intf->p_sys, p_playlist );
1321     p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1322     p_popup_parent = playlist_ItemGetById( p_playlist, i_popup_parent );
1323     if( p_popup_item != NULL )
1324     {
1325         if( p_popup_item->i_children > -1 )
1326         {
1327             if( event.GetId() == PopupPlay_Event &&
1328                 p_popup_item->i_children > 0 )
1329             {
1330                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1331                                   i_current_view, p_popup_item,
1332                                   p_popup_item->pp_children[0] );
1333             }
1334             else
1335             {
1336                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1337                                   i_current_view, p_popup_item, NULL );
1338             }
1339         }
1340         else
1341         {
1342             if( event.GetId() == PopupPlay_Event )
1343             {
1344                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1345                                   i_current_view, p_popup_parent,
1346                                   p_popup_item );
1347             }
1348         }
1349     }
1350     UnlockPlaylist( p_intf->p_sys, p_playlist );
1351 }
1352
1353 void Playlist::OnPopupPreparse( wxCommandEvent& event )
1354 {
1355     Preparse();
1356 }
1357
1358 void Playlist::Preparse()
1359 {
1360     playlist_item_t *p_popup_item;
1361     LockPlaylist( p_intf->p_sys, p_playlist );
1362     p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1363
1364     if( p_popup_item != NULL )
1365     {
1366         if( p_popup_item->i_children == -1 )
1367         {
1368             playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1369         }
1370         else
1371         {
1372             int i = 0;
1373             playlist_item_t *p_parent = p_popup_item;
1374             for( i = 0; i< p_parent->i_children ; i++ )
1375             {
1376                 wxMenuEvent dummy;
1377                 i_wx_popup_item = FindItem( treectrl->GetRootItem(),
1378                                          p_parent->pp_children[i]->input.i_id );
1379                 i_popup_item = p_parent->pp_children[i]->input.i_id;
1380                 Preparse();
1381             }
1382         }
1383     }
1384     UnlockPlaylist( p_intf->p_sys, p_playlist );
1385 }
1386
1387 void Playlist::OnPopupDel( wxCommandEvent& event )
1388 {
1389     DeleteTreeItem( i_wx_popup_item );
1390 }
1391
1392 void Playlist::OnPopupSort( wxCommandEvent& event )
1393 {
1394     PlaylistItem *p_wxitem;
1395     playlist_item_t *p_item;
1396
1397     p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_wx_popup_item );
1398     LockPlaylist( p_intf->p_sys, p_playlist );
1399
1400     p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
1401     if( p_item->i_children >= 0 )
1402     {
1403         playlist_RecursiveNodeSort( p_playlist, p_item,
1404                                     SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1405         
1406         treectrl->DeleteChildren( i_wx_popup_item );
1407         i_saved_id = -1;
1408         UpdateNodeChildren( p_item, i_wx_popup_item );
1409
1410     }
1411     UnlockPlaylist( p_intf->p_sys, p_playlist );
1412 }
1413
1414 void Playlist::OnPopupInfo( wxCommandEvent& event )
1415 {
1416     LockPlaylist( p_intf->p_sys, p_playlist );
1417     playlist_item_t *p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1418     if( p_popup_item )
1419     {
1420         iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1421         if( iteminfo_dialog->ShowModal() == wxID_OK )
1422         {
1423             UpdateItem( i_wx_popup_item );
1424         }
1425         delete iteminfo_dialog;
1426     }
1427     UnlockPlaylist( p_intf->p_sys, p_playlist );
1428 }
1429
1430
1431 /*****************************************************************************
1432  * Custom events management
1433  *****************************************************************************/
1434 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1435 {
1436     switch( event.GetId() )
1437     {
1438         case UpdateItem_Event:
1439             UpdateItem( event.GetInt() );
1440             break;
1441         case AppendItem_Event:
1442             AppendItem( event );
1443             break;
1444         case RemoveItem_Event:
1445             RemoveItem( event.GetInt() );
1446             break;
1447     }
1448 }
1449
1450 /*****************************************************************************
1451  * PlaylistChanged: callback triggered by the intf-change playlist variable
1452  *  We don't rebuild the playlist directly here because we don't want the
1453  *  caller to block for a too long time.
1454  *****************************************************************************/
1455 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1456                             vlc_value_t oval, vlc_value_t nval, void *param )
1457 {
1458     Playlist *p_playlist_dialog = (Playlist *)param;
1459     p_playlist_dialog->b_need_update = VLC_TRUE;
1460     return VLC_SUCCESS;
1461 }
1462
1463 /*****************************************************************************
1464  * Next: callback triggered by the playlist-current playlist variable
1465  *****************************************************************************/
1466 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1467                          vlc_value_t oval, vlc_value_t nval, void *param )
1468 {
1469     Playlist *p_playlist_dialog = (Playlist *)param;
1470
1471     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1472     event.SetInt( oval.i_int );
1473     p_playlist_dialog->AddPendingEvent( event );
1474     event.SetInt( nval.i_int );
1475     p_playlist_dialog->AddPendingEvent( event );
1476
1477     return 0;
1478 }
1479
1480 /*****************************************************************************
1481  * ItemChanged: callback triggered by the item-change playlist variable
1482  *****************************************************************************/
1483 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1484                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1485 {
1486     Playlist *p_playlist_dialog = (Playlist *)param;
1487
1488     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1489     event.SetInt( new_val.i_int );
1490     p_playlist_dialog->AddPendingEvent( event );
1491
1492     return 0;
1493 }
1494 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1495                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1496 {
1497     Playlist *p_playlist_dialog = (Playlist *)param;
1498
1499     wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1500     event.SetInt( new_val.i_int );
1501     p_playlist_dialog->AddPendingEvent( event );
1502
1503     return 0;
1504 }
1505
1506 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1507                          vlc_value_t oval, vlc_value_t nval, void *param )
1508 {
1509     Playlist *p_playlist_dialog = (Playlist *)param;
1510
1511     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1512
1513     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1514
1515     wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1516     event.SetClientData( (void *)p_add );
1517     p_playlist_dialog->AddPendingEvent( event );
1518
1519     return VLC_SUCCESS;
1520 }
1521 }