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 */
80 EnableSelection_Event,
81 DisableSelection_Event,
83 InvertSelection_Event,
84 DeleteSelection_Event,
106 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)
135 EVT_MENU(SortAuthor_Event, Playlist::OnSort)
136 EVT_MENU(RSortAuthor_Event, Playlist::OnSort)
138 EVT_MENU(Randomize_Event, Playlist::OnSort)
140 EVT_MENU(EnableSelection_Event, Playlist::OnEnableSelection)
141 EVT_MENU(DisableSelection_Event, Playlist::OnDisableSelection)
142 EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
143 EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
144 EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
145 EVT_MENU(Infos_Event, Playlist::OnInfos)
147 EVT_MENU_OPEN( Playlist::OnMenuOpen )
148 EVT_MENU( -1, Playlist::OnMenuEvent )
150 EVT_TOOL(Random_Event, Playlist::OnRandom)
151 EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
152 EVT_TOOL(Loop_Event, Playlist::OnLoop)
155 EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
156 EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
157 EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
158 EVT_MENU( PopupEna_Event, Playlist::OnPopupEna)
159 EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
161 /* Tree control events */
162 EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
164 EVT_CONTEXT_MENU( Playlist::OnPopup )
167 EVT_BUTTON( Search_Event, Playlist::OnSearch)
168 EVT_BUTTON( Save_Event, Playlist::OnSave)
169 EVT_BUTTON( Infos_Event, Playlist::OnInfos)
171 EVT_BUTTON( Up_Event, Playlist::OnUp)
172 EVT_BUTTON( Down_Event, Playlist::OnDown)
174 EVT_TEXT(SearchText_Event, Playlist::OnSearchTextChange)
177 EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
179 /* Special events : we don't want to destroy the window when the user
181 EVT_CLOSE(Playlist::OnClose)
184 /*****************************************************************************
186 ****************************************************************************/
187 class PlaylistItem : public wxTreeItemData
190 PlaylistItem( playlist_item_t *_p_item ) : wxTreeItemData()
195 playlist_item_t *p_item;
196 friend class Playlist;
199 /*****************************************************************************
201 *****************************************************************************/
202 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
203 wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
204 wxSize(345,400), wxDEFAULT_FRAME_STYLE )
208 /* Initializations */
210 i_update_counter = 0;
211 i_sort_mode = MODE_NONE;
212 b_need_update = VLC_FALSE;
213 SetIcon( *p_intf->p_sys->p_icon );
216 p_sd_menu = SDMenu();
218 i_current_view = VIEW_SIMPLE;
223 i_duration_sorted = 0;
225 var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
226 var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
227 var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
229 /* Create our "Manage" menu */
230 wxMenu *manage_menu = new wxMenu;
231 manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
232 manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
233 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
234 manage_menu->AppendSeparator();
235 manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
237 manage_menu->AppendSeparator();
238 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
239 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
240 manage_menu->AppendSeparator();
241 manage_menu->Append( Close_Event, wxU(_("&Close")) );
243 /* Create our "Sort" menu */
244 wxMenu *sort_menu = new wxMenu;
245 sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
246 sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
247 sort_menu->AppendSeparator();
248 sort_menu->Append( SortAuthor_Event, wxU(_("Sort by &author")) );
249 sort_menu->Append( RSortAuthor_Event, wxU(_("Reverse sort by author")) );
250 sort_menu->AppendSeparator();
251 sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
253 /* Create our "Selection" menu */
254 wxMenu *selection_menu = new wxMenu;
255 selection_menu->Append( EnableSelection_Event, wxU(_("&Enable")) );
256 selection_menu->Append( DisableSelection_Event, wxU(_("&Disable")) );
257 selection_menu->AppendSeparator();
258 selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
259 selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
260 selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
262 /* Create our "View" menu */
265 /* Append the freshly created menus to the menu bar */
266 wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
267 menubar->Append( manage_menu, wxU(_("&Manage")) );
268 menubar->Append( sort_menu, wxU(_("S&ort")) );
269 menubar->Append( selection_menu, wxU(_("&Selection")) );
270 menubar->Append( p_view_menu, wxU(_("&View items") ) );
272 /* Attach the menu bar to the frame */
273 SetMenuBar( menubar );
275 /* Create the popup menu */
276 popup_menu = new wxMenu;
277 popup_menu->Append( PopupPlay_Event, wxU(_("Play")) );
278 popup_menu->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
279 popup_menu->Append( PopupDel_Event, wxU(_("Delete")) );
280 popup_menu->Append( PopupEna_Event, wxU(_("Enable/Disable")) );
281 popup_menu->Append( PopupInfo_Event, wxU(_("Info")) );
283 /* Create a panel to put everything in */
284 wxPanel *playlist_panel = new wxPanel( this, -1 );
285 playlist_panel->SetAutoLayout( TRUE );
287 /* Create the toolbar */
289 CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT | wxTB_DOCKABLE );
291 /* Create the random tool */
292 toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
293 wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
294 wxU(_(HELP_SHUFFLE) ) );
295 var_Get( p_intf, "random", &val );
296 toolbar->ToggleTool( Random_Event, val.b_bool );
298 /* Create the Loop tool */
299 toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
300 wxBitmap( loop_xpm), wxITEM_CHECK,
301 wxU(_(HELP_LOOP ) ) );
302 var_Get( p_intf, "loop", &val );
303 toolbar->ToggleTool( Loop_Event, val.b_bool );
305 /* Create the Repeat one checkbox */
306 toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
307 wxBitmap( repeat_xpm), wxITEM_CHECK,
308 wxU(_(HELP_REPEAT ) ) );
309 var_Get( p_intf, "repeat", &val );
310 toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
312 /* Create the Search Textbox */
313 search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
314 wxDefaultPosition, wxSize(100, -1),
317 /* Create the search button */
318 search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
320 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
321 wxSize(16, 16), wxBORDER_NONE ) );
322 toolbar->AddControl( search_text );
323 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
324 wxSize(5, 5), wxBORDER_NONE ) );
325 toolbar->AddControl( search_button );
326 search_button->SetDefault();
329 /* Create the tree */
330 treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
331 wxDefaultPosition, wxDefaultSize,
332 wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
334 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
335 wxTR_MULTIPLE | wxTR_EXTENDED );
337 /* Create image list */
339 wxImageList *p_images = new wxImageList( 16 , 16, TRUE);
342 icons[ ITEM_TYPE_UNKNOWN ] = wxIcon( type_unknown_xpm );
343 icons[ ITEM_TYPE_DISC ] = wxIcon( type_disc_xpm );
344 icons[ ITEM_TYPE_DIRECTORY ] = wxIcon( type_directory_xpm );
345 icons[ ITEM_TYPE_PLAYLIST ] = wxIcon( type_playlist_xpm );
346 icons[ ITEM_TYPE_NET ] = wxIcon( type_net_xpm );
347 icons[ ITEM_TYPE_CARD ] = wxIcon( type_card_xpm );
349 for( unsigned int i = 0; i< WXSIZEOF( icons ) ; i++ )
351 p_images->Add( wxIcon( icons[i] ));
354 treectrl->AssignImageList( p_images );
356 treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
358 /* Reduce font size */
359 wxFont font= treectrl->GetFont();
360 font.SetPointSize(8);
361 treectrl->SetFont( font );
363 /* Create the Up-Down buttons */
365 wxButton *up_button =
366 new wxButton( playlist_panel, Up_Event, wxU(_("Up") ) );
367 wxButton *down_button =
368 new wxButton( playlist_panel, Down_Event, wxU(_("Down") ) );
370 wxBoxSizer *updown_sizer = new wxBoxSizer( wxHORIZONTAL );
371 updown_sizer->Layout();
372 /* The top and bottom sizers */
373 wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
374 bottom_sizer->Add( up_button, 0, wxALIGN_LEFT | wxRIGHT, 3);
375 bottom_sizer->Add( down_button, 0, wxALIGN_LEFT | wxLEFT, 3);
376 bottom_sizer->Layout();
378 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
379 panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
381 panel_sizer->Add( bottom_sizer, 0, wxALL, 5);
383 panel_sizer->Layout();
385 playlist_panel->SetSizerAndFit( panel_sizer );
387 #if wxUSE_DRAG_AND_DROP
388 /* Associate drop targets with the playlist */
389 SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
392 playlist_t *p_playlist =
393 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
395 if( p_playlist == NULL )
400 /* We want to be noticed of playlist changes */
402 /* Some global changes happened -> Rebuild all */
403 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
405 /* We went to the next item */
406 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
408 /* One item has been updated */
409 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
411 vlc_object_release( p_playlist );
413 /* Update the playlist */
417 void Playlist::OnSize( wxSizeEvent& event)
420 wxSize size = GetClientSize();
422 listview->SetColumnWidth( 0, size.x - listview->GetColumnWidth(1)
423 - 15 /* margins */ );
428 Playlist::~Playlist()
430 playlist_t *p_playlist =
431 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
433 if( p_playlist == NULL )
438 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
439 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
440 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
441 vlc_object_release( p_playlist );
444 /**********************************************************************
445 * Update one playlist item
446 **********************************************************************/
447 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
452 for( int i = 0; i< p_node->i_children ; i++ )
456 child = treectrl->GetFirstChild( node, cookie);
460 child = treectrl->GetNextChild( node, cookie );
465 /* Not enough children */
466 CreateNode( p_playlist, p_node->pp_children[i], node );
467 /* Keep the tree pointer up to date */
468 child = treectrl->GetNextChild( node, cookie );
476 /* Creates the node p_node as last child of parent */
477 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
478 wxTreeItemId parent )
482 treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
483 -1,-1, new PlaylistItem( p_node ) );
484 treectrl->SetItemImage( node, p_node->input.i_type );
486 for( int i = 0; i< p_node->i_children ; i++ )
488 /* Append the item */
489 if( p_node->pp_children[i]->i_children == -1 )
492 treectrl->AppendItem( node,
493 wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
494 new PlaylistItem( p_node->pp_children[i]) );
496 treectrl->SetItemImage( item,
497 p_node->pp_children[i]->input.i_type );
501 CreateNode( p_playlist, p_node->pp_children[i],
507 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
510 PlaylistItem *p_wxcurrent;
512 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
517 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
518 if( p_wxcurrent->p_item == p_item )
522 if( treectrl->ItemHasChildren( item ) )
524 wxTreeItemId search = FindItem( item, p_item );
530 item = treectrl->GetNextChild( root, cookie);
537 /*wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current )
540 PlaylistItem *p_wxcurrent;
542 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
547 if( treectrl->GetItemText( item).Lower().Contains(
548 search_string.Lower() ) )
551 if( treectrl->ItemHasChildren( item ) )
553 wxTreeItemId search = FindItem( item, p_item );
559 item = treectrl->GetNextChild( root, cookie);
562 /*wxTreeItemId dummy;
569 void Playlist::SetCurrentItem( wxTreeItemId item )
573 treectrl->SetItemBold( item, true );
574 treectrl->EnsureVisible( item );
578 void Playlist::UpdateItem( int i )
580 if( i < 0 ) return; /* Sanity check */
581 playlist_item_t *p_item;
583 playlist_t *p_playlist =
584 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
587 if( p_playlist == NULL )
592 p_item = playlist_ItemGetById( p_playlist, i );
594 wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
596 UpdateTreeItem( p_playlist, item );
598 vlc_object_release(p_playlist);
602 void Playlist::UpdateTreeItem( playlist_t *p_playlist ,wxTreeItemId item )
604 playlist_item_t *p_item;
611 p_item = ((PlaylistItem *)treectrl->GetItemData( item ))->p_item;
619 char *psz_author = playlist_ItemGetInfo( p_item, _("Meta-information"),
621 char psz_duration[MSTRTIME_MAX_SIZE];
622 mtime_t dur = p_item->input.i_duration;
624 secstotimestr( psz_duration, dur/1000000 );
626 memcpy( psz_duration, "-:--:--", sizeof("-:--:--") );
628 if( !strcmp( psz_author, "" ) )
630 msg.Printf( wxString( wxL2U( p_item->input.psz_name ) ) + wxU( " ( ") +
631 wxString(wxL2U(psz_duration ) ) + wxU( ")") );
635 msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
636 wxString(wxL2U(p_item->input.psz_name)) + wxU( " ( ") +
637 wxString(wxL2U(psz_duration ) ) + wxU( ")") );
639 treectrl->SetItemText( item , msg );
641 if( p_playlist->status.p_item == p_item )
643 SetCurrentItem( item );
647 treectrl->SetItemBold( item, false );
650 if( p_item->b_enabled == VLC_FALSE )
653 listitem.m_itemId = i;
654 listitem.SetTextColour( *wxLIGHT_GREY);
655 listview->SetItem(listitem);
660 /**********************************************************************
661 * Rebuild the playlist
662 **********************************************************************/
663 void Playlist::Rebuild()
665 playlist_view_t *p_view;
666 playlist_t *p_playlist =
667 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
669 if( p_playlist == NULL )
674 /* ...and rebuild it */
675 vlc_mutex_lock( &p_playlist->object_lock );
677 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
679 /* HACK we should really get new*/
680 msg_Dbg( p_intf, "rebuilding tree" );
681 treectrl->DeleteAllItems();
682 treectrl->AddRoot( wxU(_("root" )), -1, -1,
683 new PlaylistItem( p_view->p_root) );
685 wxTreeItemId root = treectrl->GetRootItem();
686 UpdateNode( p_playlist, p_view->p_root, root );
689 if( p_playlist->status.p_item != NULL )
691 item = FindItem( root, p_playlist->status.p_item );
693 else if( p_playlist->status.p_node != NULL )
695 item = FindItem( root, p_playlist->status.p_node );
702 SetCurrentItem( item );
704 vlc_mutex_unlock( &p_playlist->object_lock );
706 vlc_object_release( p_playlist );
709 void Playlist::ShowPlaylist( bool show )
711 if( show ) Rebuild();
715 void Playlist::UpdatePlaylist()
719 /* If the playlist isn't show there's no need to update it */
720 if( !IsShown() ) return;
722 if( this->b_need_update )
724 this->b_need_update = VLC_FALSE;
728 /* Updating the playing status every 0.5s is enough */
729 if( i_update_counter % 5 ) return;
731 playlist_t *p_playlist =
732 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
734 if( p_playlist == NULL )
739 /* Update the colour of items */
740 int i_playlist_index = p_playlist->i_index;
741 if( p_intf->p_sys->i_playing != i_playlist_index )
744 listitem.m_itemId = i_playlist_index;
745 listitem.SetTextColour( *wxRED );
746 listview->SetItem( listitem );
748 if( p_intf->p_sys->i_playing != -1 )
750 listitem.m_itemId = p_intf->p_sys->i_playing;
751 listitem.SetTextColour( *wxBLACK );
752 listview->SetItem( listitem );
754 p_intf->p_sys->i_playing = i_playlist_index;
757 vlc_object_release( p_playlist );
760 /*****************************************************************************
762 *****************************************************************************/
763 void Playlist::DeleteItem( int item_id )
765 playlist_t *p_playlist =
766 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
768 if( p_playlist == NULL )
773 playlist_Delete( p_playlist, item_id );
775 vlc_object_release( p_playlist );
778 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
783 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
789 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
790 { _("PLS file"), "*.pls", "export-pls" }};
792 wxString filter = wxT("");
794 playlist_t * p_playlist =
795 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
802 if( p_playlist->i_size == 0 )
804 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
805 wxICON_WARNING | wxOK, this );
806 vlc_object_release( p_playlist );
810 for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
812 filter.Append( wxU(formats[i].psz_desc) );
813 filter.Append( wxT("|") );
814 filter.Append( wxU(formats[i].psz_filter) );
815 filter.Append( wxT("|") );
817 wxFileDialog dialog( this, wxU(_("Save playlist")),
818 wxT(""), wxT(""), filter, wxSAVE );
820 if( dialog.ShowModal() == wxID_OK )
822 if( dialog.GetPath().mb_str() )
824 playlist_Export( p_playlist, dialog.GetPath().mb_str(),
825 formats[dialog.GetFilterIndex()].psz_module );
829 vlc_object_release( p_playlist );
833 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
835 playlist_t *p_playlist =
836 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
838 if( p_playlist == NULL )
843 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
844 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
846 if( dialog.ShowModal() == wxID_OK )
848 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
851 vlc_object_release( p_playlist );
854 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
856 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
860 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
862 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
866 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
868 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
872 /********************************************************************
874 ********************************************************************/
875 void Playlist::OnUp( wxCommandEvent& event )
877 playlist_t *p_playlist =
878 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
880 if( p_playlist == NULL )
885 /* We use the first selected item, so find it */
886 long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
887 wxLIST_STATE_SELECTED);
888 if( i_item > 0 && i_item < p_playlist->i_size )
890 playlist_Move( p_playlist, i_item, i_item - 1 );
891 listview->Focus( i_item - 1 );
894 vlc_object_release( p_playlist );
897 void Playlist::OnDown( wxCommandEvent& event )
899 playlist_t *p_playlist =
900 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
902 if( p_playlist == NULL )
907 /* We use the first selected item, so find it */
908 long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
909 wxLIST_STATE_SELECTED );
910 if( i_item >= 0 && i_item < p_playlist->i_size - 1 )
912 playlist_Move( p_playlist, i_item, i_item + 2 );
913 listview->Focus( i_item + 1 );
916 vlc_object_release( p_playlist );
919 /********************************************************************
921 ********************************************************************/
922 void Playlist::OnSort( wxCommandEvent& event )
924 playlist_t *p_playlist =
925 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
927 if( p_playlist == NULL )
931 switch( event.GetId() )
933 case SortTitle_Event:
934 playlist_SortTitle( p_playlist, ORDER_NORMAL );
936 case RSortTitle_Event:
937 playlist_SortTitle( p_playlist, ORDER_REVERSE );
939 case SortAuthor_Event:
940 playlist_SortAuthor(p_playlist, ORDER_NORMAL );
942 case RSortAuthor_Event:
943 playlist_SortAuthor( p_playlist, ORDER_REVERSE );
945 case Randomize_Event:
946 playlist_Sort( p_playlist, SORT_RANDOM, ORDER_NORMAL );
949 vlc_object_release( p_playlist );
954 /**********************************************************************
956 **********************************************************************/
957 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
959 search_button->SetDefault();
962 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
964 wxString search_string = search_text->GetValue();
973 for( i_current = 0; i_current < listview->GetItemCount(); i_current++ )
975 if( listview->GetItemState( i_current, wxLIST_STATE_SELECTED ) ==
976 wxLIST_STATE_SELECTED )
983 if( i_first == listview->GetItemCount() )
988 for( i_current = i_first + 1; i_current < listview->GetItemCount();
992 listitem.SetId( i_current );
993 listview->GetItem( listitem );
994 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1000 listitem.SetColumn( 1 );
1001 listview->GetItem( listitem );
1002 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1011 for( i_current = -1 ; i_current < i_first - 1;
1014 wxListItem listitem;
1015 listitem.SetId( i_current );
1016 listview->GetItem( listitem );
1017 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1023 listitem.SetColumn( 1 );
1024 listview->GetItem( listitem );
1025 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1034 if( i_item < 0 || i_item >= listview->GetItemCount() )
1039 for( long item = 0; item < listview->GetItemCount(); item++ )
1041 listview->Select( item, FALSE );
1044 wxListItem listitem;
1045 listitem.SetId(i_item);
1046 listitem.m_state = wxLIST_STATE_SELECTED;
1047 listview->Select( i_item, TRUE );
1048 listview->Focus( i_item );
1052 /**********************************************************************
1053 * Selection functions
1054 **********************************************************************/
1055 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1059 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1064 void Playlist::OnEnableSelection( wxCommandEvent& WXUNUSED(event) )
1066 playlist_t *p_playlist =
1067 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1069 if( p_playlist == NULL )
1074 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
1076 if( listview->IsSelected( item ) )
1079 playlist_Enable( p_playlist, item );
1084 vlc_object_release( p_playlist);
1087 void Playlist::OnDisableSelection( wxCommandEvent& WXUNUSED(event) )
1089 playlist_t *p_playlist =
1090 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1092 if( p_playlist == NULL )
1097 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
1099 if( listview->IsSelected( item ) )
1102 playlist_Disable( p_playlist, item );
1107 vlc_object_release( p_playlist);
1110 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1114 /**********************************************************************
1115 * Playlist mode functions
1116 **********************************************************************/
1117 void Playlist::OnRandom( wxCommandEvent& event )
1120 val.b_bool = event.IsChecked();
1121 playlist_t *p_playlist =
1122 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1124 if( p_playlist == NULL )
1128 var_Set( p_playlist, "random", val);
1129 vlc_object_release( p_playlist );
1132 void Playlist::OnLoop( wxCommandEvent& event )
1135 val.b_bool = event.IsChecked();
1136 playlist_t *p_playlist =
1137 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1139 if( p_playlist == NULL )
1143 var_Set( p_playlist, "loop", val);
1144 vlc_object_release( p_playlist );
1147 void Playlist::OnRepeat( wxCommandEvent& event )
1150 val.b_bool = event.IsChecked();
1151 playlist_t *p_playlist =
1152 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1154 if( p_playlist == NULL )
1158 var_Set( p_playlist, "repeat", val);
1159 vlc_object_release( p_playlist );
1162 void Playlist::OnActivateItem( wxTreeEvent& event )
1164 playlist_item_t *p_item,*p_node;
1165 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1166 VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1167 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1169 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1171 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1173 if( p_playlist == NULL )
1178 if( p_wxitem->p_item->i_children == -1 )
1180 p_node = p_wxparent->p_item;
1181 p_item = p_wxitem->p_item;
1185 p_node = p_wxitem->p_item;
1186 if( p_wxitem->p_item->i_children > 0 )
1188 p_item = p_wxitem->p_item->pp_children[0];
1196 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1199 vlc_object_release( p_playlist );
1202 void Playlist::OnKeyDown( wxTreeEvent& event )
1204 long keycode = event.GetKeyCode();
1205 /* Delete selected items */
1206 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1208 /* We send a dummy event */
1209 OnDeleteSelection( event );
1213 void Playlist::ShowInfos( int i_item )
1217 void Playlist::OnInfos( wxCommandEvent& WXUNUSED(event) )
1221 void Playlist::OnEnDis( wxCommandEvent& event )
1223 playlist_t *p_playlist =
1224 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1226 if( p_playlist == NULL )
1230 vlc_object_release( p_playlist );
1233 /**********************************************************************
1235 **********************************************************************/
1237 void Playlist::OnMenuOpen( wxMenuEvent& event)
1239 #if defined( __WXMSW__ )
1240 # define GetEventObject GetMenu
1243 if( event.GetEventObject() == p_view_menu )
1245 p_view_menu = ViewMenu();
1247 #if defined( __WXMSW__ )
1248 # undef GetEventObject
1252 void Playlist::OnMenuEvent( wxCommandEvent& event )
1254 playlist_t *p_playlist =
1255 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1257 if( p_playlist == NULL )
1262 if( event.GetId() < FirstView_Event )
1265 vlc_object_release( p_playlist );
1268 else if( event.GetId() < LastView_Event )
1271 int i_new_view = event.GetId() - FirstView_Event;
1273 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1275 if( p_view != NULL )
1277 i_current_view = i_new_view;
1278 playlist_ViewUpdate( p_playlist, i_new_view );
1280 vlc_object_release( p_playlist );
1283 else if( i_new_view >= VIEW_FIRST_SORTED &&
1284 i_new_view <= VIEW_LAST_SORTED )
1286 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1287 playlist_ViewUpdate( p_playlist, i_new_view );
1289 i_current_view = i_new_view;
1294 else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1296 if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1297 pp_sds[event.GetId() - FirstSD_Event] ) )
1299 playlist_ServicesDiscoveryAdd( p_playlist,
1300 pp_sds[event.GetId() - FirstSD_Event] );
1304 playlist_ServicesDiscoveryRemove( p_playlist,
1305 pp_sds[event.GetId() - FirstSD_Event] );
1308 vlc_object_release( p_playlist );
1311 wxMenu * Playlist::ViewMenu()
1313 playlist_t *p_playlist =
1314 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1316 if( p_playlist == NULL )
1323 p_view_menu = new wxMenu;
1327 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1330 wxMenuItem *item = node->GetData();
1331 node = node->GetNext();
1332 p_view_menu->Delete( item );
1336 /* FIXME : have a list of "should have" views */
1337 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1338 wxU(_("By category") ) );
1339 p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1340 wxU(_("Manually added") ) );
1341 p_view_menu->Append( FirstView_Event + VIEW_ALL,
1342 wxU(_("All items, unsorted") ) );
1343 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1344 wxU(_("Sorted by author") ) );
1346 vlc_object_release( p_playlist);
1351 wxMenu *Playlist::SDMenu()
1354 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1355 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1360 vlc_value_t val, val_list, text_list;
1361 p_sd_menu = new wxMenu;
1363 vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1367 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1369 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1371 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1373 p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1374 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1375 p_parser->psz_shortname ) );
1377 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1378 p_parser->psz_shortname ) )
1380 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1383 INSERT_ELEM( (void**)pp_sds, i_number, i_number,
1384 (void*)p_parser->psz_shortname );
1387 vlc_list_release( p_list );
1388 vlc_object_release( p_playlist );
1393 /*****************************************************************************
1394 * Popup management functions
1395 *****************************************************************************/
1396 void Playlist::OnPopup( wxContextMenuEvent& event )
1398 wxPoint pt = event.GetPosition();
1400 i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1401 if( i_popup_item.IsOk() )
1403 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1405 PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1406 treectrl->GetItemParent( i_popup_item ) );
1407 p_popup_item = p_wxitem->p_item;
1408 p_popup_parent = p_wxparent->p_item;
1409 treectrl->SelectItem( i_popup_item );
1410 Playlist::PopupMenu( popup_menu,
1411 ScreenToClient( wxGetMousePosition() ) );
1418 void Playlist::OnPopupPlay( wxMenuEvent& event )
1420 playlist_t *p_playlist =
1421 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1423 if( p_playlist == NULL )
1427 if( p_popup_item != NULL )
1429 if( p_popup_item->i_children > -1 )
1431 if( event.GetId() == PopupPlay_Event &&
1432 p_popup_item->i_children > 0 )
1434 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1435 i_current_view, p_popup_item,
1436 p_popup_item->pp_children[0] );
1440 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1441 i_current_view, p_popup_item, NULL );
1446 if( event.GetId() == PopupPlay_Event )
1448 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1449 i_current_view, p_popup_parent,
1454 vlc_object_release( p_playlist );
1457 void Playlist::OnPopupDel( wxMenuEvent& event )
1459 PlaylistItem *p_wxitem;
1461 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1463 if( p_wxitem->p_item->i_children == -1 )
1465 DeleteItem( p_wxitem->p_item->input.i_id );
1469 //DeleteNode( p_wxitem->p_item );
1473 void Playlist::OnPopupEna( wxMenuEvent& event )
1475 playlist_t *p_playlist =
1476 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1478 if( p_playlist == NULL )
1483 p_popup_item->b_enabled = VLC_TRUE - p_popup_item->b_enabled;
1485 vlc_object_release( p_playlist);
1486 UpdateItem( i_popup_item );
1489 void Playlist::OnPopupInfo( wxMenuEvent& event )
1493 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1494 if( iteminfo_dialog->ShowModal() == wxID_OK )
1496 UpdateItem( i_popup_item );
1498 delete iteminfo_dialog;
1503 /*****************************************************************************
1504 * Custom events management
1505 *****************************************************************************/
1506 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1508 switch( event.GetId() )
1510 case UpdateItem_Event:
1511 UpdateItem( event.GetInt() );
1516 /*****************************************************************************
1517 * PlaylistChanged: callback triggered by the intf-change playlist variable
1518 * We don't rebuild the playlist directly here because we don't want the
1519 * caller to block for a too long time.
1520 *****************************************************************************/
1521 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1522 vlc_value_t oval, vlc_value_t nval, void *param )
1524 Playlist *p_playlist_dialog = (Playlist *)param;
1525 p_playlist_dialog->b_need_update = VLC_TRUE;
1529 /*****************************************************************************
1530 * Next: callback triggered by the playlist-current playlist variable
1531 *****************************************************************************/
1532 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1533 vlc_value_t oval, vlc_value_t nval, void *param )
1535 Playlist *p_playlist_dialog = (Playlist *)param;
1537 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1538 event.SetInt( oval.i_int );
1539 p_playlist_dialog->AddPendingEvent( event );
1540 event.SetInt( nval.i_int );
1541 p_playlist_dialog->AddPendingEvent( event );
1546 /*****************************************************************************
1547 * ItemChanged: callback triggered by the item-change playlist variable
1548 *****************************************************************************/
1549 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1550 vlc_value_t old_val, vlc_value_t new_val, void *param )
1552 Playlist *p_playlist_dialog = (Playlist *)param;
1554 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1555 event.SetInt( new_val.i_int );
1556 p_playlist_dialog->AddPendingEvent( event );