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