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