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