]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/playlist.cpp
* modules/gui/wxwindows/*, include/vlc_interface.h: new generic "open file" dialog.
[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.14 2003/07/20 10:38:49 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, wxWindow *p_parent ):
104     wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
105              wxDefaultSize, wxDEFAULT_FRAME_STYLE )
106 {
107     /* Initializations */
108     p_intf = _p_intf;
109     i_update_counter = 0;
110     b_need_update = VLC_FALSE;
111     vlc_mutex_init( p_intf, &lock );
112     SetIcon( *p_intf->p_sys->p_icon );
113
114     /* Create our "Manage" menu */
115     wxMenu *manage_menu = new wxMenu;
116     manage_menu->Append( AddFile_Event, wxU(_("&Simple Add...")) );
117     manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
118     manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
119     manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
120     manage_menu->AppendSeparator();
121     manage_menu->Append( Close_Event, wxU(_("&Close")) );
122
123     /* Create our "Selection" menu */
124     wxMenu *selection_menu = new wxMenu;
125     selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
126     selection_menu->Append( DeleteSelection_Event, wxU(_("&Delete")) );
127     selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
128
129     /* Append the freshly created menus to the menu bar */
130     wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
131     menubar->Append( manage_menu, wxU(_("&Manage")) );
132     menubar->Append( selection_menu, wxU(_("&Selection")) );
133
134     /* Attach the menu bar to the frame */
135     SetMenuBar( menubar );
136
137     /* Create a panel to put everything in */
138     wxPanel *playlist_panel = new wxPanel( this, -1 );
139     playlist_panel->SetAutoLayout( TRUE );
140
141     /* Create the listview */
142     /* FIXME: the given size is arbitrary, and prevents us from resizing
143      * the window to smaller dimensions. But the sizers don't seem to adjust
144      * themselves to the size of a listview, and with a wxDefaultSize the
145      * playlist window is ridiculously small */
146     listview = new wxListView( playlist_panel, ListView_Event,
147                                wxDefaultPosition, wxSize( 355, 300 ),
148                                wxLC_REPORT | wxSUNKEN_BORDER );
149     listview->InsertColumn( 0, wxU(_("Url")) );
150     listview->InsertColumn( 1, wxU(_("Duration")) );
151     listview->SetColumnWidth( 0, 250 );
152     listview->SetColumnWidth( 1, 100 );
153
154     /* Create the Close button */
155     wxButton *close_button = new wxButton( playlist_panel, Close_Event,
156                                            wxU(_("Close")) );
157     close_button->SetDefault();
158
159     /* Place everything in sizers */
160     wxBoxSizer *close_button_sizer = new wxBoxSizer( wxHORIZONTAL );
161     close_button_sizer->Add( close_button, 0, wxALL, 5 );
162     close_button_sizer->Layout();
163     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
164     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
165     panel_sizer->Add( listview, 1, wxEXPAND | wxALL, 5 );
166     panel_sizer->Add( close_button_sizer, 0, wxALIGN_CENTRE );
167     panel_sizer->Layout();
168     playlist_panel->SetSizerAndFit( panel_sizer );
169     main_sizer->Add( playlist_panel, 1, wxGROW, 0 );
170     main_sizer->Layout();
171     SetSizerAndFit( main_sizer );
172
173 #if !defined(__WXX11__)
174     /* Associate drop targets with the playlist */
175     SetDropTarget( new DragAndDrop( p_intf ) );
176 #endif
177
178     playlist_t *p_playlist =
179         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
180                                        FIND_ANYWHERE );
181     if( p_playlist == NULL )
182     {
183         return;
184     }
185
186     /* We want to be noticed of playlit changes */
187     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
188     vlc_object_release( p_playlist );
189
190     /* Update the playlist */
191     Rebuild();
192 }
193
194 Playlist::~Playlist()
195 {
196     playlist_t *p_playlist =
197         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
198                                        FIND_ANYWHERE );
199     if( p_playlist == NULL )
200     {
201         return;
202     }
203
204     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
205     vlc_object_release( p_playlist );
206 }
207
208 void Playlist::Rebuild()
209 {
210     playlist_t *p_playlist =
211         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
212                                        FIND_ANYWHERE );
213     if( p_playlist == NULL )
214     {
215         return;
216     }
217
218     /* Clear the list... */
219     listview->DeleteAllItems();
220
221     /* ...and rebuild it */
222     vlc_mutex_lock( &p_playlist->object_lock );
223     for( int i = 0; i < p_playlist->i_size; i++ )
224     {
225         wxString filename = wxU(p_playlist->pp_items[i]->psz_name);
226         listview->InsertItem( i, filename );
227         /* FIXME: we should try to find the actual duration... */
228         listview->SetItem( i, 1, wxU(_("no info")) );
229     }
230     vlc_mutex_unlock( &p_playlist->object_lock );
231
232     /* Change the colour for the currenty played stream */
233     wxListItem listitem;
234     listitem.m_itemId = p_playlist->i_index;
235     listitem.SetTextColour( *wxRED );
236     listview->SetItem( listitem );
237
238     vlc_object_release( p_playlist );
239 }
240
241 void Playlist::ShowPlaylist( bool show )
242 {
243     if( show ) Rebuild();
244     Show( show );
245 }
246
247 void Playlist::UpdatePlaylist()
248 {
249     vlc_bool_t b_need_update = VLC_FALSE;
250     i_update_counter++;
251
252     /* If the playlist isn't show there's no need to update it */
253     if( !IsShown() ) return;
254
255     vlc_mutex_lock( &lock );
256     if( this->b_need_update )
257     {
258         b_need_update = VLC_TRUE;
259         this->b_need_update = VLC_FALSE;
260     }
261     vlc_mutex_unlock( &lock );
262
263     if( b_need_update ) Rebuild();
264
265     /* Updating the playing status every 0.5s is enough */
266     if( i_update_counter % 5 ) return;
267
268     playlist_t *p_playlist =
269         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
270                                        FIND_ANYWHERE );
271     if( p_playlist == NULL )
272     {
273         return;
274     }
275
276     /* Update the colour of items */
277     vlc_mutex_lock( &p_playlist->object_lock );
278     if( p_intf->p_sys->i_playing != p_playlist->i_index )
279     {
280         wxListItem listitem;
281         listitem.m_itemId = p_playlist->i_index;
282         listitem.SetTextColour( *wxRED );
283         listview->SetItem( listitem );
284
285         if( p_intf->p_sys->i_playing != -1 )
286         {
287             listitem.m_itemId = p_intf->p_sys->i_playing;
288             listitem.SetTextColour( *wxBLACK );
289             listview->SetItem( listitem );
290         }
291         p_intf->p_sys->i_playing = p_playlist->i_index;
292     }
293     vlc_mutex_unlock( &p_playlist->object_lock );
294
295     vlc_object_release( p_playlist );
296 }
297
298 /*****************************************************************************
299  * Private methods.
300  *****************************************************************************/
301 void Playlist::DeleteItem( int item )
302 {
303     playlist_t *p_playlist =
304         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
305                                        FIND_ANYWHERE );
306     if( p_playlist == NULL )
307     {
308         return;
309     }
310
311     playlist_Delete( p_playlist, item );
312     listview->DeleteItem( item );
313
314     vlc_object_release( p_playlist );
315 }
316
317 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
318 {
319     Hide();
320 }
321
322 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
323 {
324     playlist_t *p_playlist =
325         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
326                                        FIND_ANYWHERE );
327     if( p_playlist == NULL )
328     {
329         return;
330     }
331
332     wxFileDialog dialog( this, wxU(_("Save playlist")),
333                          wxT(""), wxT(""), wxT("*"), wxSAVE );
334
335     if( dialog.ShowModal() == wxID_OK )
336     {
337         playlist_SaveFile( p_playlist, dialog.GetPath().mb_str() );
338     }
339
340     vlc_object_release( p_playlist );
341 }
342
343 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
344 {
345     playlist_t *p_playlist =
346         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
347                                        FIND_ANYWHERE );
348     if( p_playlist == NULL )
349     {
350         return;
351     }
352
353     wxFileDialog dialog( this, wxU(_("Open playlist")),
354                          wxT(""), wxT(""), wxT("*"), wxOPEN );
355
356     if( dialog.ShowModal() == wxID_OK )
357     {
358         playlist_LoadFile( p_playlist, dialog.GetPath().mb_str() );
359     }
360
361     vlc_object_release( p_playlist );
362 }
363
364 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
365 {
366     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
367
368 #if 0
369     Rebuild();
370 #endif
371 }
372
373 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
374 {
375     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
376
377 #if 0
378     Rebuild();
379 #endif
380 }
381
382 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
383 {
384     for( long item = 0; item < listview->GetItemCount(); item++ )
385     {
386         listview->Select( item, ! listview->IsSelected( item ) );
387     }
388 }
389
390 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
391 {
392     /* Delete from the end to the beginning, to avoid a shift of indices */
393     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
394     {
395         if( listview->IsSelected( item ) )
396         {
397             DeleteItem( item );
398         }
399     }
400
401     Rebuild();
402 }
403
404 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
405 {
406     for( long item = 0; item < listview->GetItemCount(); item++ )
407     {
408         listview->Select( item, TRUE );
409     }
410 }
411
412 void Playlist::OnActivateItem( wxListEvent& event )
413 {
414     playlist_t *p_playlist =
415         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
416                                        FIND_ANYWHERE );
417     if( p_playlist == NULL )
418     {
419         return;
420     }
421
422     playlist_Goto( p_playlist, event.GetIndex() );
423
424     vlc_object_release( p_playlist );
425 }
426
427 void Playlist::OnKeyDown( wxListEvent& event )
428 {
429     long keycode = event.GetKeyCode();
430     /* Delete selected items */
431     if( keycode == WXK_BACK || keycode == WXK_DELETE )
432     {
433         /* We send a dummy event */
434         OnDeleteSelection( event );
435     }
436 }
437
438 /*****************************************************************************
439  * PlaylistChanged: callback triggered by the intf-change playlist variable
440  *  We don't rebuild the playlist directly here because we don't want the
441  *  caller to block for a too long time.
442  *****************************************************************************/
443 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
444                      vlc_value_t old_val, vlc_value_t new_val, void *param )
445 {
446     Playlist *p_playlist_dialog = (Playlist *)param;
447     vlc_mutex_lock( &p_playlist_dialog->lock );
448     p_playlist_dialog->b_need_update = VLC_TRUE;
449     vlc_mutex_unlock( &p_playlist_dialog->lock );
450     return VLC_SUCCESS;
451 }