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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include "dialogs/playlist.hpp"
29 #include "dialogs/iteminfo.hpp"
30 #include "interface.hpp" // Needed for D&D - TODO: Split
32 #include "bitmaps/shuffle.xpm"
33 #include "bitmaps/repeat.xpm"
34 #include "bitmaps/loop.xpm"
36 #include "bitmaps/type_unknown.xpm"
37 #include "bitmaps/type_afile.xpm"
38 #include "bitmaps/type_vfile.xpm"
39 #include "bitmaps/type_net.xpm"
40 #include "bitmaps/type_card.xpm"
41 #include "bitmaps/type_disc.xpm"
42 #include "bitmaps/type_cdda.xpm"
43 #include "bitmaps/type_directory.xpm"
44 #include "bitmaps/type_playlist.xpm"
45 #include "bitmaps/type_node.xpm"
47 #include <wx/dynarray.h>
48 #include <wx/imaglist.h>
52 #define HELP_SHUFFLE N_( "Shuffle" )
53 #define HELP_LOOP N_( "Repeat All" )
54 #define HELP_REPEAT N_( "Repeat One" )
57 /* Callback prototype */
58 static int PlaylistChanged( vlc_object_t *, const char *,
59 vlc_value_t, vlc_value_t, void * );
60 static int PlaylistNext( vlc_object_t *, const char *,
61 vlc_value_t, vlc_value_t, void * );
62 static int ItemChanged( vlc_object_t *, const char *,
63 vlc_value_t, vlc_value_t, void * );
64 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
65 vlc_value_t oval, vlc_value_t nval, void *param );
66 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
67 vlc_value_t oval, vlc_value_t nval, void *param );
69 /*****************************************************************************
71 *****************************************************************************/
73 /* IDs for the controls and the menu commands */
88 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::OnMenuClose)
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(DeleteSelection_Event, Playlist::OnDeleteSelection)
142 EVT_MENU_OPEN( Playlist::OnMenuOpen )
143 EVT_MENU( -1, Playlist::OnMenuEvent )
145 EVT_TOOL(Random_Event, Playlist::OnRandom)
146 EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
147 EVT_TOOL(Loop_Event, Playlist::OnLoop)
150 EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
151 EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
152 EVT_MENU( PopupPreparse_Event, Playlist::OnPopupPreparse)
153 EVT_MENU( PopupSort_Event, Playlist::OnPopupSort)
154 EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
155 EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
157 /* Tree control events */
158 EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
159 EVT_TREE_KEY_DOWN( -1, Playlist::OnKeyDown )
161 EVT_CONTEXT_MENU( Playlist::OnPopup )
164 EVT_BUTTON( Search_Event, Playlist::OnSearch)
165 EVT_BUTTON( Save_Event, Playlist::OnSave)
167 /*EVT_TEXT( SearchText_Event, Playlist::OnSearchTextChange )*/
168 EVT_TEXT_ENTER( SearchText_Event, Playlist::OnSearch )
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()
186 i_id = p_item->input.i_id;
190 friend class Playlist;
193 /*****************************************************************************
195 *****************************************************************************/
196 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
197 wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
198 wxSize(500,300), wxDEFAULT_FRAME_STYLE )
202 /* Initializations */
205 i_update_counter = 0;
206 i_sort_mode = MODE_NONE;
207 b_need_update = VLC_FALSE;
208 i_items_to_append = 0;
209 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
211 if( p_playlist == NULL ) return;
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()
398 if( pp_sds != NULL ) free( pp_sds );
400 if( p_playlist == NULL ) return;
402 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
403 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
404 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
405 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
406 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
407 vlc_object_release( p_playlist );
410 /**********************************************************************
412 **********************************************************************/
415 void Playlist::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
417 wxTreeItemIdValue cookie;
419 for( int i = 0; i< p_node->i_children ; i++ )
423 child = treectrl->GetFirstChild( node, cookie);
427 child = treectrl->GetNextChild( node, cookie );
432 /* Not enough children */
433 CreateNode( p_node->pp_children[i], node );
434 /* Keep the tree pointer up to date */
435 child = treectrl->GetNextChild( node, cookie );
438 treectrl->SetItemImage( node, p_node->input.i_type );
442 /* Creates the node p_node as last child of parent */
443 void Playlist::CreateNode( playlist_item_t *p_node, wxTreeItemId parent )
446 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
447 -1,-1, new PlaylistItem( p_node ) );
448 treectrl->SetItemImage( node, p_node->input.i_type );
450 UpdateNodeChildren( p_node, node );
453 /* Update all children (recursively) of this node */
454 void Playlist::UpdateNodeChildren( playlist_item_t *p_node,
458 for( int i = 0; i< p_node->i_children ; i++ )
460 /* Append the item */
461 if( p_node->pp_children[i]->i_children == -1 )
464 treectrl->AppendItem( node,
465 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
466 new PlaylistItem( p_node->pp_children[i]) );
468 UpdateTreeItem( item );
472 CreateNode( p_node->pp_children[i], node );
477 /* Update an item in the tree */
478 void Playlist::UpdateTreeItem( wxTreeItemId item )
480 if( ! item.IsOk() ) return;
482 wxTreeItemData *p_data = treectrl->GetItemData( item );
483 if( !p_data ) return;
485 LockPlaylist( p_intf->p_sys, p_playlist );
486 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
487 ((PlaylistItem *)p_data)->i_id );
490 UnlockPlaylist( p_intf->p_sys, p_playlist );
495 wxString duration = wxU( "" );
496 char *psz_author = vlc_input_item_GetInfo( &p_item->input,
497 _(VLC_META_INFO_CAT), _(VLC_META_ARTIST) );
500 UnlockPlaylist( p_intf->p_sys, p_playlist );
504 char psz_duration[MSTRTIME_MAX_SIZE];
505 mtime_t dur = p_item->input.i_duration;
509 secstotimestr( psz_duration, dur/1000000 );
510 duration.Append( wxU( " ( " ) + wxString( wxU( psz_duration ) ) +
514 if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
516 msg = wxString( wxU( p_item->input.psz_name ) ) + duration;
520 msg = wxString(wxU( psz_author )) + wxT(" - ") +
521 wxString(wxU(p_item->input.psz_name)) + duration;
524 treectrl->SetItemText( item , msg );
525 treectrl->SetItemImage( item, p_item->input.i_type );
527 if( p_playlist->status.p_item == p_item )
529 treectrl->SetItemBold( item, true );
530 while( treectrl->GetItemParent( item ).IsOk() )
532 item = treectrl->GetItemParent( item );
533 treectrl->Expand( item );
538 treectrl->SetItemBold( item, false );
540 UnlockPlaylist( p_intf->p_sys, p_playlist );
543 /* Process a AppendIt em request */
544 void Playlist::AppendItem( wxCommandEvent& event )
546 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
547 playlist_item_t *p_item = NULL;
548 wxTreeItemId item, node;
552 /* No need to do anything if the playlist is going to be rebuilt */
553 if( b_need_update ) return;
555 if( p_add->i_view != i_current_view ) goto update;
557 node = FindItem( treectrl->GetRootItem(), p_add->i_node );
558 if( !node.IsOk() ) goto update;
560 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
561 if( !p_item ) goto update;
563 item = FindItem( treectrl->GetRootItem(), p_add->i_item );
564 if( item.IsOk() ) goto update;
566 item = treectrl->AppendItem( node,
567 wxL2U( p_item->input.psz_name ), -1,-1,
568 new PlaylistItem( p_item ) );
569 treectrl->SetItemImage( item, p_item->input.i_type );
571 if( item.IsOk() && p_item->i_children == -1 )
573 UpdateTreeItem( item );
577 int i_count = CountItems( treectrl->GetRootItem());
578 if( i_count != p_playlist->i_size )
580 statusbar->SetStatusText( wxString::Format( wxU(_(
581 "%i items in playlist (%i not shown)")),
583 p_playlist->i_size - i_count ) );
584 if( !b_changed_view )
586 i_current_view = VIEW_CATEGORY;
587 b_changed_view = VLC_TRUE;
588 b_need_update = VLC_TRUE;
593 statusbar->SetStatusText( wxString::Format( wxU(_(
594 "%i items in playlist")),
595 p_playlist->i_size ), 0 );
601 /* Process a updateitem request */
602 void Playlist::UpdateItem( int i )
604 if( i < 0 ) return; /* Sanity check */
606 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
610 UpdateTreeItem( item );
614 void Playlist::RemoveItem( int i )
616 if( i <= 0 ) return; /* Sanity check */
617 if( i == i_saved_id ) i_saved_id = -1;
619 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
623 treectrl->Delete( item );
628 /**********************************************************************
629 * Search functions (internal)
630 **********************************************************************/
632 /* Find a wxItem from a playlist id */
633 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
635 wxTreeItemIdValue cookie;
636 PlaylistItem *p_wxcurrent;
638 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
641 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
648 if( i_saved_id == i_id )
650 return saved_tree_item;
659 if( p_wxcurrent->i_id == i_id )
662 saved_tree_item = root;
668 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
669 if( p_wxcurrent->i_id == i_id )
672 saved_tree_item = item;
675 if( treectrl->ItemHasChildren( item ) )
677 wxTreeItemId search = FindItem( item, i_id );
681 saved_tree_item = search;
685 item = treectrl->GetNextChild( root, cookie );
692 int Playlist::CountItems( wxTreeItemId root )
694 wxTreeItemIdValue cookie;
696 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
700 if( treectrl->ItemHasChildren( item ) )
702 count += CountItems( item );
706 playlist_item_t *p_item;
707 LockPlaylist( p_intf->p_sys, p_playlist );
708 p_item = playlist_ItemGetById( p_playlist, ((PlaylistItem *)treectrl->GetItemData( item ))->i_id );
709 if( p_item && p_item->i_children == -1 )
711 UnlockPlaylist( p_intf->p_sys, p_playlist );
713 item = treectrl->GetNextChild( root, cookie );
718 /* Find a wxItem from a name (from current) */
719 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
721 wxTreeItemIdValue cookie;
723 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
728 if( treectrl->GetItemText( item).Lower().Contains(
729 search_string.Lower() ) )
731 if( !current.IsOk() || *pb_current_found == VLC_TRUE )
735 else if( current.IsOk() && item == current )
737 *pb_current_found = VLC_TRUE;
740 if( treectrl->ItemHasChildren( item ) )
742 wxTreeItemId search = FindItemByName( item, search_string, current,
749 item = treectrl->GetNextChild( root, cookie);
756 /**********************************************************************
757 * Rebuild the playlist
758 **********************************************************************/
759 void Playlist::Rebuild( vlc_bool_t b_root )
761 playlist_view_t *p_view;
763 i_items_to_append = 0;
765 /* We can remove the callbacks before locking, anyway, we won't
769 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
770 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
771 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
772 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
773 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
775 /* ...and rebuild it */
776 LockPlaylist( p_intf->p_sys, p_playlist );
780 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
782 /* HACK we should really get new*/
783 treectrl->DeleteAllItems();
784 treectrl->AddRoot( wxU(_("root" )), -1, -1,
785 new PlaylistItem( p_view->p_root) );
787 wxTreeItemId root = treectrl->GetRootItem();
788 UpdateNode( p_view->p_root, root );
790 int i_count = CountItems( treectrl->GetRootItem() );
792 if( i_count < p_playlist->i_size && !b_changed_view )
794 i_current_view = VIEW_CATEGORY;
795 b_changed_view = VLC_TRUE;
796 Rebuild( VLC_FALSE );
798 else if( i_count != p_playlist->i_size )
800 statusbar->SetStatusText( wxString::Format( wxU(_(
801 "%i items in playlist (%i not shown)")),
803 p_playlist->i_size - i_count ) );
807 statusbar->SetStatusText( wxString::Format( wxU(_(
808 "%i items in playlist")),
809 p_playlist->i_size ), 0 );
814 /* Put callbacks back online */
815 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
816 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
817 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
818 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
819 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
821 UnlockPlaylist( p_intf->p_sys, p_playlist );
827 void Playlist::ShowPlaylist( bool show )
829 if( show ) Rebuild( VLC_TRUE );
833 /* This function is called on a regular basis */
834 void Playlist::UpdatePlaylist()
838 /* If the playlist isn't show there's no need to update it */
839 if( !IsShown() ) return;
841 if( this->b_need_update )
843 this->b_need_update = VLC_FALSE;
847 /* Updating the playing status every 0.5s is enough */
848 if( i_update_counter % 5 ) return;
851 /*****************************************************************************
853 *****************************************************************************/
854 void Playlist::DeleteTreeItem( wxTreeItemId item )
856 PlaylistItem *p_wxitem;
857 playlist_item_t *p_item;
858 p_wxitem = (PlaylistItem *)treectrl->GetItemData( item );
860 LockPlaylist( p_intf->p_sys, p_playlist );
861 p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
865 UnlockPlaylist( p_intf->p_sys, p_playlist );
869 if( p_item->i_children == -1 ) DeleteItem( p_item->input.i_id );
870 else DeleteNode( p_item );
873 UnlockPlaylist( p_intf->p_sys, p_playlist );
876 void Playlist::DeleteItem( int item_id )
878 playlist_Delete( p_playlist, item_id );
881 void Playlist::DeleteNode( playlist_item_t *p_item )
883 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE , VLC_FALSE );
886 void Playlist::OnMenuClose( wxCommandEvent& event )
892 void Playlist::OnClose( wxCloseEvent& WXUNUSED(event) )
897 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
903 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" }};
905 wxString filter = wxT("");
907 if( p_playlist->i_size == 0 )
909 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
910 wxICON_WARNING | wxOK, this );
914 for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
916 filter.Append( wxU(formats[i].psz_desc) );
917 filter.Append( wxT("|") );
918 filter.Append( wxU(formats[i].psz_filter) );
919 filter.Append( wxT("|") );
921 wxFileDialog dialog( this, wxU(_("Save playlist")),
922 wxT(""), wxT(""), filter, wxSAVE );
924 if( dialog.ShowModal() == wxID_OK )
926 if( dialog.GetPath().mb_str() )
928 playlist_Export( p_playlist, dialog.GetPath().mb_str(),
929 formats[dialog.GetFilterIndex()].psz_module );
935 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
937 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
938 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
940 if( dialog.ShowModal() == wxID_OK )
942 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
946 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
948 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
952 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
954 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
958 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
960 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
964 /********************************************************************
966 ********************************************************************/
967 void Playlist::OnSort( wxCommandEvent& event )
969 PlaylistItem *p_wxitem;
970 p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
972 LockPlaylist( p_intf->p_sys, p_playlist );
973 switch( event.GetId() )
975 case SortTitle_Event:
976 playlist_RecursiveNodeSort( p_playlist,
977 playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
978 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
980 case RSortTitle_Event:
981 playlist_RecursiveNodeSort( p_playlist,
982 playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
983 SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
985 UnlockPlaylist( p_intf->p_sys, p_playlist );
990 /**********************************************************************
991 * Search functions (user)
992 **********************************************************************/
993 /*void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
995 search_button->SetDefault();
998 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
1000 wxString search_string = search_text->GetValue();
1002 vlc_bool_t pb_found = VLC_FALSE;
1004 wxTreeItemId found =
1005 FindItemByName( treectrl->GetRootItem(), search_string,
1006 search_current, &pb_found );
1011 search_current = dummy;
1012 found = FindItemByName( treectrl->GetRootItem(), search_string,
1013 search_current, &pb_found );
1018 search_current = found;
1019 treectrl->EnsureVisible( found );
1020 treectrl->UnselectAll();
1021 treectrl->SelectItem( found, true );
1025 /**********************************************************************
1026 * Selection functions
1027 **********************************************************************/
1028 void Playlist::RecursiveDeleteSelection( wxTreeItemId root )
1030 wxTreeItemIdValue cookie;
1031 wxTreeItemId child = treectrl->GetFirstChild( root, cookie );
1032 while( child.IsOk() )
1034 if( treectrl->ItemHasChildren( child ) )
1036 RecursiveDeleteSelection( child );
1037 if( treectrl->IsSelected(child ) ) DeleteTreeItem( child );
1039 else if( treectrl->IsSelected( child ) )
1040 DeleteTreeItem( child );
1041 child = treectrl->GetNextChild( root, cookie );
1045 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1047 RecursiveDeleteSelection( treectrl->GetRootItem() );
1050 /**********************************************************************
1051 * Playlist mode functions
1052 **********************************************************************/
1053 void Playlist::OnRandom( wxCommandEvent& event )
1056 val.b_bool = event.IsChecked();
1057 var_Set( p_playlist, "random", val);
1060 void Playlist::OnLoop( wxCommandEvent& event )
1063 val.b_bool = event.IsChecked();
1064 var_Set( p_playlist, "loop", val);
1067 void Playlist::OnRepeat( wxCommandEvent& event )
1070 val.b_bool = event.IsChecked();
1071 var_Set( p_playlist, "repeat", val);
1074 /********************************************************************
1076 ********************************************************************/
1077 void Playlist::OnActivateItem( wxTreeEvent& event )
1079 playlist_item_t *p_item,*p_node,*p_item2,*p_node2;
1081 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1083 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1085 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1087 LockPlaylist( p_intf->p_sys, p_playlist );
1089 if( !( p_wxitem && p_wxparent ) )
1091 UnlockPlaylist( p_intf->p_sys, p_playlist );
1095 p_item2 = playlist_ItemGetById(p_playlist, p_wxitem->i_id);
1096 p_node2 = playlist_ItemGetById(p_playlist, p_wxparent->i_id);
1097 if( p_item2 && p_item2->i_children == -1 )
1106 /* if( p_node && p_node->i_children > 0 &&
1107 p_node->pp_children[0]->i_children == -1)
1109 p_item = p_node->pp_children[0];
1117 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1119 UnlockPlaylist( p_intf->p_sys, p_playlist );
1122 void Playlist::OnKeyDown( wxTreeEvent& event )
1124 long keycode = event.GetKeyCode();
1125 /* Delete selected items */
1126 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1128 /* We send a dummy event */
1129 OnDeleteSelection( event );
1137 void Playlist::OnEnDis( wxCommandEvent& event )
1139 msg_Warn( p_intf, "not implemented" );
1142 /**********************************************************************
1144 **********************************************************************/
1146 void Playlist::OnMenuOpen( wxMenuEvent& event)
1148 #if defined( __WXMSW__ )
1149 # define GetEventObject GetMenu
1152 if( event.GetEventObject() == p_view_menu )
1154 p_view_menu = ViewMenu();
1156 #if defined( __WXMSW__ )
1157 # undef GetEventObject
1161 void Playlist::OnMenuEvent( wxCommandEvent& event )
1163 if( event.GetId() < FirstView_Event )
1168 else if( event.GetId() < LastView_Event )
1171 int i_new_view = event.GetId() - FirstView_Event;
1173 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1175 if( p_view != NULL )
1177 b_changed_view = VLC_TRUE;
1178 i_current_view = i_new_view;
1179 playlist_ViewUpdate( p_playlist, i_new_view );
1180 Rebuild( VLC_TRUE );
1183 else if( i_new_view >= VIEW_FIRST_SORTED &&
1184 i_new_view <= VIEW_LAST_SORTED )
1186 b_changed_view = VLC_TRUE;
1187 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1188 playlist_ViewUpdate( p_playlist, i_new_view );
1190 i_current_view = i_new_view;
1192 Rebuild( VLC_TRUE );
1195 else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1197 if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1198 pp_sds[event.GetId() - FirstSD_Event] ) )
1200 playlist_ServicesDiscoveryAdd( p_playlist,
1201 pp_sds[event.GetId() - FirstSD_Event] );
1205 //wxMutexGuiLeave();
1206 playlist_ServicesDiscoveryRemove( p_playlist,
1207 pp_sds[event.GetId() - FirstSD_Event] );
1208 //wxMutexGuiEnter();
1213 wxMenu * Playlist::ViewMenu()
1217 p_view_menu = new wxMenu;
1221 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1224 wxMenuItem *item = node->GetData();
1225 node = node->GetNext();
1226 p_view_menu->Delete( item );
1230 /* FIXME : have a list of "should have" views */
1231 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1232 wxU(_("Normal") ) );
1233 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1234 wxU(_("Sorted by artist") ) );
1235 p_view_menu->Append( FirstView_Event + VIEW_S_ALBUM,
1236 wxU(_("Sorted by Album") ) );
1241 wxMenu *Playlist::SDMenu()
1243 p_sd_menu = new wxMenu;
1245 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1249 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1251 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1253 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1256 if( i_number ) pp_sds = (char **)calloc( i_number, sizeof(void *) );
1259 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1261 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1263 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1265 p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1266 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1267 (p_parser->psz_shortname ?
1268 p_parser->psz_shortname : p_parser->psz_object_name) ) );
1270 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1271 p_parser->psz_object_name ) )
1273 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1276 pp_sds[i_number++] = p_parser->psz_object_name;
1279 vlc_list_release( p_list );
1284 /*****************************************************************************
1285 * Popup management functions
1286 *****************************************************************************/
1287 void Playlist::OnPopup( wxContextMenuEvent& event )
1289 wxPoint pt = event.GetPosition();
1290 playlist_item_t *p_item;
1292 i_wx_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1293 if( i_wx_popup_item.IsOk() )
1295 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1297 PlaylistItem *p_wxparent= (PlaylistItem *)treectrl->GetItemData(
1298 treectrl->GetItemParent( i_wx_popup_item ) );
1299 i_popup_item = p_wxitem->i_id;
1300 i_popup_parent = p_wxparent->i_id;
1301 treectrl->SelectItem( i_wx_popup_item );
1303 LockPlaylist( p_intf->p_sys, p_playlist );
1304 p_item = playlist_ItemGetById( p_playlist, i_popup_item );
1308 UnlockPlaylist( p_intf->p_sys, p_playlist );
1311 if( p_item->i_children == -1 )
1313 UnlockPlaylist( p_intf->p_sys, p_playlist );
1314 Playlist::PopupMenu( item_popup,
1315 ScreenToClient( wxGetMousePosition() ) );
1319 UnlockPlaylist( p_intf->p_sys, p_playlist );
1320 Playlist::PopupMenu( node_popup,
1321 ScreenToClient( wxGetMousePosition() ) );
1326 void Playlist::OnPopupPlay( wxCommandEvent& event )
1328 playlist_item_t *p_popup_item, *p_popup_parent;
1329 LockPlaylist( p_intf->p_sys, p_playlist );
1330 p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1331 p_popup_parent = playlist_ItemGetById( p_playlist, i_popup_parent );
1332 if( p_popup_item != NULL )
1334 if( p_popup_item->i_children > -1 )
1336 if( event.GetId() == PopupPlay_Event &&
1337 p_popup_item->i_children > 0 )
1339 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1340 i_current_view, p_popup_item,
1341 p_popup_item->pp_children[0] );
1345 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1346 i_current_view, p_popup_item, NULL );
1351 if( event.GetId() == PopupPlay_Event )
1353 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1354 i_current_view, p_popup_parent,
1359 UnlockPlaylist( p_intf->p_sys, p_playlist );
1362 void Playlist::OnPopupPreparse( wxCommandEvent& event )
1367 void Playlist::Preparse()
1369 playlist_item_t *p_popup_item;
1370 LockPlaylist( p_intf->p_sys, p_playlist );
1371 p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1373 if( p_popup_item != NULL )
1375 if( p_popup_item->i_children == -1 )
1377 playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1382 playlist_item_t *p_parent = p_popup_item;
1383 for( i = 0; i< p_parent->i_children ; i++ )
1386 i_wx_popup_item = FindItem( treectrl->GetRootItem(),
1387 p_parent->pp_children[i]->input.i_id );
1388 i_popup_item = p_parent->pp_children[i]->input.i_id;
1393 UnlockPlaylist( p_intf->p_sys, p_playlist );
1396 void Playlist::OnPopupDel( wxCommandEvent& event )
1398 DeleteTreeItem( i_wx_popup_item );
1401 void Playlist::OnPopupSort( wxCommandEvent& event )
1403 PlaylistItem *p_wxitem;
1404 playlist_item_t *p_item;
1406 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_wx_popup_item );
1407 LockPlaylist( p_intf->p_sys, p_playlist );
1409 p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
1410 if( p_item->i_children >= 0 )
1412 playlist_RecursiveNodeSort( p_playlist, p_item,
1413 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1415 treectrl->DeleteChildren( i_wx_popup_item );
1417 UpdateNodeChildren( p_item, i_wx_popup_item );
1420 UnlockPlaylist( p_intf->p_sys, p_playlist );
1423 void Playlist::OnPopupInfo( wxCommandEvent& event )
1425 LockPlaylist( p_intf->p_sys, p_playlist );
1426 playlist_item_t *p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1429 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1430 if( iteminfo_dialog->ShowModal() == wxID_OK )
1432 UpdateItem( i_wx_popup_item );
1434 delete iteminfo_dialog;
1436 UnlockPlaylist( p_intf->p_sys, p_playlist );
1440 /*****************************************************************************
1441 * Custom events management
1442 *****************************************************************************/
1443 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1445 switch( event.GetId() )
1447 case UpdateItem_Event:
1448 UpdateItem( event.GetInt() );
1450 case AppendItem_Event:
1451 AppendItem( event );
1453 case RemoveItem_Event:
1454 RemoveItem( event.GetInt() );
1459 /*****************************************************************************
1460 * PlaylistChanged: callback triggered by the intf-change playlist variable
1461 * We don't rebuild the playlist directly here because we don't want the
1462 * caller to block for a too long time.
1463 *****************************************************************************/
1464 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1465 vlc_value_t oval, vlc_value_t nval, void *param )
1467 Playlist *p_playlist_dialog = (Playlist *)param;
1468 p_playlist_dialog->b_need_update = VLC_TRUE;
1472 /*****************************************************************************
1473 * Next: callback triggered by the playlist-current playlist variable
1474 *****************************************************************************/
1475 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1476 vlc_value_t oval, vlc_value_t nval, void *param )
1478 Playlist *p_playlist_dialog = (Playlist *)param;
1480 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1481 event.SetInt( oval.i_int );
1482 p_playlist_dialog->AddPendingEvent( event );
1483 event.SetInt( nval.i_int );
1484 p_playlist_dialog->AddPendingEvent( event );
1489 /*****************************************************************************
1490 * ItemChanged: callback triggered by the item-change playlist variable
1491 *****************************************************************************/
1492 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1493 vlc_value_t old_val, vlc_value_t new_val, void *param )
1495 Playlist *p_playlist_dialog = (Playlist *)param;
1497 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1498 event.SetInt( new_val.i_int );
1499 p_playlist_dialog->AddPendingEvent( event );
1503 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1504 vlc_value_t old_val, vlc_value_t new_val, void *param )
1506 Playlist *p_playlist_dialog = (Playlist *)param;
1508 wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1509 event.SetInt( new_val.i_int );
1510 p_playlist_dialog->AddPendingEvent( event );
1515 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1516 vlc_value_t oval, vlc_value_t nval, void *param )
1518 Playlist *p_playlist_dialog = (Playlist *)param;
1520 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1521 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1523 if( ++p_playlist_dialog->i_items_to_append >= 50 )
1525 /* Too many items waiting to be added, it will be quicker to rebuild
1526 * the whole playlist */
1527 p_playlist_dialog->b_need_update = VLC_TRUE;
1531 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1532 event.SetClientData( (void *)p_add );
1533 p_playlist_dialog->AddPendingEvent( event );