1 /*****************************************************************************
2 * playlist.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: playlist.cpp,v 1.16 2003/08/27 11:53:26 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 */
56 InvertSelection_Event,
57 DeleteSelection_Event,
66 BEGIN_EVENT_TABLE(Playlist, wxFrame)
68 EVT_MENU(AddFile_Event, Playlist::OnAddFile)
69 EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
70 EVT_MENU(Close_Event, Playlist::OnClose)
71 EVT_MENU(Open_Event, Playlist::OnOpen)
72 EVT_MENU(Save_Event, Playlist::OnSave)
73 EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
74 EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
75 EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
76 EVT_CHECKBOX(Random_Event, Playlist::OnRandom)
77 EVT_CHECKBOX(Loop_Event, Playlist::OnLoop)
80 EVT_LIST_ITEM_ACTIVATED(ListView_Event, Playlist::OnActivateItem)
81 EVT_LIST_KEY_DOWN(ListView_Event, Playlist::OnKeyDown)
84 EVT_BUTTON( Close_Event, Playlist::OnClose)
85 EVT_BUTTON( Save_Event, Playlist::OnSave)
87 /* Special events : we don't want to destroy the window when the user
89 EVT_CLOSE(Playlist::OnClose)
92 /*****************************************************************************
94 *****************************************************************************/
95 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
96 wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
97 wxDefaultSize, wxDEFAULT_FRAME_STYLE )
101 i_update_counter = 0;
102 b_need_update = VLC_FALSE;
103 vlc_mutex_init( p_intf, &lock );
104 SetIcon( *p_intf->p_sys->p_icon );
106 /* Create our "Manage" menu */
107 wxMenu *manage_menu = new wxMenu;
108 manage_menu->Append( AddFile_Event, wxU(_("&Simple Add...")) );
109 manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
110 manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
111 manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
112 manage_menu->AppendSeparator();
113 manage_menu->Append( Close_Event, wxU(_("&Close")) );
115 /* Create our "Selection" menu */
116 wxMenu *selection_menu = new wxMenu;
117 selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
118 selection_menu->Append( DeleteSelection_Event, wxU(_("&Delete")) );
119 selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
121 /* Append the freshly created menus to the menu bar */
122 wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
123 menubar->Append( manage_menu, wxU(_("&Manage")) );
124 menubar->Append( selection_menu, wxU(_("&Selection")) );
126 /* Attach the menu bar to the frame */
127 SetMenuBar( menubar );
129 /* Create a panel to put everything in */
130 wxPanel *playlist_panel = new wxPanel( this, -1 );
131 playlist_panel->SetAutoLayout( TRUE );
133 /* Create the listview */
134 /* FIXME: the given size is arbitrary, and prevents us from resizing
135 * the window to smaller dimensions. But the sizers don't seem to adjust
136 * themselves to the size of a listview, and with a wxDefaultSize the
137 * playlist window is ridiculously small */
138 listview = new wxListView( playlist_panel, ListView_Event,
139 wxDefaultPosition, wxSize( 355, 300 ),
140 wxLC_REPORT | wxSUNKEN_BORDER );
141 listview->InsertColumn( 0, wxU(_("Url")) );
142 listview->InsertColumn( 1, wxU(_("Duration")) );
143 listview->SetColumnWidth( 0, 250 );
144 listview->SetColumnWidth( 1, 100 );
146 /* Create the Close button */
147 wxButton *close_button = new wxButton( playlist_panel, Close_Event,
149 close_button->SetDefault();
151 /* Create the Random checkbox */
152 wxCheckBox *random_checkbox =
153 new wxCheckBox( playlist_panel, Random_Event, wxU(_("Random")) );
154 int b_random = config_GetInt( p_intf, "random") ;
155 random_checkbox->SetValue( b_random );
157 /* Create the Loop Checkbox */
158 wxCheckBox *loop_checkbox =
159 new wxCheckBox( playlist_panel, Loop_Event, wxU(_("Loop")) );
160 int b_loop = config_GetInt( p_intf, "loop") ;
161 loop_checkbox->SetValue( b_loop );
163 /* Place everything in sizers */
164 wxBoxSizer *close_button_sizer = new wxBoxSizer( wxHORIZONTAL );
165 close_button_sizer->Add( close_button, 0, wxALL, 5 );
166 close_button_sizer->Add( random_checkbox, 0,
167 wxEXPAND|wxALIGN_RIGHT|wxALL, 5);
168 close_button_sizer->Add( loop_checkbox, 0,
169 wxEXPAND|wxALIGN_RIGHT|wxALL, 5);
170 close_button_sizer->Layout();
171 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
172 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
173 panel_sizer->Add( listview, 1, wxEXPAND | wxALL, 5 );
174 panel_sizer->Add( close_button_sizer, 0, wxALIGN_CENTRE );
175 panel_sizer->Layout();
176 playlist_panel->SetSizerAndFit( panel_sizer );
177 main_sizer->Add( playlist_panel, 1, wxGROW, 0 );
178 main_sizer->Layout();
179 SetSizerAndFit( main_sizer );
181 #if !defined(__WXX11__)
182 /* Associate drop targets with the playlist */
183 SetDropTarget( new DragAndDrop( p_intf ) );
186 playlist_t *p_playlist =
187 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
189 if( p_playlist == NULL )
194 /* We want to be noticed of playlit changes */
195 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
196 vlc_object_release( p_playlist );
198 /* Update the playlist */
202 Playlist::~Playlist()
204 playlist_t *p_playlist =
205 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
207 if( p_playlist == NULL )
212 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
213 vlc_object_release( p_playlist );
216 void Playlist::Rebuild()
218 playlist_t *p_playlist =
219 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
221 if( p_playlist == NULL )
226 /* Clear the list... */
227 listview->DeleteAllItems();
229 /* ...and rebuild it */
230 vlc_mutex_lock( &p_playlist->object_lock );
231 for( int i = 0; i < p_playlist->i_size; i++ )
233 wxString filename = wxU(p_playlist->pp_items[i]->psz_name);
234 listview->InsertItem( i, filename );
235 /* FIXME: we should try to find the actual duration... */
236 listview->SetItem( i, 1, wxU(_("no info")) );
238 vlc_mutex_unlock( &p_playlist->object_lock );
240 /* Change the colour for the currenty played stream */
242 listitem.m_itemId = p_playlist->i_index;
243 listitem.SetTextColour( *wxRED );
244 listview->SetItem( listitem );
246 vlc_object_release( p_playlist );
249 void Playlist::ShowPlaylist( bool show )
251 if( show ) Rebuild();
255 void Playlist::UpdatePlaylist()
257 vlc_bool_t b_need_update = VLC_FALSE;
260 /* If the playlist isn't show there's no need to update it */
261 if( !IsShown() ) return;
263 vlc_mutex_lock( &lock );
264 if( this->b_need_update )
266 b_need_update = VLC_TRUE;
267 this->b_need_update = VLC_FALSE;
269 vlc_mutex_unlock( &lock );
271 if( b_need_update ) Rebuild();
273 /* Updating the playing status every 0.5s is enough */
274 if( i_update_counter % 5 ) return;
276 playlist_t *p_playlist =
277 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
279 if( p_playlist == NULL )
284 /* Update the colour of items */
285 vlc_mutex_lock( &p_playlist->object_lock );
286 if( p_intf->p_sys->i_playing != p_playlist->i_index )
289 listitem.m_itemId = p_playlist->i_index;
290 listitem.SetTextColour( *wxRED );
291 listview->SetItem( listitem );
293 if( p_intf->p_sys->i_playing != -1 )
295 listitem.m_itemId = p_intf->p_sys->i_playing;
296 listitem.SetTextColour( *wxBLACK );
297 listview->SetItem( listitem );
299 p_intf->p_sys->i_playing = p_playlist->i_index;
301 vlc_mutex_unlock( &p_playlist->object_lock );
303 vlc_object_release( p_playlist );
306 /*****************************************************************************
308 *****************************************************************************/
309 void Playlist::DeleteItem( int item )
311 playlist_t *p_playlist =
312 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
314 if( p_playlist == NULL )
319 playlist_Delete( p_playlist, item );
320 listview->DeleteItem( item );
322 vlc_object_release( p_playlist );
325 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
330 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
332 playlist_t *p_playlist =
333 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
335 if( p_playlist == NULL )
340 wxFileDialog dialog( this, wxU(_("Save playlist")),
341 wxT(""), wxT(""), wxT("*"), wxSAVE );
343 if( dialog.ShowModal() == wxID_OK )
345 playlist_SaveFile( p_playlist, dialog.GetPath().mb_str() );
348 vlc_object_release( p_playlist );
351 void Playlist::OnOpen( 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(_("Open playlist")),
362 wxT(""), wxT(""), wxT("*"), wxOPEN );
364 if( dialog.ShowModal() == wxID_OK )
366 playlist_LoadFile( p_playlist, dialog.GetPath().mb_str() );
369 vlc_object_release( p_playlist );
372 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
374 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
381 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
383 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
390 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
392 for( long item = 0; item < listview->GetItemCount(); item++ )
394 listview->Select( item, ! listview->IsSelected( item ) );
398 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
400 /* Delete from the end to the beginning, to avoid a shift of indices */
401 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
403 if( listview->IsSelected( item ) )
412 void Playlist::OnRandom( wxCommandEvent& event )
414 config_PutInt( p_intf , "random" ,
415 event.IsChecked() ? VLC_TRUE : VLC_FALSE );
417 void Playlist::OnLoop ( wxCommandEvent& event )
419 config_PutInt( p_intf, "loop",
420 event.IsChecked() ? VLC_TRUE : VLC_FALSE );
424 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
426 for( long item = 0; item < listview->GetItemCount(); item++ )
428 listview->Select( item, TRUE );
432 void Playlist::OnActivateItem( wxListEvent& event )
434 playlist_t *p_playlist =
435 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
437 if( p_playlist == NULL )
442 playlist_Goto( p_playlist, event.GetIndex() );
444 vlc_object_release( p_playlist );
446 void Playlist::OnKeyDown( wxListEvent& event )
448 long keycode = event.GetKeyCode();
449 /* Delete selected items */
450 if( keycode == WXK_BACK || keycode == WXK_DELETE )
452 /* We send a dummy event */
453 OnDeleteSelection( event );
457 /*****************************************************************************
458 * PlaylistChanged: callback triggered by the intf-change playlist variable
459 * We don't rebuild the playlist directly here because we don't want the
460 * caller to block for a too long time.
461 *****************************************************************************/
462 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
463 vlc_value_t old_val, vlc_value_t new_val, void *param )
465 Playlist *p_playlist_dialog = (Playlist *)param;
466 vlc_mutex_lock( &p_playlist_dialog->lock );
467 p_playlist_dialog->b_need_update = VLC_TRUE;
468 vlc_mutex_unlock( &p_playlist_dialog->lock );