]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/playlist.cpp
c67e360e1a0915ecd06f89bd6298b9ce6aeec667
[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.20 2003/09/21 18:07:51 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     Sort_Event,
53     RSort_Event,
54     Close_Event,
55     Open_Event,
56     Save_Event,
57
58     InvertSelection_Event,
59     DeleteSelection_Event,
60     Random_Event,
61     Loop_Event,
62     SelectAll_Event,
63
64     SearchText_Event,
65     Search_Event,
66
67     /* controls */
68     ListView_Event
69 };
70
71 BEGIN_EVENT_TABLE(Playlist, wxFrame)
72     /* Menu events */
73     EVT_MENU(AddFile_Event, Playlist::OnAddFile)
74     EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
75     EVT_MENU(Sort_Event, Playlist::OnSort)
76     EVT_MENU(RSort_Event, Playlist::OnRSort)
77     EVT_MENU(Close_Event, Playlist::OnClose)
78     EVT_MENU(Open_Event, Playlist::OnOpen)
79     EVT_MENU(Save_Event, Playlist::OnSave)
80     EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
81     EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
82     EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
83     EVT_CHECKBOX(Random_Event, Playlist::OnRandom)
84     EVT_CHECKBOX(Loop_Event, Playlist::OnLoop)
85
86     /* Listview events */
87     EVT_LIST_ITEM_ACTIVATED(ListView_Event, Playlist::OnActivateItem)
88     EVT_LIST_KEY_DOWN(ListView_Event, Playlist::OnKeyDown)
89
90     /* Button events */
91     EVT_BUTTON( Search_Event, Playlist::OnSearch)
92     EVT_BUTTON( Save_Event, Playlist::OnSave)
93
94     EVT_TEXT(SearchText_Event, Playlist::OnSearchTextChange)
95
96     /* Special events : we don't want to destroy the window when the user
97      * clicks on (X) */
98     EVT_CLOSE(Playlist::OnClose)
99 END_EVENT_TABLE()
100
101 /*****************************************************************************
102  * Constructor.
103  *****************************************************************************/
104 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
105     wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
106              wxDefaultSize, wxDEFAULT_FRAME_STYLE )
107 {
108     /* Initializations */
109     p_intf = _p_intf;
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( Sort_Event, wxU(_("&Sort")) );
120     manage_menu->Append( RSort_Event, wxU(_("&Reverse Sort")) );
121     manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
122     manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
123     manage_menu->AppendSeparator();
124     manage_menu->Append( Close_Event, wxU(_("&Close")) );
125
126     /* Create our "Selection" menu */
127     wxMenu *selection_menu = new wxMenu;
128     selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
129     selection_menu->Append( DeleteSelection_Event, wxU(_("&Delete")) );
130     selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
131
132     /* Append the freshly created menus to the menu bar */
133     wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
134     menubar->Append( manage_menu, wxU(_("&Manage")) );
135     menubar->Append( selection_menu, wxU(_("&Selection")) );
136
137     /* Attach the menu bar to the frame */
138     SetMenuBar( menubar );
139
140     /* Create a panel to put everything in */
141     wxPanel *playlist_panel = new wxPanel( this, -1 );
142     playlist_panel->SetAutoLayout( TRUE );
143
144     /* Create the listview */
145     /* FIXME: the given size is arbitrary, and prevents us from resizing
146      * the window to smaller dimensions. But the sizers don't seem to adjust
147      * themselves to the size of a listview, and with a wxDefaultSize the
148      * playlist window is ridiculously small */
149     listview = new wxListView( playlist_panel, ListView_Event,
150                                wxDefaultPosition, wxSize( 355, 300 ),
151                                wxLC_REPORT | wxSUNKEN_BORDER );
152     listview->InsertColumn( 0, wxU(_("Url")) );
153     listview->InsertColumn( 1, wxU(_("Duration")) );
154     listview->SetColumnWidth( 0, 250 );
155     listview->SetColumnWidth( 1, 100 );
156
157     /* Create the Random checkbox */
158     wxCheckBox *random_checkbox = 
159         new wxCheckBox( playlist_panel, Random_Event, wxU(_("Random")) );
160     int b_random = config_GetInt( p_intf, "random") ;
161     random_checkbox->SetValue( b_random );
162
163     /* Create the Loop Checkbox */
164     wxCheckBox *loop_checkbox = 
165         new wxCheckBox( playlist_panel, Loop_Event, wxU(_("Loop")) );
166     int b_loop = config_GetInt( p_intf, "loop") ; 
167     loop_checkbox->SetValue( b_loop );
168
169     /* Create the Search Textbox */
170     search_text =
171         new wxTextCtrl( playlist_panel, SearchText_Event, wxT(""), 
172                         wxDefaultPosition, wxSize( 100, -1), 
173                         wxTE_PROCESS_ENTER);    
174
175     /* Create the search button */
176     search_button = 
177         new wxButton( playlist_panel, Search_Event, wxU(_("Search")) );
178
179  
180     /* Place everything in sizers */
181     wxBoxSizer *search_sizer = new wxBoxSizer( wxHORIZONTAL );
182     search_sizer->Add( search_text, 0, wxEXPAND|wxALL, 5);
183     search_sizer->Add( search_button, 0, wxEXPAND|wxALL, 5);
184     search_sizer->Add( random_checkbox, 0, 
185                              wxEXPAND|wxALIGN_RIGHT|wxALL, 5);
186     search_sizer->Add( loop_checkbox, 0, 
187                               wxEXPAND|wxALIGN_RIGHT|wxALL, 5);
188     search_sizer->Layout();
189
190     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
191
192     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
193     panel_sizer->Add( listview, 1, wxEXPAND | wxALL, 5 );
194     panel_sizer->Add( search_sizer, 0 , wxALIGN_CENTRE);
195     panel_sizer->Layout();
196
197     playlist_panel->SetSizerAndFit( panel_sizer );
198     main_sizer->Add( playlist_panel, 1, wxGROW, 0 );
199     main_sizer->Layout();
200     SetSizerAndFit( main_sizer );
201
202 #if !defined(__WXX11__)
203     /* Associate drop targets with the playlist */
204     SetDropTarget( new DragAndDrop( p_intf ) );
205 #endif
206
207     playlist_t *p_playlist =
208         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
209                                        FIND_ANYWHERE );
210     if( p_playlist == NULL )
211     {
212         return;
213     }
214
215     /* We want to be noticed of playlit changes */
216     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
217     vlc_object_release( p_playlist );
218
219     /* Update the playlist */
220     Rebuild();
221 }
222
223 Playlist::~Playlist()
224 {
225     playlist_t *p_playlist =
226         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
227                                        FIND_ANYWHERE );
228     if( p_playlist == NULL )
229     {
230         return;
231     }
232
233     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
234     vlc_object_release( p_playlist );
235 }
236
237 void Playlist::Rebuild()
238 {
239     playlist_t *p_playlist =
240         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
241                                        FIND_ANYWHERE );
242     if( p_playlist == NULL )
243     {
244         return;
245     }
246
247     /* Clear the list... */
248     listview->DeleteAllItems();
249
250     /* ...and rebuild it */
251     vlc_mutex_lock( &p_playlist->object_lock );
252     for( int i = 0; i < p_playlist->i_size; i++ )
253     {
254         wxString filename = wxU(p_playlist->pp_items[i]->psz_name);
255         listview->InsertItem( i, filename );
256         /* FIXME: we should try to find the actual duration... */
257         listview->SetItem( i, 1, wxU(_("no info")) );
258     }
259     vlc_mutex_unlock( &p_playlist->object_lock );
260
261     /* Change the colour for the currenty played stream */
262     wxListItem listitem;
263     listitem.m_itemId = p_playlist->i_index;
264     listitem.SetTextColour( *wxRED );
265     listview->SetItem( listitem );
266
267     vlc_object_release( p_playlist );
268 }
269
270 void Playlist::ShowPlaylist( bool show )
271 {
272     if( show ) Rebuild();
273     Show( show );
274 }
275
276 void Playlist::UpdatePlaylist()
277 {
278     vlc_bool_t b_need_update = VLC_FALSE;
279     i_update_counter++;
280
281     /* If the playlist isn't show there's no need to update it */
282     if( !IsShown() ) return;
283
284     vlc_mutex_lock( &lock );
285     if( this->b_need_update )
286     {
287         b_need_update = VLC_TRUE;
288         this->b_need_update = VLC_FALSE;
289     }
290     vlc_mutex_unlock( &lock );
291
292     if( b_need_update ) Rebuild();
293
294     /* Updating the playing status every 0.5s is enough */
295     if( i_update_counter % 5 ) return;
296
297     playlist_t *p_playlist =
298         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
299                                        FIND_ANYWHERE );
300     if( p_playlist == NULL )
301     {
302         return;
303     }
304
305     /* Update the colour of items */
306     vlc_mutex_lock( &p_playlist->object_lock );
307     if( p_intf->p_sys->i_playing != p_playlist->i_index )
308     {
309         wxListItem listitem;
310         listitem.m_itemId = p_playlist->i_index;
311         listitem.SetTextColour( *wxRED );
312         listview->SetItem( listitem );
313
314         if( p_intf->p_sys->i_playing != -1 )
315         {
316             listitem.m_itemId = p_intf->p_sys->i_playing;
317             listitem.SetTextColour( *wxBLACK );
318             listview->SetItem( listitem );
319         }
320         p_intf->p_sys->i_playing = p_playlist->i_index;
321     }
322     vlc_mutex_unlock( &p_playlist->object_lock );
323
324     vlc_object_release( p_playlist );
325 }
326
327 /*****************************************************************************
328  * Private methods.
329  *****************************************************************************/
330 void Playlist::DeleteItem( int item )
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     playlist_Delete( p_playlist, item );
341     listview->DeleteItem( item );
342
343     vlc_object_release( p_playlist );
344 }
345
346 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
347 {
348     Hide();
349 }
350
351 void Playlist::OnSave( 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(_("Save playlist")),
362                          wxT(""), wxT(""), wxT("*"), wxSAVE );
363
364     if( dialog.ShowModal() == wxID_OK )
365     {
366         playlist_SaveFile( p_playlist, dialog.GetPath().mb_str() );
367     }
368
369     vlc_object_release( p_playlist );
370 }
371
372 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
373 {
374     playlist_t *p_playlist =
375         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
376                                        FIND_ANYWHERE );
377     if( p_playlist == NULL )
378     {
379         return;
380     }
381
382     wxFileDialog dialog( this, wxU(_("Open playlist")),
383                          wxT(""), wxT(""), wxT("*"), wxOPEN );
384
385     if( dialog.ShowModal() == wxID_OK )
386     {
387         playlist_LoadFile( p_playlist, dialog.GetPath().mb_str() );
388     }
389
390     vlc_object_release( p_playlist );
391 }
392
393 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
394 {
395     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
396
397 #if 0
398     Rebuild();
399 #endif
400 }
401
402 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
403 {
404     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
405
406 #if 0
407     Rebuild();
408 #endif
409 }
410
411 void Playlist::OnSort( wxCommandEvent& WXUNUSED(event) )
412 {
413     playlist_t *p_playlist =
414         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
415                                        FIND_ANYWHERE );
416     if( p_playlist == NULL )
417     {
418         return;
419     }
420
421     playlist_Sort( p_playlist , 0  );
422    
423     vlc_object_release( p_playlist );
424
425     Rebuild();
426
427     return;
428 }
429
430 void Playlist::OnRSort( wxCommandEvent& WXUNUSED(event) )
431 {
432     playlist_t *p_playlist =
433         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
434                                        FIND_ANYWHERE );
435     if( p_playlist == NULL )
436     {
437         return;
438     }
439
440     playlist_Sort( p_playlist , 1 );
441    
442     vlc_object_release( p_playlist );
443
444     Rebuild();
445
446     return;
447 }
448
449 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
450 {
451    search_button->SetDefault(); 
452 }
453
454 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
455 {
456     wxString search_string= search_text->GetValue();
457
458     int i_current;
459     int i_first = 0 ; 
460     int i_item = -1;
461
462     for( i_current = 0 ; i_current <= listview->GetItemCount() ; i_current++ ) 
463     {
464         if( listview->GetItemState( i_current, wxLIST_STATE_SELECTED)
465                 == wxLIST_STATE_SELECTED )
466         {
467             i_first = i_current;
468             break;
469         }
470     }
471
472     for ( i_current = i_first + 1; i_current <= listview->GetItemCount()
473                  ; i_current++ )
474     {
475         wxListItem listitem;
476         listitem.SetId( i_current );
477         listview->GetItem( listitem );
478         if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
479         {
480            i_item = i_current;
481            break;
482         }
483     }
484     for( long item = 0; item < listview->GetItemCount(); item++ )
485     {
486         listview->Select( item, FALSE );
487     }
488  
489     wxListItem listitem;
490     listitem.SetId(i_item);
491     listitem.m_state = wxLIST_STATE_SELECTED;
492     listview->Select( i_item, TRUE );
493     listview->Focus( i_item );
494
495 }
496
497 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
498 {
499     for( long item = 0; item < listview->GetItemCount(); item++ )
500     {
501         listview->Select( item, ! listview->IsSelected( item ) );
502     }
503 }
504
505 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
506 {
507     /* Delete from the end to the beginning, to avoid a shift of indices */
508     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
509     {
510         if( listview->IsSelected( item ) )
511         {
512             DeleteItem( item );
513         }
514     }
515
516     Rebuild();
517 }
518
519 void Playlist::OnRandom( wxCommandEvent& event )
520 {
521     config_PutInt( p_intf , "random" , 
522                    event.IsChecked() ? VLC_TRUE : VLC_FALSE );
523 }
524 void Playlist::OnLoop ( wxCommandEvent& event )
525 {
526     config_PutInt( p_intf, "loop",
527                    event.IsChecked() ? VLC_TRUE : VLC_FALSE );    
528
529 }
530
531 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
532 {
533     for( long item = 0; item < listview->GetItemCount(); item++ )
534     {
535         listview->Select( item, TRUE );
536     }
537 }
538
539 void Playlist::OnActivateItem( wxListEvent& event )
540 {
541     playlist_t *p_playlist =
542         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
543                                        FIND_ANYWHERE );
544     if( p_playlist == NULL )
545     {
546         return;
547     }
548
549     playlist_Goto( p_playlist, event.GetIndex() );
550
551     vlc_object_release( p_playlist );
552 }
553 void Playlist::OnKeyDown( wxListEvent& event )
554 {
555     long keycode = event.GetKeyCode();
556     /* Delete selected items */
557     if( keycode == WXK_BACK || keycode == WXK_DELETE )
558     {
559         /* We send a dummy event */
560         OnDeleteSelection( event );
561     }
562 }
563
564 /*****************************************************************************
565  * PlaylistChanged: callback triggered by the intf-change playlist variable
566  *  We don't rebuild the playlist directly here because we don't want the
567  *  caller to block for a too long time.
568  *****************************************************************************/
569 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
570                      vlc_value_t old_val, vlc_value_t new_val, void *param )
571 {
572     Playlist *p_playlist_dialog = (Playlist *)param;
573     vlc_mutex_lock( &p_playlist_dialog->lock );
574     p_playlist_dialog->b_need_update = VLC_TRUE;
575     vlc_mutex_unlock( &p_playlist_dialog->lock );
576     return VLC_SUCCESS;
577 }