1 /*****************************************************************************
2 * playlist.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004 VideoLAN
7 * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/OR MODIFy
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include "wxwindows.h"
33 #include "bitmaps/shuffle.xpm"
34 #include "bitmaps/repeat.xpm"
35 #include "bitmaps/loop.xpm"
37 #include "bitmaps/type_unknown.xpm"
38 #include "bitmaps/type_net.xpm"
39 #include "bitmaps/type_card.xpm"
40 #include "bitmaps/type_disc.xpm"
41 #include "bitmaps/type_directory.xpm"
42 #include "bitmaps/type_playlist.xpm"
44 #include <wx/dynarray.h>
45 #include <wx/imaglist.h>
47 #define HELP_SHUFFLE N_( "Shuffle" )
48 #define HELP_LOOP N_( "Loop" )
49 #define HELP_REPEAT N_( "Repeat" )
51 /* Callback prototype */
52 static int PlaylistChanged( vlc_object_t *, const char *,
53 vlc_value_t, vlc_value_t, void * );
54 static int PlaylistNext( vlc_object_t *, const char *,
55 vlc_value_t, vlc_value_t, void * );
56 static int ItemChanged( vlc_object_t *, const char *,
57 vlc_value_t, vlc_value_t, void * );
58 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
59 vlc_value_t oval, vlc_value_t nval, void *param );
62 /*****************************************************************************
64 *****************************************************************************/
66 /* IDs for the controls and the menu commands */
81 EnableSelection_Event,
82 DisableSelection_Event,
84 InvertSelection_Event,
85 DeleteSelection_Event,
104 Browse_Event, /* For export playlist */
110 MenuDummy_Event = wxID_HIGHEST + 999,
112 FirstView_Event = wxID_HIGHEST + 1000,
113 LastView_Event = wxID_HIGHEST + 1100,
115 FirstSD_Event = wxID_HIGHEST + 2000,
116 LastSD_Event = wxID_HIGHEST + 2100,
119 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
121 BEGIN_EVENT_TABLE(Playlist, wxFrame)
122 EVT_SIZE(Playlist::OnSize)
125 EVT_MENU(AddFile_Event, Playlist::OnAddFile)
126 EVT_MENU(AddDir_Event, Playlist::OnAddDir)
127 EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
128 EVT_MENU(Close_Event, Playlist::OnClose)
129 EVT_MENU(Open_Event, Playlist::OnOpen)
130 EVT_MENU(Save_Event, Playlist::OnSave)
132 EVT_MENU(SortTitle_Event, Playlist::OnSort)
133 EVT_MENU(RSortTitle_Event, Playlist::OnSort)
135 EVT_MENU(Randomize_Event, Playlist::OnSort)
137 EVT_MENU(EnableSelection_Event, Playlist::OnEnableSelection)
138 EVT_MENU(DisableSelection_Event, Playlist::OnDisableSelection)
139 EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
140 EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
141 EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
143 EVT_MENU_OPEN( Playlist::OnMenuOpen )
144 EVT_MENU( -1, Playlist::OnMenuEvent )
146 EVT_TOOL(Random_Event, Playlist::OnRandom)
147 EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
148 EVT_TOOL(Loop_Event, Playlist::OnLoop)
151 EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
152 EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
153 EVT_MENU( PopupSort_Event, Playlist::OnPopupSort)
154 EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
155 EVT_MENU( PopupEna_Event, Playlist::OnPopupEna)
156 EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
158 /* Tree control events */
159 EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
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)
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()
188 playlist_item_t *p_item;
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(345,400), wxDEFAULT_FRAME_STYLE )
201 /* Initializations */
203 i_update_counter = 0;
204 i_sort_mode = MODE_NONE;
205 b_need_update = VLC_FALSE;
206 SetIcon( *p_intf->p_sys->p_icon );
209 p_sd_menu = SDMenu();
211 i_current_view = VIEW_SIMPLE;
212 b_changed_view = VLC_FALSE;
216 i_duration_sorted = 0;
218 var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
219 var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
220 var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
222 /* Create our "Manage" menu */
223 wxMenu *manage_menu = new wxMenu;
224 manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
225 manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
226 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
227 manage_menu->AppendSeparator();
228 manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
230 manage_menu->AppendSeparator();
231 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
232 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
233 manage_menu->AppendSeparator();
234 manage_menu->Append( Close_Event, wxU(_("&Close")) );
236 /* Create our "Sort" menu */
237 wxMenu *sort_menu = new wxMenu;
238 sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
239 sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
240 sort_menu->AppendSeparator();
241 sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
243 /* Create our "Selection" menu */
244 wxMenu *selection_menu = new wxMenu;
245 selection_menu->Append( EnableSelection_Event, wxU(_("&Enable")) );
246 selection_menu->Append( DisableSelection_Event, wxU(_("&Disable")) );
247 selection_menu->AppendSeparator();
248 selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
249 selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
250 selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
252 /* Create our "View" menu */
255 /* Append the freshly created menus to the menu bar */
256 wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
257 menubar->Append( manage_menu, wxU(_("&Manage")) );
258 menubar->Append( sort_menu, wxU(_("S&ort")) );
259 menubar->Append( selection_menu, wxU(_("&Selection")) );
260 menubar->Append( p_view_menu, wxU(_("&View items") ) );
262 /* Attach the menu bar to the frame */
263 SetMenuBar( menubar );
265 /* Create the popup menu */
266 popup_menu = new wxMenu;
267 popup_menu->Append( PopupPlay_Event, wxU(_("Play")) );
268 popup_menu->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
269 popup_menu->Append( PopupSort_Event, wxU(_("Sort this branch")) );
270 popup_menu->Append( PopupDel_Event, wxU(_("Delete")) );
271 popup_menu->Append( PopupEna_Event, wxU(_("Enable/Disable")) );
272 popup_menu->Append( PopupInfo_Event, wxU(_("Info")) );
274 /* Create a panel to put everything in */
275 wxPanel *playlist_panel = new wxPanel( this, -1 );
276 playlist_panel->SetAutoLayout( TRUE );
278 /* Create the toolbar */
280 CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT | wxTB_DOCKABLE );
282 /* Create the random tool */
283 toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
284 wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
285 wxU(_(HELP_SHUFFLE) ) );
286 var_Get( p_intf, "random", &val );
287 toolbar->ToggleTool( Random_Event, val.b_bool );
289 /* Create the Loop tool */
290 toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
291 wxBitmap( loop_xpm), wxITEM_CHECK,
292 wxU(_(HELP_LOOP ) ) );
293 var_Get( p_intf, "loop", &val );
294 toolbar->ToggleTool( Loop_Event, val.b_bool );
296 /* Create the Repeat one checkbox */
297 toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
298 wxBitmap( repeat_xpm), wxITEM_CHECK,
299 wxU(_(HELP_REPEAT ) ) );
300 var_Get( p_intf, "repeat", &val );
301 toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
303 /* Create the Search Textbox */
304 search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
305 wxDefaultPosition, wxSize(100, -1),
308 /* Create the search button */
309 search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
311 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
312 wxSize(16, 16), wxBORDER_NONE ) );
313 toolbar->AddControl( search_text );
314 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
315 wxSize(5, 5), wxBORDER_NONE ) );
316 toolbar->AddControl( search_button );
317 search_button->SetDefault();
320 /* Create the tree */
321 treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
322 wxDefaultPosition, wxDefaultSize,
323 wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
325 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
326 wxTR_MULTIPLE | wxTR_EXTENDED );
328 /* Create image list */
329 wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
331 /* FIXME: absolutely needs to be in the right order FIXME */
332 p_images->Add( wxIcon( type_unknown_xpm ) );
333 p_images->Add( wxIcon( type_unknown_xpm ) );
334 p_images->Add( wxIcon( type_directory_xpm ) );
335 p_images->Add( wxIcon( type_disc_xpm ) );
336 p_images->Add( wxIcon( type_card_xpm ) );
337 p_images->Add( wxIcon( type_net_xpm ) );
338 p_images->Add( wxIcon( type_playlist_xpm ) );
339 treectrl->AssignImageList( p_images );
341 treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
343 /* Reduce font size */
344 wxFont font= treectrl->GetFont();
345 font.SetPointSize(8);
346 treectrl->SetFont( font );
348 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
349 panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
350 panel_sizer->Layout();
352 playlist_panel->SetSizerAndFit( panel_sizer );
354 int pi_widths[1] = { -1 };
355 statusbar = CreateStatusBar( 1 );
356 statusbar->SetStatusWidths( 1, pi_widths );
358 #if wxUSE_DRAG_AND_DROP
359 /* Associate drop targets with the playlist */
360 SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
363 playlist_t *p_playlist =
364 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
366 if( p_playlist == NULL )
371 /* We want to be noticed of playlist changes */
373 /* Some global changes happened -> Rebuild all */
374 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
376 /* We went to the next item */
377 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
379 /* One item has been updated */
380 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
382 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
384 vlc_object_release( p_playlist );
387 /* Update the playlist */
391 Playlist::~Playlist()
393 playlist_t *p_playlist =
394 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
396 if( p_playlist == NULL )
401 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
402 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
403 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
404 vlc_object_release( p_playlist );
407 /**********************************************************************
409 **********************************************************************/
412 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
417 for( int i = 0; i< p_node->i_children ; i++ )
421 child = treectrl->GetFirstChild( node, cookie);
425 child = treectrl->GetNextChild( node, cookie );
430 /* Not enough children */
431 CreateNode( p_playlist, p_node->pp_children[i], node );
432 /* Keep the tree pointer up to date */
433 child = treectrl->GetNextChild( node, cookie );
439 treectrl->SetItemImage( node, p_node->input.i_type );
443 /* Creates the node p_node as last child of parent */
444 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
445 wxTreeItemId parent )
449 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
450 -1,-1, new PlaylistItem( p_node ) );
451 treectrl->SetItemImage( node, p_node->input.i_type );
453 UpdateNodeChildren( p_playlist, p_node, node );
456 /* Update all children (recursively) of this node */
457 void Playlist::UpdateNodeChildren( playlist_t *p_playlist,
458 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( p_playlist, item );
474 treectrl->SetItemImage( item,
475 p_node->pp_children[i]->input.i_type );
479 CreateNode( p_playlist, p_node->pp_children[i],
485 /* Set current item */
486 void Playlist::SetCurrentItem( wxTreeItemId item )
490 treectrl->SetItemBold( item, true );
491 treectrl->EnsureVisible( item );
495 /* Update an item in the tree */
496 void Playlist::UpdateTreeItem( playlist_t *p_playlist, wxTreeItemId item )
498 playlist_item_t *p_item =
499 ((PlaylistItem *)treectrl->GetItemData( item ))->p_item;
507 char *psz_author = playlist_ItemGetInfo( p_item, _("Meta-information"),
509 char psz_duration[MSTRTIME_MAX_SIZE];
510 mtime_t dur = p_item->input.i_duration;
513 secstotimestr( psz_duration, dur/1000000 );
515 memcpy( psz_duration, "-:--:--", sizeof("-:--:--") );
517 if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
519 msg.Printf( wxString( wxL2U( p_item->input.psz_name ) ) + wxU( " ( ") +
520 wxString(wxL2U(psz_duration ) ) + wxU( ")") );
524 msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
525 wxString(wxL2U(p_item->input.psz_name)) + wxU( " ( ") +
526 wxString(wxL2U(psz_duration ) ) + wxU( ")") );
528 treectrl->SetItemText( item , msg );
529 treectrl->SetItemImage( item, p_item->input.i_type );
531 if( p_playlist->status.p_item == p_item )
533 SetCurrentItem( item );
537 treectrl->SetItemBold( item, false );
541 /* Process a AppendItem request */
542 void Playlist::AppendItem( wxCommandEvent& event )
544 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
546 playlist_t *p_playlist =
547 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
549 wxTreeItemId item,node;
550 if( p_playlist == NULL )
556 if( p_add->i_view != i_current_view )
561 node = FindItem( treectrl->GetRootItem(), p_add->p_node );
567 item = treectrl->AppendItem( node,
568 wxL2U( p_add->p_item->input.psz_name ), -1,-1,
569 new PlaylistItem( p_add->p_item ) );
570 treectrl->SetItemImage( item, p_add->p_item->input.i_type );
572 if( item.IsOk() && p_add->p_item->i_children == -1 )
574 UpdateTreeItem( p_playlist, item );
578 int i_count = CountItems( treectrl->GetRootItem());
579 if( i_count != p_playlist->i_size )
581 statusbar->SetStatusText( wxString::Format( wxU(_(
582 "%i items in playlist (%i not shown)")),
584 p_playlist->i_size - i_count ) );
585 if( !b_changed_view )
587 i_current_view = VIEW_CATEGORY;
588 b_changed_view = VLC_TRUE;
589 b_need_update = VLC_TRUE;
594 statusbar->SetStatusText( wxString::Format( wxU(_(
595 "%i items in playlist")),
596 p_playlist->i_size ), 0 );
599 vlc_object_release( p_playlist );
603 /* Process a updateitem request */
604 void Playlist::UpdateItem( int i )
606 if( i < 0 ) return; /* Sanity check */
607 playlist_item_t *p_item;
609 playlist_t *p_playlist =
610 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
613 if( p_playlist == NULL )
618 p_item = playlist_ItemGetById( p_playlist, i );
620 wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
624 UpdateTreeItem( p_playlist, item );
627 vlc_object_release(p_playlist);
632 /**********************************************************************
633 * Search functions (internal
634 **********************************************************************/
636 /* Find a wxItem from a playlist_item */
637 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
640 PlaylistItem *p_wxcurrent;
642 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
645 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
647 if( p_wxcurrent->p_item == p_item )
660 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
661 if( p_wxcurrent->p_item == p_item )
665 if( treectrl->ItemHasChildren( item ) )
667 wxTreeItemId search = FindItem( item, p_item );
673 item = treectrl->GetNextChild( root, cookie );
680 int Playlist::CountItems( wxTreeItemId root )
684 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
688 if( treectrl->ItemHasChildren( item ) )
690 count += CountItems( item );
692 else if( ( (PlaylistItem *)treectrl->GetItemData( item ) )->
693 p_item->i_children == -1 )
695 item = treectrl->GetNextChild( root, cookie );
700 /* Find a wxItem from a name (from current) */
701 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
705 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
710 if( treectrl->GetItemText( item).Lower().Contains(
711 search_string.Lower() ) )
713 if( !current.IsOk() || *pb_current_found == VLC_TRUE )
717 else if( current.IsOk() && item == current )
719 *pb_current_found = VLC_TRUE;
722 if( treectrl->ItemHasChildren( item ) )
724 wxTreeItemId search = FindItemByName( item, search_string, current,
731 item = treectrl->GetNextChild( root, cookie);
738 /**********************************************************************
739 * Rebuild the playlist
740 **********************************************************************/
741 void Playlist::Rebuild()
743 playlist_view_t *p_view;
744 playlist_t *p_playlist =
745 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
747 if( p_playlist == NULL )
751 int i_count = CountItems( treectrl->GetRootItem()) ;
753 if( i_count > p_playlist->i_size && !b_changed_view )
755 i_current_view = VIEW_CATEGORY;
756 b_changed_view = VLC_TRUE;
759 /* ...and rebuild it */
760 vlc_mutex_lock( &p_playlist->object_lock );
762 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
764 /* HACK we should really get new*/
765 msg_Dbg( p_intf, "rebuilding tree" );
766 treectrl->DeleteAllItems();
767 treectrl->AddRoot( wxU(_("root" )), -1, -1,
768 new PlaylistItem( p_view->p_root) );
770 wxTreeItemId root = treectrl->GetRootItem();
771 UpdateNode( p_playlist, p_view->p_root, root );
774 if( p_playlist->status.p_item != NULL )
776 item = FindItem( root, p_playlist->status.p_item );
778 else if( p_playlist->status.p_node != NULL )
780 item = FindItem( root, p_playlist->status.p_node );
787 i_count = CountItems( treectrl->GetRootItem() );
788 if( i_count != p_playlist->i_size )
790 statusbar->SetStatusText( wxString::Format( wxU(_(
791 "%i items in playlist (%i not shown)")),
793 p_playlist->i_size - i_count ) );
797 statusbar->SetStatusText( wxString::Format( wxU(_(
798 "%i items in playlist")),
799 p_playlist->i_size ), 0 );
802 SetCurrentItem( item );
804 vlc_mutex_unlock( &p_playlist->object_lock );
806 vlc_object_release( p_playlist );
811 void Playlist::ShowPlaylist( bool show )
813 if( show ) Rebuild();
817 /* This function is called on a regular basis */
818 void Playlist::UpdatePlaylist()
822 /* If the playlist isn't show there's no need to update it */
823 if( !IsShown() ) return;
825 if( this->b_need_update )
827 this->b_need_update = VLC_FALSE;
831 /* Updating the playing status every 0.5s is enough */
832 if( i_update_counter % 5 ) return;
834 playlist_t *p_playlist =
835 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
837 if( p_playlist == NULL )
842 vlc_object_release( p_playlist );
845 /*****************************************************************************
847 *****************************************************************************/
848 void Playlist::DeleteItem( int item_id )
850 playlist_t *p_playlist =
851 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
853 if( p_playlist == NULL )
858 playlist_Delete( p_playlist, item_id );
860 vlc_object_release( p_playlist );
863 void Playlist::DeleteNode( playlist_item_t *p_item )
865 playlist_t *p_playlist =
866 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
868 if( p_playlist == NULL )
873 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE );
875 vlc_object_release( p_playlist );
879 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
884 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
890 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
891 { _("PLS file"), "*.pls", "export-pls" }};
893 wxString filter = wxT("");
895 playlist_t * p_playlist =
896 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
903 if( p_playlist->i_size == 0 )
905 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
906 wxICON_WARNING | wxOK, this );
907 vlc_object_release( p_playlist );
911 for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
913 filter.Append( wxU(formats[i].psz_desc) );
914 filter.Append( wxT("|") );
915 filter.Append( wxU(formats[i].psz_filter) );
916 filter.Append( wxT("|") );
918 wxFileDialog dialog( this, wxU(_("Save playlist")),
919 wxT(""), wxT(""), filter, wxSAVE );
921 if( dialog.ShowModal() == wxID_OK )
923 if( dialog.GetPath().mb_str() )
925 playlist_Export( p_playlist, dialog.GetPath().mb_str(),
926 formats[dialog.GetFilterIndex()].psz_module );
930 vlc_object_release( p_playlist );
934 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
936 playlist_t *p_playlist =
937 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
939 if( p_playlist == NULL )
944 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
945 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
947 if( dialog.ShowModal() == wxID_OK )
949 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
952 vlc_object_release( p_playlist );
955 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
957 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
961 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
963 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
967 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
969 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
973 /********************************************************************
975 ********************************************************************/
976 void Playlist::OnSort( wxCommandEvent& event )
978 playlist_t *p_playlist =
979 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
981 PlaylistItem *p_wxitem;
982 p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
984 if( p_playlist == NULL )
988 vlc_mutex_lock( &p_playlist->object_lock );
989 switch( event.GetId() )
991 case SortTitle_Event:
992 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
993 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
995 case RSortTitle_Event:
996 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
997 SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
999 vlc_mutex_unlock( &p_playlist->object_lock );
1001 vlc_object_release( p_playlist );
1005 /**********************************************************************
1006 * Search functions (user)
1007 **********************************************************************/
1008 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
1010 search_button->SetDefault();
1013 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
1015 wxString search_string = search_text->GetValue();
1017 vlc_bool_t pb_found = VLC_FALSE;
1019 wxTreeItemId found =
1020 FindItemByName( treectrl->GetRootItem(), search_string,
1021 search_current, &pb_found );
1025 search_current = found;
1026 treectrl->SelectItem( found, true );
1031 search_current = dummy;
1032 found = FindItemByName( treectrl->GetRootItem(), search_string,
1033 search_current, &pb_found );
1036 search_current = found;
1037 treectrl->SelectItem( found, true );
1042 /**********************************************************************
1043 * Selection functions
1044 **********************************************************************/
1045 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1049 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1054 void Playlist::OnEnableSelection( wxCommandEvent& WXUNUSED(event) )
1056 playlist_t *p_playlist =
1057 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1059 if( p_playlist == NULL )
1063 msg_Warn( p_playlist, "not implemented");
1064 vlc_object_release( p_playlist);
1067 void Playlist::OnDisableSelection( wxCommandEvent& WXUNUSED(event) )
1069 playlist_t *p_playlist =
1070 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1072 if( p_playlist == NULL )
1077 msg_Warn( p_playlist, "not implemented");
1079 vlc_object_release( p_playlist);
1082 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1086 /**********************************************************************
1087 * Playlist mode functions
1088 **********************************************************************/
1089 void Playlist::OnRandom( wxCommandEvent& event )
1092 val.b_bool = event.IsChecked();
1093 playlist_t *p_playlist =
1094 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1096 if( p_playlist == NULL )
1100 var_Set( p_playlist, "random", val);
1101 vlc_object_release( p_playlist );
1104 void Playlist::OnLoop( wxCommandEvent& event )
1107 val.b_bool = event.IsChecked();
1108 playlist_t *p_playlist =
1109 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1111 if( p_playlist == NULL )
1115 var_Set( p_playlist, "loop", val);
1116 vlc_object_release( p_playlist );
1119 void Playlist::OnRepeat( wxCommandEvent& event )
1122 val.b_bool = event.IsChecked();
1123 playlist_t *p_playlist =
1124 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1126 if( p_playlist == NULL )
1130 var_Set( p_playlist, "repeat", val);
1131 vlc_object_release( p_playlist );
1134 /********************************************************************
1136 ********************************************************************/
1137 void Playlist::OnActivateItem( wxTreeEvent& event )
1139 playlist_item_t *p_item,*p_node;
1140 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1141 VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1142 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1144 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1146 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1148 if( p_playlist == NULL )
1153 if( p_wxitem->p_item->i_children == -1 )
1155 p_node = p_wxparent->p_item;
1156 p_item = p_wxitem->p_item;
1160 p_node = p_wxitem->p_item;
1161 if( p_wxitem->p_item->i_children > 0 &&
1162 p_wxitem->p_item->pp_children[0]->i_children == -1)
1164 p_item = p_wxitem->p_item->pp_children[0];
1172 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1175 vlc_object_release( p_playlist );
1178 void Playlist::OnKeyDown( wxTreeEvent& event )
1180 long keycode = event.GetKeyCode();
1181 /* Delete selected items */
1182 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1184 /* We send a dummy event */
1185 OnDeleteSelection( event );
1189 void Playlist::OnEnDis( wxCommandEvent& event )
1191 playlist_t *p_playlist =
1192 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1194 if( p_playlist == NULL )
1198 msg_Warn( p_intf, "not implemented" );
1199 vlc_object_release( p_playlist );
1202 /**********************************************************************
1204 **********************************************************************/
1206 void Playlist::OnMenuOpen( wxMenuEvent& event)
1208 #if defined( __WXMSW__ )
1209 # define GetEventObject GetMenu
1212 if( event.GetEventObject() == p_view_menu )
1214 p_view_menu = ViewMenu();
1216 #if defined( __WXMSW__ )
1217 # undef GetEventObject
1221 void Playlist::OnMenuEvent( wxCommandEvent& event )
1223 playlist_t *p_playlist =
1224 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1226 if( p_playlist == NULL )
1231 if( event.GetId() < FirstView_Event )
1234 vlc_object_release( p_playlist );
1237 else if( event.GetId() < LastView_Event )
1240 int i_new_view = event.GetId() - FirstView_Event;
1242 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1244 if( p_view != NULL )
1246 b_changed_view = VLC_TRUE;
1247 i_current_view = i_new_view;
1248 playlist_ViewUpdate( p_playlist, i_new_view );
1250 vlc_object_release( p_playlist );
1253 else if( i_new_view >= VIEW_FIRST_SORTED &&
1254 i_new_view <= VIEW_LAST_SORTED )
1256 b_changed_view = VLC_TRUE;
1257 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1258 playlist_ViewUpdate( p_playlist, i_new_view );
1260 i_current_view = i_new_view;
1265 else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1267 if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1268 pp_sds[event.GetId() - FirstSD_Event] ) )
1270 playlist_ServicesDiscoveryAdd( p_playlist,
1271 pp_sds[event.GetId() - FirstSD_Event] );
1275 playlist_ServicesDiscoveryRemove( p_playlist,
1276 pp_sds[event.GetId() - FirstSD_Event] );
1279 vlc_object_release( p_playlist );
1282 wxMenu * Playlist::ViewMenu()
1284 playlist_t *p_playlist =
1285 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1287 if( p_playlist == NULL )
1294 p_view_menu = new wxMenu;
1298 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1301 wxMenuItem *item = node->GetData();
1302 node = node->GetNext();
1303 p_view_menu->Delete( item );
1307 /* FIXME : have a list of "should have" views */
1308 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1309 wxU(_("By category") ) );
1310 p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1311 wxU(_("Manually added") ) );
1312 p_view_menu->Append( FirstView_Event + VIEW_ALL,
1313 wxU(_("All items, unsorted") ) );
1314 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1315 wxU(_("Sorted by author") ) );
1317 vlc_object_release( p_playlist);
1322 wxMenu *Playlist::SDMenu()
1325 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1326 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1331 p_sd_menu = new wxMenu;
1333 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1337 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1339 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1341 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1343 p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1344 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1345 p_parser->psz_shortname ) );
1347 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1348 p_parser->psz_shortname ) )
1350 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1353 INSERT_ELEM( (void**)pp_sds, i_number, i_number,
1354 (void*)p_parser->psz_shortname );
1357 vlc_list_release( p_list );
1358 vlc_object_release( p_playlist );
1363 /*****************************************************************************
1364 * Popup management functions
1365 *****************************************************************************/
1366 void Playlist::OnPopup( wxContextMenuEvent& event )
1368 wxPoint pt = event.GetPosition();
1370 i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1371 if( i_popup_item.IsOk() )
1373 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1375 PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1376 treectrl->GetItemParent( i_popup_item ) );
1377 p_popup_item = p_wxitem->p_item;
1378 p_popup_parent = p_wxparent->p_item;
1379 treectrl->SelectItem( i_popup_item );
1380 Playlist::PopupMenu( popup_menu,
1381 ScreenToClient( wxGetMousePosition() ) );
1388 void Playlist::OnPopupPlay( wxMenuEvent& event )
1390 playlist_t *p_playlist =
1391 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1393 if( p_playlist == NULL )
1397 if( p_popup_item != NULL )
1399 if( p_popup_item->i_children > -1 )
1401 if( event.GetId() == PopupPlay_Event &&
1402 p_popup_item->i_children > 0 )
1404 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1405 i_current_view, p_popup_item,
1406 p_popup_item->pp_children[0] );
1410 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1411 i_current_view, p_popup_item, NULL );
1416 if( event.GetId() == PopupPlay_Event )
1418 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1419 i_current_view, p_popup_parent,
1424 vlc_object_release( p_playlist );
1427 void Playlist::OnPopupDel( wxMenuEvent& event )
1429 PlaylistItem *p_wxitem;
1431 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1433 if( p_wxitem->p_item->i_children == -1 )
1435 DeleteItem( p_wxitem->p_item->input.i_id );
1439 DeleteNode( p_wxitem->p_item );
1443 void Playlist::OnPopupSort( wxMenuEvent& event )
1445 PlaylistItem *p_wxitem;
1447 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1449 if( p_wxitem->p_item->i_children >= 0 )
1451 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1452 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1456 vlc_mutex_lock( &p_playlist->object_lock );
1457 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1458 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1459 vlc_mutex_unlock( &p_playlist->object_lock );
1461 treectrl->DeleteChildren( i_popup_item );
1462 UpdateNodeChildren( p_playlist, p_wxitem->p_item, i_popup_item );
1464 vlc_object_release( p_playlist );
1469 void Playlist::OnPopupEna( wxMenuEvent& event )
1471 playlist_t *p_playlist =
1472 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1474 if( p_playlist == NULL )
1479 p_popup_item->b_enabled = VLC_TRUE - p_popup_item->b_enabled;
1481 vlc_object_release( p_playlist);
1482 UpdateItem( i_popup_item );
1485 void Playlist::OnPopupInfo( wxMenuEvent& event )
1489 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1490 if( iteminfo_dialog->ShowModal() == wxID_OK )
1492 UpdateItem( i_popup_item );
1494 delete iteminfo_dialog;
1499 /*****************************************************************************
1500 * Custom events management
1501 *****************************************************************************/
1502 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1504 switch( event.GetId() )
1506 case UpdateItem_Event:
1507 UpdateItem( event.GetInt() );
1509 case AppendItem_Event:
1510 AppendItem( event );
1515 /*****************************************************************************
1516 * PlaylistChanged: callback triggered by the intf-change playlist variable
1517 * We don't rebuild the playlist directly here because we don't want the
1518 * caller to block for a too long time.
1519 *****************************************************************************/
1520 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1521 vlc_value_t oval, vlc_value_t nval, void *param )
1523 Playlist *p_playlist_dialog = (Playlist *)param;
1524 p_playlist_dialog->b_need_update = VLC_TRUE;
1528 /*****************************************************************************
1529 * Next: callback triggered by the playlist-current playlist variable
1530 *****************************************************************************/
1531 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1532 vlc_value_t oval, vlc_value_t nval, void *param )
1534 Playlist *p_playlist_dialog = (Playlist *)param;
1536 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1537 event.SetInt( oval.i_int );
1538 p_playlist_dialog->AddPendingEvent( event );
1539 event.SetInt( nval.i_int );
1540 p_playlist_dialog->AddPendingEvent( event );
1545 /*****************************************************************************
1546 * ItemChanged: callback triggered by the item-change playlist variable
1547 *****************************************************************************/
1548 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1549 vlc_value_t old_val, vlc_value_t new_val, void *param )
1551 Playlist *p_playlist_dialog = (Playlist *)param;
1553 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1554 event.SetInt( new_val.i_int );
1555 p_playlist_dialog->AddPendingEvent( event );
1560 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1561 vlc_value_t oval, vlc_value_t nval, void *param )
1563 Playlist *p_playlist_dialog = (Playlist *)param;
1565 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1566 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1568 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1569 event.SetClientData( (void *)p_add );
1570 p_playlist_dialog->AddPendingEvent( event );