1 /*****************************************************************************
2 * playlist.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: playlist.cpp,v 1.11 2003/05/20 23:17:59 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 */
67 InvertSelection_Event,
68 DeleteSelection_Event,
75 BEGIN_EVENT_TABLE(Playlist, wxFrame)
77 EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
78 EVT_MENU(Close_Event, Playlist::OnClose)
79 EVT_MENU(Open_Event, Playlist::OnOpen)
80 EVT_MENU(Save_Event, Playlist::OnSave)
81 EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
82 EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
83 EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
86 EVT_LIST_ITEM_ACTIVATED(ListView_Event, Playlist::OnActivateItem)
87 EVT_LIST_KEY_DOWN(ListView_Event, Playlist::OnKeyDown)
90 EVT_BUTTON( Close_Event, Playlist::OnClose)
91 EVT_BUTTON( Save_Event, Playlist::OnSave)
93 /* Special events : we don't want to destroy the window when the user
95 EVT_CLOSE(Playlist::OnClose)
98 /*****************************************************************************
100 *****************************************************************************/
101 Playlist::Playlist( intf_thread_t *_p_intf, Interface *_p_main_interface ):
102 wxFrame( _p_main_interface, -1, wxU(_("Playlist")), wxDefaultPosition,
103 wxDefaultSize, wxDEFAULT_FRAME_STYLE )
105 /* Initializations */
107 p_main_interface = _p_main_interface;
108 i_update_counter = 0;
109 b_need_update = VLC_FALSE;
110 vlc_mutex_init( p_intf, &lock );
111 SetIcon( *p_intf->p_sys->p_icon );
113 /* Create our "Manage" menu */
114 wxMenu *manage_menu = new wxMenu;
115 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
116 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
117 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
118 manage_menu->AppendSeparator();
119 manage_menu->Append( Close_Event, wxU(_("&Close")) );
121 /* Create our "Selection" menu */
122 wxMenu *selection_menu = new wxMenu;
123 selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
124 selection_menu->Append( DeleteSelection_Event, wxU(_("&Delete")) );
125 selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
127 /* Append the freshly created menus to the menu bar */
128 wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
129 menubar->Append( manage_menu, wxU(_("&Manage")) );
130 menubar->Append( selection_menu, wxU(_("&Selection")) );
132 /* Attach the menu bar to the frame */
133 SetMenuBar( menubar );
135 /* Create a panel to put everything in */
136 wxPanel *playlist_panel = new wxPanel( this, -1 );
137 playlist_panel->SetAutoLayout( TRUE );
139 /* Create the listview */
140 /* FIXME: the given size is arbitrary, and prevents us from resizing
141 * the window to smaller dimensions. But the sizers don't seem to adjust
142 * themselves to the size of a listview, and with a wxDefaultSize the
143 * playlist window is ridiculously small */
144 listview = new wxListView( playlist_panel, ListView_Event,
145 wxDefaultPosition, wxSize( 355, 300 ),
146 wxLC_REPORT | wxSUNKEN_BORDER );
147 listview->InsertColumn( 0, wxU(_("Url")) );
148 listview->InsertColumn( 1, wxU(_("Duration")) );
149 listview->SetColumnWidth( 0, 250 );
150 listview->SetColumnWidth( 1, 100 );
152 /* Create the Close button */
153 wxButton *close_button = new wxButton( playlist_panel, Close_Event,
155 close_button->SetDefault();
157 /* Place everything in sizers */
158 wxBoxSizer *close_button_sizer = new wxBoxSizer( wxHORIZONTAL );
159 close_button_sizer->Add( close_button, 0, wxALL, 5 );
160 close_button_sizer->Layout();
161 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
162 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
163 panel_sizer->Add( listview, 1, wxEXPAND | wxALL, 5 );
164 panel_sizer->Add( close_button_sizer, 0, wxALIGN_CENTRE );
165 panel_sizer->Layout();
166 playlist_panel->SetSizerAndFit( panel_sizer );
167 main_sizer->Add( playlist_panel, 1, wxGROW, 0 );
168 main_sizer->Layout();
169 SetSizerAndFit( main_sizer );
171 #if !defined(__WXX11__)
172 /* Associate drop targets with the playlist */
173 SetDropTarget( new DragAndDrop( p_intf ) );
176 playlist_t *p_playlist =
177 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
179 if( p_playlist == NULL )
184 /* We want to be noticed of playlit changes */
185 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
186 vlc_object_release( p_playlist );
188 /* Update the playlist */
192 Playlist::~Playlist()
194 playlist_t *p_playlist =
195 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
197 if( p_playlist == NULL )
202 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
203 vlc_object_release( p_playlist );
206 void Playlist::Rebuild()
208 playlist_t *p_playlist =
209 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
211 if( p_playlist == NULL )
216 /* Clear the list... */
217 listview->DeleteAllItems();
219 /* ...and rebuild it */
220 vlc_mutex_lock( &p_playlist->object_lock );
221 for( int i = 0; i < p_playlist->i_size; i++ )
223 wxString filename = wxU(p_playlist->pp_items[i]->psz_name);
224 listview->InsertItem( i, filename );
225 /* FIXME: we should try to find the actual duration... */
226 listview->SetItem( i, 1, wxU(_("no info")) );
228 vlc_mutex_unlock( &p_playlist->object_lock );
230 /* Change the colour for the currenty played stream */
232 listitem.m_itemId = p_playlist->i_index;
233 listitem.SetTextColour( *wxRED );
234 listview->SetItem( listitem );
236 vlc_object_release( p_playlist );
239 void Playlist::ShowPlaylist( bool show )
241 if( show ) Rebuild();
245 void Playlist::UpdatePlaylist()
247 vlc_bool_t b_need_update = VLC_FALSE;
250 /* If the playlist isn't show there's no need to update it */
251 if( !IsShown() ) return;
253 vlc_mutex_lock( &lock );
254 if( this->b_need_update )
256 b_need_update = VLC_TRUE;
257 this->b_need_update = VLC_FALSE;
259 vlc_mutex_unlock( &lock );
261 if( b_need_update ) Rebuild();
263 /* Updating the playing status every 0.5s is enough */
264 if( i_update_counter % 5 ) return;
266 playlist_t *p_playlist =
267 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
269 if( p_playlist == NULL )
274 /* Update the colour of items */
275 vlc_mutex_lock( &p_playlist->object_lock );
276 if( p_intf->p_sys->i_playing != p_playlist->i_index )
279 listitem.m_itemId = p_playlist->i_index;
280 listitem.SetTextColour( *wxRED );
281 listview->SetItem( listitem );
283 if( p_intf->p_sys->i_playing != -1 )
285 listitem.m_itemId = p_intf->p_sys->i_playing;
286 listitem.SetTextColour( *wxBLACK );
287 listview->SetItem( listitem );
289 p_intf->p_sys->i_playing = p_playlist->i_index;
291 vlc_mutex_unlock( &p_playlist->object_lock );
293 vlc_object_release( p_playlist );
296 /*****************************************************************************
298 *****************************************************************************/
299 void Playlist::DeleteItem( int item )
301 playlist_t *p_playlist =
302 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
304 if( p_playlist == NULL )
309 playlist_Delete( p_playlist, item );
310 listview->DeleteItem( item );
312 vlc_object_release( p_playlist );
315 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
320 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
322 playlist_t *p_playlist =
323 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
325 if( p_playlist == NULL )
330 wxFileDialog dialog( this, wxU(_("Save playlist")),
331 wxT(""), wxT(""), wxT("*"), wxSAVE );
333 if( dialog.ShowModal() == wxID_OK )
335 playlist_SaveFile( p_playlist, dialog.GetPath().mb_str() );
338 vlc_object_release( p_playlist );
341 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
343 playlist_t *p_playlist =
344 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
346 if( p_playlist == NULL )
351 wxFileDialog dialog( this, wxU(_("Open playlist")),
352 wxT(""), wxT(""), wxT("*"), wxOPEN );
354 if( dialog.ShowModal() == wxID_OK )
356 playlist_LoadFile( p_playlist, dialog.GetPath().mb_str() );
359 vlc_object_release( p_playlist );
362 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
364 playlist_t *p_playlist =
365 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
367 if( p_playlist == NULL )
372 /* Show/hide the open dialog */
373 if( p_main_interface->p_open_dialog == NULL )
374 p_main_interface->p_open_dialog =
375 new OpenDialog( p_intf, this, FILE_ACCESS );
377 if( p_main_interface->p_open_dialog &&
378 p_main_interface->p_open_dialog->ShowModal() == wxID_OK )
381 i < p_main_interface->p_open_dialog->mrl.GetCount(); i++ )
383 playlist_Add( p_playlist,
384 (const char *)p_main_interface->p_open_dialog->mrl[i].mb_str(),
385 PLAYLIST_APPEND, PLAYLIST_END );
389 vlc_object_release( p_playlist );
394 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
396 for( long item = 0; item < listview->GetItemCount(); item++ )
398 listview->Select( item, ! listview->IsSelected( item ) );
402 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
404 /* Delete from the end to the beginning, to avoid a shift of indices */
405 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
407 if( listview->IsSelected( item ) )
416 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
418 for( long item = 0; item < listview->GetItemCount(); item++ )
420 listview->Select( item, TRUE );
424 void Playlist::OnActivateItem( wxListEvent& event )
426 playlist_t *p_playlist =
427 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
429 if( p_playlist == NULL )
434 playlist_Goto( p_playlist, event.GetIndex() );
436 vlc_object_release( p_playlist );
439 void Playlist::OnKeyDown( wxListEvent& event )
441 long keycode = event.GetKeyCode();
442 /* Delete selected items */
443 if( keycode == WXK_BACK || keycode == WXK_DELETE )
445 /* We send a dummy event */
446 OnDeleteSelection( event );
450 /*****************************************************************************
451 * PlaylistChanged: callback triggered by the intf-change playlist variable
452 * We don't rebuild the playlist directly here because we don't want the
453 * caller to block for a too long time.
454 *****************************************************************************/
455 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
456 vlc_value_t old_val, vlc_value_t new_val, void *param )
458 Playlist *p_playlist_dialog = (Playlist *)param;
459 vlc_mutex_lock( &p_playlist_dialog->lock );
460 p_playlist_dialog->b_need_update = VLC_TRUE;
461 vlc_mutex_unlock( &p_playlist_dialog->lock );