]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/playlist.cpp
* modules/gui/wxwindows/*: small clean-up in included headers.
[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.16 2003/08/27 11:53:26 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 #include <vlc/intf.h>
34
35 #include "wxwindows.h"
36 #include <wx/listctrl.h>
37
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 );
41
42 /*****************************************************************************
43  * Event Table.
44  *****************************************************************************/
45
46 /* IDs for the controls and the menu commands */
47 enum
48 {
49     /* menu items */
50     AddFile_Event = 1,
51     AddMRL_Event,
52     Close_Event,
53     Open_Event,
54     Save_Event,
55
56     InvertSelection_Event,
57     DeleteSelection_Event,
58     Random_Event,
59     Loop_Event,
60     SelectAll_Event,
61
62     /* controls */
63     ListView_Event
64 };
65
66 BEGIN_EVENT_TABLE(Playlist, wxFrame)
67     /* Menu events */
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)
78
79     /* Listview events */
80     EVT_LIST_ITEM_ACTIVATED(ListView_Event, Playlist::OnActivateItem)
81     EVT_LIST_KEY_DOWN(ListView_Event, Playlist::OnKeyDown)
82
83     /* Button events */
84     EVT_BUTTON( Close_Event, Playlist::OnClose)
85     EVT_BUTTON( Save_Event, Playlist::OnSave)
86
87     /* Special events : we don't want to destroy the window when the user
88      * clicks on (X) */
89     EVT_CLOSE(Playlist::OnClose)
90 END_EVENT_TABLE()
91
92 /*****************************************************************************
93  * Constructor.
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 )
98 {
99     /* Initializations */
100     p_intf = _p_intf;
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 );
105
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")) );
114
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")) );
120
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")) );
125
126     /* Attach the menu bar to the frame */
127     SetMenuBar( menubar );
128
129     /* Create a panel to put everything in */
130     wxPanel *playlist_panel = new wxPanel( this, -1 );
131     playlist_panel->SetAutoLayout( TRUE );
132
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 );
145
146     /* Create the Close button */
147     wxButton *close_button = new wxButton( playlist_panel, Close_Event,
148                                            wxU(_("Close")) );
149     close_button->SetDefault();
150    
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 );
156
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 );
162
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 );
180
181 #if !defined(__WXX11__)
182     /* Associate drop targets with the playlist */
183     SetDropTarget( new DragAndDrop( p_intf ) );
184 #endif
185
186     playlist_t *p_playlist =
187         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
188                                        FIND_ANYWHERE );
189     if( p_playlist == NULL )
190     {
191         return;
192     }
193
194     /* We want to be noticed of playlit changes */
195     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
196     vlc_object_release( p_playlist );
197
198     /* Update the playlist */
199     Rebuild();
200 }
201
202 Playlist::~Playlist()
203 {
204     playlist_t *p_playlist =
205         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
206                                        FIND_ANYWHERE );
207     if( p_playlist == NULL )
208     {
209         return;
210     }
211
212     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
213     vlc_object_release( p_playlist );
214 }
215
216 void Playlist::Rebuild()
217 {
218     playlist_t *p_playlist =
219         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
220                                        FIND_ANYWHERE );
221     if( p_playlist == NULL )
222     {
223         return;
224     }
225
226     /* Clear the list... */
227     listview->DeleteAllItems();
228
229     /* ...and rebuild it */
230     vlc_mutex_lock( &p_playlist->object_lock );
231     for( int i = 0; i < p_playlist->i_size; i++ )
232     {
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")) );
237     }
238     vlc_mutex_unlock( &p_playlist->object_lock );
239
240     /* Change the colour for the currenty played stream */
241     wxListItem listitem;
242     listitem.m_itemId = p_playlist->i_index;
243     listitem.SetTextColour( *wxRED );
244     listview->SetItem( listitem );
245
246     vlc_object_release( p_playlist );
247 }
248
249 void Playlist::ShowPlaylist( bool show )
250 {
251     if( show ) Rebuild();
252     Show( show );
253 }
254
255 void Playlist::UpdatePlaylist()
256 {
257     vlc_bool_t b_need_update = VLC_FALSE;
258     i_update_counter++;
259
260     /* If the playlist isn't show there's no need to update it */
261     if( !IsShown() ) return;
262
263     vlc_mutex_lock( &lock );
264     if( this->b_need_update )
265     {
266         b_need_update = VLC_TRUE;
267         this->b_need_update = VLC_FALSE;
268     }
269     vlc_mutex_unlock( &lock );
270
271     if( b_need_update ) Rebuild();
272
273     /* Updating the playing status every 0.5s is enough */
274     if( i_update_counter % 5 ) return;
275
276     playlist_t *p_playlist =
277         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
278                                        FIND_ANYWHERE );
279     if( p_playlist == NULL )
280     {
281         return;
282     }
283
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 )
287     {
288         wxListItem listitem;
289         listitem.m_itemId = p_playlist->i_index;
290         listitem.SetTextColour( *wxRED );
291         listview->SetItem( listitem );
292
293         if( p_intf->p_sys->i_playing != -1 )
294         {
295             listitem.m_itemId = p_intf->p_sys->i_playing;
296             listitem.SetTextColour( *wxBLACK );
297             listview->SetItem( listitem );
298         }
299         p_intf->p_sys->i_playing = p_playlist->i_index;
300     }
301     vlc_mutex_unlock( &p_playlist->object_lock );
302
303     vlc_object_release( p_playlist );
304 }
305
306 /*****************************************************************************
307  * Private methods.
308  *****************************************************************************/
309 void Playlist::DeleteItem( int item )
310 {
311     playlist_t *p_playlist =
312         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
313                                        FIND_ANYWHERE );
314     if( p_playlist == NULL )
315     {
316         return;
317     }
318
319     playlist_Delete( p_playlist, item );
320     listview->DeleteItem( item );
321
322     vlc_object_release( p_playlist );
323 }
324
325 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
326 {
327     Hide();
328 }
329
330 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
331 {
332     playlist_t *p_playlist =
333         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
334                                        FIND_ANYWHERE );
335     if( p_playlist == NULL )
336     {
337         return;
338     }
339
340     wxFileDialog dialog( this, wxU(_("Save playlist")),
341                          wxT(""), wxT(""), wxT("*"), wxSAVE );
342
343     if( dialog.ShowModal() == wxID_OK )
344     {
345         playlist_SaveFile( p_playlist, dialog.GetPath().mb_str() );
346     }
347
348     vlc_object_release( p_playlist );
349 }
350
351 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
352 {
353     playlist_t *p_playlist =
354         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
355                                        FIND_ANYWHERE );
356     if( p_playlist == NULL )
357     {
358         return;
359     }
360
361     wxFileDialog dialog( this, wxU(_("Open playlist")),
362                          wxT(""), wxT(""), wxT("*"), wxOPEN );
363
364     if( dialog.ShowModal() == wxID_OK )
365     {
366         playlist_LoadFile( p_playlist, dialog.GetPath().mb_str() );
367     }
368
369     vlc_object_release( p_playlist );
370 }
371
372 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
373 {
374     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
375
376 #if 0
377     Rebuild();
378 #endif
379 }
380
381 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
382 {
383     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
384
385 #if 0
386     Rebuild();
387 #endif
388 }
389
390 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
391 {
392     for( long item = 0; item < listview->GetItemCount(); item++ )
393     {
394         listview->Select( item, ! listview->IsSelected( item ) );
395     }
396 }
397
398 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
399 {
400     /* Delete from the end to the beginning, to avoid a shift of indices */
401     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
402     {
403         if( listview->IsSelected( item ) )
404         {
405             DeleteItem( item );
406         }
407     }
408
409     Rebuild();
410 }
411
412 void Playlist::OnRandom( wxCommandEvent& event )
413 {
414     config_PutInt( p_intf , "random" , 
415                    event.IsChecked() ? VLC_TRUE : VLC_FALSE );
416 }
417 void Playlist::OnLoop ( wxCommandEvent& event )
418 {
419     config_PutInt( p_intf, "loop",
420                    event.IsChecked() ? VLC_TRUE : VLC_FALSE );    
421
422 }
423
424 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
425 {
426     for( long item = 0; item < listview->GetItemCount(); item++ )
427     {
428         listview->Select( item, TRUE );
429     }
430 }
431
432 void Playlist::OnActivateItem( wxListEvent& event )
433 {
434     playlist_t *p_playlist =
435         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
436                                        FIND_ANYWHERE );
437     if( p_playlist == NULL )
438     {
439         return;
440     }
441
442     playlist_Goto( p_playlist, event.GetIndex() );
443
444     vlc_object_release( p_playlist );
445 }
446 void Playlist::OnKeyDown( wxListEvent& event )
447 {
448     long keycode = event.GetKeyCode();
449     /* Delete selected items */
450     if( keycode == WXK_BACK || keycode == WXK_DELETE )
451     {
452         /* We send a dummy event */
453         OnDeleteSelection( event );
454     }
455 }
456
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 )
464 {
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 );
469     return VLC_SUCCESS;
470 }