]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/dialogs/playlist.cpp
Merge branch 'master' of git@git.videolan.org:vlc
[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 = 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( true );
410 }
411
412 Playlist::~Playlist()
413 {
414     if( pp_sds != NULL )
415     {
416         char **pp_sd = pp_sds;
417         for( ; *pp_sd; pp_sd++ ) free( *pp_sd );
418         free( pp_sds );
419     }
420
421     if( p_playlist == NULL ) return;
422
423     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
424     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
425     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
426     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
427     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
428     vlc_object_release( p_playlist );
429 }
430
431 /**********************************************************************
432  * Update functions
433  **********************************************************************/
434
435 /* Update a node */
436 void Playlist::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
437 {
438     wxTreeItemIdValue cookie;
439     wxTreeItemId child;
440     for( int i = 0; i< p_node->i_children ; i++ )
441     {
442         if( i == 0 )
443         {
444             child = treectrl->GetFirstChild( node, cookie);
445         }
446         else
447         {
448             child = treectrl->GetNextChild( node, cookie );
449         }
450
451         if( !child.IsOk() )
452         {
453             /* Not enough children */
454             CreateNode( p_node->pp_children[i], node );
455             /* Keep the tree pointer up to date */
456             child = treectrl->GetNextChild( node, cookie );
457         }
458     }
459     treectrl->SetItemImage( node, p_node->p_input->i_type );
460
461 }
462
463 /* Creates the node p_node as last child of parent */
464 void Playlist::CreateNode( playlist_item_t *p_node, wxTreeItemId parent )
465 {
466     wxTreeItemId node =
467         treectrl->AppendItem( parent, wxL2U( p_node->p_input->psz_name ),
468                               -1,-1, new PlaylistItem( p_node ) );
469     treectrl->SetItemImage( node, p_node->p_input->i_type );
470
471     UpdateNodeChildren( p_node, node );
472 }
473
474 /* Update all children (recursively) of this node */
475 void Playlist::UpdateNodeChildren( playlist_item_t *p_node,
476                                    wxTreeItemId node )
477 {
478     for( int i = 0; i< p_node->i_children ; i++ )
479     {
480         /* Append the item */
481         if( p_node->pp_children[i]->i_children == -1 )
482         {
483             if( !(p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG) )
484             {
485                 wxTreeItemId item =
486                     treectrl->AppendItem( node,
487                     wxL2U( p_node->pp_children[i]->p_input->psz_name ), -1,-1,
488                            new PlaylistItem( p_node->pp_children[i]) );
489
490                 UpdateTreeItem( item );
491             }
492         }
493         else
494         {
495             CreateNode( p_node->pp_children[i], node );
496         }
497     }
498 }
499
500 /* Update an item in the tree */
501 void Playlist::UpdateTreeItem( wxTreeItemId item )
502 {
503     LockPlaylist( p_intf->p_sys, p_playlist );
504     if( ! item.IsOk() ) return;
505
506     wxTreeItemData *p_data = treectrl->GetItemData( item );
507     if( !p_data ) return;
508
509     playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
510                                     ((PlaylistItem *)p_data)->i_id, true );
511     if( !p_item )
512     {
513         UnlockPlaylist( p_intf->p_sys, p_playlist );
514         return;
515     }
516
517     wxString msg;
518     wxString duration = wxU( "" );
519
520     char *psz_artist = input_item_GetArtist( p_item->p_input );
521     char *psz_name = input_item_GetName( p_item->p_input );
522
523     char psz_duration[MSTRTIME_MAX_SIZE];
524     mtime_t dur = input_item_GetDuration( p_item->p_input );
525
526     if( dur != -1 )
527     {
528         secstotimestr( psz_duration, dur/1000000 );
529         duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
530                          wxU( " )" ) );
531     }
532
533     if( !psz_artist || !strcmp( psz_artist, "" ) || p_item->p_input->b_fixed_name == true )
534     {
535         msg = wxString( wxU( psz_name ) ) + duration;
536     }
537     else
538     {
539         msg = wxString(wxU( psz_artist )) + wxT(" - ") +
540               wxString(wxU(psz_name)) + duration;
541     }
542     free( psz_artist );
543     free( psz_name );
544     treectrl->SetItemText( item , msg );
545     treectrl->SetItemImage( item, p_item->p_input->i_type );
546
547     if( p_playlist->status.p_item == p_item )
548     {
549         treectrl->SetItemBold( item, true );
550         while( treectrl->GetItemParent( item ).IsOk() )
551         {
552             item = treectrl->GetItemParent( item );
553             if( ! (item == treectrl->GetRootItem() &&
554                 treectrl->HasFlag( wxTR_HIDE_ROOT ) ) )
555                 treectrl->Expand( item );
556         }
557     }
558     else
559     {
560         treectrl->SetItemBold( item, false );
561     }
562     UnlockPlaylist( p_intf->p_sys, p_playlist );
563 }
564
565 /* Process a AppendIt em request */
566 void Playlist::AppendItem( wxCommandEvent& event )
567 {
568     playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
569     playlist_item_t *p_item = NULL;
570     wxTreeItemId item, node;
571
572     i_items_to_append--;
573
574     /* No need to do anything if the playlist is going to be rebuilt */
575     if( b_need_update ) return;
576
577     node = FindItem( treectrl->GetRootItem(), p_add->i_node );
578     if( !node.IsOk() ) goto update;
579
580     p_item = playlist_ItemGetById( p_playlist, p_add->i_item, true );
581     if( !p_item ) goto update;
582     if( (p_item->i_flags & PLAYLIST_DBL_FLAG ) ) goto update;
583
584     item = FindItem( treectrl->GetRootItem(), p_add->i_item );
585     if( item.IsOk() ) goto update;
586
587     item = treectrl->AppendItem( node,
588                                  wxL2U( p_item->p_input->psz_name ), -1,-1,
589                                  new PlaylistItem( p_item ) );
590     treectrl->SetItemImage( item, p_item->p_input->i_type );
591
592     if( item.IsOk() && p_item->i_children == -1 )
593     {
594         UpdateTreeItem( item );
595     }
596
597 update:
598     int i_count = CountItems( treectrl->GetRootItem());
599     statusbar->SetStatusText( wxString::Format( wxU(_(
600                                   "%i items in playlist" ) ), i_count ) );
601     return;
602 }
603
604 /* Process a updateitem request */
605 void Playlist::UpdateItem( int i )
606 {
607     if( i < 0 ) return; /* Sanity check */
608     wxTreeItemId item = FindItemByInput( treectrl->GetRootItem(), i );
609     if( item.IsOk() )
610     {
611         UpdateTreeItem( item );
612     }
613 }
614
615 void Playlist::RemoveItem( int i )
616 {
617     if( i <= 0 ) return; /* Sanity check */
618     if( i == i_saved_id ) i_saved_id = -1;
619
620     /* Hack: always invalidate input item cache */
621     i_saved_input_id = -1;
622
623     /// \todo Check if it is in the source selector */
624
625     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
626
627     if( item.IsOk() )
628     {
629         treectrl->Delete( item );
630     }
631 }
632
633
634 /**********************************************************************
635  * Search functions (internal)
636  **********************************************************************/
637
638 /* Find a wxItem from a playlist id */
639 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
640 {
641     return FindItemInner( root, i_id, false );
642 }
643
644 wxTreeItemId Playlist::FindItemByInput( wxTreeItemId root, int i_input_id )
645 {
646     return FindItemInner( root, i_input_id, true );
647 }
648
649 wxTreeItemId Playlist::FindItemInner( wxTreeItemId root, int i_id, bool b_byinput )
650 {
651     wxTreeItemIdValue cookie;
652     PlaylistItem *p_wxcurrent;
653     wxTreeItemId search;
654     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
655     wxTreeItemId child;
656
657     p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
658
659     if( i_id < 0 )
660     {
661         wxTreeItemId dummy; dummy.Unset(); return dummy;
662     }
663     if( b_byinput && i_saved_input_id == i_id )
664         return saved_input_tree_item;
665     if( !b_byinput && i_saved_id == i_id)
666         return saved_tree_item;
667
668     if( !p_wxcurrent )
669     {
670         wxTreeItemId dummy; dummy.Unset(); return dummy;
671     }
672
673     if( !b_byinput && p_wxcurrent->i_id == i_id  )
674     {
675         i_saved_id = i_id;
676         saved_tree_item = root;
677         return root;
678     }
679     if( b_byinput && p_wxcurrent->i_input_id == i_id )
680     {
681         i_saved_input_id = i_id;
682         saved_input_tree_item = root;
683         return root;
684     }
685
686     while( item.IsOk() )
687     {
688         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
689         if( !b_byinput && p_wxcurrent->i_id == i_id )
690         {
691             i_saved_id = i_id;
692             saved_tree_item = item;
693             return item;
694         }
695         else if( b_byinput && p_wxcurrent->i_input_id == i_id )
696         {
697             i_saved_input_id = i_id;
698             saved_input_tree_item = item;
699             return item;
700         }
701         if( treectrl->ItemHasChildren( item ) )
702         {
703             wxTreeItemId search = FindItemInner( item, i_id, b_byinput );
704             if( search.IsOk() )
705             {
706                 if( !b_byinput )
707                 {
708                     i_saved_id = i_id;
709                     saved_tree_item = search;
710                     return search;
711                 }
712                 else
713                 {
714                     i_saved_input_id = i_id;
715                     saved_input_tree_item = search;
716                     return search;
717
718                 }
719             }
720         }
721         item = treectrl->GetNextChild( root, cookie );
722     }
723     /* Not found */
724     wxTreeItemId dummy; dummy.Unset(); return dummy;
725 }
726
727 int Playlist::CountItems( wxTreeItemId root )
728 {
729     wxTreeItemIdValue cookie;
730     int count = 0;
731     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
732
733     while( item.IsOk() )
734     {
735         if( treectrl->ItemHasChildren( item ) )
736         {
737             count += CountItems( item );
738         }
739         else
740         {
741             playlist_item_t *p_item;
742             LockPlaylist( p_intf->p_sys, p_playlist );
743             p_item = playlist_ItemGetById( p_playlist, ((PlaylistItem *)treectrl->GetItemData( item ))->i_id, true );
744             if( p_item && p_item->i_children == -1 )
745                 count++;
746             UnlockPlaylist( p_intf->p_sys, p_playlist );
747         }
748         item = treectrl->GetNextChild( root, cookie );
749     }
750     return count;
751 }
752
753 /* Find a wxItem from a name (from current) */
754 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, bool *pb_current_found )
755 {
756     wxTreeItemIdValue cookie;
757     wxTreeItemId search;
758     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
759     wxTreeItemId child;
760
761     while( item.IsOk() )
762     {
763         if( treectrl->GetItemText( item).Lower().Contains(
764                                                  search_string.Lower() ) )
765         {
766             if( !current.IsOk() || *pb_current_found == true )
767             {
768                 return item;
769             }
770             else if( current.IsOk() && item == current )
771             {
772                 *pb_current_found = true;
773             }
774         }
775         if( treectrl->ItemHasChildren( item ) )
776         {
777             wxTreeItemId search = FindItemByName( item, search_string, current,
778                                                   pb_current_found );
779             if( search.IsOk() )
780             {
781                 return search;
782             }
783         }
784         item = treectrl->GetNextChild( root, cookie);
785     }
786     /* Not found */
787     wxTreeItemId dummy; dummy.Unset();
788     return dummy;
789 }
790
791 /**********************************************************************
792  * Rebuild the playlist
793  **********************************************************************/
794 void Playlist::Rebuild( bool b_root )
795 {
796     i_items_to_append = 0;
797
798     LockPlaylist( p_intf->p_sys, p_playlist );
799
800     /* Invalidate cache */
801     i_saved_id = -1;
802     i_saved_input_id = -1;
803
804     /* Rebuild the list */
805     source_sel->ClearAll();
806     for( int i = 0 ; i< p_current_viewroot->i_children ; i++ )
807     {
808         source_sel->InsertItem( i,
809                wxL2U( p_current_viewroot->pp_children[i]->p_input->psz_name) );
810         source_sel->SetItemData( i,
811                         p_current_viewroot->pp_children[i]->i_id );
812         if( p_current_viewroot->pp_children[i] == p_current_treeroot )
813             source_sel->Select( i );
814     }
815
816     /* HACK we should really get new*/
817     treectrl->DeleteAllItems();
818     treectrl->AddRoot( wxU(_("root" )), -1, -1,
819                          new PlaylistItem( p_current_treeroot ) );
820
821     wxTreeItemId root = treectrl->GetRootItem();
822     UpdateNodeChildren( p_current_treeroot, root );
823
824     int i_count = CountItems( treectrl->GetRootItem() );
825
826     statusbar->SetStatusText( wxString::Format( wxU(_(
827                               "%i items in playlist")), i_count ), 0 );
828
829     UnlockPlaylist( p_intf->p_sys, p_playlist );
830 }
831
832 void Playlist::ShowPlaylist( bool show )
833 {
834     if( show ) Rebuild( true );
835     Show( show );
836 }
837
838 /* This function is called on a regular basis */
839 void Playlist::UpdatePlaylist()
840 {
841     i_update_counter++;
842
843     /* If the playlist isn't show there's no need to update it */
844     if( !IsShown() ) return;
845
846     if( this->b_need_update )
847     {
848         this->b_need_update = false;
849         Rebuild( true );
850     }
851
852     /* Updating the playing status every 0.5s is enough */
853     if( i_update_counter % 5 ) return;
854 }
855
856 /*****************************************************************************
857  * Private methods.
858  *****************************************************************************/
859 void Playlist::DeleteTreeItem( wxTreeItemId item )
860 {
861    PlaylistItem *p_wxitem;
862    playlist_item_t *p_item;
863    p_wxitem = (PlaylistItem *)treectrl->GetItemData( item );
864
865    LockPlaylist( p_intf->p_sys, p_playlist );
866    p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id, true );
867
868    if( !p_item )
869    {
870        UnlockPlaylist( p_intf->p_sys, p_playlist );
871        return;
872    }
873
874    if( p_item->i_children == -1 ) DeleteItem( p_item->p_input->i_id );
875    else DeleteNode( p_item );
876
877    RemoveItem( p_item->i_id );
878    UnlockPlaylist( p_intf->p_sys, p_playlist );
879 }
880
881 void Playlist::DeleteItem( int item_id )
882 {
883     playlist_DeleteFromInput( p_playlist, item_id, true );
884 }
885
886 void Playlist::DeleteNode( playlist_item_t *p_item )
887 {
888     playlist_NodeDelete( p_playlist, p_item, true , false );
889 }
890
891 void Playlist::OnMenuClose( wxCommandEvent& event )
892 {
893     wxCloseEvent cevent;
894     OnClose(cevent);
895 }
896
897 void Playlist::OnClose( wxCloseEvent& WXUNUSED(event) )
898 {
899     Hide();
900 }
901
902 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
903 {
904     struct {
905         char *psz_desc;
906         char *psz_filter;
907         char *psz_module;
908     } formats[] = {//{ _("M3U file"), "*.m3u", "export-m3u" },
909                    { _("XSPF playlist"), "*.xspf", "export-xspf"}
910     };
911
912     wxString filter = wxT("");
913
914     if( playlist_IsEmpty( p_playlist ) )
915     {
916         wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
917                       wxICON_WARNING | wxOK, this );
918         return;
919     }
920
921     for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
922     {
923         filter.Append( wxU(formats[i].psz_desc) );
924         filter.Append( wxT("|") );
925         filter.Append( wxU(formats[i].psz_filter) );
926         filter.Append( wxT("|") );
927     }
928     wxFileDialog dialog( this, wxU(_("Save playlist")),
929                          wxT(""), wxT(""), filter, wxSAVE );
930
931     if( dialog.ShowModal() == wxID_OK )
932     {
933         if( dialog.GetPath().mb_str(wxConvUTF8) )
934         {
935             /* what root should we export? */
936             if( p_playlist->p_root_category->i_children > 0 )
937             {
938                 playlist_Export( p_playlist, dialog.GetPath().mb_str(wxConvUTF8),
939                                  p_playlist->p_root_category->pp_children[0],
940                                  formats[dialog.GetFilterIndex()].psz_module );
941             }
942         }
943     }
944
945 }
946
947 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
948 {
949     wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
950         wxT("All playlists|" EXTENSIONS_PLAYLIST "|XSPF playlist|*.xspf|M3U files|*.m3u"), wxOPEN );
951
952     if( dialog.ShowModal() == wxID_OK )
953     {
954         playlist_Import( p_playlist, dialog.GetPath().mb_str(wxConvUTF8) );
955     }
956 }
957
958 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
959 {
960     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
961
962 }
963
964 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
965 {
966     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
967
968 }
969
970 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
971 {
972     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
973
974 }
975
976 /********************************************************************
977  * Sorting functions
978  ********************************************************************/
979 void Playlist::OnSort( wxCommandEvent& event )
980 {
981     PlaylistItem *p_wxitem;
982     p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
983
984     LockPlaylist( p_intf->p_sys, p_playlist );
985     switch( event.GetId() )
986     {
987         case SortTitle_Event:
988             playlist_RecursiveNodeSort( p_playlist,
989                             playlist_ItemGetById( p_playlist, p_wxitem->i_id,
990                                                   true ),
991                             SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
992             break;
993         case RSortTitle_Event:
994             playlist_RecursiveNodeSort( p_playlist,
995                             playlist_ItemGetById( p_playlist, p_wxitem->i_id,
996                                                   true ),
997                             SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
998     }
999     UnlockPlaylist( p_intf->p_sys, p_playlist );
1000
1001     Rebuild( true );
1002 }
1003
1004 /**********************************************************************
1005  * Search functions (user)
1006  **********************************************************************/
1007 /*void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
1008 {
1009    search_button->SetDefault();
1010 }*/
1011
1012 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
1013 {
1014     wxString search_string = search_text->GetValue();
1015     PlaylistItem *p_wxroot;
1016     p_wxroot = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
1017     playlist_item_t *p_root = playlist_ItemGetById( p_playlist, p_wxroot->i_id,
1018                                                     true );
1019
1020     assert( p_root );
1021     char *psz_name = wxFromLocale( search_string );
1022     playlist_LiveSearchUpdate( p_playlist, p_root, psz_name );
1023     Rebuild( true );
1024
1025     wxLocaleFree( psz_name );
1026 }
1027
1028 /**********************************************************************
1029  * Selection functions
1030  **********************************************************************/
1031 void Playlist::RecursiveDeleteSelection(  wxTreeItemId root )
1032 {
1033     wxTreeItemIdValue cookie;
1034     wxTreeItemId child = treectrl->GetFirstChild( root, cookie );
1035     wxTreeItemId nextchild;
1036     bool childIsSelected = FALSE;
1037     bool nextchildIsSelected = FALSE;
1038
1039     if( child.IsOk() ) childIsSelected = treectrl->IsSelected( child );
1040
1041     while( child.IsOk() )
1042     {
1043         nextchild = treectrl->GetNextChild( root, cookie );
1044         if( nextchild.IsOk() )
1045             nextchildIsSelected = treectrl->IsSelected( nextchild );
1046         if( childIsSelected )
1047             DeleteTreeItem( child );
1048         else if( treectrl->ItemHasChildren( child ) )
1049             RecursiveDeleteSelection( child );
1050         child = nextchild;
1051         childIsSelected = nextchildIsSelected;
1052     }
1053 }
1054
1055 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1056 {
1057     RecursiveDeleteSelection( treectrl->GetRootItem() );
1058 }
1059
1060 /**********************************************************************
1061  * Playlist mode functions
1062  **********************************************************************/
1063 void Playlist::OnRandom( wxCommandEvent& event )
1064 {
1065     vlc_value_t val;
1066     val.b_bool = event.IsChecked();
1067     var_Set( p_playlist, "random", val);
1068 }
1069
1070 void Playlist::OnLoop( wxCommandEvent& event )
1071 {
1072     vlc_value_t val;
1073     val.b_bool = event.IsChecked();
1074     var_Set( p_playlist, "loop", val);
1075 }
1076
1077 void Playlist::OnRepeat( wxCommandEvent& event )
1078 {
1079     vlc_value_t val;
1080     val.b_bool = event.IsChecked();
1081     var_Set( p_playlist, "repeat", val);
1082 }
1083
1084 /********************************************************************
1085  * Event
1086  ********************************************************************/
1087 void Playlist::OnActivateItem( wxTreeEvent& event )
1088 {
1089     playlist_item_t *p_item, *p_parent;
1090
1091     PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1092                                                             event.GetItem() );
1093
1094     LockPlaylist( p_intf->p_sys, p_playlist );
1095
1096     if( !( p_wxitem ) )
1097     {
1098         UnlockPlaylist( p_intf->p_sys, p_playlist );
1099         return;
1100     }
1101     p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id, true );
1102
1103     p_parent = p_item;
1104     while( p_parent )
1105     {
1106         if( p_parent == p_current_treeroot )
1107             break;
1108         p_parent = p_parent->p_parent;
1109     }
1110
1111     if( p_parent )
1112     {
1113         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true, p_parent, p_item );
1114     }
1115     UnlockPlaylist( p_intf->p_sys, p_playlist );
1116 }
1117
1118 void Playlist::OnKeyDown( wxTreeEvent& event )
1119 {
1120     long keycode = event.GetKeyCode();
1121     /* Delete selected items */
1122     if( keycode == WXK_BACK || keycode == WXK_DELETE || keycode == WXK_NUMPAD_DELETE )
1123     {
1124         /* We send a dummy event */
1125         OnDeleteSelection( event );
1126     }
1127     /* Work around wxWin32 bug */
1128     else if( keycode == WXK_RETURN )
1129     {
1130         wxArrayTreeItemIds items;
1131         if( treectrl->GetSelections( items ) > 0 )
1132         {
1133             wxTreeEvent event;
1134             event.SetItem( items.Item( 0 ) );
1135             OnActivateItem( event );
1136         }
1137     }
1138     else
1139     {
1140         event.Skip();
1141     }
1142 }
1143
1144 void Playlist::OnDragItemBegin( wxTreeEvent& event )
1145 {
1146     event.Allow();
1147     draged_tree_item = event.GetItem();
1148 }
1149
1150 void Playlist::OnDragItemEnd( wxTreeEvent& event )
1151 {
1152     wxTreeItemId dest_tree_item = event.GetItem();
1153
1154     if( !dest_tree_item.IsOk() ) return;
1155
1156     /* check that we're not trying to move a node into one of its children */
1157     wxTreeItemId parent = dest_tree_item;
1158     while( parent != treectrl->GetRootItem() )
1159     {
1160         if( draged_tree_item == parent ) return;
1161         parent = treectrl->GetItemParent( parent );
1162     }
1163
1164     LockPlaylist( p_intf->p_sys, p_playlist );
1165
1166     PlaylistItem *p_wxdrageditem =
1167         (PlaylistItem *)treectrl->GetItemData( draged_tree_item );
1168     PlaylistItem *p_wxdestitem =
1169         (PlaylistItem *)treectrl->GetItemData( dest_tree_item );
1170     if( !p_wxdrageditem || !p_wxdestitem )
1171     {
1172         UnlockPlaylist( p_intf->p_sys, p_playlist );
1173         return;
1174     }
1175
1176     playlist_item_t *p_drageditem =
1177         playlist_ItemGetById(p_playlist, p_wxdrageditem->i_id, true );
1178     playlist_item_t *p_destitem =
1179         playlist_ItemGetById(p_playlist, p_wxdestitem->i_id, true );
1180     if( !p_drageditem || !p_destitem )
1181     {
1182         UnlockPlaylist( p_intf->p_sys, p_playlist );
1183         return;
1184     }
1185
1186     if( p_destitem->i_children == -1 )
1187     /* this is a leaf */
1188     {
1189         parent = treectrl->GetItemParent( dest_tree_item );
1190         PlaylistItem *p_parent =
1191             (PlaylistItem *)treectrl->GetItemData( parent );
1192         if( !p_parent )
1193         {
1194             UnlockPlaylist( p_intf->p_sys, p_playlist );
1195             return;
1196         }
1197         playlist_item_t *p_destitem2 =
1198             playlist_ItemGetById( p_playlist, p_parent->i_id, true );
1199         if( !p_destitem2 )
1200         {
1201             UnlockPlaylist( p_intf->p_sys, p_playlist );
1202             return;
1203         }
1204         int i;
1205         for( i = 0; i < p_destitem2->i_children; i++ )
1206         {
1207             if( p_destitem2->pp_children[i] == p_destitem ) break;
1208         }
1209         playlist_TreeMove( p_playlist, p_drageditem, p_destitem2, i );
1210     }
1211     else
1212     /* this is a node */
1213     {
1214         playlist_TreeMove( p_playlist, p_drageditem, p_destitem, 0 );
1215     }
1216
1217     UnlockPlaylist( p_intf->p_sys, p_playlist );
1218
1219     /* FIXME: having this Rebuild() is dirty */
1220     Rebuild( true );
1221 }
1222
1223 #if wxUSE_DRAG_AND_DROP
1224 PlaylistFileDropTarget::PlaylistFileDropTarget( Playlist *p ):p( p ){}
1225
1226 /********************************************************************
1227  * File Drag And Drop handling
1228  ********************************************************************/
1229 bool PlaylistFileDropTarget::OnDropFiles( wxCoord x, wxCoord y,
1230                                const wxArrayString& filenames )
1231 {
1232     int i_pos = 0;
1233     playlist_item_t *p_dest;
1234
1235     LockPlaylist( p->p_intf->p_sys, p->p_playlist );
1236
1237     /* find the destination node and position in that node */
1238     const wxPoint pt( x, y );
1239     wxTreeItemId item = p->treectrl->HitTest( pt );
1240
1241     if( !item.IsOk() )
1242     {
1243         /* We were droped below the last item so we append to the
1244          * general node */
1245         msg_Err( p->p_playlist, "USE OF P_GENERAL" );
1246         p_dest = p->p_playlist->p_local_category;
1247         i_pos = PLAYLIST_END;
1248     }
1249     else
1250     {
1251         PlaylistItem *p_plitem =
1252             (PlaylistItem *)p->treectrl->GetItemData( item );
1253         p_dest = playlist_ItemGetById( p->p_playlist, p_plitem->i_id, true );
1254
1255         if( p_dest->i_children == -1 )
1256         {
1257             /* This is a leaf. Append right after it
1258              * We thus need to find the parrent node and the position of the
1259              * leaf in its children list */
1260             wxTreeItemId parent = p->treectrl->GetItemParent( item );
1261             PlaylistItem *p_parent =
1262                 (PlaylistItem *)p->treectrl->GetItemData( parent );
1263             if( !p_parent )
1264             {
1265                 UnlockPlaylist( p->p_intf->p_sys, p->p_playlist );
1266                 return FALSE;
1267             }
1268             playlist_item_t *p_node =
1269                 playlist_ItemGetById( p->p_playlist, p_parent->i_id, true );
1270             if( !p_node )
1271             {
1272                 UnlockPlaylist( p->p_intf->p_sys, p->p_playlist );
1273                 return FALSE;
1274             }
1275             for( i_pos = 0; i_pos < p_node->i_children; i_pos++ )
1276             {
1277                 if( p_node->pp_children[i_pos] == p_dest ) break;
1278             }
1279             p_dest = p_node;
1280         }
1281     }
1282
1283     UnlockPlaylist( p->p_intf->p_sys, p->p_playlist );
1284
1285     /* Put the items in the playlist node */
1286     for( size_t i = 0; i < filenames.GetCount(); i++ )
1287     {
1288         char *psz_utf8 = wxDnDFromLocale( filenames[i] );
1289         input_item_t *p_input = input_ItemNew( p->p_playlist,
1290                                               psz_utf8, psz_utf8 );
1291         int i_ret = ( playlist_NodeAddInput( p->p_playlist, p_input, p_dest,
1292                 PLAYLIST_PREPARSE, i_pos, false ) != VLC_SUCCESS );
1293         wxDnDLocaleFree( psz_utf8 );
1294         if( i_ret != VLC_SUCCESS )
1295             return FALSE;
1296     }
1297
1298     /* FIXME: having this Rebuild() is dirty */
1299     p->Rebuild( true );
1300
1301     return TRUE;
1302 }
1303 #endif
1304
1305 /**********************************************************************
1306  * Menu
1307  **********************************************************************/
1308
1309 void Playlist::OnMenuOpen( wxMenuEvent& event)
1310 {
1311 #if defined( __WXMSW__ )
1312 #   define GetEventObject GetMenu
1313 #endif
1314
1315     if( event.GetEventObject() == p_view_menu )
1316     {
1317         p_view_menu = ViewMenu();
1318     }
1319 #if defined( __WXMSW__ )
1320 #   undef GetEventObject
1321 #endif
1322 }
1323
1324 void Playlist::OnMenuEvent( wxCommandEvent& event )
1325 {
1326     if( event.GetId() < FirstView_Event )
1327     {
1328         event.Skip();
1329         return;
1330     }
1331     else if( event.GetId() < LastView_Event )
1332     {
1333         if( event.GetId() == CategoryView_Event )
1334         {
1335             p_current_viewroot = p_playlist->p_root_category;
1336             if( p_current_treeroot == p_playlist->p_local_category ||
1337                 p_current_treeroot == p_playlist->p_local_onelevel )
1338             {
1339                 p_current_treeroot = p_playlist->p_local_category;
1340             }
1341             else if( p_current_treeroot == p_playlist->p_ml_category ||
1342                      p_current_treeroot == p_playlist->p_ml_onelevel )
1343             {
1344                 p_current_treeroot = p_playlist->p_ml_category;
1345             }
1346         }
1347         else if( event.GetId() == OneLevelView_Event )
1348         {
1349             p_current_viewroot = p_playlist->p_root_onelevel;
1350             if( p_current_treeroot == p_playlist->p_local_category ||
1351                 p_current_treeroot == p_playlist->p_local_onelevel )
1352             {
1353                 p_current_treeroot = p_playlist->p_local_onelevel;
1354             }
1355             else if( p_current_treeroot == p_playlist->p_ml_category ||
1356                      p_current_treeroot == p_playlist->p_ml_onelevel )
1357             {
1358                 p_current_treeroot = p_playlist->p_ml_onelevel;
1359             }
1360         }
1361         wxCommandEvent event;
1362         OnSearch( event );
1363         return;
1364     }
1365     else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1366     {
1367         if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1368                                 pp_sds[event.GetId() - FirstSD_Event] ) )
1369         {
1370             playlist_ServicesDiscoveryAdd( p_playlist,
1371                             pp_sds[event.GetId() - FirstSD_Event] );
1372         }
1373         else
1374         {
1375             //wxMutexGuiLeave();
1376             playlist_ServicesDiscoveryRemove( p_playlist,
1377                             pp_sds[event.GetId() - FirstSD_Event] );
1378             //wxMutexGuiEnter();
1379         }
1380     }
1381 }
1382
1383 wxMenu * Playlist::ViewMenu()
1384 {
1385     if( !p_view_menu )
1386     {
1387         p_view_menu = new wxMenu;
1388     }
1389     else
1390     {
1391         wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1392         for( ; node; )
1393         {
1394             wxMenuItem *item = node->GetData();
1395             node = node->GetNext();
1396             p_view_menu->Delete( item );
1397         }
1398     }
1399
1400     p_view_menu->Append( CategoryView_Event, wxU(_("Normal") ) );
1401     p_view_menu->Append( OneLevelView_Event, wxU(_("One level") ) );
1402     return p_view_menu;
1403 }
1404
1405 wxMenu *Playlist::SDMenu()
1406 {
1407     p_sd_menu = new wxMenu;
1408
1409     char **ppsz_longnames;
1410     char **ppsz_names = services_discovery_GetServicesNames( p_playlist,
1411                                                              &ppsz_longnames );
1412     if( !ppsz_names )
1413         return p_sd_menu;
1414
1415     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
1416
1417     int i_number = 0;
1418
1419     for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
1420     {
1421         p_sd_menu->AppendCheckItem( FirstSD_Event + i_number++ ,
1422                                     wxU( *ppsz_longname ) );
1423
1424         if( playlist_IsServicesDiscoveryLoaded( p_playlist, *ppsz_name ) )
1425             p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1426
1427         free( *ppsz_longname );
1428     }
1429     pp_sds = ppsz_names;
1430     free( ppsz_longnames );
1431     return p_sd_menu;
1432 }
1433
1434
1435 /*****************************************************************************
1436  * Popup management functions
1437  *****************************************************************************/
1438 void Playlist::OnPopup( wxContextMenuEvent& event )
1439 {
1440     wxPoint pt = event.GetPosition();
1441     playlist_item_t *p_item;
1442
1443     i_wx_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1444     if( i_wx_popup_item.IsOk() )
1445     {
1446         PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1447                                                             i_wx_popup_item );
1448         PlaylistItem *p_wxparent= (PlaylistItem *)treectrl->GetItemData(
1449                                   treectrl->GetItemParent( i_wx_popup_item ) );
1450         i_popup_item = p_wxitem->i_id;
1451         i_popup_parent = p_wxparent->i_id;
1452         treectrl->SelectItem( i_wx_popup_item );
1453
1454         LockPlaylist( p_intf->p_sys, p_playlist );
1455         p_item = playlist_ItemGetById( p_playlist, i_popup_item, true );
1456
1457         if( !p_item )
1458         {
1459             UnlockPlaylist( p_intf->p_sys, p_playlist );
1460             return;
1461         }
1462         if( p_item->i_children == -1 )
1463         {
1464             UnlockPlaylist( p_intf->p_sys, p_playlist );
1465             Playlist::PopupMenu( item_popup,
1466                                  ScreenToClient( wxGetMousePosition() ) );
1467         }
1468         else
1469         {
1470             UnlockPlaylist( p_intf->p_sys, p_playlist );
1471             Playlist::PopupMenu( node_popup,
1472                                  ScreenToClient( wxGetMousePosition() ) );
1473         }
1474     }
1475 }
1476
1477 void Playlist::OnPopupPlay( wxCommandEvent& event )
1478 {
1479     playlist_item_t *p_popup_item, *p_popup_parent;
1480     LockPlaylist( p_intf->p_sys, p_playlist );
1481     p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item, true );
1482
1483     p_popup_parent = p_popup_item;
1484     while( p_popup_parent )
1485     {
1486         if( p_popup_parent == p_current_treeroot )
1487             break;
1488         p_popup_parent = p_popup_parent->p_parent;
1489     }
1490
1491     if( p_popup_parent )
1492     {
1493         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true, p_popup_parent,
1494                           p_popup_item );
1495     }
1496     UnlockPlaylist( p_intf->p_sys, p_playlist );
1497 }
1498
1499 void Playlist::OnPopupPreparse( wxCommandEvent& event )
1500 {
1501     Preparse();
1502 }
1503
1504 void Playlist::Preparse()
1505 {
1506     playlist_item_t *p_popup_item;
1507     LockPlaylist( p_intf->p_sys, p_playlist );
1508     p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item, true );
1509
1510     if( p_popup_item != NULL )
1511     {
1512         if( p_popup_item->i_children == -1 )
1513         {
1514             playlist_PreparseEnqueue( p_playlist, p_popup_item->p_input );
1515         }
1516         else
1517         {
1518             int i = 0;
1519             playlist_item_t *p_parent = p_popup_item;
1520             for( i = 0; i< p_parent->i_children ; i++ )
1521             {
1522                 wxMenuEvent dummy;
1523                 i_wx_popup_item = FindItem( treectrl->GetRootItem(),
1524                                          p_parent->pp_children[i]->i_id );
1525                 i_popup_item = p_parent->pp_children[i]->i_id;
1526                 Preparse();
1527             }
1528         }
1529     }
1530     UnlockPlaylist( p_intf->p_sys, p_playlist );
1531 }
1532
1533 void Playlist::OnPopupDel( wxCommandEvent& event )
1534 {
1535     DeleteTreeItem( i_wx_popup_item );
1536 }
1537
1538 void Playlist::OnPopupSort( wxCommandEvent& event )
1539 {
1540     PlaylistItem *p_wxitem;
1541     playlist_item_t *p_item;
1542
1543     p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_wx_popup_item );
1544     LockPlaylist( p_intf->p_sys, p_playlist );
1545
1546     p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id, true );
1547     if( p_item->i_children >= 0 )
1548     {
1549         playlist_RecursiveNodeSort( p_playlist, p_item,
1550                                     SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1551
1552         treectrl->DeleteChildren( i_wx_popup_item );
1553         i_saved_id = -1; i_saved_input_id = -1;
1554         UpdateNodeChildren( p_item, i_wx_popup_item );
1555
1556     }
1557     UnlockPlaylist( p_intf->p_sys, p_playlist );
1558 }
1559
1560 void Playlist::OnPopupInfo( wxCommandEvent& event )
1561 {
1562     LockPlaylist( p_intf->p_sys, p_playlist );
1563     playlist_item_t *p_popup_item = playlist_ItemGetById( p_playlist,
1564                                                           i_popup_item,
1565                                                           true );
1566     if( p_popup_item )
1567     {
1568         iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1569         if( iteminfo_dialog->ShowModal() == wxID_OK )
1570         {
1571             UpdateItem( i_wx_popup_item );
1572         }
1573         delete iteminfo_dialog;
1574     }
1575     UnlockPlaylist( p_intf->p_sys, p_playlist );
1576 }
1577
1578 void Playlist::OnPopupAddNode( wxCommandEvent& event )
1579 {
1580     wxTextEntryDialog text( NULL, wxU(_( "Please enter node name" )),
1581         wxU(_( "Add node" )), wxU(_( "New node" )) );
1582     if( text.ShowModal() != wxID_OK ) return;
1583
1584     char *psz_name = wxFromLocale( text.GetValue() );
1585
1586     LockPlaylist( p_intf->p_sys, p_playlist );
1587
1588     PlaylistItem *p_wxitem;
1589     playlist_item_t *p_item;
1590
1591     p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_wx_popup_item );
1592
1593     p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id, true );
1594
1595     playlist_NodeCreate( p_playlist, psz_name, p_item, 0, NULL );
1596
1597     UnlockPlaylist( p_intf->p_sys, p_playlist );
1598     Rebuild( true );
1599
1600     wxLocaleFree( psz_name );
1601 }
1602
1603 void Playlist::OnSourceSelected( wxListEvent &event )
1604 {
1605    int i_id = event.GetData();
1606
1607    if( !p_current_treeroot || i_id != p_current_treeroot->i_id )
1608    {
1609        playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id,
1610                                                        true );
1611        if( p_item ) p_current_treeroot = p_item;
1612        Rebuild( true );
1613    }
1614 }
1615
1616 /*****************************************************************************
1617  * Custom events management
1618  *****************************************************************************/
1619 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1620 {
1621     switch( event.GetId() )
1622     {
1623         case UpdateItem_Event:
1624             UpdateItem( event.GetInt() );
1625             break;
1626         case AppendItem_Event:
1627             AppendItem( event );
1628             break;
1629         case RemoveItem_Event:
1630             RemoveItem( event.GetInt() );
1631             break;
1632     }
1633 }
1634
1635 /*****************************************************************************
1636  * PlaylistChanged: callback triggered by the intf-change playlist variable
1637  *  We don't rebuild the playlist directly here because we don't want the
1638  *  caller to block for a too long time.
1639  *****************************************************************************/
1640 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1641                             vlc_value_t oval, vlc_value_t nval, void *param )
1642 {
1643     Playlist *p_playlist_dialog = (Playlist *)param;
1644     p_playlist_dialog->b_need_update = true;
1645     return VLC_SUCCESS;
1646 }
1647
1648 /*****************************************************************************
1649  * Next: callback triggered by the playlist-current playlist variable
1650  *****************************************************************************/
1651 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1652                          vlc_value_t oval, vlc_value_t nval, void *param )
1653 {
1654     Playlist *p_playlist_dialog = (Playlist *)param;
1655
1656     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1657     event.SetInt( oval.i_int );
1658     p_playlist_dialog->AddPendingEvent( event );
1659     event.SetInt( nval.i_int );
1660     p_playlist_dialog->AddPendingEvent( event );
1661
1662     return 0;
1663 }
1664
1665 /*****************************************************************************
1666  * ItemChanged: callback triggered by the item-change playlist variable
1667  *****************************************************************************/
1668 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1669                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1670 {
1671     Playlist *p_playlist_dialog = (Playlist *)param;
1672
1673     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1674     event.SetInt( new_val.i_int );
1675     p_playlist_dialog->AddPendingEvent( event );
1676
1677     return 0;
1678 }
1679 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1680                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1681 {
1682     Playlist *p_playlist_dialog = (Playlist *)param;
1683
1684     wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1685     event.SetInt( new_val.i_int );
1686     p_playlist_dialog->AddPendingEvent( event );
1687
1688     return 0;
1689 }
1690
1691 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1692                          vlc_value_t oval, vlc_value_t nval, void *param )
1693 {
1694     Playlist *p_playlist_dialog = (Playlist *)param;
1695
1696     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1697     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1698
1699     if( ++p_playlist_dialog->i_items_to_append >= 50 )
1700     {
1701         /* Too many items waiting to be added, it will be quicker to rebuild
1702          * the whole playlist */
1703         p_playlist_dialog->b_need_update = true;
1704         return VLC_SUCCESS;
1705     }
1706
1707     wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1708     event.SetClientData( (void *)p_add );
1709     p_playlist_dialog->AddPendingEvent( event );
1710
1711     return VLC_SUCCESS;
1712 }
1713 }