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 )
160 EVT_TREE_BEGIN_DRAG( TreeCtrl_Event, Playlist::OnDragItemBegin )
161 EVT_TREE_END_DRAG( TreeCtrl_Event, Playlist::OnDragItemEnd )
163 EVT_CONTEXT_MENU( Playlist::OnPopup )
166 EVT_BUTTON( Search_Event, Playlist::OnSearch)
167 EVT_BUTTON( Save_Event, Playlist::OnSave)
169 /*EVT_TEXT( SearchText_Event, Playlist::OnSearchTextChange )*/
170 EVT_TEXT_ENTER( SearchText_Event, Playlist::OnSearch )
173 EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
175 /* Special events : we don't want to destroy the window when the user
177 EVT_CLOSE(Playlist::OnClose)
180 /*****************************************************************************
182 ****************************************************************************/
183 class PlaylistItem : public wxTreeItemData
186 PlaylistItem( playlist_item_t *p_item ) : wxTreeItemData()
188 i_id = p_item->input.i_id;
192 friend class Playlist;
195 /*****************************************************************************
197 *****************************************************************************/
198 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
199 wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
200 wxSize(500,300), wxDEFAULT_FRAME_STYLE )
204 /* Initializations */
207 i_update_counter = 0;
208 i_sort_mode = MODE_NONE;
209 b_need_update = VLC_FALSE;
210 i_items_to_append = 0;
211 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
213 if( p_playlist == NULL ) return;
215 SetIcon( *p_intf->p_sys->p_icon );
218 p_sd_menu = SDMenu();
220 i_current_view = VIEW_CATEGORY;
221 b_changed_view = VLC_FALSE;
225 i_duration_sorted = 0;
227 var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
228 var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
229 var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
231 /* Create our "Manage" menu */
232 wxMenu *manage_menu = new wxMenu;
233 manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
234 manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
235 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
236 manage_menu->AppendSeparator();
237 manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
239 manage_menu->AppendSeparator();
240 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
241 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
242 manage_menu->AppendSeparator();
243 manage_menu->Append( Close_Event, wxU(_("&Close")) );
245 /* Create our "Sort" menu */
246 wxMenu *sort_menu = new wxMenu;
247 sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
248 sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
249 sort_menu->AppendSeparator();
250 sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
252 /* Create our "Selection" menu */
253 wxMenu *selection_menu = new wxMenu;
254 selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
256 /* Create our "View" menu */
259 /* Append the freshly created menus to the menu bar */
260 wxMenuBar *menubar = new wxMenuBar();
261 menubar->Append( manage_menu, wxU(_("&Manage")) );
262 menubar->Append( sort_menu, wxU(_("S&ort")) );
263 menubar->Append( selection_menu, wxU(_("&Selection")) );
264 menubar->Append( p_view_menu, wxU(_("&View items") ) );
266 /* Attach the menu bar to the frame */
267 SetMenuBar( menubar );
269 /* Create the popup menu */
270 node_popup = new wxMenu;
271 node_popup->Append( PopupPlay_Event, wxU(_("Play")) );
272 node_popup->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
273 node_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
274 node_popup->Append( PopupSort_Event, wxU(_("Sort this branch")) );
275 node_popup->Append( PopupDel_Event, wxU(_("Delete")) );
276 node_popup->Append( PopupInfo_Event, wxU(_("Info")) );
278 item_popup = new wxMenu;
279 item_popup->Append( PopupPlay_Event, wxU(_("Play")) );
280 item_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
281 item_popup->Append( PopupDel_Event, wxU(_("Delete")) );
282 item_popup->Append( PopupInfo_Event, wxU(_("Info")) );
284 /* Create a panel to put everything in */
285 wxPanel *playlist_panel = new wxPanel( this, -1 );
286 playlist_panel->SetAutoLayout( TRUE );
288 /* Create the toolbar */
290 CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT );
292 /* Create the random tool */
293 toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
294 wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
295 wxU(_(HELP_SHUFFLE) ) );
296 var_Get( p_intf, "random", &val );
297 toolbar->ToggleTool( Random_Event, val.b_bool );
299 /* Create the Loop tool */
300 toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
301 wxBitmap( loop_xpm), wxITEM_CHECK,
302 wxU(_(HELP_LOOP ) ) );
303 var_Get( p_intf, "loop", &val );
304 toolbar->ToggleTool( Loop_Event, val.b_bool );
306 /* Create the Repeat one checkbox */
307 toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
308 wxBitmap( repeat_xpm), wxITEM_CHECK,
309 wxU(_(HELP_REPEAT ) ) );
310 var_Get( p_intf, "repeat", &val );
311 toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
313 /* Create the Search Textbox */
314 search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
315 wxDefaultPosition, wxSize(100, -1),
318 /* Create the search button */
319 search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
321 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
322 wxSize(16, 16), wxBORDER_NONE ) );
323 toolbar->AddControl( search_text );
324 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
325 wxSize(5, 5), wxBORDER_NONE ) );
326 toolbar->AddControl( search_button );
327 search_button->SetDefault();
330 /* Create the tree */
331 treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
332 wxDefaultPosition, wxDefaultSize,
333 wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
335 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
336 wxTR_MULTIPLE | wxTR_EXTENDED );
338 /* Create image list */
339 wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
341 /* FIXME: absolutely needs to be in the right order FIXME */
342 p_images->Add( wxIcon( type_unknown_xpm ) );
343 p_images->Add( wxIcon( type_afile_xpm ) );
344 p_images->Add( wxIcon( type_vfile_xpm ) );
345 p_images->Add( wxIcon( type_directory_xpm ) );
346 p_images->Add( wxIcon( type_disc_xpm ) );
347 p_images->Add( wxIcon( type_cdda_xpm ) );
348 p_images->Add( wxIcon( type_card_xpm ) );
349 p_images->Add( wxIcon( type_net_xpm ) );
350 p_images->Add( wxIcon( type_playlist_xpm ) );
351 p_images->Add( wxIcon( type_node_xpm ) );
352 treectrl->AssignImageList( p_images );
354 treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
356 /* Reduce font size */
357 wxFont font= treectrl->GetFont();
358 font.SetPointSize(9);
359 treectrl->SetFont( font );
361 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
362 panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
363 panel_sizer->Layout();
365 playlist_panel->SetSizerAndFit( panel_sizer );
367 int pi_widths[1] = { -1 };
368 statusbar = CreateStatusBar( 1 );
369 statusbar->SetStatusWidths( 1, pi_widths );
371 #if wxUSE_DRAG_AND_DROP
372 /* Associate drop targets with the playlist */
373 SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
379 /* We want to be noticed of playlist changes */
381 /* Some global changes happened -> Rebuild all */
382 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
384 /* We went to the next item */
385 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
387 /* One item has been updated */
388 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
390 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
391 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
393 /* Update the playlist */
398 Playlist::~Playlist()
400 if( pp_sds != NULL ) free( pp_sds );
402 if( p_playlist == NULL ) return;
404 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
405 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
406 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
407 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
408 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
409 vlc_object_release( p_playlist );
412 /**********************************************************************
414 **********************************************************************/
417 void Playlist::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
419 wxTreeItemIdValue cookie;
421 for( int i = 0; i< p_node->i_children ; i++ )
425 child = treectrl->GetFirstChild( node, cookie);
429 child = treectrl->GetNextChild( node, cookie );
434 /* Not enough children */
435 CreateNode( p_node->pp_children[i], node );
436 /* Keep the tree pointer up to date */
437 child = treectrl->GetNextChild( node, cookie );
440 treectrl->SetItemImage( node, p_node->input.i_type );
444 /* Creates the node p_node as last child of parent */
445 void Playlist::CreateNode( playlist_item_t *p_node, wxTreeItemId parent )
448 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
449 -1,-1, new PlaylistItem( p_node ) );
450 treectrl->SetItemImage( node, p_node->input.i_type );
452 UpdateNodeChildren( p_node, node );
455 /* Update all children (recursively) of this node */
456 void Playlist::UpdateNodeChildren( playlist_item_t *p_node,
460 for( int i = 0; i< p_node->i_children ; i++ )
462 /* Append the item */
463 if( p_node->pp_children[i]->i_children == -1 )
466 treectrl->AppendItem( node,
467 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
468 new PlaylistItem( p_node->pp_children[i]) );
470 UpdateTreeItem( item );
474 CreateNode( p_node->pp_children[i], node );
479 /* Update an item in the tree */
480 void Playlist::UpdateTreeItem( wxTreeItemId item )
482 if( ! item.IsOk() ) return;
484 wxTreeItemData *p_data = treectrl->GetItemData( item );
485 if( !p_data ) return;
487 LockPlaylist( p_intf->p_sys, p_playlist );
488 playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
489 ((PlaylistItem *)p_data)->i_id );
492 UnlockPlaylist( p_intf->p_sys, p_playlist );
497 wxString duration = wxU( "" );
498 char *psz_author = vlc_input_item_GetInfo( &p_item->input,
499 _(VLC_META_INFO_CAT), _(VLC_META_ARTIST) );
502 UnlockPlaylist( p_intf->p_sys, p_playlist );
506 char psz_duration[MSTRTIME_MAX_SIZE];
507 mtime_t dur = p_item->input.i_duration;
511 secstotimestr( psz_duration, dur/1000000 );
512 duration.Append( wxU( " ( " ) + wxString( wxU( psz_duration ) ) +
516 if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
518 msg = wxString( wxU( p_item->input.psz_name ) ) + duration;
522 msg = wxString(wxU( psz_author )) + wxT(" - ") +
523 wxString(wxU(p_item->input.psz_name)) + duration;
526 treectrl->SetItemText( item , msg );
527 treectrl->SetItemImage( item, p_item->input.i_type );
529 if( p_playlist->status.p_item == p_item )
531 treectrl->SetItemBold( item, true );
532 while( treectrl->GetItemParent( item ).IsOk() )
534 item = treectrl->GetItemParent( item );
535 treectrl->Expand( item );
540 treectrl->SetItemBold( item, false );
542 UnlockPlaylist( p_intf->p_sys, p_playlist );
545 /* Process a AppendIt em request */
546 void Playlist::AppendItem( wxCommandEvent& event )
548 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
549 playlist_item_t *p_item = NULL;
550 wxTreeItemId item, node;
554 /* No need to do anything if the playlist is going to be rebuilt */
555 if( b_need_update ) return;
557 if( p_add->i_view != i_current_view ) goto update;
559 node = FindItem( treectrl->GetRootItem(), p_add->i_node );
560 if( !node.IsOk() ) goto update;
562 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
563 if( !p_item ) goto update;
565 item = FindItem( treectrl->GetRootItem(), p_add->i_item );
566 if( item.IsOk() ) goto update;
568 item = treectrl->AppendItem( node,
569 wxL2U( p_item->input.psz_name ), -1,-1,
570 new PlaylistItem( p_item ) );
571 treectrl->SetItemImage( item, p_item->input.i_type );
573 if( item.IsOk() && p_item->i_children == -1 )
575 UpdateTreeItem( item );
579 int i_count = CountItems( treectrl->GetRootItem());
580 if( i_count != p_playlist->i_size )
582 statusbar->SetStatusText( wxString::Format( wxU(_(
583 "%i items in playlist (%i not shown)")),
585 p_playlist->i_size - i_count ) );
586 if( !b_changed_view )
588 i_current_view = VIEW_CATEGORY;
589 b_changed_view = VLC_TRUE;
590 b_need_update = VLC_TRUE;
595 statusbar->SetStatusText( wxString::Format( wxU(_(
596 "%i items in playlist")),
597 p_playlist->i_size ), 0 );
603 /* Process a updateitem request */
604 void Playlist::UpdateItem( int i )
606 if( i < 0 ) return; /* Sanity check */
608 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
612 UpdateTreeItem( item );
616 void Playlist::RemoveItem( int i )
618 if( i <= 0 ) return; /* Sanity check */
619 if( i == i_saved_id ) i_saved_id = -1;
621 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
625 treectrl->Delete( item );
630 /**********************************************************************
631 * Search functions (internal)
632 **********************************************************************/
634 /* Find a wxItem from a playlist id */
635 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
637 wxTreeItemIdValue cookie;
638 PlaylistItem *p_wxcurrent;
640 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
643 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
650 if( i_saved_id == i_id )
652 return saved_tree_item;
661 if( p_wxcurrent->i_id == i_id )
664 saved_tree_item = root;
670 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
671 if( p_wxcurrent->i_id == i_id )
674 saved_tree_item = item;
677 if( treectrl->ItemHasChildren( item ) )
679 wxTreeItemId search = FindItem( item, i_id );
683 saved_tree_item = search;
687 item = treectrl->GetNextChild( root, cookie );
694 int Playlist::CountItems( wxTreeItemId root )
696 wxTreeItemIdValue cookie;
698 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
702 if( treectrl->ItemHasChildren( item ) )
704 count += CountItems( item );
708 playlist_item_t *p_item;
709 LockPlaylist( p_intf->p_sys, p_playlist );
710 p_item = playlist_ItemGetById( p_playlist, ((PlaylistItem *)treectrl->GetItemData( item ))->i_id );
711 if( p_item && p_item->i_children == -1 )
713 UnlockPlaylist( p_intf->p_sys, p_playlist );
715 item = treectrl->GetNextChild( root, cookie );
720 /* Find a wxItem from a name (from current) */
721 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
723 wxTreeItemIdValue cookie;
725 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
730 if( treectrl->GetItemText( item).Lower().Contains(
731 search_string.Lower() ) )
733 if( !current.IsOk() || *pb_current_found == VLC_TRUE )
737 else if( current.IsOk() && item == current )
739 *pb_current_found = VLC_TRUE;
742 if( treectrl->ItemHasChildren( item ) )
744 wxTreeItemId search = FindItemByName( item, search_string, current,
751 item = treectrl->GetNextChild( root, cookie);
758 /**********************************************************************
759 * Rebuild the playlist
760 **********************************************************************/
761 void Playlist::Rebuild( vlc_bool_t b_root )
763 playlist_view_t *p_view;
765 i_items_to_append = 0;
767 /* We can remove the callbacks before locking, anyway, we won't
771 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
772 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
773 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
774 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
775 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
777 /* ...and rebuild it */
778 LockPlaylist( p_intf->p_sys, p_playlist );
782 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
784 /* HACK we should really get new*/
785 treectrl->DeleteAllItems();
786 treectrl->AddRoot( wxU(_("root" )), -1, -1,
787 new PlaylistItem( p_view->p_root) );
789 wxTreeItemId root = treectrl->GetRootItem();
790 UpdateNode( p_view->p_root, root );
792 int i_count = CountItems( treectrl->GetRootItem() );
794 if( i_count < p_playlist->i_size && !b_changed_view )
796 i_current_view = VIEW_CATEGORY;
797 b_changed_view = VLC_TRUE;
798 Rebuild( VLC_FALSE );
800 else if( i_count != p_playlist->i_size )
802 statusbar->SetStatusText( wxString::Format( wxU(_(
803 "%i items in playlist (%i not shown)")),
805 p_playlist->i_size - i_count ) );
809 statusbar->SetStatusText( wxString::Format( wxU(_(
810 "%i items in playlist")),
811 p_playlist->i_size ), 0 );
816 /* Put callbacks back online */
817 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
818 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
819 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
820 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
821 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
823 UnlockPlaylist( p_intf->p_sys, p_playlist );
829 void Playlist::ShowPlaylist( bool show )
831 if( show ) Rebuild( VLC_TRUE );
835 /* This function is called on a regular basis */
836 void Playlist::UpdatePlaylist()
840 /* If the playlist isn't show there's no need to update it */
841 if( !IsShown() ) return;
843 if( this->b_need_update )
845 this->b_need_update = VLC_FALSE;
849 /* Updating the playing status every 0.5s is enough */
850 if( i_update_counter % 5 ) return;
853 /*****************************************************************************
855 *****************************************************************************/
856 void Playlist::DeleteTreeItem( wxTreeItemId item )
858 PlaylistItem *p_wxitem;
859 playlist_item_t *p_item;
860 p_wxitem = (PlaylistItem *)treectrl->GetItemData( item );
862 LockPlaylist( p_intf->p_sys, p_playlist );
863 p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
867 UnlockPlaylist( p_intf->p_sys, p_playlist );
871 if( p_item->i_children == -1 ) DeleteItem( p_item->input.i_id );
872 else DeleteNode( p_item );
875 UnlockPlaylist( p_intf->p_sys, p_playlist );
878 void Playlist::DeleteItem( int item_id )
880 playlist_Delete( p_playlist, item_id );
883 void Playlist::DeleteNode( playlist_item_t *p_item )
885 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE , VLC_FALSE );
888 void Playlist::OnMenuClose( wxCommandEvent& event )
894 void Playlist::OnClose( wxCloseEvent& WXUNUSED(event) )
899 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
905 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" }};
907 wxString filter = wxT("");
909 if( p_playlist->i_size == 0 )
911 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
912 wxICON_WARNING | wxOK, this );
916 for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
918 filter.Append( wxU(formats[i].psz_desc) );
919 filter.Append( wxT("|") );
920 filter.Append( wxU(formats[i].psz_filter) );
921 filter.Append( wxT("|") );
923 wxFileDialog dialog( this, wxU(_("Save playlist")),
924 wxT(""), wxT(""), filter, wxSAVE );
926 if( dialog.ShowModal() == wxID_OK )
928 if( dialog.GetPath().mb_str() )
930 playlist_Export( p_playlist, dialog.GetPath().mb_str(),
931 formats[dialog.GetFilterIndex()].psz_module );
937 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
939 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
940 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
942 if( dialog.ShowModal() == wxID_OK )
944 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
948 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
950 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
954 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
956 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
960 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
962 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
966 /********************************************************************
968 ********************************************************************/
969 void Playlist::OnSort( wxCommandEvent& event )
971 PlaylistItem *p_wxitem;
972 p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
974 LockPlaylist( p_intf->p_sys, p_playlist );
975 switch( event.GetId() )
977 case SortTitle_Event:
978 playlist_RecursiveNodeSort( p_playlist,
979 playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
980 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
982 case RSortTitle_Event:
983 playlist_RecursiveNodeSort( p_playlist,
984 playlist_ItemGetById( p_playlist, p_wxitem->i_id ),
985 SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
987 UnlockPlaylist( p_intf->p_sys, p_playlist );
992 /**********************************************************************
993 * Search functions (user)
994 **********************************************************************/
995 /*void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
997 search_button->SetDefault();
1000 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
1002 wxString search_string = search_text->GetValue();
1004 vlc_bool_t pb_found = VLC_FALSE;
1006 wxTreeItemId found =
1007 FindItemByName( treectrl->GetRootItem(), search_string,
1008 search_current, &pb_found );
1013 search_current = dummy;
1014 found = FindItemByName( treectrl->GetRootItem(), search_string,
1015 search_current, &pb_found );
1020 search_current = found;
1021 treectrl->EnsureVisible( found );
1022 treectrl->UnselectAll();
1023 treectrl->SelectItem( found, true );
1027 /**********************************************************************
1028 * Selection functions
1029 **********************************************************************/
1030 void Playlist::RecursiveDeleteSelection( wxTreeItemId root )
1032 wxTreeItemIdValue cookie;
1033 wxTreeItemId child = treectrl->GetFirstChild( root, cookie );
1034 while( child.IsOk() )
1036 if( treectrl->ItemHasChildren( child ) )
1038 RecursiveDeleteSelection( child );
1039 if( treectrl->IsSelected(child ) ) DeleteTreeItem( child );
1041 else if( treectrl->IsSelected( child ) )
1042 DeleteTreeItem( child );
1043 child = treectrl->GetNextChild( root, cookie );
1047 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1049 RecursiveDeleteSelection( treectrl->GetRootItem() );
1052 /**********************************************************************
1053 * Playlist mode functions
1054 **********************************************************************/
1055 void Playlist::OnRandom( wxCommandEvent& event )
1058 val.b_bool = event.IsChecked();
1059 var_Set( p_playlist, "random", val);
1062 void Playlist::OnLoop( wxCommandEvent& event )
1065 val.b_bool = event.IsChecked();
1066 var_Set( p_playlist, "loop", val);
1069 void Playlist::OnRepeat( wxCommandEvent& event )
1072 val.b_bool = event.IsChecked();
1073 var_Set( p_playlist, "repeat", val);
1076 /********************************************************************
1078 ********************************************************************/
1079 void Playlist::OnActivateItem( wxTreeEvent& event )
1081 playlist_item_t *p_item,*p_node,*p_item2,*p_node2;
1083 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1085 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1087 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1089 LockPlaylist( p_intf->p_sys, p_playlist );
1091 if( !( p_wxitem && p_wxparent ) )
1093 UnlockPlaylist( p_intf->p_sys, p_playlist );
1097 p_item2 = playlist_ItemGetById(p_playlist, p_wxitem->i_id);
1098 p_node2 = playlist_ItemGetById(p_playlist, p_wxparent->i_id);
1099 if( p_item2 && p_item2->i_children == -1 )
1108 /* if( p_node && p_node->i_children > 0 &&
1109 p_node->pp_children[0]->i_children == -1)
1111 p_item = p_node->pp_children[0];
1119 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1121 UnlockPlaylist( p_intf->p_sys, p_playlist );
1124 void Playlist::OnKeyDown( wxTreeEvent& event )
1126 long keycode = event.GetKeyCode();
1127 /* Delete selected items */
1128 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1130 /* We send a dummy event */
1131 OnDeleteSelection( event );
1139 void Playlist::OnEnDis( wxCommandEvent& event )
1141 msg_Warn( p_intf, "not implemented" );
1144 void Playlist::OnDragItemBegin( wxTreeEvent& event )
1147 draged_tree_item = event.GetItem();
1150 void Playlist::OnDragItemEnd( wxTreeEvent& event )
1152 wxTreeItemId dest_tree_item = event.GetItem();
1154 /* check that we're not trying to move a node into one of it's children */
1155 wxTreeItemId parent = dest_tree_item;
1156 while( parent != treectrl->GetRootItem() )
1158 if( draged_tree_item == parent ) return;
1159 parent = treectrl->GetItemParent( parent );
1162 if( draged_tree_item != dest_tree_item )
1164 LockPlaylist( p_intf->p_sys, p_playlist );
1166 PlaylistItem *p_wxdrageditem =
1167 (PlaylistItem *)treectrl->GetItemData( draged_tree_item );
1168 PlaylistItem *p_wxdestitem =
1169 (PlaylistItem *)treectrl->GetItemData( dest_tree_item );
1171 playlist_item_t *p_drageditem =
1172 playlist_ItemGetById(p_playlist, p_wxdrageditem->i_id );
1173 playlist_item_t *p_destitem =
1174 playlist_ItemGetById(p_playlist, p_wxdestitem->i_id );
1176 if( p_destitem->i_children == -1 )
1177 /* this is a leaf */
1179 parent = treectrl->GetItemParent( dest_tree_item );
1180 PlaylistItem *p_parent =
1181 (PlaylistItem *)treectrl->GetItemData( parent );
1182 playlist_item_t *p_destitem2 =
1183 playlist_ItemGetById( p_playlist, p_parent->i_id );
1185 for( i = 0; i < p_destitem2->i_children; i++ )
1187 if( p_destitem2->pp_children[i] == p_destitem ) break;
1189 playlist_TreeMove( p_playlist, p_drageditem, p_destitem2,
1190 i, i_current_view );
1193 /* this is a node */
1195 playlist_TreeMove( p_playlist, p_drageditem, p_destitem,
1196 0, i_current_view );
1198 UnlockPlaylist( p_intf->p_sys, p_playlist );
1199 Rebuild( VLC_TRUE );
1203 /**********************************************************************
1205 **********************************************************************/
1207 void Playlist::OnMenuOpen( wxMenuEvent& event)
1209 #if defined( __WXMSW__ )
1210 # define GetEventObject GetMenu
1213 if( event.GetEventObject() == p_view_menu )
1215 p_view_menu = ViewMenu();
1217 #if defined( __WXMSW__ )
1218 # undef GetEventObject
1222 void Playlist::OnMenuEvent( wxCommandEvent& event )
1224 if( event.GetId() < FirstView_Event )
1229 else if( event.GetId() < LastView_Event )
1232 int i_new_view = event.GetId() - FirstView_Event;
1234 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1236 if( p_view != NULL )
1238 b_changed_view = VLC_TRUE;
1239 i_current_view = i_new_view;
1240 playlist_ViewUpdate( p_playlist, i_new_view );
1241 Rebuild( VLC_TRUE );
1244 else if( i_new_view >= VIEW_FIRST_SORTED &&
1245 i_new_view <= VIEW_LAST_SORTED )
1247 b_changed_view = VLC_TRUE;
1248 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1249 playlist_ViewUpdate( p_playlist, i_new_view );
1251 i_current_view = i_new_view;
1253 Rebuild( VLC_TRUE );
1256 else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1258 if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1259 pp_sds[event.GetId() - FirstSD_Event] ) )
1261 playlist_ServicesDiscoveryAdd( p_playlist,
1262 pp_sds[event.GetId() - FirstSD_Event] );
1266 //wxMutexGuiLeave();
1267 playlist_ServicesDiscoveryRemove( p_playlist,
1268 pp_sds[event.GetId() - FirstSD_Event] );
1269 //wxMutexGuiEnter();
1274 wxMenu * Playlist::ViewMenu()
1278 p_view_menu = new wxMenu;
1282 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1285 wxMenuItem *item = node->GetData();
1286 node = node->GetNext();
1287 p_view_menu->Delete( item );
1291 /* FIXME : have a list of "should have" views */
1292 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1293 wxU(_("Normal") ) );
1294 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1295 wxU(_("Sorted by artist") ) );
1296 p_view_menu->Append( FirstView_Event + VIEW_S_ALBUM,
1297 wxU(_("Sorted by Album") ) );
1302 wxMenu *Playlist::SDMenu()
1304 p_sd_menu = new wxMenu;
1306 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1310 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1312 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1314 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1317 if( i_number ) pp_sds = (char **)calloc( i_number, sizeof(void *) );
1320 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1322 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1324 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1326 p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1327 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1328 (p_parser->psz_shortname ?
1329 p_parser->psz_shortname : p_parser->psz_object_name) ) );
1331 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1332 p_parser->psz_object_name ) )
1334 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1337 pp_sds[i_number++] = p_parser->psz_object_name;
1340 vlc_list_release( p_list );
1345 /*****************************************************************************
1346 * Popup management functions
1347 *****************************************************************************/
1348 void Playlist::OnPopup( wxContextMenuEvent& event )
1350 wxPoint pt = event.GetPosition();
1351 playlist_item_t *p_item;
1353 i_wx_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1354 if( i_wx_popup_item.IsOk() )
1356 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1358 PlaylistItem *p_wxparent= (PlaylistItem *)treectrl->GetItemData(
1359 treectrl->GetItemParent( i_wx_popup_item ) );
1360 i_popup_item = p_wxitem->i_id;
1361 i_popup_parent = p_wxparent->i_id;
1362 treectrl->SelectItem( i_wx_popup_item );
1364 LockPlaylist( p_intf->p_sys, p_playlist );
1365 p_item = playlist_ItemGetById( p_playlist, i_popup_item );
1369 UnlockPlaylist( p_intf->p_sys, p_playlist );
1372 if( p_item->i_children == -1 )
1374 UnlockPlaylist( p_intf->p_sys, p_playlist );
1375 Playlist::PopupMenu( item_popup,
1376 ScreenToClient( wxGetMousePosition() ) );
1380 UnlockPlaylist( p_intf->p_sys, p_playlist );
1381 Playlist::PopupMenu( node_popup,
1382 ScreenToClient( wxGetMousePosition() ) );
1387 void Playlist::OnPopupPlay( wxCommandEvent& event )
1389 playlist_item_t *p_popup_item, *p_popup_parent;
1390 LockPlaylist( p_intf->p_sys, p_playlist );
1391 p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1392 p_popup_parent = playlist_ItemGetById( p_playlist, i_popup_parent );
1393 if( p_popup_item != NULL )
1395 if( p_popup_item->i_children > -1 )
1397 if( event.GetId() == PopupPlay_Event &&
1398 p_popup_item->i_children > 0 )
1400 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1401 i_current_view, p_popup_item,
1402 p_popup_item->pp_children[0] );
1406 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1407 i_current_view, p_popup_item, NULL );
1412 if( event.GetId() == PopupPlay_Event )
1414 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1415 i_current_view, p_popup_parent,
1420 UnlockPlaylist( p_intf->p_sys, p_playlist );
1423 void Playlist::OnPopupPreparse( wxCommandEvent& event )
1428 void Playlist::Preparse()
1430 playlist_item_t *p_popup_item;
1431 LockPlaylist( p_intf->p_sys, p_playlist );
1432 p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1434 if( p_popup_item != NULL )
1436 if( p_popup_item->i_children == -1 )
1438 playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1443 playlist_item_t *p_parent = p_popup_item;
1444 for( i = 0; i< p_parent->i_children ; i++ )
1447 i_wx_popup_item = FindItem( treectrl->GetRootItem(),
1448 p_parent->pp_children[i]->input.i_id );
1449 i_popup_item = p_parent->pp_children[i]->input.i_id;
1454 UnlockPlaylist( p_intf->p_sys, p_playlist );
1457 void Playlist::OnPopupDel( wxCommandEvent& event )
1459 DeleteTreeItem( i_wx_popup_item );
1462 void Playlist::OnPopupSort( wxCommandEvent& event )
1464 PlaylistItem *p_wxitem;
1465 playlist_item_t *p_item;
1467 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_wx_popup_item );
1468 LockPlaylist( p_intf->p_sys, p_playlist );
1470 p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
1471 if( p_item->i_children >= 0 )
1473 playlist_RecursiveNodeSort( p_playlist, p_item,
1474 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1476 treectrl->DeleteChildren( i_wx_popup_item );
1478 UpdateNodeChildren( p_item, i_wx_popup_item );
1481 UnlockPlaylist( p_intf->p_sys, p_playlist );
1484 void Playlist::OnPopupInfo( wxCommandEvent& event )
1486 LockPlaylist( p_intf->p_sys, p_playlist );
1487 playlist_item_t *p_popup_item = playlist_ItemGetById( p_playlist, i_popup_item );
1490 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1491 if( iteminfo_dialog->ShowModal() == wxID_OK )
1493 UpdateItem( i_wx_popup_item );
1495 delete iteminfo_dialog;
1497 UnlockPlaylist( p_intf->p_sys, p_playlist );
1501 /*****************************************************************************
1502 * Custom events management
1503 *****************************************************************************/
1504 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1506 switch( event.GetId() )
1508 case UpdateItem_Event:
1509 UpdateItem( event.GetInt() );
1511 case AppendItem_Event:
1512 AppendItem( event );
1514 case RemoveItem_Event:
1515 RemoveItem( event.GetInt() );
1520 /*****************************************************************************
1521 * PlaylistChanged: callback triggered by the intf-change playlist variable
1522 * We don't rebuild the playlist directly here because we don't want the
1523 * caller to block for a too long time.
1524 *****************************************************************************/
1525 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1526 vlc_value_t oval, vlc_value_t nval, void *param )
1528 Playlist *p_playlist_dialog = (Playlist *)param;
1529 p_playlist_dialog->b_need_update = VLC_TRUE;
1533 /*****************************************************************************
1534 * Next: callback triggered by the playlist-current playlist variable
1535 *****************************************************************************/
1536 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1537 vlc_value_t oval, vlc_value_t nval, void *param )
1539 Playlist *p_playlist_dialog = (Playlist *)param;
1541 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1542 event.SetInt( oval.i_int );
1543 p_playlist_dialog->AddPendingEvent( event );
1544 event.SetInt( nval.i_int );
1545 p_playlist_dialog->AddPendingEvent( event );
1550 /*****************************************************************************
1551 * ItemChanged: callback triggered by the item-change playlist variable
1552 *****************************************************************************/
1553 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1554 vlc_value_t old_val, vlc_value_t new_val, void *param )
1556 Playlist *p_playlist_dialog = (Playlist *)param;
1558 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1559 event.SetInt( new_val.i_int );
1560 p_playlist_dialog->AddPendingEvent( event );
1564 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1565 vlc_value_t old_val, vlc_value_t new_val, void *param )
1567 Playlist *p_playlist_dialog = (Playlist *)param;
1569 wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1570 event.SetInt( new_val.i_int );
1571 p_playlist_dialog->AddPendingEvent( event );
1576 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1577 vlc_value_t oval, vlc_value_t nval, void *param )
1579 Playlist *p_playlist_dialog = (Playlist *)param;
1581 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1582 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1584 if( ++p_playlist_dialog->i_items_to_append >= 50 )
1586 /* Too many items waiting to be added, it will be quicker to rebuild
1587 * the whole playlist */
1588 p_playlist_dialog->b_need_update = VLC_TRUE;
1592 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1593 event.SetClientData( (void *)p_add );
1594 p_playlist_dialog->AddPendingEvent( event );