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