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