1 /*****************************************************************************
2 * playlist.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: playlist.cpp,v 1.20 2003/09/21 18:07:51 gbazin Exp $
7 * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <errno.h> /* ENOMEM */
29 #include <string.h> /* strerror() */
35 #include "wxwindows.h"
36 #include <wx/listctrl.h>
38 /* Callback prototype */
39 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
40 vlc_value_t old_val, vlc_value_t new_val, void *param );
42 /*****************************************************************************
44 *****************************************************************************/
46 /* IDs for the controls and the menu commands */
58 InvertSelection_Event,
59 DeleteSelection_Event,
71 BEGIN_EVENT_TABLE(Playlist, wxFrame)
73 EVT_MENU(AddFile_Event, Playlist::OnAddFile)
74 EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
75 EVT_MENU(Sort_Event, Playlist::OnSort)
76 EVT_MENU(RSort_Event, Playlist::OnRSort)
77 EVT_MENU(Close_Event, Playlist::OnClose)
78 EVT_MENU(Open_Event, Playlist::OnOpen)
79 EVT_MENU(Save_Event, Playlist::OnSave)
80 EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
81 EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
82 EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
83 EVT_CHECKBOX(Random_Event, Playlist::OnRandom)
84 EVT_CHECKBOX(Loop_Event, Playlist::OnLoop)
87 EVT_LIST_ITEM_ACTIVATED(ListView_Event, Playlist::OnActivateItem)
88 EVT_LIST_KEY_DOWN(ListView_Event, Playlist::OnKeyDown)
91 EVT_BUTTON( Search_Event, Playlist::OnSearch)
92 EVT_BUTTON( Save_Event, Playlist::OnSave)
94 EVT_TEXT(SearchText_Event, Playlist::OnSearchTextChange)
96 /* Special events : we don't want to destroy the window when the user
98 EVT_CLOSE(Playlist::OnClose)
101 /*****************************************************************************
103 *****************************************************************************/
104 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
105 wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
106 wxDefaultSize, wxDEFAULT_FRAME_STYLE )
108 /* Initializations */
110 i_update_counter = 0;
111 b_need_update = VLC_FALSE;
112 vlc_mutex_init( p_intf, &lock );
113 SetIcon( *p_intf->p_sys->p_icon );
115 /* Create our "Manage" menu */
116 wxMenu *manage_menu = new wxMenu;
117 manage_menu->Append( AddFile_Event, wxU(_("&Simple Add...")) );
118 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
119 manage_menu->Append( Sort_Event, wxU(_("&Sort")) );
120 manage_menu->Append( RSort_Event, wxU(_("&Reverse Sort")) );
121 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
122 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
123 manage_menu->AppendSeparator();
124 manage_menu->Append( Close_Event, wxU(_("&Close")) );
126 /* Create our "Selection" menu */
127 wxMenu *selection_menu = new wxMenu;
128 selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
129 selection_menu->Append( DeleteSelection_Event, wxU(_("&Delete")) );
130 selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
132 /* Append the freshly created menus to the menu bar */
133 wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
134 menubar->Append( manage_menu, wxU(_("&Manage")) );
135 menubar->Append( selection_menu, wxU(_("&Selection")) );
137 /* Attach the menu bar to the frame */
138 SetMenuBar( menubar );
140 /* Create a panel to put everything in */
141 wxPanel *playlist_panel = new wxPanel( this, -1 );
142 playlist_panel->SetAutoLayout( TRUE );
144 /* Create the listview */
145 /* FIXME: the given size is arbitrary, and prevents us from resizing
146 * the window to smaller dimensions. But the sizers don't seem to adjust
147 * themselves to the size of a listview, and with a wxDefaultSize the
148 * playlist window is ridiculously small */
149 listview = new wxListView( playlist_panel, ListView_Event,
150 wxDefaultPosition, wxSize( 355, 300 ),
151 wxLC_REPORT | wxSUNKEN_BORDER );
152 listview->InsertColumn( 0, wxU(_("Url")) );
153 listview->InsertColumn( 1, wxU(_("Duration")) );
154 listview->SetColumnWidth( 0, 250 );
155 listview->SetColumnWidth( 1, 100 );
157 /* Create the Random checkbox */
158 wxCheckBox *random_checkbox =
159 new wxCheckBox( playlist_panel, Random_Event, wxU(_("Random")) );
160 int b_random = config_GetInt( p_intf, "random") ;
161 random_checkbox->SetValue( b_random );
163 /* Create the Loop Checkbox */
164 wxCheckBox *loop_checkbox =
165 new wxCheckBox( playlist_panel, Loop_Event, wxU(_("Loop")) );
166 int b_loop = config_GetInt( p_intf, "loop") ;
167 loop_checkbox->SetValue( b_loop );
169 /* Create the Search Textbox */
171 new wxTextCtrl( playlist_panel, SearchText_Event, wxT(""),
172 wxDefaultPosition, wxSize( 100, -1),
175 /* Create the search button */
177 new wxButton( playlist_panel, Search_Event, wxU(_("Search")) );
180 /* Place everything in sizers */
181 wxBoxSizer *search_sizer = new wxBoxSizer( wxHORIZONTAL );
182 search_sizer->Add( search_text, 0, wxEXPAND|wxALL, 5);
183 search_sizer->Add( search_button, 0, wxEXPAND|wxALL, 5);
184 search_sizer->Add( random_checkbox, 0,
185 wxEXPAND|wxALIGN_RIGHT|wxALL, 5);
186 search_sizer->Add( loop_checkbox, 0,
187 wxEXPAND|wxALIGN_RIGHT|wxALL, 5);
188 search_sizer->Layout();
190 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
192 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
193 panel_sizer->Add( listview, 1, wxEXPAND | wxALL, 5 );
194 panel_sizer->Add( search_sizer, 0 , wxALIGN_CENTRE);
195 panel_sizer->Layout();
197 playlist_panel->SetSizerAndFit( panel_sizer );
198 main_sizer->Add( playlist_panel, 1, wxGROW, 0 );
199 main_sizer->Layout();
200 SetSizerAndFit( main_sizer );
202 #if !defined(__WXX11__)
203 /* Associate drop targets with the playlist */
204 SetDropTarget( new DragAndDrop( p_intf ) );
207 playlist_t *p_playlist =
208 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
210 if( p_playlist == NULL )
215 /* We want to be noticed of playlit changes */
216 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
217 vlc_object_release( p_playlist );
219 /* Update the playlist */
223 Playlist::~Playlist()
225 playlist_t *p_playlist =
226 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
228 if( p_playlist == NULL )
233 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
234 vlc_object_release( p_playlist );
237 void Playlist::Rebuild()
239 playlist_t *p_playlist =
240 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
242 if( p_playlist == NULL )
247 /* Clear the list... */
248 listview->DeleteAllItems();
250 /* ...and rebuild it */
251 vlc_mutex_lock( &p_playlist->object_lock );
252 for( int i = 0; i < p_playlist->i_size; i++ )
254 wxString filename = wxU(p_playlist->pp_items[i]->psz_name);
255 listview->InsertItem( i, filename );
256 /* FIXME: we should try to find the actual duration... */
257 listview->SetItem( i, 1, wxU(_("no info")) );
259 vlc_mutex_unlock( &p_playlist->object_lock );
261 /* Change the colour for the currenty played stream */
263 listitem.m_itemId = p_playlist->i_index;
264 listitem.SetTextColour( *wxRED );
265 listview->SetItem( listitem );
267 vlc_object_release( p_playlist );
270 void Playlist::ShowPlaylist( bool show )
272 if( show ) Rebuild();
276 void Playlist::UpdatePlaylist()
278 vlc_bool_t b_need_update = VLC_FALSE;
281 /* If the playlist isn't show there's no need to update it */
282 if( !IsShown() ) return;
284 vlc_mutex_lock( &lock );
285 if( this->b_need_update )
287 b_need_update = VLC_TRUE;
288 this->b_need_update = VLC_FALSE;
290 vlc_mutex_unlock( &lock );
292 if( b_need_update ) Rebuild();
294 /* Updating the playing status every 0.5s is enough */
295 if( i_update_counter % 5 ) return;
297 playlist_t *p_playlist =
298 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
300 if( p_playlist == NULL )
305 /* Update the colour of items */
306 vlc_mutex_lock( &p_playlist->object_lock );
307 if( p_intf->p_sys->i_playing != p_playlist->i_index )
310 listitem.m_itemId = p_playlist->i_index;
311 listitem.SetTextColour( *wxRED );
312 listview->SetItem( listitem );
314 if( p_intf->p_sys->i_playing != -1 )
316 listitem.m_itemId = p_intf->p_sys->i_playing;
317 listitem.SetTextColour( *wxBLACK );
318 listview->SetItem( listitem );
320 p_intf->p_sys->i_playing = p_playlist->i_index;
322 vlc_mutex_unlock( &p_playlist->object_lock );
324 vlc_object_release( p_playlist );
327 /*****************************************************************************
329 *****************************************************************************/
330 void Playlist::DeleteItem( int item )
332 playlist_t *p_playlist =
333 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
335 if( p_playlist == NULL )
340 playlist_Delete( p_playlist, item );
341 listview->DeleteItem( item );
343 vlc_object_release( p_playlist );
346 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
351 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
353 playlist_t *p_playlist =
354 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
356 if( p_playlist == NULL )
361 wxFileDialog dialog( this, wxU(_("Save playlist")),
362 wxT(""), wxT(""), wxT("*"), wxSAVE );
364 if( dialog.ShowModal() == wxID_OK )
366 playlist_SaveFile( p_playlist, dialog.GetPath().mb_str() );
369 vlc_object_release( p_playlist );
372 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
374 playlist_t *p_playlist =
375 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
377 if( p_playlist == NULL )
382 wxFileDialog dialog( this, wxU(_("Open playlist")),
383 wxT(""), wxT(""), wxT("*"), wxOPEN );
385 if( dialog.ShowModal() == wxID_OK )
387 playlist_LoadFile( p_playlist, dialog.GetPath().mb_str() );
390 vlc_object_release( p_playlist );
393 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
395 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
402 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
404 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
411 void Playlist::OnSort( wxCommandEvent& WXUNUSED(event) )
413 playlist_t *p_playlist =
414 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
416 if( p_playlist == NULL )
421 playlist_Sort( p_playlist , 0 );
423 vlc_object_release( p_playlist );
430 void Playlist::OnRSort( wxCommandEvent& WXUNUSED(event) )
432 playlist_t *p_playlist =
433 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
435 if( p_playlist == NULL )
440 playlist_Sort( p_playlist , 1 );
442 vlc_object_release( p_playlist );
449 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
451 search_button->SetDefault();
454 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
456 wxString search_string= search_text->GetValue();
462 for( i_current = 0 ; i_current <= listview->GetItemCount() ; i_current++ )
464 if( listview->GetItemState( i_current, wxLIST_STATE_SELECTED)
465 == wxLIST_STATE_SELECTED )
472 for ( i_current = i_first + 1; i_current <= listview->GetItemCount()
476 listitem.SetId( i_current );
477 listview->GetItem( listitem );
478 if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
484 for( long item = 0; item < listview->GetItemCount(); item++ )
486 listview->Select( item, FALSE );
490 listitem.SetId(i_item);
491 listitem.m_state = wxLIST_STATE_SELECTED;
492 listview->Select( i_item, TRUE );
493 listview->Focus( i_item );
497 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
499 for( long item = 0; item < listview->GetItemCount(); item++ )
501 listview->Select( item, ! listview->IsSelected( item ) );
505 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
507 /* Delete from the end to the beginning, to avoid a shift of indices */
508 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
510 if( listview->IsSelected( item ) )
519 void Playlist::OnRandom( wxCommandEvent& event )
521 config_PutInt( p_intf , "random" ,
522 event.IsChecked() ? VLC_TRUE : VLC_FALSE );
524 void Playlist::OnLoop ( wxCommandEvent& event )
526 config_PutInt( p_intf, "loop",
527 event.IsChecked() ? VLC_TRUE : VLC_FALSE );
531 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
533 for( long item = 0; item < listview->GetItemCount(); item++ )
535 listview->Select( item, TRUE );
539 void Playlist::OnActivateItem( wxListEvent& event )
541 playlist_t *p_playlist =
542 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
544 if( p_playlist == NULL )
549 playlist_Goto( p_playlist, event.GetIndex() );
551 vlc_object_release( p_playlist );
553 void Playlist::OnKeyDown( wxListEvent& event )
555 long keycode = event.GetKeyCode();
556 /* Delete selected items */
557 if( keycode == WXK_BACK || keycode == WXK_DELETE )
559 /* We send a dummy event */
560 OnDeleteSelection( event );
564 /*****************************************************************************
565 * PlaylistChanged: callback triggered by the intf-change playlist variable
566 * We don't rebuild the playlist directly here because we don't want the
567 * caller to block for a too long time.
568 *****************************************************************************/
569 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
570 vlc_value_t old_val, vlc_value_t new_val, void *param )
572 Playlist *p_playlist_dialog = (Playlist *)param;
573 vlc_mutex_lock( &p_playlist_dialog->lock );
574 p_playlist_dialog->b_need_update = VLC_TRUE;
575 vlc_mutex_unlock( &p_playlist_dialog->lock );