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