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