]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/playlist.cpp
4d83695ce0ed02578205394d6ab6058e451fd1db
[vlc] / modules / gui / wxwindows / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Clément Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/OR MODIFy
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/intf.h>
30
31 #include "wxwindows.h"
32
33 #include "bitmaps/shuffle.xpm"
34 #include "bitmaps/repeat.xpm"
35 #include "bitmaps/loop.xpm"
36
37 #include "bitmaps/type_unknown.xpm"
38 #include "bitmaps/type_net.xpm"
39 #include "bitmaps/type_card.xpm"
40 #include "bitmaps/type_disc.xpm"
41 #include "bitmaps/type_directory.xpm"
42 #include "bitmaps/type_playlist.xpm"
43
44 #include <wx/dynarray.h>
45 #include <wx/imaglist.h>
46
47 #define HELP_SHUFFLE N_( "Shuffle" )
48 #define HELP_LOOP N_( "Loop" )
49 #define HELP_REPEAT N_( "Repeat" )
50
51 /* Callback prototype */
52 static int PlaylistChanged( vlc_object_t *, const char *,
53                             vlc_value_t, vlc_value_t, void * );
54 static int PlaylistNext( vlc_object_t *, const char *,
55                          vlc_value_t, vlc_value_t, void * );
56 static int ItemChanged( vlc_object_t *, const char *,
57                         vlc_value_t, vlc_value_t, void * );
58
59 /*****************************************************************************
60  * Event Table.
61  *****************************************************************************/
62
63 /* IDs for the controls and the menu commands */
64 enum
65 {
66     /* menu items */
67     AddFile_Event = 1,
68     AddMRL_Event,
69     Close_Event,
70     Open_Event,
71     Save_Event,
72
73     SortTitle_Event,
74     RSortTitle_Event,
75     SortAuthor_Event,
76     RSortAuthor_Event,
77     Randomize_Event,
78
79     EnableSelection_Event,
80     DisableSelection_Event,
81
82     InvertSelection_Event,
83     DeleteSelection_Event,
84     Random_Event,
85     Loop_Event,
86     Repeat_Event,
87     SelectAll_Event,
88
89     Up_Event,
90     Down_Event,
91     Infos_Event,
92
93     PopupPlay_Event,
94     PopupPlayThis_Event,
95     PopupDel_Event,
96     PopupEna_Event,
97     PopupInfo_Event,
98
99     SearchText_Event,
100     Search_Event,
101
102     /* controls */
103     TreeCtrl_Event,
104
105     Browse_Event,  /* For export playlist */
106
107     /* custom events */
108     UpdateItem_Event,
109
110     MenuDummy_Event = wxID_HIGHEST + 999,
111
112     FirstView_Event = wxID_HIGHEST + 1000,
113     LastView_Event = wxID_HIGHEST + 1100,
114
115     FirstSD_Event = wxID_HIGHEST + 2000,
116     LastSD_Event = wxID_HIGHEST + 2100,
117 };
118
119 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
120
121 BEGIN_EVENT_TABLE(Playlist, wxFrame)
122     EVT_SIZE(Playlist::OnSize)
123
124     /* Menu events */
125     EVT_MENU(AddFile_Event, Playlist::OnAddFile)
126     EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
127     EVT_MENU(Close_Event, Playlist::OnClose)
128     EVT_MENU(Open_Event, Playlist::OnOpen)
129     EVT_MENU(Save_Event, Playlist::OnSave)
130
131     EVT_MENU(SortTitle_Event, Playlist::OnSort)
132     EVT_MENU(RSortTitle_Event, Playlist::OnSort)
133     EVT_MENU(SortAuthor_Event, Playlist::OnSort)
134     EVT_MENU(RSortAuthor_Event, Playlist::OnSort)
135
136     EVT_MENU(Randomize_Event, Playlist::OnSort)
137
138     EVT_MENU(EnableSelection_Event, Playlist::OnEnableSelection)
139     EVT_MENU(DisableSelection_Event, Playlist::OnDisableSelection)
140     EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
141     EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
142     EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
143     EVT_MENU(Infos_Event, Playlist::OnInfos)
144
145     EVT_MENU_OPEN( Playlist::OnMenuOpen )
146     EVT_MENU( -1, Playlist::OnMenuEvent )
147
148     EVT_TOOL(Random_Event, Playlist::OnRandom)
149     EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
150     EVT_TOOL(Loop_Event, Playlist::OnLoop)
151
152     /* Popup events */
153     EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
154     EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
155     EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
156     EVT_MENU( PopupEna_Event, Playlist::OnPopupEna)
157     EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
158
159     /* Tree control events */
160     EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
161
162     EVT_CONTEXT_MENU( Playlist::OnPopup )
163
164     /* Button events */
165     EVT_BUTTON( Search_Event, Playlist::OnSearch)
166     EVT_BUTTON( Save_Event, Playlist::OnSave)
167     EVT_BUTTON( Infos_Event, Playlist::OnInfos)
168
169     EVT_BUTTON( Up_Event, Playlist::OnUp)
170     EVT_BUTTON( Down_Event, Playlist::OnDown)
171
172     EVT_TEXT(SearchText_Event, Playlist::OnSearchTextChange)
173
174     /* Custom events */
175     EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
176
177     /* Special events : we don't want to destroy the window when the user
178      * clicks on (X) */
179     EVT_CLOSE(Playlist::OnClose)
180 END_EVENT_TABLE()
181
182 /*****************************************************************************
183  * PlaylistItem class
184  ****************************************************************************/
185 class PlaylistItem : public wxTreeItemData
186 {
187 public:
188     PlaylistItem( playlist_item_t *_p_item ) : wxTreeItemData()
189     {
190         p_item = _p_item;
191     }
192 protected:
193     playlist_item_t *p_item;
194 friend class Playlist;
195 };
196
197 /*****************************************************************************
198  * Constructor.
199  *****************************************************************************/
200 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
201     wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
202              wxSize(345,400), wxDEFAULT_FRAME_STYLE )
203 {
204     vlc_value_t val;
205
206     /* Initializations */
207     p_intf = _p_intf;
208     i_update_counter = 0;
209     i_sort_mode = MODE_NONE;
210     b_need_update = VLC_FALSE;
211     SetIcon( *p_intf->p_sys->p_icon );
212
213     p_view_menu = NULL;
214     p_sd_menu = SDMenu();
215
216     i_current_view = VIEW_SIMPLE;
217
218     i_title_sorted = 0;
219     i_author_sorted = 0;
220     i_group_sorted = 0;
221     i_duration_sorted = 0;
222
223     var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
224     var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
225     var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
226
227     /* Create our "Manage" menu */
228     wxMenu *manage_menu = new wxMenu;
229     manage_menu->Append( AddFile_Event, wxU(_("&Simple Add...")) );
230     manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
231     manage_menu->AppendSeparator();
232     manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
233                          p_sd_menu );
234     manage_menu->AppendSeparator();
235     manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
236     manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
237     manage_menu->AppendSeparator();
238     manage_menu->Append( Close_Event, wxU(_("&Close")) );
239
240     /* Create our "Sort" menu */
241     wxMenu *sort_menu = new wxMenu;
242     sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
243     sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
244     sort_menu->AppendSeparator();
245     sort_menu->Append( SortAuthor_Event, wxU(_("Sort by &author")) );
246     sort_menu->Append( RSortAuthor_Event, wxU(_("Reverse sort by author")) );
247     sort_menu->AppendSeparator();
248     sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
249
250     /* Create our "Selection" menu */
251     wxMenu *selection_menu = new wxMenu;
252     selection_menu->Append( EnableSelection_Event, wxU(_("&Enable")) );
253     selection_menu->Append( DisableSelection_Event, wxU(_("&Disable")) );
254     selection_menu->AppendSeparator();
255     selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
256     selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
257     selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
258
259     /* Create our "View" menu */
260     ViewMenu();
261
262     /* Append the freshly created menus to the menu bar */
263     wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
264     menubar->Append( manage_menu, wxU(_("&Manage")) );
265     menubar->Append( sort_menu, wxU(_("S&ort")) );
266     menubar->Append( selection_menu, wxU(_("&Selection")) );
267     menubar->Append( p_view_menu, wxU(_("&View items") ) );
268
269     /* Attach the menu bar to the frame */
270     SetMenuBar( menubar );
271
272     /* Create the popup menu */
273     popup_menu = new wxMenu;
274     popup_menu->Append( PopupPlay_Event, wxU(_("Play")) );
275     popup_menu->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
276     popup_menu->Append( PopupDel_Event, wxU(_("Delete")) );
277     popup_menu->Append( PopupEna_Event, wxU(_("Enable/Disable")) );
278     popup_menu->Append( PopupInfo_Event, wxU(_("Info")) );
279
280     /* Create a panel to put everything in */
281     wxPanel *playlist_panel = new wxPanel( this, -1 );
282     playlist_panel->SetAutoLayout( TRUE );
283
284     /* Create the toolbar */
285     wxToolBar *toolbar =
286         CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT | wxTB_DOCKABLE );
287
288     /* Create the random tool */
289     toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
290                        wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
291                        wxU(_(HELP_SHUFFLE) ) );
292     var_Get( p_intf, "random", &val );
293     toolbar->ToggleTool( Random_Event, val.b_bool );
294
295     /* Create the Loop tool */
296     toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
297                       wxBitmap( loop_xpm), wxITEM_CHECK,
298                       wxU(_(HELP_LOOP )  ) );
299     var_Get( p_intf, "loop", &val );
300     toolbar->ToggleTool( Loop_Event, val.b_bool );
301
302     /* Create the Repeat one checkbox */
303     toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
304                       wxBitmap( repeat_xpm), wxITEM_CHECK,
305                       wxU(_(HELP_REPEAT )  ) );
306     var_Get( p_intf, "repeat", &val );
307     toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
308
309     /* Create the Search Textbox */
310     search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
311                                   wxDefaultPosition, wxSize(100, -1),
312                                   wxTE_PROCESS_ENTER);
313
314     /* Create the search button */
315     search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
316
317     toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
318                          wxSize(16, 16), wxBORDER_NONE ) );
319     toolbar->AddControl( search_text );
320     toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
321                          wxSize(5, 5), wxBORDER_NONE ) );
322     toolbar->AddControl( search_button );
323     search_button->SetDefault();
324     toolbar->Realize();
325
326     /* Create the tree */
327     treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
328                                wxDefaultPosition, wxDefaultSize,
329                                wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
330                                wxTR_NO_LINES |
331                                wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
332                                wxTR_MULTIPLE | wxTR_EXTENDED );
333
334     /* Create image list */
335
336     wxImageList *p_images = new wxImageList( 16 , 16, TRUE);
337
338     wxIcon icons[10];
339     icons[ ITEM_TYPE_UNKNOWN ] = wxIcon( type_unknown_xpm );
340     icons[ ITEM_TYPE_DISC ] = wxIcon( type_disc_xpm );
341     icons[ ITEM_TYPE_DIRECTORY ] = wxIcon( type_directory_xpm );
342     icons[ ITEM_TYPE_PLAYLIST ] = wxIcon( type_playlist_xpm );
343     icons[ ITEM_TYPE_NET ] = wxIcon( type_net_xpm );
344     icons[ ITEM_TYPE_CARD ] = wxIcon( type_card_xpm );
345
346     for( unsigned int i = 0; i< WXSIZEOF( icons ) ; i++ )
347     {
348          p_images->Add( wxIcon( icons[i] ));
349     }
350
351     treectrl->AssignImageList( p_images );
352
353     treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
354
355     /* Reduce font size */
356     wxFont font= treectrl->GetFont();
357     font.SetPointSize(8);
358     treectrl->SetFont( font );
359
360     /* Create the Up-Down buttons */
361 #if 0
362     wxButton *up_button =
363         new wxButton( playlist_panel, Up_Event, wxU(_("Up") ) );
364     wxButton *down_button =
365         new wxButton( playlist_panel, Down_Event, wxU(_("Down") ) );
366
367     wxBoxSizer *updown_sizer = new wxBoxSizer( wxHORIZONTAL );
368     updown_sizer->Layout();
369     /* The top and bottom sizers */
370     wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
371     bottom_sizer->Add( up_button, 0, wxALIGN_LEFT | wxRIGHT, 3);
372     bottom_sizer->Add( down_button, 0, wxALIGN_LEFT | wxLEFT, 3);
373     bottom_sizer->Layout();
374 #endif
375     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
376     panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
377 #if 0
378     panel_sizer->Add( bottom_sizer, 0, wxALL, 5);
379 #endif
380     panel_sizer->Layout();
381
382     playlist_panel->SetSizerAndFit( panel_sizer );
383
384 #if wxUSE_DRAG_AND_DROP
385     /* Associate drop targets with the playlist */
386     SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
387 #endif
388
389     playlist_t *p_playlist =
390         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
391                                        FIND_ANYWHERE );
392     if( p_playlist == NULL )
393     {
394         return;
395     }
396
397     /* We want to be noticed of playlist changes */
398
399     /* Some global changes happened -> Rebuild all */
400     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
401
402     /* We went to the next item */
403     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
404
405     /* One item has been updated */
406     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
407
408     vlc_object_release( p_playlist );
409
410     /* Update the playlist */
411     Rebuild();
412 }
413
414 void Playlist::OnSize( wxSizeEvent& event)
415 {
416 #if 0
417     wxSize size = GetClientSize();
418     if( listview )
419         listview->SetColumnWidth( 0, size.x - listview->GetColumnWidth(1)
420                         - 15 /* margins */ );
421 #endif
422     event.Skip();
423 }
424
425 Playlist::~Playlist()
426 {
427     playlist_t *p_playlist =
428         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
429                                        FIND_ANYWHERE );
430     if( p_playlist == NULL )
431     {
432         return;
433     }
434
435     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
436     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
437     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
438     vlc_object_release( p_playlist );
439 }
440
441 /**********************************************************************
442  * Update one playlist item
443  **********************************************************************/
444 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
445                            wxTreeItemId node )
446 {
447     long cookie;
448     wxTreeItemId child;
449     for( int i = 0; i< p_node->i_children ; i++ )
450     {
451         if( i == 0 )
452         {
453             child = treectrl->GetFirstChild( node, cookie);
454         }
455         else
456         {
457             child = treectrl->GetNextChild( node, cookie );
458         }
459
460         if( !child.IsOk() )
461         {
462             /* Not enough children */
463             CreateNode( p_playlist, p_node->pp_children[i], node );
464             /* Keep the tree pointer up to date */
465             child = treectrl->GetNextChild( node, cookie );
466         }
467         else
468         {
469         }
470     }
471
472 }
473 /* Creates the node p_node as last child of parent */
474 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
475                            wxTreeItemId parent )
476 {
477     long cookie;
478     wxTreeItemId node =
479         treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
480                               -1,-1, new PlaylistItem( p_node ) );
481     treectrl->SetItemImage( node, p_node->input.i_type );
482
483     for( int i = 0; i< p_node->i_children ; i++ )
484     {
485         /* Append the item */
486         if( p_node->pp_children[i]->i_children == -1 )
487         {
488             wxTreeItemId item =
489                 treectrl->AppendItem( node,
490                     wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
491                            new PlaylistItem( p_node->pp_children[i]) );
492
493             treectrl->SetItemImage( item,
494                                     p_node->pp_children[i]->input.i_type );
495         }
496         else
497         {
498             CreateNode( p_playlist, p_node->pp_children[i],
499                         node );
500         }
501     }
502 }
503
504 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
505 {
506     long cookie;
507     PlaylistItem *p_wxcurrent;
508     wxTreeItemId search;
509     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
510     wxTreeItemId child;
511
512     while( item.IsOk() )
513     {
514         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
515         if( p_wxcurrent->p_item == p_item )
516         {
517             return item;
518         }
519         if( treectrl->ItemHasChildren( item ) )
520         {
521             wxTreeItemId search = FindItem( item, p_item );
522             if( search.IsOk() )
523             {
524                 return search;
525             }
526         }
527         item = treectrl->GetNextChild( root, cookie);
528     }
529     /* Not found */
530     wxTreeItemId dummy;
531     return dummy;
532 }
533
534 /*wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current )
535 {
536     long cookie;
537     PlaylistItem *p_wxcurrent;
538     wxTreeItemId search;
539     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
540     wxTreeItemId child;
541
542     while( item.IsOk() )
543     {
544         if( treectrl->GetItemText( item).Lower().Contains(
545                                                  search_string.Lower() ) )
546         {
547             return item;
548         if( treectrl->ItemHasChildren( item ) )
549         {
550             wxTreeItemId search = FindItem( item, p_item );
551             if( search.IsOk() )
552             {
553                 return search;
554             }
555         }
556         item = treectrl->GetNextChild( root, cookie);
557     }
558   */  /* Not found */
559     /*wxTreeItemId dummy;
560     return dummy;
561 }
562 */
563
564
565
566 void Playlist::SetCurrentItem( wxTreeItemId item )
567 {
568     if( item.IsOk() )
569     {
570         treectrl->SetItemBold( item, true );
571         treectrl->EnsureVisible( item );
572     }
573 }
574
575 void Playlist::UpdateItem( int i )
576 {
577     if( i < 0 ) return; /* Sanity check */
578     playlist_item_t *p_item;
579
580     playlist_t *p_playlist =
581         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
582                                        FIND_ANYWHERE );
583
584     if( p_playlist == NULL )
585     {
586         return;
587     }
588
589     p_item = playlist_ItemGetById( p_playlist, i );
590
591     wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
592
593     UpdateTreeItem( p_playlist, item );
594
595     vlc_object_release(p_playlist);
596
597 }
598
599 void Playlist::UpdateTreeItem( playlist_t *p_playlist ,wxTreeItemId item )
600 {
601     playlist_item_t *p_item;
602
603    if( !item.IsOk() )
604    {
605         return;
606    }
607
608     p_item  =  ((PlaylistItem *)treectrl->GetItemData( item ))->p_item;
609
610     if( !p_item )
611     {
612         return;
613     }
614
615     wxString msg;
616     char *psz_author = playlist_ItemGetInfo( p_item, _("Meta-information"),
617                                                          _("Artist"));
618     char psz_duration[MSTRTIME_MAX_SIZE];
619     mtime_t dur = p_item->input.i_duration;
620     if( dur != -1 )
621         secstotimestr( psz_duration, dur/1000000 );
622     else
623         memcpy( psz_duration, "-:--:--", sizeof("-:--:--") );
624
625     if( !strcmp( psz_author, "" ) )
626     {
627         msg.Printf( wxString( wxL2U( p_item->input.psz_name ) ) + wxU( " ( ") +
628                     wxString(wxL2U(psz_duration ) ) + wxU( ")") );
629     }
630     else
631     {
632         msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
633                     wxString(wxL2U(p_item->input.psz_name)) + wxU( " ( ") +
634                     wxString(wxL2U(psz_duration ) ) + wxU( ")") );
635     }
636     treectrl->SetItemText( item , msg );
637
638     if( p_playlist->status.p_item == p_item )
639     {
640         SetCurrentItem( item );
641     }
642     else
643     {
644         treectrl->SetItemBold( item, false );
645     }
646 #if 0
647     if( p_item->b_enabled == VLC_FALSE )
648     {
649         wxListItem listitem;
650         listitem.m_itemId = i;
651         listitem.SetTextColour( *wxLIGHT_GREY);
652         listview->SetItem(listitem);
653     }
654 #endif
655 }
656
657 /**********************************************************************
658  * Rebuild the playlist
659  **********************************************************************/
660 void Playlist::Rebuild()
661 {
662     playlist_view_t *p_view;
663     playlist_t *p_playlist =
664         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
665                                        FIND_ANYWHERE );
666     if( p_playlist == NULL )
667     {
668         return;
669     }
670
671     /* ...and rebuild it */
672     vlc_mutex_lock( &p_playlist->object_lock );
673
674     p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
675
676     /* HACK we should really get new*/
677     msg_Dbg( p_intf, "rebuilding tree" );
678     treectrl->DeleteAllItems();
679     treectrl->AddRoot( wxU(_("root" )), -1, -1,
680                          new PlaylistItem( p_view->p_root) );
681
682     wxTreeItemId root = treectrl->GetRootItem();
683     UpdateNode( p_playlist, p_view->p_root, root );
684
685     wxTreeItemId item;
686     if( p_playlist->status.p_item != NULL )
687     {
688         item = FindItem( root, p_playlist->status.p_item );
689     }
690     else if( p_playlist->status.p_node != NULL )
691     {
692         item = FindItem( root, p_playlist->status.p_node );
693     }
694     else
695     {
696         item = root;
697     }
698
699     SetCurrentItem( item );
700
701     vlc_mutex_unlock( &p_playlist->object_lock );
702
703     vlc_object_release( p_playlist );
704 }
705
706 void Playlist::ShowPlaylist( bool show )
707 {
708     if( show ) Rebuild();
709     Show( show );
710 }
711
712 void Playlist::UpdatePlaylist()
713 {
714     i_update_counter++;
715
716     /* If the playlist isn't show there's no need to update it */
717     if( !IsShown() ) return;
718
719     if( this->b_need_update )
720     {
721         this->b_need_update = VLC_FALSE;
722         Rebuild();
723     }
724
725     /* Updating the playing status every 0.5s is enough */
726     if( i_update_counter % 5 ) return;
727
728     playlist_t *p_playlist =
729         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
730                                        FIND_ANYWHERE );
731     if( p_playlist == NULL )
732     {
733         return;
734     }
735 #if 0
736     /* Update the colour of items */
737     int i_playlist_index = p_playlist->i_index;
738     if( p_intf->p_sys->i_playing != i_playlist_index )
739     {
740         wxListItem listitem;
741         listitem.m_itemId = i_playlist_index;
742         listitem.SetTextColour( *wxRED );
743         listview->SetItem( listitem );
744
745         if( p_intf->p_sys->i_playing != -1 )
746         {
747             listitem.m_itemId = p_intf->p_sys->i_playing;
748             listitem.SetTextColour( *wxBLACK );
749             listview->SetItem( listitem );
750         }
751         p_intf->p_sys->i_playing = i_playlist_index;
752     }
753 #endif
754     vlc_object_release( p_playlist );
755 }
756
757 /*****************************************************************************
758  * Private methods.
759  *****************************************************************************/
760 void Playlist::DeleteItem( int item_id )
761 {
762     playlist_t *p_playlist =
763         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
764                                        FIND_ANYWHERE );
765     if( p_playlist == NULL )
766     {
767         return;
768     }
769
770     playlist_Delete( p_playlist, item_id );
771
772     vlc_object_release( p_playlist );
773 }
774
775 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
776 {
777     Hide();
778 }
779
780 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
781 {
782     struct {
783         char *psz_desc;
784         char *psz_filter;
785         char *psz_module;
786     } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
787                    { _("PLS file"), "*.pls", "export-pls" }};
788
789     wxString filter = wxT("");
790
791     playlist_t * p_playlist =
792                 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
793                                                FIND_ANYWHERE );
794
795     if( ! p_playlist )
796     {
797         return;
798     }
799     if( p_playlist->i_size == 0 )
800     {
801         wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
802                       wxICON_WARNING | wxOK, this );
803         vlc_object_release( p_playlist );
804         return;
805     }
806
807     for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
808     {
809         filter.Append( wxU(formats[i].psz_desc) );
810         filter.Append( wxT("|") );
811         filter.Append( wxU(formats[i].psz_filter) );
812         filter.Append( wxT("|") );
813     }
814     wxFileDialog dialog( this, wxU(_("Save playlist")),
815                          wxT(""), wxT(""), filter, wxSAVE );
816
817     if( dialog.ShowModal() == wxID_OK )
818     {
819         if( dialog.GetPath().mb_str() )
820         {
821             playlist_Export( p_playlist, dialog.GetPath().mb_str(),
822                              formats[dialog.GetFilterIndex()].psz_module );
823         }
824     }
825
826     vlc_object_release( p_playlist );
827
828 }
829
830 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
831 {
832     playlist_t *p_playlist =
833         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
834                                        FIND_ANYWHERE );
835     if( p_playlist == NULL )
836     {
837         return;
838     }
839
840     wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
841         wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
842
843     if( dialog.ShowModal() == wxID_OK )
844     {
845         playlist_Import( p_playlist, dialog.GetPath().mb_str() );
846     }
847
848     vlc_object_release( p_playlist );
849 }
850
851 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
852 {
853     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
854
855 }
856
857 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
858 {
859     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
860
861 }
862
863 /********************************************************************
864  * Move functions
865  ********************************************************************/
866 void Playlist::OnUp( wxCommandEvent& event )
867 {
868     playlist_t *p_playlist =
869         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
870                                        FIND_ANYWHERE );
871     if( p_playlist == NULL )
872     {
873         return;
874     }
875 #if 0
876     /* We use the first selected item, so find it */
877     long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
878                                          wxLIST_STATE_SELECTED);
879     if( i_item > 0 && i_item < p_playlist->i_size )
880     {
881         playlist_Move( p_playlist, i_item, i_item - 1 );
882         listview->Focus( i_item - 1 );
883     }
884 #endif
885     vlc_object_release( p_playlist );
886 }
887
888 void Playlist::OnDown( wxCommandEvent& event )
889 {
890     playlist_t *p_playlist =
891         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
892                                        FIND_ANYWHERE );
893     if( p_playlist == NULL )
894     {
895         return;
896     }
897 #if 0
898     /* We use the first selected item, so find it */
899     long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
900                                          wxLIST_STATE_SELECTED );
901     if( i_item >= 0 && i_item < p_playlist->i_size - 1 )
902     {
903         playlist_Move( p_playlist, i_item, i_item + 2 );
904         listview->Focus( i_item + 1 );
905     }
906 #endif
907     vlc_object_release( p_playlist );
908 }
909
910 /********************************************************************
911  * Sorting functions
912  ********************************************************************/
913 void Playlist::OnSort( wxCommandEvent& event )
914 {
915     playlist_t *p_playlist =
916         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
917                                        FIND_ANYWHERE );
918     if( p_playlist == NULL )
919     {
920         return;
921     }
922     switch( event.GetId() )
923     {
924         case SortTitle_Event:
925            playlist_SortTitle( p_playlist, ORDER_NORMAL );
926            break;
927         case RSortTitle_Event:
928            playlist_SortTitle( p_playlist, ORDER_REVERSE );
929            break;
930         case SortAuthor_Event:
931            playlist_SortAuthor(p_playlist, ORDER_NORMAL );
932            break;
933         case RSortAuthor_Event:
934            playlist_SortAuthor( p_playlist, ORDER_REVERSE );
935            break;
936         case Randomize_Event:
937            playlist_Sort( p_playlist, SORT_RANDOM, ORDER_NORMAL );
938            break;
939     }
940     vlc_object_release( p_playlist );
941
942     Rebuild();
943 }
944
945 /**********************************************************************
946  * Search functions
947  **********************************************************************/
948 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
949 {
950    search_button->SetDefault();
951 }
952
953 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
954 {
955     wxString search_string = search_text->GetValue();
956
957     bool b_ok = false;
958     int i_current;
959     int i_first = 0 ;
960     int i_item = -1;
961 }
962
963 #if 0
964     for( i_current = 0; i_current < listview->GetItemCount(); i_current++ )
965     {
966         if( listview->GetItemState( i_current, wxLIST_STATE_SELECTED ) ==
967               wxLIST_STATE_SELECTED )
968         {
969             i_first = i_current;
970             break;
971         }
972     }
973
974     if( i_first == listview->GetItemCount() )
975     {
976         i_first = -1;
977     }
978
979     for( i_current = i_first + 1; i_current < listview->GetItemCount();
980          i_current++ )
981     {
982         wxListItem listitem;
983         listitem.SetId( i_current );
984         listview->GetItem( listitem );
985         if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
986         {
987             i_item = i_current;
988             b_ok = true;
989             break;
990         }
991         listitem.SetColumn( 1 );
992         listview->GetItem( listitem );
993         if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
994         {
995             i_item = i_current;
996             b_ok = true;
997             break;
998         }
999     }
1000     if( !b_ok )
1001     {
1002         for( i_current = -1 ; i_current < i_first - 1;
1003              i_current++ )
1004         {
1005             wxListItem listitem;
1006             listitem.SetId( i_current );
1007             listview->GetItem( listitem );
1008             if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1009             {
1010                 i_item = i_current;
1011                 b_ok = true;
1012                 break;
1013             }
1014             listitem.SetColumn( 1 );
1015             listview->GetItem( listitem );
1016             if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
1017             {
1018                 i_item = i_current;
1019                 b_ok = true;
1020                 break;
1021             }
1022         }
1023     }
1024
1025     if( i_item < 0 || i_item >= listview->GetItemCount() )
1026     {
1027         return;
1028     }
1029
1030     for( long item = 0; item < listview->GetItemCount(); item++ )
1031     {
1032         listview->Select( item, FALSE );
1033     }
1034
1035     wxListItem listitem;
1036     listitem.SetId(i_item);
1037     listitem.m_state = wxLIST_STATE_SELECTED;
1038     listview->Select( i_item, TRUE );
1039     listview->Focus( i_item );
1040 }
1041 #endif
1042
1043 /**********************************************************************
1044  * Selection functions
1045  **********************************************************************/
1046 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1047 {
1048 }
1049
1050 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1051 {
1052     Rebuild();
1053 }
1054
1055 void Playlist::OnEnableSelection( wxCommandEvent& WXUNUSED(event) )
1056 {
1057     playlist_t *p_playlist =
1058         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1059                                        FIND_ANYWHERE );
1060     if( p_playlist == NULL )
1061     {
1062         return;
1063     }
1064 #if 0
1065     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
1066     {
1067         if( listview->IsSelected( item ) )
1068         {
1069             /*XXX*/
1070             playlist_Enable( p_playlist, item );
1071             UpdateItem( item );
1072         }
1073     }
1074 #endif
1075     vlc_object_release( p_playlist);
1076 }
1077
1078 void Playlist::OnDisableSelection( wxCommandEvent& WXUNUSED(event) )
1079 {
1080     playlist_t *p_playlist =
1081         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1082                                        FIND_ANYWHERE );
1083     if( p_playlist == NULL )
1084     {
1085         return;
1086     }
1087 #if 0
1088     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
1089     {
1090         if( listview->IsSelected( item ) )
1091         {
1092             /*XXX*/
1093             playlist_Disable( p_playlist, item );
1094             UpdateItem( item );
1095         }
1096     }
1097 #endif
1098     vlc_object_release( p_playlist);
1099 }
1100
1101 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1102 {
1103 }
1104
1105 /**********************************************************************
1106  * Playlist mode functions
1107  **********************************************************************/
1108 void Playlist::OnRandom( wxCommandEvent& event )
1109 {
1110     vlc_value_t val;
1111     val.b_bool = event.IsChecked();
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     var_Set( p_playlist, "random", val);
1120     vlc_object_release( p_playlist );
1121 }
1122
1123 void Playlist::OnLoop( wxCommandEvent& event )
1124 {
1125     vlc_value_t val;
1126     val.b_bool = event.IsChecked();
1127     playlist_t *p_playlist =
1128         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1129                                        FIND_ANYWHERE );
1130     if( p_playlist == NULL )
1131     {
1132         return;
1133     }
1134     var_Set( p_playlist, "loop", val);
1135     vlc_object_release( p_playlist );
1136 }
1137
1138 void Playlist::OnRepeat( wxCommandEvent& event )
1139 {
1140     vlc_value_t val;
1141     val.b_bool = event.IsChecked();
1142     playlist_t *p_playlist =
1143         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1144                                        FIND_ANYWHERE );
1145     if( p_playlist == NULL )
1146     {
1147         return;
1148     }
1149     var_Set( p_playlist, "repeat", val);
1150     vlc_object_release( p_playlist );
1151 }
1152
1153 void Playlist::OnActivateItem( wxTreeEvent& event )
1154 {
1155     playlist_item_t *p_item,*p_node;
1156     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1157                                     VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1158     PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1159                                                         event.GetItem() );
1160     wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1161
1162     PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1163
1164     if( p_playlist == NULL )
1165     {
1166         return;
1167     }
1168
1169     if( p_wxitem->p_item->i_children == -1 )
1170     {
1171         p_node = p_wxparent->p_item;
1172         p_item = p_wxitem->p_item;
1173     }
1174     else
1175     {
1176         p_node = p_wxitem->p_item;
1177         if( p_wxitem->p_item->i_children > 0 )
1178         {
1179             p_item = p_wxitem->p_item->pp_children[0];
1180         }
1181         else
1182         {
1183             p_item = NULL;
1184         }
1185     }
1186
1187     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1188                       p_node, p_item );
1189
1190     vlc_object_release( p_playlist );
1191 }
1192
1193 void Playlist::OnKeyDown( wxTreeEvent& event )
1194 {
1195     long keycode = event.GetKeyCode();
1196     /* Delete selected items */
1197     if( keycode == WXK_BACK || keycode == WXK_DELETE )
1198     {
1199         /* We send a dummy event */
1200         OnDeleteSelection( event );
1201     }
1202 }
1203
1204 void Playlist::ShowInfos( int i_item )
1205 {
1206 }
1207
1208 void Playlist::OnInfos( wxCommandEvent& WXUNUSED(event) )
1209 {
1210 }
1211
1212 void Playlist::OnEnDis( wxCommandEvent& event )
1213 {
1214     playlist_t *p_playlist =
1215         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1216                                        FIND_ANYWHERE );
1217     if( p_playlist == NULL )
1218     {
1219         return;
1220     }
1221     vlc_object_release( p_playlist );
1222 }
1223
1224 /**********************************************************************
1225  * Menu
1226  **********************************************************************/
1227
1228 void Playlist::OnMenuOpen( wxMenuEvent& event)
1229 {
1230 #if defined( __WXMSW__ )
1231 #   define GetEventObject GetMenu
1232 #endif
1233
1234     if( event.GetEventObject() == p_view_menu )
1235     {
1236         p_view_menu = ViewMenu();
1237     }
1238 #if defined( __WXMSW__ )
1239 #   undef GetEventObject
1240 #endif
1241 }
1242
1243 void Playlist::OnMenuEvent( wxCommandEvent& event )
1244 {
1245     playlist_t *p_playlist =
1246         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1247                                        FIND_ANYWHERE );
1248     if( p_playlist == NULL )
1249     {
1250         return;
1251     }
1252
1253     if( event.GetId() < FirstView_Event )
1254     {
1255         event.Skip();
1256         vlc_object_release( p_playlist );
1257         return;
1258     }
1259     else if( event.GetId() < LastView_Event )
1260     {
1261
1262         int i_new_view = event.GetId() - FirstView_Event;
1263
1264         playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1265
1266         if( p_view != NULL )
1267         {
1268             i_current_view = i_new_view;
1269             playlist_ViewUpdate( p_playlist, i_new_view );
1270             Rebuild();
1271             vlc_object_release( p_playlist );
1272             return;
1273         }
1274         else if( i_new_view >= VIEW_FIRST_SORTED &&
1275                  i_new_view <= VIEW_LAST_SORTED )
1276         {
1277             playlist_ViewInsert( p_playlist, i_new_view, "View" );
1278             playlist_ViewUpdate( p_playlist, i_new_view );
1279
1280             i_current_view = i_new_view;
1281
1282             Rebuild();
1283         }
1284     }
1285     else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1286     {
1287         if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1288                                 pp_sds[event.GetId() - FirstSD_Event] ) )
1289         {
1290             playlist_ServicesDiscoveryAdd( p_playlist,
1291                             pp_sds[event.GetId() - FirstSD_Event] );
1292         }
1293         else
1294         {
1295             playlist_ServicesDiscoveryRemove( p_playlist,
1296                             pp_sds[event.GetId() - FirstSD_Event] );
1297         }
1298     }
1299     vlc_object_release( p_playlist );
1300 }
1301
1302 wxMenu * Playlist::ViewMenu()
1303 {
1304     playlist_t *p_playlist =
1305         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1306                                        FIND_ANYWHERE );
1307     if( p_playlist == NULL )
1308     {
1309         return NULL;
1310     }
1311
1312     if( !p_view_menu )
1313     {
1314         p_view_menu = new wxMenu;
1315     }
1316     else
1317     {
1318         wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1319         for( ; node; )
1320         {
1321             wxMenuItem *item = node->GetData();
1322             node = node->GetNext();
1323             p_view_menu->Delete( item );
1324         }
1325     }
1326
1327     /* FIXME : have a list of "should have" views */
1328     p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1329                            wxU(_("By category") ) );
1330     p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1331                            wxU(_("Manually added") ) );
1332     p_view_menu->Append( FirstView_Event + VIEW_ALL,
1333                            wxU(_("All items, unsorted") ) );
1334     p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1335                            wxU(_("Sorted by author") ) );
1336
1337     vlc_object_release( p_playlist);
1338
1339     return p_view_menu;
1340 }
1341
1342 wxMenu *Playlist::SDMenu()
1343 {
1344
1345     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1346                               VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1347     if( !p_playlist )
1348     {
1349         return NULL;
1350     }
1351     vlc_value_t val, val_list, text_list;
1352     p_sd_menu = new wxMenu;
1353
1354     vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1355                                         FIND_ANYWHERE );
1356
1357     int i_number = 0;
1358     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1359     {
1360         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1361
1362         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1363         {
1364             p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1365                        wxU( p_parser->psz_longname ? p_parser->psz_longname :
1366                             p_parser->psz_shortname ) );
1367
1368             if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1369                                     p_parser->psz_shortname ) )
1370             {
1371                 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1372             }
1373
1374             INSERT_ELEM( (void**)pp_sds, i_number, i_number,
1375                          (void*)p_parser->psz_shortname );
1376         }
1377     }
1378     vlc_list_release( p_list );
1379     vlc_object_release( p_playlist );
1380     return p_sd_menu;
1381 }
1382
1383
1384 /*****************************************************************************
1385  * Popup management functions
1386  *****************************************************************************/
1387 void Playlist::OnPopup( wxContextMenuEvent& event )
1388 {
1389     wxPoint pt = event.GetPosition();
1390
1391     i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1392     if( i_popup_item.IsOk() )
1393     {
1394         PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1395                                                             i_popup_item );
1396         PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1397                                    treectrl->GetItemParent( i_popup_item ) );
1398         p_popup_item = p_wxitem->p_item;
1399         p_popup_parent = p_wxparent->p_item;
1400         treectrl->SelectItem( i_popup_item );
1401         Playlist::PopupMenu( popup_menu,
1402                              ScreenToClient( wxGetMousePosition() ) );
1403     }
1404     else
1405     {
1406     }
1407 }
1408
1409 void Playlist::OnPopupPlay( wxMenuEvent& event )
1410 {
1411     playlist_t *p_playlist =
1412         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1413                                        FIND_ANYWHERE );
1414     if( p_playlist == NULL )
1415     {
1416         return;
1417     }
1418     if( p_popup_item != NULL )
1419     {
1420         if( p_popup_item->i_children > -1 )
1421         {
1422             if( event.GetId() == PopupPlay_Event &&
1423                 p_popup_item->i_children > 0 )
1424             {
1425                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1426                                   i_current_view, p_popup_item,
1427                                   p_popup_item->pp_children[0] );
1428             }
1429             else
1430             {
1431                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1432                                   i_current_view, p_popup_item, NULL );
1433             }
1434         }
1435         else
1436         {
1437             if( event.GetId() == PopupPlay_Event )
1438             {
1439                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1440                                   i_current_view, p_popup_parent,
1441                                   p_popup_item );
1442             }
1443         }
1444     }
1445     vlc_object_release( p_playlist );
1446 }
1447
1448 void Playlist::OnPopupDel( wxMenuEvent& event )
1449 {
1450     PlaylistItem *p_wxitem;
1451
1452     p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1453
1454     if( p_wxitem->p_item->i_children == -1 )
1455     {
1456         DeleteItem( p_wxitem->p_item->input.i_id );
1457     }
1458     else
1459     {
1460         //DeleteNode( p_wxitem->p_item );
1461     }
1462 }
1463
1464 void Playlist::OnPopupEna( wxMenuEvent& event )
1465 {
1466     playlist_t *p_playlist =
1467         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1468                                        FIND_ANYWHERE );
1469     if( p_playlist == NULL )
1470     {
1471         return;
1472     }
1473
1474     p_popup_item->b_enabled = VLC_TRUE - p_popup_item->b_enabled;
1475
1476     vlc_object_release( p_playlist);
1477     UpdateItem( i_popup_item );
1478 }
1479
1480 void Playlist::OnPopupInfo( wxMenuEvent& event )
1481 {
1482     if( p_popup_item )
1483     {
1484         iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1485         if( iteminfo_dialog->ShowModal() == wxID_OK )
1486         {
1487             UpdateItem( i_popup_item );
1488         }
1489         delete iteminfo_dialog;
1490     }
1491 }
1492
1493
1494 /*****************************************************************************
1495  * Custom events management
1496  *****************************************************************************/
1497 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1498 {
1499     switch( event.GetId() )
1500     {
1501     case UpdateItem_Event:
1502         UpdateItem( event.GetInt() );
1503         break;
1504     }
1505 }
1506
1507 /*****************************************************************************
1508  * PlaylistChanged: callback triggered by the intf-change playlist variable
1509  *  We don't rebuild the playlist directly here because we don't want the
1510  *  caller to block for a too long time.
1511  *****************************************************************************/
1512 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1513                             vlc_value_t oval, vlc_value_t nval, void *param )
1514 {
1515     Playlist *p_playlist_dialog = (Playlist *)param;
1516     p_playlist_dialog->b_need_update = VLC_TRUE;
1517     return VLC_SUCCESS;
1518 }
1519
1520 /*****************************************************************************
1521  * Next: callback triggered by the playlist-current playlist variable
1522  *****************************************************************************/
1523 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1524                          vlc_value_t oval, vlc_value_t nval, void *param )
1525 {
1526     Playlist *p_playlist_dialog = (Playlist *)param;
1527
1528     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1529     event.SetInt( oval.i_int );
1530     p_playlist_dialog->AddPendingEvent( event );
1531     event.SetInt( nval.i_int );
1532     p_playlist_dialog->AddPendingEvent( event );
1533
1534     return 0;
1535 }
1536
1537 /*****************************************************************************
1538  * ItemChanged: callback triggered by the item-change playlist variable
1539  *****************************************************************************/
1540 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1541                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1542 {
1543     Playlist *p_playlist_dialog = (Playlist *)param;
1544
1545     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1546     event.SetInt( new_val.i_int );
1547     p_playlist_dialog->AddPendingEvent( event );
1548
1549     return 0;
1550 }