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();
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 );
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(9);
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 )
383 /* We want to be noticed of playlist changes */
385 /* Some global changes happened -> Rebuild all */
386 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
388 /* We went to the next item */
389 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
391 /* One item has been updated */
392 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
394 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
395 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
397 /* Update the playlist */
400 vlc_object_release( p_playlist );
403 Playlist::~Playlist()
405 playlist_t *p_playlist =
406 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
408 if( p_playlist == NULL )
413 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
414 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
415 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
416 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
417 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
418 vlc_object_release( p_playlist );
421 /**********************************************************************
423 **********************************************************************/
426 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
431 for( int i = 0; i< p_node->i_children ; i++ )
435 child = treectrl->GetFirstChild( node, cookie);
439 child = treectrl->GetNextChild( node, cookie );
444 /* Not enough children */
445 CreateNode( p_playlist, p_node->pp_children[i], node );
446 /* Keep the tree pointer up to date */
447 child = treectrl->GetNextChild( node, cookie );
453 treectrl->SetItemImage( node, p_node->input.i_type );
457 /* Creates the node p_node as last child of parent */
458 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
459 wxTreeItemId parent )
462 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
463 -1,-1, new PlaylistItem( p_node ) );
464 treectrl->SetItemImage( node, p_node->input.i_type );
466 UpdateNodeChildren( p_playlist, p_node, node );
469 /* Update all children (recursively) of this node */
470 void Playlist::UpdateNodeChildren( playlist_t *p_playlist,
471 playlist_item_t *p_node,
475 for( int i = 0; i< p_node->i_children ; i++ )
477 /* Append the item */
478 if( p_node->pp_children[i]->i_children == -1 )
481 treectrl->AppendItem( node,
482 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
483 new PlaylistItem( p_node->pp_children[i]) );
485 UpdateTreeItem( p_playlist, item );
489 CreateNode( p_playlist, p_node->pp_children[i],
495 /* Update an item in the tree */
496 void Playlist::UpdateTreeItem( playlist_t *p_playlist, wxTreeItemId item )
498 if( ! item.IsOk() ) return;
500 playlist_item_t *p_item =
501 ((PlaylistItem *)treectrl->GetItemData( item ))->p_item;
509 wxString duration = wxU( "" );
510 char *psz_author = vlc_input_item_GetInfo( &p_item->input,
511 _("Meta-information"),
513 char psz_duration[MSTRTIME_MAX_SIZE];
514 mtime_t dur = p_item->input.i_duration;
518 secstotimestr( psz_duration, dur/1000000 );
519 duration.Append( wxU( " ( " ) + wxString( wxU( psz_duration ) ) +
523 if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
525 msg.Printf( wxString( wxU( p_item->input.psz_name ) ) + duration );
529 msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
530 wxString(wxU(p_item->input.psz_name)) + duration );
532 treectrl->SetItemText( item , msg );
533 treectrl->SetItemImage( item, p_item->input.i_type );
535 if( p_playlist->status.p_item == p_item )
537 treectrl->SetItemBold( item, true );
538 treectrl->EnsureVisible( item );
542 treectrl->SetItemBold( item, false );
546 /* Process a AppendItem request */
547 void Playlist::AppendItem( wxCommandEvent& event )
549 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
551 playlist_t *p_playlist =
552 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
554 wxTreeItemId item,node;
555 if( p_playlist == NULL )
561 if( p_add->i_view != i_current_view )
566 node = FindItem( treectrl->GetRootItem(), p_add->p_node );
572 item = treectrl->AppendItem( node,
573 wxL2U( p_add->p_item->input.psz_name ), -1,-1,
574 new PlaylistItem( p_add->p_item ) );
575 treectrl->SetItemImage( item, p_add->p_item->input.i_type );
577 if( item.IsOk() && p_add->p_item->i_children == -1 )
579 UpdateTreeItem( p_playlist, item );
583 int i_count = CountItems( treectrl->GetRootItem());
584 if( i_count != p_playlist->i_size )
586 statusbar->SetStatusText( wxString::Format( wxU(_(
587 "%i items in playlist (%i not shown)")),
589 p_playlist->i_size - i_count ) );
590 if( !b_changed_view )
592 i_current_view = VIEW_CATEGORY;
593 b_changed_view = VLC_TRUE;
594 b_need_update = VLC_TRUE;
599 statusbar->SetStatusText( wxString::Format( wxU(_(
600 "%i items in playlist")),
601 p_playlist->i_size ), 0 );
604 vlc_object_release( p_playlist );
608 /* Process a updateitem request */
609 void Playlist::UpdateItem( int i )
611 if( i < 0 ) return; /* Sanity check */
612 playlist_item_t *p_item;
614 playlist_t *p_playlist =
615 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
618 if( p_playlist == NULL )
623 p_item = playlist_LockItemGetById( p_playlist, i );
625 wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
629 UpdateTreeItem( p_playlist, item );
632 vlc_object_release(p_playlist);
635 void Playlist::RemoveItem( int i )
637 if( i <= 0 ) return; /* Sanity check */
639 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
643 treectrl->Delete( item );
648 /**********************************************************************
649 * Search functions (internal)
650 **********************************************************************/
652 /* Find a wxItem from a playlist_item */
653 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
656 PlaylistItem *p_wxcurrent;
658 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
661 if( p_item == p_saved_item && saved_tree_item.IsOk() )
663 return saved_tree_item;
666 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
668 if( !p_item || !p_wxcurrent )
674 if( p_wxcurrent->p_item == p_item )
681 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
682 if( p_wxcurrent->p_item == p_item )
684 saved_tree_item = item;
685 p_saved_item = p_item;
688 if( treectrl->ItemHasChildren( item ) )
690 wxTreeItemId search = FindItem( item, p_item );
693 saved_tree_item = search;
694 p_saved_item = p_item;
698 item = treectrl->GetNextChild( root, cookie );
704 /* Find a wxItem from a playlist id */
705 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
708 PlaylistItem *p_wxcurrent;
710 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
713 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
727 if( p_wxcurrent->i_id == i_id )
734 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
735 if( p_wxcurrent->i_id == i_id )
739 if( treectrl->ItemHasChildren( item ) )
741 wxTreeItemId search = FindItem( item, i_id );
747 item = treectrl->GetNextChild( root, cookie );
754 int Playlist::CountItems( wxTreeItemId root )
758 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
762 if( treectrl->ItemHasChildren( item ) )
764 count += CountItems( item );
766 else if( ( (PlaylistItem *)treectrl->GetItemData( item ) )->
767 p_item->i_children == -1 )
769 item = treectrl->GetNextChild( root, cookie );
774 /* Find a wxItem from a name (from current) */
775 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
779 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
784 if( treectrl->GetItemText( item).Lower().Contains(
785 search_string.Lower() ) )
787 if( !current.IsOk() || *pb_current_found == VLC_TRUE )
791 else if( current.IsOk() && item == current )
793 *pb_current_found = VLC_TRUE;
796 if( treectrl->ItemHasChildren( item ) )
798 wxTreeItemId search = FindItemByName( item, search_string, current,
805 item = treectrl->GetNextChild( root, cookie);
812 /**********************************************************************
813 * Rebuild the playlist
814 **********************************************************************/
815 void Playlist::Rebuild()
817 playlist_view_t *p_view;
818 playlist_t *p_playlist =
819 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
821 if( p_playlist == NULL )
826 /* We can remove the callbacks before locking, anyway, we won't
828 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
829 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
830 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
831 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
832 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
834 /* ...and rebuild it */
835 vlc_mutex_lock( &p_playlist->object_lock );
837 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
839 /* HACK we should really get new*/
840 msg_Dbg( p_intf, "rebuilding tree for view %i", i_current_view );
841 treectrl->DeleteAllItems();
842 treectrl->AddRoot( wxU(_("root" )), -1, -1,
843 new PlaylistItem( p_view->p_root) );
845 wxTreeItemId root = treectrl->GetRootItem();
846 UpdateNode( p_playlist, p_view->p_root, root );
850 if( p_playlist->status.p_item != NULL )
852 item = FindItem( root, p_playlist->status.p_item );
854 else if( p_playlist->status.p_node != NULL )
856 item = FindItem( root, p_playlist->status.p_node );
863 if( p_playlist->i_size )
865 SetCurrentItem( item );
868 int i_count = CountItems( treectrl->GetRootItem() );
870 if( i_count < p_playlist->i_size && !b_changed_view )
872 i_current_view = VIEW_CATEGORY;
873 b_changed_view = VLC_TRUE;
874 vlc_mutex_unlock( &p_playlist->object_lock );
876 vlc_mutex_lock( &p_playlist->object_lock );
878 else if( i_count != p_playlist->i_size )
880 statusbar->SetStatusText( wxString::Format( wxU(_(
881 "%i items in playlist (%i not shown)")),
883 p_playlist->i_size - i_count ) );
887 statusbar->SetStatusText( wxString::Format( wxU(_(
888 "%i items in playlist")),
889 p_playlist->i_size ), 0 );
892 /* Put callbacks back online */
893 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
894 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
895 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
896 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
897 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
899 vlc_mutex_unlock( &p_playlist->object_lock );
901 vlc_object_release( p_playlist );
906 void Playlist::ShowPlaylist( bool show )
908 if( show ) Rebuild();
912 /* This function is called on a regular basis */
913 void Playlist::UpdatePlaylist()
917 /* If the playlist isn't show there's no need to update it */
918 if( !IsShown() ) return;
920 if( this->b_need_update )
922 this->b_need_update = VLC_FALSE;
926 /* Updating the playing status every 0.5s is enough */
927 if( i_update_counter % 5 ) return;
929 playlist_t *p_playlist =
930 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
932 if( p_playlist == NULL )
937 vlc_object_release( p_playlist );
940 /*****************************************************************************
942 *****************************************************************************/
943 void Playlist::DeleteItem( int item_id )
945 playlist_t *p_playlist =
946 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
948 if( p_playlist == NULL )
953 playlist_LockDelete( p_playlist, item_id );
955 vlc_object_release( p_playlist );
958 void Playlist::DeleteNode( playlist_item_t *p_item )
960 playlist_t *p_playlist =
961 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
963 if( p_playlist == NULL )
968 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE , VLC_FALSE );
970 vlc_object_release( p_playlist );
974 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
979 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
985 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
986 { _("PLS file"), "*.pls", "export-pls" }};
988 wxString filter = wxT("");
990 playlist_t * p_playlist =
991 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
998 if( p_playlist->i_size == 0 )
1000 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
1001 wxICON_WARNING | wxOK, this );
1002 vlc_object_release( p_playlist );
1006 for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
1008 filter.Append( wxU(formats[i].psz_desc) );
1009 filter.Append( wxT("|") );
1010 filter.Append( wxU(formats[i].psz_filter) );
1011 filter.Append( wxT("|") );
1013 wxFileDialog dialog( this, wxU(_("Save playlist")),
1014 wxT(""), wxT(""), filter, wxSAVE );
1016 if( dialog.ShowModal() == wxID_OK )
1018 if( dialog.GetPath().mb_str() )
1020 playlist_Export( p_playlist, dialog.GetPath().mb_str(),
1021 formats[dialog.GetFilterIndex()].psz_module );
1025 vlc_object_release( p_playlist );
1029 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
1031 playlist_t *p_playlist =
1032 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1034 if( p_playlist == NULL )
1039 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
1040 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
1042 if( dialog.ShowModal() == wxID_OK )
1044 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
1047 vlc_object_release( p_playlist );
1050 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
1052 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
1056 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
1058 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
1062 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
1064 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
1068 /********************************************************************
1070 ********************************************************************/
1071 void Playlist::OnSort( wxCommandEvent& event )
1073 playlist_t *p_playlist =
1074 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1076 PlaylistItem *p_wxitem;
1077 p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
1079 if( p_playlist == NULL )
1083 vlc_mutex_lock( &p_playlist->object_lock );
1084 switch( event.GetId() )
1086 case SortTitle_Event:
1087 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1088 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1090 case RSortTitle_Event:
1091 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1092 SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
1094 vlc_mutex_unlock( &p_playlist->object_lock );
1096 vlc_object_release( p_playlist );
1100 /**********************************************************************
1101 * Search functions (user)
1102 **********************************************************************/
1103 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
1105 search_button->SetDefault();
1108 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
1110 wxString search_string = search_text->GetValue();
1112 vlc_bool_t pb_found = VLC_FALSE;
1114 wxTreeItemId found =
1115 FindItemByName( treectrl->GetRootItem(), search_string,
1116 search_current, &pb_found );
1120 search_current = found;
1121 treectrl->SelectItem( found, true );
1126 search_current = dummy;
1127 found = FindItemByName( treectrl->GetRootItem(), search_string,
1128 search_current, &pb_found );
1131 search_current = found;
1132 treectrl->SelectItem( found, true );
1137 /**********************************************************************
1138 * Selection functions
1139 **********************************************************************/
1140 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1144 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1149 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1153 /**********************************************************************
1154 * Playlist mode functions
1155 **********************************************************************/
1156 void Playlist::OnRandom( wxCommandEvent& event )
1159 val.b_bool = event.IsChecked();
1160 playlist_t *p_playlist =
1161 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1163 if( p_playlist == NULL )
1167 var_Set( p_playlist, "random", val);
1168 vlc_object_release( p_playlist );
1171 void Playlist::OnLoop( wxCommandEvent& event )
1174 val.b_bool = event.IsChecked();
1175 playlist_t *p_playlist =
1176 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1178 if( p_playlist == NULL )
1182 var_Set( p_playlist, "loop", val);
1183 vlc_object_release( p_playlist );
1186 void Playlist::OnRepeat( wxCommandEvent& event )
1189 val.b_bool = event.IsChecked();
1190 playlist_t *p_playlist =
1191 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1193 if( p_playlist == NULL )
1197 var_Set( p_playlist, "repeat", val);
1198 vlc_object_release( p_playlist );
1201 /********************************************************************
1203 ********************************************************************/
1204 void Playlist::OnActivateItem( wxTreeEvent& event )
1206 playlist_item_t *p_item,*p_node;
1207 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1208 VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1210 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1212 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1214 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1216 if( p_playlist == NULL )
1221 if( p_wxitem->p_item->i_children == -1 )
1223 p_node = p_wxparent->p_item;
1224 p_item = p_wxitem->p_item;
1228 p_node = p_wxitem->p_item;
1229 if( p_wxitem->p_item->i_children > 0 &&
1230 p_wxitem->p_item->pp_children[0]->i_children == -1)
1232 p_item = p_wxitem->p_item->pp_children[0];
1240 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1243 vlc_object_release( p_playlist );
1246 void Playlist::OnKeyDown( wxTreeEvent& event )
1248 long keycode = event.GetKeyCode();
1249 /* Delete selected items */
1250 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1252 /* We send a dummy event */
1253 OnDeleteSelection( event );
1257 void Playlist::OnEnDis( wxCommandEvent& event )
1259 playlist_t *p_playlist =
1260 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1262 if( p_playlist == NULL )
1266 msg_Warn( p_intf, "not implemented" );
1267 vlc_object_release( p_playlist );
1270 /**********************************************************************
1272 **********************************************************************/
1274 void Playlist::OnMenuOpen( wxMenuEvent& event)
1276 #if defined( __WXMSW__ )
1277 # define GetEventObject GetMenu
1280 if( event.GetEventObject() == p_view_menu )
1282 p_view_menu = ViewMenu();
1284 #if defined( __WXMSW__ )
1285 # undef GetEventObject
1289 void Playlist::OnMenuEvent( wxCommandEvent& event )
1291 playlist_t *p_playlist =
1292 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1294 if( p_playlist == NULL )
1299 if( event.GetId() < FirstView_Event )
1302 vlc_object_release( p_playlist );
1305 else if( event.GetId() < LastView_Event )
1308 int i_new_view = event.GetId() - FirstView_Event;
1310 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1312 if( p_view != NULL )
1314 b_changed_view = VLC_TRUE;
1315 i_current_view = i_new_view;
1316 playlist_ViewUpdate( p_playlist, i_new_view );
1318 vlc_object_release( p_playlist );
1321 else if( i_new_view >= VIEW_FIRST_SORTED &&
1322 i_new_view <= VIEW_LAST_SORTED )
1324 b_changed_view = VLC_TRUE;
1325 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1326 playlist_ViewUpdate( p_playlist, i_new_view );
1328 i_current_view = i_new_view;
1333 else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1335 if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1336 pp_sds[event.GetId() - FirstSD_Event] ) )
1338 playlist_ServicesDiscoveryAdd( p_playlist,
1339 pp_sds[event.GetId() - FirstSD_Event] );
1344 playlist_ServicesDiscoveryRemove( p_playlist,
1345 pp_sds[event.GetId() - FirstSD_Event] );
1349 vlc_object_release( p_playlist );
1352 wxMenu * Playlist::ViewMenu()
1354 playlist_t *p_playlist =
1355 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1357 if( p_playlist == NULL )
1364 p_view_menu = new wxMenu;
1368 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1371 wxMenuItem *item = node->GetData();
1372 node = node->GetNext();
1373 p_view_menu->Delete( item );
1377 /* FIXME : have a list of "should have" views */
1378 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1379 wxU(_("By category") ) );
1380 p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1381 wxU(_("Manually added") ) );
1382 p_view_menu->Append( FirstView_Event + VIEW_ALL,
1383 wxU(_("All items, unsorted") ) );
1384 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1385 wxU(_("Sorted by author") ) );
1387 vlc_object_release( p_playlist);
1392 wxMenu *Playlist::SDMenu()
1395 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1396 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1401 p_sd_menu = new wxMenu;
1403 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1407 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1409 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1411 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1413 p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1414 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1415 ( p_parser->psz_shortname ?
1416 p_parser->psz_shortname :p_parser->psz_object_name)) );
1418 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1419 p_parser->psz_object_name ) )
1421 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1424 INSERT_ELEM( (void**)pp_sds, i_number, i_number,
1425 (void*)p_parser->psz_object_name );
1428 vlc_list_release( p_list );
1429 vlc_object_release( p_playlist );
1434 /*****************************************************************************
1435 * Popup management functions
1436 *****************************************************************************/
1437 void Playlist::OnPopup( wxContextMenuEvent& event )
1439 wxPoint pt = event.GetPosition();
1441 i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1442 if( i_popup_item.IsOk() )
1444 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1446 PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1447 treectrl->GetItemParent( i_popup_item ) );
1448 p_popup_item = p_wxitem->p_item;
1449 p_popup_parent = p_wxparent->p_item;
1450 treectrl->SelectItem( i_popup_item );
1451 if( p_popup_item->i_children == -1 )
1452 Playlist::PopupMenu( item_popup,
1453 ScreenToClient( wxGetMousePosition() ) );
1455 Playlist::PopupMenu( node_popup,
1456 ScreenToClient( wxGetMousePosition() ) );
1460 void Playlist::OnPopupPlay( wxMenuEvent& event )
1462 playlist_t *p_playlist =
1463 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1465 if( p_playlist == NULL )
1469 if( p_popup_item != NULL )
1471 if( p_popup_item->i_children > -1 )
1473 if( event.GetId() == PopupPlay_Event &&
1474 p_popup_item->i_children > 0 )
1476 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1477 i_current_view, p_popup_item,
1478 p_popup_item->pp_children[0] );
1482 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1483 i_current_view, p_popup_item, NULL );
1488 if( event.GetId() == PopupPlay_Event )
1490 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1491 i_current_view, p_popup_parent,
1496 vlc_object_release( p_playlist );
1499 void Playlist::OnPopupPreparse( wxMenuEvent& event )
1501 playlist_t *p_playlist =
1502 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1504 if( p_playlist == NULL )
1508 Preparse( p_playlist );
1509 vlc_object_release( p_playlist );
1512 void Playlist::Preparse( playlist_t *p_playlist )
1514 if( p_popup_item != NULL )
1516 if( p_popup_item->i_children == -1 )
1518 playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1523 playlist_item_t *p_parent = p_popup_item;
1524 for( i = 0; i< p_parent->i_children ; i++ )
1527 i_popup_item = FindItem( treectrl->GetRootItem(),
1528 p_parent->pp_children[i] );
1529 p_popup_item = p_parent->pp_children[i];
1530 Preparse( p_playlist );
1536 void Playlist::OnPopupDel( wxMenuEvent& event )
1538 PlaylistItem *p_wxitem;
1540 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1542 if( p_wxitem->p_item->i_children == -1 )
1544 DeleteItem( p_wxitem->p_item->input.i_id );
1548 DeleteNode( p_wxitem->p_item );
1552 void Playlist::OnPopupSort( wxMenuEvent& event )
1554 PlaylistItem *p_wxitem;
1556 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1558 if( p_wxitem->p_item->i_children >= 0 )
1560 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1561 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1565 vlc_mutex_lock( &p_playlist->object_lock );
1566 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1567 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1568 vlc_mutex_unlock( &p_playlist->object_lock );
1570 treectrl->DeleteChildren( i_popup_item );
1571 UpdateNodeChildren( p_playlist, p_wxitem->p_item, i_popup_item );
1573 vlc_object_release( p_playlist );
1578 void Playlist::OnPopupInfo( wxMenuEvent& event )
1582 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1583 if( iteminfo_dialog->ShowModal() == wxID_OK )
1585 UpdateItem( i_popup_item );
1587 delete iteminfo_dialog;
1592 /*****************************************************************************
1593 * Custom events management
1594 *****************************************************************************/
1595 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1597 switch( event.GetId() )
1599 case UpdateItem_Event:
1600 UpdateItem( event.GetInt() );
1602 case AppendItem_Event:
1603 AppendItem( event );
1605 case RemoveItem_Event:
1606 RemoveItem( event.GetInt() );
1611 /*****************************************************************************
1612 * PlaylistChanged: callback triggered by the intf-change playlist variable
1613 * We don't rebuild the playlist directly here because we don't want the
1614 * caller to block for a too long time.
1615 *****************************************************************************/
1616 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1617 vlc_value_t oval, vlc_value_t nval, void *param )
1619 Playlist *p_playlist_dialog = (Playlist *)param;
1620 p_playlist_dialog->b_need_update = VLC_TRUE;
1624 /*****************************************************************************
1625 * Next: callback triggered by the playlist-current playlist variable
1626 *****************************************************************************/
1627 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1628 vlc_value_t oval, vlc_value_t nval, void *param )
1630 Playlist *p_playlist_dialog = (Playlist *)param;
1632 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1633 event.SetInt( oval.i_int );
1634 p_playlist_dialog->AddPendingEvent( event );
1635 event.SetInt( nval.i_int );
1636 p_playlist_dialog->AddPendingEvent( event );
1641 /*****************************************************************************
1642 * ItemChanged: callback triggered by the item-change playlist variable
1643 *****************************************************************************/
1644 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1645 vlc_value_t old_val, vlc_value_t new_val, void *param )
1647 Playlist *p_playlist_dialog = (Playlist *)param;
1649 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1650 event.SetInt( new_val.i_int );
1651 p_playlist_dialog->AddPendingEvent( event );
1655 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1656 vlc_value_t old_val, vlc_value_t new_val, void *param )
1658 Playlist *p_playlist_dialog = (Playlist *)param;
1660 wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1661 event.SetInt( new_val.i_int );
1662 p_playlist_dialog->AddPendingEvent( event );
1667 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1668 vlc_value_t oval, vlc_value_t nval, void *param )
1670 Playlist *p_playlist_dialog = (Playlist *)param;
1672 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1673 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1675 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1676 event.SetClientData( (void *)p_add );
1677 p_playlist_dialog->AddPendingEvent( event );