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 * );
59 /*****************************************************************************
61 *****************************************************************************/
63 /* IDs for the controls and the menu commands */
79 EnableSelection_Event,
80 DisableSelection_Event,
82 InvertSelection_Event,
83 DeleteSelection_Event,
105 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(AddMRL_Event, Playlist::OnAddMRL)
127 EVT_MENU(Close_Event, Playlist::OnClose)
128 EVT_MENU(Open_Event, Playlist::OnOpen)
129 EVT_MENU(Save_Event, Playlist::OnSave)
131 EVT_MENU(SortTitle_Event, Playlist::OnSort)
132 EVT_MENU(RSortTitle_Event, Playlist::OnSort)
133 EVT_MENU(SortAuthor_Event, Playlist::OnSort)
134 EVT_MENU(RSortAuthor_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)
143 EVT_MENU(Infos_Event, Playlist::OnInfos)
145 EVT_MENU_OPEN( Playlist::OnMenuOpen )
146 EVT_MENU( -1, Playlist::OnMenuEvent )
148 EVT_TOOL(Random_Event, Playlist::OnRandom)
149 EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
150 EVT_TOOL(Loop_Event, Playlist::OnLoop)
153 EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
154 EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
155 EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
156 EVT_MENU( PopupEna_Event, Playlist::OnPopupEna)
157 EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
159 /* Tree control events */
160 EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
162 EVT_CONTEXT_MENU( Playlist::OnPopup )
165 EVT_BUTTON( Search_Event, Playlist::OnSearch)
166 EVT_BUTTON( Save_Event, Playlist::OnSave)
167 EVT_BUTTON( Infos_Event, Playlist::OnInfos)
169 EVT_BUTTON( Up_Event, Playlist::OnUp)
170 EVT_BUTTON( Down_Event, Playlist::OnDown)
172 EVT_TEXT(SearchText_Event, Playlist::OnSearchTextChange)
175 EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
177 /* Special events : we don't want to destroy the window when the user
179 EVT_CLOSE(Playlist::OnClose)
182 /*****************************************************************************
184 ****************************************************************************/
185 class PlaylistItem : public wxTreeItemData
188 PlaylistItem( playlist_item_t *_p_item ) : wxTreeItemData()
193 playlist_item_t *p_item;
194 friend class Playlist;
197 /*****************************************************************************
199 *****************************************************************************/
200 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
201 wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
202 wxSize(345,400), wxDEFAULT_FRAME_STYLE )
206 /* Initializations */
208 i_update_counter = 0;
209 i_sort_mode = MODE_NONE;
210 b_need_update = VLC_FALSE;
211 SetIcon( *p_intf->p_sys->p_icon );
214 p_sd_menu = SDMenu();
216 i_current_view = VIEW_SIMPLE;
221 i_duration_sorted = 0;
223 var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
224 var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
225 var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
227 /* Create our "Manage" menu */
228 wxMenu *manage_menu = new wxMenu;
229 manage_menu->Append( AddFile_Event, wxU(_("&Simple Add...")) );
230 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
231 manage_menu->AppendSeparator();
232 manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
234 manage_menu->AppendSeparator();
235 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
236 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
237 manage_menu->AppendSeparator();
238 manage_menu->Append( Close_Event, wxU(_("&Close")) );
240 /* Create our "Sort" menu */
241 wxMenu *sort_menu = new wxMenu;
242 sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
243 sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
244 sort_menu->AppendSeparator();
245 sort_menu->Append( SortAuthor_Event, wxU(_("Sort by &author")) );
246 sort_menu->Append( RSortAuthor_Event, wxU(_("Reverse sort by author")) );
247 sort_menu->AppendSeparator();
248 sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
250 /* Create our "Selection" menu */
251 wxMenu *selection_menu = new wxMenu;
252 selection_menu->Append( EnableSelection_Event, wxU(_("&Enable")) );
253 selection_menu->Append( DisableSelection_Event, wxU(_("&Disable")) );
254 selection_menu->AppendSeparator();
255 selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
256 selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
257 selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
259 /* Create our "View" menu */
262 /* Append the freshly created menus to the menu bar */
263 wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
264 menubar->Append( manage_menu, wxU(_("&Manage")) );
265 menubar->Append( sort_menu, wxU(_("S&ort")) );
266 menubar->Append( selection_menu, wxU(_("&Selection")) );
267 menubar->Append( p_view_menu, wxU(_("&View items") ) );
269 /* Attach the menu bar to the frame */
270 SetMenuBar( menubar );
272 /* Create the popup menu */
273 popup_menu = new wxMenu;
274 popup_menu->Append( PopupPlay_Event, wxU(_("Play")) );
275 popup_menu->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
276 popup_menu->Append( PopupDel_Event, wxU(_("Delete")) );
277 popup_menu->Append( PopupEna_Event, wxU(_("Enable/Disable")) );
278 popup_menu->Append( PopupInfo_Event, wxU(_("Info")) );
280 /* Create a panel to put everything in */
281 wxPanel *playlist_panel = new wxPanel( this, -1 );
282 playlist_panel->SetAutoLayout( TRUE );
284 /* Create the toolbar */
286 CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT | wxTB_DOCKABLE );
288 /* Create the random tool */
289 toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
290 wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
291 wxU(_(HELP_SHUFFLE) ) );
292 var_Get( p_intf, "random", &val );
293 toolbar->ToggleTool( Random_Event, val.b_bool );
295 /* Create the Loop tool */
296 toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
297 wxBitmap( loop_xpm), wxITEM_CHECK,
298 wxU(_(HELP_LOOP ) ) );
299 var_Get( p_intf, "loop", &val );
300 toolbar->ToggleTool( Loop_Event, val.b_bool );
302 /* Create the Repeat one checkbox */
303 toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
304 wxBitmap( repeat_xpm), wxITEM_CHECK,
305 wxU(_(HELP_REPEAT ) ) );
306 var_Get( p_intf, "repeat", &val );
307 toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
309 /* Create the Search Textbox */
310 search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
311 wxDefaultPosition, wxSize(100, -1),
314 /* Create the search button */
315 search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
317 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
318 wxSize(16, 16), wxBORDER_NONE ) );
319 toolbar->AddControl( search_text );
320 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
321 wxSize(5, 5), wxBORDER_NONE ) );
322 toolbar->AddControl( search_button );
323 search_button->SetDefault();
326 /* Create the tree */
327 treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
328 wxDefaultPosition, wxDefaultSize,
329 wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
331 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
332 wxTR_MULTIPLE | wxTR_EXTENDED );
334 /* Create image list */
336 wxImageList *p_images = new wxImageList( 16 , 16, TRUE);
339 icons[ ITEM_TYPE_UNKNOWN ] = wxIcon( type_unknown_xpm );
340 icons[ ITEM_TYPE_DISC ] = wxIcon( type_disc_xpm );
341 icons[ ITEM_TYPE_DIRECTORY ] = wxIcon( type_directory_xpm );
342 icons[ ITEM_TYPE_PLAYLIST ] = wxIcon( type_playlist_xpm );
343 icons[ ITEM_TYPE_NET ] = wxIcon( type_net_xpm );
344 icons[ ITEM_TYPE_CARD ] = wxIcon( type_card_xpm );
346 for( unsigned int i = 0; i< WXSIZEOF( icons ) ; i++ )
348 p_images->Add( wxIcon( icons[i] ));
351 treectrl->AssignImageList( p_images );
353 treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
355 /* Reduce font size */
356 wxFont font= treectrl->GetFont();
357 font.SetPointSize(8);
358 treectrl->SetFont( font );
360 /* Create the Up-Down buttons */
362 wxButton *up_button =
363 new wxButton( playlist_panel, Up_Event, wxU(_("Up") ) );
364 wxButton *down_button =
365 new wxButton( playlist_panel, Down_Event, wxU(_("Down") ) );
367 wxBoxSizer *updown_sizer = new wxBoxSizer( wxHORIZONTAL );
368 updown_sizer->Layout();
369 /* The top and bottom sizers */
370 wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
371 bottom_sizer->Add( up_button, 0, wxALIGN_LEFT | wxRIGHT, 3);
372 bottom_sizer->Add( down_button, 0, wxALIGN_LEFT | wxLEFT, 3);
373 bottom_sizer->Layout();
375 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
376 panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
378 panel_sizer->Add( bottom_sizer, 0, wxALL, 5);
380 panel_sizer->Layout();
382 playlist_panel->SetSizerAndFit( panel_sizer );
384 #if wxUSE_DRAG_AND_DROP
385 /* Associate drop targets with the playlist */
386 SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
389 playlist_t *p_playlist =
390 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
392 if( p_playlist == NULL )
397 /* We want to be noticed of playlist changes */
399 /* Some global changes happened -> Rebuild all */
400 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
402 /* We went to the next item */
403 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
405 /* One item has been updated */
406 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
408 vlc_object_release( p_playlist );
410 /* Update the playlist */
414 void Playlist::OnSize( wxSizeEvent& event)
417 wxSize size = GetClientSize();
419 listview->SetColumnWidth( 0, size.x - listview->GetColumnWidth(1)
420 - 15 /* margins */ );
425 Playlist::~Playlist()
427 playlist_t *p_playlist =
428 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
430 if( p_playlist == NULL )
435 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
436 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
437 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
438 vlc_object_release( p_playlist );
441 /**********************************************************************
442 * Update one playlist item
443 **********************************************************************/
444 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
449 for( int i = 0; i< p_node->i_children ; i++ )
453 child = treectrl->GetFirstChild( node, cookie);
457 child = treectrl->GetNextChild( node, cookie );
462 /* Not enough children */
463 CreateNode( p_playlist, p_node->pp_children[i], node );
464 /* Keep the tree pointer up to date */
465 child = treectrl->GetNextChild( node, cookie );
473 /* Creates the node p_node as last child of parent */
474 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
475 wxTreeItemId parent )
479 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
480 -1,-1, new PlaylistItem( p_node ) );
481 treectrl->SetItemImage( node, p_node->input.i_type );
483 for( int i = 0; i< p_node->i_children ; i++ )
485 /* Append the item */
486 if( p_node->pp_children[i]->i_children == -1 )
489 treectrl->AppendItem( node,
490 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
491 new PlaylistItem( p_node->pp_children[i]) );
493 treectrl->SetItemImage( item,
494 p_node->pp_children[i]->input.i_type );
498 CreateNode( p_playlist, p_node->pp_children[i],
504 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
507 PlaylistItem *p_wxcurrent;
509 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
514 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
515 if( p_wxcurrent->p_item == p_item )
519 if( treectrl->ItemHasChildren( item ) )
521 wxTreeItemId search = FindItem( item, p_item );
527 item = treectrl->GetNextChild( root, cookie);
534 /*wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current )
537 PlaylistItem *p_wxcurrent;
539 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
544 if( treectrl->GetItemText( item).Lower().Contains(
545 search_string.Lower() ) )
548 if( treectrl->ItemHasChildren( item ) )
550 wxTreeItemId search = FindItem( item, p_item );
556 item = treectrl->GetNextChild( root, cookie);
559 /*wxTreeItemId dummy;
566 void Playlist::SetCurrentItem( wxTreeItemId item )
570 treectrl->SetItemBold( item, true );
571 treectrl->EnsureVisible( item );
575 void Playlist::UpdateItem( int i )
577 if( i < 0 ) return; /* Sanity check */
578 playlist_item_t *p_item;
580 playlist_t *p_playlist =
581 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
584 if( p_playlist == NULL )
589 p_item = playlist_ItemGetById( p_playlist, i );
591 wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
593 UpdateTreeItem( p_playlist, item );
595 vlc_object_release(p_playlist);
599 void Playlist::UpdateTreeItem( playlist_t *p_playlist ,wxTreeItemId item )
601 playlist_item_t *p_item;
608 p_item = ((PlaylistItem *)treectrl->GetItemData( item ))->p_item;
616 char *psz_author = playlist_ItemGetInfo( p_item, _("Meta-information"),
618 char psz_duration[MSTRTIME_MAX_SIZE];
619 mtime_t dur = p_item->input.i_duration;
621 secstotimestr( psz_duration, dur/1000000 );
623 memcpy( psz_duration, "-:--:--", sizeof("-:--:--") );
625 if( !strcmp( psz_author, "" ) )
627 msg.Printf( wxString( wxL2U( p_item->input.psz_name ) ) + wxU( " ( ") +
628 wxString(wxL2U(psz_duration ) ) + wxU( ")") );
632 msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
633 wxString(wxL2U(p_item->input.psz_name)) + wxU( " ( ") +
634 wxString(wxL2U(psz_duration ) ) + wxU( ")") );
636 treectrl->SetItemText( item , msg );
638 if( p_playlist->status.p_item == p_item )
640 SetCurrentItem( item );
644 treectrl->SetItemBold( item, false );
647 if( p_item->b_enabled == VLC_FALSE )
650 listitem.m_itemId = i;
651 listitem.SetTextColour( *wxLIGHT_GREY);
652 listview->SetItem(listitem);
657 /**********************************************************************
658 * Rebuild the playlist
659 **********************************************************************/
660 void Playlist::Rebuild()
662 playlist_view_t *p_view;
663 playlist_t *p_playlist =
664 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
666 if( p_playlist == NULL )
671 /* ...and rebuild it */
672 vlc_mutex_lock( &p_playlist->object_lock );
674 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
676 /* HACK we should really get new*/
677 msg_Dbg( p_intf, "rebuilding tree" );
678 treectrl->DeleteAllItems();
679 treectrl->AddRoot( wxU(_("root" )), -1, -1,
680 new PlaylistItem( p_view->p_root) );
682 wxTreeItemId root = treectrl->GetRootItem();
683 UpdateNode( p_playlist, p_view->p_root, root );
686 if( p_playlist->status.p_item != NULL )
688 item = FindItem( root, p_playlist->status.p_item );
690 else if( p_playlist->status.p_node != NULL )
692 item = FindItem( root, p_playlist->status.p_node );
699 SetCurrentItem( item );
701 vlc_mutex_unlock( &p_playlist->object_lock );
703 vlc_object_release( p_playlist );
706 void Playlist::ShowPlaylist( bool show )
708 if( show ) Rebuild();
712 void Playlist::UpdatePlaylist()
716 /* If the playlist isn't show there's no need to update it */
717 if( !IsShown() ) return;
719 if( this->b_need_update )
721 this->b_need_update = VLC_FALSE;
725 /* Updating the playing status every 0.5s is enough */
726 if( i_update_counter % 5 ) return;
728 playlist_t *p_playlist =
729 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
731 if( p_playlist == NULL )
736 /* Update the colour of items */
737 int i_playlist_index = p_playlist->i_index;
738 if( p_intf->p_sys->i_playing != i_playlist_index )
741 listitem.m_itemId = i_playlist_index;
742 listitem.SetTextColour( *wxRED );
743 listview->SetItem( listitem );
745 if( p_intf->p_sys->i_playing != -1 )
747 listitem.m_itemId = p_intf->p_sys->i_playing;
748 listitem.SetTextColour( *wxBLACK );
749 listview->SetItem( listitem );
751 p_intf->p_sys->i_playing = i_playlist_index;
754 vlc_object_release( p_playlist );
757 /*****************************************************************************
759 *****************************************************************************/
760 void Playlist::DeleteItem( int item_id )
762 playlist_t *p_playlist =
763 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
765 if( p_playlist == NULL )
770 playlist_Delete( p_playlist, item_id );
772 vlc_object_release( p_playlist );
775 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
780 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
786 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
787 { _("PLS file"), "*.pls", "export-pls" }};
789 wxString filter = wxT("");
791 playlist_t * p_playlist =
792 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
799 if( p_playlist->i_size == 0 )
801 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
802 wxICON_WARNING | wxOK, this );
803 vlc_object_release( p_playlist );
807 for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
809 filter.Append( wxU(formats[i].psz_desc) );
810 filter.Append( wxT("|") );
811 filter.Append( wxU(formats[i].psz_filter) );
812 filter.Append( wxT("|") );
814 wxFileDialog dialog( this, wxU(_("Save playlist")),
815 wxT(""), wxT(""), filter, wxSAVE );
817 if( dialog.ShowModal() == wxID_OK )
819 if( dialog.GetPath().mb_str() )
821 playlist_Export( p_playlist, dialog.GetPath().mb_str(),
822 formats[dialog.GetFilterIndex()].psz_module );
826 vlc_object_release( p_playlist );
830 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
832 playlist_t *p_playlist =
833 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
835 if( p_playlist == NULL )
840 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
841 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
843 if( dialog.ShowModal() == wxID_OK )
845 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
848 vlc_object_release( p_playlist );
851 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
853 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
857 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
859 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
863 /********************************************************************
865 ********************************************************************/
866 void Playlist::OnUp( wxCommandEvent& event )
868 playlist_t *p_playlist =
869 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
871 if( p_playlist == NULL )
876 /* We use the first selected item, so find it */
877 long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
878 wxLIST_STATE_SELECTED);
879 if( i_item > 0 && i_item < p_playlist->i_size )
881 playlist_Move( p_playlist, i_item, i_item - 1 );
882 listview->Focus( i_item - 1 );
885 vlc_object_release( p_playlist );
888 void Playlist::OnDown( wxCommandEvent& event )
890 playlist_t *p_playlist =
891 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
893 if( p_playlist == NULL )
898 /* We use the first selected item, so find it */
899 long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
900 wxLIST_STATE_SELECTED );
901 if( i_item >= 0 && i_item < p_playlist->i_size - 1 )
903 playlist_Move( p_playlist, i_item, i_item + 2 );
904 listview->Focus( i_item + 1 );
907 vlc_object_release( p_playlist );
910 /********************************************************************
912 ********************************************************************/
913 void Playlist::OnSort( wxCommandEvent& event )
915 playlist_t *p_playlist =
916 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
918 if( p_playlist == NULL )
922 switch( event.GetId() )
924 case SortTitle_Event:
925 playlist_SortTitle( p_playlist, ORDER_NORMAL );
927 case RSortTitle_Event:
928 playlist_SortTitle( p_playlist, ORDER_REVERSE );
930 case SortAuthor_Event:
931 playlist_SortAuthor(p_playlist, ORDER_NORMAL );
933 case RSortAuthor_Event:
934 playlist_SortAuthor( p_playlist, ORDER_REVERSE );
936 case Randomize_Event:
937 playlist_Sort( p_playlist, SORT_RANDOM, ORDER_NORMAL );
940 vlc_object_release( p_playlist );
945 /**********************************************************************
947 **********************************************************************/
948 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
950 search_button->SetDefault();
953 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
955 wxString search_string = search_text->GetValue();
964 for( i_current = 0; i_current < listview->GetItemCount(); i_current++ )
966 if( listview->GetItemState( i_current, wxLIST_STATE_SELECTED ) ==
967 wxLIST_STATE_SELECTED )
974 if( i_first == listview->GetItemCount() )
979 for( i_current = i_first + 1; i_current < listview->GetItemCount();
983 listitem.SetId( i_current );
984 listview->GetItem( listitem );
985 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
991 listitem.SetColumn( 1 );
992 listview->GetItem( listitem );
993 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1002 for( i_current = -1 ; i_current < i_first - 1;
1005 wxListItem listitem;
1006 listitem.SetId( i_current );
1007 listview->GetItem( listitem );
1008 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1014 listitem.SetColumn( 1 );
1015 listview->GetItem( listitem );
1016 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1025 if( i_item < 0 || i_item >= listview->GetItemCount() )
1030 for( long item = 0; item < listview->GetItemCount(); item++ )
1032 listview->Select( item, FALSE );
1035 wxListItem listitem;
1036 listitem.SetId(i_item);
1037 listitem.m_state = wxLIST_STATE_SELECTED;
1038 listview->Select( i_item, TRUE );
1039 listview->Focus( i_item );
1043 /**********************************************************************
1044 * Selection functions
1045 **********************************************************************/
1046 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1050 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1055 void Playlist::OnEnableSelection( wxCommandEvent& WXUNUSED(event) )
1057 playlist_t *p_playlist =
1058 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1060 if( p_playlist == NULL )
1065 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
1067 if( listview->IsSelected( item ) )
1070 playlist_Enable( p_playlist, item );
1075 vlc_object_release( p_playlist);
1078 void Playlist::OnDisableSelection( wxCommandEvent& WXUNUSED(event) )
1080 playlist_t *p_playlist =
1081 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1083 if( p_playlist == NULL )
1088 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
1090 if( listview->IsSelected( item ) )
1093 playlist_Disable( p_playlist, item );
1098 vlc_object_release( p_playlist);
1101 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1105 /**********************************************************************
1106 * Playlist mode functions
1107 **********************************************************************/
1108 void Playlist::OnRandom( wxCommandEvent& event )
1111 val.b_bool = event.IsChecked();
1112 playlist_t *p_playlist =
1113 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1115 if( p_playlist == NULL )
1119 var_Set( p_playlist, "random", val);
1120 vlc_object_release( p_playlist );
1123 void Playlist::OnLoop( wxCommandEvent& event )
1126 val.b_bool = event.IsChecked();
1127 playlist_t *p_playlist =
1128 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1130 if( p_playlist == NULL )
1134 var_Set( p_playlist, "loop", val);
1135 vlc_object_release( p_playlist );
1138 void Playlist::OnRepeat( wxCommandEvent& event )
1141 val.b_bool = event.IsChecked();
1142 playlist_t *p_playlist =
1143 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1145 if( p_playlist == NULL )
1149 var_Set( p_playlist, "repeat", val);
1150 vlc_object_release( p_playlist );
1153 void Playlist::OnActivateItem( wxTreeEvent& event )
1155 playlist_item_t *p_item,*p_node;
1156 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1157 VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1158 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1160 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1162 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1164 if( p_playlist == NULL )
1169 if( p_wxitem->p_item->i_children == -1 )
1171 p_node = p_wxparent->p_item;
1172 p_item = p_wxitem->p_item;
1176 p_node = p_wxitem->p_item;
1177 if( p_wxitem->p_item->i_children > 0 )
1179 p_item = p_wxitem->p_item->pp_children[0];
1187 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1190 vlc_object_release( p_playlist );
1193 void Playlist::OnKeyDown( wxTreeEvent& event )
1195 long keycode = event.GetKeyCode();
1196 /* Delete selected items */
1197 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1199 /* We send a dummy event */
1200 OnDeleteSelection( event );
1204 void Playlist::ShowInfos( int i_item )
1208 void Playlist::OnInfos( wxCommandEvent& WXUNUSED(event) )
1212 void Playlist::OnEnDis( wxCommandEvent& event )
1214 playlist_t *p_playlist =
1215 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1217 if( p_playlist == NULL )
1221 vlc_object_release( p_playlist );
1224 /**********************************************************************
1226 **********************************************************************/
1228 void Playlist::OnMenuOpen( wxMenuEvent& event)
1230 #if defined( __WXMSW__ )
1231 # define GetEventObject GetMenu
1234 if( event.GetEventObject() == p_view_menu )
1236 p_view_menu = ViewMenu();
1238 #if defined( __WXMSW__ )
1239 # undef GetEventObject
1243 void Playlist::OnMenuEvent( wxCommandEvent& event )
1245 playlist_t *p_playlist =
1246 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1248 if( p_playlist == NULL )
1253 if( event.GetId() < FirstView_Event )
1256 vlc_object_release( p_playlist );
1259 else if( event.GetId() < LastView_Event )
1262 int i_new_view = event.GetId() - FirstView_Event;
1264 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1266 if( p_view != NULL )
1268 i_current_view = i_new_view;
1269 playlist_ViewUpdate( p_playlist, i_new_view );
1271 vlc_object_release( p_playlist );
1274 else if( i_new_view >= VIEW_FIRST_SORTED &&
1275 i_new_view <= VIEW_LAST_SORTED )
1277 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1278 playlist_ViewUpdate( p_playlist, i_new_view );
1280 i_current_view = i_new_view;
1285 else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1287 if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1288 pp_sds[event.GetId() - FirstSD_Event] ) )
1290 playlist_ServicesDiscoveryAdd( p_playlist,
1291 pp_sds[event.GetId() - FirstSD_Event] );
1295 playlist_ServicesDiscoveryRemove( p_playlist,
1296 pp_sds[event.GetId() - FirstSD_Event] );
1299 vlc_object_release( p_playlist );
1302 wxMenu * Playlist::ViewMenu()
1304 playlist_t *p_playlist =
1305 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1307 if( p_playlist == NULL )
1314 p_view_menu = new wxMenu;
1318 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1321 wxMenuItem *item = node->GetData();
1322 node = node->GetNext();
1323 p_view_menu->Delete( item );
1327 /* FIXME : have a list of "should have" views */
1328 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1329 wxU(_("By category") ) );
1330 p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1331 wxU(_("Manually added") ) );
1332 p_view_menu->Append( FirstView_Event + VIEW_ALL,
1333 wxU(_("All items, unsorted") ) );
1334 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1335 wxU(_("Sorted by author") ) );
1337 vlc_object_release( p_playlist);
1342 wxMenu *Playlist::SDMenu()
1345 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1346 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1351 vlc_value_t val, val_list, text_list;
1352 p_sd_menu = new wxMenu;
1354 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1358 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1360 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1362 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1364 p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1365 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1366 p_parser->psz_shortname ) );
1368 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1369 p_parser->psz_shortname ) )
1371 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1374 INSERT_ELEM( (void**)pp_sds, i_number, i_number,
1375 (void*)p_parser->psz_shortname );
1378 vlc_list_release( p_list );
1379 vlc_object_release( p_playlist );
1384 /*****************************************************************************
1385 * Popup management functions
1386 *****************************************************************************/
1387 void Playlist::OnPopup( wxContextMenuEvent& event )
1389 wxPoint pt = event.GetPosition();
1391 i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1392 if( i_popup_item.IsOk() )
1394 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1396 PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1397 treectrl->GetItemParent( i_popup_item ) );
1398 p_popup_item = p_wxitem->p_item;
1399 p_popup_parent = p_wxparent->p_item;
1400 treectrl->SelectItem( i_popup_item );
1401 Playlist::PopupMenu( popup_menu,
1402 ScreenToClient( wxGetMousePosition() ) );
1409 void Playlist::OnPopupPlay( wxMenuEvent& event )
1411 playlist_t *p_playlist =
1412 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1414 if( p_playlist == NULL )
1418 if( p_popup_item != NULL )
1420 if( p_popup_item->i_children > -1 )
1422 if( event.GetId() == PopupPlay_Event &&
1423 p_popup_item->i_children > 0 )
1425 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1426 i_current_view, p_popup_item,
1427 p_popup_item->pp_children[0] );
1431 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1432 i_current_view, p_popup_item, NULL );
1437 if( event.GetId() == PopupPlay_Event )
1439 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1440 i_current_view, p_popup_parent,
1445 vlc_object_release( p_playlist );
1448 void Playlist::OnPopupDel( wxMenuEvent& event )
1450 PlaylistItem *p_wxitem;
1452 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1454 if( p_wxitem->p_item->i_children == -1 )
1456 DeleteItem( p_wxitem->p_item->input.i_id );
1460 //DeleteNode( p_wxitem->p_item );
1464 void Playlist::OnPopupEna( wxMenuEvent& event )
1466 playlist_t *p_playlist =
1467 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1469 if( p_playlist == NULL )
1474 p_popup_item->b_enabled = VLC_TRUE - p_popup_item->b_enabled;
1476 vlc_object_release( p_playlist);
1477 UpdateItem( i_popup_item );
1480 void Playlist::OnPopupInfo( wxMenuEvent& event )
1484 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1485 if( iteminfo_dialog->ShowModal() == wxID_OK )
1487 UpdateItem( i_popup_item );
1489 delete iteminfo_dialog;
1494 /*****************************************************************************
1495 * Custom events management
1496 *****************************************************************************/
1497 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1499 switch( event.GetId() )
1501 case UpdateItem_Event:
1502 UpdateItem( event.GetInt() );
1507 /*****************************************************************************
1508 * PlaylistChanged: callback triggered by the intf-change playlist variable
1509 * We don't rebuild the playlist directly here because we don't want the
1510 * caller to block for a too long time.
1511 *****************************************************************************/
1512 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1513 vlc_value_t oval, vlc_value_t nval, void *param )
1515 Playlist *p_playlist_dialog = (Playlist *)param;
1516 p_playlist_dialog->b_need_update = VLC_TRUE;
1520 /*****************************************************************************
1521 * Next: callback triggered by the playlist-current playlist variable
1522 *****************************************************************************/
1523 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1524 vlc_value_t oval, vlc_value_t nval, void *param )
1526 Playlist *p_playlist_dialog = (Playlist *)param;
1528 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1529 event.SetInt( oval.i_int );
1530 p_playlist_dialog->AddPendingEvent( event );
1531 event.SetInt( nval.i_int );
1532 p_playlist_dialog->AddPendingEvent( event );
1537 /*****************************************************************************
1538 * ItemChanged: callback triggered by the item-change playlist variable
1539 *****************************************************************************/
1540 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1541 vlc_value_t old_val, vlc_value_t new_val, void *param )
1543 Playlist *p_playlist_dialog = (Playlist *)param;
1545 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1546 event.SetInt( new_val.i_int );
1547 p_playlist_dialog->AddPendingEvent( event );