]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/playlist.cpp
* src/video_output/video_output.c: new "deinterlace" object variable.
[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.11 2003/05/20 23:17:59 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     AddMRL_Event = 1,
63     Close_Event,
64     Open_Event,
65     Save_Event,
66
67     InvertSelection_Event,
68     DeleteSelection_Event,
69     SelectAll_Event,
70
71     /* controls */
72     ListView_Event
73 };
74
75 BEGIN_EVENT_TABLE(Playlist, wxFrame)
76     /* Menu events */
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)
84
85     /* Listview events */
86     EVT_LIST_ITEM_ACTIVATED(ListView_Event, Playlist::OnActivateItem)
87     EVT_LIST_KEY_DOWN(ListView_Event, Playlist::OnKeyDown)
88
89     /* Button events */
90     EVT_BUTTON( Close_Event, Playlist::OnClose)
91     EVT_BUTTON( Save_Event, Playlist::OnSave)
92
93     /* Special events : we don't want to destroy the window when the user
94      * clicks on (X) */
95     EVT_CLOSE(Playlist::OnClose)
96 END_EVENT_TABLE()
97
98 /*****************************************************************************
99  * Constructor.
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 )
104 {
105     /* Initializations */
106     p_intf = _p_intf;
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 );
112
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")) );
120
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")) );
126
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")) );
131
132     /* Attach the menu bar to the frame */
133     SetMenuBar( menubar );
134
135     /* Create a panel to put everything in */
136     wxPanel *playlist_panel = new wxPanel( this, -1 );
137     playlist_panel->SetAutoLayout( TRUE );
138
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 );
151
152     /* Create the Close button */
153     wxButton *close_button = new wxButton( playlist_panel, Close_Event,
154                                            wxU(_("Close")) );
155     close_button->SetDefault();
156
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 );
170
171 #if !defined(__WXX11__)
172     /* Associate drop targets with the playlist */
173     SetDropTarget( new DragAndDrop( p_intf ) );
174 #endif
175
176     playlist_t *p_playlist =
177         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
178                                        FIND_ANYWHERE );
179     if( p_playlist == NULL )
180     {
181         return;
182     }
183
184     /* We want to be noticed of playlit changes */
185     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
186     vlc_object_release( p_playlist );
187
188     /* Update the playlist */
189     Rebuild();
190 }
191
192 Playlist::~Playlist()
193 {
194     playlist_t *p_playlist =
195         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
196                                        FIND_ANYWHERE );
197     if( p_playlist == NULL )
198     {
199         return;
200     }
201
202     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
203     vlc_object_release( p_playlist );
204 }
205
206 void Playlist::Rebuild()
207 {
208     playlist_t *p_playlist =
209         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
210                                        FIND_ANYWHERE );
211     if( p_playlist == NULL )
212     {
213         return;
214     }
215
216     /* Clear the list... */
217     listview->DeleteAllItems();
218
219     /* ...and rebuild it */
220     vlc_mutex_lock( &p_playlist->object_lock );
221     for( int i = 0; i < p_playlist->i_size; i++ )
222     {
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")) );
227     }
228     vlc_mutex_unlock( &p_playlist->object_lock );
229
230     /* Change the colour for the currenty played stream */
231     wxListItem listitem;
232     listitem.m_itemId = p_playlist->i_index;
233     listitem.SetTextColour( *wxRED );
234     listview->SetItem( listitem );
235
236     vlc_object_release( p_playlist );
237 }
238
239 void Playlist::ShowPlaylist( bool show )
240 {
241     if( show ) Rebuild();
242     Show( show );
243 }
244
245 void Playlist::UpdatePlaylist()
246 {
247     vlc_bool_t b_need_update = VLC_FALSE;
248     i_update_counter++;
249
250     /* If the playlist isn't show there's no need to update it */
251     if( !IsShown() ) return;
252
253     vlc_mutex_lock( &lock );
254     if( this->b_need_update )
255     {
256         b_need_update = VLC_TRUE;
257         this->b_need_update = VLC_FALSE;
258     }
259     vlc_mutex_unlock( &lock );
260
261     if( b_need_update ) Rebuild();
262
263     /* Updating the playing status every 0.5s is enough */
264     if( i_update_counter % 5 ) return;
265
266     playlist_t *p_playlist =
267         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
268                                        FIND_ANYWHERE );
269     if( p_playlist == NULL )
270     {
271         return;
272     }
273
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 )
277     {
278         wxListItem listitem;
279         listitem.m_itemId = p_playlist->i_index;
280         listitem.SetTextColour( *wxRED );
281         listview->SetItem( listitem );
282
283         if( p_intf->p_sys->i_playing != -1 )
284         {
285             listitem.m_itemId = p_intf->p_sys->i_playing;
286             listitem.SetTextColour( *wxBLACK );
287             listview->SetItem( listitem );
288         }
289         p_intf->p_sys->i_playing = p_playlist->i_index;
290     }
291     vlc_mutex_unlock( &p_playlist->object_lock );
292
293     vlc_object_release( p_playlist );
294 }
295
296 /*****************************************************************************
297  * Private methods.
298  *****************************************************************************/
299 void Playlist::DeleteItem( int item )
300 {
301     playlist_t *p_playlist =
302         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
303                                        FIND_ANYWHERE );
304     if( p_playlist == NULL )
305     {
306         return;
307     }
308
309     playlist_Delete( p_playlist, item );
310     listview->DeleteItem( item );
311
312     vlc_object_release( p_playlist );
313 }
314
315 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
316 {
317     Hide();
318 }
319
320 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
321 {
322     playlist_t *p_playlist =
323         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
324                                        FIND_ANYWHERE );
325     if( p_playlist == NULL )
326     {
327         return;
328     }
329
330     wxFileDialog dialog( this, wxU(_("Save playlist")),
331                          wxT(""), wxT(""), wxT("*"), wxSAVE );
332
333     if( dialog.ShowModal() == wxID_OK )
334     {
335         playlist_SaveFile( p_playlist, dialog.GetPath().mb_str() );
336     }
337
338     vlc_object_release( p_playlist );
339 }
340
341 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
342 {
343     playlist_t *p_playlist =
344         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
345                                        FIND_ANYWHERE );
346     if( p_playlist == NULL )
347     {
348         return;
349     }
350
351     wxFileDialog dialog( this, wxU(_("Open playlist")),
352                          wxT(""), wxT(""), wxT("*"), wxOPEN );
353
354     if( dialog.ShowModal() == wxID_OK )
355     {
356         playlist_LoadFile( p_playlist, dialog.GetPath().mb_str() );
357     }
358
359     vlc_object_release( p_playlist );
360 }
361
362 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
363 {
364     playlist_t *p_playlist =
365         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
366                                        FIND_ANYWHERE );
367     if( p_playlist == NULL )
368     {
369         return;
370     }
371
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 );
376
377     if( p_main_interface->p_open_dialog &&
378         p_main_interface->p_open_dialog->ShowModal() == wxID_OK )
379     {
380         for( size_t i = 0;
381              i < p_main_interface->p_open_dialog->mrl.GetCount(); i++ )
382         {
383             playlist_Add( p_playlist,
384                 (const char *)p_main_interface->p_open_dialog->mrl[i].mb_str(),
385                 PLAYLIST_APPEND, PLAYLIST_END );
386         }
387     }
388
389     vlc_object_release( p_playlist );
390
391     Rebuild();
392 }
393
394 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
395 {
396     for( long item = 0; item < listview->GetItemCount(); item++ )
397     {
398         listview->Select( item, ! listview->IsSelected( item ) );
399     }
400 }
401
402 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
403 {
404     /* Delete from the end to the beginning, to avoid a shift of indices */
405     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
406     {
407         if( listview->IsSelected( item ) )
408         {
409             DeleteItem( item );
410         }
411     }
412
413     Rebuild();
414 }
415
416 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
417 {
418     for( long item = 0; item < listview->GetItemCount(); item++ )
419     {
420         listview->Select( item, TRUE );
421     }
422 }
423
424 void Playlist::OnActivateItem( wxListEvent& event )
425 {
426     playlist_t *p_playlist =
427         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
428                                        FIND_ANYWHERE );
429     if( p_playlist == NULL )
430     {
431         return;
432     }
433
434     playlist_Goto( p_playlist, event.GetIndex() );
435
436     vlc_object_release( p_playlist );
437 }
438
439 void Playlist::OnKeyDown( wxListEvent& event )
440 {
441     long keycode = event.GetKeyCode();
442     /* Delete selected items */
443     if( keycode == WXK_BACK || keycode == WXK_DELETE )
444     {
445         /* We send a dummy event */
446         OnDeleteSelection( event );
447     }
448 }
449
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 )
457 {
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 );
462     return VLC_SUCCESS;
463 }