1 /*****************************************************************************
2 * playlist.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004 VideoLAN
7 * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8 * Clément Stenac <zorglub@videolan.org>
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.
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.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include "wxwindows.h"
33 #include "bitmaps/shuffle.xpm"
34 #include "bitmaps/repeat.xpm"
35 #include "bitmaps/loop.xpm"
37 #include "bitmaps/type_unknown.xpm"
38 #include "bitmaps/type_afile.xpm"
39 #include "bitmaps/type_vfile.xpm"
40 #include "bitmaps/type_net.xpm"
41 #include "bitmaps/type_card.xpm"
42 #include "bitmaps/type_disc.xpm"
43 #include "bitmaps/type_cdda.xpm"
44 #include "bitmaps/type_directory.xpm"
45 #include "bitmaps/type_playlist.xpm"
46 #include "bitmaps/type_node.xpm"
48 #include <wx/dynarray.h>
49 #include <wx/imaglist.h>
51 #define HELP_SHUFFLE N_( "Shuffle" )
52 #define HELP_LOOP N_( "Loop" )
53 #define HELP_REPEAT N_( "Repeat" )
55 /* Callback prototype */
56 static int PlaylistChanged( vlc_object_t *, const char *,
57 vlc_value_t, vlc_value_t, void * );
58 static int PlaylistNext( vlc_object_t *, const char *,
59 vlc_value_t, vlc_value_t, void * );
60 static int ItemChanged( vlc_object_t *, const char *,
61 vlc_value_t, vlc_value_t, void * );
62 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
63 vlc_value_t oval, vlc_value_t nval, void *param );
64 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
65 vlc_value_t oval, vlc_value_t nval, void *param );
67 /*****************************************************************************
69 *****************************************************************************/
71 /* IDs for the controls and the menu commands */
86 InvertSelection_Event,
87 DeleteSelection_Event,
106 Browse_Event, /* For export playlist */
113 MenuDummy_Event = wxID_HIGHEST + 999,
115 FirstView_Event = wxID_HIGHEST + 1000,
116 LastView_Event = wxID_HIGHEST + 1100,
118 FirstSD_Event = wxID_HIGHEST + 2000,
119 LastSD_Event = wxID_HIGHEST + 2100,
122 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
124 BEGIN_EVENT_TABLE(Playlist, wxFrame)
125 EVT_SIZE(Playlist::OnSize)
128 EVT_MENU(AddFile_Event, Playlist::OnAddFile)
129 EVT_MENU(AddDir_Event, Playlist::OnAddDir)
130 EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
131 EVT_MENU(Close_Event, Playlist::OnClose)
132 EVT_MENU(Open_Event, Playlist::OnOpen)
133 EVT_MENU(Save_Event, Playlist::OnSave)
135 EVT_MENU(SortTitle_Event, Playlist::OnSort)
136 EVT_MENU(RSortTitle_Event, Playlist::OnSort)
138 EVT_MENU(Randomize_Event, Playlist::OnSort)
140 EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
141 EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
142 EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
144 EVT_MENU_OPEN( Playlist::OnMenuOpen )
145 EVT_MENU( -1, Playlist::OnMenuEvent )
147 EVT_TOOL(Random_Event, Playlist::OnRandom)
148 EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
149 EVT_TOOL(Loop_Event, Playlist::OnLoop)
152 EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
153 EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
154 EVT_MENU( PopupPreparse_Event, Playlist::OnPopupPreparse)
155 EVT_MENU( PopupSort_Event, Playlist::OnPopupSort)
156 EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
157 EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
159 /* Tree control events */
160 EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
162 EVT_CONTEXT_MENU( Playlist::OnPopup )
165 EVT_BUTTON( Search_Event, Playlist::OnSearch)
166 EVT_BUTTON( Save_Event, Playlist::OnSave)
168 EVT_TEXT(SearchText_Event, Playlist::OnSearchTextChange)
171 EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
173 /* Special events : we don't want to destroy the window when the user
175 EVT_CLOSE(Playlist::OnClose)
178 /*****************************************************************************
180 ****************************************************************************/
181 class PlaylistItem : public wxTreeItemData
184 PlaylistItem( playlist_item_t *_p_item ) : wxTreeItemData()
187 i_id = p_item->input.i_id;
190 playlist_item_t *p_item;
192 friend class Playlist;
195 /*****************************************************************************
197 *****************************************************************************/
198 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
199 wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
200 wxSize(345,400), wxDEFAULT_FRAME_STYLE )
204 /* Initializations */
206 i_update_counter = 0;
207 i_sort_mode = MODE_NONE;
208 b_need_update = VLC_FALSE;
209 SetIcon( *p_intf->p_sys->p_icon );
212 p_sd_menu = SDMenu();
214 i_current_view = VIEW_SIMPLE;
215 b_changed_view = VLC_FALSE;
219 i_duration_sorted = 0;
221 var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
222 var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
223 var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
225 /* Create our "Manage" menu */
226 wxMenu *manage_menu = new wxMenu;
227 manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
228 manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
229 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
230 manage_menu->AppendSeparator();
231 manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
233 manage_menu->AppendSeparator();
234 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
235 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
236 manage_menu->AppendSeparator();
237 manage_menu->Append( Close_Event, wxU(_("&Close")) );
239 /* Create our "Sort" menu */
240 wxMenu *sort_menu = new wxMenu;
241 sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
242 sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
243 sort_menu->AppendSeparator();
244 sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
246 /* Create our "Selection" menu */
247 wxMenu *selection_menu = new wxMenu;
248 selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
249 selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
250 selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
252 /* Create our "View" menu */
255 /* Append the freshly created menus to the menu bar */
256 wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
257 menubar->Append( manage_menu, wxU(_("&Manage")) );
258 menubar->Append( sort_menu, wxU(_("S&ort")) );
259 menubar->Append( selection_menu, wxU(_("&Selection")) );
260 menubar->Append( p_view_menu, wxU(_("&View items") ) );
262 /* Attach the menu bar to the frame */
263 SetMenuBar( menubar );
265 /* Create the popup menu */
266 node_popup = new wxMenu;
267 node_popup->Append( PopupPlay_Event, wxU(_("Play")) );
268 node_popup->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
269 node_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
270 node_popup->Append( PopupSort_Event, wxU(_("Sort this branch")) );
271 node_popup->Append( PopupDel_Event, wxU(_("Delete")) );
272 node_popup->Append( PopupInfo_Event, wxU(_("Info")) );
274 item_popup = new wxMenu;
275 item_popup->Append( PopupPlay_Event, wxU(_("Play")) );
276 item_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
277 item_popup->Append( PopupDel_Event, wxU(_("Delete")) );
278 item_popup->Append( PopupInfo_Event, wxU(_("Info")) );
280 /* Create a panel to put everything in */
281 wxPanel *playlist_panel = new wxPanel( this, -1 );
282 playlist_panel->SetAutoLayout( TRUE );
284 /* Create the toolbar */
286 CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT | wxTB_DOCKABLE );
288 /* Create the random tool */
289 toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
290 wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
291 wxU(_(HELP_SHUFFLE) ) );
292 var_Get( p_intf, "random", &val );
293 toolbar->ToggleTool( Random_Event, val.b_bool );
295 /* Create the Loop tool */
296 toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
297 wxBitmap( loop_xpm), wxITEM_CHECK,
298 wxU(_(HELP_LOOP ) ) );
299 var_Get( p_intf, "loop", &val );
300 toolbar->ToggleTool( Loop_Event, val.b_bool );
302 /* Create the Repeat one checkbox */
303 toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
304 wxBitmap( repeat_xpm), wxITEM_CHECK,
305 wxU(_(HELP_REPEAT ) ) );
306 var_Get( p_intf, "repeat", &val );
307 toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
309 /* Create the Search Textbox */
310 search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
311 wxDefaultPosition, wxSize(100, -1),
314 /* Create the search button */
315 search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
317 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
318 wxSize(16, 16), wxBORDER_NONE ) );
319 toolbar->AddControl( search_text );
320 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
321 wxSize(5, 5), wxBORDER_NONE ) );
322 toolbar->AddControl( search_button );
323 search_button->SetDefault();
326 /* Create the tree */
327 treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
328 wxDefaultPosition, wxDefaultSize,
329 wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
331 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
332 wxTR_MULTIPLE | wxTR_EXTENDED );
334 /* Create image list */
335 wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
337 /* FIXME: absolutely needs to be in the right order FIXME */
338 p_images->Add( wxIcon( type_unknown_xpm ) );
339 p_images->Add( wxIcon( type_afile_xpm ) );
340 p_images->Add( wxIcon( type_vfile_xpm ) );
341 p_images->Add( wxIcon( type_directory_xpm ) );
342 p_images->Add( wxIcon( type_disc_xpm ) );
343 p_images->Add( wxIcon( type_cdda_xpm ) );
344 p_images->Add( wxIcon( type_card_xpm ) );
345 p_images->Add( wxIcon( type_net_xpm ) );
346 p_images->Add( wxIcon( type_playlist_xpm ) );
347 p_images->Add( wxIcon( type_node_xpm ) );
348 treectrl->AssignImageList( p_images );
350 treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
352 /* Reduce font size */
353 wxFont font= treectrl->GetFont();
354 font.SetPointSize(8);
355 treectrl->SetFont( font );
357 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
358 panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
359 panel_sizer->Layout();
361 playlist_panel->SetSizerAndFit( panel_sizer );
363 int pi_widths[1] = { -1 };
364 statusbar = CreateStatusBar( 1 );
365 statusbar->SetStatusWidths( 1, pi_widths );
367 #if wxUSE_DRAG_AND_DROP
368 /* Associate drop targets with the playlist */
369 SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
372 playlist_t *p_playlist =
373 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
375 if( p_playlist == NULL )
380 /* We want to be noticed of playlist changes */
382 /* Some global changes happened -> Rebuild all */
383 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
385 /* We went to the next item */
386 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
388 /* One item has been updated */
389 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
391 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
392 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
394 vlc_object_release( p_playlist );
397 /* Update the playlist */
401 Playlist::~Playlist()
403 playlist_t *p_playlist =
404 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
406 if( p_playlist == NULL )
411 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
412 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
413 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
414 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
415 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
416 vlc_object_release( p_playlist );
419 /**********************************************************************
421 **********************************************************************/
424 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
429 for( int i = 0; i< p_node->i_children ; i++ )
433 child = treectrl->GetFirstChild( node, cookie);
437 child = treectrl->GetNextChild( node, cookie );
442 /* Not enough children */
443 CreateNode( p_playlist, p_node->pp_children[i], node );
444 /* Keep the tree pointer up to date */
445 child = treectrl->GetNextChild( node, cookie );
451 treectrl->SetItemImage( node, p_node->input.i_type );
455 /* Creates the node p_node as last child of parent */
456 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
457 wxTreeItemId parent )
460 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
461 -1,-1, new PlaylistItem( p_node ) );
462 treectrl->SetItemImage( node, p_node->input.i_type );
464 UpdateNodeChildren( p_playlist, p_node, node );
467 /* Update all children (recursively) of this node */
468 void Playlist::UpdateNodeChildren( playlist_t *p_playlist,
469 playlist_item_t *p_node,
473 for( int i = 0; i< p_node->i_children ; i++ )
475 /* Append the item */
476 if( p_node->pp_children[i]->i_children == -1 )
479 treectrl->AppendItem( node,
480 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
481 new PlaylistItem( p_node->pp_children[i]) );
483 UpdateTreeItem( p_playlist, item );
485 treectrl->SetItemImage( item,
486 p_node->pp_children[i]->input.i_type );
490 CreateNode( p_playlist, p_node->pp_children[i],
496 /* Set current item */
497 void Playlist::SetCurrentItem( wxTreeItemId item )
501 treectrl->SetItemBold( item, true );
502 treectrl->EnsureVisible( item );
506 /* Update an item in the tree */
507 void Playlist::UpdateTreeItem( playlist_t *p_playlist, wxTreeItemId item )
509 playlist_item_t *p_item =
510 ((PlaylistItem *)treectrl->GetItemData( item ))->p_item;
518 char *psz_author = playlist_ItemGetInfo( p_item, _("Meta-information"),
520 char psz_duration[MSTRTIME_MAX_SIZE];
521 mtime_t dur = p_item->input.i_duration;
524 secstotimestr( psz_duration, dur/1000000 );
526 memcpy( psz_duration, "-:--:--", sizeof("-:--:--") );
528 if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
530 msg.Printf( wxString( wxU( p_item->input.psz_name ) ) + wxU( " ( ") +
531 wxString(wxU(psz_duration ) ) + wxU( ")") );
535 msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
536 wxString(wxU(p_item->input.psz_name)) + wxU( " ( ") +
537 wxString(wxU(psz_duration ) ) + wxU( ")") );
539 treectrl->SetItemText( item , msg );
540 treectrl->SetItemImage( item, p_item->input.i_type );
542 if( p_playlist->status.p_item == p_item )
544 SetCurrentItem( item );
548 treectrl->SetItemBold( item, false );
552 /* Process a AppendItem request */
553 void Playlist::AppendItem( wxCommandEvent& event )
555 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
557 playlist_t *p_playlist =
558 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
560 wxTreeItemId item,node;
561 if( p_playlist == NULL )
567 if( p_add->i_view != i_current_view )
572 node = FindItem( treectrl->GetRootItem(), p_add->p_node );
578 item = treectrl->AppendItem( node,
579 wxL2U( p_add->p_item->input.psz_name ), -1,-1,
580 new PlaylistItem( p_add->p_item ) );
581 treectrl->SetItemImage( item, p_add->p_item->input.i_type );
583 if( item.IsOk() && p_add->p_item->i_children == -1 )
585 UpdateTreeItem( p_playlist, item );
589 int i_count = CountItems( treectrl->GetRootItem());
590 if( i_count != p_playlist->i_size )
592 statusbar->SetStatusText( wxString::Format( wxU(_(
593 "%i items in playlist (%i not shown)")),
595 p_playlist->i_size - i_count ) );
596 if( !b_changed_view )
598 i_current_view = VIEW_CATEGORY;
599 b_changed_view = VLC_TRUE;
600 b_need_update = VLC_TRUE;
605 statusbar->SetStatusText( wxString::Format( wxU(_(
606 "%i items in playlist")),
607 p_playlist->i_size ), 0 );
610 vlc_object_release( p_playlist );
614 /* Process a updateitem request */
615 void Playlist::UpdateItem( int i )
617 if( i < 0 ) return; /* Sanity check */
618 playlist_item_t *p_item;
620 playlist_t *p_playlist =
621 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
624 if( p_playlist == NULL )
629 p_item = playlist_LockItemGetById( p_playlist, i );
631 wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
635 UpdateTreeItem( p_playlist, item );
638 vlc_object_release(p_playlist);
641 void Playlist::RemoveItem( int i )
643 if( i <= 0 ) return; /* Sanity check */
645 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
649 treectrl->Delete( item );
654 /**********************************************************************
655 * Search functions (internal
656 **********************************************************************/
658 /* Find a wxItem from a playlist_item */
659 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
662 PlaylistItem *p_wxcurrent;
664 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
667 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
675 if( p_wxcurrent->p_item == p_item )
682 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
683 if( p_wxcurrent->p_item == p_item )
687 if( treectrl->ItemHasChildren( item ) )
689 wxTreeItemId search = FindItem( item, p_item );
695 item = treectrl->GetNextChild( root, cookie );
701 /* Find a wxItem from a playlist id */
702 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
705 PlaylistItem *p_wxcurrent;
707 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
710 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
724 if( p_wxcurrent->i_id == i_id )
731 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
732 if( p_wxcurrent->i_id == i_id )
736 if( treectrl->ItemHasChildren( item ) )
738 wxTreeItemId search = FindItem( item, i_id );
744 item = treectrl->GetNextChild( root, cookie );
751 int Playlist::CountItems( wxTreeItemId root )
755 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
759 if( treectrl->ItemHasChildren( item ) )
761 count += CountItems( item );
763 else if( ( (PlaylistItem *)treectrl->GetItemData( item ) )->
764 p_item->i_children == -1 )
766 item = treectrl->GetNextChild( root, cookie );
771 /* Find a wxItem from a name (from current) */
772 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
776 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
781 if( treectrl->GetItemText( item).Lower().Contains(
782 search_string.Lower() ) )
784 if( !current.IsOk() || *pb_current_found == VLC_TRUE )
788 else if( current.IsOk() && item == current )
790 *pb_current_found = VLC_TRUE;
793 if( treectrl->ItemHasChildren( item ) )
795 wxTreeItemId search = FindItemByName( item, search_string, current,
802 item = treectrl->GetNextChild( root, cookie);
809 /**********************************************************************
810 * Rebuild the playlist
811 **********************************************************************/
812 void Playlist::Rebuild()
814 playlist_view_t *p_view;
815 playlist_t *p_playlist =
816 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
818 if( p_playlist == NULL )
822 /* ...and rebuild it */
823 vlc_mutex_lock( &p_playlist->object_lock );
825 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
827 /* HACK we should really get new*/
828 msg_Dbg( p_intf, "rebuilding tree for view %i", i_current_view );
829 treectrl->DeleteAllItems();
830 treectrl->AddRoot( wxU(_("root" )), -1, -1,
831 new PlaylistItem( p_view->p_root) );
833 wxTreeItemId root = treectrl->GetRootItem();
834 UpdateNode( p_playlist, p_view->p_root, root );
837 if( p_playlist->status.p_item != NULL )
839 item = FindItem( root, p_playlist->status.p_item );
841 else if( p_playlist->status.p_node != NULL )
843 item = FindItem( root, p_playlist->status.p_node );
850 if( p_playlist->i_size )
852 SetCurrentItem( item );
855 int i_count = CountItems( treectrl->GetRootItem() );
857 if( i_count < p_playlist->i_size && !b_changed_view )
859 i_current_view = VIEW_CATEGORY;
860 b_changed_view = VLC_TRUE;
861 vlc_mutex_unlock( &p_playlist->object_lock );
863 vlc_mutex_lock( &p_playlist->object_lock );
865 else if( i_count != p_playlist->i_size )
867 statusbar->SetStatusText( wxString::Format( wxU(_(
868 "%i items in playlist (%i not shown)")),
870 p_playlist->i_size - i_count ) );
874 statusbar->SetStatusText( wxString::Format( wxU(_(
875 "%i items in playlist")),
876 p_playlist->i_size ), 0 );
879 vlc_mutex_unlock( &p_playlist->object_lock );
881 vlc_object_release( p_playlist );
886 void Playlist::ShowPlaylist( bool show )
888 if( show ) Rebuild();
892 /* This function is called on a regular basis */
893 void Playlist::UpdatePlaylist()
897 /* If the playlist isn't show there's no need to update it */
898 if( !IsShown() ) return;
900 if( this->b_need_update )
902 this->b_need_update = VLC_FALSE;
906 /* Updating the playing status every 0.5s is enough */
907 if( i_update_counter % 5 ) return;
909 playlist_t *p_playlist =
910 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
912 if( p_playlist == NULL )
917 vlc_object_release( p_playlist );
920 /*****************************************************************************
922 *****************************************************************************/
923 void Playlist::DeleteItem( int item_id )
925 playlist_t *p_playlist =
926 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
928 if( p_playlist == NULL )
933 playlist_LockDelete( p_playlist, item_id );
935 vlc_object_release( p_playlist );
938 void Playlist::DeleteNode( playlist_item_t *p_item )
940 playlist_t *p_playlist =
941 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
943 if( p_playlist == NULL )
948 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE );
950 vlc_object_release( p_playlist );
954 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
959 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
965 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
966 { _("PLS file"), "*.pls", "export-pls" }};
968 wxString filter = wxT("");
970 playlist_t * p_playlist =
971 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
978 if( p_playlist->i_size == 0 )
980 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
981 wxICON_WARNING | wxOK, this );
982 vlc_object_release( p_playlist );
986 for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
988 filter.Append( wxU(formats[i].psz_desc) );
989 filter.Append( wxT("|") );
990 filter.Append( wxU(formats[i].psz_filter) );
991 filter.Append( wxT("|") );
993 wxFileDialog dialog( this, wxU(_("Save playlist")),
994 wxT(""), wxT(""), filter, wxSAVE );
996 if( dialog.ShowModal() == wxID_OK )
998 if( dialog.GetPath().mb_str() )
1000 playlist_Export( p_playlist, dialog.GetPath().mb_str(),
1001 formats[dialog.GetFilterIndex()].psz_module );
1005 vlc_object_release( p_playlist );
1009 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
1011 playlist_t *p_playlist =
1012 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1014 if( p_playlist == NULL )
1019 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
1020 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
1022 if( dialog.ShowModal() == wxID_OK )
1024 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
1027 vlc_object_release( p_playlist );
1030 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
1032 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
1036 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
1038 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
1042 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
1044 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
1048 /********************************************************************
1050 ********************************************************************/
1051 void Playlist::OnSort( wxCommandEvent& event )
1053 playlist_t *p_playlist =
1054 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1056 PlaylistItem *p_wxitem;
1057 p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
1059 if( p_playlist == NULL )
1063 vlc_mutex_lock( &p_playlist->object_lock );
1064 switch( event.GetId() )
1066 case SortTitle_Event:
1067 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1068 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1070 case RSortTitle_Event:
1071 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1072 SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
1074 vlc_mutex_unlock( &p_playlist->object_lock );
1076 vlc_object_release( p_playlist );
1080 /**********************************************************************
1081 * Search functions (user)
1082 **********************************************************************/
1083 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
1085 search_button->SetDefault();
1088 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
1090 wxString search_string = search_text->GetValue();
1092 vlc_bool_t pb_found = VLC_FALSE;
1094 wxTreeItemId found =
1095 FindItemByName( treectrl->GetRootItem(), search_string,
1096 search_current, &pb_found );
1100 search_current = found;
1101 treectrl->SelectItem( found, true );
1106 search_current = dummy;
1107 found = FindItemByName( treectrl->GetRootItem(), search_string,
1108 search_current, &pb_found );
1111 search_current = found;
1112 treectrl->SelectItem( found, true );
1117 /**********************************************************************
1118 * Selection functions
1119 **********************************************************************/
1120 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1124 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1129 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1133 /**********************************************************************
1134 * Playlist mode functions
1135 **********************************************************************/
1136 void Playlist::OnRandom( wxCommandEvent& event )
1139 val.b_bool = event.IsChecked();
1140 playlist_t *p_playlist =
1141 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1143 if( p_playlist == NULL )
1147 var_Set( p_playlist, "random", val);
1148 vlc_object_release( p_playlist );
1151 void Playlist::OnLoop( wxCommandEvent& event )
1154 val.b_bool = event.IsChecked();
1155 playlist_t *p_playlist =
1156 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1158 if( p_playlist == NULL )
1162 var_Set( p_playlist, "loop", val);
1163 vlc_object_release( p_playlist );
1166 void Playlist::OnRepeat( wxCommandEvent& event )
1169 val.b_bool = event.IsChecked();
1170 playlist_t *p_playlist =
1171 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1173 if( p_playlist == NULL )
1177 var_Set( p_playlist, "repeat", val);
1178 vlc_object_release( p_playlist );
1181 /********************************************************************
1183 ********************************************************************/
1184 void Playlist::OnActivateItem( wxTreeEvent& event )
1186 playlist_item_t *p_item,*p_node;
1187 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1188 VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1190 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1192 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1194 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1196 if( p_playlist == NULL )
1201 if( p_wxitem->p_item->i_children == -1 )
1203 p_node = p_wxparent->p_item;
1204 p_item = p_wxitem->p_item;
1208 p_node = p_wxitem->p_item;
1209 if( p_wxitem->p_item->i_children > 0 &&
1210 p_wxitem->p_item->pp_children[0]->i_children == -1)
1212 p_item = p_wxitem->p_item->pp_children[0];
1220 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1223 vlc_object_release( p_playlist );
1226 void Playlist::OnKeyDown( wxTreeEvent& event )
1228 long keycode = event.GetKeyCode();
1229 /* Delete selected items */
1230 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1232 /* We send a dummy event */
1233 OnDeleteSelection( event );
1237 void Playlist::OnEnDis( wxCommandEvent& event )
1239 playlist_t *p_playlist =
1240 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1242 if( p_playlist == NULL )
1246 msg_Warn( p_intf, "not implemented" );
1247 vlc_object_release( p_playlist );
1250 /**********************************************************************
1252 **********************************************************************/
1254 void Playlist::OnMenuOpen( wxMenuEvent& event)
1256 #if defined( __WXMSW__ )
1257 # define GetEventObject GetMenu
1260 if( event.GetEventObject() == p_view_menu )
1262 p_view_menu = ViewMenu();
1264 #if defined( __WXMSW__ )
1265 # undef GetEventObject
1269 void Playlist::OnMenuEvent( wxCommandEvent& event )
1271 playlist_t *p_playlist =
1272 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1274 if( p_playlist == NULL )
1279 if( event.GetId() < FirstView_Event )
1282 vlc_object_release( p_playlist );
1285 else if( event.GetId() < LastView_Event )
1288 int i_new_view = event.GetId() - FirstView_Event;
1290 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1292 if( p_view != NULL )
1294 b_changed_view = VLC_TRUE;
1295 i_current_view = i_new_view;
1296 playlist_ViewUpdate( p_playlist, i_new_view );
1298 vlc_object_release( p_playlist );
1301 else if( i_new_view >= VIEW_FIRST_SORTED &&
1302 i_new_view <= VIEW_LAST_SORTED )
1304 b_changed_view = VLC_TRUE;
1305 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1306 playlist_ViewUpdate( p_playlist, i_new_view );
1308 i_current_view = i_new_view;
1313 else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1315 if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1316 pp_sds[event.GetId() - FirstSD_Event] ) )
1318 playlist_ServicesDiscoveryAdd( p_playlist,
1319 pp_sds[event.GetId() - FirstSD_Event] );
1323 playlist_ServicesDiscoveryRemove( p_playlist,
1324 pp_sds[event.GetId() - FirstSD_Event] );
1327 vlc_object_release( p_playlist );
1330 wxMenu * Playlist::ViewMenu()
1332 playlist_t *p_playlist =
1333 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1335 if( p_playlist == NULL )
1342 p_view_menu = new wxMenu;
1346 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1349 wxMenuItem *item = node->GetData();
1350 node = node->GetNext();
1351 p_view_menu->Delete( item );
1355 /* FIXME : have a list of "should have" views */
1356 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1357 wxU(_("By category") ) );
1358 p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1359 wxU(_("Manually added") ) );
1360 p_view_menu->Append( FirstView_Event + VIEW_ALL,
1361 wxU(_("All items, unsorted") ) );
1362 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1363 wxU(_("Sorted by author") ) );
1365 vlc_object_release( p_playlist);
1370 wxMenu *Playlist::SDMenu()
1373 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1374 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1379 p_sd_menu = new wxMenu;
1381 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1385 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1387 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1389 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1391 p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1392 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1393 ( p_parser->psz_shortname ?
1394 p_parser->psz_shortname :p_parser->psz_object_name)) );
1396 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1397 p_parser->psz_object_name ) )
1399 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1402 INSERT_ELEM( (void**)pp_sds, i_number, i_number,
1403 (void*)p_parser->psz_object_name );
1406 vlc_list_release( p_list );
1407 vlc_object_release( p_playlist );
1412 /*****************************************************************************
1413 * Popup management functions
1414 *****************************************************************************/
1415 void Playlist::OnPopup( wxContextMenuEvent& event )
1417 wxPoint pt = event.GetPosition();
1419 i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1420 if( i_popup_item.IsOk() )
1422 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1424 PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1425 treectrl->GetItemParent( i_popup_item ) );
1426 p_popup_item = p_wxitem->p_item;
1427 p_popup_parent = p_wxparent->p_item;
1428 treectrl->SelectItem( i_popup_item );
1429 if( p_popup_item->i_children == -1 )
1430 Playlist::PopupMenu( item_popup,
1431 ScreenToClient( wxGetMousePosition() ) );
1433 Playlist::PopupMenu( node_popup,
1434 ScreenToClient( wxGetMousePosition() ) );
1438 void Playlist::OnPopupPlay( wxMenuEvent& event )
1440 playlist_t *p_playlist =
1441 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1443 if( p_playlist == NULL )
1447 if( p_popup_item != NULL )
1449 if( p_popup_item->i_children > -1 )
1451 if( event.GetId() == PopupPlay_Event &&
1452 p_popup_item->i_children > 0 )
1454 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1455 i_current_view, p_popup_item,
1456 p_popup_item->pp_children[0] );
1460 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1461 i_current_view, p_popup_item, NULL );
1466 if( event.GetId() == PopupPlay_Event )
1468 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1469 i_current_view, p_popup_parent,
1474 vlc_object_release( p_playlist );
1477 void Playlist::OnPopupPreparse( wxMenuEvent& event )
1479 playlist_t *p_playlist =
1480 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1482 if( p_playlist == NULL )
1486 Preparse( p_playlist );
1487 vlc_object_release( p_playlist );
1490 void Playlist::Preparse( playlist_t *p_playlist )
1492 if( p_popup_item != NULL )
1494 if( p_popup_item->i_children == -1 )
1496 playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1501 playlist_item_t *p_parent = p_popup_item;
1502 for( i = 0; i< p_parent->i_children ; i++ )
1505 i_popup_item = FindItem( treectrl->GetRootItem(),
1506 p_parent->pp_children[i] );
1507 p_popup_item = p_parent->pp_children[i];
1508 Preparse( p_playlist );
1514 void Playlist::OnPopupDel( wxMenuEvent& event )
1516 PlaylistItem *p_wxitem;
1518 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1520 if( p_wxitem->p_item->i_children == -1 )
1522 DeleteItem( p_wxitem->p_item->input.i_id );
1526 DeleteNode( p_wxitem->p_item );
1530 void Playlist::OnPopupSort( wxMenuEvent& event )
1532 PlaylistItem *p_wxitem;
1534 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1536 if( p_wxitem->p_item->i_children >= 0 )
1538 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1539 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1543 vlc_mutex_lock( &p_playlist->object_lock );
1544 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1545 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1546 vlc_mutex_unlock( &p_playlist->object_lock );
1548 treectrl->DeleteChildren( i_popup_item );
1549 UpdateNodeChildren( p_playlist, p_wxitem->p_item, i_popup_item );
1551 vlc_object_release( p_playlist );
1556 void Playlist::OnPopupInfo( wxMenuEvent& event )
1560 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1561 if( iteminfo_dialog->ShowModal() == wxID_OK )
1563 UpdateItem( i_popup_item );
1565 delete iteminfo_dialog;
1570 /*****************************************************************************
1571 * Custom events management
1572 *****************************************************************************/
1573 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1575 switch( event.GetId() )
1577 case UpdateItem_Event:
1578 UpdateItem( event.GetInt() );
1580 case AppendItem_Event:
1581 AppendItem( event );
1583 case RemoveItem_Event:
1584 RemoveItem( event.GetInt() );
1589 /*****************************************************************************
1590 * PlaylistChanged: callback triggered by the intf-change playlist variable
1591 * We don't rebuild the playlist directly here because we don't want the
1592 * caller to block for a too long time.
1593 *****************************************************************************/
1594 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1595 vlc_value_t oval, vlc_value_t nval, void *param )
1597 Playlist *p_playlist_dialog = (Playlist *)param;
1598 p_playlist_dialog->b_need_update = VLC_TRUE;
1602 /*****************************************************************************
1603 * Next: callback triggered by the playlist-current playlist variable
1604 *****************************************************************************/
1605 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1606 vlc_value_t oval, vlc_value_t nval, void *param )
1608 Playlist *p_playlist_dialog = (Playlist *)param;
1610 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1611 event.SetInt( oval.i_int );
1612 p_playlist_dialog->AddPendingEvent( event );
1613 event.SetInt( nval.i_int );
1614 p_playlist_dialog->AddPendingEvent( event );
1619 /*****************************************************************************
1620 * ItemChanged: callback triggered by the item-change playlist variable
1621 *****************************************************************************/
1622 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1623 vlc_value_t old_val, vlc_value_t new_val, void *param )
1625 Playlist *p_playlist_dialog = (Playlist *)param;
1627 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1628 event.SetInt( new_val.i_int );
1629 p_playlist_dialog->AddPendingEvent( event );
1633 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1634 vlc_value_t old_val, vlc_value_t new_val, void *param )
1636 Playlist *p_playlist_dialog = (Playlist *)param;
1638 wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1639 event.SetInt( new_val.i_int );
1640 p_playlist_dialog->AddPendingEvent( event );
1645 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1646 vlc_value_t oval, vlc_value_t nval, void *param )
1648 Playlist *p_playlist_dialog = (Playlist *)param;
1650 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1651 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1653 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1654 event.SetClientData( (void *)p_add );
1655 p_playlist_dialog->AddPendingEvent( event );