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