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,
105 Browse_Event, /* For export playlist */
111 MenuDummy_Event = wxID_HIGHEST + 999,
113 FirstView_Event = wxID_HIGHEST + 1000,
114 LastView_Event = wxID_HIGHEST + 1100,
116 FirstSD_Event = wxID_HIGHEST + 2000,
117 LastSD_Event = wxID_HIGHEST + 2100,
120 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
122 BEGIN_EVENT_TABLE(Playlist, wxFrame)
123 EVT_SIZE(Playlist::OnSize)
126 EVT_MENU(AddFile_Event, Playlist::OnAddFile)
127 EVT_MENU(AddDir_Event, Playlist::OnAddDir)
128 EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
129 EVT_MENU(Close_Event, Playlist::OnClose)
130 EVT_MENU(Open_Event, Playlist::OnOpen)
131 EVT_MENU(Save_Event, Playlist::OnSave)
133 EVT_MENU(SortTitle_Event, Playlist::OnSort)
134 EVT_MENU(RSortTitle_Event, Playlist::OnSort)
136 EVT_MENU(Randomize_Event, Playlist::OnSort)
138 EVT_MENU(EnableSelection_Event, Playlist::OnEnableSelection)
139 EVT_MENU(DisableSelection_Event, Playlist::OnDisableSelection)
140 EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
141 EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
142 EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
144 EVT_MENU_OPEN( Playlist::OnMenuOpen )
145 EVT_MENU( -1, Playlist::OnMenuEvent )
147 EVT_TOOL(Random_Event, Playlist::OnRandom)
148 EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
149 EVT_TOOL(Loop_Event, Playlist::OnLoop)
152 EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
153 EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
154 EVT_MENU( PopupPreparse_Event, Playlist::OnPopupPreparse)
155 EVT_MENU( PopupSort_Event, Playlist::OnPopupSort)
156 EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
157 EVT_MENU( PopupEna_Event, Playlist::OnPopupEna)
158 EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
160 /* Tree control events */
161 EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
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)
172 EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
174 /* Special events : we don't want to destroy the window when the user
176 EVT_CLOSE(Playlist::OnClose)
179 /*****************************************************************************
181 ****************************************************************************/
182 class PlaylistItem : public wxTreeItemData
185 PlaylistItem( playlist_item_t *_p_item ) : wxTreeItemData()
190 playlist_item_t *p_item;
191 friend class Playlist;
194 /*****************************************************************************
196 *****************************************************************************/
197 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
198 wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
199 wxSize(345,400), wxDEFAULT_FRAME_STYLE )
203 /* Initializations */
205 i_update_counter = 0;
206 i_sort_mode = MODE_NONE;
207 b_need_update = VLC_FALSE;
208 SetIcon( *p_intf->p_sys->p_icon );
211 p_sd_menu = SDMenu();
213 i_current_view = VIEW_SIMPLE;
214 b_changed_view = VLC_FALSE;
218 i_duration_sorted = 0;
220 var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
221 var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
222 var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
224 /* Create our "Manage" menu */
225 wxMenu *manage_menu = new wxMenu;
226 manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
227 manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
228 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
229 manage_menu->AppendSeparator();
230 manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
232 manage_menu->AppendSeparator();
233 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
234 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
235 manage_menu->AppendSeparator();
236 manage_menu->Append( Close_Event, wxU(_("&Close")) );
238 /* Create our "Sort" menu */
239 wxMenu *sort_menu = new wxMenu;
240 sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
241 sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
242 sort_menu->AppendSeparator();
243 sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
245 /* Create our "Selection" menu */
246 wxMenu *selection_menu = new wxMenu;
247 selection_menu->Append( EnableSelection_Event, wxU(_("&Enable")) );
248 selection_menu->Append( DisableSelection_Event, wxU(_("&Disable")) );
249 selection_menu->AppendSeparator();
250 selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
251 selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
252 selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
254 /* Create our "View" menu */
257 /* Append the freshly created menus to the menu bar */
258 wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
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 popup_menu = new wxMenu;
269 popup_menu->Append( PopupPlay_Event, wxU(_("Play")) );
270 popup_menu->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
271 popup_menu->Append( PopupPreparse_Event, wxU(_("Preparse")) );
272 popup_menu->Append( PopupSort_Event, wxU(_("Sort this branch")) );
273 popup_menu->Append( PopupDel_Event, wxU(_("Delete")) );
274 popup_menu->Append( PopupEna_Event, wxU(_("Enable/Disable")) );
275 popup_menu->Append( PopupInfo_Event, wxU(_("Info")) );
277 /* Create a panel to put everything in */
278 wxPanel *playlist_panel = new wxPanel( this, -1 );
279 playlist_panel->SetAutoLayout( TRUE );
281 /* Create the toolbar */
283 CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT | wxTB_DOCKABLE );
285 /* Create the random tool */
286 toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
287 wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
288 wxU(_(HELP_SHUFFLE) ) );
289 var_Get( p_intf, "random", &val );
290 toolbar->ToggleTool( Random_Event, val.b_bool );
292 /* Create the Loop tool */
293 toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
294 wxBitmap( loop_xpm), wxITEM_CHECK,
295 wxU(_(HELP_LOOP ) ) );
296 var_Get( p_intf, "loop", &val );
297 toolbar->ToggleTool( Loop_Event, val.b_bool );
299 /* Create the Repeat one checkbox */
300 toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
301 wxBitmap( repeat_xpm), wxITEM_CHECK,
302 wxU(_(HELP_REPEAT ) ) );
303 var_Get( p_intf, "repeat", &val );
304 toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
306 /* Create the Search Textbox */
307 search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
308 wxDefaultPosition, wxSize(100, -1),
311 /* Create the search button */
312 search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
314 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
315 wxSize(16, 16), wxBORDER_NONE ) );
316 toolbar->AddControl( search_text );
317 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
318 wxSize(5, 5), wxBORDER_NONE ) );
319 toolbar->AddControl( search_button );
320 search_button->SetDefault();
323 /* Create the tree */
324 treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
325 wxDefaultPosition, wxDefaultSize,
326 wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
328 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
329 wxTR_MULTIPLE | wxTR_EXTENDED );
331 /* Create image list */
332 wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
334 /* FIXME: absolutely needs to be in the right order FIXME */
335 p_images->Add( wxIcon( type_unknown_xpm ) );
336 p_images->Add( wxIcon( type_unknown_xpm ) );
337 p_images->Add( wxIcon( type_directory_xpm ) );
338 p_images->Add( wxIcon( type_disc_xpm ) );
339 p_images->Add( wxIcon( type_card_xpm ) );
340 p_images->Add( wxIcon( type_net_xpm ) );
341 p_images->Add( wxIcon( type_playlist_xpm ) );
342 treectrl->AssignImageList( p_images );
344 treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
346 /* Reduce font size */
347 wxFont font= treectrl->GetFont();
348 font.SetPointSize(8);
349 treectrl->SetFont( font );
351 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
352 panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
353 panel_sizer->Layout();
355 playlist_panel->SetSizerAndFit( panel_sizer );
357 int pi_widths[1] = { -1 };
358 statusbar = CreateStatusBar( 1 );
359 statusbar->SetStatusWidths( 1, pi_widths );
361 #if wxUSE_DRAG_AND_DROP
362 /* Associate drop targets with the playlist */
363 SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
366 playlist_t *p_playlist =
367 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
369 if( p_playlist == NULL )
374 /* We want to be noticed of playlist changes */
376 /* Some global changes happened -> Rebuild all */
377 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
379 /* We went to the next item */
380 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
382 /* One item has been updated */
383 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
385 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
387 vlc_object_release( p_playlist );
390 /* Update the playlist */
394 Playlist::~Playlist()
396 playlist_t *p_playlist =
397 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
399 if( p_playlist == NULL )
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 vlc_object_release( p_playlist );
410 /**********************************************************************
412 **********************************************************************/
415 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
420 for( int i = 0; i< p_node->i_children ; i++ )
424 child = treectrl->GetFirstChild( node, cookie);
428 child = treectrl->GetNextChild( node, cookie );
433 /* Not enough children */
434 CreateNode( p_playlist, p_node->pp_children[i], node );
435 /* Keep the tree pointer up to date */
436 child = treectrl->GetNextChild( node, cookie );
442 treectrl->SetItemImage( node, p_node->input.i_type );
446 /* Creates the node p_node as last child of parent */
447 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
448 wxTreeItemId parent )
452 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
453 -1,-1, new PlaylistItem( p_node ) );
454 treectrl->SetItemImage( node, p_node->input.i_type );
456 UpdateNodeChildren( p_playlist, p_node, node );
459 /* Update all children (recursively) of this node */
460 void Playlist::UpdateNodeChildren( playlist_t *p_playlist,
461 playlist_item_t *p_node,
465 for( int i = 0; i< p_node->i_children ; i++ )
467 /* Append the item */
468 if( p_node->pp_children[i]->i_children == -1 )
471 treectrl->AppendItem( node,
472 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
473 new PlaylistItem( p_node->pp_children[i]) );
475 UpdateTreeItem( p_playlist, item );
477 treectrl->SetItemImage( item,
478 p_node->pp_children[i]->input.i_type );
482 CreateNode( p_playlist, p_node->pp_children[i],
488 /* Set current item */
489 void Playlist::SetCurrentItem( wxTreeItemId item )
493 treectrl->SetItemBold( item, true );
494 treectrl->EnsureVisible( item );
498 /* Update an item in the tree */
499 void Playlist::UpdateTreeItem( playlist_t *p_playlist, wxTreeItemId item )
501 playlist_item_t *p_item =
502 ((PlaylistItem *)treectrl->GetItemData( item ))->p_item;
510 char *psz_author = playlist_ItemGetInfo( p_item, _("Meta-information"),
512 char psz_duration[MSTRTIME_MAX_SIZE];
513 mtime_t dur = p_item->input.i_duration;
516 secstotimestr( psz_duration, dur/1000000 );
518 memcpy( psz_duration, "-:--:--", sizeof("-:--:--") );
520 if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
522 msg.Printf( wxString( wxL2U( p_item->input.psz_name ) ) + wxU( " ( ") +
523 wxString(wxL2U(psz_duration ) ) + wxU( ")") );
527 msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
528 wxString(wxL2U(p_item->input.psz_name)) + wxU( " ( ") +
529 wxString(wxL2U(psz_duration ) ) + wxU( ")") );
531 treectrl->SetItemText( item , msg );
532 treectrl->SetItemImage( item, p_item->input.i_type );
534 if( p_playlist->status.p_item == p_item )
536 SetCurrentItem( item );
540 treectrl->SetItemBold( item, false );
544 /* Process a AppendItem request */
545 void Playlist::AppendItem( wxCommandEvent& event )
547 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
549 playlist_t *p_playlist =
550 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
552 wxTreeItemId item,node;
553 if( p_playlist == NULL )
559 if( p_add->i_view != i_current_view )
564 node = FindItem( treectrl->GetRootItem(), p_add->p_node );
570 item = treectrl->AppendItem( node,
571 wxL2U( p_add->p_item->input.psz_name ), -1,-1,
572 new PlaylistItem( p_add->p_item ) );
573 treectrl->SetItemImage( item, p_add->p_item->input.i_type );
575 if( item.IsOk() && p_add->p_item->i_children == -1 )
577 UpdateTreeItem( p_playlist, item );
581 int i_count = CountItems( treectrl->GetRootItem());
582 if( i_count != p_playlist->i_size )
584 statusbar->SetStatusText( wxString::Format( wxU(_(
585 "%i items in playlist (%i not shown)")),
587 p_playlist->i_size - i_count ) );
588 if( !b_changed_view )
590 i_current_view = VIEW_CATEGORY;
591 b_changed_view = VLC_TRUE;
592 b_need_update = VLC_TRUE;
597 statusbar->SetStatusText( wxString::Format( wxU(_(
598 "%i items in playlist")),
599 p_playlist->i_size ), 0 );
602 vlc_object_release( p_playlist );
606 /* Process a updateitem request */
607 void Playlist::UpdateItem( int i )
609 if( i < 0 ) return; /* Sanity check */
610 playlist_item_t *p_item;
612 playlist_t *p_playlist =
613 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
616 if( p_playlist == NULL )
621 p_item = playlist_ItemGetById( p_playlist, i );
623 wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
627 UpdateTreeItem( p_playlist, item );
630 vlc_object_release(p_playlist);
635 /**********************************************************************
636 * Search functions (internal
637 **********************************************************************/
639 /* Find a wxItem from a playlist_item */
640 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
643 PlaylistItem *p_wxcurrent;
645 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
648 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
650 if( p_wxcurrent->p_item == p_item )
663 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
664 if( p_wxcurrent->p_item == p_item )
668 if( treectrl->ItemHasChildren( item ) )
670 wxTreeItemId search = FindItem( item, p_item );
676 item = treectrl->GetNextChild( root, cookie );
683 int Playlist::CountItems( wxTreeItemId root )
687 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
691 if( treectrl->ItemHasChildren( item ) )
693 count += CountItems( item );
695 else if( ( (PlaylistItem *)treectrl->GetItemData( item ) )->
696 p_item->i_children == -1 )
698 item = treectrl->GetNextChild( root, cookie );
703 /* Find a wxItem from a name (from current) */
704 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
708 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
713 if( treectrl->GetItemText( item).Lower().Contains(
714 search_string.Lower() ) )
716 if( !current.IsOk() || *pb_current_found == VLC_TRUE )
720 else if( current.IsOk() && item == current )
722 *pb_current_found = VLC_TRUE;
725 if( treectrl->ItemHasChildren( item ) )
727 wxTreeItemId search = FindItemByName( item, search_string, current,
734 item = treectrl->GetNextChild( root, cookie);
741 /**********************************************************************
742 * Rebuild the playlist
743 **********************************************************************/
744 void Playlist::Rebuild()
746 playlist_view_t *p_view;
747 playlist_t *p_playlist =
748 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
750 if( p_playlist == NULL )
754 int i_count = CountItems( treectrl->GetRootItem()) ;
756 if( i_count > p_playlist->i_size && !b_changed_view )
758 i_current_view = VIEW_CATEGORY;
759 b_changed_view = VLC_TRUE;
762 /* ...and rebuild it */
763 vlc_mutex_lock( &p_playlist->object_lock );
765 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
767 /* HACK we should really get new*/
768 msg_Dbg( p_intf, "rebuilding tree" );
769 treectrl->DeleteAllItems();
770 treectrl->AddRoot( wxU(_("root" )), -1, -1,
771 new PlaylistItem( p_view->p_root) );
773 wxTreeItemId root = treectrl->GetRootItem();
774 UpdateNode( p_playlist, p_view->p_root, root );
777 if( p_playlist->status.p_item != NULL )
779 item = FindItem( root, p_playlist->status.p_item );
781 else if( p_playlist->status.p_node != NULL )
783 item = FindItem( root, p_playlist->status.p_node );
790 i_count = CountItems( treectrl->GetRootItem() );
791 if( i_count != p_playlist->i_size )
793 statusbar->SetStatusText( wxString::Format( wxU(_(
794 "%i items in playlist (%i not shown)")),
796 p_playlist->i_size - i_count ) );
800 statusbar->SetStatusText( wxString::Format( wxU(_(
801 "%i items in playlist")),
802 p_playlist->i_size ), 0 );
805 SetCurrentItem( item );
807 vlc_mutex_unlock( &p_playlist->object_lock );
809 vlc_object_release( p_playlist );
814 void Playlist::ShowPlaylist( bool show )
816 if( show ) Rebuild();
820 /* This function is called on a regular basis */
821 void Playlist::UpdatePlaylist()
825 /* If the playlist isn't show there's no need to update it */
826 if( !IsShown() ) return;
828 if( this->b_need_update )
830 this->b_need_update = VLC_FALSE;
834 /* Updating the playing status every 0.5s is enough */
835 if( i_update_counter % 5 ) return;
837 playlist_t *p_playlist =
838 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
840 if( p_playlist == NULL )
845 vlc_object_release( p_playlist );
848 /*****************************************************************************
850 *****************************************************************************/
851 void Playlist::DeleteItem( int item_id )
853 playlist_t *p_playlist =
854 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
856 if( p_playlist == NULL )
861 playlist_Delete( p_playlist, item_id );
863 vlc_object_release( p_playlist );
866 void Playlist::DeleteNode( playlist_item_t *p_item )
868 playlist_t *p_playlist =
869 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
871 if( p_playlist == NULL )
876 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE );
878 vlc_object_release( p_playlist );
882 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
887 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
893 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
894 { _("PLS file"), "*.pls", "export-pls" }};
896 wxString filter = wxT("");
898 playlist_t * p_playlist =
899 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
906 if( p_playlist->i_size == 0 )
908 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
909 wxICON_WARNING | wxOK, this );
910 vlc_object_release( p_playlist );
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 );
933 vlc_object_release( p_playlist );
937 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
939 playlist_t *p_playlist =
940 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
942 if( p_playlist == NULL )
947 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
948 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
950 if( dialog.ShowModal() == wxID_OK )
952 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
955 vlc_object_release( p_playlist );
958 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
960 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
964 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
966 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
970 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
972 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
976 /********************************************************************
978 ********************************************************************/
979 void Playlist::OnSort( wxCommandEvent& event )
981 playlist_t *p_playlist =
982 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
984 PlaylistItem *p_wxitem;
985 p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
987 if( p_playlist == NULL )
991 vlc_mutex_lock( &p_playlist->object_lock );
992 switch( event.GetId() )
994 case SortTitle_Event:
995 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
996 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
998 case RSortTitle_Event:
999 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1000 SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
1002 vlc_mutex_unlock( &p_playlist->object_lock );
1004 vlc_object_release( p_playlist );
1008 /**********************************************************************
1009 * Search functions (user)
1010 **********************************************************************/
1011 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
1013 search_button->SetDefault();
1016 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
1018 wxString search_string = search_text->GetValue();
1020 vlc_bool_t pb_found = VLC_FALSE;
1022 wxTreeItemId found =
1023 FindItemByName( treectrl->GetRootItem(), search_string,
1024 search_current, &pb_found );
1028 search_current = found;
1029 treectrl->SelectItem( found, true );
1034 search_current = dummy;
1035 found = FindItemByName( treectrl->GetRootItem(), search_string,
1036 search_current, &pb_found );
1039 search_current = found;
1040 treectrl->SelectItem( found, true );
1045 /**********************************************************************
1046 * Selection functions
1047 **********************************************************************/
1048 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1052 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1057 void Playlist::OnEnableSelection( wxCommandEvent& WXUNUSED(event) )
1059 playlist_t *p_playlist =
1060 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1062 if( p_playlist == NULL )
1066 msg_Warn( p_playlist, "not implemented");
1067 vlc_object_release( p_playlist);
1070 void Playlist::OnDisableSelection( wxCommandEvent& WXUNUSED(event) )
1072 playlist_t *p_playlist =
1073 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1075 if( p_playlist == NULL )
1080 msg_Warn( p_playlist, "not implemented");
1082 vlc_object_release( p_playlist);
1085 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1089 /**********************************************************************
1090 * Playlist mode functions
1091 **********************************************************************/
1092 void Playlist::OnRandom( wxCommandEvent& event )
1095 val.b_bool = event.IsChecked();
1096 playlist_t *p_playlist =
1097 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1099 if( p_playlist == NULL )
1103 var_Set( p_playlist, "random", val);
1104 vlc_object_release( p_playlist );
1107 void Playlist::OnLoop( wxCommandEvent& event )
1110 val.b_bool = event.IsChecked();
1111 playlist_t *p_playlist =
1112 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1114 if( p_playlist == NULL )
1118 var_Set( p_playlist, "loop", val);
1119 vlc_object_release( p_playlist );
1122 void Playlist::OnRepeat( wxCommandEvent& event )
1125 val.b_bool = event.IsChecked();
1126 playlist_t *p_playlist =
1127 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1129 if( p_playlist == NULL )
1133 var_Set( p_playlist, "repeat", val);
1134 vlc_object_release( p_playlist );
1137 /********************************************************************
1139 ********************************************************************/
1140 void Playlist::OnActivateItem( wxTreeEvent& event )
1142 playlist_item_t *p_item,*p_node;
1143 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1144 VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1146 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1148 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1150 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1152 if( p_playlist == NULL )
1157 if( p_wxitem->p_item->i_children == -1 )
1159 p_node = p_wxparent->p_item;
1160 p_item = p_wxitem->p_item;
1164 p_node = p_wxitem->p_item;
1165 if( p_wxitem->p_item->i_children > 0 &&
1166 p_wxitem->p_item->pp_children[0]->i_children == -1)
1168 p_item = p_wxitem->p_item->pp_children[0];
1176 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1179 vlc_object_release( p_playlist );
1182 void Playlist::OnKeyDown( wxTreeEvent& event )
1184 long keycode = event.GetKeyCode();
1185 /* Delete selected items */
1186 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1188 /* We send a dummy event */
1189 OnDeleteSelection( event );
1193 void Playlist::OnEnDis( wxCommandEvent& event )
1195 playlist_t *p_playlist =
1196 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1198 if( p_playlist == NULL )
1202 msg_Warn( p_intf, "not implemented" );
1203 vlc_object_release( p_playlist );
1206 /**********************************************************************
1208 **********************************************************************/
1210 void Playlist::OnMenuOpen( wxMenuEvent& event)
1212 #if defined( __WXMSW__ )
1213 # define GetEventObject GetMenu
1216 if( event.GetEventObject() == p_view_menu )
1218 p_view_menu = ViewMenu();
1220 #if defined( __WXMSW__ )
1221 # undef GetEventObject
1225 void Playlist::OnMenuEvent( wxCommandEvent& event )
1227 playlist_t *p_playlist =
1228 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1230 if( p_playlist == NULL )
1235 if( event.GetId() < FirstView_Event )
1238 vlc_object_release( p_playlist );
1241 else if( event.GetId() < LastView_Event )
1244 int i_new_view = event.GetId() - FirstView_Event;
1246 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1248 if( p_view != NULL )
1250 b_changed_view = VLC_TRUE;
1251 i_current_view = i_new_view;
1252 playlist_ViewUpdate( p_playlist, i_new_view );
1254 vlc_object_release( p_playlist );
1257 else if( i_new_view >= VIEW_FIRST_SORTED &&
1258 i_new_view <= VIEW_LAST_SORTED )
1260 b_changed_view = VLC_TRUE;
1261 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1262 playlist_ViewUpdate( p_playlist, i_new_view );
1264 i_current_view = i_new_view;
1269 else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1271 if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1272 pp_sds[event.GetId() - FirstSD_Event] ) )
1274 playlist_ServicesDiscoveryAdd( p_playlist,
1275 pp_sds[event.GetId() - FirstSD_Event] );
1279 playlist_ServicesDiscoveryRemove( p_playlist,
1280 pp_sds[event.GetId() - FirstSD_Event] );
1283 vlc_object_release( p_playlist );
1286 wxMenu * Playlist::ViewMenu()
1288 playlist_t *p_playlist =
1289 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1291 if( p_playlist == NULL )
1298 p_view_menu = new wxMenu;
1302 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1305 wxMenuItem *item = node->GetData();
1306 node = node->GetNext();
1307 p_view_menu->Delete( item );
1311 /* FIXME : have a list of "should have" views */
1312 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1313 wxU(_("By category") ) );
1314 p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1315 wxU(_("Manually added") ) );
1316 p_view_menu->Append( FirstView_Event + VIEW_ALL,
1317 wxU(_("All items, unsorted") ) );
1318 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1319 wxU(_("Sorted by author") ) );
1321 vlc_object_release( p_playlist);
1326 wxMenu *Playlist::SDMenu()
1329 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1330 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1335 p_sd_menu = new wxMenu;
1337 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1341 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1343 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1345 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1347 p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1348 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1349 p_parser->psz_shortname ) );
1351 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1352 p_parser->psz_shortname ) )
1354 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1357 INSERT_ELEM( (void**)pp_sds, i_number, i_number,
1358 (void*)p_parser->psz_shortname );
1361 vlc_list_release( p_list );
1362 vlc_object_release( p_playlist );
1367 /*****************************************************************************
1368 * Popup management functions
1369 *****************************************************************************/
1370 void Playlist::OnPopup( wxContextMenuEvent& event )
1372 wxPoint pt = event.GetPosition();
1374 i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1375 if( i_popup_item.IsOk() )
1377 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1379 PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1380 treectrl->GetItemParent( i_popup_item ) );
1381 p_popup_item = p_wxitem->p_item;
1382 p_popup_parent = p_wxparent->p_item;
1383 treectrl->SelectItem( i_popup_item );
1384 Playlist::PopupMenu( popup_menu,
1385 ScreenToClient( wxGetMousePosition() ) );
1389 void Playlist::OnPopupPlay( wxMenuEvent& event )
1391 playlist_t *p_playlist =
1392 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1394 if( p_playlist == NULL )
1398 if( p_popup_item != NULL )
1400 if( p_popup_item->i_children > -1 )
1402 if( event.GetId() == PopupPlay_Event &&
1403 p_popup_item->i_children > 0 )
1405 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1406 i_current_view, p_popup_item,
1407 p_popup_item->pp_children[0] );
1411 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1412 i_current_view, p_popup_item, NULL );
1417 if( event.GetId() == PopupPlay_Event )
1419 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1420 i_current_view, p_popup_parent,
1425 vlc_object_release( p_playlist );
1428 void Playlist::OnPopupPreparse( wxMenuEvent& event )
1430 playlist_t *p_playlist =
1431 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1433 if( p_playlist == NULL )
1437 if( p_popup_item != NULL )
1439 if( p_popup_item->i_children == -1 )
1441 playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1446 playlist_item_t *p_parent = p_popup_item;
1447 for( i = 0; i< p_parent->i_children ; i++ )
1450 i_popup_item = FindItem( treectrl->GetRootItem(),
1451 p_parent->pp_children[i] );
1452 p_popup_item = p_parent->pp_children[i];
1453 OnPopupPreparse( dummy );
1457 vlc_object_release( p_playlist );
1460 void Playlist::OnPopupDel( wxMenuEvent& event )
1462 PlaylistItem *p_wxitem;
1464 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1466 if( p_wxitem->p_item->i_children == -1 )
1468 DeleteItem( p_wxitem->p_item->input.i_id );
1472 DeleteNode( p_wxitem->p_item );
1476 void Playlist::OnPopupSort( wxMenuEvent& event )
1478 PlaylistItem *p_wxitem;
1480 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1482 if( p_wxitem->p_item->i_children >= 0 )
1484 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1485 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1489 vlc_mutex_lock( &p_playlist->object_lock );
1490 playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1491 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1492 vlc_mutex_unlock( &p_playlist->object_lock );
1494 treectrl->DeleteChildren( i_popup_item );
1495 UpdateNodeChildren( p_playlist, p_wxitem->p_item, i_popup_item );
1497 vlc_object_release( p_playlist );
1502 void Playlist::OnPopupEna( wxMenuEvent& event )
1504 playlist_t *p_playlist =
1505 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1507 if( p_playlist == NULL )
1512 p_popup_item->b_enabled = VLC_TRUE - p_popup_item->b_enabled;
1514 vlc_object_release( p_playlist);
1515 UpdateItem( i_popup_item );
1518 void Playlist::OnPopupInfo( wxMenuEvent& event )
1522 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1523 if( iteminfo_dialog->ShowModal() == wxID_OK )
1525 UpdateItem( i_popup_item );
1527 delete iteminfo_dialog;
1532 /*****************************************************************************
1533 * Custom events management
1534 *****************************************************************************/
1535 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1537 switch( event.GetId() )
1539 case UpdateItem_Event:
1540 UpdateItem( event.GetInt() );
1542 case AppendItem_Event:
1543 AppendItem( event );
1548 /*****************************************************************************
1549 * PlaylistChanged: callback triggered by the intf-change playlist variable
1550 * We don't rebuild the playlist directly here because we don't want the
1551 * caller to block for a too long time.
1552 *****************************************************************************/
1553 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1554 vlc_value_t oval, vlc_value_t nval, void *param )
1556 Playlist *p_playlist_dialog = (Playlist *)param;
1557 p_playlist_dialog->b_need_update = VLC_TRUE;
1561 /*****************************************************************************
1562 * Next: callback triggered by the playlist-current playlist variable
1563 *****************************************************************************/
1564 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1565 vlc_value_t oval, vlc_value_t nval, void *param )
1567 Playlist *p_playlist_dialog = (Playlist *)param;
1569 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1570 event.SetInt( oval.i_int );
1571 p_playlist_dialog->AddPendingEvent( event );
1572 event.SetInt( nval.i_int );
1573 p_playlist_dialog->AddPendingEvent( event );
1578 /*****************************************************************************
1579 * ItemChanged: callback triggered by the item-change playlist variable
1580 *****************************************************************************/
1581 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1582 vlc_value_t old_val, vlc_value_t new_val, void *param )
1584 Playlist *p_playlist_dialog = (Playlist *)param;
1586 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1587 event.SetInt( new_val.i_int );
1588 p_playlist_dialog->AddPendingEvent( event );
1593 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1594 vlc_value_t oval, vlc_value_t nval, void *param )
1596 Playlist *p_playlist_dialog = (Playlist *)param;
1598 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1599 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1601 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1602 event.SetClientData( (void *)p_add );
1603 p_playlist_dialog->AddPendingEvent( event );