]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/playlist.cpp
0294fa654bcb159b14dce7b2eddbb0ab2c94f8ab
[vlc] / modules / gui / wxwindows / playlist.cpp
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 $
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33
34 #ifdef WIN32                                                 /* mingw32 hack */
35 #undef Yield
36 #undef CreateDialog
37 #endif
38
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
41
42 #include <wx/wxprec.h>
43 #include <wx/wx.h>
44 #include <wx/listctrl.h>
45
46 #include <vlc/intf.h>
47
48 #include "wxwindows.h"
49
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 );
53
54 /*****************************************************************************
55  * Event Table.
56  *****************************************************************************/
57
58 /* IDs for the controls and the menu commands */
59 enum
60 {
61     /* menu items */
62     AddFile_Event = 1,
63     AddMRL_Event,
64     Close_Event,
65     Open_Event,
66     Save_Event,
67
68     InvertSelection_Event,
69     DeleteSelection_Event,
70     SelectAll_Event,
71
72     /* controls */
73     ListView_Event
74 };
75
76 BEGIN_EVENT_TABLE(Playlist, wxFrame)
77     /* Menu events */
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)
86
87     /* Listview events */
88     EVT_LIST_ITEM_ACTIVATED(ListView_Event, Playlist::OnActivateItem)
89     EVT_LIST_KEY_DOWN(ListView_Event, Playlist::OnKeyDown)
90
91     /* Button events */
92     EVT_BUTTON( Close_Event, Playlist::OnClose)
93     EVT_BUTTON( Save_Event, Playlist::OnSave)
94
95     /* Special events : we don't want to destroy the window when the user
96      * clicks on (X) */
97     EVT_CLOSE(Playlist::OnClose)
98 END_EVENT_TABLE()
99
100 /*****************************************************************************
101  * Constructor.
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 )
106 {
107     /* Initializations */
108     p_intf = _p_intf;
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 );
114
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")) );
123
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")) );
129
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")) );
134
135     /* Attach the menu bar to the frame */
136     SetMenuBar( menubar );
137
138     /* Create a panel to put everything in */
139     wxPanel *playlist_panel = new wxPanel( this, -1 );
140     playlist_panel->SetAutoLayout( TRUE );
141
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 );
154
155     /* Create the Close button */
156     wxButton *close_button = new wxButton( playlist_panel, Close_Event,
157                                            wxU(_("Close")) );
158     close_button->SetDefault();
159
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 );
173
174 #if !defined(__WXX11__)
175     /* Associate drop targets with the playlist */
176     SetDropTarget( new DragAndDrop( p_intf ) );
177 #endif
178
179     playlist_t *p_playlist =
180         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
181                                        FIND_ANYWHERE );
182     if( p_playlist == NULL )
183     {
184         return;
185     }
186
187     /* We want to be noticed of playlit changes */
188     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
189     vlc_object_release( p_playlist );
190
191     /* Update the playlist */
192     Rebuild();
193 }
194
195 Playlist::~Playlist()
196 {
197     playlist_t *p_playlist =
198         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
199                                        FIND_ANYWHERE );
200     if( p_playlist == NULL )
201     {
202         return;
203     }
204
205     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
206     vlc_object_release( p_playlist );
207 }
208
209 void Playlist::Rebuild()
210 {
211     playlist_t *p_playlist =
212         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
213                                        FIND_ANYWHERE );
214     if( p_playlist == NULL )
215     {
216         return;
217     }
218
219     /* Clear the list... */
220     listview->DeleteAllItems();
221
222     /* ...and rebuild it */
223     vlc_mutex_lock( &p_playlist->object_lock );
224     for( int i = 0; i < p_playlist->i_size; i++ )
225     {
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")) );
230     }
231     vlc_mutex_unlock( &p_playlist->object_lock );
232
233     /* Change the colour for the currenty played stream */
234     wxListItem listitem;
235     listitem.m_itemId = p_playlist->i_index;
236     listitem.SetTextColour( *wxRED );
237     listview->SetItem( listitem );
238
239     vlc_object_release( p_playlist );
240 }
241
242 void Playlist::ShowPlaylist( bool show )
243 {
244     if( show ) Rebuild();
245     Show( show );
246 }
247
248 void Playlist::UpdatePlaylist()
249 {
250     vlc_bool_t b_need_update = VLC_FALSE;
251     i_update_counter++;
252
253     /* If the playlist isn't show there's no need to update it */
254     if( !IsShown() ) return;
255
256     vlc_mutex_lock( &lock );
257     if( this->b_need_update )
258     {
259         b_need_update = VLC_TRUE;
260         this->b_need_update = VLC_FALSE;
261     }
262     vlc_mutex_unlock( &lock );
263
264     if( b_need_update ) Rebuild();
265
266     /* Updating the playing status every 0.5s is enough */
267     if( i_update_counter % 5 ) return;
268
269     playlist_t *p_playlist =
270         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
271                                        FIND_ANYWHERE );
272     if( p_playlist == NULL )
273     {
274         return;
275     }
276
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 )
280     {
281         wxListItem listitem;
282         listitem.m_itemId = p_playlist->i_index;
283         listitem.SetTextColour( *wxRED );
284         listview->SetItem( listitem );
285
286         if( p_intf->p_sys->i_playing != -1 )
287         {
288             listitem.m_itemId = p_intf->p_sys->i_playing;
289             listitem.SetTextColour( *wxBLACK );
290             listview->SetItem( listitem );
291         }
292         p_intf->p_sys->i_playing = p_playlist->i_index;
293     }
294     vlc_mutex_unlock( &p_playlist->object_lock );
295
296     vlc_object_release( p_playlist );
297 }
298
299 /*****************************************************************************
300  * Private methods.
301  *****************************************************************************/
302 void Playlist::DeleteItem( int item )
303 {
304     playlist_t *p_playlist =
305         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
306                                        FIND_ANYWHERE );
307     if( p_playlist == NULL )
308     {
309         return;
310     }
311
312     playlist_Delete( p_playlist, item );
313     listview->DeleteItem( item );
314
315     vlc_object_release( p_playlist );
316 }
317
318 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
319 {
320     Hide();
321 }
322
323 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
324 {
325     playlist_t *p_playlist =
326         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
327                                        FIND_ANYWHERE );
328     if( p_playlist == NULL )
329     {
330         return;
331     }
332
333     wxFileDialog dialog( this, wxU(_("Save playlist")),
334                          wxT(""), wxT(""), wxT("*"), wxSAVE );
335
336     if( dialog.ShowModal() == wxID_OK )
337     {
338         playlist_SaveFile( p_playlist, dialog.GetPath().mb_str() );
339     }
340
341     vlc_object_release( p_playlist );
342 }
343
344 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
345 {
346     playlist_t *p_playlist =
347         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
348                                        FIND_ANYWHERE );
349     if( p_playlist == NULL )
350     {
351         return;
352     }
353
354     wxFileDialog dialog( this, wxU(_("Open playlist")),
355                          wxT(""), wxT(""), wxT("*"), wxOPEN );
356
357     if( dialog.ShowModal() == wxID_OK )
358     {
359         playlist_LoadFile( p_playlist, dialog.GetPath().mb_str() );
360     }
361
362     vlc_object_release( p_playlist );
363 }
364
365 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
366 {
367     playlist_t *p_playlist =
368         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
369                                        FIND_ANYWHERE );
370     if( p_playlist == NULL )
371     {
372         return;
373     }
374
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 );
379
380     if( p_main_interface->p_file_dialog &&
381         p_main_interface->p_file_dialog->ShowModal() == wxID_OK )
382     {
383         wxArrayString paths;
384
385         p_main_interface->p_file_dialog->GetPaths( paths );
386
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 );
390     }
391
392     vlc_object_release( p_playlist );
393
394     Rebuild();
395 }
396
397 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
398 {
399     playlist_t *p_playlist =
400         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
401                                        FIND_ANYWHERE );
402     if( p_playlist == NULL )
403     {
404         return;
405     }
406
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 );
411
412     if( p_main_interface->p_open_dialog &&
413         p_main_interface->p_open_dialog->ShowModal() == wxID_OK )
414     {
415         for( size_t i = 0;
416              i < p_main_interface->p_open_dialog->mrl.GetCount(); i++ )
417         {
418             playlist_Add( p_playlist,
419                 (const char *)p_main_interface->p_open_dialog->mrl[i].mb_str(),
420                 PLAYLIST_APPEND, PLAYLIST_END );
421         }
422     }
423
424     vlc_object_release( p_playlist );
425
426     Rebuild();
427 }
428
429 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
430 {
431     for( long item = 0; item < listview->GetItemCount(); item++ )
432     {
433         listview->Select( item, ! listview->IsSelected( item ) );
434     }
435 }
436
437 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
438 {
439     /* Delete from the end to the beginning, to avoid a shift of indices */
440     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
441     {
442         if( listview->IsSelected( item ) )
443         {
444             DeleteItem( item );
445         }
446     }
447
448     Rebuild();
449 }
450
451 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
452 {
453     for( long item = 0; item < listview->GetItemCount(); item++ )
454     {
455         listview->Select( item, TRUE );
456     }
457 }
458
459 void Playlist::OnActivateItem( wxListEvent& event )
460 {
461     playlist_t *p_playlist =
462         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
463                                        FIND_ANYWHERE );
464     if( p_playlist == NULL )
465     {
466         return;
467     }
468
469     playlist_Goto( p_playlist, event.GetIndex() );
470
471     vlc_object_release( p_playlist );
472 }
473
474 void Playlist::OnKeyDown( wxListEvent& event )
475 {
476     long keycode = event.GetKeyCode();
477     /* Delete selected items */
478     if( keycode == WXK_BACK || keycode == WXK_DELETE )
479     {
480         /* We send a dummy event */
481         OnDeleteSelection( event );
482     }
483 }
484
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 )
492 {
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 );
497     return VLC_SUCCESS;
498 }