1 /*****************************************************************************
2 * playlist.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2005 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_( "Repeat All" )
53 #define HELP_REPEAT N_( "Repeat One" )
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(500,300), 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()
408 playlist_t *p_playlist =
409 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
411 if( p_playlist == NULL )
416 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
417 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
418 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
419 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
420 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
421 vlc_object_release( p_playlist );
424 /**********************************************************************
426 **********************************************************************/
429 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
434 for( int i = 0; i< p_node->i_children ; i++ )
438 child = treectrl->GetFirstChild( node, cookie);
442 child = treectrl->GetNextChild( node, cookie );
447 /* Not enough children */
448 CreateNode( p_playlist, p_node->pp_children[i], node );
449 /* Keep the tree pointer up to date */
450 child = treectrl->GetNextChild( node, cookie );
456 treectrl->SetItemImage( node, p_node->input.i_type );
460 /* Creates the node p_node as last child of parent */
461 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
462 wxTreeItemId parent )
465 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
466 -1,-1, new PlaylistItem( p_node ) );
467 treectrl->SetItemImage( node, p_node->input.i_type );
469 UpdateNodeChildren( p_playlist, p_node, node );
472 /* Update all children (recursively) of this node */
473 void Playlist::UpdateNodeChildren( playlist_t *p_playlist,
474 playlist_item_t *p_node,
478 for( int i = 0; i< p_node->i_children ; i++ )
480 /* Append the item */
481 if( p_node->pp_children[i]->i_children == -1 )
484 treectrl->AppendItem( node,
485 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
486 new PlaylistItem( p_node->pp_children[i]) );
488 UpdateTreeItem( p_playlist, item );
492 CreateNode( p_playlist, p_node->pp_children[i],
498 /* Update an item in the tree */
499 void Playlist::UpdateTreeItem( playlist_t *p_playlist, wxTreeItemId item )
501 if( ! item.IsOk() ) return;
503 wxTreeItemData *p_data = treectrl->GetItemData( item );
504 if( !p_data ) return;
506 playlist_item_t *p_item = ((PlaylistItem *)p_data)->p_item;
507 if( !p_item ) return;
510 wxString duration = wxU( "" );
511 char *psz_author = vlc_input_item_GetInfo( &p_item->input,
512 _("Meta-information"),
514 if( psz_author == NULL )
516 char psz_duration[MSTRTIME_MAX_SIZE];
517 mtime_t dur = p_item->input.i_duration;
521 secstotimestr( psz_duration, dur/1000000 );
522 duration.Append( wxU( " ( " ) + wxString( wxU( psz_duration ) ) +
526 if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
528 msg.Printf( wxString( wxU( p_item->input.psz_name ) ) + duration );
532 msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
533 wxString(wxU(p_item->input.psz_name)) + duration );
536 treectrl->SetItemText( item , msg );
537 treectrl->SetItemImage( item, p_item->input.i_type );
539 if( p_playlist->status.p_item == p_item )
541 treectrl->SetItemBold( item, true );
542 treectrl->EnsureVisible( item );
546 treectrl->SetItemBold( item, false );
550 /* Process a AppendItem request */
551 void Playlist::AppendItem( wxCommandEvent& event )
553 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
555 playlist_t *p_playlist =
556 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
558 wxTreeItemId item,node;
559 if( p_playlist == NULL )
565 if( p_add->i_view != i_current_view )
570 node = FindItem( treectrl->GetRootItem(), p_add->p_node );
576 item = treectrl->AppendItem( node,
577 wxL2U( p_add->p_item->input.psz_name ), -1,-1,
578 new PlaylistItem( p_add->p_item ) );
579 treectrl->SetItemImage( item, p_add->p_item->input.i_type );
581 if( item.IsOk() && p_add->p_item->i_children == -1 )
583 UpdateTreeItem( p_playlist, item );
587 int i_count = CountItems( treectrl->GetRootItem());
588 if( i_count != p_playlist->i_size )
590 statusbar->SetStatusText( wxString::Format( wxU(_(
591 "%i items in playlist (%i not shown)")),
593 p_playlist->i_size - i_count ) );
594 if( !b_changed_view )
596 i_current_view = VIEW_CATEGORY;
597 b_changed_view = VLC_TRUE;
598 b_need_update = VLC_TRUE;
603 statusbar->SetStatusText( wxString::Format( wxU(_(
604 "%i items in playlist")),
605 p_playlist->i_size ), 0 );
608 vlc_object_release( p_playlist );
612 /* Process a updateitem request */
613 void Playlist::UpdateItem( int i )
615 if( i < 0 ) return; /* Sanity check */
616 playlist_item_t *p_item;
618 playlist_t *p_playlist =
619 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
622 if( p_playlist == NULL )
627 p_item = playlist_LockItemGetById( p_playlist, i );
629 wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
633 UpdateTreeItem( p_playlist, item );
636 vlc_object_release(p_playlist);
639 void Playlist::RemoveItem( int i )
641 if( i <= 0 ) return; /* Sanity check */
643 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
647 treectrl->Delete( item );
652 /**********************************************************************
653 * Search functions (internal)
654 **********************************************************************/
656 /* Find a wxItem from a playlist_item */
657 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
660 PlaylistItem *p_wxcurrent;
662 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
665 if( p_item == p_saved_item && saved_tree_item.IsOk() )
667 return saved_tree_item;
670 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
672 if( !p_item || !p_wxcurrent )
678 if( p_wxcurrent->p_item == p_item )
685 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
686 if( p_wxcurrent->p_item == p_item )
688 saved_tree_item = item;
689 p_saved_item = p_item;
692 if( treectrl->ItemHasChildren( item ) )
694 wxTreeItemId search = FindItem( item, p_item );
697 saved_tree_item = search;
698 p_saved_item = p_item;
702 item = treectrl->GetNextChild( root, cookie );
708 /* Find a wxItem from a playlist id */
709 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
712 PlaylistItem *p_wxcurrent;
714 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
717 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
731 if( p_wxcurrent->i_id == i_id )
738 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
739 if( p_wxcurrent->i_id == i_id )
743 if( treectrl->ItemHasChildren( item ) )
745 wxTreeItemId search = FindItem( item, i_id );
751 item = treectrl->GetNextChild( root, cookie );
758 int Playlist::CountItems( wxTreeItemId root )
762 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
766 if( treectrl->ItemHasChildren( item ) )
768 count += CountItems( item );
770 else if( ( (PlaylistItem *)treectrl->GetItemData( item ) )->
771 p_item->i_children == -1 )
773 item = treectrl->GetNextChild( root, cookie );
778 /* Find a wxItem from a name (from current) */
779 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
783 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
788 if( treectrl->GetItemText( item).Lower().Contains(
789 search_string.Lower() ) )
791 if( !current.IsOk() || *pb_current_found == VLC_TRUE )
795 else if( current.IsOk() && item == current )
797 *pb_current_found = VLC_TRUE;
800 if( treectrl->ItemHasChildren( item ) )
802 wxTreeItemId search = FindItemByName( item, search_string, current,
809 item = treectrl->GetNextChild( root, cookie);
816 /**********************************************************************
817 * Rebuild the playlist
818 **********************************************************************/
819 void Playlist::Rebuild( vlc_bool_t b_root )
821 playlist_view_t *p_view;
822 playlist_t *p_playlist =
823 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
825 if( p_playlist == NULL )
830 /* We can remove the callbacks before locking, anyway, we won't
834 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
835 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
836 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
837 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
838 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
840 /* ...and rebuild it */
841 LockPlaylist( p_intf->p_sys, p_playlist );
844 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
846 /* HACK we should really get new*/
847 treectrl->DeleteAllItems();
848 treectrl->AddRoot( wxU(_("root" )), -1, -1,
849 new PlaylistItem( p_view->p_root) );
851 wxTreeItemId root = treectrl->GetRootItem();
852 UpdateNode( p_playlist, p_view->p_root, root );
854 int i_count = CountItems( treectrl->GetRootItem() );
856 if( i_count < p_playlist->i_size && !b_changed_view )
858 i_current_view = VIEW_CATEGORY;
859 b_changed_view = VLC_TRUE;
860 Rebuild( VLC_FALSE );
862 else if( i_count != p_playlist->i_size )
864 statusbar->SetStatusText( wxString::Format( wxU(_(
865 "%i items in playlist (%i not shown)")),
867 p_playlist->i_size - i_count ) );
871 statusbar->SetStatusText( wxString::Format( wxU(_(
872 "%i items in playlist")),
873 p_playlist->i_size ), 0 );
878 /* Put callbacks back online */
879 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
880 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
881 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
882 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
883 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
885 UnlockPlaylist( p_intf->p_sys, p_playlist );
887 vlc_object_release( p_playlist );
892 void Playlist::ShowPlaylist( bool show )
894 if( show ) Rebuild( VLC_TRUE );
898 /* This function is called on a regular basis */
899 void Playlist::UpdatePlaylist()
903 /* If the playlist isn't show there's no need to update it */
904 if( !IsShown() ) return;
906 if( this->b_need_update )
908 this->b_need_update = VLC_FALSE;
912 /* Updating the playing status every 0.5s is enough */
913 if( i_update_counter % 5 ) return;
915 playlist_t *p_playlist =
916 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
918 if( p_playlist == NULL )
923 vlc_object_release( p_playlist );
926 /*****************************************************************************
928 *****************************************************************************/
929 void Playlist::DeleteItem( int item_id )
931 playlist_t *p_playlist =
932 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
934 if( p_playlist == NULL )
939 playlist_LockDelete( p_playlist, item_id );
941 vlc_object_release( p_playlist );
944 void Playlist::DeleteNode( playlist_item_t *p_item )
946 playlist_t *p_playlist =
947 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
949 if( p_playlist == NULL )
954 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE , VLC_FALSE );
956 vlc_object_release( p_playlist );
960 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
965 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
971 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
972 { _("PLS file"), "*.pls", "export-pls" }};
974 wxString filter = wxT("");
976 playlist_t * p_playlist =
977 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
984 if( p_playlist->i_size == 0 )
986 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
987 wxICON_WARNING | wxOK, this );
988 vlc_object_release( p_playlist );
992 for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
994 filter.Append( wxU(formats[i].psz_desc) );
995 filter.Append( wxT("|") );
996 filter.Append( wxU(formats[i].psz_filter) );
997 filter.Append( wxT("|") );
999 wxFileDialog dialog( this, wxU(_("Save playlist")),
1000 wxT(""), wxT(""), filter, wxSAVE );
1002 if( dialog.ShowModal() == wxID_OK )
1004 if( dialog.GetPath().mb_str() )
1006 playlist_Export( p_playlist, dialog.GetPath().mb_str(),
1007 formats[dialog.GetFilterIndex()].psz_module );
1011 vlc_object_release( p_playlist );
1015 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
1017 playlist_t *p_playlist =
1018 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1020 if( p_playlist == NULL )
1025 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
1026 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
1028 if( dialog.ShowModal() == wxID_OK )
1030 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
1033 vlc_object_release( p_playlist );
1036 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
1038 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
1042 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
1044 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
1048 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
1050 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
1054 /********************************************************************
1056 ********************************************************************/
1057 void Playlist::OnSort( wxCommandEvent& event )
1059 playlist_t *p_playlist =
1060 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1062 PlaylistItem *p_wxitem;
1063 p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
1065 if( p_playlist == NULL )
1069 LockPlaylist( p_intf->p_sys, p_playlist );
1070 switch( event.GetId() )
1072 case SortTitle_Event:
1073 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1074 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1076 case RSortTitle_Event:
1077 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1078 SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
1080 UnlockPlaylist( p_intf->p_sys, p_playlist );
1082 vlc_object_release( p_playlist );
1083 Rebuild( VLC_TRUE );
1086 /**********************************************************************
1087 * Search functions (user)
1088 **********************************************************************/
1089 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
1091 search_button->SetDefault();
1094 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
1096 wxString search_string = search_text->GetValue();
1098 vlc_bool_t pb_found = VLC_FALSE;
1100 wxTreeItemId found =
1101 FindItemByName( treectrl->GetRootItem(), search_string,
1102 search_current, &pb_found );
1106 search_current = found;
1107 treectrl->SelectItem( found, true );
1112 search_current = dummy;
1113 found = FindItemByName( treectrl->GetRootItem(), search_string,
1114 search_current, &pb_found );
1117 search_current = found;
1118 treectrl->SelectItem( found, true );
1123 /**********************************************************************
1124 * Selection functions
1125 **********************************************************************/
1126 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1130 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1132 Rebuild( VLC_TRUE );
1135 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1139 /**********************************************************************
1140 * Playlist mode functions
1141 **********************************************************************/
1142 void Playlist::OnRandom( wxCommandEvent& event )
1145 val.b_bool = event.IsChecked();
1146 playlist_t *p_playlist =
1147 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1149 if( p_playlist == NULL )
1153 var_Set( p_playlist, "random", val);
1154 vlc_object_release( p_playlist );
1157 void Playlist::OnLoop( wxCommandEvent& event )
1160 val.b_bool = event.IsChecked();
1161 playlist_t *p_playlist =
1162 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1164 if( p_playlist == NULL )
1168 var_Set( p_playlist, "loop", val);
1169 vlc_object_release( p_playlist );
1172 void Playlist::OnRepeat( wxCommandEvent& event )
1175 val.b_bool = event.IsChecked();
1176 playlist_t *p_playlist =
1177 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1179 if( p_playlist == NULL )
1183 var_Set( p_playlist, "repeat", val);
1184 vlc_object_release( p_playlist );
1187 /********************************************************************
1189 ********************************************************************/
1190 void Playlist::OnActivateItem( wxTreeEvent& event )
1192 playlist_item_t *p_item,*p_node;
1193 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1194 VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1196 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1198 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1200 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1202 if( p_playlist == NULL )
1207 if( p_wxitem->p_item->i_children == -1 )
1209 p_node = p_wxparent->p_item;
1210 p_item = p_wxitem->p_item;
1214 p_node = p_wxitem->p_item;
1215 if( p_wxitem->p_item->i_children > 0 &&
1216 p_wxitem->p_item->pp_children[0]->i_children == -1)
1218 p_item = p_wxitem->p_item->pp_children[0];
1226 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1229 vlc_object_release( p_playlist );
1232 void Playlist::OnKeyDown( wxTreeEvent& event )
1234 long keycode = event.GetKeyCode();
1235 /* Delete selected items */
1236 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1238 /* We send a dummy event */
1239 OnDeleteSelection( event );
1243 void Playlist::OnEnDis( wxCommandEvent& event )
1245 playlist_t *p_playlist =
1246 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1248 if( p_playlist == NULL )
1252 msg_Warn( p_intf, "not implemented" );
1253 vlc_object_release( p_playlist );
1256 /**********************************************************************
1258 **********************************************************************/
1260 void Playlist::OnMenuOpen( wxMenuEvent& event)
1262 #if defined( __WXMSW__ )
1263 # define GetEventObject GetMenu
1266 if( event.GetEventObject() == p_view_menu )
1268 p_view_menu = ViewMenu();
1270 #if defined( __WXMSW__ )
1271 # undef GetEventObject
1275 void Playlist::OnMenuEvent( wxCommandEvent& event )
1277 playlist_t *p_playlist =
1278 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1280 if( p_playlist == NULL )
1285 if( event.GetId() < FirstView_Event )
1288 vlc_object_release( p_playlist );
1291 else if( event.GetId() < LastView_Event )
1294 int i_new_view = event.GetId() - FirstView_Event;
1296 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1298 if( p_view != NULL )
1300 b_changed_view = VLC_TRUE;
1301 i_current_view = i_new_view;
1302 playlist_ViewUpdate( p_playlist, i_new_view );
1303 Rebuild( VLC_TRUE );
1304 vlc_object_release( p_playlist );
1307 else if( i_new_view >= VIEW_FIRST_SORTED &&
1308 i_new_view <= VIEW_LAST_SORTED )
1310 b_changed_view = VLC_TRUE;
1311 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1312 playlist_ViewUpdate( p_playlist, i_new_view );
1314 i_current_view = i_new_view;
1316 Rebuild( VLC_TRUE );
1319 else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1321 if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1322 pp_sds[event.GetId() - FirstSD_Event] ) )
1324 playlist_ServicesDiscoveryAdd( p_playlist,
1325 pp_sds[event.GetId() - FirstSD_Event] );
1330 playlist_ServicesDiscoveryRemove( p_playlist,
1331 pp_sds[event.GetId() - FirstSD_Event] );
1335 vlc_object_release( p_playlist );
1338 wxMenu * Playlist::ViewMenu()
1340 playlist_t *p_playlist =
1341 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1343 if( p_playlist == NULL )
1350 p_view_menu = new wxMenu;
1354 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1357 wxMenuItem *item = node->GetData();
1358 node = node->GetNext();
1359 p_view_menu->Delete( item );
1363 /* FIXME : have a list of "should have" views */
1364 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1365 wxU(_("Normal") ) );
1366 /* p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1367 wxU(_("Manually added") ) );
1368 p_view_menu->Append( FirstView_Event + VIEW_ALL,
1369 wxU(_("All items, unsorted") ) ); */
1370 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1371 wxU(_("Sorted by artist") ) );
1373 vlc_object_release( p_playlist);
1378 wxMenu *Playlist::SDMenu()
1381 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1382 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1387 p_sd_menu = new wxMenu;
1389 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1393 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1395 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1397 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1400 if( i_number ) pp_sds = (char **)calloc( i_number, sizeof(void *) );
1403 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1405 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1407 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1409 p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1410 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1411 (p_parser->psz_shortname ?
1412 p_parser->psz_shortname : p_parser->psz_object_name) ) );
1414 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1415 p_parser->psz_object_name ) )
1417 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1420 pp_sds[i_number++] = p_parser->psz_object_name;
1423 vlc_list_release( p_list );
1424 vlc_object_release( p_playlist );
1429 /*****************************************************************************
1430 * Popup management functions
1431 *****************************************************************************/
1432 void Playlist::OnPopup( wxContextMenuEvent& event )
1434 wxPoint pt = event.GetPosition();
1436 i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1437 if( i_popup_item.IsOk() )
1439 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1441 PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1442 treectrl->GetItemParent( i_popup_item ) );
1443 p_popup_item = p_wxitem->p_item;
1444 p_popup_parent = p_wxparent->p_item;
1445 treectrl->SelectItem( i_popup_item );
1446 if( p_popup_item->i_children == -1 )
1447 Playlist::PopupMenu( item_popup,
1448 ScreenToClient( wxGetMousePosition() ) );
1450 Playlist::PopupMenu( node_popup,
1451 ScreenToClient( wxGetMousePosition() ) );
1455 void Playlist::OnPopupPlay( wxMenuEvent& event )
1457 playlist_t *p_playlist =
1458 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1460 if( p_playlist == NULL )
1464 if( p_popup_item != NULL )
1466 if( p_popup_item->i_children > -1 )
1468 if( event.GetId() == PopupPlay_Event &&
1469 p_popup_item->i_children > 0 )
1471 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1472 i_current_view, p_popup_item,
1473 p_popup_item->pp_children[0] );
1477 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1478 i_current_view, p_popup_item, NULL );
1483 if( event.GetId() == PopupPlay_Event )
1485 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1486 i_current_view, p_popup_parent,
1491 vlc_object_release( p_playlist );
1494 void Playlist::OnPopupPreparse( wxMenuEvent& event )
1496 playlist_t *p_playlist =
1497 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1499 if( p_playlist == NULL )
1503 Preparse( p_playlist );
1504 vlc_object_release( p_playlist );
1507 void Playlist::Preparse( playlist_t *p_playlist )
1509 if( p_popup_item != NULL )
1511 if( p_popup_item->i_children == -1 )
1513 playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1518 playlist_item_t *p_parent = p_popup_item;
1519 for( i = 0; i< p_parent->i_children ; i++ )
1522 i_popup_item = FindItem( treectrl->GetRootItem(),
1523 p_parent->pp_children[i] );
1524 p_popup_item = p_parent->pp_children[i];
1525 Preparse( p_playlist );
1531 void Playlist::OnPopupDel( wxMenuEvent& event )
1533 PlaylistItem *p_wxitem;
1535 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1537 if( p_wxitem->p_item->i_children == -1 )
1539 DeleteItem( p_wxitem->p_item->input.i_id );
1543 DeleteNode( p_wxitem->p_item );
1547 void Playlist::OnPopupSort( wxMenuEvent& event )
1549 PlaylistItem *p_wxitem;
1551 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1553 if( p_wxitem->p_item->i_children >= 0 )
1555 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1556 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1560 LockPlaylist( p_intf->p_sys, p_playlist );
1561 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1562 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1563 UnlockPlaylist( p_intf->p_sys, p_playlist );
1565 treectrl->DeleteChildren( i_popup_item );
1566 UpdateNodeChildren( p_playlist, p_wxitem->p_item, i_popup_item );
1568 vlc_object_release( p_playlist );
1573 void Playlist::OnPopupInfo( wxMenuEvent& event )
1577 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1578 if( iteminfo_dialog->ShowModal() == wxID_OK )
1580 UpdateItem( i_popup_item );
1582 delete iteminfo_dialog;
1587 /*****************************************************************************
1588 * Custom events management
1589 *****************************************************************************/
1590 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1592 switch( event.GetId() )
1594 case UpdateItem_Event:
1595 UpdateItem( event.GetInt() );
1597 case AppendItem_Event:
1598 AppendItem( event );
1600 case RemoveItem_Event:
1601 RemoveItem( event.GetInt() );
1606 /*****************************************************************************
1607 * PlaylistChanged: callback triggered by the intf-change playlist variable
1608 * We don't rebuild the playlist directly here because we don't want the
1609 * caller to block for a too long time.
1610 *****************************************************************************/
1611 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1612 vlc_value_t oval, vlc_value_t nval, void *param )
1614 Playlist *p_playlist_dialog = (Playlist *)param;
1615 p_playlist_dialog->b_need_update = VLC_TRUE;
1619 /*****************************************************************************
1620 * Next: callback triggered by the playlist-current playlist variable
1621 *****************************************************************************/
1622 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1623 vlc_value_t oval, vlc_value_t nval, void *param )
1625 Playlist *p_playlist_dialog = (Playlist *)param;
1627 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1628 event.SetInt( oval.i_int );
1629 p_playlist_dialog->AddPendingEvent( event );
1630 event.SetInt( nval.i_int );
1631 p_playlist_dialog->AddPendingEvent( event );
1636 /*****************************************************************************
1637 * ItemChanged: callback triggered by the item-change playlist variable
1638 *****************************************************************************/
1639 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1640 vlc_value_t old_val, vlc_value_t new_val, void *param )
1642 Playlist *p_playlist_dialog = (Playlist *)param;
1644 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1645 event.SetInt( new_val.i_int );
1646 p_playlist_dialog->AddPendingEvent( event );
1650 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1651 vlc_value_t old_val, vlc_value_t new_val, void *param )
1653 Playlist *p_playlist_dialog = (Playlist *)param;
1655 wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1656 event.SetInt( new_val.i_int );
1657 p_playlist_dialog->AddPendingEvent( event );
1662 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1663 vlc_value_t oval, vlc_value_t nval, void *param )
1665 Playlist *p_playlist_dialog = (Playlist *)param;
1667 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1669 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1671 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1672 event.SetClientData( (void *)p_add );
1673 p_playlist_dialog->AddPendingEvent( event );