]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/playlist.cpp
* modules/gui/wxwindows/playlist.cpp: unicode compilation fixes.
[vlc] / modules / gui / wxwindows / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id: playlist.cpp,v 1.38 2004/01/16 00:01:19 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 <vlc/vlc.h>
28 #include <vlc/intf.h>
29
30 #include "wxwindows.h"
31 #include <wx/listctrl.h>
32
33 /* Callback prototype */
34 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
35                      vlc_value_t old_val, vlc_value_t new_val, void *param );
36 int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
37                      vlc_value_t old_val, vlc_value_t new_val, void *param );
38 int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
39                      vlc_value_t old_val, vlc_value_t new_val, void *param );
40
41 /*****************************************************************************
42  * Event Table.
43  *****************************************************************************/
44
45 /* IDs for the controls and the menu commands */
46 enum
47 {
48     /* menu items */
49     AddFile_Event = 1,
50     AddMRL_Event,
51     Close_Event,
52     Open_Event,
53     Save_Event,
54
55     SortTitle_Event,
56     RSortTitle_Event,
57     SortAuthor_Event,
58     RSortAuthor_Event,
59     SortGroup_Event,
60     RSortGroup_Event,
61     Randomize_Event,
62
63     EnableSelection_Event,
64     DisableSelection_Event,
65
66     InvertSelection_Event,
67     DeleteSelection_Event,
68     Random_Event,
69     Loop_Event,
70     Repeat_Event,
71     SelectAll_Event,
72
73     EnableGroup_Event,
74     DisableGroup_Event,
75
76     Up_Event,
77     Down_Event,
78     Infos_Event,
79
80     PopupPlay_Event,
81     PopupDel_Event,
82     PopupEna_Event,
83     PopupInfo_Event,
84
85     SearchText_Event,
86     Search_Event,
87
88     /* controls */
89     ListView_Event,
90
91     Browse_Event,  /* For export playlist */
92 };
93
94 BEGIN_EVENT_TABLE(Playlist, wxFrame)
95     /* Menu events */
96     EVT_MENU(AddFile_Event, Playlist::OnAddFile)
97     EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
98     EVT_MENU(Close_Event, Playlist::OnClose)
99     EVT_MENU(Open_Event, Playlist::OnOpen)
100     EVT_MENU(Save_Event, Playlist::OnSave)
101
102     EVT_MENU(SortTitle_Event, Playlist::OnSort)
103     EVT_MENU(RSortTitle_Event, Playlist::OnSort)
104     EVT_MENU(SortAuthor_Event, Playlist::OnSort)
105     EVT_MENU(RSortAuthor_Event, Playlist::OnSort)
106     EVT_MENU(SortGroup_Event, Playlist::OnSort)
107     EVT_MENU(RSortGroup_Event, Playlist::OnSort)
108
109     EVT_MENU(Randomize_Event, Playlist::OnSort)
110
111     EVT_MENU(EnableSelection_Event, Playlist::OnEnableSelection)
112     EVT_MENU(DisableSelection_Event, Playlist::OnDisableSelection)
113     EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
114     EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
115     EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
116     EVT_MENU(Infos_Event, Playlist::OnInfos)
117     EVT_CHECKBOX(Random_Event, Playlist::OnRandom)
118     EVT_CHECKBOX(Repeat_Event, Playlist::OnRepeat)
119     EVT_CHECKBOX(Loop_Event, Playlist::OnLoop)
120
121     EVT_MENU(EnableGroup_Event, Playlist::OnEnDis)
122     EVT_MENU(DisableGroup_Event, Playlist::OnEnDis)
123
124     /* Listview events */
125     EVT_LIST_ITEM_ACTIVATED(ListView_Event, Playlist::OnActivateItem)
126     EVT_LIST_COL_CLICK(ListView_Event, Playlist::OnColSelect)
127     EVT_LIST_KEY_DOWN(ListView_Event, Playlist::OnKeyDown)
128     EVT_LIST_ITEM_RIGHT_CLICK(ListView_Event, Playlist::OnPopup)
129
130     /* Popup events */
131     EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
132     EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
133     EVT_MENU( PopupEna_Event, Playlist::OnPopupEna)
134     EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
135
136
137     /* Button events */
138     EVT_BUTTON( Search_Event, Playlist::OnSearch)
139     EVT_BUTTON( Save_Event, Playlist::OnSave)
140     EVT_BUTTON( Infos_Event, Playlist::OnInfos)
141
142     EVT_BUTTON( Up_Event, Playlist::OnUp)
143     EVT_BUTTON( Down_Event, Playlist::OnDown)
144
145     EVT_TEXT(SearchText_Event, Playlist::OnSearchTextChange)
146
147     /* Special events : we don't want to destroy the window when the user
148      * clicks on (X) */
149     EVT_CLOSE(Playlist::OnClose)
150 END_EVENT_TABLE()
151
152
153 /* Event Table for the Newgroup class */
154 BEGIN_EVENT_TABLE(NewGroup, wxDialog)
155     EVT_BUTTON( wxID_OK, NewGroup::OnOk)
156     EVT_BUTTON( wxID_CANCEL, NewGroup::OnCancel)
157 END_EVENT_TABLE()
158
159
160 /*****************************************************************************
161  * Constructor.
162  *****************************************************************************/
163 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
164     wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
165              wxDefaultSize, wxDEFAULT_FRAME_STYLE )
166 {
167     /* Initializations */
168     iteminfo_dialog = NULL;
169     p_intf = _p_intf;
170     vlc_value_t  val;
171     i_update_counter = 0;
172     i_sort_mode = MODE_NONE;
173     b_need_update = VLC_FALSE;
174     vlc_mutex_init( p_intf, &lock );
175     SetIcon( *p_intf->p_sys->p_icon );
176
177     i_title_sorted = 0;
178     i_author_sorted = 0;
179     i_group_sorted = 0;
180
181
182     var_Create( p_intf, "random", VLC_VAR_BOOL );
183     var_Change( p_intf, "random", VLC_VAR_INHERITVALUE, & val, NULL );
184     var_Create( p_intf, "loop", VLC_VAR_BOOL );
185     var_Create( p_intf, "loop", VLC_VAR_BOOL );
186     var_Change( p_intf, "repeat", VLC_VAR_INHERITVALUE, & val, NULL );
187     var_Change( p_intf, "repeat", VLC_VAR_INHERITVALUE, & val, NULL );
188
189     /* Create our "Manage" menu */
190     wxMenu *manage_menu = new wxMenu;
191     manage_menu->Append( AddFile_Event, wxU(_("&Simple Add...")) );
192     manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
193     manage_menu->AppendSeparator();
194     manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
195     manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
196     manage_menu->AppendSeparator();
197     manage_menu->Append( Close_Event, wxU(_("&Close")) );
198
199     /* Create our "Sort" menu */
200     wxMenu *sort_menu = new wxMenu;
201     sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
202     sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
203     sort_menu->AppendSeparator();
204     sort_menu->Append( SortAuthor_Event, wxU(_("Sort by &author")) );
205     sort_menu->Append( RSortAuthor_Event, wxU(_("&Reverse sort by author")) );
206     sort_menu->AppendSeparator();
207     sort_menu->Append( SortGroup_Event, wxU(_("Sort by &group")) );
208     sort_menu->Append( RSortGroup_Event, wxU(_("&Reverse sort by group")) );
209     sort_menu->AppendSeparator();
210     sort_menu->Append( Randomize_Event, wxU(_("&Randomize Playlist")) );
211
212     /* Create our "Selection" menu */
213     wxMenu *selection_menu = new wxMenu;
214     selection_menu->Append( EnableSelection_Event, wxU(_("&Enable")) );
215     selection_menu->Append( DisableSelection_Event, wxU(_("&Disable")) );
216     selection_menu->AppendSeparator();
217     selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
218     selection_menu->Append( DeleteSelection_Event, wxU(_("&Delete")) );
219     selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
220
221     /* Create our "Group" menu */
222     wxMenu *group_menu = new wxMenu;
223     group_menu->Append( EnableGroup_Event, wxU(_("&Enable all group items")) );
224     group_menu->Append( DisableGroup_Event,
225                         wxU(_("&Disable all group items")) );
226
227     /* Append the freshly created menus to the menu bar */
228     wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
229     menubar->Append( manage_menu, wxU(_("&Manage")) );
230     menubar->Append( sort_menu, wxU(_("S&ort")) );
231     menubar->Append( selection_menu, wxU(_("&Selection")) );
232     menubar->Append( group_menu, wxU(_("&Groups")) );
233
234     /* Attach the menu bar to the frame */
235     SetMenuBar( menubar );
236
237     /* Create the popup menu */
238     popup_menu = new wxMenu;
239     popup_menu->Append( PopupPlay_Event, wxU(_("Play item")) );
240     popup_menu->Append( PopupDel_Event, wxU(_("Delete item")) );
241     popup_menu->Append( PopupEna_Event, wxU(_("Toggle enabled")) );
242     popup_menu->Append( PopupInfo_Event, wxU(_("Info on item")) );
243
244     /* Create a panel to put everything in */
245     wxPanel *playlist_panel = new wxPanel( this, -1 );
246     playlist_panel->SetAutoLayout( TRUE );
247
248     /* Create the Random checkbox */
249     wxCheckBox *random_checkbox =
250         new wxCheckBox( playlist_panel, Random_Event, wxU(_("Random")) );
251     var_Get( p_intf, "random", &val );
252     vlc_bool_t b_random = val.b_bool;
253     random_checkbox->SetValue( b_random == VLC_FALSE ? 0 : 1 );
254
255     /* Create the Loop Checkbox */
256     wxCheckBox *loop_checkbox =
257         new wxCheckBox( playlist_panel, Loop_Event, wxU(_("Loop")) );
258     var_Get( p_intf, "loop", &val );
259     int b_loop = val.b_bool ;
260     loop_checkbox->SetValue( b_loop );
261
262     /* Create the Repeat one checkbox */
263     wxCheckBox *repeat_checkbox =
264         new wxCheckBox( playlist_panel, Repeat_Event, wxU(_("Repeat one")) );
265     var_Get( p_intf, "repeat", &val );
266     int b_repeat = val.b_bool ;
267     repeat_checkbox->SetValue( b_repeat );
268
269     /* Create the Search Textbox */
270     search_text =
271         new wxTextCtrl( playlist_panel, SearchText_Event, wxT(""),
272                         wxDefaultPosition, wxSize(140, -1),
273                         wxTE_PROCESS_ENTER);
274
275     /* Create the search button */
276     search_button =
277         new wxButton( playlist_panel, Search_Event, wxU(_("Search")) );
278
279
280     /* Create the listview */
281     /* FIXME: the given size is arbitrary, and prevents us from resizing
282      * the window to smaller dimensions. But the sizers don't seem to adjust
283      * themselves to the size of a listview, and with a wxDefaultSize the
284      * playlist window is ridiculously small */
285     listview = new wxListView( playlist_panel, ListView_Event,
286                                wxDefaultPosition, wxSize( 500, 300 ),
287                                wxLC_REPORT | wxSUNKEN_BORDER );
288     listview->InsertColumn( 0, wxU(_("Name")) );
289     listview->InsertColumn( 1, wxU(_("Author")) );
290     listview->InsertColumn( 2, wxU(_("Group")) );
291     listview->InsertColumn( 3, wxU(_("Duration")) );
292     listview->SetColumnWidth( 0, 270 );
293     listview->SetColumnWidth( 1, 150 );
294     listview->SetColumnWidth( 2, 80 );
295
296     /* Create the Up-Down buttons */
297     wxButton *up_button =
298         new wxButton( playlist_panel, Up_Event, wxU(_("Up") ) );
299
300     wxButton *down_button =
301         new wxButton( playlist_panel, Down_Event, wxU(_("Down") ) );
302
303     /* Create the iteminfo button */
304     wxButton *iteminfo_button =
305         new wxButton( playlist_panel, Infos_Event, wxU(_("Item info") ) );
306
307     /* Place everything in sizers */
308     wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
309     button_sizer->Add( iteminfo_button, 0, wxALIGN_CENTER|wxLEFT , 5);
310     button_sizer->Layout();
311
312     wxBoxSizer *updown_sizer = new wxBoxSizer( wxHORIZONTAL );
313     updown_sizer->Add( up_button, 0, wxALIGN_LEFT|wxRIGHT, 3);
314     updown_sizer->Add( down_button, 0, wxALIGN_LEFT|wxLEFT, 3);
315     updown_sizer->Layout();
316
317     wxBoxSizer *checkbox_sizer = new wxBoxSizer( wxHORIZONTAL );
318     checkbox_sizer->Add( random_checkbox, 0,
319                          wxEXPAND | wxALIGN_RIGHT | wxALL, 5);
320     checkbox_sizer->Add( loop_checkbox, 0,
321                          wxEXPAND | wxALIGN_RIGHT | wxALL, 5);
322     checkbox_sizer->Add( repeat_checkbox, 0,
323                          wxEXPAND | wxALIGN_RIGHT | wxALL, 5);
324     checkbox_sizer->Layout();
325
326     wxBoxSizer *search_sizer = new wxBoxSizer( wxHORIZONTAL );
327     search_sizer->Add( search_text, 0, wxRIGHT|wxALIGN_CENTER, 3);
328     search_sizer->Add( search_button, 0, wxLEFT|wxALIGN_CENTER, 3);
329     search_sizer->Layout();
330
331     /* The top and bottom sizers */
332     wxBoxSizer *top_sizer = new wxBoxSizer( wxHORIZONTAL );
333     top_sizer->Add( checkbox_sizer, 1, wxLEFT|wxRIGHT|wxALIGN_LEFT, 4 );
334     top_sizer->Add( search_sizer, 1, wxLEFT|wxRIGHT|wxALIGN_RIGHT, 4 );
335     top_sizer->Layout();
336
337     wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
338     bottom_sizer->Add( updown_sizer, 0, wxEXPAND |wxRIGHT | wxLEFT | wxALIGN_LEFT, 4);
339     bottom_sizer->Add( button_sizer , 0, wxEXPAND|wxLEFT | wxRIGHT | wxALIGN_RIGHT, 4 );
340     bottom_sizer->Layout();
341
342     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
343
344     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
345     panel_sizer->Add( top_sizer, 0, wxEXPAND | wxALL, 5 );
346     panel_sizer->Add( listview, 1, wxEXPAND | wxALL, 5 );
347     panel_sizer->Add( bottom_sizer, 0 , wxEXPAND | wxALL, 5);
348     panel_sizer->Layout();
349
350     playlist_panel->SetSizerAndFit( panel_sizer );
351     main_sizer->Add( playlist_panel, 1, wxGROW, 0 );
352     main_sizer->Layout();
353     SetSizerAndFit( main_sizer );
354
355 #if !defined(__WXX11__)
356     /* Associate drop targets with the playlist */
357     SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
358 #endif
359
360     playlist_t *p_playlist =
361         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
362                                        FIND_ANYWHERE );
363     if( p_playlist == NULL )
364     {
365         return;
366     }
367
368     /* We want to be noticed of playlist changes */
369
370     /* Some global changes happened -> Rebuild all */
371     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
372
373     /* We went to the next item */
374     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
375
376     /* One item has been updated */
377     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
378
379
380     vlc_object_release( p_playlist );
381
382     /* Update the playlist */
383     Rebuild();
384 }
385
386 Playlist::~Playlist()
387 {
388     playlist_t *p_playlist =
389         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
390                                        FIND_ANYWHERE );
391     if( p_playlist == NULL )
392     {
393         return;
394     }
395
396     delete iteminfo_dialog;
397
398     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
399     vlc_object_release( p_playlist );
400 }
401
402 /**********************************************************************
403  * Update one playlist item
404  **********************************************************************/
405 void Playlist::UpdateItem( int i )
406 {
407     playlist_t *p_playlist =
408         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
409                                        FIND_ANYWHERE );
410     if( p_playlist == NULL )
411     {
412         return;
413     }
414     if( i < 0 || i >= p_playlist->i_size || !p_playlist->pp_items[i] )
415     {
416         vlc_object_release(p_playlist);
417         return;
418     }
419     listview->SetItem( i, 0, wxL2U(p_playlist->pp_items[i]->psz_name) );
420     listview->SetItem( i, 1, wxU( playlist_GetInfo( p_playlist, i,
421                                        _("General") , _("Author") ) ) );
422     char *psz_group = playlist_FindGroup(p_playlist,
423                                          p_playlist->pp_items[i]->i_group);
424     listview->SetItem( i, 2,
425              wxL2U( psz_group ? psz_group : _("Normal") ) );
426
427     if( p_playlist->pp_items[i]->b_enabled == VLC_FALSE )
428     {
429         wxListItem listitem;
430         listitem.m_itemId = i;
431         listitem.SetTextColour( *wxLIGHT_GREY);
432         listview->SetItem(listitem);
433     }
434
435     char psz_duration[MSTRTIME_MAX_SIZE];
436     mtime_t dur = p_playlist->pp_items[i]->i_duration;
437     if( dur != -1 ) secstotimestr( psz_duration, dur/1000000 );
438     else memcpy( psz_duration , "-:--:--", sizeof("-:--:--") );
439     listview->SetItem( i, 3, wxU(psz_duration) );
440
441     /* Change the colour for the currenty played stream */
442     wxListItem listitem;
443     listitem.m_itemId = i;
444     if( i == p_playlist->i_index )
445     {
446         listitem.SetTextColour( *wxRED );
447     }
448     else
449     {
450         listitem.SetTextColour( *wxBLACK );
451     }
452     listview->SetItem( listitem );
453
454     vlc_object_release(p_playlist);
455 }
456
457 /**********************************************************************
458  * Rebuild the playlist
459  **********************************************************************/
460 void Playlist::Rebuild()
461 {
462     playlist_t *p_playlist =
463         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
464                                        FIND_ANYWHERE );
465     if( p_playlist == NULL )
466     {
467         return;
468     }
469
470     int i_focused = listview->GetFocusedItem();
471
472     /* Clear the list... */
473     listview->DeleteAllItems();
474
475     /* ...and rebuild it */
476     vlc_mutex_lock( &p_playlist->object_lock );
477     for( int i = 0; i < p_playlist->i_size; i++ )
478     {
479         wxString filename = wxL2U(p_playlist->pp_items[i]->psz_name);
480         listview->InsertItem( i, filename );
481         UpdateItem( i );
482     }
483     vlc_mutex_unlock( &p_playlist->object_lock );
484
485     if( i_focused )
486     {
487         listview->Focus( i_focused );
488         listview->Select( i_focused );
489     }
490     else
491     {
492         listview->Focus( p_playlist->i_index );
493     }
494
495     vlc_object_release( p_playlist );
496 }
497
498 void Playlist::ShowPlaylist( bool show )
499 {
500     if( show ) Rebuild();
501     Show( show );
502 }
503
504 void Playlist::UpdatePlaylist()
505 {
506     vlc_bool_t b_need_update = VLC_FALSE;
507     i_update_counter++;
508
509     /* If the playlist isn't show there's no need to update it */
510     if( !IsShown() ) return;
511
512     vlc_mutex_lock( &lock );
513     if( this->b_need_update )
514     {
515         b_need_update =VLC_TRUE;
516         this->b_need_update = VLC_FALSE;
517     }
518     vlc_mutex_unlock( &lock );
519
520     if( b_need_update )
521     {
522         Rebuild();
523     }
524
525     /* Updating the playing status every 0.5s is enough */
526     if( i_update_counter % 5 ) return;
527
528     playlist_t *p_playlist =
529         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
530                                        FIND_ANYWHERE );
531     if( p_playlist == NULL )
532     {
533         return;
534     }
535
536     /* Update the colour of items */
537
538     vlc_mutex_lock( &p_playlist->object_lock );
539     if( p_intf->p_sys->i_playing != p_playlist->i_index )
540     {
541         wxListItem listitem;
542         listitem.m_itemId = p_playlist->i_index;
543         listitem.SetTextColour( *wxRED );
544         listview->SetItem( listitem );
545
546         if( p_intf->p_sys->i_playing != -1 )
547         {
548             listitem.m_itemId = p_intf->p_sys->i_playing;
549             listitem.SetTextColour( *wxBLACK );
550             listview->SetItem( listitem );
551         }
552         p_intf->p_sys->i_playing = p_playlist->i_index;
553     }
554     vlc_mutex_unlock( &p_playlist->object_lock );
555     vlc_object_release( p_playlist );
556 }
557
558 /*****************************************************************************
559  * Private methods.
560  *****************************************************************************/
561 void Playlist::DeleteItem( int item )
562 {
563     playlist_t *p_playlist =
564         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
565                                        FIND_ANYWHERE );
566     if( p_playlist == NULL )
567     {
568         return;
569     }
570
571     playlist_Delete( p_playlist, item );
572     listview->DeleteItem( item );
573
574     vlc_object_release( p_playlist );
575 }
576
577 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
578 {
579     Hide();
580 }
581
582 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
583 {
584     struct {
585         char *psz_desc;
586         char *psz_filter;
587         char *psz_module;
588     } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
589                    { _("PLS file"), "*.pls", "export-pls" }};
590     wxString filter = wxT("");
591     for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
592     {
593         filter.Append( wxU(formats[i].psz_desc) );
594         filter.Append( wxT("|") );
595         filter.Append( wxU(formats[i].psz_filter) );
596         filter.Append( wxT("|") );
597     }
598     wxFileDialog dialog( this, wxU(_("Save playlist")),
599                          wxT(""), wxT(""), filter, wxSAVE );
600
601     if( dialog.ShowModal() == wxID_OK )
602     {
603
604         playlist_t * p_playlist =
605             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
606                                            FIND_ANYWHERE );
607
608         if( p_playlist )
609         {
610             if( dialog.GetFilename().mb_str() )
611             {
612                 playlist_Export( p_playlist, dialog.GetFilename().mb_str(),
613                                  formats[dialog.GetFilterIndex()].psz_module );
614             }
615         }
616         
617         vlc_object_release( p_playlist );
618         
619     }
620 }
621
622 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
623 {
624     playlist_t *p_playlist =
625         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
626                                        FIND_ANYWHERE );
627     if( p_playlist == NULL )
628     {
629         return;
630     }
631
632     wxFileDialog dialog( this, wxU(_("Open playlist")),
633                          wxT(""), wxT(""), wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
634
635     if( dialog.ShowModal() == wxID_OK )
636     {
637         playlist_Import( p_playlist, dialog.GetPath().mb_str() );
638     }
639
640     vlc_object_release( p_playlist );
641 }
642
643 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
644 {
645     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
646
647 #if 0
648     Rebuild();
649 #endif
650 }
651
652 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
653 {
654     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
655
656 #if 0
657     Rebuild();
658 #endif
659 }
660
661 /********************************************************************
662  * Move functions
663  ********************************************************************/
664 void Playlist::OnUp( wxCommandEvent& event)
665 {
666     playlist_t *p_playlist =
667         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
668                                        FIND_ANYWHERE );
669     if( p_playlist == NULL )
670     {
671         return;
672     }
673
674     /* We use the first selected item, so find it */
675     long i_item = listview->GetNextItem( i_item, wxLIST_NEXT_ALL,
676                                          wxLIST_STATE_SELECTED);
677     if( i_item > 0 && i_item < p_playlist->i_size )
678     {
679         playlist_Move( p_playlist , i_item, i_item - 1);
680         if( i_item > 1 )
681         {
682             listview->Focus( i_item - 1 );
683         }
684         else
685         {
686             listview->Focus(0);
687         }
688     }
689     vlc_object_release( p_playlist );
690     return;
691 }
692
693 void Playlist::OnDown( wxCommandEvent& event)
694 {
695     playlist_t *p_playlist =
696         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
697                                        FIND_ANYWHERE );
698     if( p_playlist == NULL )
699     {
700         return;
701     }
702
703     /* We use the first selected item, so find it */
704     long i_item = listview->GetNextItem( i_item, wxLIST_NEXT_ALL,
705                                          wxLIST_STATE_SELECTED );
706     if( i_item >= 0 && i_item < p_playlist->i_size - 1 )
707     {
708         playlist_Move( p_playlist , i_item, i_item + 2 );
709         listview->Focus( i_item + 1 );
710     }
711     vlc_object_release( p_playlist );
712     return;
713 }
714
715 /********************************************************************
716  * Sorting functions
717  ********************************************************************/
718 void Playlist::OnSort( wxCommandEvent& event )
719 {
720     playlist_t *p_playlist =
721         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
722                                        FIND_ANYWHERE );
723     if( p_playlist == NULL )
724     {
725         return;
726     }
727     switch( event.GetId() )
728     {
729         case SortTitle_Event:
730            playlist_SortTitle( p_playlist , 0 );
731            break;
732         case RSortTitle_Event:
733            playlist_SortTitle( p_playlist , 1 );
734            break;
735         case SortAuthor_Event:
736            playlist_SortAuthor(p_playlist , 0 );
737            break;
738         case RSortAuthor_Event:
739            playlist_SortAuthor( p_playlist , 1 );
740            break;
741         case SortGroup_Event:
742            playlist_SortGroup( p_playlist , 0 );
743            break;
744         case RSortGroup_Event:
745            playlist_SortGroup( p_playlist , 1 );
746            break;
747         case Randomize_Event:
748            playlist_Sort( p_playlist , SORT_RANDOM, ORDER_NORMAL );
749            break;
750     }
751     vlc_object_release( p_playlist );
752
753     Rebuild();
754
755     return;
756 }
757
758 void Playlist::OnColSelect( wxListEvent& event )
759 {
760     playlist_t *p_playlist =
761         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
762                                        FIND_ANYWHERE );
763     if( p_playlist == NULL )
764     {
765         return;
766     }
767     switch( event.GetColumn() )
768     {
769         case 0:
770             if( i_title_sorted != 1 )
771             {
772                 playlist_SortTitle( p_playlist, 0 );
773                 i_title_sorted = 1;
774             }
775             else
776             {
777                 playlist_SortTitle( p_playlist, 1 );
778                 i_title_sorted = -1;
779             }
780             break;
781         case 1:
782             if( i_author_sorted != 1 )
783             {
784                 playlist_SortAuthor( p_playlist, 0 );
785                 i_author_sorted = 1;
786             }
787             else
788             {
789                 playlist_SortAuthor( p_playlist, 1 );
790                 i_author_sorted = -1;
791             }
792             break;
793         case 2:
794             if( i_group_sorted != 1 )
795             {
796                 playlist_SortGroup( p_playlist, 0 );
797                 i_group_sorted = 1;
798             }
799             else
800             {
801                 playlist_SortGroup( p_playlist, 1 );
802                 i_group_sorted = -1;
803             }
804             break;
805         default:
806             break;
807     }
808     vlc_object_release( p_playlist );
809
810     Rebuild();
811
812     return;
813 }
814
815 /**********************************************************************
816  * Search functions
817  **********************************************************************/
818 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
819 {
820    search_button->SetDefault();
821 }
822
823 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
824 {
825     wxString search_string= search_text->GetValue();
826
827     int i_current;
828     int i_first = 0 ;
829     int i_item = -1;
830
831     for( i_current = 0 ; i_current <= listview->GetItemCount() ; i_current++ )
832     {
833         if( listview->GetItemState( i_current, wxLIST_STATE_SELECTED )
834                    == wxLIST_STATE_SELECTED )
835         {
836             i_first = i_current;
837             break;
838         }
839     }
840
841     for ( i_current = i_first + 1; i_current <= listview->GetItemCount() ;
842           i_current++ )
843     {
844         wxListItem listitem;
845         listitem.SetId( i_current );
846         listview->GetItem( listitem );
847         if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
848         {
849             i_item = i_current;
850             break;
851         }
852     }
853     for( long item = 0; item < listview->GetItemCount(); item++ )
854     {
855         listview->Select( item, FALSE );
856     }
857
858     wxListItem listitem;
859     listitem.SetId(i_item);
860     listitem.m_state = wxLIST_STATE_SELECTED;
861     listview->Select( i_item, TRUE );
862     listview->Focus( i_item );
863
864 }
865
866 /**********************************************************************
867  * Selection functions
868  **********************************************************************/
869 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
870 {
871     for( long item = 0; item < listview->GetItemCount(); item++ )
872     {
873         listview->Select( item, ! listview->IsSelected( item ) );
874     }
875 }
876
877 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
878 {
879     /* Delete from the end to the beginning, to avoid a shift of indices */
880     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
881     {
882         if( listview->IsSelected( item ) )
883         {
884             DeleteItem( item );
885         }
886     }
887
888     Rebuild();
889 }
890
891 void Playlist::OnEnableSelection( wxCommandEvent& WXUNUSED(event) )
892 {
893     playlist_t *p_playlist =
894         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
895                                        FIND_ANYWHERE );
896     if( p_playlist == NULL )
897     {
898         return;
899     }
900
901     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
902     {
903         if( listview->IsSelected( item ) )
904         {
905             playlist_Enable( p_playlist, item );
906             UpdateItem( item );
907         }
908     }
909     vlc_object_release( p_playlist);
910 }
911
912 void Playlist::OnDisableSelection( wxCommandEvent& WXUNUSED(event) )
913 {
914     playlist_t *p_playlist =
915         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
916                                        FIND_ANYWHERE );
917     if( p_playlist == NULL )
918     {
919         return;
920     }
921
922     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
923     {
924         if( listview->IsSelected( item ) )
925         {
926             playlist_Disable( p_playlist, item );
927             UpdateItem( item );
928         }
929     }
930     vlc_object_release( p_playlist);
931 }
932
933 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
934 {
935     for( long item = 0; item < listview->GetItemCount(); item++ )
936     {
937         listview->Select( item, TRUE );
938     }
939 }
940
941 /**********************************************************************
942  * Playlist mode functions
943  **********************************************************************/
944 void Playlist::OnRandom( wxCommandEvent& event )
945 {
946     vlc_value_t val;
947     val.b_bool = event.IsChecked();
948     playlist_t *p_playlist =
949         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
950                                        FIND_ANYWHERE );
951     if( p_playlist == NULL )
952     {
953         return;
954     }
955     var_Set( p_playlist , "random", val);
956     vlc_object_release( p_playlist );
957 }
958 void Playlist::OnLoop ( wxCommandEvent& event )
959 {
960     vlc_value_t val;
961     val.b_bool = event.IsChecked();
962     playlist_t *p_playlist =
963         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
964                                        FIND_ANYWHERE );
965     if( p_playlist == NULL )
966     {
967         return;
968     }
969     var_Set( p_playlist , "loop", val);
970     vlc_object_release( p_playlist );
971 }
972
973 void Playlist::OnRepeat ( wxCommandEvent& event )
974 {
975     vlc_value_t val;
976     val.b_bool = event.IsChecked();
977     playlist_t *p_playlist =
978         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
979                                        FIND_ANYWHERE );
980     if( p_playlist == NULL )
981     {
982         return;
983     }
984     var_Set( p_playlist , "repeat", val);
985     vlc_object_release( p_playlist );
986 }
987
988
989
990 void Playlist::OnActivateItem( wxListEvent& event )
991 {
992     playlist_t *p_playlist =
993         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
994                                        FIND_ANYWHERE );
995     if( p_playlist == NULL )
996     {
997         return;
998     }
999     playlist_Goto( p_playlist, event.GetIndex() );
1000
1001     vlc_object_release( p_playlist );
1002 }
1003
1004 void Playlist::OnKeyDown( wxListEvent& event )
1005 {
1006     long keycode = event.GetKeyCode();
1007     /* Delete selected items */
1008     if( keycode == WXK_BACK || keycode == WXK_DELETE )
1009     {
1010         /* We send a dummy event */
1011         OnDeleteSelection( event );
1012     }
1013 }
1014
1015 void Playlist::ShowInfos( int i_item )
1016 {
1017     playlist_t *p_playlist =
1018         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1019                                        FIND_ANYWHERE );
1020     if( p_playlist == NULL )
1021     {
1022         return;
1023     }
1024     if( iteminfo_dialog == NULL )
1025     {
1026         if( i_item >= 0 && i_item < p_playlist->i_size )
1027         {
1028             iteminfo_dialog = new ItemInfoDialog(
1029                               p_intf, p_playlist->pp_items[i_item], this );
1030             if( iteminfo_dialog->ShowModal()  == wxID_OK )
1031                 UpdateItem( i_item );
1032             delete iteminfo_dialog;
1033             iteminfo_dialog = NULL;
1034         }
1035     }
1036     vlc_object_release( p_playlist );
1037 }
1038
1039 void Playlist::OnInfos( wxCommandEvent& WXUNUSED(event) )
1040 {
1041     /* We use the first selected item, so find it */
1042     long i_item = listview->GetNextItem( -1 , wxLIST_NEXT_ALL,
1043                                          wxLIST_STATE_SELECTED );
1044     ShowInfos( i_item );
1045 }
1046
1047 void Playlist::OnEnDis( wxCommandEvent& event )
1048 {
1049     playlist_t *p_playlist =
1050         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1051                                        FIND_ANYWHERE );
1052     if( p_playlist == NULL )
1053     {
1054         return;
1055     }
1056
1057     long i_item = listview->GetNextItem( i_item, wxLIST_NEXT_ALL,
1058                                          wxLIST_STATE_SELECTED );
1059
1060     if( i_item >= 0 && i_item < p_playlist->i_size )
1061     {
1062        switch( event.GetId() )
1063        {
1064            case EnableGroup_Event:
1065                playlist_EnableGroup( p_playlist ,
1066                                   p_playlist->pp_items[i_item]->i_group );
1067                break;
1068            case DisableGroup_Event:
1069                playlist_DisableGroup( p_playlist ,
1070                                   p_playlist->pp_items[i_item]->i_group );
1071                break;
1072        }
1073        Rebuild();
1074     }
1075
1076     vlc_object_release( p_playlist );
1077 }
1078
1079 /*****************************************************************************
1080  * Popup management functions
1081  *****************************************************************************/
1082 void Playlist::OnPopup( wxListEvent& event )
1083 {
1084     i_popup_item = event.GetIndex();
1085     Playlist::PopupMenu( popup_menu , ScreenToClient( wxGetMousePosition() ) );
1086 }
1087
1088
1089 void Playlist::OnPopupPlay( wxMenuEvent& event )
1090 {
1091     playlist_t *p_playlist =
1092         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1093                                        FIND_ANYWHERE );
1094     if( p_playlist == NULL )
1095     {
1096         return;
1097     }
1098     if( i_popup_item != -1 )
1099     {
1100         playlist_Goto( p_playlist, i_popup_item );
1101     }
1102     vlc_object_release( p_playlist );
1103 }
1104
1105 void Playlist::OnPopupDel( wxMenuEvent& event )
1106 {
1107     DeleteItem( i_popup_item );
1108 }
1109
1110 void Playlist::OnPopupEna( wxMenuEvent& event )
1111 {
1112     playlist_t *p_playlist =
1113         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1114                                        FIND_ANYWHERE );
1115     if( p_playlist == NULL )
1116     {
1117         return;
1118     }
1119
1120     if( p_playlist->pp_items[i_popup_item]->b_enabled )
1121         //playlist_IsEnabled( p_playlist, i_popup_item ) )
1122     {
1123         playlist_Disable( p_playlist, i_popup_item );
1124     }
1125     else
1126     {
1127         playlist_Enable( p_playlist, i_popup_item );
1128     }
1129     vlc_object_release( p_playlist);
1130     UpdateItem( i_popup_item );
1131 }
1132
1133 void Playlist::OnPopupInfo( wxMenuEvent& event )
1134 {
1135     ShowInfos( i_popup_item );
1136 }
1137
1138 /*****************************************************************************
1139  * PlaylistChanged: callback triggered by the intf-change playlist variable
1140  *  We don't rebuild the playlist directly here because we don't want the
1141  *  caller to block for a too long time.
1142  *****************************************************************************/
1143 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1144                      vlc_value_t old_val, vlc_value_t new_val, void *param )
1145 {
1146     Playlist *p_playlist_dialog = (Playlist *)param;
1147     vlc_mutex_lock( &p_playlist_dialog->lock );
1148     p_playlist_dialog->b_need_update = VLC_TRUE;
1149     vlc_mutex_unlock( &p_playlist_dialog->lock );
1150     return VLC_SUCCESS;
1151 }
1152
1153 /*****************************************************************************
1154  * Next: callback triggered by the playlist-current playlist variable
1155  *****************************************************************************/
1156 int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1157                  vlc_value_t old_val, vlc_value_t new_val, void *param )
1158 {
1159     Playlist *p_playlist_dialog = (Playlist *)param;
1160     p_playlist_dialog->UpdateItem( old_val.i_int );
1161     p_playlist_dialog->UpdateItem( new_val.i_int );
1162     return 0;
1163 }
1164
1165
1166 /*****************************************************************************
1167  * ItemChanged: callback triggered by the item-change playlist variable
1168  *****************************************************************************/
1169 int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1170                  vlc_value_t old_val, vlc_value_t new_val, void *param )
1171 {
1172     Playlist *p_playlist_dialog = (Playlist *)param;
1173     p_playlist_dialog->UpdateItem( new_val.i_int );
1174     return 0;
1175 }
1176
1177
1178 /***************************************************************************
1179  * NewGroup Class
1180  ***************************************************************************/
1181 NewGroup::NewGroup( intf_thread_t *_p_intf, wxWindow *_p_parent ):
1182     wxDialog( _p_parent, -1, wxU(_("New Group")), wxDefaultPosition,
1183              wxDefaultSize, wxDEFAULT_FRAME_STYLE )
1184 {
1185     /* Initializations */
1186     p_intf = _p_intf;
1187     psz_name = NULL;
1188     SetIcon( *p_intf->p_sys->p_icon );
1189
1190     /* Create a panel to put everything in*/
1191     wxPanel *panel = new wxPanel( this, -1 );
1192     panel->SetAutoLayout( TRUE );
1193
1194     wxStaticText *group_label =
1195             new wxStaticText( panel , -1,
1196                 wxU(_("Enter a name for the new group")));
1197
1198     groupname = new wxTextCtrl(panel, -1, wxU(""),wxDefaultPosition,
1199                                wxSize(80,27),wxTE_PROCESS_ENTER);
1200
1201     wxButton *ok_button = new wxButton(panel, wxID_OK, wxU(_("OK")) );
1202     ok_button->SetDefault();
1203     wxButton *cancel_button = new wxButton( panel, wxID_CANCEL,
1204                                             wxU(_("Cancel")) );
1205
1206     wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
1207
1208     button_sizer->Add( ok_button, 0, wxALL, 5 );
1209     button_sizer->Add( cancel_button, 0, wxALL, 5 );
1210     button_sizer->Layout();
1211
1212     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
1213     panel_sizer->Add( group_label, 0, wxEXPAND | wxALL, 5 );
1214     panel_sizer->Add( groupname, 0, wxEXPAND | wxALL, 5 );
1215     panel_sizer->Add( button_sizer, 0, wxEXPAND | wxALL, 5 );
1216     panel_sizer->Layout();
1217
1218     panel->SetSizerAndFit( panel_sizer );
1219
1220     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
1221     main_sizer->Add( panel, 1, wxEXPAND, 0 );
1222     main_sizer->Layout();
1223     SetSizerAndFit( main_sizer );
1224 }
1225
1226 NewGroup::~NewGroup()
1227 {
1228 }
1229
1230 void NewGroup::OnOk( wxCommandEvent& event )
1231 {
1232     psz_name = strdup( groupname->GetLineText(0).mb_str() );
1233
1234     playlist_t * p_playlist =
1235           (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1236                                        FIND_ANYWHERE );
1237
1238     if( p_playlist )
1239     {
1240         if( !playlist_CreateGroup( p_playlist, psz_name ) )
1241         {
1242             psz_name = NULL;
1243         }
1244         vlc_object_release( p_playlist );
1245     }
1246
1247     EndModal( wxID_OK );
1248 }
1249
1250 void NewGroup::OnCancel( wxCommandEvent& WXUNUSED(event) )
1251 {
1252     EndModal( wxID_CANCEL );
1253 }