1 /*****************************************************************************
2 * playlist.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: playlist.cpp,v 1.12 2003/07/12 13:33:10 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() */
34 #ifdef WIN32 /* mingw32 hack */
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
42 #include <wx/wxprec.h>
44 #include <wx/listctrl.h>
48 #include "wxwindows.h"
50 /* Callback prototype */
51 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
52 vlc_value_t old_val, vlc_value_t new_val, void *param );
54 /*****************************************************************************
56 *****************************************************************************/
58 /* IDs for the controls and the menu commands */
68 InvertSelection_Event,
69 DeleteSelection_Event,
76 BEGIN_EVENT_TABLE(Playlist, wxFrame)
78 EVT_MENU(AddFile_Event, Playlist::OnAddFile)
79 EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
80 EVT_MENU(Close_Event, Playlist::OnClose)
81 EVT_MENU(Open_Event, Playlist::OnOpen)
82 EVT_MENU(Save_Event, Playlist::OnSave)
83 EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
84 EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
85 EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
88 EVT_LIST_ITEM_ACTIVATED(ListView_Event, Playlist::OnActivateItem)
89 EVT_LIST_KEY_DOWN(ListView_Event, Playlist::OnKeyDown)
92 EVT_BUTTON( Close_Event, Playlist::OnClose)
93 EVT_BUTTON( Save_Event, Playlist::OnSave)
95 /* Special events : we don't want to destroy the window when the user
97 EVT_CLOSE(Playlist::OnClose)
100 /*****************************************************************************
102 *****************************************************************************/
103 Playlist::Playlist( intf_thread_t *_p_intf, Interface *_p_main_interface ):
104 wxFrame( _p_main_interface, -1, wxU(_("Playlist")), wxDefaultPosition,
105 wxDefaultSize, wxDEFAULT_FRAME_STYLE )
107 /* Initializations */
109 p_main_interface = _p_main_interface;
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( Open_Event, wxU(_("&Open Playlist...")) );
120 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
121 manage_menu->AppendSeparator();
122 manage_menu->Append( Close_Event, wxU(_("&Close")) );
124 /* Create our "Selection" menu */
125 wxMenu *selection_menu = new wxMenu;
126 selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
127 selection_menu->Append( DeleteSelection_Event, wxU(_("&Delete")) );
128 selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
130 /* Append the freshly created menus to the menu bar */
131 wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
132 menubar->Append( manage_menu, wxU(_("&Manage")) );
133 menubar->Append( selection_menu, wxU(_("&Selection")) );
135 /* Attach the menu bar to the frame */
136 SetMenuBar( menubar );
138 /* Create a panel to put everything in */
139 wxPanel *playlist_panel = new wxPanel( this, -1 );
140 playlist_panel->SetAutoLayout( TRUE );
142 /* Create the listview */
143 /* FIXME: the given size is arbitrary, and prevents us from resizing
144 * the window to smaller dimensions. But the sizers don't seem to adjust
145 * themselves to the size of a listview, and with a wxDefaultSize the
146 * playlist window is ridiculously small */
147 listview = new wxListView( playlist_panel, ListView_Event,
148 wxDefaultPosition, wxSize( 355, 300 ),
149 wxLC_REPORT | wxSUNKEN_BORDER );
150 listview->InsertColumn( 0, wxU(_("Url")) );
151 listview->InsertColumn( 1, wxU(_("Duration")) );
152 listview->SetColumnWidth( 0, 250 );
153 listview->SetColumnWidth( 1, 100 );
155 /* Create the Close button */
156 wxButton *close_button = new wxButton( playlist_panel, Close_Event,
158 close_button->SetDefault();
160 /* Place everything in sizers */
161 wxBoxSizer *close_button_sizer = new wxBoxSizer( wxHORIZONTAL );
162 close_button_sizer->Add( close_button, 0, wxALL, 5 );
163 close_button_sizer->Layout();
164 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
165 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
166 panel_sizer->Add( listview, 1, wxEXPAND | wxALL, 5 );
167 panel_sizer->Add( close_button_sizer, 0, wxALIGN_CENTRE );
168 panel_sizer->Layout();
169 playlist_panel->SetSizerAndFit( panel_sizer );
170 main_sizer->Add( playlist_panel, 1, wxGROW, 0 );
171 main_sizer->Layout();
172 SetSizerAndFit( main_sizer );
174 #if !defined(__WXX11__)
175 /* Associate drop targets with the playlist */
176 SetDropTarget( new DragAndDrop( p_intf ) );
179 playlist_t *p_playlist =
180 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
182 if( p_playlist == NULL )
187 /* We want to be noticed of playlit changes */
188 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
189 vlc_object_release( p_playlist );
191 /* Update the playlist */
195 Playlist::~Playlist()
197 playlist_t *p_playlist =
198 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
200 if( p_playlist == NULL )
205 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
206 vlc_object_release( p_playlist );
209 void Playlist::Rebuild()
211 playlist_t *p_playlist =
212 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
214 if( p_playlist == NULL )
219 /* Clear the list... */
220 listview->DeleteAllItems();
222 /* ...and rebuild it */
223 vlc_mutex_lock( &p_playlist->object_lock );
224 for( int i = 0; i < p_playlist->i_size; i++ )
226 wxString filename = wxU(p_playlist->pp_items[i]->psz_name);
227 listview->InsertItem( i, filename );
228 /* FIXME: we should try to find the actual duration... */
229 listview->SetItem( i, 1, wxU(_("no info")) );
231 vlc_mutex_unlock( &p_playlist->object_lock );
233 /* Change the colour for the currenty played stream */
235 listitem.m_itemId = p_playlist->i_index;
236 listitem.SetTextColour( *wxRED );
237 listview->SetItem( listitem );
239 vlc_object_release( p_playlist );
242 void Playlist::ShowPlaylist( bool show )
244 if( show ) Rebuild();
248 void Playlist::UpdatePlaylist()
250 vlc_bool_t b_need_update = VLC_FALSE;
253 /* If the playlist isn't show there's no need to update it */
254 if( !IsShown() ) return;
256 vlc_mutex_lock( &lock );
257 if( this->b_need_update )
259 b_need_update = VLC_TRUE;
260 this->b_need_update = VLC_FALSE;
262 vlc_mutex_unlock( &lock );
264 if( b_need_update ) Rebuild();
266 /* Updating the playing status every 0.5s is enough */
267 if( i_update_counter % 5 ) return;
269 playlist_t *p_playlist =
270 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
272 if( p_playlist == NULL )
277 /* Update the colour of items */
278 vlc_mutex_lock( &p_playlist->object_lock );
279 if( p_intf->p_sys->i_playing != p_playlist->i_index )
282 listitem.m_itemId = p_playlist->i_index;
283 listitem.SetTextColour( *wxRED );
284 listview->SetItem( listitem );
286 if( p_intf->p_sys->i_playing != -1 )
288 listitem.m_itemId = p_intf->p_sys->i_playing;
289 listitem.SetTextColour( *wxBLACK );
290 listview->SetItem( listitem );
292 p_intf->p_sys->i_playing = p_playlist->i_index;
294 vlc_mutex_unlock( &p_playlist->object_lock );
296 vlc_object_release( p_playlist );
299 /*****************************************************************************
301 *****************************************************************************/
302 void Playlist::DeleteItem( int item )
304 playlist_t *p_playlist =
305 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
307 if( p_playlist == NULL )
312 playlist_Delete( p_playlist, item );
313 listview->DeleteItem( item );
315 vlc_object_release( p_playlist );
318 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
323 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
325 playlist_t *p_playlist =
326 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
328 if( p_playlist == NULL )
333 wxFileDialog dialog( this, wxU(_("Save playlist")),
334 wxT(""), wxT(""), wxT("*"), wxSAVE );
336 if( dialog.ShowModal() == wxID_OK )
338 playlist_SaveFile( p_playlist, dialog.GetPath().mb_str() );
341 vlc_object_release( p_playlist );
344 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
346 playlist_t *p_playlist =
347 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
349 if( p_playlist == NULL )
354 wxFileDialog dialog( this, wxU(_("Open playlist")),
355 wxT(""), wxT(""), wxT("*"), wxOPEN );
357 if( dialog.ShowModal() == wxID_OK )
359 playlist_LoadFile( p_playlist, dialog.GetPath().mb_str() );
362 vlc_object_release( p_playlist );
365 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
367 playlist_t *p_playlist =
368 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
370 if( p_playlist == NULL )
375 if( p_main_interface->p_file_dialog == NULL )
376 p_main_interface->p_file_dialog =
377 new wxFileDialog( this, wxU(_("Open file")), wxT(""), wxT(""),
378 wxT("*"), wxOPEN | wxMULTIPLE );
380 if( p_main_interface->p_file_dialog &&
381 p_main_interface->p_file_dialog->ShowModal() == wxID_OK )
385 p_main_interface->p_file_dialog->GetPaths( paths );
387 for( size_t i = 0; i < paths.GetCount(); i++ )
388 playlist_Add( p_playlist, (const char *)paths[i].mb_str(),
389 PLAYLIST_APPEND, PLAYLIST_END );
392 vlc_object_release( p_playlist );
397 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
399 playlist_t *p_playlist =
400 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
402 if( p_playlist == NULL )
407 /* Show/hide the open dialog */
408 if( p_main_interface->p_open_dialog == NULL )
409 p_main_interface->p_open_dialog =
410 new OpenDialog( p_intf, this, FILE_ACCESS );
412 if( p_main_interface->p_open_dialog &&
413 p_main_interface->p_open_dialog->ShowModal() == wxID_OK )
416 i < p_main_interface->p_open_dialog->mrl.GetCount(); i++ )
418 playlist_Add( p_playlist,
419 (const char *)p_main_interface->p_open_dialog->mrl[i].mb_str(),
420 PLAYLIST_APPEND, PLAYLIST_END );
424 vlc_object_release( p_playlist );
429 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
431 for( long item = 0; item < listview->GetItemCount(); item++ )
433 listview->Select( item, ! listview->IsSelected( item ) );
437 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
439 /* Delete from the end to the beginning, to avoid a shift of indices */
440 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
442 if( listview->IsSelected( item ) )
451 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
453 for( long item = 0; item < listview->GetItemCount(); item++ )
455 listview->Select( item, TRUE );
459 void Playlist::OnActivateItem( wxListEvent& event )
461 playlist_t *p_playlist =
462 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
464 if( p_playlist == NULL )
469 playlist_Goto( p_playlist, event.GetIndex() );
471 vlc_object_release( p_playlist );
474 void Playlist::OnKeyDown( wxListEvent& event )
476 long keycode = event.GetKeyCode();
477 /* Delete selected items */
478 if( keycode == WXK_BACK || keycode == WXK_DELETE )
480 /* We send a dummy event */
481 OnDeleteSelection( event );
485 /*****************************************************************************
486 * PlaylistChanged: callback triggered by the intf-change playlist variable
487 * We don't rebuild the playlist directly here because we don't want the
488 * caller to block for a too long time.
489 *****************************************************************************/
490 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
491 vlc_value_t old_val, vlc_value_t new_val, void *param )
493 Playlist *p_playlist_dialog = (Playlist *)param;
494 vlc_mutex_lock( &p_playlist_dialog->lock );
495 p_playlist_dialog->b_need_update = VLC_TRUE;
496 vlc_mutex_unlock( &p_playlist_dialog->lock );