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