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>
46 #define HELP_SHUFFLE N_( "Shuffle" )
47 #define HELP_LOOP N_( "Loop" )
48 #define HELP_REPEAT N_( "Repeat" )
50 /* Callback prototype */
51 static int PlaylistChanged( vlc_object_t *, const char *,
52 vlc_value_t, vlc_value_t, void * );
53 static int PlaylistNext( vlc_object_t *, const char *,
54 vlc_value_t, vlc_value_t, void * );
55 static int ItemChanged( vlc_object_t *, const char *,
56 vlc_value_t, vlc_value_t, void * );
58 /*****************************************************************************
60 *****************************************************************************/
62 /* IDs for the controls and the menu commands */
78 EnableSelection_Event,
79 DisableSelection_Event,
81 InvertSelection_Event,
82 DeleteSelection_Event,
103 Browse_Event, /* For export playlist */
108 FirstView_Event = wxID_HIGHEST + 1000,
111 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
113 BEGIN_EVENT_TABLE(Playlist, wxFrame)
114 EVT_SIZE(Playlist::OnSize)
117 EVT_MENU(AddFile_Event, Playlist::OnAddFile)
118 EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
119 EVT_MENU(Close_Event, Playlist::OnClose)
120 EVT_MENU(Open_Event, Playlist::OnOpen)
121 EVT_MENU(Save_Event, Playlist::OnSave)
123 EVT_MENU(SortTitle_Event, Playlist::OnSort)
124 EVT_MENU(RSortTitle_Event, Playlist::OnSort)
125 EVT_MENU(SortAuthor_Event, Playlist::OnSort)
126 EVT_MENU(RSortAuthor_Event, Playlist::OnSort)
128 EVT_MENU(Randomize_Event, Playlist::OnSort)
130 EVT_MENU(EnableSelection_Event, Playlist::OnEnableSelection)
131 EVT_MENU(DisableSelection_Event, Playlist::OnDisableSelection)
132 EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
133 EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
134 EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
135 EVT_MENU(Infos_Event, Playlist::OnInfos)
137 EVT_MENU_OPEN( Playlist::OnMenuOpen )
138 EVT_MENU( -1, Playlist::OnMenuEvent )
140 EVT_TOOL(Random_Event, Playlist::OnRandom)
141 EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
142 EVT_TOOL(Loop_Event, Playlist::OnLoop)
145 EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
146 EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
147 EVT_MENU( PopupEna_Event, Playlist::OnPopupEna)
148 EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
150 /* Tree control events */
151 EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
153 EVT_CONTEXT_MENU( Playlist::OnPopup )
156 EVT_BUTTON( Search_Event, Playlist::OnSearch)
157 EVT_BUTTON( Save_Event, Playlist::OnSave)
158 EVT_BUTTON( Infos_Event, Playlist::OnInfos)
160 EVT_BUTTON( Up_Event, Playlist::OnUp)
161 EVT_BUTTON( Down_Event, Playlist::OnDown)
163 EVT_TEXT(SearchText_Event, Playlist::OnSearchTextChange)
166 EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
168 /* Special events : we don't want to destroy the window when the user
170 EVT_CLOSE(Playlist::OnClose)
173 /*****************************************************************************
175 ****************************************************************************/
176 class PlaylistItem : public wxTreeItemData
179 PlaylistItem( playlist_item_t *_p_item ) : wxTreeItemData()
184 playlist_item_t *p_item;
185 friend class Playlist;
188 /*****************************************************************************
190 *****************************************************************************/
191 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
192 wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
193 wxSize(345,400), wxDEFAULT_FRAME_STYLE )
197 /* Initializations */
199 i_update_counter = 0;
200 i_sort_mode = MODE_NONE;
201 b_need_update = VLC_FALSE;
202 SetIcon( *p_intf->p_sys->p_icon );
206 i_current_view = VIEW_SIMPLE;
211 i_duration_sorted = 0;
213 var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
214 var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
215 var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
217 /* Create our "Manage" menu */
218 wxMenu *manage_menu = new wxMenu;
219 manage_menu->Append( AddFile_Event, wxU(_("&Simple Add...")) );
220 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
221 manage_menu->AppendSeparator();
222 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
223 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
224 manage_menu->AppendSeparator();
225 manage_menu->Append( Close_Event, wxU(_("&Close")) );
227 /* Create our "Sort" menu */
228 wxMenu *sort_menu = new wxMenu;
229 sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
230 sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
231 sort_menu->AppendSeparator();
232 sort_menu->Append( SortAuthor_Event, wxU(_("Sort by &author")) );
233 sort_menu->Append( RSortAuthor_Event, wxU(_("Reverse sort by author")) );
234 sort_menu->AppendSeparator();
235 sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
237 /* Create our "Selection" menu */
238 wxMenu *selection_menu = new wxMenu;
239 selection_menu->Append( EnableSelection_Event, wxU(_("&Enable")) );
240 selection_menu->Append( DisableSelection_Event, wxU(_("&Disable")) );
241 selection_menu->AppendSeparator();
242 selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
243 selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
244 selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
246 /* Create our "View" menu */
249 /* Append the freshly created menus to the menu bar */
250 wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
251 menubar->Append( manage_menu, wxU(_("&Manage")) );
252 menubar->Append( sort_menu, wxU(_("S&ort")) );
253 menubar->Append( selection_menu, wxU(_("&Selection")) );
254 menubar->Append( p_view_menu, wxU(_("&View items") ) );
256 /* Attach the menu bar to the frame */
257 SetMenuBar( menubar );
259 /* Create the popup menu */
260 popup_menu = new wxMenu;
261 popup_menu->Append( PopupPlay_Event, wxU(_("Play")) );
262 popup_menu->Append( PopupDel_Event, wxU(_("Delete")) );
263 popup_menu->Append( PopupEna_Event, wxU(_("Enable/Disable")) );
264 popup_menu->Append( PopupInfo_Event, wxU(_("Info")) );
266 /* Create a panel to put everything in */
267 wxPanel *playlist_panel = new wxPanel( this, -1 );
268 playlist_panel->SetAutoLayout( TRUE );
270 /* Create the toolbar */
272 CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT | wxTB_DOCKABLE );
274 /* Create the random tool */
275 toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
276 wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
277 wxU(_(HELP_SHUFFLE) ) );
278 var_Get( p_intf, "random", &val );
279 toolbar->ToggleTool( Random_Event, val.b_bool );
281 /* Create the Loop tool */
282 toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
283 wxBitmap( loop_xpm), wxITEM_CHECK,
284 wxU(_(HELP_LOOP ) ) );
285 var_Get( p_intf, "loop", &val );
286 toolbar->ToggleTool( Loop_Event, val.b_bool );
288 /* Create the Repeat one checkbox */
289 toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
290 wxBitmap( repeat_xpm), wxITEM_CHECK,
291 wxU(_(HELP_REPEAT ) ) );
292 var_Get( p_intf, "repeat", &val );
293 toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
295 /* Create the Search Textbox */
296 search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
297 wxDefaultPosition, wxSize(100, -1),
300 /* Create the search button */
301 search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
303 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
304 wxSize(16, 16), wxBORDER_NONE ) );
305 toolbar->AddControl( search_text );
306 toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
307 wxSize(5, 5), wxBORDER_NONE ) );
308 toolbar->AddControl( search_button );
309 search_button->SetDefault();
312 /* Create the tree */
313 treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
314 wxDefaultPosition, wxDefaultSize,
315 wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
317 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
318 wxTR_MULTIPLE | wxTR_EXTENDED );
320 /* Create image list */
322 wxImageList *p_images = new wxImageList( 16 , 16, TRUE);
325 icons[ ITEM_TYPE_UNKNOWN ] = wxIcon( type_unknown_xpm );
326 icons[ ITEM_TYPE_DISC ] = wxIcon( type_disc_xpm );
327 icons[ ITEM_TYPE_DIRECTORY ] = wxIcon( type_directory_xpm );
328 icons[ ITEM_TYPE_PLAYLIST ] = wxIcon( type_playlist_xpm );
329 icons[ ITEM_TYPE_NET ] = wxIcon( type_net_xpm );
330 icons[ ITEM_TYPE_CARD ] = wxIcon( type_card_xpm );
332 for( int i = 0; i< WXSIZEOF( icons ) ; i++ )
334 p_images->Add( wxBitmap( wxBitmap(icons[i]).ConvertToImage().Rescale(16,16) ) );
337 treectrl->AssignImageList( p_images );
339 treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
341 /* Reduce font size */
342 wxFont font= treectrl->GetFont();
343 font.SetPointSize(8);
344 treectrl->SetFont( font );
346 /* Create the Up-Down buttons */
348 wxButton *up_button =
349 new wxButton( playlist_panel, Up_Event, wxU(_("Up") ) );
350 wxButton *down_button =
351 new wxButton( playlist_panel, Down_Event, wxU(_("Down") ) );
353 wxBoxSizer *updown_sizer = new wxBoxSizer( wxHORIZONTAL );
354 updown_sizer->Layout();
355 /* The top and bottom sizers */
356 wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
357 bottom_sizer->Add( up_button, 0, wxALIGN_LEFT | wxRIGHT, 3);
358 bottom_sizer->Add( down_button, 0, wxALIGN_LEFT | wxLEFT, 3);
359 bottom_sizer->Layout();
361 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
362 panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
364 panel_sizer->Add( bottom_sizer, 0, wxALL, 5);
366 panel_sizer->Layout();
368 playlist_panel->SetSizerAndFit( panel_sizer );
370 #if wxUSE_DRAG_AND_DROP
371 /* Associate drop targets with the playlist */
372 SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
375 playlist_t *p_playlist =
376 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
378 if( p_playlist == NULL )
383 /* We want to be noticed of playlist changes */
385 /* Some global changes happened -> Rebuild all */
386 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
388 /* We went to the next item */
389 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
391 /* One item has been updated */
392 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
394 vlc_object_release( p_playlist );
396 /* Update the playlist */
400 void Playlist::OnSize( wxSizeEvent& event)
403 wxSize size = GetClientSize();
405 listview->SetColumnWidth( 0, size.x - listview->GetColumnWidth(1)
406 - 15 /* margins */ );
411 Playlist::~Playlist()
413 playlist_t *p_playlist =
414 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
416 if( p_playlist == NULL )
421 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
422 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
423 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
424 vlc_object_release( p_playlist );
427 /**********************************************************************
428 * Update one playlist item
429 **********************************************************************/
430 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
435 for( int i = 0; i< p_node->i_children ; i++ )
439 child = treectrl->GetFirstChild( node, cookie);
443 child = treectrl->GetNextChild( node, cookie );
448 /* Not enough children */
449 CreateNode( p_playlist, p_node->pp_children[i], node );
450 /* Keep the tree pointer up to date */
451 child = treectrl->GetNextChild( node, cookie );
459 /* Creates the node p_node as last child of parent */
460 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
461 wxTreeItemId parent )
464 wxTreeItemId node = treectrl->AppendItem( parent, p_node->input.psz_name,
466 new PlaylistItem( p_node ) );
467 treectrl->SetItemImage( node, p_node->input.i_type );
468 for( int i = 0; i< p_node->i_children ; i++ )
470 /* Append the item */
471 if( p_node->pp_children[i]->i_children == -1 )
473 wxTreeItemId item = treectrl->AppendItem( node,
474 p_node->pp_children[i]->input.psz_name,
476 new PlaylistItem( p_node->pp_children[i]) );
478 treectrl->SetItemImage( item,
479 p_node->pp_children[i]->input.i_type);
483 CreateNode( p_playlist, p_node->pp_children[i],
489 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
492 PlaylistItem *p_wxcurrent;
494 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
499 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
500 if( p_wxcurrent->p_item == p_item )
504 if( treectrl->ItemHasChildren( item ) )
506 wxTreeItemId search = FindItem( item, p_item );
512 item = treectrl->GetNextChild( root, cookie);
519 /*wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current )
522 PlaylistItem *p_wxcurrent;
524 wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
529 if( treectrl->GetItemText( item).Lower().Contains(
530 search_string.Lower() ) )
533 if( treectrl->ItemHasChildren( item ) )
535 wxTreeItemId search = FindItem( item, p_item );
541 item = treectrl->GetNextChild( root, cookie);
544 /*wxTreeItemId dummy;
551 void Playlist::SetCurrentItem( wxTreeItemId item )
555 treectrl->SetItemBold( item, true );
556 treectrl->EnsureVisible( item );
560 void Playlist::UpdateItem( int i )
562 if( i < 0 ) return; /* Sanity check */
563 playlist_item_t *p_item;
565 playlist_t *p_playlist =
566 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
569 if( p_playlist == NULL )
574 p_item = playlist_ItemGetById( p_playlist, i );
576 wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
578 UpdateTreeItem( p_playlist, item );
580 vlc_object_release(p_playlist);
584 void Playlist::UpdateTreeItem( playlist_t *p_playlist ,wxTreeItemId item )
586 playlist_item_t *p_item;
593 p_item = ((PlaylistItem *)treectrl->GetItemData( item ))->p_item;
601 char *psz_author = playlist_ItemGetInfo( p_item, _("Meta-information"),
603 char psz_duration[MSTRTIME_MAX_SIZE];
604 mtime_t dur = p_item->input.i_duration;
606 secstotimestr( psz_duration, dur/1000000 );
608 memcpy( psz_duration, "-:--:--", sizeof("-:--:--") );
610 if( !strcmp( psz_author, "" ) )
612 msg.Printf( wxString( wxL2U( p_item->input.psz_name ) ) + wxU( " ( ") +
613 wxString(wxL2U(psz_duration ) ) + wxU( ")") );
617 msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
618 wxString(wxL2U(p_item->input.psz_name)) + wxU( " ( ") +
619 wxString(wxL2U(psz_duration ) ) + wxU( ")") );
621 treectrl->SetItemText( item , msg );
623 if( p_playlist->status.p_item == p_item )
625 SetCurrentItem( item );
629 treectrl->SetItemBold( item, false );
632 if( p_item->b_enabled == VLC_FALSE )
635 listitem.m_itemId = i;
636 listitem.SetTextColour( *wxLIGHT_GREY);
637 listview->SetItem(listitem);
642 /**********************************************************************
643 * Rebuild the playlist
644 **********************************************************************/
645 void Playlist::Rebuild()
647 playlist_view_t *p_view;
648 playlist_t *p_playlist =
649 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
651 if( p_playlist == NULL )
656 /* ...and rebuild it */
657 vlc_mutex_lock( &p_playlist->object_lock );
659 p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
661 /* HACK we should really get new*/
662 msg_Dbg( p_intf, "rebuilding tree" );
663 treectrl->DeleteAllItems();
664 treectrl->AddRoot( wxU(_("root" )), -1, -1,
665 new PlaylistItem( p_view->p_root) );
667 wxTreeItemId root = treectrl->GetRootItem();
668 UpdateNode( p_playlist, p_view->p_root, root );
671 if( p_playlist->status.p_item != NULL )
673 item = FindItem( root, p_playlist->status.p_item );
675 else if( p_playlist->status.p_node != NULL )
677 item = FindItem( root, p_playlist->status.p_node );
684 SetCurrentItem( item );
686 vlc_mutex_unlock( &p_playlist->object_lock );
688 vlc_object_release( p_playlist );
691 void Playlist::ShowPlaylist( bool show )
693 if( show ) Rebuild();
697 void Playlist::UpdatePlaylist()
701 /* If the playlist isn't show there's no need to update it */
702 if( !IsShown() ) return;
704 if( this->b_need_update )
706 this->b_need_update = VLC_FALSE;
710 /* Updating the playing status every 0.5s is enough */
711 if( i_update_counter % 5 ) return;
713 playlist_t *p_playlist =
714 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
716 if( p_playlist == NULL )
721 /* Update the colour of items */
722 int i_playlist_index = p_playlist->i_index;
723 if( p_intf->p_sys->i_playing != i_playlist_index )
726 listitem.m_itemId = i_playlist_index;
727 listitem.SetTextColour( *wxRED );
728 listview->SetItem( listitem );
730 if( p_intf->p_sys->i_playing != -1 )
732 listitem.m_itemId = p_intf->p_sys->i_playing;
733 listitem.SetTextColour( *wxBLACK );
734 listview->SetItem( listitem );
736 p_intf->p_sys->i_playing = i_playlist_index;
739 vlc_object_release( p_playlist );
742 /*****************************************************************************
744 *****************************************************************************/
745 void Playlist::DeleteItem( int item_id )
747 playlist_t *p_playlist =
748 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
750 if( p_playlist == NULL )
755 playlist_Delete( p_playlist, item_id );
757 vlc_object_release( p_playlist );
760 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
765 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
771 } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
772 { _("PLS file"), "*.pls", "export-pls" }};
774 wxString filter = wxT("");
776 playlist_t * p_playlist =
777 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
784 if( p_playlist->i_size == 0 )
786 wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
787 wxICON_WARNING | wxOK, this );
788 vlc_object_release( p_playlist );
792 for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
794 filter.Append( wxU(formats[i].psz_desc) );
795 filter.Append( wxT("|") );
796 filter.Append( wxU(formats[i].psz_filter) );
797 filter.Append( wxT("|") );
799 wxFileDialog dialog( this, wxU(_("Save playlist")),
800 wxT(""), wxT(""), filter, wxSAVE );
802 if( dialog.ShowModal() == wxID_OK )
804 if( dialog.GetPath().mb_str() )
806 playlist_Export( p_playlist, dialog.GetPath().mb_str(),
807 formats[dialog.GetFilterIndex()].psz_module );
811 vlc_object_release( p_playlist );
815 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
817 playlist_t *p_playlist =
818 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
820 if( p_playlist == NULL )
825 wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
826 wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
828 if( dialog.ShowModal() == wxID_OK )
830 playlist_Import( p_playlist, dialog.GetPath().mb_str() );
833 vlc_object_release( p_playlist );
836 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
838 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
842 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
844 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
848 /********************************************************************
850 ********************************************************************/
851 void Playlist::OnUp( wxCommandEvent& event )
853 playlist_t *p_playlist =
854 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
856 if( p_playlist == NULL )
861 /* We use the first selected item, so find it */
862 long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
863 wxLIST_STATE_SELECTED);
864 if( i_item > 0 && i_item < p_playlist->i_size )
866 playlist_Move( p_playlist, i_item, i_item - 1 );
867 listview->Focus( i_item - 1 );
870 vlc_object_release( p_playlist );
873 void Playlist::OnDown( wxCommandEvent& event )
875 playlist_t *p_playlist =
876 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
878 if( p_playlist == NULL )
883 /* We use the first selected item, so find it */
884 long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
885 wxLIST_STATE_SELECTED );
886 if( i_item >= 0 && i_item < p_playlist->i_size - 1 )
888 playlist_Move( p_playlist, i_item, i_item + 2 );
889 listview->Focus( i_item + 1 );
892 vlc_object_release( p_playlist );
895 /********************************************************************
897 ********************************************************************/
898 void Playlist::OnSort( wxCommandEvent& event )
900 playlist_t *p_playlist =
901 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
903 if( p_playlist == NULL )
907 switch( event.GetId() )
909 case SortTitle_Event:
910 playlist_SortTitle( p_playlist, ORDER_NORMAL );
912 case RSortTitle_Event:
913 playlist_SortTitle( p_playlist, ORDER_REVERSE );
915 case SortAuthor_Event:
916 playlist_SortAuthor(p_playlist, ORDER_NORMAL );
918 case RSortAuthor_Event:
919 playlist_SortAuthor( p_playlist, ORDER_REVERSE );
921 case Randomize_Event:
922 playlist_Sort( p_playlist, SORT_RANDOM, ORDER_NORMAL );
925 vlc_object_release( p_playlist );
930 /**********************************************************************
932 **********************************************************************/
933 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
935 search_button->SetDefault();
938 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
940 wxString search_string = search_text->GetValue();
949 for( i_current = 0; i_current < listview->GetItemCount(); i_current++ )
951 if( listview->GetItemState( i_current, wxLIST_STATE_SELECTED ) ==
952 wxLIST_STATE_SELECTED )
959 if( i_first == listview->GetItemCount() )
964 for( i_current = i_first + 1; i_current < listview->GetItemCount();
968 listitem.SetId( i_current );
969 listview->GetItem( listitem );
970 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
976 listitem.SetColumn( 1 );
977 listview->GetItem( listitem );
978 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
987 for( i_current = -1 ; i_current < i_first - 1;
991 listitem.SetId( i_current );
992 listview->GetItem( listitem );
993 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
999 listitem.SetColumn( 1 );
1000 listview->GetItem( listitem );
1001 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1010 if( i_item < 0 || i_item >= listview->GetItemCount() )
1015 for( long item = 0; item < listview->GetItemCount(); item++ )
1017 listview->Select( item, FALSE );
1020 wxListItem listitem;
1021 listitem.SetId(i_item);
1022 listitem.m_state = wxLIST_STATE_SELECTED;
1023 listview->Select( i_item, TRUE );
1024 listview->Focus( i_item );
1028 /**********************************************************************
1029 * Selection functions
1030 **********************************************************************/
1031 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1035 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1040 void Playlist::OnEnableSelection( wxCommandEvent& WXUNUSED(event) )
1042 playlist_t *p_playlist =
1043 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1045 if( p_playlist == NULL )
1050 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
1052 if( listview->IsSelected( item ) )
1055 playlist_Enable( p_playlist, item );
1060 vlc_object_release( p_playlist);
1063 void Playlist::OnDisableSelection( wxCommandEvent& WXUNUSED(event) )
1065 playlist_t *p_playlist =
1066 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1068 if( p_playlist == NULL )
1073 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
1075 if( listview->IsSelected( item ) )
1078 playlist_Disable( p_playlist, item );
1083 vlc_object_release( p_playlist);
1086 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1089 for( long item = 0; item < listview->GetItemCount(); item++ )
1091 listview->Select( item, TRUE );
1096 /**********************************************************************
1097 * Playlist mode functions
1098 **********************************************************************/
1099 void Playlist::OnRandom( wxCommandEvent& event )
1102 val.b_bool = event.IsChecked();
1103 playlist_t *p_playlist =
1104 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1106 if( p_playlist == NULL )
1110 var_Set( p_playlist, "random", val);
1111 vlc_object_release( p_playlist );
1114 void Playlist::OnLoop( wxCommandEvent& event )
1117 val.b_bool = event.IsChecked();
1118 playlist_t *p_playlist =
1119 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1121 if( p_playlist == NULL )
1125 var_Set( p_playlist, "loop", val);
1126 vlc_object_release( p_playlist );
1129 void Playlist::OnRepeat( wxCommandEvent& event )
1132 val.b_bool = event.IsChecked();
1133 playlist_t *p_playlist =
1134 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1136 if( p_playlist == NULL )
1140 var_Set( p_playlist, "repeat", val);
1141 vlc_object_release( p_playlist );
1146 void Playlist::OnActivateItem( wxTreeEvent& event )
1148 playlist_item_t *p_item,*p_node;
1149 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1150 VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1151 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1153 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1156 fprintf(stderr,"Ca gère\n" );
1160 fprintf(stderr,"Ca craint\n" );
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;
1180 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1183 vlc_object_release( p_playlist );
1186 void Playlist::OnKeyDown( wxTreeEvent& event )
1188 long keycode = event.GetKeyCode();
1189 /* Delete selected items */
1190 if( keycode == WXK_BACK || keycode == WXK_DELETE )
1192 /* We send a dummy event */
1193 OnDeleteSelection( event );
1197 void Playlist::ShowInfos( int i_item )
1201 void Playlist::OnInfos( wxCommandEvent& WXUNUSED(event) )
1203 /* We use the first selected item, so find it */
1205 long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
1206 wxLIST_STATE_SELECTED );
1207 ShowInfos( i_item );
1211 void Playlist::OnEnDis( wxCommandEvent& event )
1213 playlist_t *p_playlist =
1214 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1216 if( p_playlist == NULL )
1221 long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
1222 wxLIST_STATE_SELECTED );
1224 if( i_item >= 0 && i_item < p_playlist->i_size )
1229 vlc_object_release( p_playlist );
1232 /**********************************************************************
1234 **********************************************************************/
1236 void Playlist::OnMenuOpen( wxMenuEvent& event)
1238 #if defined( __WXMSW__ )
1239 # define GetEventObject GetMenu
1242 if( event.GetEventObject() == p_view_menu )
1244 p_view_menu = ViewMenu();
1246 #if defined( __WXMSW__ )
1247 # undef GetEventObject
1251 void Playlist::OnMenuEvent( wxCommandEvent& event )
1253 playlist_t *p_playlist =
1254 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1256 if( p_playlist == NULL )
1261 if( event.GetId() < FirstView_Event )
1267 int i_new_view = event.GetId() - FirstView_Event;
1269 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1271 if( p_view != NULL )
1273 i_current_view = i_new_view;
1274 playlist_ViewUpdate( p_playlist, i_new_view );
1276 vlc_object_release( p_playlist );
1279 else if( i_new_view >= VIEW_FIRST_SORTED && i_new_view <= VIEW_LAST_SORTED )
1281 playlist_ViewInsert( p_playlist, i_new_view, "View" );
1282 playlist_ViewUpdate( p_playlist, i_new_view );
1284 i_current_view = i_new_view;
1289 vlc_object_release( p_playlist );
1292 wxMenu * Playlist::ViewMenu()
1294 playlist_t *p_playlist =
1295 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1297 if( p_playlist == NULL )
1304 p_view_menu = new wxMenu;
1308 wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1311 wxMenuItem *item = node->GetData();
1312 node = node->GetNext();
1313 p_view_menu->Delete( item );
1317 /* FIXME : have a list of "should have" views */
1318 p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1319 wxU(_("By category") ) );
1320 p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1321 wxU(_("Manually added") ) );
1322 p_view_menu->Append( FirstView_Event + VIEW_ALL,
1323 wxU(_("All items, unsorted") ) );
1324 p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1325 wxU(_("Sorted by author") ) );
1327 for( int i = 0; i< p_playlist->i_views; i++ )
1329 p_view_menu->Append( FirstView_Event + p_playlist->pp_views[i]->i_id,
1330 wxU( p_playlist->pp_views[i]->psz_name ) );
1334 vlc_object_release( p_playlist);
1340 /*****************************************************************************
1341 * Popup management functions
1342 *****************************************************************************/
1343 void Playlist::OnPopup( wxContextMenuEvent& event )
1345 wxPoint pt = event.GetPosition();
1347 i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1348 if( i_popup_item.IsOk() )
1350 PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1352 PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1353 treectrl->GetItemParent( i_popup_item ) );
1354 p_popup_item = p_wxitem->p_item;
1355 p_popup_parent = p_wxparent->p_item;
1356 treectrl->SelectItem( i_popup_item );
1357 Playlist::PopupMenu( popup_menu,
1358 ScreenToClient( wxGetMousePosition() ) );
1365 void Playlist::OnPopupPlay( wxMenuEvent& event )
1367 playlist_t *p_playlist =
1368 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1370 if( p_playlist == NULL )
1374 if( p_popup_item != NULL )
1376 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1377 VIEW_SIMPLE, p_popup_parent, p_popup_item );
1380 vlc_object_release( p_playlist );
1383 void Playlist::OnPopupDel( wxMenuEvent& event )
1385 PlaylistItem *p_wxitem;
1387 p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1389 if( p_wxitem->p_item->i_children == -1 )
1391 DeleteItem( p_wxitem->p_item->input.i_id );
1395 //DeleteNode( p_wxitem->p_item );
1399 void Playlist::OnPopupEna( wxMenuEvent& event )
1401 playlist_t *p_playlist =
1402 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1404 if( p_playlist == NULL )
1409 p_popup_item->b_enabled = VLC_TRUE - p_popup_item->b_enabled;
1411 vlc_object_release( p_playlist);
1412 UpdateItem( i_popup_item );
1415 void Playlist::OnPopupInfo( wxMenuEvent& event )
1419 iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1420 if( iteminfo_dialog->ShowModal() == wxID_OK )
1422 UpdateItem( i_popup_item );
1424 delete iteminfo_dialog;
1429 /*****************************************************************************
1430 * Custom events management
1431 *****************************************************************************/
1432 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1434 switch( event.GetId() )
1436 case UpdateItem_Event:
1437 UpdateItem( event.GetInt() );
1442 /*****************************************************************************
1443 * PlaylistChanged: callback triggered by the intf-change playlist variable
1444 * We don't rebuild the playlist directly here because we don't want the
1445 * caller to block for a too long time.
1446 *****************************************************************************/
1447 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1448 vlc_value_t oval, vlc_value_t nval, void *param )
1450 Playlist *p_playlist_dialog = (Playlist *)param;
1451 p_playlist_dialog->b_need_update = VLC_TRUE;
1455 /*****************************************************************************
1456 * Next: callback triggered by the playlist-current playlist variable
1457 *****************************************************************************/
1458 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1459 vlc_value_t oval, vlc_value_t nval, void *param )
1461 Playlist *p_playlist_dialog = (Playlist *)param;
1463 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1464 event.SetInt( oval.i_int );
1465 p_playlist_dialog->AddPendingEvent( event );
1466 event.SetInt( nval.i_int );
1467 p_playlist_dialog->AddPendingEvent( event );
1472 /*****************************************************************************
1473 * ItemChanged: callback triggered by the item-change playlist variable
1474 *****************************************************************************/
1475 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1476 vlc_value_t old_val, vlc_value_t new_val, void *param )
1478 Playlist *p_playlist_dialog = (Playlist *)param;
1480 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1481 event.SetInt( new_val.i_int );
1482 p_playlist_dialog->AddPendingEvent( event );