1 /*****************************************************************************
2 * playlist.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
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" )
56 /* Callback prototype */
57 static int PlaylistChanged( vlc_object_t *, const char *,
58 vlc_value_t, vlc_value_t, void * );
59 static int PlaylistNext( vlc_object_t *, const char *,
60 vlc_value_t, vlc_value_t, void * );
61 static int ItemChanged( vlc_object_t *, const char *,
62 vlc_value_t, vlc_value_t, void * );
63 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
64 vlc_value_t oval, vlc_value_t nval, void *param );
65 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
66 vlc_value_t oval, vlc_value_t nval, void *param );
68 /*****************************************************************************
70 *****************************************************************************/
72 /* IDs for the controls and the menu commands */
87 DeleteSelection_Event,
105 Browse_Event, /* For export playlist */
112 MenuDummy_Event = wxID_HIGHEST + 999,
114 FirstView_Event = wxID_HIGHEST + 1000,
115 LastView_Event = wxID_HIGHEST + 1100,
117 FirstSD_Event = wxID_HIGHEST + 2000,
118 LastSD_Event = wxID_HIGHEST + 2100,
121 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
123 BEGIN_EVENT_TABLE(Playlist, wxFrame)
124 EVT_SIZE(Playlist::OnSize)
127 EVT_MENU(AddFile_Event, Playlist::OnAddFile)
128 EVT_MENU(AddDir_Event, Playlist::OnAddDir)
129 EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
130 EVT_MENU(Close_Event, Playlist::OnMenuClose)
131 EVT_MENU(Open_Event, Playlist::OnOpen)
132 EVT_MENU(Save_Event, Playlist::OnSave)
134 EVT_MENU(SortTitle_Event, Playlist::OnSort)
135 EVT_MENU(RSortTitle_Event, Playlist::OnSort)
137 EVT_MENU(Randomize_Event, Playlist::OnSort)
139 EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
141 EVT_MENU_OPEN( Playlist::OnMenuOpen )
142 EVT_MENU( -1, Playlist::OnMenuEvent )
144 EVT_TOOL(Random_Event, Playlist::OnRandom)
145 EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
146 EVT_TOOL(Loop_Event, Playlist::OnLoop)
149 EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
150 EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
151 EVT_MENU( PopupPreparse_Event, Playlist::OnPopupPreparse)
152 EVT_MENU( PopupSort_Event, Playlist::OnPopupSort)
153 EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
154 EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
156 /* Tree control events */
157 EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
158 EVT_TREE_KEY_DOWN( -1, Playlist::OnKeyDown )
160 EVT_CONTEXT_MENU( Playlist::OnPopup )
163 EVT_BUTTON( Search_Event, Playlist::OnSearch)
164 EVT_BUTTON( Save_Event, Playlist::OnSave)
166 /*EVT_TEXT( SearchText_Event, Playlist::OnSearchTextChange )*/
167 EVT_TEXT_ENTER( SearchText_Event, Playlist::OnSearch )
170 EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
172 /* Special events : we don't want to destroy the window when the user
174 EVT_CLOSE(Playlist::OnClose)
177 /*****************************************************************************
179 ****************************************************************************/
180 class PlaylistItem : public wxTreeItemData
183 PlaylistItem( playlist_item_t *p_item ) : wxTreeItemData()
185 i_id = p_item->input.i_id;
189 friend class Playlist;
192 /*****************************************************************************
194 *****************************************************************************/
195 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
196 wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
197 wxSize(500,300), wxDEFAULT_FRAME_STYLE )
201 /* Initializations */
203 i_update_counter = 0;
204 i_sort_mode = MODE_NONE;
205 b_need_update = VLC_FALSE;
206 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
208 if( p_playlist == NULL )
213 SetIcon( *p_intf->p_sys->p_icon );
216 p_sd_menu = SDMenu();
218 i_current_view = VIEW_CATEGORY;
219 b_changed_view = VLC_FALSE;
223 i_duration_sorted = 0;
225 var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
226 var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
227 var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
229 /* Create our "Manage" menu */
230 wxMenu *manage_menu = new wxMenu;
231 manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
232 manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
233 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
234 manage_menu->AppendSeparator();
235 manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
237 manage_menu->AppendSeparator();
238 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
239 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
240 manage_menu->AppendSeparator();
241 manage_menu->Append( Close_Event, wxU(_("&Close")) );
243 /* Create our "Sort" menu */
244 wxMenu *sort_menu = new wxMenu;
245 sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
246 sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
247 sort_menu->AppendSeparator();
248 sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
250 /* Create our "Selection" menu */
251 wxMenu *selection_menu = new wxMenu;
252 selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
254 /* Create our "View" menu */
257 /* Append the freshly created menus to the menu bar */
258 wxMenuBar *menubar = new wxMenuBar();
259 menubar->Append( manage_menu, wxU(_("&Manage")) );
260 menubar->Append( sort_menu, wxU(_("S&ort")) );
261 menubar->Append( selection_menu, wxU(_("&Selection")) );
262 menubar->Append( p_view_menu, wxU(_("&View items") ) );
264 /* Attach the menu bar to the frame */
265 SetMenuBar( menubar );
267 /* Create the popup menu */
268 node_popup = new wxMenu;
269 node_popup->Append( PopupPlay_Event, wxU(_("Play")) );
270 node_popup->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
271 node_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
272 node_popup->Append( PopupSort_Event, wxU(_("Sort this branch")) );
273 node_popup->Append( PopupDel_Event, wxU(_("Delete")) );
274 node_popup->Append( PopupInfo_Event, wxU(_("Info")) );
276 item_popup = new wxMenu;
277 item_popup->Append( PopupPlay_Event, wxU(_("Play")) );
278 item_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
279 item_popup->Append( PopupDel_Event, wxU(_("Delete")) );
280 item_popup->Append( PopupInfo_Event, wxU(_("Info")) );
282 /* Create a panel to put everything in */
283 wxPanel *playlist_panel = new wxPanel( this, -1 );
284 playlist_panel->SetAutoLayout( TRUE );
286 /* Create the toolbar */
288 CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT );
290 /* Create the random tool */
291 toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
292 wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
293 wxU(_(HELP_SHUFFLE) ) );
294 var_Get( p_intf, "random", &val );
295 toolbar->ToggleTool( Random_Event, val.b_bool );
297 /* Create the Loop tool */
298 toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
299 wxBitmap( loop_xpm), wxITEM_CHECK,
300 wxU(_(HELP_LOOP ) ) );
301 var_Get( p_intf, "loop", &val );
302 toolbar->ToggleTool( Loop_Event, val.b_bool );
304 /* Create the Repeat one checkbox */
305 toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
306 wxBitmap( repeat_xpm), wxITEM_CHECK,
307 wxU(_(HELP_REPEAT ) ) );
308 var_Get( p_intf, "repeat", &val );
309 toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
311 /* Create the Search Textbox */
312 search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
313 wxDefaultPosition, wxSize(100, -1),
316 /* Create the search button */
317 search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
319 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
320 wxSize(16, 16), wxBORDER_NONE ) );
321 toolbar->AddControl( search_text );
322 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
323 wxSize(5, 5), wxBORDER_NONE ) );
324 toolbar->AddControl( search_button );
325 search_button->SetDefault();
328 /* Create the tree */
329 treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
330 wxDefaultPosition, wxDefaultSize,
331 wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
333 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
334 wxTR_MULTIPLE | wxTR_EXTENDED );
336 /* Create image list */
337 wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
339 /* FIXME: absolutely needs to be in the right order FIXME */
340 p_images->Add( wxIcon( type_unknown_xpm ) );
341 p_images->Add( wxIcon( type_afile_xpm ) );
342 p_images->Add( wxIcon( type_vfile_xpm ) );
343 p_images->Add( wxIcon( type_directory_xpm ) );
344 p_images->Add( wxIcon( type_disc_xpm ) );
345 p_images->Add( wxIcon( type_cdda_xpm ) );
346 p_images->Add( wxIcon( type_card_xpm ) );
347 p_images->Add( wxIcon( type_net_xpm ) );
348 p_images->Add( wxIcon( type_playlist_xpm ) );
349 p_images->Add( wxIcon( type_node_xpm ) );
350 treectrl->AssignImageList( p_images );
352 treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
354 /* Reduce font size */
355 wxFont font= treectrl->GetFont();
356 font.SetPointSize(9);
357 treectrl->SetFont( font );
359 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
360 panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
361 panel_sizer->Layout();
363 playlist_panel->SetSizerAndFit( panel_sizer );
365 int pi_widths[1] = { -1 };
366 statusbar = CreateStatusBar( 1 );
367 statusbar->SetStatusWidths( 1, pi_widths );
369 #if wxUSE_DRAG_AND_DROP
370 /* Associate drop targets with the playlist */
371 SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
377 /* We want to be noticed of playlist changes */
379 /* Some global changes happened -> Rebuild all */
380 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
382 /* We went to the next item */
383 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
385 /* One item has been updated */
386 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
388 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
389 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
391 /* Update the playlist */
396 Playlist::~Playlist()
401 if( p_playlist == NULL )
406 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
407 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
408 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
409 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
410 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
411 vlc_object_release( p_playlist );
414 /**********************************************************************
416 **********************************************************************/
419 void Playlist::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
421 wxTreeItemIdValue cookie;
423 for( int i = 0; i< p_node->i_children ; i++ )
427 child = treectrl->GetFirstChild( node, cookie);
431 child = treectrl->GetNextChild( node, cookie );
436 /* Not enough children */
437 CreateNode( p_node->pp_children[i], node );
438 /* Keep the tree pointer up to date */
439 child = treectrl->GetNextChild( node, cookie );
442 treectrl->SetItemImage( node, p_node->input.i_type );
446 /* Creates the node p_node as last child of parent */
447 void Playlist::CreateNode( playlist_item_t *p_node, wxTreeItemId parent )
450 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
451 -1,-1, new PlaylistItem( p_node ) );
452 treectrl->SetItemImage( node, p_node->input.i_type );
454 UpdateNodeChildren( p_node, node );
457 /* Update all children (recursively) of this node */
458 void Playlist::UpdateNodeChildren( playlist_item_t *p_node,
462 for( int i = 0; i< p_node->i_children ; i++ )
464 /* Append the item */
465 if( p_node->pp_children[i]->i_children == -1 )
468 treectrl->AppendItem( node,
469 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
470 new PlaylistItem( p_node->pp_children[i]) );
472 UpdateTreeItem( item );
476 CreateNode( p_node->pp_children[i], node );
481 /* Update an item in the tree */
482 void Playlist::UpdateTreeItem( wxTreeItemId item )
484 if( ! item.IsOk() ) return;
486 wxTreeItemData *p_data = treectrl->GetItemData( item );
487 if( !p_data ) return;
489 LockPlaylist( p_intf->p_sys, p_playlist );
490 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
491 ((PlaylistItem *)p_data)->i_id );
492 if( !p_item ) return;
495 wxString duration = wxU( "" );
496 char *psz_author = vlc_input_item_GetInfo( &p_item->input,
497 _("Meta-information"),
499 if( psz_author == NULL )
501 char psz_duration[MSTRTIME_MAX_SIZE];
502 mtime_t dur = p_item->input.i_duration;
506 secstotimestr( psz_duration, dur/1000000 );
507 duration.Append( wxU( " ( " ) + wxString( wxU( psz_duration ) ) +
511 if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
513 msg.Printf( wxString( wxU( p_item->input.psz_name ) ) + duration );
517 msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
518 wxString(wxU(p_item->input.psz_name)) + duration );
521 treectrl->SetItemText( item , msg );
522 treectrl->SetItemImage( item, p_item->input.i_type );
524 if( p_playlist->status.p_item == p_item )
526 treectrl->SetItemBold( item, true );
527 treectrl->EnsureVisible( item );
531 treectrl->SetItemBold( item, false );
533 UnlockPlaylist( p_intf->p_sys, p_playlist );
536 /* Process a AppendIt em request */
537 void Playlist::AppendItem( wxCommandEvent& event )
539 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
540 playlist_item_t *p_item = NULL;
542 wxTreeItemId item,node;
544 if( p_add->i_view != i_current_view )
549 node = FindItem( treectrl->GetRootItem(), p_add->i_node );
555 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
559 item = treectrl->AppendItem( node,
560 wxL2U( p_item->input.psz_name ), -1,-1,
561 new PlaylistItem( p_item ) );
562 treectrl->SetItemImage( item, p_item->input.i_type );
564 if( item.IsOk() && p_item->i_children == -1 )
566 UpdateTreeItem( item );
570 int i_count = CountItems( treectrl->GetRootItem());
571 if( i_count != p_playlist->i_size )
573 statusbar->SetStatusText( wxString::Format( wxU(_(
574 "%i items in playlist (%i not shown)")),
576 p_playlist->i_size - i_count ) );
577 if( !b_changed_view )
579 i_current_view = VIEW_CATEGORY;
580 b_changed_view = VLC_TRUE;
581 b_need_update = VLC_TRUE;
586 statusbar->SetStatusText( wxString::Format( wxU(_(
587 "%i items in playlist")),
588 p_playlist->i_size ), 0 );
594 /* Process a updateitem request */
595 void Playlist::UpdateItem( int i )
597 if( i < 0 ) return; /* Sanity check */
599 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
603 UpdateTreeItem( item );
607 void Playlist::RemoveItem( int i )
609 if( i <= 0 ) return; /* Sanity check */
610 if( i == i_saved_id ) i_saved_id = -1;
612 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
616 treectrl->Delete( item );
621 /**********************************************************************
622 * Search functions (internal)
623 **********************************************************************/
625 /* Find a wxItem from a playlist id */
626 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
628 wxTreeItemIdValue cookie;
629 PlaylistItem *p_wxcurrent;
631 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
634 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
641 if( i_saved_id == i_id )
643 return saved_tree_item;
652 if( p_wxcurrent->i_id == i_id )
655 saved_tree_item = root;
661 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
662 if( p_wxcurrent->i_id == i_id )
665 saved_tree_item = item;
668 if( treectrl->ItemHasChildren( item ) )
670 wxTreeItemId search = FindItem( item, i_id );
674 saved_tree_item = search;
678 item = treectrl->GetNextChild( root, cookie );
685 int Playlist::CountItems( wxTreeItemId root )
687 wxTreeItemIdValue cookie;
689 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
693 if( treectrl->ItemHasChildren( item ) )
695 count += CountItems( item );
699 playlist_item_t *p_item;
700 LockPlaylist( p_intf->p_sys, p_playlist );
701 p_item = playlist_ItemGetById( p_playlist, ((PlaylistItem *)treectrl->GetItemData( item ))->i_id );
702 if( p_item && p_item->i_children == -1 )
704 UnlockPlaylist( p_intf->p_sys, p_playlist );
706 item = treectrl->GetNextChild( root, cookie );
711 /* Find a wxItem from a name (from current) */
712 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
714 wxTreeItemIdValue cookie;
716 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
721 if( treectrl->GetItemText( item).Lower().Contains(
722 search_string.Lower() ) )
724 if( !current.IsOk() || *pb_current_found == VLC_TRUE )
728 else if( current.IsOk() && item == current )
730 *pb_current_found = VLC_TRUE;
733 if( treectrl->ItemHasChildren( item ) )
735 wxTreeItemId search = FindItemByName( item, search_string, current,
742 item = treectrl->GetNextChild( root, cookie);
749 /**********************************************************************
750 * Rebuild the playlist
751 **********************************************************************/
752 void Playlist::Rebuild( vlc_bool_t b_root )
754 playlist_view_t *p_view;
756 /* We can remove the callbacks before locking, anyway, we won't
760 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
761 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
762 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
763 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
764 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
766 /* ...and rebuild it */
767 LockPlaylist( p_intf->p_sys, p_playlist );
771 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
773 /* HACK we should really get new*/
774 treectrl->DeleteAllItems();
775 treectrl->AddRoot( wxU(_("root" )), -1, -1,
776 new PlaylistItem( p_view->p_root) );
778 wxTreeItemId root = treectrl->GetRootItem();
779 UpdateNode( p_view->p_root, root );
781 int i_count = CountItems( treectrl->GetRootItem() );
783 if( i_count < p_playlist->i_size && !b_changed_view )
785 i_current_view = VIEW_CATEGORY;
786 b_changed_view = VLC_TRUE;
787 Rebuild( VLC_FALSE );
789 else if( i_count != p_playlist->i_size )
791 statusbar->SetStatusText( wxString::Format( wxU(_(
792 "%i items in playlist (%i not shown)")),
794 p_playlist->i_size - i_count ) );
798 statusbar->SetStatusText( wxString::Format( wxU(_(
799 "%i items in playlist")),
800 p_playlist->i_size ), 0 );
805 /* Put callbacks back online */
806 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
807 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
808 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
809 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
810 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
812 UnlockPlaylist( p_intf->p_sys, p_playlist );
818 void Playlist::ShowPlaylist( bool show )
820 if( show ) Rebuild( VLC_TRUE );
824 /* This function is called on a regular basis */
825 void Playlist::UpdatePlaylist()
829 /* If the playlist isn't show there's no need to update it */
830 if( !IsShown() ) return;
832 if( this->b_need_update )
834 this->b_need_update = VLC_FALSE;
838 /* Updating the playing status every 0.5s is enough */
839 if( i_update_counter % 5 ) return;
842 /*****************************************************************************
844 *****************************************************************************/
845 void Playlist::DeleteTreeItem( wxTreeItemId item )
847 PlaylistItem *p_wxitem;
848 playlist_item_t *p_item;
849 p_wxitem = (PlaylistItem *)treectrl->GetItemData( item );
851 LockPlaylist( p_intf->p_sys, p_playlist );
852 p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
856 UnlockPlaylist( p_intf->p_sys, p_playlist );
860 if( p_item->i_children == -1 )
862 UnlockPlaylist( p_intf->p_sys, p_playlist );
863 DeleteItem( p_item->input.i_id );
867 UnlockPlaylist( p_intf->p_sys, p_playlist );
868 DeleteNode( p_item );
875 void Playlist::DeleteItem( int item_id )
877 playlist_LockDelete( p_playlist, item_id );
880 void Playlist::DeleteNode( playlist_item_t *p_item )
882 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE , VLC_FALSE );
885 void Playlist::OnMenuClose( wxCommandEvent& event )
891 void Playlist::OnClose( wxCloseEvent& WXUNUSED(event) )
896 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
902 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" }};
904 wxString filter = wxT("");
906 if( p_playlist->i_size == 0 )
908 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
909 wxICON_WARNING | wxOK, this );
913 for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
915 filter.Append( wxU(formats[i].psz_desc) );
916 filter.Append( wxT("|") );
917 filter.Append( wxU(formats[i].psz_filter) );
918 filter.Append( wxT("|") );
920 wxFileDialog dialog( this, wxU(_("Save playlist")),
921 wxT(""), wxT(""), filter, wxSAVE );
923 if( dialog.ShowModal() == wxID_OK )
925 if( dialog.GetPath().mb_str() )
927 playlist_Export( p_playlist, dialog.GetPath().mb_str(),
928 formats[dialog.GetFilterIndex()].psz_module );
934 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
936 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
937 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
939 if( dialog.ShowModal() == wxID_OK )
941 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
945 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
947 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
951 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
953 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
957 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
959 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
963 /********************************************************************
965 ********************************************************************/
966 void Playlist::OnSort( wxCommandEvent& event )
968 PlaylistItem *p_wxitem;
969 p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
971 LockPlaylist( p_intf->p_sys, p_playlist );
972 switch( event.GetId() )
974 case SortTitle_Event:
975 playlist_RecursiveNodeSort( p_playlist,
976 playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
977 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
979 case RSortTitle_Event:
980 playlist_RecursiveNodeSort( p_playlist,
981 playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
982 SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
984 UnlockPlaylist( p_intf->p_sys, p_playlist );
989 /**********************************************************************
990 * Search functions (user)
991 **********************************************************************/
992 /*void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
994 search_button->SetDefault();
997 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
999 wxString search_string = search_text->GetValue();
1001 vlc_bool_t pb_found = VLC_FALSE;
1003 wxTreeItemId found =
1004 FindItemByName( treectrl->GetRootItem(), search_string,
1005 search_current, &pb_found );
1010 search_current = dummy;
1011 found = FindItemByName( treectrl->GetRootItem(), search_string,
1012 search_current, &pb_found );
1017 search_current = found;
1018 treectrl->EnsureVisible( found );
1019 treectrl->SelectItem( found, true );
1023 /**********************************************************************
1024 * Selection functions
1025 **********************************************************************/
1026 void Playlist::RecursiveDeleteSelection( wxTreeItemId root )
1028 wxTreeItemIdValue cookie;
1029 wxTreeItemId child = treectrl->GetFirstChild( root, cookie );
1030 while( child.IsOk() )
1032 if( treectrl->ItemHasChildren( child ) )
1034 RecursiveDeleteSelection( child );
1035 if( treectrl->IsSelected(child ) ) DeleteTreeItem( child );
1037 else if( treectrl->IsSelected( child ) )
1038 DeleteTreeItem( child );
1039 child = treectrl->GetNextChild( root, cookie );
1043 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1045 RecursiveDeleteSelection( treectrl->GetRootItem() );
1048 /**********************************************************************
1049 * Playlist mode functions
1050 **********************************************************************/
1051 void Playlist::OnRandom( wxCommandEvent& event )
1054 val.b_bool = event.IsChecked();
1055 var_Set( p_playlist, "random", val);
1058 void Playlist::OnLoop( wxCommandEvent& event )
1061 val.b_bool = event.IsChecked();
1062 var_Set( p_playlist, "loop", val);
1065 void Playlist::OnRepeat( wxCommandEvent& event )
1068 val.b_bool = event.IsChecked();
1069 var_Set( p_playlist, "repeat", val);
1072 /********************************************************************
1074 ********************************************************************/
1075 void Playlist::OnActivateItem( wxTreeEvent& event )
1077 playlist_item_t *p_item,*p_node,*p_item2,*p_node2;
1079 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1081 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1083 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1085 LockPlaylist( p_intf->p_sys, p_playlist );
1087 if( !( p_wxitem && p_wxparent ) ) return;
1089 p_item2 = playlist_ItemGetById(p_playlist, p_wxitem->i_id);
1090 p_node2 = playlist_ItemGetById(p_playlist, p_wxparent->i_id);
1091 if( p_item2 && p_item2->i_children == -1 )
1099 if( p_node && p_node->i_children > 0 &&
1100 p_node->pp_children[0]->i_children == -1)
1102 p_item = p_node->pp_children[0];
1110 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1112 UnlockPlaylist( p_intf->p_sys, p_playlist );
1115 void Playlist::OnKeyDown( wxTreeEvent& event )
1117 long keycode = event.GetKeyCode();
1118 /* Delete selected items */
1119 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1121 /* We send a dummy event */
1122 OnDeleteSelection( event );
1130 void Playlist::OnEnDis( wxCommandEvent& event )
1132 msg_Warn( p_intf, "not implemented" );
1135 /**********************************************************************
1137 **********************************************************************/
1139 void Playlist::OnMenuOpen( wxMenuEvent& event)
1141 #if defined( __WXMSW__ )
1142 # define GetEventObject GetMenu
1145 if( event.GetEventObject() == p_view_menu )
1147 p_view_menu = ViewMenu();
1149 #if defined( __WXMSW__ )
1150 # undef GetEventObject
1154 void Playlist::OnMenuEvent( wxCommandEvent& event )
1156 if( event.GetId() < FirstView_Event )
1161 else if( event.GetId() < LastView_Event )
1164 int i_new_view = event.GetId() - FirstView_Event;
1166 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1168 if( p_view != NULL )
1170 b_changed_view = VLC_TRUE;
1171 i_current_view = i_new_view;
1172 playlist_ViewUpdate( p_playlist, i_new_view );
1173 Rebuild( VLC_TRUE );
1176 else if( i_new_view >= VIEW_FIRST_SORTED &&
1177 i_new_view <= VIEW_LAST_SORTED )
1179 b_changed_view = VLC_TRUE;
1180 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1181 playlist_ViewUpdate( p_playlist, i_new_view );
1183 i_current_view = i_new_view;
1185 Rebuild( VLC_TRUE );
1188 else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1190 if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1191 pp_sds[event.GetId() - FirstSD_Event] ) )
1193 playlist_ServicesDiscoveryAdd( p_playlist,
1194 pp_sds[event.GetId() - FirstSD_Event] );
1198 //wxMutexGuiLeave();
1199 playlist_ServicesDiscoveryRemove( p_playlist,
1200 pp_sds[event.GetId() - FirstSD_Event] );
1201 //wxMutexGuiEnter();
1206 wxMenu * Playlist::ViewMenu()
1210 p_view_menu = new wxMenu;
1214 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1217 wxMenuItem *item = node->GetData();
1218 node = node->GetNext();
1219 p_view_menu->Delete( item );
1223 /* FIXME : have a list of "should have" views */
1224 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1225 wxU(_("Normal") ) );
1226 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1227 wxU(_("Sorted by artist") ) );
1232 wxMenu *Playlist::SDMenu()
1234 p_sd_menu = new wxMenu;
1236 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1240 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1242 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1244 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1247 if( i_number ) pp_sds = (char **)calloc( i_number, sizeof(void *) );
1250 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1252 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1254 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1256 p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1257 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1258 (p_parser->psz_shortname ?
1259 p_parser->psz_shortname : p_parser->psz_object_name) ) );
1261 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1262 p_parser->psz_object_name ) )
1264 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1267 pp_sds[i_number++] = p_parser->psz_object_name;
1270 vlc_list_release( p_list );
1275 /*****************************************************************************
1276 * Popup management functions
1277 *****************************************************************************/
1278 void Playlist::OnPopup( wxContextMenuEvent& event )
1280 wxPoint pt = event.GetPosition();
1281 playlist_item_t *p_item;
1283 i_wx_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1284 if( i_wx_popup_item.IsOk() )
1286 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1288 PlaylistItem *p_wxparent= (PlaylistItem *)treectrl->GetItemData(
1289 treectrl->GetItemParent( i_wx_popup_item ) );
1290 i_popup_item = p_wxitem->i_id;
1291 i_popup_parent = p_wxparent->i_id;
1292 treectrl->SelectItem( i_wx_popup_item );
1294 LockPlaylist( p_intf->p_sys, p_playlist );
1295 p_item = playlist_ItemGetById( p_playlist, i_popup_item );
1299 UnlockPlaylist( p_intf->p_sys, p_playlist );
1302 if( p_item->i_children == -1 )
1304 UnlockPlaylist( p_intf->p_sys, p_playlist );
1305 Playlist::PopupMenu( item_popup,
1306 ScreenToClient( wxGetMousePosition() ) );
1310 UnlockPlaylist( p_intf->p_sys, p_playlist );
1311 Playlist::PopupMenu( node_popup,
1312 ScreenToClient( wxGetMousePosition() ) );
1317 void Playlist::OnPopupPlay( wxCommandEvent& event )
1319 playlist_item_t *p_popup_item, *p_popup_parent;
1320 LockPlaylist( p_intf->p_sys, p_playlist );
1321 p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1322 p_popup_parent = playlist_ItemGetById( p_playlist, i_popup_parent );
1323 if( p_popup_item != NULL )
1325 if( p_popup_item->i_children > -1 )
1327 if( event.GetId() == PopupPlay_Event &&
1328 p_popup_item->i_children > 0 )
1330 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1331 i_current_view, p_popup_item,
1332 p_popup_item->pp_children[0] );
1336 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1337 i_current_view, p_popup_item, NULL );
1342 if( event.GetId() == PopupPlay_Event )
1344 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1345 i_current_view, p_popup_parent,
1350 UnlockPlaylist( p_intf->p_sys, p_playlist );
1353 void Playlist::OnPopupPreparse( wxCommandEvent& event )
1358 void Playlist::Preparse()
1360 playlist_item_t *p_popup_item;
1361 LockPlaylist( p_intf->p_sys, p_playlist );
1362 p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1364 if( p_popup_item != NULL )
1366 if( p_popup_item->i_children == -1 )
1368 playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1373 playlist_item_t *p_parent = p_popup_item;
1374 for( i = 0; i< p_parent->i_children ; i++ )
1377 i_wx_popup_item = FindItem( treectrl->GetRootItem(),
1378 p_parent->pp_children[i]->input.i_id );
1379 i_popup_item = p_parent->pp_children[i]->input.i_id;
1384 UnlockPlaylist( p_intf->p_sys, p_playlist );
1387 void Playlist::OnPopupDel( wxCommandEvent& event )
1389 DeleteTreeItem( i_wx_popup_item );
1392 void Playlist::OnPopupSort( wxCommandEvent& event )
1394 PlaylistItem *p_wxitem;
1395 playlist_item_t *p_item;
1397 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_wx_popup_item );
1398 LockPlaylist( p_intf->p_sys, p_playlist );
1400 p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
1401 if( p_item->i_children >= 0 )
1403 playlist_RecursiveNodeSort( p_playlist, p_item,
1404 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1406 treectrl->DeleteChildren( i_wx_popup_item );
1408 UpdateNodeChildren( p_item, i_wx_popup_item );
1411 UnlockPlaylist( p_intf->p_sys, p_playlist );
1414 void Playlist::OnPopupInfo( wxCommandEvent& event )
1416 LockPlaylist( p_intf->p_sys, p_playlist );
1417 playlist_item_t *p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1420 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1421 if( iteminfo_dialog->ShowModal() == wxID_OK )
1423 UpdateItem( i_wx_popup_item );
1425 delete iteminfo_dialog;
1427 UnlockPlaylist( p_intf->p_sys, p_playlist );
1431 /*****************************************************************************
1432 * Custom events management
1433 *****************************************************************************/
1434 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1436 switch( event.GetId() )
1438 case UpdateItem_Event:
1439 UpdateItem( event.GetInt() );
1441 case AppendItem_Event:
1442 AppendItem( event );
1444 case RemoveItem_Event:
1445 RemoveItem( event.GetInt() );
1450 /*****************************************************************************
1451 * PlaylistChanged: callback triggered by the intf-change playlist variable
1452 * We don't rebuild the playlist directly here because we don't want the
1453 * caller to block for a too long time.
1454 *****************************************************************************/
1455 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1456 vlc_value_t oval, vlc_value_t nval, void *param )
1458 Playlist *p_playlist_dialog = (Playlist *)param;
1459 p_playlist_dialog->b_need_update = VLC_TRUE;
1463 /*****************************************************************************
1464 * Next: callback triggered by the playlist-current playlist variable
1465 *****************************************************************************/
1466 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1467 vlc_value_t oval, vlc_value_t nval, void *param )
1469 Playlist *p_playlist_dialog = (Playlist *)param;
1471 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1472 event.SetInt( oval.i_int );
1473 p_playlist_dialog->AddPendingEvent( event );
1474 event.SetInt( nval.i_int );
1475 p_playlist_dialog->AddPendingEvent( event );
1480 /*****************************************************************************
1481 * ItemChanged: callback triggered by the item-change playlist variable
1482 *****************************************************************************/
1483 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1484 vlc_value_t old_val, vlc_value_t new_val, void *param )
1486 Playlist *p_playlist_dialog = (Playlist *)param;
1488 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1489 event.SetInt( new_val.i_int );
1490 p_playlist_dialog->AddPendingEvent( event );
1494 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1495 vlc_value_t old_val, vlc_value_t new_val, void *param )
1497 Playlist *p_playlist_dialog = (Playlist *)param;
1499 wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1500 event.SetInt( new_val.i_int );
1501 p_playlist_dialog->AddPendingEvent( event );
1506 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1507 vlc_value_t oval, vlc_value_t nval, void *param )
1509 Playlist *p_playlist_dialog = (Playlist *)param;
1511 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1513 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1515 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1516 event.SetClientData( (void *)p_add );
1517 p_playlist_dialog->AddPendingEvent( event );