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