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