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 )
382 /* Update the playlist */
385 /* We want to be noticed of playlist changes */
387 /* Some global changes happened -> Rebuild all */
388 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
390 /* We went to the next item */
391 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
393 /* One item has been updated */
394 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
396 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
397 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
399 vlc_object_release( p_playlist );
404 Playlist::~Playlist()
406 playlist_t *p_playlist =
407 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
409 if( p_playlist == NULL )
414 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
415 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
416 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
417 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
418 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
419 vlc_object_release( p_playlist );
422 /**********************************************************************
424 **********************************************************************/
427 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
432 for( int i = 0; i< p_node->i_children ; i++ )
436 child = treectrl->GetFirstChild( node, cookie);
440 child = treectrl->GetNextChild( node, cookie );
445 /* Not enough children */
446 CreateNode( p_playlist, p_node->pp_children[i], node );
447 /* Keep the tree pointer up to date */
448 child = treectrl->GetNextChild( node, cookie );
454 treectrl->SetItemImage( node, p_node->input.i_type );
458 /* Creates the node p_node as last child of parent */
459 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
460 wxTreeItemId parent )
463 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
464 -1,-1, new PlaylistItem( p_node ) );
465 treectrl->SetItemImage( node, p_node->input.i_type );
467 UpdateNodeChildren( p_playlist, p_node, node );
470 /* Update all children (recursively) of this node */
471 void Playlist::UpdateNodeChildren( playlist_t *p_playlist,
472 playlist_item_t *p_node,
476 for( int i = 0; i< p_node->i_children ; i++ )
478 /* Append the item */
479 if( p_node->pp_children[i]->i_children == -1 )
482 treectrl->AppendItem( node,
483 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
484 new PlaylistItem( p_node->pp_children[i]) );
486 UpdateTreeItem( p_playlist, item );
488 treectrl->SetItemImage( item,
489 p_node->pp_children[i]->input.i_type );
493 CreateNode( p_playlist, p_node->pp_children[i],
499 /* Set current item */
500 void Playlist::SetCurrentItem( wxTreeItemId item )
504 treectrl->SetItemBold( item, true );
505 treectrl->EnsureVisible( item );
509 /* Update an item in the tree */
510 void Playlist::UpdateTreeItem( playlist_t *p_playlist, wxTreeItemId item )
512 playlist_item_t *p_item =
513 ((PlaylistItem *)treectrl->GetItemData( item ))->p_item;
521 wxString duration = wxU( "" );
522 char *psz_author = vlc_input_item_GetInfo( &p_item->input,
523 _("Meta-information"),
525 char psz_duration[MSTRTIME_MAX_SIZE];
526 mtime_t dur = p_item->input.i_duration;
530 secstotimestr( psz_duration, dur/1000000 );
531 duration.Append( wxU( " ( " ) + wxString( wxU( psz_duration ) ) +
535 if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
537 msg.Printf( wxString( wxU( p_item->input.psz_name ) ) + duration );
541 msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
542 wxString(wxU(p_item->input.psz_name)) + duration );
544 treectrl->SetItemText( item , msg );
545 treectrl->SetItemImage( item, p_item->input.i_type );
547 if( p_playlist->status.p_item == p_item )
549 SetCurrentItem( item );
553 treectrl->SetItemBold( item, false );
557 /* Process a AppendItem request */
558 void Playlist::AppendItem( wxCommandEvent& event )
560 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
562 playlist_t *p_playlist =
563 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
565 wxTreeItemId item,node;
566 if( p_playlist == NULL )
572 if( p_add->i_view != i_current_view )
577 node = FindItem( treectrl->GetRootItem(), p_add->p_node );
583 item = treectrl->AppendItem( node,
584 wxL2U( p_add->p_item->input.psz_name ), -1,-1,
585 new PlaylistItem( p_add->p_item ) );
586 treectrl->SetItemImage( item, p_add->p_item->input.i_type );
588 if( item.IsOk() && p_add->p_item->i_children == -1 )
590 UpdateTreeItem( p_playlist, item );
594 int i_count = CountItems( treectrl->GetRootItem());
595 if( i_count != p_playlist->i_size )
597 statusbar->SetStatusText( wxString::Format( wxU(_(
598 "%i items in playlist (%i not shown)")),
600 p_playlist->i_size - i_count ) );
601 if( !b_changed_view )
603 i_current_view = VIEW_CATEGORY;
604 b_changed_view = VLC_TRUE;
605 b_need_update = VLC_TRUE;
610 statusbar->SetStatusText( wxString::Format( wxU(_(
611 "%i items in playlist")),
612 p_playlist->i_size ), 0 );
615 vlc_object_release( p_playlist );
619 /* Process a updateitem request */
620 void Playlist::UpdateItem( int i )
622 if( i < 0 ) return; /* Sanity check */
623 playlist_item_t *p_item;
625 playlist_t *p_playlist =
626 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
629 if( p_playlist == NULL )
634 p_item = playlist_LockItemGetById( p_playlist, i );
636 wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
640 UpdateTreeItem( p_playlist, item );
643 vlc_object_release(p_playlist);
646 void Playlist::RemoveItem( int i )
648 if( i <= 0 ) return; /* Sanity check */
650 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
654 treectrl->Delete( item );
659 /**********************************************************************
660 * Search functions (internal)
661 **********************************************************************/
663 /* Find a wxItem from a playlist_item */
664 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
667 PlaylistItem *p_wxcurrent;
669 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
672 if( p_item == p_saved_item && saved_tree_item.IsOk() )
674 return saved_tree_item;
677 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
685 if( p_wxcurrent->p_item == p_item )
692 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
693 if( p_wxcurrent->p_item == p_item )
695 saved_tree_item = item;
696 p_saved_item = p_item;
699 if( treectrl->ItemHasChildren( item ) )
701 wxTreeItemId search = FindItem( item, p_item );
704 saved_tree_item = search;
705 p_saved_item = p_item;
709 item = treectrl->GetNextChild( root, cookie );
715 /* Find a wxItem from a playlist id */
716 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
719 PlaylistItem *p_wxcurrent;
721 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
724 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
738 if( p_wxcurrent->i_id == i_id )
745 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
746 if( p_wxcurrent->i_id == i_id )
750 if( treectrl->ItemHasChildren( item ) )
752 wxTreeItemId search = FindItem( item, i_id );
758 item = treectrl->GetNextChild( root, cookie );
765 int Playlist::CountItems( wxTreeItemId root )
769 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
773 if( treectrl->ItemHasChildren( item ) )
775 count += CountItems( item );
777 else if( ( (PlaylistItem *)treectrl->GetItemData( item ) )->
778 p_item->i_children == -1 )
780 item = treectrl->GetNextChild( root, cookie );
785 /* Find a wxItem from a name (from current) */
786 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
790 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
795 if( treectrl->GetItemText( item).Lower().Contains(
796 search_string.Lower() ) )
798 if( !current.IsOk() || *pb_current_found == VLC_TRUE )
802 else if( current.IsOk() && item == current )
804 *pb_current_found = VLC_TRUE;
807 if( treectrl->ItemHasChildren( item ) )
809 wxTreeItemId search = FindItemByName( item, search_string, current,
816 item = treectrl->GetNextChild( root, cookie);
823 /**********************************************************************
824 * Rebuild the playlist
825 **********************************************************************/
826 void Playlist::Rebuild()
828 playlist_view_t *p_view;
829 playlist_t *p_playlist =
830 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
832 if( p_playlist == NULL )
836 /* ...and rebuild it */
837 vlc_mutex_lock( &p_playlist->object_lock );
839 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
841 /* HACK we should really get new*/
842 msg_Dbg( p_intf, "rebuilding tree for view %i", i_current_view );
843 treectrl->DeleteAllItems();
844 treectrl->AddRoot( wxU(_("root" )), -1, -1,
845 new PlaylistItem( p_view->p_root) );
847 wxTreeItemId root = treectrl->GetRootItem();
848 UpdateNode( p_playlist, p_view->p_root, root );
851 if( p_playlist->status.p_item != NULL )
853 item = FindItem( root, p_playlist->status.p_item );
855 else if( p_playlist->status.p_node != NULL )
857 item = FindItem( root, p_playlist->status.p_node );
864 if( p_playlist->i_size )
866 SetCurrentItem( item );
869 int i_count = CountItems( treectrl->GetRootItem() );
871 if( i_count < p_playlist->i_size && !b_changed_view )
873 i_current_view = VIEW_CATEGORY;
874 b_changed_view = VLC_TRUE;
875 vlc_mutex_unlock( &p_playlist->object_lock );
877 vlc_mutex_lock( &p_playlist->object_lock );
879 else if( i_count != p_playlist->i_size )
881 statusbar->SetStatusText( wxString::Format( wxU(_(
882 "%i items in playlist (%i not shown)")),
884 p_playlist->i_size - i_count ) );
888 statusbar->SetStatusText( wxString::Format( wxU(_(
889 "%i items in playlist")),
890 p_playlist->i_size ), 0 );
893 vlc_mutex_unlock( &p_playlist->object_lock );
895 vlc_object_release( p_playlist );
900 void Playlist::ShowPlaylist( bool show )
902 if( show ) Rebuild();
906 /* This function is called on a regular basis */
907 void Playlist::UpdatePlaylist()
911 /* If the playlist isn't show there's no need to update it */
912 if( !IsShown() ) return;
914 if( this->b_need_update )
916 this->b_need_update = VLC_FALSE;
920 /* Updating the playing status every 0.5s is enough */
921 if( i_update_counter % 5 ) return;
923 playlist_t *p_playlist =
924 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
926 if( p_playlist == NULL )
931 vlc_object_release( p_playlist );
934 /*****************************************************************************
936 *****************************************************************************/
937 void Playlist::DeleteItem( int item_id )
939 playlist_t *p_playlist =
940 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
942 if( p_playlist == NULL )
947 playlist_LockDelete( p_playlist, item_id );
949 vlc_object_release( p_playlist );
952 void Playlist::DeleteNode( playlist_item_t *p_item )
954 playlist_t *p_playlist =
955 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
957 if( p_playlist == NULL )
962 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE );
964 vlc_object_release( p_playlist );
968 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
973 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
979 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
980 { _("PLS file"), "*.pls", "export-pls" }};
982 wxString filter = wxT("");
984 playlist_t * p_playlist =
985 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
992 if( p_playlist->i_size == 0 )
994 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
995 wxICON_WARNING | wxOK, this );
996 vlc_object_release( p_playlist );
1000 for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
1002 filter.Append( wxU(formats[i].psz_desc) );
1003 filter.Append( wxT("|") );
1004 filter.Append( wxU(formats[i].psz_filter) );
1005 filter.Append( wxT("|") );
1007 wxFileDialog dialog( this, wxU(_("Save playlist")),
1008 wxT(""), wxT(""), filter, wxSAVE );
1010 if( dialog.ShowModal() == wxID_OK )
1012 if( dialog.GetPath().mb_str() )
1014 playlist_Export( p_playlist, dialog.GetPath().mb_str(),
1015 formats[dialog.GetFilterIndex()].psz_module );
1019 vlc_object_release( p_playlist );
1023 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
1025 playlist_t *p_playlist =
1026 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1028 if( p_playlist == NULL )
1033 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
1034 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
1036 if( dialog.ShowModal() == wxID_OK )
1038 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
1041 vlc_object_release( p_playlist );
1044 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
1046 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
1050 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
1052 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
1056 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
1058 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
1062 /********************************************************************
1064 ********************************************************************/
1065 void Playlist::OnSort( wxCommandEvent& event )
1067 playlist_t *p_playlist =
1068 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1070 PlaylistItem *p_wxitem;
1071 p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
1073 if( p_playlist == NULL )
1077 vlc_mutex_lock( &p_playlist->object_lock );
1078 switch( event.GetId() )
1080 case SortTitle_Event:
1081 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1082 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1084 case RSortTitle_Event:
1085 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1086 SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
1088 vlc_mutex_unlock( &p_playlist->object_lock );
1090 vlc_object_release( p_playlist );
1094 /**********************************************************************
1095 * Search functions (user)
1096 **********************************************************************/
1097 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
1099 search_button->SetDefault();
1102 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
1104 wxString search_string = search_text->GetValue();
1106 vlc_bool_t pb_found = VLC_FALSE;
1108 wxTreeItemId found =
1109 FindItemByName( treectrl->GetRootItem(), search_string,
1110 search_current, &pb_found );
1114 search_current = found;
1115 treectrl->SelectItem( found, true );
1120 search_current = dummy;
1121 found = FindItemByName( treectrl->GetRootItem(), search_string,
1122 search_current, &pb_found );
1125 search_current = found;
1126 treectrl->SelectItem( found, true );
1131 /**********************************************************************
1132 * Selection functions
1133 **********************************************************************/
1134 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1138 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1143 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1147 /**********************************************************************
1148 * Playlist mode functions
1149 **********************************************************************/
1150 void Playlist::OnRandom( wxCommandEvent& event )
1153 val.b_bool = event.IsChecked();
1154 playlist_t *p_playlist =
1155 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1157 if( p_playlist == NULL )
1161 var_Set( p_playlist, "random", val);
1162 vlc_object_release( p_playlist );
1165 void Playlist::OnLoop( wxCommandEvent& event )
1168 val.b_bool = event.IsChecked();
1169 playlist_t *p_playlist =
1170 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1172 if( p_playlist == NULL )
1176 var_Set( p_playlist, "loop", val);
1177 vlc_object_release( p_playlist );
1180 void Playlist::OnRepeat( wxCommandEvent& event )
1183 val.b_bool = event.IsChecked();
1184 playlist_t *p_playlist =
1185 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1187 if( p_playlist == NULL )
1191 var_Set( p_playlist, "repeat", val);
1192 vlc_object_release( p_playlist );
1195 /********************************************************************
1197 ********************************************************************/
1198 void Playlist::OnActivateItem( wxTreeEvent& event )
1200 playlist_item_t *p_item,*p_node;
1201 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1202 VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1204 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1206 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1208 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1210 if( p_playlist == NULL )
1215 if( p_wxitem->p_item->i_children == -1 )
1217 p_node = p_wxparent->p_item;
1218 p_item = p_wxitem->p_item;
1222 p_node = p_wxitem->p_item;
1223 if( p_wxitem->p_item->i_children > 0 &&
1224 p_wxitem->p_item->pp_children[0]->i_children == -1)
1226 p_item = p_wxitem->p_item->pp_children[0];
1234 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1237 vlc_object_release( p_playlist );
1240 void Playlist::OnKeyDown( wxTreeEvent& event )
1242 long keycode = event.GetKeyCode();
1243 /* Delete selected items */
1244 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1246 /* We send a dummy event */
1247 OnDeleteSelection( event );
1251 void Playlist::OnEnDis( wxCommandEvent& event )
1253 playlist_t *p_playlist =
1254 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1256 if( p_playlist == NULL )
1260 msg_Warn( p_intf, "not implemented" );
1261 vlc_object_release( p_playlist );
1264 /**********************************************************************
1266 **********************************************************************/
1268 void Playlist::OnMenuOpen( wxMenuEvent& event)
1270 #if defined( __WXMSW__ )
1271 # define GetEventObject GetMenu
1274 if( event.GetEventObject() == p_view_menu )
1276 p_view_menu = ViewMenu();
1278 #if defined( __WXMSW__ )
1279 # undef GetEventObject
1283 void Playlist::OnMenuEvent( wxCommandEvent& event )
1285 playlist_t *p_playlist =
1286 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1288 if( p_playlist == NULL )
1293 if( event.GetId() < FirstView_Event )
1296 vlc_object_release( p_playlist );
1299 else if( event.GetId() < LastView_Event )
1302 int i_new_view = event.GetId() - FirstView_Event;
1304 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1306 if( p_view != NULL )
1308 b_changed_view = VLC_TRUE;
1309 i_current_view = i_new_view;
1310 playlist_ViewUpdate( p_playlist, i_new_view );
1312 vlc_object_release( p_playlist );
1315 else if( i_new_view >= VIEW_FIRST_SORTED &&
1316 i_new_view <= VIEW_LAST_SORTED )
1318 b_changed_view = VLC_TRUE;
1319 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1320 playlist_ViewUpdate( p_playlist, i_new_view );
1322 i_current_view = i_new_view;
1327 else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1329 if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1330 pp_sds[event.GetId() - FirstSD_Event] ) )
1332 playlist_ServicesDiscoveryAdd( p_playlist,
1333 pp_sds[event.GetId() - FirstSD_Event] );
1337 playlist_ServicesDiscoveryRemove( p_playlist,
1338 pp_sds[event.GetId() - FirstSD_Event] );
1341 vlc_object_release( p_playlist );
1344 wxMenu * Playlist::ViewMenu()
1346 playlist_t *p_playlist =
1347 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1349 if( p_playlist == NULL )
1356 p_view_menu = new wxMenu;
1360 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1363 wxMenuItem *item = node->GetData();
1364 node = node->GetNext();
1365 p_view_menu->Delete( item );
1369 /* FIXME : have a list of "should have" views */
1370 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1371 wxU(_("By category") ) );
1372 p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1373 wxU(_("Manually added") ) );
1374 p_view_menu->Append( FirstView_Event + VIEW_ALL,
1375 wxU(_("All items, unsorted") ) );
1376 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1377 wxU(_("Sorted by author") ) );
1379 vlc_object_release( p_playlist);
1384 wxMenu *Playlist::SDMenu()
1387 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1388 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1393 p_sd_menu = new wxMenu;
1395 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1399 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1401 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1403 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1405 p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1406 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1407 ( p_parser->psz_shortname ?
1408 p_parser->psz_shortname :p_parser->psz_object_name)) );
1410 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1411 p_parser->psz_object_name ) )
1413 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1416 INSERT_ELEM( (void**)pp_sds, i_number, i_number,
1417 (void*)p_parser->psz_object_name );
1420 vlc_list_release( p_list );
1421 vlc_object_release( p_playlist );
1426 /*****************************************************************************
1427 * Popup management functions
1428 *****************************************************************************/
1429 void Playlist::OnPopup( wxContextMenuEvent& event )
1431 wxPoint pt = event.GetPosition();
1433 i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1434 if( i_popup_item.IsOk() )
1436 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1438 PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1439 treectrl->GetItemParent( i_popup_item ) );
1440 p_popup_item = p_wxitem->p_item;
1441 p_popup_parent = p_wxparent->p_item;
1442 treectrl->SelectItem( i_popup_item );
1443 if( p_popup_item->i_children == -1 )
1444 Playlist::PopupMenu( item_popup,
1445 ScreenToClient( wxGetMousePosition() ) );
1447 Playlist::PopupMenu( node_popup,
1448 ScreenToClient( wxGetMousePosition() ) );
1452 void Playlist::OnPopupPlay( wxMenuEvent& event )
1454 playlist_t *p_playlist =
1455 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1457 if( p_playlist == NULL )
1461 if( p_popup_item != NULL )
1463 if( p_popup_item->i_children > -1 )
1465 if( event.GetId() == PopupPlay_Event &&
1466 p_popup_item->i_children > 0 )
1468 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1469 i_current_view, p_popup_item,
1470 p_popup_item->pp_children[0] );
1474 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1475 i_current_view, p_popup_item, NULL );
1480 if( event.GetId() == PopupPlay_Event )
1482 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1483 i_current_view, p_popup_parent,
1488 vlc_object_release( p_playlist );
1491 void Playlist::OnPopupPreparse( wxMenuEvent& event )
1493 playlist_t *p_playlist =
1494 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1496 if( p_playlist == NULL )
1500 Preparse( p_playlist );
1501 vlc_object_release( p_playlist );
1504 void Playlist::Preparse( playlist_t *p_playlist )
1506 if( p_popup_item != NULL )
1508 if( p_popup_item->i_children == -1 )
1510 playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1515 playlist_item_t *p_parent = p_popup_item;
1516 for( i = 0; i< p_parent->i_children ; i++ )
1519 i_popup_item = FindItem( treectrl->GetRootItem(),
1520 p_parent->pp_children[i] );
1521 p_popup_item = p_parent->pp_children[i];
1522 Preparse( p_playlist );
1528 void Playlist::OnPopupDel( wxMenuEvent& event )
1530 PlaylistItem *p_wxitem;
1532 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1534 if( p_wxitem->p_item->i_children == -1 )
1536 DeleteItem( p_wxitem->p_item->input.i_id );
1540 DeleteNode( p_wxitem->p_item );
1544 void Playlist::OnPopupSort( wxMenuEvent& event )
1546 PlaylistItem *p_wxitem;
1548 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1550 if( p_wxitem->p_item->i_children >= 0 )
1552 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1553 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1557 vlc_mutex_lock( &p_playlist->object_lock );
1558 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1559 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1560 vlc_mutex_unlock( &p_playlist->object_lock );
1562 treectrl->DeleteChildren( i_popup_item );
1563 UpdateNodeChildren( p_playlist, p_wxitem->p_item, i_popup_item );
1565 vlc_object_release( p_playlist );
1570 void Playlist::OnPopupInfo( wxMenuEvent& event )
1574 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1575 if( iteminfo_dialog->ShowModal() == wxID_OK )
1577 UpdateItem( i_popup_item );
1579 delete iteminfo_dialog;
1584 /*****************************************************************************
1585 * Custom events management
1586 *****************************************************************************/
1587 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1589 switch( event.GetId() )
1591 case UpdateItem_Event:
1592 UpdateItem( event.GetInt() );
1594 case AppendItem_Event:
1595 AppendItem( event );
1597 case RemoveItem_Event:
1598 RemoveItem( event.GetInt() );
1603 /*****************************************************************************
1604 * PlaylistChanged: callback triggered by the intf-change playlist variable
1605 * We don't rebuild the playlist directly here because we don't want the
1606 * caller to block for a too long time.
1607 *****************************************************************************/
1608 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1609 vlc_value_t oval, vlc_value_t nval, void *param )
1611 Playlist *p_playlist_dialog = (Playlist *)param;
1612 p_playlist_dialog->b_need_update = VLC_TRUE;
1616 /*****************************************************************************
1617 * Next: callback triggered by the playlist-current playlist variable
1618 *****************************************************************************/
1619 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1620 vlc_value_t oval, vlc_value_t nval, void *param )
1622 Playlist *p_playlist_dialog = (Playlist *)param;
1624 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1625 event.SetInt( oval.i_int );
1626 p_playlist_dialog->AddPendingEvent( event );
1627 event.SetInt( nval.i_int );
1628 p_playlist_dialog->AddPendingEvent( event );
1633 /*****************************************************************************
1634 * ItemChanged: callback triggered by the item-change playlist variable
1635 *****************************************************************************/
1636 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1637 vlc_value_t old_val, vlc_value_t new_val, void *param )
1639 Playlist *p_playlist_dialog = (Playlist *)param;
1641 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1642 event.SetInt( new_val.i_int );
1643 p_playlist_dialog->AddPendingEvent( event );
1647 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1648 vlc_value_t old_val, vlc_value_t new_val, void *param )
1650 Playlist *p_playlist_dialog = (Playlist *)param;
1652 wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1653 event.SetInt( new_val.i_int );
1654 p_playlist_dialog->AddPendingEvent( event );
1659 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1660 vlc_value_t oval, vlc_value_t nval, void *param )
1662 Playlist *p_playlist_dialog = (Playlist *)param;
1664 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1665 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1667 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1668 event.SetClientData( (void *)p_add );
1669 p_playlist_dialog->AddPendingEvent( event );