1 /*****************************************************************************
2 * playlist.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: playlist.cpp,v 1.13 2003/07/17 17:30:40 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, wxWindow *p_parent ):
104 wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
105 wxDefaultSize, wxDEFAULT_FRAME_STYLE )
107 /* Initializations */
109 i_update_counter = 0;
110 b_need_update = VLC_FALSE;
111 vlc_mutex_init( p_intf, &lock );
112 SetIcon( *p_intf->p_sys->p_icon );
114 /* Create our "Manage" menu */
115 wxMenu *manage_menu = new wxMenu;
116 manage_menu->Append( AddFile_Event, wxU(_("&Simple Add...")) );
117 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
118 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
119 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
120 manage_menu->AppendSeparator();
121 manage_menu->Append( Close_Event, wxU(_("&Close")) );
123 /* Create our "Selection" menu */
124 wxMenu *selection_menu = new wxMenu;
125 selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
126 selection_menu->Append( DeleteSelection_Event, wxU(_("&Delete")) );
127 selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
129 /* Append the freshly created menus to the menu bar */
130 wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
131 menubar->Append( manage_menu, wxU(_("&Manage")) );
132 menubar->Append( selection_menu, wxU(_("&Selection")) );
134 /* Attach the menu bar to the frame */
135 SetMenuBar( menubar );
137 /* Create a panel to put everything in */
138 wxPanel *playlist_panel = new wxPanel( this, -1 );
139 playlist_panel->SetAutoLayout( TRUE );
141 /* Create the listview */
142 /* FIXME: the given size is arbitrary, and prevents us from resizing
143 * the window to smaller dimensions. But the sizers don't seem to adjust
144 * themselves to the size of a listview, and with a wxDefaultSize the
145 * playlist window is ridiculously small */
146 listview = new wxListView( playlist_panel, ListView_Event,
147 wxDefaultPosition, wxSize( 355, 300 ),
148 wxLC_REPORT | wxSUNKEN_BORDER );
149 listview->InsertColumn( 0, wxU(_("Url")) );
150 listview->InsertColumn( 1, wxU(_("Duration")) );
151 listview->SetColumnWidth( 0, 250 );
152 listview->SetColumnWidth( 1, 100 );
154 /* Create the Close button */
155 wxButton *close_button = new wxButton( playlist_panel, Close_Event,
157 close_button->SetDefault();
159 /* Place everything in sizers */
160 wxBoxSizer *close_button_sizer = new wxBoxSizer( wxHORIZONTAL );
161 close_button_sizer->Add( close_button, 0, wxALL, 5 );
162 close_button_sizer->Layout();
163 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
164 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
165 panel_sizer->Add( listview, 1, wxEXPAND | wxALL, 5 );
166 panel_sizer->Add( close_button_sizer, 0, wxALIGN_CENTRE );
167 panel_sizer->Layout();
168 playlist_panel->SetSizerAndFit( panel_sizer );
169 main_sizer->Add( playlist_panel, 1, wxGROW, 0 );
170 main_sizer->Layout();
171 SetSizerAndFit( main_sizer );
173 #if !defined(__WXX11__)
174 /* Associate drop targets with the playlist */
175 SetDropTarget( new DragAndDrop( p_intf ) );
178 playlist_t *p_playlist =
179 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
181 if( p_playlist == NULL )
186 /* We want to be noticed of playlit changes */
187 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
188 vlc_object_release( p_playlist );
190 /* Update the playlist */
194 Playlist::~Playlist()
196 playlist_t *p_playlist =
197 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
199 if( p_playlist == NULL )
204 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
205 vlc_object_release( p_playlist );
208 void Playlist::Rebuild()
210 playlist_t *p_playlist =
211 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
213 if( p_playlist == NULL )
218 /* Clear the list... */
219 listview->DeleteAllItems();
221 /* ...and rebuild it */
222 vlc_mutex_lock( &p_playlist->object_lock );
223 for( int i = 0; i < p_playlist->i_size; i++ )
225 wxString filename = wxU(p_playlist->pp_items[i]->psz_name);
226 listview->InsertItem( i, filename );
227 /* FIXME: we should try to find the actual duration... */
228 listview->SetItem( i, 1, wxU(_("no info")) );
230 vlc_mutex_unlock( &p_playlist->object_lock );
232 /* Change the colour for the currenty played stream */
234 listitem.m_itemId = p_playlist->i_index;
235 listitem.SetTextColour( *wxRED );
236 listview->SetItem( listitem );
238 vlc_object_release( p_playlist );
241 void Playlist::ShowPlaylist( bool show )
243 if( show ) Rebuild();
247 void Playlist::UpdatePlaylist()
249 vlc_bool_t b_need_update = VLC_FALSE;
252 /* If the playlist isn't show there's no need to update it */
253 if( !IsShown() ) return;
255 vlc_mutex_lock( &lock );
256 if( this->b_need_update )
258 b_need_update = VLC_TRUE;
259 this->b_need_update = VLC_FALSE;
261 vlc_mutex_unlock( &lock );
263 if( b_need_update ) Rebuild();
265 /* Updating the playing status every 0.5s is enough */
266 if( i_update_counter % 5 ) return;
268 playlist_t *p_playlist =
269 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
271 if( p_playlist == NULL )
276 /* Update the colour of items */
277 vlc_mutex_lock( &p_playlist->object_lock );
278 if( p_intf->p_sys->i_playing != p_playlist->i_index )
281 listitem.m_itemId = p_playlist->i_index;
282 listitem.SetTextColour( *wxRED );
283 listview->SetItem( listitem );
285 if( p_intf->p_sys->i_playing != -1 )
287 listitem.m_itemId = p_intf->p_sys->i_playing;
288 listitem.SetTextColour( *wxBLACK );
289 listview->SetItem( listitem );
291 p_intf->p_sys->i_playing = p_playlist->i_index;
293 vlc_mutex_unlock( &p_playlist->object_lock );
295 vlc_object_release( p_playlist );
298 /*****************************************************************************
300 *****************************************************************************/
301 void Playlist::DeleteItem( int item )
303 playlist_t *p_playlist =
304 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
306 if( p_playlist == NULL )
311 playlist_Delete( p_playlist, item );
312 listview->DeleteItem( item );
314 vlc_object_release( p_playlist );
317 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
322 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
324 playlist_t *p_playlist =
325 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
327 if( p_playlist == NULL )
332 wxFileDialog dialog( this, wxU(_("Save playlist")),
333 wxT(""), wxT(""), wxT("*"), wxSAVE );
335 if( dialog.ShowModal() == wxID_OK )
337 playlist_SaveFile( p_playlist, dialog.GetPath().mb_str() );
340 vlc_object_release( p_playlist );
343 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
345 playlist_t *p_playlist =
346 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
348 if( p_playlist == NULL )
353 wxFileDialog dialog( this, wxU(_("Open playlist")),
354 wxT(""), wxT(""), wxT("*"), wxOPEN );
356 if( dialog.ShowModal() == wxID_OK )
358 playlist_LoadFile( p_playlist, dialog.GetPath().mb_str() );
361 vlc_object_release( p_playlist );
364 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
366 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0 );
373 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
375 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0 );
382 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
384 for( long item = 0; item < listview->GetItemCount(); item++ )
386 listview->Select( item, ! listview->IsSelected( item ) );
390 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
392 /* Delete from the end to the beginning, to avoid a shift of indices */
393 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
395 if( listview->IsSelected( item ) )
404 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
406 for( long item = 0; item < listview->GetItemCount(); item++ )
408 listview->Select( item, TRUE );
412 void Playlist::OnActivateItem( wxListEvent& event )
414 playlist_t *p_playlist =
415 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
417 if( p_playlist == NULL )
422 playlist_Goto( p_playlist, event.GetIndex() );
424 vlc_object_release( p_playlist );
427 void Playlist::OnKeyDown( wxListEvent& event )
429 long keycode = event.GetKeyCode();
430 /* Delete selected items */
431 if( keycode == WXK_BACK || keycode == WXK_DELETE )
433 /* We send a dummy event */
434 OnDeleteSelection( event );
438 /*****************************************************************************
439 * PlaylistChanged: callback triggered by the intf-change playlist variable
440 * We don't rebuild the playlist directly here because we don't want the
441 * caller to block for a too long time.
442 *****************************************************************************/
443 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
444 vlc_value_t old_val, vlc_value_t new_val, void *param )
446 Playlist *p_playlist_dialog = (Playlist *)param;
447 vlc_mutex_lock( &p_playlist_dialog->lock );
448 p_playlist_dialog->b_need_update = VLC_TRUE;
449 vlc_mutex_unlock( &p_playlist_dialog->lock );